@chatpanel/gateway 0.6.99 → 0.6.101
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 +1 -1
- package/src/server.js +3 -2
- package/src/team-board.js +1 -1
- package/src/team-record.js +82 -0
- package/src/team-store.js +15 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chatpanel/gateway",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.101",
|
|
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.
|
|
66
|
+
export const VERSION = '0.6.101';
|
|
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
|
-
|
|
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); });
|
package/src/team-board.js
CHANGED
|
@@ -235,7 +235,7 @@ export function createBoard({ now = () => Date.now(), newId = null, state = null
|
|
|
235
235
|
function asFinding(st) {
|
|
236
236
|
return (p) => {
|
|
237
237
|
const t = st.threads.find((x) => x.id === p.threadId);
|
|
238
|
-
return { id: p.id, kind: p.finding?.kind || 'claim', text: p.text, refs: p.refs || [], confidence: p.finding?.confidence ?? null, role: p.by, taskId: t?.taskId || null, at: p.at, status: p.status };
|
|
238
|
+
return { id: p.id, kind: p.finding?.kind || 'claim', text: p.text, refs: p.refs || [], confidence: p.finding?.confidence ?? null, role: p.by, taskId: t?.taskId || null, at: p.at, status: p.status, ...(p.finding?.prior ? { prior: true } : {}) };
|
|
239
239
|
};
|
|
240
240
|
}
|
|
241
241
|
|
package/src/team-record.js
CHANGED
|
@@ -123,3 +123,85 @@ 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
|
+
// The clock runs while the client is writing, and STOPS where it stopped: a run whose
|
|
137
|
+
// record says `running` because its client died (or its end never landed) is not still
|
|
138
|
+
// spending — it read "18m13s / 5m00s" and counting for a member that had finished.
|
|
139
|
+
const st = runState(run, { now });
|
|
140
|
+
if (LIVE_RUN_STATUSES.includes(run?.status) && run?.startedAt) spent.ms = Math.max(spent.ms || 0, (st.key === 'stalled' ? Number(run.lastEventAt) || now : now) - run.startedAt);
|
|
141
|
+
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;
|
|
142
|
+
const over = BUDGET_KEYS.filter((k) => cap[k] && (spent[k] || 0) >= cap[k]);
|
|
143
|
+
return { cap, spent, pct, exhausted: run?.usage?.exhausted || over[0] || null, over };
|
|
144
|
+
}
|
|
145
|
+
const BUDGET_KEYS = ['tokens', 'calls', 'usd', 'ms'];
|
|
146
|
+
|
|
147
|
+
export const STALLED_AFTER_MS = 2 * 60_000;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* What a run IS right now, for a person: the record's status read against the clock. A
|
|
151
|
+
* record that says `running` with no event for minutes is STALLED — its client stopped
|
|
152
|
+
* writing (died, or its end never landed) — not running. `{ key, label, tone, detail }`;
|
|
153
|
+
* `tone` is the chip: on · warn · ok · err · muted.
|
|
154
|
+
*/
|
|
155
|
+
export function runState(run, { now = Date.now(), stalledAfterMs = STALLED_AFTER_MS } = {}) {
|
|
156
|
+
const s = String(run?.status || 'planning');
|
|
157
|
+
const quiet = Number.isFinite(run?.quietMs) ? run.quietMs : Math.max(0, now - (Number(run?.lastEventAt) || now));
|
|
158
|
+
const agoText = (ms) => { const sec = Math.round(ms / 1000); return sec < 60 ? `${sec} s` : sec < 3600 ? `${Math.round(sec / 60)} min` : `${Math.round(sec / 3600)} h`; };
|
|
159
|
+
if (s === 'waiting') return { key: 'waiting', label: 'waiting on you', tone: 'warn', detail: '' };
|
|
160
|
+
if (LIVE_RUN_STATUSES.includes(s)) {
|
|
161
|
+
if (run?.stale === true || quiet > stalledAfterMs) return { key: 'stalled', label: 'stalled', tone: 'err', detail: `no events for ${agoText(quiet)} — its client stopped writing; Resume here picks it up from the record` };
|
|
162
|
+
return { key: 'running', label: s === 'merging' ? 'merging' : s === 'planning' ? 'planning' : 'running', tone: 'on', detail: `last event ${agoText(quiet)} ago` };
|
|
163
|
+
}
|
|
164
|
+
if (s === 'completed') return { key: 'done', label: 'done', tone: 'ok', detail: '' };
|
|
165
|
+
if (s === 'partial') return { key: 'partial', label: 'done with failures', tone: 'warn', detail: 'a member failed; the rest merged' };
|
|
166
|
+
if (s === 'answered') return { key: 'answered', label: 'answered — resume to continue', tone: 'warn', detail: '' };
|
|
167
|
+
if (s === 'over-budget') return { key: 'over-budget', label: 'over budget', tone: 'err', detail: 'stopped with what it had' };
|
|
168
|
+
if (s === 'stopped') return { key: 'stopped', label: 'stopped', tone: 'muted', detail: '' };
|
|
169
|
+
if (s === 'failed') return { key: 'failed', label: 'failed', tone: 'err', detail: '' };
|
|
170
|
+
return { key: s, label: s, tone: 'muted', detail: '' };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Earlier runs whose work a new run should read before repeating it (§12.2.6, the librarian's
|
|
175
|
+
* first step): the same team (or any, when `team` is empty), a request that says the same
|
|
176
|
+
* thing (word overlap ≥ `minSimilarity`), findings on the record, not the run itself, newest
|
|
177
|
+
* first. `runs` is the store's list (`findings` is a count there). Returns
|
|
178
|
+
* `[{ id, at, similarity, findings }]`; the host fetches the record for the findings.
|
|
179
|
+
*/
|
|
180
|
+
export function priorWorkFor(runs, { team = '', request = '', excludeId = null, minSimilarity = 0.6, maxAgeMs = 7 * 24 * 3600_000, now = Date.now(), limit = 3 } = {}) {
|
|
181
|
+
const words = (t) => new Set(String(t || '').toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, ' ').split(/\s+/).filter((w) => w.length > 2));
|
|
182
|
+
const want = words(request);
|
|
183
|
+
if (!want.size) return [];
|
|
184
|
+
const sim = (t) => { const have = words(t); if (!have.size) return 0; let hit = 0; for (const w of want) if (have.has(w)) hit += 1; return hit / Math.max(want.size, have.size); };
|
|
185
|
+
return (runs || [])
|
|
186
|
+
.filter((r) => r && r.id && r.id !== excludeId && (!team || r.team === team) && !LIVE_RUN_STATUSES.includes(r.status) && (Number(r.findings) || (Array.isArray(r.board) ? r.board.length : 0)) > 0 && now - (Number(r.createdAt) || 0) <= maxAgeMs)
|
|
187
|
+
.map((r) => ({ id: r.id, at: r.createdAt, similarity: Math.round(sim(r.request) * 100) / 100, findings: Number(r.findings) || (Array.isArray(r.board) ? r.board.length : 0), status: r.status }))
|
|
188
|
+
.filter((r) => r.similarity >= minSimilarity)
|
|
189
|
+
.sort((a, b) => b.similarity - a.similarity || b.at - a.at)
|
|
190
|
+
.slice(0, limit);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
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`; };
|
|
194
|
+
const num = (n) => (Number(n) || 0).toLocaleString('en-US');
|
|
195
|
+
|
|
196
|
+
/** One line: `1,240 / 40,000 tokens · 3 / 20 calls · 2m10s / 15m00s`. Only the capped dimensions. */
|
|
197
|
+
export function describeSpend(spend) {
|
|
198
|
+
if (!spend?.cap) return '';
|
|
199
|
+
const { cap, spent } = spend;
|
|
200
|
+
const over = (k) => ((spend.over || []).includes(k) ? ' (over)' : '');
|
|
201
|
+
return [
|
|
202
|
+
cap.tokens ? `${num(spent.tokens)} / ${num(cap.tokens)} tokens${over('tokens')}` : '',
|
|
203
|
+
cap.calls ? `${num(spent.calls)} / ${num(cap.calls)} calls${over('calls')}` : '',
|
|
204
|
+
cap.usd ? `$${(Number(spent.usd) || 0).toFixed(2)} / $${Number(cap.usd).toFixed(2)}${over('usd')}` : '',
|
|
205
|
+
cap.ms ? `${secs(spent.ms)} / ${secs(cap.ms)}${over('ms')}` : '',
|
|
206
|
+
].filter(Boolean).join(' · ');
|
|
207
|
+
}
|
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
|
-
|
|
134
|
-
|
|
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
|
-
|
|
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; }
|