aegiscode 7.0.25 → 7.0.26
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 +7 -3
- package/scripts/check.mjs +52 -1
- package/scripts/demo.mjs +94 -1
- package/scripts/predist.mjs +75 -1
- package/scripts/runtests.mjs +56 -1
- package/scripts/seal-gate.mjs +74 -1
- package/src/agents.js +139 -1
- package/src/app.js +2957 -1
- package/src/art.js +289 -1
- package/src/chatflow.js +3023 -1
- package/src/checkpoint.js +90 -1
- package/src/clipboard.js +424 -1
- package/src/cloudsync.js +528 -1
- package/src/commands.js +4089 -1
- package/src/config.js +581 -1
- package/src/connect.js +349 -1
- package/src/credentials.js +33 -1
- package/src/deps.js +232 -1
- package/src/devrun.js +187 -1
- package/src/diff.js +333 -1
- package/src/diffview.js +296 -1
- package/src/engine.js +297 -1
- package/src/entitlement.js +355 -1
- package/src/events.js +707 -1
- package/src/export.js +67 -1
- package/src/filepreview.js +153 -1
- package/src/fingerprint.js +592 -1
- package/src/format.js +259 -1
- package/src/fuzzy.js +119 -1
- package/src/history.js +423 -1
- package/src/init.js +175 -1
- package/src/input.js +139 -1
- package/src/keys.js +183 -1
- package/src/markdown.js +306 -1
- package/src/models.js +507 -1
- package/src/naming.js +57 -1
- package/src/overlays.js +462 -1
- package/src/panels.js +1418 -1
- package/src/ping.js +226 -1
- package/src/presence.js +238 -1
- package/src/render.js +516 -1
- package/src/screen.js +518 -1
- package/src/screens.js +896 -1
- package/src/secret.js +59 -1
- package/src/securefs.js +359 -1
- package/src/settings.js +155 -1
- package/src/shared.js +52 -1
- package/src/sharedpaths.js +53 -1
- package/src/signup.js +156 -1
- package/src/summarize.js +95 -1
- package/src/sysdiag.js +585 -1
- package/src/system.js +40 -1
- package/src/theme.js +570 -1
- package/src/title.js +220 -1
- package/src/todos.js +115 -1
- package/src/tokens.js +263 -1
- package/src/topup.js +210 -1
- package/src/update.js +191 -1
- package/vendor/client/aegis.js +2441 -1
- package/vendor/client/attest.js +518 -1
- package/vendor/client/authorship-key.js +410 -1
- package/vendor/client/auto-provision.js +134 -1
- package/vendor/client/brain-catalog.js +268 -1
- package/vendor/client/byok-catalog.js +199 -1
- package/vendor/client/catalog-client.js +487 -1
- package/vendor/client/conversation-export.js +177 -1
- package/vendor/client/credentials.js +494 -1
- package/vendor/client/device-fingerprint.js +141 -1
- package/vendor/client/drive-connect.js +110 -1
- package/vendor/client/drive-upload.js +234 -1
- package/vendor/client/env-file.js +1273 -1
- package/vendor/client/fingerprint.js +1064 -1
- package/vendor/client/foreign-memory.js +669 -1
- package/vendor/client/google-signin.js +133 -1
- package/vendor/client/hooks.js +430 -1
- package/vendor/client/model-discovery.js +373 -1
- package/vendor/client/naming.js +283 -1
- package/vendor/client/platform.js +937 -1
- package/vendor/client/prefs.js +351 -1
- package/vendor/client/presence-constants.js +107 -1
- package/vendor/client/presence.js +438 -1
- package/vendor/client/record-vault.js +1055 -3
- package/vendor/client/session-store.js +575 -1
- package/vendor/client/topup.js +297 -1
- package/vendor/client/touch.js +458 -1
- package/vendor/client/update.js +763 -1
- package/vendor/client/user-models.js +381 -1
- package/vendor/desktop/lib/local/agents.js +105 -1
- package/vendor/desktop/lib/local/attachments.js +276 -1
- package/vendor/desktop/lib/local/autonomous.js +1045 -1
- package/vendor/desktop/lib/local/claude-pro.js +1028 -1
- package/vendor/desktop/lib/local/context.js +125 -1
- package/vendor/desktop/lib/local/engine.js +3666 -1
- package/vendor/desktop/lib/local/entitlement.js +660 -1
- package/vendor/desktop/lib/local/git-scope.js +228 -1
- package/vendor/desktop/lib/local/local.js +2145 -1
- package/vendor/desktop/lib/local/prompt.js +280 -1
- package/vendor/desktop/lib/local/queue.js +560 -1
- package/vendor/desktop/lib/local/session-rounds.js +238 -1
- package/vendor/desktop/lib/local/spill.js +75 -1
- package/vendor/desktop/lib/local/turn-guard.js +300 -1
- package/vendor/desktop/lib/local/worktree-lock.js +235 -1
- package/vendor/desktop/lib/settings.js +890 -1
- package/vendor/desktop/renderer/usage.js +1239 -1
- package/vendor/hosts/install.js +638 -1
- package/vendor/hosts/openai-shim.js +515 -1
- package/vendor/hosts/spec.js +164 -1
- package/vendor/hosts/targets.js +346 -1
- package/vendor/mcp/server.js +221 -1
- package/vendor/mcp/tools.js +408 -1
- package/vendor/one/src/child.js +285 -1
- package/vendor/one/src/permissions.js +383 -1
- package/SOURCE-OFFER.txt +0 -9
|
@@ -1,3 +1,489 @@
|
|
|
1
1
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
2
|
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
3
|
-
|
|
3
|
+
// AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
|
|
4
|
+
// Conveying a modified copy, or running one as a network service, owes the
|
|
5
|
+
// corresponding source under this licence (§5/§13) — see LICENSING.md.
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The SERVER's full provider/model catalog, cached on disk, layered under the
|
|
10
|
+
* live payload.
|
|
11
|
+
*
|
|
12
|
+
* ── what this replaces ─────────────────────────────────────────────────────
|
|
13
|
+
*
|
|
14
|
+
* `client/byok-catalog.js` is a hand-written list of ids the server's catalog
|
|
15
|
+
* did not name (its own header says the durable fix is the server catalog). The
|
|
16
|
+
* server now has one — `services/catalog_store.py` in aegis1, seeded from
|
|
17
|
+
* `PROVIDER_CATALOG` and fed by the aggregators — and publishes it as a static
|
|
18
|
+
* artifact. This module is the client half: fetch it, cache it, and merge it
|
|
19
|
+
* into the BYOK payload.
|
|
20
|
+
*
|
|
21
|
+
* ── the three layers, and why the order is that order ──────────────────────
|
|
22
|
+
*
|
|
23
|
+
* 1. the LIVE payload from `/api/v1/byok/providers` — the server's truth
|
|
24
|
+
* about what *this* account's relay will accept. Nothing may contradict it.
|
|
25
|
+
* 2. the CACHED catalog artifact — ids the vendor serves
|
|
26
|
+
* that the live payload did not name. Cached precisely because it is the
|
|
27
|
+
* fallback for a call that failed or has not happened yet.
|
|
28
|
+
* 3. the USER's own `models.json` overlay — applied by
|
|
29
|
+
* `user-models.js`, last, because it is the most specific opinion.
|
|
30
|
+
*
|
|
31
|
+
* ── rules inherited from byok-catalog.js, all kept ─────────────────────────
|
|
32
|
+
*
|
|
33
|
+
* - NEVER invent a provider. Only a provider the live payload already lists
|
|
34
|
+
* is augmented. A row a client fabricated would invite a key for a slug the
|
|
35
|
+
* relay may not accept, and the failure would land on the user's first turn.
|
|
36
|
+
* - Additions are APPENDED and the server's order is preserved, so a
|
|
37
|
+
* provider's `default_model` — which server order puts first, and which
|
|
38
|
+
* pickers therefore treat as the default — stays first.
|
|
39
|
+
* - Idempotent and non-mutating. A payload that already carries every id comes
|
|
40
|
+
* back with the SAME provider objects, so a caller cannot tell it ran.
|
|
41
|
+
*
|
|
42
|
+
* ── why the cache is not the source of truth ───────────────────────────────
|
|
43
|
+
*
|
|
44
|
+
* A stale catalog is a list of ids that *were* valid. That is strictly better
|
|
45
|
+
* than an empty picker on a failed call, and strictly worse than the live
|
|
46
|
+
* answer — so this is stale-while-revalidate, never stale-instead-of. Every
|
|
47
|
+
* entry point reports its own freshness (`stale`) rather than presenting a
|
|
48
|
+
* week-old file as current, and nothing here ever blocks startup on a network
|
|
49
|
+
* call: `read()` is synchronous and returns `null` when there is no cache.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
const fs = require('fs');
|
|
53
|
+
const path = require('path');
|
|
54
|
+
const credentials = require('./credentials.js');
|
|
55
|
+
|
|
56
|
+
/** Cached artifact name, beside `models.json` in the data dir. */
|
|
57
|
+
const CATALOG_FILE = 'catalog.json';
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* How old a cache may be before callers should revalidate, in ms.
|
|
61
|
+
*
|
|
62
|
+
* The server's ingest is a daily job, so a day-old cache is normal and a
|
|
63
|
+
* week-old one is not news. Chosen to be longer than one missed cron run and
|
|
64
|
+
* shorter than anything a person would call "the catalog is wrong now".
|
|
65
|
+
*/
|
|
66
|
+
const STALE_AFTER_MS = 24 * 60 * 60 * 1000;
|
|
67
|
+
|
|
68
|
+
/** The data dir (`$AEGISCODE_HOME` or `~/.aegiscode`), from the ONE definition. */
|
|
69
|
+
function catalogDir(opts) {
|
|
70
|
+
return (opts && opts.dir) || credentials.aegisHome(opts && opts.env);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The absolute path of the cached catalog artifact. */
|
|
74
|
+
function catalogPath(opts) {
|
|
75
|
+
return path.join(catalogDir(opts), CATALOG_FILE);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Model ids out of a provider row in either shape (`string` or `{id}`). */
|
|
79
|
+
function modelIdsOf(provider) {
|
|
80
|
+
return (Array.isArray(provider && provider.models) ? provider.models : [])
|
|
81
|
+
.map((m) => (typeof m === 'string' ? m : m && m.id))
|
|
82
|
+
.map((m) => (typeof m === 'string' ? m.trim() : ''))
|
|
83
|
+
.filter(Boolean);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The provider array out of either shape the server sends (or `null`). */
|
|
87
|
+
function providerArray(providers) {
|
|
88
|
+
if (Array.isArray(providers)) return providers;
|
|
89
|
+
if (Array.isArray(providers && providers.providers)) return providers.providers;
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Index an artifact's `models` rows as provider → [model_id].
|
|
95
|
+
*
|
|
96
|
+
* Takes the shape `scripts/catalog_sync.py` writes: `{providers: [...],
|
|
97
|
+
* models: [{provider_id, model_id, status}, ...]}`. A retired row is skipped —
|
|
98
|
+
* adding an id the server has marked deprecated would offer a row that pins and
|
|
99
|
+
* then fails upstream, which is the failure `brain-catalog.js` exists to end.
|
|
100
|
+
*/
|
|
101
|
+
function indexCatalog(catalog) {
|
|
102
|
+
const out = Object.create(null);
|
|
103
|
+
const rows = Array.isArray(catalog && catalog.models) ? catalog.models : [];
|
|
104
|
+
for (const m of rows) {
|
|
105
|
+
if (!m || typeof m !== 'object') continue;
|
|
106
|
+
const pid = typeof m.provider_id === 'string' ? m.provider_id.trim() : '';
|
|
107
|
+
const mid = typeof m.model_id === 'string' ? m.model_id.trim() : '';
|
|
108
|
+
if (!pid || !mid) continue;
|
|
109
|
+
const status = typeof m.status === 'string' ? m.status.toLowerCase() : 'active';
|
|
110
|
+
if (status !== 'active') continue;
|
|
111
|
+
(out[pid] || (out[pid] = [])).push(mid);
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Read the cached artifact.
|
|
118
|
+
*
|
|
119
|
+
* Never throws. A corrupt, truncated or absent file returns `null` and is
|
|
120
|
+
* reported as such — the caller falls back to the live payload, which is always
|
|
121
|
+
* the better answer anyway. Reading a catalog must not be able to take down a
|
|
122
|
+
* startup path.
|
|
123
|
+
*
|
|
124
|
+
* @param {object} [opts] `{dir?, env?}` — `dir` makes this testable without a
|
|
125
|
+
* real data dir, exactly as `user-models.js` does it.
|
|
126
|
+
* @returns {{data: object, fetchedAt: number|null, ageMs: number|null,
|
|
127
|
+
* stale: boolean, path: string}|null}
|
|
128
|
+
*/
|
|
129
|
+
function read(opts) {
|
|
130
|
+
const file = catalogPath(opts);
|
|
131
|
+
let raw;
|
|
132
|
+
try {
|
|
133
|
+
raw = fs.readFileSync(file, 'utf8');
|
|
134
|
+
} catch (e) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
let data;
|
|
138
|
+
try {
|
|
139
|
+
data = JSON.parse(raw);
|
|
140
|
+
} catch (e) {
|
|
141
|
+
// A half-written file is the case the atomic write below exists to prevent;
|
|
142
|
+
// if one is found anyway, say so and degrade rather than throw.
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
if (!data || typeof data !== 'object' || !Array.isArray(data.providers)) return null;
|
|
146
|
+
|
|
147
|
+
// `fetched_at` is the artifact's own stamp (what the server knew, when).
|
|
148
|
+
// The file's mtime is the fallback for an artifact written without one.
|
|
149
|
+
let fetchedAt = null;
|
|
150
|
+
const stamp = data.snapshot && (data.snapshot.fetched_at || data.snapshot.fetchedAt);
|
|
151
|
+
if (typeof stamp === 'string' && stamp) {
|
|
152
|
+
const t = Date.parse(stamp);
|
|
153
|
+
if (!Number.isNaN(t)) fetchedAt = t;
|
|
154
|
+
}
|
|
155
|
+
if (fetchedAt === null) {
|
|
156
|
+
try {
|
|
157
|
+
fetchedAt = fs.statSync(file).mtimeMs;
|
|
158
|
+
} catch (e) {
|
|
159
|
+
fetchedAt = null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const ageMs = fetchedAt === null ? null : Math.max(0, Date.now() - fetchedAt);
|
|
163
|
+
|
|
164
|
+
// A 304 revalidation (`refresh` below) leaves `fetched_at` alone — that field
|
|
165
|
+
// says when the DATA was fetched, and rewriting it on a no-change response
|
|
166
|
+
// would make a month-old artifact claim to be new. But freshness is a
|
|
167
|
+
// separate question: if the server confirmed the content this morning, the
|
|
168
|
+
// cache IS current. So the newest of the two stamps decides `stale`, while
|
|
169
|
+
// `fetchedAt`/`ageMs` keep reporting the age of the data itself. Without
|
|
170
|
+
// this, every run would re-ask for a catalog the server just confirmed.
|
|
171
|
+
let revalidatedAt = null;
|
|
172
|
+
const rev = data.snapshot && data.snapshot.revalidated_at;
|
|
173
|
+
if (typeof rev === 'string' && rev) {
|
|
174
|
+
const t = Date.parse(rev);
|
|
175
|
+
if (!Number.isNaN(t)) revalidatedAt = t;
|
|
176
|
+
}
|
|
177
|
+
const freshFrom = revalidatedAt !== null && (fetchedAt === null || revalidatedAt > fetchedAt)
|
|
178
|
+
? revalidatedAt
|
|
179
|
+
: fetchedAt;
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
data,
|
|
183
|
+
fetchedAt,
|
|
184
|
+
revalidatedAt,
|
|
185
|
+
ageMs,
|
|
186
|
+
stale: freshFrom === null ? true : (Date.now() - freshFrom) > STALE_AFTER_MS,
|
|
187
|
+
path: file,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Write the artifact atomically, creating the data dir if needed.
|
|
193
|
+
*
|
|
194
|
+
* Temp file + `rename` because a client may read this while another process
|
|
195
|
+
* writes it, and a truncated JSON file is indistinguishable from a broken
|
|
196
|
+
* server — which would send every reader down the fallback path for no reason.
|
|
197
|
+
*
|
|
198
|
+
* @returns {string} the path written.
|
|
199
|
+
*/
|
|
200
|
+
function write(catalog, opts) {
|
|
201
|
+
const file = catalogPath(opts);
|
|
202
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
203
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
204
|
+
fs.writeFileSync(tmp, JSON.stringify(catalog), 'utf8');
|
|
205
|
+
fs.renameSync(tmp, file);
|
|
206
|
+
return file;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The live payload with the cached catalog's ids filled in.
|
|
211
|
+
*
|
|
212
|
+
* @param {Array<object>|object} providers the live `/api/v1/byok/providers`
|
|
213
|
+
* payload (array, or `{providers}`).
|
|
214
|
+
* @param {object|null} [catalog] an artifact as `read().data`. Pass `null` to
|
|
215
|
+
* mean "no cache" — deliberately explicit rather than "read the disk", so
|
|
216
|
+
* callers that already have a catalog do not get a silent second read.
|
|
217
|
+
* @param {object} [opts] `{dir?, env?}`; only used when `catalog` is undefined.
|
|
218
|
+
* @returns {Array<object>} a NEW array. Providers that gained nothing come back
|
|
219
|
+
* as the SAME objects, so a complete payload is byte-identical after this
|
|
220
|
+
* pass. An empty/absent payload returns `[]` — this never invents a list.
|
|
221
|
+
*/
|
|
222
|
+
function withCatalog(providers, catalog, opts) {
|
|
223
|
+
const raw = providerArray(providers);
|
|
224
|
+
if (!raw) return [];
|
|
225
|
+
|
|
226
|
+
let art = catalog;
|
|
227
|
+
if (art === undefined) {
|
|
228
|
+
const hit = read(opts);
|
|
229
|
+
art = hit ? hit.data : null;
|
|
230
|
+
}
|
|
231
|
+
const byProvider = indexCatalog(art);
|
|
232
|
+
if (!Object.keys(byProvider).length) return raw.slice();
|
|
233
|
+
|
|
234
|
+
const out = [];
|
|
235
|
+
for (const p of raw) {
|
|
236
|
+
if (!p || typeof p !== 'object') continue;
|
|
237
|
+
const id = typeof p.id === 'string' ? p.id.trim() : '';
|
|
238
|
+
// NEVER invent a provider: only a row the server already lists is extended.
|
|
239
|
+
const known = id ? byProvider[id] : null;
|
|
240
|
+
if (!known || !known.length) {
|
|
241
|
+
out.push(p);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
const have = new Set(modelIdsOf(p));
|
|
245
|
+
const missing = known.filter((m) => !have.has(m));
|
|
246
|
+
if (!missing.length) {
|
|
247
|
+
out.push(p); // same object: nothing to add, caller cannot tell it ran
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
const models = Array.isArray(p.models) ? p.models : [];
|
|
251
|
+
// Append, never prepend: the server's order puts `default_model` first and
|
|
252
|
+
// pickers take the first entry as the default.
|
|
253
|
+
out.push({ ...p, models: [...models, ...missing] });
|
|
254
|
+
}
|
|
255
|
+
return out;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Ids the cached catalog knows for a provider that the live payload did not
|
|
260
|
+
* name — for a host that wants to SAY what it added instead of only showing it.
|
|
261
|
+
*
|
|
262
|
+
* @returns {string[]} possibly empty; never throws.
|
|
263
|
+
*/
|
|
264
|
+
function addedModelsFor(providerId, providers, catalog, opts) {
|
|
265
|
+
const pid = typeof providerId === 'string' ? providerId.trim() : '';
|
|
266
|
+
if (!pid) return [];
|
|
267
|
+
let art = catalog;
|
|
268
|
+
if (art === undefined) {
|
|
269
|
+
const hit = read(opts);
|
|
270
|
+
art = hit ? hit.data : null;
|
|
271
|
+
}
|
|
272
|
+
const known = indexCatalog(art)[pid] || [];
|
|
273
|
+
const row = (providerArray(providers) || [])
|
|
274
|
+
.find((p) => p && typeof p.id === 'string' && p.id.trim() === pid);
|
|
275
|
+
const have = new Set(modelIdsOf(row));
|
|
276
|
+
return known.filter((m) => !have.has(m));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/* ── refresh ──────────────────────────────────────────────────────────────── */
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Most pages one refresh will walk, at `limit` rows each.
|
|
283
|
+
*
|
|
284
|
+
* The server clamps `limit` to 500, so 200 pages is ~100k models — far past
|
|
285
|
+
* anything the ingest currently sees, and therefore a runaway guard rather than
|
|
286
|
+
* a limit anyone will hit. Stopping is the right failure mode: a truncated
|
|
287
|
+
* cache is a *reported* stale one, while an unbounded loop against a server
|
|
288
|
+
* that never clears `has_more` would hang the refresh forever and write
|
|
289
|
+
* nothing at all.
|
|
290
|
+
*/
|
|
291
|
+
const MAX_PAGES = 200;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Rebuild the cached artifact from the paged catalog endpoint.
|
|
295
|
+
*
|
|
296
|
+
* ── why this takes a function instead of an HTTP client ────────────────────
|
|
297
|
+
*
|
|
298
|
+
* This module deliberately requires only `fs`, `path` and `credentials.js`.
|
|
299
|
+
* It is vendored into three hosts (`client/`, `cli/vendor/`, `desktop/`) and
|
|
300
|
+
* runs on startup paths, so a require of the API client here would drag the
|
|
301
|
+
* whole HTTP stack — and its config — into a file that only needs to read and
|
|
302
|
+
* write JSON. The caller owns the transport and passes it in.
|
|
303
|
+
*
|
|
304
|
+
* ── the contract the caller must honour ────────────────────────────────────
|
|
305
|
+
*
|
|
306
|
+
* `fetchPage({cursor, limit, etag})` resolves to one page:
|
|
307
|
+
*
|
|
308
|
+
* `{ models: [...], next_cursor?, has_more?, etag?, notModified? }`
|
|
309
|
+
*
|
|
310
|
+
* `notModified: true` (a 304, or an `If-None-Match` hit) means "the server's
|
|
311
|
+
* answer has not changed" — the cache is already correct and this returns
|
|
312
|
+
* `{ changed: false }` without touching the file. Rejecting is also fine: a
|
|
313
|
+
* failed refresh never disturbs a good cache, and this never throws.
|
|
314
|
+
*
|
|
315
|
+
* ── what is preserved from the previous artifact ───────────────────────────
|
|
316
|
+
*
|
|
317
|
+
* `providers`. The models endpoint does not carry provider rows, and dropping
|
|
318
|
+
* them would make a refreshed artifact strictly less useful than the one it
|
|
319
|
+
* replaced — `indexCatalog` would still work, but any host reading provider
|
|
320
|
+
* metadata would see it vanish on first refresh.
|
|
321
|
+
*
|
|
322
|
+
* @param {Function} fetchPage see above; required.
|
|
323
|
+
* @param {object} [opts] `{dir?, env?, limit?, etag?, maxPages?, now?}`.
|
|
324
|
+
* @returns {Promise<{ok: boolean, changed: boolean, written: string|null,
|
|
325
|
+
* pages: number, models: number, providers: number, staleKept: boolean,
|
|
326
|
+
* reason?: string}>} never rejects.
|
|
327
|
+
*/
|
|
328
|
+
async function refresh(fetchPage, opts) {
|
|
329
|
+
const o = opts || {};
|
|
330
|
+
const limit = o.limit || 500;
|
|
331
|
+
const maxPages = o.maxPages || MAX_PAGES;
|
|
332
|
+
const previous = read(o);
|
|
333
|
+
const prevData = (previous && previous.data) || null;
|
|
334
|
+
|
|
335
|
+
const models = [];
|
|
336
|
+
let cursor = null;
|
|
337
|
+
let pages = 0;
|
|
338
|
+
let etag = o.etag || null;
|
|
339
|
+
// The ETag to persist: taken from the FIRST page only. It identifies the
|
|
340
|
+
// whole response, so page two onward carries a different one and storing the
|
|
341
|
+
// last one would make every future revalidation a guaranteed miss.
|
|
342
|
+
let firstPageEtag = null;
|
|
343
|
+
let notModified = false;
|
|
344
|
+
|
|
345
|
+
try {
|
|
346
|
+
for (;;) {
|
|
347
|
+
const page = await fetchPage({ cursor, limit, etag });
|
|
348
|
+
if (!page || page.notModified) {
|
|
349
|
+
notModified = true;
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
if (pages === 0 && page.etag) firstPageEtag = page.etag;
|
|
353
|
+
const rows = Array.isArray(page.models) ? page.models : [];
|
|
354
|
+
for (const m of rows) {
|
|
355
|
+
if (m && typeof m === 'object') models.push(m);
|
|
356
|
+
}
|
|
357
|
+
pages += 1;
|
|
358
|
+
// A page that reports more but gives no cursor would loop forever
|
|
359
|
+
// re-fetching the same rows. Treat it as the end and say so via `ok`.
|
|
360
|
+
if (!page.has_more || !page.next_cursor) break;
|
|
361
|
+
if (pages >= maxPages) {
|
|
362
|
+
return _refreshResult({
|
|
363
|
+
ok: false, changed: false, pages, models: models.length,
|
|
364
|
+
providers: 0, staleKept: Boolean(prevData),
|
|
365
|
+
reason: `stopped after ${pages} pages; cache left untouched`,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
cursor = page.next_cursor;
|
|
369
|
+
// The ETag is only meaningful for the FIRST page: it identifies the whole
|
|
370
|
+
// response body, and page two onward has a different one.
|
|
371
|
+
etag = null;
|
|
372
|
+
}
|
|
373
|
+
} catch (err) {
|
|
374
|
+
// A refresh is best-effort. The previous artifact is still a valid (if
|
|
375
|
+
// older) answer, and `read()` reports its staleness, so nothing here
|
|
376
|
+
// escalates into a startup failure.
|
|
377
|
+
return _refreshResult({
|
|
378
|
+
ok: false, changed: false, pages, models: 0,
|
|
379
|
+
providers: 0, staleKept: Boolean(prevData),
|
|
380
|
+
reason: (err && err.message) || String(err),
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (notModified) {
|
|
385
|
+
// A 304 says "this content is still current", so the cache needs no data
|
|
386
|
+
// change — but its `fetched_at` is now misleading, and without a stamp
|
|
387
|
+
// every future run would re-ask. Record the REVALIDATION separately rather
|
|
388
|
+
// than overwriting `fetched_at`: that field answers "when was this data
|
|
389
|
+
// fetched from the vendor catalog", and resetting it on a 304 would make a
|
|
390
|
+
// month-old artifact claim to be new.
|
|
391
|
+
let written = null;
|
|
392
|
+
if (prevData) {
|
|
393
|
+
try {
|
|
394
|
+
const now = typeof o.now === 'function' ? o.now() : Date.now();
|
|
395
|
+
written = write({
|
|
396
|
+
...prevData,
|
|
397
|
+
snapshot: {
|
|
398
|
+
...(prevData.snapshot || {}),
|
|
399
|
+
revalidated_at: new Date(now).toISOString(),
|
|
400
|
+
...(firstPageEtag ? { etag: firstPageEtag } : {}),
|
|
401
|
+
},
|
|
402
|
+
}, o);
|
|
403
|
+
} catch {
|
|
404
|
+
written = null;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return _refreshResult({
|
|
408
|
+
ok: true, changed: false, pages: 0, models: 0, providers: 0,
|
|
409
|
+
staleKept: !written, written, reason: 'not-modified',
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
if (!models.length) {
|
|
413
|
+
// An empty catalog is the one result that must not overwrite a good cache:
|
|
414
|
+
// a server mid-ingest would otherwise replace a full artifact with nothing,
|
|
415
|
+
// and every picker would go empty on the next start.
|
|
416
|
+
return _refreshResult({
|
|
417
|
+
ok: false, changed: false, pages, models: 0, providers: 0,
|
|
418
|
+
staleKept: Boolean(prevData), reason: 'empty catalog; cache left untouched',
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const providers = (prevData && Array.isArray(prevData.providers))
|
|
423
|
+
? prevData.providers
|
|
424
|
+
: [];
|
|
425
|
+
const now = typeof o.now === 'function' ? o.now() : Date.now();
|
|
426
|
+
const artifact = {
|
|
427
|
+
schema_version: (prevData && prevData.schema_version) || 1,
|
|
428
|
+
providers,
|
|
429
|
+
models,
|
|
430
|
+
snapshot: {
|
|
431
|
+
// `read()` prefers this stamp over mtime, so it must be the time of the
|
|
432
|
+
// FETCH, not of the write — they are the same here, and writing it makes
|
|
433
|
+
// the age survive a copy that resets mtime.
|
|
434
|
+
fetched_at: new Date(now).toISOString(),
|
|
435
|
+
models_seen: models.length,
|
|
436
|
+
providers_seen: providers.length,
|
|
437
|
+
source: 'catalog-api',
|
|
438
|
+
// Persisted so the NEXT refresh can send If-None-Match and pay a 304
|
|
439
|
+
// instead of walking every page. Without this the revalidation path
|
|
440
|
+
// existed but could never be reached.
|
|
441
|
+
...(firstPageEtag ? { etag: firstPageEtag } : {}),
|
|
442
|
+
},
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
let written = null;
|
|
446
|
+
try {
|
|
447
|
+
written = write(artifact, o);
|
|
448
|
+
} catch (err) {
|
|
449
|
+
return _refreshResult({
|
|
450
|
+
ok: false, changed: false, pages, models: models.length,
|
|
451
|
+
providers: providers.length, staleKept: Boolean(prevData),
|
|
452
|
+
reason: (err && err.message) || String(err),
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return _refreshResult({
|
|
457
|
+
ok: true, changed: true, pages, models: models.length,
|
|
458
|
+
providers: providers.length, staleKept: false, written,
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** Normalise the refresh result so every exit reports the same keys. */
|
|
463
|
+
function _refreshResult(r) {
|
|
464
|
+
return {
|
|
465
|
+
ok: Boolean(r.ok),
|
|
466
|
+
changed: Boolean(r.changed),
|
|
467
|
+
written: r.written || null,
|
|
468
|
+
pages: r.pages || 0,
|
|
469
|
+
models: r.models || 0,
|
|
470
|
+
providers: r.providers || 0,
|
|
471
|
+
staleKept: Boolean(r.staleKept),
|
|
472
|
+
...(r.reason ? { reason: r.reason } : {}),
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
module.exports = {
|
|
478
|
+
CATALOG_FILE,
|
|
479
|
+
STALE_AFTER_MS,
|
|
480
|
+
MAX_PAGES,
|
|
481
|
+
catalogDir,
|
|
482
|
+
catalogPath,
|
|
483
|
+
indexCatalog,
|
|
484
|
+
read,
|
|
485
|
+
write,
|
|
486
|
+
refresh,
|
|
487
|
+
withCatalog,
|
|
488
|
+
addedModelsFor,
|
|
489
|
+
};
|
|
@@ -1,3 +1,179 @@
|
|
|
1
1
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
2
|
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
3
|
-
|
|
3
|
+
// AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
|
|
4
|
+
// Conveying a modified copy, or running one as a network service, owes the
|
|
5
|
+
// corresponding source under this licence (§5/§13) — see LICENSING.md.
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* conversation-export.js — the words at the TOP of a file that leaves this
|
|
10
|
+
* machine, saying who wrote it.
|
|
11
|
+
*
|
|
12
|
+
* ## Why this file exists
|
|
13
|
+
*
|
|
14
|
+
* A backup that lands in someone's Google Drive has to answer one question
|
|
15
|
+
* without the app that made it: **whose writing is this?** A transcript with no
|
|
16
|
+
* header is indistinguishable from a transcript anyone could have typed. So
|
|
17
|
+
* every file this app uploads carries an authorship stamp in its first screenful
|
|
18
|
+
* — the key tag, the account, which of the two scopes was chosen, and when it
|
|
19
|
+
* was taken.
|
|
20
|
+
*
|
|
21
|
+
* ## The two scopes, named the way a person would say them
|
|
22
|
+
*
|
|
23
|
+
* 'conversation' one conversation — a single chat, exactly as it happened
|
|
24
|
+
* 'memory' the whole memory base — every stored memory entry
|
|
25
|
+
*
|
|
26
|
+
* These are deliberately separate acts with separate buttons. "Back up my
|
|
27
|
+
* memory" and "save this conversation" are different intentions, and a UI that
|
|
28
|
+
* collapses them into one control makes the user guess which one they got.
|
|
29
|
+
*
|
|
30
|
+
* ## The honesty rule
|
|
31
|
+
*
|
|
32
|
+
* With no authorship key, the stamp says so in words — `no key — this copy is
|
|
33
|
+
* unsigned` — and never invents a tag. An unsigned export that admitted it is
|
|
34
|
+
* useful; one that displayed a plausible-looking φ_… would be a forgery with
|
|
35
|
+
* our name on it. `keyFingerprint` here is the 12-hex DERIVED tag
|
|
36
|
+
* (client/authorship-key.js `keyFingerprint(secret)`), never the secret itself:
|
|
37
|
+
* the tag proves two machines hold the same key without either one revealing it.
|
|
38
|
+
*
|
|
39
|
+
* Pure by construction — no `fs`, no clock of its own (`exportedAt` is passed
|
|
40
|
+
* in), no network — so the exact bytes are testable without a Drive account.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/** The scope names, in one place, because both the stamp and the UI say them. */
|
|
44
|
+
const SCOPE_CONVERSATION = 'conversation';
|
|
45
|
+
const SCOPE_MEMORY = 'memory';
|
|
46
|
+
|
|
47
|
+
/** What each scope is called in a sentence a person reads, not a token. */
|
|
48
|
+
const SCOPE_TEXT = {
|
|
49
|
+
[SCOPE_CONVERSATION]: 'one conversation',
|
|
50
|
+
[SCOPE_MEMORY]: 'the whole memory base',
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** `6a1f4091b189` -> `φ_6a1f4091b189`; anything falsy -> null, never a fake. */
|
|
54
|
+
function keyTag(keyFingerprint) {
|
|
55
|
+
const fp = typeof keyFingerprint === 'string' ? keyFingerprint.trim() : '';
|
|
56
|
+
return fp ? `φ_${fp}` : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The line that answers "who wrote this?". Unsigned is stated, not implied. */
|
|
60
|
+
function authorLine(keyFingerprint) {
|
|
61
|
+
const tag = keyTag(keyFingerprint);
|
|
62
|
+
return tag || 'no key — this copy is unsigned';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A filename that cannot escape the folder it is written into. */
|
|
66
|
+
function safeFileName(title, fallback) {
|
|
67
|
+
const base = String(title || fallback || 'aegis-export')
|
|
68
|
+
.normalize('NFKD')
|
|
69
|
+
.replace(/[^\w.-]+/g, '-')
|
|
70
|
+
.replace(/^-+|-+$/g, '')
|
|
71
|
+
.slice(0, 80);
|
|
72
|
+
return base || 'aegis-export';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The stamp every export starts with.
|
|
77
|
+
*
|
|
78
|
+
* Rendered as bold-label markdown lines rather than YAML front matter on
|
|
79
|
+
* purpose: front matter is machine food, and the first thing a human opens this
|
|
80
|
+
* file in is Drive's own preview, which shows the markdown as plain text. Bold
|
|
81
|
+
* labels read correctly in both.
|
|
82
|
+
*
|
|
83
|
+
* @param {{scope:string, keyFingerprint?:string|null, email?:string|null,
|
|
84
|
+
* exportedAt?:string, subject?:string|null, count?:number|null}} opts
|
|
85
|
+
* @returns {string} the header block, ending with `\n\n`
|
|
86
|
+
*/
|
|
87
|
+
function stamp({
|
|
88
|
+
scope,
|
|
89
|
+
keyFingerprint = null,
|
|
90
|
+
email = null,
|
|
91
|
+
exportedAt = null,
|
|
92
|
+
subject = null,
|
|
93
|
+
count = null,
|
|
94
|
+
} = {}) {
|
|
95
|
+
const tag = keyTag(keyFingerprint);
|
|
96
|
+
const lines = [];
|
|
97
|
+
if (subject) lines.push(`**What this is:** ${subject}`);
|
|
98
|
+
lines.push(`**Scope:** ${SCOPE_TEXT[scope] || String(scope || 'unknown')} (${scope || 'unknown'})`);
|
|
99
|
+
lines.push(`**Author:** ${authorLine(keyFingerprint)}`);
|
|
100
|
+
lines.push(`**Account:** ${email || 'not reported by the server'}`);
|
|
101
|
+
if (count !== null && count !== undefined) {
|
|
102
|
+
lines.push(`**Entries:** ${count}`);
|
|
103
|
+
}
|
|
104
|
+
lines.push(`**Exported:** ${exportedAt || 'timestamp not recorded'}`);
|
|
105
|
+
lines.push(
|
|
106
|
+
tag
|
|
107
|
+
? `**Sealed by:** ${tag} — this is a key tag, not the key. It proves the same machine wrote it.`
|
|
108
|
+
: '**Sealed by:** nothing — no authorship key is set on this machine, so this copy cannot prove it came from you.'
|
|
109
|
+
);
|
|
110
|
+
return `${lines.join('\n')}\n\n`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* One conversation, header + transcript.
|
|
115
|
+
*
|
|
116
|
+
* `transcript` is passed in already rendered (client/session-store.js
|
|
117
|
+
* `toMarkdown`) rather than imported here: this module stays free of the store
|
|
118
|
+
* and of `fs`, and the caller keeps one implementation of what a transcript
|
|
119
|
+
* looks like.
|
|
120
|
+
*
|
|
121
|
+
* @param {object} session the session record (for title/id only)
|
|
122
|
+
* @param {{transcript:string, keyFingerprint?:string|null, email?:string|null,
|
|
123
|
+
* exportedAt?:string, count?:number|null}} opts
|
|
124
|
+
* @returns {string}
|
|
125
|
+
*/
|
|
126
|
+
function conversationMarkdown(session, opts = {}) {
|
|
127
|
+
const title = (session && (session.title || session.id)) || 'Untitled conversation';
|
|
128
|
+
const header = stamp({
|
|
129
|
+
scope: SCOPE_CONVERSATION,
|
|
130
|
+
subject: null,
|
|
131
|
+
keyFingerprint: opts.keyFingerprint,
|
|
132
|
+
email: opts.email,
|
|
133
|
+
exportedAt: opts.exportedAt,
|
|
134
|
+
count: opts.count === undefined ? null : opts.count,
|
|
135
|
+
});
|
|
136
|
+
return `# ${title}\n\n${header}---\n\n${opts.transcript || ''}`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The whole memory base. `content` is the serialised entries, unchanged — the
|
|
141
|
+
* point of this call is the header above it, not a second export format.
|
|
142
|
+
*
|
|
143
|
+
* @param {string} content
|
|
144
|
+
* @param {{keyFingerprint?:string|null, email?:string|null, exportedAt?:string,
|
|
145
|
+
* count?:number|null}} opts
|
|
146
|
+
*/
|
|
147
|
+
function memoryMarkdown(content, opts = {}) {
|
|
148
|
+
const header = stamp({
|
|
149
|
+
scope: SCOPE_MEMORY,
|
|
150
|
+
subject: null,
|
|
151
|
+
keyFingerprint: opts.keyFingerprint,
|
|
152
|
+
email: opts.email,
|
|
153
|
+
exportedAt: opts.exportedAt,
|
|
154
|
+
count: opts.count === undefined ? null : opts.count,
|
|
155
|
+
});
|
|
156
|
+
return `${header}---\n\n${content || ''}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** `aegis-conversation-my-chat-2026-10-24T12-00-00Z.md` — one new file per save. */
|
|
160
|
+
function conversationFileName(session, exportedAt) {
|
|
161
|
+
const stampText = String(exportedAt || '')
|
|
162
|
+
.replace(/[:.]/g, '-')
|
|
163
|
+
.replace(/[^\w-]+/g, '');
|
|
164
|
+
const title = safeFileName(session && (session.title || session.id), 'conversation');
|
|
165
|
+
return stampText ? `${title}-${stampText}.md` : `${title}.md`;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
module.exports = {
|
|
169
|
+
SCOPE_CONVERSATION,
|
|
170
|
+
SCOPE_MEMORY,
|
|
171
|
+
SCOPE_TEXT,
|
|
172
|
+
keyTag,
|
|
173
|
+
authorLine,
|
|
174
|
+
safeFileName,
|
|
175
|
+
stamp,
|
|
176
|
+
conversationMarkdown,
|
|
177
|
+
memoryMarkdown,
|
|
178
|
+
conversationFileName,
|
|
179
|
+
};
|