@qvac/core 1.0.0-rc.8 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +243 -3
- package/README.md +1 -0
- package/dist-lib/lib/api/attachments.d.ts +1 -1
- package/dist-lib/lib/api/attachments.js +11 -0
- package/dist-lib/lib/api/chunks.d.ts +4 -2
- package/dist-lib/lib/api/chunks.js +6 -3
- package/dist-lib/lib/api/devices.js +6 -18
- package/dist-lib/lib/api/execution.d.ts +6 -2
- package/dist-lib/lib/api/execution.js +29 -3
- package/dist-lib/lib/api/host-run-log.d.ts +7 -1
- package/dist-lib/lib/api/host-run-log.js +3 -1
- package/dist-lib/lib/api/index.d.ts +3 -1
- package/dist-lib/lib/api/index.js +17 -6
- package/dist-lib/lib/api/members.js +13 -29
- package/dist-lib/lib/api/mesh.d.ts +2 -1
- package/dist-lib/lib/api/projects.d.ts +1 -1
- package/dist-lib/lib/api/projects.js +9 -5
- package/dist-lib/lib/api/rag-label.d.ts +4 -0
- package/dist-lib/lib/api/rag-label.js +21 -0
- package/dist-lib/lib/api/rag-lexical.d.ts +22 -0
- package/dist-lib/lib/api/rag-lexical.js +1058 -0
- package/dist-lib/lib/api/rag-manifest.d.ts +13 -0
- package/dist-lib/lib/api/rag-manifest.js +46 -0
- package/dist-lib/lib/api/rag-outline.d.ts +36 -0
- package/dist-lib/lib/api/rag-outline.js +426 -0
- package/dist-lib/lib/api/rag-plugin.d.ts +20 -0
- package/dist-lib/lib/api/rag-plugin.js +70 -0
- package/dist-lib/lib/api/rag-synthesis.d.ts +7 -0
- package/dist-lib/lib/api/rag-synthesis.js +102 -0
- package/dist-lib/lib/api/rag-targets.d.ts +23 -0
- package/dist-lib/lib/api/rag-targets.js +215 -0
- package/dist-lib/lib/api/rag-window.d.ts +5 -0
- package/dist-lib/lib/api/rag-window.js +46 -0
- package/dist-lib/lib/api/rag.d.ts +3 -2
- package/dist-lib/lib/api/rag.js +421 -181
- package/dist-lib/lib/api/served.js +8 -14
- package/dist-lib/lib/api/workspace.d.ts +8 -1
- package/dist-lib/lib/api/workspace.js +27 -13
- package/dist-lib/lib/assistant/harness.d.ts +9 -1
- package/dist-lib/lib/assistant/history.d.ts +11 -2
- package/dist-lib/lib/assistant/history.js +74 -4
- package/dist-lib/lib/assistant/media-round.d.ts +61 -0
- package/dist-lib/lib/assistant/media-round.js +111 -0
- package/dist-lib/lib/assistant/message.d.ts +7 -0
- package/dist-lib/lib/assistant/model-round.d.ts +12 -0
- package/dist-lib/lib/assistant/model-round.js +130 -17
- package/dist-lib/lib/assistant/pdf-pages.d.ts +16 -1
- package/dist-lib/lib/assistant/pdf-pages.js +67 -3
- package/dist-lib/lib/assistant/round-host.d.ts +22 -0
- package/dist-lib/lib/assistant/round-host.js +142 -8
- package/dist-lib/lib/assistant/upload.d.ts +12 -0
- package/dist-lib/lib/assistant/upload.js +36 -0
- package/dist-lib/lib/assistant-client.d.ts +1 -0
- package/dist-lib/lib/assistant-client.js +40 -19
- package/dist-lib/lib/assistant-tree.d.ts +2 -0
- package/dist-lib/lib/assistant-tree.js +41 -1
- package/dist-lib/lib/assistant.d.ts +15 -0
- package/dist-lib/lib/assistant.js +474 -89
- package/dist-lib/lib/client.d.ts +1 -0
- package/dist-lib/lib/client.js +4 -1
- package/dist-lib/lib/compaction.js +2 -2
- package/dist-lib/lib/core/agent.js +12 -45
- package/dist-lib/lib/core/approval-rules.js +1 -8
- package/dist-lib/lib/core/archived-mesh.d.ts +2 -1
- package/dist-lib/lib/core/archived-mesh.js +12 -8
- package/dist-lib/lib/core/chat-copy.d.ts +18 -3
- package/dist-lib/lib/core/chat-copy.js +74 -25
- package/dist-lib/lib/core/chat-fork.js +15 -6
- package/dist-lib/lib/core/chat-group.js +16 -24
- package/dist-lib/lib/core/chat-move.d.ts +9 -0
- package/dist-lib/lib/core/chat-move.js +94 -3
- package/dist-lib/lib/core/chat-order.js +12 -18
- package/dist-lib/lib/core/chat.js +38 -31
- package/dist-lib/lib/core/chunk.d.ts +2 -2
- package/dist-lib/lib/core/chunk.js +18 -25
- 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/activity.d.ts +4 -0
- package/dist-lib/lib/core/contract/activity.js +34 -4
- package/dist-lib/lib/core/contract/chat-group.d.ts +1 -0
- package/dist-lib/lib/core/contract/chat-group.js +21 -1
- package/dist-lib/lib/core/contract/chat.d.ts +9 -0
- package/dist-lib/lib/core/contract/chat.js +127 -5
- package/dist-lib/lib/core/contract/chunks.d.ts +1 -0
- package/dist-lib/lib/core/contract/chunks.js +37 -6
- package/dist-lib/lib/core/contract/features.d.ts +21 -0
- package/dist-lib/lib/core/contract/features.js +22 -0
- package/dist-lib/lib/core/contract/index.js +2 -0
- package/dist-lib/lib/core/contract/indexing.js +4 -2
- package/dist-lib/lib/core/contract/knowledge.js +5 -4
- package/dist-lib/lib/core/contract/members.js +16 -10
- package/dist-lib/lib/core/contract/mesh-kind.d.ts +1 -0
- package/dist-lib/lib/core/contract/mesh-kind.js +3 -1
- package/dist-lib/lib/core/contract/projects.d.ts +5 -0
- package/dist-lib/lib/core/contract/projects.js +13 -5
- package/dist-lib/lib/core/contract/roles.js +32 -4
- package/dist-lib/lib/core/contract/run-requests.d.ts +1 -0
- package/dist-lib/lib/core/contract/run-requests.js +35 -2
- package/dist-lib/lib/core/contract/seed-defaults.js +2 -2
- package/dist-lib/lib/core/contract/tool-configs.js +3 -2
- package/dist-lib/lib/core/contract/workspace.d.ts +2 -0
- package/dist-lib/lib/core/contract/workspace.js +40 -7
- package/dist-lib/lib/core/contract/writers.js +2 -2
- package/dist-lib/lib/core/departures.d.ts +3 -1
- package/dist-lib/lib/core/departures.js +3 -0
- package/dist-lib/lib/core/devices.js +1 -8
- package/dist-lib/lib/core/files/attachments.d.ts +2 -0
- package/dist-lib/lib/core/files/attachments.js +19 -1
- package/dist-lib/lib/core/files/index.d.ts +20 -4
- package/dist-lib/lib/core/files/index.js +132 -21
- package/dist-lib/lib/core/files/processing/document.d.ts +4 -0
- package/dist-lib/lib/core/files/processing/document.js +7 -0
- package/dist-lib/lib/core/files/processing/index.d.ts +13 -0
- package/dist-lib/lib/core/files/processing/index.js +144 -12
- package/dist-lib/lib/core/files/processing/protocol.js +13 -34
- package/dist-lib/lib/core/files/processing/state.d.ts +2 -0
- package/dist-lib/lib/core/files/rag/build.d.ts +19 -2
- package/dist-lib/lib/core/files/rag/build.js +123 -19
- package/dist-lib/lib/core/files/rag/failure.js +2 -1
- package/dist-lib/lib/core/files/rag/index.d.ts +11 -3
- package/dist-lib/lib/core/files/rag/index.js +13 -1
- package/dist-lib/lib/core/files/rag/load.d.ts +6 -3
- package/dist-lib/lib/core/files/rag/load.js +27 -6
- package/dist-lib/lib/core/files/rag/plugin-index.d.ts +32 -0
- package/dist-lib/lib/core/files/rag/plugin-index.js +127 -0
- package/dist-lib/lib/core/files/rag/produce.d.ts +5 -3
- package/dist-lib/lib/core/files/rag/produce.js +104 -16
- package/dist-lib/lib/core/files/rag/retrieval/full-text.d.ts +3 -1
- package/dist-lib/lib/core/files/rag/retrieval/full-text.js +62 -13
- package/dist-lib/lib/core/files/rag/text-cache.d.ts +28 -0
- package/dist-lib/lib/core/files/rag/text-cache.js +141 -0
- package/dist-lib/lib/core/files/records/knowledge.d.ts +1 -3
- package/dist-lib/lib/core/files/records/knowledge.js +7 -40
- package/dist-lib/lib/core/files/records/reclaim.js +5 -10
- package/dist-lib/lib/core/files/upload.js +68 -41
- package/dist-lib/lib/core/fs-drives.js +14 -26
- package/dist-lib/lib/core/invite.d.ts +1 -0
- package/dist-lib/lib/core/invite.js +4 -2
- package/dist-lib/lib/core/local/files.d.ts +1 -1
- package/dist-lib/lib/core/local/files.js +5 -5
- package/dist-lib/lib/core/local/index.d.ts +13 -6
- package/dist-lib/lib/core/local/index.js +103 -69
- package/dist-lib/lib/core/local/run-logs.d.ts +1 -1
- package/dist-lib/lib/core/local/run-logs.js +5 -5
- package/dist-lib/lib/core/mesh-processor.d.ts +19 -0
- package/dist-lib/lib/core/mesh-processor.js +87 -0
- package/dist-lib/lib/core/mesh.d.ts +48 -5
- package/dist-lib/lib/core/mesh.js +318 -129
- package/dist-lib/lib/core/migrations/agent-catalog.js +2 -8
- package/dist-lib/lib/core/migrations/agent-tools-scope.js +5 -11
- package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +88 -0
- package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +731 -0
- package/dist-lib/lib/core/migrations/writer-links.d.ts +2 -0
- package/dist-lib/lib/core/migrations/writer-links.js +32 -0
- package/dist-lib/lib/core/project-keys.d.ts +6 -0
- package/dist-lib/lib/core/project-keys.js +11 -0
- package/dist-lib/lib/core/public-mesh.d.ts +3 -0
- package/dist-lib/lib/core/public-mesh.js +10 -8
- package/dist-lib/lib/core/run-requests.d.ts +3 -0
- package/dist-lib/lib/core/run-requests.js +21 -1
- package/dist-lib/lib/core/skill-availability.d.ts +33 -0
- package/dist-lib/lib/core/skill-availability.js +67 -0
- package/dist-lib/lib/core/sync/background.d.ts +0 -1
- package/dist-lib/lib/core/sync/background.js +2 -1
- package/dist-lib/lib/core/sync/changed.d.ts +4 -2
- package/dist-lib/lib/core/sync/changed.js +1 -1
- package/dist-lib/lib/core/sync/index.js +52 -10
- package/dist-lib/lib/core/sync/view-updates.d.ts +18 -12
- package/dist-lib/lib/core/sync/view-updates.js +82 -91
- package/dist-lib/lib/core/sync-status.js +97 -41
- package/dist-lib/lib/core/tool-activity.js +6 -12
- package/dist-lib/lib/core/tool-configs.js +17 -29
- package/dist-lib/lib/core/upgrade-summary.d.ts +19 -0
- package/dist-lib/lib/core/upgrade-summary.js +67 -0
- package/dist-lib/lib/core/view-read.d.ts +4 -0
- package/dist-lib/lib/core/view-read.js +23 -0
- package/dist-lib/lib/core/workspace-upgrade.d.ts +7 -0
- package/dist-lib/lib/core/workspace-upgrade.js +27 -16
- package/dist-lib/lib/core.d.ts +39 -42
- package/dist-lib/lib/core.js +661 -639
- package/dist-lib/lib/errors.d.ts +20 -0
- package/dist-lib/lib/errors.js +20 -0
- package/dist-lib/lib/llm-model-names.d.ts +1 -1
- package/dist-lib/lib/llm-model-names.js +1 -1
- package/dist-lib/lib/mesh-scoped-engine.js +4 -0
- package/dist-lib/lib/model-context.js +1 -2
- package/dist-lib/lib/probe-rounds.d.ts +7 -2
- package/dist-lib/lib/probe-rounds.js +6 -1
- package/dist-lib/lib/run-transcript.d.ts +4 -1
- package/dist-lib/lib/run-transcript.js +15 -6
- package/dist-lib/schema/engine.js +83 -6
- package/dist-lib/schema/indexes.d.ts +3 -0
- package/dist-lib/schema/indexes.js +5 -0
- package/dist-lib/schema/local.js +11 -2
- package/dist-lib/schema/qvac.js +49 -5
- package/dist-lib/spec/bind.js +34 -0
- package/dist-lib/spec/calls.js +5 -0
- package/dist-lib/spec/capabilities.d.ts +17 -0
- package/dist-lib/spec/hrpc/hrpc.json +65 -0
- package/dist-lib/spec/hrpc/index.js +68 -5
- package/dist-lib/spec/hrpc/messages.js +260 -23
- package/dist-lib/spec/hyperschema/index.js +260 -23
- package/dist-lib/spec/hyperschema/schema.json +181 -0
- package/dist-lib/spec/hyperschema/types.d.ts +48 -0
- package/dist-lib/spec/local/hyperdb/messages.js +270 -224
- package/dist-lib/spec/local/hyperschema/index.js +73 -38
- package/dist-lib/spec/local/hyperschema/schema.json +31 -0
- package/dist-lib/spec/local/hyperschema/types.d.ts +8 -0
- package/dist-lib/spec/mesh/hyperdb/db.json +39 -2
- package/dist-lib/spec/mesh/hyperdb/index.js +120 -3
- package/dist-lib/spec/mesh/hyperdb/messages.js +1328 -838
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +30 -0
- package/dist-lib/spec/mesh/hyperdispatch/index.js +86 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +682 -287
- package/dist-lib/spec/mesh/hyperschema/index.js +682 -287
- package/dist-lib/spec/mesh/hyperschema/schema.json +201 -0
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +52 -0
- package/package.json +75 -70
- package/tui/README.md +2 -1
- package/tui/app.mjs +28 -7
- package/tui/boot.mjs +5 -1
- package/tui/lib/attachment-files.mjs +82 -0
- package/tui/lib/reopen-meshes.mjs +15 -0
- package/tui/main.mjs +4 -43
- package/worker/assistant-sidecar.mjs +10 -3
- package/worker/stow-entry.mjs +3 -52
|
@@ -30,6 +30,9 @@ export class ChatMoves {
|
|
|
30
30
|
_listeners = new Set();
|
|
31
31
|
_settling = new Map();
|
|
32
32
|
_moving = new Map();
|
|
33
|
+
_followed = new WeakSet();
|
|
34
|
+
_forwards = new Set();
|
|
35
|
+
_forwarding = new Set();
|
|
33
36
|
_abort = new AbortController();
|
|
34
37
|
_onChange = () => this._changed();
|
|
35
38
|
_patching = Promise.resolve();
|
|
@@ -104,6 +107,12 @@ export class ChatMoves {
|
|
|
104
107
|
if (!row.completedAt && !row.failure) {
|
|
105
108
|
this._start(row).copied.catch(noop);
|
|
106
109
|
}
|
|
110
|
+
else if (row.completedAt && !row.failure) {
|
|
111
|
+
const pair = await this._pairOf(row);
|
|
112
|
+
if (pair) {
|
|
113
|
+
this._follow(pair.source, new Set([row.chatId]));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
107
116
|
}
|
|
108
117
|
}
|
|
109
118
|
async close() {
|
|
@@ -111,6 +120,7 @@ export class ChatMoves {
|
|
|
111
120
|
this._abort.abort(new AbortedError());
|
|
112
121
|
this._changed();
|
|
113
122
|
await Promise.allSettled([...this._settling.values()].map((settling) => settling.done));
|
|
123
|
+
await Promise.all(this._forwards);
|
|
114
124
|
}
|
|
115
125
|
async _begin({ meshId, chatId, targetMeshId, agentId }) {
|
|
116
126
|
if (meshId === targetMeshId)
|
|
@@ -220,8 +230,8 @@ export class ChatMoves {
|
|
|
220
230
|
await this._stopped(pair, row, moved.open);
|
|
221
231
|
continue;
|
|
222
232
|
}
|
|
223
|
-
if (moved.copied === 0 && carry.pending === 0) {
|
|
224
|
-
return
|
|
233
|
+
if (moved.copied === 0 && carry.pending === 0 && (await this._leave(pair, row))) {
|
|
234
|
+
return;
|
|
225
235
|
}
|
|
226
236
|
if (carry.pending > 0) {
|
|
227
237
|
await this._changeOf(pair);
|
|
@@ -231,14 +241,95 @@ export class ChatMoves {
|
|
|
231
241
|
async _leave(pair, row) {
|
|
232
242
|
const gone = await this._gone(pair, row);
|
|
233
243
|
if (gone) {
|
|
234
|
-
|
|
244
|
+
await this._giveUp(pair, row, gone);
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
const late = await this._copy(pair, row);
|
|
248
|
+
if (late.copied > 0 || late.open.size > 0 || late.withheld > 0) {
|
|
249
|
+
return false;
|
|
235
250
|
}
|
|
236
251
|
await this._restart(pair, row);
|
|
237
252
|
const { agentId } = await deleteChat(pair.source, this._deviceId, row.chatId);
|
|
238
253
|
await this._patch(row, { completedAt: Date.now() });
|
|
254
|
+
this._follow(pair.source, new Set([row.chatId]));
|
|
239
255
|
await this._agents.unpick(pair.source, agentId).catch((err) => {
|
|
240
256
|
this._log?.warn('chat-move', "letting go of the moved chat's agent failed", { err });
|
|
241
257
|
});
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
async followSource(source) {
|
|
261
|
+
const rows = await this._local.listChatMovesFrom(source.key);
|
|
262
|
+
if (rows.some((row) => row.completedAt && !row.failure)) {
|
|
263
|
+
this._follow(source);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
_follow(source, chatIds = null) {
|
|
267
|
+
if (!this._followed.has(source)) {
|
|
268
|
+
this._followed.add(source);
|
|
269
|
+
source.on('chats-edited', (edited) => this._forward(source, edited));
|
|
270
|
+
source.on('landed', () => this._forward(source, null));
|
|
271
|
+
}
|
|
272
|
+
this._forward(source, chatIds);
|
|
273
|
+
}
|
|
274
|
+
_forward(source, chatIds) {
|
|
275
|
+
if (this._closing || source.closing)
|
|
276
|
+
return;
|
|
277
|
+
const forwarding = this._forwardMoved(source, chatIds).catch((err) => {
|
|
278
|
+
if (this._closing || source.closing)
|
|
279
|
+
return;
|
|
280
|
+
this._log?.warn('chat-move', 'forwarding a write into the moved chat failed', { err });
|
|
281
|
+
});
|
|
282
|
+
this._forwards.add(forwarding);
|
|
283
|
+
void forwarding.then(() => this._forwards.delete(forwarding));
|
|
284
|
+
}
|
|
285
|
+
async _forwardMoved(source, chatIds) {
|
|
286
|
+
const rows = chatIds
|
|
287
|
+
? await Promise.all([...chatIds].map((id) => this._local.readChatMove(source.key, id)))
|
|
288
|
+
: await this._local.listChatMovesFrom(source.key);
|
|
289
|
+
for (const row of rows) {
|
|
290
|
+
if (this._closing)
|
|
291
|
+
return;
|
|
292
|
+
if (!row?.completedAt || row.failure)
|
|
293
|
+
continue;
|
|
294
|
+
const [late] = await listChunks(source, { chatId: row.chatId, limit: 1 });
|
|
295
|
+
const pair = late ? await this._pairOf(row) : null;
|
|
296
|
+
if (!pair || (await this._unreachable(pair.target, row))) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const snap = source.view.snapshot();
|
|
300
|
+
try {
|
|
301
|
+
await copyMovedChunks(snap, row.chatId, this._copyOf(pair, row));
|
|
302
|
+
}
|
|
303
|
+
finally {
|
|
304
|
+
await snap.close();
|
|
305
|
+
}
|
|
306
|
+
this._carryLate(pair, row);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
async _unreachable(target, row) {
|
|
310
|
+
if ((await this._left(target)) || (await this._revoked(target))) {
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
const moved = await getChat(target, row.targetChatId);
|
|
314
|
+
return !moved || !!moved.deletedAt;
|
|
315
|
+
}
|
|
316
|
+
_carryLate(pair, row) {
|
|
317
|
+
const carry = { pair, row, signal: this._abort.signal };
|
|
318
|
+
void pendingFiles(pair.target, row.targetChatId).then((pending) => {
|
|
319
|
+
for (const file of pending) {
|
|
320
|
+
if (!file.chunk || !this._owns(file.chunk) || this._forwarding.has(file.id)) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
this._forwarding.add(file.id);
|
|
324
|
+
this._carryChunk(carry, file)
|
|
325
|
+
.catch((err) => {
|
|
326
|
+
if (err instanceof AbortedError)
|
|
327
|
+
return;
|
|
328
|
+
this._log?.warn('chat-move', 'carrying a late file failed', { id: file.id, err });
|
|
329
|
+
})
|
|
330
|
+
.finally(() => this._forwarding.delete(file.id));
|
|
331
|
+
}
|
|
332
|
+
}, noop);
|
|
242
333
|
}
|
|
243
334
|
async _cancel({ source }, row) {
|
|
244
335
|
await cancelActiveRuns(source, row.chatId, { deviceId: this._deviceId, reason: CHAT_MOVED });
|
|
@@ -4,6 +4,7 @@ import { ownReads } from './chat-read.js';
|
|
|
4
4
|
import { listChatGroupsIn } from './chat-group.js';
|
|
5
5
|
import { ACTIVE_CHATS, CHATS, CHUNK_COUNTERS } from './collections.js';
|
|
6
6
|
import { syncChatTombstone } from './sync/chats.js';
|
|
7
|
+
import { readView } from './view-read.js';
|
|
7
8
|
export function chatSortKey(chat) {
|
|
8
9
|
return chat.rank && (chat.rankedAt ?? 0) >= chat.lastActivityAt ? chat.rank : chat.lastActivityAt;
|
|
9
10
|
}
|
|
@@ -32,20 +33,17 @@ export async function refreshChatOrder(mesh, local, chatIds) {
|
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
export async function rebuildChatOrder(mesh, local) {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
const rows = [];
|
|
36
|
+
const rows = await readView(mesh, async (snap) => {
|
|
37
|
+
const built = [];
|
|
38
38
|
for (const chat of await snap.find(ACTIVE_CHATS).toArray()) {
|
|
39
|
-
|
|
39
|
+
built.push({ chatId: chat.id, sortKey: await sortKeyOf(snap, chat) });
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
await snap.close();
|
|
48
|
-
}
|
|
41
|
+
return built;
|
|
42
|
+
});
|
|
43
|
+
const live = new Set(rows.map((row) => row.chatId));
|
|
44
|
+
const stored = await local.listChatOrder(mesh.key);
|
|
45
|
+
await local.writeChatOrder(mesh.key, rows);
|
|
46
|
+
return stored.map((row) => row.chatId).filter((chatId) => !live.has(chatId));
|
|
49
47
|
}
|
|
50
48
|
export async function retireGoneChats(mesh, local, chatIds) {
|
|
51
49
|
for (const chatId of chatIds) {
|
|
@@ -67,8 +65,7 @@ export async function listChatPage(mesh, local, { limit, cursor } = {}) {
|
|
|
67
65
|
await mesh.chatOrderBuilt;
|
|
68
66
|
const before = cursor === undefined ? undefined : decodeCursor(cursor);
|
|
69
67
|
const ordered = await local.listChatOrder(mesh.key, { limit, before });
|
|
70
|
-
|
|
71
|
-
try {
|
|
68
|
+
return readView(mesh, async (snap) => {
|
|
72
69
|
const rows = [];
|
|
73
70
|
const activity = new Map();
|
|
74
71
|
for (const entry of ordered) {
|
|
@@ -88,10 +85,7 @@ export async function listChatPage(mesh, local, { limit, cursor } = {}) {
|
|
|
88
85
|
groups,
|
|
89
86
|
nextCursor: nextCursorOf(ordered, limit)
|
|
90
87
|
};
|
|
91
|
-
}
|
|
92
|
-
finally {
|
|
93
|
-
await snap.close();
|
|
94
|
-
}
|
|
88
|
+
});
|
|
95
89
|
}
|
|
96
90
|
function nextCursorOf(ordered, limit) {
|
|
97
91
|
if (limit === undefined || ordered.length < limit) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CoreError } from '../errors.js';
|
|
2
2
|
import { TERMINAL_RUN_STATES } from './contract/run-requests.js';
|
|
3
|
+
import { nextUpdatedAt } from './contract/chat.js';
|
|
3
4
|
import { mustGetActiveAgent } from './agent.js';
|
|
4
5
|
import { listChatGroupsIn, mustGetActiveChatGroup } from './chat-group.js';
|
|
5
6
|
import { appendChunk, listChunks, updateOp } from './chunk.js';
|
|
@@ -9,6 +10,7 @@ import { chatReferences, reclaimReferences } from './files/records/purge.js';
|
|
|
9
10
|
import { place } from './rank.js';
|
|
10
11
|
import { chatSortKey } from './chat-order.js';
|
|
11
12
|
import { ownReadThrough, ownReads } from './chat-read.js';
|
|
13
|
+
import { readView } from './view-read.js';
|
|
12
14
|
import b4a from 'b4a';
|
|
13
15
|
import { ACTIVE_CHATS, ACTIVE_CHATS_BY_AGENT, ACTIVE_CHAT_GROUPS, CHATS, CHAT_GROUPS, CHUNK_COUNTERS } from './collections.js';
|
|
14
16
|
function byChatOrder(a, b) {
|
|
@@ -83,14 +85,7 @@ export async function listChats(store) {
|
|
|
83
85
|
return (await listSidebar(store)).chats;
|
|
84
86
|
}
|
|
85
87
|
export async function getChat(store, id) {
|
|
86
|
-
|
|
87
|
-
try {
|
|
88
|
-
const chat = await snap.get(CHATS, { id });
|
|
89
|
-
return chat;
|
|
90
|
-
}
|
|
91
|
-
finally {
|
|
92
|
-
await snap.close();
|
|
93
|
-
}
|
|
88
|
+
return store.view.get(CHATS, { id });
|
|
94
89
|
}
|
|
95
90
|
export async function mustGetChat(store, id) {
|
|
96
91
|
const chat = await getChat(store, id);
|
|
@@ -106,8 +101,14 @@ export async function mustGetActiveChat(store, id) {
|
|
|
106
101
|
}
|
|
107
102
|
return chat;
|
|
108
103
|
}
|
|
109
|
-
export
|
|
110
|
-
return
|
|
104
|
+
export function mustGetActiveChatListing(store, id) {
|
|
105
|
+
return readView(store, async (snap) => {
|
|
106
|
+
const chat = await snap.get(CHATS, { id });
|
|
107
|
+
if (!chat || chat.deletedAt) {
|
|
108
|
+
throw new CoreError('CHAT_NOT_FOUND', [id]);
|
|
109
|
+
}
|
|
110
|
+
return enrichIn(snap, chat, store.ourMemberId);
|
|
111
|
+
});
|
|
111
112
|
}
|
|
112
113
|
export function buildChat(input) {
|
|
113
114
|
const now = Date.now();
|
|
@@ -132,8 +133,12 @@ async function patchChat(store, id, patch) {
|
|
|
132
133
|
return patchChatRow(store, await mustGetActiveChat(store, id), patch);
|
|
133
134
|
}
|
|
134
135
|
async function patchChatRow(store, current, patch) {
|
|
135
|
-
const updated = {
|
|
136
|
-
|
|
136
|
+
const updated = {
|
|
137
|
+
...current,
|
|
138
|
+
...patch,
|
|
139
|
+
updatedAt: nextUpdatedAt(current.updatedAt, Date.now())
|
|
140
|
+
};
|
|
141
|
+
await store.dispatch('patch-chat', { chat: updated, fields: Object.keys(patch) });
|
|
137
142
|
return enrichChat(store, updated);
|
|
138
143
|
}
|
|
139
144
|
export function renameChat(store, id, title) {
|
|
@@ -156,7 +161,11 @@ export async function switchChatAgent(store, id, { deviceId, agentId }) {
|
|
|
156
161
|
if (current.agentId === agentId) {
|
|
157
162
|
return enrichChat(store, current);
|
|
158
163
|
}
|
|
159
|
-
const switched = {
|
|
164
|
+
const switched = {
|
|
165
|
+
...current,
|
|
166
|
+
agentId,
|
|
167
|
+
updatedAt: nextUpdatedAt(current.updatedAt, Date.now())
|
|
168
|
+
};
|
|
160
169
|
const batch = store.batch();
|
|
161
170
|
await appendChunk(store, {
|
|
162
171
|
chatId: id,
|
|
@@ -164,7 +173,7 @@ export async function switchChatAgent(store, id, { deviceId, agentId }) {
|
|
|
164
173
|
originDeviceId: deviceId,
|
|
165
174
|
agentSwitch: { agentId, name: agent.name }
|
|
166
175
|
}, batch);
|
|
167
|
-
batch.dispatch('
|
|
176
|
+
batch.dispatch('patch-chat', { chat: switched, fields: ['agentId'] });
|
|
168
177
|
await batch.flush();
|
|
169
178
|
await cancelActiveRuns(store, id, {
|
|
170
179
|
deviceId,
|
|
@@ -257,14 +266,18 @@ export async function moveChat(store, id, { groupId, afterId }) {
|
|
|
257
266
|
...chat,
|
|
258
267
|
rank,
|
|
259
268
|
rankedAt: Math.max(now, lastActivityAt),
|
|
260
|
-
updatedAt:
|
|
269
|
+
updatedAt: nextUpdatedAt(chat.updatedAt, now)
|
|
261
270
|
});
|
|
262
271
|
const batch = store.batch();
|
|
263
272
|
for (const { chat, lastActivityAt, rank } of respaced) {
|
|
264
|
-
batch.dispatch('
|
|
273
|
+
batch.dispatch('patch-chat', {
|
|
274
|
+
chat: stamp(chat, lastActivityAt, rank),
|
|
275
|
+
fields: ['rank', 'rankedAt']
|
|
276
|
+
});
|
|
265
277
|
}
|
|
266
|
-
batch.dispatch('
|
|
267
|
-
chat: { ...stamp(moved.chat, moved.lastActivityAt, placement.rank), groupId: target }
|
|
278
|
+
batch.dispatch('patch-chat', {
|
|
279
|
+
chat: { ...stamp(moved.chat, moved.lastActivityAt, placement.rank), groupId: target },
|
|
280
|
+
fields: ['rank', 'rankedAt', 'groupId']
|
|
268
281
|
});
|
|
269
282
|
await batch.flush();
|
|
270
283
|
return enrichChat(store, await mustGetActiveChat(store, id));
|
|
@@ -359,18 +372,12 @@ export function softDeleteChatsForAgent(store, deviceId, agentId) {
|
|
|
359
372
|
}
|
|
360
373
|
export const AGENT_DELETED = 'agent-deleted';
|
|
361
374
|
async function eachChatOfAgent(store, agentId, fn) {
|
|
362
|
-
const
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
return;
|
|
370
|
-
await fn(chat.id);
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
finally {
|
|
374
|
-
await snap.close();
|
|
375
|
+
for await (const chat of store.view.find(ACTIVE_CHATS_BY_AGENT, {
|
|
376
|
+
gte: { agentId },
|
|
377
|
+
lte: { agentId }
|
|
378
|
+
})) {
|
|
379
|
+
if (store.closing)
|
|
380
|
+
return;
|
|
381
|
+
await fn(chat.id);
|
|
375
382
|
}
|
|
376
383
|
}
|
|
@@ -4,7 +4,7 @@ import type { AssistantChunkContentToolResult } from '../../spec/hyperschema/typ
|
|
|
4
4
|
import type { Mesh, MeshDispatcher } from './mesh.ts';
|
|
5
5
|
import { type Bounds } from './range.ts';
|
|
6
6
|
type ChunkPayload = Pick<QvacChunk, 'userMessage' | 'content' | 'thinking' | 'toolCall' | 'toolResult' | 'attachment' | 'approvalRequest' | 'runStatus' | 'runRequest' | 'sources' | 'compaction' | 'context' | 'modelRequest' | 'roundReset' | 'agentSwitch'>;
|
|
7
|
-
type AppendChunkInput = Pick<QvacChunk, 'chatId' | 'runId' | 'type' | 'originDeviceId' | 'parentId' | 'effect' | 'intent' | 'display' | 'rawCall' | 'capability' | 'capabilityArgs' | 'enhance' | 'senderNameAtSend' | 'sourceId' | 'round' | 'callId' | 'resumed' | 'runnerDeviceId'> & ChunkPayload & {
|
|
7
|
+
type AppendChunkInput = Pick<QvacChunk, 'chatId' | 'runId' | 'type' | 'originDeviceId' | 'parentId' | 'effect' | 'intent' | 'display' | 'rawCall' | 'capability' | 'capabilityArgs' | 'enhance' | 'senderNameAtSend' | 'sourceId' | 'round' | 'callId' | 'resumed' | 'runnerDeviceId' | 'unavailableSkills'> & ChunkPayload & {
|
|
8
8
|
id?: string;
|
|
9
9
|
};
|
|
10
10
|
type UpdateChunkInput = Pick<QvacChunk, 'id' | 'runStatus' | 'attachment'>;
|
|
@@ -63,7 +63,7 @@ interface RunAddress {
|
|
|
63
63
|
readonly runId: string;
|
|
64
64
|
}
|
|
65
65
|
export declare function requireStoppableRun(store: Mesh, { meshId, chatId, runId }: RunAddress): Promise<void>;
|
|
66
|
-
export declare function appendChunk(store: Mesh, { id, chatId, runId, type, originDeviceId, parentId, effect, intent, display, rawCall, capability, capabilityArgs, enhance, senderNameAtSend, sourceId, round, callId, resumed, runnerDeviceId, ...payload }: AppendChunkInput, into?: MeshDispatcher): Promise<AppendedChunk>;
|
|
66
|
+
export declare function appendChunk(store: Mesh, { id, chatId, runId, type, originDeviceId, parentId, effect, intent, display, rawCall, capability, capabilityArgs, enhance, senderNameAtSend, sourceId, round, callId, resumed, runnerDeviceId, unavailableSkills, ...payload }: AppendChunkInput, into?: MeshDispatcher): Promise<AppendedChunk>;
|
|
67
67
|
export declare function updateChunk(store: Mesh, { id, runStatus, attachment }: UpdateChunkInput): Promise<QvacChunk>;
|
|
68
68
|
export declare function updateOp(chunk: QvacChunk, { id, runStatus, attachment }: UpdateChunkInput): QvacUpdateChunkOperation;
|
|
69
69
|
export interface ResolveApprovalInput {
|
|
@@ -53,9 +53,9 @@ export async function recordLinks(store, chunks) {
|
|
|
53
53
|
await store.fileIndex?.record(chunks.filter((chunk) => chunk.attachment?.url));
|
|
54
54
|
return chunks;
|
|
55
55
|
}
|
|
56
|
-
export function findChunksOfType(snap, { chatId, type, ...bounds }) {
|
|
56
|
+
export async function findChunksOfType(snap, { chatId, type, ...bounds }) {
|
|
57
57
|
const prefix = { chatId, type: chunkTypeOrdinal(type) };
|
|
58
|
-
return snap
|
|
58
|
+
return await snap
|
|
59
59
|
.find(CHUNKS_BY_TYPE, keyRange(prefix, 'seq', bounds))
|
|
60
60
|
.toArray();
|
|
61
61
|
}
|
|
@@ -147,23 +147,17 @@ function seqSpan(count, { gt, gte, lt, lte, reverse, limit }) {
|
|
|
147
147
|
}
|
|
148
148
|
export async function listAttachmentsByKind(store, { kind, origin, ...bounds }) {
|
|
149
149
|
const mediaKind = mediaKindOrdinal(kind);
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const chunks = await snap
|
|
154
|
-
.find(ATTACHMENTS_BY_MEDIA_KIND, keyRange({ mediaKind }, 'createdAt', bounds))
|
|
155
|
-
.toArray();
|
|
156
|
-
return linkChunks(store, chunks);
|
|
157
|
-
}
|
|
158
|
-
const generated = origin === 'generated' ? 1 : 0;
|
|
159
|
-
const chunks = await snap
|
|
160
|
-
.find(ATTACHMENTS_BY_MEDIA_KIND_ORIGIN, keyRange({ mediaKind, generated }, 'createdAt', bounds))
|
|
150
|
+
if (origin === undefined) {
|
|
151
|
+
const chunks = await store.view
|
|
152
|
+
.find(ATTACHMENTS_BY_MEDIA_KIND, keyRange({ mediaKind }, 'createdAt', bounds))
|
|
161
153
|
.toArray();
|
|
162
154
|
return linkChunks(store, chunks);
|
|
163
155
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
156
|
+
const generated = origin === 'generated' ? 1 : 0;
|
|
157
|
+
const chunks = await store.view
|
|
158
|
+
.find(ATTACHMENTS_BY_MEDIA_KIND_ORIGIN, keyRange({ mediaKind, generated }, 'createdAt', bounds))
|
|
159
|
+
.toArray();
|
|
160
|
+
return linkChunks(store, chunks);
|
|
167
161
|
}
|
|
168
162
|
function selectedChunkTypes(type, types) {
|
|
169
163
|
if (!type && types === undefined)
|
|
@@ -188,14 +182,7 @@ function isHistorySelection(types) {
|
|
|
188
182
|
types.every((type) => HISTORY_CHUNK_TYPES.includes(type)));
|
|
189
183
|
}
|
|
190
184
|
export async function getChunk(store, id) {
|
|
191
|
-
|
|
192
|
-
try {
|
|
193
|
-
const chunk = await snap.get(CHUNKS, { id });
|
|
194
|
-
return chunk;
|
|
195
|
-
}
|
|
196
|
-
finally {
|
|
197
|
-
await snap.close();
|
|
198
|
-
}
|
|
185
|
+
return store.view.get(CHUNKS, { id });
|
|
199
186
|
}
|
|
200
187
|
export async function mustGetChunk(store, id) {
|
|
201
188
|
const chunk = await getChunk(store, id);
|
|
@@ -286,7 +273,7 @@ async function namedHost(store, agentId) {
|
|
|
286
273
|
}
|
|
287
274
|
return (await runDefinition(store, row))?.providerDeviceId;
|
|
288
275
|
}
|
|
289
|
-
export async function appendChunk(store, { id = randomId(), chatId, runId, type, originDeviceId, parentId, effect, intent, display, rawCall, capability, capabilityArgs, enhance, senderNameAtSend, sourceId, round, callId, resumed, runnerDeviceId, ...payload }, into = store) {
|
|
276
|
+
export async function appendChunk(store, { id = randomId(), chatId, runId, type, originDeviceId, parentId, effect, intent, display, rawCall, capability, capabilityArgs, enhance, senderNameAtSend, sourceId, round, callId, resumed, runnerDeviceId, unavailableSkills, ...payload }, into = store) {
|
|
290
277
|
assertPayloadMatchesType(type, payload);
|
|
291
278
|
if (type === 'sources' && !runId) {
|
|
292
279
|
throw new CoreError('CHUNK_PAYLOAD_INVALID', ['a sources chunk requires runId']);
|
|
@@ -300,6 +287,11 @@ export async function appendChunk(store, { id = randomId(), chatId, runId, type,
|
|
|
300
287
|
if (runnerDeviceId && (type !== 'run-request' || capability)) {
|
|
301
288
|
throw new CoreError('CHUNK_PAYLOAD_INVALID', ['only a run-request for an agent names a runner']);
|
|
302
289
|
}
|
|
290
|
+
if (unavailableSkills?.length && type !== 'run-status') {
|
|
291
|
+
throw new CoreError('CHUNK_PAYLOAD_INVALID', [
|
|
292
|
+
'only a run-status names the skills a run lacked'
|
|
293
|
+
]);
|
|
294
|
+
}
|
|
303
295
|
if (sourceId && type !== 'attachment') {
|
|
304
296
|
throw new CoreError('CHUNK_PAYLOAD_INVALID', ['only an attachment is derived from another']);
|
|
305
297
|
}
|
|
@@ -350,6 +342,7 @@ export async function appendChunk(store, { id = randomId(), chatId, runId, type,
|
|
|
350
342
|
callId,
|
|
351
343
|
resumed,
|
|
352
344
|
runnerDeviceId,
|
|
345
|
+
unavailableSkills: unavailableSkills?.length ? unavailableSkills : undefined,
|
|
353
346
|
createdAt: Date.now(),
|
|
354
347
|
senderMemberId,
|
|
355
348
|
senderNameAtSend: senderMemberId ? senderNameAtSend : undefined,
|
|
@@ -36,6 +36,7 @@ export declare const MEMBERS = "@qvac/members";
|
|
|
36
36
|
export declare const CHAT_GROUPS = "@qvac/chat-groups";
|
|
37
37
|
export declare const ACTIVE_CHAT_GROUPS = "@qvac/active-chat-groups";
|
|
38
38
|
export declare const LIVE_WRITERS_BY_MOST_RECENT = "@qvac/live-writers-by-most-recent";
|
|
39
|
+
export declare const LINKED_WRITERS_BY_MOST_RECENT = "@qvac/linked-writers-by-most-recent";
|
|
39
40
|
export declare const JOINED_MESHES = "@qvac/joined-meshes";
|
|
40
41
|
export declare const AGENTS_BY_PROVIDER = "@qvac/agents-by-provider";
|
|
41
42
|
export declare const DEFAULT_AGENTS = "@qvac/default-agents";
|
|
@@ -107,3 +108,4 @@ export declare const PROJECT_MEMBER_SEALS = "@qvac/project-member-seals";
|
|
|
107
108
|
export declare const PROJECT_HOST_SEALS = "@qvac/project-host-seals";
|
|
108
109
|
export declare const PROJECT_DEVICE_SEALS = "@qvac/project-device-seals";
|
|
109
110
|
export declare const SKILL_SECRETS = "@qvac/skill-secrets";
|
|
111
|
+
export declare const MOVED_OUT_CHATS = "@qvac/moved-out-chats";
|
|
@@ -36,6 +36,7 @@ export const MEMBERS = '@qvac/members';
|
|
|
36
36
|
export const CHAT_GROUPS = '@qvac/chat-groups';
|
|
37
37
|
export const ACTIVE_CHAT_GROUPS = '@qvac/active-chat-groups';
|
|
38
38
|
export const LIVE_WRITERS_BY_MOST_RECENT = '@qvac/live-writers-by-most-recent';
|
|
39
|
+
export const LINKED_WRITERS_BY_MOST_RECENT = '@qvac/linked-writers-by-most-recent';
|
|
39
40
|
export const JOINED_MESHES = '@qvac/joined-meshes';
|
|
40
41
|
export const AGENTS_BY_PROVIDER = '@qvac/agents-by-provider';
|
|
41
42
|
export const DEFAULT_AGENTS = '@qvac/default-agents';
|
|
@@ -107,3 +108,4 @@ export const PROJECT_MEMBER_SEALS = '@qvac/project-member-seals';
|
|
|
107
108
|
export const PROJECT_HOST_SEALS = '@qvac/project-host-seals';
|
|
108
109
|
export const PROJECT_DEVICE_SEALS = '@qvac/project-device-seals';
|
|
109
110
|
export const SKILL_SECRETS = '@qvac/skill-secrets';
|
|
111
|
+
export const MOVED_OUT_CHATS = '@qvac/moved-out-chats';
|
|
@@ -2,6 +2,7 @@ import type { AutobeeNode } from 'autobee';
|
|
|
2
2
|
import type HyperDB from 'hyperdb';
|
|
3
3
|
import type { HyperDBStream } from 'hyperdb';
|
|
4
4
|
import type { QvacDevice } from '../../../spec/mesh/hyperschema/types.d.ts';
|
|
5
|
+
import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
|
|
5
6
|
import type { RouterContext } from './types.ts';
|
|
6
7
|
type RouteHandler<T> = (op: T, ctx: RouterContext) => Promise<unknown>;
|
|
7
8
|
export declare function trackActivity<T>(handler: RouteHandler<T>): RouteHandler<T>;
|
|
@@ -14,5 +15,8 @@ export interface MarkWriterRevokedOptions {
|
|
|
14
15
|
readonly revoked: boolean;
|
|
15
16
|
}
|
|
16
17
|
export declare function markWriterRevoked(tx: HyperDB, writerKey: Buffer, opts: MarkWriterRevokedOptions): Promise<void>;
|
|
18
|
+
export declare function linkedTo(device: QvacDevice | null | undefined): boolean;
|
|
17
19
|
export declare function recordActivity(tx: HyperDB, node: AutobeeNode, clock: number): Promise<void>;
|
|
20
|
+
export declare function markWriterLinked(tx: HyperDB, device: QvacDevice): Promise<void>;
|
|
21
|
+
export declare function registerActivityRoutes(router: Router): void;
|
|
18
22
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
|
-
import {
|
|
2
|
+
import { FEATURES, isFeatureSupported } from './features.js';
|
|
3
3
|
import { DEVICES, DEVICES_BY_WRITER_KEY, DEVICES_BY_MEMBER, LIVE_DEVICES_BY_MEMBER, LIVE_DEVICES_BY_WRITER_KEY, WRITER_ACTIVITY } from '../collections.js';
|
|
4
4
|
export function trackActivity(handler) {
|
|
5
5
|
return async (op, ctx) => {
|
|
@@ -23,7 +23,7 @@ export function liveDeviceForWriter(db, writerKey) {
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
async function indexed(db) {
|
|
26
|
-
return (
|
|
26
|
+
return isFeatureSupported(db, FEATURES.SHIPPED_ROWS_INDEXED);
|
|
27
27
|
}
|
|
28
28
|
function ownerOf(device) {
|
|
29
29
|
return device.memberId ?? device.claimedMemberId ?? null;
|
|
@@ -71,12 +71,15 @@ export async function markWriterRevoked(tx, writerKey, opts) {
|
|
|
71
71
|
}
|
|
72
72
|
await tx.insert(WRITER_ACTIVITY, { ...existing, revoked });
|
|
73
73
|
}
|
|
74
|
+
export function linkedTo(device) {
|
|
75
|
+
return !!device && !device.revoked && !!device.memberId;
|
|
76
|
+
}
|
|
74
77
|
export async function recordActivity(tx, node, clock) {
|
|
75
78
|
const existing = await tx.get(WRITER_ACTIVITY, { writerKey: node.key });
|
|
76
79
|
if (existing && existing.length >= node.length) {
|
|
77
80
|
return;
|
|
78
81
|
}
|
|
79
|
-
const device = existing ? null : await devicesByWriterKey(tx, node.key).one();
|
|
82
|
+
const device = existing?.linked ? null : await devicesByWriterKey(tx, node.key).one();
|
|
80
83
|
const deviceId = existing?.deviceId ?? device?.id;
|
|
81
84
|
if (!deviceId) {
|
|
82
85
|
return;
|
|
@@ -86,6 +89,33 @@ export async function recordActivity(tx, node, clock) {
|
|
|
86
89
|
deviceId,
|
|
87
90
|
length: node.length,
|
|
88
91
|
clock,
|
|
89
|
-
revoked: existing ? existing.revoked : device?.revoked
|
|
92
|
+
revoked: existing ? existing.revoked : device?.revoked,
|
|
93
|
+
linked: existing?.linked || linkedTo(device)
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export async function markWriterLinked(tx, device) {
|
|
97
|
+
if (!device.writerKey) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const existing = await tx.get(WRITER_ACTIVITY, {
|
|
101
|
+
writerKey: device.writerKey
|
|
102
|
+
});
|
|
103
|
+
if (!existing || existing.linked === linkedTo(device)) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
await tx.insert(WRITER_ACTIVITY, {
|
|
107
|
+
...existing,
|
|
108
|
+
version: undefined,
|
|
109
|
+
linked: linkedTo(device)
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
export function registerActivityRoutes(router) {
|
|
113
|
+
router.add('@qvac/reindex-writers', async ({ writerKeys }, { tx }) => {
|
|
114
|
+
for (const writerKey of writerKeys) {
|
|
115
|
+
const device = await liveDeviceForWriter(tx, writerKey);
|
|
116
|
+
if (device) {
|
|
117
|
+
await markWriterLinked(tx, device);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
90
120
|
});
|
|
91
121
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { trackActivity } from './activity.js';
|
|
2
|
+
import { nextUpdatedAt, patched } from './chat.js';
|
|
3
|
+
import { FEATURES, isFeatureSupported } from './features.js';
|
|
2
4
|
import { CHAT_GROUPS } from '../collections.js';
|
|
5
|
+
export const PATCHABLE_GROUP = ['name', 'rank'];
|
|
3
6
|
export function registerChatGroupRoutes(router) {
|
|
4
7
|
router.add('@qvac/put-chat-group', trackActivity(async ({ group }, { tx, log }) => {
|
|
5
8
|
const existing = await tx.get(CHAT_GROUPS, { id: group.id });
|
|
@@ -7,7 +10,10 @@ export function registerChatGroupRoutes(router) {
|
|
|
7
10
|
log?.debug('contract', 'put-chat-group dropped: tombstone is sticky', { id: group.id });
|
|
8
11
|
return;
|
|
9
12
|
}
|
|
10
|
-
if (existing &&
|
|
13
|
+
if (existing &&
|
|
14
|
+
!group.deletedAt &&
|
|
15
|
+
existing.updatedAt > group.updatedAt &&
|
|
16
|
+
!(await isFeatureSupported(tx, FEATURES.KEEP_STALE_PUTS))) {
|
|
11
17
|
log?.debug('contract', 'put-chat-group dropped: stale updatedAt', { id: group.id });
|
|
12
18
|
return;
|
|
13
19
|
}
|
|
@@ -17,4 +23,18 @@ export function registerChatGroupRoutes(router) {
|
|
|
17
23
|
});
|
|
18
24
|
return group;
|
|
19
25
|
}));
|
|
26
|
+
router.add('@qvac/patch-chat-group', trackActivity(async ({ group, fields }, { tx, log }) => {
|
|
27
|
+
const existing = await tx.get(CHAT_GROUPS, { id: group.id });
|
|
28
|
+
if (existing?.deletedAt) {
|
|
29
|
+
log?.debug('contract', 'patch-chat-group dropped: group is deleted', { id: group.id });
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const row = {
|
|
33
|
+
...patched(existing ?? group, { from: group, fields, patchable: PATCHABLE_GROUP }),
|
|
34
|
+
updatedAt: nextUpdatedAt(existing?.updatedAt, group.updatedAt)
|
|
35
|
+
};
|
|
36
|
+
await tx.insert(CHAT_GROUPS, row);
|
|
37
|
+
log?.debug('contract', 'chat group patched', { id: group.id });
|
|
38
|
+
return row;
|
|
39
|
+
}));
|
|
20
40
|
}
|
|
@@ -3,6 +3,15 @@ import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
|
|
|
3
3
|
import type { QvacChunk } from '../../../spec/mesh/hyperschema/types.d.ts';
|
|
4
4
|
export declare function isPurged(tx: HyperDB, chatId: string): Promise<boolean>;
|
|
5
5
|
export declare function foreignChat(tx: HyperDB, chatId: string | null | undefined): Promise<boolean>;
|
|
6
|
+
export declare function nextUpdatedAt(prev: number | null | undefined, at: number): number;
|
|
7
|
+
export declare const PATCHABLE_CHAT: readonly ["title", "agentId", "ragEnabled", "webSearchEnabled", "groupId", "rank", "rankedAt", "projectId", "knowledgeSourcesOff", "suggestion"];
|
|
8
|
+
export interface Patch<T> {
|
|
9
|
+
readonly from: T;
|
|
10
|
+
readonly fields: readonly string[] | null | undefined;
|
|
11
|
+
readonly patchable: ReadonlyArray<keyof T & string>;
|
|
12
|
+
}
|
|
13
|
+
export declare function patched<T>(row: T, { from, fields, patchable }: Patch<T>): T;
|
|
6
14
|
export declare function registerChatRoutes(router: Router): void;
|
|
15
|
+
export declare function movedOut(tx: HyperDB, chatId: string): Promise<boolean>;
|
|
7
16
|
export declare function putChunk(tx: HyperDB, chunk: QvacChunk): Promise<void>;
|
|
8
17
|
export declare function deleteChatChunks(tx: HyperDB, chatId: string): Promise<void>;
|