@yeaft/webchat-agent 0.1.120 → 0.1.122

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 CHANGED
@@ -13,6 +13,22 @@ 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
+ // Default: 128k — Copilot API models (Sonnet 4, Opus 4, Claude 3.5 etc.)
28
+ // report 200k context but actual usable window is 128k
29
+ return { maxContext: 128000, compactThreshold: 110000 };
30
+ }
31
+
16
32
  /**
17
33
  * Start a Claude SDK query for a conversation
18
34
  * Uses the SDK with AsyncIterable input stream for bidirectional communication
@@ -288,8 +304,13 @@ async function processClaudeOutput(conversationId, claudeQuery, state) {
288
304
  state.tools = message.tools || [];
289
305
  state.slashCommands = message.slash_commands || [];
290
306
  state.model = message.model || null;
307
+ // Set per-conversation context config based on model
308
+ const modelConfig = getModelContextConfig(state.model);
309
+ state.maxContextTokens = modelConfig.maxContext;
310
+ state.autoCompactThreshold = modelConfig.compactThreshold;
291
311
  console.log(`Claude session ID: ${state.claudeSessionId}`);
292
312
  console.log(`Model: ${state.model}`);
313
+ console.log(`Model context: ${state.maxContextTokens} tokens, compact threshold: ${state.autoCompactThreshold}`);
293
314
  console.log(`Available tools: ${state.tools.length}`);
294
315
  console.log(`Tools: ${state.tools.join(', ')}`);
295
316
  console.log(`Available slash commands: ${state.slashCommands.join(', ')}`);
@@ -420,7 +441,7 @@ async function processClaudeOutput(conversationId, claudeQuery, state) {
420
441
 
421
442
  // 计算上下文使用百分比
422
443
  const inputTokens = message.usage?.input_tokens || 0;
423
- const maxContextTokens = ctx.CONFIG?.maxContextTokens || 128000;
444
+ const maxContextTokens = state.maxContextTokens || ctx.CONFIG?.maxContextTokens || 128000;
424
445
  if (inputTokens > 0) {
425
446
  ctx.sendToServer({
426
447
  type: 'context_usage',
@@ -527,7 +548,7 @@ async function processClaudeOutput(conversationId, claudeQuery, state) {
527
548
  });
528
549
 
529
550
  // ★ Pre-send compact check for RolePlay auto-continue
530
- const rpAutoCompactThreshold = ctx.CONFIG?.autoCompactThreshold || 100000;
551
+ const rpAutoCompactThreshold = state.autoCompactThreshold || ctx.CONFIG?.autoCompactThreshold || 110000;
531
552
  const rpEstimatedNewTokens = Math.ceil(prompt.length / 3);
532
553
  // Include output_tokens: the assistant's output becomes part of context for the next turn
533
554
  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 || 100000;
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.1.120",
3
+ "version": "0.1.122",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",