@yeaft/webchat-agent 0.0.148 → 0.0.150

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.
Files changed (3) hide show
  1. package/claude.js +13 -6
  2. package/crew.js +16 -7
  3. package/package.json +1 -1
package/claude.js CHANGED
@@ -346,14 +346,21 @@ async function processClaudeOutput(conversationId, claudeQuery, state) {
346
346
 
347
347
  // 捕获 result 消息中的 usage 信息
348
348
  if (message.type === 'result') {
349
- // 累计 usage(无论是否重复,始终统计)
349
+ // 差值计算:usage 中的值是 query 实例级累计值
350
350
  if (message.usage) {
351
- state.usage.inputTokens += message.usage.input_tokens || 0;
352
- state.usage.outputTokens += message.usage.output_tokens || 0;
353
- state.usage.cacheRead += message.usage.cache_read_input_tokens || 0;
354
- state.usage.cacheCreation += message.usage.cache_creation_input_tokens || 0;
351
+ const inputDelta = (message.usage.input_tokens || 0) - (state.lastResultInputTokens || 0);
352
+ const outputDelta = (message.usage.output_tokens || 0) - (state.lastResultOutputTokens || 0);
353
+ if (inputDelta > 0) state.usage.inputTokens += inputDelta;
354
+ if (outputDelta > 0) state.usage.outputTokens += outputDelta;
355
+ state.usage.cacheRead += Math.max(0, (message.usage.cache_read_input_tokens || 0) - (state.lastResultCacheRead || 0));
356
+ state.usage.cacheCreation += Math.max(0, (message.usage.cache_creation_input_tokens || 0) - (state.lastResultCacheCreation || 0));
357
+ state.lastResultInputTokens = message.usage.input_tokens || 0;
358
+ state.lastResultOutputTokens = message.usage.output_tokens || 0;
359
+ state.lastResultCacheRead = message.usage.cache_read_input_tokens || 0;
360
+ state.lastResultCacheCreation = message.usage.cache_creation_input_tokens || 0;
355
361
  }
356
- state.usage.totalCostUsd += message.total_cost_usd || 0;
362
+ // total_cost_usd 是全局累计值,直接赋值
363
+ state.usage.totalCostUsd = message.total_cost_usd || 0;
357
364
 
358
365
  // 计算上下文使用百分比
359
366
  const inputTokens = message.usage?.input_tokens || 0;
package/crew.js CHANGED
@@ -977,7 +977,10 @@ async function createRoleQuery(session, roleName) {
977
977
  abortController,
978
978
  accumulatedText: '',
979
979
  turnActive: false,
980
- claudeSessionId: savedSessionId
980
+ claudeSessionId: savedSessionId,
981
+ lastCostUsd: 0,
982
+ lastInputTokens: 0,
983
+ lastOutputTokens: 0
981
984
  };
982
985
 
983
986
  session.roleStates.set(roleName, roleState);
@@ -1242,14 +1245,20 @@ async function processRoleOutput(session, roleName, roleQuery, roleState) {
1242
1245
  // ★ 修复2: 反向搜索该角色最后一条 streaming 消息并结束
1243
1246
  endRoleStreaming(session, roleName);
1244
1247
 
1245
- // 更新费用
1246
- if (message.total_cost_usd) {
1247
- session.costUsd += message.total_cost_usd;
1248
+ // 更新费用(差值计算:每个角色独立进程,total_cost_usd 是该角色的累计值)
1249
+ if (message.total_cost_usd != null) {
1250
+ const costDelta = message.total_cost_usd - roleState.lastCostUsd;
1251
+ if (costDelta > 0) session.costUsd += costDelta;
1252
+ roleState.lastCostUsd = message.total_cost_usd;
1248
1253
  }
1249
- // 更新 token 用量
1254
+ // 更新 token 用量(差值计算:usage 是 query 实例级累计值)
1250
1255
  if (message.usage) {
1251
- session.totalInputTokens += message.usage.input_tokens || 0;
1252
- session.totalOutputTokens += message.usage.output_tokens || 0;
1256
+ const inputDelta = (message.usage.input_tokens || 0) - (roleState.lastInputTokens || 0);
1257
+ const outputDelta = (message.usage.output_tokens || 0) - (roleState.lastOutputTokens || 0);
1258
+ if (inputDelta > 0) session.totalInputTokens += inputDelta;
1259
+ if (outputDelta > 0) session.totalOutputTokens += outputDelta;
1260
+ roleState.lastInputTokens = message.usage.input_tokens || 0;
1261
+ roleState.lastOutputTokens = message.usage.output_tokens || 0;
1253
1262
  }
1254
1263
 
1255
1264
  // ★ 持久化 sessionId(每次 turn 完成后保存,用于后续 resume)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.0.148",
3
+ "version": "0.0.150",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",