跳到主要内容

对话 API

@grant CAT.agent.conversation

对话 API 是 Agent 系统的核心,允许脚本创建 AI 对话、发送消息并接收回复。

创建对话

const conv = await CAT.agent.conversation.create(options?);

ConversationCreateOptions

参数类型默认值说明
idstring自动生成对话 ID,用于恢复已有对话
systemstring自定义系统提示词,追加到内置提示词之后
modelstring默认模型模型 ID(在管理页面配置后获取)
maxIterationsnumber20单次对话中工具调用的最大循环次数
skills"auto" | string[]"auto" 自动加载所有 Skill,或指定 Skill 名称数组
toolsToolDefinition[]自定义工具列表(见下方)
commandsRecord<string, CommandHandler>自定义对话命令
ephemeralbooleanfalse临时对话,不持久化到存储
cachebooleantrue启用 Prompt Caching(减少 Token 消耗)

自定义工具

脚本可以注册自己的工具供 AI 调用:

const conv = await CAT.agent.conversation.create({
tools: [{
name: "get_weather",
description: "获取指定城市的天气信息",
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "城市名称"
},
unit: {
type: "string",
enum: ["celsius", "fahrenheit"],
description: "温度单位"
}
},
required: ["city"]
},
handler: async (args) => {
// args = { city: "北京", unit: "celsius" }
const data = await fetchWeather(args.city, args.unit);
return { temperature: data.temp, condition: data.condition };
}
}]
});

工具的 parameters 遵循 JSON Schema 规范,AI 会根据 description 理解何时及如何调用工具。

自定义命令

可以注册以 / 开头的自定义命令:

const conv = await CAT.agent.conversation.create({
commands: {
"/export": async (args) => {
// 用户输入 "/export pdf" 时触发
await exportToPdf(args);
return "导出完成";
}
}
});

内置命令:/new(新建对话)、/reset(重置上下文)、/compact(压缩历史消息)。

获取已有对话

const conv = await CAT.agent.conversation.get(conversationId);
// 如果对话不存在则返回 null

ConversationInstance 方法

chat — 同步聊天

const reply = await conv.chat(content, options?);

发送消息并等待完整回复返回。AI 可能在回复过程中调用工具,chat 会等待所有工具执行完成后返回最终结果。

参数:

参数类型说明
contentstring | ContentBlock[]消息内容,文本或多模态内容块
options.toolsToolDefinition[]本次调用额外追加的工具(与创建时的工具合并)

返回值 ChatReply:

字段类型说明
contentstring | ContentBlock[]AI 回复内容
thinkingstring模型思考过程(仅部分模型支持)
toolCallsToolCall[]本次回复中的工具调用记录
usage{ inputTokens, outputTokens }Token 用量
commandboolean是否为命令触发的回复

chatStream — 流式聊天

const stream = await conv.chatStream(content, options?);
for await (const chunk of stream) {
// 处理流式事件
}

实时接收 AI 回复,适用于需要逐步展示输出的场景。

StreamChunk 事件类型:

type字段说明
content_deltacontent: string增量文本内容
thinking_deltathinking: string增量思考内容
tool_calltoolCall: ToolCall工具调用信息(状态变化时触发)
content_blockblock: ContentBlock内容块(图片、文件等)
doneusage: { inputTokens, outputTokens }对话完成
errorerror: string, errorCode?: string错误

错误码(errorCode):

错误码说明
rate_limitAPI 频率限制,通常会自动重试
auth认证失败,检查 API Key
tool_timeout工具执行超时
max_iterations达到最大工具调用循环次数
api_error其他 API 错误

getMessages — 获取消息历史

const messages = await conv.getMessages();

返回 ChatMessage[],包含对话中所有消息。

ChatMessage 结构:

字段类型说明
idstring消息 ID
role"user" | "assistant" | "system" | "tool"消息角色
contentstring | ContentBlock[]消息内容
thinkingstring思考过程(assistant 消息)
toolCallsToolCall[]工具调用记录(assistant 消息)
toolCallIdstring对应的工具调用 ID(tool 消息)
usage{ inputTokens, outputTokens }Token 用量
createtimenumber创建时间戳

clear — 清空对话

await conv.clear();

清空对话的所有消息历史。

save — 持久化保存

await conv.save();

将对话元数据保存到存储。临时对话(ephemeral: true)默认不保存,调用此方法可以将其转为持久化。

多模态内容

消息内容可以是纯文本字符串,也可以是 ContentBlock[] 数组以支持多模态:

// 发送文本 + 图片
await conv.chat([
{ type: "text", text: "请分析这张图片的内容" },
{ type: "image", attachmentId: "img-id", mimeType: "image/png" }
]);

ContentBlock 类型

type必填字段说明
texttext: string文本内容
imageattachmentId: string, mimeType: string图片,需要模型支持视觉
fileattachmentId: string, mimeType: string, name: string文件
audioattachmentId: string, mimeType: string音频

临时对话 vs 持久化对话

特性持久化对话(默认)临时对话(ephemeral)
消息存储OPFS 持久化仅内存
内置工具全部可用不包含,需自行通过 tools 提供
对话列表可见不可见
Prompt Caching支持可选关闭
用途通用对话轻量临时任务、快速问答

上下文管理

自动压缩(Auto-compact)

当对话上下文使用量超过模型 context window 的 80% 时,系统会自动调用 LLM 生成历史摘要,替换旧消息以释放空间。

Prompt Caching

默认启用。对于 Anthropic 模型,系统提示词和历史消息会被缓存,重复对话时可显著减少 Token 消耗和延迟。

可通过 cache: false 关闭:

const conv = await CAT.agent.conversation.create({ cache: false });

完整示例

// ==UserScript==
// @name 智能翻译助手
// @match *://*/*
// @grant CAT.agent.conversation
// @grant CAT.agent.dom
// ==/UserScript==

// 创建带有自定义工具的对话
const conv = await CAT.agent.conversation.create({
system: "你是一个翻译助手。用户会给你网页内容,请翻译成中文。",
tools: [{
name: "get_selection",
description: "获取用户在页面上选中的文本",
parameters: { type: "object", properties: {} },
handler: async () => {
return { text: window.getSelection()?.toString() || "没有选中文本" };
}
}]
});

// 流式输出翻译结果
const stream = await conv.chatStream("请获取选中的文本并翻译成中文");
let result = "";
for await (const chunk of stream) {
if (chunk.type === "content_delta") {
result += chunk.content;
// 实时更新 UI
updateTranslationUI(result);
}
}