@qvac/core 0.11.1 → 0.11.3

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 (66) hide show
  1. package/CHANGELOG.md +88 -1
  2. package/README.md +19 -16
  3. package/dist-lib/lib/api/chunks.js +4 -2
  4. package/dist-lib/lib/api/rag.js +7 -5
  5. package/dist-lib/lib/assistant/history.d.ts +1 -0
  6. package/dist-lib/lib/assistant/history.js +1 -17
  7. package/dist-lib/lib/assistant/message.d.ts +4 -1
  8. package/dist-lib/lib/assistant/pdf-budget.d.ts +10 -2
  9. package/dist-lib/lib/assistant/pdf-budget.js +10 -11
  10. package/dist-lib/lib/assistant/pdf-pages.d.ts +18 -0
  11. package/dist-lib/lib/assistant/pdf-pages.js +75 -0
  12. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  13. package/dist-lib/lib/assistant-tree.js +4 -2
  14. package/dist-lib/lib/assistant.d.ts +10 -6
  15. package/dist-lib/lib/assistant.js +205 -140
  16. package/dist-lib/lib/compaction.d.ts +2 -1
  17. package/dist-lib/lib/compaction.js +14 -4
  18. package/dist-lib/lib/constants.d.ts +3 -0
  19. package/dist-lib/lib/constants.js +3 -0
  20. package/dist-lib/lib/core/chat-activity.js +12 -15
  21. package/dist-lib/lib/core/chunk.d.ts +9 -1
  22. package/dist-lib/lib/core/chunk.js +42 -8
  23. package/dist-lib/lib/core/document-attachment.d.ts +6 -3
  24. package/dist-lib/lib/core/document-attachment.js +10 -5
  25. package/dist-lib/lib/core/files/attachments.js +3 -3
  26. package/dist-lib/lib/core/files/index.d.ts +1 -1
  27. package/dist-lib/lib/core/files/index.js +3 -2
  28. package/dist-lib/lib/core/files/processing/index.d.ts +1 -0
  29. package/dist-lib/lib/core/files/processing/index.js +8 -0
  30. package/dist-lib/lib/core/files/processing/settle.d.ts +1 -0
  31. package/dist-lib/lib/core/files/processing/settle.js +20 -13
  32. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  33. package/dist-lib/lib/core/files/rag/index.js +1 -1
  34. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +1 -0
  35. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +3 -0
  36. package/dist-lib/lib/core/hosted-agents.d.ts +3 -0
  37. package/dist-lib/lib/core/hosted-agents.js +19 -0
  38. package/dist-lib/lib/core/notification.js +16 -13
  39. package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
  40. package/dist-lib/lib/core/pdf/figure.js +64 -0
  41. package/dist-lib/lib/core/pdf/rasterize.d.ts +10 -1
  42. package/dist-lib/lib/core/pdf/rasterize.js +72 -22
  43. package/dist-lib/lib/core.d.ts +6 -0
  44. package/dist-lib/lib/core.js +5 -0
  45. package/dist-lib/lib/credential-pusher.d.ts +11 -7
  46. package/dist-lib/lib/credential-pusher.js +40 -8
  47. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  48. package/dist-lib/lib/kv-cache-retention.d.ts +34 -0
  49. package/dist-lib/lib/kv-cache-retention.js +98 -0
  50. package/dist-lib/schema/content.js +2 -1
  51. package/dist-lib/schema/qvac.js +4 -2
  52. package/dist-lib/spec/hrpc/messages.js +4 -10
  53. package/dist-lib/spec/hyperschema/index.js +4 -10
  54. package/dist-lib/spec/hyperschema/schema.json +2 -1
  55. package/dist-lib/spec/hyperschema/types.d.ts +1 -1
  56. package/dist-lib/spec/mesh/hyperdb/messages.js +34 -14
  57. package/dist-lib/spec/mesh/hyperdispatch/messages.js +18 -8
  58. package/dist-lib/spec/mesh/hyperschema/index.js +18 -8
  59. package/dist-lib/spec/mesh/hyperschema/schema.json +11 -1
  60. package/dist-lib/spec/mesh/hyperschema/types.d.ts +3 -1
  61. package/package.json +6 -3
  62. package/tui/app.mjs +743 -266
  63. package/tui/boot.mjs +2 -2
  64. package/tui/lib/connect-sheet.mjs +350 -0
  65. package/tui/lib/setup.mjs +59 -25
  66. package/tui/lib/skill-connect.mjs +145 -0
@@ -0,0 +1,64 @@
1
+ const CAPTION = /^(?:Figure|Fig\.|Table|Chart|Exhibit)\s+[\w.-]*\w/i;
2
+ const CAPTION_GAP = 72;
3
+ const DECORATION_SHARE = 0.005;
4
+ const WHOLE_PAGE_SHARE = 0.6;
5
+ const MARGIN = 4;
6
+ function boxOf({ x, y, width, height }) {
7
+ return { left: x, bottom: y, right: x + width, top: y + height };
8
+ }
9
+ function join(a, b) {
10
+ return {
11
+ left: Math.min(a.left, b.left),
12
+ bottom: Math.min(a.bottom, b.bottom),
13
+ right: Math.max(a.right, b.right),
14
+ top: Math.max(a.top, b.top)
15
+ };
16
+ }
17
+ function overlapsAcross(span, box) {
18
+ return span.left < box.right && span.right > box.left;
19
+ }
20
+ function captionBlock(caption, lines, figure) {
21
+ const height = caption.top - caption.bottom;
22
+ let block = caption;
23
+ const below = lines
24
+ .filter((line) => line.top <= caption.top + 1 && line.bottom < caption.bottom)
25
+ .filter((line) => overlapsAcross(line, figure))
26
+ .sort((a, b) => b.top - a.top);
27
+ for (const line of below) {
28
+ if (block.bottom - line.top > height)
29
+ break;
30
+ block = join(block, line);
31
+ }
32
+ return block;
33
+ }
34
+ function captionName(text, line, figure) {
35
+ const name = text.trim().match(CAPTION)?.[0];
36
+ const near = line.top >= figure.bottom - CAPTION_GAP && line.bottom <= figure.top + CAPTION_GAP;
37
+ return name && near && overlapsAcross(line, figure) ? name : null;
38
+ }
39
+ export function figureRegion({ size, images, spans }) {
40
+ const area = size.width * size.height;
41
+ const content = images.filter((image) => image.width * image.height >= DECORATION_SHARE * area);
42
+ const pictures = (content.length ? content : images).map(boxOf);
43
+ if (!pictures.length) {
44
+ return null;
45
+ }
46
+ let box = pictures.reduce(join);
47
+ const figure = box;
48
+ const lines = spans.map(boxOf);
49
+ const names = [];
50
+ for (const [index, span] of spans.entries()) {
51
+ const name = captionName(span.text, lines[index], figure);
52
+ if (name) {
53
+ names.push(name);
54
+ box = join(box, captionBlock(lines[index], lines, figure));
55
+ }
56
+ }
57
+ const clip = {
58
+ x: Math.max(0, box.left - MARGIN),
59
+ y: Math.max(0, box.bottom - MARGIN),
60
+ width: Math.min(size.width, box.right + MARGIN) - Math.max(0, box.left - MARGIN),
61
+ height: Math.min(size.height, box.top + MARGIN) - Math.max(0, box.bottom - MARGIN)
62
+ };
63
+ return clip.width * clip.height >= WHOLE_PAGE_SHARE * area ? null : { clip, names };
64
+ }
@@ -1,14 +1,23 @@
1
1
  import type { LoggerInterface } from '@qvac/logging';
2
+ import { type PdfBounds, type PdfFigure } from './figure.ts';
2
3
  export declare const PDF_RENDER_SCALE = 2;
3
4
  interface PageFlags {
4
5
  hasImage: boolean;
5
6
  hasText: boolean;
6
7
  }
8
+ interface PageSize {
9
+ width: number;
10
+ height: number;
11
+ }
7
12
  export declare function renderScale(pageWidth: number): number;
13
+ export declare function renderSize(page: PageSize, clip?: PdfBounds): PageSize;
8
14
  export interface PdfDoc {
9
15
  pageCount(): number;
10
16
  pageFlags(page: number): PageFlags;
11
- renderPage(page: number): Promise<Uint8Array>;
17
+ figure(page: number): PdfFigure | null;
18
+ blank(page: number): Promise<boolean>;
19
+ renderSize(page: number, clip?: PdfBounds): PageSize;
20
+ renderPage(page: number, clip?: PdfBounds): Promise<Uint8Array>;
12
21
  close(): void;
13
22
  }
14
23
  export interface PdfRasterizer {
@@ -1,8 +1,30 @@
1
1
  import { MAX_IMAGE_WIDTH, rasterToModelJpeg } from '../image/normalize.js';
2
+ import { figureRegion } from './figure.js';
2
3
  export const PDF_RENDER_SCALE = 2;
4
+ const BLANK_SCALE = 0.25;
5
+ const PAPER = 230;
6
+ const INK_SHARE = 0.001;
3
7
  export function renderScale(pageWidth) {
4
8
  return Math.min(PDF_RENDER_SCALE, MAX_IMAGE_WIDTH / pageWidth);
5
9
  }
10
+ export function renderSize(page, clip) {
11
+ const scale = renderScale(page.width);
12
+ const width = Math.max(1, Math.round((clip?.width ?? page.width) * scale));
13
+ const height = Math.max(1, Math.round((clip?.height ?? page.height) * scale));
14
+ const shrink = Math.min(1, MAX_IMAGE_WIDTH / width);
15
+ return { width: Math.round(width * shrink), height: Math.round(height * shrink) };
16
+ }
17
+ function blankRaster({ data }) {
18
+ let samples = 0;
19
+ let ink = 0;
20
+ for (let i = 0; i < data.length; i += 4) {
21
+ samples++;
22
+ if (data[i] < PAPER || data[i + 1] < PAPER || data[i + 2] < PAPER) {
23
+ ink++;
24
+ }
25
+ }
26
+ return ink <= samples * INK_SHARE;
27
+ }
6
28
  let pdfium;
7
29
  async function loadPdfium(log) {
8
30
  if (pdfium !== undefined)
@@ -17,6 +39,55 @@ async function loadPdfium(log) {
17
39
  }
18
40
  return pdfium;
19
41
  }
42
+ function pageFigure(doc, page) {
43
+ if (doc.pageRotation(page) !== 0) {
44
+ return null;
45
+ }
46
+ const images = doc.imageBounds(page);
47
+ if (!images.length) {
48
+ return null;
49
+ }
50
+ const box = doc.pageBox(page);
51
+ const shown = (at) => ({ ...at, x: at.x - box.x, y: at.y - box.y });
52
+ return figureRegion({
53
+ size: { width: box.width, height: box.height },
54
+ images: images.map(shown),
55
+ spans: doc.textSpans(page).map(shown)
56
+ });
57
+ }
58
+ function wrapDoc(doc) {
59
+ return {
60
+ pageCount() {
61
+ return doc.pageCount();
62
+ },
63
+ pageFlags(page) {
64
+ return doc.pageFlags(page);
65
+ },
66
+ figure(page) {
67
+ return pageFigure(doc, page);
68
+ },
69
+ async blank(page) {
70
+ return blankRaster(await doc.renderAsync(page, { scale: BLANK_SCALE }));
71
+ },
72
+ renderSize(page, clip) {
73
+ return renderSize(doc.pageSize(page), clip);
74
+ },
75
+ async renderPage(page, clip) {
76
+ const scale = renderScale(doc.pageSize(page).width);
77
+ const jpeg = await rasterToModelJpeg(await doc.renderAsync(page, { scale, clip }));
78
+ if (!jpeg) {
79
+ throw new Error('no JPEG encoder: bare-media did not load');
80
+ }
81
+ return jpeg;
82
+ },
83
+ close() {
84
+ try {
85
+ doc.close();
86
+ }
87
+ catch { }
88
+ }
89
+ };
90
+ }
20
91
  export const createPdfRasterizer = (log) => ({
21
92
  async open(bytes) {
22
93
  const mod = await loadPdfium(log);
@@ -30,27 +101,6 @@ export const createPdfRasterizer = (log) => ({
30
101
  catch {
31
102
  return null;
32
103
  }
33
- return {
34
- pageCount() {
35
- return doc.pageCount();
36
- },
37
- pageFlags(page) {
38
- return doc.pageFlags(page);
39
- },
40
- async renderPage(page) {
41
- const scale = renderScale(doc.pageSize(page).width);
42
- const jpeg = await rasterToModelJpeg(await doc.renderAsync(page, { scale }));
43
- if (!jpeg) {
44
- throw new Error('no JPEG encoder: bare-media did not load');
45
- }
46
- return jpeg;
47
- },
48
- close() {
49
- try {
50
- doc.close();
51
- }
52
- catch { }
53
- }
54
- };
104
+ return wrapDoc(doc);
55
105
  }
56
106
  });
@@ -87,6 +87,12 @@ export interface NetworkConnection {
87
87
  }
88
88
  export interface NetworkGate {
89
89
  readonly onConnection?: (conn: NetworkConnection) => void;
90
+ readonly onMeshTopic?: (topic: MeshTopic) => void;
91
+ }
92
+ export interface MeshTopic {
93
+ readonly meshId: string;
94
+ readonly topic: Buffer;
95
+ readonly swarm: Hyperswarm;
90
96
  }
91
97
  export declare const DEVICE_UNKNOWN = "This device has no device details: pass `device` when creating Core";
92
98
  export interface SharedMesh {
@@ -1502,6 +1502,11 @@ export class Core extends ReadyResource {
1502
1502
  return;
1503
1503
  if (this._swarm) {
1504
1504
  this._swarm.join(mesh.discoveryKey, { server: true, client: true });
1505
+ this._network?.onMeshTopic?.({
1506
+ meshId: this._meshIdOf(mesh),
1507
+ topic: mesh.discoveryKey,
1508
+ swarm: this._swarm
1509
+ });
1505
1510
  this._startBlindPeering(mesh);
1506
1511
  if (this.suspended) {
1507
1512
  void mesh.suspend().catch(noop);
@@ -1,6 +1,6 @@
1
1
  import type { Harness } from './assistant.ts';
2
2
  import { type ConfigEngine } from './oauth.ts';
3
- import type { OAuthRouteLike } from './oauth/keys.ts';
3
+ import type { OAuthKeySpec, OAuthRouteLike } from './oauth/keys.ts';
4
4
  export interface CredentialEntry {
5
5
  key: string;
6
6
  token?: string | null;
@@ -14,19 +14,21 @@ export interface SettingEntry {
14
14
  value: string;
15
15
  agentId?: string | null;
16
16
  }
17
+ type PusherRoute = OAuthRouteLike & {
18
+ fields?: ReadonlyArray<{
19
+ key: string;
20
+ secret?: boolean;
21
+ }>;
22
+ };
17
23
  export interface PusherSkill {
18
24
  name: string;
25
+ origin?: string;
19
26
  credentials?: readonly string[];
20
27
  requires?: {
21
28
  credentials?: readonly string[];
22
29
  };
23
30
  setup?: {
24
- routes?: ReadonlyArray<OAuthRouteLike & {
25
- fields?: ReadonlyArray<{
26
- key: string;
27
- secret?: boolean;
28
- }>;
29
- }>;
31
+ routes?: ReadonlyArray<PusherRoute>;
30
32
  };
31
33
  }
32
34
  export interface CredentialEngine extends ConfigEngine {
@@ -67,6 +69,8 @@ export type CredentialPusherHandle = (() => Promise<void>) & {
67
69
  };
68
70
  export declare function nextRefreshDelay(entries: ReadonlyArray<Pick<CredentialEntry, 'expiresAt'>>, now?: number): number;
69
71
  export declare function credentialOwners(catalog: ReadonlyArray<PusherSkill>): ReadonlyMap<string, readonly string[]>;
72
+ export declare function plainCredentialKeys(catalog: ReadonlyArray<PusherSkill>, specs: ReadonlyArray<OAuthKeySpec>): ReadonlyArray<string>;
70
73
  export declare function settingFields(catalog: ReadonlyArray<PusherSkill>): ReadonlyArray<Pick<SettingEntry, 'skill' | 'key'>>;
71
74
  export declare function settingConfigKey(agentId: string | undefined, skill: string, key: string): string;
72
75
  export declare function startCredentialPusher(harness: CredentialTarget, engine: CredentialEngine, logger?: CredentialPusherLogger | null): CredentialPusherHandle;
76
+ export {};
@@ -8,6 +8,7 @@ import ScopeLock from 'scope-lock';
8
8
  import { credentialSlots } from '@qvac/harness/lib/tools/skills/credential-slots.ts';
9
9
  import { errorMessage } from './guards.js';
10
10
  const PLAIN_KEYS = ['github_access_token', OBSIDIAN_VAULT_PATH_CREDENTIAL_KEY];
11
+ const PLAIN_KEY = /^[A-Za-z0-9_-]+$/;
11
12
  const REFRESH_INTERVAL = 10 * 60 * 1000;
12
13
  const MIN_REFRESH_DELAY = 30_000;
13
14
  function storeKey(key, agentId) {
@@ -69,7 +70,7 @@ async function plainEntry(engine, key, agentId) {
69
70
  }
70
71
  export function credentialOwners(catalog) {
71
72
  const owners = new Map();
72
- for (const skill of catalog) {
73
+ for (const skill of trusted(catalog)) {
73
74
  for (const key of credentialSlots(skill).flat()) {
74
75
  const list = owners.get(key) ?? [];
75
76
  if (!list.includes(skill.name)) {
@@ -80,6 +81,37 @@ export function credentialOwners(catalog) {
80
81
  }
81
82
  return owners;
82
83
  }
84
+ function trusted(catalog) {
85
+ return catalog.filter((skill) => skill.origin !== 'overlay');
86
+ }
87
+ export function plainCredentialKeys(catalog, specs) {
88
+ const oauth = new Set(specs.map((spec) => spec.credentialKey));
89
+ const reserved = new Set();
90
+ const declared = new Set(PLAIN_KEYS);
91
+ for (const skill of trusted(catalog)) {
92
+ for (const key of credentialSlots(skill).flat()) {
93
+ declared.add(key);
94
+ }
95
+ for (const route of skill.setup?.routes ?? []) {
96
+ routeKeys(route, route.oauth ? reserved : declared);
97
+ }
98
+ }
99
+ const allowed = (key) => PLAIN_KEYS.includes(key) || (!reserved.has(key) && PLAIN_KEY.test(key));
100
+ return [...declared].filter((key) => !oauth.has(key) && allowed(key));
101
+ }
102
+ function routeKeys(route, into) {
103
+ if (route.oauth?.stateKey) {
104
+ into.add(route.oauth.stateKey);
105
+ }
106
+ if (route.credentialKey && !route.oauth) {
107
+ into.add(route.credentialKey);
108
+ }
109
+ for (const field of route.fields ?? []) {
110
+ if (route.oauth || field.secret === true) {
111
+ into.add(field.key);
112
+ }
113
+ }
114
+ }
83
115
  export function settingFields(catalog) {
84
116
  const fields = [];
85
117
  for (const skill of catalog) {
@@ -118,15 +150,14 @@ async function resolveEntry({ mk, failure, read }, { last, owners, logger }) {
118
150
  }
119
151
  }
120
152
  async function resolveScope(agentId, resolution) {
121
- const { engine, specs } = resolution;
122
- const specKeys = new Set(specs.map((spec) => spec.credentialKey));
153
+ const { engine, specs, plainKeys } = resolution;
123
154
  const pending = [
124
155
  ...specs.map((spec) => ({
125
156
  mk: storeKey(spec.credentialKey, agentId),
126
157
  failure: `credential resolve failed for ${spec.credentialKey}`,
127
158
  read: () => oauthEntry(engine, spec, agentId)
128
159
  })),
129
- ...PLAIN_KEYS.filter((key) => !specKeys.has(key)).map((key) => ({
160
+ ...plainKeys.map((key) => ({
130
161
  mk: storeKey(key, agentId),
131
162
  failure: `credential read failed for ${key}`,
132
163
  read: () => plainEntry(engine, key, agentId)
@@ -192,10 +223,10 @@ export function startCredentialPusher(harness, engine, logger) {
192
223
  logger?.warn('credential-pusher', `credential watch ended for ${key}`, errorMessage(err));
193
224
  });
194
225
  };
195
- const syncWatches = (oauthKeys, scopes) => {
226
+ const syncWatches = (pushedKeys, scopes) => {
196
227
  if (disposed)
197
228
  return;
198
- const keys = new Set([...oauthKeys, ...PLAIN_KEYS]);
229
+ const keys = new Set(pushedKeys);
199
230
  const wanted = new Set(scopes.flatMap((agentId) => [...keys].map((key) => scopedKey(agentId, key))));
200
231
  for (const [key, stream] of watches) {
201
232
  if (wanted.has(key))
@@ -272,10 +303,11 @@ export function startCredentialPusher(harness, engine, logger) {
272
303
  }
273
304
  const owners = credentialOwners(skills);
274
305
  const specs = oauthSpecs(skills);
306
+ const plainKeys = plainCredentialKeys(skills, specs);
275
307
  const { meshId, scopes } = await accountScopes(engine);
276
- syncWatches(specs.map((spec) => spec.credentialKey), scopes);
308
+ syncWatches([...specs.map((spec) => spec.credentialKey), ...plainKeys], scopes);
277
309
  syncAgentsWatch(meshId);
278
- const resolution = { engine, last, owners, specs, logger };
310
+ const resolution = { engine, last, owners, specs, plainKeys, logger };
279
311
  const resolved = await Promise.all(scopes.map((agentId) => resolveScope(agentId, resolution)));
280
312
  entries.push(...resolved.flat());
281
313
  if (disposed) {
@@ -47,10 +47,7 @@ function prioritizeVaultPicker(setup) {
47
47
  function derivedRoutes(entry) {
48
48
  const routes = [];
49
49
  for (const key of credentialKeysFor(entry)) {
50
- const route = credentialRoute(key);
51
- if (route) {
52
- routes.push(route);
53
- }
50
+ routes.push(credentialRoute(key));
54
51
  }
55
52
  const install = installRoute(entry);
56
53
  if (install) {
@@ -79,7 +76,12 @@ function credentialRoute(credentialKey) {
79
76
  fields: [{ key: credentialKey, label: token.label, secret: true, required: true }]
80
77
  };
81
78
  }
82
- return null;
79
+ return {
80
+ kind: SETUP_ROUTE_KIND.token,
81
+ label: `Enter ${credentialKey}`,
82
+ credentialKey,
83
+ fields: [{ key: credentialKey, label: credentialKey, secret: true, required: true }]
84
+ };
83
85
  }
84
86
  function installRoute(entry) {
85
87
  const steps = entry.metadata.install ?? [];
@@ -0,0 +1,34 @@
1
+ import type { LoggerInterface } from '@qvac/logging';
2
+ import type { Capabilities } from '../spec/capabilities.d.ts';
3
+ import type { AssistantChat } from '../spec/hyperschema/types.d.ts';
4
+ type RetentionEngine = Pick<Capabilities, 'configGet' | 'configSet' | 'listChats'>;
5
+ export interface KvCacheRetentionOptions {
6
+ readonly active: () => boolean;
7
+ readonly running: (chatId: string) => boolean;
8
+ readonly deleteChatKvCache: (chatId: string) => Promise<boolean>;
9
+ readonly defaultMs?: number | undefined;
10
+ readonly logger?: LoggerInterface | null;
11
+ readonly now?: () => number;
12
+ }
13
+ interface IdleWindow {
14
+ readonly from: number;
15
+ readonly cutoff: number;
16
+ }
17
+ export declare function kvCacheRetentionMs(value: unknown, defaultMs?: number): number | null;
18
+ export declare function kvCacheSweptKey(meshId: string): string;
19
+ export declare function idleChatIds(chats: ReadonlyArray<AssistantChat>, { from, cutoff }: IdleWindow): string[];
20
+ export declare class KvCacheRetention {
21
+ private readonly _engine;
22
+ private readonly _opts;
23
+ private readonly _now;
24
+ private readonly _queued;
25
+ private _queue;
26
+ private _timer;
27
+ constructor(engine: RetentionEngine, opts: KvCacheRetentionOptions);
28
+ sweep(meshId: string): Promise<void>;
29
+ start(meshIds: () => Iterable<string>): void;
30
+ stop(): void;
31
+ private _sweep;
32
+ private _deleteIdle;
33
+ }
34
+ export {};
@@ -0,0 +1,98 @@
1
+ import { KV_CACHE_RETENTION_DAYS_DEFAULT, KV_CACHE_RETENTION_KEY } from './constants.js';
2
+ import { errorMessage } from './guards.js';
3
+ import { unref } from './unref.js';
4
+ const DAY_MS = 24 * 60 * 60_000;
5
+ const DEFAULT_MS = KV_CACHE_RETENTION_DAYS_DEFAULT * DAY_MS;
6
+ const SWEEP_TICK_MS = 60 * 60_000;
7
+ export function kvCacheRetentionMs(value, defaultMs = DEFAULT_MS) {
8
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
9
+ return defaultMs > 0 ? defaultMs : null;
10
+ }
11
+ return value > 0 ? value * DAY_MS : null;
12
+ }
13
+ export function kvCacheSweptKey(meshId) {
14
+ return `kvCache.sweptThrough.${meshId}`;
15
+ }
16
+ export function idleChatIds(chats, { from, cutoff }) {
17
+ return chats
18
+ .filter((chat) => {
19
+ const at = chat.lastActivityAt || chat.createdAt;
20
+ return at >= from && at < cutoff;
21
+ })
22
+ .map((chat) => chat.id);
23
+ }
24
+ export class KvCacheRetention {
25
+ _engine;
26
+ _opts;
27
+ _now;
28
+ _queued;
29
+ _queue;
30
+ _timer;
31
+ constructor(engine, opts) {
32
+ this._engine = engine;
33
+ this._opts = opts;
34
+ this._now = opts.now ?? Date.now;
35
+ this._queued = new Set();
36
+ this._queue = Promise.resolve();
37
+ this._timer = null;
38
+ }
39
+ sweep(meshId) {
40
+ if (this._queued.has(meshId)) {
41
+ return this._queue;
42
+ }
43
+ this._queued.add(meshId);
44
+ this._queue = this._queue.then(() => {
45
+ this._queued.delete(meshId);
46
+ return this._sweep(meshId).catch((err) => this._opts.logger?.warn('kv-cache-retention', 'sweep failed', {
47
+ meshId,
48
+ error: errorMessage(err)
49
+ }));
50
+ });
51
+ return this._queue;
52
+ }
53
+ start(meshIds) {
54
+ this._timer = setInterval(() => {
55
+ for (const meshId of meshIds()) {
56
+ void this.sweep(meshId);
57
+ }
58
+ }, SWEEP_TICK_MS);
59
+ unref(this._timer);
60
+ }
61
+ stop() {
62
+ if (this._timer) {
63
+ clearInterval(this._timer);
64
+ }
65
+ this._timer = null;
66
+ }
67
+ async _sweep(meshId) {
68
+ const retention = await this._engine.configGet({ key: KV_CACHE_RETENTION_KEY });
69
+ const retentionMs = kvCacheRetentionMs(retention.value, this._opts.defaultMs);
70
+ if (retentionMs === null || !this._opts.active()) {
71
+ return;
72
+ }
73
+ const markKey = kvCacheSweptKey(meshId);
74
+ const mark = await this._engine.configGet({ key: markKey });
75
+ const from = typeof mark.value === 'number' ? mark.value : 0;
76
+ const cutoff = this._now() - retentionMs;
77
+ if (cutoff <= from) {
78
+ return;
79
+ }
80
+ const { chats } = await this._engine.listChats({ meshId });
81
+ if (await this._deleteIdle(idleChatIds(chats ?? [], { from, cutoff }))) {
82
+ await this._engine.configSet({ key: markKey, value: cutoff });
83
+ }
84
+ }
85
+ async _deleteIdle(chatIds) {
86
+ let complete = true;
87
+ for (const chatId of chatIds) {
88
+ if (!this._opts.active())
89
+ return false;
90
+ if (this._opts.running(chatId))
91
+ continue;
92
+ if (!(await this._opts.deleteChatKvCache(chatId))) {
93
+ complete = false;
94
+ }
95
+ }
96
+ return complete;
97
+ }
98
+ }
@@ -95,9 +95,10 @@ export function registerContentTypes(ns, namespace) {
95
95
  { name: 'name', type: 'string', required: true },
96
96
  { name: 'arguments', type: 'json' }
97
97
  ]);
98
+ const result = namespace === 'assistant' ? { required: true } : {};
98
99
  versioned('chunk-content-tool-result', [
99
100
  { name: 'name', type: 'string', required: true },
100
- { name: 'result', type: 'json' }
101
+ { name: 'result', type: 'json', ...result }
101
102
  ]);
102
103
  const attachment = [
103
104
  { name: 'id', type: 'string', required: true },
@@ -83,7 +83,8 @@ export function registerQvacTypes(ns) {
83
83
  { name: 'capability', type: 'string' },
84
84
  { name: 'capabilityArgs', type: 'json' },
85
85
  { name: 'enhance', type: '@qvac/enhance-mode' },
86
- { name: 'sourceId', type: 'string' }
86
+ { name: 'sourceId', type: 'string' },
87
+ { name: 'falsyResult', type: 'string' }
87
88
  ]);
88
89
  versioned('chunk-counter', [
89
90
  { name: 'chatId', type: 'string', required: true },
@@ -117,7 +118,8 @@ export function registerQvacTypes(ns) {
117
118
  { name: 'capability', type: 'string' },
118
119
  { name: 'capabilityArgs', type: 'json' },
119
120
  { name: 'enhance', type: '@qvac/enhance-mode' },
120
- { name: 'sourceId', type: 'string' }
121
+ { name: 'sourceId', type: 'string' },
122
+ { name: 'falsyResult', type: 'string' }
121
123
  ]);
122
124
  versioned('update-chunk-operation', [
123
125
  { name: 'id', type: 'string', required: true },
@@ -3293,26 +3293,20 @@ const encoding87 = {
3293
3293
  const encoding88 = {
3294
3294
  preencode(state, m) {
3295
3295
  c.string.preencode(state, m.name)
3296
- state.end++ // max flag is 1 so always one byte
3297
-
3298
- if (m.result) c.json.preencode(state, m.result)
3296
+ c.json.preencode(state, m.result)
3299
3297
  },
3300
3298
  encode(state, m) {
3301
- const flags = m.result ? 1 : 0
3302
-
3303
3299
  c.string.encode(state, m.name)
3304
- c.uint.encode(state, flags)
3305
-
3306
- if (m.result) c.json.encode(state, m.result)
3300
+ c.json.encode(state, m.result)
3307
3301
  },
3308
3302
  decode(state) {
3309
3303
  const r0 = c.string.decode(state)
3310
- const flags = c.uint.decode(state)
3304
+ const r1 = c.json.decode(state)
3311
3305
 
3312
3306
  return {
3313
3307
  version: 0,
3314
3308
  name: r0,
3315
- result: (flags & 1) !== 0 ? c.json.decode(state) : null
3309
+ result: r1
3316
3310
  }
3317
3311
  }
3318
3312
  }
@@ -3293,26 +3293,20 @@ const encoding87 = {
3293
3293
  const encoding88 = {
3294
3294
  preencode(state, m) {
3295
3295
  c.string.preencode(state, m.name)
3296
- state.end++ // max flag is 1 so always one byte
3297
-
3298
- if (m.result) c.json.preencode(state, m.result)
3296
+ c.json.preencode(state, m.result)
3299
3297
  },
3300
3298
  encode(state, m) {
3301
- const flags = m.result ? 1 : 0
3302
-
3303
3299
  c.string.encode(state, m.name)
3304
- c.uint.encode(state, flags)
3305
-
3306
- if (m.result) c.json.encode(state, m.result)
3300
+ c.json.encode(state, m.result)
3307
3301
  },
3308
3302
  decode(state) {
3309
3303
  const r0 = c.string.decode(state)
3310
- const flags = c.uint.decode(state)
3304
+ const r1 = c.json.decode(state)
3311
3305
 
3312
3306
  return {
3313
3307
  version: 0,
3314
3308
  name: r0,
3315
- result: (flags & 1) !== 0 ? c.json.decode(state) : null
3309
+ result: r1
3316
3310
  }
3317
3311
  }
3318
3312
  }
@@ -2524,7 +2524,7 @@
2524
2524
  "name": "chunk-content-tool-result-v1",
2525
2525
  "namespace": "assistant",
2526
2526
  "compact": false,
2527
- "flagsPosition": 1,
2527
+ "flagsPosition": -1,
2528
2528
  "fields": [
2529
2529
  {
2530
2530
  "name": "name",
@@ -2534,6 +2534,7 @@
2534
2534
  },
2535
2535
  {
2536
2536
  "name": "result",
2537
+ "required": true,
2537
2538
  "type": "json",
2538
2539
  "version": 1
2539
2540
  }
@@ -516,7 +516,7 @@ export type AssistantChunkContentToolCall = (AssistantChunkContentToolCallV1 & {
516
516
 
517
517
  export interface AssistantChunkContentToolResultV1 {
518
518
  name: string
519
- result?: unknown | null
519
+ result: unknown
520
520
  }
521
521
 
522
522
  export type AssistantChunkContentToolResult = (AssistantChunkContentToolResultV1 & { version?: number })