pi-free 2.0.13 → 2.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,371 +1,398 @@
1
- import { existsSync, lstatSync, readFileSync } from "node:fs";
2
- import { basename, dirname, join } from "node:path";
3
- import { randomBytes } from "node:crypto";
4
- import { createRequire } from "node:module";
5
- import { pathToFileURL } from "node:url";
6
- import type {
7
- Api,
8
- AssistantMessage,
9
- AssistantMessageEvent,
10
- AssistantMessageEventStream,
11
- Context,
12
- Model,
13
- SimpleStreamOptions,
14
- } from "@earendil-works/pi-ai";
15
- import type { ProviderConfig } from "@earendil-works/pi-coding-agent";
16
-
17
- export const OPENCODE_DYNAMIC_API = "opencode-dynamic" as const;
18
-
19
- export const OPENCODE_STATIC_HEADERS = {
20
- "User-Agent": "opencode/1.15.5",
21
- "x-opencode-client": "cli",
22
- } as const;
23
-
24
- /**
25
- * OpenCode-native identifier generation.
26
- *
27
- * OpenCode's server uses checkHeaders to distinguish native CLI requests from
28
- * third-party clients. Native identifiers use ULID-style prefixes:
29
- *
30
- * Session: ses_<hex><base62> (e.g. ses_a1b2c3d4e5f6g7h8i9j0k1l2m3n4)
31
- * Request: msg_<hex><base62> (e.g. msg_01KA1B2C3D4E5F6G7H8I9J0K1L2M)
32
- *
33
- * If the server does not see the expected prefix it applies a fallback rate
34
- * limit (~2 req/day) which causes models to "freeze" after a few prompts.
35
- */
36
- function generateOpenCodeId(prefix: string): string {
37
- // Timestamp in ms as big-endian hex (matches ULID-style sortability).
38
- const ms = BigInt(Date.now());
39
- const timeHex = ms.toString(16).padStart(12, "0");
40
- // Random suffix (crypto) encoded as base62 for compactness.
41
- const randomLen = 14;
42
- const base62Chars =
43
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
44
- const bytes = randomBytes(randomLen);
45
- let suffix = "";
46
- for (let i = 0; i < randomLen; i++) {
47
- suffix += base62Chars[bytes[i] % 62];
48
- }
49
- return `${prefix}${timeHex}${suffix}`;
50
- }
51
-
52
- /**
53
- * Shared OpenCode session/request tracking.
54
- *
55
- * OpenCode endpoints require native-format identifiers (ses_ / msg_ prefix)
56
- * to receive the full daily rate limit. Without matching prefixes the server
57
- * falls back to a ~2 req/day limit, causing free models to freeze after a
58
- * couple of prompts.
59
- */
60
- export function createOpenCodeSessionTracker() {
61
- let sessionId = "";
62
-
63
- function getSessionId(): string {
64
- if (!sessionId) {
65
- sessionId = generateOpenCodeId("ses_");
66
- }
67
- return sessionId;
68
- }
69
-
70
- function nextRequestId(): string {
71
- return generateOpenCodeId("msg_");
72
- }
73
-
74
- return {
75
- getSessionId,
76
- nextRequestId,
77
- };
78
- }
79
-
80
- export type OpenCodeSessionTracker = ReturnType<
81
- typeof createOpenCodeSessionTracker
82
- >;
83
-
84
- export function createOpenCodeHeaders(
85
- tracker: OpenCodeSessionTracker,
86
- existingHeaders?: Record<string, string>,
87
- ): Record<string, string> {
88
- return {
89
- ...existingHeaders,
90
- ...OPENCODE_STATIC_HEADERS,
91
- "x-opencode-session": tracker.getSessionId(),
92
- "x-opencode-request": tracker.nextRequestId(),
93
- };
94
- }
95
-
96
- export function isOpenCodeProvider(providerId: string): boolean {
97
- return providerId === "opencode" || providerId === "opencode-go";
98
- }
99
-
100
- function stripTrailingSlashes(value: string): string {
101
- let end = value.length;
102
- while (end > 0 && value.codePointAt(end - 1) === 47) {
103
- end--;
104
- }
105
- return value.slice(0, end);
106
- }
107
-
108
- function isAnthropicOpenCodeEndpoint(model: Model<Api>): boolean {
109
- return !stripTrailingSlashes(model.baseUrl).endsWith("/v1");
110
- }
111
-
112
- type StreamSimpleFn<TApi extends Api> = (
113
- model: Model<TApi>,
114
- context: Context,
115
- options?: SimpleStreamOptions,
116
- ) => AssistantMessageEventStream;
117
-
118
- type AnthropicStreamModule = {
119
- streamSimpleAnthropic: StreamSimpleFn<"anthropic-messages">;
120
- };
121
-
122
- type OpenAICompletionsStreamModule = {
123
- streamSimpleOpenAICompletions: StreamSimpleFn<"openai-completions">;
124
- };
125
-
126
- const piAiSubpathCache = new Map<string, Promise<unknown>>();
127
-
128
- async function importPiAiSubpath<T>(subpath: string): Promise<T> {
129
- const specifier = `@earendil-works/pi-ai/${subpath}`;
130
- const cached = piAiSubpathCache.get(specifier) as Promise<T> | undefined;
131
- if (cached) return cached;
132
-
133
- const promise = importPiAiSubpathUncached<T>(specifier);
134
- piAiSubpathCache.set(specifier, promise);
135
- return promise;
136
- }
137
-
138
- async function importPiAiSubpathUncached<T>(specifier: string): Promise<T> {
139
- try {
140
- return (await import(specifier)) as T;
141
- } catch (directError) {
142
- const resolved = resolvePiAiSubpathFromPackage(specifier);
143
- if (!resolved) throw directError;
144
- try {
145
- return (await import(pathToFileURL(resolved).href)) as T;
146
- } catch {
147
- throw directError;
148
- }
149
- }
150
- }
151
-
152
- const PI_AI_DEPENDENCY_CANARY = "openai";
153
-
154
- function findPiAiPackageDir(requireBase: string): string | undefined {
155
- try {
156
- const require = createRequire(requireBase);
157
- const resolved = require.resolve(PI_AI_DEPENDENCY_CANARY);
158
- let dir = dirname(resolved);
159
- while (dir !== dirname(dir)) {
160
- if (basename(dir) === "node_modules") {
161
- const piAiDir = join(dir, "@earendil-works", "pi-ai");
162
- const pkgJsonPath = join(piAiDir, "package.json");
163
- if (existsSync(pkgJsonPath) && lstatSync(pkgJsonPath).isFile()) {
164
- return piAiDir;
165
- }
166
- }
167
- dir = dirname(dir);
168
- }
169
- } catch {
170
- // Resolution failed — try the next base.
171
- }
172
- return undefined;
173
- }
174
-
175
- function resolvePiAiSubpathFromPackage(specifier: string): string | undefined {
176
- const subpath = specifier.replace("@earendil-works/pi-ai/", "");
177
- const candidates = [process.argv[1], import.meta.url].filter(
178
- (value): value is string => Boolean(value),
179
- );
180
-
181
- for (const candidate of candidates) {
182
- const pkgDir = findPiAiPackageDir(candidate);
183
- if (!pkgDir) continue;
184
- try {
185
- const pkg = JSON.parse(
186
- readFileSync(join(pkgDir, "package.json"), "utf-8"),
187
- );
188
- const exportEntry = pkg.exports?.[`./${subpath}`];
189
- const targetPath = exportEntry?.import ?? exportEntry?.default;
190
- if (typeof targetPath === "string") {
191
- return join(pkgDir, targetPath);
192
- }
193
- } catch {
194
- // Try the next resolution base.
195
- }
196
- }
197
-
198
- return undefined;
199
- }
200
-
201
- class DeferredAssistantMessageEventStream {
202
- private queue: AssistantMessageEvent[] = [];
203
- private waiting: Array<
204
- (result: IteratorResult<AssistantMessageEvent>) => void
205
- > = [];
206
- private done = false;
207
- private resolveResult!: (message: AssistantMessage) => void;
208
- private readonly finalResultPromise: Promise<AssistantMessage>;
209
-
210
- constructor() {
211
- this.finalResultPromise = new Promise((resolve) => {
212
- this.resolveResult = resolve;
213
- });
214
- }
215
-
216
- push(event: AssistantMessageEvent): void {
217
- if (this.done) return;
218
-
219
- if (event.type === "done" || event.type === "error") {
220
- this.done = true;
221
- this.resolveResult(event.type === "done" ? event.message : event.error);
222
- }
223
-
224
- const waiter = this.waiting.shift();
225
- if (waiter) {
226
- waiter({ value: event, done: false });
227
- } else {
228
- this.queue.push(event);
229
- }
230
- }
231
-
232
- end(result?: AssistantMessage): void {
233
- if (this.done) return;
234
- this.done = true;
235
- if (result) this.resolveResult(result);
236
- while (this.waiting.length > 0) {
237
- this.waiting.shift()?.({ value: undefined, done: true });
238
- }
239
- }
240
-
241
- async *[Symbol.asyncIterator](): AsyncIterator<AssistantMessageEvent> {
242
- while (true) {
243
- if (this.queue.length > 0) {
244
- yield this.queue.shift()!;
245
- } else if (this.done) {
246
- return;
247
- } else {
248
- const result = await new Promise<IteratorResult<AssistantMessageEvent>>(
249
- (resolve) => this.waiting.push(resolve),
250
- );
251
- if (result.done) return;
252
- yield result.value;
253
- }
254
- }
255
- }
256
-
257
- result(): Promise<AssistantMessage> {
258
- return this.finalResultPromise;
259
- }
260
- }
261
-
262
- function createErrorMessage(
263
- model: Model<Api>,
264
- error: unknown,
265
- ): AssistantMessage {
266
- const message = error instanceof Error ? error.message : String(error);
267
- return {
268
- role: "assistant",
269
- content: [],
270
- api: model.api,
271
- provider: model.provider,
272
- model: model.id,
273
- usage: {
274
- input: 0,
275
- output: 0,
276
- cacheRead: 0,
277
- cacheWrite: 0,
278
- totalTokens: 0,
279
- cost: {
280
- input: 0,
281
- output: 0,
282
- cacheRead: 0,
283
- cacheWrite: 0,
284
- total: 0,
285
- },
286
- },
287
- stopReason: "error",
288
- errorMessage: message,
289
- timestamp: Date.now(),
290
- };
291
- }
292
-
293
- async function pipeStream(
294
- stream: DeferredAssistantMessageEventStream,
295
- upstream: AssistantMessageEventStream,
296
- ): Promise<void> {
297
- let finalMessage: AssistantMessage | undefined;
298
- try {
299
- for await (const event of upstream) {
300
- stream.push(event);
301
- if (event.type === "done") finalMessage = event.message;
302
- if (event.type === "error") finalMessage = event.error;
303
- }
304
- stream.end(finalMessage ?? (await upstream.result()));
305
- } catch (error) {
306
- if (finalMessage) {
307
- stream.end(finalMessage);
308
- } else {
309
- throw error;
310
- }
311
- }
312
- }
313
-
314
- /**
315
- * Pi's static model headers are evaluated at registration time. OpenCode treats
316
- * x-opencode-request like a per-request id, so reusing one value across turns can
317
- * leave later requests attached to an old/in-flight generation. Registering a
318
- * provider-specific stream keeps the normal Pi parsers but refreshes headers for
319
- * every LLM call.
320
- */
321
- export function createOpenCodeStreamSimple(
322
- tracker: OpenCodeSessionTracker,
323
- ): NonNullable<ProviderConfig["streamSimple"]> {
324
- return (model, context, options) => {
325
- const headers = createOpenCodeHeaders(tracker, options?.headers);
326
- const stream = new DeferredAssistantMessageEventStream();
327
-
328
- void (async () => {
329
- try {
330
- if (isAnthropicOpenCodeEndpoint(model)) {
331
- const { streamSimpleAnthropic } =
332
- await importPiAiSubpath<AnthropicStreamModule>("anthropic");
333
- await pipeStream(
334
- stream,
335
- streamSimpleAnthropic(
336
- {
337
- ...model,
338
- api: "anthropic-messages",
339
- } as Model<"anthropic-messages">,
340
- context,
341
- { ...options, headers },
342
- ),
343
- );
344
- return;
345
- }
346
-
347
- const { streamSimpleOpenAICompletions } =
348
- await importPiAiSubpath<OpenAICompletionsStreamModule>(
349
- "openai-completions",
350
- );
351
- await pipeStream(
352
- stream,
353
- streamSimpleOpenAICompletions(
354
- {
355
- ...model,
356
- api: "openai-completions",
357
- } as Model<"openai-completions">,
358
- context,
359
- { ...options, headers },
360
- ),
361
- );
362
- } catch (error) {
363
- const errorMessage = createErrorMessage(model, error);
364
- stream.push({ type: "start", partial: errorMessage });
365
- stream.push({ type: "error", reason: "error", error: errorMessage });
366
- }
367
- })();
368
-
369
- return stream as unknown as AssistantMessageEventStream;
370
- };
371
- }
1
+ import { existsSync, lstatSync, readFileSync } from "node:fs";
2
+ import { basename, dirname, join } from "node:path";
3
+ import { randomBytes } from "node:crypto";
4
+ import { createRequire } from "node:module";
5
+ import { pathToFileURL } from "node:url";
6
+ import type {
7
+ Api,
8
+ AssistantMessage,
9
+ AssistantMessageEvent,
10
+ AssistantMessageEventStream,
11
+ Context,
12
+ Model,
13
+ SimpleStreamOptions,
14
+ } from "@earendil-works/pi-ai";
15
+ import type { ProviderConfig } from "@earendil-works/pi-coding-agent";
16
+
17
+ export const OPENCODE_DYNAMIC_API = "opencode-dynamic" as const;
18
+
19
+ export const OPENCODE_STATIC_HEADERS = {
20
+ "User-Agent": "opencode/1.15.5",
21
+ "x-opencode-client": "cli",
22
+ } as const;
23
+
24
+ /**
25
+ * OpenCode-native identifier generation.
26
+ *
27
+ * OpenCode's server uses checkHeaders to distinguish native CLI requests from
28
+ * third-party clients. Native identifiers use ULID-style prefixes:
29
+ *
30
+ * Session: ses_<hex><base62> (e.g. ses_a1b2c3d4e5f6g7h8i9j0k1l2m3n4)
31
+ * Request: msg_<hex><base62> (e.g. msg_01KA1B2C3D4E5F6G7H8I9J0K1L2M)
32
+ *
33
+ * If the server does not see the expected prefix it applies a fallback rate
34
+ * limit (~2 req/day) which causes models to "freeze" after a few prompts.
35
+ */
36
+ function generateOpenCodeId(prefix: string): string {
37
+ // Timestamp in ms as big-endian hex (matches ULID-style sortability).
38
+ const ms = BigInt(Date.now());
39
+ const timeHex = ms.toString(16).padStart(12, "0");
40
+ // Random suffix (crypto) encoded as base62 for compactness.
41
+ const randomLen = 14;
42
+ const base62Chars =
43
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
44
+ const bytes = randomBytes(randomLen);
45
+ let suffix = "";
46
+ for (let i = 0; i < randomLen; i++) {
47
+ suffix += base62Chars[bytes[i] % 62];
48
+ }
49
+ return `${prefix}${timeHex}${suffix}`;
50
+ }
51
+
52
+ /**
53
+ * Shared OpenCode session/request tracking.
54
+ *
55
+ * OpenCode endpoints require native-format identifiers (ses_ / msg_ prefix)
56
+ * to receive the full daily rate limit. Without matching prefixes the server
57
+ * falls back to a ~2 req/day limit, causing free models to freeze after a
58
+ * couple of prompts.
59
+ */
60
+ export function createOpenCodeSessionTracker() {
61
+ let sessionId = "";
62
+
63
+ function getSessionId(): string {
64
+ if (!sessionId) {
65
+ sessionId = generateOpenCodeId("ses_");
66
+ }
67
+ return sessionId;
68
+ }
69
+
70
+ function nextRequestId(): string {
71
+ return generateOpenCodeId("msg_");
72
+ }
73
+
74
+ return {
75
+ getSessionId,
76
+ nextRequestId,
77
+ };
78
+ }
79
+
80
+ export type OpenCodeSessionTracker = ReturnType<
81
+ typeof createOpenCodeSessionTracker
82
+ >;
83
+
84
+ export function createOpenCodeHeaders(
85
+ tracker: OpenCodeSessionTracker,
86
+ existingHeaders?: Record<string, string>,
87
+ ): Record<string, string> {
88
+ return {
89
+ ...existingHeaders,
90
+ ...OPENCODE_STATIC_HEADERS,
91
+ "x-opencode-session": tracker.getSessionId(),
92
+ "x-opencode-request": tracker.nextRequestId(),
93
+ };
94
+ }
95
+
96
+ export function isOpenCodeProvider(providerId: string): boolean {
97
+ return providerId === "opencode" || providerId === "opencode-go";
98
+ }
99
+
100
+ function stripTrailingSlashes(value: string): string {
101
+ let end = value.length;
102
+ while (end > 0 && value.codePointAt(end - 1) === 47) {
103
+ end--;
104
+ }
105
+ return value.slice(0, end);
106
+ }
107
+
108
+ function isAnthropicOpenCodeEndpoint(model: Model<Api>): boolean {
109
+ return !stripTrailingSlashes(model.baseUrl).endsWith("/v1");
110
+ }
111
+
112
+ type StreamSimpleFn<TApi extends Api> = (
113
+ model: Model<TApi>,
114
+ context: Context,
115
+ options?: SimpleStreamOptions,
116
+ ) => AssistantMessageEventStream;
117
+
118
+ type AnthropicStreamModule = {
119
+ streamSimpleAnthropic: StreamSimpleFn<"anthropic-messages">;
120
+ };
121
+
122
+ type OpenAICompletionsStreamModule = {
123
+ streamSimpleOpenAICompletions: StreamSimpleFn<"openai-completions">;
124
+ };
125
+
126
+ const piAiSubpathCache = new Map<string, Promise<unknown>>();
127
+
128
+ async function importPiAiSubpath<T>(subpath: string): Promise<T> {
129
+ const specifier = `@earendil-works/pi-ai/${subpath}`;
130
+ const cached = piAiSubpathCache.get(specifier) as Promise<T> | undefined;
131
+ if (cached) return cached;
132
+
133
+ const promise = importPiAiSubpathUncached<T>(specifier);
134
+ piAiSubpathCache.set(specifier, promise);
135
+ return promise;
136
+ }
137
+
138
+ async function importPiAiSubpathUncached<T>(specifier: string): Promise<T> {
139
+ try {
140
+ return (await import(specifier)) as T;
141
+ } catch (directError) {
142
+ const rootFallback = await importPiAiRootFallback<T>(specifier);
143
+ if (rootFallback) return rootFallback;
144
+
145
+ const resolved = resolvePiAiSubpathFromPackage(specifier);
146
+ if (!resolved) throw directError;
147
+ try {
148
+ return (await import(pathToFileURL(resolved).href)) as T;
149
+ } catch {
150
+ throw directError;
151
+ }
152
+ }
153
+ }
154
+
155
+ async function importPiAiRootFallback<T>(
156
+ specifier: string,
157
+ ): Promise<T | undefined> {
158
+ const subpath = specifier.replace("@earendil-works/pi-ai/", "");
159
+ const requiredExport: Record<string, string> = {
160
+ anthropic: "streamSimpleAnthropic",
161
+ "openai-completions": "streamSimpleOpenAICompletions",
162
+ };
163
+ const exportName = requiredExport[subpath];
164
+ if (!exportName) return undefined;
165
+
166
+ try {
167
+ const rootModule = (await import("@earendil-works/pi-ai")) as Record<
168
+ string,
169
+ unknown
170
+ >;
171
+ return typeof rootModule[exportName] === "function"
172
+ ? (rootModule as T)
173
+ : undefined;
174
+ } catch {
175
+ return undefined;
176
+ }
177
+ }
178
+
179
+ const PI_AI_DEPENDENCY_CANARY = "openai";
180
+
181
+ function findPiAiPackageDir(requireBase: string): string | undefined {
182
+ try {
183
+ const require = createRequire(requireBase);
184
+ const resolved = require.resolve(PI_AI_DEPENDENCY_CANARY);
185
+ let dir = dirname(resolved);
186
+ while (dir !== dirname(dir)) {
187
+ if (basename(dir) === "node_modules") {
188
+ const piAiDir = join(dir, "@earendil-works", "pi-ai");
189
+ const pkgJsonPath = join(piAiDir, "package.json");
190
+ if (existsSync(pkgJsonPath) && lstatSync(pkgJsonPath).isFile()) {
191
+ return piAiDir;
192
+ }
193
+ }
194
+ dir = dirname(dir);
195
+ }
196
+ } catch {
197
+ // Resolution failed — try the next base.
198
+ }
199
+ return undefined;
200
+ }
201
+
202
+ function resolvePiAiSubpathFromPackage(specifier: string): string | undefined {
203
+ const subpath = specifier.replace("@earendil-works/pi-ai/", "");
204
+ const candidates = [process.argv[1], import.meta.url].filter(
205
+ (value): value is string => Boolean(value),
206
+ );
207
+
208
+ for (const candidate of candidates) {
209
+ const pkgDir = findPiAiPackageDir(candidate);
210
+ if (!pkgDir) continue;
211
+ try {
212
+ const pkg = JSON.parse(
213
+ readFileSync(join(pkgDir, "package.json"), "utf-8"),
214
+ );
215
+ const exportEntry = pkg.exports?.[`./${subpath}`];
216
+ const targetPath = exportEntry?.import ?? exportEntry?.default;
217
+ if (typeof targetPath === "string") {
218
+ return join(pkgDir, targetPath);
219
+ }
220
+ } catch {
221
+ // Try the next resolution base.
222
+ }
223
+ }
224
+
225
+ return undefined;
226
+ }
227
+
228
+ class DeferredAssistantMessageEventStream {
229
+ private queue: AssistantMessageEvent[] = [];
230
+ private waiting: Array<
231
+ (result: IteratorResult<AssistantMessageEvent>) => void
232
+ > = [];
233
+ private done = false;
234
+ private resolveResult!: (message: AssistantMessage) => void;
235
+ private readonly finalResultPromise: Promise<AssistantMessage>;
236
+
237
+ constructor() {
238
+ this.finalResultPromise = new Promise((resolve) => {
239
+ this.resolveResult = resolve;
240
+ });
241
+ }
242
+
243
+ push(event: AssistantMessageEvent): void {
244
+ if (this.done) return;
245
+
246
+ if (event.type === "done" || event.type === "error") {
247
+ this.done = true;
248
+ this.resolveResult(event.type === "done" ? event.message : event.error);
249
+ }
250
+
251
+ const waiter = this.waiting.shift();
252
+ if (waiter) {
253
+ waiter({ value: event, done: false });
254
+ } else {
255
+ this.queue.push(event);
256
+ }
257
+ }
258
+
259
+ end(result?: AssistantMessage): void {
260
+ if (this.done) return;
261
+ this.done = true;
262
+ if (result) this.resolveResult(result);
263
+ while (this.waiting.length > 0) {
264
+ this.waiting.shift()?.({ value: undefined, done: true });
265
+ }
266
+ }
267
+
268
+ async *[Symbol.asyncIterator](): AsyncIterator<AssistantMessageEvent> {
269
+ while (true) {
270
+ if (this.queue.length > 0) {
271
+ yield this.queue.shift()!;
272
+ } else if (this.done) {
273
+ return;
274
+ } else {
275
+ const result = await new Promise<IteratorResult<AssistantMessageEvent>>(
276
+ (resolve) => this.waiting.push(resolve),
277
+ );
278
+ if (result.done) return;
279
+ yield result.value;
280
+ }
281
+ }
282
+ }
283
+
284
+ result(): Promise<AssistantMessage> {
285
+ return this.finalResultPromise;
286
+ }
287
+ }
288
+
289
+ function createErrorMessage(
290
+ model: Model<Api>,
291
+ error: unknown,
292
+ ): AssistantMessage {
293
+ const message = error instanceof Error ? error.message : String(error);
294
+ return {
295
+ role: "assistant",
296
+ content: [],
297
+ api: model.api,
298
+ provider: model.provider,
299
+ model: model.id,
300
+ usage: {
301
+ input: 0,
302
+ output: 0,
303
+ cacheRead: 0,
304
+ cacheWrite: 0,
305
+ totalTokens: 0,
306
+ cost: {
307
+ input: 0,
308
+ output: 0,
309
+ cacheRead: 0,
310
+ cacheWrite: 0,
311
+ total: 0,
312
+ },
313
+ },
314
+ stopReason: "error",
315
+ errorMessage: message,
316
+ timestamp: Date.now(),
317
+ };
318
+ }
319
+
320
+ async function pipeStream(
321
+ stream: DeferredAssistantMessageEventStream,
322
+ upstream: AssistantMessageEventStream,
323
+ ): Promise<void> {
324
+ let finalMessage: AssistantMessage | undefined;
325
+ try {
326
+ for await (const event of upstream) {
327
+ stream.push(event);
328
+ if (event.type === "done") finalMessage = event.message;
329
+ if (event.type === "error") finalMessage = event.error;
330
+ }
331
+ stream.end(finalMessage ?? (await upstream.result()));
332
+ } catch (error) {
333
+ if (finalMessage) {
334
+ stream.end(finalMessage);
335
+ } else {
336
+ throw error;
337
+ }
338
+ }
339
+ }
340
+
341
+ /**
342
+ * Pi's static model headers are evaluated at registration time. OpenCode treats
343
+ * x-opencode-request like a per-request id, so reusing one value across turns can
344
+ * leave later requests attached to an old/in-flight generation. Registering a
345
+ * provider-specific stream keeps the normal Pi parsers but refreshes headers for
346
+ * every LLM call.
347
+ */
348
+ export function createOpenCodeStreamSimple(
349
+ tracker: OpenCodeSessionTracker,
350
+ ): NonNullable<ProviderConfig["streamSimple"]> {
351
+ return (model, context, options) => {
352
+ const headers = createOpenCodeHeaders(tracker, options?.headers);
353
+ const stream = new DeferredAssistantMessageEventStream();
354
+
355
+ void (async () => {
356
+ try {
357
+ if (isAnthropicOpenCodeEndpoint(model)) {
358
+ const { streamSimpleAnthropic } =
359
+ await importPiAiSubpath<AnthropicStreamModule>("anthropic");
360
+ await pipeStream(
361
+ stream,
362
+ streamSimpleAnthropic(
363
+ {
364
+ ...model,
365
+ api: "anthropic-messages",
366
+ } as Model<"anthropic-messages">,
367
+ context,
368
+ { ...options, headers },
369
+ ),
370
+ );
371
+ return;
372
+ }
373
+
374
+ const { streamSimpleOpenAICompletions } =
375
+ await importPiAiSubpath<OpenAICompletionsStreamModule>(
376
+ "openai-completions",
377
+ );
378
+ await pipeStream(
379
+ stream,
380
+ streamSimpleOpenAICompletions(
381
+ {
382
+ ...model,
383
+ api: "openai-completions",
384
+ } as Model<"openai-completions">,
385
+ context,
386
+ { ...options, headers },
387
+ ),
388
+ );
389
+ } catch (error) {
390
+ const errorMessage = createErrorMessage(model, error);
391
+ stream.push({ type: "start", partial: errorMessage });
392
+ stream.push({ type: "error", reason: "error", error: errorMessage });
393
+ }
394
+ })();
395
+
396
+ return stream as unknown as AssistantMessageEventStream;
397
+ };
398
+ }