@qvac/core 1.0.0-rc.2 → 1.0.0-rc.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 (195) hide show
  1. package/CHANGELOG.md +158 -2
  2. package/dist-lib/index.d.ts +1 -0
  3. package/dist-lib/index.js +1 -0
  4. package/dist-lib/lib/api/chunk-delta.d.ts +7 -2
  5. package/dist-lib/lib/api/chunk-delta.js +89 -7
  6. package/dist-lib/lib/api/chunks.js +14 -8
  7. package/dist-lib/lib/api/config.d.ts +4 -1
  8. package/dist-lib/lib/api/config.js +45 -4
  9. package/dist-lib/lib/api/custom-models.js +3 -5
  10. package/dist-lib/lib/api/devices.js +3 -1
  11. package/dist-lib/lib/api/files.d.ts +1 -1
  12. package/dist-lib/lib/api/files.js +65 -8
  13. package/dist-lib/lib/api/instrument.js +12 -4
  14. package/dist-lib/lib/api/members.js +7 -13
  15. package/dist-lib/lib/api/mesh.d.ts +1 -3
  16. package/dist-lib/lib/api/mesh.js +2 -2
  17. package/dist-lib/lib/api/notifications.js +8 -4
  18. package/dist-lib/lib/api/projects.js +5 -1
  19. package/dist-lib/lib/api/rag.js +32 -19
  20. package/dist-lib/lib/api/served.js +2 -1
  21. package/dist-lib/lib/api/workspace.js +0 -1
  22. package/dist-lib/lib/assistant/harness.d.ts +8 -2
  23. package/dist-lib/lib/assistant/harness.js +17 -4
  24. package/dist-lib/lib/assistant/history.d.ts +1 -1
  25. package/dist-lib/lib/assistant/history.js +19 -13
  26. package/dist-lib/lib/assistant/message.d.ts +6 -1
  27. package/dist-lib/lib/assistant/message.js +22 -2
  28. package/dist-lib/lib/assistant/model-round.d.ts +5 -0
  29. package/dist-lib/lib/assistant/model-round.js +46 -18
  30. package/dist-lib/lib/assistant/pdf-budget.d.ts +10 -2
  31. package/dist-lib/lib/assistant/pdf-budget.js +10 -11
  32. package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
  33. package/dist-lib/lib/assistant/pdf-pages.js +75 -0
  34. package/dist-lib/lib/assistant/round-host.js +5 -3
  35. package/dist-lib/lib/assistant-client.d.ts +2 -1
  36. package/dist-lib/lib/assistant-client.js +24 -6
  37. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  38. package/dist-lib/lib/assistant-tree.js +8 -5
  39. package/dist-lib/lib/assistant.d.ts +20 -7
  40. package/dist-lib/lib/assistant.js +468 -240
  41. package/dist-lib/lib/client.js +10 -1
  42. package/dist-lib/lib/compaction.d.ts +3 -1
  43. package/dist-lib/lib/compaction.js +18 -4
  44. package/dist-lib/lib/constants.d.ts +3 -0
  45. package/dist-lib/lib/constants.js +3 -0
  46. package/dist-lib/lib/core/chat-activity.js +13 -16
  47. package/dist-lib/lib/core/chat-copy.js +1 -1
  48. package/dist-lib/lib/core/chat-fork.js +2 -1
  49. package/dist-lib/lib/core/chat-move.d.ts +4 -0
  50. package/dist-lib/lib/core/chat-move.js +42 -11
  51. package/dist-lib/lib/core/chat.d.ts +0 -1
  52. package/dist-lib/lib/core/chat.js +1 -1
  53. package/dist-lib/lib/core/chunk.d.ts +14 -3
  54. package/dist-lib/lib/core/chunk.js +112 -22
  55. package/dist-lib/lib/core/collections.d.ts +12 -1
  56. package/dist-lib/lib/core/collections.js +12 -1
  57. package/dist-lib/lib/core/config-value.d.ts +6 -0
  58. package/dist-lib/lib/core/config-value.js +14 -0
  59. package/dist-lib/lib/core/contract/activity.d.ts +3 -2
  60. package/dist-lib/lib/core/contract/activity.js +39 -21
  61. package/dist-lib/lib/core/contract/agent-join.js +15 -19
  62. package/dist-lib/lib/core/contract/agents.js +8 -7
  63. package/dist-lib/lib/core/contract/chat.d.ts +3 -0
  64. package/dist-lib/lib/core/contract/chat.js +12 -1
  65. package/dist-lib/lib/core/contract/chunks.js +16 -6
  66. package/dist-lib/lib/core/contract/devices.js +6 -4
  67. package/dist-lib/lib/core/contract/index.js +2 -1
  68. package/dist-lib/lib/core/contract/invites.js +12 -31
  69. package/dist-lib/lib/core/contract/joined-meshes.js +2 -1
  70. package/dist-lib/lib/core/contract/mesh-rules.js +4 -0
  71. package/dist-lib/lib/core/contract/model-requests.js +7 -6
  72. package/dist-lib/lib/core/contract/project-device-keys.js +15 -6
  73. package/dist-lib/lib/core/contract/project-host-keys.js +5 -4
  74. package/dist-lib/lib/core/contract/projects.js +12 -13
  75. package/dist-lib/lib/core/contract/public-seeds.js +5 -5
  76. package/dist-lib/lib/core/contract/roles.js +2 -1
  77. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -1
  78. package/dist-lib/lib/core/contract/run-requests.js +19 -13
  79. package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
  80. package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
  81. package/dist-lib/lib/core/contract/workspace.js +4 -10
  82. package/dist-lib/lib/core/contract/writers.js +20 -6
  83. package/dist-lib/lib/core/devices.d.ts +2 -1
  84. package/dist-lib/lib/core/devices.js +4 -2
  85. package/dist-lib/lib/core/document-attachment.d.ts +6 -3
  86. package/dist-lib/lib/core/document-attachment.js +10 -5
  87. package/dist-lib/lib/core/files/attachments.js +3 -3
  88. package/dist-lib/lib/core/files/index.d.ts +8 -5
  89. package/dist-lib/lib/core/files/index.js +109 -18
  90. package/dist-lib/lib/core/files/processing/index.d.ts +1 -1
  91. package/dist-lib/lib/core/files/processing/index.js +8 -2
  92. package/dist-lib/lib/core/files/processing/source.d.ts +1 -0
  93. package/dist-lib/lib/core/files/processing/source.js +3 -0
  94. package/dist-lib/lib/core/files/processing/state.d.ts +1 -0
  95. package/dist-lib/lib/core/files/processing/state.js +7 -0
  96. package/dist-lib/lib/core/files/rag/build.d.ts +1 -0
  97. package/dist-lib/lib/core/files/rag/build.js +14 -1
  98. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  99. package/dist-lib/lib/core/files/rag/index.js +1 -1
  100. package/dist-lib/lib/core/files/rag/produce.js +1 -0
  101. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
  102. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +7 -1
  103. package/dist-lib/lib/core/first-event.d.ts +6 -0
  104. package/dist-lib/lib/core/first-event.js +13 -0
  105. package/dist-lib/lib/core/invite.d.ts +2 -2
  106. package/dist-lib/lib/core/invite.js +5 -4
  107. package/dist-lib/lib/core/local/index.d.ts +10 -1
  108. package/dist-lib/lib/core/local/index.js +105 -10
  109. package/dist-lib/lib/core/mesh.d.ts +15 -4
  110. package/dist-lib/lib/core/mesh.js +128 -57
  111. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  112. package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
  113. package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
  114. package/dist-lib/lib/core/migrations/file-index.js +6 -2
  115. package/dist-lib/lib/core/migrations/index.d.ts +2 -1
  116. package/dist-lib/lib/core/migrations/index.js +66 -9
  117. package/dist-lib/lib/core/notification.js +96 -23
  118. package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
  119. package/dist-lib/lib/core/pdf/figure.js +64 -0
  120. package/dist-lib/lib/core/pdf/rasterize.d.ts +10 -1
  121. package/dist-lib/lib/core/pdf/rasterize.js +72 -22
  122. package/dist-lib/lib/core/peer-duplex.js +57 -5
  123. package/dist-lib/lib/core/public-mesh.js +8 -2
  124. package/dist-lib/lib/core/run-requests.js +2 -3
  125. package/dist-lib/lib/core/senders.d.ts +7 -6
  126. package/dist-lib/lib/core/senders.js +23 -30
  127. package/dist-lib/lib/core/shared-agents.d.ts +1 -0
  128. package/dist-lib/lib/core/shared-agents.js +47 -19
  129. package/dist-lib/lib/core/sync/background.d.ts +3 -15
  130. package/dist-lib/lib/core/sync/background.js +25 -102
  131. package/dist-lib/lib/core/sync/devices.js +6 -3
  132. package/dist-lib/lib/core/sync/index.js +12 -2
  133. package/dist-lib/lib/core/sync-status.js +3 -3
  134. package/dist-lib/lib/core.d.ts +59 -10
  135. package/dist-lib/lib/core.js +706 -174
  136. package/dist-lib/lib/credential-pusher.d.ts +11 -7
  137. package/dist-lib/lib/credential-pusher.js +40 -8
  138. package/dist-lib/lib/device-tree.d.ts +18 -0
  139. package/dist-lib/lib/device-tree.js +32 -0
  140. package/dist-lib/lib/doctor/redact.js +1 -1
  141. package/dist-lib/lib/errors.d.ts +4 -0
  142. package/dist-lib/lib/errors.js +4 -0
  143. package/dist-lib/lib/fs-drive-bridge.d.ts +7 -0
  144. package/dist-lib/lib/fs-drive-bridge.js +308 -30
  145. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  146. package/dist-lib/lib/knowledge-search.d.ts +1 -1
  147. package/dist-lib/lib/knowledge-search.js +5 -46
  148. package/dist-lib/lib/kv-cache-retention.d.ts +35 -0
  149. package/dist-lib/lib/kv-cache-retention.js +98 -0
  150. package/dist-lib/lib/latest-frame.d.ts +10 -0
  151. package/dist-lib/lib/latest-frame.js +48 -0
  152. package/dist-lib/lib/model-download.d.ts +6 -1
  153. package/dist-lib/lib/model-download.js +32 -17
  154. package/dist-lib/lib/oauth/resolve.js +13 -5
  155. package/dist-lib/lib/reconnecting-watch.js +19 -11
  156. package/dist-lib/lib/run-notices.d.ts +2 -0
  157. package/dist-lib/lib/run-notices.js +5 -0
  158. package/dist-lib/lib/run-replay.d.ts +13 -1
  159. package/dist-lib/lib/run-replay.js +58 -21
  160. package/dist-lib/schema/build.js +66 -57
  161. package/dist-lib/schema/content.js +2 -1
  162. package/dist-lib/schema/engine.js +37 -5
  163. package/dist-lib/schema/field-order.js +7 -1
  164. package/dist-lib/schema/indexes.d.ts +4 -0
  165. package/dist-lib/schema/indexes.js +18 -0
  166. package/dist-lib/schema/local.js +9 -2
  167. package/dist-lib/schema/qvac.js +112 -4
  168. package/dist-lib/spec/bind.js +3 -0
  169. package/dist-lib/spec/calls.js +1 -0
  170. package/dist-lib/spec/capabilities.d.ts +2 -0
  171. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  172. package/dist-lib/spec/hrpc/index.js +15 -3
  173. package/dist-lib/spec/hrpc/messages.js +1475 -1392
  174. package/dist-lib/spec/hyperschema/index.js +1475 -1392
  175. package/dist-lib/spec/hyperschema/schema.json +86 -1
  176. package/dist-lib/spec/hyperschema/types.d.ts +21 -1
  177. package/dist-lib/spec/local/hyperdb/messages.js +110 -54
  178. package/dist-lib/spec/local/hyperschema/index.js +56 -28
  179. package/dist-lib/spec/local/hyperschema/schema.json +55 -20
  180. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -1
  181. package/dist-lib/spec/mesh/hyperdb/db.json +255 -51
  182. package/dist-lib/spec/mesh/hyperdb/index.js +1063 -404
  183. package/dist-lib/spec/mesh/hyperdb/messages.js +1647 -1214
  184. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +62 -56
  185. package/dist-lib/spec/mesh/hyperdispatch/index.js +156 -139
  186. package/dist-lib/spec/mesh/hyperdispatch/messages.js +787 -629
  187. package/dist-lib/spec/mesh/hyperschema/index.js +787 -629
  188. package/dist-lib/spec/mesh/hyperschema/schema.json +283 -174
  189. package/dist-lib/spec/mesh/hyperschema/types.d.ts +29 -1
  190. package/package.json +23 -17
  191. package/worker/assistant-sidecar.mjs +3 -0
  192. package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
  193. package/dist-lib/lib/core/files/processing/settle.js +0 -86
  194. package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
  195. package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
@@ -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 ?? [];
@@ -5,4 +5,4 @@ export interface SearchKnowledgeOptions {
5
5
  readonly signal: AbortSignal;
6
6
  readonly preparing: (active: boolean) => void;
7
7
  }
8
- export declare function searchKnowledge(engine: Pick<ScopedCapabilities, 'ragSearch' | 'fileStatusWatch'>, request: Omit<AssistantRagSearchRequest, 'meshId'>, options: SearchKnowledgeOptions): Promise<AssistantRagSearchResponse>;
8
+ export declare function searchKnowledge(engine: Pick<ScopedCapabilities, 'ragSearch'>, request: Omit<AssistantRagSearchRequest, 'meshId'>, options: SearchKnowledgeOptions): Promise<AssistantRagSearchResponse>;
@@ -1,56 +1,15 @@
1
1
  import { raceAbort } from '@qvac/harness/race-abort';
2
2
  export async function searchKnowledge(engine, request, options) {
3
3
  const { signal, preparing } = options;
4
- const search = await raceAbort(engine.ragSearch(request), signal);
5
- const { attachmentIds } = request;
6
- if (!search.enabled ||
7
- !search.hasKnowledge ||
8
- !attachmentIds?.length ||
9
- !search.pendingFiles?.length) {
4
+ const search = await raceAbort(engine.ragSearch({ ...request, immediate: true }), signal);
5
+ if (!search.enabled || !search.hasKnowledge || !search.chatFilesIndexingHere) {
10
6
  return search;
11
7
  }
12
- const watch = engine.fileStatusWatch({ chatId: request.chatId });
13
- const iterator = watch[Symbol.asyncIterator]();
14
- let wanted = null;
15
- let waiting = false;
8
+ preparing(true);
16
9
  try {
17
- while (true) {
18
- const next = await raceAbort(iterator.next(), signal);
19
- if (next.done) {
20
- throw new Error('file status closed while preparing documents');
21
- }
22
- const files = next.value.files;
23
- const ids = (wanted ??= attachedPending(attachmentIds, {
24
- files,
25
- pendingFiles: search.pendingFiles
26
- }));
27
- if (ids.size === 0) {
28
- return search;
29
- }
30
- if (!files.some((file) => ids.has(file.fileId) && pending(file))) {
31
- return await raceAbort(engine.ragSearch(request), signal);
32
- }
33
- if (!waiting) {
34
- waiting = true;
35
- preparing(true);
36
- }
37
- }
10
+ return await raceAbort(engine.ragSearch(request), signal);
38
11
  }
39
12
  finally {
40
- watch.destroy();
41
- if (waiting) {
42
- preparing(false);
43
- }
13
+ preparing(false);
44
14
  }
45
15
  }
46
- function pending(file) {
47
- const rag = file.processors.find((processor) => processor.processorId === 'rag');
48
- return (!file.readyForChat &&
49
- ['pending', 'running', 'uploading'].includes(file.state) &&
50
- rag?.state !== 'not-applicable');
51
- }
52
- function attachedPending(attachmentIds, { files, pendingFiles }) {
53
- return new Set(files
54
- .filter((file) => attachmentIds.includes(file.fileId) && pendingFiles.includes(file.fileName))
55
- .map((file) => file.fileId));
56
- }
@@ -0,0 +1,35 @@
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
+ readonly tickMs?: number;
13
+ }
14
+ interface IdleWindow {
15
+ readonly from: number;
16
+ readonly cutoff: number;
17
+ }
18
+ export declare function kvCacheRetentionMs(value: unknown, defaultMs?: number): number | null;
19
+ export declare function kvCacheSweptKey(meshId: string): string;
20
+ export declare function idleChatIds(chats: ReadonlyArray<AssistantChat>, { from, cutoff }: IdleWindow): string[];
21
+ export declare class KvCacheRetention {
22
+ private readonly _engine;
23
+ private readonly _opts;
24
+ private readonly _now;
25
+ private readonly _queued;
26
+ private _queue;
27
+ private _timer;
28
+ constructor(engine: RetentionEngine, opts: KvCacheRetentionOptions);
29
+ sweep(meshId: string): Promise<void>;
30
+ start(meshIds: () => Iterable<string>): void;
31
+ stop(): void;
32
+ private _sweep;
33
+ private _deleteIdle;
34
+ }
35
+ 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 = 12 * 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
+ }, this._opts.tickMs ?? 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
+ }
@@ -0,0 +1,10 @@
1
+ type ReadCallback = (error: Error | null) => void;
2
+ export interface LatestFrame<Frame> {
3
+ offer(frame: Frame): void;
4
+ end(): void;
5
+ read(callback: ReadCallback): void;
6
+ clear(): void;
7
+ release(): void;
8
+ }
9
+ export declare function latestFrame<Frame>(push: (frame: Frame | null) => void): LatestFrame<Frame>;
10
+ export {};
@@ -0,0 +1,48 @@
1
+ export function latestFrame(push) {
2
+ let latest = null;
3
+ let ended = false;
4
+ let pending = null;
5
+ const settle = () => {
6
+ const callback = pending;
7
+ pending = null;
8
+ callback?.(null);
9
+ };
10
+ return {
11
+ offer(frame) {
12
+ if (pending) {
13
+ push(frame);
14
+ settle();
15
+ }
16
+ else {
17
+ latest = { frame };
18
+ }
19
+ },
20
+ end() {
21
+ ended = true;
22
+ if (pending) {
23
+ push(null);
24
+ settle();
25
+ }
26
+ },
27
+ read(callback) {
28
+ if (latest) {
29
+ const { frame } = latest;
30
+ latest = null;
31
+ push(frame);
32
+ return callback(null);
33
+ }
34
+ if (ended) {
35
+ push(null);
36
+ return callback(null);
37
+ }
38
+ pending = callback;
39
+ },
40
+ clear() {
41
+ latest = null;
42
+ },
43
+ release() {
44
+ latest = null;
45
+ settle();
46
+ }
47
+ };
48
+ }
@@ -22,6 +22,8 @@ export type DownloadFrame = ModelProgressFrame;
22
22
  export type DownloadEventStream = AsyncIterable<ModelProgressFrame>;
23
23
  export interface CacheWaitOptions {
24
24
  readonly onHold?: () => void;
25
+ readonly onProgress?: (percentage: number) => void;
26
+ readonly onStall?: () => void;
25
27
  }
26
28
  interface DownloadHarness {
27
29
  list?(options?: {
@@ -171,7 +173,7 @@ export declare class ModelDownloadReconciler {
171
173
  observeImageProgress(observation: ImageDownloadObservation): void;
172
174
  private _transfer;
173
175
  private _completeTransfer;
174
- waitUntilCached(model: string, signal: AbortSignal, { onHold }?: CacheWaitOptions): Promise<void>;
176
+ waitUntilCached(model: string, signal: AbortSignal, { onHold, onProgress, onStall }?: CacheWaitOptions): Promise<void>;
175
177
  retry(name: string): Promise<void>;
176
178
  release<T>(name: string, removeFiles: () => Promise<T>): Promise<T>;
177
179
  close(): Promise<void>;
@@ -187,6 +189,9 @@ export declare class ModelDownloadReconciler {
187
189
  private _step;
188
190
  private _writeWaiting;
189
191
  private _announceHolds;
192
+ private _announceProgress;
193
+ private _announceStall;
194
+ private _writeFailure;
190
195
  private _flushObservedProgress;
191
196
  private _shouldPreempt;
192
197
  private _waiting;
@@ -182,7 +182,7 @@ export class ModelDownloadReconciler {
182
182
  this._pendingCompletions.delete(name);
183
183
  this._completedTransfers.add(name);
184
184
  }
185
- async waitUntilCached(model, signal, { onHold } = {}) {
185
+ async waitUntilCached(model, signal, { onHold, onProgress, onStall } = {}) {
186
186
  if (this._closed ||
187
187
  !this._harness.list ||
188
188
  !this._harness.download ||
@@ -207,7 +207,15 @@ export class ModelDownloadReconciler {
207
207
  this._removeDemandWaiter(model, waiter);
208
208
  resolve();
209
209
  };
210
- const waiter = { signal, abort, reject, resolve, onHold: onHold ?? null };
210
+ const waiter = {
211
+ signal,
212
+ abort,
213
+ reject,
214
+ resolve,
215
+ onHold: onHold ?? null,
216
+ onProgress: onProgress ?? null,
217
+ onStall: onStall ?? null
218
+ };
211
219
  const demand = this._demands.get(model) ?? {
212
220
  addon,
213
221
  failures: 0,
@@ -455,6 +463,7 @@ export class ModelDownloadReconciler {
455
463
  for (const target of targets) {
456
464
  if (target.paused) {
457
465
  await this._status.write(target.name, 'paused', { bytesPerSecond: 0 });
466
+ this._announceStall(target.name);
458
467
  }
459
468
  else if (target.held) {
460
469
  await this._status.write(target.name, 'held', { total: target.total, bytesPerSecond: 0 });
@@ -472,12 +481,24 @@ export class ModelDownloadReconciler {
472
481
  if (!target.held)
473
482
  continue;
474
483
  for (const waiter of this._demands.get(target.name)?.waiters ?? []) {
475
- const onHold = waiter.onHold;
476
- waiter.onHold = null;
477
- onHold?.();
484
+ waiter.onHold?.();
478
485
  }
479
486
  }
480
487
  }
488
+ _announceProgress(model, percentage) {
489
+ for (const waiter of this._demands.get(model)?.waiters ?? []) {
490
+ waiter.onProgress?.(percentage);
491
+ }
492
+ }
493
+ _announceStall(model) {
494
+ for (const waiter of this._demands.get(model)?.waiters ?? []) {
495
+ waiter.onStall?.();
496
+ }
497
+ }
498
+ async _writeFailure(model, error) {
499
+ await this._status.write(model, 'error', { error, bytesPerSecond: 0 });
500
+ this._announceStall(model);
501
+ }
481
502
  async _flushObservedProgress() {
482
503
  for (const [model, progress] of this._observedProgress) {
483
504
  await this._transfer(model);
@@ -513,6 +534,7 @@ export class ModelDownloadReconciler {
513
534
  total,
514
535
  bytesPerSecond: speed.sample({ downloaded, total, aggregate: true }, progress.at, model)
515
536
  });
537
+ this._announceProgress(model, progress.percentage);
516
538
  }
517
539
  }
518
540
  _shouldPreempt(preferred, preemptLlm) {
@@ -828,6 +850,7 @@ export class ModelDownloadReconciler {
828
850
  }
829
851
  const bytes = transfer ?? progressBytes(event);
830
852
  const percentage = transfer?.percentage ?? 0;
853
+ this._announceProgress(model, percentage);
831
854
  if (shouldFlush(localFlush, { at, percentage })) {
832
855
  const written = await this._status.write(model, 'downloading', {
833
856
  percentage,
@@ -859,6 +882,7 @@ export class ModelDownloadReconciler {
859
882
  })) {
860
883
  localFlush = { at: initialAt, percentage: initialPercentage };
861
884
  }
885
+ this._announceProgress(model, initialPercentage);
862
886
  if (!this._harness.download) {
863
887
  throw new Error('the harness cannot download models');
864
888
  }
@@ -887,18 +911,12 @@ export class ModelDownloadReconciler {
887
911
  else if (event.type === 'error' && current()) {
888
912
  retry = true;
889
913
  failureMessage = typeof event.message === 'string' ? event.message : 'download failed';
890
- await this._status.write(model, 'error', {
891
- error: failureMessage,
892
- bytesPerSecond: 0
893
- });
914
+ await this._writeFailure(model, failureMessage);
894
915
  }
895
916
  else if (event.type === 'aborted' && current() && !controller.signal.aborted) {
896
917
  retry = true;
897
918
  failureMessage = 'download interrupted';
898
- await this._status.write(model, 'error', {
899
- error: failureMessage,
900
- bytesPerSecond: 0
901
- });
919
+ await this._writeFailure(model, failureMessage);
902
920
  }
903
921
  }
904
922
  if (current() && !controller.signal.aborted && !completed && !retry) {
@@ -909,10 +927,7 @@ export class ModelDownloadReconciler {
909
927
  if (!this._closed && current()) {
910
928
  retry = true;
911
929
  failureMessage = errorMessage(err);
912
- await this._status.write(model, 'error', {
913
- error: failureMessage,
914
- bytesPerSecond: 0
915
- });
930
+ await this._writeFailure(model, failureMessage);
916
931
  }
917
932
  }
918
933
  finally {
@@ -1,6 +1,6 @@
1
1
  import { scopedKey } from './keys.js';
2
2
  export const REFRESH_SKEW_MS = 60_000;
3
- const inFlightRefresh = new Map();
3
+ const inFlightRefresh = new WeakMap();
4
4
  export async function resolveOAuthCredential(opts) {
5
5
  const { engine, agentId, credentialKey, stateKey, refresh, requireClientSecret = false } = opts;
6
6
  const { value: token } = await engine.configGet({ key: scopedKey(agentId, credentialKey) });
@@ -28,9 +28,17 @@ export async function resolveOAuthCredential(opts) {
28
28
  if (typeof expiresAt !== 'number' || expiresAt - REFRESH_SKEW_MS > Date.now()) {
29
29
  return token;
30
30
  }
31
- const refreshKey = `${credentialKey}:${agentId ?? ''}`;
31
+ const refreshKey = JSON.stringify([
32
+ scopedKey(agentId, credentialKey),
33
+ scopedKey(agentId, stateKey)
34
+ ]);
32
35
  try {
33
- let pending = inFlightRefresh.get(refreshKey);
36
+ let engineRefreshes = inFlightRefresh.get(engine);
37
+ if (!engineRefreshes) {
38
+ engineRefreshes = new Map();
39
+ inFlightRefresh.set(engine, engineRefreshes);
40
+ }
41
+ let pending = engineRefreshes.get(refreshKey);
34
42
  if (!pending) {
35
43
  pending = (async () => {
36
44
  const refreshed = await refresh({
@@ -52,8 +60,8 @@ export async function resolveOAuthCredential(opts) {
52
60
  })
53
61
  });
54
62
  return refreshed.accessToken;
55
- })().finally(() => inFlightRefresh.delete(refreshKey));
56
- inFlightRefresh.set(refreshKey, pending);
63
+ })().finally(() => engineRefreshes?.delete(refreshKey));
64
+ engineRefreshes.set(refreshKey, pending);
57
65
  }
58
66
  return await pending;
59
67
  }
@@ -1,6 +1,7 @@
1
1
  import { Readable } from 'streamx';
2
2
  import { frameReadable } from './frame-readable.js';
3
3
  import { asError } from './guards.js';
4
+ import { latestFrame } from './latest-frame.js';
4
5
  const RETRY_MIN_MS = 250;
5
6
  const RETRY_MAX_MS = 5_000;
6
7
  export function reconnectingWatch(open, onDisconnect) {
@@ -8,8 +9,9 @@ export function reconnectingWatch(open, onDisconnect) {
8
9
  let retry = null;
9
10
  let retryMs = RETRY_MIN_MS;
10
11
  let output;
12
+ const frames = latestFrame((frame) => output.push(frame));
11
13
  const schedule = (error) => {
12
- if (output.destroying)
14
+ if (output.destroying || retry)
13
15
  return;
14
16
  onDisconnect(error);
15
17
  retry = setTimeout(() => {
@@ -19,7 +21,7 @@ export function reconnectingWatch(open, onDisconnect) {
19
21
  retryMs = Math.min(retryMs * 2, RETRY_MAX_MS);
20
22
  };
21
23
  const connect = () => {
22
- if (output.destroying)
24
+ if (output.destroying || closeConnection)
23
25
  return;
24
26
  let source;
25
27
  try {
@@ -40,12 +42,13 @@ export function reconnectingWatch(open, onDisconnect) {
40
42
  if (closeConnection !== close)
41
43
  return;
42
44
  closeConnection = null;
45
+ frames.clear();
43
46
  close();
44
47
  schedule(error);
45
48
  };
46
49
  const onData = (frame) => {
47
50
  retryMs = RETRY_MIN_MS;
48
- output.push(frame);
51
+ frames.offer(frame);
49
52
  };
50
53
  const onError = (error) => disconnect(error);
51
54
  const onEnd = () => disconnect(new Error('Watch stream ended unexpectedly'));
@@ -55,22 +58,27 @@ export function reconnectingWatch(open, onDisconnect) {
55
58
  source.on('end', onEnd);
56
59
  source.on('close', onEnd);
57
60
  };
61
+ const stop = () => {
62
+ if (retry) {
63
+ clearTimeout(retry);
64
+ }
65
+ retry = null;
66
+ frames.release();
67
+ const close = closeConnection;
68
+ closeConnection = null;
69
+ close?.();
70
+ };
58
71
  output = new Readable({
59
72
  open(cb) {
60
73
  connect();
61
74
  cb(null);
62
75
  },
63
76
  read(cb) {
64
- cb(null);
77
+ frames.read(cb);
65
78
  },
79
+ predestroy: stop,
66
80
  destroy(cb) {
67
- if (retry) {
68
- clearTimeout(retry);
69
- }
70
- retry = null;
71
- const close = closeConnection;
72
- closeConnection = null;
73
- close?.();
81
+ stop();
74
82
  cb(null);
75
83
  }
76
84
  });
@@ -1,6 +1,7 @@
1
1
  export declare const RUN_ABORT_REASONS: {
2
2
  readonly suspended: 'suspended';
3
3
  readonly ownershipLost: 'ownership-lost';
4
+ readonly agentRemoved: 'agent-removed';
4
5
  readonly runSettled: 'run-settled';
5
6
  readonly runnerDied: 'runner-died';
6
7
  readonly runnerClosed: 'runner-closed';
@@ -13,6 +14,7 @@ export declare const RUN_FAILURE_REASONS: {
13
14
  readonly runtimeEndedTurn: 'the model runtime ended this turn';
14
15
  readonly capabilityUnavailable: 'this device cannot run a capability without a model';
15
16
  readonly stalled: 'the agent stopped responding';
17
+ readonly agentRemoved: 'the agent was removed';
16
18
  readonly videoNeedsVision: 'this model cannot see video: switch to a model that reads images to ask about one';
17
19
  };
18
20
  export type AbortOutcome = {
@@ -1,6 +1,7 @@
1
1
  export const RUN_ABORT_REASONS = {
2
2
  suspended: 'suspended',
3
3
  ownershipLost: 'ownership-lost',
4
+ agentRemoved: 'agent-removed',
4
5
  runSettled: 'run-settled',
5
6
  runnerDied: 'runner-died',
6
7
  runnerClosed: 'runner-closed',
@@ -12,6 +13,7 @@ export const RUN_FAILURE_REASONS = {
12
13
  runtimeEndedTurn: 'the model runtime ended this turn',
13
14
  capabilityUnavailable: 'this device cannot run a capability without a model',
14
15
  stalled: 'the agent stopped responding',
16
+ agentRemoved: 'the agent was removed',
15
17
  videoNeedsVision: 'this model cannot see video: switch to a model that reads images to ask about one'
16
18
  };
17
19
  export function knownAbortOutcome(reason) {
@@ -26,6 +28,9 @@ export function knownAbortOutcome(reason) {
26
28
  if (reason === RUN_ABORT_REASONS.runnerDied) {
27
29
  return { kind: 'failed', reason: RUN_FAILURE_REASONS.harnessDied };
28
30
  }
31
+ if (reason === RUN_ABORT_REASONS.agentRemoved) {
32
+ return { kind: 'failed', reason: RUN_FAILURE_REASONS.agentRemoved };
33
+ }
29
34
  if (reason === RUN_ABORT_REASONS.stalled) {
30
35
  return { kind: 'failed', reason: RUN_FAILURE_REASONS.stalled };
31
36
  }
@@ -4,10 +4,18 @@ export interface ReplayChat {
4
4
  readonly holder: string;
5
5
  readonly chatId: string;
6
6
  }
7
+ export interface Forgotten {
8
+ readonly holder?: string | undefined;
9
+ readonly reason?: string | undefined;
10
+ }
7
11
  export interface SentRun {
8
12
  readonly messages: ChatMessage[];
9
13
  readonly asked?: string | undefined;
10
14
  }
15
+ export interface ReplayData {
16
+ readonly sentRuns: Map<string, ChatMessage[]>;
17
+ readonly askedRuns: Map<string, string>;
18
+ }
11
19
  export declare class RunReplayStore {
12
20
  private readonly _dir;
13
21
  constructor(dir: string);
@@ -15,15 +23,19 @@ export declare class RunReplayStore {
15
23
  start(): Promise<void>;
16
24
  load(chat: ReplayChat): Promise<Map<string, ChatMessage[]>>;
17
25
  loadAsked(chat: ReplayChat): Promise<Map<string, string>>;
26
+ loadReplay(chat: ReplayChat): Promise<ReplayData>;
18
27
  save({ holder, chatId }: ReplayChat, runId: string, { messages, asked }: SentRun): Promise<void>;
28
+ rebuild({ holder, chatId }: ReplayChat): Promise<string | null>;
29
+ consume({ holder, chatId }: ReplayChat): Promise<void>;
19
30
  private _records;
20
- forget(chatId: string, holder?: string): Promise<void>;
31
+ forget(chatId: string, { holder, reason }?: Forgotten): Promise<void>;
21
32
  clear(holder: string): Promise<void>;
22
33
  private _chatDir;
23
34
  private _holders;
24
35
  }
25
36
  export interface SentRunTracker {
26
37
  readonly used: boolean;
38
+ readonly onKey: boolean;
27
39
  observe(call: Record<string, unknown>): void;
28
40
  turns(inputLength: number): ChatMessage[] | null;
29
41
  }