@qvac/core 1.0.0-rc.7 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. package/CHANGELOG.md +241 -3
  2. package/README.md +1 -0
  3. package/dist-lib/lib/api/attachments.d.ts +1 -1
  4. package/dist-lib/lib/api/attachments.js +11 -0
  5. package/dist-lib/lib/api/chunks.d.ts +4 -2
  6. package/dist-lib/lib/api/chunks.js +6 -3
  7. package/dist-lib/lib/api/device-routes.d.ts +6 -1
  8. package/dist-lib/lib/api/device-routes.js +20 -1
  9. package/dist-lib/lib/api/devices.js +6 -18
  10. package/dist-lib/lib/api/execution.d.ts +6 -2
  11. package/dist-lib/lib/api/execution.js +29 -3
  12. package/dist-lib/lib/api/host-run-log.d.ts +7 -1
  13. package/dist-lib/lib/api/host-run-log.js +3 -1
  14. package/dist-lib/lib/api/index.d.ts +3 -1
  15. package/dist-lib/lib/api/index.js +17 -6
  16. package/dist-lib/lib/api/members.js +13 -29
  17. package/dist-lib/lib/api/mesh.d.ts +2 -1
  18. package/dist-lib/lib/api/projects.d.ts +1 -1
  19. package/dist-lib/lib/api/projects.js +9 -5
  20. package/dist-lib/lib/api/rag-label.d.ts +4 -0
  21. package/dist-lib/lib/api/rag-label.js +21 -0
  22. package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
  23. package/dist-lib/lib/api/rag-lexical.js +1058 -0
  24. package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
  25. package/dist-lib/lib/api/rag-manifest.js +46 -0
  26. package/dist-lib/lib/api/rag-outline.d.ts +36 -0
  27. package/dist-lib/lib/api/rag-outline.js +426 -0
  28. package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
  29. package/dist-lib/lib/api/rag-plugin.js +70 -0
  30. package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
  31. package/dist-lib/lib/api/rag-synthesis.js +102 -0
  32. package/dist-lib/lib/api/rag-targets.d.ts +23 -0
  33. package/dist-lib/lib/api/rag-targets.js +215 -0
  34. package/dist-lib/lib/api/rag-window.d.ts +5 -0
  35. package/dist-lib/lib/api/rag-window.js +46 -0
  36. package/dist-lib/lib/api/rag.d.ts +3 -2
  37. package/dist-lib/lib/api/rag.js +421 -184
  38. package/dist-lib/lib/api/served.js +8 -14
  39. package/dist-lib/lib/api/workspace.d.ts +8 -1
  40. package/dist-lib/lib/api/workspace.js +27 -13
  41. package/dist-lib/lib/assistant/harness.d.ts +9 -1
  42. package/dist-lib/lib/assistant/history.d.ts +11 -2
  43. package/dist-lib/lib/assistant/history.js +74 -4
  44. package/dist-lib/lib/assistant/media-round.d.ts +61 -0
  45. package/dist-lib/lib/assistant/media-round.js +111 -0
  46. package/dist-lib/lib/assistant/message.d.ts +7 -0
  47. package/dist-lib/lib/assistant/model-round.d.ts +19 -1
  48. package/dist-lib/lib/assistant/model-round.js +163 -28
  49. package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
  50. package/dist-lib/lib/assistant/pdf-pages.js +67 -3
  51. package/dist-lib/lib/assistant/round-host.d.ts +22 -0
  52. package/dist-lib/lib/assistant/round-host.js +142 -8
  53. package/dist-lib/lib/assistant/upload.d.ts +12 -0
  54. package/dist-lib/lib/assistant/upload.js +36 -0
  55. package/dist-lib/lib/assistant-client.d.ts +1 -0
  56. package/dist-lib/lib/assistant-client.js +40 -19
  57. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  58. package/dist-lib/lib/assistant-tree.js +48 -1
  59. package/dist-lib/lib/assistant.d.ts +18 -1
  60. package/dist-lib/lib/assistant.js +484 -95
  61. package/dist-lib/lib/client.d.ts +1 -0
  62. package/dist-lib/lib/client.js +4 -1
  63. package/dist-lib/lib/compaction.js +2 -2
  64. package/dist-lib/lib/core/agent.js +12 -45
  65. package/dist-lib/lib/core/approval-rules.js +1 -8
  66. package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
  67. package/dist-lib/lib/core/archived-mesh.js +12 -8
  68. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  69. package/dist-lib/lib/core/chat-copy.js +74 -25
  70. package/dist-lib/lib/core/chat-fork.js +15 -6
  71. package/dist-lib/lib/core/chat-group.js +16 -24
  72. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  73. package/dist-lib/lib/core/chat-move.js +94 -3
  74. package/dist-lib/lib/core/chat-order.js +12 -18
  75. package/dist-lib/lib/core/chat.js +38 -31
  76. package/dist-lib/lib/core/chunk.d.ts +2 -2
  77. package/dist-lib/lib/core/chunk.js +18 -25
  78. package/dist-lib/lib/core/collections.d.ts +2 -0
  79. package/dist-lib/lib/core/collections.js +2 -0
  80. package/dist-lib/lib/core/contract/activity.d.ts +4 -0
  81. package/dist-lib/lib/core/contract/activity.js +34 -4
  82. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  83. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  84. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  85. package/dist-lib/lib/core/contract/chat.js +127 -5
  86. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  87. package/dist-lib/lib/core/contract/chunks.js +37 -6
  88. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  89. package/dist-lib/lib/core/contract/features.js +22 -0
  90. package/dist-lib/lib/core/contract/index.js +2 -0
  91. package/dist-lib/lib/core/contract/indexing.js +4 -2
  92. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  93. package/dist-lib/lib/core/contract/members.js +16 -10
  94. package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
  95. package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
  96. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  97. package/dist-lib/lib/core/contract/projects.js +13 -5
  98. package/dist-lib/lib/core/contract/roles.js +32 -4
  99. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
  100. package/dist-lib/lib/core/contract/run-requests.js +35 -2
  101. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  102. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  103. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  104. package/dist-lib/lib/core/contract/workspace.js +40 -7
  105. package/dist-lib/lib/core/contract/writers.js +2 -2
  106. package/dist-lib/lib/core/departures.d.ts +3 -1
  107. package/dist-lib/lib/core/departures.js +3 -0
  108. package/dist-lib/lib/core/devices.js +1 -8
  109. package/dist-lib/lib/core/files/attachments.d.ts +2 -0
  110. package/dist-lib/lib/core/files/attachments.js +19 -1
  111. package/dist-lib/lib/core/files/index.d.ts +20 -4
  112. package/dist-lib/lib/core/files/index.js +132 -21
  113. package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
  114. package/dist-lib/lib/core/files/processing/document.js +7 -0
  115. package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
  116. package/dist-lib/lib/core/files/processing/index.js +144 -12
  117. package/dist-lib/lib/core/files/processing/protocol.js +13 -34
  118. package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
  119. package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
  120. package/dist-lib/lib/core/files/rag/build.js +123 -19
  121. package/dist-lib/lib/core/files/rag/failure.js +2 -1
  122. package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
  123. package/dist-lib/lib/core/files/rag/index.js +13 -1
  124. package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
  125. package/dist-lib/lib/core/files/rag/load.js +27 -6
  126. package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
  127. package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
  128. package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
  129. package/dist-lib/lib/core/files/rag/produce.js +104 -16
  130. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
  131. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
  132. package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
  133. package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
  134. package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
  135. package/dist-lib/lib/core/files/records/knowledge.js +7 -40
  136. package/dist-lib/lib/core/files/records/reclaim.js +5 -10
  137. package/dist-lib/lib/core/files/upload.js +68 -41
  138. package/dist-lib/lib/core/fs-drives.js +14 -26
  139. package/dist-lib/lib/core/invite.d.ts +1 -0
  140. package/dist-lib/lib/core/invite.js +4 -2
  141. package/dist-lib/lib/core/local/files.d.ts +1 -1
  142. package/dist-lib/lib/core/local/files.js +5 -5
  143. package/dist-lib/lib/core/local/index.d.ts +13 -6
  144. package/dist-lib/lib/core/local/index.js +103 -69
  145. package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
  146. package/dist-lib/lib/core/local/run-logs.js +5 -5
  147. package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
  148. package/dist-lib/lib/core/mesh-processor.js +87 -0
  149. package/dist-lib/lib/core/mesh.d.ts +48 -5
  150. package/dist-lib/lib/core/mesh.js +318 -129
  151. package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
  152. package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
  153. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
  154. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
  155. package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
  156. package/dist-lib/lib/core/migrations/writer-links.js +32 -0
  157. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  158. package/dist-lib/lib/core/project-keys.js +11 -0
  159. package/dist-lib/lib/core/public-mesh.d.ts +3 -0
  160. package/dist-lib/lib/core/public-mesh.js +10 -8
  161. package/dist-lib/lib/core/run-requests.d.ts +3 -0
  162. package/dist-lib/lib/core/run-requests.js +21 -1
  163. package/dist-lib/lib/core/skill-availability.d.ts +33 -0
  164. package/dist-lib/lib/core/skill-availability.js +67 -0
  165. package/dist-lib/lib/core/sync/background.d.ts +0 -1
  166. package/dist-lib/lib/core/sync/background.js +2 -1
  167. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  168. package/dist-lib/lib/core/sync/changed.js +1 -1
  169. package/dist-lib/lib/core/sync/index.js +52 -10
  170. package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
  171. package/dist-lib/lib/core/sync/view-updates.js +82 -91
  172. package/dist-lib/lib/core/sync-status.js +97 -41
  173. package/dist-lib/lib/core/tool-activity.js +6 -12
  174. package/dist-lib/lib/core/tool-configs.js +17 -29
  175. package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
  176. package/dist-lib/lib/core/upgrade-summary.js +67 -0
  177. package/dist-lib/lib/core/view-read.d.ts +4 -0
  178. package/dist-lib/lib/core/view-read.js +23 -0
  179. package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
  180. package/dist-lib/lib/core/workspace-upgrade.js +27 -16
  181. package/dist-lib/lib/core.d.ts +45 -42
  182. package/dist-lib/lib/core.js +675 -640
  183. package/dist-lib/lib/errors.d.ts +24 -0
  184. package/dist-lib/lib/errors.js +24 -0
  185. package/dist-lib/lib/llm-model-names.d.ts +1 -1
  186. package/dist-lib/lib/llm-model-names.js +1 -1
  187. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  188. package/dist-lib/lib/model-context.js +1 -2
  189. package/dist-lib/lib/model-download.d.ts +6 -1
  190. package/dist-lib/lib/model-download.js +16 -3
  191. package/dist-lib/lib/oauth/resolve.js +29 -8
  192. package/dist-lib/lib/oauth/token-exchange-core.js +11 -0
  193. package/dist-lib/lib/probe-rounds.d.ts +7 -2
  194. package/dist-lib/lib/probe-rounds.js +6 -1
  195. package/dist-lib/lib/run-transcript.d.ts +4 -1
  196. package/dist-lib/lib/run-transcript.js +15 -6
  197. package/dist-lib/schema/engine.js +83 -6
  198. package/dist-lib/schema/indexes.d.ts +3 -0
  199. package/dist-lib/schema/indexes.js +5 -0
  200. package/dist-lib/schema/local.js +11 -2
  201. package/dist-lib/schema/qvac.js +49 -5
  202. package/dist-lib/spec/bind.js +34 -0
  203. package/dist-lib/spec/calls.js +5 -0
  204. package/dist-lib/spec/capabilities.d.ts +17 -0
  205. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  206. package/dist-lib/spec/hrpc/index.js +68 -5
  207. package/dist-lib/spec/hrpc/messages.js +260 -23
  208. package/dist-lib/spec/hyperschema/index.js +260 -23
  209. package/dist-lib/spec/hyperschema/schema.json +181 -0
  210. package/dist-lib/spec/hyperschema/types.d.ts +48 -0
  211. package/dist-lib/spec/local/hyperdb/messages.js +270 -224
  212. package/dist-lib/spec/local/hyperschema/index.js +73 -38
  213. package/dist-lib/spec/local/hyperschema/schema.json +31 -0
  214. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
  215. package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
  216. package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
  217. package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
  218. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
  219. package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
  220. package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
  221. package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
  222. package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
  223. package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
  224. package/package.json +82 -71
  225. package/tui/README.md +52 -0
  226. package/tui/app.mjs +69 -12
  227. package/tui/boot.mjs +27 -6
  228. package/tui/cli.mjs +93 -0
  229. package/tui/index.mjs +2 -87
  230. package/tui/lib/attachment-files.mjs +82 -0
  231. package/tui/lib/reopen-meshes.mjs +15 -0
  232. package/tui/lib/updater.mjs +86 -0
  233. package/tui/main.mjs +29 -57
  234. package/worker/assistant-sidecar.mjs +10 -3
  235. package/worker/stow-entry.mjs +3 -52
  236. package/dist-lib/lib/core/chat-project.d.ts +0 -2
  237. package/dist-lib/lib/core/chat-project.js +0 -15
@@ -1,11 +1,12 @@
1
1
  import b4a from 'b4a';
2
2
  import { AbortController } from 'bare-abort-controller';
3
3
  import { createRagSourceTagFilter } from '../core/files/index.js';
4
- import { errorMessage } from '../guards.js';
4
+ import { errorMessage, isRecord } from '../guards.js';
5
5
  import { chatKvCacheKey } from '../kv-cache-reclaim.js';
6
6
  import { meshScoped } from '../mesh-scoped-engine.js';
7
7
  import { isProbeRound } from '../probe-rounds.js';
8
8
  import { RunTranscript } from '../run-transcript.js';
9
+ import { MODEL_MISSING, isModelMissing, mediaKindOf, mediaOptions, putBlob, readAttachment, readBlob, storedBytes } from './media-round.js';
9
10
  import { servedRequest } from './model-round.js';
10
11
  const LEFT = 'the round left this device';
11
12
  const PLAIN_ID = /^[\w-]+(\/[\w-]+)*$/;
@@ -31,8 +32,14 @@ export class RoundHost {
31
32
  this._opts = opts;
32
33
  }
33
34
  async follow(meshId, frames) {
34
- for await (const { chunks } of frames) {
35
- this._reconcile(meshId, chunks ?? []);
35
+ this._opts.log?.info('rounds', 'watching rounds', { mesh: meshId.slice(0, 8) });
36
+ try {
37
+ for await (const { chunks } of frames) {
38
+ this._reconcile(meshId, chunks ?? []);
39
+ }
40
+ }
41
+ finally {
42
+ this._opts.log?.info('rounds', 'stopped watching rounds', { mesh: meshId.slice(0, 8) });
36
43
  }
37
44
  }
38
45
  left(chatId, agentId) {
@@ -50,9 +57,19 @@ export class RoundHost {
50
57
  controller.abort(LEFT);
51
58
  }
52
59
  }
60
+ drop(meshId) {
61
+ for (const [id, serving] of this._serving) {
62
+ if (serving.meshId !== meshId)
63
+ continue;
64
+ serving.controller.abort(LEFT);
65
+ this._serving.delete(id);
66
+ }
67
+ }
53
68
  _reconcile(meshId, rounds) {
54
69
  const me = this._opts.deviceId();
55
70
  for (const [id, serving] of this._serving) {
71
+ if (serving.meshId !== meshId)
72
+ continue;
56
73
  const round = rounds.find((chunk) => chunk.id === id);
57
74
  const takenBy = round?.modelRequest?.takenBy;
58
75
  const stale = (round?.updatedAt ?? 0) < serving.takenAt;
@@ -61,6 +78,11 @@ export class RoundHost {
61
78
  ? state === 'taken' && b4a.equals(takenBy, me)
62
79
  : state === 'open' && stale;
63
80
  if (serving.taken && !ours) {
81
+ this._opts.log?.info('rounds', 'served round dropped', {
82
+ roundId: id,
83
+ reason: round ? `round ${state}` : 'round gone',
84
+ takenBy: takenBy ? shortId(takenBy) : undefined
85
+ });
64
86
  serving.controller.abort(LEFT);
65
87
  this._serving.delete(id);
66
88
  }
@@ -74,6 +96,10 @@ export class RoundHost {
74
96
  _pick(meshId, round, me) {
75
97
  const { state, takenBy, hostDeviceId } = round.modelRequest ?? {};
76
98
  if (hostDeviceId && !b4a.equals(hostDeviceId, me)) {
99
+ this._opts.log?.debug('rounds', 'round skipped: it names another host', {
100
+ roundId: round.id,
101
+ host: shortId(hostDeviceId)
102
+ });
77
103
  return;
78
104
  }
79
105
  if (state === 'open' || (takenBy && b4a.equals(takenBy, me))) {
@@ -84,6 +110,7 @@ export class RoundHost {
84
110
  const serving = {
85
111
  meshId,
86
112
  controller: new AbortController(),
113
+ since: Date.now(),
87
114
  taken: false,
88
115
  takenAt: 0
89
116
  };
@@ -101,18 +128,28 @@ export class RoundHost {
101
128
  if (!(await this._provides(engine, round))) {
102
129
  return;
103
130
  }
131
+ const kind = mediaKindOf(round.modelRequest?.options);
132
+ if (kind) {
133
+ return this._generate(engine, { round, serving }, kind);
134
+ }
104
135
  const { media } = this._opts;
105
136
  const { signal } = serving.controller;
106
137
  try {
107
138
  const request = await servedRequest(engine, round, media
108
139
  ? (sources, modelId) => media.resolve(sources, { id: round.id, modelId, signal })
109
140
  : undefined);
141
+ if (!request) {
142
+ this._opts.log?.debug('rounds', 'round skipped: its prompt is not readable here', {
143
+ roundId: round.id
144
+ });
145
+ }
110
146
  if (!request || signal.aborted) {
111
147
  return;
112
148
  }
149
+ const load = await this._load(engine, round);
113
150
  if (await this._takeFor(engine, round, serving)) {
114
151
  serving.taken = true;
115
- await this._answer(engine, { round, request, serving });
152
+ await this._answer(engine, { round, request: { ...request, load }, serving });
116
153
  }
117
154
  }
118
155
  finally {
@@ -128,9 +165,19 @@ export class RoundHost {
128
165
  if (agent && b4a.equals(agent.providerDeviceId, this._opts.deviceId())) {
129
166
  return true;
130
167
  }
168
+ this._opts.log?.debug('rounds', 'round skipped: not our agent', {
169
+ roundId: round.id,
170
+ agentId,
171
+ reason: agent ? 'provided by another device' : 'no such agent in this mesh',
172
+ provider: agent ? shortId(agent.providerDeviceId) : undefined
173
+ });
131
174
  }
132
175
  return false;
133
176
  }
177
+ async _load(engine, round) {
178
+ const agentId = round.modelRequest?.agents?.[0];
179
+ return agentId ? this._opts.load?.(engine, agentId) : undefined;
180
+ }
134
181
  async _answer(engine, { round, request, serving }) {
135
182
  const { signal } = serving.controller;
136
183
  if (isProbeRound(round.modelRequest?.options)) {
@@ -171,7 +218,7 @@ export class RoundHost {
171
218
  write('content', tags.flush());
172
219
  await writing;
173
220
  await transcript.flush();
174
- await engine.settleModelRequest({ chunkId: round.id, state: 'done', result });
221
+ await this._settle(serving, { chunkId: round.id, state: 'done', result });
175
222
  }
176
223
  catch (err) {
177
224
  await writing.catch(noop);
@@ -179,7 +226,7 @@ export class RoundHost {
179
226
  return;
180
227
  }
181
228
  await transcript.flush().catch(noop);
182
- await engine.settleModelRequest({
229
+ await this._settle(serving, {
183
230
  chunkId: round.id,
184
231
  state: 'failed',
185
232
  reason: errorMessage(err)
@@ -192,16 +239,76 @@ export class RoundHost {
192
239
  }
193
240
  }
194
241
  }
242
+ async _generate(engine, { round, serving }, kind) {
243
+ const { generation } = this._opts;
244
+ const options = mediaOptions.safeParse(isRecord(round.modelRequest?.options) ? round.modelRequest.options.mediaRound : undefined);
245
+ const agentId = round.modelRequest?.agents?.[0];
246
+ if (!generation || !options.success || !agentId || !round.runId) {
247
+ return;
248
+ }
249
+ if (!(await this._takeFor(engine, round, serving))) {
250
+ return;
251
+ }
252
+ serving.taken = true;
253
+ const { signal } = serving.controller;
254
+ const settle = (result) => this._settle(serving, { chunkId: round.id, state: 'done', result });
255
+ try {
256
+ if (!(await generation.ready({ engine, agentId, kind, signal }))) {
257
+ await settle(missing(kind));
258
+ return;
259
+ }
260
+ const { modelChoice, params, sources = [], refs } = options.data;
261
+ const { chatId } = round;
262
+ const images = await Promise.all(refs
263
+ ? refs.map(({ attachmentId }) => readAttachment(engine, { chatId, attachmentId }, signal))
264
+ : sources.map((blobId) => readBlob(engine, blobId, signal)));
265
+ const request = {
266
+ kind,
267
+ runId: round.runId,
268
+ modelChoice,
269
+ params,
270
+ images: images.length ? images : undefined,
271
+ sources: refs
272
+ };
273
+ const result = await generation.serve(request, { signal, tick: noop });
274
+ await settle(await this._stored(serving.meshId, engine, result));
275
+ }
276
+ catch (err) {
277
+ if (signal.aborted) {
278
+ return;
279
+ }
280
+ if (isModelMissing(err)) {
281
+ await settle(missing(kind));
282
+ return;
283
+ }
284
+ await this._settle(serving, {
285
+ chunkId: round.id,
286
+ state: 'failed',
287
+ reason: errorMessage(err)
288
+ });
289
+ }
290
+ }
291
+ async _stored(meshId, engine, result) {
292
+ if ('error' in result) {
293
+ return result;
294
+ }
295
+ const blobId = await putBlob(engine, meshId, storedBytes(result));
296
+ if (result.kind === 'image') {
297
+ return { kind: 'image', blobId };
298
+ }
299
+ const { format, gpuRequested, stats } = result;
300
+ return { kind: 'music', blobId, format, gpuRequested, stats };
301
+ }
195
302
  async _probe(engine, { round, request, serving }) {
196
303
  const { signal } = serving.controller;
197
304
  const io = { signal, emit: noop, retake: () => noop };
198
305
  try {
199
306
  const result = await this._opts.serve(request, io);
200
- await engine.settleModelRequest({ chunkId: round.id, state: 'done', result });
307
+ await this._settle(serving, { chunkId: round.id, state: 'done', result });
201
308
  }
202
309
  catch (err) {
203
310
  if (!signal.aborted) {
204
- await engine.settleModelRequest({
311
+ await this._settle(serving, {
205
312
  chunkId: round.id,
206
313
  state: 'failed',
207
314
  reason: errorMessage(err)
@@ -233,8 +340,29 @@ export class RoundHost {
233
340
  const taken = (await engine.takeModelRequest({ chunkId: round.id })).modelRequest;
234
341
  const held = !!taken?.takenBy && b4a.equals(taken.takenBy, this._opts.deviceId());
235
342
  serving.takenAt = held ? (taken?.takenAt ?? 0) : serving.takenAt;
343
+ if (held) {
344
+ this._opts.log?.info('rounds', 'round taken', {
345
+ roundId: round.id,
346
+ mesh: serving.meshId.slice(0, 8)
347
+ });
348
+ }
349
+ else {
350
+ this._opts.log?.debug('rounds', 'round not taken', {
351
+ roundId: round.id,
352
+ state: taken?.state,
353
+ takenBy: taken?.takenBy ? shortId(taken.takenBy) : undefined
354
+ });
355
+ }
236
356
  return held;
237
357
  }
358
+ async _settle(serving, settled) {
359
+ await meshScoped(this._opts.engine, serving.meshId).settleModelRequest(settled);
360
+ this._opts.log?.info('rounds', 'round settled', {
361
+ roundId: settled.chunkId,
362
+ state: settled.state,
363
+ ms: Date.now() - serving.since
364
+ });
365
+ }
238
366
  }
239
367
  async function sourceTitles(engine, round) {
240
368
  const { chunks } = await engine.listChunks({ chatId: round.chatId, type: 'sources' });
@@ -246,4 +374,10 @@ function sourceTags(titles) {
246
374
  filter.add(titles);
247
375
  return filter;
248
376
  }
377
+ function missing(kind) {
378
+ return { error: `this device does not hold the ${kind} model`, code: MODEL_MISSING };
379
+ }
249
380
  function noop() { }
381
+ function shortId(key) {
382
+ return b4a.toHex(key).slice(0, 8);
383
+ }
@@ -0,0 +1,12 @@
1
+ import type { AssistantChunkContentAttachment, AssistantUploadAttachmentResponse } from '../../spec/hyperschema/types.d.ts';
2
+ interface UploadWriter<Frame> {
3
+ readonly destroyed: boolean;
4
+ write(frame: Frame): boolean;
5
+ on(event: 'drain' | 'close', listener: () => void): unknown;
6
+ on(event: 'error', listener: (error: unknown) => void): unknown;
7
+ off(event: 'drain' | 'close', listener: () => void): unknown;
8
+ off(event: 'error', listener: (error: unknown) => void): unknown;
9
+ }
10
+ export declare function readUploadAttachment(frames: AsyncIterable<AssistantUploadAttachmentResponse>): Promise<AssistantChunkContentAttachment | null>;
11
+ export declare function writeUploadFrame<Frame>(stream: UploadWriter<Frame>, frame: Frame): Promise<void>;
12
+ export {};
@@ -0,0 +1,36 @@
1
+ export async function readUploadAttachment(frames) {
2
+ let attachment = null;
3
+ for await (const frame of frames) {
4
+ if (frame.attachment) {
5
+ attachment = frame.attachment;
6
+ }
7
+ }
8
+ return attachment;
9
+ }
10
+ export async function writeUploadFrame(stream, frame) {
11
+ if (stream.destroyed) {
12
+ throw new Error('attachment upload closed while writing');
13
+ }
14
+ if (stream.write(frame)) {
15
+ return;
16
+ }
17
+ await new Promise((resolve, reject) => {
18
+ const cleanup = () => {
19
+ stream.off('drain', drained);
20
+ stream.off('error', failed);
21
+ stream.off('close', closed);
22
+ };
23
+ const drained = () => {
24
+ cleanup();
25
+ resolve();
26
+ };
27
+ const failed = (error) => {
28
+ cleanup();
29
+ reject(error);
30
+ };
31
+ const closed = () => failed(new Error('attachment upload closed while writing'));
32
+ stream.on('drain', drained);
33
+ stream.on('error', failed);
34
+ stream.on('close', closed);
35
+ });
36
+ }
@@ -18,6 +18,7 @@ export interface AssistantClientOptions extends ClientOptions {
18
18
  }
19
19
  export declare class AssistantClient extends Client {
20
20
  private readonly _inline;
21
+ private readonly _sidecar;
21
22
  constructor({ sidecar: spawnSidecar, storagePath, device, meshSeed, meshKey, bootstrapNodes, blindPeerKeys, entry, ...engineOptions }: AssistantClientOptions);
22
23
  get core(): Core | null;
23
24
  _close(): Promise<void>;
@@ -4,8 +4,10 @@ import b4a from 'b4a';
4
4
  import { CoreError } from './errors.js';
5
5
  export class AssistantClient extends Client {
6
6
  _inline;
7
+ _sidecar;
7
8
  constructor({ sidecar: spawnSidecar = true, storagePath, device, meshSeed, meshKey, bootstrapNodes, blindPeerKeys, entry, ...engineOptions }) {
8
9
  const inline = { core: null };
10
+ const sidecar = { stop: null };
9
11
  if (spawnSidecar && !entry) {
10
12
  throw new CoreError('SIDECAR_ENTRY_MISSING');
11
13
  }
@@ -21,25 +23,11 @@ export class AssistantClient extends Client {
21
23
  (blindPeerKeys ?? []).map((key) => b4a.toHex(key)).join(',')
22
24
  ];
23
25
  const transport = sidecarEntry
24
- ? async () => {
25
- const { default: sidecarRunner } = await import('bare-supervisor/runner/sidecar');
26
- const logger = engineOptions.logger ?? null;
27
- const { ipc, exit } = sidecarRunner(sidecarEntry, args, logger);
28
- let firstError = null;
29
- ipc.on('error', (err) => {
30
- firstError ??= err;
31
- logger?.warn('assistant', 'the sidecar wire failed', err);
32
- });
33
- const died = exit.then((code) => sidecarDeath(code, firstError));
34
- const failure = await Promise.race([
35
- ipc.ready.then(() => null, () => died),
36
- died
37
- ]);
38
- if (failure) {
39
- throw failure;
40
- }
41
- return ipc;
42
- }
26
+ ? () => openSidecarWire(sidecarEntry, {
27
+ args,
28
+ logger: engineOptions.logger ?? null,
29
+ handle: sidecar
30
+ })
43
31
  : async () => {
44
32
  const [serverStream, clientStream] = duplexPair();
45
33
  const { Core } = await import('./core.js');
@@ -57,15 +45,48 @@ export class AssistantClient extends Client {
57
45
  };
58
46
  super(transport, engineOptions);
59
47
  this._inline = inline;
48
+ this._sidecar = sidecar;
60
49
  }
61
50
  get core() {
62
51
  return this._inline.core;
63
52
  }
64
53
  async _close() {
54
+ this._closeWatches();
55
+ await this._sidecar.stop?.();
65
56
  await super._close();
66
57
  await this._inline.core?.close();
67
58
  }
68
59
  }
60
+ async function openSidecarWire(entry, { args, logger, handle }) {
61
+ const { default: sidecarRunner } = await import('bare-supervisor/runner/sidecar');
62
+ const { ipc, exit } = sidecarRunner(entry, args, logger);
63
+ let firstError = null;
64
+ ipc.on('error', (err) => {
65
+ firstError ??= err;
66
+ logger?.warn('assistant', 'the sidecar wire failed', err);
67
+ });
68
+ const died = exit.then((code) => sidecarDeath(code, firstError));
69
+ const failure = await Promise.race([
70
+ ipc.ready.then(() => null, () => died),
71
+ died
72
+ ]);
73
+ if (failure) {
74
+ throw failure;
75
+ }
76
+ handle.stop = stopper(ipc, exit);
77
+ return ipc;
78
+ }
79
+ function stopper(ipc, exit) {
80
+ return async () => {
81
+ if (isHostIpc(ipc)) {
82
+ void ipc.terminate();
83
+ }
84
+ await exit;
85
+ };
86
+ }
87
+ function isHostIpc(ipc) {
88
+ return 'terminate' in ipc;
89
+ }
69
90
  function sidecarDeath(code, err) {
70
91
  if (!err) {
71
92
  return new CoreError('SIDECAR_EXITED', [code]);
@@ -4,6 +4,7 @@ import { Client } from './client.ts';
4
4
  import type { Core } from './core.ts';
5
5
  import type { IndexingEmbedder, RagExtractors } from './core/files/index.ts';
6
6
  import type { LoggerInterface } from '@qvac/logging';
7
+ import type { KnowledgeService } from '@qvac/harness/lib/contract/knowledge.ts';
7
8
  export interface HarnessContext {
8
9
  engine: Client;
9
10
  onDeath: (error?: Error) => void;
@@ -36,6 +37,7 @@ export interface AssistantTreeOptions {
36
37
  fakeModels?: boolean;
37
38
  }
38
39
  export declare function assistantTree(opts: AssistantTreeOptions): Supervisor;
40
+ export declare function harnessKnowledge(harness: Harness, model: string, logger: LoggerInterface | null): Promise<KnowledgeService | null>;
39
41
  export interface AssistantChildOptions {
40
42
  harness: HarnessFactory | ChildSpec<Harness>;
41
43
  core?: string;
@@ -6,6 +6,7 @@ import { startCustomModelPusher } from './custom-model-pusher.js';
6
6
  import { startModelIdlePusher } from './model-idle-pusher.js';
7
7
  import { startModelMemoryCap } from './model-memory-cap.js';
8
8
  import { startDownloadsAllowedPusher } from './downloads-allowed-pusher.js';
9
+ import { pdfPages } from '@qvac/harness/pdf-pages';
9
10
  import { duplexPair } from '@qvac/harness/transport';
10
11
  export function assistantTree(opts) {
11
12
  const { core, harness, logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, kvCacheChatsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, embedder, extractors, onHarness, platform, preloadCurrentAgentModel, fakeModels = false } = opts;
@@ -23,6 +24,7 @@ export function assistantTree(opts) {
23
24
  let offDeleteChatKvCache = null;
24
25
  let offCompactChat = null;
25
26
  let offRetryModelDownload = null;
27
+ let offSiblingDownloads = null;
26
28
  let offModelResidency = null;
27
29
  let offModelUsers = null;
28
30
  let offSkills = null;
@@ -118,6 +120,10 @@ export function assistantTree(opts) {
118
120
  logger
119
121
  });
120
122
  offRetryModelDownload = core.registerRetryModelDownload((name) => assistant.retryModelDownload(name));
123
+ offSiblingDownloads = core.registerSiblingDownloads({
124
+ asked: (name) => assistant.retryModelDownload(name, { sibling: true }),
125
+ resumed: (name) => assistant.siblingResumedDownload(name)
126
+ });
121
127
  offModelResidency = core.registerModelResidency({
122
128
  load: (name) => assistant.loadModel(name),
123
129
  unload: (name) => assistant.unloadModel(name)
@@ -165,7 +171,9 @@ export function assistantTree(opts) {
165
171
  },
166
172
  idle: (signal) => assistant.idle(signal)
167
173
  };
168
- offEmbedder = core.registerEmbedder(wrapped, extractors);
174
+ const knowledge = await harnessKnowledge(harness, emb.model, logger);
175
+ const pages = knowledge ? { ...extractors, pdfPages: harnessPdfPages(harness) } : extractors;
176
+ offEmbedder = core.registerEmbedder(wrapped, pages, { knowledge });
169
177
  }
170
178
  const off = onHarness?.(supervised, { engine });
171
179
  if (typeof off === 'function') {
@@ -204,6 +212,8 @@ export function assistantTree(opts) {
204
212
  offFakeModels = null;
205
213
  offRetryModelDownload?.();
206
214
  offRetryModelDownload = null;
215
+ offSiblingDownloads?.();
216
+ offSiblingDownloads = null;
207
217
  offModelResidency?.();
208
218
  offModelResidency = null;
209
219
  offModelUsers?.();
@@ -239,6 +249,43 @@ export function assistantTree(opts) {
239
249
  });
240
250
  return sup;
241
251
  }
252
+ export async function harnessKnowledge(harness, model, logger) {
253
+ const { knowledgeUpsert, knowledgeRemove, knowledgeSearch, knowledgeStatus } = harness;
254
+ if (!knowledgeUpsert || !knowledgeRemove || !knowledgeSearch || !knowledgeStatus) {
255
+ return null;
256
+ }
257
+ try {
258
+ const status = await knowledgeStatus.call(harness);
259
+ if (status.model !== model) {
260
+ logger?.warn('assistant-tree', 'the knowledge plugin embeds with another model', {
261
+ plugin: status.model,
262
+ model
263
+ });
264
+ return null;
265
+ }
266
+ }
267
+ catch {
268
+ return null;
269
+ }
270
+ return {
271
+ upsert: (artifact) => knowledgeUpsert.call(harness, artifact),
272
+ remove: (artifactId) => knowledgeRemove.call(harness, artifactId),
273
+ search: (query) => knowledgeSearch.call(harness, query),
274
+ status: () => knowledgeStatus.call(harness)
275
+ };
276
+ }
277
+ function harnessPdfPages(harness) {
278
+ const { ocrRead } = harness;
279
+ return (bytes, { signal, beforeOcr }) => {
280
+ const ocr = ocrRead && {
281
+ read: async (image, pageSignal) => {
282
+ await beforeOcr?.();
283
+ return ocrRead.call(harness, { image, signal: pageSignal });
284
+ }
285
+ };
286
+ return pdfPages(bytes, { ocr, signal });
287
+ };
288
+ }
242
289
  export function assistantChild(opts) {
243
290
  const { harness, core = 'core', logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, kvCacheChatsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, embedder, extractors, onHarness, preloadCurrentAgentModel, fakeModels, ...spec } = opts;
244
291
  return {
@@ -8,6 +8,8 @@ import type { DriveHost } from './core/fs-drives.ts';
8
8
  import { type DiagnosticsSlice } from './diagnostics.ts';
9
9
  import { type InferenceCallPage } from './inference-calls.ts';
10
10
  import type { LoggerInterface } from '@qvac/logging';
11
+ import { type RetryOptions } from './model-download.ts';
12
+ import { searchKnowledge } from './knowledge-search.ts';
11
13
  import { type DeleteModelSummary, type Harness, type ListModelsOptions, type ModelInfo, type ToolCatalogEntry } from './assistant/harness.ts';
12
14
  import type { Skill } from './assistant/skill-encode.ts';
13
15
  import { type CompactOptions, type CompactResult } from './assistant/history.ts';
@@ -38,6 +40,7 @@ export interface AssistantOptions {
38
40
  modelReloadDelayMs?: number;
39
41
  pdfRasterizer?: PdfRasterizer;
40
42
  }
43
+ type KnowledgeSearch = Awaited<ReturnType<typeof searchKnowledge>>;
41
44
  export interface SkillListOptions extends SkillOptions {
42
45
  readonly skipCredentialProbe?: boolean;
43
46
  }
@@ -70,6 +73,7 @@ export declare class QvacAssistant extends ReadyResource {
70
73
  private _listed;
71
74
  private _accountHosted;
72
75
  private _readySkills;
76
+ private _skillsHere;
73
77
  private _recordingReadiness;
74
78
  private _readinessDirty;
75
79
  private _reconciling;
@@ -96,6 +100,7 @@ export declare class QvacAssistant extends ReadyResource {
96
100
  private readonly _idleWaiters;
97
101
  private readonly _seen;
98
102
  private readonly _parked;
103
+ private readonly _retakes;
99
104
  private readonly _handedOff;
100
105
  private readonly _backgroundReplays;
101
106
  private readonly _inferenceCalls;
@@ -115,6 +120,7 @@ export declare class QvacAssistant extends ReadyResource {
115
120
  private readonly _runImageCacheNames;
116
121
  private readonly _reconciledCompactions;
117
122
  private readonly _knowledgeCaps;
123
+ private readonly _excerptsOnly;
118
124
  private readonly _modelCatalog;
119
125
  private readonly _onHarnessStatus;
120
126
  private readonly _onModelCatalogEvent;
@@ -157,6 +163,7 @@ export declare class QvacAssistant extends ReadyResource {
157
163
  deleteModel(name: string): Promise<DeleteModelSummary>;
158
164
  modelCatalogWatch(): AsyncIterable<AssistantModelCatalogRevision>;
159
165
  loadModel(name: string): Promise<AssistantLoadModelResponse>;
166
+ private _servedLoad;
160
167
  private _isCached;
161
168
  private _scheduleModelReload;
162
169
  private _reloadLoadedModel;
@@ -168,12 +175,17 @@ export declare class QvacAssistant extends ReadyResource {
168
175
  private _unloadModel;
169
176
  modelUsers(name: string): Promise<AssistantModelUsers>;
170
177
  private _meshModelUsers;
178
+ private _generationReady;
171
179
  private _runModel;
172
180
  private _runnableHost;
173
181
  notifyModelCatalog(): void;
174
182
  cachedModelsChanged(): void;
175
- retryModelDownload(name: string): Promise<void>;
183
+ retryModelDownload(name: string, options?: RetryOptions): Promise<void>;
184
+ siblingResumedDownload(name: string): void;
176
185
  skills(agentId?: string | null, modelName?: string | null, options?: SkillListOptions): Promise<ReadonlyArray<Skill>>;
186
+ private _skillsNotHere;
187
+ private _skillsOn;
188
+ private _readSkillsHere;
177
189
  private _recordSkillReadiness;
178
190
  private _readySkillNames;
179
191
  tools(): Promise<ReadonlyArray<ToolCatalogEntry>>;
@@ -215,11 +227,13 @@ export declare class QvacAssistant extends ReadyResource {
215
227
  private _claim;
216
228
  private _offerSteer;
217
229
  private _requeue;
230
+ private _retakeOnSync;
218
231
  private _citeWebSources;
219
232
  private _searchKnowledge;
220
233
  private _injectKnowledge;
221
234
  private _writeUnreadNotice;
222
235
  private _openRunLog;
236
+ private _callsOf;
223
237
  private _isHandled;
224
238
  private _execute;
225
239
  private _invokeCapability;
@@ -257,6 +271,8 @@ export declare class QvacAssistant extends ReadyResource {
257
271
  private _customModel;
258
272
  private _tryHydratePdf;
259
273
  private _hydratePdf;
274
+ private _replayPdfShots;
275
+ private _renderStoredShots;
260
276
  private _storedPdfSelection;
261
277
  private _renderPdfShots;
262
278
  private _storePdfSelections;
@@ -287,3 +303,4 @@ export declare class QvacAssistant extends ReadyResource {
287
303
  private _modelHolder;
288
304
  private _watchModelProgress;
289
305
  }
306
+ export declare function notReadyLine(search: KnowledgeSearch): string | null;