Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/controllers/anthropic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1262,8 +1262,6 @@ const handleAnthropicStream = async (res, ctx, upstream) => {
let textBlockOpen = false;
let thinkingBlockOpen = false;
let thinkingSignature = null;
let promptTokens = 0;
let completionTokens = 0;
let upstreamUsage = null; // 上游逐帧累计的 usage(DashScope 命名已归一化;null = 还没报)
let upstreamFinishReason = null;
let upstreamCompleted;
Expand Down Expand Up @@ -2001,8 +1999,8 @@ const handleAnthropicStream = async (res, ctx, upstream) => {

// 只对上游没报的字段补本地估算(早停的回合收不到尾部 usage 帧)
const usage = reportUsage(upstreamUsage, () => createUsageObject(requestBody?.messages || '', completionContent), 'ANTHROPIC');
promptTokens = usage.prompt_tokens;
completionTokens = usage.completion_tokens;
const promptTokens = usage.prompt_tokens;
const completionTokens = usage.completion_tokens;

// Daily stats 累计——一次性归属主账户(见模块顶部 attributeChatUsage 注释)
attributeChatUsage(ctx.currentAccount, promptTokens, completionTokens);
Expand Down Expand Up @@ -2041,8 +2039,6 @@ const handleAnthropicNonStream = async (res, ctx, upstream) => {
// 与流式分支同一条纪律,上一轮的泄漏已经重试过了。
let attemptThinkingContent = '';
let answerContent = '';
let promptTokens = 0;
let completionTokens = 0;
let upstreamUsage = null; // 上游逐帧累计的 usage(DashScope 命名已归一化;null = 还没报)
let webSearchInfo = null;
let upstreamFinishReason = null;
Expand Down Expand Up @@ -2577,8 +2573,8 @@ const handleAnthropicNonStream = async (res, ctx, upstream) => {
const nativeArgsText = nativeToolCalls.map(call => call.function.arguments || '').join('');
return createUsageObject(requestBody?.messages || '', thinkingContent + answerContent + nativeArgsText);
}, 'ANTHROPIC');
promptTokens = usage.prompt_tokens;
completionTokens = usage.completion_tokens;
const promptTokens = usage.prompt_tokens;
const completionTokens = usage.completion_tokens;

const contentBlocks = [];
if (thinkingContent && thinkingContent.trim()) {
Expand Down
6 changes: 5 additions & 1 deletion tests/tool-prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,11 @@ test('lockstep: ningun sitio de prompt/hint re-ensena la forma nativa <tool_call
const code = src
.replace(/\/\*[\s\S]*?\*\//g, '')
.split('\n')
.map(line => line.replace(/\/\/.*$/, ''))
// Bounded by the newline, not by `$`: on a CRLF checkout `$` matches before
// the CR, so `.*` backtracked to the CR and left the comment body in `code`
// — the rationale comments that legitimately name `<tool_call>` then failed
// the scan below.
.map(line => line.replace(/\/\/[^\r\n]*/, ''))
.join('\n')
assert.doesNotMatch(code, /<[ \t]*\/?[ \t]*tool_call[ >]/i, `${rel}: cadena con la forma nativa <tool_call> legible por el modelo`)
}
Expand Down