Files
xk-ai-agent/internal/types/types.go
2026-08-14 21:50:48 +08:00

87 lines
4.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package types
import (
"context"
)
// ========================================================================
// 公共类型定义types 包)
// ========================================================================
// 为什么需要独立的 types 包?
// 原结构中 llm 包用 agent.Message/agent.Toolagent 包又用 llm.ModelRouter
// 形成 llm <-> agent 的循环依赖Go 不允许)。
//
// 解决方案:
// 把 LLM 调用所需的"消息/工具/工具调用"等纯数据类型抽到 types 包,
// agent 和 llm 都依赖 types单向依赖agent → types ← llmagent → llm。
//
// 迁移范围:
// - Message原 agent.Message
// - Tool 接口(原 agent.Tool
// - ToolCallInfo原 agent.ToolCallInfo
//
// 兼容策略:
// agent 包用 type 别名保留旧 APIagent.Message = types.Message
// 老调用方无需改 importllm 包改用 types.Xxx。
// ========================================================================
// Message 单条对话消息system / user / assistant / tool
type Message struct {
Role string `json:"role"` // 角色system/user/assistant/tool
Content string `json:"content"` // 消息正文
Timestamp int64 `json:"timestamp"` // 时间戳
ToolCall *ToolCallInfo `json:"tool_call,omitempty"` // 工具调用信息assistant 触发工具时)
// ToolCallID tool 结果帧回传时对应的 tool_calls[].id
//
// OpenAI Function Calling 协议要求:
// assistant 帧的 tool_calls[].id 必须与后续 tool 帧的 tool_call_id 一一对应,
// 否则部分厂商(讯飞 10003 / OpenAI 400会直接拒绝请求。
// 只在 Role=="tool" 时有意义。
ToolCallID string `json:"tool_call_id,omitempty"`
}
// Tool Agent 可调用的工具接口
//
// 实现此接口的对象注册到 Runner 后LLM 可通过 Function Calling 自主决定调用
type Tool interface {
Name() string
Description() string
Execute(ctx context.Context, params map[string]any) (string, error)
}
// ToolCallInfo 一次工具调用的完整信息
type ToolCallInfo struct {
// ID 厂商返回的 tool_calls[].id回放对话历史时必须原样带回
// 供 tool 结果帧的 tool_call_id 对应;厂商未返回时由客户端生成)
ID string `json:"id,omitempty"`
ToolName string `json:"tool_name"` // 工具名
Params map[string]any `json:"params"` // 调用参数
Result string `json:"result"` // 调用结果
Error string `json:"error,omitempty"` // 错误信息(失败时)
}
// ========================================================================
// 统一 chat 结果(供 LLM 客户端与上层共享)
// ========================================================================
// 含 token 用量与计时,便于 Agent 上层把每一步落到 xk_ai_generation_step
// ========================================================================
// ChatResult LLM 调用统一返回结构
//
// 各 providerDeepSeek/Spark/Qwen 等)调用 LLM 后把内容、token 用量、耗时
// 等打包到本结构,方便 KnowledgeEnhancer 把每一步记入 step 子表
type ChatResult struct {
Content string `json:"content"` // 助手文本choices[0].message.content
Provider string `json:"provider"` // 供应商标识
Model string `json:"model"` // 实际使用的模型名
APIKeyID int `json:"api_key_id"` // 使用的密钥 IDenv 回落为 0
PromptTokens int `json:"prompt_tokens"` // 输入 token 数
CompletionTokens int `json:"completion_tokens"` // 输出 token 数
TotalTokens int `json:"total_tokens"` // 总 token 数
Usage map[string]any `json:"usage"` // 完整 usage 原始对象
DurationMs int `json:"duration_ms"` // 耗时毫秒
FinishReason string `json:"finish_reason"` // 终止原因stop/length/content_filter/tool_callslength=被 max_tokens 截断
ToolCall *ToolCallInfo `json:"tool_call,omitempty"` // 模型触发的工具调用Function Calling
Raw any `json:"-"` // 供应商原始响应(不入 JSON
}