@qvac/core 1.0.0-rc.8 → 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 (226) hide show
  1. package/CHANGELOG.md +234 -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/devices.js +6 -18
  8. package/dist-lib/lib/api/execution.d.ts +6 -2
  9. package/dist-lib/lib/api/execution.js +29 -3
  10. package/dist-lib/lib/api/host-run-log.d.ts +7 -1
  11. package/dist-lib/lib/api/host-run-log.js +3 -1
  12. package/dist-lib/lib/api/index.d.ts +3 -1
  13. package/dist-lib/lib/api/index.js +17 -6
  14. package/dist-lib/lib/api/members.js +13 -29
  15. package/dist-lib/lib/api/mesh.d.ts +2 -1
  16. package/dist-lib/lib/api/projects.d.ts +1 -1
  17. package/dist-lib/lib/api/projects.js +9 -5
  18. package/dist-lib/lib/api/rag-label.d.ts +4 -0
  19. package/dist-lib/lib/api/rag-label.js +21 -0
  20. package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
  21. package/dist-lib/lib/api/rag-lexical.js +1058 -0
  22. package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
  23. package/dist-lib/lib/api/rag-manifest.js +46 -0
  24. package/dist-lib/lib/api/rag-outline.d.ts +36 -0
  25. package/dist-lib/lib/api/rag-outline.js +426 -0
  26. package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
  27. package/dist-lib/lib/api/rag-plugin.js +70 -0
  28. package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
  29. package/dist-lib/lib/api/rag-synthesis.js +102 -0
  30. package/dist-lib/lib/api/rag-targets.d.ts +23 -0
  31. package/dist-lib/lib/api/rag-targets.js +215 -0
  32. package/dist-lib/lib/api/rag-window.d.ts +5 -0
  33. package/dist-lib/lib/api/rag-window.js +46 -0
  34. package/dist-lib/lib/api/rag.d.ts +3 -2
  35. package/dist-lib/lib/api/rag.js +421 -181
  36. package/dist-lib/lib/api/served.js +8 -14
  37. package/dist-lib/lib/api/workspace.d.ts +8 -1
  38. package/dist-lib/lib/api/workspace.js +27 -13
  39. package/dist-lib/lib/assistant/harness.d.ts +9 -1
  40. package/dist-lib/lib/assistant/history.d.ts +11 -2
  41. package/dist-lib/lib/assistant/history.js +74 -4
  42. package/dist-lib/lib/assistant/media-round.d.ts +61 -0
  43. package/dist-lib/lib/assistant/media-round.js +111 -0
  44. package/dist-lib/lib/assistant/message.d.ts +7 -0
  45. package/dist-lib/lib/assistant/model-round.d.ts +12 -0
  46. package/dist-lib/lib/assistant/model-round.js +130 -17
  47. package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
  48. package/dist-lib/lib/assistant/pdf-pages.js +67 -3
  49. package/dist-lib/lib/assistant/round-host.d.ts +22 -0
  50. package/dist-lib/lib/assistant/round-host.js +142 -8
  51. package/dist-lib/lib/assistant/upload.d.ts +12 -0
  52. package/dist-lib/lib/assistant/upload.js +36 -0
  53. package/dist-lib/lib/assistant-client.d.ts +1 -0
  54. package/dist-lib/lib/assistant-client.js +40 -19
  55. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  56. package/dist-lib/lib/assistant-tree.js +41 -1
  57. package/dist-lib/lib/assistant.d.ts +15 -0
  58. package/dist-lib/lib/assistant.js +474 -89
  59. package/dist-lib/lib/client.d.ts +1 -0
  60. package/dist-lib/lib/client.js +4 -1
  61. package/dist-lib/lib/compaction.js +2 -2
  62. package/dist-lib/lib/core/agent.js +12 -45
  63. package/dist-lib/lib/core/approval-rules.js +1 -8
  64. package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
  65. package/dist-lib/lib/core/archived-mesh.js +12 -8
  66. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  67. package/dist-lib/lib/core/chat-copy.js +74 -25
  68. package/dist-lib/lib/core/chat-fork.js +15 -6
  69. package/dist-lib/lib/core/chat-group.js +16 -24
  70. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  71. package/dist-lib/lib/core/chat-move.js +94 -3
  72. package/dist-lib/lib/core/chat-order.js +12 -18
  73. package/dist-lib/lib/core/chat.js +38 -31
  74. package/dist-lib/lib/core/chunk.d.ts +2 -2
  75. package/dist-lib/lib/core/chunk.js +18 -25
  76. package/dist-lib/lib/core/collections.d.ts +2 -0
  77. package/dist-lib/lib/core/collections.js +2 -0
  78. package/dist-lib/lib/core/contract/activity.d.ts +4 -0
  79. package/dist-lib/lib/core/contract/activity.js +34 -4
  80. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  81. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  82. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  83. package/dist-lib/lib/core/contract/chat.js +127 -5
  84. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  85. package/dist-lib/lib/core/contract/chunks.js +37 -6
  86. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  87. package/dist-lib/lib/core/contract/features.js +22 -0
  88. package/dist-lib/lib/core/contract/index.js +2 -0
  89. package/dist-lib/lib/core/contract/indexing.js +4 -2
  90. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  91. package/dist-lib/lib/core/contract/members.js +16 -10
  92. package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
  93. package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
  94. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  95. package/dist-lib/lib/core/contract/projects.js +13 -5
  96. package/dist-lib/lib/core/contract/roles.js +32 -4
  97. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
  98. package/dist-lib/lib/core/contract/run-requests.js +35 -2
  99. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  100. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  101. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  102. package/dist-lib/lib/core/contract/workspace.js +40 -7
  103. package/dist-lib/lib/core/contract/writers.js +2 -2
  104. package/dist-lib/lib/core/departures.d.ts +3 -1
  105. package/dist-lib/lib/core/departures.js +3 -0
  106. package/dist-lib/lib/core/devices.js +1 -8
  107. package/dist-lib/lib/core/files/attachments.d.ts +2 -0
  108. package/dist-lib/lib/core/files/attachments.js +19 -1
  109. package/dist-lib/lib/core/files/index.d.ts +20 -4
  110. package/dist-lib/lib/core/files/index.js +132 -21
  111. package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
  112. package/dist-lib/lib/core/files/processing/document.js +7 -0
  113. package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
  114. package/dist-lib/lib/core/files/processing/index.js +144 -12
  115. package/dist-lib/lib/core/files/processing/protocol.js +13 -34
  116. package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
  117. package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
  118. package/dist-lib/lib/core/files/rag/build.js +123 -19
  119. package/dist-lib/lib/core/files/rag/failure.js +2 -1
  120. package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
  121. package/dist-lib/lib/core/files/rag/index.js +13 -1
  122. package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
  123. package/dist-lib/lib/core/files/rag/load.js +27 -6
  124. package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
  125. package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
  126. package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
  127. package/dist-lib/lib/core/files/rag/produce.js +104 -16
  128. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
  129. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
  130. package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
  131. package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
  132. package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
  133. package/dist-lib/lib/core/files/records/knowledge.js +7 -40
  134. package/dist-lib/lib/core/files/records/reclaim.js +5 -10
  135. package/dist-lib/lib/core/files/upload.js +68 -41
  136. package/dist-lib/lib/core/fs-drives.js +14 -26
  137. package/dist-lib/lib/core/invite.d.ts +1 -0
  138. package/dist-lib/lib/core/invite.js +4 -2
  139. package/dist-lib/lib/core/local/files.d.ts +1 -1
  140. package/dist-lib/lib/core/local/files.js +5 -5
  141. package/dist-lib/lib/core/local/index.d.ts +13 -6
  142. package/dist-lib/lib/core/local/index.js +103 -69
  143. package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
  144. package/dist-lib/lib/core/local/run-logs.js +5 -5
  145. package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
  146. package/dist-lib/lib/core/mesh-processor.js +87 -0
  147. package/dist-lib/lib/core/mesh.d.ts +48 -5
  148. package/dist-lib/lib/core/mesh.js +318 -129
  149. package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
  150. package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
  151. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
  152. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
  153. package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
  154. package/dist-lib/lib/core/migrations/writer-links.js +32 -0
  155. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  156. package/dist-lib/lib/core/project-keys.js +11 -0
  157. package/dist-lib/lib/core/public-mesh.d.ts +3 -0
  158. package/dist-lib/lib/core/public-mesh.js +10 -8
  159. package/dist-lib/lib/core/run-requests.d.ts +3 -0
  160. package/dist-lib/lib/core/run-requests.js +21 -1
  161. package/dist-lib/lib/core/skill-availability.d.ts +33 -0
  162. package/dist-lib/lib/core/skill-availability.js +67 -0
  163. package/dist-lib/lib/core/sync/background.d.ts +0 -1
  164. package/dist-lib/lib/core/sync/background.js +2 -1
  165. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  166. package/dist-lib/lib/core/sync/changed.js +1 -1
  167. package/dist-lib/lib/core/sync/index.js +52 -10
  168. package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
  169. package/dist-lib/lib/core/sync/view-updates.js +82 -91
  170. package/dist-lib/lib/core/sync-status.js +97 -41
  171. package/dist-lib/lib/core/tool-activity.js +6 -12
  172. package/dist-lib/lib/core/tool-configs.js +17 -29
  173. package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
  174. package/dist-lib/lib/core/upgrade-summary.js +67 -0
  175. package/dist-lib/lib/core/view-read.d.ts +4 -0
  176. package/dist-lib/lib/core/view-read.js +23 -0
  177. package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
  178. package/dist-lib/lib/core/workspace-upgrade.js +27 -16
  179. package/dist-lib/lib/core.d.ts +39 -42
  180. package/dist-lib/lib/core.js +661 -639
  181. package/dist-lib/lib/errors.d.ts +20 -0
  182. package/dist-lib/lib/errors.js +20 -0
  183. package/dist-lib/lib/llm-model-names.d.ts +1 -1
  184. package/dist-lib/lib/llm-model-names.js +1 -1
  185. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  186. package/dist-lib/lib/model-context.js +1 -2
  187. package/dist-lib/lib/probe-rounds.d.ts +7 -2
  188. package/dist-lib/lib/probe-rounds.js +6 -1
  189. package/dist-lib/lib/run-transcript.d.ts +4 -1
  190. package/dist-lib/lib/run-transcript.js +15 -6
  191. package/dist-lib/schema/engine.js +83 -6
  192. package/dist-lib/schema/indexes.d.ts +3 -0
  193. package/dist-lib/schema/indexes.js +5 -0
  194. package/dist-lib/schema/local.js +11 -2
  195. package/dist-lib/schema/qvac.js +49 -5
  196. package/dist-lib/spec/bind.js +34 -0
  197. package/dist-lib/spec/calls.js +5 -0
  198. package/dist-lib/spec/capabilities.d.ts +17 -0
  199. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  200. package/dist-lib/spec/hrpc/index.js +68 -5
  201. package/dist-lib/spec/hrpc/messages.js +260 -23
  202. package/dist-lib/spec/hyperschema/index.js +260 -23
  203. package/dist-lib/spec/hyperschema/schema.json +181 -0
  204. package/dist-lib/spec/hyperschema/types.d.ts +48 -0
  205. package/dist-lib/spec/local/hyperdb/messages.js +270 -224
  206. package/dist-lib/spec/local/hyperschema/index.js +73 -38
  207. package/dist-lib/spec/local/hyperschema/schema.json +31 -0
  208. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
  209. package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
  210. package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
  211. package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
  212. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
  213. package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
  214. package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
  215. package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
  216. package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
  217. package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
  218. package/package.json +75 -70
  219. package/tui/README.md +2 -1
  220. package/tui/app.mjs +28 -7
  221. package/tui/boot.mjs +5 -1
  222. package/tui/lib/attachment-files.mjs +82 -0
  223. package/tui/lib/reopen-meshes.mjs +15 -0
  224. package/tui/main.mjs +4 -43
  225. package/worker/assistant-sidecar.mjs +10 -3
  226. package/worker/stow-entry.mjs +3 -52
@@ -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;
@@ -170,7 +171,9 @@ export function assistantTree(opts) {
170
171
  },
171
172
  idle: (signal) => assistant.idle(signal)
172
173
  };
173
- 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 });
174
177
  }
175
178
  const off = onHarness?.(supervised, { engine });
176
179
  if (typeof off === 'function') {
@@ -246,6 +249,43 @@ export function assistantTree(opts) {
246
249
  });
247
250
  return sup;
248
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
+ }
249
289
  export function assistantChild(opts) {
250
290
  const { harness, core = 'core', logger = null, imageCacheDir, inferenceCallsDir, runReplayDir, kvCacheChatsDir, imageCacheMaxBytes, imageCacheMaxAgeMs, kvCacheRetentionMs, embedder, extractors, onHarness, preloadCurrentAgentModel, fakeModels, ...spec } = opts;
251
291
  return {
@@ -9,6 +9,7 @@ import { type DiagnosticsSlice } from './diagnostics.ts';
9
9
  import { type InferenceCallPage } from './inference-calls.ts';
10
10
  import type { LoggerInterface } from '@qvac/logging';
11
11
  import { type RetryOptions } from './model-download.ts';
12
+ import { searchKnowledge } from './knowledge-search.ts';
12
13
  import { type DeleteModelSummary, type Harness, type ListModelsOptions, type ModelInfo, type ToolCatalogEntry } from './assistant/harness.ts';
13
14
  import type { Skill } from './assistant/skill-encode.ts';
14
15
  import { type CompactOptions, type CompactResult } from './assistant/history.ts';
@@ -39,6 +40,7 @@ export interface AssistantOptions {
39
40
  modelReloadDelayMs?: number;
40
41
  pdfRasterizer?: PdfRasterizer;
41
42
  }
43
+ type KnowledgeSearch = Awaited<ReturnType<typeof searchKnowledge>>;
42
44
  export interface SkillListOptions extends SkillOptions {
43
45
  readonly skipCredentialProbe?: boolean;
44
46
  }
@@ -71,6 +73,7 @@ export declare class QvacAssistant extends ReadyResource {
71
73
  private _listed;
72
74
  private _accountHosted;
73
75
  private _readySkills;
76
+ private _skillsHere;
74
77
  private _recordingReadiness;
75
78
  private _readinessDirty;
76
79
  private _reconciling;
@@ -97,6 +100,7 @@ export declare class QvacAssistant extends ReadyResource {
97
100
  private readonly _idleWaiters;
98
101
  private readonly _seen;
99
102
  private readonly _parked;
103
+ private readonly _retakes;
100
104
  private readonly _handedOff;
101
105
  private readonly _backgroundReplays;
102
106
  private readonly _inferenceCalls;
@@ -116,6 +120,7 @@ export declare class QvacAssistant extends ReadyResource {
116
120
  private readonly _runImageCacheNames;
117
121
  private readonly _reconciledCompactions;
118
122
  private readonly _knowledgeCaps;
123
+ private readonly _excerptsOnly;
119
124
  private readonly _modelCatalog;
120
125
  private readonly _onHarnessStatus;
121
126
  private readonly _onModelCatalogEvent;
@@ -158,6 +163,7 @@ export declare class QvacAssistant extends ReadyResource {
158
163
  deleteModel(name: string): Promise<DeleteModelSummary>;
159
164
  modelCatalogWatch(): AsyncIterable<AssistantModelCatalogRevision>;
160
165
  loadModel(name: string): Promise<AssistantLoadModelResponse>;
166
+ private _servedLoad;
161
167
  private _isCached;
162
168
  private _scheduleModelReload;
163
169
  private _reloadLoadedModel;
@@ -169,6 +175,7 @@ export declare class QvacAssistant extends ReadyResource {
169
175
  private _unloadModel;
170
176
  modelUsers(name: string): Promise<AssistantModelUsers>;
171
177
  private _meshModelUsers;
178
+ private _generationReady;
172
179
  private _runModel;
173
180
  private _runnableHost;
174
181
  notifyModelCatalog(): void;
@@ -176,6 +183,9 @@ export declare class QvacAssistant extends ReadyResource {
176
183
  retryModelDownload(name: string, options?: RetryOptions): Promise<void>;
177
184
  siblingResumedDownload(name: string): void;
178
185
  skills(agentId?: string | null, modelName?: string | null, options?: SkillListOptions): Promise<ReadonlyArray<Skill>>;
186
+ private _skillsNotHere;
187
+ private _skillsOn;
188
+ private _readSkillsHere;
179
189
  private _recordSkillReadiness;
180
190
  private _readySkillNames;
181
191
  tools(): Promise<ReadonlyArray<ToolCatalogEntry>>;
@@ -217,11 +227,13 @@ export declare class QvacAssistant extends ReadyResource {
217
227
  private _claim;
218
228
  private _offerSteer;
219
229
  private _requeue;
230
+ private _retakeOnSync;
220
231
  private _citeWebSources;
221
232
  private _searchKnowledge;
222
233
  private _injectKnowledge;
223
234
  private _writeUnreadNotice;
224
235
  private _openRunLog;
236
+ private _callsOf;
225
237
  private _isHandled;
226
238
  private _execute;
227
239
  private _invokeCapability;
@@ -259,6 +271,8 @@ export declare class QvacAssistant extends ReadyResource {
259
271
  private _customModel;
260
272
  private _tryHydratePdf;
261
273
  private _hydratePdf;
274
+ private _replayPdfShots;
275
+ private _renderStoredShots;
262
276
  private _storedPdfSelection;
263
277
  private _renderPdfShots;
264
278
  private _storePdfSelections;
@@ -289,3 +303,4 @@ export declare class QvacAssistant extends ReadyResource {
289
303
  private _modelHolder;
290
304
  private _watchModelProgress;
291
305
  }
306
+ export declare function notReadyLine(search: KnowledgeSearch): string | null;