Elffuss BraidHub

kikocis/continue public

feat: return usage for openai providers
Changeset3c52fb4450251184f0baf779742726179e8c0adeff27f889258b2e8d548d856e
AuthorTing-Wai To <40179554+tingwai@users.noreply.github.com> · 2025-08-28 11:46 -07:00
CommitterTing-Wai To <40179554+tingwai@users.noreply.github.com> · 2025-08-28 11:46 -07:00
Parents7a6a62e41ecd

Browse the files at this changeset

3 files changed

modified packages/openai-adapters/src/apis/Gemini.ts +29 −2
@@ -22,6 +22,7 @@
2222 chatChunkFromDelta,
2323 customFetch,
2424 embedding,
25+ usageChatChunk,
2526 } from "../util.js";
2627 import {
2728 convertOpenAIToolToGeminiFunction,
@@ -37,6 +38,8 @@
3738 RerankCreateParams,
3839 } from "./base.js";
3940
41+type UsageInfo = { total_tokens: number; completion_tokens: number; prompt_tokens: number };
42+
4043 export class GeminiApi implements BaseLlmApi {
4144 apiBase: string = "https://generativelanguage.googleapis.com/v1beta/";
4245
@@ -240,6 +243,7 @@
240243 signal: AbortSignal,
241244 ): Promise<ChatCompletion> {
242245 let completion = "";
246+ let usage: UsageInfo | undefined = undefined;
243247 for await (const chunk of this.chatCompletionStream(
244248 {
245249 ...body,
@@ -247,7 +251,12 @@
247251 },
248252 signal,
249253 )) {
250- completion += chunk.choices[0].delta.content;
254+ if (chunk.choices.length > 0) {
255+ completion += chunk.choices[0].delta.content || "";
256+ }
257+ if (chunk.usage) {
258+ usage = chunk.usage;
259+ }
251260 }
252261 return {
253262 id: "",
@@ -266,12 +275,13 @@
266275 },
267276 },
268277 ],
269- usage: undefined,
278+ usage,
270279 };
271280 }
272281
273282 async *handleStreamResponse(response: any, model: string) {
274283 let buffer = "";
284+ let usage: UsageInfo | undefined = undefined;
275285 for await (const chunk of streamResponse(response as any)) {
276286 buffer += chunk;
277287 if (buffer.startsWith("[")) {
@@ -300,6 +310,15 @@
300310 throw new Error(data.error.message);
301311 }
302312
313+ // Check for usage metadata
314+ if (data.usageMetadata) {
315+ usage = {
316+ prompt_tokens: data.usageMetadata.promptTokenCount || 0,
317+ completion_tokens: data.usageMetadata.candidatesTokenCount || 0,
318+ total_tokens: data.usageMetadata.totalTokenCount || 0,
319+ };
320+ }
321+
303322 // In case of max tokens reached, gemini will sometimes return content with no parts, even though that doesn't match the API spec
304323 const contentParts = data?.candidates?.[0]?.content?.parts;
305324 if (contentParts) {
@@ -338,6 +357,14 @@
338357 buffer = "";
339358 }
340359 }
360+
361+ // Emit usage at the end if we have it
362+ if (usage) {
363+ yield usageChatChunk({
364+ model,
365+ usage,
366+ });
367+ }
341368 }
342369
343370 async *chatCompletionStream(
modified packages/openai-adapters/src/apis/Relace.ts +25 −2
@@ -16,7 +16,12 @@
1616 import { Model } from "openai/resources/models.mjs";
1717 import { z } from "zod";
1818 import { OpenAIConfigSchema } from "../types.js";
19-import { chatChunk, chatCompletion, customFetch } from "../util.js";
19+import {
20+ chatChunk,
21+ chatCompletion,
22+ customFetch,
23+ usageChatChunk,
24+} from "../util.js";
2025 import {
2126 BaseLlmApi,
2227 CreateRerankResponse,
@@ -24,6 +29,8 @@
2429 RerankCreateParams,
2530 } from "./base.js";
2631
32+type UsageInfo = { total_tokens: number; completion_tokens: number; prompt_tokens: number };
33+
2734 // Relace only supports apply through a /v1/apply endpoint
2835 export class RelaceApi implements BaseLlmApi {
2936 private apiBase = "https://instantapply.endpoint.relace.run/v1/";
@@ -41,6 +48,7 @@
4148 signal: AbortSignal,
4249 ): Promise<ChatCompletion> {
4350 let content = "";
51+ let usage: UsageInfo | undefined = undefined;
4452
4553 // Convert the non-streaming params to streaming params
4654 const streamingBody: ChatCompletionCreateParamsStreaming = {
@@ -52,12 +60,18 @@
5260 streamingBody,
5361 signal,
5462 )) {
55- content += chunk.choices[0]?.delta?.content || "";
63+ if (chunk.choices.length > 0) {
64+ content += chunk.choices[0]?.delta?.content || "";
65+ }
66+ if (chunk.usage) {
67+ usage = chunk.usage;
68+ }
5669 }
5770
5871 return chatCompletion({
5972 content,
6073 model: body.model,
74+ usage,
6175 });
6276 }
6377
@@ -115,6 +129,15 @@
115129 content: mergedCode,
116130 model: body.model,
117131 });
132+
133+ yield usageChatChunk({
134+ model: body.model,
135+ usage: {
136+ prompt_tokens: result.usage.prompt_tokens || 0,
137+ completion_tokens: result.usage.completion_tokens || 0,
138+ total_tokens: result.usage.total_tokens,
139+ },
140+ });
118141 }
119142
120143 completionNonStream(
modified packages/openai-adapters/src/test/main.test.ts +58 −7
@@ -51,6 +51,20 @@
5151 model: "gpt-4o",
5252 apiKey: process.env.OPENAI_API_KEY!,
5353 roles: ["chat"],
54+ options: {
55+ skipTools: false,
56+ expectUsage: true,
57+ },
58+ },
59+ {
60+ provider: "openai",
61+ model: "gpt-4o-mini",
62+ apiKey: process.env.OPENAI_API_KEY!,
63+ roles: ["chat"],
64+ options: {
65+ skipTools: false,
66+ expectUsage: true,
67+ },
5468 },
5569 {
5670 provider: "anthropic",
@@ -67,15 +81,32 @@
6781 model: "gemini-1.5-flash-latest",
6882 apiKey: process.env.GEMINI_API_KEY!,
6983 roles: ["chat"],
84+ options: {
85+ skipTools: false,
86+ expectUsage: true,
87+ },
88+ },
89+ {
90+ provider: "gemini",
91+ model: "gemini-2.5-flash",
92+ apiKey: process.env.GEMINI_API_KEY!,
93+ roles: ["chat"],
94+ options: {
95+ skipTools: false,
96+ expectUsage: true,
97+ },
98+ },
99+ {
100+ provider: "mistral",
101+ model: "codestral",
102+ apiKey: process.env.MISTRAL_API_KEY!,
103+ roles: ["chat"],
104+ options: {
105+ skipTools: false,
106+ expectUsage: true,
107+ },
70108 },
71109 // {
72- // provider: "mistral",
73- // model: "codestral-latest",
74- // apiKey: process.env.MISTRAL_API_KEY!,
75- // apiBase: "https://api.mistral.ai/v1",
76- // roles: ["autocomplete"],
77- // },
78- // {
79110 // provider: "deepseek",
80111 // model: "deepseek-coder",
81112 // apiKey: process.env.DEEPSEEK_API_KEY!,
@@ -111,6 +142,26 @@
111142 apiKey: process.env.VOYAGE_API_KEY!,
112143 roles: ["rerank"],
113144 },
145+ {
146+ provider: "relace",
147+ model: "instant-apply",
148+ apiKey: process.env.RELACE_API_KEY!,
149+ roles: ["chat"],
150+ options: {
151+ skipTools: true,
152+ expectUsage: true,
153+ },
154+ },
155+ {
156+ provider: "inception",
157+ model: "mercury-coder",
158+ apiKey: process.env.INCEPTION_API_KEY!,
159+ roles: ["chat"],
160+ options: {
161+ skipTools: false,
162+ expectUsage: true,
163+ },
164+ },
114165 // {
115166 // provider: "cohere",
116167 // model: "rerank-v3.5",