跳至主要内容

對話 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" 自動載入所有 Skills,或指定 Skill 名稱的陣列
toolsToolDefinition[]自訂工具清單(見下文)
commandsRecord<string, CommandHandler>自訂對話指令
ephemeralbooleanfalse不會持久化儲存的暫時對話
cachebooleantrue啟用提示詞快取(減少 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(清除對話歷史)— 可被自訂處理器覆寫。

取得現有對話

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 事件類型:

類型欄位說明
content_deltacontent: string增量文字內容
thinking_deltathinking: string增量思考內容
tool_calltoolCall: ToolCall工具呼叫資訊(狀態變更時觸發)
content_blockblock: ContentBlock內容區塊(圖片、檔案等)
doneusage: { inputTokens, outputTokens }對話輪次完成
errorerror: string, errorCode?: string錯誤

錯誤代碼(errorCode):

代碼說明
rate_limit觸及 API 速率限制;通常會自動重試
auth驗證失敗;請檢查 API 金鑰
tool_timeout工具執行逾時
max_iterations觸及最大工具呼叫迴圈數
api_error其他 API 錯誤

getMessages — 取得訊息歷史

const messages = await conv.getMessages();

回傳包含對話中所有訊息的 ChatMessage[]

ChatMessage 結構:

欄位型別說明
idstring訊息 ID
role"user" | "assistant" | "system" | "tool"訊息角色
contentstring | ContentBlock[]訊息內容
thinking{ content: string }思考過程(助理訊息 — 注意這是物件,不是純字串)
errorstring此輪次出錯時的錯誤訊息
modelIdstring此訊息使用的模型 ID
durationMsnumber總回應時間(毫秒)
parentIdstring親訊息 ID(用於分支)
toolCallsToolCall[]工具呼叫記錄(助理訊息)
toolCallIdstring對應的工具呼叫 ID(工具訊息)
usage{ inputTokens, outputTokens }Token 使用量
createtimenumber建立時間戳

clear — 清除對話

await conv.clear();

清除對話中的所有訊息歷史。

save — 持久化對話

await conv.save();

將對話的中繼資料儲存到儲存空間。暫時對話(ephemeral: true)預設不會儲存;呼叫此方法將其轉換為持久化對話。

Instance 屬性

屬性型別說明
idstring對話 ID
titlestring對話標題
modelIdstring使用中的模型 ID

多媒體內容

訊息內容可以是純文字字串,或 ContentBlock[] 陣列以支援多媒體輸入:

// 傳送文字 + 圖片
await conv.chat([
{ type: "text", text: "請分析這張圖片中的內容" },
{ type: "image", attachmentId: "img-id", mimeType: "image/png" }
]);

ContentBlock 類型 | 類型 | 必填欄位 | 說明 |

|------|---------|------| | text | text: string | 文字內容 | | image | attachmentId: string, mimeType: string | 圖片;需要支援視覺的模型 | | file | attachmentId: string, mimeType: string, name: string | 檔案 | | audio | attachmentId: string, mimeType: string | 音訊 |

暫時對話 vs. 持久化對話

功能持久化對話(預設)暫時對話
訊息儲存持久化到 OPFS僅在記憶體中
內建工具全部可用不包含;透過 tools 提供您自己的
對話清單可見不可見
提示詞快取支援可停用
使用情境通用對話輕量級、一次性任務和快速問答

語境管理

自動壓縮

當對話的語境使用量超過模型語境視窗的 80% 時,系統會自動呼叫 LLM 產生歷史摘要,替換較舊的訊息以釋放空間。

提示詞快取

預設啟用。對於 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;
// 即時更新介面
updateTranslationUI(result);
}
}