Skip to main content

Conversation API

@grant CAT.agent.conversation

The Conversation API is the core of the Agent system, letting a script create AI conversations, send messages, and receive replies.

Creating a Conversation

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

ConversationCreateOptions

ParameterTypeDefaultDescription
idstringauto-generatedConversation ID, used to resume an existing conversation
systemstringCustom system prompt, appended after the built-in prompt
modelstringdefault modelModel ID (obtained after configuring it in the dashboard)
maxIterationsnumber20Maximum number of tool-call loop iterations within a single conversation turn
skills"auto" | string[]"auto" loads all Skills automatically, or specify an array of Skill names
toolsToolDefinition[]Custom tool list (see below)
commandsRecord<string, CommandHandler>Custom conversation commands
ephemeralbooleanfalseEphemeral conversation, not persisted to storage
cachebooleantrueEnable Prompt Caching (reduces token usage)
backgroundbooleanfalseBackground conversation that keeps running after the UI disconnects; can be reconnected via attach()

Custom Tools

A script can register its own tools for the AI to call:

const conv = await CAT.agent.conversation.create({
tools: [{
name: "get_weather",
description: "Get weather information for a given city",
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "City name"
},
unit: {
type: "string",
enum: ["celsius", "fahrenheit"],
description: "Temperature unit"
}
},
required: ["city"]
},
handler: async (args) => {
// args = { city: "Beijing", unit: "celsius" }
const data = await fetchWeather(args.city, args.unit);
return { temperature: data.temp, condition: data.condition };
}
}]
});

A tool's parameters follows the JSON Schema spec; the AI uses description to understand when and how to call the tool.

Custom Commands

You can register custom commands starting with /:

const conv = await CAT.agent.conversation.create({
commands: {
"/export": async (args) => {
// triggered when the user types "/export pdf"
await exportToPdf(args);
return "Export complete";
}
}
});

Built-in commands: /new (start a new conversation), /reset (reset context), /compact (compact message history).

Getting an Existing Conversation

const conv = await CAT.agent.conversation.get(conversationId);
// returns null if the conversation doesn't exist

ConversationInstance Methods

chat — Synchronous Chat

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

Sends a message and waits for the full reply to come back. The AI may call tools while replying; chat waits for all tool calls to finish before returning the final result.

Parameters:

ParameterTypeDescription
contentstring | ContentBlock[]Message content: text or multi-modal content blocks
options.toolsToolDefinition[]Extra tools appended just for this call (merged with the tools set at creation time)

Return value, ChatReply:

FieldTypeDescription
contentstring | ContentBlock[]The AI's reply content
thinkingstringThe model's thinking process (only some models support this)
toolCallsToolCall[]The tool calls recorded during this reply
usage{ inputTokens, outputTokens }Token usage
commandbooleanWhether this reply was triggered by a command

chatStream — Streaming Chat

const stream = await conv.chatStream(content, options?);
for await (const chunk of stream) {
// handle streaming events
}

Receives the AI's reply in real time — useful when you need to progressively display the output.

StreamChunk event types:

typeFieldDescription
content_deltacontent: stringIncremental text content
thinking_deltathinking: stringIncremental thinking content
tool_calltoolCall: ToolCallTool call info (fires on state changes)
content_blockblock: ContentBlockA content block (image, file, etc.)
doneusage: { inputTokens, outputTokens }The conversation turn is complete
errorerror: string, errorCode?: stringAn error occurred

Error codes (errorCode):

Error codeDescription
rate_limitAPI rate limit; usually retried automatically
authAuthentication failed; check the API Key
tool_timeoutTool execution timed out
max_iterationsReached the maximum tool-call loop count
api_errorOther API error

getMessages — Get Message History

const messages = await conv.getMessages();

Returns ChatMessage[], containing every message in the conversation.

ChatMessage structure:

FieldTypeDescription
idstringMessage ID
role"user" | "assistant" | "system" | "tool"Message role
contentstring | ContentBlock[]Message content
thinkingstringThinking process (assistant messages)
toolCallsToolCall[]Tool call records (assistant messages)
toolCallIdstringThe corresponding tool call ID (tool messages)
usage{ inputTokens, outputTokens }Token usage
createtimenumberCreation timestamp

clear — Clear the Conversation

await conv.clear();

Clears all message history in the conversation.

save — Persist the Conversation

await conv.save();

Saves the conversation's metadata to storage. Ephemeral conversations (ephemeral: true) are not saved by default; calling this method converts them to persistent.

attach — Reconnect to a Background Conversation

const stream = await conv.attach();
for await (const chunk of stream) {
// receive real-time events from the background conversation
}

When a conversation was created with background: true and is still running in the background, you can reconnect to it with attach() to receive its subsequent streaming events.

Instance Properties

PropertyTypeDescription
idstringConversation ID
titlestringConversation title
modelIdstringThe model ID in use

Multi-modal Content

Message content can be a plain text string, or a ContentBlock[] array to support multiple modalities:

// Send text + an image
await conv.chat([
{ type: "text", text: "Please analyze the content of this image" },
{ type: "image", attachmentId: "img-id", mimeType: "image/png" }
]);

ContentBlock Types

typeRequired fieldsDescription
texttext: stringText content
imageattachmentId: string, mimeType: stringAn image; the model must support vision
fileattachmentId: string, mimeType: string, name: stringA file
audioattachmentId: string, mimeType: stringAudio

Ephemeral vs. Persistent Conversations

FeaturePersistent conversation (default)Ephemeral conversation
Message storagePersisted to OPFSMemory only
Built-in toolsAll availableNot included; must be supplied via tools
Conversation listVisibleNot visible
Prompt CachingSupportedCan be disabled
Use caseGeneral conversationLightweight one-off tasks, quick Q&A

Context Management

Auto-compact

When the conversation's context usage exceeds 80% of the model's context window, the system automatically calls the LLM to generate a summary of the history, replacing older messages to free up space.

Prompt Caching

Enabled by default. For Anthropic models, the system prompt and message history are cached, which can significantly reduce token usage and latency on repeated conversation turns.

Can be disabled with cache: false:

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

Full Example

// ==UserScript==
// @name Smart Translation Assistant
// @match *://*/*
// @grant CAT.agent.conversation
// @grant CAT.agent.dom
// ==/UserScript==

// Create a conversation with a custom tool
const conv = await CAT.agent.conversation.create({
system: "You are a translation assistant. The user will give you web page content — please translate it into Chinese.",
tools: [{
name: "get_selection",
description: "Get the text the user has selected on the page",
parameters: { type: "object", properties: {} },
handler: async () => {
return { text: window.getSelection()?.toString() || "No text selected" };
}
}]
});

// Stream the translation result
const stream = await conv.chatStream("Please get the selected text and translate it into Chinese");
let result = "";
for await (const chunk of stream) {
if (chunk.type === "content_delta") {
result += chunk.content;
// update the UI in real time
updateTranslationUI(result);
}
}