@chatpanel/gateway 0.6.99 → 0.6.100

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": "@chatpanel/gateway",
3
- "version": "0.6.99",
3
+ "version": "0.6.100",
4
4
  "description": "Local privacy gateway — redacts PII out of OpenAI/Anthropic API traffic before it reaches a model, then restores it in the reply. Point opencode, codex, aider, Claude Code, etc. at it.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/server.js CHANGED
@@ -63,7 +63,7 @@ import * as openai from './openai.js';
63
63
  import * as responses from './responses.js';
64
64
  import * as anthropic from './anthropic.js';
65
65
 
66
- export const VERSION = '0.6.99';
66
+ export const VERSION = '0.6.100';
67
67
 
68
68
  // WARM search tier — SQLite + FTS5 record store (falls back to an encrypted-JSON
69
69
  // store if SQLite can't load), fed by the extension's ingest sync + backup-ingest.
@@ -1061,7 +1061,8 @@ export function createGateway(cfg = loadConfig()) {
1061
1061
  // duplicates are dropped by seq, so nothing lands in the gap.
1062
1062
  let last = Number.isFinite(after) ? after : -1;
1063
1063
  sendEv({ seq: -1, type: 'hello', at: Date.now(), payload: { run: teamStore.get(id), after: last } });
1064
- const off = teamStore.watch(id, (ev) => { if (ev.seq > last) { last = ev.seq; sendEv(ev); } });
1064
+ // A stream event (a task's text delta) has no seq: it is never stored, so it is passed live and never replayed.
1065
+ const off = teamStore.watch(id, (ev) => { if (ev.seq == null) sendEv(ev); else if (ev.seq > last) { last = ev.seq; sendEv(ev); } });
1065
1066
  for (const ev of teamStore.eventsSince(id, last)) { last = ev.seq; sendEv(ev); }
1066
1067
  const beat = setInterval(() => { try { res.write(': keep-alive\n\n'); } catch { /* closed */ } }, 25_000);
1067
1068
  res.on('close', () => { off(); clearInterval(beat); });
@@ -123,3 +123,32 @@ export function isResumable(run) {
123
123
  if (RESUMABLE_RUN_STATUSES.includes(run.status)) return true;
124
124
  return !!run.stale && LIVE_RUN_STATUSES.includes(run.status); // its client went away mid-run
125
125
  }
126
+
127
+ /**
128
+ * The run's spend against its cap, as a board shows it: the record's last `run.usage` (or
129
+ * nothing spent yet) with `ms` measured LIVE for a run still going — the stored figure is as
130
+ * of the last task's end, and a board read "0 s" through a ten-minute research task.
131
+ */
132
+ export function spendOf(run, { now = Date.now() } = {}) {
133
+ const cap = run?.usage?.cap || run?.budget || null;
134
+ if (!cap || !Object.keys(cap).length) return null;
135
+ const spent = { tokens: 0, calls: 0, usd: 0, ms: 0, ...(run?.usage?.spent || {}) };
136
+ if (LIVE_RUN_STATUSES.includes(run?.status) && run?.startedAt) spent.ms = Math.max(spent.ms || 0, now - run.startedAt);
137
+ const pct = cap.tokens ? Math.min(100, Math.round(((spent.tokens || 0) / cap.tokens) * 100)) : cap.ms ? Math.min(100, Math.round(((spent.ms || 0) / cap.ms) * 100)) : null;
138
+ return { cap, spent, pct, exhausted: run?.usage?.exhausted || null };
139
+ }
140
+
141
+ const secs = (ms) => { const s = Math.max(0, Math.round((Number(ms) || 0) / 1000)); return s >= 60 ? `${Math.floor(s / 60)}m${String(s % 60).padStart(2, '0')}s` : `${s}s`; };
142
+ const num = (n) => (Number(n) || 0).toLocaleString('en-US');
143
+
144
+ /** One line: `1,240 / 40,000 tokens · 3 / 20 calls · 2m10s / 15m00s`. Only the capped dimensions. */
145
+ export function describeSpend(spend) {
146
+ if (!spend?.cap) return '';
147
+ const { cap, spent } = spend;
148
+ return [
149
+ cap.tokens ? `${num(spent.tokens)} / ${num(cap.tokens)} tokens` : '',
150
+ cap.calls ? `${num(spent.calls)} / ${num(cap.calls)} calls` : '',
151
+ cap.usd ? `$${(Number(spent.usd) || 0).toFixed(2)} / $${Number(cap.usd).toFixed(2)}` : '',
152
+ cap.ms ? `${secs(spent.ms)} / ${secs(cap.ms)}` : '',
153
+ ].filter(Boolean).join(' · ');
154
+ }
package/src/team-store.js CHANGED
@@ -124,14 +124,23 @@ export class TeamStore {
124
124
  if (!run) throw new Error(`no run ${id}`);
125
125
  const list = Array.isArray(events) ? events : [events];
126
126
  let seq = run.events.length;
127
+ let stored = 0;
127
128
  for (const e of list) {
128
129
  if (!e || typeof e !== 'object' || !e.type) continue;
129
130
  let bytes; try { bytes = Buffer.byteLength(JSON.stringify(e), 'utf8'); } catch { continue; }
130
131
  if (bytes > MAX_EVENT_BYTES) continue;
131
- if (run.events.length >= MAX_EVENTS_PER_RUN) break;
132
132
  const { type, at, runId: _r, ...payload } = e;
133
- const ev = { seq: seq++, type: String(type), at: Number(at) || this.now(), payload };
134
- run.events.push(ev);
133
+ // A STREAM IS NOT A FACT. A task's streamed text (`task.delta`) is folded into the record
134
+ // — the task's live text — and handed to the tail so the other client reads along, but it
135
+ // is never stored: the final text is on the record as a step. Deltas alone filled a run's
136
+ // event log in three minutes, and the store then dropped the task.done, the writer's
137
+ // start and the board that followed — a run that read "running" with a finished member.
138
+ const stream = String(type) === 'task.delta';
139
+ const ev = { seq: stream ? null : seq++, type: String(type), at: Number(at) || this.now(), payload };
140
+ // At the cap the RECORD still moves — the fold keeps status, tasks and the board true —
141
+ // only the replayable list stops growing, and says so.
142
+ if (!stream) { if (run.events.length < MAX_EVENTS_PER_RUN) run.events.push(ev); else { ev.seq = null; run.eventsTruncated = true; seq -= 1; } }
143
+ if (!stream) stored += 1;
135
144
  applyEvent(run, ev);
136
145
  // A finished task's fact goes to the member's scorecard — chained and attested there.
137
146
  if (this.scorecards && ev.type === 'task.scored') this.scorecards.fromRunEvent(ev, run);
@@ -139,7 +148,9 @@ export class TeamStore {
139
148
  if (this.engines && (ev.type === 'task.routed' || ev.type === 'task.reappointed' || ev.type === 'task.handoff' || ev.type === 'task.scored')) this.engines.fromRunEvent(ev, run);
140
149
  for (const fn of this.watchers.get(run.id) || []) { try { fn(ev); } catch { /* a dead watcher */ } }
141
150
  }
142
- this.save();
151
+ // A batch of nothing but stream text does not rewrite the store — the live text is
152
+ // persisted with the next fact (a step, a finding, the task's end).
153
+ if (stored) this.save();
143
154
  return this._view(run);
144
155
  }
145
156
  get(id, opts) { const r = this.runs.get(String(id || '')); return r ? this._view(r, opts) : null; }