Skip to content

Commit f96ac97

Browse files
fix: avoid console usage
1 parent 1ca6677 commit f96ac97

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/core/streaming.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { findDoubleNewlineIndex, LineDecoder } from '../internal/decoders/line';
55
import { ReadableStreamToAsyncIterable } from '../internal/shims';
66
import { isAbortError } from '../internal/errors';
77
import { encodeUTF8 } from '../internal/utils/bytes';
8+
import { loggerFor } from '../internal/utils/log';
9+
import type { Opencode } from '../client';
810

911
type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
1012

@@ -16,16 +18,24 @@ export type ServerSentEvent = {
1618

1719
export class Stream<Item> implements AsyncIterable<Item> {
1820
controller: AbortController;
21+
#client: Opencode | undefined;
1922

2023
constructor(
2124
private iterator: () => AsyncIterator<Item>,
2225
controller: AbortController,
26+
client?: Opencode,
2327
) {
2428
this.controller = controller;
29+
this.#client = client;
2530
}
2631

27-
static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item> {
32+
static fromSSEResponse<Item>(
33+
response: Response,
34+
controller: AbortController,
35+
client?: Opencode,
36+
): Stream<Item> {
2837
let consumed = false;
38+
const logger = client ? loggerFor(client) : console;
2939

3040
async function* iterator(): AsyncIterator<Item, any, undefined> {
3141
if (consumed) {
@@ -38,8 +48,8 @@ export class Stream<Item> implements AsyncIterable<Item> {
3848
try {
3949
yield JSON.parse(sse.data);
4050
} catch (e) {
41-
console.error(`Could not parse message into JSON:`, sse.data);
42-
console.error(`From chunk:`, sse.raw);
51+
logger.error(`Could not parse message into JSON:`, sse.data);
52+
logger.error(`From chunk:`, sse.raw);
4353
throw e;
4454
}
4555
}
@@ -54,14 +64,18 @@ export class Stream<Item> implements AsyncIterable<Item> {
5464
}
5565
}
5666

57-
return new Stream(iterator, controller);
67+
return new Stream(iterator, controller, client);
5868
}
5969

6070
/**
6171
* Generates a Stream from a newline-separated ReadableStream
6272
* where each item is a JSON value.
6373
*/
64-
static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item> {
74+
static fromReadableStream<Item>(
75+
readableStream: ReadableStream,
76+
controller: AbortController,
77+
client?: Opencode,
78+
): Stream<Item> {
6579
let consumed = false;
6680

6781
async function* iterLines(): AsyncGenerator<string, void, unknown> {
@@ -101,7 +115,7 @@ export class Stream<Item> implements AsyncIterable<Item> {
101115
}
102116
}
103117

104-
return new Stream(iterator, controller);
118+
return new Stream(iterator, controller, client);
105119
}
106120

107121
[Symbol.asyncIterator](): AsyncIterator<Item> {
@@ -131,8 +145,8 @@ export class Stream<Item> implements AsyncIterable<Item> {
131145
};
132146

133147
return [
134-
new Stream(() => teeIterator(left), this.controller),
135-
new Stream(() => teeIterator(right), this.controller),
148+
new Stream(() => teeIterator(left), this.controller, this.#client),
149+
new Stream(() => teeIterator(right), this.controller, this.#client),
136150
];
137151
}
138152

src/internal/parse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export async function defaultParseResponse<T>(client: Opencode, props: APIRespon
2424
// that if you set `stream: true` the response type must also be `Stream<T>`
2525

2626
if (props.options.__streamClass) {
27-
return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;
27+
return props.options.__streamClass.fromSSEResponse(response, props.controller, client) as any;
2828
}
2929

30-
return Stream.fromSSEResponse(response, props.controller) as any;
30+
return Stream.fromSSEResponse(response, props.controller, client) as any;
3131
}
3232

3333
// fetch refuses to read the body when the status code is 204.

0 commit comments

Comments
 (0)