@@ -3,7 +3,6 @@ import { createGoogleGenerativeAI } from "@ai-sdk/google";
33 import { createOpenAI } from "@ai-sdk/openai";
44 import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
55 import type { LanguageModel } from "ai";
6-import { logApiEvent } from "../logging/context.ts";
76 import { getAccessToken, isLoggedIn } from "../session/oauth/auth-storage.ts";
87 import {
98 type AvailableModelsSnapshot,
@@ -33,56 +32,10 @@ type GoogleProvider = ReturnType<typeof createGoogleGenerativeAI>;
3332 type OpenAICompatProvider = ReturnType<typeof createOpenAICompatible>;
3433 type ModelResolver = (modelId: string) => LanguageModel;
3534
36-const REDACTED_HEADERS = new Set(["authorization", "x-api-key"]);
37-
38-function redactHeaders(
39- headers: RequestInit["headers"],
40-): Record<string, string> | undefined {
41- if (!headers) return undefined;
42- const h = new Headers(headers as ConstructorParameters<typeof Headers>[0]);
43- const out: Record<string, string> = {};
44- h.forEach((v, k) => {
45- out[k] = REDACTED_HEADERS.has(k) ? "[REDACTED]" : v;
46- });
47- return out;
48-}
49-
50-function createFetchWithLogging(): ProviderFetch {
51- const customFetch = async (
52- input: Parameters<ProviderFetch>[0],
53- init?: Parameters<ProviderFetch>[1],
54- ): Promise<Response> => {
55- if (init?.body) {
56- try {
57- const bodyStr = init.body.toString();
58- const bodyJson = JSON.parse(bodyStr);
59- logApiEvent("Provider Request", {
60- url: input.toString(),
61- method: init.method,
62- headers: redactHeaders(init.headers),
63- body: bodyJson,
64- });
65- } catch {
66- logApiEvent("Provider Request", {
67- url: input.toString(),
68- method: init.method,
69- headers: redactHeaders(init.headers),
70- body: init.body,
71- });
72- }
73- }
74- return fetch(input, init);
75- };
76- return customFetch as ProviderFetch;
77-}
78-
79-const fetchWithLogging = createFetchWithLogging();
80-
8135 /** Betas the AI SDK adds that are not compatible with the OAuth endpoint. */
8236 const OAUTH_STRIP_BETAS = new Set<string>();
8337
8438 function createOAuthFetch(accessToken: string): ProviderFetch {
85- const baseFetch = createFetchWithLogging();
8639 const oauthFetch = async (
8740 input: Parameters<ProviderFetch>[0],
8841 init?: Parameters<ProviderFetch>[1],
@@ -106,7 +59,7 @@ function createOAuthFetch(accessToken: string): ProviderFetch {
10659 h.set("x-app", "cli");
10760 opts = { ...opts, headers: Object.fromEntries(h.entries()) };
10861 }
109- return baseFetch(input, opts);
62+ return fetch(input, opts);
11063 };
11164 return oauthFetch as ProviderFetch;
11265 }
@@ -138,28 +91,28 @@ function lazy<T>(factory: () => T): () => T {
13891 const zenProviders = {
13992 anthropic: lazy<AnthropicProvider>(() =>
14093 createAnthropic({
141- fetch: fetchWithLogging,
94+ fetch,
14295 apiKey: requireEnv("OPENCODE_API_KEY"),
14396 baseURL: ZEN_BASE,
14497 }),
14598 ),
14699 openai: lazy<OpenAIProvider>(() =>
147100 createOpenAI({
148- fetch: fetchWithLogging,
101+ fetch,
149102 apiKey: requireEnv("OPENCODE_API_KEY"),
150103 baseURL: ZEN_BASE,
151104 }),
152105 ),
153106 google: lazy<GoogleProvider>(() =>
154107 createGoogleGenerativeAI({
155- fetch: fetchWithLogging,
108+ fetch,
156109 apiKey: requireEnv("OPENCODE_API_KEY"),
157110 baseURL: ZEN_BASE,
158111 }),
159112 ),
160113 compat: lazy<OpenAICompatProvider>(() =>
161114 createOpenAICompatible({
162- fetch: fetchWithLogging,
115+ fetch,
163116 name: "zen-compat",
164117 apiKey: requireEnv("OPENCODE_API_KEY"),
165118 baseURL: ZEN_BASE,
@@ -170,19 +123,19 @@ const zenProviders = {
170123 const directProviders = {
171124 anthropic: lazy<AnthropicProvider>(() =>
172125 createAnthropic({
173- fetch: fetchWithLogging,
126+ fetch,
174127 apiKey: requireEnv("ANTHROPIC_API_KEY"),
175128 }),
176129 ),
177130 openai: lazy<OpenAIProvider>(() =>
178131 createOpenAI({
179- fetch: fetchWithLogging,
132+ fetch,
180133 apiKey: requireEnv("OPENAI_API_KEY"),
181134 }),
182135 ),
183136 google: lazy<GoogleProvider>(() =>
184137 createGoogleGenerativeAI({
185- fetch: fetchWithLogging,
138+ fetch,
186139 apiKey: requireAnyEnv(["GOOGLE_API_KEY", "GEMINI_API_KEY"]),
187140 }),
188141 ),
@@ -192,7 +145,7 @@ const directProviders = {
192145 name: "ollama",
193146 baseURL: `${baseURL}/v1`,
194147 apiKey: "ollama",
195- fetch: fetchWithLogging,
148+ fetch,
196149 });
197150 }),
198151 };