@qvac/core 1.0.0-rc.8 → 1.0.1

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 +243 -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
@@ -0,0 +1,2 @@
1
+ import type { Mesh } from '../mesh.ts';
2
+ export declare function backfillWriterLinks(mesh: Mesh): Promise<number>;
@@ -0,0 +1,32 @@
1
+ import { LIVE_WRITERS_BY_MOST_RECENT } from '../collections.js';
2
+ import { linkedTo, liveDeviceForWriter } from '../contract/activity.js';
3
+ const PAGE = 64;
4
+ export async function backfillWriterLinks(mesh) {
5
+ const due = await unmarkedLinkedWriters(mesh.view);
6
+ let sent = 0;
7
+ for (let i = 0; i < due.length; i += PAGE) {
8
+ if (mesh.closing || !mesh.writable)
9
+ break;
10
+ const writerKeys = due.slice(i, i + PAGE);
11
+ await mesh.dispatch('reindex-writers', { writerKeys });
12
+ sent += writerKeys.length;
13
+ }
14
+ return sent;
15
+ }
16
+ async function unmarkedLinkedWriters(db) {
17
+ const snap = db.snapshot();
18
+ try {
19
+ const due = [];
20
+ for await (const row of snap.find(LIVE_WRITERS_BY_MOST_RECENT, {
21
+ reverse: true
22
+ })) {
23
+ if (!row.linked && linkedTo(await liveDeviceForWriter(snap, row.writerKey))) {
24
+ due.push(row.writerKey);
25
+ }
26
+ }
27
+ return due;
28
+ }
29
+ finally {
30
+ await snap.close();
31
+ }
32
+ }
@@ -1,5 +1,6 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { QvacAgent, QvacProject, QvacProjectDeviceKey, QvacProjectHostKey, QvacProjectKey } from '../../spec/mesh/hyperschema/types.d.ts';
3
+ import type { MeshBatch } from './mesh.ts';
3
4
  export interface ProjectSeed {
4
5
  readonly projectId: Buffer;
5
6
  readonly seed: Buffer;
@@ -14,3 +15,8 @@ export declare function seedOpens(project: QvacProject, seed: Buffer): boolean;
14
15
  export declare function dueProjectKey(view: HyperDB, memberId: Buffer, { projectId, seed, sealedBy }: ProjectSeed): Promise<QvacProjectKey | null>;
15
16
  export declare function dueProjectHostKey(view: HyperDB, agent: QvacAgent, seal: ProjectHostSeed): Promise<QvacProjectHostKey | null>;
16
17
  export declare function dueProjectDeviceKeys(view: HyperDB, row: QvacProject, { seed, sealedBy }: Omit<ProjectSeed, 'projectId'>): Promise<QvacProjectDeviceKey[]>;
18
+ export interface MemberSealing extends Omit<ProjectSeed, 'projectId'> {
19
+ readonly view: HyperDB;
20
+ readonly row: QvacProject;
21
+ }
22
+ export declare function sealMembers(batch: MeshBatch, { view, row, seed, sealedBy }: MemberSealing): Promise<void>;
@@ -63,3 +63,14 @@ export async function dueProjectDeviceKeys(view, row, { seed, sealedBy }) {
63
63
  }
64
64
  return due;
65
65
  }
66
+ export async function sealMembers(batch, { view, row, seed, sealedBy }) {
67
+ for (const memberId of row.memberIds ?? []) {
68
+ const due = await dueProjectKey(view, memberId, { projectId: row.id, seed, sealedBy });
69
+ if (due) {
70
+ batch.dispatch('put-project-key', due);
71
+ }
72
+ }
73
+ for (const due of await dueProjectDeviceKeys(view, row, { seed, sealedBy })) {
74
+ batch.dispatch('put-project-device-key', due);
75
+ }
76
+ }
@@ -2,6 +2,7 @@ import type { QvacChat, QvacJoinedMesh } from '../../spec/mesh/hyperschema/types
2
2
  import type { Mesh } from './mesh.ts';
3
3
  export declare function publicSeed(accountSeed: Buffer, workspaceKey: Buffer): Buffer;
4
4
  export declare function ourRows(account: Mesh): Promise<QvacJoinedMesh[]>;
5
+ export declare function firstWorkspace(account: Mesh): Promise<string | null>;
5
6
  export interface OldChats {
6
7
  readonly mark: string;
7
8
  readonly messages: boolean;
@@ -17,7 +18,9 @@ export interface PublicRecord {
17
18
  readonly key: string | null;
18
19
  readonly writerSeed: string;
19
20
  readonly migrated: boolean;
21
+ readonly copied?: string;
20
22
  }
23
+ export declare function publicRecordsOf(accountKey: Buffer): string;
21
24
  export declare function publicRecordKey(accountKey: Buffer, workspaceKey: Buffer): string;
22
25
  export declare function leftPublicRecordKey(accountKey: Buffer, workspaceKey: Buffer): string;
23
26
  export declare function isPublicRecord(value: unknown): value is PublicRecord;
@@ -11,6 +11,11 @@ export async function ourRows(account) {
11
11
  const rows = await account.view.find(JOINED_MESHES).toArray();
12
12
  return rows.filter((row) => ours && b4a.equals(row.memberId, ours));
13
13
  }
14
+ export async function firstWorkspace(account) {
15
+ const live = (await ourRows(account)).filter((row) => row.workspace && !row.leftAt);
16
+ const [first] = live.sort((a, b) => a.joinedAt - b.joinedAt);
17
+ return first ? b4a.toHex(first.meshKey) : null;
18
+ }
14
19
  export async function oldChats(account) {
15
20
  let chats = 0;
16
21
  let chunks = 0;
@@ -30,14 +35,8 @@ export function derivedSeed(account) {
30
35
  }
31
36
  export async function publicMeshKey(account, workspaceKey) {
32
37
  const id = b4a.toHex(workspaceKey);
33
- const snap = account.view.snapshot();
34
- try {
35
- const row = await snap.get(PUBLIC_MESHES, { id });
36
- return row?.meshKey ?? null;
37
- }
38
- finally {
39
- await snap.close();
40
- }
38
+ const row = await account.view.get(PUBLIC_MESHES, { id });
39
+ return row?.meshKey ?? null;
41
40
  }
42
41
  function serving(agent, deviceId) {
43
42
  return !!agent && !agent.deletedAt && b4a.equals(agent.providerDeviceId, deviceId);
@@ -67,6 +66,9 @@ export async function servedChats(mesh, deviceId) {
67
66
  }
68
67
  return chats;
69
68
  }
69
+ export function publicRecordsOf(accountKey) {
70
+ return `public-mesh/${b4a.toHex(accountKey)}/`;
71
+ }
70
72
  export function publicRecordKey(accountKey, workspaceKey) {
71
73
  return `public-mesh/${b4a.toHex(accountKey)}/${b4a.toHex(workspaceKey)}`;
72
74
  }
@@ -7,5 +7,8 @@ export interface RunnerChoice {
7
7
  }
8
8
  export declare function pickRunner(mesh: Mesh, { deviceId, peers, agent }: RunnerChoice): Promise<Buffer | undefined>;
9
9
  export declare function takeRunRequest(store: Mesh, id: string): Promise<QvacChunk>;
10
+ export declare function continueRunHere(store: Mesh, id: string, { offline }?: {
11
+ offline?: boolean | undefined;
12
+ }): Promise<QvacChunk>;
10
13
  export declare function handedOffRequests(store: Mesh, deviceId: Buffer, chatId: string): Promise<QvacChunk[]>;
11
14
  export declare function takeBack(requests: ReadonlyArray<QvacChunk>, into: MeshDispatcher): void;
@@ -3,7 +3,7 @@ import { isMobilePlatform } from '@qvac/harness/lib/models/model-catalog.ts';
3
3
  import { CAPABILITY_PROFILES } from '../../schema/qvac.js';
4
4
  import { CoreError } from '../errors.js';
5
5
  import { getChunk, mustGetChunk } from './chunk.js';
6
- import { openToTake, runHasTerminalStatus, runTaker } from './contract/run-requests.js';
6
+ import { openToTake, runHasTerminalStatus, runMover, runTaker } from './contract/run-requests.js';
7
7
  import { liveDevicesOf } from './contract/activity.js';
8
8
  import { DEVICES, RUN_REQUESTS } from './collections.js';
9
9
  const rank = (device) => CAPABILITY_PROFILES.indexOf(device.capabilityProfile);
@@ -40,6 +40,26 @@ export async function takeRunRequest(store, id) {
40
40
  await store.dispatch('take-run-request', { id, at: Date.now() });
41
41
  return mustGetChunk(store, id);
42
42
  }
43
+ export async function continueRunHere(store, id, { offline = false } = {}) {
44
+ const request = await getChunk(store, id);
45
+ if (request?.type !== 'run-request' || !request.runId) {
46
+ throw new CoreError('CHUNK_NOT_FOUND', [id]);
47
+ }
48
+ const ours = store.ourDeviceId && b4a.equals(store.ourDeviceId, request.originDeviceId);
49
+ if (!ours) {
50
+ throw new CoreError('RUN_REQUEST_NOT_OURS', [id]);
51
+ }
52
+ const { chatId, runId } = request;
53
+ const movable = await runMover(store.view, request, store.localWriterKey);
54
+ if (!movable || (await runHasTerminalStatus(store.view, chatId, runId))) {
55
+ throw new CoreError('RUN_REQUEST_NOT_MOVABLE', [id]);
56
+ }
57
+ if (offline) {
58
+ throw new CoreError('RUN_REQUEST_OFFLINE', [id]);
59
+ }
60
+ await store.dispatch('continue-run-here', { id, at: Date.now() });
61
+ return mustGetChunk(store, id);
62
+ }
43
63
  export async function handedOffRequests(store, deviceId, chatId) {
44
64
  const range = { gte: { chatId }, lte: { chatId } };
45
65
  const handed = [];
@@ -0,0 +1,33 @@
1
+ import type { AssistantSkillAvailability } from '../../spec/hyperschema/types.d.ts';
2
+ import type { QvacAgent } from '../../spec/mesh/hyperschema/types.d.ts';
3
+ import type { Mesh } from './mesh.ts';
4
+ export interface CatalogSkill {
5
+ readonly name: string;
6
+ readonly status: string;
7
+ }
8
+ export interface SkillAvailabilityInput {
9
+ readonly deviceId: Buffer;
10
+ readonly platform: string | null;
11
+ readonly peers: ReadonlyArray<string>;
12
+ readonly catalog: (() => Promise<ReadonlyArray<CatalogSkill>>) | null;
13
+ readonly account: Mesh | null;
14
+ }
15
+ export declare function loadableSkills(catalog: ReadonlyArray<CatalogSkill>): Set<string>;
16
+ export declare function skillsNotHere(enabled: ReadonlyArray<string>, catalog: ReadonlyArray<CatalogSkill>): string[];
17
+ interface CatalogResponse {
18
+ readonly skills?: ReadonlyArray<CatalogSkill> | null;
19
+ }
20
+ interface CatalogOptions {
21
+ readonly refresh?: boolean;
22
+ readonly skipCredentialProbe?: boolean;
23
+ }
24
+ type CatalogRead<R> = (agentId?: string | null, modelName?: string | null, options?: CatalogOptions) => Promise<R>;
25
+ export interface RememberedCatalog<R> {
26
+ readonly read: CatalogRead<R>;
27
+ readonly last: () => Promise<ReadonlyArray<CatalogSkill>>;
28
+ }
29
+ export declare function rememberCatalog<R extends CatalogResponse>(read: CatalogRead<R>): RememberedCatalog<R>;
30
+ export declare function enabledSkillsOf(agent: Pick<QvacAgent, 'skillsScope' | 'enabledSkills'>, here: Iterable<string>, provider: ReadonlySet<string> | null): string[];
31
+ export declare function chatSkillAvailability(mesh: Mesh, chatId: string, { deviceId, platform, peers, catalog, account }: SkillAvailabilityInput): Promise<AssistantSkillAvailability[]>;
32
+ export declare function skillsOn(account: Mesh | null, deviceId: Buffer): Promise<Set<string> | null>;
33
+ export {};
@@ -0,0 +1,67 @@
1
+ import b4a from 'b4a';
2
+ import { isMobilePlatform } from '@qvac/harness/lib/models/model-catalog.ts';
3
+ import { getAgent } from './agent.js';
4
+ import { mustGetActiveChat } from './chat.js';
5
+ import { DEVICES } from './collections.js';
6
+ import { handedOffRequests, pickRunner } from './run-requests.js';
7
+ import { runDefinition } from './shared-agents.js';
8
+ export function loadableSkills(catalog) {
9
+ return new Set(catalog.filter((skill) => skill.status !== 'unsupported').map((skill) => skill.name));
10
+ }
11
+ export function skillsNotHere(enabled, catalog) {
12
+ const here = loadableSkills(catalog);
13
+ return enabled.filter((name) => !here.has(name));
14
+ }
15
+ export function rememberCatalog(read) {
16
+ let last = null;
17
+ const remember = (pending) => {
18
+ last = pending.then((response) => response.skills ?? [], () => {
19
+ last = null;
20
+ return [];
21
+ });
22
+ return pending;
23
+ };
24
+ return {
25
+ read: (agentId, modelName, options) => remember(read(agentId, modelName, options)),
26
+ last: () => last ?? remember(read(null, null, { skipCredentialProbe: true })).then(skillsOf)
27
+ };
28
+ }
29
+ const skillsOf = (response) => response.skills ?? [];
30
+ export function enabledSkillsOf(agent, here, provider) {
31
+ if (agent.skillsScope !== 'all') {
32
+ return [...(agent.enabledSkills ?? [])];
33
+ }
34
+ return [...new Set([...(provider ?? []), ...here])];
35
+ }
36
+ export async function chatSkillAvailability(mesh, chatId, { deviceId, platform, peers, catalog, account }) {
37
+ const chat = await mustGetActiveChat(mesh, chatId);
38
+ const agent = await runDefinition(mesh, await getAgent(mesh, chat.agentId));
39
+ if (!agent || agent.deletedAt) {
40
+ return [];
41
+ }
42
+ const mobile = isMobilePlatform(platform ?? '');
43
+ const all = agent.skillsScope === 'all';
44
+ if (!catalog || (!all && (!mobile || !agent.enabledSkills?.length))) {
45
+ return (agent.enabledSkills ?? []).map((name) => ({ name, available: true }));
46
+ }
47
+ const here = await catalog();
48
+ const provider = all && mobile ? await skillsOn(account, agent.providerDeviceId) : null;
49
+ const enabled = enabledSkillsOf(agent, here.map((skill) => skill.name), provider);
50
+ if (!mobile) {
51
+ return enabled.map((name) => ({ name, available: true }));
52
+ }
53
+ const missing = new Set(skillsNotHere(enabled, here));
54
+ const handed = missing.size ? await handedOffRequests(mesh, deviceId, chatId) : [];
55
+ const stuck = handed.some((request) => !request.runnerDeviceId || !peers.includes(b4a.toHex(request.runnerDeviceId)));
56
+ const runner = missing.size && !stuck ? await pickRunner(mesh, { deviceId, peers, agent }) : undefined;
57
+ const there = runner ? await skillsOn(account, runner) : null;
58
+ const covered = (name) => !!runner && (!there || there.has(name));
59
+ return enabled.map((name) => missing.has(name) && !covered(name)
60
+ ? { name, available: false, reason: 'platform' }
61
+ : { name, available: true });
62
+ }
63
+ export async function skillsOn(account, deviceId) {
64
+ const row = await account?.view.get(DEVICES, { id: deviceId });
65
+ const names = row?.installedSkillNames ?? [];
66
+ return names.length ? new Set(names) : null;
67
+ }
@@ -8,7 +8,6 @@ export interface BackgroundSyncHost {
8
8
  emit(event: string, ...args: unknown[]): boolean;
9
9
  }
10
10
  export interface BackgroundSyncOptions {
11
- readonly parallelism?: number;
12
11
  readonly logger: LoggerInterface | null;
13
12
  readonly scope: ReadonlyArray<string>;
14
13
  }
@@ -1,3 +1,4 @@
1
+ const WALK_PARALLELISM = 16;
1
2
  export class BackgroundSync {
2
3
  _host;
3
4
  _opts;
@@ -58,7 +59,7 @@ export class BackgroundSync {
58
59
  const started = Date.now();
59
60
  logger?.info(scope, 'background sync started', { length: head?.length ?? 0 });
60
61
  try {
61
- await this._host.autobee.bee.download({ parallelism: this._opts.parallelism });
62
+ await this._host.autobee.bee.download({ parallelism: WALK_PARALLELISM });
62
63
  logger?.info(scope, 'background sync finished', {
63
64
  length: head?.length ?? 0,
64
65
  ms: Date.now() - started,
@@ -1,6 +1,6 @@
1
1
  import type { ViewChange } from 'hyperdb';
2
- import type { QvacAgent, QvacChat, QvacChunk, QvacChunkCounter, QvacDevice, QvacIndexingStatus, QvacKnowledge, QvacKnowledgeArtifact, QvacPurgedChat } from '../../../spec/mesh/hyperschema/types.d.ts';
3
- import { AGENTS, CHATS, CHUNKS, CHUNK_COUNTERS, DEVICES, INDEXING_STATUS, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, PURGED_CHATS } from '../collections.ts';
2
+ import type { QvacAgent, QvacChat, QvacChunk, QvacChunkCounter, QvacDevice, QvacIndexingStatus, QvacKnowledge, QvacKnowledgeArtifact, QvacMovedOutChat, QvacPurgedChat, QvacToolActivity } from '../../../spec/mesh/hyperschema/types.d.ts';
3
+ import { AGENTS, CHATS, CHUNKS, CHUNK_COUNTERS, DEVICES, INDEXING_STATUS, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, MOVED_OUT_CHATS, PURGED_CHATS, TOOL_ACTIVITY } from '../collections.ts';
4
4
  export interface ChangedRows {
5
5
  [AGENTS]: QvacAgent;
6
6
  [DEVICES]: QvacDevice;
@@ -10,7 +10,9 @@ export interface ChangedRows {
10
10
  [INDEXING_STATUS]: QvacIndexingStatus;
11
11
  [KNOWLEDGE]: QvacKnowledge;
12
12
  [KNOWLEDGE_ARTIFACTS]: QvacKnowledgeArtifact;
13
+ [MOVED_OUT_CHATS]: QvacMovedOutChat;
13
14
  [PURGED_CHATS]: QvacPurgedChat;
15
+ [TOOL_ACTIVITY]: QvacToolActivity;
14
16
  }
15
17
  export type ChangedRow<C extends keyof ChangedRows> = ViewChange & {
16
18
  readonly collection: C;
@@ -1,4 +1,4 @@
1
- import { AGENTS, CHATS, CHUNKS, CHUNK_COUNTERS, DEVICES, INDEXING_STATUS, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, PURGED_CHATS } from '../collections.js';
1
+ import { AGENTS, CHATS, CHUNKS, CHUNK_COUNTERS, DEVICES, INDEXING_STATUS, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, MOVED_OUT_CHATS, PURGED_CHATS, TOOL_ACTIVITY } from '../collections.js';
2
2
  export function changed(change, collection) {
3
3
  return change.collection === collection;
4
4
  }
@@ -3,7 +3,10 @@ import { FileChanges } from './files.js';
3
3
  import { syncDeviceRevocation, syncOwnMember } from './devices.js';
4
4
  import { syncChatTombstone } from './chats.js';
5
5
  import { syncNotificationChunk } from './chunks.js';
6
- import { AGENT_HOSTS, AGENT_USERS, AGENTS, CHATS, CHAT_GROUPS, CHUNKS, CHUNK_COUNTERS, DEVICES, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, MEMBER_KEYS, MEMBERS, MESH_NAMES, MESH_RULES, MESH_SEEDS, PROJECT_DEVICE_SEALS, PROJECT_HOST_SEALS, PROJECT_MEMBER_SEALS, PROJECTS, PUBLIC_MESHES, PUBLIC_SEEDS, PURGED_CHATS, SKILL_SECRETS, WORKSPACE } from '../collections.js';
6
+ import { isRecord } from '../../guards.js';
7
+ import { isMissingBlock } from '../view-read.js';
8
+ import { isPendingBlobReference } from '../files/store/index.js';
9
+ import { AGENT_HOSTS, AGENT_USERS, AGENTS, CHATS, CHAT_GROUPS, CHUNKS, CHUNK_COUNTERS, DEVICES, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, MEMBER_KEYS, MEMBERS, MESH_NAMES, MESH_RULES, MESH_SEEDS, MOVED_OUT_CHATS, PROJECT_DEVICE_SEALS, PROJECT_HOST_SEALS, PROJECT_MEMBER_SEALS, PROJECTS, PUBLIC_MESHES, PUBLIC_SEEDS, PURGED_CHATS, SKILL_SECRETS, TOOL_ACTIVITY, WORKSPACE } from '../collections.js';
7
10
  export async function syncChanges(mesh, view, from) {
8
11
  if (mesh.closing)
9
12
  return null;
@@ -15,9 +18,13 @@ export async function syncChanges(mesh, view, from) {
15
18
  await walkChanges(mesh, { snap, from, files, pass });
16
19
  }
17
20
  catch (err) {
18
- mesh.log?.warn('sync', 'change sync cut short', err);
21
+ if (unserved(err)) {
22
+ await snap.close();
23
+ throw err;
24
+ }
25
+ mesh.reportError(err);
19
26
  pass.membershipMoved = pass.agentsDirty = pass.meshesChanged = pass.nameChanged = true;
20
- pass.runs = null;
27
+ pass.runs = pass.edited = null;
21
28
  pass.groupsChanged = pass.secretsChanged = true;
22
29
  }
23
30
  try {
@@ -27,7 +34,7 @@ export async function syncChanges(mesh, view, from) {
27
34
  void offPath(mesh, files, pass)
28
35
  .catch((err) => {
29
36
  if (!mesh.closing) {
30
- mesh.log?.warn('sync', 'off-path sync failed', err);
37
+ mesh.reportError(err);
31
38
  }
32
39
  })
33
40
  .finally(() => snap.close());
@@ -57,6 +64,7 @@ function newPass() {
57
64
  knowledge: [],
58
65
  artifacts: [],
59
66
  orderDirty: new Set(),
67
+ edited: new Set(),
60
68
  purged: new Map(),
61
69
  notifications: [],
62
70
  runs: { requests: [], deletedAgents: new Set() },
@@ -79,15 +87,30 @@ function syncDevice(mesh, pass, device) {
79
87
  async function settle(mesh, pass) {
80
88
  if (mesh.closing)
81
89
  return;
90
+ await deriveFromPass(mesh, pass);
91
+ announcePass(mesh, pass);
92
+ }
93
+ async function deriveFromPass(mesh, pass) {
82
94
  if (pass.purged.size) {
83
95
  await mesh.fileIndex?.retireChats(pass.purged);
84
- for (const chatId of pass.purged.keys()) {
85
- mesh.emit('chat-purged', chatId);
86
- }
87
96
  }
88
97
  if (pass.attachments.length) {
89
98
  await mesh.fileIndex?.refresh(pass.attachments);
90
99
  }
100
+ if (pass.orderDirty.size && mesh.syncChatOrder) {
101
+ await mesh.syncChatOrder(pass.orderDirty);
102
+ }
103
+ if (pass.nameChanged) {
104
+ await mesh.refreshName();
105
+ }
106
+ if (pass.edited === null || pass.edited.size > 0) {
107
+ await mesh.history?.edited(pass.edited);
108
+ }
109
+ }
110
+ function announcePass(mesh, pass) {
111
+ for (const chatId of pass.purged.keys()) {
112
+ mesh.emit('chat-purged', chatId);
113
+ }
91
114
  if (!pass.rulesRaised) {
92
115
  for (const chunk of pass.notifications) {
93
116
  syncNotificationChunk(mesh, chunk);
@@ -103,18 +126,20 @@ async function settle(mesh, pass) {
103
126
  if (pass.agentsDirty) {
104
127
  mesh.emit('agents-dirty');
105
128
  }
129
+ if (pass.edited === null || pass.edited.size > 0) {
130
+ mesh.emit('chats-edited', pass.edited);
131
+ }
106
132
  if (!pass.runs || pass.runs.requests.length || pass.runs.deletedAgents.size) {
107
133
  mesh.emit('runs-landed', pass.runs);
108
134
  }
109
135
  if (pass.secretsChanged) {
110
136
  mesh.emit('skill-secrets-dirty');
111
137
  }
112
- if (pass.orderDirty.size && mesh.syncChatOrder) {
113
- await mesh.syncChatOrder(pass.orderDirty);
138
+ if (pass.attachments.some(({ attachment }) => isPendingBlobReference(attachment?.blobId ?? ''))) {
139
+ mesh.emit('files-pending');
114
140
  }
115
141
  mesh.emit('meshes-dirty', pass.meshesChanged, pass.groupsChanged, pass.orderDirty.size > 0);
116
142
  if (pass.nameChanged) {
117
- await mesh.refreshName();
118
143
  mesh.emit('mesh-named');
119
144
  }
120
145
  }
@@ -146,6 +171,7 @@ async function walkChanges(mesh, { snap, from, files, pass }) {
146
171
  }
147
172
  else if (changed(change, CHATS)) {
148
173
  pass.orderDirty.add(change.value.id);
174
+ edit(pass, { chatId: change.value.id });
149
175
  syncChatTombstone(mesh, change.value);
150
176
  }
151
177
  else if (changed(change, CHUNK_COUNTERS)) {
@@ -155,6 +181,7 @@ async function walkChanges(mesh, { snap, from, files, pass }) {
155
181
  pass.rulesRaised = true;
156
182
  }
157
183
  else if (changed(change, CHUNKS)) {
184
+ edit(pass, change.value);
158
185
  pass.notifications.push(change.value);
159
186
  if (change.value.type === 'run-request') {
160
187
  pass.runs?.requests.push(change.value);
@@ -164,11 +191,18 @@ async function walkChanges(mesh, { snap, from, files, pass }) {
164
191
  }
165
192
  }
166
193
  else if (changed(change, KNOWLEDGE)) {
194
+ edit(pass, change.value);
167
195
  pass.knowledge.push(change.value);
168
196
  }
169
197
  else if (changed(change, KNOWLEDGE_ARTIFACTS)) {
170
198
  pass.artifacts.push(change.value);
171
199
  }
200
+ else if (changed(change, TOOL_ACTIVITY)) {
201
+ edit(pass, change.value);
202
+ }
203
+ else if (changed(change, MOVED_OUT_CHATS)) {
204
+ edit(pass, change.value);
205
+ }
172
206
  else if (changed(change, PURGED_CHATS)) {
173
207
  pass.purged.set(change.value.chatId, change.value.at);
174
208
  }
@@ -186,6 +220,14 @@ async function walkChanges(mesh, { snap, from, files, pass }) {
186
220
  }
187
221
  }
188
222
  }
223
+ function edit(pass, { chatId }) {
224
+ if (chatId) {
225
+ pass.edited?.add(chatId);
226
+ }
227
+ }
228
+ function unserved(err) {
229
+ return isMissingBlock(err) || (isRecord(err) && err.code === 'SESSION_CLOSED');
230
+ }
189
231
  const HOSTING = new Set([
190
232
  AGENT_USERS,
191
233
  PUBLIC_SEEDS,
@@ -6,28 +6,34 @@ export interface ViewUpdate {
6
6
  }
7
7
  export interface PendingUpdates {
8
8
  read(): Promise<ViewUpdate | null>;
9
- write(update: ViewUpdate): Promise<void>;
10
- clear(): Promise<void>;
9
+ update(change: (row: ViewUpdate | null) => ViewUpdate | null): Promise<void>;
11
10
  }
12
11
  type Run = (update: ViewUpdate) => Promise<ViewHead | null | void>;
12
+ export interface ViewUpdatesOptions {
13
+ readonly pending: PendingUpdates;
14
+ readonly onerror: (err: unknown) => void;
15
+ }
13
16
  export declare class ViewUpdates {
14
17
  private readonly _run;
15
18
  private readonly _pending;
16
- private _next;
17
- private _running;
18
- private _kept;
19
+ private readonly _onerror;
20
+ private readonly _pushing;
21
+ private _waiting;
22
+ private _draining;
23
+ private _written;
24
+ private _bumped;
19
25
  private _started;
20
26
  private _closed;
21
- constructor(run: Run, pending?: PendingUpdates | null);
22
- start(): Promise<void>;
27
+ constructor(run: Run, { pending, onerror }: ViewUpdatesOptions);
28
+ start(): void;
23
29
  push(update: ViewUpdate): Promise<void>;
30
+ bump(): void;
24
31
  close(): void;
25
32
  flush(): Promise<void>;
26
- private _kick;
27
33
  private _drain;
28
- private _queue;
29
- private _keepNext;
30
- private _keep;
31
- private _clear;
34
+ private _passes;
35
+ private _release;
36
+ private _settle;
32
37
  }
38
+ export declare function memoryPending(): PendingUpdates;
33
39
  export {};