Skip to content
Merged
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
62 changes: 62 additions & 0 deletions packages/console/app/src/routes/zen/util/provider/gatewayz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { ProviderHelper, CommonRequest, CommonResponse, CommonChunk } from "./provider"

type Usage = {
prompt_tokens?: number
completion_tokens?: number
total_tokens?: number
prompt_tokens_details?: {
cached_tokens?: number
}
completion_tokens_details?: {
reasoning_tokens?: number
}
}

export const gatewayzHelper = {
format: "oa-compat",
modifyUrl: (providerApi: string) => providerApi + "/v1/chat/completions",
modifyHeaders: (headers: Headers, body: Record<string, any>, apiKey: string) => {
headers.set("authorization", `Bearer ${apiKey}`)
},
modifyBody: (body: Record<string, any>) => {
return {
...body,
...(body.stream ? { stream_options: { include_usage: true } } : {}),
}
},
streamSeparator: "\n\n",
createUsageParser: () => {
let usage: Usage

return {
parse: (chunk: string) => {
if (!chunk.startsWith("data: ")) return

let json
try {
json = JSON.parse(chunk.slice(6)) as { usage?: Usage }
} catch (e) {
return
}

if (!json.usage) return
usage = json.usage
},
retrieve: () => usage,
}
},
normalizeUsage: (usage: Usage) => {
const inputTokens = usage.prompt_tokens ?? 0
const outputTokens = usage.completion_tokens ?? 0
const reasoningTokens = usage.completion_tokens_details?.reasoning_tokens ?? undefined
const cacheReadTokens = usage.prompt_tokens_details?.cached_tokens ?? undefined
return {
inputTokens: inputTokens - (cacheReadTokens ?? 0),
outputTokens,
reasoningTokens,
cacheReadTokens,
cacheWrite5mTokens: undefined,
cacheWrite1hTokens: undefined,
}
},
} satisfies ProviderHelper
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
toOaCompatibleRequest,
toOaCompatibleResponse,
} from "./openai-compatible"
import { gatewayzHelper } from "./gatewayz"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Gatewayz helper imported but never used

The gatewayzHelper is imported but never used or exported, meaning Gatewayz providers will incorrectly fall back to oaCompatHelper in the handler. This causes Gatewayz API calls to use the wrong endpoint path (/chat/completions instead of /v1/chat/completions), resulting in failed requests since Gatewayz requires the /v1 prefix according to the PR description.

Fix in Cursor Fix in Web


Comment on lines 23 to 27

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Wire gatewayzHelper into provider selection

The new helper is imported but never used, so selectProvider still returns oaCompatHelper for every provider whose format is "oa-compat". When a Gatewayz provider is configured (base URL https://api.gatewayz.ai), requests will still call modifyUrl from oaCompatHelper and hit /chat/completions instead of the /v1/chat/completions endpoint defined in gatewayz.ts, and any future Gatewayz‑specific header logic will also be skipped. As a result the new provider will 404 or fail auth. This helper needs to be explicitly selected when the provider ID is Gatewayz.

Useful? React with 👍 / 👎.

export type ProviderHelper = {
format: ZenData.Format
Expand Down