@@ -5,8 +5,38 @@ import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
55 import type { LanguageModel } from "ai";
66 import { createOllama } from "ollama-ai-provider";
77
8+import { logApiEvent } from "./api-log.ts";
89 // ─── Zen endpoint constants ────────────────────────────────────────────────────
910
11+function getFetchWithLogging(): typeof fetch {
12+ const customFetch = async (
13+ input: Parameters<typeof fetch>[0],
14+ init?: Parameters<typeof fetch>[1],
15+ ): Promise<Response> => {
16+ if (init?.body) {
17+ try {
18+ const bodyStr = init.body.toString();
19+ const bodyJson = JSON.parse(bodyStr);
20+ logApiEvent("Provider Request", {
21+ url: input.toString(),
22+ method: init.method,
23+ headers: init.headers,
24+ body: bodyJson,
25+ });
26+ } catch {
27+ logApiEvent("Provider Request", {
28+ url: input.toString(),
29+ method: init.method,
30+ headers: init.headers,
31+ body: init.body,
32+ });
33+ }
34+ }
35+ return fetch(input, init);
36+ };
37+ return customFetch as unknown as typeof fetch;
38+}
39+
1040 const ZEN_BASE = "https://opencode.ai/zen/v1";
1141
1242 // Zen endpoint routing — matched in order, fallthrough to OpenAI-compatible
@@ -35,6 +65,7 @@ function zenAnthropic() {
3565 if (!_zenAnthropic) {
3666 // @ai-sdk/anthropic appends /messages automatically — baseURL is the v1 root
3767 _zenAnthropic = createAnthropic({
68+ fetch: getFetchWithLogging(),
3869 apiKey: getZenApiKey(),
3970 baseURL: ZEN_BASE,
4071 });
@@ -46,6 +77,7 @@ function zenOpenAI() {
4677 if (!_zenOpenAI) {
4778 // @ai-sdk/openai appends /responses automatically — baseURL is the v1 root
4879 _zenOpenAI = createOpenAI({
80+ fetch: getFetchWithLogging(),
4981 apiKey: getZenApiKey(),
5082 baseURL: ZEN_BASE,
5183 });
@@ -57,6 +89,7 @@ function zenGoogle() {
5789 if (!_zenGoogle) {
5890 // @ai-sdk/google constructs its own path; we pass the base up to /models
5991 _zenGoogle = createGoogleGenerativeAI({
92+ fetch: getFetchWithLogging(),
6093 apiKey: getZenApiKey(),
6194 baseURL: ZEN_BASE,
6295 });
@@ -68,6 +101,7 @@ function zenCompat() {
68101 if (!_zenCompat) {
69102 // @ai-sdk/openai-compatible appends /chat/completions — baseURL is the v1 root
70103 _zenCompat = createOpenAICompatible({
104+ fetch: getFetchWithLogging(),
71105 name: "zen-compat",
72106 apiKey: getZenApiKey(),
73107 baseURL: ZEN_BASE,
@@ -86,7 +120,10 @@ function directAnthropic() {
86120 if (!_directAnthropic) {
87121 const key = process.env.ANTHROPIC_API_KEY;
88122 if (!key) throw new Error("ANTHROPIC_API_KEY is not set");
89- _directAnthropic = createAnthropic({ apiKey: key });
123+ _directAnthropic = createAnthropic({
124+ fetch: getFetchWithLogging(),
125+ apiKey: key,
126+ });
90127 }
91128 return _directAnthropic;
92129 }
@@ -95,7 +132,7 @@ function directOpenAI() {
95132 if (!_directOpenAI) {
96133 const key = process.env.OPENAI_API_KEY;
97134 if (!key) throw new Error("OPENAI_API_KEY is not set");
98- _directOpenAI = createOpenAI({ apiKey: key });
135+ _directOpenAI = createOpenAI({ fetch: getFetchWithLogging(), apiKey: key });
99136 }
100137 return _directOpenAI;
101138 }
@@ -104,7 +141,10 @@ function directGoogle() {
104141 if (!_directGoogle) {
105142 const key = process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY;
106143 if (!key) throw new Error("GOOGLE_API_KEY or GEMINI_API_KEY is not set");
107- _directGoogle = createGoogleGenerativeAI({ apiKey: key });
144+ _directGoogle = createGoogleGenerativeAI({
145+ fetch: getFetchWithLogging(),
146+ apiKey: key,
147+ });
108148 }
109149 return _directGoogle;
110150 }
@@ -207,7 +247,10 @@ export function resolveModel(modelString: string): LanguageModel {
207247
208248 case "ollama": {
209249 const baseURL = process.env.OLLAMA_BASE_URL ?? "http://localhost:11434";
210- const ollamaProvider = createOllama({ baseURL });
250+ const ollamaProvider = createOllama({
251+ fetch: getFetchWithLogging(),
252+ baseURL,
253+ });
211254 // ollama-ai-provider returns LanguageModelV1; cast to LanguageModel
212255 return ollamaProvider(modelId) as unknown as LanguageModel;
213256 }