import { type AssistantMessage, type Context, type Api, type Model, type SimpleStreamOptions as PiSimpleStreamOptions, type StopReason, type Tool, type ToolCall, calculateCost, createAssistantMessageEventStream, } from "@earendil-works/pi-ai"; import * as piAi from "@earendil-works/pi-ai"; import { buildCodeAssistRequest, formatCodeAssistError, parseCodeAssistSse, unwrapCodeAssistBody } from "./code-assist"; import { PROVIDER_ID } from "./constants"; import { resolveProjectId, saveUserProjectIdConfig } from "./config"; import { refreshAccessToken, type OAuthCredentials } from "./oauth"; import { getCodeAssistRetryDelayMs, getRetryCooldownMs, isRetryableNetworkError, recordRetryCooldown, shouldRetryCodeAssistResponse, wait, } from "./retry"; import { resolveEffectiveProjectId } from "./project"; import { createGeminiDebugLogger, type GeminiDebugLogger } from "./debug"; type GeminiCliOAuthApi = "gemini-cli-oauth-api"; // pi-ai 0.87 把 ToolChoice 收窄成 "auto" | "none";本插件额外支持 Gemini 的 ANY,所以不能直接 extends。 interface SimpleStreamOptions extends Omit { cwd?: string; homeDir?: string; toolChoice?: "auto" | "none" | "any"; includeThoughts?: boolean; thinking?: { budgetTokens?: number; level?: string; includeThoughts?: boolean; enabled?: boolean }; oauthCredentials?: OAuthCredentials; disableManagedProject?: boolean; onCredentialsRefreshed?: (credentials: OAuthCredentials) => void | Promise; debugLogPath?: string; } interface GeminiResponsePart { text?: string; thought?: boolean; functionCall?: { id?: string; name?: string; args?: Record; }; thoughtSignature?: string; } interface GeminiResponseBody { candidates?: Array<{ content?: { parts?: GeminiResponsePart[] }; finishReason?: string; }>; usageMetadata?: { promptTokenCount?: number; candidatesTokenCount?: number; cachedContentTokenCount?: number; thoughtsTokenCount?: number; totalTokenCount?: number; }; traceId?: string; responseId?: string; } interface TextStreamState { contentIndex?: number; type?: "text" | "thinking"; text: string; /** 是否收到过 finishReason,以及原样的值(报错时带上,空回复是被拦还是模型自己收工才分得开)。 */ finished?: boolean; finishReason?: string; /** 是否收到过 thought:关闭 thinking 输出时它不进 content,只能另记。 */ sawThought?: boolean; } /** * 没有工具调用、却也没给出正文或 finishReason 的回复是坏回复,判据照抄 gemini-cli(geminiChat 的 * NO_FINISH_REASON/NO_RESPONSE_TEXT,它对这两种都重试)。真机形状(gemini-3.8-flash,约 0.6% 的 * 请求):只回了一段 thought、usage 全 0。原来按 stop 报,pi 看到「没有工具调用的正常结束」就收工, * 整轮零产物。0.1.3 只认「finishReason 与 usage 都缺」,真机证明这种回复带着 finishReason,没拦住。 * 报成 error 且文案命中 pi-ai 的可重试规则("ended without"),pi 原地重发这一次请求。 * 只改写 stop:length、toolUse、SAFETY 等已有的结束原因原样保留。 * 0.1.6 起文案后面带上原样的 finishReason、有没有 thought、输出 token 数:认不得的结束原因(RECITATION、 * PROHIBITED_CONTENT、OTHER…)都按 stop 处理,原来一律报成「only thoughts」——真机上连 thought 都没有、 * 输出 0 token、同一请求 4 次空回,文案却说只有思考,是被拦还是模型自己收工无从分辨。 */ export const TRUNCATED_STREAM_ERROR = "Gemini stream ended without finishReason (response truncated)"; export const EMPTY_RESPONSE_ERROR = "Gemini stream ended without response text"; export function streamGeminiCliOAuth( model: Model, context: Context, options: SimpleStreamOptions = {}, ) { const stream = createAssistantMessageEventStream(); (async () => { try { if (!options.apiKey) { throw new Error("缺少 Gemini CLI OAuth access token。请先运行 /login gemini-cli-oauth。"); } let accessToken = options.apiKey; let oauthCredentials = options.oauthCredentials; if (shouldRefreshOauthCredentials(oauthCredentials)) { oauthCredentials = await refreshAccessToken(oauthCredentials); accessToken = oauthCredentials.access; await options.onCredentialsRefreshed?.(oauthCredentials); } const debugLogger = createGeminiDebugLogger({ env: options.env, logPath: options.debugLogPath }); const configuredProjectId = resolveProjectId({ env: options.env, cwd: options.cwd, homeDir: options.homeDir }); const projectId = await resolveEffectiveProjectId({ configuredProjectId, accessToken, modelId: model.id, disableManagedProject: options.disableManagedProject, onManagedProjectResolved: (managedProjectId) => { saveUserProjectIdConfig(managedProjectId, { homeDir: options.homeDir }); }, }); if (!projectId) { throw new Error( "缺少 projectId。请设置 PI_GEMINI_CLI_PROJECT_ID,或在 .pi/gemini-cli-oauth.json 中配置 projectId。", ); } const output = createBaseMessage(model); stream.push({ type: "start", partial: output }); const generationConfig = buildGenerationConfig(model.id, options); const { systemPrompt, tools } = resolvePromptAndTools(context); const basePayload = { systemInstruction: systemPrompt ? { parts: [{ text: systemPrompt }] } : undefined, contents: convertContextMessages(context, model), tools: tools.length ? convertContextTools(tools) : undefined, toolConfig: tools.length && options.toolChoice ? { functionCallingConfig: { mode: mapToolChoice(options.toolChoice) } } : undefined, ...(generationConfig ? { generationConfig } : {}), }; const payload = await applyPayloadHook(basePayload, model, options); const response = await sendCodeAssistRequestWithRecovery({ modelId: model.id, projectId, payload, accessToken, oauthCredentials, signal: options.signal, maxRetries: options.maxRetries, maxRetryDelayMs: options.maxRetryDelayMs, headers: options.headers, onCredentialsRefreshed: options.onCredentialsRefreshed, debugLogger, }); await callResponseHook(response, model, options); if (!response.ok) { const errorBody = await response.text(); throw new Error(formatCodeAssistError(response, errorBody)); } const textState: TextStreamState = { text: "" }; const includeThoughts = shouldIncludeThoughts(options); if (response.body) { await applyGeminiSseStream(model, output, response.body, stream, textState, includeThoughts); } else { const text = await response.text(); applyGeminiSseText(model, output, text, stream, textState, includeThoughts); } finalizeTextBlock(output, stream, textState); if (options.signal?.aborted) { output.stopReason = "aborted"; } else if (output.stopReason === "stop" && !output.content.some((block) => block.type === "toolCall")) { const hasText = output.content.some((block) => block.type === "text" && block.text.trim() !== ""); if (!textState.finished || !hasText) { const received = hasText ? "has text" : textState.sawThought ? "only thoughts" : "no thoughts"; const detail = `${received}, output tokens ${output.usage.output}`; output.stopReason = "error"; output.errorMessage = textState.finished ? `${EMPTY_RESPONSE_ERROR} (finishReason ${textState.finishReason}, ${detail})` : `${TRUNCATED_STREAM_ERROR}: ${detail}`; } } stream.push({ type: "done", reason: getDoneReason(output.stopReason), message: output } as never); stream.end(output); } catch (error) { const output = createBaseMessage(model); output.stopReason = options.signal?.aborted ? "aborted" : "error"; output.errorMessage = error instanceof Error ? error.message : String(error); stream.push({ type: "error", reason: output.stopReason, error: output }); stream.end(output); } })(); return stream; } interface CodeAssistSendInput { modelId: string; projectId: string; payload: unknown; accessToken: string; oauthCredentials?: OAuthCredentials; signal?: AbortSignal; maxRetries?: number; maxRetryDelayMs?: number; headers?: SimpleStreamOptions["headers"]; onCredentialsRefreshed?: (credentials: OAuthCredentials) => void | Promise; debugLogger?: GeminiDebugLogger; } async function sendCodeAssistRequestWithRecovery(input: CodeAssistSendInput): Promise { let accessToken = input.accessToken; let credentials = input.oauthCredentials; let refreshedAfter401 = false; let attempt = 0; const maxRetries = input.maxRetries ?? 2; const maxRetryDelayMs = input.maxRetryDelayMs ?? 60_000; while (true) { try { const request = buildCodeAssistRequest({ modelId: input.modelId, projectId: input.projectId, payload: input.payload, stream: true, accessToken, headers: input.headers }); const cooldownKey = `${request.url}|${input.projectId}|${input.modelId}`; const cooldownMs = getRetryCooldownMs(cooldownKey); if (cooldownMs > 0) { input.debugLogger?.logRetry({ attempt, delayMs: cooldownMs, reason: "cooldown" }); await wait(cooldownMs); } const startedAt = Date.now(); input.debugLogger?.logRequest({ url: request.url, method: "POST", headers: request.headers, body: request.body, projectId: input.projectId, modelId: input.modelId }); const response = await fetch(request.url, { method: "POST", headers: request.headers, body: JSON.stringify(request.body), signal: input.signal, }); input.debugLogger?.logResponse({ status: response.status, statusText: response.statusText, traceId: response.headers.get("x-cloudaicompanion-trace-id") ?? undefined, durationMs: Date.now() - startedAt, }); if (response.status === 401 && credentials?.refresh && !refreshedAfter401) { refreshedAfter401 = true; credentials = await refreshAccessToken(credentials); accessToken = credentials.access; await input.onCredentialsRefreshed?.(credentials); continue; } if (await shouldRetryCodeAssistResponse(response, attempt, maxRetries)) { const delayMs = await getCodeAssistRetryDelayMs(response, attempt, maxRetryDelayMs); if (response.status === 429) recordRetryCooldown(cooldownKey, delayMs); input.debugLogger?.logRetry({ attempt: attempt + 1, delayMs, reason: response.statusText || String(response.status) }); await wait(delayMs); attempt += 1; continue; } return response; } catch (error) { if (attempt >= maxRetries || !isRetryableNetworkError(error)) throw error; const delayMs = Math.min(1000 * 2 ** attempt, maxRetryDelayMs); input.debugLogger?.logRetry({ attempt: attempt + 1, delayMs, reason: error instanceof Error ? error.message : String(error) }); await wait(delayMs); attempt += 1; } } } function createBaseMessage(model: Model): AssistantMessage { return { role: "assistant", content: [], api: model.api, provider: model.provider, model: model.id, usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, }, stopReason: "stop", timestamp: Date.now(), }; } function applyGeminiBodyToMessage( model: Model, output: AssistantMessage, rawBody: GeminiResponseBody, stream: ReturnType, textState: TextStreamState, includeThoughts: boolean, ): void { const body = unwrapCodeAssistBody(rawBody) as GeminiResponseBody; const parts = body.candidates?.[0]?.content?.parts ?? []; for (const part of parts) { if (part.text !== undefined) { if (part.thought === true) { textState.sawThought = true; if (includeThoughts) appendThinking(output, stream, textState, part.text, part.thoughtSignature); } else appendText(output, stream, textState, part.text); } } for (const part of parts) { if (part.functionCall) { finalizeTextBlock(output, stream, textState); textState.contentIndex = undefined; textState.text = ""; appendToolCall(output, stream, part); } } const candidate = body.candidates?.[0]; if (candidate?.finishReason) { textState.finished = true; textState.finishReason = candidate.finishReason; output.stopReason = mapGeminiStopReason(candidate.finishReason); if (output.stopReason === "error") output.errorMessage = `Gemini finishReason: ${candidate.finishReason}`; } const responseId = body.responseId ?? body.traceId; if (responseId) output.responseId = responseId; if (body.usageMetadata) { output.usage.input = body.usageMetadata.promptTokenCount ?? output.usage.input; output.usage.output = body.usageMetadata.candidatesTokenCount ?? output.usage.output; output.usage.cacheRead = body.usageMetadata.cachedContentTokenCount ?? output.usage.cacheRead; output.usage.totalTokens = body.usageMetadata.totalTokenCount ?? output.usage.input + output.usage.output; if (body.usageMetadata.thoughtsTokenCount !== undefined) { (output.usage as typeof output.usage & { reasoning?: number }).reasoning = body.usageMetadata.thoughtsTokenCount; } if ("cost" in model && model.cost) calculateCost(model, output.usage); } } function appendText( output: AssistantMessage, stream: ReturnType, textState: TextStreamState, text: string, ): void { if (textState.contentIndex !== undefined && textState.type !== "text") finalizeTextBlock(output, stream, textState); if (textState.contentIndex === undefined) { textState.contentIndex = output.content.length; textState.type = "text"; output.content.push({ type: "text", text: "" }); stream.push({ type: "text_start", contentIndex: textState.contentIndex, partial: output }); } const block = output.content[textState.contentIndex]; if (block?.type === "text") block.text += text; textState.text += text; stream.push({ type: "text_delta", contentIndex: textState.contentIndex, delta: text, partial: output }); } function appendThinking( output: AssistantMessage, stream: ReturnType, textState: TextStreamState, thinking: string, signature?: string, ): void { if (textState.contentIndex !== undefined && textState.type !== "thinking") finalizeTextBlock(output, stream, textState); if (textState.contentIndex === undefined) { textState.contentIndex = output.content.length; textState.type = "thinking"; output.content.push({ type: "thinking", thinking: "" } as never); stream.push({ type: "thinking_start", contentIndex: textState.contentIndex, partial: output } as never); } const block = output.content[textState.contentIndex] as { type?: string; thinking?: string; thinkingSignature?: string } | undefined; if (block?.type === "thinking") { block.thinking = `${block.thinking ?? ""}${thinking}`; if (signature) block.thinkingSignature = block.thinkingSignature ?? signature; } textState.text += thinking; stream.push({ type: "thinking_delta", contentIndex: textState.contentIndex, delta: thinking, partial: output } as never); } function appendToolCall( output: AssistantMessage, stream: ReturnType, part: GeminiResponsePart, ): void { const functionCall = part.functionCall; if (!functionCall) return; const contentIndex = output.content.length; const toolCall = { type: "toolCall" as const, id: functionCall.id || `${functionCall.name ?? "tool"}_${Date.now()}_${contentIndex}`, name: functionCall.name ?? "", // Gemini 的 args 就是解析好的 JSON,pi-ai 0.86 起把 ToolCall.arguments 限定为 JsonObject arguments: (functionCall.args ?? {}) as ToolCall["arguments"], ...(part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {}), }; output.content.push(toolCall); stream.push({ type: "toolcall_start", contentIndex, partial: output }); stream.push({ type: "toolcall_delta", contentIndex, delta: JSON.stringify(toolCall.arguments), partial: output }); stream.push({ type: "toolcall_end", contentIndex, toolCall, partial: output }); } function finalizeTextBlock( output: AssistantMessage, stream: ReturnType, textState: TextStreamState, ): void { if (textState.contentIndex === undefined) return; if (textState.type === "thinking") { stream.push({ type: "thinking_end", contentIndex: textState.contentIndex, content: textState.text, partial: output } as never); } else { stream.push({ type: "text_end", contentIndex: textState.contentIndex, content: textState.text, partial: output }); } textState.contentIndex = undefined; textState.type = undefined; textState.text = ""; } function applyGeminiSseText( model: Model, output: AssistantMessage, text: string, stream: ReturnType, textState: TextStreamState, includeThoughts: boolean, ): void { for (const line of parseCodeAssistSse(text)) { applyGeminiSseLine(model, output, line, stream, textState, includeThoughts); } } async function applyGeminiSseStream( model: Model, output: AssistantMessage, body: ReadableStream, stream: ReturnType, textState: TextStreamState, includeThoughts: boolean, ): Promise { const reader = body.getReader(); const decoder = new TextDecoder(); let buffer = ""; while (true) { const { done, value } = await reader.read(); if (done) break; buffer += decoder.decode(value, { stream: true }); let newlineIndex = buffer.indexOf("\n"); while (newlineIndex !== -1) { const line = buffer.slice(0, newlineIndex).replace(/\r$/, ""); buffer = buffer.slice(newlineIndex + 1); applyGeminiSseText(model, output, `${line}\n`, stream, textState, includeThoughts); newlineIndex = buffer.indexOf("\n"); } } buffer += decoder.decode(); if (buffer.trim()) applyGeminiSseText(model, output, buffer, stream, textState, includeThoughts); } function applyGeminiSseLine( model: Model, output: AssistantMessage, line: string, stream: ReturnType, textState: TextStreamState, includeThoughts: boolean, ): void { if (!line.startsWith("data:")) return; const json = line.slice(5).trim(); if (!json || json === "[DONE]") return; try { applyGeminiBodyToMessage(model, output, JSON.parse(json) as GeminiResponseBody, stream, textState, includeThoughts); } catch { // 忽略无法解析的 SSE 行,保持与流式协议容错一致。 } } function shouldIncludeThoughts(options: SimpleStreamOptions): boolean { if (options.thinking?.enabled === false) return false; if (options.thinking?.includeThoughts !== undefined) return options.thinking.includeThoughts; return options.includeThoughts !== false; } function buildGenerationConfig(modelId: string, options: SimpleStreamOptions): Record | undefined { const config: Record = {}; if (options.temperature !== undefined) config.temperature = options.temperature; if (options.maxTokens !== undefined) config.maxOutputTokens = options.maxTokens; const thinkingConfig = buildThinkingConfig(modelId, options.reasoning, options.thinkingBudgets, shouldIncludeThoughts(options), options.thinking); if (thinkingConfig) config.thinkingConfig = thinkingConfig; return Object.keys(config).length > 0 ? config : undefined; } async function applyPayloadHook(payload: Record, model: Model, options: SimpleStreamOptions): Promise { if (!options.onPayload) return payload; const replaced = await options.onPayload(payload, model); return replaced === undefined ? payload : replaced; } async function callResponseHook(response: Response, model: Model, options: SimpleStreamOptions): Promise { if (!options.onResponse) return; const headers: Record = {}; response.headers.forEach((value, key) => { headers[key] = value; }); await options.onResponse({ status: response.status, headers }, model); } function buildThinkingConfig( modelId: string, reasoning: SimpleStreamOptions["reasoning"], customBudgets?: SimpleStreamOptions["thinkingBudgets"], includeThoughts = false, explicitThinking?: SimpleStreamOptions["thinking"], ): Record | undefined { if (explicitThinking) { if (explicitThinking.enabled === false) return { includeThoughts: false }; if (explicitThinking.budgetTokens !== undefined) return { thinkingBudget: explicitThinking.budgetTokens, includeThoughts: explicitThinking.includeThoughts ?? includeThoughts }; if (explicitThinking.level !== undefined) return { thinkingLevel: explicitThinking.level, includeThoughts: explicitThinking.includeThoughts ?? includeThoughts }; if (explicitThinking.includeThoughts !== undefined && reasoning) includeThoughts = explicitThinking.includeThoughts; } if (!reasoning) return undefined; if (modelId.includes("2.5-flash")) { return { thinkingBudget: getCustomThinkingBudget(customBudgets, reasoning) ?? getThinkingBudget(reasoning, 24576), includeThoughts }; } if (modelId.includes("2.5-pro")) { return { thinkingBudget: getCustomThinkingBudget(customBudgets, reasoning) ?? getThinkingBudget(reasoning, 32768), includeThoughts }; } return { thinkingLevel: reasoning, includeThoughts }; } function getCustomThinkingBudget( customBudgets: SimpleStreamOptions["thinkingBudgets"] | undefined, reasoning: Exclude, ): number | undefined { // ThinkingBudgets 只有 minimal/low/medium/high,xhigh 与 max 落到 high if (reasoning === "xhigh" || reasoning === "max") return customBudgets?.high; return customBudgets?.[reasoning]; } function getThinkingBudget(reasoning: Exclude, highBudget: number): number { switch (reasoning) { case "minimal": return 128; case "low": return 2048; case "medium": return 8192; case "high": case "xhigh": case "max": default: return highBudget; } } type TranscriptHelpers = { getCurrentSystemPrompt?: (messages: readonly { role: string }[]) => string; getCurrentTools?: (messages: readonly { role: string }[]) => Tool[]; }; /** * pi-ai 0.86 起 provider 收到的是 TranscriptContext:systemPrompt 与 tools 被折进 role=system 的 * 消息里,`context.systemPrompt` / `context.tools` 恒为 undefined。只读旧字段会把系统提示词和工具 * 声明整个漏掉——模型仍按提示词去调工具,Gemini 回 MALFORMED_FUNCTION_CALL。新版本用它自带的回放 * 函数取回;老版本没有这两个函数(命名空间取属性得 undefined,不会像具名 import 那样在加载期报错), * 仍读旧字段。 */ function resolvePromptAndTools(context: Context): { systemPrompt: string | undefined; tools: Tool[] } { const helpers = piAi as unknown as TranscriptHelpers; const canReplay = typeof helpers.getCurrentSystemPrompt === "function" && typeof helpers.getCurrentTools === "function"; const systemPrompt = context.systemPrompt ?? (canReplay ? helpers.getCurrentSystemPrompt!(context.messages) || undefined : undefined); const tools = context.tools ?? (canReplay ? helpers.getCurrentTools!(context.messages) : []); return { systemPrompt, tools }; } function convertContextMessages(context: Context, model: Model): Array<{ role: "user" | "model"; parts: Array> }> { const contents: Array<{ role: "user" | "model"; parts: Array> }> = []; for (const message of context.messages) { if (message.role === "user") { const parts = Array.isArray(message.content) ? (message.content.map(contentToGeminiPart).filter(Boolean) as Array>) : [{ text: cleanUnicodeSurrogates(message.content) }]; contents.push({ role: "user", parts }); continue; } if (message.role === "assistant") { // 报错或中断的回复不回放(同 pi-ai 自带 provider 的 transformMessages)。会话续跑时最后那条报错的回复 // 还在历史里:空回复会变成一个空的 model 轮次,半截的工具调用没有对应的结果,请求会被拒。 if (message.stopReason === "error" || message.stopReason === "aborted") continue; const parts = message.content .map((content) => { if (content.type === "text") return { text: content.text }; if (content.type === "toolCall") { return { functionCall: { name: content.name, args: content.arguments ?? {}, ...(requiresToolCallId(model.id) ? { id: content.id } : {}), }, ...(content.thoughtSignature ? { thoughtSignature: content.thoughtSignature } : {}), }; } return undefined; }) .filter(Boolean) as Array>; if (parts.length > 0) contents.push({ role: "model", parts }); continue; } if (message.role === "toolResult") { const text = message.content .map((content) => (content.type === "text" ? cleanUnicodeSurrogates(content.text) : "")) .join("\n") .trimEnd(); const imageParts = message.content .map((content) => (content.type === "image" ? contentToGeminiPart(content) : undefined)) .filter(Boolean) as Array>; const functionResponseBody: Record = { name: message.toolName, response: message.isError ? { error: text } : { output: text }, ...(requiresToolCallId(model.id) ? { id: message.toolCallId } : {}), }; if (requiresToolCallId(model.id) && imageParts.length > 0) functionResponseBody.parts = imageParts; const functionResponse = { functionResponse: functionResponseBody }; const lastContent = contents[contents.length - 1]; if (lastContent?.role === "user" && lastContent.parts.some((part) => "functionResponse" in part)) { lastContent.parts.push(functionResponse); } else { contents.push({ role: "user", parts: [functionResponse] }); } if (!requiresToolCallId(model.id) && imageParts.length > 0) contents.push({ role: "user", parts: imageParts }); } } return contents.length > 0 ? contents : [{ role: "user", parts: [{ text: "" }] }]; } function contentToGeminiPart(content: { type: string; text?: string; data?: string; mimeType?: string }): Record | undefined { if (content.type === "text") return { text: cleanUnicodeSurrogates(content.text ?? "") }; if (content.type === "image" && content.data && content.mimeType) { return { inlineData: { data: content.data, mimeType: content.mimeType } }; } return undefined; } function convertContextTools(tools: Tool[]): Array<{ functionDeclarations: Array> }> { return [ { functionDeclarations: tools.map((tool) => ({ name: tool.name, description: tool.description, parametersJsonSchema: tool.parameters, })), }, ]; } function requiresToolCallId(modelId: string): boolean { return modelId.includes("gemini-3"); } function mapToolChoice(choice: "auto" | "none" | "any"): "AUTO" | "NONE" | "ANY" { switch (choice) { case "none": return "NONE"; case "any": return "ANY"; case "auto": default: return "AUTO"; } } function getDoneReason(stopReason: StopReason): StopReason { return stopReason === "toolUse" || stopReason === "length" || stopReason === "error" ? stopReason : "stop"; } function shouldRefreshOauthCredentials(credentials: OAuthCredentials | undefined): credentials is OAuthCredentials { return Boolean(credentials?.refresh && typeof credentials.expires === "number" && credentials.expires <= Date.now() + 60_000); } function cleanUnicodeSurrogates(text: string): string { return text.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?, cost: GEMINI_CODE_ASSIST_COST, contextWindow: 1_048_576, maxTokens: 65_536, }, { id: "gemini-3-flash", name: "Gemini 3 Flash (Gemini CLI OAuth)", aliases: ["preview", "flash-preview", "gemini-preview"], reasoning: true, input: ["text", "image"] as Array<"text" | "image">, cost: GEMINI_CODE_ASSIST_COST, contextWindow: 1_000_000, maxTokens: 65_536, }, { id: "gemini-3.1-pro-preview", name: "Gemini 3.1 Pro Preview (Gemini CLI OAuth)", aliases: ["pro", "pro-preview", "gemini-pro-preview"], reasoning: true, input: ["text", "image"] as Array<"text" | "image">, cost: { input: 0.00000125, output: 0.00001, cacheRead: 0.0000003125, cacheWrite: 0.00000125 }, contextWindow: 1_000_000, maxTokens: 65_536, }, ]; export const GEMINI_CLI_OAUTH_API = "gemini-cli-oauth-api"; export { PROVIDER_ID };