@qvac/core 0.8.0 → 0.10.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 (204) hide show
  1. package/CHANGELOG.md +114 -1
  2. package/README.md +5 -5
  3. package/dist-lib/index.d.ts +0 -1
  4. package/dist-lib/index.js +0 -1
  5. package/dist-lib/lib/api/agents.d.ts +7 -1
  6. package/dist-lib/lib/api/agents.js +2 -2
  7. package/dist-lib/lib/api/app-config.d.ts +2 -1
  8. package/dist-lib/lib/api/app-config.js +2 -2
  9. package/dist-lib/lib/api/attachments.js +3 -2
  10. package/dist-lib/lib/api/chat-move.d.ts +2 -1
  11. package/dist-lib/lib/api/chat-move.js +2 -2
  12. package/dist-lib/lib/api/chat.d.ts +3 -1
  13. package/dist-lib/lib/api/chat.js +5 -5
  14. package/dist-lib/lib/api/chunks.d.ts +2 -1
  15. package/dist-lib/lib/api/chunks.js +3 -3
  16. package/dist-lib/lib/api/config.d.ts +2 -1
  17. package/dist-lib/lib/api/config.js +2 -2
  18. package/dist-lib/lib/api/current-mesh.d.ts +1 -3
  19. package/dist-lib/lib/api/current-mesh.js +4 -10
  20. package/dist-lib/lib/api/custom-models.d.ts +7 -1
  21. package/dist-lib/lib/api/custom-models.js +10 -8
  22. package/dist-lib/lib/api/device-routes.d.ts +0 -2
  23. package/dist-lib/lib/api/device-routes.js +4 -5
  24. package/dist-lib/lib/api/devices.d.ts +3 -1
  25. package/dist-lib/lib/api/devices.js +9 -8
  26. package/dist-lib/lib/api/diagnostics.d.ts +2 -1
  27. package/dist-lib/lib/api/diagnostics.js +2 -2
  28. package/dist-lib/lib/api/index.d.ts +2 -0
  29. package/dist-lib/lib/api/index.js +39 -21
  30. package/dist-lib/lib/api/instrument.d.ts +2 -2
  31. package/dist-lib/lib/api/instrument.js +10 -11
  32. package/dist-lib/lib/api/knowledge.d.ts +3 -1
  33. package/dist-lib/lib/api/knowledge.js +9 -7
  34. package/dist-lib/lib/api/logs.js +3 -3
  35. package/dist-lib/lib/api/members.d.ts +6 -1
  36. package/dist-lib/lib/api/members.js +11 -10
  37. package/dist-lib/lib/api/mesh.d.ts +2 -2
  38. package/dist-lib/lib/api/mesh.js +6 -6
  39. package/dist-lib/lib/api/model-download.d.ts +2 -1
  40. package/dist-lib/lib/api/model-download.js +2 -2
  41. package/dist-lib/lib/api/network.d.ts +3 -2
  42. package/dist-lib/lib/api/network.js +4 -4
  43. package/dist-lib/lib/api/notifications.d.ts +3 -1
  44. package/dist-lib/lib/api/notifications.js +5 -4
  45. package/dist-lib/lib/api/rag.d.ts +0 -1
  46. package/dist-lib/lib/api/rag.js +3 -4
  47. package/dist-lib/lib/api/sync.d.ts +2 -1
  48. package/dist-lib/lib/api/sync.js +2 -2
  49. package/dist-lib/lib/api/tool-activity.d.ts +2 -1
  50. package/dist-lib/lib/api/tool-activity.js +4 -3
  51. package/dist-lib/lib/api/tool-configs.d.ts +2 -1
  52. package/dist-lib/lib/api/tool-configs.js +4 -3
  53. package/dist-lib/lib/api/upload-frames.d.ts +0 -1
  54. package/dist-lib/lib/api/upload-frames.js +0 -1
  55. package/dist-lib/lib/api/watchable.d.ts +3 -2
  56. package/dist-lib/lib/api/watchable.js +2 -3
  57. package/dist-lib/lib/assistant/fake-models.d.ts +2 -4
  58. package/dist-lib/lib/assistant/harness.js +8 -6
  59. package/dist-lib/lib/assistant-client.d.ts +2 -0
  60. package/dist-lib/lib/assistant-client.js +4 -3
  61. package/dist-lib/lib/assistant-tree.d.ts +3 -3
  62. package/dist-lib/lib/assistant.d.ts +2 -2
  63. package/dist-lib/lib/assistant.js +11 -11
  64. package/dist-lib/lib/client.d.ts +2 -2
  65. package/dist-lib/lib/core/agent.js +5 -4
  66. package/dist-lib/lib/core/archived-mesh.d.ts +2 -3
  67. package/dist-lib/lib/core/archived-mesh.js +3 -3
  68. package/dist-lib/lib/core/chat-activity.js +5 -3
  69. package/dist-lib/lib/core/chat-copy.d.ts +0 -1
  70. package/dist-lib/lib/core/chat-copy.js +2 -2
  71. package/dist-lib/lib/core/chat-fork.d.ts +0 -6
  72. package/dist-lib/lib/core/chat-fork.js +11 -17
  73. package/dist-lib/lib/core/chat-group.js +8 -7
  74. package/dist-lib/lib/core/chat-move.d.ts +4 -6
  75. package/dist-lib/lib/core/chat-move.js +14 -15
  76. package/dist-lib/lib/core/chat-order.d.ts +2 -1
  77. package/dist-lib/lib/core/chat-order.js +15 -2
  78. package/dist-lib/lib/core/chat.js +8 -7
  79. package/dist-lib/lib/core/chunk.js +13 -8
  80. package/dist-lib/lib/core/collections.d.ts +1 -0
  81. package/dist-lib/lib/core/collections.js +1 -0
  82. package/dist-lib/lib/core/contract/compute.d.ts +0 -4
  83. package/dist-lib/lib/core/contract/compute.js +2 -9
  84. package/dist-lib/lib/core/contract/types.d.ts +2 -2
  85. package/dist-lib/lib/core/departures.d.ts +2 -2
  86. package/dist-lib/lib/core/device-control.d.ts +0 -2
  87. package/dist-lib/lib/core/device-control.js +5 -6
  88. package/dist-lib/lib/core/files/attachments.js +5 -4
  89. package/dist-lib/lib/core/files/index.d.ts +2 -1
  90. package/dist-lib/lib/core/files/index.js +20 -8
  91. package/dist-lib/lib/core/files/processing/claims.js +3 -3
  92. package/dist-lib/lib/core/files/processing/document.d.ts +2 -0
  93. package/dist-lib/lib/core/files/processing/document.js +3 -0
  94. package/dist-lib/lib/core/files/processing/index.js +4 -2
  95. package/dist-lib/lib/core/files/processing/source.d.ts +3 -0
  96. package/dist-lib/lib/core/files/processing/source.js +5 -3
  97. package/dist-lib/lib/core/files/processing/state.js +1 -0
  98. package/dist-lib/lib/core/files/rag/index.d.ts +3 -0
  99. package/dist-lib/lib/core/files/rag/index.js +3 -1
  100. package/dist-lib/lib/core/files/rag/load.js +6 -3
  101. package/dist-lib/lib/core/files/rag/produce.js +3 -3
  102. package/dist-lib/lib/core/files/rag/retrieval/full-text.js +1 -1
  103. package/dist-lib/lib/core/files/rag/similarity.d.ts +2 -2
  104. package/dist-lib/lib/core/files/rag/similarity.js +2 -3
  105. package/dist-lib/lib/core/files/records/chunks.js +1 -1
  106. package/dist-lib/lib/core/files/records/knowledge.js +2 -1
  107. package/dist-lib/lib/core/files/records/purge.js +9 -5
  108. package/dist-lib/lib/core/files/records/reclaim.js +20 -48
  109. package/dist-lib/lib/core/files/records/sweep.d.ts +3 -0
  110. package/dist-lib/lib/core/files/records/sweep.js +9 -3
  111. package/dist-lib/lib/core/files/store/blob.d.ts +2 -0
  112. package/dist-lib/lib/core/files/store/blob.js +2 -2
  113. package/dist-lib/lib/core/files/store/index.d.ts +1 -0
  114. package/dist-lib/lib/core/files/store/index.js +2 -0
  115. package/dist-lib/lib/core/files/store/reference.js +4 -2
  116. package/dist-lib/lib/core/files/upload.d.ts +1 -1
  117. package/dist-lib/lib/core/files/upload.js +10 -9
  118. package/dist-lib/lib/core/handoff-channel.d.ts +6 -4
  119. package/dist-lib/lib/core/handoff-channel.js +8 -9
  120. package/dist-lib/lib/core/harness-loan.d.ts +0 -2
  121. package/dist-lib/lib/core/harness-loan.js +3 -4
  122. package/dist-lib/lib/core/hosted-agents.d.ts +2 -2
  123. package/dist-lib/lib/core/inference-calls-handoff.d.ts +2 -2
  124. package/dist-lib/lib/core/inference-calls-handoff.js +6 -5
  125. package/dist-lib/lib/core/inference-calls-receiver.d.ts +0 -2
  126. package/dist-lib/lib/core/inference-calls-receiver.js +5 -7
  127. package/dist-lib/lib/core/invite.js +9 -4
  128. package/dist-lib/lib/core/local/index.d.ts +4 -1
  129. package/dist-lib/lib/core/local/index.js +23 -2
  130. package/dist-lib/lib/core/membership.d.ts +2 -2
  131. package/dist-lib/lib/core/mesh.d.ts +13 -7
  132. package/dist-lib/lib/core/mesh.js +55 -47
  133. package/dist-lib/lib/core/migrations/file-index.js +2 -4
  134. package/dist-lib/lib/core/next-frame.js +2 -1
  135. package/dist-lib/lib/core/pdf/rasterize.d.ts +2 -1
  136. package/dist-lib/lib/core/pdf/rasterize.js +5 -5
  137. package/dist-lib/lib/core/peer-duplex.d.ts +3 -3
  138. package/dist-lib/lib/core/peer-duplex.js +4 -4
  139. package/dist-lib/lib/core/secret-handoff.d.ts +2 -2
  140. package/dist-lib/lib/core/secret-handoff.js +7 -4
  141. package/dist-lib/lib/core/send.js +3 -2
  142. package/dist-lib/lib/core/skill-overlay-handoff.d.ts +2 -2
  143. package/dist-lib/lib/core/skill-overlay-handoff.js +6 -5
  144. package/dist-lib/lib/core/skill-overlay-receiver.js +5 -5
  145. package/dist-lib/lib/core/skill-secrets.d.ts +0 -5
  146. package/dist-lib/lib/core/skill-secrets.js +5 -9
  147. package/dist-lib/lib/core/sync/changed.d.ts +20 -0
  148. package/dist-lib/lib/core/sync/changed.js +7 -0
  149. package/dist-lib/lib/core/sync/devices.js +1 -1
  150. package/dist-lib/lib/core/sync/files.d.ts +1 -0
  151. package/dist-lib/lib/core/sync/files.js +50 -72
  152. package/dist-lib/lib/core/sync/index.d.ts +1 -2
  153. package/dist-lib/lib/core/sync/index.js +40 -62
  154. package/dist-lib/lib/core/sync/view-updates.d.ts +33 -0
  155. package/dist-lib/lib/core/sync/view-updates.js +133 -0
  156. package/dist-lib/lib/core/system-info.d.ts +2 -2
  157. package/dist-lib/lib/core/system-info.js +10 -11
  158. package/dist-lib/lib/core/tool-configs.js +6 -6
  159. package/dist-lib/lib/core.d.ts +1 -7
  160. package/dist-lib/lib/core.js +147 -131
  161. package/dist-lib/lib/custom-model-pusher.d.ts +2 -4
  162. package/dist-lib/lib/custom-model-task.d.ts +2 -1
  163. package/dist-lib/lib/custom-model-task.js +2 -2
  164. package/dist-lib/lib/downloads-allowed-pusher.d.ts +2 -4
  165. package/dist-lib/lib/errors.d.ts +603 -0
  166. package/dist-lib/lib/errors.js +552 -0
  167. package/dist-lib/lib/log.d.ts +7 -6
  168. package/dist-lib/lib/log.js +10 -6
  169. package/dist-lib/lib/logger-type.d.ts +2 -5
  170. package/dist-lib/lib/model-download-status.d.ts +2 -2
  171. package/dist-lib/lib/model-download.d.ts +2 -2
  172. package/dist-lib/lib/model-download.js +3 -2
  173. package/dist-lib/lib/model-idle-pusher.d.ts +2 -4
  174. package/dist-lib/lib/model-memory-cap.d.ts +2 -4
  175. package/dist-lib/lib/oauth/browser-connect.js +3 -2
  176. package/dist-lib/lib/oauth/flow-core.js +3 -3
  177. package/dist-lib/lib/oauth/loopback.js +10 -9
  178. package/dist-lib/lib/oauth/providers.js +4 -2
  179. package/dist-lib/lib/oauth/register.js +3 -2
  180. package/dist-lib/lib/oauth/token-exchange-core.js +5 -4
  181. package/dist-lib/lib/run-transcript.d.ts +2 -2
  182. package/dist-lib/lib/task.js +2 -1
  183. package/dist-lib/schema/engine.js +6 -3
  184. package/dist-lib/schema/field-order.js +1 -3
  185. package/dist-lib/schema/local.js +11 -0
  186. package/dist-lib/spec/hrpc/messages.js +20 -9
  187. package/dist-lib/spec/hyperschema/index.js +20 -9
  188. package/dist-lib/spec/hyperschema/schema.json +15 -0
  189. package/dist-lib/spec/hyperschema/types.d.ts +3 -0
  190. package/dist-lib/spec/local/hyperdb/db.json +16 -1
  191. package/dist-lib/spec/local/hyperdb/index.js +71 -2
  192. package/dist-lib/spec/local/hyperdb/messages.js +300 -143
  193. package/dist-lib/spec/local/hyperschema/index.js +82 -2
  194. package/dist-lib/spec/local/hyperschema/schema.json +43 -1
  195. package/dist-lib/spec/local/hyperschema/types.d.ts +12 -1
  196. package/package.json +9 -2
  197. package/tui/README.md +2 -2
  198. package/tui/app.mjs +11 -5
  199. package/tui/boot.mjs +1 -1
  200. package/worker/assistant-sidecar.mjs +7 -0
  201. package/dist-lib/lib/log-levels.d.ts +0 -7
  202. package/dist-lib/lib/log-levels.js +0 -1
  203. package/dist-lib/lib/logger.d.ts +0 -5
  204. package/dist-lib/lib/logger.js +0 -18
@@ -1,3 +1,4 @@
1
+ import { CoreError } from '../errors.js';
1
2
  import { randomId } from './id.js';
2
3
  import { place } from './rank.js';
3
4
  import { ACTIVE_CHAT_GROUPS, CHAT_GROUPS } from './collections.js';
@@ -43,24 +44,24 @@ export async function getChatGroup(store, id) {
43
44
  export async function mustGetChatGroup(store, id) {
44
45
  const group = await getChatGroup(store, id);
45
46
  if (!group) {
46
- throw new Error(`chat group not found: ${id}`);
47
+ throw new CoreError('CHAT_GROUP_NOT_FOUND', [id]);
47
48
  }
48
49
  return group;
49
50
  }
50
51
  export async function mustGetActiveChatGroup(store, id) {
51
52
  const group = await mustGetChatGroup(store, id);
52
53
  if (group.deletedAt) {
53
- throw new Error(`chat group not found: ${id}`);
54
+ throw new CoreError('CHAT_GROUP_NOT_FOUND', [id]);
54
55
  }
55
56
  return group;
56
57
  }
57
58
  function assertGroupName(name) {
58
59
  const trimmed = name.trim();
59
60
  if (!trimmed) {
60
- throw new Error('chat group name is required');
61
+ throw new CoreError('CHAT_GROUP_NAME_EMPTY');
61
62
  }
62
63
  if (trimmed.length > NAME_MAX_LENGTH) {
63
- throw new Error(`chat group name exceeds ${NAME_MAX_LENGTH} characters`);
64
+ throw new CoreError('CHAT_GROUP_NAME_TOO_LONG', [NAME_MAX_LENGTH]);
64
65
  }
65
66
  return trimmed;
66
67
  }
@@ -88,7 +89,7 @@ export async function renameChatGroup(store, id, name) {
88
89
  export async function moveChatGroup(store, id, { afterId }) {
89
90
  const anchor = afterId || undefined;
90
91
  if (anchor === id) {
91
- throw new Error(`a chat group cannot be placed after itself: ${id}`);
92
+ throw new CoreError('CHAT_GROUP_ANCHOR_SELF', [id]);
92
93
  }
93
94
  const now = Date.now();
94
95
  const snap = store.view.snapshot();
@@ -97,7 +98,7 @@ export async function moveChatGroup(store, id, { afterId }) {
97
98
  try {
98
99
  const group = await snap.get(CHAT_GROUPS, { id });
99
100
  if (!group || group.deletedAt) {
100
- throw new Error(`chat group not found: ${id}`);
101
+ throw new CoreError('CHAT_GROUP_NOT_FOUND', [id]);
101
102
  }
102
103
  current = group;
103
104
  const siblings = (await listChatGroupsIn(snap)).filter((sibling) => sibling.id !== id);
@@ -105,7 +106,7 @@ export async function moveChatGroup(store, id, { afterId }) {
105
106
  if (anchor) {
106
107
  at = siblings.findIndex((sibling) => sibling.id === anchor);
107
108
  if (at === -1) {
108
- throw new Error(`chat group not found: ${anchor}`);
109
+ throw new CoreError('CHAT_GROUP_NOT_FOUND', [anchor]);
109
110
  }
110
111
  }
111
112
  placement = place(siblings, groupSortKey, { at, now });
@@ -1,12 +1,8 @@
1
+ import type { LoggerInterface } from '@qvac/logging';
1
2
  import type { AssistantChatId, AssistantChatMove, AssistantMoveChatToMeshRequest } from '../../spec/hyperschema/types.d.ts';
2
3
  import type { Mesh } from './mesh.ts';
3
4
  import type { openLocalStore } from './local/index.ts';
4
5
  export declare const CHAT_MOVED = "chat-moved";
5
- export declare const SAME_MESH = "a chat is moved to another mesh, not the one it is in";
6
- export declare const COMPUTE_MESH = "a compute mesh carries no chats";
7
- export declare const NOT_A_WRITER = "this device cannot write to the target mesh";
8
- export declare const STILL_ARRIVING = "this chat is still arriving from a move; move it once it has landed";
9
- export declare const NO_MOVE = "no move of this chat is recorded on this device";
10
6
  export declare const SOURCE_GONE = "the chat was deleted before its files arrived";
11
7
  export declare const TARGET_GONE = "the moved chat was deleted before its files arrived";
12
8
  export declare const NOT_CARRIED = "a file could not be carried into the target";
@@ -14,11 +10,13 @@ type LocalStore = Awaited<ReturnType<typeof openLocalStore>>;
14
10
  type OpenMesh = (meshId: string) => Promise<Mesh>;
15
11
  export interface ChatMovesOptions {
16
12
  readonly deviceId: Buffer;
13
+ readonly log: LoggerInterface | null;
17
14
  }
18
15
  export declare class ChatMoves {
19
16
  private readonly _open;
20
17
  private readonly _local;
21
18
  private readonly _deviceId;
19
+ private readonly _log;
22
20
  private readonly _listeners;
23
21
  private readonly _settling;
24
22
  private readonly _moving;
@@ -26,7 +24,7 @@ export declare class ChatMoves {
26
24
  private readonly _onChange;
27
25
  private _patching;
28
26
  private _closing;
29
- constructor(open: OpenMesh, local: LocalStore, { deviceId }: ChatMovesOptions);
27
+ constructor(open: OpenMesh, local: LocalStore, { deviceId, log }: ChatMovesOptions);
30
28
  get closing(): boolean;
31
29
  watch(listener: () => void): void;
32
30
  unwatch(listener: () => void): void;
@@ -3,18 +3,14 @@ import { AbortController } from 'bare-abort-controller';
3
3
  import { AbortedError } from '@qvac/harness/race-abort';
4
4
  import { getAgent, getDefaultAgent, mustGetActiveAgent } from './agent.js';
5
5
  import { cancelActiveRuns, deleteChat, getChat, mustGetActiveChat } from './chat.js';
6
- import { CHUNK_IN_ANOTHER_CHAT, copyMovedChunks, copyMovedKnowledge, movedBlob, stripped } from './chat-copy.js';
6
+ import { copyMovedChunks, copyMovedKnowledge, movedBlob, stripped } from './chat-copy.js';
7
7
  import { appendChunk, getChunk, listChunks, updateOp } from './chunk.js';
8
8
  import { derivedId } from './id.js';
9
9
  import { isPendingBlobReference } from './files/index.js';
10
+ import { CoreError } from '../errors.js';
10
11
  import { errorMessage } from '../guards.js';
11
12
  import { CHATS, DEVICES, KNOWLEDGE, KNOWLEDGE_BY_CHAT } from './collections.js';
12
13
  export const CHAT_MOVED = 'chat-moved';
13
- export const SAME_MESH = 'a chat is moved to another mesh, not the one it is in';
14
- export const COMPUTE_MESH = 'a compute mesh carries no chats';
15
- export const NOT_A_WRITER = 'this device cannot write to the target mesh';
16
- export const STILL_ARRIVING = 'this chat is still arriving from a move; move it once it has landed';
17
- export const NO_MOVE = 'no move of this chat is recorded on this device';
18
14
  export const SOURCE_GONE = 'the chat was deleted before its files arrived';
19
15
  export const TARGET_GONE = 'the moved chat was deleted before its files arrived';
20
16
  export const NOT_CARRIED = 'a file could not be carried into the target';
@@ -22,6 +18,7 @@ export class ChatMoves {
22
18
  _open;
23
19
  _local;
24
20
  _deviceId;
21
+ _log;
25
22
  _listeners = new Set();
26
23
  _settling = new Map();
27
24
  _moving = new Map();
@@ -29,10 +26,11 @@ export class ChatMoves {
29
26
  _onChange = () => this._changed();
30
27
  _patching = Promise.resolve();
31
28
  _closing = false;
32
- constructor(open, local, { deviceId }) {
29
+ constructor(open, local, { deviceId, log }) {
33
30
  this._open = open;
34
31
  this._local = local;
35
32
  this._deviceId = deviceId;
33
+ this._log = log;
36
34
  }
37
35
  get closing() {
38
36
  return this._closing;
@@ -60,7 +58,7 @@ export class ChatMoves {
60
58
  async status({ meshId, chatId }) {
61
59
  const row = await this._local.readChatMove(b4a.from(meshId, 'hex'), chatId);
62
60
  if (!row) {
63
- throw new Error(NO_MOVE);
61
+ throw new CoreError('CHAT_MOVE_NOT_FOUND');
64
62
  }
65
63
  const settled = row.completedAt || row.failure;
66
64
  const pair = settled
@@ -98,7 +96,7 @@ export class ChatMoves {
98
96
  }
99
97
  async _begin({ meshId, chatId, targetMeshId, agentId }) {
100
98
  if (meshId === targetMeshId)
101
- throw new Error(SAME_MESH);
99
+ throw new CoreError('CHAT_MOVE_SAME_MESH');
102
100
  const pair = { source: await this._open(meshId), target: await this._open(targetMeshId) };
103
101
  await this._assertMovable(pair, chatId);
104
102
  const held = await this._local.readChatMove(pair.source.key, chatId);
@@ -110,7 +108,7 @@ export class ChatMoves {
110
108
  const [first] = await listChunks(pair.source, { chatId, limit: 1 });
111
109
  const clash = first ? await getChunk(pair.target, first.id) : null;
112
110
  if (clash && clash.chatId !== targetChatId) {
113
- throw new Error(CHUNK_IN_ANOTHER_CHAT);
111
+ throw new CoreError('CHAT_MOVE_ID_CONFLICT');
114
112
  }
115
113
  const row = {
116
114
  meshKey: pair.source.key,
@@ -126,15 +124,16 @@ export class ChatMoves {
126
124
  return this._start(row).copied;
127
125
  }
128
126
  async _assertMovable({ source, target }, chatId) {
129
- if (source.compute || target.compute)
130
- throw new Error(COMPUTE_MESH);
127
+ if (source.compute || target.compute) {
128
+ throw new CoreError('CHAT_COMPUTE_MESH');
129
+ }
131
130
  const device = await target.view.get(DEVICES, { id: this._deviceId });
132
131
  const writer = device?.writerKey && b4a.equals(device.writerKey, target.localWriterKey);
133
132
  if (!target.writable || !writer || device?.revoked) {
134
- throw new Error(NOT_A_WRITER);
133
+ throw new CoreError('CHAT_TARGET_NOT_WRITER');
135
134
  }
136
135
  if ((await pendingFiles(source, chatId)).length > 0 || (await this._arriving(source, chatId))) {
137
- throw new Error(STILL_ARRIVING);
136
+ throw new CoreError('CHAT_MOVE_STILL_ARRIVING');
138
137
  }
139
138
  }
140
139
  async _arriving(mesh, chatId) {
@@ -161,7 +160,7 @@ export class ChatMoves {
161
160
  async _fail(row, err) {
162
161
  if (this._closing)
163
162
  return;
164
- logger.warn('chat-move', 'move failed', { chatId: row.chatId, err });
163
+ this._log?.warn('chat-move', 'move failed', { chatId: row.chatId, err });
165
164
  await this._patch(row, { failure: errorMessage(err) });
166
165
  }
167
166
  async _settle(row, copied) {
@@ -10,7 +10,8 @@ export interface RankedChat {
10
10
  export declare function chatSortKey(chat: RankedChat): number;
11
11
  export declare function sortKeyBytes(value: number): Buffer;
12
12
  export declare function refreshChatOrder(mesh: Mesh, local: LocalStore, chatIds: Iterable<string>): Promise<void>;
13
- export declare function rebuildChatOrder(mesh: Mesh, local: LocalStore): Promise<void>;
13
+ export declare function rebuildChatOrder(mesh: Mesh, local: LocalStore): Promise<string[]>;
14
+ export declare function retireGoneChats(mesh: Mesh, local: LocalStore, chatIds: ReadonlyArray<string>): Promise<void>;
14
15
  export interface ChatPage extends Sidebar {
15
16
  readonly nextCursor?: string;
16
17
  }
@@ -2,6 +2,7 @@ import b4a from 'b4a';
2
2
  import { enrichAll } from './chat.js';
3
3
  import { listChatGroupsIn } from './chat-group.js';
4
4
  import { ACTIVE_CHATS, CHATS, CHUNK_COUNTERS } from './collections.js';
5
+ import { syncChatTombstone } from './sync/chats.js';
5
6
  export function chatSortKey(chat) {
6
7
  return chat.rank && (chat.rankedAt ?? 0) >= chat.lastActivityAt ? chat.rank : chat.lastActivityAt;
7
8
  }
@@ -38,18 +39,30 @@ export async function rebuildChatOrder(mesh, local) {
38
39
  }
39
40
  const live = new Set(rows.map((row) => row.chatId));
40
41
  const stored = await local.listChatOrder(mesh.key);
41
- const gone = stored.map((row) => row.chatId).filter((chatId) => !live.has(chatId));
42
- await local.writeChatOrder(mesh.key, rows, gone);
42
+ await local.writeChatOrder(mesh.key, rows);
43
+ return stored.map((row) => row.chatId).filter((chatId) => !live.has(chatId));
43
44
  }
44
45
  finally {
45
46
  await snap.close();
46
47
  }
47
48
  }
49
+ export async function retireGoneChats(mesh, local, chatIds) {
50
+ for (const chatId of chatIds) {
51
+ if (mesh.closing)
52
+ return;
53
+ const chat = await mesh.view.get(CHATS, { id: chatId });
54
+ if (chat) {
55
+ syncChatTombstone(mesh, chat);
56
+ }
57
+ await local.writeChatOrder(mesh.key, [], [chatId]);
58
+ }
59
+ }
48
60
  async function sortKeyOf(snap, chat) {
49
61
  const counter = await snap.get(CHUNK_COUNTERS, { chatId: chat.id });
50
62
  return sortKeyBytes(chatSortKey({ ...chat, lastActivityAt: counter?.lastActivityAt || chat.createdAt }));
51
63
  }
52
64
  export async function listChatPage(mesh, local, { limit, cursor } = {}) {
65
+ await mesh.synced();
53
66
  await mesh.chatOrderBuilt;
54
67
  const before = cursor === undefined ? undefined : decodeCursor(cursor);
55
68
  const ordered = await local.listChatOrder(mesh.key, { limit, before });
@@ -1,3 +1,4 @@
1
+ import { CoreError } from '../errors.js';
1
2
  import { mustGetActiveAgent } from './agent.js';
2
3
  import { listChatGroupsIn, mustGetActiveChatGroup } from './chat-group.js';
3
4
  import { appendChunk, listChunks, updateOp } from './chunk.js';
@@ -85,14 +86,14 @@ export async function getChat(store, id) {
85
86
  export async function mustGetChat(store, id) {
86
87
  const chat = await getChat(store, id);
87
88
  if (!chat) {
88
- throw new Error(`chat not found: ${id}`);
89
+ throw new CoreError('CHAT_NOT_FOUND', [id]);
89
90
  }
90
91
  return chat;
91
92
  }
92
93
  export async function mustGetActiveChat(store, id) {
93
94
  const chat = await mustGetChat(store, id);
94
95
  if (chat.deletedAt) {
95
- throw new Error(`chat not found: ${id}`);
96
+ throw new CoreError('CHAT_NOT_FOUND', [id]);
96
97
  }
97
98
  return chat;
98
99
  }
@@ -148,7 +149,7 @@ export async function moveChat(store, id, { groupId, afterId }) {
148
149
  const target = groupId || undefined;
149
150
  const anchor = afterId || undefined;
150
151
  if (anchor === id) {
151
- throw new Error(`a chat cannot be placed after itself: ${id}`);
152
+ throw new CoreError('CHAT_ANCHOR_SELF', [id]);
152
153
  }
153
154
  const now = Date.now();
154
155
  const snap = store.view.snapshot();
@@ -159,13 +160,13 @@ export async function moveChat(store, id, { groupId, afterId }) {
159
160
  if (target) {
160
161
  const group = await snap.get(CHAT_GROUPS, { id: target });
161
162
  if (!group || group.deletedAt) {
162
- throw new Error(`chat group not found: ${target}`);
163
+ throw new CoreError('CHAT_GROUP_NOT_FOUND', [target]);
163
164
  }
164
165
  }
165
166
  const { chats } = await listSidebarIn(snap);
166
167
  const me = chats.find((chat) => chat.id === id);
167
168
  if (!me) {
168
- throw new Error(`chat not found: ${id}`);
169
+ throw new CoreError('CHAT_NOT_FOUND', [id]);
169
170
  }
170
171
  moved = me;
171
172
  const siblings = chats
@@ -175,7 +176,7 @@ export async function moveChat(store, id, { groupId, afterId }) {
175
176
  if (anchor) {
176
177
  at = siblings.findIndex((sibling) => sibling.id === anchor);
177
178
  if (at === -1) {
178
- throw new Error(`chat ${anchor} is not in the ${target ? `group ${target}` : 'ungrouped list'}`);
179
+ throw new CoreError('CHAT_ANCHOR_MISPLACED', [anchor, target]);
179
180
  }
180
181
  }
181
182
  placement = place(siblings, chatSortKey, { at, now });
@@ -283,7 +284,7 @@ export async function deleteChat(store, deviceId, id) {
283
284
  batch.dispatch('put-chat', { chat: deleted });
284
285
  batch.dispatch('purge-chat', { chatId: id, at: now });
285
286
  await batch.flush();
286
- await reclaimReferences(store, references).catch((err) => logger.warn('sync', "reclaiming a deleted chat's files failed", { id, err }));
287
+ await reclaimReferences(store, references).catch((err) => store.log?.warn('sync', "reclaiming a deleted chat's files failed", { id, err }));
287
288
  return listing;
288
289
  }
289
290
  export async function softDeleteChatsForAgent(store, deviceId, agentId) {
@@ -1,6 +1,7 @@
1
1
  import { CHUNK_PAYLOAD_FIELDS, ragSourcesWithinBounds } from '../../schema/qvac.js';
2
2
  import { chunkTypeOrdinal } from '../../schema/indexes.js';
3
3
  import { mediaKindOrdinal } from '../../schema/media-kind.js';
4
+ import { CoreError } from '../errors.js';
4
5
  import { putApprovalRule } from './approval-rules.js';
5
6
  import { linkAttachment } from './attachment-link.js';
6
7
  import { randomId } from './id.js';
@@ -37,7 +38,7 @@ export async function listChunks(store, { chatId, type, types, ...bounds }) {
37
38
  return [];
38
39
  }
39
40
  if (selected && selected.length > 1 && !isHistorySelection(selected)) {
40
- throw new Error('multiple chunk types require a mapped index');
41
+ throw new CoreError('CHUNK_TYPES_UNMAPPED');
41
42
  }
42
43
  const snap = store.view.snapshot();
43
44
  try {
@@ -103,7 +104,7 @@ export async function getChunk(store, id) {
103
104
  async function mustGetChunk(store, id) {
104
105
  const chunk = await getChunk(store, id);
105
106
  if (!chunk) {
106
- throw new Error(`chunk not found: ${id}`);
107
+ throw new CoreError('CHUNK_NOT_FOUND', [id]);
107
108
  }
108
109
  return chunk;
109
110
  }
@@ -114,19 +115,19 @@ function assertPayloadMatchesType(type, payload) {
114
115
  (candidate === field)) {
115
116
  continue;
116
117
  }
117
- throw new Error(`chunk payload invalid: ${type} carries ${field} and nothing else`);
118
+ throw new CoreError('CHUNK_PAYLOAD_INVALID', [`${type} carries ${field} and nothing else`]);
118
119
  }
119
120
  }
120
121
  export async function appendChunk(store, { id = randomId(), chatId, runId, type, originDeviceId, parentId, effect, intent, display, rawCall, capability, capabilityArgs, enhance, ...payload }, into = store) {
121
122
  assertPayloadMatchesType(type, payload);
122
123
  if (type === 'sources' && !runId) {
123
- throw new Error('chunk payload invalid: a sources chunk requires runId');
124
+ throw new CoreError('CHUNK_PAYLOAD_INVALID', ['a sources chunk requires runId']);
124
125
  }
125
126
  if (type === 'sources' && !ragSourcesWithinBounds(payload.sources)) {
126
- throw new Error('chunk payload invalid: a sources chunk exceeds its wire bounds');
127
+ throw new CoreError('CHUNK_PAYLOAD_INVALID', ['a sources chunk exceeds its wire bounds']);
127
128
  }
128
129
  if (capability && type !== 'run-request') {
129
- throw new Error('chunk payload invalid: only a run-request carries a capability');
130
+ throw new CoreError('CHUNK_PAYLOAD_INVALID', ['only a run-request carries a capability']);
130
131
  }
131
132
  if (type === 'user-message' && !store.ourMemberId) {
132
133
  await store.refreshOurMember();
@@ -134,7 +135,9 @@ export async function appendChunk(store, { id = randomId(), chatId, runId, type,
134
135
  if (parentId) {
135
136
  const parent = await getChunk(store, parentId);
136
137
  if (!parent || parent.chatId !== chatId) {
137
- throw new Error(`chunk payload invalid: parentId is not a chunk in this chat: ${parentId}`);
138
+ throw new CoreError('CHUNK_PAYLOAD_INVALID', [
139
+ `parentId is not a chunk in this chat: ${parentId}`
140
+ ]);
138
141
  }
139
142
  }
140
143
  const op = {
@@ -182,7 +185,9 @@ export function updateOp(chunk, { id, runStatus, attachment }) {
182
185
  (runStatus === null || runStatus === undefined)) {
183
186
  return { id, attachment, updatedAt: Date.now() };
184
187
  }
185
- throw new Error('chunk payload invalid: a run-status edits runStatus, an attachment edits attachment');
188
+ throw new CoreError('CHUNK_PAYLOAD_INVALID', [
189
+ 'a run-status edits runStatus, an attachment edits attachment'
190
+ ]);
186
191
  }
187
192
  const DEFAULT_APPROVAL_RULE_TTL_MS = 60 * 60 * 1000;
188
193
  export async function resolveApproval(store, { id, approved, scope, expiresAt, agentId }) {
@@ -58,6 +58,7 @@ export declare const LOCAL_FILES_BY_BLOB = "@local/files-by-blob";
58
58
  export declare const LOCAL_RECLAIMABLE_FILES_BY_LAST_ACCESSED = "@local/reclaimable-files-by-last-accessed";
59
59
  export declare const LOCAL_LIVE_FILES = "@local/live-files";
60
60
  export declare const LOCAL_CHAT_MOVES = "@local/chat-moves";
61
+ export declare const LOCAL_MESH_UPDATES = "@local/mesh-updates";
61
62
  export declare const LOCAL_MESH_SESSION = "@local/mesh-session";
62
63
  export declare const LOCAL_MESH_HISTORY = "@local/mesh-history";
63
64
  export declare const LOCAL_MEMBERSHIP = "@local/membership";
@@ -58,6 +58,7 @@ export const LOCAL_FILES_BY_BLOB = '@local/files-by-blob';
58
58
  export const LOCAL_RECLAIMABLE_FILES_BY_LAST_ACCESSED = '@local/reclaimable-files-by-last-accessed';
59
59
  export const LOCAL_LIVE_FILES = '@local/live-files';
60
60
  export const LOCAL_CHAT_MOVES = '@local/chat-moves';
61
+ export const LOCAL_MESH_UPDATES = '@local/mesh-updates';
61
62
  export const LOCAL_MESH_SESSION = '@local/mesh-session';
62
63
  export const LOCAL_MESH_HISTORY = '@local/mesh-history';
63
64
  export const LOCAL_MEMBERSHIP = '@local/membership';
@@ -2,10 +2,6 @@ import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
2
2
  declare const DROPPED: readonly ['@qvac/append-chunk', '@qvac/update-chunk', '@qvac/resolve-approval', '@qvac/put-agent', '@qvac/put-approval-rule', '@qvac/seed-defaults', '@qvac/add-knowledge', '@qvac/remove-knowledge', '@qvac/add-knowledge-artifact', '@qvac/claim-indexing', '@qvac/resolve-indexing', '@qvac/put-chat', '@qvac/put-tool-configs', '@qvac/append-tool-activity', '@qvac/migrate-agent-catalog', '@qvac/reindex-agents', '@qvac/put-chat-group', '@qvac/put-joined-mesh', '@qvac/purge-chat', '@qvac/name-mesh', '@qvac/put-mesh-seed'];
3
3
  export type DroppedComputeRoute = (typeof DROPPED)[number];
4
4
  export declare function droppedComputeRoutes(): ReadonlyArray<DroppedComputeRoute>;
5
- export declare class ComputeRouteDropped extends Error {
6
- readonly route: string;
7
- constructor(route: string);
8
- }
9
5
  export declare function assertComputeRoute(compute: boolean, name: string): void;
10
6
  export declare function registerComputeDrops(router: Router): void;
11
7
  export {};
@@ -1,3 +1,4 @@
1
+ import { CoreError } from '../../errors.js';
1
2
  const DROPPED = [
2
3
  '@qvac/append-chunk',
3
4
  '@qvac/update-chunk',
@@ -25,20 +26,12 @@ const DROPPED_ROUTES = new Set(DROPPED);
25
26
  export function droppedComputeRoutes() {
26
27
  return DROPPED;
27
28
  }
28
- export class ComputeRouteDropped extends Error {
29
- route;
30
- constructor(route) {
31
- super(`${route} is dropped on a compute mesh: it carries only delegated work`);
32
- this.name = 'ComputeRouteDropped';
33
- this.route = route;
34
- }
35
- }
36
29
  export function assertComputeRoute(compute, name) {
37
30
  if (!compute)
38
31
  return;
39
32
  const route = `@qvac/${name}`;
40
33
  if (DROPPED_ROUTES.has(route)) {
41
- throw new ComputeRouteDropped(route);
34
+ throw new CoreError('COMPUTE_ROUTE_DROPPED', [route]);
42
35
  }
43
36
  }
44
37
  export function registerComputeDrops(router) {
@@ -1,11 +1,11 @@
1
1
  import type { AutobeeHost, AutobeeNode } from 'autobee';
2
2
  import type HyperDB from 'hyperdb';
3
- import type { Logger } from '../../log.ts';
3
+ import type { LoggerInterface } from '@qvac/logging';
4
4
  export interface RouterContext {
5
5
  readonly tx: HyperDB;
6
6
  readonly host: AutobeeHost;
7
7
  readonly node: AutobeeNode;
8
8
  readonly meshPublicKey: Buffer;
9
9
  readonly meshDevicePublicKey: Buffer;
10
- readonly log: Logger | null;
10
+ readonly log: LoggerInterface | null;
11
11
  }
@@ -1,7 +1,7 @@
1
1
  import type Corestore from 'corestore';
2
2
  import type Hyperswarm from 'hyperswarm';
3
3
  import type Wakeup from 'protomux-wakeup';
4
- import type { Logger } from '../../index.ts';
4
+ import type { LoggerInterface } from '@qvac/logging';
5
5
  import type { LocalMeshDeparture } from '../../spec/local/hyperschema/types.d.ts';
6
6
  import type BlindPeering from 'blind-peering';
7
7
  interface DepartureRows {
@@ -12,7 +12,7 @@ export interface PendingDeparturesOptions {
12
12
  swarm: Hyperswarm;
13
13
  isPooled: (key: Buffer) => boolean;
14
14
  deviceId: () => Buffer | null;
15
- logger?: Logger | null;
15
+ logger?: LoggerInterface | null;
16
16
  blindPeerKeys?: ReadonlyArray<Buffer>;
17
17
  blindPeering: () => BlindPeering | null;
18
18
  wakeup?: Wakeup;
@@ -1,7 +1,5 @@
1
1
  import type { Capabilities, CapabilityHandlers } from '../../spec/capabilities.d.ts';
2
2
  import { type PeerStream } from './peer-duplex.ts';
3
- export declare const DEVICE_UNREACHABLE = "the device is not reachable";
4
- export declare const DEVICE_REVOKED = "the device no longer takes requests from this one";
5
3
  export interface DeviceControlOptions {
6
4
  readonly allowed: (deviceId: Uint8Array) => Promise<boolean>;
7
5
  readonly served: () => Partial<CapabilityHandlers> | null;
@@ -5,9 +5,8 @@ import { bindApi } from '../../spec/bind.js';
5
5
  import { engineClient } from '../../spec/calls.js';
6
6
  import HRPC from '../../spec/hrpc/index.js';
7
7
  import { PeerDuplexChannel } from './peer-duplex.js';
8
+ import { CoreError } from '../errors.js';
8
9
  const PROTOCOL = 'qvac/device-control';
9
- export const DEVICE_UNREACHABLE = 'the device is not reachable';
10
- export const DEVICE_REVOKED = 'the device no longer takes requests from this one';
11
10
  export class DeviceControl {
12
11
  _opts;
13
12
  _channel;
@@ -19,8 +18,8 @@ export class DeviceControl {
19
18
  this._channel = new PeerDuplexChannel({
20
19
  protocol: PROTOCOL,
21
20
  onaccept: (from) => this._accept(from),
22
- unreachable: DEVICE_UNREACHABLE,
23
- revoked: DEVICE_REVOKED,
21
+ unreachable: () => new CoreError('DEVICE_UNREACHABLE'),
22
+ revoked: () => new CoreError('DEVICE_REVOKED'),
24
23
  onchange: (deviceId) => this._changed(deviceId)
25
24
  });
26
25
  this._engines = new Map();
@@ -33,7 +32,7 @@ export class DeviceControl {
33
32
  engine(deviceId) {
34
33
  const id = b4a.toHex(deviceId);
35
34
  if (!this._ours.has(id)) {
36
- throw new Error(DEVICE_UNREACHABLE);
35
+ throw new CoreError('DEVICE_UNREACHABLE');
37
36
  }
38
37
  const held = this._engines.get(id);
39
38
  if (held) {
@@ -87,6 +86,6 @@ export class DeviceControl {
87
86
  }
88
87
  this._ours.delete(deviceId);
89
88
  this._engines.delete(deviceId);
90
- this._channel.closeDialled(deviceId, DEVICE_REVOKED);
89
+ this._channel.closeDialled(deviceId, () => new CoreError('DEVICE_REVOKED'));
91
90
  }
92
91
  }
@@ -1,3 +1,4 @@
1
+ import { CoreError } from '../../errors.js';
1
2
  import { linkAttachment } from '../attachment-link.js';
2
3
  import { getChunk, updateChunk } from '../chunk.js';
3
4
  import { uploadFile } from './upload.js';
@@ -28,13 +29,13 @@ export function attachmentFiles(mesh, { maxBytes = MAX_ATTACHMENT_BYTES, deviceI
28
29
  }
29
30
  const chunk = await getChunk(mesh, chunkId);
30
31
  if (!chunk) {
31
- throw new Error(`chunk not found: ${chunkId}`);
32
+ throw new CoreError('CHUNK_NOT_FOUND', [chunkId]);
32
33
  }
33
34
  if (chunk.type !== 'attachment' || !chunk.attachment) {
34
- throw new Error(`not an attachment chunk: ${chunkId}`);
35
+ throw new CoreError('NOT_AN_ATTACHMENT', [chunkId]);
35
36
  }
36
37
  if (deviceId && !b4a.equals(chunk.originDeviceId, deviceId)) {
37
- throw new Error('only the origin device may delete an attachment');
38
+ throw new CoreError('ATTACHMENT_DELETE_NOT_ORIGIN');
38
39
  }
39
40
  const attachment = chunk.attachment;
40
41
  if (attachment.deletedAt) {
@@ -45,7 +46,7 @@ export function attachmentFiles(mesh, { maxBytes = MAX_ATTACHMENT_BYTES, deviceI
45
46
  attachment: { ...attachment, deletedAt: at }
46
47
  });
47
48
  if (!updated.attachment?.deletedAt) {
48
- throw new Error('delete-attachment dropped — only the origin device may delete an attachment');
49
+ throw new CoreError('ATTACHMENT_DELETE_DROPPED');
49
50
  }
50
51
  return updated;
51
52
  }
@@ -82,7 +82,8 @@ export declare class Files extends EventEmitter {
82
82
  artifacts(id: string): Promise<QvacKnowledgeArtifact[]>;
83
83
  publish(input: Parameters<typeof addKnowledgeArtifact>[1]): Promise<QvacKnowledgeArtifact>;
84
84
  hasLiveSource(chatId: string, reference: string): Promise<boolean>;
85
- syncAttachment(chunk: QvacChunk): Promise<void>;
85
+ syncAttachments(chunks: ReadonlyArray<QvacChunk>): Promise<void>;
86
+ reclaimAttachments(chunks: ReadonlyArray<QvacChunk>): Promise<void>;
86
87
  syncKnowledge(row: QvacKnowledge): Promise<void>;
87
88
  syncArtifact(row: QvacKnowledgeArtifact): Promise<void>;
88
89
  reclaimPurged(): Promise<number>;
@@ -12,6 +12,7 @@ import { BlobStore, parseBlobReference } from './store/index.js';
12
12
  import { AbortedError } from '@qvac/harness/race-abort';
13
13
  import { RagProcessor } from './rag/index.js';
14
14
  import { PreviewProcessor, PREVIEW_VERSION, previewId } from './preview/index.js';
15
+ import { CoreError } from '../../errors.js';
15
16
  import { errorMessage } from '../../guards.js';
16
17
  import { ACTIVE_KNOWLEDGE_BY_CHAT, AGENTS, CHATS, CHUNKS_BY_CHAT, INDEXING_STATUS_BY_CHAT, KNOWLEDGE, KNOWLEDGE_ARTIFACTS_BY_KNOWLEDGE } from '../collections.js';
17
18
  export { fullTextBudgetChars, fullTextHits, retrieveRagKnowledge, searchRagChunks, referencesPendingFile } from './rag/index.js';
@@ -153,9 +154,16 @@ export class Files extends EventEmitter {
153
154
  hasLiveSource(chatId, reference) {
154
155
  return hasLiveSource(this._mesh, chatId, reference);
155
156
  }
156
- async syncAttachment(chunk) {
157
- await this._mesh.fileIndex?.refresh([chunk]);
158
- await syncAttachmentChunk(this._mesh, chunk);
157
+ async syncAttachments(chunks) {
158
+ await this._mesh.fileIndex?.refresh(chunks);
159
+ await this.reclaimAttachments(chunks);
160
+ }
161
+ async reclaimAttachments(chunks) {
162
+ for (const chunk of chunks) {
163
+ if (this._mesh.closing)
164
+ return;
165
+ await syncAttachmentChunk(this._mesh, chunk);
166
+ }
159
167
  }
160
168
  syncKnowledge(row) {
161
169
  return syncKnowledgeTombstone(this._mesh, row);
@@ -218,7 +226,8 @@ export class Files extends EventEmitter {
218
226
  this.setModel(options.embedder.model);
219
227
  this.startProcessor(new RagProcessor({
220
228
  ...options,
221
- model: options.embedder.model
229
+ model: options.embedder.model,
230
+ log: this._mesh.log
222
231
  }), { deviceId: options.deviceId });
223
232
  }
224
233
  async stopProcessing() {
@@ -348,13 +357,13 @@ export class Files extends EventEmitter {
348
357
  async beginUpload(input) {
349
358
  const { chatId, fileName, mimeType } = input;
350
359
  if (!chatId || !fileName || !mimeType) {
351
- throw new Error('upload metadata is incomplete');
360
+ throw new CoreError('UPLOAD_METADATA_INCOMPLETE');
352
361
  }
353
362
  const id = input.fileId || randomId();
354
363
  this._pruneUploads();
355
364
  const previous = this._uploads.get(id);
356
365
  if (previous && !previous.expiresAt) {
357
- throw new Error('file id already exists');
366
+ throw new CoreError('FILE_ID_EXISTS');
358
367
  }
359
368
  this._uploads.set(id, {
360
369
  status: {
@@ -377,7 +386,7 @@ export class Files extends EventEmitter {
377
386
  ((await getKnowledge(this._mesh, id)) ||
378
387
  (await getChunk(this._mesh, id)) ||
379
388
  (await listChunks(this._mesh, { chatId, type: 'attachment' })).some((chunk) => chunk.attachment?.id === id))) {
380
- throw new Error('file id already exists');
389
+ throw new CoreError('FILE_ID_EXISTS');
381
390
  }
382
391
  }
383
392
  catch (error) {
@@ -512,7 +521,7 @@ export class Files extends EventEmitter {
512
521
  else if (local?.work &&
513
522
  !published &&
514
523
  local.work.state !== 'running' &&
515
- (!terminalFailure || local.work.state !== 'pending')) {
524
+ (!terminalFailure || !yieldsToVerdict(local.work))) {
516
525
  Object.assign(processor, local.work);
517
526
  }
518
527
  else if (local?.load && published) {
@@ -636,6 +645,9 @@ export class Files extends EventEmitter {
636
645
  await this._store.close();
637
646
  }
638
647
  }
648
+ function yieldsToVerdict(work) {
649
+ return work.state === 'pending' || (work.state === 'failed' && work.retryAt === undefined);
650
+ }
639
651
  function fileState(processor, opts) {
640
652
  if (opts.readyForChat) {
641
653
  return { state: 'ready', reason: undefined };