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

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 (195) hide show
  1. package/CHANGELOG.md +158 -2
  2. package/dist-lib/index.d.ts +1 -0
  3. package/dist-lib/index.js +1 -0
  4. package/dist-lib/lib/api/chunk-delta.d.ts +7 -2
  5. package/dist-lib/lib/api/chunk-delta.js +89 -7
  6. package/dist-lib/lib/api/chunks.js +14 -8
  7. package/dist-lib/lib/api/config.d.ts +4 -1
  8. package/dist-lib/lib/api/config.js +45 -4
  9. package/dist-lib/lib/api/custom-models.js +3 -5
  10. package/dist-lib/lib/api/devices.js +3 -1
  11. package/dist-lib/lib/api/files.d.ts +1 -1
  12. package/dist-lib/lib/api/files.js +65 -8
  13. package/dist-lib/lib/api/instrument.js +12 -4
  14. package/dist-lib/lib/api/members.js +7 -13
  15. package/dist-lib/lib/api/mesh.d.ts +1 -3
  16. package/dist-lib/lib/api/mesh.js +2 -2
  17. package/dist-lib/lib/api/notifications.js +8 -4
  18. package/dist-lib/lib/api/projects.js +5 -1
  19. package/dist-lib/lib/api/rag.js +32 -19
  20. package/dist-lib/lib/api/served.js +2 -1
  21. package/dist-lib/lib/api/workspace.js +0 -1
  22. package/dist-lib/lib/assistant/harness.d.ts +8 -2
  23. package/dist-lib/lib/assistant/harness.js +17 -4
  24. package/dist-lib/lib/assistant/history.d.ts +1 -1
  25. package/dist-lib/lib/assistant/history.js +19 -13
  26. package/dist-lib/lib/assistant/message.d.ts +6 -1
  27. package/dist-lib/lib/assistant/message.js +22 -2
  28. package/dist-lib/lib/assistant/model-round.d.ts +5 -0
  29. package/dist-lib/lib/assistant/model-round.js +46 -18
  30. package/dist-lib/lib/assistant/pdf-budget.d.ts +10 -2
  31. package/dist-lib/lib/assistant/pdf-budget.js +10 -11
  32. package/dist-lib/lib/assistant/pdf-pages.d.ts +25 -0
  33. package/dist-lib/lib/assistant/pdf-pages.js +75 -0
  34. package/dist-lib/lib/assistant/round-host.js +5 -3
  35. package/dist-lib/lib/assistant-client.d.ts +2 -1
  36. package/dist-lib/lib/assistant-client.js +24 -6
  37. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  38. package/dist-lib/lib/assistant-tree.js +8 -5
  39. package/dist-lib/lib/assistant.d.ts +20 -7
  40. package/dist-lib/lib/assistant.js +468 -240
  41. package/dist-lib/lib/client.js +10 -1
  42. package/dist-lib/lib/compaction.d.ts +3 -1
  43. package/dist-lib/lib/compaction.js +18 -4
  44. package/dist-lib/lib/constants.d.ts +3 -0
  45. package/dist-lib/lib/constants.js +3 -0
  46. package/dist-lib/lib/core/chat-activity.js +13 -16
  47. package/dist-lib/lib/core/chat-copy.js +1 -1
  48. package/dist-lib/lib/core/chat-fork.js +2 -1
  49. package/dist-lib/lib/core/chat-move.d.ts +4 -0
  50. package/dist-lib/lib/core/chat-move.js +42 -11
  51. package/dist-lib/lib/core/chat.d.ts +0 -1
  52. package/dist-lib/lib/core/chat.js +1 -1
  53. package/dist-lib/lib/core/chunk.d.ts +14 -3
  54. package/dist-lib/lib/core/chunk.js +112 -22
  55. package/dist-lib/lib/core/collections.d.ts +12 -1
  56. package/dist-lib/lib/core/collections.js +12 -1
  57. package/dist-lib/lib/core/config-value.d.ts +6 -0
  58. package/dist-lib/lib/core/config-value.js +14 -0
  59. package/dist-lib/lib/core/contract/activity.d.ts +3 -2
  60. package/dist-lib/lib/core/contract/activity.js +39 -21
  61. package/dist-lib/lib/core/contract/agent-join.js +15 -19
  62. package/dist-lib/lib/core/contract/agents.js +8 -7
  63. package/dist-lib/lib/core/contract/chat.d.ts +3 -0
  64. package/dist-lib/lib/core/contract/chat.js +12 -1
  65. package/dist-lib/lib/core/contract/chunks.js +16 -6
  66. package/dist-lib/lib/core/contract/devices.js +6 -4
  67. package/dist-lib/lib/core/contract/index.js +2 -1
  68. package/dist-lib/lib/core/contract/invites.js +12 -31
  69. package/dist-lib/lib/core/contract/joined-meshes.js +2 -1
  70. package/dist-lib/lib/core/contract/mesh-rules.js +4 -0
  71. package/dist-lib/lib/core/contract/model-requests.js +7 -6
  72. package/dist-lib/lib/core/contract/project-device-keys.js +15 -6
  73. package/dist-lib/lib/core/contract/project-host-keys.js +5 -4
  74. package/dist-lib/lib/core/contract/projects.js +12 -13
  75. package/dist-lib/lib/core/contract/public-seeds.js +5 -5
  76. package/dist-lib/lib/core/contract/roles.js +2 -1
  77. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -1
  78. package/dist-lib/lib/core/contract/run-requests.js +19 -13
  79. package/dist-lib/lib/core/contract/shipped-rows.d.ts +2 -0
  80. package/dist-lib/lib/core/contract/shipped-rows.js +22 -0
  81. package/dist-lib/lib/core/contract/workspace.js +4 -10
  82. package/dist-lib/lib/core/contract/writers.js +20 -6
  83. package/dist-lib/lib/core/devices.d.ts +2 -1
  84. package/dist-lib/lib/core/devices.js +4 -2
  85. package/dist-lib/lib/core/document-attachment.d.ts +6 -3
  86. package/dist-lib/lib/core/document-attachment.js +10 -5
  87. package/dist-lib/lib/core/files/attachments.js +3 -3
  88. package/dist-lib/lib/core/files/index.d.ts +8 -5
  89. package/dist-lib/lib/core/files/index.js +109 -18
  90. package/dist-lib/lib/core/files/processing/index.d.ts +1 -1
  91. package/dist-lib/lib/core/files/processing/index.js +8 -2
  92. package/dist-lib/lib/core/files/processing/source.d.ts +1 -0
  93. package/dist-lib/lib/core/files/processing/source.js +3 -0
  94. package/dist-lib/lib/core/files/processing/state.d.ts +1 -0
  95. package/dist-lib/lib/core/files/processing/state.js +7 -0
  96. package/dist-lib/lib/core/files/rag/build.d.ts +1 -0
  97. package/dist-lib/lib/core/files/rag/build.js +14 -1
  98. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  99. package/dist-lib/lib/core/files/rag/index.js +1 -1
  100. package/dist-lib/lib/core/files/rag/produce.js +1 -0
  101. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +2 -0
  102. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +7 -1
  103. package/dist-lib/lib/core/first-event.d.ts +6 -0
  104. package/dist-lib/lib/core/first-event.js +13 -0
  105. package/dist-lib/lib/core/invite.d.ts +2 -2
  106. package/dist-lib/lib/core/invite.js +5 -4
  107. package/dist-lib/lib/core/local/index.d.ts +10 -1
  108. package/dist-lib/lib/core/local/index.js +105 -10
  109. package/dist-lib/lib/core/mesh.d.ts +15 -4
  110. package/dist-lib/lib/core/mesh.js +128 -57
  111. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  112. package/dist-lib/lib/core/migrations/chat-chunks.js +47 -0
  113. package/dist-lib/lib/core/migrations/file-index.d.ts +1 -1
  114. package/dist-lib/lib/core/migrations/file-index.js +6 -2
  115. package/dist-lib/lib/core/migrations/index.d.ts +2 -1
  116. package/dist-lib/lib/core/migrations/index.js +66 -9
  117. package/dist-lib/lib/core/notification.js +96 -23
  118. package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
  119. package/dist-lib/lib/core/pdf/figure.js +64 -0
  120. package/dist-lib/lib/core/pdf/rasterize.d.ts +10 -1
  121. package/dist-lib/lib/core/pdf/rasterize.js +72 -22
  122. package/dist-lib/lib/core/peer-duplex.js +57 -5
  123. package/dist-lib/lib/core/public-mesh.js +8 -2
  124. package/dist-lib/lib/core/run-requests.js +2 -3
  125. package/dist-lib/lib/core/senders.d.ts +7 -6
  126. package/dist-lib/lib/core/senders.js +23 -30
  127. package/dist-lib/lib/core/shared-agents.d.ts +1 -0
  128. package/dist-lib/lib/core/shared-agents.js +47 -19
  129. package/dist-lib/lib/core/sync/background.d.ts +3 -15
  130. package/dist-lib/lib/core/sync/background.js +25 -102
  131. package/dist-lib/lib/core/sync/devices.js +6 -3
  132. package/dist-lib/lib/core/sync/index.js +12 -2
  133. package/dist-lib/lib/core/sync-status.js +3 -3
  134. package/dist-lib/lib/core.d.ts +59 -10
  135. package/dist-lib/lib/core.js +706 -174
  136. package/dist-lib/lib/credential-pusher.d.ts +11 -7
  137. package/dist-lib/lib/credential-pusher.js +40 -8
  138. package/dist-lib/lib/device-tree.d.ts +18 -0
  139. package/dist-lib/lib/device-tree.js +32 -0
  140. package/dist-lib/lib/doctor/redact.js +1 -1
  141. package/dist-lib/lib/errors.d.ts +4 -0
  142. package/dist-lib/lib/errors.js +4 -0
  143. package/dist-lib/lib/fs-drive-bridge.d.ts +7 -0
  144. package/dist-lib/lib/fs-drive-bridge.js +308 -30
  145. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  146. package/dist-lib/lib/knowledge-search.d.ts +1 -1
  147. package/dist-lib/lib/knowledge-search.js +5 -46
  148. package/dist-lib/lib/kv-cache-retention.d.ts +35 -0
  149. package/dist-lib/lib/kv-cache-retention.js +98 -0
  150. package/dist-lib/lib/latest-frame.d.ts +10 -0
  151. package/dist-lib/lib/latest-frame.js +48 -0
  152. package/dist-lib/lib/model-download.d.ts +6 -1
  153. package/dist-lib/lib/model-download.js +32 -17
  154. package/dist-lib/lib/oauth/resolve.js +13 -5
  155. package/dist-lib/lib/reconnecting-watch.js +19 -11
  156. package/dist-lib/lib/run-notices.d.ts +2 -0
  157. package/dist-lib/lib/run-notices.js +5 -0
  158. package/dist-lib/lib/run-replay.d.ts +13 -1
  159. package/dist-lib/lib/run-replay.js +58 -21
  160. package/dist-lib/schema/build.js +66 -57
  161. package/dist-lib/schema/content.js +2 -1
  162. package/dist-lib/schema/engine.js +37 -5
  163. package/dist-lib/schema/field-order.js +7 -1
  164. package/dist-lib/schema/indexes.d.ts +4 -0
  165. package/dist-lib/schema/indexes.js +18 -0
  166. package/dist-lib/schema/local.js +9 -2
  167. package/dist-lib/schema/qvac.js +112 -4
  168. package/dist-lib/spec/bind.js +3 -0
  169. package/dist-lib/spec/calls.js +1 -0
  170. package/dist-lib/spec/capabilities.d.ts +2 -0
  171. package/dist-lib/spec/hrpc/hrpc.json +13 -0
  172. package/dist-lib/spec/hrpc/index.js +15 -3
  173. package/dist-lib/spec/hrpc/messages.js +1475 -1392
  174. package/dist-lib/spec/hyperschema/index.js +1475 -1392
  175. package/dist-lib/spec/hyperschema/schema.json +86 -1
  176. package/dist-lib/spec/hyperschema/types.d.ts +21 -1
  177. package/dist-lib/spec/local/hyperdb/messages.js +110 -54
  178. package/dist-lib/spec/local/hyperschema/index.js +56 -28
  179. package/dist-lib/spec/local/hyperschema/schema.json +55 -20
  180. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -1
  181. package/dist-lib/spec/mesh/hyperdb/db.json +255 -51
  182. package/dist-lib/spec/mesh/hyperdb/index.js +1063 -404
  183. package/dist-lib/spec/mesh/hyperdb/messages.js +1647 -1214
  184. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +62 -56
  185. package/dist-lib/spec/mesh/hyperdispatch/index.js +156 -139
  186. package/dist-lib/spec/mesh/hyperdispatch/messages.js +787 -629
  187. package/dist-lib/spec/mesh/hyperschema/index.js +787 -629
  188. package/dist-lib/spec/mesh/hyperschema/schema.json +283 -174
  189. package/dist-lib/spec/mesh/hyperschema/types.d.ts +29 -1
  190. package/package.json +23 -17
  191. package/worker/assistant-sidecar.mjs +3 -0
  192. package/dist-lib/lib/core/files/processing/settle.d.ts +0 -13
  193. package/dist-lib/lib/core/files/processing/settle.js +0 -86
  194. package/dist-lib/lib/core/migrations/device-indexes.d.ts +0 -2
  195. package/dist-lib/lib/core/migrations/device-indexes.js +0 -7
@@ -1,5 +1,5 @@
1
1
  import { diagramRepairLinks, isDiagramRepairPrompt } from '@qvac/harness/lib/diagram-repair.ts';
2
- import { findChunksOfType } from './chunk.js';
2
+ import { findChunksOfType, findChunksOfTypes } from './chunk.js';
3
3
  import { runDefinition } from './shared-agents.js';
4
4
  import b4a from 'b4a';
5
5
  import { ACTIVE_CHATS, AGENTS, CHUNK_COUNTERS, DEVICES, MEMBERS } from './collections.js';
@@ -40,14 +40,29 @@ export async function deriveNotifications(mesh, local, { limit: requestedLimit,
40
40
  selected.push(candidate);
41
41
  }
42
42
  const terminalSeqByChat = new Map();
43
+ const terminalSeqByRunByChat = new Map();
43
44
  for (const candidate of selected) {
44
45
  if (candidate.kind === 'approval-required' || !candidate.runId)
45
46
  continue;
46
47
  terminalSeqByChat.set(candidate.chatId, Math.max(terminalSeqByChat.get(candidate.chatId) ?? 0, candidate.seq));
48
+ let terminalSeqByRun = terminalSeqByRunByChat.get(candidate.chatId);
49
+ if (!terminalSeqByRun) {
50
+ terminalSeqByRun = new Map();
51
+ terminalSeqByRunByChat.set(candidate.chatId, terminalSeqByRun);
52
+ }
53
+ const terminalSeqs = terminalSeqByRun.get(candidate.runId) ?? [];
54
+ terminalSeqs.push(candidate.seq);
55
+ terminalSeqByRun.set(candidate.runId, terminalSeqs);
47
56
  }
48
- const contentByChat = new Map();
57
+ const previewsByChat = new Map();
49
58
  for (const [chatId, terminalSeq] of terminalSeqByChat) {
50
- contentByChat.set(chatId, await findChunksOfType(snap, { chatId, type: 'content', lte: terminalSeq }));
59
+ const content = await findChunksOfTypes(snap, {
60
+ chatId,
61
+ types: ['content'],
62
+ lte: terminalSeq
63
+ });
64
+ const terminalSeqByRun = terminalSeqByRunByChat.get(chatId) ?? new Map();
65
+ previewsByChat.set(chatId, runPreviews(content, terminalSeqByRun));
51
66
  }
52
67
  const agentNameById = new Map();
53
68
  for (const agentId of new Set(selected.map((candidate) => candidate.agentId))) {
@@ -55,8 +70,9 @@ export async function deriveNotifications(mesh, local, { limit: requestedLimit,
55
70
  agentNameById.set(agentId, (await runDefinition(mesh, row))?.name);
56
71
  }
57
72
  const notifications = selected.map((candidate) => {
58
- const content = candidate.kind === 'approval-required' ? undefined : contentByChat.get(candidate.chatId);
59
- const preview = candidate.runId && content ? runPreview(content, candidate.runId, candidate.seq) : undefined;
73
+ const preview = candidate.runId
74
+ ? previewsByChat.get(candidate.chatId)?.get(runPreviewKey(candidate.runId, candidate.seq))
75
+ : undefined;
60
76
  const { runId, ...event } = candidate;
61
77
  return {
62
78
  ...event,
@@ -76,15 +92,18 @@ export async function deriveNotifications(mesh, local, { limit: requestedLimit,
76
92
  async function chatCandidates(snap, chat, { local, deviceId, own }) {
77
93
  const counter = await snap.get(CHUNK_COUNTERS, { chatId: chat.id });
78
94
  const ackedThrough = (await local.getChatNotify(chat.id))?.ackedThrough ?? 0;
79
- const [statuses, approvals, messages] = await Promise.all([
80
- (counter?.count ?? 0) <= ackedThrough
81
- ? []
82
- : findChunksOfType(snap, { chatId: chat.id, type: 'run-status', gte: ackedThrough }),
83
- findChunksOfType(snap, { chatId: chat.id, type: 'approval-request' }),
95
+ const [unread, approvals] = await Promise.all([
84
96
  (counter?.count ?? 0) <= ackedThrough
85
97
  ? []
86
- : findChunksOfType(snap, { chatId: chat.id, type: 'user-message', gte: ackedThrough })
98
+ : findChunksOfTypes(snap, {
99
+ chatId: chat.id,
100
+ types: ['run-status', 'user-message'],
101
+ gte: ackedThrough
102
+ }),
103
+ findChunksOfType(snap, { chatId: chat.id, type: 'approval-request' })
87
104
  ]);
105
+ const statuses = unread.filter((chunk) => chunk.type === 'run-status');
106
+ const messages = unread.filter((chunk) => chunk.type === 'user-message');
88
107
  const terminalStatuses = statuses.filter((chunk) => {
89
108
  const state = chunk.runStatus?.state;
90
109
  return state === 'completed' || state === 'failed';
@@ -150,11 +169,11 @@ async function messageCandidate(snap, chunk, { chat, deviceId, own }) {
150
169
  };
151
170
  }
152
171
  async function diagramRepairRunIds(snap, chatId, toSeq) {
153
- const [users, requests] = await Promise.all([
154
- findChunksOfType(snap, { chatId, type: 'user-message', lte: toSeq }),
155
- findChunksOfType(snap, { chatId, type: 'run-request', lte: toSeq })
156
- ]);
157
- const records = [...users, ...requests];
172
+ const records = await findChunksOfTypes(snap, {
173
+ chatId,
174
+ types: ['user-message', 'run-request'],
175
+ lte: toSeq
176
+ });
158
177
  const repairs = diagramRepairLinks(records, (chunk) => chunk.userMessage?.text ?? null);
159
178
  const runIds = new Set();
160
179
  for (const chunk of records) {
@@ -167,11 +186,65 @@ async function diagramRepairRunIds(snap, chatId, toSeq) {
167
186
  }
168
187
  return runIds;
169
188
  }
170
- function runPreview(content, runId, terminalSeq) {
171
- const text = content
172
- .filter((chunk) => chunk.seq <= terminalSeq && chunk.runId === runId)
173
- .map((chunk) => chunk.content?.text ?? '')
174
- .join('')
175
- .slice(0, PREVIEW_LIMIT);
176
- return text || undefined;
189
+ function runPreviews(content, terminalSeqByRun) {
190
+ const seqsByRun = new Map([...terminalSeqByRun].map(([runId, seqs]) => [runId, [...seqs].sort((a, b) => a - b)]));
191
+ const textByRun = new Map();
192
+ const boundaryByRun = new Map();
193
+ const previews = new Map();
194
+ for (const chunk of content) {
195
+ const runId = chunk.runId;
196
+ if (!runId) {
197
+ continue;
198
+ }
199
+ const seqs = seqsByRun.get(runId);
200
+ if (!seqs) {
201
+ continue;
202
+ }
203
+ let text = textByRun.get(runId) ?? '';
204
+ let boundary = boundaryByRun.get(runId) ?? 0;
205
+ while (boundary < seqs.length) {
206
+ const terminalSeq = seqs[boundary];
207
+ if (terminalSeq === undefined || terminalSeq >= chunk.seq) {
208
+ break;
209
+ }
210
+ const key = runPreviewKey(runId, terminalSeq);
211
+ if (text) {
212
+ previews.set(key, text);
213
+ }
214
+ boundary++;
215
+ }
216
+ const next = text.length >= PREVIEW_LIMIT
217
+ ? ''
218
+ : (chunk.content?.text ?? '').slice(0, PREVIEW_LIMIT - text.length);
219
+ text += next;
220
+ textByRun.set(runId, text);
221
+ while (boundary < seqs.length) {
222
+ const terminalSeq = seqs[boundary];
223
+ if (terminalSeq === undefined || terminalSeq > chunk.seq) {
224
+ break;
225
+ }
226
+ const key = runPreviewKey(runId, terminalSeq);
227
+ if (text) {
228
+ previews.set(key, text);
229
+ }
230
+ boundary++;
231
+ }
232
+ boundaryByRun.set(runId, boundary);
233
+ }
234
+ for (const [runId, seqs] of seqsByRun) {
235
+ const text = textByRun.get(runId);
236
+ if (!text) {
237
+ continue;
238
+ }
239
+ for (let boundary = boundaryByRun.get(runId) ?? 0; boundary < seqs.length; boundary++) {
240
+ const terminalSeq = seqs[boundary];
241
+ if (terminalSeq !== undefined) {
242
+ previews.set(runPreviewKey(runId, terminalSeq), text);
243
+ }
244
+ }
245
+ }
246
+ return previews;
247
+ }
248
+ function runPreviewKey(runId, seq) {
249
+ return JSON.stringify([runId, seq]);
177
250
  }
@@ -0,0 +1,22 @@
1
+ export interface PdfBounds {
2
+ readonly x: number;
3
+ readonly y: number;
4
+ readonly width: number;
5
+ readonly height: number;
6
+ }
7
+ export interface PdfTextSpan extends PdfBounds {
8
+ readonly text: string;
9
+ }
10
+ export interface PdfFigure {
11
+ readonly clip: PdfBounds;
12
+ readonly names: ReadonlyArray<string>;
13
+ }
14
+ export interface PdfPageLayout {
15
+ readonly size: {
16
+ readonly width: number;
17
+ readonly height: number;
18
+ };
19
+ readonly images: ReadonlyArray<PdfBounds>;
20
+ readonly spans: ReadonlyArray<PdfTextSpan>;
21
+ }
22
+ export declare function figureRegion({ size, images, spans }: PdfPageLayout): PdfFigure | null;
@@ -0,0 +1,64 @@
1
+ const CAPTION = /^(?:Figure|Fig\.|Table|Chart|Exhibit)\s+[\w.-]*\w/i;
2
+ const CAPTION_GAP = 72;
3
+ const DECORATION_SHARE = 0.005;
4
+ const WHOLE_PAGE_SHARE = 0.6;
5
+ const MARGIN = 4;
6
+ function boxOf({ x, y, width, height }) {
7
+ return { left: x, bottom: y, right: x + width, top: y + height };
8
+ }
9
+ function join(a, b) {
10
+ return {
11
+ left: Math.min(a.left, b.left),
12
+ bottom: Math.min(a.bottom, b.bottom),
13
+ right: Math.max(a.right, b.right),
14
+ top: Math.max(a.top, b.top)
15
+ };
16
+ }
17
+ function overlapsAcross(span, box) {
18
+ return span.left < box.right && span.right > box.left;
19
+ }
20
+ function captionBlock(caption, lines, figure) {
21
+ const height = caption.top - caption.bottom;
22
+ let block = caption;
23
+ const below = lines
24
+ .filter((line) => line.top <= caption.top + 1 && line.bottom < caption.bottom)
25
+ .filter((line) => overlapsAcross(line, figure))
26
+ .sort((a, b) => b.top - a.top);
27
+ for (const line of below) {
28
+ if (block.bottom - line.top > height)
29
+ break;
30
+ block = join(block, line);
31
+ }
32
+ return block;
33
+ }
34
+ function captionName(text, line, figure) {
35
+ const name = text.trim().match(CAPTION)?.[0];
36
+ const near = line.top >= figure.bottom - CAPTION_GAP && line.bottom <= figure.top + CAPTION_GAP;
37
+ return name && near && overlapsAcross(line, figure) ? name : null;
38
+ }
39
+ export function figureRegion({ size, images, spans }) {
40
+ const area = size.width * size.height;
41
+ const content = images.filter((image) => image.width * image.height >= DECORATION_SHARE * area);
42
+ const pictures = (content.length ? content : images).map(boxOf);
43
+ if (!pictures.length) {
44
+ return null;
45
+ }
46
+ let box = pictures.reduce(join);
47
+ const figure = box;
48
+ const lines = spans.map(boxOf);
49
+ const names = [];
50
+ for (const [index, span] of spans.entries()) {
51
+ const name = captionName(span.text, lines[index], figure);
52
+ if (name) {
53
+ names.push(name);
54
+ box = join(box, captionBlock(lines[index], lines, figure));
55
+ }
56
+ }
57
+ const clip = {
58
+ x: Math.max(0, box.left - MARGIN),
59
+ y: Math.max(0, box.bottom - MARGIN),
60
+ width: Math.min(size.width, box.right + MARGIN) - Math.max(0, box.left - MARGIN),
61
+ height: Math.min(size.height, box.top + MARGIN) - Math.max(0, box.bottom - MARGIN)
62
+ };
63
+ return clip.width * clip.height >= WHOLE_PAGE_SHARE * area ? null : { clip, names };
64
+ }
@@ -1,14 +1,23 @@
1
1
  import type { LoggerInterface } from '@qvac/logging';
2
+ import { type PdfBounds, type PdfFigure } from './figure.ts';
2
3
  export declare const PDF_RENDER_SCALE = 2;
3
4
  interface PageFlags {
4
5
  hasImage: boolean;
5
6
  hasText: boolean;
6
7
  }
8
+ interface PageSize {
9
+ width: number;
10
+ height: number;
11
+ }
7
12
  export declare function renderScale(pageWidth: number): number;
13
+ export declare function renderSize(page: PageSize, clip?: PdfBounds): PageSize;
8
14
  export interface PdfDoc {
9
15
  pageCount(): number;
10
16
  pageFlags(page: number): PageFlags;
11
- renderPage(page: number): Promise<Uint8Array>;
17
+ figure(page: number): PdfFigure | null;
18
+ blank(page: number): Promise<boolean>;
19
+ renderSize(page: number, clip?: PdfBounds): PageSize;
20
+ renderPage(page: number, clip?: PdfBounds): Promise<Uint8Array>;
12
21
  close(): void;
13
22
  }
14
23
  export interface PdfRasterizer {
@@ -1,8 +1,30 @@
1
1
  import { MAX_IMAGE_WIDTH, rasterToModelJpeg } from '../image/normalize.js';
2
+ import { figureRegion } from './figure.js';
2
3
  export const PDF_RENDER_SCALE = 2;
4
+ const BLANK_SCALE = 0.25;
5
+ const PAPER = 230;
6
+ const INK_SHARE = 0.001;
3
7
  export function renderScale(pageWidth) {
4
8
  return Math.min(PDF_RENDER_SCALE, MAX_IMAGE_WIDTH / pageWidth);
5
9
  }
10
+ export function renderSize(page, clip) {
11
+ const scale = renderScale(page.width);
12
+ const width = Math.max(1, Math.round((clip?.width ?? page.width) * scale));
13
+ const height = Math.max(1, Math.round((clip?.height ?? page.height) * scale));
14
+ const shrink = Math.min(1, MAX_IMAGE_WIDTH / width);
15
+ return { width: Math.round(width * shrink), height: Math.round(height * shrink) };
16
+ }
17
+ function blankRaster({ data }) {
18
+ let samples = 0;
19
+ let ink = 0;
20
+ for (let i = 0; i < data.length; i += 4) {
21
+ samples++;
22
+ if (data[i] < PAPER || data[i + 1] < PAPER || data[i + 2] < PAPER) {
23
+ ink++;
24
+ }
25
+ }
26
+ return ink <= samples * INK_SHARE;
27
+ }
6
28
  let pdfium;
7
29
  async function loadPdfium(log) {
8
30
  if (pdfium !== undefined)
@@ -17,6 +39,55 @@ async function loadPdfium(log) {
17
39
  }
18
40
  return pdfium;
19
41
  }
42
+ function pageFigure(doc, page) {
43
+ if (doc.pageRotation(page) !== 0) {
44
+ return null;
45
+ }
46
+ const images = doc.imageBounds(page);
47
+ if (!images.length) {
48
+ return null;
49
+ }
50
+ const box = doc.pageBox(page);
51
+ const shown = (at) => ({ ...at, x: at.x - box.x, y: at.y - box.y });
52
+ return figureRegion({
53
+ size: { width: box.width, height: box.height },
54
+ images: images.map(shown),
55
+ spans: doc.textSpans(page).map(shown)
56
+ });
57
+ }
58
+ function wrapDoc(doc) {
59
+ return {
60
+ pageCount() {
61
+ return doc.pageCount();
62
+ },
63
+ pageFlags(page) {
64
+ return doc.pageFlags(page);
65
+ },
66
+ figure(page) {
67
+ return pageFigure(doc, page);
68
+ },
69
+ async blank(page) {
70
+ return blankRaster(await doc.renderAsync(page, { scale: BLANK_SCALE }));
71
+ },
72
+ renderSize(page, clip) {
73
+ return renderSize(doc.pageSize(page), clip);
74
+ },
75
+ async renderPage(page, clip) {
76
+ const scale = renderScale(doc.pageSize(page).width);
77
+ const jpeg = await rasterToModelJpeg(await doc.renderAsync(page, { scale, clip }));
78
+ if (!jpeg) {
79
+ throw new Error('no JPEG encoder: bare-media did not load');
80
+ }
81
+ return jpeg;
82
+ },
83
+ close() {
84
+ try {
85
+ doc.close();
86
+ }
87
+ catch { }
88
+ }
89
+ };
90
+ }
20
91
  export const createPdfRasterizer = (log) => ({
21
92
  async open(bytes) {
22
93
  const mod = await loadPdfium(log);
@@ -30,27 +101,6 @@ export const createPdfRasterizer = (log) => ({
30
101
  catch {
31
102
  return null;
32
103
  }
33
- return {
34
- pageCount() {
35
- return doc.pageCount();
36
- },
37
- pageFlags(page) {
38
- return doc.pageFlags(page);
39
- },
40
- async renderPage(page) {
41
- const scale = renderScale(doc.pageSize(page).width);
42
- const jpeg = await rasterToModelJpeg(await doc.renderAsync(page, { scale }));
43
- if (!jpeg) {
44
- throw new Error('no JPEG encoder: bare-media did not load');
45
- }
46
- return jpeg;
47
- },
48
- close() {
49
- try {
50
- doc.close();
51
- }
52
- catch { }
53
- }
54
- };
104
+ return wrapDoc(doc);
55
105
  }
56
106
  });
@@ -4,6 +4,7 @@ import { Duplex } from 'streamx';
4
4
  import { createProtocolPairing } from './protocol-pairing.js';
5
5
  function noop() { }
6
6
  const bytes = raw;
7
+ const PEER_FRAME_BYTES = 64 * 1024;
7
8
  const frame = {
8
9
  preencode(state, f) {
9
10
  uint.preencode(state, f.id);
@@ -50,12 +51,18 @@ export class PeerDuplexChannel {
50
51
  if (this._peers.has(deviceId)) {
51
52
  return;
52
53
  }
54
+ const drainWaiters = new Set();
53
55
  const channel = this._pairing
54
56
  .mux(stream, () => this.addStream(stream))
55
57
  .createChannel({
56
58
  protocol: this._opts.protocol,
57
59
  onopen: () => this._opened(deviceId),
58
- onclose: () => this._drop(deviceId)
60
+ onclose: () => this._drop(deviceId),
61
+ ondrain: () => {
62
+ for (const waiter of [...drainWaiters]) {
63
+ waiter();
64
+ }
65
+ }
59
66
  });
60
67
  if (!channel) {
61
68
  return;
@@ -80,6 +87,7 @@ export class PeerDuplexChannel {
80
87
  close,
81
88
  dialled: new Map(),
82
89
  served: new Map(),
90
+ drainWaiters,
83
91
  opened: false
84
92
  });
85
93
  channel.open();
@@ -140,21 +148,57 @@ export class PeerDuplexChannel {
140
148
  _attach(peer, id, opts) {
141
149
  const { mine } = opts;
142
150
  const held = mine ? peer.dialled : peer.served;
151
+ let pendingWrite = null;
143
152
  const stream = new Duplex({
144
153
  write: (chunk, cb) => {
145
- peer.data.send({ id, mine, data: chunk });
146
- cb(null);
154
+ let offset = 0;
155
+ const sendNext = () => {
156
+ if (this._peers.get(b4a.toHex(peer.remoteKey)) !== peer) {
157
+ return cb(this._opts.unreachable());
158
+ }
159
+ while (offset < chunk.byteLength) {
160
+ const end = Math.min(offset + PEER_FRAME_BYTES, chunk.byteLength);
161
+ const drained = peer.data.send({ id, mine, data: chunk.subarray(offset, end) });
162
+ offset = end;
163
+ if (drained) {
164
+ continue;
165
+ }
166
+ let settled = false;
167
+ const finish = (err) => {
168
+ if (settled)
169
+ return;
170
+ settled = true;
171
+ pendingWrite = null;
172
+ peer.drainWaiters.delete(finish);
173
+ if (err) {
174
+ cb(err);
175
+ }
176
+ else {
177
+ sendNext();
178
+ }
179
+ };
180
+ pendingWrite = finish;
181
+ peer.drainWaiters.add(finish);
182
+ return;
183
+ }
184
+ cb(null);
185
+ };
186
+ sendNext();
147
187
  },
148
188
  final: (cb) => {
149
189
  peer.close.send({ id, mine });
150
190
  cb(null);
151
191
  },
192
+ predestroy: () => pendingWrite?.(new Error('peer stream closed')),
152
193
  destroy: (cb) => {
194
+ pendingWrite?.(new Error('peer stream closed'));
153
195
  if (held.delete(id)) {
154
196
  peer.close.send({ id, mine });
155
197
  }
156
198
  cb(null);
157
- }
199
+ },
200
+ byteLengthReadable: (chunk) => (chunk instanceof Uint8Array ? chunk.byteLength : 0),
201
+ byteLengthWritable: (chunk) => (chunk instanceof Uint8Array ? chunk.byteLength : 0)
158
202
  });
159
203
  held.set(id, stream);
160
204
  return stream;
@@ -181,7 +225,11 @@ export class PeerDuplexChannel {
181
225
  return;
182
226
  }
183
227
  const held = f.mine ? peer.served : peer.dialled;
184
- held.get(f.id)?.push(f.data);
228
+ const stream = held.get(f.id);
229
+ if (!stream) {
230
+ return;
231
+ }
232
+ stream.push(f.data);
185
233
  }
186
234
  _end(deviceId, e) {
187
235
  const peer = this._peers.get(deviceId);
@@ -203,6 +251,10 @@ export class PeerDuplexChannel {
203
251
  return;
204
252
  }
205
253
  this._peers.delete(deviceId);
254
+ for (const waiter of [...peer.drainWaiters]) {
255
+ waiter(this._opts.unreachable());
256
+ }
257
+ peer.drainWaiters.clear();
206
258
  for (const held of [peer.dialled, peer.served]) {
207
259
  for (const stream of held.values()) {
208
260
  stream.destroy(this._opts.unreachable());
@@ -40,8 +40,14 @@ export async function lostMint(account, meshKey) {
40
40
  }
41
41
  export async function publicMeshKey(account, workspaceKey) {
42
42
  const id = b4a.toHex(workspaceKey);
43
- const row = await account.view.get(PUBLIC_MESHES, { id });
44
- return row?.meshKey ?? null;
43
+ const snap = account.view.snapshot();
44
+ try {
45
+ const row = await snap.get(PUBLIC_MESHES, { id });
46
+ return row?.meshKey ?? null;
47
+ }
48
+ finally {
49
+ await snap.close();
50
+ }
45
51
  }
46
52
  function serving(agent, deviceId) {
47
53
  return !!agent && !agent.deletedAt && b4a.equals(agent.providerDeviceId, deviceId);
@@ -4,6 +4,7 @@ import { CAPABILITY_PROFILES } from '../../schema/qvac.js';
4
4
  import { CoreError } from '../errors.js';
5
5
  import { getChunk, mustGetChunk } from './chunk.js';
6
6
  import { openToTake, runHasTerminalStatus, runTaker } from './contract/run-requests.js';
7
+ import { liveDevicesOf } from './contract/activity.js';
7
8
  import { DEVICES, RUN_REQUESTS } from './collections.js';
8
9
  const rank = (device) => CAPABILITY_PROFILES.indexOf(device.capabilityProfile);
9
10
  export async function pickRunner(mesh, { deviceId, peers, agent }) {
@@ -13,9 +14,7 @@ export async function pickRunner(mesh, { deviceId, peers, agent }) {
13
14
  return undefined;
14
15
  }
15
16
  const connected = new Set(peers);
16
- const devices = await mesh.view.find(DEVICES).toArray();
17
- const ours = devices.filter((device) => !device.revoked &&
18
- !!device.memberId &&
17
+ const ours = (await liveDevicesOf(mesh.view, member)).filter((device) => !!device.memberId &&
19
18
  b4a.equals(device.memberId, member) &&
20
19
  !b4a.equals(device.id, deviceId) &&
21
20
  connected.has(b4a.toHex(device.id)));
@@ -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>;