Repository navigation
fix(qoder): Cosy auth fallback for PAT tokens + vision support for qwen3-vl-plus #2629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,7 @@ import { | |||||||
| QODER_DEFAULT_USER_AGENT, | ||||||||
| } from "../config/providerHeaderProfiles.ts"; | ||||||||
| import { sanitizeQwenThinkingToolChoice } from "../services/qwenThinking.ts"; | ||||||||
| import { buildCosyHeadersForValidation } from "../services/qoderCli.ts"; | ||||||||
|
|
||||||||
| function getAuthToken(credentials: ProviderCredentials): string { | ||||||||
| if (typeof credentials.apiKey === "string" && credentials.apiKey.trim()) { | ||||||||
|
|
@@ -119,14 +120,73 @@ export class QoderExecutor extends BaseExecutor { | |||||||
| const bodyStr = JSON.stringify(payload); | ||||||||
|
|
||||||||
| try { | ||||||||
| const response = await fetch(endpointUrl, { | ||||||||
| let response = await fetch(endpointUrl, { | ||||||||
| method: "POST", | ||||||||
| headers, | ||||||||
| body: bodyStr, | ||||||||
| signal, | ||||||||
| }); | ||||||||
|
|
||||||||
| const newHeaders = new Headers(response.headers); | ||||||||
| // PAT tokens (pt-*) are not accepted as Bearer tokens by api.qoder.com/v1/chat/completions. | ||||||||
| // They return 401 TOKEN_INVALID. Fallback to Cosy auth against api1.qoder.sh. | ||||||||
| if (!response.ok && response.status === 401 && isPatToken) { | ||||||||
| const cosyHeaders = buildCosyHeadersForValidation(bodyStr, token); | ||||||||
| const cosyEndpoint = | ||||||||
| "https://api1.qoder.sh/algo/api/v2/service/pro/sse/agent_chat_generation?AgentId=agent_common"; | ||||||||
| const cosyRes = await fetch(cosyEndpoint, { | ||||||||
| method: "POST", | ||||||||
| headers: cosyHeaders, | ||||||||
| body: bodyStr, | ||||||||
| signal, | ||||||||
| }); | ||||||||
|
|
||||||||
| if (cosyRes.ok || cosyRes.status === 200) { | ||||||||
| // Cosy SSE response - read full body and parse | ||||||||
| const rawText = await cosyRes.text(); | ||||||||
| const lines = rawText.split("\n").filter(l => l.startsWith("data: ")); | ||||||||
| let fullContent = ""; | ||||||||
| for (const line of lines) { | ||||||||
| try { | ||||||||
| const jsonData = JSON.parse(line.slice(6)); | ||||||||
| const { extractTextFromQoderEnvelope } = await import("../services/qoderCli.ts"); | ||||||||
| const chunkText = extractTextFromQoderEnvelope(jsonData); | ||||||||
|
Comment on lines
+151
to
+152
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dynamic imports inside a loop are highly inefficient as they trigger module resolution on every iteration. Use the static import suggested at the top of the file.
Suggested change
|
||||||||
| if (chunkText) fullContent += chunkText; | ||||||||
| } catch { | ||||||||
| // skip unparseable chunks | ||||||||
| } | ||||||||
| } | ||||||||
| const { buildQoderCompletionPayload } = await import("../services/qoderCli.ts"); | ||||||||
| const cosyPayload = buildQoderCompletionPayload({ model: mappedModel || resolvedModel, text: fullContent }); | ||||||||
|
Comment on lines
+158
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace the dynamic import with a static one. Additionally,
Suggested change
|
||||||||
| return { | ||||||||
| response: new Response(JSON.stringify(cosyPayload), { | ||||||||
| status: 200, | ||||||||
| headers: { "Content-Type": "application/json" }, | ||||||||
| }), | ||||||||
| url: cosyEndpoint, | ||||||||
| headers: cosyHeaders, | ||||||||
| transformedBody: payload, | ||||||||
| }; | ||||||||
| } | ||||||||
|
Comment on lines
+143
to
+169
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fallback implementation buffers the entire SSE stream from the Cosy endpoint into |
||||||||
|
|
||||||||
| // Cosy also failed - return the original 401 error | ||||||||
| let errText = await cosyRes.text(); | ||||||||
| return { | ||||||||
| response: new Response( | ||||||||
| JSON.stringify({ | ||||||||
| error: { | ||||||||
| message: `Qoder API (Cosy) failed with status ${cosyRes.status}: ${errText}. Your PAT token may not be valid for the chat API.` + | ||||||||
| " Try using an OAuth token or a different auth method.", | ||||||||
| type: "authentication_error", | ||||||||
| code: "token_invalid", | ||||||||
| }, | ||||||||
| }), | ||||||||
| { status: 401, headers: { "Content-Type": "application/json" } } | ||||||||
| ), | ||||||||
| url: cosyEndpoint, | ||||||||
| headers: cosyHeaders, | ||||||||
| transformedBody: payload, | ||||||||
| }; | ||||||||
| } | ||||||||
|
|
||||||||
| if (!response.ok) { | ||||||||
| let errText = await response.text(); | ||||||||
|
|
@@ -146,6 +206,7 @@ export class QoderExecutor extends BaseExecutor { | |||||||
| }; | ||||||||
| } | ||||||||
|
|
||||||||
| const newHeaders = new Headers(response.headers); | ||||||||
| return { | ||||||||
| response: new Response(response.body, { | ||||||||
| status: response.status, | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functions
extractTextFromQoderEnvelopeandbuildQoderCompletionPayloadshould be imported statically at the top level. Dynamic imports during request execution, especially within loops, introduce unnecessary performance overhead and latency.