Skip to content

Commit 6ff2a93

Browse files
committed
feat: add API protocol handling for providers and update related types
1 parent 1459ddc commit 6ff2a93

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

packages/types/src/message.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export const clineMessageSchema = z.object({
155155
progressStatus: toolProgressStatusSchema.optional(),
156156
contextCondense: contextCondenseSchema.optional(),
157157
isProtected: z.boolean().optional(),
158+
apiProtocol: z.union([z.literal("openai"), z.literal("anthropic")]).optional(),
158159
})
159160

160161
export type ClineMessage = z.infer<typeof clineMessageSchema>

packages/types/src/provider-settings.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,11 @@ export const getModelId = (settings: ProviderSettings): string | undefined => {
292292
const modelIdKey = MODEL_ID_KEYS.find((key) => settings[key])
293293
return modelIdKey ? (settings[modelIdKey] as string) : undefined
294294
}
295+
296+
// Providers that use Anthropic-style API protocol
297+
export const ANTHROPIC_STYLE_PROVIDERS: ProviderName[] = ["anthropic", "claude-code"]
298+
299+
// Helper function to determine API protocol for a provider
300+
export const getApiProtocol = (provider: ProviderName | undefined): "anthropic" | "openai" => {
301+
return provider && ANTHROPIC_STYLE_PROVIDERS.includes(provider) ? "anthropic" : "openai"
302+
}

src/core/task/Task.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
type HistoryItem,
2222
TelemetryEventName,
2323
TodoItem,
24+
getApiProtocol,
2425
} from "@roo-code/types"
2526
import { TelemetryService } from "@roo-code/telemetry"
2627
import { CloudService } from "@roo-code/cloud"
@@ -1207,11 +1208,16 @@ export class Task extends EventEmitter<ClineEvents> {
12071208
// top-down build file structure of project which for large projects can
12081209
// take a few seconds. For the best UX we show a placeholder api_req_started
12091210
// message with a loading spinner as this happens.
1211+
1212+
// Determine API protocol based on provider
1213+
const apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider)
1214+
12101215
await this.say(
12111216
"api_req_started",
12121217
JSON.stringify({
12131218
request:
12141219
userContent.map((block) => formatContentBlockToMarkdown(block)).join("\n\n") + "\n\nLoading...",
1220+
apiProtocol,
12151221
}),
12161222
)
12171223

@@ -1243,6 +1249,7 @@ export class Task extends EventEmitter<ClineEvents> {
12431249

12441250
this.clineMessages[lastApiReqIndex].text = JSON.stringify({
12451251
request: finalUserContent.map((block) => formatContentBlockToMarkdown(block)).join("\n\n"),
1252+
apiProtocol,
12461253
} satisfies ClineApiReqInfo)
12471254

12481255
await this.saveClineMessages()
@@ -1263,8 +1270,9 @@ export class Task extends EventEmitter<ClineEvents> {
12631270
// of prices in tasks from history (it's worth removing a few months
12641271
// from now).
12651272
const updateApiReqMsg = (cancelReason?: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
1273+
const existingData = JSON.parse(this.clineMessages[lastApiReqIndex].text || "{}")
12661274
this.clineMessages[lastApiReqIndex].text = JSON.stringify({
1267-
...JSON.parse(this.clineMessages[lastApiReqIndex].text || "{}"),
1275+
...existingData,
12681276
tokensIn: inputTokens,
12691277
tokensOut: outputTokens,
12701278
cacheWrites: cacheWriteTokens,

src/shared/ExtensionMessage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ export interface ClineApiReqInfo {
379379
cost?: number
380380
cancelReason?: ClineApiReqCancelReason
381381
streamingFailedMessage?: string
382+
apiProtocol?: "anthropic" | "openai"
382383
}
383384

384385
export type ClineApiReqCancelReason = "streaming_failed" | "user_cancelled"

src/shared/getApiMetrics.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type ParsedApiReqStartedTextType = {
66
cacheWrites: number
77
cacheReads: number
88
cost?: number // Only present if combineApiRequests has been called
9+
apiProtocol?: "anthropic" | "openai"
910
}
1011

1112
/**
@@ -72,8 +73,17 @@ export function getApiMetrics(messages: ClineMessage[]) {
7273
if (message.type === "say" && message.say === "api_req_started" && message.text) {
7374
try {
7475
const parsedText: ParsedApiReqStartedTextType = JSON.parse(message.text)
75-
const { tokensIn, tokensOut } = parsedText
76-
result.contextTokens = (tokensIn || 0) + (tokensOut || 0)
76+
const { tokensIn, tokensOut, cacheWrites, cacheReads, apiProtocol } = parsedText
77+
78+
console.log("APi Protocol:", apiProtocol)
79+
80+
// Calculate context tokens based on API protocol
81+
if (apiProtocol === "openai") {
82+
result.contextTokens = (tokensIn || 0) + (tokensOut || 0)
83+
} else {
84+
// For Anthropic (or when protocol is not specified)
85+
result.contextTokens = (tokensIn || 0) + (tokensOut || 0) + (cacheWrites || 0) + (cacheReads || 0)
86+
}
7787
} catch (error) {
7888
console.error("Error parsing JSON:", error)
7989
continue

0 commit comments

Comments
 (0)