// Package query 定义与模型 API 交互的核心数据类型. // Message, Content, Usage, StreamEvent 等类型被 engine, provider, permission 等包广泛引用. // 实际的会话管理和 API 调用由 pkg/engine 负责. package query import ( "time" ) // Message 是对话消息. // 对应原项目中 Message 联合类型的简化版. type Message struct { Role Role `json:"role"` Content []Content `json:"content"` Time time.Time `json:"time,omitempty"` // 升华改进(ELEVATED): 可扩展元数据,不污染核心字段. // 用途:is_attachment(标记附件),attachment_type(附件类型), // is_virtual(虚拟消息,不发 API),error_source(错误来源映射). // 替代方案:为每个场景加专用字段(僵硬,每加一个都要改结构体). Metadata map[string]any `json:"metadata,omitempty"` } // Role 是消息角色. type Role string const ( RoleUser Role = "user" RoleAssistant Role = "assistant" RoleSystem Role = "system" ) // Content 是消息内容块. // 对应原项目中 ContentBlock 联合类型. type Content struct { Type ContentType `json:"type"` Text string `json:"text,omitempty"` ID string `json:"id,omitempty"` // tool_use ID Name string `json:"name,omitempty"` // tool name Input map[string]any `json:"input,omitempty"` // tool input ToolUseID string `json:"tool_use_id,omitempty"` // tool_result 对应的 tool_use ID IsError bool `json:"is_error,omitempty"` Source *ContentSource `json:"source,omitempty"` // image/document 数据源 SizeBytes int64 `json:"size_bytes,omitempty"` // 原始数据大小(字节) // ResultBlocks carries array-form tool_result content when the result // includes non-text payloads (image / document). Only meaningful when // Type == ContentToolResult. Nil for plain-text tool_result (engine // writes Text). Consumed by engine.conversion (→ flyto.Block.ResultBlocks) // and Anthropic provider (→ content:[{...}] array wire form). // // ResultBlocks 以数组形式承载含非文本载荷 (图片 / 文档) 的 tool_result // 内容. 仅 Type == ContentToolResult 时有意义. 纯文本 tool_result 时为 // nil (引擎写 Text). 由 engine.conversion (→ flyto.Block.ResultBlocks) // 和 Anthropic provider (→ content:[{...}] array wire) 消费. ResultBlocks []Content `json:"result_blocks,omitempty"` } // ContentSource 是图片/文档的数据源. type ContentSource struct { Type string `json:"type"` // "base64" / "url" MediaType string `json:"media_type"` // "image/png", "application/pdf" 等 Data string `json:"data,omitempty"` URL string `json:"url,omitempty"` } // ContentType 是内容块类型. type ContentType string const ( ContentText ContentType = "text" ContentThinking ContentType = "thinking" ContentToolUse ContentType = "tool_use" ContentToolResult ContentType = "tool_result" ContentImage ContentType = "image" ContentDocument ContentType = "document" ) // CacheTokens 缓存 token 统计(Provider 无关). // 升华改进(ELEVATED): 替代 CacheReadInputTokens/CacheCreationInputTokens Anthropic 字段名. // Anthropic 叫 cache_read_input_tokens / cache_creation_input_tokens, // OpenAI 叫 prompt_cache_hit_details,Gemini 叫 cachedContentTokenCount. // 统一为 Read/Written,Provider 适配层负责字段映射. // 替代方案:保留 Anthropic 字段名(绑死单一 Provider,换 Provider 时全量改名). type CacheTokens struct { Read int `json:"cache_read"` // 从缓存读取的 token 数 Written int `json:"cache_written"` // 写入缓存的 token 数 } // Usage 是 API 使用统计. type Usage struct { InputTokens int `json:"input_tokens"` OutputTokens int `json:"output_tokens"` Cache CacheTokens `json:"cache"` CostUSD float64 `json:"cost_usd"` } // StreamEvent 是 API 流式响应事件. // 对应 SDK 的 StreamEvent 类型. type StreamEvent struct { Type StreamEventType Delta string // text / thinking delta Block *Content // 完整块(content_block_stop 时) Usage *Usage // 使用统计(message_delta / message_stop 时) } // StreamEventType 是流式事件类型. type StreamEventType string const ( EventMessageStart StreamEventType = "message_start" EventContentBlockStart StreamEventType = "content_block_start" EventContentDelta StreamEventType = "content_block_delta" EventContentStop StreamEventType = "content_block_stop" EventMessageDelta StreamEventType = "message_delta" EventMessageStop StreamEventType = "message_stop" ) // NewTextMsg 创建纯文本消息的辅助函数. func NewTextMsg(role Role, text string) Message { return Message{ Role: role, Content: []Content{{Type: ContentText, Text: text}}, } }