@qvac/core 0.6.4 → 0.6.6
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.
- package/CHANGELOG.md +78 -1
- package/README.md +20 -4
- package/dist-lib/lib/api/chat-move.d.ts +3 -0
- package/dist-lib/lib/api/chat-move.js +7 -0
- package/dist-lib/lib/api/chat.d.ts +1 -1
- package/dist-lib/lib/api/chat.js +2 -1
- package/dist-lib/lib/api/chunk-delta.d.ts +7 -0
- package/dist-lib/lib/api/chunk-delta.js +33 -0
- package/dist-lib/lib/api/chunks.d.ts +1 -1
- package/dist-lib/lib/api/chunks.js +6 -3
- package/dist-lib/lib/api/index.d.ts +2 -0
- package/dist-lib/lib/api/index.js +2 -0
- package/dist-lib/lib/api/mesh.d.ts +2 -1
- package/dist-lib/lib/api/mesh.js +4 -0
- package/dist-lib/lib/assistant/harness.d.ts +8 -10
- package/dist-lib/lib/assistant.js +1 -1
- package/dist-lib/lib/constants.d.ts +1 -0
- package/dist-lib/lib/constants.js +1 -0
- package/dist-lib/lib/core/archived-mesh.js +3 -42
- package/dist-lib/lib/core/chat-activity.js +23 -10
- package/dist-lib/lib/core/chat-copy.d.ts +24 -0
- package/dist-lib/lib/core/chat-copy.js +115 -0
- package/dist-lib/lib/core/chat-move.d.ts +65 -0
- package/dist-lib/lib/core/chat-move.js +525 -0
- package/dist-lib/lib/core/chat.d.ts +6 -0
- package/dist-lib/lib/core/chat.js +11 -3
- package/dist-lib/lib/core/collections.d.ts +2 -0
- package/dist-lib/lib/core/collections.js +2 -0
- package/dist-lib/lib/core/contract/compute.d.ts +1 -1
- package/dist-lib/lib/core/contract/compute.js +2 -1
- package/dist-lib/lib/core/contract/index.js +2 -0
- package/dist-lib/lib/core/contract/invites.d.ts +1 -0
- package/dist-lib/lib/core/contract/invites.js +1 -1
- package/dist-lib/lib/core/contract/mesh-name.d.ts +3 -0
- package/dist-lib/lib/core/contract/mesh-name.js +26 -0
- package/dist-lib/lib/core/contract/writers.js +10 -3
- package/dist-lib/lib/core/files/index.d.ts +1 -1
- package/dist-lib/lib/core/files/index.js +1 -1
- package/dist-lib/lib/core/files/preview/index.js +4 -1
- package/dist-lib/lib/core/files/processing/source.js +4 -1
- package/dist-lib/lib/core/files/store/index.d.ts +2 -1
- package/dist-lib/lib/core/files/store/index.js +2 -1
- package/dist-lib/lib/core/files/store/reference.d.ts +3 -0
- package/dist-lib/lib/core/files/store/reference.js +8 -0
- package/dist-lib/lib/core/files/store/sweep.js +3 -1
- package/dist-lib/lib/core/local/index.d.ts +4 -1
- package/dist-lib/lib/core/local/index.js +14 -1
- package/dist-lib/lib/core/mesh-seed-handoff.d.ts +3 -1
- package/dist-lib/lib/core/mesh-seed-handoff.js +1 -0
- package/dist-lib/lib/core/mesh.d.ts +5 -1
- package/dist-lib/lib/core/mesh.js +16 -1
- package/dist-lib/lib/core/sync/index.js +9 -1
- package/dist-lib/lib/core.d.ts +9 -0
- package/dist-lib/lib/core.js +156 -30
- package/dist-lib/lib/custom-model-pusher.js +9 -5
- package/dist-lib/lib/mesh-scoped-engine.js +5 -0
- package/dist-lib/schema/engine.js +80 -1
- package/dist-lib/schema/local.js +17 -0
- package/dist-lib/schema/qvac.d.ts +2 -0
- package/dist-lib/schema/qvac.js +16 -0
- package/dist-lib/spec/bind.js +25 -0
- package/dist-lib/spec/calls.js +5 -0
- package/dist-lib/spec/capabilities.d.ts +10 -0
- package/dist-lib/spec/hrpc/hrpc.json +65 -0
- package/dist-lib/spec/hrpc/index.js +66 -4
- package/dist-lib/spec/hrpc/messages.js +1398 -1154
- package/dist-lib/spec/hyperschema/index.js +1398 -1154
- package/dist-lib/spec/hyperschema/schema.json +204 -0
- package/dist-lib/spec/hyperschema/types.d.ts +47 -0
- package/dist-lib/spec/local/hyperdb/db.json +17 -1
- package/dist-lib/spec/local/hyperdb/index.js +83 -2
- package/dist-lib/spec/local/hyperdb/messages.js +340 -136
- package/dist-lib/spec/local/hyperschema/index.js +107 -2
- package/dist-lib/spec/local/hyperschema/schema.json +78 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +18 -1
- package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
- package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
- package/dist-lib/spec/mesh/hyperdb/messages.js +454 -228
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
- package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +150 -2
- package/dist-lib/spec/mesh/hyperschema/index.js +150 -2
- package/dist-lib/spec/mesh/hyperschema/schema.json +76 -1
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +21 -1
- package/package.json +5 -3
- package/tui/app.mjs +89 -3
- package/tui/lib/move-command.mjs +17 -0
|
@@ -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() { }
|
|
@@ -27,6 +27,7 @@ export interface CreateChatInput {
|
|
|
27
27
|
export declare function buildChat(input: CreateChatInput): QvacChat;
|
|
28
28
|
export declare function createChat(store: Mesh, input: CreateChatInput): Promise<ChatListing>;
|
|
29
29
|
export declare function renameChat(store: Mesh, id: string, title: string): Promise<ChatListing>;
|
|
30
|
+
export declare function setChatAgent(store: Mesh, id: string, agentId: string): Promise<ChatListing>;
|
|
30
31
|
export interface SetChatRagEnabledOptions {
|
|
31
32
|
readonly ragEnabled: boolean;
|
|
32
33
|
}
|
|
@@ -39,6 +40,11 @@ export interface MoveChatOptions {
|
|
|
39
40
|
export declare function moveChat(store: Mesh, id: string, { groupId, afterId }: MoveChatOptions): Promise<ChatListing>;
|
|
40
41
|
export declare function setChatWebSearchEnabled(store: Mesh, id: string, webSearchEnabled: boolean): Promise<ChatListing>;
|
|
41
42
|
export declare function markChatRead(store: Mesh, id: string): Promise<ChatListing>;
|
|
43
|
+
export interface CancelActiveRunsOptions {
|
|
44
|
+
readonly deviceId: Buffer;
|
|
45
|
+
readonly reason?: string;
|
|
46
|
+
}
|
|
47
|
+
export declare function cancelActiveRuns(store: Mesh, chatId: string, { deviceId, reason }: CancelActiveRunsOptions): Promise<string[]>;
|
|
42
48
|
export declare function removeChatKnowledge(store: Mesh, chatId: string, at: number): Promise<void>;
|
|
43
49
|
export interface RemoveChatAttachmentsOptions {
|
|
44
50
|
readonly deviceId: Buffer;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { mustGetActiveAgent } from './agent.js';
|
|
1
2
|
import { listChatGroupsIn, mustGetActiveChatGroup } from './chat-group.js';
|
|
2
3
|
import { appendChunk, listChunks, updateOp } from './chunk.js';
|
|
3
4
|
import { randomId } from './id.js';
|
|
@@ -127,6 +128,10 @@ async function patchChatRow(store, current, patch) {
|
|
|
127
128
|
export function renameChat(store, id, title) {
|
|
128
129
|
return patchChat(store, id, { title });
|
|
129
130
|
}
|
|
131
|
+
export async function setChatAgent(store, id, agentId) {
|
|
132
|
+
await mustGetActiveAgent(store, agentId);
|
|
133
|
+
return patchChat(store, id, { agentId });
|
|
134
|
+
}
|
|
130
135
|
export function setChatRagEnabled(store, id, opts) {
|
|
131
136
|
return patchChat(store, id, { ragEnabled: opts.ragEnabled });
|
|
132
137
|
}
|
|
@@ -220,24 +225,27 @@ export async function markChatRead(store, id) {
|
|
|
220
225
|
await store.dispatch('put-chat', { chat: read });
|
|
221
226
|
return enrichChat(store, read);
|
|
222
227
|
}
|
|
223
|
-
async function cancelActiveRuns(store, deviceId,
|
|
228
|
+
export async function cancelActiveRuns(store, chatId, { deviceId, reason }) {
|
|
224
229
|
const statuses = await listChunks(store, { chatId, type: 'run-status' });
|
|
225
230
|
const terminal = new Set(statuses.filter((c) => TERMINAL_RUN_STATES.has(c.runStatus?.state ?? '')).map((c) => c.runId));
|
|
226
231
|
const requests = await listChunks(store, { chatId, type: 'run-request' });
|
|
227
232
|
const runIds = new Set(requests.map((c) => c.runId).filter((id) => Boolean(id)));
|
|
233
|
+
const canceled = [];
|
|
228
234
|
const batch = store.batch();
|
|
229
235
|
for (const runId of runIds) {
|
|
230
236
|
if (terminal.has(runId))
|
|
231
237
|
continue;
|
|
238
|
+
canceled.push(runId);
|
|
232
239
|
await appendChunk(store, {
|
|
233
240
|
chatId,
|
|
234
241
|
runId,
|
|
235
242
|
type: 'run-status',
|
|
236
|
-
runStatus: { state: 'canceled' },
|
|
243
|
+
runStatus: { state: 'canceled', reason },
|
|
237
244
|
originDeviceId: deviceId
|
|
238
245
|
}, batch);
|
|
239
246
|
}
|
|
240
247
|
await batch.flush();
|
|
248
|
+
return canceled;
|
|
241
249
|
}
|
|
242
250
|
export async function removeChatKnowledge(store, chatId, at) {
|
|
243
251
|
assertRemovableAt(at);
|
|
@@ -267,7 +275,7 @@ export async function deleteChat(store, deviceId, id) {
|
|
|
267
275
|
const references = await chatReferences(store, id);
|
|
268
276
|
const now = Date.now();
|
|
269
277
|
if (deviceId) {
|
|
270
|
-
await cancelActiveRuns(store,
|
|
278
|
+
await cancelActiveRuns(store, id, { deviceId });
|
|
271
279
|
}
|
|
272
280
|
const deleted = { ...current, deletedAt: now, updatedAt: now };
|
|
273
281
|
const listing = await enrichChat(store, deleted);
|
|
@@ -57,6 +57,7 @@ export declare const LOCAL_FILES = "@local/files";
|
|
|
57
57
|
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
|
+
export declare const LOCAL_CHAT_MOVES = "@local/chat-moves";
|
|
60
61
|
export declare const LOCAL_MESH_SESSION = "@local/mesh-session";
|
|
61
62
|
export declare const LOCAL_MESH_HISTORY = "@local/mesh-history";
|
|
62
63
|
export declare const LOCAL_MEMBERSHIP = "@local/membership";
|
|
@@ -68,3 +69,4 @@ export declare const MESH_INVITE_SPENDS = "@qvac/mesh-invite-spends";
|
|
|
68
69
|
export declare const MESH_INVITE_GRANTS = "@qvac/mesh-invite-grants";
|
|
69
70
|
export declare const MESH_FOUNDERS = "@qvac/mesh-founders";
|
|
70
71
|
export declare const MESH_KINDS = "@qvac/mesh-kinds";
|
|
72
|
+
export declare const MESH_NAMES = "@qvac/mesh-names";
|
|
@@ -57,6 +57,7 @@ export const LOCAL_FILES = '@local/files';
|
|
|
57
57
|
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
|
+
export const LOCAL_CHAT_MOVES = '@local/chat-moves';
|
|
60
61
|
export const LOCAL_MESH_SESSION = '@local/mesh-session';
|
|
61
62
|
export const LOCAL_MESH_HISTORY = '@local/mesh-history';
|
|
62
63
|
export const LOCAL_MEMBERSHIP = '@local/membership';
|
|
@@ -68,3 +69,4 @@ export const MESH_INVITE_SPENDS = '@qvac/mesh-invite-spends';
|
|
|
68
69
|
export const MESH_INVITE_GRANTS = '@qvac/mesh-invite-grants';
|
|
69
70
|
export const MESH_FOUNDERS = '@qvac/mesh-founders';
|
|
70
71
|
export const MESH_KINDS = '@qvac/mesh-kinds';
|
|
72
|
+
export const MESH_NAMES = '@qvac/mesh-names';
|