Documentation
¶
Index ¶
- func EmitToolUpdate(ctx context.Context, content string)
- func GetInterruptState[T any](ctx context.Context) (wasInterrupted bool, hasState bool, state T)
- func GetResumeContext[T any](ctx context.Context) (isResumeTarget bool, hasData bool, data T)
- func Interrupt(ctx context.Context, info any) error
- func StatefulInterrupt(ctx context.Context, info any, state any) error
- type Agent
- func (a *Agent) Abort()
- func (a *Agent) ClearAllQueues()
- func (a *Agent) ClearFollowUpQueue()
- func (a *Agent) ClearSteeringQueue()
- func (a *Agent) Close() error
- func (a *Agent) Continue(ctx context.Context) error
- func (a *Agent) FollowUp(content string)
- func (a *Agent) History() []*schema.Message
- func (a *Agent) Name() string
- func (a *Agent) Prompt(ctx context.Context, input string) error
- func (a *Agent) Reset()
- func (a *Agent) Resume(ctx context.Context, targets map[string]any) error
- func (a *Agent) Send(ctx context.Context, parts ...ContentPart) error
- func (a *Agent) SetFollowUpMode(mode QueueMode)
- func (a *Agent) SetSteeringMode(mode QueueMode)
- func (a *Agent) State() *State
- func (a *Agent) Steer(content string)
- func (a *Agent) Subscribe(fn Subscriber) func()
- type ChatModel
- type Config
- type ContentPart
- func AudioBase64(data, mimeType string) ContentPart
- func AudioURL(url string) ContentPart
- func FileBase64(data, mimeType string, name ...string) ContentPart
- func FileURL(url string) ContentPart
- func ImageBase64(data, mimeType string, detail ...ImageURLDetail) ContentPart
- func ImageURL(url string, detail ...ImageURLDetail) ContentPart
- func Text(s string) ContentPart
- func VideoBase64(data, mimeType string) ContentPart
- func VideoURL(url string) ContentPart
- type Event
- type EventType
- type ImageURLDetail
- type InterruptPoint
- type Message
- type QueueMode
- type ResponseMeta
- type RoleType
- type State
- type Subscriber
- type TokenUsage
- type Tool
- type ToolCall
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EmitToolUpdate ¶
EmitToolUpdate 在工具执行中发送进度更新事件。 工具通过 context 获取 Emitter 并发送 tool_update 事件:
func myTool(ctx context.Context, input string) (string, error) {
agentkit.EmitToolUpdate(ctx, "正在处理...")
return "done", nil
}
func GetInterruptState ¶
GetInterruptState 在工具中检查是否从中断恢复,并获取之前保存的状态。
返回值:
- wasInterrupted: 此工具是否曾被中断
- hasState: 是否有保存的状态且成功转换为类型 T
- state: 保存的状态(hasState 为 false 时为零值)
func GetResumeContext ¶
GetResumeContext 在工具中检查是否是 Resume 的显式目标,并获取恢复数据。
返回值:
- isResumeTarget: 此工具是否被显式指定为恢复目标
- hasData: 是否提供了恢复数据
- data: 恢复数据(hasData 为 false 时为零值)
用于区分:
- 作为恢复目标被调用(应继续执行)
- 因兄弟工具被恢复而重新执行(应再次中断)
用法:
func myTool(ctx context.Context, input string) (string, error) {
wasInterrupted, _, _ := agentkit.GetInterruptState[any](ctx)
if !wasInterrupted {
return "", agentkit.Interrupt(ctx, "需要用户输入")
}
isTarget, hasData, data := agentkit.GetResumeContext[string](ctx)
if !isTarget {
return "", agentkit.Interrupt(ctx, nil) // 非目标,重新中断
}
if hasData {
return "用户输入: " + data, nil
}
return "已确认", nil
}
func Interrupt ¶
Interrupt 在工具执行中触发 HITL 中断。 调用后工具应立即返回,Agent 将暂停执行并发出 EventInterrupted 事件。 用户可通过 Agent.Resume 恢复执行。
info 是面向用户的中断原因描述,会通过 InterruptPoint.Info 传递给订阅者。
用法:
func myTool(ctx context.Context, input string) (string, error) {
if needsConfirmation(input) {
return "", agentkit.Interrupt(ctx, "请确认是否继续")
}
return doWork(input), nil
}
func StatefulInterrupt ¶
StatefulInterrupt 在工具执行中触发带状态保存的 HITL 中断。 恢复时可通过 GetInterruptState 取回保存的状态。
state 必须是可通过 gob 序列化的类型。
用法:
type MyState struct { Step int }
func myTool(ctx context.Context, input string) (string, error) {
wasInterrupted, hasState, state := agentkit.GetInterruptState[MyState](ctx)
if !wasInterrupted {
return "", agentkit.StatefulInterrupt(ctx, "处理中", MyState{Step: 1})
}
if hasState {
return fmt.Sprintf("从步骤 %d 恢复", state.Step), nil
}
return "已恢复", nil
}
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent 独立的轻量级 Agent,封装 eino ADK,提供事件流驱动的交互
func (*Agent) Resume ¶
Resume 从 HITL 中断恢复执行。 targets 格式为 map[interruptID]data,interruptID 来自 Event.Interrupt[].ID。 如果 Agent 已在执行中,返回错误。
func (*Agent) Send ¶ added in v1.1.0
func (a *Agent) Send(ctx context.Context, parts ...ContentPart) error
Send 发送多模态内容并驱动 Agent 执行。 使用 Text、ImageURL、AudioURL 等构造函数创建 ContentPart。 如果 Agent 已在执行中,返回错误。
func (*Agent) SetFollowUpMode ¶
SetFollowUpMode 设置后续消息处理模式
func (*Agent) SetSteeringMode ¶
SetSteeringMode 设置转向消息处理模式
type Config ¶
type Config struct {
Name string
Description string
SystemPrompt string
Model ChatModel // 聊天模型(可直接使用 agentkit.ChatModel 别名)
Tools []Tool // 工具列表(可直接使用 agentkit.Tool 别名)
Middlewares []adk.AgentMiddleware // Agent 级中间件:结构体式钩子(BeforeChatModel/AfterChatModel)和工具调用包装 WrapToolCall(高级)
ModelMiddlewares []adk.ChatModelAgentMiddleware // 模型级处理器:接口式扩展(WrapModel/WrapToolCall)和状态重写(BeforeModelRewriteState/AfterModelRewriteState),在 Middlewares 之后执行(高级)
MaxIterations int // 默认 20
CheckPointStore compose.CheckPointStore // 自定义 CheckPoint 存储,默认使用内存存储
}
Config Agent 配置
type ContentPart ¶ added in v1.1.0
type ContentPart = schema.MessageInputPart
ContentPart 表示用户输入的一个内容片段(文本、图片、音频、视频、文件)。 通过 Text、ImageURL 等构造函数创建,用于 Agent.Send 多模态输入。
func AudioBase64 ¶ added in v1.1.0
func AudioBase64(data, mimeType string) ContentPart
AudioBase64 创建 Base64 编码音频内容片段。
func FileBase64 ¶ added in v1.1.0
func FileBase64(data, mimeType string, name ...string) ContentPart
FileBase64 创建 Base64 编码文件内容片段。name 为文件名(可选)。
func ImageBase64 ¶ added in v1.1.0
func ImageBase64(data, mimeType string, detail ...ImageURLDetail) ContentPart
ImageBase64 创建 Base64 编码图片内容片段。
func ImageURL ¶ added in v1.1.0
func ImageURL(url string, detail ...ImageURLDetail) ContentPart
ImageURL 创建图片 URL 内容片段。 detail 可选,控制图片识别质量(默认由模型决定)。
func VideoBase64 ¶ added in v1.1.0
func VideoBase64(data, mimeType string) ContentPart
VideoBase64 创建 Base64 编码视频内容片段。
type Event ¶
type Event struct {
Type EventType
Agent string // 产生事件的 Agent 名称
Content string // 文本内容(message_end / tool_end)
Delta string // 流式增量内容(message_delta / reasoning_delta)
ReasoningContent string // 完整推理内容(message_end,仅推理模型)
ResponseMeta *ResponseMeta // 响应元数据:token 用量、完成原因(message_end)
ToolCalls []ToolCall // 工具调用列表(tool_start)
Interrupt []InterruptPoint // 中断点列表(interrupted)
Error error // 错误信息(error)
}
Event 统一事件
type EventType ¶
type EventType string
EventType 事件类型
const ( EventAgentStart EventType = "agent_start" // Agent 开始处理 EventTurnStart EventType = "turn_start" // 新一轮开始(一次 LLM 调用 + 工具执行) EventMessageStart EventType = "message_start" // 消息开始(流式或非流式) EventReasoningDelta EventType = "reasoning_delta" // 推理模型思考过程增量(如 DeepSeek-R1、o1) EventMessageDelta EventType = "message_delta" // 流式增量文本 EventMessageEnd EventType = "message_end" // 消息结束 EventToolStart EventType = "tool_start" // 工具调用请求 EventToolUpdate EventType = "tool_update" // 工具执行进度更新 EventToolEnd EventType = "tool_end" // 工具调用结果 EventTurnEnd EventType = "turn_end" // 一轮结束 EventTransfer EventType = "transfer" // Agent 转移 EventInterrupted EventType = "interrupted" // HITL 中断(等待用户输入) EventAgentEnd EventType = "agent_end" // Agent 处理完成 EventError EventType = "error" // 错误 )
type ImageURLDetail ¶ added in v1.1.0
type ImageURLDetail = schema.ImageURLDetail
ImageURLDetail 控制图片识别质量。
const ( ImageDetailHigh ImageURLDetail = schema.ImageURLDetailHigh ImageDetailLow ImageURLDetail = schema.ImageURLDetailLow ImageDetailAuto ImageURLDetail = schema.ImageURLDetailAuto )
type InterruptPoint ¶
InterruptPoint HITL 中断点信息
type Message ¶
type Message struct {
Role RoleType
Agent string // 产生消息的 Agent 名称
Content string
ReasoningContent string // 推理模型的思考内容(如 DeepSeek-R1、o1),非推理模型为空
}
Message 消息记录
type ResponseMeta ¶
type ResponseMeta = schema.ResponseMeta
ResponseMeta 聊天响应元数据,包含 token 用量、完成原因、log probabilities 等。 通常附着于 EventMessageEnd 事件,在流式场景下来自最后一个 chunk。