@qvac/core 1.0.0-rc.2 → 1.0.0-rc.5

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 (213) hide show
  1. package/CHANGELOG.md +238 -12
  2. package/dist-lib/index.d.ts +3 -1
  3. package/dist-lib/index.js +1 -0
  4. package/dist-lib/lib/api/approval-rules.d.ts +1 -1
  5. package/dist-lib/lib/api/approval-rules.js +5 -1
  6. package/dist-lib/lib/api/chunk-delta.d.ts +7 -2
  7. package/dist-lib/lib/api/chunk-delta.js +89 -7
  8. package/dist-lib/lib/api/chunks.js +14 -8
  9. package/dist-lib/lib/api/config.d.ts +10 -3
  10. package/dist-lib/lib/api/config.js +51 -6
  11. package/dist-lib/lib/api/custom-models.js +3 -5
  12. package/dist-lib/lib/api/devices.js +3 -1
  13. package/dist-lib/lib/api/files.d.ts +1 -1
  14. package/dist-lib/lib/api/files.js +65 -8
  15. package/dist-lib/lib/api/index.d.ts +12 -2
  16. package/dist-lib/lib/api/index.js +6 -1
  17. package/dist-lib/lib/api/instrument.js +12 -4
  18. package/dist-lib/lib/api/members.js +7 -13
  19. package/dist-lib/lib/api/mesh.d.ts +1 -3
  20. package/dist-lib/lib/api/mesh.js +2 -2
  21. package/dist-lib/lib/api/notifications.js +8 -4
  22. package/dist-lib/lib/api/projects.js +5 -1
  23. package/dist-lib/lib/api/rag.js +32 -19
  24. package/dist-lib/lib/api/served.js +2 -1
  25. package/dist-lib/lib/api/workspace.js +0 -1
  26. package/dist-lib/lib/assistant/harness.d.ts +8 -2
  27. package/dist-lib/lib/assistant/harness.js +17 -4
  28. package/dist-lib/lib/assistant/history.d.ts +1 -1
  29. package/dist-lib/lib/assistant/history.js +19 -13
  30. package/dist-lib/lib/assistant/message.d.ts +6 -1
  31. package/dist-lib/lib/assistant/message.js +22 -2
  32. package/dist-lib/lib/assistant/model-round.d.ts +5 -0
  33. package/dist-lib/lib/assistant/model-round.js +47 -19
  34. package/dist-lib/lib/assistant/pdf-budget.d.ts +10 -2
  35. package/dist-lib/lib/assistant/pdf-budget.js +10 -11
  36. package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
  37. package/dist-lib/lib/assistant/pdf-pages.js +75 -0
  38. package/dist-lib/lib/assistant/round-host.js +5 -3
  39. package/dist-lib/lib/assistant-client.d.ts +2 -1
  40. package/dist-lib/lib/assistant-client.js +24 -6
  41. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  42. package/dist-lib/lib/assistant-tree.js +8 -5
  43. package/dist-lib/lib/assistant.d.ts +21 -7
  44. package/dist-lib/lib/assistant.js +481 -244
  45. package/dist-lib/lib/client.js +10 -1
  46. package/dist-lib/lib/compaction.d.ts +3 -1
  47. package/dist-lib/lib/compaction.js +18 -4
  48. package/dist-lib/lib/constants.d.ts +3 -0
  49. package/dist-lib/lib/constants.js +3 -0
  50. package/dist-lib/lib/core/agent-copies.js +7 -1
  51. package/dist-lib/lib/core/agent.js +1 -0
  52. package/dist-lib/lib/core/approval-rules.d.ts +5 -1
  53. package/dist-lib/lib/core/approval-rules.js +16 -1
  54. package/dist-lib/lib/core/chat-activity.js +13 -16
  55. package/dist-lib/lib/core/chat-copy.js +1 -1
  56. package/dist-lib/lib/core/chat-fork.js +2 -1
  57. package/dist-lib/lib/core/chat-move.d.ts +4 -0
  58. package/dist-lib/lib/core/chat-move.js +42 -11
  59. package/dist-lib/lib/core/chat.d.ts +0 -1
  60. package/dist-lib/lib/core/chat.js +1 -1
  61. package/dist-lib/lib/core/chunk.d.ts +14 -3
  62. package/dist-lib/lib/core/chunk.js +116 -25
  63. package/dist-lib/lib/core/collections.d.ts +12 -1
  64. package/dist-lib/lib/core/collections.js +12 -1
  65. package/dist-lib/lib/core/config-value.d.ts +7 -0
  66. package/dist-lib/lib/core/config-value.js +15 -0
  67. package/dist-lib/lib/core/contract/activity.d.ts +3 -2
  68. package/dist-lib/lib/core/contract/activity.js +39 -21
  69. package/dist-lib/lib/core/contract/agent-join.js +15 -19
  70. package/dist-lib/lib/core/contract/agents.js +8 -7
  71. package/dist-lib/lib/core/contract/chat.d.ts +3 -0
  72. package/dist-lib/lib/core/contract/chat.js +12 -1
  73. package/dist-lib/lib/core/contract/chunks.js +16 -6
  74. package/dist-lib/lib/core/contract/devices.js +6 -4
  75. package/dist-lib/lib/core/contract/index.js +2 -1
  76. package/dist-lib/lib/core/contract/invites.js +12 -31
  77. package/dist-lib/lib/core/contract/joined-meshes.js +2 -1
  78. package/dist-lib/lib/core/contract/mesh-rules.js +4 -0
  79. package/dist-lib/lib/core/contract/model-requests.js +7 -6
  80. package/dist-lib/lib/core/contract/project-device-keys.js +15 -6
  81. package/dist-lib/lib/core/contract/project-host-keys.js +5 -4
  82. package/dist-lib/lib/core/contract/projects.js +12 -13
  83. package/dist-lib/lib/core/contract/public-seeds.js +5 -5
  84. package/dist-lib/lib/core/contract/roles.js +2 -1
  85. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -1
  86. package/dist-lib/lib/core/contract/run-requests.js +19 -13
  87. package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
  88. package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
  89. package/dist-lib/lib/core/contract/workspace.js +6 -12
  90. package/dist-lib/lib/core/contract/writers.js +20 -6
  91. package/dist-lib/lib/core/deleted-agent-runs.d.ts +7 -0
  92. package/dist-lib/lib/core/deleted-agent-runs.js +46 -0
  93. package/dist-lib/lib/core/devices.d.ts +2 -1
  94. package/dist-lib/lib/core/devices.js +4 -2
  95. package/dist-lib/lib/core/document-attachment.d.ts +6 -3
  96. package/dist-lib/lib/core/document-attachment.js +10 -5
  97. package/dist-lib/lib/core/files/attachments.js +3 -3
  98. package/dist-lib/lib/core/files/index.d.ts +8 -5
  99. package/dist-lib/lib/core/files/index.js +111 -18
  100. package/dist-lib/lib/core/files/processing/index.d.ts +1 -1
  101. package/dist-lib/lib/core/files/processing/index.js +8 -2
  102. package/dist-lib/lib/core/files/processing/source.d.ts +1 -0
  103. package/dist-lib/lib/core/files/processing/source.js +3 -0
  104. package/dist-lib/lib/core/files/processing/state.d.ts +1 -0
  105. package/dist-lib/lib/core/files/processing/state.js +7 -0
  106. package/dist-lib/lib/core/files/rag/build.d.ts +1 -0
  107. package/dist-lib/lib/core/files/rag/build.js +14 -1
  108. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  109. package/dist-lib/lib/core/files/rag/index.js +1 -1
  110. package/dist-lib/lib/core/files/rag/produce.js +1 -0
  111. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
  112. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +7 -1
  113. package/dist-lib/lib/core/first-event.d.ts +6 -0
  114. package/dist-lib/lib/core/first-event.js +13 -0
  115. package/dist-lib/lib/core/invite.d.ts +2 -2
  116. package/dist-lib/lib/core/invite.js +5 -4
  117. package/dist-lib/lib/core/local/index.d.ts +16 -1
  118. package/dist-lib/lib/core/local/index.js +119 -10
  119. package/dist-lib/lib/core/mesh.d.ts +14 -3
  120. package/dist-lib/lib/core/mesh.js +127 -54
  121. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  122. package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
  123. package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
  124. package/dist-lib/lib/core/migrations/file-index.js +6 -2
  125. package/dist-lib/lib/core/migrations/index.d.ts +2 -1
  126. package/dist-lib/lib/core/migrations/index.js +66 -9
  127. package/dist-lib/lib/core/notification.js +96 -23
  128. package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
  129. package/dist-lib/lib/core/pdf/figure.js +64 -0
  130. package/dist-lib/lib/core/pdf/rasterize.d.ts +10 -1
  131. package/dist-lib/lib/core/pdf/rasterize.js +72 -22
  132. package/dist-lib/lib/core/peer-duplex.js +57 -5
  133. package/dist-lib/lib/core/public-mesh.js +8 -2
  134. package/dist-lib/lib/core/run-requests.js +2 -3
  135. package/dist-lib/lib/core/senders.d.ts +7 -6
  136. package/dist-lib/lib/core/senders.js +23 -30
  137. package/dist-lib/lib/core/shared-agents.d.ts +1 -0
  138. package/dist-lib/lib/core/shared-agents.js +47 -19
  139. package/dist-lib/lib/core/skill-secrets.d.ts +3 -2
  140. package/dist-lib/lib/core/skill-secrets.js +4 -1
  141. package/dist-lib/lib/core/sync/background.d.ts +3 -15
  142. package/dist-lib/lib/core/sync/background.js +25 -102
  143. package/dist-lib/lib/core/sync/devices.js +6 -3
  144. package/dist-lib/lib/core/sync/index.js +25 -3
  145. package/dist-lib/lib/core/sync-status.js +3 -3
  146. package/dist-lib/lib/core/vault-managed-skills.d.ts +18 -0
  147. package/dist-lib/lib/core/vault-managed-skills.js +105 -0
  148. package/dist-lib/lib/core.d.ts +66 -10
  149. package/dist-lib/lib/core.js +768 -179
  150. package/dist-lib/lib/credential-pusher.d.ts +11 -7
  151. package/dist-lib/lib/credential-pusher.js +40 -8
  152. package/dist-lib/lib/device-tree.d.ts +18 -0
  153. package/dist-lib/lib/device-tree.js +32 -0
  154. package/dist-lib/lib/doctor/redact.js +1 -1
  155. package/dist-lib/lib/errors.d.ts +12 -0
  156. package/dist-lib/lib/errors.js +12 -0
  157. package/dist-lib/lib/fs-drive-bridge.d.ts +7 -0
  158. package/dist-lib/lib/fs-drive-bridge.js +308 -30
  159. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  160. package/dist-lib/lib/knowledge-search.d.ts +1 -1
  161. package/dist-lib/lib/knowledge-search.js +5 -46
  162. package/dist-lib/lib/kv-cache-retention.d.ts +35 -0
  163. package/dist-lib/lib/kv-cache-retention.js +98 -0
  164. package/dist-lib/lib/latest-frame.d.ts +10 -0
  165. package/dist-lib/lib/latest-frame.js +48 -0
  166. package/dist-lib/lib/mesh-scoped-engine.js +1 -0
  167. package/dist-lib/lib/model-download.d.ts +6 -1
  168. package/dist-lib/lib/model-download.js +32 -17
  169. package/dist-lib/lib/oauth/resolve.js +13 -5
  170. package/dist-lib/lib/reconnecting-watch.js +19 -11
  171. package/dist-lib/lib/run-notices.d.ts +2 -0
  172. package/dist-lib/lib/run-notices.js +5 -0
  173. package/dist-lib/lib/run-replay.d.ts +13 -1
  174. package/dist-lib/lib/run-replay.js +58 -21
  175. package/dist-lib/schema/build.js +66 -57
  176. package/dist-lib/schema/config.d.ts +1 -1
  177. package/dist-lib/schema/config.js +1 -1
  178. package/dist-lib/schema/content.js +2 -1
  179. package/dist-lib/schema/engine.js +70 -6
  180. package/dist-lib/schema/field-order.js +7 -1
  181. package/dist-lib/schema/indexes.d.ts +4 -0
  182. package/dist-lib/schema/indexes.js +18 -0
  183. package/dist-lib/schema/local.js +9 -2
  184. package/dist-lib/schema/qvac.js +116 -6
  185. package/dist-lib/spec/bind.js +15 -0
  186. package/dist-lib/spec/calls.js +5 -0
  187. package/dist-lib/spec/capabilities.d.ts +10 -0
  188. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  189. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  190. package/dist-lib/spec/hrpc/index.js +63 -3
  191. package/dist-lib/spec/hrpc/messages.js +1537 -1395
  192. package/dist-lib/spec/hyperschema/index.js +1537 -1395
  193. package/dist-lib/spec/hyperschema/schema.json +126 -1
  194. package/dist-lib/spec/hyperschema/types.d.ts +33 -1
  195. package/dist-lib/spec/local/hyperdb/messages.js +110 -54
  196. package/dist-lib/spec/local/hyperschema/index.js +56 -28
  197. package/dist-lib/spec/local/hyperschema/schema.json +55 -20
  198. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -1
  199. package/dist-lib/spec/mesh/hyperdb/db.json +255 -51
  200. package/dist-lib/spec/mesh/hyperdb/index.js +1063 -404
  201. package/dist-lib/spec/mesh/hyperdb/messages.js +1673 -1202
  202. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +62 -56
  203. package/dist-lib/spec/mesh/hyperdispatch/index.js +156 -139
  204. package/dist-lib/spec/mesh/hyperdispatch/messages.js +809 -632
  205. package/dist-lib/spec/mesh/hyperschema/index.js +809 -632
  206. package/dist-lib/spec/mesh/hyperschema/schema.json +293 -174
  207. package/dist-lib/spec/mesh/hyperschema/types.d.ts +31 -1
  208. package/package.json +32 -26
  209. package/worker/assistant-sidecar.mjs +3 -0
  210. package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
  211. package/dist-lib/lib/core/files/processing/settle.js +0 -86
  212. package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
  213. package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
@@ -1,11 +1,12 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { ReadChunk } from './chunk.ts';
3
3
  import type { Mesh } from './mesh.ts';
4
- export interface Senders {
5
- readonly names: ReadonlyMap<string, string>;
6
- readonly live: ReadonlySet<string>;
7
- readonly inProject: ReadonlySet<string>;
4
+ export interface Sender {
5
+ readonly name?: string | undefined;
6
+ readonly live: boolean;
7
+ readonly inProject: boolean;
8
8
  }
9
+ export type Senders = (memberId: Buffer) => Promise<Sender>;
9
10
  export type SenderLookup = (meshId: string) => Promise<Senders | null>;
10
11
  export type SentChunk = ReadChunk & {
11
12
  readonly senderName?: string | undefined;
@@ -13,6 +14,6 @@ export type SentChunk = ReadChunk & {
13
14
  readonly senderFormerMember?: boolean;
14
15
  readonly senderCopiedBy?: Buffer | undefined;
15
16
  };
16
- export declare function projectSenders(workspace: HyperDB, project: HyperDB): Promise<Senders>;
17
+ export declare function projectSenders(workspace: HyperDB, project: HyperDB): Senders;
17
18
  export declare function ourSenderName(mesh: Mesh, meshId: string, lookup: SenderLookup | null | undefined): Promise<string | undefined>;
18
- export declare function withSenders(chunks: ReadonlyArray<ReadChunk>, senders: Senders | null): SentChunk[];
19
+ export declare function withSenders(chunks: ReadonlyArray<ReadChunk>, senders: Senders | null): Promise<SentChunk[]>;
@@ -1,52 +1,45 @@
1
1
  import b4a from 'b4a';
2
2
  import { MEMBERS, PROJECT_MEMBERS } from './collections.js';
3
- import { liveMemberIds } from './contract/workspace.js';
4
- export async function projectSenders(workspace, project) {
5
- const [names, live, inProject] = await Promise.all([
6
- memberNames(workspace),
7
- liveMemberIds(workspace),
8
- projectMemberIds(project)
9
- ]);
10
- return { names, live, inProject };
3
+ import { liveDevicesOf } from './contract/activity.js';
4
+ export function projectSenders(workspace, project) {
5
+ return async (memberId) => {
6
+ const [member, live, inProject] = await Promise.all([
7
+ workspace.get(MEMBERS, { id: memberId }),
8
+ liveDevicesOf(workspace, memberId),
9
+ project.get(PROJECT_MEMBERS, { memberId })
10
+ ]);
11
+ return { name: member?.name, live: live.length > 0, inProject: inProject !== null };
12
+ };
11
13
  }
12
14
  export async function ourSenderName(mesh, meshId, lookup) {
13
15
  const ours = mesh.ourMemberId ?? (await mesh.refreshOurMember())?.memberId;
14
16
  const senders = ours && lookup ? await lookup(meshId) : null;
15
- return ours ? senders?.names.get(b4a.toHex(ours)) : undefined;
17
+ return ours && senders ? (await senders(ours)).name : undefined;
16
18
  }
17
- export function withSenders(chunks, senders) {
19
+ export async function withSenders(chunks, senders) {
18
20
  if (!senders) {
19
21
  return [...chunks];
20
22
  }
21
- return chunks.map((chunk) => {
23
+ const known = new Map();
24
+ const senderOf = (id) => {
25
+ const hex = b4a.toHex(id);
26
+ const found = known.get(hex) ?? senders(id);
27
+ known.set(hex, found);
28
+ return found;
29
+ };
30
+ return Promise.all(chunks.map(async (chunk) => {
22
31
  if (chunk.type !== 'user-message' || !chunk.requesterMemberId) {
23
32
  return chunk;
24
33
  }
25
34
  const writer = chunk.requesterMemberId;
26
35
  const sender = chunk.senderMemberId ?? writer;
27
- const id = b4a.toHex(sender);
28
- const name = senders.names.get(id);
29
- const live = senders.live.has(id);
36
+ const { name, live, inProject } = await senderOf(sender);
30
37
  return {
31
38
  ...chunk,
32
39
  senderName: name,
33
- senderNotInProject: live && !senders.inProject.has(id),
40
+ senderNotInProject: live && !inProject,
34
41
  senderFormerMember: name !== undefined && !live,
35
42
  senderCopiedBy: b4a.equals(sender, writer) ? undefined : writer
36
43
  };
37
- });
38
- }
39
- async function memberNames(view) {
40
- const names = new Map();
41
- for await (const member of view.find(MEMBERS)) {
42
- names.set(b4a.toHex(member.id), member.name);
43
- }
44
- return names;
45
- }
46
- async function projectMemberIds(view) {
47
- const ids = new Set();
48
- for await (const row of view.find(PROJECT_MEMBERS)) {
49
- ids.add(b4a.toHex(row.memberId));
50
- }
51
- return ids;
44
+ }));
52
45
  }
@@ -37,3 +37,4 @@ export declare function pickAgent(scope: PickScope, { into, agentId, refusal }:
37
37
  export declare function unpickAgent(workspace: Mesh, into: Mesh, agentId: string): Promise<void>;
38
38
  export declare function followPickedAgents(workspace: Mesh, into: Mesh): Promise<void>;
39
39
  export declare function runDefinition(mesh: Mesh, row: QvacAgent | null): Promise<QvacAgent | null>;
40
+ export declare function unpickMemberAgents(workspace: Mesh, into: Mesh, memberId: Buffer): Promise<void>;
@@ -5,7 +5,8 @@ import { mustGetActiveAgent } from './agent.js';
5
5
  import { copyAgent } from './agent-copies.js';
6
6
  import { stripped } from './chat-copy.js';
7
7
  import { listsMember, readProject } from './contract/projects.js';
8
- import { ACTIVE_CHATS_BY_AGENT, AGENT_USERS, AGENTS, DEVICES } from './collections.js';
8
+ import { ACTIVE_CHATS_BY_AGENT, AGENT_USERS, AGENTS, AGENTS_BY_PROVIDER, DEVICES } from './collections.js';
9
+ import { devicesOfMember } from './contract/activity.js';
9
10
  export async function ownerOf(workspace, agent) {
10
11
  const device = await workspace.view.get(DEVICES, { id: agent.providerDeviceId });
11
12
  return device?.memberId ?? null;
@@ -133,7 +134,7 @@ export async function pickAgent(scope, { into, agentId, refusal = 'AGENT_NOT_SHA
133
134
  throw refuse(refusal, agentId);
134
135
  }
135
136
  const held = await into.view.get(AGENTS, { id: agentId });
136
- if (await heldByAnother(workspace, held, shared)) {
137
+ if (into.role === 'public' && (await heldByAnother(into, { workspace, held, shared }))) {
137
138
  throw new CoreError('CHAT_AGENT_UNAVAILABLE');
138
139
  }
139
140
  if (refusal === 'CHAT_AGENT_UNAVAILABLE' &&
@@ -164,11 +165,16 @@ export async function pickAgent(scope, { into, agentId, refusal = 'AGENT_NOT_SHA
164
165
  }
165
166
  export async function unpickAgent(workspace, into, agentId) {
166
167
  const copy = await into.view.get(AGENTS, { id: agentId });
167
- if (!picked(copy, workspace) || (await inUse(into, agentId))) {
168
+ if (await inUse(into, agentId)) {
169
+ return;
170
+ }
171
+ if (picked(copy, workspace)) {
172
+ const at = Math.max(Date.now(), copy.updatedAt + 1);
173
+ await into.dispatch('put-agent', { agent: { ...stripped(copy), deletedAt: at, updatedAt: at } });
174
+ }
175
+ else if (into.role !== 'project') {
168
176
  return;
169
177
  }
170
- const at = Math.max(Date.now(), copy.updatedAt + 1);
171
- await into.dispatch('put-agent', { agent: { ...stripped(copy), deletedAt: at, updatedAt: at } });
172
178
  const user = userOf(workspace, into);
173
179
  const userId = user?.memberId ?? user?.projectId;
174
180
  if (user && userId && (await workspace.view.get(AGENT_USERS, { agentId, userId }))) {
@@ -183,7 +189,7 @@ export async function followPickedAgents(workspace, into) {
183
189
  if (!follows(workspace, into, copy)) {
184
190
  continue;
185
191
  }
186
- if (await ownerRemoved(workspace, copy, shared)) {
192
+ if (await ownerRemoved(into, { workspace, copy, shared })) {
187
193
  if (!copy.deletedAt) {
188
194
  const at = copy.updatedAt + 1;
189
195
  await into.dispatch('put-agent', {
@@ -204,7 +210,10 @@ export async function followPickedAgents(workspace, into) {
204
210
  }
205
211
  }
206
212
  }
207
- async function ownerRemoved(workspace, copy, shared) {
213
+ async function ownerRemoved(into, { workspace, copy, shared }) {
214
+ if (into.role === 'project' && shared) {
215
+ return false;
216
+ }
208
217
  const host = await workspace.view.get(DEVICES, { id: copy.providerDeviceId });
209
218
  if (!host?.revoked || !host.memberId) {
210
219
  return false;
@@ -212,32 +221,34 @@ async function ownerRemoved(workspace, copy, shared) {
212
221
  const owner = shared && (await ownerOf(workspace, shared));
213
222
  return !owner || !b4a.equals(owner, host.memberId);
214
223
  }
215
- async function heldByAnother(workspace, held, shared) {
224
+ async function heldByAnother(into, { workspace, held, shared }) {
216
225
  if (!held || b4a.equals(held.providerDeviceId, shared.providerDeviceId)) {
217
226
  return false;
218
227
  }
219
- if (await ownerRemoved(workspace, held, shared)) {
228
+ if (await ownerRemoved(into, { workspace, copy: held, shared })) {
220
229
  return true;
221
230
  }
231
+ if (!held.deletedAt) {
232
+ return false;
233
+ }
222
234
  const [was, now] = [await ownerOf(workspace, held), await ownerOf(workspace, shared)];
223
235
  return !was || !now || !b4a.equals(was, now);
224
236
  }
225
- async function leftProject(into, { workspace, held, shared }) {
237
+ async function leftProject(into, { workspace, shared }) {
226
238
  if (into.role !== 'project') {
227
239
  return false;
228
240
  }
229
- const host = held && (await into.view.get(DEVICES, { id: held.providerDeviceId }));
230
- if (held?.deletedAt && host?.revoked) {
231
- return true;
232
- }
233
- if (await workspace.view.get(AGENT_USERS, { agentId: shared.id, userId: into.key })) {
234
- return false;
235
- }
241
+ const used = await workspace.view.get(AGENT_USERS, { agentId: shared.id, userId: into.key });
242
+ const host = await into.view.get(DEVICES, { id: shared.providerDeviceId });
236
243
  const [owner, project] = [
237
244
  await ownerOf(workspace, shared),
238
245
  await readProject(workspace.view, into.key)
239
246
  ];
240
- return !owner || !project || !listsMember(project, owner);
247
+ const listed = !!owner && !!project && listsMember(project, owner);
248
+ if (host?.revoked) {
249
+ return listed || !used;
250
+ }
251
+ return !used && !listed;
241
252
  }
242
253
  async function unpicked(into, copy, shared) {
243
254
  return !!copy.deletedAt && (!!shared.deletedAt || !(await inUse(into, copy.id)));
@@ -248,8 +259,25 @@ export async function runDefinition(mesh, row) {
248
259
  return row;
249
260
  }
250
261
  const shared = await workspace.view.get(AGENTS, { id: row.id });
251
- if (!shared || shared.deletedAt || (await ownerRemoved(workspace, row, shared))) {
262
+ if (!shared || shared.deletedAt || (await ownerRemoved(mesh, { workspace, copy: row, shared }))) {
252
263
  return null;
253
264
  }
254
265
  return pickedRow(shared, workspace, row.updatedAt);
255
266
  }
267
+ export async function unpickMemberAgents(workspace, into, memberId) {
268
+ for (const device of await devicesOfMember(workspace.view, memberId)) {
269
+ if (!device.memberId || !b4a.equals(device.memberId, memberId)) {
270
+ continue;
271
+ }
272
+ const range = { gte: { providerDeviceId: device.id }, lte: { providerDeviceId: device.id } };
273
+ for (const copy of await into.view.find(AGENTS_BY_PROVIDER, range).toArray()) {
274
+ if (copy.deletedAt || !copy.sharedIn) {
275
+ continue;
276
+ }
277
+ const at = Math.max(Date.now(), copy.updatedAt + 1);
278
+ await into.dispatch('put-agent', {
279
+ agent: { ...stripped(copy), deletedAt: at, updatedAt: at }
280
+ });
281
+ }
282
+ }
283
+ }
@@ -1,12 +1,13 @@
1
1
  import type { AssistantPushSkillSecretRequest } from '../../spec/hyperschema/types.d.ts';
2
- import type { openLocalStore } from './local/index.ts';
2
+ import type { ConfigEntry, openLocalStore } from './local/index.ts';
3
3
  import type { Mesh } from './mesh.ts';
4
4
  export type SkillSecretRequest = AssistantPushSkillSecretRequest;
5
5
  export type SetupFields = (skillName: string) => Promise<ReadonlyMap<string, boolean> | null>;
6
6
  export interface SkillSecretsOptions {
7
7
  readonly local: ReturnType<typeof openLocalStore>;
8
8
  readonly setupFields: SetupFields;
9
- readonly onstored: () => Promise<unknown>;
9
+ readonly validate?: (entry: ConfigEntry) => void;
10
+ readonly onstored: (entries: ReadonlyArray<ConfigEntry>) => Promise<unknown>;
10
11
  }
11
12
  export declare function storeSkillSecret(opts: SkillSecretsOptions, request: SkillSecretRequest): Promise<void>;
12
13
  export declare function isDeviceKey(key: string): boolean;
@@ -22,8 +22,11 @@ export async function storeSkillSecret(opts, request) {
22
22
  if (entries.length === 0) {
23
23
  return;
24
24
  }
25
+ for (const entry of entries) {
26
+ opts.validate?.(entry);
27
+ }
25
28
  await opts.local.setAll(entries);
26
- await opts.onstored();
29
+ await opts.onstored(entries);
27
30
  }
28
31
  export function isDeviceKey(key) {
29
32
  return DEVICE_SHARED_CREDENTIAL_KEYS.has(key.slice(key.lastIndexOf('.') + 1));
@@ -1,8 +1,6 @@
1
1
  import type { AutobeeView } from 'autobee';
2
- import type HyperDB from 'hyperdb';
3
2
  import type { LoggerInterface } from '@qvac/logging';
4
3
  export interface BackgroundSyncHost {
5
- readonly view: HyperDB;
6
4
  readonly autobee: {
7
5
  readonly bee: AutobeeView;
8
6
  };
@@ -10,8 +8,7 @@ export interface BackgroundSyncHost {
10
8
  emit(event: string, ...args: unknown[]): boolean;
11
9
  }
12
10
  export interface BackgroundSyncOptions {
13
- readonly batch: number;
14
- readonly pauseMs: number;
11
+ readonly parallelism?: number;
15
12
  readonly logger: LoggerInterface | null;
16
13
  readonly scope: ReadonlyArray<string>;
17
14
  }
@@ -21,27 +18,18 @@ export interface BackgroundSynced {
21
18
  export declare class BackgroundSync {
22
19
  private readonly _host;
23
20
  private readonly _opts;
24
- private _focus;
25
21
  private _running;
26
22
  private _again;
27
23
  private _stopped;
28
24
  private _paused;
29
25
  private _unpause;
30
- private _wake;
31
- private _entries;
32
26
  constructor(host: BackgroundSyncHost, opts: BackgroundSyncOptions);
33
- focus(chatId: string): void;
34
- run(focus?: string): Promise<boolean>;
27
+ focus(_chatId: string): void;
28
+ run(_focus?: string): Promise<boolean>;
35
29
  pause(): void;
36
30
  resume(): void;
37
31
  stop(): void;
38
32
  private _passes;
39
33
  private _pass;
40
- private _walkChats;
41
- private _next;
42
- private _walkChat;
43
- private _walkTree;
44
- private _step;
45
34
  private get _cut();
46
- private _sleep;
47
35
  }
@@ -1,27 +1,17 @@
1
- import { ACTIVE_CHATS, CHUNKS_BY_CHAT } from '../collections.js';
2
- const CUT_SHORT = new Error('background sync cut short');
3
1
  export class BackgroundSync {
4
2
  _host;
5
3
  _opts;
6
- _focus = null;
7
4
  _running = null;
8
5
  _again = false;
9
6
  _stopped = false;
10
7
  _paused = null;
11
8
  _unpause = noop;
12
- _wake = noop;
13
- _entries = 0;
14
9
  constructor(host, opts) {
15
10
  this._host = host;
16
11
  this._opts = opts;
17
12
  }
18
- focus(chatId) {
19
- this._focus = chatId;
20
- }
21
- run(focus) {
22
- if (focus) {
23
- this._focus = focus;
24
- }
13
+ focus(_chatId) { }
14
+ run(_focus) {
25
15
  if (this._stopped) {
26
16
  return Promise.resolve(false);
27
17
  }
@@ -41,7 +31,6 @@ export class BackgroundSync {
41
31
  }
42
32
  stop() {
43
33
  this._stopped = true;
44
- this._wake();
45
34
  this.resume();
46
35
  }
47
36
  async _passes() {
@@ -59,104 +48,38 @@ export class BackgroundSync {
59
48
  }
60
49
  async _pass() {
61
50
  const { logger, scope } = this._opts;
62
- const started = Date.now();
63
- const head = this._host.autobee.bee.head();
64
- this._entries = 0;
65
- logger?.info(scope, 'background sync started', {
66
- length: head?.length ?? 0,
67
- focus: this._focus !== null
68
- });
69
- let finished = false;
70
- try {
71
- if (head) {
72
- await this._walkChats();
73
- await this._walkTree(head);
74
- }
75
- finished = true;
51
+ while (this._paused) {
52
+ await this._paused;
76
53
  }
77
- catch (err) {
78
- if (err !== CUT_SHORT && !this._cut) {
79
- logger?.warn(scope, 'background sync failed', err);
80
- }
54
+ if (this._cut) {
55
+ return null;
81
56
  }
82
- const ms = Date.now() - started;
83
- logger?.info(scope, 'background sync finished', {
84
- entries: this._entries,
85
- ms,
86
- cutShort: !finished
87
- });
88
- return finished ? (head?.length ?? 0) : null;
89
- }
90
- async _walkChats() {
91
- const snap = this._host.view.snapshot();
57
+ const head = this._host.autobee.bee.head();
58
+ const started = Date.now();
59
+ logger?.info(scope, 'background sync started', { length: head?.length ?? 0 });
92
60
  try {
93
- const walked = new Set();
94
- if (this._focus) {
95
- await this._walkChat(snap, this._focus, walked);
96
- }
97
- const order = [];
98
- for await (const chat of snap.find(ACTIVE_CHATS, { reverse: true })) {
99
- order.push(chat.id);
100
- await this._step();
101
- }
102
- for (let chatId = this._next(order, walked); chatId; chatId = this._next(order, walked)) {
103
- await this._walkChat(snap, chatId, walked);
104
- }
105
- }
106
- finally {
107
- await snap.close();
61
+ await this._host.autobee.bee.download({ parallelism: this._opts.parallelism });
62
+ logger?.info(scope, 'background sync finished', {
63
+ length: head?.length ?? 0,
64
+ ms: Date.now() - started,
65
+ cutShort: false
66
+ });
67
+ return head?.length ?? 0;
108
68
  }
109
- }
110
- _next(order, walked) {
111
- if (this._focus && !walked.has(this._focus)) {
112
- return this._focus;
113
- }
114
- return order.find((chatId) => !walked.has(chatId)) ?? null;
115
- }
116
- async _walkChat(snap, chatId, walked) {
117
- walked.add(chatId);
118
- const range = { gte: { chatId }, lte: { chatId }, reverse: true };
119
- for await (const _chunk of snap.find(CHUNKS_BY_CHAT, range)) {
120
- await this._step();
121
- }
122
- this._opts.logger?.debug(this._opts.scope, 'background sync walked a chat', { chatId });
123
- }
124
- async _walkTree(head) {
125
- const bee = this._host.autobee.bee.checkout(head);
126
- try {
127
- for await (const _entry of bee.createReadStream()) {
128
- await this._step();
69
+ catch (err) {
70
+ if (this._cut) {
71
+ logger?.info(scope, 'background sync finished', {
72
+ ms: Date.now() - started,
73
+ cutShort: true
74
+ });
75
+ return null;
129
76
  }
130
- }
131
- finally {
132
- await bee.close();
133
- }
134
- }
135
- async _step() {
136
- if (++this._entries % this._opts.batch === 0) {
137
- await this._sleep(this._opts.pauseMs);
138
- }
139
- while (this._paused) {
140
- await this._paused;
141
- }
142
- if (this._cut) {
143
- throw CUT_SHORT;
77
+ logger?.warn(scope, 'background sync failed', err);
78
+ return null;
144
79
  }
145
80
  }
146
81
  get _cut() {
147
82
  return this._stopped || !!this._host.closing;
148
83
  }
149
- _sleep(ms) {
150
- if (ms === 0 || this._stopped) {
151
- return Promise.resolve();
152
- }
153
- return new Promise((resolve) => {
154
- const timer = setTimeout(resolve, ms);
155
- this._wake = () => {
156
- clearTimeout(timer);
157
- resolve();
158
- };
159
- });
160
- }
161
84
  }
162
85
  function noop() { }
@@ -7,8 +7,11 @@ export function syncOwnMember(mesh, device) {
7
7
  void mesh.refreshOurMember();
8
8
  }
9
9
  export function syncDeviceRevocation(mesh, device) {
10
- if (!isOwnKickedRow(mesh, device))
10
+ if (!device.writerKey || !b4a.equals(device.writerKey, mesh.localWriterKey)) {
11
11
  return;
12
- mesh.log?.info('sync', 'our writer was revoked (kicked)');
13
- mesh.emit('kicked');
12
+ }
13
+ if (isOwnKickedRow(mesh, device)) {
14
+ mesh.log?.info('sync', 'our writer was revoked (kicked)');
15
+ }
16
+ mesh.kickDirty().catch((err) => mesh.log?.warn('sync', 'reading our kick state failed', err));
14
17
  }
@@ -1,9 +1,9 @@
1
- import { changed } from './changed.js';
1
+ import { changed, liveRow } from './changed.js';
2
2
  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_SEEDS, PROJECT_DEVICE_KEYS, PROJECT_HOST_KEYS, PROJECT_KEYS, PROJECTS, PUBLIC_MESHES, PUBLIC_SEEDS, PURGED_CHATS, SKILL_SECRETS, WORKSPACE } from '../collections.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_KEYS, PROJECT_HOST_KEYS, PROJECT_KEYS, PROJECTS, PUBLIC_MESHES, PUBLIC_SEEDS, PURGED_CHATS, SKILL_SECRETS, WORKSPACE } from '../collections.js';
7
7
  export async function syncChanges(mesh, view, from) {
8
8
  if (mesh.closing)
9
9
  return null;
@@ -17,6 +17,7 @@ export async function syncChanges(mesh, view, from) {
17
17
  catch (err) {
18
18
  mesh.log?.warn('sync', 'change sync cut short', err);
19
19
  pass.membershipMoved = pass.agentsDirty = pass.meshesChanged = pass.nameChanged = true;
20
+ pass.runs = null;
20
21
  pass.groupsChanged = pass.secretsChanged = true;
21
22
  }
22
23
  try {
@@ -57,6 +58,9 @@ function newPass() {
57
58
  artifacts: [],
58
59
  orderDirty: new Set(),
59
60
  purged: new Map(),
61
+ notifications: [],
62
+ runs: { requests: [], deletedAgents: new Set() },
63
+ rulesRaised: false,
60
64
  unattestedDevice: false,
61
65
  membershipMoved: false,
62
66
  agentsDirty: false,
@@ -84,6 +88,11 @@ async function settle(mesh, pass) {
84
88
  if (pass.attachments.length) {
85
89
  await mesh.fileIndex?.refresh(pass.attachments);
86
90
  }
91
+ if (!pass.rulesRaised) {
92
+ for (const chunk of pass.notifications) {
93
+ syncNotificationChunk(mesh, chunk);
94
+ }
95
+ }
87
96
  if (pass.unattestedDevice) {
88
97
  mesh.emit('unattested-device');
89
98
  }
@@ -94,6 +103,9 @@ async function settle(mesh, pass) {
94
103
  if (pass.agentsDirty) {
95
104
  mesh.emit('agents-dirty');
96
105
  }
106
+ if (!pass.runs || pass.runs.requests.length || pass.runs.deletedAgents.size) {
107
+ mesh.emit('runs-landed', pass.runs);
108
+ }
97
109
  if (pass.secretsChanged) {
98
110
  mesh.emit('skill-secrets-dirty');
99
111
  }
@@ -127,6 +139,10 @@ async function walkChanges(mesh, { snap, from, files, pass }) {
127
139
  }
128
140
  else if (change.collection === AGENTS) {
129
141
  pass.agentsDirty = true;
142
+ const agent = changed(change, AGENTS) ? liveRow(change) : null;
143
+ if (agent?.deletedAt) {
144
+ pass.runs?.deletedAgents.add(agent.id);
145
+ }
130
146
  }
131
147
  else if (changed(change, CHATS)) {
132
148
  pass.orderDirty.add(change.value.id);
@@ -135,8 +151,14 @@ async function walkChanges(mesh, { snap, from, files, pass }) {
135
151
  else if (changed(change, CHUNK_COUNTERS)) {
136
152
  pass.orderDirty.add(change.value.chatId);
137
153
  }
154
+ else if (change.collection === MESH_RULES) {
155
+ pass.rulesRaised = true;
156
+ }
138
157
  else if (changed(change, CHUNKS)) {
139
- syncNotificationChunk(mesh, change.value);
158
+ pass.notifications.push(change.value);
159
+ if (change.value.type === 'run-request') {
160
+ pass.runs?.requests.push(change.value);
161
+ }
140
162
  if (change.value.attachment) {
141
163
  pass.attachments.push(change.value);
142
164
  }
@@ -1,6 +1,6 @@
1
1
  import { unref } from '../unref.js';
2
2
  import b4a from 'b4a';
3
- import { DEVICES } from './collections.js';
3
+ import { liveDevices } from './contract/activity.js';
4
4
  const MIN_SYNCING_MS = 1000;
5
5
  export function createSyncActivity(mesh) {
6
6
  const listeners = new Set();
@@ -120,8 +120,8 @@ async function writerDeviceMap(mesh) {
120
120
  const map = new Map();
121
121
  const snap = mesh.view.snapshot();
122
122
  try {
123
- for await (const device of snap.find(DEVICES)) {
124
- if (device.writerKey && !device.revoked) {
123
+ for await (const device of liveDevices(snap)) {
124
+ if (device.writerKey) {
125
125
  map.set(b4a.toHex(device.writerKey), device);
126
126
  }
127
127
  }
@@ -0,0 +1,18 @@
1
+ import { QUARANTINE_PREFIX } from './config-value.ts';
2
+ import type { ConfigEntry, openLocalStore } from './local/index.ts';
3
+ import type { Logger } from '../logger-type.ts';
4
+ export type VaultManagedSkillField = 'access-token' | 'oauth-expiry' | 'setting';
5
+ export interface VaultManagedSkills {
6
+ readonly mode: 'vault-managed';
7
+ readonly classify: (configKey: string) => VaultManagedSkillField | null;
8
+ readonly migrateLegacy?: (entries: ReadonlyArray<ConfigEntry>, context: {
9
+ readonly safeEntries: ReadonlyArray<ConfigEntry>;
10
+ }) => Promise<ReadonlyArray<string>>;
11
+ }
12
+ export { QUARANTINE_PREFIX };
13
+ type LocalStore = ReturnType<typeof openLocalStore>;
14
+ export declare function validateVaultManagedSkill(policy: VaultManagedSkills | undefined, entry: ConfigEntry): void;
15
+ export declare function quarantinedSkillKeys(local: LocalStore): Promise<string[]>;
16
+ export declare function discardQuarantinedSkills(local: LocalStore, keys: ReadonlyArray<string>): Promise<string[]>;
17
+ export declare function releaseQuarantinedSkills(local: LocalStore, entries: ReadonlyArray<ConfigEntry>): Promise<void>;
18
+ export declare function prepareVaultManagedSkills(policy: VaultManagedSkills | undefined, local: LocalStore, log?: Logger | null): Promise<string[]>;