@qvac/core 0.3.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/README.md +19 -17
  2. package/dist-lib/lib/api/agents.d.ts +3 -3
  3. package/dist-lib/lib/api/agents.js +31 -20
  4. package/dist-lib/lib/api/approval-rules.d.ts +2 -2
  5. package/dist-lib/lib/api/approval-rules.js +2 -2
  6. package/dist-lib/lib/api/attachments.d.ts +2 -2
  7. package/dist-lib/lib/api/attachments.js +14 -25
  8. package/dist-lib/lib/api/chat-groups.d.ts +2 -2
  9. package/dist-lib/lib/api/chat-groups.js +9 -9
  10. package/dist-lib/lib/api/chat.d.ts +4 -2
  11. package/dist-lib/lib/api/chat.js +18 -18
  12. package/dist-lib/lib/api/chunks.d.ts +2 -2
  13. package/dist-lib/lib/api/chunks.js +27 -36
  14. package/dist-lib/lib/api/current-mesh.d.ts +7 -15
  15. package/dist-lib/lib/api/current-mesh.js +7 -22
  16. package/dist-lib/lib/api/devices.d.ts +4 -3
  17. package/dist-lib/lib/api/devices.js +18 -18
  18. package/dist-lib/lib/api/execution.d.ts +2 -2
  19. package/dist-lib/lib/api/execution.js +6 -6
  20. package/dist-lib/lib/api/files.d.ts +2 -2
  21. package/dist-lib/lib/api/files.js +4 -4
  22. package/dist-lib/lib/api/index.d.ts +12 -11
  23. package/dist-lib/lib/api/index.js +15 -13
  24. package/dist-lib/lib/api/inference-calls.d.ts +1 -1
  25. package/dist-lib/lib/api/inference-calls.js +2 -2
  26. package/dist-lib/lib/api/knowledge.d.ts +2 -2
  27. package/dist-lib/lib/api/knowledge.js +22 -11
  28. package/dist-lib/lib/api/local-files.d.ts +3 -0
  29. package/dist-lib/lib/api/local-files.js +23 -0
  30. package/dist-lib/lib/api/members.d.ts +6 -2
  31. package/dist-lib/lib/api/members.js +24 -17
  32. package/dist-lib/lib/api/mesh.d.ts +4 -5
  33. package/dist-lib/lib/api/mesh.js +7 -9
  34. package/dist-lib/lib/api/notifications.d.ts +2 -13
  35. package/dist-lib/lib/api/notifications.js +20 -52
  36. package/dist-lib/lib/api/rag.d.ts +2 -2
  37. package/dist-lib/lib/api/rag.js +4 -4
  38. package/dist-lib/lib/api/range.d.ts +11 -0
  39. package/dist-lib/lib/api/range.js +10 -0
  40. package/dist-lib/lib/api/sync.d.ts +2 -2
  41. package/dist-lib/lib/api/sync.js +3 -3
  42. package/dist-lib/lib/api/tool-activity.d.ts +2 -2
  43. package/dist-lib/lib/api/tool-activity.js +11 -10
  44. package/dist-lib/lib/api/tool-configs.d.ts +2 -2
  45. package/dist-lib/lib/api/tool-configs.js +6 -7
  46. package/dist-lib/lib/api/upload-frames.d.ts +9 -0
  47. package/dist-lib/lib/api/upload-frames.js +17 -0
  48. package/dist-lib/lib/api/watchable.d.ts +1 -6
  49. package/dist-lib/lib/api/watchable.js +4 -13
  50. package/dist-lib/lib/assistant/history.d.ts +3 -3
  51. package/dist-lib/lib/assistant-client.js +3 -2
  52. package/dist-lib/lib/assistant-tree.js +1 -1
  53. package/dist-lib/lib/assistant.d.ts +16 -14
  54. package/dist-lib/lib/assistant.js +166 -137
  55. package/dist-lib/lib/collabora/convert.js +1 -1
  56. package/dist-lib/lib/core/agent.d.ts +5 -7
  57. package/dist-lib/lib/core/agent.js +26 -6
  58. package/dist-lib/lib/core/archived-mesh.js +1 -0
  59. package/dist-lib/lib/core/chat-order.d.ts +20 -0
  60. package/dist-lib/lib/core/chat-order.js +96 -0
  61. package/dist-lib/lib/core/chat.d.ts +0 -5
  62. package/dist-lib/lib/core/chat.js +1 -3
  63. package/dist-lib/lib/core/chunk.d.ts +7 -14
  64. package/dist-lib/lib/core/chunk.js +17 -35
  65. package/dist-lib/lib/core/contract/agents.js +8 -0
  66. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  67. package/dist-lib/lib/core/contract/compute.js +1 -0
  68. package/dist-lib/lib/core/contract/joined-meshes.js +4 -3
  69. package/dist-lib/lib/core/contract/members.js +4 -3
  70. package/dist-lib/lib/core/contract/writers.js +5 -4
  71. package/dist-lib/lib/core/departures.js +5 -4
  72. package/dist-lib/lib/core/device-invites.js +2 -1
  73. package/dist-lib/lib/core/files/attachments.d.ts +8 -1
  74. package/dist-lib/lib/core/files/index.d.ts +4 -4
  75. package/dist-lib/lib/core/files/index.js +18 -8
  76. package/dist-lib/lib/core/files/local-index.d.ts +16 -0
  77. package/dist-lib/lib/core/files/local-index.js +38 -0
  78. package/dist-lib/lib/core/files/processing/document.js +2 -1
  79. package/dist-lib/lib/core/files/processing/ownership.js +6 -3
  80. package/dist-lib/lib/core/files/records/chunks.js +1 -0
  81. package/dist-lib/lib/core/files/records/reclaim.js +1 -0
  82. package/dist-lib/lib/core/files/records/routing.js +2 -1
  83. package/dist-lib/lib/core/files/store/blob.d.ts +1 -1
  84. package/dist-lib/lib/core/files/store/blob.js +6 -3
  85. package/dist-lib/lib/core/files/store/index.d.ts +1 -1
  86. package/dist-lib/lib/core/files/store/reference.js +4 -3
  87. package/dist-lib/lib/core/files/store/server.d.ts +8 -4
  88. package/dist-lib/lib/core/files/store/server.js +16 -12
  89. package/dist-lib/lib/core/files/store/sweep.js +2 -1
  90. package/dist-lib/lib/core/files/upload.d.ts +1 -1
  91. package/dist-lib/lib/core/hosted-agents.js +1 -1
  92. package/dist-lib/lib/core/image/cache-sweep.d.ts +1 -0
  93. package/dist-lib/lib/core/image/cache-sweep.js +7 -3
  94. package/dist-lib/lib/core/local/files.d.ts +24 -0
  95. package/dist-lib/lib/core/local/files.js +154 -0
  96. package/dist-lib/lib/core/local/index.d.ts +21 -5
  97. package/dist-lib/lib/core/local/index.js +75 -45
  98. package/dist-lib/lib/core/membership.d.ts +4 -1
  99. package/dist-lib/lib/core/membership.js +17 -2
  100. package/dist-lib/lib/core/mesh.d.ts +5 -0
  101. package/dist-lib/lib/core/mesh.js +11 -5
  102. package/dist-lib/lib/core/migrations/agent-indexes.d.ts +2 -0
  103. package/dist-lib/lib/core/migrations/agent-indexes.js +30 -0
  104. package/dist-lib/lib/core/migrations/file-index.d.ts +2 -0
  105. package/dist-lib/lib/core/migrations/file-index.js +27 -0
  106. package/dist-lib/lib/core/migrations/index.js +3 -1
  107. package/dist-lib/lib/core/notification.js +5 -5
  108. package/dist-lib/lib/core/range.d.ts +18 -0
  109. package/dist-lib/lib/core/range.js +17 -0
  110. package/dist-lib/lib/core/sync/index.js +39 -32
  111. package/dist-lib/lib/core/sync-status.js +3 -2
  112. package/dist-lib/lib/core/tool-activity.d.ts +2 -5
  113. package/dist-lib/lib/core/tool-activity.js +4 -7
  114. package/dist-lib/lib/core.d.ts +48 -29
  115. package/dist-lib/lib/core.js +465 -359
  116. package/dist-lib/lib/knowledge-search.d.ts +2 -2
  117. package/dist-lib/lib/kv-cache.js +1 -1
  118. package/dist-lib/lib/log.js +2 -3
  119. package/dist-lib/lib/mesh-scoped-engine.d.ts +8 -1
  120. package/dist-lib/lib/mesh-scoped-engine.js +62 -15
  121. package/dist-lib/lib/model-download.d.ts +14 -5
  122. package/dist-lib/lib/model-download.js +25 -17
  123. package/dist-lib/lib/run-transcript.d.ts +2 -2
  124. package/dist-lib/lib/run-transcript.js +3 -2
  125. package/dist-lib/lib/task.d.ts +1 -0
  126. package/dist-lib/lib/task.js +3 -2
  127. package/dist-lib/schema/build.js +4 -10
  128. package/dist-lib/schema/engine.js +261 -91
  129. package/dist-lib/schema/helpers.d.ts +4 -0
  130. package/dist-lib/schema/helpers.js +20 -0
  131. package/dist-lib/schema/local.d.ts +1 -1
  132. package/dist-lib/schema/local.js +70 -2
  133. package/dist-lib/schema/qvac.js +30 -0
  134. package/dist-lib/spec/bind.js +20 -3
  135. package/dist-lib/spec/calls.js +19 -15
  136. package/dist-lib/spec/capabilities.d.ts +42 -34
  137. package/dist-lib/spec/hrpc/hrpc.json +82 -30
  138. package/dist-lib/spec/hrpc/index.js +106 -57
  139. package/dist-lib/spec/hrpc/messages.js +5420 -5019
  140. package/dist-lib/spec/hyperschema/index.js +5424 -5023
  141. package/dist-lib/spec/hyperschema/schema.json +3202 -2666
  142. package/dist-lib/spec/hyperschema/types.d.ts +455 -319
  143. package/dist-lib/spec/local/hyperdb/db.json +119 -1
  144. package/dist-lib/spec/local/hyperdb/index.js +414 -2
  145. package/dist-lib/spec/local/hyperdb/messages.js +505 -124
  146. package/dist-lib/spec/local/hyperschema/index.js +203 -5
  147. package/dist-lib/spec/local/hyperschema/schema.json +136 -1
  148. package/dist-lib/spec/local/hyperschema/types.d.ts +32 -1
  149. package/dist-lib/spec/mesh/hyperdb/db.json +55 -3
  150. package/dist-lib/spec/mesh/hyperdb/index.js +119 -3
  151. package/dist-lib/spec/mesh/hyperdb/messages.js +244 -172
  152. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
  153. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  154. package/dist-lib/spec/mesh/hyperdispatch/messages.js +74 -2
  155. package/dist-lib/spec/mesh/hyperschema/index.js +74 -2
  156. package/dist-lib/spec/mesh/hyperschema/schema.json +27 -1
  157. package/dist-lib/spec/mesh/hyperschema/types.d.ts +9 -1
  158. package/package.json +6 -6
  159. package/tui/app.mjs +376 -151
  160. package/tui/boot.mjs +2 -1
  161. package/tui/lib/busy-words.mjs +2 -1
  162. package/tui/lib/knowledge-command.mjs +11 -10
  163. package/tui/lib/knowledge-import.mjs +7 -5
  164. package/tui/lib/render.mjs +4 -3
  165. package/tui/lib/usage.mjs +2 -1
  166. package/tui/main.mjs +2 -1
@@ -1,16 +1,14 @@
1
- import type { AssistantCreateAgentRequest, AssistantUpdateAgentRequest } from '../../spec/hyperschema/types.d.ts';
1
+ import type { AssistantAgentConfig, AssistantAgentUpdateConfig } from '../../spec/hyperschema/types.d.ts';
2
2
  import type { QvacAgent } from '../../spec/mesh/hyperschema/types.d.ts';
3
3
  import type { Mesh } from './mesh.ts';
4
- type CreateAgentInput = AssistantCreateAgentRequest;
5
- type UpdateAgentInput = AssistantUpdateAgentRequest;
6
- export declare function listAgents(store: Mesh): Promise<QvacAgent[]>;
4
+ export declare function listAgents(store: Mesh, providerDeviceId?: Buffer): Promise<QvacAgent[]>;
5
+ export declare function getDefaultAgent(store: Mesh, providerDeviceId?: Buffer): Promise<QvacAgent | null>;
7
6
  export declare function getAgent(store: Mesh, id: string): Promise<QvacAgent | null>;
8
7
  export declare function isOrphaned(store: Mesh, agent: QvacAgent): Promise<boolean>;
9
8
  export declare function liveProviderIds(store: Mesh): Promise<Set<string>>;
10
9
  export declare function mustGetAgent(store: Mesh, id: string): Promise<QvacAgent>;
11
10
  export declare function mustGetActiveAgent(store: Mesh, id: string): Promise<QvacAgent>;
12
- export declare function createAgent(store: Mesh, input: CreateAgentInput): Promise<QvacAgent>;
11
+ export declare function createAgent(store: Mesh, input: AssistantAgentConfig): Promise<QvacAgent>;
13
12
  export declare function ownDefaultAgents(store: Mesh): Promise<QvacAgent[]>;
14
- export declare function updateAgent(store: Mesh, input: UpdateAgentInput): Promise<QvacAgent>;
13
+ export declare function updateAgent(store: Mesh, agentId: string, input: AssistantAgentUpdateConfig): Promise<QvacAgent>;
15
14
  export declare function deleteAgent(store: Mesh, deviceId: Buffer | null, id: string): Promise<QvacAgent>;
16
- export {};
@@ -1,8 +1,11 @@
1
1
  import { softDeleteChatsForAgent } from './chat.js';
2
2
  import { randomId } from './id.js';
3
3
  import { softDeleteToolConfigsForAgent } from './tool-configs.js';
4
+ import b4a from 'b4a';
4
5
  const AGENTS = '@qvac/agents';
5
6
  const ACTIVE_AGENTS = '@qvac/active-agents';
7
+ const AGENTS_BY_PROVIDER = '@qvac/agents-by-provider';
8
+ const DEFAULT_AGENTS = '@qvac/default-agents';
6
9
  const DEVICES = '@qvac/devices';
7
10
  const DEFAULT_ICON_NAME = 'artificial-intelligence';
8
11
  function mergeSkills(base, enabled) {
@@ -11,16 +14,33 @@ function mergeSkills(base, enabled) {
11
14
  function patch(next, current) {
12
15
  return next === null || next === undefined ? current : next;
13
16
  }
14
- export async function listAgents(store) {
17
+ export async function listAgents(store, providerDeviceId) {
15
18
  const snap = store.view.snapshot();
16
19
  try {
17
- const agents = await snap.find(ACTIVE_AGENTS).toArray();
18
- return agents;
20
+ if (providerDeviceId) {
21
+ return await snap
22
+ .find(AGENTS_BY_PROVIDER, providerRange(providerDeviceId))
23
+ .toArray();
24
+ }
25
+ return await snap.find(ACTIVE_AGENTS).toArray();
19
26
  }
20
27
  finally {
21
28
  await snap.close();
22
29
  }
23
30
  }
31
+ export async function getDefaultAgent(store, providerDeviceId) {
32
+ const snap = store.view.snapshot();
33
+ try {
34
+ const range = providerDeviceId ? providerRange(providerDeviceId) : {};
35
+ return await snap.findOne(DEFAULT_AGENTS, range);
36
+ }
37
+ finally {
38
+ await snap.close();
39
+ }
40
+ }
41
+ function providerRange(providerDeviceId) {
42
+ return { gte: { providerDeviceId }, lte: { providerDeviceId } };
43
+ }
24
44
  export async function getAgent(store, id) {
25
45
  const snap = store.view.snapshot();
26
46
  try {
@@ -47,7 +67,7 @@ export async function liveProviderIds(store) {
47
67
  try {
48
68
  for await (const device of snap.find(DEVICES)) {
49
69
  if (!device.revoked)
50
- live.add(device.id.toString('hex'));
70
+ live.add(b4a.toHex(device.id));
51
71
  }
52
72
  return live;
53
73
  }
@@ -124,8 +144,8 @@ export async function ownDefaultAgents(store) {
124
144
  await snap.close();
125
145
  }
126
146
  }
127
- export async function updateAgent(store, input) {
128
- const current = await mustGetActiveAgent(store, input.id);
147
+ export async function updateAgent(store, agentId, input) {
148
+ const current = await mustGetActiveAgent(store, agentId);
129
149
  if (input.clearIsDefault && current.isDefault) {
130
150
  const own = await ownDefaultAgents(store);
131
151
  if (own.length === 1 && own[0].id === current.id) {
@@ -131,6 +131,7 @@ async function migrateChunks(snap, from, into, chatId) {
131
131
  continue;
132
132
  batch.dispatch('append-chunk', { ...op, attachment: { ...attachment, blobId } });
133
133
  await batch.flush();
134
+ await into.fileIndex?.record([{ ...chunk, attachment: { ...attachment, blobId } }]);
134
135
  }
135
136
  await batch.flush();
136
137
  }
@@ -0,0 +1,20 @@
1
+ import { type Sidebar } from './chat.ts';
2
+ import type { openLocalStore } from './local/index.ts';
3
+ import type { Mesh } from './mesh.ts';
4
+ type LocalStore = ReturnType<typeof openLocalStore>;
5
+ export declare function chatSortKey(chat: {
6
+ rank?: number;
7
+ rankedAt?: number;
8
+ lastActivityAt: number;
9
+ }): number;
10
+ export declare function sortKeyBytes(value: number): Buffer;
11
+ export declare function refreshChatOrder(mesh: Mesh, local: LocalStore, chatIds: Iterable<string>): Promise<void>;
12
+ export declare function rebuildChatOrder(mesh: Mesh, local: LocalStore): Promise<void>;
13
+ export interface ChatPage extends Sidebar {
14
+ readonly nextCursor?: string;
15
+ }
16
+ export declare function listChatPage(mesh: Mesh, local: LocalStore, { limit, cursor }?: {
17
+ limit?: number;
18
+ cursor?: string;
19
+ }): Promise<ChatPage>;
20
+ export {};
@@ -0,0 +1,96 @@
1
+ import b4a from 'b4a';
2
+ import { enrichAll } from './chat.js';
3
+ import { listChatGroupsIn } from './chat-group.js';
4
+ const CHATS = '@qvac/chats';
5
+ const ACTIVE_CHATS = '@qvac/active-chats';
6
+ const CHUNK_COUNTERS = '@qvac/chunk-counters';
7
+ export function chatSortKey(chat) {
8
+ return chat.rank && (chat.rankedAt ?? 0) >= chat.lastActivityAt ? chat.rank : chat.lastActivityAt;
9
+ }
10
+ export function sortKeyBytes(value) {
11
+ const bytes = b4a.alloc(8);
12
+ new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).setFloat64(0, value);
13
+ return bytes;
14
+ }
15
+ export async function refreshChatOrder(mesh, local, chatIds) {
16
+ const snap = mesh.view.snapshot();
17
+ try {
18
+ const rows = [];
19
+ const gone = [];
20
+ for (const chatId of chatIds) {
21
+ const chat = await snap.get(CHATS, { id: chatId });
22
+ if (!chat || chat.deletedAt) {
23
+ gone.push(chatId);
24
+ continue;
25
+ }
26
+ rows.push({ chatId, sortKey: await sortKeyOf(snap, chat) });
27
+ }
28
+ await local.writeChatOrder(mesh.key, rows, gone);
29
+ }
30
+ finally {
31
+ await snap.close();
32
+ }
33
+ }
34
+ export async function rebuildChatOrder(mesh, local) {
35
+ const snap = mesh.view.snapshot();
36
+ try {
37
+ const rows = [];
38
+ for (const chat of await snap.find(ACTIVE_CHATS).toArray()) {
39
+ rows.push({ chatId: chat.id, sortKey: await sortKeyOf(snap, chat) });
40
+ }
41
+ const live = new Set(rows.map((row) => row.chatId));
42
+ const stored = await local.listChatOrder(mesh.key);
43
+ const gone = stored.map((row) => row.chatId).filter((chatId) => !live.has(chatId));
44
+ await local.writeChatOrder(mesh.key, rows, gone);
45
+ }
46
+ finally {
47
+ await snap.close();
48
+ }
49
+ }
50
+ async function sortKeyOf(snap, chat) {
51
+ const counter = await snap.get(CHUNK_COUNTERS, { chatId: chat.id });
52
+ return sortKeyBytes(chatSortKey({ ...chat, lastActivityAt: counter?.lastActivityAt || chat.createdAt }));
53
+ }
54
+ export async function listChatPage(mesh, local, { limit, cursor } = {}) {
55
+ await mesh.chatOrderBuilt;
56
+ const before = cursor === undefined ? undefined : decodeCursor(cursor);
57
+ const ordered = await local.listChatOrder(mesh.key, { limit, before });
58
+ const snap = mesh.view.snapshot();
59
+ try {
60
+ const rows = [];
61
+ const activity = new Map();
62
+ for (const entry of ordered) {
63
+ const chat = await snap.get(CHATS, { id: entry.chatId });
64
+ if (!chat || chat.deletedAt) {
65
+ continue;
66
+ }
67
+ const counter = await snap.get(CHUNK_COUNTERS, { chatId: chat.id });
68
+ activity.set(chat.id, counter?.lastActivityAt || chat.createdAt);
69
+ rows.push(chat);
70
+ }
71
+ const groups = await listChatGroupsIn(snap);
72
+ const live = new Set(groups.map((group) => group.id));
73
+ return {
74
+ chats: enrichAll(rows, activity, live).reverse(),
75
+ groups,
76
+ nextCursor: nextCursorOf(ordered, limit)
77
+ };
78
+ }
79
+ finally {
80
+ await snap.close();
81
+ }
82
+ }
83
+ function nextCursorOf(ordered, limit) {
84
+ if (limit === undefined || ordered.length < limit) {
85
+ return undefined;
86
+ }
87
+ const last = ordered[ordered.length - 1];
88
+ return encodeCursor(last.sortKey, last.chatId);
89
+ }
90
+ function encodeCursor(sortKey, chatId) {
91
+ return b4a.toString(b4a.concat([sortKey, b4a.from(chatId)]), 'base64');
92
+ }
93
+ function decodeCursor(cursor) {
94
+ const raw = b4a.from(cursor, 'base64');
95
+ return { sortKey: raw.subarray(0, 8), chatId: b4a.toString(raw.subarray(8)) };
96
+ }
@@ -8,11 +8,6 @@ export interface Sidebar {
8
8
  readonly chats: ChatListing[];
9
9
  readonly groups: QvacChatGroup[];
10
10
  }
11
- export declare function chatSortKey(chat: {
12
- rank?: number;
13
- rankedAt?: number;
14
- lastActivityAt: number;
15
- }): number;
16
11
  export declare function liveGroupIds(snap: HyperDB): Promise<Set<string>>;
17
12
  export declare function lastActivity(snap: HyperDB): Promise<Map<string, number>>;
18
13
  export declare function enrichChat(store: Mesh, chat: QvacChat): Promise<ChatListing>;
@@ -3,6 +3,7 @@ import { appendChunk, listChunks, updateOp } from './chunk.js';
3
3
  import { randomId } from './id.js';
4
4
  import { assertRemovableAt } from './files/index.js';
5
5
  import { place } from './rank.js';
6
+ import { chatSortKey } from './chat-order.js';
6
7
  const CHATS = '@qvac/chats';
7
8
  const CHAT_GROUPS = '@qvac/chat-groups';
8
9
  const ACTIVE_CHATS = '@qvac/active-chats';
@@ -10,9 +11,6 @@ const ACTIVE_CHATS_BY_AGENT = '@qvac/active-chats-by-agent';
10
11
  const ACTIVE_CHAT_GROUPS = '@qvac/active-chat-groups';
11
12
  const CHUNK_COUNTERS = '@qvac/chunk-counters';
12
13
  const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
13
- export function chatSortKey(chat) {
14
- return chat.rank && (chat.rankedAt ?? 0) >= chat.lastActivityAt ? chat.rank : chat.lastActivityAt;
15
- }
16
14
  function byChatOrder(a, b) {
17
15
  return chatSortKey(a) - chatSortKey(b) || (a.id < b.id ? -1 : 1);
18
16
  }
@@ -1,6 +1,7 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { QvacChunk } from '../../spec/mesh/hyperschema/types.d.ts';
3
3
  import type { Mesh, MeshDispatcher } from './mesh.ts';
4
+ import { type Bounds } from './range.ts';
4
5
  type ChunkPayload = Pick<QvacChunk, 'userMessage' | 'content' | 'thinking' | 'toolCall' | 'toolResult' | 'attachment' | 'approvalRequest' | 'runStatus' | 'runRequest' | 'sources' | 'compaction'>;
5
6
  type AppendChunkInput = Pick<QvacChunk, 'chatId' | 'runId' | 'type' | 'originDeviceId' | 'parentId' | 'effect' | 'intent' | 'display' | 'rawCall'> & ChunkPayload & {
6
7
  id?: string;
@@ -9,32 +10,24 @@ type UpdateChunkInput = Pick<QvacChunk, 'id' | 'runStatus' | 'attachment'>;
9
10
  export declare const HISTORY_CHUNK_TYPES: QvacChunk['type'][];
10
11
  type MediaKind = 'audio' | 'image' | 'video' | 'other';
11
12
  type AttachmentOrigin = 'uploaded' | 'generated';
12
- type ListAttachmentsByKindInput = {
13
+ type ListAttachmentsByKindInput = Bounds & {
13
14
  kind: MediaKind;
14
15
  origin?: AttachmentOrigin;
15
- fromCreatedAt?: number;
16
- reverse?: boolean;
17
- limit?: number;
18
16
  };
19
17
  export type ReadChunk = QvacChunk & {
20
18
  readonly attachment?: (NonNullable<QvacChunk['attachment']> & {
21
19
  readonly url?: string;
22
20
  }) | null;
23
21
  };
24
- type ListChunksInput = {
22
+ export declare function recordLinks(store: Mesh, chunks: ReadChunk[]): Promise<ReadChunk[]>;
23
+ type ListChunksInput = Bounds & {
25
24
  chatId: string;
26
25
  type?: QvacChunk['type'];
27
26
  types?: ReadonlyArray<QvacChunk['type']>;
28
- fromSeq?: number;
29
- reverse?: boolean;
30
- limit?: number;
31
27
  };
32
- export declare function findChunksOfType(snap: HyperDB, chatId: string, type: QvacChunk['type'], { fromSeq, toSeq }?: {
33
- fromSeq?: number;
34
- toSeq?: number;
35
- }): Promise<QvacChunk[]>;
36
- export declare function listChunks(store: Mesh, { chatId, type, types, fromSeq, reverse, limit }: ListChunksInput): Promise<ReadChunk[]>;
37
- export declare function listAttachmentsByKind(store: Mesh, { kind, origin, fromCreatedAt, reverse, limit }: ListAttachmentsByKindInput): Promise<ReadChunk[]>;
28
+ export declare function findChunksOfType(snap: HyperDB, chatId: string, type: QvacChunk['type'], bounds?: Bounds): Promise<QvacChunk[]>;
29
+ export declare function listChunks(store: Mesh, { chatId, type, types, ...bounds }: ListChunksInput): Promise<ReadChunk[]>;
30
+ export declare function listAttachmentsByKind(store: Mesh, { kind, origin, ...bounds }: ListAttachmentsByKindInput): Promise<ReadChunk[]>;
38
31
  export declare function getChunk(store: Mesh, id: string): Promise<QvacChunk | null>;
39
32
  export declare function appendChunk(store: Mesh, { id, chatId, runId, type, originDeviceId, parentId, effect, intent, display, rawCall, ...payload }: AppendChunkInput, into?: MeshDispatcher): Promise<{
40
33
  id: string;
@@ -3,6 +3,7 @@ import { chunkTypeOrdinal, mediaKindOrdinal } from '../../schema/helpers.js';
3
3
  import { putApprovalRule } from './approval-rules.js';
4
4
  import { linkAttachment } from './attachment-link.js';
5
5
  import { randomId } from './id.js';
6
+ import { keyRange } from './range.js';
6
7
  const CHUNKS = '@qvac/chunks';
7
8
  const CHUNKS_BY_CHAT = '@qvac/chunks-by-chat';
8
9
  const CHUNKS_BY_TYPE = '@qvac/chunks-by-type';
@@ -22,44 +23,34 @@ function linkChunks(store, chunks) {
22
23
  return chunks;
23
24
  return chunks.map((chunk) => chunk.attachment ? { ...chunk, attachment: linkAttachment(mint, chunk.attachment) } : chunk);
24
25
  }
25
- export function findChunksOfType(snap, chatId, type, { fromSeq, toSeq } = {}) {
26
- const ordinal = chunkTypeOrdinal(type);
26
+ export async function recordLinks(store, chunks) {
27
+ await store.fileIndex?.record(chunks.filter((chunk) => chunk.attachment?.url));
28
+ return chunks;
29
+ }
30
+ export function findChunksOfType(snap, chatId, type, bounds = {}) {
31
+ const prefix = { chatId, type: chunkTypeOrdinal(type) };
27
32
  return snap
28
- .find(CHUNKS_BY_TYPE, {
29
- gte: fromSeq === undefined ? { chatId, type: ordinal } : { chatId, type: ordinal, seq: fromSeq },
30
- lte: toSeq === undefined ? { chatId, type: ordinal } : { chatId, type: ordinal, seq: toSeq }
31
- })
33
+ .find(CHUNKS_BY_TYPE, keyRange(prefix, 'seq', bounds))
32
34
  .toArray();
33
35
  }
34
- export async function listChunks(store, { chatId, type, types, fromSeq, reverse, limit }) {
36
+ export async function listChunks(store, { chatId, type, types, ...bounds }) {
35
37
  const selected = selectedChunkTypes(type, types);
36
38
  if (selected?.length === 0)
37
39
  return [];
38
- const options = { reverse: reverse === true, limit: limit || undefined };
39
40
  if (selected && selected.length > 1 && !isHistorySelection(selected)) {
40
41
  throw new Error('multiple chunk types require a mapped index');
41
42
  }
42
43
  const snap = store.view.snapshot();
43
44
  try {
44
45
  if (selected?.length === 1) {
45
- const selectedType = chunkTypeOrdinal(selected[0]);
46
+ const prefix = { chatId, type: chunkTypeOrdinal(selected[0]) };
46
47
  const chunks = await snap
47
- .find(CHUNKS_BY_TYPE, {
48
- gte: fromSeq === undefined
49
- ? { chatId, type: selectedType }
50
- : { chatId, type: selectedType, seq: fromSeq },
51
- lte: { chatId, type: selectedType },
52
- ...options
53
- })
48
+ .find(CHUNKS_BY_TYPE, keyRange(prefix, 'seq', bounds))
54
49
  .toArray();
55
50
  return linkChunks(store, chunks);
56
51
  }
57
52
  const chunks = await snap
58
- .find(selected ? HISTORY_CHUNKS : CHUNKS_BY_CHAT, {
59
- gte: fromSeq === undefined ? { chatId } : { chatId, seq: fromSeq },
60
- lte: { chatId },
61
- ...options
62
- })
53
+ .find(selected ? HISTORY_CHUNKS : CHUNKS_BY_CHAT, keyRange({ chatId }, 'seq', bounds))
63
54
  .toArray();
64
55
  return linkChunks(store, chunks);
65
56
  }
@@ -67,30 +58,19 @@ export async function listChunks(store, { chatId, type, types, fromSeq, reverse,
67
58
  await snap.close();
68
59
  }
69
60
  }
70
- export async function listAttachmentsByKind(store, { kind, origin, fromCreatedAt, reverse, limit }) {
61
+ export async function listAttachmentsByKind(store, { kind, origin, ...bounds }) {
71
62
  const mediaKind = mediaKindOrdinal(kind);
72
- const options = { reverse: reverse === true, limit: limit || undefined };
73
63
  const snap = store.view.snapshot();
74
64
  try {
75
65
  if (origin === undefined) {
76
66
  const chunks = await snap
77
- .find(ATTACHMENTS_BY_MEDIA_KIND, {
78
- gte: fromCreatedAt === undefined ? { mediaKind } : { mediaKind, createdAt: fromCreatedAt },
79
- lte: { mediaKind },
80
- ...options
81
- })
67
+ .find(ATTACHMENTS_BY_MEDIA_KIND, keyRange({ mediaKind }, 'createdAt', bounds))
82
68
  .toArray();
83
69
  return linkChunks(store, chunks);
84
70
  }
85
71
  const generated = origin === 'generated' ? 1 : 0;
86
72
  const chunks = await snap
87
- .find(ATTACHMENTS_BY_MEDIA_KIND_ORIGIN, {
88
- gte: fromCreatedAt === undefined
89
- ? { mediaKind, generated }
90
- : { mediaKind, generated, createdAt: fromCreatedAt },
91
- lte: { mediaKind, generated },
92
- ...options
93
- })
73
+ .find(ATTACHMENTS_BY_MEDIA_KIND_ORIGIN, keyRange({ mediaKind, generated }, 'createdAt', bounds))
94
74
  .toArray();
95
75
  return linkChunks(store, chunks);
96
76
  }
@@ -170,6 +150,8 @@ export async function appendChunk(store, { id = randomId(), chatId, runId, type,
170
150
  ...payload
171
151
  };
172
152
  await into.dispatch('append-chunk', op);
153
+ if (type === 'attachment')
154
+ await store.fileIndex?.record([op]);
173
155
  return { id };
174
156
  }
175
157
  export async function updateChunk(store, { id, runStatus, attachment }) {
@@ -11,6 +11,14 @@ export function registerAgentRoutes(router) {
11
11
  log?.debug('contract', existing ? 'agent updated' : 'agent created', { id: agent.id });
12
12
  return agent;
13
13
  }));
14
+ router.add('@qvac/reindex-agents', async ({ agentIds }, { tx }) => {
15
+ for (const id of agentIds ?? []) {
16
+ const agent = await tx.get(AGENTS, { id });
17
+ if (agent) {
18
+ await tx.insert(AGENTS, { ...agent, version: undefined });
19
+ }
20
+ }
21
+ });
14
22
  router.add('@qvac/migrate-agent-catalog', trackActivity(async ({ agent: healthAssistant }, { tx, log }) => {
15
23
  if (await tx.get(AGENTS, { id: healthAssistant.id }))
16
24
  return false;
@@ -1,5 +1,5 @@
1
1
  import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
2
- declare const DROPPED: readonly ['@qvac/append-chunk', '@qvac/update-chunk', '@qvac/resolve-approval', '@qvac/put-agent', '@qvac/put-approval-rule', '@qvac/seed-defaults', '@qvac/add-knowledge', '@qvac/remove-knowledge', '@qvac/add-knowledge-artifact', '@qvac/claim-indexing', '@qvac/resolve-indexing', '@qvac/put-chat', '@qvac/put-tool-configs', '@qvac/append-tool-activity', '@qvac/migrate-agent-catalog', '@qvac/put-chat-group', '@qvac/put-joined-mesh'];
2
+ declare const DROPPED: readonly ['@qvac/append-chunk', '@qvac/update-chunk', '@qvac/resolve-approval', '@qvac/put-agent', '@qvac/put-approval-rule', '@qvac/seed-defaults', '@qvac/add-knowledge', '@qvac/remove-knowledge', '@qvac/add-knowledge-artifact', '@qvac/claim-indexing', '@qvac/resolve-indexing', '@qvac/put-chat', '@qvac/put-tool-configs', '@qvac/append-tool-activity', '@qvac/migrate-agent-catalog', '@qvac/reindex-agents', '@qvac/put-chat-group', '@qvac/put-joined-mesh'];
3
3
  export type DroppedComputeRoute = (typeof DROPPED)[number];
4
4
  export declare function droppedComputeRoutes(): ReadonlyArray<DroppedComputeRoute>;
5
5
  export declare class ComputeRouteDropped extends Error {
@@ -14,6 +14,7 @@ const DROPPED = [
14
14
  '@qvac/put-tool-configs',
15
15
  '@qvac/append-tool-activity',
16
16
  '@qvac/migrate-agent-catalog',
17
+ '@qvac/reindex-agents',
17
18
  '@qvac/put-chat-group',
18
19
  '@qvac/put-joined-mesh'
19
20
  ];
@@ -1,18 +1,19 @@
1
1
  import { liveDeviceForWriter, trackActivity } from './activity.js';
2
+ import b4a from 'b4a';
2
3
  const JOINED_MESHES = '@qvac/joined-meshes';
3
4
  export function registerJoinedMeshRoutes(router) {
4
5
  router.add('@qvac/put-joined-mesh', trackActivity(async ({ room }, { tx, node, log }) => {
5
6
  const author = await liveDeviceForWriter(tx, node.key);
6
7
  if (!author?.memberId?.equals(room.memberId)) {
7
8
  log?.warn('contract', 'put-joined-mesh refused: author is not a device of that member', {
8
- meshKey: room.meshKey.toString('hex').slice(0, 8)
9
+ meshKey: b4a.toHex(room.meshKey).slice(0, 8)
9
10
  });
10
11
  return;
11
12
  }
12
13
  const existing = await tx.get(JOINED_MESHES, { meshKey: room.meshKey });
13
14
  if (existing && existing.joinedAt > room.joinedAt) {
14
15
  log?.debug('contract', 'put-joined-mesh dropped: stale joinedAt', {
15
- meshKey: room.meshKey.toString('hex').slice(0, 8)
16
+ meshKey: b4a.toHex(room.meshKey).slice(0, 8)
16
17
  });
17
18
  return;
18
19
  }
@@ -20,7 +21,7 @@ export function registerJoinedMeshRoutes(router) {
20
21
  const next = existing ? merge(room, existing, rejoin) : room;
21
22
  await tx.insert(JOINED_MESHES, next);
22
23
  log?.debug('contract', next.leftAt ? 'joined room released' : 'joined room recorded', {
23
- meshKey: room.meshKey.toString('hex').slice(0, 8)
24
+ meshKey: b4a.toHex(room.meshKey).slice(0, 8)
24
25
  });
25
26
  return next;
26
27
  }));
@@ -1,9 +1,10 @@
1
1
  import { bareDeviceProof, claimAttestation, verifyDeviceProof, verifyMemberClaim } from '../identity.js';
2
2
  import { liveDeviceForWriter, trackActivity } from './activity.js';
3
+ import b4a from 'b4a';
3
4
  const DEVICES = '@qvac/devices';
4
5
  const MEMBERS = '@qvac/members';
5
6
  function hex(buffer) {
6
- return buffer.toString('hex').slice(0, 16);
7
+ return b4a.toHex(buffer).slice(0, 16);
7
8
  }
8
9
  function link(device, memberId, proof, attestedAt) {
9
10
  return {
@@ -42,7 +43,7 @@ export function registerMemberRoutes(router) {
42
43
  };
43
44
  await tx.insert(MEMBERS, minted);
44
45
  await tx.insert(DEVICES, link(minter, member.id, proof, member.joinedAt));
45
- log?.info('contract', 'member minted', { id: member.id.toString('hex').slice(0, 16) });
46
+ log?.info('contract', 'member minted', { id: b4a.toHex(member.id).slice(0, 16) });
46
47
  return minted;
47
48
  }));
48
49
  router.add('@qvac/update-member', trackActivity(async ({ member }, { tx, node, log }) => {
@@ -156,7 +157,7 @@ export function registerMemberRoutes(router) {
156
157
  }
157
158
  await tx.insert(DEVICES, link(device, op.memberId, op.proof, op.attestedAt));
158
159
  log?.info('contract', 'device attested to a member', {
159
- id: op.deviceId.toString('hex').slice(0, 16)
160
+ id: b4a.toHex(op.deviceId).slice(0, 16)
160
161
  });
161
162
  });
162
163
  }
@@ -2,6 +2,7 @@ import crypto from 'hypercore-crypto';
2
2
  import { markWriterRevoked, trackActivity } from './activity.js';
3
3
  import { verifyWriterClaim } from '../device-attestation.js';
4
4
  import { sealed } from './seal.js';
5
+ import b4a from 'b4a';
5
6
  const DEVICES = '@qvac/devices';
6
7
  async function admitDevice({ tx, node, meshPublicKey, log }, id, device, writerKey, claim) {
7
8
  const existing = await tx.get(DEVICES, { id });
@@ -29,13 +30,13 @@ async function admitDevice({ tx, node, meshPublicKey, log }, id, device, writerK
29
30
  }
30
31
  if (alreadyAdmitted && !node.key.equals(writerKey)) {
31
32
  log?.warn('contract', 'admission refused: another writer may not rewrite a live device row', {
32
- id: id.toString('hex').slice(0, 16)
33
+ id: b4a.toHex(id).slice(0, 16)
33
34
  });
34
35
  return 'refused';
35
36
  }
36
37
  if (!signed && (rebinding || (await sealed(tx)))) {
37
38
  log?.warn('contract', 'admission refused: the device did not sign for this writer key', {
38
- id: id.toString('hex').slice(0, 16)
39
+ id: b4a.toHex(id).slice(0, 16)
39
40
  });
40
41
  return 'refused';
41
42
  }
@@ -67,7 +68,7 @@ async function admitDevice({ tx, node, meshPublicKey, log }, id, device, writerK
67
68
  await markWriterRevoked(tx, bound, true);
68
69
  await markWriterRevoked(tx, writerKey, false);
69
70
  if (!alreadyAdmitted) {
70
- log?.info('contract', 'device admitted', { id: id.toString('hex').slice(0, 16) });
71
+ log?.info('contract', 'device admitted', { id: b4a.toHex(id).slice(0, 16) });
71
72
  }
72
73
  return 'admitted';
73
74
  }
@@ -94,7 +95,7 @@ async function revokeWriter(payload, context) {
94
95
  await context.tx.insert(DEVICES, revoked);
95
96
  await markWriterRevoked(context.tx, payload.writerKey, true);
96
97
  context.log?.info('contract', 'writer removed, device revoked', {
97
- id: id.toString('hex').slice(0, 16)
98
+ id: b4a.toHex(id).slice(0, 16)
98
99
  });
99
100
  if (context.node.key.equals(payload.writerKey))
100
101
  return;
@@ -1,5 +1,6 @@
1
1
  import ScopeLock from 'scope-lock';
2
2
  import { Mesh } from './mesh.js';
3
+ import b4a from 'b4a';
3
4
  const SCOPE = ['p2p', 'departure'];
4
5
  export class PendingDepartures {
5
6
  _store;
@@ -35,7 +36,7 @@ export class PendingDepartures {
35
36
  for (const row of await this._local.departures()) {
36
37
  if (this._closing)
37
38
  return;
38
- const hex = row.key.toString('hex');
39
+ const hex = b4a.toHex(row.key);
39
40
  if (this._isPooled(row.key) || this._parked.has(hex) || this._active.has(hex)) {
40
41
  continue;
41
42
  }
@@ -59,7 +60,7 @@ export class PendingDepartures {
59
60
  }
60
61
  }
61
62
  async _deliver(row, mesh) {
62
- const scope = [...SCOPE, row.key.toString('hex').slice(0, 8)];
63
+ const scope = [...SCOPE, b4a.toHex(row.key).slice(0, 8)];
63
64
  try {
64
65
  await mesh.ready();
65
66
  if (this._closing || mesh.closing)
@@ -78,7 +79,7 @@ export class PendingDepartures {
78
79
  }
79
80
  }
80
81
  async park(key) {
81
- const hex = key.toString('hex');
82
+ const hex = b4a.toHex(key);
82
83
  await this._lock.lock();
83
84
  try {
84
85
  this._parked.add(hex);
@@ -93,7 +94,7 @@ export class PendingDepartures {
93
94
  }
94
95
  }
95
96
  unpark(key) {
96
- this._parked.delete(key.toString('hex'));
97
+ this._parked.delete(b4a.toHex(key));
97
98
  }
98
99
  drop(key) {
99
100
  return this._local.deleteDeparture(key);
@@ -1,6 +1,7 @@
1
1
  import crypto from 'hypercore-crypto';
2
2
  import ScopeLock from 'scope-lock';
3
3
  import { deviceInviteToken } from './identity.js';
4
+ import b4a from 'b4a';
4
5
  const NONCES_KEY = 'device-invite-nonces';
5
6
  export function openDeviceInvites(store) {
6
7
  const lock = new ScopeLock();
@@ -13,7 +14,7 @@ export function openDeviceInvites(store) {
13
14
  const nonce = crypto.randomBytes(32);
14
15
  await lock.lock();
15
16
  try {
16
- await store.set(NONCES_KEY, [...(await nonces()), nonce.toString('hex')]);
17
+ await store.set(NONCES_KEY, [...(await nonces()), b4a.toHex(nonce)]);
17
18
  }
18
19
  finally {
19
20
  lock.unlock();
@@ -1,8 +1,15 @@
1
1
  import type { Mesh } from '../mesh.ts';
2
2
  import type { CapabilityHandlers } from '../../../spec/capabilities.d.ts';
3
3
  export declare const MAX_ATTACHMENT_BYTES: number;
4
+ type Bound<K extends keyof CapabilityHandlers> = CapabilityHandlers[K] extends (input: infer I, ...rest: infer R) => infer O ? (input: Omit<I, 'meshId'>, ...rest: R) => O : CapabilityHandlers[K];
5
+ export interface BoundAttachments {
6
+ uploadAttachment: CapabilityHandlers['uploadAttachment'];
7
+ downloadAttachment: Bound<'downloadAttachment'>;
8
+ deleteAttachment: Bound<'deleteAttachment'>;
9
+ }
4
10
  export declare function attachmentFiles(mesh: Mesh, { maxBytes, deviceId, inactivityMs }?: {
5
11
  maxBytes?: number;
6
12
  deviceId?: Buffer | null;
7
13
  inactivityMs?: number;
8
- }): Pick<CapabilityHandlers, 'uploadAttachment' | 'downloadAttachment' | 'deleteAttachment'>;
14
+ }): BoundAttachments;
15
+ export {};
@@ -32,7 +32,7 @@ export declare class Files extends EventEmitter {
32
32
  data: Buffer;
33
33
  bytesRead: number;
34
34
  byteLength: number;
35
- }, any, any>;
35
+ }, void, unknown>;
36
36
  readBytes(reference: string, options?: Parameters<BlobStore['readBytes']>[1]): Promise<Buffer<ArrayBufferLike>>;
37
37
  download(reference: string, options: Parameters<BlobStore['download']>[1]): Promise<void>;
38
38
  write(chunks: AsyncIterable<Uint8Array>, options?: Parameters<BlobStore['write']>[1]): AsyncGenerator<import("./store/blob.ts").WriteProgress, any, any>;
@@ -42,8 +42,8 @@ export declare class Files extends EventEmitter {
42
42
  }>;
43
43
  inline(bytes: Buffer, mimeType: string): string;
44
44
  reference(id: Parameters<BlobStore['reference']>[0]): string;
45
- clear(reference: string, id: string): Promise<void>;
46
- attachments(options?: Parameters<typeof attachmentFiles>[1]): Pick<import("../../../spec/capabilities").CapabilityHandlers, "deleteAttachment" | "downloadAttachment" | "uploadAttachment">;
45
+ clear(reference: string, id: string): Promise<boolean>;
46
+ attachments(options?: Parameters<typeof attachmentFiles>[1]): import("./attachments.ts").BoundAttachments;
47
47
  knowledge(options?: Parameters<typeof knowledgeFiles>[1]): Pick<import("../../../spec/capabilities").CapabilityHandlers, "downloadKnowledge" | "listIndexing" | "listKnowledge" | "removeKnowledge" | "uploadKnowledge">;
48
48
  listKnowledge(chatId: string): Promise<QvacKnowledge[]>;
49
49
  record(id: string): Promise<QvacKnowledge | null>;
@@ -51,7 +51,7 @@ export declare class Files extends EventEmitter {
51
51
  chatId: string;
52
52
  fileId: string;
53
53
  }): Promise<FileRecord | null>;
54
- fileRead(request: AssistantFileReadRequest, options?: {
54
+ fileRead(request: Omit<AssistantFileReadRequest, 'meshId'>, options?: {
55
55
  signal?: AbortSignal;
56
56
  }): AsyncGenerator<AssistantFileReadFrame>;
57
57
  preview(request: {