@chatpanel/gateway 0.6.110 → 0.8.0

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 (81) hide show
  1. package/README.md +24 -4
  2. package/bin/chatpanel-gateway-bin.mjs +1 -24
  3. package/bin/chatpanel-gateway.js +3 -97
  4. package/package.json +2 -7
  5. package/src/access-log-store.js +1 -45
  6. package/src/agent.js +2 -256
  7. package/src/anthropic.js +3 -83
  8. package/src/backup-decrypt.js +1 -45
  9. package/src/backup-ingest.js +6 -120
  10. package/src/bridge-supervisor.js +3 -212
  11. package/src/bridge.js +4 -252
  12. package/src/budget.js +1 -117
  13. package/src/config.js +1 -209
  14. package/src/configstore.js +1 -181
  15. package/src/connect-agents.js +22 -179
  16. package/src/diarize-engine.js +1 -188
  17. package/src/egress.js +2 -0
  18. package/src/engine-ledger-store.js +1 -150
  19. package/src/engine.js +1 -132
  20. package/src/entitlement-refresh.js +1 -84
  21. package/src/entitlement.js +1 -76
  22. package/src/extraction.js +4 -294
  23. package/src/freegate.js +1 -47
  24. package/src/gate.js +1 -75
  25. package/src/gateway-token.js +1 -60
  26. package/src/history-store.js +1 -251
  27. package/src/job.js +3 -148
  28. package/src/knowledge.js +3 -215
  29. package/src/local-status.js +6 -102
  30. package/src/log.js +1 -21
  31. package/src/mcp-cli.js +19 -113
  32. package/src/mcp.js +34 -546
  33. package/src/memory-store.js +1 -176
  34. package/src/memory.js +2 -642
  35. package/src/model-integrity.js +1 -82
  36. package/src/model-ledger.js +1 -229
  37. package/src/model-runtime.js +1 -37
  38. package/src/models.js +1 -92
  39. package/src/ner-engine.js +1 -318
  40. package/src/ner.js +1 -113
  41. package/src/observability.js +1 -123
  42. package/src/openai.js +2 -84
  43. package/src/ort.js +1 -52
  44. package/src/parakeet-engine.js +2 -228
  45. package/src/pocket-tts-engine.js +1 -520
  46. package/src/policy.js +2 -0
  47. package/src/prefs-store.js +1 -149
  48. package/src/project-store.js +1 -139
  49. package/src/project.js +1 -171
  50. package/src/recruit.js +3 -424
  51. package/src/recruiting.js +1 -93
  52. package/src/redact.js +2 -123
  53. package/src/responses.js +2 -80
  54. package/src/router.js +1 -292
  55. package/src/rrf.js +1 -93
  56. package/src/scorecard-store.js +1 -92
  57. package/src/scorecard.js +1 -336
  58. package/src/search-index.js +2 -134
  59. package/src/secure-fetch.js +1 -58
  60. package/src/sentencepiece.js +1 -184
  61. package/src/server.js +10 -2296
  62. package/src/service.js +11 -189
  63. package/src/shape.js +5 -121
  64. package/src/sqlite-store.js +3 -212
  65. package/src/stream.js +5 -182
  66. package/src/structured.js +4 -902
  67. package/src/stt-engine.js +1 -486
  68. package/src/stt-models.js +1 -144
  69. package/src/subject-kinds.js +1 -6
  70. package/src/subject-name.js +1 -97
  71. package/src/team-board.js +6 -338
  72. package/src/team-record.js +1 -226
  73. package/src/team-store.js +1 -261
  74. package/src/team.js +1 -303
  75. package/src/toolrelay.js +3 -122
  76. package/src/tts-engine.js +1 -453
  77. package/src/tts-models.js +1 -254
  78. package/src/tts-remote.js +1 -109
  79. package/src/tts-voice-resolve.js +1 -78
  80. package/src/tts-voices.js +1 -192
  81. package/src/update.js +1 -305
@@ -1,176 +1 @@
1
- // MEMORY on the gateway — the durable facts about the user, available to every local agent.
2
- //
3
- // The extension has its own copy in chrome.storage; this is the one CLI agents can reach.
4
- // Claude Code, Codex, OpenCode and anything else that speaks MCP get the user's standing
5
- // preferences through `chatpanel-gateway mcp`, so "call me Alex, never open with a preamble"
6
- // holds in the terminal exactly as it does in the side panel. That was the whole point of
7
- // putting memory in a shared contract rather than in the panel.
8
- //
9
- // TWO STORES, ONE TRUTH, because reconcile is idempotent. The extension pushes its memories
10
- // here and pulls back what the agents wrote (see the extension's warm-sync). A naive
11
- // two-way sync would duplicate on every pass; this one converges because `reconcile` keys on
12
- // the FACT (its slot, then its wording), not on a row id — so pushing the same memory twice
13
- // is a no-op and a corrected one supersedes rather than accumulating. The merge is the same
14
- // function on both sides, from the same file, which is the only reason that holds.
15
- //
16
- // ENCRYPTED AT REST with the same local key as the history store — this is the on-device
17
- // tier, and a local key is correct for it.
18
-
19
- import { readFileSync, writeFileSync, existsSync, mkdirSync, statSync } from 'node:fs';
20
- import { join, dirname } from 'node:path';
21
- import os from 'node:os';
22
- import { randomBytes, createCipheriv, createDecipheriv } from 'node:crypto';
23
- import {
24
- normalizeMemory, reconcile, recall, memoryBlock, matchForForget, pruneMemories, markUsed,
25
- isValidMemory, upcastMemory, DEFAULT_MAX_MEMORIES,
26
- } from './memory.js';
27
-
28
- const DIR = join(os.homedir(), '.chatpanel');
29
- const STORE_PATH = process.env.CHATPANEL_MEMORY_STORE || join(DIR, 'memory-store.enc');
30
- const KEY_PATH = process.env.CHATPANEL_HISTORY_KEY || join(DIR, 'history-key');
31
-
32
- const uid = () => `mem_${Date.now().toString(36)}${randomBytes(4).toString('hex')}`;
33
-
34
- // The same local key file the history store uses. One device key, not two: a second key file
35
- // is a second thing to lose, and both stores are the same tier with the same threat model.
36
- function loadOrCreateKey() {
37
- try {
38
- if (existsSync(KEY_PATH)) return Buffer.from(readFileSync(KEY_PATH, 'utf8').trim(), 'base64');
39
- } catch { /* regenerate below */ }
40
- const key = randomBytes(32);
41
- mkdirSync(dirname(KEY_PATH), { recursive: true });
42
- writeFileSync(KEY_PATH, key.toString('base64'), { mode: 0o600 });
43
- return key;
44
- }
45
-
46
- function encrypt(key, buf) {
47
- const iv = randomBytes(12);
48
- const cipher = createCipheriv('aes-256-gcm', key, iv);
49
- const ct = Buffer.concat([cipher.update(buf), cipher.final()]);
50
- return { v: 1, iv: iv.toString('base64'), tag: cipher.getAuthTag().toString('base64'), ct: ct.toString('base64') };
51
- }
52
-
53
- function decrypt(key, env) {
54
- const d = createDecipheriv('aes-256-gcm', key, Buffer.from(env.iv, 'base64'));
55
- d.setAuthTag(Buffer.from(env.tag, 'base64'));
56
- return Buffer.concat([d.update(Buffer.from(env.ct, 'base64')), d.final()]);
57
- }
58
-
59
- export class MemoryStore {
60
- constructor({ storePath = STORE_PATH } = {}) {
61
- this.storePath = storePath;
62
- this.memories = [];
63
- this._key = null;
64
- }
65
-
66
- get size() { return this.memories.length; }
67
-
68
- get bytes() {
69
- try { return existsSync(this.storePath) ? statSync(this.storePath).size : 0; } catch { return 0; }
70
- }
71
-
72
- key() {
73
- if (!this._key) this._key = loadOrCreateKey();
74
- return this._key;
75
- }
76
-
77
- // Fail-open on a missing or corrupt file — memory is an enhancement, and refusing to start
78
- // the gateway because one cache file is unreadable trades a small loss for a total one.
79
- // A record that fails validation is DROPPED, never repaired: it would otherwise be handed
80
- // to a model as a standing fact about the user.
81
- load() {
82
- try {
83
- if (!existsSync(this.storePath)) return this;
84
- const raw = JSON.parse(decrypt(this.key(), JSON.parse(readFileSync(this.storePath, 'utf8'))).toString('utf8'));
85
- this.memories = (Array.isArray(raw) ? raw : [])
86
- .map((m) => { try { return upcastMemory(m); } catch { return null; } })
87
- .filter((m) => m && isValidMemory(m));
88
- } catch {
89
- this.memories = [];
90
- }
91
- return this;
92
- }
93
-
94
- persistNow() {
95
- try {
96
- mkdirSync(dirname(this.storePath), { recursive: true });
97
- writeFileSync(this.storePath, JSON.stringify(encrypt(this.key(), Buffer.from(JSON.stringify(this.memories), 'utf8'))), { mode: 0o600 });
98
- } catch { /* a failed cache write must not fail the call that triggered it */ }
99
- }
100
-
101
- #commit(list) {
102
- const { kept } = pruneMemories(list, { now: Date.now(), max: DEFAULT_MAX_MEMORIES });
103
- this.memories = kept.sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0));
104
- this.persistNow();
105
- return this.memories;
106
- }
107
-
108
- list() { return this.memories; }
109
-
110
- /** Save one memory, reconciled against what is held. The only write path. */
111
- remember(input) {
112
- const now = Date.now();
113
- const { action, record, replaces } = reconcile(this.memories, input, { now, newId: uid });
114
- this.#commit([record, ...(replaces ? this.memories.filter((m) => m.id !== replaces.id) : this.memories)]);
115
- return { action, record, replaces };
116
- }
117
-
118
- /** Drop by id, or by however a person named it ("forget the Frankfurt thing"). */
119
- forget(query) {
120
- const q = String(query || '').trim();
121
- const hits = this.memories.some((m) => m.id === q)
122
- ? this.memories.filter((m) => m.id === q)
123
- : matchForForget(this.memories, q);
124
- if (!hits.length) return { removed: [] };
125
- const gone = new Set(hits.map((m) => m.id));
126
- this.#commit(this.memories.filter((m) => !gone.has(m.id)));
127
- return { removed: hits };
128
- }
129
-
130
- /** The memories a turn should carry, and the rendered block — the SAME ranking the panel uses. */
131
- recall({ text = '', scopes = ['global'], limit, maxChars } = {}) {
132
- const chosen = recall(this.memories, {
133
- text, scopes, now: Date.now(),
134
- ...(limit ? { limit } : {}),
135
- ...(maxChars ? { maxChars } : {}),
136
- });
137
- if (chosen.length) {
138
- this.memories = markUsed(this.memories, chosen.map((m) => m.id), { now: Date.now() });
139
- this.persistNow();
140
- }
141
- return { memories: chosen, block: memoryBlock(chosen) };
142
- }
143
-
144
- /**
145
- * Bulk merge from the extension's sync. Every incoming record goes through `reconcile`, so
146
- * a repeated push is a no-op rather than a doubling — that idempotence is what makes the
147
- * two-way sync safe.
148
- */
149
- bulk({ upserts = [], removes = [] } = {}) {
150
- for (const id of removes) {
151
- const gone = new Set([String(id)]);
152
- this.memories = this.memories.filter((m) => !gone.has(m.id));
153
- }
154
- let merged = 0;
155
- for (const raw of upserts) {
156
- let candidate;
157
- try { candidate = normalizeMemory(raw, { now: Date.now(), newId: uid }); } catch { continue; }
158
- const { action, record, replaces } = reconcile(this.memories, candidate, { now: Date.now(), newId: uid });
159
- if (action === 'duplicate') continue;
160
- this.#commit([record, ...(replaces ? this.memories.filter((m) => m.id !== replaces.id) : this.memories)]);
161
- merged += 1;
162
- }
163
- if (removes.length && !merged) this.#commit(this.memories);
164
- return { size: this.memories.length, merged };
165
- }
166
-
167
- clear() {
168
- const dropped = this.memories.length;
169
- this.#commit([]);
170
- return dropped;
171
- }
172
- }
173
-
174
- export async function createMemoryStore(opts) {
175
- return new MemoryStore(opts).load();
176
- }
1
+ import{readFileSync as d,writeFileSync as l,existsSync as c,mkdirSync as y,statSync as A}from"node:fs";import{join as a,dirname as p}from"node:path";import b from"node:os";import{randomBytes as h,createCipheriv as _,createDecipheriv as M}from"node:crypto";import{normalizeMemory as P,reconcile as g,recall as D,memoryBlock as E,matchForForget as O,pruneMemories as T,markUsed as v,isValidMemory as B,upcastMemory as N,DEFAULT_MAX_MEMORIES as I}from"./memory.js";const S=a(b.homedir(),".chatpanel"),R=process.env.CHATPANEL_MEMORY_STORE||a(S,"memory-store.enc"),m=process.env.CHATPANEL_HISTORY_KEY||a(S,"history-key"),u=()=>`mem_${Date.now().toString(36)}${h(4).toString("hex")}`;function F(){try{if(c(m))return Buffer.from(d(m,"utf8").trim(),"base64")}catch{}const o=h(32);return y(p(m),{recursive:!0}),l(m,o.toString("base64"),{mode:384}),o}function H(o,t){const e=h(12),i=_("aes-256-gcm",o,e),s=Buffer.concat([i.update(t),i.final()]);return{v:1,iv:e.toString("base64"),tag:i.getAuthTag().toString("base64"),ct:s.toString("base64")}}function x(o,t){const e=M("aes-256-gcm",o,Buffer.from(t.iv,"base64"));return e.setAuthTag(Buffer.from(t.tag,"base64")),Buffer.concat([e.update(Buffer.from(t.ct,"base64")),e.final()])}class z{constructor({storePath:t=R}={}){this.storePath=t,this.memories=[],this._key=null}get size(){return this.memories.length}get bytes(){try{return c(this.storePath)?A(this.storePath).size:0}catch{return 0}}key(){return this._key||(this._key=F()),this._key}load(){try{if(!c(this.storePath))return this;const t=JSON.parse(x(this.key(),JSON.parse(d(this.storePath,"utf8"))).toString("utf8"));this.memories=(Array.isArray(t)?t:[]).map(e=>{try{return N(e)}catch{return null}}).filter(e=>e&&B(e))}catch{this.memories=[]}return this}persistNow(){try{y(p(this.storePath),{recursive:!0}),l(this.storePath,JSON.stringify(H(this.key(),Buffer.from(JSON.stringify(this.memories),"utf8"))),{mode:384})}catch{}}#e(t){const{kept:e}=T(t,{now:Date.now(),max:I});return this.memories=e.sort((i,s)=>(s.updatedAt||0)-(i.updatedAt||0)),this.persistNow(),this.memories}list(){return this.memories}remember(t){const e=Date.now(),{action:i,record:s,replaces:r}=g(this.memories,t,{now:e,newId:u});return this.#e([s,...r?this.memories.filter(n=>n.id!==r.id):this.memories]),{action:i,record:s,replaces:r}}forget(t){const e=String(t||"").trim(),i=this.memories.some(r=>r.id===e)?this.memories.filter(r=>r.id===e):O(this.memories,e);if(!i.length)return{removed:[]};const s=new Set(i.map(r=>r.id));return this.#e(this.memories.filter(r=>!s.has(r.id))),{removed:i}}recall({text:t="",scopes:e=["global"],limit:i,maxChars:s}={}){const r=D(this.memories,{text:t,scopes:e,now:Date.now(),...i?{limit:i}:{},...s?{maxChars:s}:{}});return r.length&&(this.memories=v(this.memories,r.map(n=>n.id),{now:Date.now()}),this.persistNow()),{memories:r,block:E(r)}}bulk({upserts:t=[],removes:e=[]}={}){for(const s of e){const r=new Set([String(s)]);this.memories=this.memories.filter(n=>!r.has(n.id))}let i=0;for(const s of t){let r;try{r=P(s,{now:Date.now(),newId:u})}catch{continue}const{action:n,record:w,replaces:f}=g(this.memories,r,{now:Date.now(),newId:u});n!=="duplicate"&&(this.#e([w,...f?this.memories.filter(k=>k.id!==f.id):this.memories]),i+=1)}return e.length&&!i&&this.#e(this.memories),{size:this.memories.length,merged:i}}clear(){const t=this.memories.length;return this.#e([]),t}}async function U(o){return new z(o).load()}export{z as MemoryStore,U as createMemoryStore};