@qvac/core 0.10.4 → 0.11.1

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 (104) hide show
  1. package/CHANGELOG.md +114 -1
  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 +6 -2
  5. package/dist-lib/lib/api/chunk-delta.js +69 -7
  6. package/dist-lib/lib/api/chunks.js +6 -2
  7. package/dist-lib/lib/api/config.d.ts +4 -1
  8. package/dist-lib/lib/api/config.js +23 -4
  9. package/dist-lib/lib/api/devices.js +3 -1
  10. package/dist-lib/lib/api/rag.js +4 -2
  11. package/dist-lib/lib/assistant/harness.d.ts +3 -1
  12. package/dist-lib/lib/assistant/harness.js +10 -0
  13. package/dist-lib/lib/assistant/history.d.ts +2 -1
  14. package/dist-lib/lib/assistant/history.js +39 -6
  15. package/dist-lib/lib/assistant/message.d.ts +3 -0
  16. package/dist-lib/lib/assistant/message.js +10 -2
  17. package/dist-lib/lib/assistant/pdf-budget.d.ts +3 -0
  18. package/dist-lib/lib/assistant/pdf-budget.js +13 -0
  19. package/dist-lib/lib/assistant-tree.d.ts +1 -0
  20. package/dist-lib/lib/assistant-tree.js +10 -3
  21. package/dist-lib/lib/assistant.d.ts +5 -1
  22. package/dist-lib/lib/assistant.js +112 -37
  23. package/dist-lib/lib/client.js +10 -1
  24. package/dist-lib/lib/core/archived-mesh.js +1 -0
  25. package/dist-lib/lib/core/chunk.d.ts +5 -2
  26. package/dist-lib/lib/core/chunk.js +74 -18
  27. package/dist-lib/lib/core/collections.d.ts +1 -0
  28. package/dist-lib/lib/core/collections.js +1 -0
  29. package/dist-lib/lib/core/config-value.d.ts +6 -0
  30. package/dist-lib/lib/core/config-value.js +14 -0
  31. package/dist-lib/lib/core/contract/chat.d.ts +1 -0
  32. package/dist-lib/lib/core/contract/chat.js +8 -1
  33. package/dist-lib/lib/core/contract/chunks.js +20 -6
  34. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  35. package/dist-lib/lib/core/contract/compute.js +2 -1
  36. package/dist-lib/lib/core/contract/devices.js +3 -2
  37. package/dist-lib/lib/core/departures.js +7 -0
  38. package/dist-lib/lib/core/devices.d.ts +2 -1
  39. package/dist-lib/lib/core/devices.js +4 -2
  40. package/dist-lib/lib/core/document-attachment.d.ts +7 -0
  41. package/dist-lib/lib/core/document-attachment.js +6 -0
  42. package/dist-lib/lib/core/files/index.d.ts +1 -1
  43. package/dist-lib/lib/core/files/index.js +1 -1
  44. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  45. package/dist-lib/lib/core/files/rag/index.js +1 -1
  46. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +1 -0
  47. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +4 -1
  48. package/dist-lib/lib/core/identity.d.ts +2 -0
  49. package/dist-lib/lib/core/identity.js +19 -0
  50. package/dist-lib/lib/core/inference-calls-receiver.js +2 -2
  51. package/dist-lib/lib/core/known-sender.d.ts +3 -0
  52. package/dist-lib/lib/core/known-sender.js +26 -0
  53. package/dist-lib/lib/core/local/index.d.ts +2 -0
  54. package/dist-lib/lib/core/local/index.js +17 -10
  55. package/dist-lib/lib/core/mesh.d.ts +5 -1
  56. package/dist-lib/lib/core/mesh.js +6 -6
  57. package/dist-lib/lib/core/migrations/chat-chunks.d.ts +4 -0
  58. package/dist-lib/lib/core/migrations/chat-chunks.js +43 -0
  59. package/dist-lib/lib/core/pdf/rasterize.d.ts +0 -1
  60. package/dist-lib/lib/core/pdf/rasterize.js +0 -1
  61. package/dist-lib/lib/core/skill-overlay-receiver.js +2 -2
  62. package/dist-lib/lib/core/skill-secrets.js +2 -2
  63. package/dist-lib/lib/core.d.ts +12 -1
  64. package/dist-lib/lib/core.js +75 -11
  65. package/dist-lib/lib/credential-pusher.d.ts +1 -0
  66. package/dist-lib/lib/credential-pusher.js +34 -48
  67. package/dist-lib/lib/device-tree.d.ts +18 -0
  68. package/dist-lib/lib/device-tree.js +32 -0
  69. package/dist-lib/lib/errors.d.ts +8 -0
  70. package/dist-lib/lib/errors.js +8 -0
  71. package/dist-lib/lib/oauth/flow-core.js +2 -0
  72. package/dist-lib/lib/oauth/loopback.d.ts +1 -0
  73. package/dist-lib/lib/oauth/loopback.js +31 -1
  74. package/dist-lib/lib/oauth/register.js +33 -18
  75. package/dist-lib/lib/oauth/request.d.ts +10 -0
  76. package/dist-lib/lib/oauth/request.js +22 -0
  77. package/dist-lib/lib/oauth/token-exchange-core.js +29 -32
  78. package/dist-lib/lib/oauth/types.d.ts +3 -0
  79. package/dist-lib/lib/oauth.d.ts +2 -1
  80. package/dist-lib/lib/oauth.js +1 -0
  81. package/dist-lib/lib/run-replay.d.ts +8 -2
  82. package/dist-lib/lib/run-replay.js +29 -11
  83. package/dist-lib/schema/engine.js +20 -3
  84. package/dist-lib/schema/field-order.js +8 -1
  85. package/dist-lib/schema/local.js +7 -3
  86. package/dist-lib/schema/qvac.js +19 -2
  87. package/dist-lib/spec/hrpc/messages.js +1236 -1184
  88. package/dist-lib/spec/hyperschema/index.js +1236 -1184
  89. package/dist-lib/spec/hyperschema/schema.json +47 -0
  90. package/dist-lib/spec/hyperschema/types.d.ts +11 -0
  91. package/dist-lib/spec/local/hyperdb/messages.js +70 -20
  92. package/dist-lib/spec/local/hyperschema/index.js +36 -11
  93. package/dist-lib/spec/local/hyperschema/schema.json +21 -1
  94. package/dist-lib/spec/local/hyperschema/types.d.ts +5 -1
  95. package/dist-lib/spec/mesh/hyperdb/db.json +17 -1
  96. package/dist-lib/spec/mesh/hyperdb/index.js +83 -2
  97. package/dist-lib/spec/mesh/hyperdb/messages.js +573 -255
  98. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
  99. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  100. package/dist-lib/spec/mesh/hyperdispatch/messages.js +121 -8
  101. package/dist-lib/spec/mesh/hyperschema/index.js +121 -8
  102. package/dist-lib/spec/mesh/hyperschema/schema.json +69 -1
  103. package/dist-lib/spec/mesh/hyperschema/types.d.ts +19 -1
  104. package/package.json +12 -10
@@ -13,7 +13,7 @@ import type { Skill } from './assistant/skill-encode.ts';
13
13
  import { type CompactOptions, type CompactResult } from './assistant/history.ts';
14
14
  export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, type DeleteModelSummary, type Harness, type HarnessInfo, type ListModelsOptions, type ModelInfo, type ModelType, type ToolCatalogEntry } from './assistant/harness.ts';
15
15
  export { type Skill } from './assistant/skill-encode.ts';
16
- export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders } from './assistant/message.ts';
16
+ export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders, restoreSentContent } from './assistant/message.ts';
17
17
  export { couldNameSenders, historyFromChunks, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds, type CompactOptions, type CompactResult, type CompactionTrigger, type StaleToolRuns } from './assistant/history.ts';
18
18
  export declare const CAPTURE_INFERENCE_CALLS_KEY = "inspector.captureInferenceCalls";
19
19
  export declare const CAPTURE_ON = "on";
@@ -66,6 +66,7 @@ export declare class QvacAssistant extends ReadyResource {
66
66
  private _info;
67
67
  private readonly _lastCapabilities;
68
68
  private _sharedMeshes;
69
+ private _writableMeshes;
69
70
  private readonly _hostedIn;
70
71
  private _accountHosted;
71
72
  private _readySkills;
@@ -224,6 +225,9 @@ export declare class QvacAssistant extends ReadyResource {
224
225
  private _hydrateTurnImages;
225
226
  private _hydrateImage;
226
227
  private _hydratePdfPages;
228
+ private _pdfPageBudget;
229
+ private _customModel;
230
+ private _hydrateTurnPdfs;
227
231
  private _tryHydratePdf;
228
232
  private _hydratePdf;
229
233
  private _renderPdf;
@@ -22,10 +22,10 @@ import { buildTitlePrompt, deriveChatTitle, isDefaultChatTitle, parseTitleRespon
22
22
  import { matchApprovalRule } from './core/approval-rules.js';
23
23
  import { isAudioAttachment } from './core/audio-attachment.js';
24
24
  import { attachTurnDocumentIds, formatRagContext, chooseRagNotice, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, createRagSourceTagFilter, citedByPhraseOverlap, formatFullDocumentContext, isFullTextHit, isTruncatedFullTextHit, sourcesFromHits, WEB_TOOLS, webSourcesFromToolResult, withoutDataChannel } from './core/files/index.js';
25
- import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason } from './core/document-attachment.js';
25
+ import { annotateDocumentAttachments, documentUnreadNotice, documentUnreadReason, pdfPagesCutNotice, pdfPagesSkippedNotice } from './core/document-attachment.js';
26
26
  import { imageCacheKey, imageCacheName, sweepImageCache } from './core/image/cache-sweep.js';
27
27
  import { IMAGE_MIMES, normalizeImageForModel } from './core/image/normalize.js';
28
- import { MAX_PDF_PAGE_IMAGES, createPdfRasterizer } from './core/pdf/rasterize.js';
28
+ import { createPdfRasterizer } from './core/pdf/rasterize.js';
29
29
  import { recordBackends, summarizeRuns } from './diagnostics.js';
30
30
  import { resolveFsGrants } from './fs-drive-bridge.js';
31
31
  import { InferenceCallStore } from './inference-calls.js';
@@ -43,18 +43,20 @@ import { searchKnowledge } from './knowledge-search.js';
43
43
  import { isLifecycleTeardown, knownAbortOutcome, RUN_ABORT_REASONS, RUN_FAILURE_REASONS, runFailureNotice } from './run-notices.js';
44
44
  import { unref } from './unref.js';
45
45
  import { encodeAgentSkillReadiness } from './skill-readiness.js';
46
- import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, heldLoadModel, droppedHeldLoadModel, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, toolReportEntry } from './assistant/harness.js';
47
- import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, nameSenders, hiddenVideoNotice, lentVideoNotice, unreadVideoNotice, unsyncedVideoNotice, VIDEO_NO_AUDIO, VIDEO_SILENT, videoAnnotation, videoTranscriptBlock } from './assistant/message.js';
46
+ import { harnessClearKvCache, harnessDeathReason, harnessDeleteModel, harnessDied, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe, heldLoadModel, droppedHeldLoadModel, imageDownloadProgress, isHarnessEvent, modelCatalogChanged, customModelReportEntries, toolReportEntry } from './assistant/harness.js';
47
+ import { annotateUnreadableImages, audioTranscriptBlock, backfillDocumentOnlyTurn, backfillImageOnlyTurns, silentAudioNotice, unheardAudioNotice, nameSenders, restoreSentContent, hiddenVideoNotice, lentVideoNotice, unreadVideoNotice, unsyncedVideoNotice, VIDEO_NO_AUDIO, VIDEO_SILENT, videoAnnotation, videoTranscriptBlock } from './assistant/message.js';
48
48
  import { frameTimes, probeVideo, sampleFrames } from './core/video/frames.js';
49
49
  import { packVideoDigest, parseVideoDigestHeader, unpackVideoDigest, VIDEO_DIGEST_MIME, videoDigestFileName } from './core/video/digest.js';
50
50
  import { VIDEO_FRAME_EDGE, videoFrameCount } from './assistant/video-budget.js';
51
51
  import { drainBody, raceInactivity } from './assistant/fetch-body.js';
52
+ import { pdfPageBudget } from './assistant/pdf-budget.js';
53
+ import { effectiveCtxSize } from './api/effective-ctx-size.js';
52
54
  import { GENERIC_ACKNOWLEDGEMENTS, HISTORY_TYPES, SKILL_TOOL, TERMINAL_STATES, couldNameSenders, historyFromChunks, inputAvailableToRun, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds } from './assistant/history.js';
53
55
  import { errorMessage, isRecord } from './guards.js';
54
56
  import { CoreError, isCoreError } from './errors.js';
55
57
  export { harnessClearKvCache, harnessDeleteModel, harnessListModels, harnessPreviewModelSource, harnessSkillCatalog, harnessSkillManagement, harnessDetectLanguage, harnessSpeak, harnessSpeakStream, harnessTools, harnessTranscribe } from './assistant/harness.js';
56
58
  export {} from './assistant/skill-encode.js';
57
- export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders } from './assistant/message.js';
59
+ export { annotateUnreadableImages, unheardAudioNotice, unreadableImagesNotice, DOCUMENT_ONLY_USER_PROMPT, IMAGE_AND_DOCUMENT_USER_PROMPT, IMAGE_ONLY_USER_PROMPT, nameSenders, restoreSentContent } from './assistant/message.js';
58
60
  export { couldNameSenders, historyFromChunks, listHistoryChunks, senderAttribution, senderNamesFrom, staleToolRunIds } from './assistant/history.js';
59
61
  const NO_SENDERS = new Map();
60
62
  const DEFAULT_PROFILE = 'mid';
@@ -110,6 +112,20 @@ const RunOccupancySchema = z.object({
110
112
  export const CAPTURE_INFERENCE_CALLS_KEY = 'inspector.captureInferenceCalls';
111
113
  export const CAPTURE_ON = 'on';
112
114
  export const CAPTURE_OFF = 'off';
115
+ function pdfCut(doc, from) {
116
+ const pageCount = doc.pageCount();
117
+ for (let page = from; page < pageCount; page++) {
118
+ try {
119
+ const flags = doc.pageFlags(page);
120
+ if (flags.hasImage || !flags.hasText) {
121
+ return { lastPage: page, pageCount };
122
+ }
123
+ }
124
+ catch {
125
+ }
126
+ }
127
+ return undefined;
128
+ }
113
129
  function attachPages(turn, files) {
114
130
  if (!files.length)
115
131
  return;
@@ -217,6 +233,7 @@ export class QvacAssistant extends ReadyResource {
217
233
  _info;
218
234
  _lastCapabilities = new Map();
219
235
  _sharedMeshes = new Set();
236
+ _writableMeshes = new Set();
220
237
  _hostedIn = new Map();
221
238
  _accountHosted = [];
222
239
  _readySkills;
@@ -608,6 +625,7 @@ export class QvacAssistant extends ReadyResource {
608
625
  }
609
626
  const installedModelNames = models.map((model) => model.name);
610
627
  const cachedModelNames = models.filter((model) => model.isCached).map((model) => model.name);
628
+ const customModels = customModelReportEntries(models);
611
629
  let toolCatalog = null;
612
630
  if (this._harness.tools) {
613
631
  try {
@@ -623,7 +641,8 @@ export class QvacAssistant extends ReadyResource {
623
641
  installedModelNames,
624
642
  cachedModelNames,
625
643
  this._readySkills,
626
- toolCatalog
644
+ toolCatalog,
645
+ customModels
627
646
  ]);
628
647
  const meshId = this._accountMeshId;
629
648
  if (!meshId) {
@@ -636,14 +655,15 @@ export class QvacAssistant extends ReadyResource {
636
655
  installedModelNames,
637
656
  cachedModelNames,
638
657
  readySkillNames: readySkillNames ?? undefined,
639
- toolCatalog: toolCatalog ?? undefined
658
+ toolCatalog: toolCatalog ?? undefined,
659
+ customModels
640
660
  };
641
661
  await this._reportInto(meshId, key, report);
642
662
  if (!readySkillNames) {
643
663
  return;
644
664
  }
645
665
  for (const [shared, hosted] of this._hostedIn) {
646
- if (shared === meshId)
666
+ if (shared === meshId || !this._writableMeshes.has(shared))
647
667
  continue;
648
668
  await this._reportInto(shared, JSON.stringify([readySkillNames, hosted]), report);
649
669
  }
@@ -1272,18 +1292,21 @@ export class QvacAssistant extends ReadyResource {
1272
1292
  }
1273
1293
  async _sentRuns(chatId, holder) {
1274
1294
  try {
1275
- return await this._runReplay.load({ holder, chatId });
1295
+ return {
1296
+ sentRuns: await this._runReplay.load({ holder, chatId }),
1297
+ askedRuns: await this._runReplay.loadAsked({ holder, chatId })
1298
+ };
1276
1299
  }
1277
1300
  catch (err) {
1278
1301
  this._log?.warn('assistant', 'run replay unreadable', { chatId, err });
1279
1302
  await this._deleteChatKvCache(chatId, 'run replay unreadable', holder);
1280
- return new Map();
1303
+ return { sentRuns: new Map(), askedRuns: new Map() };
1281
1304
  }
1282
1305
  }
1283
- async _recordSentRun(chatId, { runId, turns, holder }) {
1306
+ async _recordSentRun(chatId, { runId, turns, asked, holder }) {
1284
1307
  if (turns) {
1285
1308
  try {
1286
- await this._runReplay.save({ holder, chatId }, runId, turns);
1309
+ await this._runReplay.save({ holder, chatId }, runId, { messages: turns, asked });
1287
1310
  return;
1288
1311
  }
1289
1312
  catch (err) {
@@ -1343,6 +1366,12 @@ export class QvacAssistant extends ReadyResource {
1343
1366
  this._hostedIn.delete(meshId);
1344
1367
  this._lastCapabilities.delete(meshId);
1345
1368
  }
1369
+ const writable = new Set(meshes.filter((mesh) => mesh.writable).map((mesh) => mesh.meshId));
1370
+ const joined = [...this._hostedIn.keys()].some((meshId) => writable.has(meshId) && !this._writableMeshes.has(meshId));
1371
+ this._writableMeshes = writable;
1372
+ if (joined) {
1373
+ void this._reportHosted().catch(noop);
1374
+ }
1346
1375
  for (const [meshId, consumer] of this._meshConsumers) {
1347
1376
  if (open.has(meshId))
1348
1377
  continue;
@@ -1730,8 +1759,12 @@ export class QvacAssistant extends ReadyResource {
1730
1759
  await this._reconcileAttribution(chatId, senders, holder);
1731
1760
  }
1732
1761
  const stale = staleToolRunIds(history, priorAsks ?? [], runId);
1733
- const sentRuns = await this._sentRuns(chatId, holder);
1734
- const messages = historyFromChunks(history, stale, { senderNames: senders, sentRuns });
1762
+ const { sentRuns, askedRuns } = await this._sentRuns(chatId, holder);
1763
+ const messages = historyFromChunks(history, stale, {
1764
+ senderNames: senders,
1765
+ sentRuns,
1766
+ askedRuns
1767
+ });
1735
1768
  const typedText = currentRepair
1736
1769
  ? undefined
1737
1770
  : history.findLast((chunk) => chunk.userMessage)?.userMessage?.text;
@@ -1753,7 +1786,7 @@ export class QvacAssistant extends ReadyResource {
1753
1786
  const seesImages = sendsFiles && carriesMedia && (await this._seesImages(model));
1754
1787
  let unreadableImages = new Map();
1755
1788
  const sampled = [];
1756
- let renderedPdfIds = new Set();
1789
+ let pdfPages = { rendered: new Set(), cuts: [] };
1757
1790
  if (!seesImages) {
1758
1791
  const dropped = messages.reduce((count, turn) => count + (turn.images?.length ?? 0), 0);
1759
1792
  if (dropped && (this._harness.supportsImages ?? this._info.supportsImages)) {
@@ -1857,6 +1890,7 @@ export class QvacAssistant extends ReadyResource {
1857
1890
  let runVerdict = 'completed';
1858
1891
  let sentRun = null;
1859
1892
  let inputLength = 0;
1893
+ let asked;
1860
1894
  const writeText = async (type, raw) => {
1861
1895
  const text = type === 'content' ? sourceTagFilter.push(raw) : raw;
1862
1896
  if (!text) {
@@ -1964,10 +1998,17 @@ export class QvacAssistant extends ReadyResource {
1964
1998
  }
1965
1999
  backfillImageOnlyTurns(messages);
1966
2000
  if (seesImages) {
1967
- renderedPdfIds = await this._hydratePdfPages(messages, controller.signal, runId);
2001
+ pdfPages = await this._hydratePdfPages(messages, {
2002
+ signal: controller.signal,
2003
+ runId,
2004
+ window: { ctxSize: configuredAgent.ctxSize, modelName: model }
2005
+ });
1968
2006
  }
1969
2007
  annotateDocumentAttachments(messages);
1970
- const unreadIds = attachTurnIds.filter((id) => !renderedPdfIds.has(id));
2008
+ for (const { turn, notice } of pdfPages.cuts) {
2009
+ turn.content = `${turn.content}\n${notice}`;
2010
+ }
2011
+ const unreadIds = attachTurnIds.filter((id) => !pdfPages.rendered.has(id));
1971
2012
  if (!diagramRepair && unreadIds.length > 0 && !injected.documentsInjected) {
1972
2013
  const reason = documentUnreadReason({
1973
2014
  knowledgeOn: toolsetEnabled('knowledge', runAgent.disabledToolsets),
@@ -2006,6 +2047,8 @@ export class QvacAssistant extends ReadyResource {
2006
2047
  cacheKey,
2007
2048
  turns: messages.length
2008
2049
  });
2050
+ restoreSentContent(messages);
2051
+ asked = messages.findLast((turn) => turn.role === 'user')?.content;
2009
2052
  const input = nameSenders(messages);
2010
2053
  inputLength = input.length;
2011
2054
  const inspecting = await this._capturingInferenceCalls();
@@ -2156,6 +2199,7 @@ export class QvacAssistant extends ReadyResource {
2156
2199
  await this._recordSentRun(chatId, {
2157
2200
  runId,
2158
2201
  turns: sentRun.turns(inputLength),
2202
+ asked,
2159
2203
  holder
2160
2204
  });
2161
2205
  sentRun = null;
@@ -2803,36 +2847,66 @@ export class QvacAssistant extends ReadyResource {
2803
2847
  });
2804
2848
  return file;
2805
2849
  }
2806
- async _hydratePdfPages(messages, signal, runId) {
2850
+ async _hydratePdfPages(messages, { signal, runId, window }) {
2807
2851
  const rendered = new Set();
2852
+ const cuts = [];
2808
2853
  const turns = messages.filter((turn) => turn.pdfs?.length);
2809
2854
  if (!turns.length) {
2810
- return rendered;
2855
+ return { rendered, cuts };
2811
2856
  }
2812
2857
  const dir = this._imageCacheDir;
2813
2858
  await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
2814
2859
  const pinned = this._runImageCacheNames.get(runId) ?? new Set();
2815
2860
  this._runImageCacheNames.set(runId, pinned);
2816
2861
  const latestUser = messages.findLast((turn) => turn.role === 'user');
2862
+ const maxPages = await this._pdfPageBudget(window);
2817
2863
  for (const turn of turns) {
2818
- let budget = MAX_PDF_PAGE_IMAGES;
2819
- for (const pdf of turn.pdfs ?? []) {
2820
- if (budget <= 0)
2821
- break;
2822
- const pages = await this._tryHydratePdf(dir, turn, {
2823
- pdf,
2824
- pinned,
2825
- maxPages: budget,
2826
- signal
2827
- });
2828
- budget -= pages.tried;
2829
- if (turn === latestUser && pages.names.length) {
2830
- rendered.add(pdf.id);
2864
+ const { shown, notices } = await this._hydrateTurnPdfs(turn, {
2865
+ dir,
2866
+ pinned,
2867
+ signal,
2868
+ maxPages
2869
+ });
2870
+ if (turn === latestUser) {
2871
+ for (const id of shown) {
2872
+ rendered.add(id);
2831
2873
  }
2832
2874
  }
2833
- delete turn.pdfs;
2875
+ cuts.push(...notices.map((notice) => ({ turn, notice })));
2876
+ }
2877
+ return { rendered, cuts };
2878
+ }
2879
+ async _pdfPageBudget({ ctxSize, modelName }) {
2880
+ const custom = await this._customModel(modelName);
2881
+ const window = effectiveCtxSize({ ctxSize, modelName }, { appCtxSize: 0, platform: this._platform, custom });
2882
+ return pdfPageBudget(window);
2883
+ }
2884
+ async _customModel(name) {
2885
+ for await (const list of this._engine.customModelsWatch({})) {
2886
+ return list.models?.find((row) => row.name === name) ?? null;
2887
+ }
2888
+ return null;
2889
+ }
2890
+ async _hydrateTurnPdfs(turn, { dir, pinned, signal, maxPages }) {
2891
+ const shown = [];
2892
+ const notices = [];
2893
+ let budget = maxPages;
2894
+ for (const pdf of turn.pdfs ?? []) {
2895
+ if (budget <= 0) {
2896
+ notices.push(pdfPagesSkippedNotice(pdf.fileName));
2897
+ continue;
2898
+ }
2899
+ const pages = await this._tryHydratePdf(dir, turn, { pdf, pinned, maxPages: budget, signal });
2900
+ budget -= pages.tried;
2901
+ if (pages.names.length) {
2902
+ shown.push(pdf.id);
2903
+ }
2904
+ if (pages.cut) {
2905
+ notices.push(pdfPagesCutNotice({ fileName: pdf.fileName, ...pages.cut }));
2906
+ }
2834
2907
  }
2835
- return rendered;
2908
+ delete turn.pdfs;
2909
+ return { shown, notices };
2836
2910
  }
2837
2911
  async _tryHydratePdf(dir, turn, opts) {
2838
2912
  try {
@@ -2882,8 +2956,9 @@ export class QvacAssistant extends ReadyResource {
2882
2956
  try {
2883
2957
  const names = [];
2884
2958
  let tried = 0;
2885
- const count = doc.pageCount();
2886
- for (let page = 0; page < count && tried < maxPages; page++) {
2959
+ let page = 0;
2960
+ const pageCount = doc.pageCount();
2961
+ for (; page < pageCount && tried < maxPages; page++) {
2887
2962
  if (signal.aborted)
2888
2963
  throw new Error('hydration aborted');
2889
2964
  const outcome = await this._renderPdfPage(doc, { dir, pdf, page, pinned, signal });
@@ -2894,7 +2969,7 @@ export class QvacAssistant extends ReadyResource {
2894
2969
  names.push(outcome.name);
2895
2970
  }
2896
2971
  }
2897
- return { names, tried };
2972
+ return { names, tried, cut: pdfCut(doc, page) };
2898
2973
  }
2899
2974
  finally {
2900
2975
  doc.close();
@@ -1,5 +1,6 @@
1
1
  import ReadyResource from 'ready-resource';
2
2
  import { reconnectingWatch } from './reconnecting-watch.js';
3
+ import { readConfigAnswer, readConfigFrames } from './api/config.js';
3
4
  import { openLocalEngineSpec } from './spec-loader.js';
4
5
  import { settleOnClose } from '@qvac/harness/rpc';
5
6
  export class Client extends ReadyResource {
@@ -28,7 +29,15 @@ export class Client extends ReadyResource {
28
29
  const modelDownloadsWatch = this._reconnecting(capabilities.modelDownloadsWatch, 'model-download');
29
30
  const modelCatalogWatch = this._reconnecting(capabilities.modelCatalogWatch, 'model-catalog');
30
31
  const modelsWatch = this._reconnecting(capabilities.modelsWatch, 'models');
31
- Object.assign(this, capabilities, { modelCatalogWatch, modelDownloadsWatch, modelsWatch });
32
+ const config = {
33
+ configGet: async (input) => readConfigAnswer(await capabilities.configGet(input)),
34
+ configWatch: (input) => readConfigFrames(capabilities.configWatch(input))
35
+ };
36
+ Object.assign(this, capabilities, config, {
37
+ modelCatalogWatch,
38
+ modelDownloadsWatch,
39
+ modelsWatch
40
+ });
32
41
  this._log?.info('lifecycle', 'client open');
33
42
  }
34
43
  _reconnecting(open, scope) {
@@ -13,6 +13,7 @@ export async function withArchivedMesh(row, opts, fn) {
13
13
  key: row.key,
14
14
  deviceId: opts.deviceId,
15
15
  writerSeed: row.writerSeed ?? undefined,
16
+ compute: row.compute,
16
17
  logger: opts.logger
17
18
  });
18
19
  await mesh.ready();
@@ -23,8 +23,9 @@ export type ReadChunk = QvacChunk & {
23
23
  readonly url?: string;
24
24
  }) | null;
25
25
  };
26
+ export declare function linkChunks(store: Mesh, chunks: QvacChunk[]): ReadChunk[];
26
27
  export declare function recordLinks(store: Mesh, chunks: ReadChunk[]): Promise<ReadChunk[]>;
27
- type ListChunksInput = Bounds & {
28
+ export type ListChunksInput = Bounds & {
28
29
  chatId: string;
29
30
  type?: QvacChunk['type'];
30
31
  types?: ReadonlyArray<QvacChunk['type']>;
@@ -34,8 +35,10 @@ export interface FindChunksOfTypeInput extends Bounds {
34
35
  readonly type: QvacChunk['type'];
35
36
  }
36
37
  export declare function findChunksOfType(snap: HyperDB, { chatId, type, ...bounds }: FindChunksOfTypeInput): Promise<QvacChunk[]>;
37
- export declare function listChunks(store: Mesh, { chatId, type, types, ...bounds }: ListChunksInput): Promise<ReadChunk[]>;
38
+ export declare function listChunks(store: Mesh, input: ListChunksInput): Promise<ReadChunk[]>;
39
+ export declare function readChunks(db: HyperDB, { chatId, type, types, ...bounds }: ListChunksInput): Promise<QvacChunk[]>;
38
40
  export declare function listAttachmentsByKind(store: Mesh, { kind, origin, ...bounds }: ListAttachmentsByKindInput): Promise<ReadChunk[]>;
41
+ export declare function selectsChunk({ chatId, type, types, gt, gte, lt, lte }: ListChunksInput, chunk: QvacChunk): boolean;
39
42
  export declare function getChunk(store: Mesh, id: string): Promise<QvacChunk | null>;
40
43
  export interface AppendedChunk {
41
44
  readonly id: string;
@@ -7,7 +7,8 @@ import { linkAttachment } from './attachment-link.js';
7
7
  import { randomId } from './id.js';
8
8
  import { keyRange } from './range.js';
9
9
  import b4a from 'b4a';
10
- import { AGENTS, ATTACHMENTS_BY_MEDIA_KIND, ATTACHMENTS_BY_MEDIA_KIND_ORIGIN, CHUNKS, CHUNKS_BY_CHAT, CHUNKS_BY_TYPE, HISTORY_CHUNKS, RUN_REQUESTS } from './collections.js';
10
+ import { AGENTS, ATTACHMENTS_BY_MEDIA_KIND, ATTACHMENTS_BY_MEDIA_KIND_ORIGIN, CHAT_CHUNKS, CHUNKS, CHUNKS_BY_CHAT, CHUNKS_BY_TYPE, CHUNK_COUNTERS, HISTORY_CHUNKS, RUN_REQUESTS } from './collections.js';
11
+ const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
11
12
  export const HISTORY_CHUNK_TYPES = [
12
13
  'user-message',
13
14
  'run-request',
@@ -15,7 +16,7 @@ export const HISTORY_CHUNK_TYPES = [
15
16
  'attachment',
16
17
  'compaction'
17
18
  ];
18
- function linkChunks(store, chunks) {
19
+ export function linkChunks(store, chunks) {
19
20
  const mint = store.attachmentUrl;
20
21
  if (!mint) {
21
22
  return chunks;
@@ -32,7 +33,16 @@ export function findChunksOfType(snap, { chatId, type, ...bounds }) {
32
33
  .find(CHUNKS_BY_TYPE, keyRange(prefix, 'seq', bounds))
33
34
  .toArray();
34
35
  }
35
- export async function listChunks(store, { chatId, type, types, ...bounds }) {
36
+ export async function listChunks(store, input) {
37
+ const snap = store.view.snapshot();
38
+ try {
39
+ return linkChunks(store, await readChunks(snap, input));
40
+ }
41
+ finally {
42
+ await snap.close();
43
+ }
44
+ }
45
+ export async function readChunks(db, { chatId, type, types, ...bounds }) {
36
46
  const selected = selectedChunkTypes(type, types);
37
47
  if (selected?.length === 0) {
38
48
  return [];
@@ -40,23 +50,60 @@ export async function listChunks(store, { chatId, type, types, ...bounds }) {
40
50
  if (selected && selected.length > 1 && !isHistorySelection(selected)) {
41
51
  throw new CoreError('CHUNK_TYPES_UNMAPPED');
42
52
  }
43
- const snap = store.view.snapshot();
44
- try {
45
- if (selected?.length === 1) {
46
- const prefix = { chatId, type: chunkTypeOrdinal(selected[0]) };
47
- const chunks = await snap
48
- .find(CHUNKS_BY_TYPE, keyRange(prefix, 'seq', bounds))
49
- .toArray();
50
- return linkChunks(store, chunks);
51
- }
52
- const chunks = await snap
53
- .find(selected ? HISTORY_CHUNKS : CHUNKS_BY_CHAT, keyRange({ chatId }, 'seq', bounds))
54
- .toArray();
55
- return linkChunks(store, chunks);
53
+ if (selected?.length === 1) {
54
+ return findChunksOfType(db, { chatId, type: selected[0], ...bounds });
56
55
  }
57
- finally {
58
- await snap.close();
56
+ const history = selected !== null;
57
+ const ordered = history && bounds.limit ? null : await readChatOrdered(db, chatId, bounds);
58
+ if (ordered) {
59
+ return history ? ordered.filter((chunk) => HISTORY_CHUNK_TYPES.includes(chunk.type)) : ordered;
60
+ }
61
+ return db
62
+ .find(history ? HISTORY_CHUNKS : CHUNKS_BY_CHAT, keyRange({ chatId }, 'seq', bounds))
63
+ .toArray();
64
+ }
65
+ async function readChatOrdered(db, chatId, bounds) {
66
+ const counter = await db.get(CHUNK_COUNTERS, { chatId });
67
+ if (!counter) {
68
+ return null;
69
+ }
70
+ const chunks = await db
71
+ .find(CHAT_CHUNKS, keyRange({ chatId }, 'seq', bounds))
72
+ .toArray();
73
+ const { first, last } = seqSpan(counter.count, bounds);
74
+ if (last < first) {
75
+ return chunks.length === 0 ? chunks : null;
76
+ }
77
+ const [low, high] = bounds.reverse ? [chunks.at(-1), chunks[0]] : [chunks[0], chunks.at(-1)];
78
+ const whole = chunks.length === last - first + 1 && low?.seq === first && high?.seq === last;
79
+ if (!whole) {
80
+ return null;
59
81
  }
82
+ const settled = new Set(chunks
83
+ .filter((chunk) => TERMINAL_RUN_STATES.has(chunk.runStatus?.state ?? ''))
84
+ .map((c) => c.runId));
85
+ return Promise.all(chunks.map((chunk) => (changeable(chunk, settled) ? current(db, chunk) : chunk)));
86
+ }
87
+ function changeable(chunk, settled) {
88
+ return ((chunk.type === 'run-status' && !settled.has(chunk.runId)) ||
89
+ (chunk.type === 'approval-request' && chunk.approvalRequest?.status === 'pending') ||
90
+ (chunk.type === 'attachment' && !chunk.attachment?.deletedAt));
91
+ }
92
+ async function current(db, chunk) {
93
+ return (await db.get(CHUNKS, { id: chunk.id })) ?? chunk;
94
+ }
95
+ function seqSpan(count, { gt, gte, lt, lte, reverse, limit }) {
96
+ let first = Math.max(0, gte ?? 0, gt === undefined ? 0 : gt + 1);
97
+ let last = Math.min(count - 1, lte ?? Infinity, lt === undefined ? Infinity : lt - 1);
98
+ if (limit) {
99
+ if (reverse) {
100
+ first = Math.max(first, last - limit + 1);
101
+ }
102
+ else {
103
+ last = Math.min(last, first + limit - 1);
104
+ }
105
+ }
106
+ return { first, last };
60
107
  }
61
108
  export async function listAttachmentsByKind(store, { kind, origin, ...bounds }) {
62
109
  const mediaKind = mediaKindOrdinal(kind);
@@ -87,6 +134,15 @@ function selectedChunkTypes(type, types) {
87
134
  }
88
135
  return [...selected];
89
136
  }
137
+ export function selectsChunk({ chatId, type, types, gt, gte, lt, lte }, chunk) {
138
+ const selected = selectedChunkTypes(type, types);
139
+ return (chunk.chatId === chatId &&
140
+ (selected === null || selected.includes(chunk.type)) &&
141
+ (gt === undefined || chunk.seq > gt) &&
142
+ (gte === undefined || chunk.seq >= gte) &&
143
+ (lt === undefined || chunk.seq < lt) &&
144
+ (lte === undefined || chunk.seq <= lte));
145
+ }
90
146
  function isHistorySelection(types) {
91
147
  return (types.length === HISTORY_CHUNK_TYPES.length &&
92
148
  types.every((type) => HISTORY_CHUNK_TYPES.includes(type)));
@@ -1,4 +1,5 @@
1
1
  export declare const CHUNKS = "@qvac/chunks";
2
+ export declare const CHAT_CHUNKS = "@qvac/chat-chunks";
2
3
  export declare const CHUNK_COUNTERS = "@qvac/chunk-counters";
3
4
  export declare const AGENTS = "@qvac/agents";
4
5
  export declare const APPROVAL_RULES = "@qvac/approval-rules";
@@ -1,4 +1,5 @@
1
1
  export const CHUNKS = '@qvac/chunks';
2
+ export const CHAT_CHUNKS = '@qvac/chat-chunks';
2
3
  export const CHUNK_COUNTERS = '@qvac/chunk-counters';
3
4
  export const AGENTS = '@qvac/agents';
4
5
  export const APPROVAL_RULES = '@qvac/approval-rules';
@@ -0,0 +1,6 @@
1
+ export interface StoredConfigValue {
2
+ readonly value?: unknown;
3
+ readonly falsyValue?: string | null;
4
+ }
5
+ export declare function configValue(value: unknown): StoredConfigValue;
6
+ export declare function readConfigValue({ value, falsyValue }: StoredConfigValue): unknown;
@@ -0,0 +1,14 @@
1
+ const FALSY_VALUES = new Map([
2
+ ['false', false],
3
+ ['0', 0],
4
+ ['""', '']
5
+ ]);
6
+ export function configValue(value) {
7
+ return value === false || value === 0 || value === ''
8
+ ? { value: null, falsyValue: JSON.stringify(value) }
9
+ : { value };
10
+ }
11
+ export function readConfigValue({ value, falsyValue }) {
12
+ const falsy = falsyValue ?? '';
13
+ return FALSY_VALUES.has(falsy) ? FALSY_VALUES.get(falsy) : (value ?? null);
14
+ }
@@ -2,3 +2,4 @@ import type HyperDB from 'hyperdb';
2
2
  import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
3
3
  export declare function isPurged(tx: HyperDB, chatId: string): Promise<boolean>;
4
4
  export declare function registerChatRoutes(router: Router): void;
5
+ export declare function deleteChatChunks(tx: HyperDB, chatId: string): Promise<void>;
@@ -1,5 +1,5 @@
1
1
  import { trackActivity } from './activity.js';
2
- import { CHATS, CHUNKS, CHUNKS_BY_CHAT, CHUNK_COUNTERS, INDEXING_STATUS, INDEXING_STATUS_BY_CHAT, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, KNOWLEDGE_ARTIFACTS_BY_KNOWLEDGE, KNOWLEDGE_BY_CHAT, PURGED_CHATS, TOOL_ACTIVITY, TOOL_ACTIVITY_BY_AGENT } from '../collections.js';
2
+ import { CHATS, CHAT_CHUNKS, CHUNKS, CHUNKS_BY_CHAT, CHUNK_COUNTERS, INDEXING_STATUS, INDEXING_STATUS_BY_CHAT, KNOWLEDGE, KNOWLEDGE_ARTIFACTS, KNOWLEDGE_ARTIFACTS_BY_KNOWLEDGE, KNOWLEDGE_BY_CHAT, PURGED_CHATS, TOOL_ACTIVITY, TOOL_ACTIVITY_BY_AGENT } from '../collections.js';
3
3
  export async function isPurged(tx, chatId) {
4
4
  return (await tx.get(PURGED_CHATS, { chatId })) !== null;
5
5
  }
@@ -45,12 +45,19 @@ export function registerChatRoutes(router) {
45
45
  return purged;
46
46
  }));
47
47
  }
48
+ export async function deleteChatChunks(tx, chatId) {
49
+ const inChat = { gte: { chatId }, lte: { chatId } };
50
+ for (const chunk of await tx.find(CHAT_CHUNKS, inChat).toArray()) {
51
+ await tx.delete(CHAT_CHUNKS, { chatId, seq: chunk.seq });
52
+ }
53
+ }
48
54
  async function purgeChat(tx, chat) {
49
55
  const chatId = chat.id;
50
56
  const inChat = { gte: { chatId }, lte: { chatId } };
51
57
  for (const id of await idsOf(tx.find(CHUNKS_BY_CHAT, inChat))) {
52
58
  await tx.delete(CHUNKS, { id });
53
59
  }
60
+ await deleteChatChunks(tx, chatId);
54
61
  await tx.delete(CHUNK_COUNTERS, { chatId });
55
62
  for (const knowledge of await tx.find(KNOWLEDGE_BY_CHAT, inChat).toArray()) {
56
63
  const ofKnowledge = { gte: { knowledgeId: knowledge.id }, lte: { knowledgeId: knowledge.id } };