@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
@@ -21,5 +21,6 @@ export declare class Client extends ReadyResource {
21
21
  constructor(transport: Transport, { logger, spec, handlers }?: ClientOptions);
22
22
  _open(): Promise<void>;
23
23
  private _reconnecting;
24
+ protected _closeWatches(): void;
24
25
  _close(): Promise<void>;
25
26
  }
@@ -51,11 +51,14 @@ export class Client extends ReadyResource {
51
51
  return watch;
52
52
  };
53
53
  }
54
- async _close() {
54
+ _closeWatches() {
55
55
  for (const watch of this._watches) {
56
56
  watch.destroy();
57
57
  }
58
58
  this._watches.clear();
59
+ }
60
+ async _close() {
61
+ this._closeWatches();
59
62
  const stream = this._stream;
60
63
  if (stream && !stream.destroyed) {
61
64
  const closed = new Promise((resolve) => stream.on('close', resolve));
@@ -1,4 +1,4 @@
1
- import { CONTEXT_OVERFLOW_MESSAGE } from '@qvac/harness';
1
+ import { HarnessError } from '@qvac/harness/errors';
2
2
  import { COMPACTION_MIN_CTX_SIZE } from './model-context.js';
3
3
  export const COMPACTION_KEEP_TURNS = 1;
4
4
  export const COMPACTION_TURN_PREFIX = '[Summary of the earlier conversation in this chat, written by another model. Treat it as established context, not as a new request.]';
@@ -114,5 +114,5 @@ export function nearContextLimit(cacheTokens, ctxSize) {
114
114
  return cacheTokens >= ctxSize * compactionTriggerRatio(ctxSize);
115
115
  }
116
116
  export function isContextOverflowFailure(message) {
117
- return message === CONTEXT_OVERFLOW_MESSAGE;
117
+ return message === HarnessError.CONTEXT_OVERFLOW_MESSAGE;
118
118
  }
@@ -16,66 +16,33 @@ function patch(next, current) {
16
16
  return next === null || next === undefined ? current : next;
17
17
  }
18
18
  export async function listAgents(store, providerDeviceId) {
19
- const snap = store.view.snapshot();
20
- try {
21
- if (providerDeviceId) {
22
- return await snap
23
- .find(AGENTS_BY_PROVIDER, providerRange(providerDeviceId))
24
- .toArray();
25
- }
26
- return await snap.find(ACTIVE_AGENTS).toArray();
27
- }
28
- finally {
29
- await snap.close();
19
+ if (providerDeviceId) {
20
+ return store.view.find(AGENTS_BY_PROVIDER, providerRange(providerDeviceId)).toArray();
30
21
  }
22
+ return store.view.find(ACTIVE_AGENTS).toArray();
31
23
  }
32
24
  export async function getDefaultAgent(store, providerDeviceId) {
33
- const snap = store.view.snapshot();
34
- try {
35
- const range = providerDeviceId ? providerRange(providerDeviceId) : {};
36
- return await snap.findOne(DEFAULT_AGENTS, range);
37
- }
38
- finally {
39
- await snap.close();
40
- }
25
+ const range = providerDeviceId ? providerRange(providerDeviceId) : {};
26
+ return store.view.findOne(DEFAULT_AGENTS, range);
41
27
  }
42
28
  function providerRange(providerDeviceId) {
43
29
  return { gte: { providerDeviceId }, lte: { providerDeviceId } };
44
30
  }
45
31
  export async function getAgent(store, id) {
46
- const snap = store.view.snapshot();
47
- try {
48
- const agent = await snap.get(AGENTS, { id });
49
- return agent;
50
- }
51
- finally {
52
- await snap.close();
53
- }
32
+ return store.view.get(AGENTS, { id });
54
33
  }
55
34
  export async function isOrphaned(store, agent) {
56
- const snap = store.view.snapshot();
57
- try {
58
- const device = await snap.get(DEVICES, { id: agent.providerDeviceId });
59
- return !device || device.revoked === true;
60
- }
61
- finally {
62
- await snap.close();
63
- }
35
+ const device = await store.view.get(DEVICES, { id: agent.providerDeviceId });
36
+ return !device || device.revoked === true;
64
37
  }
65
38
  export async function liveProviderIds(store) {
66
39
  const live = new Set();
67
- const snap = store.view.snapshot();
68
- try {
69
- for await (const device of snap.find(DEVICES)) {
70
- if (!device.revoked) {
71
- live.add(b4a.toHex(device.id));
72
- }
40
+ for await (const device of store.view.find(DEVICES)) {
41
+ if (!device.revoked) {
42
+ live.add(b4a.toHex(device.id));
73
43
  }
74
- return live;
75
- }
76
- finally {
77
- await snap.close();
78
44
  }
45
+ return live;
79
46
  }
80
47
  export async function mustGetAgent(store, id) {
81
48
  const agent = await getAgent(store, id);
@@ -3,14 +3,7 @@ import b4a from 'b4a';
3
3
  import { APPROVAL_RULES } from './collections.js';
4
4
  import { CoreError } from '../errors.js';
5
5
  export async function listApprovalRules(store) {
6
- const snap = store.view.snapshot();
7
- try {
8
- const rules = await snap.find(APPROVAL_RULES).toArray();
9
- return rules;
10
- }
11
- finally {
12
- await snap.close();
13
- }
6
+ return store.view.find(APPROVAL_RULES).toArray();
14
7
  }
15
8
  export async function putApprovalRule(store, input) {
16
9
  const now = Date.now();
@@ -3,7 +3,7 @@ import type CorestoreStorage from 'hypercore-storage';
3
3
  import type { LoggerInterface } from '@qvac/logging';
4
4
  import type { LocalMeshMembership } from '../../spec/local/hyperschema/types.d.ts';
5
5
  import { type PublicRecord } from './public-mesh.ts';
6
- import { Mesh } from './mesh.ts';
6
+ import { Mesh, type ErrorSink } from './mesh.ts';
7
7
  export interface ArchivedMeshCounts {
8
8
  readonly chats: number;
9
9
  readonly agents: number;
@@ -22,6 +22,7 @@ export interface ArchivedMeshOptions {
22
22
  readonly storage: CorestoreStorage;
23
23
  readonly deviceId: Buffer;
24
24
  readonly logger: LoggerInterface | null;
25
+ readonly onerror?: ErrorSink;
25
26
  readonly publicRecord?: (workspaceKey: Buffer) => Promise<PublicRecord | null>;
26
27
  }
27
28
  export declare function withArchivedMesh<T>(row: LocalMeshMembership, opts: ArchivedMeshOptions, fn: (mesh: Mesh, account: Mesh) => Promise<T>): Promise<T>;
@@ -4,7 +4,7 @@ import { copyBlob, copyChunks, stripped } from './chat-copy.js';
4
4
  import { publicMeshKey, publicSeed } from './public-mesh.js';
5
5
  import { Mesh } from './mesh.js';
6
6
  import { recordedRole } from './local/index.js';
7
- import { ACTIVE_AGENTS, ACTIVE_CHATS, ACTIVE_CHAT_GROUPS, ACTIVE_TOOL_CONFIGS_BY_AGENT, AGENTS, KNOWLEDGE, MESH_SEEDS } from './collections.js';
7
+ import { ACTIVE_AGENTS, ACTIVE_CHATS, ACTIVE_CHAT_GROUPS, ACTIVE_TOOL_CONFIGS_BY_AGENT, AGENTS, CHATS, CHAT_GROUPS, KNOWLEDGE, MESH_SEEDS } from './collections.js';
8
8
  import { CoreError } from '../errors.js';
9
9
  const MIGRATED_SUFFIX = ' (migrated)';
10
10
  export async function withArchivedMesh(row, opts, fn) {
@@ -17,7 +17,8 @@ export async function withArchivedMesh(row, opts, fn) {
17
17
  role: recordedRole(row),
18
18
  deviceId: opts.deviceId,
19
19
  writerSeed: row.writerSeed ?? undefined,
20
- logger: opts.logger
20
+ logger: opts.logger,
21
+ onerror: opts.onerror
21
22
  });
22
23
  await mesh.ready();
23
24
  try {
@@ -62,7 +63,8 @@ async function openArchivedPublic(account, opts) {
62
63
  role: 'public',
63
64
  deviceId: opts.deviceId,
64
65
  writerSeed: record.writerSeed,
65
- logger: opts.logger
66
+ logger: opts.logger,
67
+ onerror: opts.onerror
66
68
  });
67
69
  await mesh.ready();
68
70
  if (mesh.localLength === 0) {
@@ -74,17 +76,15 @@ async function openArchivedPublic(account, opts) {
74
76
  }
75
77
  export async function countContents(mesh, into, agents = mesh) {
76
78
  const snap = mesh.view.snapshot();
77
- const defined = agents.view.snapshot();
78
79
  try {
79
80
  return {
80
81
  chats: await count(snap.find(ACTIVE_CHATS)),
81
- agents: await countAgents(defined, into),
82
+ agents: await countAgents(agents === mesh ? snap : agents.view, into),
82
83
  knowledge: (await liveKnowledge(snap)).length
83
84
  };
84
85
  }
85
86
  finally {
86
87
  await snap.close();
87
- await defined.close();
88
88
  }
89
89
  }
90
90
  async function countAgents(snap, into) {
@@ -109,10 +109,14 @@ export async function migrateContents(from, into, configs = {}) {
109
109
  const targets = { into, configsFrom: configs.from ?? from, configsInto: configs.into ?? into };
110
110
  const agents = await migrateAgents(targets, new Set(chats.map((chat) => chat.agentId)));
111
111
  for (const group of groups) {
112
- batch.dispatch('put-chat-group', { group: stripped(group) });
112
+ if (!(await into.view.get(CHAT_GROUPS, { id: group.id }))) {
113
+ batch.dispatch('put-chat-group', { group: stripped(group) });
114
+ }
113
115
  }
114
116
  for (const chat of chats) {
115
- batch.dispatch('put-chat', { chat: stripped(chat) });
117
+ if (!(await into.view.get(CHATS, { id: chat.id }))) {
118
+ batch.dispatch('put-chat', { chat: stripped(chat) });
119
+ }
116
120
  }
117
121
  await batch.flush();
118
122
  for (const chat of chats) {
@@ -1,14 +1,17 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { QvacChunk } from '../../spec/mesh/hyperschema/types.d.ts';
3
3
  import type { Mesh } from './mesh.ts';
4
+ export declare const UNCOPIED: ReadonlySet<string>;
4
5
  export interface MeshCopy {
5
6
  readonly from: Mesh;
6
7
  readonly into: Mesh;
7
8
  }
8
- export interface ChatMoveCopy extends MeshCopy {
9
+ export interface BlobCopy extends MeshCopy {
10
+ readonly references: Map<string, string>;
11
+ }
12
+ export interface ChatMoveCopy extends BlobCopy {
9
13
  readonly chatId: string;
10
14
  readonly originDeviceId: Buffer;
11
- readonly references: Map<string, string>;
12
15
  }
13
16
  export declare function copyChunks(snap: HyperDB, chatId: string, { from, into }: MeshCopy): Promise<void>;
14
17
  export interface MovedChunks {
@@ -19,7 +22,7 @@ export interface MovedChunks {
19
22
  export declare function copyMovedChunks(snap: HyperDB, chatId: string, copy: ChatMoveCopy): Promise<MovedChunks>;
20
23
  export declare function sentBy(chunk: QvacChunk): Buffer | undefined;
21
24
  export declare function copyMovedKnowledge(snap: HyperDB, chatId: string, copy: ChatMoveCopy): Promise<void>;
22
- export declare function movedBlob({ from, into, references }: ChatMoveCopy, { blobId }: BlobHolder): Promise<string>;
25
+ export declare function movedBlob({ from, into, references }: BlobCopy, { blobId }: BlobHolder): Promise<string>;
23
26
  export declare function removedReference({ mimeType }: TypedFile): string;
24
27
  interface TypedFile {
25
28
  readonly mimeType: string;
@@ -36,6 +39,18 @@ export interface ContentCopy extends MeshCopy {
36
39
  readonly deviceId: Buffer;
37
40
  readonly chats?: ReadonlySet<string>;
38
41
  readonly endsOpenRuns?: boolean;
42
+ readonly baseline?: CopyBaseline;
43
+ readonly historyOnly?: boolean;
44
+ }
45
+ export interface CopyBaseline {
46
+ get(key: string): Promise<CopiedFields | null>;
47
+ set(rows: ReadonlyArray<CopiedRow>): Promise<void>;
48
+ }
49
+ export type CopiedFields = Readonly<Record<string, string>>;
50
+ export declare function isCopiedFields(value: unknown): value is CopiedFields;
51
+ export interface CopiedRow {
52
+ readonly key: string;
53
+ readonly fields: CopiedFields;
39
54
  }
40
55
  export declare function copyContent(copy: ContentCopy): Promise<void>;
41
56
  export declare const UPGRADED_RUN_REASON = "the app was updated before this was answered, send it again";
@@ -1,12 +1,15 @@
1
1
  import b4a from 'b4a';
2
2
  import { PENDING_BLOB_REFERENCE, isPendingBlobReference, parseBlobReference } from './files/store/index.js';
3
3
  import { CoreError } from '../errors.js';
4
+ import { isRecord } from '../guards.js';
4
5
  import { inlineReference } from './files/store/inline.js';
5
6
  import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
6
7
  import { appendChunk, ownedByUs, updateOp } from './chunk.js';
7
- import { runRequestOf } from './contract/chunks.js';
8
+ import { mayEditChunk, runRequestOf } from './contract/chunks.js';
9
+ import { PATCHABLE_CHAT } from './contract/chat.js';
10
+ import { PATCHABLE_GROUP } from './contract/chat-group.js';
8
11
  import { ACTIVE_AGENTS, AGENTS, CHATS, CHAT_GROUPS, CHUNKS, CHUNKS_BY_CHAT, DEVICES, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, KNOWLEDGE_BY_CHAT, PURGED_CHATS, TOOL_ACTIVITY } from './collections.js';
9
- const UNCOPIED = new Set(['context', 'model-request']);
12
+ export const UNCOPIED = new Set(['context', 'model-request']);
10
13
  export async function copyChunks(snap, chatId, { from, into }) {
11
14
  const batch = into.batch();
12
15
  for (const chunk of await chunksOf(snap, chatId)) {
@@ -175,16 +178,20 @@ export function stripped(row) {
175
178
  const { version: _version, ...rest } = row;
176
179
  return rest;
177
180
  }
181
+ export function isCopiedFields(value) {
182
+ return isRecord(value) && Object.values(value).every((field) => typeof field === 'string');
183
+ }
178
184
  export async function copyContent(copy) {
179
185
  await copy.into.autobee.update();
180
186
  await copy.from.autobee.update();
181
187
  const snap = copy.from.view.snapshot();
182
188
  try {
183
- const remap = await defaultAgentRemap(snap, copy.into);
184
- const chats = await copyRows(snap, copy, remap);
189
+ const chats = copy.historyOnly
190
+ ? [...(copy.chats ?? [])]
191
+ : (await copyRows(snap, copy, await defaultAgentRemap(snap, copy.into))).map(({ id }) => id);
185
192
  const references = new Map();
186
- for (const chat of chats) {
187
- await copyChatChunks(snap, chat.id, { ...copy, references });
193
+ for (const chatId of chats) {
194
+ await copyChatChunks(snap, chatId, { ...copy, references });
188
195
  }
189
196
  await copyFiles(snap, { ...copy, references });
190
197
  await copyToolActivity(snap, copy);
@@ -207,10 +214,12 @@ async function defaultAgentRemap(snap, into) {
207
214
  return remap;
208
215
  }
209
216
  async function copyRows(snap, copy, remap) {
210
- const { into } = copy;
217
+ const { into, baseline } = copy;
211
218
  const batch = into.batch();
212
219
  const chats = await copiedChats(snap, copy);
213
- await copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup: !!copy.chats });
220
+ const copied = [];
221
+ const catalog = { into, batch, remap, chats, ungroup: !!copy.chats, baseline, copied };
222
+ await copyAgentsAndGroups(snap, catalog);
214
223
  const live = [];
215
224
  for (const chat of chats) {
216
225
  const agent = remap.get(chat.agentId);
@@ -220,12 +229,10 @@ async function copyRows(snap, copy, remap) {
220
229
  if (!chat.deletedAt) {
221
230
  live.push(chat);
222
231
  }
223
- if (await newer(into, CHATS, chat)) {
224
- const groupId = copy.chats ? undefined : chat.groupId;
225
- batch.dispatch('put-chat', {
226
- chat: { ...stripped(chat), agentId: agent?.id ?? chat.agentId, groupId }
227
- });
228
- }
232
+ const groupId = copy.chats ? undefined : chat.groupId;
233
+ const sent = { ...stripped(chat), agentId: agent?.id ?? chat.agentId, groupId };
234
+ const edit = await editOf(into, { collection: CHATS, row: sent, patchable: PATCHABLE_CHAT, baseline }, copied);
235
+ dispatchEdit(batch, edit, { put: 'put-chat', patch: 'patch-chat', payload: { chat: sent } });
229
236
  }
230
237
  const purged = copy.chats ? [] : await snap.find(PURGED_CHATS).toArray();
231
238
  for (const row of purged) {
@@ -234,13 +241,14 @@ async function copyRows(snap, copy, remap) {
234
241
  }
235
242
  }
236
243
  await batch.flush();
244
+ await baseline?.set(copied);
237
245
  return live;
238
246
  }
239
247
  async function copiedChats(snap, { chats }) {
240
248
  const all = await snap.find(CHATS).toArray();
241
249
  return chats ? all.filter((chat) => chats.has(chat.id) && !chat.deletedAt) : all;
242
250
  }
243
- async function copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup }) {
251
+ async function copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup, baseline, copied }) {
244
252
  const used = new Set(chats.map((chat) => chat.agentId));
245
253
  for (const agent of await snap.find(AGENTS).toArray()) {
246
254
  if (used.has(agent.id) && !remap.has(agent.id) && (await newer(into, AGENTS, agent))) {
@@ -248,10 +256,37 @@ async function copyAgentsAndGroups(snap, { into, batch, remap, chats, ungroup })
248
256
  }
249
257
  }
250
258
  const groups = ungroup ? [] : await snap.find(CHAT_GROUPS).toArray();
251
- for (const group of groups) {
252
- if (await newer(into, CHAT_GROUPS, group)) {
253
- batch.dispatch('put-chat-group', { group: stripped(group) });
254
- }
259
+ for (const row of groups) {
260
+ const group = stripped(row);
261
+ const copy = { collection: CHAT_GROUPS, row: group, patchable: PATCHABLE_GROUP, baseline };
262
+ const edit = await editOf(into, copy, copied);
263
+ dispatchEdit(batch, edit, {
264
+ put: 'put-chat-group',
265
+ patch: 'patch-chat-group',
266
+ payload: { group }
267
+ });
268
+ }
269
+ }
270
+ async function editOf(into, { collection, row, patchable, baseline }, copied) {
271
+ const held = await into.view.get(collection, { id: row.id });
272
+ if (!baseline) {
273
+ return !held || (held.updatedAt ?? 0) < (row.updatedAt ?? 0) ? 'put' : [];
274
+ }
275
+ const key = `${collection}/${row.id}`;
276
+ const fields = Object.fromEntries(patchable.map((f) => [f, JSON.stringify(row[f] ?? null)]));
277
+ const last = await baseline.get(key);
278
+ const changed = last ? patchable.filter((field) => fields[field] !== last[field]) : [];
279
+ if (!last || changed.length > 0) {
280
+ copied.push({ key, fields });
281
+ }
282
+ return !held || (row.deletedAt && !held.deletedAt) ? 'put' : changed;
283
+ }
284
+ function dispatchEdit(batch, edit, { put, patch, payload }) {
285
+ if (edit === 'put') {
286
+ batch.dispatch(put, payload);
287
+ }
288
+ else if (edit.length > 0) {
289
+ batch.dispatch(patch, { ...payload, fields: edit });
255
290
  }
256
291
  }
257
292
  async function seededChat(snap, chat, agent) {
@@ -323,16 +358,27 @@ async function senderOf(snap, chunk) {
323
358
  return sentBy(chunk) ?? device?.memberId ?? undefined;
324
359
  }
325
360
  async function updateHeldChunk(chunk, held, copy) {
326
- if (!b4a.equals(chunk.originDeviceId, copy.deviceId))
327
- return;
328
361
  const { into } = copy;
362
+ const decided = chunk.approvalRequest?.status;
363
+ if (held.approvalRequest?.status === 'pending' && decided && decided !== 'pending') {
364
+ const resolvedAt = chunk.updatedAt ?? chunk.createdAt;
365
+ await into.dispatch('resolve-approval', {
366
+ id: chunk.id,
367
+ approved: decided === 'approved',
368
+ resolvedAt
369
+ });
370
+ return;
371
+ }
329
372
  if (chunk.runStatus) {
330
- if (newerEdit(chunk, held)) {
373
+ if (b4a.equals(chunk.originDeviceId, copy.deviceId) && newerEdit(chunk, held)) {
331
374
  const op = updateOp(chunk, { id: chunk.id, runStatus: chunk.runStatus });
332
375
  await into.dispatch('update-chunk', op);
333
376
  }
334
377
  return;
335
378
  }
379
+ if (!(await mayEditChunk(into.view, held, into.localWriterKey))) {
380
+ return;
381
+ }
336
382
  const attachment = await repointed(chunk, held, copy);
337
383
  if (attachment) {
338
384
  await into.dispatch('update-chunk', updateOp(chunk, { id: chunk.id, attachment }));
@@ -366,7 +412,11 @@ async function copyFiles(snap, copy) {
366
412
  const copied = chats ? rows.filter((row) => !!row.chatId && chats.has(row.chatId)) : rows;
367
413
  const ids = new Set(copied.map((row) => row.id));
368
414
  for (const row of copied) {
369
- if (await into.view.get(KNOWLEDGE, { id: row.id })) {
415
+ const held = await into.view.get(KNOWLEDGE, { id: row.id });
416
+ if (held && row.deletedAt && !held.deletedAt) {
417
+ batch.dispatch('remove-knowledge', { knowledgeId: row.id, at: row.deletedAt });
418
+ }
419
+ if (held) {
370
420
  continue;
371
421
  }
372
422
  const blobId = row.deletedAt ? removedReference(row) : await movedBlob(moveOf(copy), row);
@@ -398,8 +448,7 @@ async function copyToolActivity(snap, { into, chats }) {
398
448
  if (await into.view.get(TOOL_ACTIVITY, { id: row.id })) {
399
449
  continue;
400
450
  }
401
- const { seq: _seq, ...activity } = stripped(row);
402
- batch.dispatch('append-tool-activity', { activity });
451
+ batch.dispatch('append-tool-activity', { activity: { ...stripped(row), seq: 0 } });
403
452
  }
404
453
  await batch.flush();
405
454
  }
@@ -157,6 +157,9 @@ async function forkedChunks(ctx, chunks) {
157
157
  if (through && !ctx.kept.has(through)) {
158
158
  continue;
159
159
  }
160
+ if (chunk.sourceId && chunk.attachment && !(await heldReference(ctx, chunk.attachment))) {
161
+ continue;
162
+ }
160
163
  forked.push(await forkedChunk(ctx, chunk));
161
164
  }
162
165
  return forked;
@@ -200,21 +203,27 @@ async function forkedDocuments(ctx, rows) {
200
203
  return forked;
201
204
  }
202
205
  async function forkedReference(ctx, file) {
206
+ if (!file.deletedAt && isPendingBlobReference(file.blobId)) {
207
+ throw new CoreError('CHAT_FORK_FILE_ARRIVING');
208
+ }
209
+ const held = await heldReference(ctx, file);
210
+ if (!held) {
211
+ throw new CoreError('CHAT_FORK_FILE_NOT_HERE');
212
+ }
213
+ return held;
214
+ }
215
+ async function heldReference(ctx, file) {
203
216
  if (file.deletedAt) {
204
217
  return removedReference(file);
205
218
  }
206
- if (isPendingBlobReference(file.blobId)) {
207
- throw new CoreError('CHAT_FORK_FILE_ARRIVING');
208
- }
209
219
  const known = ctx.references.get(file.blobId);
210
220
  if (known) {
211
221
  return known;
212
222
  }
213
223
  const copied = await copyBlob(ctx.from, ctx.into, file.blobId);
214
- if (!copied) {
215
- throw new CoreError('CHAT_FORK_FILE_NOT_HERE');
224
+ if (copied) {
225
+ ctx.references.set(file.blobId, copied);
216
226
  }
217
- ctx.references.set(file.blobId, copied);
218
227
  return copied;
219
228
  }
220
229
  async function forkedChat(ctx, input) {
@@ -1,4 +1,5 @@
1
1
  import { CoreError } from '../errors.js';
2
+ import { nextUpdatedAt } from './contract/chat.js';
2
3
  import { randomId } from './id.js';
3
4
  import { place } from './rank.js';
4
5
  import { ACTIVE_CHAT_GROUPS, CHAT_GROUPS } from './collections.js';
@@ -23,23 +24,10 @@ export async function listChatGroupsIn(snap) {
23
24
  return groups.sort(byGroupOrder);
24
25
  }
25
26
  export async function listChatGroups(store) {
26
- const snap = store.view.snapshot();
27
- try {
28
- return await listChatGroupsIn(snap);
29
- }
30
- finally {
31
- await snap.close();
32
- }
27
+ return listChatGroupsIn(store.view);
33
28
  }
34
29
  export async function getChatGroup(store, id) {
35
- const snap = store.view.snapshot();
36
- try {
37
- const group = await snap.get(CHAT_GROUPS, { id });
38
- return group;
39
- }
40
- finally {
41
- await snap.close();
42
- }
30
+ return store.view.get(CHAT_GROUPS, { id });
43
31
  }
44
32
  export async function mustGetChatGroup(store, id) {
45
33
  const group = await getChatGroup(store, id);
@@ -79,8 +67,12 @@ export async function createChatGroup(store, input) {
79
67
  }
80
68
  async function patchChatGroup(store, id, patch) {
81
69
  const current = await mustGetActiveChatGroup(store, id);
82
- const updated = { ...current, ...patch, updatedAt: Date.now() };
83
- await store.dispatch('put-chat-group', { group: updated });
70
+ const updated = {
71
+ ...current,
72
+ ...patch,
73
+ updatedAt: nextUpdatedAt(current.updatedAt, Date.now())
74
+ };
75
+ await store.dispatch('patch-chat-group', { group: updated, fields: Object.keys(patch) });
84
76
  return updated;
85
77
  }
86
78
  export async function renameChatGroup(store, id, name) {
@@ -115,15 +107,14 @@ export async function moveChatGroup(store, id, { afterId }) {
115
107
  await snap.close();
116
108
  }
117
109
  const stamp = (row, rank) => ({
118
- ...row,
119
- rank,
120
- updatedAt: Math.max(now, row.updatedAt)
110
+ group: { ...row, rank, updatedAt: nextUpdatedAt(row.updatedAt, now) },
111
+ fields: ['rank']
121
112
  });
122
113
  const batch = store.batch();
123
114
  for (const { row, rank } of placement.respaced) {
124
- batch.dispatch('put-chat-group', { group: stamp(row, rank) });
115
+ batch.dispatch('patch-chat-group', stamp(row, rank));
125
116
  }
126
- batch.dispatch('put-chat-group', { group: stamp(current, placement.rank) });
117
+ batch.dispatch('patch-chat-group', stamp(current, placement.rank));
127
118
  await batch.flush();
128
119
  return mustGetActiveChatGroup(store, id);
129
120
  }
@@ -133,8 +124,9 @@ export async function sortChatGroupsByName(store) {
133
124
  for (const group of await listChatGroups(store)) {
134
125
  if (!group.rank)
135
126
  continue;
136
- batch.dispatch('put-chat-group', {
137
- group: { ...group, rank: 0, updatedAt: Math.max(now, group.updatedAt) }
127
+ batch.dispatch('patch-chat-group', {
128
+ group: { ...group, rank: 0, updatedAt: nextUpdatedAt(group.updatedAt, now) },
129
+ fields: ['rank']
138
130
  });
139
131
  }
140
132
  await batch.flush();
@@ -28,6 +28,9 @@ export declare class ChatMoves {
28
28
  private readonly _listeners;
29
29
  private readonly _settling;
30
30
  private readonly _moving;
31
+ private readonly _followed;
32
+ private readonly _forwards;
33
+ private readonly _forwarding;
31
34
  private readonly _abort;
32
35
  private readonly _onChange;
33
36
  private _patching;
@@ -49,6 +52,12 @@ export declare class ChatMoves {
49
52
  private _settle;
50
53
  private _land;
51
54
  private _leave;
55
+ followSource(source: Mesh): Promise<void>;
56
+ private _follow;
57
+ private _forward;
58
+ private _forwardMoved;
59
+ private _unreachable;
60
+ private _carryLate;
52
61
  private _cancel;
53
62
  private _stopped;
54
63
  private _copy;