@qvac/core 1.0.0-rc.7 → 1.0.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 (237) hide show
  1. package/CHANGELOG.md +241 -3
  2. package/README.md +1 -0
  3. package/dist-lib/lib/api/attachments.d.ts +1 -1
  4. package/dist-lib/lib/api/attachments.js +11 -0
  5. package/dist-lib/lib/api/chunks.d.ts +4 -2
  6. package/dist-lib/lib/api/chunks.js +6 -3
  7. package/dist-lib/lib/api/device-routes.d.ts +6 -1
  8. package/dist-lib/lib/api/device-routes.js +20 -1
  9. package/dist-lib/lib/api/devices.js +6 -18
  10. package/dist-lib/lib/api/execution.d.ts +6 -2
  11. package/dist-lib/lib/api/execution.js +29 -3
  12. package/dist-lib/lib/api/host-run-log.d.ts +7 -1
  13. package/dist-lib/lib/api/host-run-log.js +3 -1
  14. package/dist-lib/lib/api/index.d.ts +3 -1
  15. package/dist-lib/lib/api/index.js +17 -6
  16. package/dist-lib/lib/api/members.js +13 -29
  17. package/dist-lib/lib/api/mesh.d.ts +2 -1
  18. package/dist-lib/lib/api/projects.d.ts +1 -1
  19. package/dist-lib/lib/api/projects.js +9 -5
  20. package/dist-lib/lib/api/rag-label.d.ts +4 -0
  21. package/dist-lib/lib/api/rag-label.js +21 -0
  22. package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
  23. package/dist-lib/lib/api/rag-lexical.js +1058 -0
  24. package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
  25. package/dist-lib/lib/api/rag-manifest.js +46 -0
  26. package/dist-lib/lib/api/rag-outline.d.ts +36 -0
  27. package/dist-lib/lib/api/rag-outline.js +426 -0
  28. package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
  29. package/dist-lib/lib/api/rag-plugin.js +70 -0
  30. package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
  31. package/dist-lib/lib/api/rag-synthesis.js +102 -0
  32. package/dist-lib/lib/api/rag-targets.d.ts +23 -0
  33. package/dist-lib/lib/api/rag-targets.js +215 -0
  34. package/dist-lib/lib/api/rag-window.d.ts +5 -0
  35. package/dist-lib/lib/api/rag-window.js +46 -0
  36. package/dist-lib/lib/api/rag.d.ts +3 -2
  37. package/dist-lib/lib/api/rag.js +421 -184
  38. package/dist-lib/lib/api/served.js +8 -14
  39. package/dist-lib/lib/api/workspace.d.ts +8 -1
  40. package/dist-lib/lib/api/workspace.js +27 -13
  41. package/dist-lib/lib/assistant/harness.d.ts +9 -1
  42. package/dist-lib/lib/assistant/history.d.ts +11 -2
  43. package/dist-lib/lib/assistant/history.js +74 -4
  44. package/dist-lib/lib/assistant/media-round.d.ts +61 -0
  45. package/dist-lib/lib/assistant/media-round.js +111 -0
  46. package/dist-lib/lib/assistant/message.d.ts +7 -0
  47. package/dist-lib/lib/assistant/model-round.d.ts +19 -1
  48. package/dist-lib/lib/assistant/model-round.js +163 -28
  49. package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
  50. package/dist-lib/lib/assistant/pdf-pages.js +67 -3
  51. package/dist-lib/lib/assistant/round-host.d.ts +22 -0
  52. package/dist-lib/lib/assistant/round-host.js +142 -8
  53. package/dist-lib/lib/assistant/upload.d.ts +12 -0
  54. package/dist-lib/lib/assistant/upload.js +36 -0
  55. package/dist-lib/lib/assistant-client.d.ts +1 -0
  56. package/dist-lib/lib/assistant-client.js +40 -19
  57. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  58. package/dist-lib/lib/assistant-tree.js +48 -1
  59. package/dist-lib/lib/assistant.d.ts +18 -1
  60. package/dist-lib/lib/assistant.js +484 -95
  61. package/dist-lib/lib/client.d.ts +1 -0
  62. package/dist-lib/lib/client.js +4 -1
  63. package/dist-lib/lib/compaction.js +2 -2
  64. package/dist-lib/lib/core/agent.js +12 -45
  65. package/dist-lib/lib/core/approval-rules.js +1 -8
  66. package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
  67. package/dist-lib/lib/core/archived-mesh.js +12 -8
  68. package/dist-lib/lib/core/chat-copy.d.ts +18 -3
  69. package/dist-lib/lib/core/chat-copy.js +74 -25
  70. package/dist-lib/lib/core/chat-fork.js +15 -6
  71. package/dist-lib/lib/core/chat-group.js +16 -24
  72. package/dist-lib/lib/core/chat-move.d.ts +9 -0
  73. package/dist-lib/lib/core/chat-move.js +94 -3
  74. package/dist-lib/lib/core/chat-order.js +12 -18
  75. package/dist-lib/lib/core/chat.js +38 -31
  76. package/dist-lib/lib/core/chunk.d.ts +2 -2
  77. package/dist-lib/lib/core/chunk.js +18 -25
  78. package/dist-lib/lib/core/collections.d.ts +2 -0
  79. package/dist-lib/lib/core/collections.js +2 -0
  80. package/dist-lib/lib/core/contract/activity.d.ts +4 -0
  81. package/dist-lib/lib/core/contract/activity.js +34 -4
  82. package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
  83. package/dist-lib/lib/core/contract/chat-group.js +21 -1
  84. package/dist-lib/lib/core/contract/chat.d.ts +9 -0
  85. package/dist-lib/lib/core/contract/chat.js +127 -5
  86. package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
  87. package/dist-lib/lib/core/contract/chunks.js +37 -6
  88. package/dist-lib/lib/core/contract/features.d.ts +21 -0
  89. package/dist-lib/lib/core/contract/features.js +22 -0
  90. package/dist-lib/lib/core/contract/index.js +2 -0
  91. package/dist-lib/lib/core/contract/indexing.js +4 -2
  92. package/dist-lib/lib/core/contract/knowledge.js +5 -4
  93. package/dist-lib/lib/core/contract/members.js +16 -10
  94. package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
  95. package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
  96. package/dist-lib/lib/core/contract/projects.d.ts +5 -0
  97. package/dist-lib/lib/core/contract/projects.js +13 -5
  98. package/dist-lib/lib/core/contract/roles.js +32 -4
  99. package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
  100. package/dist-lib/lib/core/contract/run-requests.js +35 -2
  101. package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
  102. package/dist-lib/lib/core/contract/tool-configs.js +3 -2
  103. package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
  104. package/dist-lib/lib/core/contract/workspace.js +40 -7
  105. package/dist-lib/lib/core/contract/writers.js +2 -2
  106. package/dist-lib/lib/core/departures.d.ts +3 -1
  107. package/dist-lib/lib/core/departures.js +3 -0
  108. package/dist-lib/lib/core/devices.js +1 -8
  109. package/dist-lib/lib/core/files/attachments.d.ts +2 -0
  110. package/dist-lib/lib/core/files/attachments.js +19 -1
  111. package/dist-lib/lib/core/files/index.d.ts +20 -4
  112. package/dist-lib/lib/core/files/index.js +132 -21
  113. package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
  114. package/dist-lib/lib/core/files/processing/document.js +7 -0
  115. package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
  116. package/dist-lib/lib/core/files/processing/index.js +144 -12
  117. package/dist-lib/lib/core/files/processing/protocol.js +13 -34
  118. package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
  119. package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
  120. package/dist-lib/lib/core/files/rag/build.js +123 -19
  121. package/dist-lib/lib/core/files/rag/failure.js +2 -1
  122. package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
  123. package/dist-lib/lib/core/files/rag/index.js +13 -1
  124. package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
  125. package/dist-lib/lib/core/files/rag/load.js +27 -6
  126. package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
  127. package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
  128. package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
  129. package/dist-lib/lib/core/files/rag/produce.js +104 -16
  130. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
  131. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
  132. package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
  133. package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
  134. package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
  135. package/dist-lib/lib/core/files/records/knowledge.js +7 -40
  136. package/dist-lib/lib/core/files/records/reclaim.js +5 -10
  137. package/dist-lib/lib/core/files/upload.js +68 -41
  138. package/dist-lib/lib/core/fs-drives.js +14 -26
  139. package/dist-lib/lib/core/invite.d.ts +1 -0
  140. package/dist-lib/lib/core/invite.js +4 -2
  141. package/dist-lib/lib/core/local/files.d.ts +1 -1
  142. package/dist-lib/lib/core/local/files.js +5 -5
  143. package/dist-lib/lib/core/local/index.d.ts +13 -6
  144. package/dist-lib/lib/core/local/index.js +103 -69
  145. package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
  146. package/dist-lib/lib/core/local/run-logs.js +5 -5
  147. package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
  148. package/dist-lib/lib/core/mesh-processor.js +87 -0
  149. package/dist-lib/lib/core/mesh.d.ts +48 -5
  150. package/dist-lib/lib/core/mesh.js +318 -129
  151. package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
  152. package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
  153. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
  154. package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
  155. package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
  156. package/dist-lib/lib/core/migrations/writer-links.js +32 -0
  157. package/dist-lib/lib/core/project-keys.d.ts +6 -0
  158. package/dist-lib/lib/core/project-keys.js +11 -0
  159. package/dist-lib/lib/core/public-mesh.d.ts +3 -0
  160. package/dist-lib/lib/core/public-mesh.js +10 -8
  161. package/dist-lib/lib/core/run-requests.d.ts +3 -0
  162. package/dist-lib/lib/core/run-requests.js +21 -1
  163. package/dist-lib/lib/core/skill-availability.d.ts +33 -0
  164. package/dist-lib/lib/core/skill-availability.js +67 -0
  165. package/dist-lib/lib/core/sync/background.d.ts +0 -1
  166. package/dist-lib/lib/core/sync/background.js +2 -1
  167. package/dist-lib/lib/core/sync/changed.d.ts +4 -2
  168. package/dist-lib/lib/core/sync/changed.js +1 -1
  169. package/dist-lib/lib/core/sync/index.js +52 -10
  170. package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
  171. package/dist-lib/lib/core/sync/view-updates.js +82 -91
  172. package/dist-lib/lib/core/sync-status.js +97 -41
  173. package/dist-lib/lib/core/tool-activity.js +6 -12
  174. package/dist-lib/lib/core/tool-configs.js +17 -29
  175. package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
  176. package/dist-lib/lib/core/upgrade-summary.js +67 -0
  177. package/dist-lib/lib/core/view-read.d.ts +4 -0
  178. package/dist-lib/lib/core/view-read.js +23 -0
  179. package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
  180. package/dist-lib/lib/core/workspace-upgrade.js +27 -16
  181. package/dist-lib/lib/core.d.ts +45 -42
  182. package/dist-lib/lib/core.js +675 -640
  183. package/dist-lib/lib/errors.d.ts +24 -0
  184. package/dist-lib/lib/errors.js +24 -0
  185. package/dist-lib/lib/llm-model-names.d.ts +1 -1
  186. package/dist-lib/lib/llm-model-names.js +1 -1
  187. package/dist-lib/lib/mesh-scoped-engine.js +4 -0
  188. package/dist-lib/lib/model-context.js +1 -2
  189. package/dist-lib/lib/model-download.d.ts +6 -1
  190. package/dist-lib/lib/model-download.js +16 -3
  191. package/dist-lib/lib/oauth/resolve.js +29 -8
  192. package/dist-lib/lib/oauth/token-exchange-core.js +11 -0
  193. package/dist-lib/lib/probe-rounds.d.ts +7 -2
  194. package/dist-lib/lib/probe-rounds.js +6 -1
  195. package/dist-lib/lib/run-transcript.d.ts +4 -1
  196. package/dist-lib/lib/run-transcript.js +15 -6
  197. package/dist-lib/schema/engine.js +83 -6
  198. package/dist-lib/schema/indexes.d.ts +3 -0
  199. package/dist-lib/schema/indexes.js +5 -0
  200. package/dist-lib/schema/local.js +11 -2
  201. package/dist-lib/schema/qvac.js +49 -5
  202. package/dist-lib/spec/bind.js +34 -0
  203. package/dist-lib/spec/calls.js +5 -0
  204. package/dist-lib/spec/capabilities.d.ts +17 -0
  205. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  206. package/dist-lib/spec/hrpc/index.js +68 -5
  207. package/dist-lib/spec/hrpc/messages.js +260 -23
  208. package/dist-lib/spec/hyperschema/index.js +260 -23
  209. package/dist-lib/spec/hyperschema/schema.json +181 -0
  210. package/dist-lib/spec/hyperschema/types.d.ts +48 -0
  211. package/dist-lib/spec/local/hyperdb/messages.js +270 -224
  212. package/dist-lib/spec/local/hyperschema/index.js +73 -38
  213. package/dist-lib/spec/local/hyperschema/schema.json +31 -0
  214. package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
  215. package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
  216. package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
  217. package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
  218. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
  219. package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
  220. package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
  221. package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
  222. package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
  223. package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
  224. package/package.json +82 -71
  225. package/tui/README.md +52 -0
  226. package/tui/app.mjs +69 -12
  227. package/tui/boot.mjs +27 -6
  228. package/tui/cli.mjs +93 -0
  229. package/tui/index.mjs +2 -87
  230. package/tui/lib/attachment-files.mjs +82 -0
  231. package/tui/lib/reopen-meshes.mjs +15 -0
  232. package/tui/lib/updater.mjs +86 -0
  233. package/tui/main.mjs +29 -57
  234. package/worker/assistant-sidecar.mjs +10 -3
  235. package/worker/stow-entry.mjs +3 -52
  236. package/dist-lib/lib/core/chat-project.d.ts +0 -2
  237. package/dist-lib/lib/core/chat-project.js +0 -15
@@ -0,0 +1,102 @@
1
+ import { isWholeDocumentQuery, parseRagMetadata } from 'rag-extract';
2
+ import { asksForItems, keptWords } from './rag-lexical.js';
3
+ import { shortTitle } from './rag-label.js';
4
+ const SUMMARY_VERBS = new Set([
5
+ 'summarize',
6
+ 'summarise',
7
+ 'summarizing',
8
+ 'summarising',
9
+ 'condense',
10
+ 'recap',
11
+ 'resume',
12
+ 'resumir'
13
+ ]);
14
+ const GENERATION_VERBS = new Set([
15
+ 'write',
16
+ 'draft',
17
+ 'produce',
18
+ 'generate',
19
+ 'create',
20
+ 'prepare',
21
+ 'make',
22
+ 'compile',
23
+ 'build',
24
+ 'turn',
25
+ 'genera',
26
+ 'generar',
27
+ 'redacta',
28
+ 'redactar',
29
+ 'elabora',
30
+ 'elaborar',
31
+ 'prepara',
32
+ 'preparar',
33
+ 'haz',
34
+ 'hazme',
35
+ 'hacer'
36
+ ]);
37
+ const words = (list) => new Set(list.split(' '));
38
+ const FORMATS = words('sheet summary memo report table outline brief checklist overview digest note notes deck ' +
39
+ 'abstract synopsis faq draft version resumen ficha informe memorando');
40
+ const SOURCES = words('this that these those attached uploaded document documents agreement agreements file files ' +
41
+ 'contract law act policy text pdf paper manual regulation regulations source sources ' +
42
+ 'este esta estos estas ese esa documento documentos contrato contratos acuerdo acuerdos ' +
43
+ 'ley leyes archivo archivos texto');
44
+ const OUTPUTS = words('word docx doc excel xlsx spreadsheet slides pptx markdown md txt csv');
45
+ const MODIFIERS = words('one page pages short shorter long brief detailed bullet bullets bulleted point points concise ' +
46
+ 'simple plain quick full complete entire whole key main spanish english french german ' +
47
+ 'portuguese español inglés francés');
48
+ const FILLER = words('the a an all of un una unos unas del los las con por para sobre need needs want wants like give ' +
49
+ 'send get save necesito quiero dame');
50
+ const FORMAT_PHRASES = /term sheet|spread[- ]sheet|hoja de t[eé]rminos|ficha t[eé]cnica/g;
51
+ const MENTION = /^\s*@[a-z0-9][a-z0-9_-]*/;
52
+ const POLITE = /^(?:(?:please|kindly|por favor)\s+|(?:can|could|would|will)\s+you\s+)+/;
53
+ const WANTS = /^(?:(?:i|we)\s+(?:need|want|would\s+like)|i'd\s+like|(?:give|get|send)\s+me|necesito|quiero|dame)\s+(?:you\s+to\s+)?/;
54
+ const ABOUT = /\bwhat(?:'s|\s+is|\s+are)\s+((?:[\p{L}]+\s+){0,3}?[\p{L}]+)\s+about\b/u;
55
+ const ABOUT_ES = /\bde\s+qu[eé]\s+trat(?:a|an)\s+([\p{L}\s]+?)\s*[?!.]*$/u;
56
+ const CLAUSES = /[.!?;,\n]+|\band\b|\bthen\b|\balso\b/;
57
+ export function classifyRequest(request) {
58
+ const query = request.replace(MENTION, '');
59
+ if (asksForItems(query)) {
60
+ return { synthesis: false, wholeDocument: false };
61
+ }
62
+ const clauses = query
63
+ .toLowerCase()
64
+ .split(CLAUSES)
65
+ .map((clause) => clause.trim().replace(POLITE, ''));
66
+ const asked = clauses.map((clause) => ({
67
+ verb: clause.replace(WANTS, '').split(/\s+/)[0] ?? '',
68
+ wants: WANTS.test(clause) && clause.split(/\s+/).some((word) => FORMATS.has(word))
69
+ }));
70
+ const generic = !hasTopic(query);
71
+ const summarizes = generic && asked.some(({ verb }) => SUMMARY_VERBS.has(verb));
72
+ const wholeDocument = isWholeDocumentQuery(query) || summarizes || asksAbout(query);
73
+ const derives = asked.some(({ verb, wants }) => wants || GENERATION_VERBS.has(verb));
74
+ return { synthesis: wholeDocument || (generic && derives), wholeDocument };
75
+ }
76
+ export function outlineBlock({ hits, outline }) {
77
+ if (!outline?.length) {
78
+ return undefined;
79
+ }
80
+ const title = parseRagMetadata(hits[0]?.metadataJson ?? null)?.title;
81
+ return [
82
+ `Document outline${title ? ` (${shortTitle(title)})` : ''}. A section listed here with no excerpt below was not read: refer to it by its number and never call it blank or missing.`,
83
+ ...outline
84
+ ];
85
+ }
86
+ function asksAbout(query) {
87
+ const lower = query.toLowerCase().trim();
88
+ const subject = ABOUT.exec(lower)?.[1] ?? ABOUT_ES.exec(lower)?.[1];
89
+ return (subject !== undefined &&
90
+ subject.split(/\s+/).every((word) => SOURCES.has(word) || FILLER.has(word)));
91
+ }
92
+ function hasTopic(query) {
93
+ return keptWords(query.replace(FORMAT_PHRASES, ' ')).some((word) => !word
94
+ .split('-')
95
+ .every((part) => FORMATS.has(part) ||
96
+ SOURCES.has(part) ||
97
+ MODIFIERS.has(part) ||
98
+ OUTPUTS.has(part) ||
99
+ FILLER.has(part) ||
100
+ SUMMARY_VERBS.has(part) ||
101
+ GENERATION_VERBS.has(part)));
102
+ }
@@ -0,0 +1,23 @@
1
+ import { type Outline } from './rag-outline.ts';
2
+ export interface Target {
3
+ readonly label: string;
4
+ readonly start: number;
5
+ readonly end: number;
6
+ readonly chars: number;
7
+ readonly focus?: number | undefined;
8
+ }
9
+ export interface TargetOptions {
10
+ readonly budgetChars: number;
11
+ readonly snippetChars: number;
12
+ readonly documents?: number;
13
+ }
14
+ export interface Plan {
15
+ readonly front: Target[];
16
+ readonly terms: Target[];
17
+ readonly rest: Target[];
18
+ readonly spent: number;
19
+ }
20
+ export declare const DEFINITION_SHARE = 0.4;
21
+ export declare function planOf(outline: Outline, lines: ReadonlyArray<string>, options: TargetOptions): Plan;
22
+ export declare function referenced(terms: ReadonlyArray<Target>, text: string): Target[];
23
+ export declare function setsValue(lines: ReadonlyArray<string>): boolean;
@@ -0,0 +1,215 @@
1
+ import { isDefinition, isSubstantive, occurrences } from './rag-outline.js';
2
+ const VALUED_SHARE = 0.5;
3
+ const DEFINITION_CHARS_SHARE = 0.5;
4
+ const DEFINITION_LINES = 6;
5
+ const MIN_VALUED_REFS = 3;
6
+ const PREAMBLE_SNIPPETS = 2;
7
+ const GLIMPSE_CHARS = 300;
8
+ const VALUE_LOOKAHEAD = 10;
9
+ const LEAD_LINES = 3;
10
+ const VALUE_CHARS = 520;
11
+ const MIN_WINDOW_CHARS = 60;
12
+ const MAX_CONTINUATION_CHARS = 80;
13
+ const MIN_WINDOWS = 3;
14
+ const NUMERIC = /^\d/;
15
+ const SPARE = 0.9;
16
+ const APPENDIX = /^(?:schedule|annex|appendix)\b/i;
17
+ const SENTENCE_END = /[.;:!?)\]”"]$/;
18
+ const CONTINUES = /[;.,]$|\b(?:and|or)$/;
19
+ const VALUE = /\[\s*\]|\d(?:[.,]\d+)*\s*(?:%|per cent)|[$€£¥]\s?\d/u;
20
+ export const DEFINITION_SHARE = 0.4;
21
+ export function planOf(outline, lines, options) {
22
+ const { preamble, headings } = outline;
23
+ const parties = preamble ? [preambleTarget(preamble, options)] : [];
24
+ const appendix = headings.find(({ toc, number }) => !toc && APPENDIX.test(number ?? ''))?.line ?? Infinity;
25
+ const { valued, others } = definitionTargets(outline, lines, { ...options, appendix });
26
+ const reserved = Math.min(options.budgetChars * DEFINITION_SHARE, sum(valued) + sum(others));
27
+ const left = options.budgetChars - reserved - sum(parties);
28
+ const { main, rest } = sectionTargets(outline, lines, { ...options, appendix, left });
29
+ const structure = valued.length + main.length + others.length + rest.length;
30
+ const windows = structure === 0 ? windowTargets(lines, options) : [];
31
+ return {
32
+ front: [...parties, ...valued, ...main, ...windows],
33
+ terms: others,
34
+ rest,
35
+ spent: sum(valued)
36
+ };
37
+ }
38
+ export function referenced(terms, text) {
39
+ return terms
40
+ .map((target) => ({ target, uses: occurrences(text, target.label) }))
41
+ .filter(({ uses }) => uses > 0)
42
+ .sort((a, b) => b.uses - a.uses)
43
+ .map(({ target }) => target);
44
+ }
45
+ function sum(targets) {
46
+ return targets.reduce((total, { chars }) => total + chars, 0);
47
+ }
48
+ function preambleTarget([start, end], { snippetChars }) {
49
+ return { label: '', start, end, chars: snippetChars * PREAMBLE_SNIPPETS };
50
+ }
51
+ function windowTargets(lines, { budgetChars, snippetChars, documents = 1 }) {
52
+ const substantive = lines.flatMap((line, at) => (isSubstantive(line) ? [at] : []));
53
+ const paragraphs = substantive.filter((at) => !lines[at - 1]?.trim());
54
+ const pool = paragraphs.length >= MIN_WINDOWS ? paragraphs : substantive;
55
+ const count = Math.min(pool.length, Math.max(MIN_WINDOWS, Math.floor(budgetChars / documents / snippetChars)));
56
+ const spread = (i) => (count > 1 ? Math.round((i * (pool.length - 1)) / (count - 1)) : 0);
57
+ return Array.from({ length: count }, (_, i) => pool[spread(i)])
58
+ .filter((start) => start !== undefined)
59
+ .map((start) => ({ label: '', start, end: lines.length, chars: snippetChars }));
60
+ }
61
+ function definitionTargets({ definitions }, lines, { budgetChars, snippetChars, appendix }) {
62
+ const chars = Math.floor(snippetChars * DEFINITION_CHARS_SHARE);
63
+ const described = definitions.map((definition, at) => ({
64
+ definition,
65
+ body: lines.slice(definition.line, Math.min(definitions[at + 1]?.line ?? lines.length, definition.line + DEFINITION_LINES))
66
+ }));
67
+ const targetOf = ({ definition, body }) => ({
68
+ label: definition.term,
69
+ start: definition.line,
70
+ end: lines.length,
71
+ chars: Math.min(body.reduce((total, text) => total + text.length + 1, 0), chars)
72
+ });
73
+ const byRefs = [...described].sort((a, b) => Number(a.definition.line >= appendix) - Number(b.definition.line >= appendix) ||
74
+ b.definition.refs - a.definition.refs ||
75
+ a.definition.line - b.definition.line);
76
+ const valued = [];
77
+ let left = budgetChars * DEFINITION_SHARE * VALUED_SHARE;
78
+ for (const entry of byRefs) {
79
+ const target = targetOf(entry);
80
+ if (entry.definition.refs < MIN_VALUED_REFS ||
81
+ entry.definition.line >= appendix ||
82
+ !setsValue(entry.body) ||
83
+ target.chars > left) {
84
+ continue;
85
+ }
86
+ valued.push(target);
87
+ left -= target.chars;
88
+ }
89
+ const taken = new Set(valued.map(({ start }) => start));
90
+ return {
91
+ valued,
92
+ others: byRefs.filter(({ definition }) => !taken.has(definition.line)).map(targetOf)
93
+ };
94
+ }
95
+ export function setsValue(lines) {
96
+ return lines.some((line) => VALUE.test(line));
97
+ }
98
+ function sectionTargets({ headings, contents }, lines, options) {
99
+ const { snippetChars, appendix, left } = options;
100
+ const texted = headings.filter((heading) => hasText(heading, lines));
101
+ const mapped = contents.flatMap(({ text, line, end }) => line === null ? [] : [{ label: text, line, end }]);
102
+ const tops = mapped.length > 0 ? mapped : mainHeadings(texted, appendix);
103
+ const body = headings.filter(({ toc }) => !toc);
104
+ const titles = {
105
+ lines: new Set(body.flatMap(({ line, size }) => Array.from({ length: size }, (_, i) => line + i))),
106
+ sizes: new Map(body.map(({ line, size }) => [line, size]))
107
+ };
108
+ const found = tops.flatMap((section) => glimpseOf(section, lines, titles));
109
+ const valuedFirst = found.filter(({ hasValue }) => hasValue).length * 2 <= found.length;
110
+ const glimpses = found.sort((a, b) => Number(a.line >= appendix) - Number(b.line >= appendix) ||
111
+ Number(valuedFirst && b.hasValue) - Number(valuedFirst && a.hasValue) ||
112
+ a.line - b.line);
113
+ let room = left * SPARE;
114
+ const cut = glimpses.findIndex(({ target }) => (room -= target.chars) < 0);
115
+ const main = glimpses.slice(0, cut < 0 ? glimpses.length : cut);
116
+ const chosen = new Set(tops.map(({ line }) => line));
117
+ const valued = new Map(texted.map((heading) => [heading, setsValue(lines.slice(heading.line, heading.own))]));
118
+ const others = texted
119
+ .filter((heading) => !chosen.has(heading.line) && hasText({ ...heading, end: heading.own }, lines))
120
+ .sort((a, b) => Number(a.line >= appendix) - Number(b.line >= appendix) ||
121
+ Number(valued.get(b)) - Number(valued.get(a)) ||
122
+ b.refs - a.refs ||
123
+ a.line - b.line);
124
+ return {
125
+ main: main.map(({ target }) => target),
126
+ rest: [
127
+ ...glimpses.slice(main.length).map(({ target }) => target),
128
+ ...others.map(({ title, line, own }) => ({
129
+ label: title,
130
+ start: line,
131
+ end: own,
132
+ chars: snippetChars
133
+ }))
134
+ ]
135
+ };
136
+ }
137
+ function mainHeadings(texted, appendix) {
138
+ const numbered = texted.filter(({ number }) => NUMERIC.test(number ?? '')).length * 2 >= texted.length;
139
+ const isMain = (heading) => numbered ? NUMERIC.test(heading.number ?? '') : heading.level <= 2;
140
+ return texted
141
+ .filter((heading) => heading.line === appendix || (heading.line < appendix && isMain(heading)))
142
+ .map(({ title, line, end }) => ({ label: title, line, end }));
143
+ }
144
+ function glimpseOf({ label, line, end }, lines, titles) {
145
+ const from = line + (titles.sizes.get(line) ?? 1);
146
+ const body = Array.from({ length: Math.max(0, end - from) }, (_, at) => from + at).filter((at) => lines[at]?.trim());
147
+ const has = (at) => VALUE.test(lines[at] ?? '');
148
+ const first = body.find((at) => !titles.lines.has(at) && (isSubstantive(lines[at] ?? '') || has(at)));
149
+ if (first === undefined || isDefinition(lines[first] ?? '')) {
150
+ return [];
151
+ }
152
+ const value = body.slice(0, VALUE_LOOKAHEAD).find(has);
153
+ const anchor = value ?? first;
154
+ const lead = value !== undefined && body.indexOf(value) <= LEAD_LINES
155
+ ? (body[0] ?? value)
156
+ : sentenceStart(anchor, lines, { floor: line, titles });
157
+ const lede = charsOf(lines, lead, anchor);
158
+ const chars = lede + (value === undefined ? unitChars(lines, anchor, end) : valueChars(lines, value, end));
159
+ const focus = lead === value ? VALUE.exec(lines[lead] ?? '')?.index : undefined;
160
+ return [
161
+ {
162
+ label,
163
+ line,
164
+ end,
165
+ target: { label, start: lead, end, chars, focus },
166
+ hasValue: value !== undefined
167
+ }
168
+ ];
169
+ }
170
+ function sentenceStart(at, lines, { floor, titles }) {
171
+ let start = at;
172
+ while (start - 1 > floor && !titles.lines.has(start - 1) && continues(lines[start - 1])) {
173
+ start--;
174
+ }
175
+ return start;
176
+ }
177
+ function continues(line) {
178
+ const text = line?.trim() ?? '';
179
+ return text !== '' && !SENTENCE_END.test(text);
180
+ }
181
+ function charsOf(lines, from, to) {
182
+ return lines
183
+ .slice(from, to)
184
+ .reduce((total, line) => total + (line.trim() ? line.length + 1 : 0), 0);
185
+ }
186
+ function valueChars(lines, start, end) {
187
+ let at = start;
188
+ while (at < end && (!lines[at]?.trim() || VALUE.test(lines[at] ?? ''))) {
189
+ at++;
190
+ }
191
+ return Math.min(charsOf(lines, start, at) + MIN_WINDOW_CHARS, VALUE_CHARS);
192
+ }
193
+ function unitChars(lines, start, end) {
194
+ let chars = (lines[start] ?? '').length;
195
+ for (let at = start + 1; at < end && chars < GLIMPSE_CHARS; at++) {
196
+ const text = (lines[at] ?? '').trim();
197
+ if (text && (text.length > MAX_CONTINUATION_CHARS || !CONTINUES.test(text))) {
198
+ break;
199
+ }
200
+ chars += text.length + 1;
201
+ }
202
+ return Math.min(chars, GLIMPSE_CHARS);
203
+ }
204
+ function hasText({ toc, line, size, end }, lines) {
205
+ if (toc) {
206
+ return false;
207
+ }
208
+ for (let at = line + size; at < end; at++) {
209
+ const text = lines[at] ?? '';
210
+ if (isSubstantive(text) || VALUE.test(text)) {
211
+ return true;
212
+ }
213
+ }
214
+ return false;
215
+ }
@@ -0,0 +1,5 @@
1
+ export type Cut = readonly [number, number];
2
+ export declare function windowAround(text: string, centre: number, size: number): Cut;
3
+ export declare function render(text: string, [from, to]: Cut): string;
4
+ export declare function unionOf(cuts: ReadonlyArray<Cut>, cut: Cut): Cut[];
5
+ export declare function newChars(cuts: ReadonlyArray<Cut>, cut: Cut): number;
@@ -0,0 +1,46 @@
1
+ const SENTENCE_END = /[.;:!?)\]”"]\s/g;
2
+ export function windowAround(text, centre, size) {
3
+ if (text.length <= size) {
4
+ return [0, text.length];
5
+ }
6
+ const from0 = Math.max(0, Math.min(centre - Math.floor(size / 2), text.length - size));
7
+ const to0 = from0 + size;
8
+ const ends = [...text.matchAll(SENTENCE_END)].map(({ index }) => index + 2);
9
+ const from = from0 === 0
10
+ ? 0
11
+ : (ends.find((end) => end >= from0 && end <= centre) ?? wordStart(text, from0, centre));
12
+ const to = to0 >= text.length
13
+ ? text.length
14
+ : (ends.findLast((end) => end > centre && end <= to0 && end - from >= size / 2) ??
15
+ wordEnd(text, to0, centre));
16
+ return [from, Math.max(to, Math.min(text.length, from + 1))];
17
+ }
18
+ function wordStart(text, at, centre) {
19
+ const space = text.indexOf(' ', at);
20
+ return space < 0 || space >= centre ? at : space + 1;
21
+ }
22
+ function wordEnd(text, at, centre) {
23
+ const space = text.lastIndexOf(' ', at);
24
+ return space <= centre ? at : space;
25
+ }
26
+ export function render(text, [from, to]) {
27
+ return `${from > 0 ? '…' : ''}${text.slice(from, to)}${to < text.length ? '…' : ''}`;
28
+ }
29
+ export function unionOf(cuts, cut) {
30
+ const all = [...cuts, cut].sort((a, b) => a[0] - b[0]);
31
+ const merged = [];
32
+ for (const [from, to] of all) {
33
+ const last = merged[merged.length - 1];
34
+ if (last && from <= last[1]) {
35
+ last[1] = Math.max(last[1], to);
36
+ }
37
+ else {
38
+ merged.push([from, to]);
39
+ }
40
+ }
41
+ return merged;
42
+ }
43
+ export function newChars(cuts, cut) {
44
+ const held = cuts.reduce((sum, [from, to]) => sum + Math.max(0, Math.min(to, cut[1]) - Math.max(from, cut[0])), 0);
45
+ return cut[1] - cut[0] - held;
46
+ }
@@ -1,11 +1,12 @@
1
1
  import type { CapabilityHandlers } from '../../spec/capabilities.d.ts';
2
2
  import type { QvacAgent } from '../../spec/mesh/hyperschema/types.d.ts';
3
- import { type IndexingEmbedder, type VectorIndex } from '../core/files/index.ts';
3
+ import { type IndexingEmbedder, type KnowledgeExtractors, type VectorIndex } from '../core/files/index.ts';
4
4
  import { type MeshPool } from './current-mesh.ts';
5
5
  export declare const MAX_RAG_SEARCH_LIMIT = 20;
6
6
  export interface CreateRagApiOptions {
7
7
  readonly index: (meshId: string) => VectorIndex | null;
8
8
  readonly embedder: (meshId: string) => IndexingEmbedder | null;
9
9
  readonly effectiveCtxSize?: ((agent: QvacAgent | null) => Promise<number>) | null;
10
+ readonly extractors?: () => KnowledgeExtractors | undefined;
10
11
  }
11
- export declare function createRagApi(pool: MeshPool, { index, embedder, effectiveCtxSize }: CreateRagApiOptions): Pick<CapabilityHandlers, 'ragSearch'>;
12
+ export declare function createRagApi(pool: MeshPool, { index, embedder, effectiveCtxSize, extractors }: CreateRagApiOptions): Pick<CapabilityHandlers, 'ragSearch'>;