@qvac/core 0.2.0 → 0.2.2

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 (265) hide show
  1. package/.claude/skills/compaction/SKILL.md +9 -4
  2. package/.claude/skills/pr-create/SKILL.md +24 -3
  3. package/.claude/skills/pr-review/SKILL.md +248 -40
  4. package/CLAUDE.md +1 -3
  5. package/README.md +3 -1
  6. package/dist-lib/client.js +1 -1
  7. package/dist-lib/index.js +8 -8
  8. package/dist-lib/lib/api/agents.d.ts +1 -1
  9. package/dist-lib/lib/api/agents.js +10 -9
  10. package/dist-lib/lib/api/app-config.js +1 -1
  11. package/dist-lib/lib/api/approval-rules.js +9 -5
  12. package/dist-lib/lib/api/attachments.js +25 -3
  13. package/dist-lib/lib/api/chat-groups.d.ts +1 -1
  14. package/dist-lib/lib/api/chat-groups.js +5 -4
  15. package/dist-lib/lib/api/chat.js +8 -8
  16. package/dist-lib/lib/api/chunks.js +11 -11
  17. package/dist-lib/lib/api/config.js +1 -1
  18. package/dist-lib/lib/api/custom-models.js +2 -2
  19. package/dist-lib/lib/api/devices.d.ts +1 -1
  20. package/dist-lib/lib/api/devices.js +9 -9
  21. package/dist-lib/lib/api/diagnostics.js +1 -1
  22. package/dist-lib/lib/api/effective-ctx-size.js +1 -1
  23. package/dist-lib/lib/api/execution.js +4 -4
  24. package/dist-lib/lib/api/files.js +2 -2
  25. package/dist-lib/lib/api/index.d.ts +1 -0
  26. package/dist-lib/lib/api/index.js +33 -33
  27. package/dist-lib/lib/api/instrument.js +1 -1
  28. package/dist-lib/lib/api/knowledge.js +4 -4
  29. package/dist-lib/lib/api/logs.js +1 -1
  30. package/dist-lib/lib/api/members.d.ts +4 -1
  31. package/dist-lib/lib/api/members.js +88 -18
  32. package/dist-lib/lib/api/mesh.d.ts +13 -3
  33. package/dist-lib/lib/api/mesh.js +25 -2
  34. package/dist-lib/lib/api/model-download.js +1 -1
  35. package/dist-lib/lib/api/network.js +1 -1
  36. package/dist-lib/lib/api/notifications.js +3 -3
  37. package/dist-lib/lib/api/rag.js +7 -7
  38. package/dist-lib/lib/api/sync.js +1 -1
  39. package/dist-lib/lib/api/tool-activity.js +4 -4
  40. package/dist-lib/lib/api/tool-configs.js +5 -5
  41. package/dist-lib/lib/api/watchable.js +1 -1
  42. package/dist-lib/lib/assistant/fetch-body.d.ts +5 -0
  43. package/dist-lib/lib/assistant/fetch-body.js +36 -0
  44. package/dist-lib/lib/assistant/harness.d.ts +225 -0
  45. package/dist-lib/lib/assistant/harness.js +154 -0
  46. package/dist-lib/lib/assistant/history.d.ts +32 -0
  47. package/dist-lib/lib/assistant/history.js +232 -0
  48. package/dist-lib/lib/assistant/message.d.ts +45 -0
  49. package/dist-lib/lib/assistant/message.js +47 -0
  50. package/dist-lib/lib/assistant/skill-encode.d.ts +21 -0
  51. package/dist-lib/lib/assistant/skill-encode.js +71 -0
  52. package/dist-lib/lib/assistant-client.js +2 -2
  53. package/dist-lib/lib/assistant-tree.js +10 -4
  54. package/dist-lib/lib/assistant.d.ts +25 -285
  55. package/dist-lib/lib/assistant.js +250 -637
  56. package/dist-lib/lib/audio-format.js +2 -2
  57. package/dist-lib/lib/client.js +2 -2
  58. package/dist-lib/lib/collabora/convert.js +1 -1
  59. package/dist-lib/lib/collabora/entry.js +1 -1
  60. package/dist-lib/lib/collabora/worker.js +1 -1
  61. package/dist-lib/lib/compaction.d.ts +1 -2
  62. package/dist-lib/lib/compaction.js +8 -6
  63. package/dist-lib/lib/constants.js +1 -1
  64. package/dist-lib/lib/core/account-recovery.d.ts +19 -0
  65. package/dist-lib/lib/core/account-recovery.js +50 -0
  66. package/dist-lib/lib/core/agent.js +8 -4
  67. package/dist-lib/lib/core/app-build.d.ts +1 -1
  68. package/dist-lib/lib/core/approval-rules.d.ts +1 -0
  69. package/dist-lib/lib/core/approval-rules.js +3 -1
  70. package/dist-lib/lib/core/archived-mesh.d.ts +25 -0
  71. package/dist-lib/lib/core/archived-mesh.js +162 -0
  72. package/dist-lib/lib/core/chat-activity.js +1 -1
  73. package/dist-lib/lib/core/chat-group.d.ts +1 -0
  74. package/dist-lib/lib/core/chat-group.js +24 -4
  75. package/dist-lib/lib/core/chat-search.js +2 -2
  76. package/dist-lib/lib/core/chat.js +5 -5
  77. package/dist-lib/lib/core/chunk.d.ts +1 -1
  78. package/dist-lib/lib/core/chunk.js +4 -4
  79. package/dist-lib/lib/core/contract/agents.js +1 -1
  80. package/dist-lib/lib/core/contract/approval-rules.js +6 -4
  81. package/dist-lib/lib/core/contract/attestations.js +1 -1
  82. package/dist-lib/lib/core/contract/chat-group.js +1 -1
  83. package/dist-lib/lib/core/contract/chat.js +1 -1
  84. package/dist-lib/lib/core/contract/chunks.js +2 -2
  85. package/dist-lib/lib/core/contract/compute.d.ts +11 -0
  86. package/dist-lib/lib/core/contract/compute.js +45 -0
  87. package/dist-lib/lib/core/contract/devices.js +16 -3
  88. package/dist-lib/lib/core/contract/index.d.ts +3 -1
  89. package/dist-lib/lib/core/contract/index.js +19 -16
  90. package/dist-lib/lib/core/contract/indexing.js +2 -2
  91. package/dist-lib/lib/core/contract/joined-meshes.js +1 -1
  92. package/dist-lib/lib/core/contract/knowledge.js +2 -2
  93. package/dist-lib/lib/core/contract/members.js +2 -2
  94. package/dist-lib/lib/core/contract/seal.d.ts +2 -0
  95. package/dist-lib/lib/core/contract/seal.js +8 -0
  96. package/dist-lib/lib/core/contract/seed-defaults.js +1 -1
  97. package/dist-lib/lib/core/contract/tool-activity.js +1 -1
  98. package/dist-lib/lib/core/contract/tool-configs.js +3 -3
  99. package/dist-lib/lib/core/contract/writers.js +66 -42
  100. package/dist-lib/lib/core/departures.js +1 -1
  101. package/dist-lib/lib/core/device-invites.js +1 -1
  102. package/dist-lib/lib/core/document-attachment.js +2 -2
  103. package/dist-lib/lib/core/files/attachments.js +3 -3
  104. package/dist-lib/lib/core/files/index.d.ts +2 -2
  105. package/dist-lib/lib/core/files/index.js +19 -19
  106. package/dist-lib/lib/core/files/knowledge.js +3 -3
  107. package/dist-lib/lib/core/files/preview/image.js +1 -1
  108. package/dist-lib/lib/core/files/preview/index.js +6 -6
  109. package/dist-lib/lib/core/files/processing/claims.js +1 -1
  110. package/dist-lib/lib/core/files/processing/document.js +5 -5
  111. package/dist-lib/lib/core/files/processing/index.js +8 -8
  112. package/dist-lib/lib/core/files/processing/ownership.js +1 -1
  113. package/dist-lib/lib/core/files/processing/protocol.js +3 -3
  114. package/dist-lib/lib/core/files/processing/source.js +1 -1
  115. package/dist-lib/lib/core/files/rag/artifact/codec.js +1 -1
  116. package/dist-lib/lib/core/files/rag/artifact/id.js +1 -1
  117. package/dist-lib/lib/core/files/rag/artifact/types.d.ts +4 -4
  118. package/dist-lib/lib/core/files/rag/build.js +2 -2
  119. package/dist-lib/lib/core/files/rag/chunk/chunking.js +2 -2
  120. package/dist-lib/lib/core/files/rag/document-types.d.ts +3 -3
  121. package/dist-lib/lib/core/files/rag/extract/document-types.d.ts +9 -9
  122. package/dist-lib/lib/core/files/rag/extract/extract.js +7 -7
  123. package/dist-lib/lib/core/files/rag/extract/html-text.js +1 -1
  124. package/dist-lib/lib/core/files/rag/extract/office-collabora.js +2 -2
  125. package/dist-lib/lib/core/files/rag/extract/pdf-platform.js +1 -1
  126. package/dist-lib/lib/core/files/rag/extract/pdf-text.js +1 -1
  127. package/dist-lib/lib/core/files/rag/extract/text-decoding.js +1 -1
  128. package/dist-lib/lib/core/files/rag/extract/types.d.ts +2 -2
  129. package/dist-lib/lib/core/files/rag/index.js +22 -22
  130. package/dist-lib/lib/core/files/rag/load.js +5 -5
  131. package/dist-lib/lib/core/files/rag/policy.js +2 -2
  132. package/dist-lib/lib/core/files/rag/produce.js +5 -5
  133. package/dist-lib/lib/core/files/rag/retrieval/coverage.js +2 -2
  134. package/dist-lib/lib/core/files/rag/retrieval/diversity.js +1 -1
  135. package/dist-lib/lib/core/files/rag/retrieval/floor.js +2 -2
  136. package/dist-lib/lib/core/files/rag/retrieval/format.js +1 -1
  137. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +18 -11
  138. package/dist-lib/lib/core/files/rag/retrieval/knowledge.js +6 -5
  139. package/dist-lib/lib/core/files/rag/retrieval/scoring.js +2 -2
  140. package/dist-lib/lib/core/files/rag/retrieval/search.js +7 -4
  141. package/dist-lib/lib/core/files/rag/retrieval/selection.d.ts +1 -0
  142. package/dist-lib/lib/core/files/rag/retrieval/selection.js +32 -2
  143. package/dist-lib/lib/core/files/rag/retrieval/sources.js +2 -2
  144. package/dist-lib/lib/core/files/rag/retrieval/terms.js +1 -1
  145. package/dist-lib/lib/core/files/rag/retrieval/types.d.ts +1 -0
  146. package/dist-lib/lib/core/files/rag/retrieval/web-sources.js +1 -1
  147. package/dist-lib/lib/core/files/records/chunks.js +1 -1
  148. package/dist-lib/lib/core/files/records/index.js +8 -8
  149. package/dist-lib/lib/core/files/records/knowledge.js +2 -2
  150. package/dist-lib/lib/core/files/records/output.js +1 -1
  151. package/dist-lib/lib/core/files/records/reclaim.js +5 -5
  152. package/dist-lib/lib/core/files/records/routing.js +7 -7
  153. package/dist-lib/lib/core/files/store/blob.js +2 -2
  154. package/dist-lib/lib/core/files/store/index.js +7 -7
  155. package/dist-lib/lib/core/files/store/reference.js +2 -2
  156. package/dist-lib/lib/core/files/store/server.js +1 -1
  157. package/dist-lib/lib/core/files/store/sweep.js +1 -1
  158. package/dist-lib/lib/core/files/upload-input.js +2 -2
  159. package/dist-lib/lib/core/files/upload.d.ts +1 -1
  160. package/dist-lib/lib/core/files/upload.js +6 -6
  161. package/dist-lib/lib/core/harness-loan.d.ts +29 -0
  162. package/dist-lib/lib/core/harness-loan.js +187 -0
  163. package/dist-lib/lib/core/helpers/index.d.ts +2 -1
  164. package/dist-lib/lib/core/helpers/index.js +4 -4
  165. package/dist-lib/lib/core/hosted-agents.js +2 -2
  166. package/dist-lib/lib/core/identity.d.ts +2 -0
  167. package/dist-lib/lib/core/identity.js +4 -2
  168. package/dist-lib/lib/core/invite.d.ts +1 -0
  169. package/dist-lib/lib/core/invite.js +4 -2
  170. package/dist-lib/lib/core/local/index.d.ts +12 -3
  171. package/dist-lib/lib/core/local/index.js +75 -16
  172. package/dist-lib/lib/core/membership.d.ts +3 -0
  173. package/dist-lib/lib/core/membership.js +15 -1
  174. package/dist-lib/lib/core/mesh-seed-handoff.js +1 -1
  175. package/dist-lib/lib/core/mesh.d.ts +9 -3
  176. package/dist-lib/lib/core/mesh.js +41 -26
  177. package/dist-lib/lib/core/migrations/agent-catalog.js +1 -1
  178. package/dist-lib/lib/core/migrations/index.js +5 -3
  179. package/dist-lib/lib/core/migrations/membership.js +2 -2
  180. package/dist-lib/lib/core/migrations/seal.d.ts +3 -0
  181. package/dist-lib/lib/core/migrations/seal.js +30 -0
  182. package/dist-lib/lib/core/next-frame.js +1 -1
  183. package/dist-lib/lib/core/notification.js +1 -1
  184. package/dist-lib/lib/core/ota-config.js +1 -1
  185. package/dist-lib/lib/core/secret-handoff.js +1 -1
  186. package/dist-lib/lib/core/send.js +3 -3
  187. package/dist-lib/lib/core/skill-overlay-handoff.js +1 -1
  188. package/dist-lib/lib/core/skill-overlay-receiver.js +2 -2
  189. package/dist-lib/lib/core/skill-secrets.js +3 -3
  190. package/dist-lib/lib/core/sync/devices.js +1 -1
  191. package/dist-lib/lib/core/sync/index.js +8 -5
  192. package/dist-lib/lib/core/sync-status.js +1 -1
  193. package/dist-lib/lib/core/system-info.js +1 -1
  194. package/dist-lib/lib/core/tool-activity.js +2 -2
  195. package/dist-lib/lib/core/tool-configs.js +2 -2
  196. package/dist-lib/lib/core.d.ts +41 -3
  197. package/dist-lib/lib/core.js +525 -90
  198. package/dist-lib/lib/credential-pusher.js +4 -4
  199. package/dist-lib/lib/deadline.js +1 -1
  200. package/dist-lib/lib/fs-drive-bridge.js +4 -2
  201. package/dist-lib/lib/integrations/index.js +3 -3
  202. package/dist-lib/lib/integrations/oauth-providers.js +1 -1
  203. package/dist-lib/lib/integrations/setup-routes.d.ts +5 -5
  204. package/dist-lib/lib/integrations/setup-routes.js +3 -3
  205. package/dist-lib/lib/knowledge-search.js +1 -1
  206. package/dist-lib/lib/log.js +3 -3
  207. package/dist-lib/lib/mesh-scoped-engine.d.ts +3 -0
  208. package/dist-lib/lib/mesh-scoped-engine.js +96 -0
  209. package/dist-lib/lib/model-context.d.ts +2 -2
  210. package/dist-lib/lib/model-context.js +1 -1
  211. package/dist-lib/lib/model-download.js +6 -6
  212. package/dist-lib/lib/oauth/asana.js +3 -3
  213. package/dist-lib/lib/oauth/browser-connect.js +1 -1
  214. package/dist-lib/lib/oauth/client.js +2 -2
  215. package/dist-lib/lib/oauth/common.d.ts +1 -1
  216. package/dist-lib/lib/oauth/common.js +1 -1
  217. package/dist-lib/lib/oauth/flow-core.js +3 -3
  218. package/dist-lib/lib/oauth/flow.js +2 -2
  219. package/dist-lib/lib/oauth/google.js +4 -4
  220. package/dist-lib/lib/oauth/host.js +3 -3
  221. package/dist-lib/lib/oauth/loopback.js +1 -1
  222. package/dist-lib/lib/oauth/notion.js +3 -3
  223. package/dist-lib/lib/oauth/providers.js +1 -1
  224. package/dist-lib/lib/oauth/resolve.js +1 -1
  225. package/dist-lib/lib/oauth/spotify.js +3 -3
  226. package/dist-lib/lib/oauth/token-exchange.js +1 -1
  227. package/dist-lib/lib/oauth.client.js +2 -2
  228. package/dist-lib/lib/oauth.js +12 -12
  229. package/dist-lib/lib/run-notices.d.ts +7 -7
  230. package/dist-lib/lib/run-transcript.js +1 -1
  231. package/dist-lib/schema/build.js +12 -6
  232. package/dist-lib/schema/content.d.ts +11 -11
  233. package/dist-lib/schema/engine.js +193 -30
  234. package/dist-lib/schema/field-order.js +2 -2
  235. package/dist-lib/schema/local.js +18 -4
  236. package/dist-lib/schema/qvac.js +15 -6
  237. package/dist-lib/spec/bind.js +41 -0
  238. package/dist-lib/spec/calls.js +12 -0
  239. package/dist-lib/spec/capabilities.d.ts +36 -12
  240. package/dist-lib/spec/hrpc/hrpc.json +161 -5
  241. package/dist-lib/spec/hrpc/index.js +154 -9
  242. package/dist-lib/spec/hrpc/messages.js +549 -129
  243. package/dist-lib/spec/hyperschema/index.js +549 -129
  244. package/dist-lib/spec/hyperschema/schema.json +370 -40
  245. package/dist-lib/spec/hyperschema/types.d.ts +85 -1
  246. package/dist-lib/spec/local/hyperdb/db.json +15 -0
  247. package/dist-lib/spec/local/hyperdb/index.js +70 -1
  248. package/dist-lib/spec/local/hyperdb/messages.js +300 -117
  249. package/dist-lib/spec/local/hyperschema/index.js +97 -4
  250. package/dist-lib/spec/local/hyperschema/schema.json +58 -1
  251. package/dist-lib/spec/local/hyperschema/types.d.ts +14 -0
  252. package/dist-lib/spec/mesh/hyperdb/messages.js +393 -288
  253. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +6 -0
  254. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  255. package/dist-lib/spec/mesh/hyperdispatch/messages.js +176 -85
  256. package/dist-lib/spec/mesh/hyperschema/index.js +176 -85
  257. package/dist-lib/spec/mesh/hyperschema/schema.json +71 -21
  258. package/dist-lib/spec/mesh/hyperschema/types.d.ts +14 -1
  259. package/package.json +16 -10
  260. package/tui/README.md +7 -0
  261. package/tui/app.mjs +311 -14
  262. package/tui/lib/busy-words.mjs +57 -5
  263. package/tui/lib/export.mjs +1 -0
  264. package/tui/lib/render.mjs +9 -2
  265. package/tui/main.mjs +5 -2
@@ -0,0 +1,232 @@
1
+ import { attachmentTurn, skillLoadedTurn } from '@qvac/harness/lib/capabilities/tool-turns.ts';
2
+ import { HISTORY_CHUNK_TYPES } from '../core/chunk.js';
3
+ import { isAudioAttachment } from '../core/audio-attachment.js';
4
+ import { isDocumentAttachment } from '../core/document-attachment.js';
5
+ import { groupAttachmentsByMessage } from '../core/files/index.js';
6
+ import { IMAGE_MIMES } from '../core/image/normalize.js';
7
+ import { compactionTurn, foldCompactedChunks } from '../compaction.js';
8
+ export const TERMINAL_STATES = new Set(['completed', 'failed', 'canceled']);
9
+ export const HISTORY_TYPES = HISTORY_CHUNK_TYPES;
10
+ export const SKILL_TOOL = 'skill';
11
+ export async function listHistoryChunks(engine, chatId) {
12
+ const [history, toolResults, runStatuses] = await Promise.all([
13
+ engine.listChunks({ chatId, types: HISTORY_TYPES }),
14
+ engine.listChunks({ chatId, type: 'tool-result' }),
15
+ engine.listChunks({ chatId, type: 'run-status' })
16
+ ]);
17
+ return [
18
+ ...(history.chunks ?? []),
19
+ ...(toolResults.chunks ?? []),
20
+ ...(runStatuses.chunks ?? [])
21
+ ].sort((a, b) => a.seq - b.seq);
22
+ }
23
+ export function latestAttemptStarts(chunks) {
24
+ const starts = new Map();
25
+ for (const chunk of chunks) {
26
+ if (chunk.type !== 'run-status' || !chunk.runId)
27
+ continue;
28
+ if (TERMINAL_STATES.has(chunk.runStatus?.state ?? ''))
29
+ continue;
30
+ if (chunk.runStatus?.progress?.phase === 'compacting')
31
+ continue;
32
+ starts.set(chunk.runId, chunk.seq);
33
+ }
34
+ return starts;
35
+ }
36
+ export function beforeLatestAttempt(chunk, starts) {
37
+ if (!chunk.runId)
38
+ return false;
39
+ const start = starts.get(chunk.runId);
40
+ return start !== undefined && chunk.seq < start;
41
+ }
42
+ export function historyFromChunks(chunks, stale = {}) {
43
+ const attemptStarts = latestAttemptStarts(chunks);
44
+ const staleRunIds = stale.runIds ?? new Set();
45
+ const staleQuestionChunkIds = stale.questionChunkIds ?? new Set();
46
+ const folded = foldCompactedChunks(chunks);
47
+ const source = folded.chunks;
48
+ const attachments = [];
49
+ const userMessages = [];
50
+ for (const chunk of source) {
51
+ if (chunk.type === 'attachment')
52
+ attachments.push(chunk);
53
+ else if (chunk.userMessage)
54
+ userMessages.push(chunk);
55
+ }
56
+ const byMessage = groupAttachmentsByMessage(attachments, userMessages);
57
+ const history = [];
58
+ const replayedSkills = new Set();
59
+ if (folded.summary)
60
+ history.push({ role: 'user', content: compactionTurn(folded.summary) });
61
+ const byRun = new Map();
62
+ const anchor = (runId) => {
63
+ let turn = byRun.get(runId);
64
+ if (!turn) {
65
+ turn = { role: 'assistant', content: '' };
66
+ byRun.set(runId, turn);
67
+ history.push(turn);
68
+ }
69
+ return turn;
70
+ };
71
+ for (const chunk of source) {
72
+ if (chunk.userMessage) {
73
+ if (staleQuestionChunkIds.has(chunk.id))
74
+ continue;
75
+ const turn = { role: 'user', content: chunk.userMessage.text };
76
+ const refs = (byMessage.get(chunk.id) ?? [])
77
+ .map((attachmentChunk) => attachmentChunk.attachment)
78
+ .filter((ref) => !ref.deletedAt);
79
+ const images = refs.filter((ref) => IMAGE_MIMES.has(ref.mimeType.toLowerCase()));
80
+ const audio = refs.filter((ref) => isAudioAttachment(ref.fileName, ref.mimeType));
81
+ const documents = refs.filter((ref) => isDocumentAttachment(ref));
82
+ if (images.length)
83
+ turn.images = images;
84
+ if (audio.length)
85
+ turn.audioAttachments = audio;
86
+ if (documents.length)
87
+ turn.documents = documents;
88
+ history.push(turn);
89
+ continue;
90
+ }
91
+ if (chunk.attachment)
92
+ continue;
93
+ if (chunk.type === 'run-request' && chunk.runId) {
94
+ anchor(chunk.runId);
95
+ continue;
96
+ }
97
+ if (chunk.toolResult?.name === SKILL_TOOL) {
98
+ if (chunk.runId && staleRunIds.has(chunk.runId))
99
+ continue;
100
+ if (beforeLatestAttempt(chunk, attemptStarts))
101
+ continue;
102
+ const loaded = chunk.toolResult.result?.name;
103
+ if (typeof loaded === 'string' && !replayedSkills.has(loaded)) {
104
+ replayedSkills.add(loaded);
105
+ const at = history.indexOf(anchor(chunk.runId)) + 1;
106
+ history.splice(at, 0, { role: 'tool', content: skillLoadedTurn(loaded) });
107
+ }
108
+ continue;
109
+ }
110
+ if (chunk.toolResult) {
111
+ if (chunk.runId && staleRunIds.has(chunk.runId))
112
+ continue;
113
+ if (beforeLatestAttempt(chunk, attemptStarts))
114
+ continue;
115
+ const produced = attachmentTurn(chunk.toolResult.name, chunk.toolResult.result);
116
+ if (produced)
117
+ history.push({ role: 'tool', content: produced });
118
+ continue;
119
+ }
120
+ if (!chunk.content)
121
+ continue;
122
+ if (chunk.runId && staleRunIds.has(chunk.runId))
123
+ continue;
124
+ if (beforeLatestAttempt(chunk, attemptStarts))
125
+ continue;
126
+ anchor(chunk.runId).content += chunk.content.text;
127
+ }
128
+ return history.filter((turn) => turn.role === 'user' || turn.content.length > 0);
129
+ }
130
+ export function withoutOccupancy(metrics) {
131
+ if (!metrics)
132
+ return metrics;
133
+ const { cacheTokens, promptTokens, ...rest } = metrics;
134
+ return rest;
135
+ }
136
+ export function inputAvailableToRun(chunk, requestSeq) {
137
+ const userInput = Boolean(chunk.userMessage || (chunk.attachment && !chunk.runId));
138
+ return !userInput || chunk.seq < requestSeq;
139
+ }
140
+ export function normalizeQuestion(text) {
141
+ return text.trim().toLowerCase().replace(/\s+/g, ' ');
142
+ }
143
+ export const GENERIC_ACKNOWLEDGEMENTS = new Set([
144
+ 'yes',
145
+ 'y',
146
+ 'yeah',
147
+ 'yep',
148
+ 'yup',
149
+ 'ok',
150
+ 'okay',
151
+ 'k',
152
+ 'sure',
153
+ 'fine',
154
+ 'no',
155
+ 'n',
156
+ 'nope',
157
+ 'nah',
158
+ 'again',
159
+ 'continue',
160
+ 'go on',
161
+ 'go ahead',
162
+ 'keep going',
163
+ 'proceed',
164
+ 'do it',
165
+ 'more',
166
+ 'please',
167
+ 'thanks',
168
+ 'thank you',
169
+ 'retry',
170
+ 'redo'
171
+ ]);
172
+ export function isGenericAcknowledgement(question) {
173
+ return GENERIC_ACKNOWLEDGEMENTS.has(question.replace(/[?.!]+$/, '').trim());
174
+ }
175
+ export function staleToolRunIds(historyChunks, approvalChunks, currentRunId) {
176
+ const empty = { runIds: new Set(), questionChunkIds: new Set() };
177
+ const toolRuns = new Set();
178
+ for (const chunk of approvalChunks) {
179
+ if (chunk.runId)
180
+ toolRuns.add(chunk.runId);
181
+ }
182
+ if (!toolRuns.size)
183
+ return empty;
184
+ const turnOrder = [];
185
+ const turnText = new Map();
186
+ const turnRuns = new Map();
187
+ const runChunk = new Map();
188
+ let lastUser = null;
189
+ for (const chunk of historyChunks) {
190
+ if (chunk.userMessage) {
191
+ lastUser = { text: chunk.userMessage.text, chunkId: chunk.id };
192
+ continue;
193
+ }
194
+ if (chunk.type === 'run-request' && chunk.runId && lastUser !== null) {
195
+ const chunkId = lastUser.chunkId;
196
+ if (!turnText.has(chunkId)) {
197
+ turnOrder.push(chunkId);
198
+ turnText.set(chunkId, lastUser.text);
199
+ }
200
+ turnRuns.set(chunkId, [...(turnRuns.get(chunkId) ?? []), chunk.runId]);
201
+ runChunk.set(chunk.runId, chunkId);
202
+ }
203
+ }
204
+ const currentChunkId = runChunk.get(currentRunId) ?? lastUser?.chunkId;
205
+ if (currentChunkId === undefined)
206
+ return empty;
207
+ const currentText = turnText.get(currentChunkId) ?? lastUser?.text;
208
+ if (currentText === undefined)
209
+ return empty;
210
+ const target = normalizeQuestion(currentText);
211
+ if (isGenericAcknowledgement(target))
212
+ return empty;
213
+ const currentIndex = turnOrder.indexOf(currentChunkId);
214
+ const start = currentIndex === -1 ? turnOrder.length - 1 : currentIndex - 1;
215
+ const runIds = new Set();
216
+ const questionChunkIds = new Set();
217
+ let blockHadApproval = false;
218
+ for (let i = start; i >= 0; i--) {
219
+ const chunkId = turnOrder[i];
220
+ if (normalizeQuestion(turnText.get(chunkId)) !== target)
221
+ break;
222
+ questionChunkIds.add(chunkId);
223
+ for (const rid of turnRuns.get(chunkId) ?? []) {
224
+ runIds.add(rid);
225
+ if (toolRuns.has(rid))
226
+ blockHadApproval = true;
227
+ }
228
+ }
229
+ if (!blockHadApproval)
230
+ return empty;
231
+ return { runIds, questionChunkIds };
232
+ }
@@ -0,0 +1,45 @@
1
+ import type { DocumentAttachment } from '../core/document-attachment.ts';
2
+ export interface ChatMessage {
3
+ role: 'user' | 'assistant' | 'tool';
4
+ content: string;
5
+ attachments?: {
6
+ path: string;
7
+ }[];
8
+ images?: AttachmentRef[];
9
+ audioAttachments?: AttachmentRef[];
10
+ documents?: DocumentAttachment[];
11
+ }
12
+ export declare const IMAGE_ONLY_USER_PROMPT = "The user sent one or more images without any accompanying text. Describe what the images show and respond to anything notable in them.";
13
+ export declare const DOCUMENT_ONLY_USER_PROMPT = "The user sent one or more documents without any accompanying text. Summarize the attached documents.";
14
+ export declare const IMAGE_AND_DOCUMENT_USER_PROMPT = "The user sent one or more images together with one or more documents, and no accompanying text. Describe what the images show and summarize the attached documents.";
15
+ export declare function backfillDocumentOnlyTurn(messages: ChatMessage[]): void;
16
+ export declare function backfillImageOnlyTurns(messages: ChatMessage[]): void;
17
+ export declare function unreadableImagesNotice(fileNames: readonly string[]): string;
18
+ export declare function annotateUnreadableImages(unreadable: ReadonlyMap<ChatMessage, string[]>): void;
19
+ export declare function unheardAudioNotice(label: string, reason: string, opts?: {
20
+ retryable?: boolean;
21
+ }): string;
22
+ export declare function silentAudioNotice(fileName: string): string;
23
+ export declare function audioTranscriptBlock(fileName: string, transcript: string): string;
24
+ export interface AttachmentRef {
25
+ id: string;
26
+ fileName: string;
27
+ mimeType: string;
28
+ blobId: string;
29
+ url?: string | null;
30
+ }
31
+ export interface AttachInput {
32
+ fileName: string;
33
+ mimeType: string;
34
+ bytes: Uint8Array;
35
+ raw?: boolean;
36
+ }
37
+ export interface AttachmentMeta {
38
+ id: string;
39
+ fileName: string;
40
+ mimeType: string;
41
+ byteLength: number;
42
+ }
43
+ export interface FetchedAttachment extends AttachmentMeta {
44
+ bytes: Uint8Array;
45
+ }
@@ -0,0 +1,47 @@
1
+ export const IMAGE_ONLY_USER_PROMPT = 'The user sent one or more images without any accompanying text. Describe what the images show and respond to anything notable in them.';
2
+ export const DOCUMENT_ONLY_USER_PROMPT = 'The user sent one or more documents without any accompanying text. Summarize the attached documents.';
3
+ export const IMAGE_AND_DOCUMENT_USER_PROMPT = 'The user sent one or more images together with one or more documents, and no accompanying text. Describe what the images show and summarize the attached documents.';
4
+ export function backfillDocumentOnlyTurn(messages) {
5
+ const last = messages.findLast((turn) => turn.role === 'user');
6
+ if (!last || last.content.trim())
7
+ return;
8
+ last.content = last.attachments?.length
9
+ ? IMAGE_AND_DOCUMENT_USER_PROMPT
10
+ : DOCUMENT_ONLY_USER_PROMPT;
11
+ }
12
+ export function backfillImageOnlyTurns(messages) {
13
+ for (const turn of messages) {
14
+ if (turn.role !== 'user' || turn.content.trim())
15
+ continue;
16
+ if (turn.attachments?.length)
17
+ turn.content = IMAGE_ONLY_USER_PROMPT;
18
+ }
19
+ }
20
+ export function unreadableImagesNotice(fileNames) {
21
+ const plural = fileNames.length > 1;
22
+ return (`[Attached image${plural ? 's' : ''}: ${fileNames.join(', ')} — could not be read, so ` +
23
+ `${plural ? 'they are' : 'it is'} not visible to you. Tell the user the upload failed and ` +
24
+ `ask for a re-upload; never describe or recreate ${plural ? 'them' : 'it'}]`);
25
+ }
26
+ export function annotateUnreadableImages(unreadable) {
27
+ for (const [turn, names] of unreadable) {
28
+ const notice = unreadableImagesNotice(names);
29
+ turn.content = turn.content ? `${turn.content}\n${notice}` : notice;
30
+ }
31
+ }
32
+ export function unheardAudioNotice(label, reason, opts = {}) {
33
+ const retry = opts.retryable === false ? '' : ', and ask them to try again';
34
+ return (`${label} could not be transcribed (${reason}). Tell the user the audio was not ` +
35
+ `transcribed and why${retry}; never invent what it might contain.`);
36
+ }
37
+ export function silentAudioNotice(fileName) {
38
+ return (`Attached audio "${fileName}" was transcribed and contained no detectable speech. ` +
39
+ 'Tell the user; never invent what it might contain.');
40
+ }
41
+ export function audioTranscriptBlock(fileName, transcript) {
42
+ return [
43
+ `The audio file "${fileName}" has already been transcribed on-device. You cannot open the file itself — use the transcript below as its full content and answer from it.`,
44
+ 'Transcript:',
45
+ transcript
46
+ ].join('\n');
47
+ }
@@ -0,0 +1,21 @@
1
+ import type { InstallStep, SkillRequires, SkillSetup } from '@qvac/harness';
2
+ import type { AssistantSkill } from '../../spec/hyperschema/types.d.ts';
3
+ export interface Skill {
4
+ name: string;
5
+ description: string;
6
+ status: string;
7
+ tools: string[];
8
+ credentials: string[];
9
+ allowList: string[];
10
+ platform: string[];
11
+ requires?: SkillRequires;
12
+ install?: InstallStep[];
13
+ setup?: SkillSetup;
14
+ stub?: boolean;
15
+ minCapabilityProfile?: string;
16
+ emoji?: string;
17
+ }
18
+ export declare function toEngineSkill(skill: Skill): AssistantSkill;
19
+ export declare function toEngineSkillRequirements(requires: SkillRequires): NonNullable<AssistantSkill['requirements']>;
20
+ export declare function toEngineInstallStep(step: InstallStep): NonNullable<AssistantSkill['install']>[number];
21
+ export declare function toEngineSkillSetup(setup: SkillSetup): NonNullable<AssistantSkill['setup']>;
@@ -0,0 +1,71 @@
1
+ import { readSetup as normalizeSkillSetup } from '@qvac/harness/lib/tools/skills/manifest-parse.ts';
2
+ export function toEngineSkill(skill) {
3
+ const setup = normalizeSkillSetup(skill.setup);
4
+ return {
5
+ name: skill.name,
6
+ description: skill.description,
7
+ tools: [...skill.tools],
8
+ credentials: [...skill.credentials],
9
+ allowList: [...skill.allowList],
10
+ platforms: [...skill.platform],
11
+ status: skill.status,
12
+ ...(skill.requires ? { requirements: toEngineSkillRequirements(skill.requires) } : {}),
13
+ ...(skill.install ? { install: skill.install.map(toEngineInstallStep) } : {}),
14
+ ...(setup ? { setup: toEngineSkillSetup(setup) } : {}),
15
+ ...(skill.stub !== undefined ? { stub: skill.stub } : {}),
16
+ ...(skill.minCapabilityProfile ? { minCapabilityProfile: skill.minCapabilityProfile } : {}),
17
+ ...(skill.emoji ? { emoji: skill.emoji } : {})
18
+ };
19
+ }
20
+ export function toEngineSkillRequirements(requires) {
21
+ return {
22
+ binaries: (requires.bins ?? []).map((bin) => ({
23
+ bin,
24
+ minVersion: requires.binMinVersions?.[bin]?.min,
25
+ command: requires.binMinVersions?.[bin]?.command
26
+ })),
27
+ credentials: [...(requires.credentials ?? [])]
28
+ };
29
+ }
30
+ export function toEngineInstallStep(step) {
31
+ return {
32
+ id: step.id,
33
+ kind: step.kind,
34
+ label: step.label,
35
+ ...(step.formula ? { formula: step.formula } : {}),
36
+ ...(step.command ? { command: step.command } : {}),
37
+ ...(step.manual !== undefined ? { manual: step.manual } : {}),
38
+ ...(step.bins ? { bins: [...step.bins] } : {})
39
+ };
40
+ }
41
+ export function toEngineSkillSetup(setup) {
42
+ return {
43
+ ...(setup.summary ? { summary: setup.summary } : {}),
44
+ ...(setup.routes
45
+ ? {
46
+ routes: setup.routes.map((route) => ({
47
+ kind: route.kind,
48
+ label: route.label,
49
+ ...(route.provider ? { provider: route.provider } : {}),
50
+ ...(route.credentialKey ? { credentialKey: route.credentialKey } : {}),
51
+ ...(route.description ? { description: route.description } : {}),
52
+ ...(route.helpUrl ? { helpUrl: route.helpUrl } : {}),
53
+ ...(route.steps ? { steps: [...route.steps] } : {}),
54
+ ...(route.configSteps ? { configSteps: [...route.configSteps] } : {}),
55
+ ...(route.fields
56
+ ? {
57
+ fields: route.fields.map((field) => ({
58
+ key: field.key,
59
+ label: field.label,
60
+ ...(field.placeholder ? { placeholder: field.placeholder } : {}),
61
+ ...(field.secret !== undefined ? { secret: field.secret } : {}),
62
+ ...(field.required !== undefined ? { required: field.required } : {})
63
+ }))
64
+ }
65
+ : {}),
66
+ ...(route.satisfied !== undefined ? { satisfied: route.satisfied } : {})
67
+ }))
68
+ }
69
+ : {})
70
+ };
71
+ }
@@ -1,4 +1,4 @@
1
- import { Client } from "./client.js";
1
+ import { Client } from './client.js';
2
2
  import { duplexPair } from '@qvac/harness/transport';
3
3
  export class AssistantClient extends Client {
4
4
  _inline;
@@ -27,7 +27,7 @@ export class AssistantClient extends Client {
27
27
  }
28
28
  : async () => {
29
29
  const [serverStream, clientStream] = duplexPair();
30
- const { Core } = await import("./core.js");
30
+ const { Core } = await import('./core.js');
31
31
  inline.core = new Core(() => serverStream, {
32
32
  logger: engineOptions.logger ?? null,
33
33
  storagePath,
@@ -1,7 +1,8 @@
1
1
  import Supervisor, {} from 'bare-supervisor';
2
- import { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, QvacAssistant } from "./assistant.js";
3
- import { Client } from "./client.js";
4
- import { startCustomModelPusher } from "./custom-model-pusher.js";
2
+ import { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessSkillCatalog, harnessSkillManagement, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, QvacAssistant } from './assistant.js';
3
+ import { Client } from './client.js';
4
+ import { startCustomModelPusher } from './custom-model-pusher.js';
5
+ import { serveHarness } from '@qvac/harness';
5
6
  import { duplexPair } from '@qvac/harness/transport';
6
7
  export function assistantTree(opts) {
7
8
  const { core, harness, logger = null, imageCacheDir, imageCacheMaxBytes, imageCacheMaxAgeMs, embedder, extractors, onHarness, platform, preloadCurrentAgentModel } = opts;
@@ -29,6 +30,7 @@ export function assistantTree(opts) {
29
30
  let offSpeakStream = null;
30
31
  let offModelCatalogWatch = null;
31
32
  let offCustomModels = null;
33
+ let offLoanableHarness = null;
32
34
  sup.add('engine', {
33
35
  start: async () => {
34
36
  const [server, client] = duplexPair();
@@ -61,7 +63,8 @@ export function assistantTree(opts) {
61
63
  imageCacheMaxAgeMs,
62
64
  ...(platform ? { platform } : {}),
63
65
  ...(preloadCurrentAgentModel !== undefined ? { preloadCurrentAgentModel } : {}),
64
- ...(core.fsDrives ? { drives: core.fsDrives } : {})
66
+ ...(core.fsDrives ? { drives: core.fsDrives } : {}),
67
+ borrowHarness: (deviceId) => core.borrowHarness(deviceId)
65
68
  });
66
69
  offDiagnostics = core.registerDiagnostics(() => assistant.diagnosticsSlice());
67
70
  offListModels = core.registerListModels(harnessListModels(harness));
@@ -71,6 +74,7 @@ export function assistantTree(opts) {
71
74
  offCompactChat = core.registerCompactChat((chatId) => assistant.compactChat(chatId));
72
75
  offModelCatalogWatch = core.registerModelCatalogWatch(() => assistant.modelCatalogWatch());
73
76
  offCustomModels = startCustomModelPusher(harness, core.local, logger);
77
+ offLoanableHarness = core.registerLoanableHarness((stream) => serveHarness(stream, harness));
74
78
  offRetryModelDownload = core.registerRetryModelDownload((name) => assistant.retryModelDownload(name));
75
79
  offSkills = core.registerSkills((agentId, modelName, options) => assistant.skills(agentId, modelName, options));
76
80
  const transcribe = harnessTranscribe(harness);
@@ -126,6 +130,8 @@ export function assistantTree(opts) {
126
130
  offModelCatalogWatch = null;
127
131
  await offCustomModels?.();
128
132
  offCustomModels = null;
133
+ offLoanableHarness?.();
134
+ offLoanableHarness = null;
129
135
  offRetryModelDownload?.();
130
136
  offRetryModelDownload = null;
131
137
  offSkills?.();