@qvac/core 0.11.1 → 0.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/CHANGELOG.md +88 -1
  2. package/README.md +19 -16
  3. package/dist-lib/lib/api/chunks.js +4 -2
  4. package/dist-lib/lib/api/rag.js +7 -5
  5. package/dist-lib/lib/assistant/history.d.ts +1 -0
  6. package/dist-lib/lib/assistant/history.js +1 -17
  7. package/dist-lib/lib/assistant/message.d.ts +4 -1
  8. package/dist-lib/lib/assistant/pdf-budget.d.ts +10 -2
  9. package/dist-lib/lib/assistant/pdf-budget.js +10 -11
  10. package/dist-lib/lib/assistant/pdf-pages.d.ts +18 -0
  11. package/dist-lib/lib/assistant/pdf-pages.js +75 -0
  12. package/dist-lib/lib/assistant-tree.d.ts +2 -0
  13. package/dist-lib/lib/assistant-tree.js +4 -2
  14. package/dist-lib/lib/assistant.d.ts +10 -6
  15. package/dist-lib/lib/assistant.js +205 -140
  16. package/dist-lib/lib/compaction.d.ts +2 -1
  17. package/dist-lib/lib/compaction.js +14 -4
  18. package/dist-lib/lib/constants.d.ts +3 -0
  19. package/dist-lib/lib/constants.js +3 -0
  20. package/dist-lib/lib/core/chat-activity.js +12 -15
  21. package/dist-lib/lib/core/chunk.d.ts +9 -1
  22. package/dist-lib/lib/core/chunk.js +42 -8
  23. package/dist-lib/lib/core/document-attachment.d.ts +6 -3
  24. package/dist-lib/lib/core/document-attachment.js +10 -5
  25. package/dist-lib/lib/core/files/attachments.js +3 -3
  26. package/dist-lib/lib/core/files/index.d.ts +1 -1
  27. package/dist-lib/lib/core/files/index.js +3 -2
  28. package/dist-lib/lib/core/files/processing/index.d.ts +1 -0
  29. package/dist-lib/lib/core/files/processing/index.js +8 -0
  30. package/dist-lib/lib/core/files/processing/settle.d.ts +1 -0
  31. package/dist-lib/lib/core/files/processing/settle.js +20 -13
  32. package/dist-lib/lib/core/files/rag/index.d.ts +1 -1
  33. package/dist-lib/lib/core/files/rag/index.js +1 -1
  34. package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +1 -0
  35. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +3 -0
  36. package/dist-lib/lib/core/hosted-agents.d.ts +3 -0
  37. package/dist-lib/lib/core/hosted-agents.js +19 -0
  38. package/dist-lib/lib/core/notification.js +16 -13
  39. package/dist-lib/lib/core/pdf/figure.d.ts +22 -0
  40. package/dist-lib/lib/core/pdf/figure.js +64 -0
  41. package/dist-lib/lib/core/pdf/rasterize.d.ts +10 -1
  42. package/dist-lib/lib/core/pdf/rasterize.js +72 -22
  43. package/dist-lib/lib/core.d.ts +6 -0
  44. package/dist-lib/lib/core.js +5 -0
  45. package/dist-lib/lib/credential-pusher.d.ts +11 -7
  46. package/dist-lib/lib/credential-pusher.js +40 -8
  47. package/dist-lib/lib/integrations/setup-routes.js +7 -5
  48. package/dist-lib/lib/kv-cache-retention.d.ts +34 -0
  49. package/dist-lib/lib/kv-cache-retention.js +98 -0
  50. package/dist-lib/schema/content.js +2 -1
  51. package/dist-lib/schema/qvac.js +4 -2
  52. package/dist-lib/spec/hrpc/messages.js +4 -10
  53. package/dist-lib/spec/hyperschema/index.js +4 -10
  54. package/dist-lib/spec/hyperschema/schema.json +2 -1
  55. package/dist-lib/spec/hyperschema/types.d.ts +1 -1
  56. package/dist-lib/spec/mesh/hyperdb/messages.js +34 -14
  57. package/dist-lib/spec/mesh/hyperdispatch/messages.js +18 -8
  58. package/dist-lib/spec/mesh/hyperschema/index.js +18 -8
  59. package/dist-lib/spec/mesh/hyperschema/schema.json +11 -1
  60. package/dist-lib/spec/mesh/hyperschema/types.d.ts +3 -1
  61. package/package.json +6 -3
  62. package/tui/app.mjs +743 -266
  63. package/tui/boot.mjs +2 -2
  64. package/tui/lib/connect-sheet.mjs +350 -0
  65. package/tui/lib/setup.mjs +59 -25
  66. package/tui/lib/skill-connect.mjs +145 -0
@@ -35,10 +35,10 @@ export function parseCompactionSummary(text) {
35
35
  const summary = text.trim();
36
36
  return summary.length > 0 ? summary : null;
37
37
  }
38
- export function compactionBoundary(chunks, keepTurns = COMPACTION_KEEP_TURNS) {
38
+ export function compactionBoundary(chunks, keepTurns = COMPACTION_KEEP_TURNS, beforeSeq = Infinity) {
39
39
  const userTurns = [];
40
40
  for (let index = 0; index < chunks.length; index++) {
41
- if (chunks[index].userMessage) {
41
+ if (chunks[index].userMessage && chunks[index].seq < beforeSeq) {
42
42
  userTurns.push(index);
43
43
  }
44
44
  }
@@ -52,12 +52,21 @@ export function compactionBoundary(chunks, keepTurns = COMPACTION_KEEP_TURNS) {
52
52
  from = index;
53
53
  }
54
54
  }
55
- const boundary = chunks[from - 1];
55
+ const boundary = chunks.slice(0, from).findLast((chunk) => chunk.type === 'run-request') ?? chunks[from - 1];
56
56
  if (!boundary) {
57
57
  return null;
58
58
  }
59
59
  return { throughId: boundary.id, foldedTurns: userTurns.length - keepTurns };
60
60
  }
61
+ export function foldsThrough(chunks, through) {
62
+ const requested = new Map();
63
+ for (const chunk of chunks) {
64
+ if (chunk.type === 'run-request' && chunk.runId && !requested.has(chunk.runId)) {
65
+ requested.set(chunk.runId, chunk.seq);
66
+ }
67
+ }
68
+ return (chunk) => (chunk.runId ? (requested.get(chunk.runId) ?? chunk.seq) : chunk.seq) <= through.seq;
69
+ }
61
70
  function newestCompaction(chunks) {
62
71
  let newest = null;
63
72
  for (const chunk of chunks) {
@@ -79,8 +88,9 @@ export function foldCompactedChunks(chunks) {
79
88
  if (!through) {
80
89
  return { chunks, summary: null };
81
90
  }
91
+ const folded = foldsThrough(chunks, through);
82
92
  return {
83
- chunks: chunks.filter((chunk) => chunk.seq > through.seq && !chunk.compaction),
93
+ chunks: chunks.filter((chunk) => !folded(chunk) && !chunk.compaction),
84
94
  summary: compaction.summary
85
95
  };
86
96
  }
@@ -9,5 +9,8 @@ export declare const MODEL_MEMORY_CAP_KEY = "model.memoryCapPercent";
9
9
  export declare const MODEL_MEMORY_CAP_DEFAULT = 85;
10
10
  export declare const MODEL_MEMORY_CAP_MIN = 10;
11
11
  export declare const MODEL_MEMORY_CAP_MAX = 100;
12
+ export declare const KV_CACHE_RETENTION_KEY = "kvCache.retentionDays";
13
+ export declare const KV_CACHE_RETENTION_DAYS_DEFAULT = 14;
14
+ export declare const KV_CACHE_RETENTION_NEVER = 0;
12
15
  export declare const FAKE_MODELS_KEY = "debug.fakeModels";
13
16
  export declare const QUACK_MODEL = "quack";
@@ -11,5 +11,8 @@ export const MODEL_MEMORY_CAP_KEY = 'model.memoryCapPercent';
11
11
  export const MODEL_MEMORY_CAP_DEFAULT = 85;
12
12
  export const MODEL_MEMORY_CAP_MIN = 10;
13
13
  export const MODEL_MEMORY_CAP_MAX = 100;
14
+ export const KV_CACHE_RETENTION_KEY = 'kvCache.retentionDays';
15
+ export const KV_CACHE_RETENTION_DAYS_DEFAULT = 14;
16
+ export const KV_CACHE_RETENTION_NEVER = 0;
14
17
  export const FAKE_MODELS_KEY = 'debug.fakeModels';
15
18
  export const QUACK_MODEL = 'quack';
@@ -1,5 +1,6 @@
1
+ import b4a from 'b4a';
1
2
  import ScopeLock from 'scope-lock';
2
- import { findChunksOfType } from './chunk.js';
3
+ import { findChunksOfTypes } from './chunk.js';
3
4
  import { ACTIVE_CHATS, CHUNKS } from './collections.js';
4
5
  const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
5
6
  const ACTIVITY_CHUNK_TYPES = [
@@ -39,15 +40,16 @@ class ChatActivityCatalog {
39
40
  try {
40
41
  const head = store.view.db.head();
41
42
  const forwards = store.autobee.stats.fastForwards;
42
- if (forwards !== this._forwards) {
43
- this._head = null;
44
- }
45
- this._forwards = forwards;
43
+ const from = forwards === this._forwards && this._head && head && b4a.equals(this._head.key, head.key)
44
+ ? this._head
45
+ : null;
46
46
  const snap = store.view.snapshot();
47
47
  try {
48
48
  const chats = await snap.find(ACTIVE_CHATS).toArray();
49
49
  const activeChatIds = new Set(chats.map((chat) => chat.id));
50
- await this._refresh(snap, head, activeChatIds);
50
+ await this._refresh(snap, from, activeChatIds);
51
+ this._head = head;
52
+ this._forwards = forwards;
51
53
  const window = chatIds && chatIds.length > 0 ? new Set(chatIds) : null;
52
54
  return [...this._states()]
53
55
  .filter(([id]) => !window || window.has(id))
@@ -62,18 +64,17 @@ class ChatActivityCatalog {
62
64
  this._lock.unlock();
63
65
  }
64
66
  }
65
- async _refresh(snap, head, activeChatIds) {
66
- if (!this._head || !head) {
67
+ async _refresh(snap, from, activeChatIds) {
68
+ if (!from) {
67
69
  await this._rebuild(snap, activeChatIds);
68
70
  }
69
71
  else {
70
72
  this._prune(activeChatIds);
71
73
  await this._loadMissing(snap, activeChatIds);
72
- if (!(await this._applyChanges(snap, this._head, activeChatIds))) {
74
+ if (!(await this._applyChanges(snap, from, activeChatIds))) {
73
75
  await this._rebuild(snap, activeChatIds);
74
76
  }
75
77
  }
76
- this._head = head;
77
78
  }
78
79
  async _rebuild(snap, activeChatIds) {
79
80
  this._chats.clear();
@@ -98,7 +99,7 @@ class ChatActivityCatalog {
98
99
  async _loadChat(snap, chatId) {
99
100
  const activity = { runs: new Map() };
100
101
  this._chats.set(chatId, activity);
101
- for (const chunk of await activityChunks(snap, chatId)) {
102
+ for (const chunk of await findChunksOfTypes(snap, { chatId, types: ACTIVITY_CHUNK_TYPES })) {
102
103
  applyChunk(activity, chunk);
103
104
  }
104
105
  }
@@ -252,10 +253,6 @@ function applyApproval(run, chunk) {
252
253
  run.pendingApprovals.delete(chunk.id);
253
254
  }
254
255
  }
255
- async function activityChunks(snap, chatId) {
256
- const groups = await Promise.all(ACTIVITY_CHUNK_TYPES.map((type) => findChunksOfType(snap, { chatId, type })));
257
- return groups.flat().sort((left, right) => left.seq - right.seq);
258
- }
259
256
  function settleTool(tools, name) {
260
257
  for (let index = tools.length - 1; index >= 0; index -= 1) {
261
258
  if (tools[index]?.name !== name)
@@ -1,5 +1,6 @@
1
1
  import type HyperDB from 'hyperdb';
2
2
  import type { QvacChunk, QvacUpdateChunkOperation } from '../../spec/mesh/hyperschema/types.d.ts';
3
+ import type { AssistantChunkContentToolResult } from '../../spec/hyperschema/types.d.ts';
3
4
  import type { Mesh, MeshDispatcher } from './mesh.ts';
4
5
  import { type Bounds } from './range.ts';
5
6
  type ChunkPayload = Pick<QvacChunk, 'userMessage' | 'content' | 'thinking' | 'toolCall' | 'toolResult' | 'attachment' | 'approvalRequest' | 'runStatus' | 'runRequest' | 'sources' | 'compaction'>;
@@ -18,11 +19,13 @@ type ListAttachmentsByKindInput = Bounds & {
18
19
  kind: MediaKind;
19
20
  origin?: AttachmentOrigin;
20
21
  };
21
- export type ReadChunk = QvacChunk & {
22
+ export type ReadChunk = Omit<QvacChunk, 'toolResult' | 'attachment'> & {
23
+ readonly toolResult: AssistantChunkContentToolResult | null;
22
24
  readonly attachment?: (NonNullable<QvacChunk['attachment']> & {
23
25
  readonly url?: string;
24
26
  }) | null;
25
27
  };
28
+ export declare function readChunk({ toolResult, ...chunk }: QvacChunk): ReadChunk;
26
29
  export declare function linkChunks(store: Mesh, chunks: QvacChunk[]): ReadChunk[];
27
30
  export declare function recordLinks(store: Mesh, chunks: ReadChunk[]): Promise<ReadChunk[]>;
28
31
  export type ListChunksInput = Bounds & {
@@ -35,6 +38,11 @@ export interface FindChunksOfTypeInput extends Bounds {
35
38
  readonly type: QvacChunk['type'];
36
39
  }
37
40
  export declare function findChunksOfType(snap: HyperDB, { chatId, type, ...bounds }: FindChunksOfTypeInput): Promise<QvacChunk[]>;
41
+ export interface FindChunksOfTypesInput extends Omit<Bounds, 'limit'> {
42
+ readonly chatId: string;
43
+ readonly types: ReadonlyArray<QvacChunk['type']>;
44
+ }
45
+ export declare function findChunksOfTypes(db: HyperDB, { chatId, types, ...bounds }: FindChunksOfTypesInput): Promise<QvacChunk[]>;
38
46
  export declare function listChunks(store: Mesh, input: ListChunksInput): Promise<ReadChunk[]>;
39
47
  export declare function readChunks(db: HyperDB, { chatId, type, types, ...bounds }: ListChunksInput): Promise<QvacChunk[]>;
40
48
  export declare function listAttachmentsByKind(store: Mesh, { kind, origin, ...bounds }: ListAttachmentsByKindInput): Promise<ReadChunk[]>;
@@ -16,12 +16,31 @@ export const HISTORY_CHUNK_TYPES = [
16
16
  'attachment',
17
17
  'compaction'
18
18
  ];
19
+ const FALSY_RESULTS = new Map([
20
+ ['false', false],
21
+ ['0', 0],
22
+ ['""', '']
23
+ ]);
24
+ export function readChunk({ toolResult, ...chunk }) {
25
+ if (!toolResult) {
26
+ return { ...chunk, toolResult: null };
27
+ }
28
+ const stored = chunk.falsyResult ?? '';
29
+ const result = FALSY_RESULTS.has(stored) ? FALSY_RESULTS.get(stored) : (toolResult.result ?? null);
30
+ return { ...chunk, toolResult: { ...toolResult, result } };
31
+ }
19
32
  export function linkChunks(store, chunks) {
20
33
  const mint = store.attachmentUrl;
21
- if (!mint) {
22
- return chunks;
23
- }
24
- return chunks.map((chunk) => chunk.attachment ? { ...chunk, attachment: linkAttachment(mint, chunk.attachment) } : chunk);
34
+ return chunks.map((row) => {
35
+ const chunk = readChunk(row);
36
+ return mint && chunk.attachment
37
+ ? { ...chunk, attachment: linkAttachment(mint, chunk.attachment) }
38
+ : chunk;
39
+ });
40
+ }
41
+ function falsyResultOf(payload) {
42
+ const result = payload.toolResult?.result;
43
+ return result === false || result === 0 || result === '' ? JSON.stringify(result) : undefined;
25
44
  }
26
45
  export async function recordLinks(store, chunks) {
27
46
  await store.fileIndex?.record(chunks.filter((chunk) => chunk.attachment?.url));
@@ -33,6 +52,14 @@ export function findChunksOfType(snap, { chatId, type, ...bounds }) {
33
52
  .find(CHUNKS_BY_TYPE, keyRange(prefix, 'seq', bounds))
34
53
  .toArray();
35
54
  }
55
+ export async function findChunksOfTypes(db, { chatId, types, ...bounds }) {
56
+ const ordered = await readChatOrdered(db, chatId, { types, ...bounds });
57
+ if (ordered) {
58
+ return ordered;
59
+ }
60
+ const byType = await Promise.all(types.map((type) => findChunksOfType(db, { chatId, type, ...bounds })));
61
+ return byType.flat().sort((left, right) => left.seq - right.seq);
62
+ }
36
63
  export async function listChunks(store, input) {
37
64
  const snap = store.view.snapshot();
38
65
  try {
@@ -54,15 +81,20 @@ export async function readChunks(db, { chatId, type, types, ...bounds }) {
54
81
  return findChunksOfType(db, { chatId, type: selected[0], ...bounds });
55
82
  }
56
83
  const history = selected !== null;
57
- const ordered = history && bounds.limit ? null : await readChatOrdered(db, chatId, bounds);
84
+ const ordered = history && bounds.limit
85
+ ? null
86
+ : await readChatOrdered(db, chatId, {
87
+ types: history ? HISTORY_CHUNK_TYPES : null,
88
+ ...bounds
89
+ });
58
90
  if (ordered) {
59
- return history ? ordered.filter((chunk) => HISTORY_CHUNK_TYPES.includes(chunk.type)) : ordered;
91
+ return ordered;
60
92
  }
61
93
  return db
62
94
  .find(history ? HISTORY_CHUNKS : CHUNKS_BY_CHAT, keyRange({ chatId }, 'seq', bounds))
63
95
  .toArray();
64
96
  }
65
- async function readChatOrdered(db, chatId, bounds) {
97
+ async function readChatOrdered(db, chatId, { types, ...bounds }) {
66
98
  const counter = await db.get(CHUNK_COUNTERS, { chatId });
67
99
  if (!counter) {
68
100
  return null;
@@ -82,7 +114,8 @@ async function readChatOrdered(db, chatId, bounds) {
82
114
  const settled = new Set(chunks
83
115
  .filter((chunk) => TERMINAL_RUN_STATES.has(chunk.runStatus?.state ?? ''))
84
116
  .map((c) => c.runId));
85
- return Promise.all(chunks.map((chunk) => (changeable(chunk, settled) ? current(db, chunk) : chunk)));
117
+ const selected = types ? chunks.filter((chunk) => types.includes(chunk.type)) : chunks;
118
+ return Promise.all(selected.map((chunk) => (changeable(chunk, settled) ? current(db, chunk) : chunk)));
86
119
  }
87
120
  function changeable(chunk, settled) {
88
121
  return ((chunk.type === 'run-status' && !settled.has(chunk.runId)) ||
@@ -214,6 +247,7 @@ export async function appendChunk(store, { id = randomId(), chatId, runId, type,
214
247
  capabilityArgs,
215
248
  enhance,
216
249
  sourceId,
250
+ falsyResult: falsyResultOf(payload),
217
251
  createdAt: Date.now(),
218
252
  senderMemberId: type === 'user-message' && store.ourDeviceId && b4a.equals(store.ourDeviceId, originDeviceId)
219
253
  ? store.ourMemberId || undefined
@@ -25,11 +25,14 @@ export declare function documentUnreadReason({ knowledgeOn, failedFiles, attache
25
25
  export declare function documentUnreadNotice(reason: DocumentUnreadReason, plural: boolean): string;
26
26
  export interface PdfPagesCutState {
27
27
  readonly fileName: string;
28
- readonly lastPage: number;
28
+ readonly cutAt: number;
29
29
  readonly pageCount: number;
30
+ readonly shown: number;
31
+ readonly wholeText: boolean;
30
32
  }
31
- export declare function pdfPagesCutNotice({ fileName, lastPage, pageCount }: PdfPagesCutState): string;
32
- export declare function pdfPagesSkippedNotice(fileName: string): string;
33
+ export declare function pdfPagesCutNotice({ fileName, cutAt, pageCount, shown, wholeText }: PdfPagesCutState): string;
34
+ export declare const PDF_PAGES_SOURCE_LINE = "Page images from the PDFs in this chat are a source too: when an image shows what the excerpts do not, answer from the image.";
35
+ export declare function pdfPagesShownNotice(fileName: string, labels: ReadonlyArray<string>): string;
33
36
  export interface AnnotatedMessage {
34
37
  content: string;
35
38
  documents?: DocumentAttachment[];
@@ -33,11 +33,16 @@ export function documentUnreadNotice(reason, plural) {
33
33
  }[reason];
34
34
  return `[The attached document${plural ? 's' : ''} could not be read for this reply: ${why}.]`;
35
35
  }
36
- export function pdfPagesCutNotice({ fileName, lastPage, pageCount }) {
37
- return `[Only pages 1–${lastPage} of "${fileName}" (${pageCount} pages) were shown to you as images; pages ${lastPage + 1}–${pageCount} were not, and no tool can show them. If asked about a later page you have no excerpt for, tell the user only the first ${lastPage} pages could be read. Do not guess its content.]`;
38
- }
39
- export function pdfPagesSkippedNotice(fileName) {
40
- return `[No page of "${fileName}" was shown to you as an image: the files before it in this message used up the page images, and no tool can show them. If asked about it and you have no excerpt, tell the user it could not be read. Do not guess its content.]`;
36
+ export function pdfPagesCutNotice({ fileName, cutAt, pageCount, shown, wholeText }) {
37
+ const missing = shown
38
+ ? `Some pictures in "${fileName}" (${pageCount} pages), the first on page ${cutAt + 1}, were not shown to you as images`
39
+ : `No picture in "${fileName}" (${pageCount} pages) was shown to you as an image`;
40
+ const text = wholeText ? '; its text is all above' : '';
41
+ return `[${missing}: they did not fit in the context window, and no tool can show them${text}. If asked about a picture you cannot see, say it could not be shown. Do not guess its content.]`;
42
+ }
43
+ export const PDF_PAGES_SOURCE_LINE = 'Page images from the PDFs in this chat are a source too: when an image shows what the excerpts do not, answer from the image.';
44
+ export function pdfPagesShownNotice(fileName, labels) {
45
+ return `[The images from "${fileName}" are, in order: ${labels.join('; ')}.]`;
41
46
  }
42
47
  export function annotateDocumentAttachments(messages) {
43
48
  for (const turn of messages.filter((message) => message.documents?.length)) {
@@ -1,6 +1,6 @@
1
1
  import { CoreError } from '../../errors.js';
2
2
  import { linkAttachment } from '../attachment-link.js';
3
- import { getChunk, updateChunk } from '../chunk.js';
3
+ import { getChunk, readChunk, updateChunk } from '../chunk.js';
4
4
  import { uploadFile } from './upload.js';
5
5
  import b4a from 'b4a';
6
6
  export const MAX_ATTACHMENT_BYTES = 100 * 1024 * 1024;
@@ -39,7 +39,7 @@ export function attachmentFiles(mesh, { maxBytes = MAX_ATTACHMENT_BYTES, deviceI
39
39
  }
40
40
  const attachment = chunk.attachment;
41
41
  if (attachment.deletedAt) {
42
- return chunk;
42
+ return readChunk(chunk);
43
43
  }
44
44
  const updated = await updateChunk(mesh, {
45
45
  id: chunkId,
@@ -48,7 +48,7 @@ export function attachmentFiles(mesh, { maxBytes = MAX_ATTACHMENT_BYTES, deviceI
48
48
  if (!updated.attachment?.deletedAt) {
49
49
  throw new CoreError('ATTACHMENT_DELETE_DROPPED');
50
50
  }
51
- return updated;
51
+ return readChunk(updated);
52
52
  }
53
53
  };
54
54
  }
@@ -122,7 +122,7 @@ export declare class Files extends EventEmitter {
122
122
  private _processorStatus;
123
123
  close(): Promise<void>;
124
124
  }
125
- export { MemoryVectorIndex, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, WEB_TOOLS, chooseRagNotice, citedByPhraseOverlap, createRagSourceTagFilter, formatFullDocumentContext, formatRagContext, isFullTextHit, isWholeDocumentQuery, referencedDocuments, isTruncatedFullTextHit, sourcesFromHits, webSourcesFromToolResult, withoutDataChannel } from './rag/index.ts';
125
+ export { MemoryVectorIndex, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, WEB_TOOLS, chooseRagNotice, citedByPhraseOverlap, createRagSourceTagFilter, formatFullDocumentContext, formatRagContext, fullTextAttachmentId, isFullTextHit, isWholeDocumentQuery, referencedDocuments, isTruncatedFullTextHit, sourcesFromHits, webSourcesFromToolResult, withoutDataChannel } from './rag/index.ts';
126
126
  export type { IndexingEmbedder, RagExtractors, RagSource, VectorIndex } from './rag/index.ts';
127
127
  export { attachTurnDocumentIds, groupAttachmentsByMessage } from './records/index.ts';
128
128
  export { CoreBlobServer, PENDING_BLOB_REFERENCE, isPendingBlobReference, parseBlobReferenceCoreKey } from './store/index.ts';
@@ -332,7 +332,8 @@ export class Files extends EventEmitter {
332
332
  processing: this._processing,
333
333
  chatIds: async () => {
334
334
  const rows = await Promise.all(ids.map((id) => getKnowledge(this._mesh, id)));
335
- return [...new Set(rows.flatMap((row) => (row ? [row.chatId] : [])))];
335
+ const chatIds = rows.flatMap((row) => (row ? [row.chatId] : []));
336
+ return [...new Set(options.chatId ? [options.chatId, ...chatIds] : chatIds)];
336
337
  }
337
338
  });
338
339
  }
@@ -657,6 +658,6 @@ function fileState(processor, opts) {
657
658
  }
658
659
  return { state: 'pending', reason: 'source unavailable on this device' };
659
660
  }
660
- export { MemoryVectorIndex, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, WEB_TOOLS, chooseRagNotice, citedByPhraseOverlap, createRagSourceTagFilter, formatFullDocumentContext, formatRagContext, isFullTextHit, isWholeDocumentQuery, referencedDocuments, isTruncatedFullTextHit, sourcesFromHits, webSourcesFromToolResult, withoutDataChannel } from './rag/index.js';
661
+ export { MemoryVectorIndex, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, WEB_TOOLS, chooseRagNotice, citedByPhraseOverlap, createRagSourceTagFilter, formatFullDocumentContext, formatRagContext, fullTextAttachmentId, isFullTextHit, isWholeDocumentQuery, referencedDocuments, isTruncatedFullTextHit, sourcesFromHits, webSourcesFromToolResult, withoutDataChannel } from './rag/index.js';
661
662
  export { attachTurnDocumentIds, groupAttachmentsByMessage } from './records/index.js';
662
663
  export { CoreBlobServer, PENDING_BLOB_REFERENCE, isPendingBlobReference, parseBlobReferenceCoreKey } from './store/index.js';
@@ -61,6 +61,7 @@ export declare class FileProcessing {
61
61
  enqueue(ids: Iterable<string>, options?: EnqueueOptions): Promise<void>;
62
62
  private _enqueue;
63
63
  get active(): boolean;
64
+ canProduce(id: string): Promise<boolean>;
64
65
  pending(id: string): boolean;
65
66
  prioritize(ids: string[]): void;
66
67
  private _shutdown;
@@ -118,6 +118,14 @@ export class FileProcessing {
118
118
  get active() {
119
119
  return !this._paused && !this._closed && !this._mesh.closing;
120
120
  }
121
+ async canProduce(id) {
122
+ const record = await fileRecord(this._mesh.view, id, this._processor.input);
123
+ return Boolean(record &&
124
+ !record.deletedAt &&
125
+ this._mesh.writable &&
126
+ this._processor.applies(record) &&
127
+ (await this._processor.canProduce(record, { mesh: this._mesh, deviceId: this._deviceId })));
128
+ }
121
129
  pending(id) {
122
130
  return (!this._paused &&
123
131
  !this._closed &&
@@ -3,6 +3,7 @@ import type { Files } from '../index.ts';
3
3
  import type { FileProcessing } from './index.ts';
4
4
  export interface SettleOptions {
5
5
  readonly timeoutMs?: number;
6
+ readonly chatId?: string;
6
7
  readonly signal?: AbortSignal;
7
8
  }
8
9
  export interface SettleFilesOptions extends SettleOptions {
@@ -48,22 +48,29 @@ export function settleFiles(files, options) {
48
48
  if (!processing.active) {
49
49
  return finish();
50
50
  }
51
- const waiting = snapshots
52
- .flatMap((snapshot) => snapshot.files)
53
- .some((file) => {
51
+ let waiting = false;
52
+ for (const file of snapshots.flatMap((snapshot) => snapshot.files)) {
53
+ if (waiting)
54
+ break;
54
55
  if (!wanted.has(file.fileId) || file.readyForChat)
55
- return false;
56
+ continue;
56
57
  const rag = file.processors.find((entry) => entry.processorId === 'rag');
57
- if (!rag || rag.state === 'not-applicable') {
58
- return false;
58
+ const settled = !rag ||
59
+ rag.state === 'not-applicable' ||
60
+ (rag.state === 'failed' && (!rag.retryAt || rag.retryAt > deadline));
61
+ if (settled) {
62
+ continue;
59
63
  }
60
- if (rag.state === 'failed' && (!rag.retryAt || rag.retryAt > deadline)) {
61
- return false;
62
- }
63
- return (processing.pending(file.fileId) ||
64
- rag.state === 'running' ||
65
- (rag.retryAt !== undefined && rag.retryAt <= deadline));
66
- });
64
+ waiting =
65
+ processing.pending(file.fileId) ||
66
+ rag.state === 'running' ||
67
+ (rag.retryAt !== undefined && rag.retryAt <= deadline) ||
68
+ (rag.reason === 'waiting for agent' &&
69
+ (!(await files.record(file.fileId)) || (await processing.canProduce(file.fileId))));
70
+ }
71
+ if (done) {
72
+ return;
73
+ }
67
74
  if (!waiting) {
68
75
  finish();
69
76
  }
@@ -31,6 +31,6 @@ export declare class RagProcessor implements FileProcessor {
31
31
  export { MAX_KNOWLEDGE_BYTES } from './limits.ts';
32
32
  export { canIndexChat } from './policy.ts';
33
33
  export type { IndexingEmbedder, IndexingProgress } from './produce.ts';
34
- export { formatFullDocumentContext, fullTextBudgetChars, fullTextHits, isFullTextHit, isTruncatedFullTextHit, retrievalBudgetTokens } from './retrieval/full-text.ts';
34
+ export { formatFullDocumentContext, fullTextBudgetChars, fullTextHits, fullTextAttachmentId, isFullTextHit, isTruncatedFullTextHit, retrievalBudgetTokens } from './retrieval/full-text.ts';
35
35
  export { MemoryVectorIndex, RAG_DOCUMENT_KIND, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, WEB_TOOLS, chooseRagNotice, citedByPhraseOverlap, classifyRagDocument, createRagSourceTagFilter, encodeRagArtifact, formatRagContext, isSupportedRagDocument, isWholeDocumentQuery, referencedDocuments, referencesPendingFile, retrieveRagKnowledge, searchRagChunks, sourcesFromHits, webSourcesFromToolResult, withoutDataChannel } from 'rag-extract';
36
36
  export type { RagArtifactPayload, RagDocumentRef, RagExtractors, RagSource, VectorHit, VectorIndex } from 'rag-extract';
@@ -70,5 +70,5 @@ export class RagProcessor {
70
70
  }
71
71
  export { MAX_KNOWLEDGE_BYTES } from './limits.js';
72
72
  export { canIndexChat } from './policy.js';
73
- export { formatFullDocumentContext, fullTextBudgetChars, fullTextHits, isFullTextHit, isTruncatedFullTextHit, retrievalBudgetTokens } from './retrieval/full-text.js';
73
+ export { formatFullDocumentContext, fullTextBudgetChars, fullTextHits, fullTextAttachmentId, isFullTextHit, isTruncatedFullTextHit, retrievalBudgetTokens } from './retrieval/full-text.js';
74
74
  export { MemoryVectorIndex, RAG_DOCUMENT_KIND, RAG_KNOWLEDGE_OFF_NOTICE, RAG_TRUNCATED_NOTICE, RAG_UNAVAILABLE_NOTICE, WEB_TOOLS, chooseRagNotice, citedByPhraseOverlap, classifyRagDocument, createRagSourceTagFilter, encodeRagArtifact, formatRagContext, isSupportedRagDocument, isWholeDocumentQuery, referencedDocuments, referencesPendingFile, retrieveRagKnowledge, searchRagChunks, sourcesFromHits, webSourcesFromToolResult, withoutDataChannel } from 'rag-extract';
@@ -7,6 +7,7 @@ export interface IdentifiedHit {
7
7
  readonly id: string;
8
8
  }
9
9
  export declare function isFullTextHit(hit: IdentifiedHit): boolean;
10
+ export declare function fullTextAttachmentId(hit: IdentifiedHit): string | null;
10
11
  export type FullTextCoverage = {
11
12
  readonly unit: 'pages';
12
13
  readonly included: number;
@@ -21,6 +21,9 @@ const FULL_TEXT_HIT_PREFIX = 'full-text-';
21
21
  export function isFullTextHit(hit) {
22
22
  return hit.id.startsWith(FULL_TEXT_HIT_PREFIX);
23
23
  }
24
+ export function fullTextAttachmentId(hit) {
25
+ return isFullTextHit(hit) ? hit.id.slice(FULL_TEXT_HIT_PREFIX.length) : null;
26
+ }
24
27
  const FullTextExtrasSchema = z.object({
25
28
  truncated: z.boolean().optional().catch(undefined),
26
29
  coverage: z
@@ -24,6 +24,8 @@ export declare class HostedAgents {
24
24
  private readonly _log;
25
25
  private readonly _heldKey;
26
26
  private readonly _lock;
27
+ private readonly _retry;
28
+ private readonly _deadline;
27
29
  private _closed;
28
30
  private readonly _onDirty;
29
31
  constructor(opts: HostedAgentsOptions);
@@ -32,6 +34,7 @@ export declare class HostedAgents {
32
34
  close(): Promise<void>;
33
35
  private _run;
34
36
  private _pass;
37
+ private _scheduleRetry;
35
38
  private _handOver;
36
39
  private _stored;
37
40
  private _held;
@@ -1,8 +1,11 @@
1
1
  import b4a from 'b4a';
2
2
  import ScopeLock from 'scope-lock';
3
+ import { DeadlineScheduler } from '../deadline.js';
4
+ import { RetryBudget } from '../retry-budget.js';
3
5
  import { getAgent, listAgents } from './agent.js';
4
6
  import { agentSkillValues } from './skill-secrets.js';
5
7
  const SCOPE = ['p2p', 'skill-values'];
8
+ const RETRY_MS = 1_000;
6
9
  export const LEGACY_HELD_AGENTS_KEY = 'skill-values.held-agents';
7
10
  export function heldAgentsKey(meshKey) {
8
11
  return `${LEGACY_HELD_AGENTS_KEY}.${b4a.toHex(meshKey)}`;
@@ -17,6 +20,8 @@ export class HostedAgents {
17
20
  _log;
18
21
  _heldKey;
19
22
  _lock = new ScopeLock({ debounce: true });
23
+ _retry = new RetryBudget({ retryMs: RETRY_MS });
24
+ _deadline = new DeadlineScheduler(() => this.kick());
20
25
  _closed;
21
26
  _onDirty = () => this.kick();
22
27
  constructor(opts) {
@@ -40,6 +45,7 @@ export class HostedAgents {
40
45
  async close() {
41
46
  this._closed = true;
42
47
  this._mesh.off('agents-dirty', this._onDirty);
48
+ this._deadline.clear();
43
49
  this._lock.destroy();
44
50
  await this._lock.flush();
45
51
  }
@@ -76,13 +82,26 @@ export class HostedAgents {
76
82
  });
77
83
  if (released) {
78
84
  holding.delete(id);
85
+ this._retry.reset(id);
86
+ }
87
+ else {
88
+ this._retry.fail(id, Date.now());
79
89
  }
80
90
  }
81
91
  }
92
+ this._scheduleRetry(new Set(departed.filter((id) => holding.has(id))));
82
93
  if (!sameIds(held, holding)) {
83
94
  await this._local.set(this._heldKey, [...holding]);
84
95
  }
85
96
  }
97
+ _scheduleRetry(pending) {
98
+ this._retry.retain(pending);
99
+ this._deadline.clear();
100
+ const next = this._retry.nextDeadline(pending, Date.now());
101
+ if (next !== null && !this._closed) {
102
+ this._deadline.schedule(next);
103
+ }
104
+ }
86
105
  async _handOver(id, catalog) {
87
106
  const agent = await getAgent(this._mesh, id);
88
107
  if (!agent) {
@@ -1,5 +1,5 @@
1
1
  import { diagramRepairLinks, isDiagramRepairPrompt } from '@qvac/harness/lib/diagram-repair.ts';
2
- import { findChunksOfType } from './chunk.js';
2
+ import { findChunksOfType, findChunksOfTypes } from './chunk.js';
3
3
  import b4a from 'b4a';
4
4
  import { ACTIVE_CHATS, AGENTS, CHUNK_COUNTERS, DEVICES, MEMBERS } from './collections.js';
5
5
  const DEFAULT_LIMIT = 200;
@@ -46,7 +46,7 @@ export async function deriveNotifications(mesh, local, { limit: requestedLimit,
46
46
  }
47
47
  const contentByChat = new Map();
48
48
  for (const [chatId, terminalSeq] of terminalSeqByChat) {
49
- contentByChat.set(chatId, await findChunksOfType(snap, { chatId, type: 'content', lte: terminalSeq }));
49
+ contentByChat.set(chatId, await findChunksOfTypes(snap, { chatId, types: ['content'], lte: terminalSeq }));
50
50
  }
51
51
  const agentNameById = new Map();
52
52
  for (const agentId of new Set(selected.map((candidate) => candidate.agentId))) {
@@ -74,15 +74,18 @@ export async function deriveNotifications(mesh, local, { limit: requestedLimit,
74
74
  async function chatCandidates(snap, chat, { local, deviceId, own }) {
75
75
  const counter = await snap.get(CHUNK_COUNTERS, { chatId: chat.id });
76
76
  const ackedThrough = (await local.getChatNotify(chat.id))?.ackedThrough ?? 0;
77
- const [statuses, approvals, messages] = await Promise.all([
77
+ const [unread, approvals] = await Promise.all([
78
78
  (counter?.count ?? 0) <= ackedThrough
79
79
  ? []
80
- : findChunksOfType(snap, { chatId: chat.id, type: 'run-status', gte: ackedThrough }),
81
- findChunksOfType(snap, { chatId: chat.id, type: 'approval-request' }),
82
- (counter?.count ?? 0) <= ackedThrough
83
- ? []
84
- : findChunksOfType(snap, { chatId: chat.id, type: 'user-message', gte: ackedThrough })
80
+ : findChunksOfTypes(snap, {
81
+ chatId: chat.id,
82
+ types: ['run-status', 'user-message'],
83
+ gte: ackedThrough
84
+ }),
85
+ findChunksOfType(snap, { chatId: chat.id, type: 'approval-request' })
85
86
  ]);
87
+ const statuses = unread.filter((chunk) => chunk.type === 'run-status');
88
+ const messages = unread.filter((chunk) => chunk.type === 'user-message');
86
89
  const terminalStatuses = statuses.filter((chunk) => {
87
90
  const state = chunk.runStatus?.state;
88
91
  return state === 'completed' || state === 'failed';
@@ -148,11 +151,11 @@ async function messageCandidate(snap, chunk, { chat, deviceId, own }) {
148
151
  };
149
152
  }
150
153
  async function diagramRepairRunIds(snap, chatId, toSeq) {
151
- const [users, requests] = await Promise.all([
152
- findChunksOfType(snap, { chatId, type: 'user-message', lte: toSeq }),
153
- findChunksOfType(snap, { chatId, type: 'run-request', lte: toSeq })
154
- ]);
155
- const records = [...users, ...requests];
154
+ const records = await findChunksOfTypes(snap, {
155
+ chatId,
156
+ types: ['user-message', 'run-request'],
157
+ lte: toSeq
158
+ });
156
159
  const repairs = diagramRepairLinks(records, (chunk) => chunk.userMessage?.text ?? null);
157
160
  const runIds = new Set();
158
161
  for (const chunk of records) {
@@ -0,0 +1,22 @@
1
+ export interface PdfBounds {
2
+ readonly x: number;
3
+ readonly y: number;
4
+ readonly width: number;
5
+ readonly height: number;
6
+ }
7
+ export interface PdfTextSpan extends PdfBounds {
8
+ readonly text: string;
9
+ }
10
+ export interface PdfFigure {
11
+ readonly clip: PdfBounds;
12
+ readonly names: ReadonlyArray<string>;
13
+ }
14
+ export interface PdfPageLayout {
15
+ readonly size: {
16
+ readonly width: number;
17
+ readonly height: number;
18
+ };
19
+ readonly images: ReadonlyArray<PdfBounds>;
20
+ readonly spans: ReadonlyArray<PdfTextSpan>;
21
+ }
22
+ export declare function figureRegion({ size, images, spans }: PdfPageLayout): PdfFigure | null;