@yeaft/webchat-agent 0.1.120 → 0.1.121
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/claude.js +28 -2
- package/conversation.js +1 -1
- package/package.json +1 -1
package/claude.js
CHANGED
|
@@ -13,6 +13,27 @@ import {
|
|
|
13
13
|
writeBackRouteContext
|
|
14
14
|
} from './roleplay.js';
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Determine maxContextTokens and autoCompactThreshold from model name.
|
|
18
|
+
* Returns defaults suitable for the model's context window size.
|
|
19
|
+
*/
|
|
20
|
+
export function getModelContextConfig(modelName) {
|
|
21
|
+
if (!modelName) return { maxContext: 128000, compactThreshold: 110000 };
|
|
22
|
+
const name = modelName.toLowerCase();
|
|
23
|
+
// Explicit 1M context indicators
|
|
24
|
+
if (name.includes('1m') || name.includes('1000k')) {
|
|
25
|
+
return { maxContext: 1000000, compactThreshold: 256000 };
|
|
26
|
+
}
|
|
27
|
+
// 200k context models: Claude 3.5, Claude Sonnet 4, Claude Opus 4, etc.
|
|
28
|
+
if (name.includes('claude-sonnet-4') || name.includes('claude-opus-4') ||
|
|
29
|
+
name.includes('claude-3-5') || name.includes('claude-3.5') ||
|
|
30
|
+
name.includes('200k')) {
|
|
31
|
+
return { maxContext: 200000, compactThreshold: 160000 };
|
|
32
|
+
}
|
|
33
|
+
// Default: 128k (Claude 3 Haiku, older models, unknown)
|
|
34
|
+
return { maxContext: 128000, compactThreshold: 110000 };
|
|
35
|
+
}
|
|
36
|
+
|
|
16
37
|
/**
|
|
17
38
|
* Start a Claude SDK query for a conversation
|
|
18
39
|
* Uses the SDK with AsyncIterable input stream for bidirectional communication
|
|
@@ -288,8 +309,13 @@ async function processClaudeOutput(conversationId, claudeQuery, state) {
|
|
|
288
309
|
state.tools = message.tools || [];
|
|
289
310
|
state.slashCommands = message.slash_commands || [];
|
|
290
311
|
state.model = message.model || null;
|
|
312
|
+
// Set per-conversation context config based on model
|
|
313
|
+
const modelConfig = getModelContextConfig(state.model);
|
|
314
|
+
state.maxContextTokens = modelConfig.maxContext;
|
|
315
|
+
state.autoCompactThreshold = modelConfig.compactThreshold;
|
|
291
316
|
console.log(`Claude session ID: ${state.claudeSessionId}`);
|
|
292
317
|
console.log(`Model: ${state.model}`);
|
|
318
|
+
console.log(`Model context: ${state.maxContextTokens} tokens, compact threshold: ${state.autoCompactThreshold}`);
|
|
293
319
|
console.log(`Available tools: ${state.tools.length}`);
|
|
294
320
|
console.log(`Tools: ${state.tools.join(', ')}`);
|
|
295
321
|
console.log(`Available slash commands: ${state.slashCommands.join(', ')}`);
|
|
@@ -420,7 +446,7 @@ async function processClaudeOutput(conversationId, claudeQuery, state) {
|
|
|
420
446
|
|
|
421
447
|
// 计算上下文使用百分比
|
|
422
448
|
const inputTokens = message.usage?.input_tokens || 0;
|
|
423
|
-
const maxContextTokens = ctx.CONFIG?.maxContextTokens || 128000;
|
|
449
|
+
const maxContextTokens = state.maxContextTokens || ctx.CONFIG?.maxContextTokens || 128000;
|
|
424
450
|
if (inputTokens > 0) {
|
|
425
451
|
ctx.sendToServer({
|
|
426
452
|
type: 'context_usage',
|
|
@@ -527,7 +553,7 @@ async function processClaudeOutput(conversationId, claudeQuery, state) {
|
|
|
527
553
|
});
|
|
528
554
|
|
|
529
555
|
// ★ Pre-send compact check for RolePlay auto-continue
|
|
530
|
-
const rpAutoCompactThreshold = ctx.CONFIG?.autoCompactThreshold ||
|
|
556
|
+
const rpAutoCompactThreshold = state.autoCompactThreshold || ctx.CONFIG?.autoCompactThreshold || 110000;
|
|
531
557
|
const rpEstimatedNewTokens = Math.ceil(prompt.length / 3);
|
|
532
558
|
// Include output_tokens: the assistant's output becomes part of context for the next turn
|
|
533
559
|
const rpOutputTokens = message.usage?.output_tokens || 0;
|
package/conversation.js
CHANGED
|
@@ -673,7 +673,7 @@ export async function handleUserInput(msg) {
|
|
|
673
673
|
console.log(`[${conversationId}] Sending: ${prompt.substring(0, 100)}...`);
|
|
674
674
|
|
|
675
675
|
// ★ Pre-send compact check: estimate total tokens and compact before sending if needed
|
|
676
|
-
const autoCompactThreshold = ctx.CONFIG?.autoCompactThreshold ||
|
|
676
|
+
const autoCompactThreshold = state.autoCompactThreshold || ctx.CONFIG?.autoCompactThreshold || 110000;
|
|
677
677
|
const lastInputTokens = state.lastResultInputTokens || 0;
|
|
678
678
|
const lastOutputTokens = state.lastResultOutputTokens || 0;
|
|
679
679
|
const estimatedNewTokens = Math.ceil(effectivePrompt.length / 3); // conservative: ~3 chars per token
|