@juanhuttemann/pi-deep-research 0.1.3 → 0.1.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/bridge.ts +21 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juanhuttemann/pi-deep-research",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Fact-checked, cited web research inside Pi: runs the deep-research CLI (drsh) on Pi's own model, with live progress, clarifying questions and plan review",
5
5
  "keywords": [
6
6
  "pi-package",
package/src/bridge.ts CHANGED
@@ -100,6 +100,9 @@ export async function startBridge(o: { model: Model<Api>; stream: StreamFn; sign
100
100
  });
101
101
  const outer = () => abort.abort();
102
102
  o.signal?.addEventListener("abort", outer, { once: true });
103
+ // A signal does not replay an abort to a listener added after it:
104
+ // a call that arrived once the run was stopped went to the provider.
105
+ if (o.signal?.aborted) abort.abort();
103
106
  bridge.calls++;
104
107
  const stream = o.stream(o.model, context, {
105
108
  signal: abort.signal,
@@ -183,6 +186,23 @@ async function collect(stream: AsyncIterable<AssistantMessageEvent>): Promise<Fi
183
186
  return final;
184
187
  }
185
188
 
189
+ // send writes data and, when the socket's buffer is full, waits for it to
190
+ // drain before the stream is read on. Written regardless, a client reading
191
+ // slowly had the whole answer queued in memory as fast as the provider sent
192
+ // it. A client that hangs up ends the wait too: drain would never come.
193
+ async function send(res: ServerResponse, data: string): Promise<void> {
194
+ if (res.write(data) || res.destroyed) return;
195
+ await new Promise<void>((resolve) => {
196
+ const done = () => {
197
+ res.off("drain", done);
198
+ res.off("close", done);
199
+ resolve();
200
+ };
201
+ res.on("drain", done);
202
+ res.on("close", done);
203
+ });
204
+ }
205
+
186
206
  // streamTo writes the answer as server-sent events. Headers wait for the
187
207
  // first event that is not a failure: a provider that refuses the call
188
208
  // (a bad key, a rate limit) does so before anything streams, and is then
@@ -198,7 +218,7 @@ async function streamTo(res: ServerResponse, stream: AsyncIterable<AssistantMess
198
218
  res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" });
199
219
  }
200
220
  for (const c of streamChunks(s, e)) {
201
- res.write(`data: ${JSON.stringify(c)}\n\n`);
221
+ await send(res, `data: ${JSON.stringify(c)}\n\n`);
202
222
  }
203
223
  if (e.type === "done") {
204
224
  final = e;