Agent

interface Agent(source)

用于管理单个 agent 实例的 api。

大部分方法都重新返回自身(Agent)以支持链式调用。

关于 agent 上下文的基本概念(如“轮次”)请参阅 AgentContextIndex

为了避免内存泄漏,长时间空闲的 agent 会停止运行,状态变为 AgentStatus.DEAD,AutoTweaker 会内部解除对 agent 对象的引用。

statusAgentStatus.DEAD 时调用 agent 的任何方法都将抛出 AgentDeadException,请在观察到 AgentStatus.DEAD 或收到 AgentDeadException 后解除对于 Agent 对象的引用。

agent 的关机过程中也可能存在一个较小窗口,方法抛出 AgentDeadException 但状态不为 AgentStatus.DEAD

Properties

Link copied to clipboard
abstract val activeTools: StateFlow<Set<String>>

实时的已激活工具名称列表,未激活的工具可能被 agent 激活,已激活的工具也可能被 AutoTweaker 取消激活。

Link copied to clipboard
abstract val compacting: StateFlow<Boolean>

agent 的上下文压缩是否正在进行。

Link copied to clipboard
abstract val context: StateFlow<AgentContext>

agent 的实时上下文,完整的消息、工具调用请求均在此索引。

Link copied to clipboard
abstract val id: UUID

agent 的 id,永远唯一。

Link copied to clipboard
abstract val model: ModelConfig

agent 的模型配置,每个 agent 可以拥有独立的模型配置。

Link copied to clipboard
abstract val name: KebabCase

agent 的名称,用于多 agent 场景下的显示区分。

Link copied to clipboard
abstract val output: SharedFlow<AgentOutput>

agent 的实时输出流,通常为无需持久化的流式数据块或错误信息。

Link copied to clipboard
abstract val status: StateFlow<AgentStatus>

agent 的实时状态,参见 AgentStatus

Link copied to clipboard
abstract val toolCalling: StateFlow<Pair<String, ToolPresentation>?>

first 为 agent 正在调用工具的 callId,也就是 io.github.autotweaker.api.types.agent.AgentMessage.Tool.Call.callId

Functions

Link copied to clipboard
abstract suspend fun approve(approval: ToolApprove): Agent

批准一个工具调用请求。

Link copied to clipboard
abstract suspend fun cancelCompact(): Agent

取消正在进行的上下文压缩进程。

Link copied to clipboard
abstract suspend fun cancelTool(): Agent

取消正在进行的工具调用。

Link copied to clipboard
abstract suspend fun compact(): Agent

触发 agent 的上下文压缩,如果上下文压缩正在进行,不会重复触发,上下文压缩在后台进行,此方法不会挂起等待上下文压缩完毕。

Link copied to clipboard
abstract suspend fun inject(injection: ContextInjection): Agent

在 agent 的上下文中注入 XML 标签,相同 id 的标签会去重。

Link copied to clipboard
abstract suspend fun pause(): Agent

令 agent 在当前任务完成之后停下,并归档当前上下文。状态将变为 AgentStatus.FREE

Link copied to clipboard
abstract suspend fun removeInjection(id: UUID): Agent

从 agent 的上下文中移除一条标签注入。

Link copied to clipboard
abstract suspend fun send(content: MessageContent): Delivery

向 agent 发送消息,无论 agent 状态如何,消息将始终进入队列。 若 agent 空闲(AgentStatus.FREE),消息将被立即消费,触发 agent 的事件循环。

Link copied to clipboard
abstract suspend fun sendCoalescing(content: MessageContent): Delivery

向 agent 发送消息,与 send 不同,如果 agent 刚刚结束轮次,或正处于空闲状态,sendCoalescing 不会触发新的 LLM 思考。

Link copied to clipboard
abstract suspend fun setModel(config: ModelConfig): Agent

更新 agent 使用的大模型,下次请求立即生效。

Link copied to clipboard
abstract suspend fun stop(): Agent

立即停止 agent 的工具调用或 LLM 请求,并归档当前上下文,等待状态变为 AgentStatus.FREE(Stop 完成)。