Skip to content

Commit 1bd8afd

Browse files
committed
feat: Improve non-streaming translation and token usage mapping
1 parent e50725d commit 1bd8afd

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/routes/messages/non-stream-translation.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ function mapContent(
166166
return null
167167
}
168168

169+
const hasImage = content.some((block) => block.type === "image")
170+
if (!hasImage) {
171+
return content
172+
.filter((block): block is AnthropicTextBlock => block.type === "text")
173+
.map((block) => block.text)
174+
.join("\n\n")
175+
}
176+
169177
const contentParts: Array<ContentPart> = []
170178
for (const block of content) {
171179
if (block.type === "text") {
@@ -248,8 +256,8 @@ export function translateToAnthropic(
248256
stop_reason: mapOpenAIStopReasonToAnthropic(choice.finish_reason),
249257
stop_sequence: null,
250258
usage: {
251-
input_tokens: 1,
252-
output_tokens: 1,
259+
input_tokens: response.usage?.prompt_tokens ?? 0,
260+
output_tokens: response.usage?.completion_tokens ?? 0,
253261
},
254262
}
255263
}

0 commit comments

Comments
 (0)