companionbot 0.13.1 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai/claude.js +71 -74
- package/package.json +1 -1
package/dist/ai/claude.js
CHANGED
|
@@ -23,7 +23,7 @@ function getClient() {
|
|
|
23
23
|
}
|
|
24
24
|
return anthropic;
|
|
25
25
|
}
|
|
26
|
-
// Thinking 레벨별 설정
|
|
26
|
+
// Thinking 레벨별 설정
|
|
27
27
|
export const THINKING_CONFIGS = {
|
|
28
28
|
off: { ratio: 0, maxBudget: 0 },
|
|
29
29
|
low: { ratio: 0.3, maxBudget: 5000 },
|
|
@@ -36,7 +36,7 @@ export const MODELS = {
|
|
|
36
36
|
id: "claude-haiku-3-5-20241022",
|
|
37
37
|
name: "Claude Haiku 3.5",
|
|
38
38
|
contextWindow: 200000,
|
|
39
|
-
supportsThinking: false,
|
|
39
|
+
supportsThinking: false,
|
|
40
40
|
},
|
|
41
41
|
sonnet: {
|
|
42
42
|
id: "claude-sonnet-4-20250514",
|
|
@@ -51,59 +51,47 @@ export const MODELS = {
|
|
|
51
51
|
supportsThinking: true,
|
|
52
52
|
},
|
|
53
53
|
};
|
|
54
|
-
// 동적 토큰
|
|
55
|
-
const MIN_OUTPUT_TOKENS = 4096;
|
|
56
|
-
const OUTPUT_BUFFER_RATIO = 0.3;
|
|
54
|
+
// 동적 토큰 계산 설정
|
|
55
|
+
const MIN_OUTPUT_TOKENS = 4096;
|
|
56
|
+
const OUTPUT_BUFFER_RATIO = 0.3;
|
|
57
57
|
/**
|
|
58
58
|
* 동적으로 max_tokens와 thinking budget 계산
|
|
59
|
-
*
|
|
60
|
-
* @param modelId 모델 ID
|
|
61
|
-
* @param thinkingLevel thinking 레벨
|
|
62
|
-
* @param inputTokens 현재 입력 토큰 수 (시스템 프롬프트 + 히스토리)
|
|
63
|
-
* @returns { maxTokens, thinkingBudget }
|
|
64
59
|
*/
|
|
65
60
|
export function calculateTokenBudgets(modelId, thinkingLevel, inputTokens) {
|
|
66
61
|
const model = MODELS[modelId];
|
|
67
62
|
const thinkingConfig = THINKING_CONFIGS[thinkingLevel];
|
|
68
|
-
// Thinking 미지원 모델이거나 off인 경우
|
|
69
63
|
if (!model.supportsThinking || thinkingLevel === "off") {
|
|
70
|
-
// 간단히 고정 max_tokens 사용
|
|
71
64
|
return { maxTokens: 8192, thinkingBudget: 0 };
|
|
72
65
|
}
|
|
73
|
-
// 사용 가능한 출력 토큰 계산
|
|
74
|
-
// 컨텍스트 윈도우 - 입력 토큰 = 출력 가능 토큰
|
|
75
66
|
const availableOutputTokens = model.contextWindow - inputTokens;
|
|
76
|
-
// 최소 출력 토큰 보장
|
|
77
67
|
const maxTokens = Math.max(MIN_OUTPUT_TOKENS, Math.floor(availableOutputTokens * OUTPUT_BUFFER_RATIO));
|
|
78
|
-
// thinking budget 계산: min(레벨별 최대값, max_tokens * 비율)
|
|
79
|
-
// API 조건: max_tokens > budget_tokens 이므로 max_tokens - 1024 로 상한 설정
|
|
80
68
|
const calculatedBudget = Math.floor(maxTokens * thinkingConfig.ratio);
|
|
81
|
-
const thinkingBudget = Math.min(thinkingConfig.maxBudget, calculatedBudget, maxTokens - 1024
|
|
82
|
-
);
|
|
83
|
-
// budget이 1024 미만이면 thinking 비활성화 (의미 없음)
|
|
69
|
+
const thinkingBudget = Math.min(thinkingConfig.maxBudget, calculatedBudget, maxTokens - 1024);
|
|
84
70
|
if (thinkingBudget < 1024) {
|
|
85
71
|
return { maxTokens, thinkingBudget: 0 };
|
|
86
72
|
}
|
|
87
73
|
return { maxTokens, thinkingBudget };
|
|
88
74
|
}
|
|
89
|
-
|
|
90
|
-
)
|
|
75
|
+
/**
|
|
76
|
+
* Claude API 호출 (스트리밍 내부 사용, thinking 지원)
|
|
77
|
+
* - 스트리밍으로 호출하되 최종 응답만 반환 (사용자에게 중간 메시지 안 보냄)
|
|
78
|
+
* - thinking 활성화 가능
|
|
79
|
+
* - 도구 사용 시에는 non-streaming으로 폴백 (thinking off)
|
|
80
|
+
*/
|
|
81
|
+
export async function chat(messages, systemPrompt, modelId = "sonnet", thinkingLevel = "medium") {
|
|
91
82
|
const client = getClient();
|
|
92
83
|
const modelConfig = MODELS[modelId];
|
|
93
84
|
const toolsUsed = [];
|
|
94
|
-
// 메시지를 API 형식으로 변환
|
|
95
85
|
const apiMessages = messages.map((m) => ({
|
|
96
86
|
role: m.role,
|
|
97
87
|
content: m.content,
|
|
98
88
|
}));
|
|
99
|
-
// 입력 토큰 추정
|
|
89
|
+
// 입력 토큰 추정
|
|
100
90
|
const estimateInputTokens = () => {
|
|
101
91
|
let total = 0;
|
|
102
|
-
// 시스템 프롬프트
|
|
103
92
|
if (systemPrompt) {
|
|
104
|
-
total += Math.ceil(systemPrompt.length / 3);
|
|
93
|
+
total += Math.ceil(systemPrompt.length / 3);
|
|
105
94
|
}
|
|
106
|
-
// 메시지들
|
|
107
95
|
for (const msg of apiMessages) {
|
|
108
96
|
const content = typeof msg.content === "string"
|
|
109
97
|
? msg.content
|
|
@@ -112,37 +100,65 @@ export async function chat(messages, systemPrompt, modelId = "sonnet", _thinking
|
|
|
112
100
|
}
|
|
113
101
|
return total;
|
|
114
102
|
};
|
|
115
|
-
// 토큰 계산 (thinking 비활성화 - non-streaming에서 에러 발생)
|
|
116
103
|
const inputTokens = estimateInputTokens();
|
|
117
|
-
const maxTokens =
|
|
118
|
-
console.log(`[Chat] model=${modelId}, input~${inputTokens}, maxTokens=${maxTokens}`);
|
|
119
|
-
//
|
|
120
|
-
const
|
|
104
|
+
const { maxTokens, thinkingBudget } = calculateTokenBudgets(modelId, thinkingLevel, inputTokens);
|
|
105
|
+
console.log(`[Chat] model=${modelId}, thinking=${thinkingLevel}, input~${inputTokens}, maxTokens=${maxTokens}, budget=${thinkingBudget}`);
|
|
106
|
+
// 스트리밍 호출 (thinking 사용 가능)
|
|
107
|
+
const streamRequest = async () => {
|
|
121
108
|
const params = {
|
|
122
109
|
model: modelConfig.id,
|
|
123
110
|
max_tokens: maxTokens,
|
|
124
111
|
messages: apiMessages,
|
|
125
112
|
tools: tools,
|
|
113
|
+
stream: true,
|
|
114
|
+
};
|
|
115
|
+
if (systemPrompt) {
|
|
116
|
+
params.system = systemPrompt;
|
|
117
|
+
}
|
|
118
|
+
// thinking 활성화
|
|
119
|
+
if (thinkingBudget > 0) {
|
|
120
|
+
params.thinking = {
|
|
121
|
+
type: "enabled",
|
|
122
|
+
budget_tokens: thinkingBudget,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// 스트리밍하되 최종 메시지만 반환
|
|
126
|
+
const stream = client.messages.stream(params);
|
|
127
|
+
return await stream.finalMessage();
|
|
128
|
+
};
|
|
129
|
+
// Non-streaming 호출 (도구 사용 루프용, thinking off)
|
|
130
|
+
const nonStreamRequest = async () => {
|
|
131
|
+
const params = {
|
|
132
|
+
model: modelConfig.id,
|
|
133
|
+
max_tokens: 8192,
|
|
134
|
+
messages: apiMessages,
|
|
135
|
+
tools: tools,
|
|
126
136
|
};
|
|
127
137
|
if (systemPrompt) {
|
|
128
138
|
params.system = systemPrompt;
|
|
129
139
|
}
|
|
130
|
-
return params;
|
|
140
|
+
return await client.messages.create(params);
|
|
131
141
|
};
|
|
142
|
+
// 첫 번째 호출은 스트리밍 (thinking 사용)
|
|
132
143
|
let response;
|
|
133
|
-
|
|
134
|
-
|
|
144
|
+
try {
|
|
145
|
+
response = await withRetry(() => withTimeout(streamRequest, API_TIMEOUT_MS, "API 응답 시간 초과"), API_RETRY_OPTIONS);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
// 스트리밍 실패 시 non-streaming 폴백
|
|
149
|
+
console.log("[Chat] Streaming failed, falling back to non-streaming");
|
|
150
|
+
response = await withRetry(() => withTimeout(nonStreamRequest, API_TIMEOUT_MS, "API 응답 시간 초과"), API_RETRY_OPTIONS);
|
|
151
|
+
}
|
|
152
|
+
// Tool use 루프 (non-streaming, thinking off)
|
|
135
153
|
let iterations = 0;
|
|
136
154
|
while (response.stop_reason === "tool_use" && iterations < MAX_TOOL_ITERATIONS) {
|
|
137
155
|
iterations++;
|
|
138
156
|
const toolUseBlocks = response.content.filter((block) => block.type === "tool_use");
|
|
139
|
-
// 도구 병렬 실행 (성능 최적화)
|
|
140
157
|
console.log(`[Tool] Executing ${toolUseBlocks.length} tool(s) in parallel`);
|
|
141
158
|
const toolExecutions = await Promise.all(toolUseBlocks.map(async (toolUse) => {
|
|
142
159
|
const startTime = Date.now();
|
|
143
|
-
console.log(`[Tool] ${toolUse.name}:`, JSON.stringify(toolUse.input).slice(0,
|
|
160
|
+
console.log(`[Tool] ${toolUse.name}:`, JSON.stringify(toolUse.input).slice(0, TOOL_INPUT_SUMMARY_LENGTH));
|
|
144
161
|
try {
|
|
145
|
-
// 도구별 타임아웃 적용
|
|
146
162
|
const timeout = getToolTimeout(toolUse.name);
|
|
147
163
|
const result = await Promise.race([
|
|
148
164
|
executeTool(toolUse.name, toolUse.input),
|
|
@@ -150,32 +166,17 @@ export async function chat(messages, systemPrompt, modelId = "sonnet", _thinking
|
|
|
150
166
|
]);
|
|
151
167
|
const elapsed = Date.now() - startTime;
|
|
152
168
|
console.log(`[Tool] ${toolUse.name} completed in ${elapsed}ms`);
|
|
153
|
-
// 스마트 결과 압축
|
|
154
169
|
const compressedResult = compressToolResult(toolUse.name, result);
|
|
155
|
-
return {
|
|
156
|
-
toolUse,
|
|
157
|
-
result: compressedResult,
|
|
158
|
-
success: true,
|
|
159
|
-
};
|
|
170
|
+
return { toolUse, result: compressedResult, success: true };
|
|
160
171
|
}
|
|
161
172
|
catch (error) {
|
|
162
173
|
const elapsed = Date.now() - startTime;
|
|
163
174
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
164
175
|
console.error(`[Tool] ${toolUse.name} failed after ${elapsed}ms:`, errorMsg);
|
|
165
|
-
return {
|
|
166
|
-
toolUse,
|
|
167
|
-
result: `Error: ${errorMsg}`,
|
|
168
|
-
success: false,
|
|
169
|
-
};
|
|
176
|
+
return { toolUse, result: `Error: ${errorMsg}`, success: false };
|
|
170
177
|
}
|
|
171
178
|
}));
|
|
172
|
-
// 결과
|
|
173
|
-
const toolResults = toolExecutions.map((exec) => ({
|
|
174
|
-
type: "tool_result",
|
|
175
|
-
tool_use_id: exec.toolUse.id,
|
|
176
|
-
content: exec.result,
|
|
177
|
-
}));
|
|
178
|
-
// 도구 사용 기록
|
|
179
|
+
// 도구 결과 기록
|
|
179
180
|
for (const exec of toolExecutions) {
|
|
180
181
|
toolsUsed.push({
|
|
181
182
|
name: exec.toolUse.name,
|
|
@@ -183,34 +184,30 @@ export async function chat(messages, systemPrompt, modelId = "sonnet", _thinking
|
|
|
183
184
|
output: exec.result.slice(0, TOOL_OUTPUT_SUMMARY_LENGTH),
|
|
184
185
|
});
|
|
185
186
|
}
|
|
186
|
-
// 어시스턴트
|
|
187
|
+
// 어시스턴트 메시지 추가 (도구 호출)
|
|
187
188
|
apiMessages.push({
|
|
188
189
|
role: "assistant",
|
|
189
190
|
content: response.content,
|
|
190
191
|
});
|
|
192
|
+
// 도구 결과 메시지 추가
|
|
191
193
|
apiMessages.push({
|
|
192
194
|
role: "user",
|
|
193
|
-
content:
|
|
195
|
+
content: toolExecutions.map((exec) => ({
|
|
196
|
+
type: "tool_result",
|
|
197
|
+
tool_use_id: exec.toolUse.id,
|
|
198
|
+
content: exec.result,
|
|
199
|
+
})),
|
|
194
200
|
});
|
|
195
|
-
// 다음
|
|
196
|
-
response = await withRetry(() => withTimeout(
|
|
197
|
-
}
|
|
198
|
-
// 반복 횟수 초과 시 경고
|
|
199
|
-
if (iterations >= MAX_TOOL_ITERATIONS) {
|
|
200
|
-
console.warn(`[Warning] Tool use loop reached max iterations (${MAX_TOOL_ITERATIONS})`);
|
|
201
|
-
return { text: "도구 실행이 너무 많이 반복됐어. 다시 시도해줄래?", toolsUsed };
|
|
201
|
+
// 다음 API 호출 (non-streaming, thinking off - 도구 결과 처리)
|
|
202
|
+
response = await withRetry(() => withTimeout(nonStreamRequest, API_TIMEOUT_MS, "API 응답 시간 초과"), API_RETRY_OPTIONS);
|
|
202
203
|
}
|
|
203
|
-
// 최종 텍스트
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
toolsUsed
|
|
208
|
-
};
|
|
204
|
+
// 최종 텍스트 추출
|
|
205
|
+
const textBlocks = response.content.filter((block) => block.type === "text");
|
|
206
|
+
const text = textBlocks.map((b) => b.text).join("\n");
|
|
207
|
+
return { text, toolsUsed };
|
|
209
208
|
}
|
|
210
209
|
/**
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* 도구 사용 여부를 별도로 반환하여 호출자가 구분할 수 있게 함
|
|
210
|
+
* chat()의 간단한 래퍼 - 도구 사용 여부 반환
|
|
214
211
|
*/
|
|
215
212
|
export async function chatSmart(messages, systemPrompt, modelId, thinkingLevel = "medium") {
|
|
216
213
|
const result = await chat(messages, systemPrompt, modelId, thinkingLevel);
|