feat(opencode): add external browser OAuth for snowflake cortex provider (#31700)

Co-authored-by: santiago.gonzalezcarvajcentenera <santiago.gonzalezcarvajcentenera@colaborador.elcorteingles.es>
Co-authored-by: David Fierro <14184197+davidfierro@users.noreply.github.com>
Co-authored-by: Kamesh Sampath <kamesh.sampath@hotmail.com>
Co-authored-by: Cortex Code <noreply@snowflake.com>
This commit is contained in:
santigc6
2026-06-13 20:09:45 +02:00
committed by GitHub
parent fcca731a96
commit 3f174531b9
8 changed files with 977 additions and 94 deletions
+74 -62
View File
@@ -853,17 +853,23 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
const account =
env["SNOWFLAKE_ACCOUNT"] ??
(auth?.type === "api" ? auth.metadata?.account : undefined) ??
(auth?.type === "oauth" ? auth.accountId : undefined) ??
input.options?.account
const pat = env["SNOWFLAKE_CORTEX_PAT"] ?? (auth?.type === "api" ? auth.key : undefined) ?? input.options?.apiKey
const envToken = env["SNOWFLAKE_CORTEX_TOKEN"] ?? env["SNOWFLAKE_CORTEX_PAT"]
const apiKeyToken = auth?.type === "api" ? auth.key : undefined
const oauthToken = auth?.type === "oauth" ? auth.access : undefined
const configToken = input.options?.token ?? input.options?.apiKey
if (!account || !pat) {
const missing = [!account && "SNOWFLAKE_ACCOUNT", !pat && "SNOWFLAKE_CORTEX_PAT"].filter(Boolean).join(", ")
const token = envToken ?? apiKeyToken ?? oauthToken ?? configToken
if (!account || !token) {
const missing = [!account && "SNOWFLAKE_ACCOUNT", !token && "SNOWFLAKE_CORTEX_TOKEN"].filter(Boolean).join(", ")
return {
autoload: false,
async getModel() {
throw new Error(
`Snowflake Cortex: missing credentials (${missing}). Set via env var, opencode auth, or provider options.`,
`Snowflake Cortex: missing credentials (${missing}). Provide a bearer token (OAuth, JWT, or PAT) via env var, opencode auth, or provider options.`,
)
},
}
@@ -871,66 +877,72 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
const baseURL = `https://${account}.snowflakecomputing.com/api/v2/cortex/v1`
const options: Record<string, any> = { baseURL, apiKey: token }
// Only skip provider-level fetch when the token is from OAuth with no override.
// For OAuth tokens, the plugin auth loader's combined fetch handles
// OAuth refresh + snowflake transformations in one place.
// For env/config/API-key tokens, the provider fetch applies snowflake
// transformations directly.
const useOAuthHandler = oauthToken !== undefined && envToken === undefined && apiKeyToken === undefined && configToken === undefined
if (!useOAuthHandler) {
options.fetch = async (url: RequestInfo | URL, init?: RequestInit) => {
if (init?.body && typeof init.body === "string") {
try {
const body = JSON.parse(init.body)
if ("max_tokens" in body) {
body.max_completion_tokens = body.max_tokens
delete body.max_tokens
init = { ...init, body: JSON.stringify(body) }
}
} catch {}
}
const response = await fetch(url, init)
if (!response.ok && response.status === 400) {
try {
const errorData = await response.clone().json()
const errorMessage = String(errorData.message || errorData.error || "")
if (errorMessage.toLowerCase().includes("conversation complete")) {
return new Response(
JSON.stringify({
choices: [{ finish_reason: "stop", message: { content: "", role: "assistant" } }],
}),
{ status: 200, headers: new Headers({ "content-type": "application/json" }) },
)
}
} catch {}
}
if (response.body && response.headers.get("content-type")?.includes("text/event-stream")) {
const reader = response.body.getReader()
const encoder = new TextEncoder()
const decoder = new TextDecoder()
const stream = new ReadableStream({
async pull(ctrl) {
const { done, value } = await reader.read()
if (done) {
ctrl.close()
return
}
const text = decoder.decode(value, { stream: true })
ctrl.enqueue(encoder.encode(text.replace(/"role"\s*:\s*""/g, '"role":"assistant"')))
},
cancel() {
reader.cancel()
},
})
return new Response(stream, { headers: response.headers, status: response.status })
}
return response
}
}
return {
autoload: input.source === "config",
options: {
baseURL,
apiKey: pat,
fetch: async (url: RequestInfo | URL, init?: RequestInit) => {
if (init?.body && typeof init.body === "string") {
try {
const body = JSON.parse(init.body)
if ("max_tokens" in body) {
body.max_completion_tokens = body.max_tokens
delete body.max_tokens
init = { ...init, body: JSON.stringify(body) }
}
} catch {}
}
const response = await fetch(url, init)
// Cortex returns 400 "conversation complete" as a normal stop condition
if (!response.ok && response.status === 400) {
try {
const errorData = await response.clone().json()
const errorMessage = String(errorData.message || errorData.error || "")
if (errorMessage.toLowerCase().includes("conversation complete")) {
return new Response(
JSON.stringify({
choices: [{ finish_reason: "stop", message: { content: "", role: "assistant" } }],
}),
{ status: 200, headers: new Headers({ "content-type": "application/json" }) },
)
}
} catch {}
}
// Cortex returns role:"" in streaming deltas; the AI SDK schema requires "assistant"
if (response.body && response.headers.get("content-type")?.includes("text/event-stream")) {
const reader = response.body.getReader()
const encoder = new TextEncoder()
const decoder = new TextDecoder()
const stream = new ReadableStream({
async pull(ctrl) {
const { done, value } = await reader.read()
if (done) {
ctrl.close()
return
}
const text = decoder.decode(value, { stream: true })
ctrl.enqueue(encoder.encode(text.replace(/"role"\s*:\s*""/g, '"role":"assistant"')))
},
cancel() {
reader.cancel()
},
})
return new Response(stream, { headers: response.headers, status: response.status })
}
return response
},
},
options,
}
}),
}