본문으로 건너뛰기

대화 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프롬프트 캐싱 활성화 (토큰 사용량 감소)

사용자 지정 도구

스크립트는 AI가 호출할 수 있는 자체 도구를 등록할 수 있습니다:

const conv = await CAT.agent.conversation.create({
tools: [{
name: "get_weather",
description: "Get weather information for the specified 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 };
}
}]
});

도구의 parametersJSON Schema 사양을 따릅니다. AI는 description을 사용하여 도구를 언제 어떻게 호출할지 이해합니다.

사용자 지정 명령

/로 시작하는 사용자 지정 명령을 등록할 수 있습니다:

const conv = await CAT.agent.conversation.create({
commands: {
"/export": async (args) => {
// 사용자가 "/export pdf"를 입력할 때 트리거됨
await exportToPdf(args);
return "Export complete";
}
}
});

기본 제공 명령: /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 }토큰 사용량
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 키 확인
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 }사고 과정 (assistant 메시지 — 객체이며 일반 문자열이 아님을 참고)
errorstring이 턴에서 오류가 발생한 경우 오류 메시지
modelIdstring이 메시지에 사용된 모델 ID
durationMsnumber총 응답 시간 (ms)
parentIdstring부모 메시지 ID (분기용)
toolCallsToolCall[]도구 호출 기록 (assistant 메시지)
toolCallIdstring해당 도구 호출 ID (tool 메시지)
usage{ inputTokens, outputTokens }토큰 사용량
createtimenumber생성 타임스탬프

clear — 대화 지우기

await conv.clear();

대화의 모든 메시지 기록을 지웁니다.

save — 대화 유지

await conv.save();

대화의 메타데이터를 저장소에 저장합니다. 임시 대화(ephemeral: true)는 기본적으로 저장되지 않습니다. 이 메서드를 호출하면 임시 대화가 유지 대화로 변환됩니다.

인스턴스 속성

속성유형설명
idstring대화 ID
titlestring대화 제목
modelIdstring사용 중인 모델 ID

멀티모달 콘텐츠

메시지 내용은 일반 텍스트 문자열이거나 멀티모달 입력을 지원하는 ContentBlock[] 배열일 수 있습니다:

// 텍스트 + 이미지 전송
await conv.chat([
{ type: "text", text: "Please analyze what's in this image" },
{ 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 유지 대화

기능유지 대화 (기본값)임시 대화
메시지 저장OPFS에 유지메모리에만
기본 제공 도구모두 사용 가능포함되지 않음, tools로 직접 제공
대화 목록표시됨표시되지 않음
프롬프트 캐싱지원됨비활성화 가능
사용 사례일반 목적 대화경량, 일회성 작업 및 빠른 Q&A

컨텍스트 관리

자동 압축

대화의 컨텍스트 사용량이 모델 컨텍스트 창의 80%를 초과하면 시스템이 자동으로 LLM을 호출하여 기록 요약을 생성하고 이전 메시지를 교체하여 공간을 확보합니다.

프롬프트 캐싱

기본적으로 활성화되어 있습니다. Anthropic 모델의 경우 시스템 프롬프트와 메시지 기록이 캐시되어 반복 턴의 토큰 사용량과 대기 시간이 크게 줄어듭니다.

cache: false로 비활성화할 수 있습니다:

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

전체 예제

// ==UserScript==
// @name Smart translation assistant
// @match *://*/*
// @grant CAT.agent.conversation
// @grant CAT.agent.dom
// ==/UserScript==

// 사용자 지정 도구로 대화 만들기
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" };
}
}]
});

// 번역 결과 스트리밍
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;
// UI 실시간 업데이트
updateTranslationUI(result);
}
}