@qvac/core 0.6.3 → 0.6.5

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 (102) hide show
  1. package/CHANGELOG.md +78 -1
  2. package/README.md +24 -16
  3. package/dist-lib/lib/api/chat-move.d.ts +3 -0
  4. package/dist-lib/lib/api/chat-move.js +7 -0
  5. package/dist-lib/lib/api/chat.d.ts +1 -1
  6. package/dist-lib/lib/api/chat.js +2 -1
  7. package/dist-lib/lib/api/chunk-delta.d.ts +7 -0
  8. package/dist-lib/lib/api/chunk-delta.js +33 -0
  9. package/dist-lib/lib/api/chunks.d.ts +1 -1
  10. package/dist-lib/lib/api/chunks.js +6 -3
  11. package/dist-lib/lib/api/index.d.ts +2 -0
  12. package/dist-lib/lib/api/index.js +2 -0
  13. package/dist-lib/lib/api/mesh.d.ts +2 -1
  14. package/dist-lib/lib/api/mesh.js +4 -0
  15. package/dist-lib/lib/assistant/harness.d.ts +8 -10
  16. package/dist-lib/lib/assistant.js +1 -1
  17. package/dist-lib/lib/constants.d.ts +1 -0
  18. package/dist-lib/lib/constants.js +1 -0
  19. package/dist-lib/lib/core/archived-mesh.js +3 -42
  20. package/dist-lib/lib/core/chat-activity.js +23 -10
  21. package/dist-lib/lib/core/chat-copy.d.ts +24 -0
  22. package/dist-lib/lib/core/chat-copy.js +115 -0
  23. package/dist-lib/lib/core/chat-move.d.ts +65 -0
  24. package/dist-lib/lib/core/chat-move.js +525 -0
  25. package/dist-lib/lib/core/chat.d.ts +6 -0
  26. package/dist-lib/lib/core/chat.js +11 -3
  27. package/dist-lib/lib/core/collections.d.ts +2 -0
  28. package/dist-lib/lib/core/collections.js +2 -0
  29. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  30. package/dist-lib/lib/core/contract/compute.js +2 -1
  31. package/dist-lib/lib/core/contract/index.js +2 -0
  32. package/dist-lib/lib/core/contract/invites.d.ts +1 -0
  33. package/dist-lib/lib/core/contract/invites.js +1 -1
  34. package/dist-lib/lib/core/contract/mesh-name.d.ts +3 -0
  35. package/dist-lib/lib/core/contract/mesh-name.js +26 -0
  36. package/dist-lib/lib/core/contract/writers.js +10 -3
  37. package/dist-lib/lib/core/files/index.d.ts +1 -1
  38. package/dist-lib/lib/core/files/index.js +1 -1
  39. package/dist-lib/lib/core/files/preview/index.js +4 -1
  40. package/dist-lib/lib/core/files/processing/source.js +4 -1
  41. package/dist-lib/lib/core/files/rag/limits.d.ts +0 -1
  42. package/dist-lib/lib/core/files/rag/limits.js +0 -1
  43. package/dist-lib/lib/core/files/store/index.d.ts +2 -1
  44. package/dist-lib/lib/core/files/store/index.js +2 -1
  45. package/dist-lib/lib/core/files/store/reference.d.ts +3 -0
  46. package/dist-lib/lib/core/files/store/reference.js +8 -0
  47. package/dist-lib/lib/core/files/store/sweep.js +3 -1
  48. package/dist-lib/lib/core/local/index.d.ts +4 -1
  49. package/dist-lib/lib/core/local/index.js +14 -1
  50. package/dist-lib/lib/core/mesh-seed-handoff.d.ts +3 -1
  51. package/dist-lib/lib/core/mesh-seed-handoff.js +1 -0
  52. package/dist-lib/lib/core/mesh.d.ts +5 -1
  53. package/dist-lib/lib/core/mesh.js +16 -1
  54. package/dist-lib/lib/core/sync/index.js +9 -1
  55. package/dist-lib/lib/core.d.ts +9 -0
  56. package/dist-lib/lib/core.js +156 -32
  57. package/dist-lib/lib/custom-model-pusher.js +9 -5
  58. package/dist-lib/lib/mesh-scoped-engine.js +5 -0
  59. package/dist-lib/schema/config.d.ts +1 -1
  60. package/dist-lib/schema/config.js +1 -1
  61. package/dist-lib/schema/engine.js +80 -1
  62. package/dist-lib/schema/local.js +17 -0
  63. package/dist-lib/schema/qvac.d.ts +2 -0
  64. package/dist-lib/schema/qvac.js +16 -0
  65. package/dist-lib/spec/bind.js +25 -0
  66. package/dist-lib/spec/calls.js +5 -0
  67. package/dist-lib/spec/capabilities.d.ts +10 -0
  68. package/dist-lib/spec/config/hyperconf/index.js +1 -1
  69. package/dist-lib/spec/hrpc/hrpc.json +65 -0
  70. package/dist-lib/spec/hrpc/index.js +66 -4
  71. package/dist-lib/spec/hrpc/messages.js +1398 -1154
  72. package/dist-lib/spec/hyperschema/index.js +1398 -1154
  73. package/dist-lib/spec/hyperschema/schema.json +204 -0
  74. package/dist-lib/spec/hyperschema/types.d.ts +47 -0
  75. package/dist-lib/spec/local/hyperdb/db.json +17 -1
  76. package/dist-lib/spec/local/hyperdb/index.js +83 -2
  77. package/dist-lib/spec/local/hyperdb/messages.js +340 -136
  78. package/dist-lib/spec/local/hyperschema/index.js +107 -2
  79. package/dist-lib/spec/local/hyperschema/schema.json +78 -1
  80. package/dist-lib/spec/local/hyperschema/types.d.ts +18 -1
  81. package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
  82. package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
  83. package/dist-lib/spec/mesh/hyperdb/messages.js +454 -228
  84. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
  85. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  86. package/dist-lib/spec/mesh/hyperdispatch/messages.js +150 -2
  87. package/dist-lib/spec/mesh/hyperschema/index.js +150 -2
  88. package/dist-lib/spec/mesh/hyperschema/schema.json +76 -1
  89. package/dist-lib/spec/mesh/hyperschema/types.d.ts +21 -1
  90. package/package.json +5 -8
  91. package/tui/app.mjs +89 -3
  92. package/tui/lib/move-command.mjs +17 -0
  93. package/dist-lib/lib/collabora/convert.d.ts +0 -9
  94. package/dist-lib/lib/collabora/convert.js +0 -65
  95. package/dist-lib/lib/collabora/entry.d.ts +0 -1
  96. package/dist-lib/lib/collabora/entry.js +0 -42
  97. package/dist-lib/lib/collabora/protocol.d.ts +0 -15
  98. package/dist-lib/lib/collabora/protocol.js +0 -31
  99. package/dist-lib/lib/collabora/worker.d.ts +0 -5
  100. package/dist-lib/lib/collabora/worker.js +0 -177
  101. package/dist-lib/lib/core/files/rag/extract/office-collabora.d.ts +0 -2
  102. package/dist-lib/lib/core/files/rag/extract/office-collabora.js +0 -8
@@ -0,0 +1,115 @@
1
+ import b4a from 'b4a';
2
+ import { PENDING_BLOB_REFERENCE, isPendingBlobReference, parseBlobReference } from './files/store/index.js';
3
+ import { inlineReference } from './files/store/inline.js';
4
+ import { CHUNKS, CHUNKS_BY_CHAT, KNOWLEDGE, KNOWLEDGE_BY_CHAT } from './collections.js';
5
+ export const CHUNK_IN_ANOTHER_CHAT = 'the target already holds this chat under another id';
6
+ export async function copyChunks(snap, chatId, { from, into }) {
7
+ const batch = into.batch();
8
+ for (const chunk of await chunksOf(snap, chatId)) {
9
+ if (await into.view.get(CHUNKS, { id: chunk.id }))
10
+ continue;
11
+ const { seq, updatedAt, attachment, ...op } = stripped(chunk);
12
+ if (!attachment) {
13
+ batch.dispatch('append-chunk', op);
14
+ continue;
15
+ }
16
+ const blobId = await copyBlob(from, into, attachment.blobId);
17
+ if (!blobId) {
18
+ continue;
19
+ }
20
+ batch.dispatch('append-chunk', { ...op, attachment: { ...attachment, blobId } });
21
+ await batch.flush();
22
+ await into.fileIndex?.record([{ ...chunk, attachment: { ...attachment, blobId } }]);
23
+ }
24
+ await batch.flush();
25
+ }
26
+ export async function copyMovedChunks(snap, chatId, copy) {
27
+ const { into, originDeviceId } = copy;
28
+ let copied = 0;
29
+ const batch = into.batch();
30
+ for (const chunk of await chunksOf(snap, chatId)) {
31
+ if (await heldInTarget(into, { collection: CHUNKS, id: chunk.id, chatId: copy.chatId })) {
32
+ continue;
33
+ }
34
+ const { seq, updatedAt, attachment, ...op } = stripped(chunk);
35
+ copied++;
36
+ if (!attachment) {
37
+ batch.dispatch('append-chunk', { ...op, chatId: copy.chatId });
38
+ continue;
39
+ }
40
+ const blobId = attachment.deletedAt
41
+ ? removedReference(attachment)
42
+ : await movedBlob(copy, attachment);
43
+ const moved = {
44
+ ...op,
45
+ chatId: copy.chatId,
46
+ originDeviceId,
47
+ attachment: { ...attachment, blobId }
48
+ };
49
+ batch.dispatch('append-chunk', moved);
50
+ await batch.flush();
51
+ if (!isPendingBlobReference(blobId)) {
52
+ await into.fileIndex?.record([{ ...chunk, ...moved }]);
53
+ }
54
+ }
55
+ await batch.flush();
56
+ return copied;
57
+ }
58
+ export async function copyMovedKnowledge(snap, chatId, copy) {
59
+ const batch = copy.into.batch();
60
+ const inChat = { gte: { chatId }, lte: { chatId } };
61
+ for (const row of await snap.find(KNOWLEDGE_BY_CHAT, inChat).toArray()) {
62
+ if (await heldInTarget(copy.into, { collection: KNOWLEDGE, id: row.id, chatId: copy.chatId })) {
63
+ continue;
64
+ }
65
+ const blobId = row.deletedAt ? removedReference(row) : await movedBlob(copy, row);
66
+ if (isPendingBlobReference(blobId)) {
67
+ continue;
68
+ }
69
+ batch.dispatch('add-knowledge', {
70
+ knowledge: { ...stripped(row), chatId: copy.chatId, blobId }
71
+ });
72
+ }
73
+ await batch.flush();
74
+ }
75
+ export async function movedBlob({ from, into, references }, { blobId }) {
76
+ const known = references.get(blobId);
77
+ if (known) {
78
+ return known;
79
+ }
80
+ const moved = (await copyBlob(from, into, blobId)) ?? PENDING_BLOB_REFERENCE;
81
+ if (!isPendingBlobReference(moved)) {
82
+ references.set(blobId, moved);
83
+ }
84
+ return moved;
85
+ }
86
+ function removedReference({ mimeType }) {
87
+ return inlineReference(b4a.alloc(0), mimeType);
88
+ }
89
+ async function heldInTarget(into, { collection, id, chatId }) {
90
+ const existing = await into.view.get(collection, { id });
91
+ if (existing && existing.chatId !== chatId) {
92
+ throw new Error(CHUNK_IN_ANOTHER_CHAT);
93
+ }
94
+ return !!existing;
95
+ }
96
+ function chunksOf(snap, chatId) {
97
+ return snap.find(CHUNKS_BY_CHAT, { gte: { chatId }, lte: { chatId } }).toArray();
98
+ }
99
+ export async function copyBlob(from, into, reference) {
100
+ let bytes;
101
+ try {
102
+ if (parseBlobReference(reference).inline) {
103
+ return reference;
104
+ }
105
+ bytes = await from.files.readBytes(reference, { wait: false });
106
+ }
107
+ catch {
108
+ return null;
109
+ }
110
+ return (await into.files.put(bytes)).reference;
111
+ }
112
+ export function stripped(row) {
113
+ const { version: _version, ...rest } = row;
114
+ return rest;
115
+ }
@@ -0,0 +1,65 @@
1
+ import type { AssistantChatId, AssistantChatMove, AssistantMoveChatToMeshRequest } from '../../spec/hyperschema/types.d.ts';
2
+ import type { Mesh } from './mesh.ts';
3
+ import type { openLocalStore } from './local/index.ts';
4
+ 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
+ export declare const SOURCE_GONE = "the chat was deleted before its files arrived";
11
+ export declare const TARGET_GONE = "the moved chat was deleted before its files arrived";
12
+ export declare const NOT_CARRIED = "a file could not be carried into the target";
13
+ type LocalStore = Awaited<ReturnType<typeof openLocalStore>>;
14
+ type OpenMesh = (meshId: string) => Promise<Mesh>;
15
+ export interface ChatMovesOptions {
16
+ readonly deviceId: Buffer;
17
+ }
18
+ export declare class ChatMoves {
19
+ private readonly _open;
20
+ private readonly _local;
21
+ private readonly _deviceId;
22
+ private readonly _listeners;
23
+ private readonly _settling;
24
+ private readonly _moving;
25
+ private readonly _abort;
26
+ private readonly _onChange;
27
+ private _patching;
28
+ private _closing;
29
+ constructor(open: OpenMesh, local: LocalStore, { deviceId }: ChatMovesOptions);
30
+ get closing(): boolean;
31
+ watch(listener: () => void): void;
32
+ unwatch(listener: () => void): void;
33
+ move(request: AssistantMoveChatToMeshRequest): Promise<AssistantChatMove>;
34
+ status({ meshId, chatId }: AssistantChatId): Promise<AssistantChatMove>;
35
+ resume(): Promise<void>;
36
+ close(): Promise<void>;
37
+ private _begin;
38
+ private _assertMovable;
39
+ private _arriving;
40
+ private _start;
41
+ private _fail;
42
+ private _settle;
43
+ private _land;
44
+ private _leave;
45
+ private _cancel;
46
+ private _copy;
47
+ private _copyOf;
48
+ private _carry;
49
+ private _each;
50
+ private _carryChunk;
51
+ private _carryKnowledge;
52
+ private _fetch;
53
+ private _owns;
54
+ private _gone;
55
+ private _giveUp;
56
+ private _dropPending;
57
+ private _restart;
58
+ private _counted;
59
+ private _patch;
60
+ private _changed;
61
+ private _changeOf;
62
+ private _watchGone;
63
+ }
64
+ export declare function movedChatId(target: Mesh, chatId: string): Promise<string>;
65
+ export {};
@@ -0,0 +1,525 @@
1
+ import b4a from 'b4a';
2
+ import crypto from 'hypercore-crypto';
3
+ import { AbortController } from 'bare-abort-controller';
4
+ import { AbortedError } from '@qvac/harness/race-abort';
5
+ import { getAgent, getDefaultAgent, mustGetActiveAgent } from './agent.js';
6
+ import { cancelActiveRuns, deleteChat, getChat, mustGetActiveChat } from './chat.js';
7
+ import { CHUNK_IN_ANOTHER_CHAT, copyMovedChunks, copyMovedKnowledge, movedBlob, stripped } from './chat-copy.js';
8
+ import { appendChunk, getChunk, listChunks, updateOp } from './chunk.js';
9
+ import { isPendingBlobReference } from './files/index.js';
10
+ import { errorMessage } from '../guards.js';
11
+ import { CHATS, DEVICES, KNOWLEDGE, KNOWLEDGE_BY_CHAT } from './collections.js';
12
+ 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
+ export const SOURCE_GONE = 'the chat was deleted before its files arrived';
19
+ export const TARGET_GONE = 'the moved chat was deleted before its files arrived';
20
+ export const NOT_CARRIED = 'a file could not be carried into the target';
21
+ export class ChatMoves {
22
+ _open;
23
+ _local;
24
+ _deviceId;
25
+ _listeners = new Set();
26
+ _settling = new Map();
27
+ _moving = new Map();
28
+ _abort = new AbortController();
29
+ _onChange = () => this._changed();
30
+ _patching = Promise.resolve();
31
+ _closing = false;
32
+ constructor(open, local, { deviceId }) {
33
+ this._open = open;
34
+ this._local = local;
35
+ this._deviceId = deviceId;
36
+ }
37
+ get closing() {
38
+ return this._closing;
39
+ }
40
+ watch(listener) {
41
+ this._listeners.add(listener);
42
+ }
43
+ unwatch(listener) {
44
+ this._listeners.delete(listener);
45
+ }
46
+ async move(request) {
47
+ const key = request.meshId + request.chatId;
48
+ const moving = this._moving.get(key) ?? this._begin(request);
49
+ this._moving.set(key, moving);
50
+ try {
51
+ await moving;
52
+ }
53
+ finally {
54
+ if (this._moving.get(key) === moving) {
55
+ this._moving.delete(key);
56
+ }
57
+ }
58
+ return this.status(request);
59
+ }
60
+ async status({ meshId, chatId }) {
61
+ const row = await this._local.readChatMove(b4a.from(meshId, 'hex'), chatId);
62
+ if (!row) {
63
+ throw new Error(NO_MOVE);
64
+ }
65
+ const settled = row.completedAt || row.failure;
66
+ const pair = settled
67
+ ? null
68
+ : {
69
+ source: await this._open(hex(row.meshKey)),
70
+ target: await this._open(hex(row.targetMeshKey))
71
+ };
72
+ const waiting = pair ? filesOf(await waitingFiles(pair, row)) : [];
73
+ return {
74
+ meshId,
75
+ chatId,
76
+ targetMeshId: hex(row.targetMeshKey),
77
+ targetChatId: row.targetChatId,
78
+ state: stateOf(row, waiting.length),
79
+ files: row.carried + waiting.length,
80
+ carried: row.carried,
81
+ waiting: waiting.map((file) => file.fileName),
82
+ restarted: !!row.restartRunId,
83
+ failure: row.failure ?? undefined
84
+ };
85
+ }
86
+ async resume() {
87
+ for (const row of await this._local.listChatMoves()) {
88
+ if (!row.completedAt && !row.failure) {
89
+ this._start(row).copied.catch(noop);
90
+ }
91
+ }
92
+ }
93
+ async close() {
94
+ this._closing = true;
95
+ this._abort.abort(new AbortedError());
96
+ this._changed();
97
+ await Promise.allSettled([...this._settling.values()].map((settling) => settling.done));
98
+ }
99
+ async _begin({ meshId, chatId, targetMeshId, agentId }) {
100
+ if (meshId === targetMeshId)
101
+ throw new Error(SAME_MESH);
102
+ const pair = { source: await this._open(meshId), target: await this._open(targetMeshId) };
103
+ await this._assertMovable(pair, chatId);
104
+ const held = await this._local.readChatMove(pair.source.key, chatId);
105
+ if (held && !held.completedAt && !held.failure) {
106
+ return this._start(held).copied;
107
+ }
108
+ const chat = await mustGetActiveChat(pair.source, chatId);
109
+ const targetChatId = await movedChatId(pair.target, chatId);
110
+ const [first] = await listChunks(pair.source, { chatId, limit: 1 });
111
+ const clash = first ? await getChunk(pair.target, first.id) : null;
112
+ if (clash && clash.chatId !== targetChatId) {
113
+ throw new Error(CHUNK_IN_ANOTHER_CHAT);
114
+ }
115
+ const row = {
116
+ meshKey: pair.source.key,
117
+ chatId,
118
+ targetMeshKey: pair.target.key,
119
+ targetChatId,
120
+ agentId: await targetAgentId(pair.target, chat, agentId),
121
+ carried: 0,
122
+ startedAt: Date.now()
123
+ };
124
+ await this._local.recordChatMove(row);
125
+ await this._cancel(pair, row);
126
+ return this._start(row).copied;
127
+ }
128
+ async _assertMovable({ source, target }, chatId) {
129
+ if (source.compute || target.compute)
130
+ throw new Error(COMPUTE_MESH);
131
+ const device = await target.view.get(DEVICES, { id: this._deviceId });
132
+ const writer = device?.writerKey && b4a.equals(device.writerKey, target.localWriterKey);
133
+ if (!target.writable || !writer || device?.revoked) {
134
+ throw new Error(NOT_A_WRITER);
135
+ }
136
+ if ((await pendingFiles(source, chatId)).length > 0 || (await this._arriving(source, chatId))) {
137
+ throw new Error(STILL_ARRIVING);
138
+ }
139
+ }
140
+ async _arriving(mesh, chatId) {
141
+ const moves = await this._local.listChatMoves();
142
+ return moves.some((move) => !move.completedAt &&
143
+ !move.failure &&
144
+ move.targetChatId === chatId &&
145
+ b4a.equals(move.targetMeshKey, mesh.key));
146
+ }
147
+ _start(row) {
148
+ const key = hex(row.meshKey) + row.chatId;
149
+ const held = this._settling.get(key);
150
+ if (held) {
151
+ return held;
152
+ }
153
+ const copied = Promise.withResolvers();
154
+ const done = this._settle(row, copied)
155
+ .catch((err) => this._fail(row, err))
156
+ .finally(() => this._settling.delete(key));
157
+ const settling = { copied: copied.promise, done };
158
+ this._settling.set(key, settling);
159
+ return settling;
160
+ }
161
+ async _fail(row, err) {
162
+ if (this._closing)
163
+ return;
164
+ logger.warn('chat-move', 'move failed', { chatId: row.chatId, err });
165
+ await this._patch(row, { failure: errorMessage(err) });
166
+ }
167
+ async _settle(row, copied) {
168
+ const pair = {
169
+ source: await this._open(hex(row.meshKey)),
170
+ target: await this._open(hex(row.targetMeshKey))
171
+ };
172
+ try {
173
+ await this._copy(pair, row);
174
+ copied.resolve();
175
+ }
176
+ catch (err) {
177
+ copied.reject(err);
178
+ throw err;
179
+ }
180
+ pair.target.watch(this._onChange);
181
+ try {
182
+ await this._land(pair, row);
183
+ }
184
+ finally {
185
+ pair.target.unwatch(this._onChange);
186
+ }
187
+ }
188
+ async _land(pair, row) {
189
+ while (!this._closing) {
190
+ const gone = await this._gone(pair, row);
191
+ if (gone) {
192
+ return this._giveUp(pair, row, gone);
193
+ }
194
+ const copied = await this._copy(pair, row);
195
+ const carry = await this._carry(pair, row);
196
+ if (carry.cut || this._closing) {
197
+ continue;
198
+ }
199
+ if (copied === 0 && carry.pending === 0) {
200
+ return this._leave(pair, row);
201
+ }
202
+ if (carry.pending > 0) {
203
+ await this._changeOf(pair);
204
+ }
205
+ }
206
+ }
207
+ async _leave(pair, row) {
208
+ const gone = await this._gone(pair, row);
209
+ if (gone) {
210
+ return this._giveUp(pair, row, gone);
211
+ }
212
+ await this._restart(pair, row);
213
+ await deleteChat(pair.source, this._deviceId, row.chatId);
214
+ await this._patch(row, { completedAt: Date.now() });
215
+ }
216
+ async _cancel({ source }, row) {
217
+ await cancelActiveRuns(source, row.chatId, { deviceId: this._deviceId, reason: CHAT_MOVED });
218
+ }
219
+ async _copy(pair, row) {
220
+ await this._cancel(pair, row);
221
+ const snap = pair.source.view.snapshot();
222
+ try {
223
+ const chat = await snap.get(CHATS, { id: row.chatId });
224
+ if (!chat || chat.deletedAt) {
225
+ return 0;
226
+ }
227
+ if (!(await getChat(pair.target, row.targetChatId))) {
228
+ await pair.target.dispatch('put-chat', { chat: movedChat(chat, row) });
229
+ }
230
+ const copy = this._copyOf(pair, row);
231
+ const copied = await copyMovedChunks(snap, row.chatId, copy);
232
+ await copyMovedKnowledge(snap, row.chatId, copy);
233
+ this._changed();
234
+ return copied;
235
+ }
236
+ finally {
237
+ await snap.close();
238
+ }
239
+ }
240
+ _copyOf({ source, target }, row) {
241
+ return {
242
+ from: source,
243
+ into: target,
244
+ chatId: row.targetChatId,
245
+ originDeviceId: this._deviceId,
246
+ references: new Map()
247
+ };
248
+ }
249
+ async _carry(pair, row) {
250
+ const pending = await waitingFiles(pair, row);
251
+ const cut = this._watchGone(pair, { row, carrying: new Set(pending.map((file) => file.id)) });
252
+ const carry = { pair, row, signal: cut.signal };
253
+ try {
254
+ const own = pending.filter((file) => file.chunk && this._owns(file.chunk));
255
+ await this._each(own, (file) => this._carryChunk(carry, file));
256
+ const rows = (await waitingFiles(pair, row)).filter((f) => !f.chunk);
257
+ await this._each(rows, (file) => this._carryKnowledge(carry, file));
258
+ }
259
+ finally {
260
+ cut.stop();
261
+ }
262
+ const left = filesOf(await waitingFiles(pair, row)).length;
263
+ return { pending: left, cut: cut.signal.aborted };
264
+ }
265
+ async _each(files, fn) {
266
+ const results = await Promise.allSettled(files.map(fn));
267
+ for (const result of results) {
268
+ if (result.status === 'rejected' && !(result.reason instanceof AbortedError)) {
269
+ throw result.reason;
270
+ }
271
+ }
272
+ }
273
+ async _carryChunk({ pair, row, signal }, file) {
274
+ const origin = (await getChunk(pair.source, file.id))?.attachment;
275
+ if (!origin || !file.chunk?.attachment) {
276
+ throw new Error(NOT_CARRIED);
277
+ }
278
+ if (origin.deletedAt) {
279
+ const attachment = { ...file.chunk.attachment, deletedAt: origin.deletedAt };
280
+ await pair.target.dispatch('update-chunk', updateOp(file.chunk, { id: file.id, attachment }));
281
+ return;
282
+ }
283
+ const blobId = await this._fetch(pair, { row, origin, signal });
284
+ const attachment = { ...file.chunk.attachment, blobId };
285
+ await this._counted(row, { quiet: true });
286
+ await pair.target.dispatch('update-chunk', updateOp(file.chunk, { id: file.id, attachment }));
287
+ const landed = (await getChunk(pair.target, file.id))?.attachment;
288
+ if (landed && !landed.deletedAt && isPendingBlobReference(landed.blobId)) {
289
+ throw new Error(NOT_CARRIED);
290
+ }
291
+ await pair.target.fileIndex?.record([{ ...file.chunk, attachment }]);
292
+ }
293
+ async _carryKnowledge({ pair, row, signal }, file) {
294
+ const origin = await pair.source.view.get(KNOWLEDGE, { id: file.id });
295
+ if (!origin) {
296
+ throw new Error(NOT_CARRIED);
297
+ }
298
+ const attached = await liveAttachment(pair.target, {
299
+ chatId: row.targetChatId,
300
+ fileId: file.id
301
+ });
302
+ if (attached && isPendingBlobReference(attached.blobId)) {
303
+ return;
304
+ }
305
+ let blobId = attached?.blobId;
306
+ if (!blobId) {
307
+ blobId = await this._fetch(pair, { row, origin, signal });
308
+ await this._counted(row);
309
+ }
310
+ const knowledge = { ...stripped(origin), chatId: row.targetChatId, blobId };
311
+ await pair.target.dispatch('add-knowledge', { knowledge });
312
+ }
313
+ async _fetch(pair, { row, origin, signal }) {
314
+ await pair.source.files.download(origin.blobId, { signal });
315
+ const blobId = await movedBlob(this._copyOf(pair, row), origin);
316
+ if (isPendingBlobReference(blobId)) {
317
+ throw new Error(NOT_CARRIED);
318
+ }
319
+ return blobId;
320
+ }
321
+ _owns(chunk) {
322
+ return b4a.equals(chunk.originDeviceId, this._deviceId);
323
+ }
324
+ async _gone({ source, target }, row) {
325
+ const chat = await getChat(source, row.chatId);
326
+ if (!chat || chat.deletedAt) {
327
+ return SOURCE_GONE;
328
+ }
329
+ const moved = await getChat(target, row.targetChatId);
330
+ return moved?.deletedAt ? TARGET_GONE : null;
331
+ }
332
+ async _giveUp(pair, row, reason) {
333
+ const pending = await waitingFiles(pair, row);
334
+ if (reason === SOURCE_GONE && pending.length === 0) {
335
+ await this._patch(row, { completedAt: Date.now() });
336
+ return;
337
+ }
338
+ if (reason === SOURCE_GONE) {
339
+ await this._dropPending(pair.target, pending);
340
+ }
341
+ await this._patch(row, { failure: reason });
342
+ }
343
+ async _dropPending(target, pending) {
344
+ const at = Date.now();
345
+ for (const file of pending) {
346
+ if (file.chunk?.attachment && this._owns(file.chunk)) {
347
+ const attachment = { ...file.chunk.attachment, deletedAt: at };
348
+ await target.dispatch('update-chunk', updateOp(file.chunk, { id: file.id, attachment }));
349
+ }
350
+ }
351
+ }
352
+ async _restart({ target }, row) {
353
+ const agent = await getAgent(target, row.agentId);
354
+ if (!agent || agent.deletedAt) {
355
+ return;
356
+ }
357
+ const chatId = row.targetChatId;
358
+ const interrupted = (await listChunks(target, { chatId, type: 'run-request' })).at(-1);
359
+ if (interrupted?.parentId && interrupted.id === derivedId(interrupted.parentId, 'restart')) {
360
+ await this._patch(row, { restartRunId: interrupted.id });
361
+ return;
362
+ }
363
+ if (!interrupted?.runId || !(await stoppedByMove(target, interrupted))) {
364
+ return;
365
+ }
366
+ const restartId = derivedId(interrupted.id, 'restart');
367
+ await appendChunk(target, {
368
+ id: restartId,
369
+ chatId,
370
+ runId: restartId,
371
+ type: 'run-request',
372
+ runRequest: { agentId: row.agentId },
373
+ capability: interrupted.capability,
374
+ capabilityArgs: interrupted.capabilityArgs,
375
+ enhance: interrupted.enhance,
376
+ parentId: interrupted.id,
377
+ originDeviceId: this._deviceId
378
+ });
379
+ await this._patch(row, { restartRunId: restartId });
380
+ }
381
+ _counted(row, opts = {}) {
382
+ return this._patch(row, (stored) => ({ carried: stored.carried + 1 }), opts);
383
+ }
384
+ _patch(row, patch, { quiet = false } = {}) {
385
+ const write = this._patching.then(async () => {
386
+ const stored = (await this._local.readChatMove(row.meshKey, row.chatId)) ?? row;
387
+ const next = { ...stored, ...(typeof patch === 'function' ? patch(stored) : patch) };
388
+ await this._local.recordChatMove(next);
389
+ if (!quiet) {
390
+ this._changed();
391
+ }
392
+ return next;
393
+ });
394
+ this._patching = write.catch(noop);
395
+ return write;
396
+ }
397
+ _changed() {
398
+ for (const listener of this._listeners) {
399
+ listener();
400
+ }
401
+ }
402
+ _changeOf({ source, target }) {
403
+ return new Promise((resolve) => {
404
+ const done = () => {
405
+ source.unwatch(done);
406
+ target.unwatch(done);
407
+ this._abort.signal.removeEventListener('abort', done);
408
+ resolve();
409
+ };
410
+ source.watch(done);
411
+ target.watch(done);
412
+ this._abort.signal.addEventListener('abort', done, { once: true });
413
+ });
414
+ }
415
+ _watchGone(pair, { row, carrying }) {
416
+ const controller = new AbortController();
417
+ const abort = () => controller.abort(new AbortedError());
418
+ const cut = async () => {
419
+ if (await this._gone(pair, row))
420
+ return true;
421
+ const pending = await waitingFiles(pair, row);
422
+ const ids = new Set(pending.map((file) => file.id));
423
+ return [...carrying].some((id) => !ids.has(id));
424
+ };
425
+ const check = () => {
426
+ void cut().then((done) => done && abort(), noop);
427
+ };
428
+ pair.source.watch(check);
429
+ pair.target.watch(check);
430
+ this._abort.signal.addEventListener('abort', abort, { once: true });
431
+ if (this._abort.signal.aborted) {
432
+ abort();
433
+ }
434
+ return {
435
+ signal: controller.signal,
436
+ stop: () => {
437
+ pair.source.unwatch(check);
438
+ pair.target.unwatch(check);
439
+ this._abort.signal.removeEventListener('abort', abort);
440
+ }
441
+ };
442
+ }
443
+ }
444
+ async function stoppedByMove(mesh, request) {
445
+ const statuses = await listChunks(mesh, { chatId: request.chatId, type: 'run-status' });
446
+ return statuses.some((chunk) => chunk.runId === request.runId &&
447
+ chunk.runStatus?.state === 'canceled' &&
448
+ chunk.runStatus.reason === CHAT_MOVED);
449
+ }
450
+ function filesOf(pending) {
451
+ return [...new Map(pending.map((file) => [file.fileId, file])).values()];
452
+ }
453
+ async function liveAttachment(mesh, { chatId, fileId }) {
454
+ const chunks = await listChunks(mesh, { chatId, type: 'attachment' });
455
+ const attachment = chunks.find((chunk) => chunk.attachment?.id === fileId)?.attachment;
456
+ return attachment && !attachment.deletedAt ? attachment : null;
457
+ }
458
+ async function pendingFiles(mesh, chatId) {
459
+ const files = [];
460
+ for (const chunk of await listChunks(mesh, { chatId, type: 'attachment' })) {
461
+ const attachment = chunk.attachment;
462
+ if (attachment && !attachment.deletedAt && isPendingBlobReference(attachment.blobId)) {
463
+ files.push({ id: chunk.id, fileId: attachment.id, fileName: attachment.fileName, chunk });
464
+ }
465
+ }
466
+ return files;
467
+ }
468
+ async function waitingFiles(pair, row) {
469
+ const files = await pendingFiles(pair.target, row.targetChatId);
470
+ const inChat = { gte: { chatId: row.chatId }, lte: { chatId: row.chatId } };
471
+ const rows = await pair.source.view.find(KNOWLEDGE_BY_CHAT, inChat).toArray();
472
+ for (const knowledge of rows) {
473
+ if (knowledge.deletedAt)
474
+ continue;
475
+ if (await pair.target.view.get(KNOWLEDGE, { id: knowledge.id })) {
476
+ continue;
477
+ }
478
+ files.push({
479
+ id: knowledge.id,
480
+ fileId: knowledge.id,
481
+ fileName: knowledge.fileName,
482
+ chunk: null
483
+ });
484
+ }
485
+ return files;
486
+ }
487
+ async function targetAgentId(target, chat, agentId) {
488
+ if (agentId) {
489
+ return (await mustGetActiveAgent(target, agentId)).id;
490
+ }
491
+ return (await getDefaultAgent(target))?.id ?? chat.agentId;
492
+ }
493
+ function movedChat(chat, row) {
494
+ return {
495
+ ...stripped(chat),
496
+ id: row.targetChatId,
497
+ agentId: row.agentId,
498
+ groupId: undefined,
499
+ rank: undefined,
500
+ rankedAt: undefined,
501
+ updatedAt: Date.now()
502
+ };
503
+ }
504
+ function stateOf(row, waiting) {
505
+ if (row.failure)
506
+ return 'failed';
507
+ if (row.completedAt)
508
+ return 'complete';
509
+ return waiting > 0 ? 'carrying' : 'leaving';
510
+ }
511
+ export async function movedChatId(target, chatId) {
512
+ for (let attempt = 0;; attempt++) {
513
+ const id = derivedId(chatId, hex(target.key), String(attempt));
514
+ if (!(await getChat(target, id))?.deletedAt) {
515
+ return id;
516
+ }
517
+ }
518
+ }
519
+ function derivedId(...parts) {
520
+ return b4a.toHex(crypto.hash(parts.map((part) => b4a.from(part))).subarray(0, 16));
521
+ }
522
+ function hex(key) {
523
+ return b4a.toHex(key);
524
+ }
525
+ function noop() { }