@juanhuttemann/pi-deep-research 0.1.3 → 0.1.5

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.
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.5",
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;
package/src/result.ts CHANGED
@@ -16,6 +16,16 @@ export interface Blocked {
16
16
  failed: string[];
17
17
  }
18
18
 
19
+ // Figure is a computation the program ran over the claims' values: its
20
+ // result when every input passed the fact-check, or why it did not.
21
+ export interface Figure {
22
+ id: string;
23
+ label: string;
24
+ value?: string;
25
+ formula?: string;
26
+ reason?: string;
27
+ }
28
+
19
29
  export interface Digest {
20
30
  question: string;
21
31
  depth: string;
@@ -28,6 +38,7 @@ export interface Digest {
28
38
  verdicts: Record<string, number>;
29
39
  unconfirmed: Unconfirmed[];
30
40
  blocked: Blocked[];
41
+ figures: Figure[];
31
42
  gaps: string[];
32
43
  sources: number;
33
44
  cited: number;
@@ -40,7 +51,8 @@ interface RunJSON {
40
51
  confidence?: string;
41
52
  error?: string;
42
53
  analysis?: {
43
- claims?: { id: string; claim: string; status?: string }[];
54
+ claims?: { id: string; claim: string; status?: string; note?: string; computed?: string }[];
55
+ computations?: { id: string; label?: string; result?: string; unit?: string; formula?: string; valid?: boolean; reason?: string }[];
44
56
  recommendations?: Rec[];
45
57
  conclusions?: Rec[];
46
58
  gaps?: string[] | null;
@@ -75,6 +87,18 @@ export function digest(raw: unknown): Digest {
75
87
  const unconfirmed = verdicts
76
88
  .filter((v) => v.status !== "supported")
77
89
  .map((v) => ({ id: v.id, claim: v.claim || claims.get(v.id) || "", status: v.status, reason: v.reason }));
90
+ // A computed claim has no verdict: the program decided it against its
91
+ // computation, and its status and note say how.
92
+ for (const c of j.analysis?.claims ?? []) {
93
+ if (c.computed && c.status && c.status !== "supported") {
94
+ unconfirmed.push({ id: c.id, claim: c.claim, status: c.status, reason: c.note });
95
+ }
96
+ }
97
+ const figures = (j.analysis?.computations ?? []).map((m) =>
98
+ m.valid
99
+ ? { id: m.id, label: m.label ?? "", value: [m.result, m.unit].filter(Boolean).join(" "), formula: m.formula }
100
+ : { id: m.id, label: m.label ?? "", reason: m.reason || "not computed" },
101
+ );
78
102
  const blocked: Blocked[] = [];
79
103
  for (const [kind, list] of [
80
104
  ["recommendation", j.analysis?.recommendations],
@@ -94,6 +118,7 @@ export function digest(raw: unknown): Digest {
94
118
  verdicts: counts,
95
119
  unconfirmed,
96
120
  blocked,
121
+ figures,
97
122
  gaps: j.analysis?.gaps ?? [],
98
123
  sources: citations.length,
99
124
  cited: citations.filter((c) => c.cited).length,
@@ -120,6 +145,7 @@ const rules = [
120
145
  "Answer from this report and keep its citations.",
121
146
  "Say the confidence it gives.",
122
147
  "Say which claims the fact-check did not confirm, and do not present an unconfirmed claim or a blocked recommendation or conclusion as fact.",
148
+ "State a figure only as the report or a COMPUTED line gives it; do no arithmetic of your own.",
123
149
  "Do not add facts the run did not establish; offer research_ask for a question its pages may answer, or a new run.",
124
150
  "Give the user the artifact paths.",
125
151
  ];
@@ -143,6 +169,14 @@ function digestLines(d: Digest): string[] {
143
169
  for (const b of d.blocked) {
144
170
  out.push(`- BLOCKED ${b.kind}, not established${b.failed.length ? ` (failed: ${b.failed.join(", ")})` : ""}: ${b.text}`);
145
171
  }
172
+ for (const f of d.figures) {
173
+ const name = f.label ? `${f.id} (${f.label})` : f.id;
174
+ out.push(
175
+ f.value
176
+ ? `- COMPUTED by the program: ${name} = ${f.value}, from ${f.formula}`
177
+ : `- NOT COMPUTED, not established: ${name}: ${f.reason}`,
178
+ );
179
+ }
146
180
  if (d.gaps.length) {
147
181
  out.push(`Gaps the run could not close: ${d.gaps.join("; ")}`);
148
182
  }