@qvac/core 0.6.7 → 0.7.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 +211 -1
- package/README.md +2 -0
- package/dist-lib/lib/api/app-config.d.ts +3 -1
- package/dist-lib/lib/api/app-config.js +15 -4
- package/dist-lib/lib/api/chat.d.ts +1 -1
- package/dist-lib/lib/api/chat.js +9 -0
- package/dist-lib/lib/api/index.d.ts +2 -1
- package/dist-lib/lib/api/index.js +2 -1
- package/dist-lib/lib/api/network.d.ts +8 -1
- package/dist-lib/lib/api/network.js +6 -0
- package/dist-lib/lib/assistant/fake-models.d.ts +22 -0
- package/dist-lib/lib/assistant/fake-models.js +112 -0
- package/dist-lib/lib/assistant/harness.d.ts +12 -0
- package/dist-lib/lib/assistant/harness.js +26 -0
- package/dist-lib/lib/assistant/history.d.ts +5 -1
- package/dist-lib/lib/assistant/history.js +14 -2
- package/dist-lib/lib/assistant-tree.d.ts +4 -0
- package/dist-lib/lib/assistant-tree.js +45 -7
- package/dist-lib/lib/assistant.d.ts +15 -2
- package/dist-lib/lib/assistant.js +181 -42
- package/dist-lib/lib/constants.d.ts +6 -0
- package/dist-lib/lib/constants.js +6 -0
- package/dist-lib/lib/core/agent.d.ts +2 -0
- package/dist-lib/lib/core/agent.js +16 -5
- package/dist-lib/lib/core/blind-peers.d.ts +4 -0
- package/dist-lib/lib/core/blind-peers.js +1 -0
- package/dist-lib/lib/core/chat-activity.d.ts +1 -1
- package/dist-lib/lib/core/chat-activity.js +3 -0
- package/dist-lib/lib/core/chat-copy.d.ts +4 -0
- package/dist-lib/lib/core/chat-copy.js +1 -1
- package/dist-lib/lib/core/chat-fork.d.ts +21 -0
- package/dist-lib/lib/core/chat-fork.js +245 -0
- package/dist-lib/lib/core/chat-move.js +1 -4
- package/dist-lib/lib/core/chat.d.ts +1 -0
- package/dist-lib/lib/core/chat.js +1 -1
- package/dist-lib/lib/core/collections.d.ts +1 -0
- package/dist-lib/lib/core/collections.js +1 -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 +6 -1
- package/dist-lib/lib/core/contract/invites.d.ts +1 -11
- package/dist-lib/lib/core/contract/invites.js +3 -3
- package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
- package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
- package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
- package/dist-lib/lib/core/contract/open-join.js +57 -0
- package/dist-lib/lib/core/departures.d.ts +8 -1
- package/dist-lib/lib/core/departures.js +59 -2
- package/dist-lib/lib/core/fs-drives.d.ts +8 -1
- package/dist-lib/lib/core/fs-drives.js +47 -15
- package/dist-lib/lib/core/harness-loan.d.ts +4 -20
- package/dist-lib/lib/core/harness-loan.js +7 -184
- package/dist-lib/lib/core/id.d.ts +1 -0
- package/dist-lib/lib/core/id.js +3 -0
- package/dist-lib/lib/core/identity.d.ts +1 -1
- package/dist-lib/lib/core/identity.js +6 -4
- package/dist-lib/lib/core/invite.d.ts +2 -0
- package/dist-lib/lib/core/invite.js +6 -2
- package/dist-lib/lib/core/membership.d.ts +1 -12
- package/dist-lib/lib/core/membership.js +1 -94
- package/dist-lib/lib/core/mesh.d.ts +11 -15
- package/dist-lib/lib/core/mesh.js +52 -88
- package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
- package/dist-lib/lib/core/migrations/agent-tools-scope.js +24 -0
- package/dist-lib/lib/core/migrations/index.js +3 -1
- package/dist-lib/lib/core/peer-duplex.d.ts +32 -0
- package/dist-lib/lib/core/peer-duplex.js +207 -0
- package/dist-lib/lib/core/sync/index.js +2 -16
- package/dist-lib/lib/core.d.ts +47 -50
- package/dist-lib/lib/core.js +524 -878
- package/dist-lib/lib/diagnostics.d.ts +7 -0
- package/dist-lib/lib/diagnostics.js +22 -0
- package/dist-lib/lib/doctor/checks.d.ts +15 -0
- package/dist-lib/lib/doctor/checks.js +272 -0
- package/dist-lib/lib/doctor/redact.d.ts +4 -0
- package/dist-lib/lib/doctor/redact.js +31 -0
- package/dist-lib/lib/doctor/render.d.ts +8 -0
- package/dist-lib/lib/doctor/render.js +83 -0
- package/dist-lib/lib/doctor/tree.d.ts +5 -0
- package/dist-lib/lib/doctor/tree.js +167 -0
- package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
- package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
- package/dist-lib/lib/guards.d.ts +1 -0
- package/dist-lib/lib/guards.js +4 -0
- package/dist-lib/lib/inference-calls.js +1 -5
- package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
- package/dist-lib/lib/kv-cache-reclaim.js +24 -0
- package/dist-lib/lib/mesh-scoped-engine.js +4 -0
- package/dist-lib/lib/model-download-status.d.ts +1 -1
- package/dist-lib/lib/model-download.d.ts +9 -10
- package/dist-lib/lib/model-download.js +49 -16
- package/dist-lib/lib/model-memory-cap.d.ts +29 -0
- package/dist-lib/lib/model-memory-cap.js +69 -0
- package/dist-lib/lib/models-watch.d.ts +1 -1
- package/dist-lib/lib/models-watch.js +4 -4
- package/dist-lib/lib/oauth/flow.js +1 -1
- package/dist-lib/lib/oauth/loopback.js +16 -2
- package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
- package/dist-lib/lib/run-replay.d.ts +26 -0
- package/dist-lib/lib/run-replay.js +107 -0
- package/dist-lib/schema/config.d.ts +1 -1
- package/dist-lib/schema/config.js +1 -1
- package/dist-lib/schema/content.js +9 -1
- package/dist-lib/schema/engine.js +75 -6
- package/dist-lib/schema/field-order.js +1 -1
- package/dist-lib/schema/local.js +3 -2
- package/dist-lib/schema/qvac.js +28 -2
- package/dist-lib/spec/bind.js +17 -0
- package/dist-lib/spec/calls.js +4 -0
- package/dist-lib/spec/capabilities.d.ts +8 -0
- package/dist-lib/spec/config/hyperconf/index.js +1 -1
- package/dist-lib/spec/hrpc/hrpc.json +52 -0
- package/dist-lib/spec/hrpc/index.js +53 -4
- package/dist-lib/spec/hrpc/messages.js +1498 -1278
- package/dist-lib/spec/hyperschema/index.js +1498 -1278
- package/dist-lib/spec/hyperschema/schema.json +182 -0
- package/dist-lib/spec/hyperschema/types.d.ts +49 -3
- package/dist-lib/spec/local/hyperdb/messages.js +22 -10
- package/dist-lib/spec/local/hyperschema/index.js +16 -7
- package/dist-lib/spec/local/hyperschema/schema.json +10 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
- 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 +530 -246
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
- package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +223 -10
- package/dist-lib/spec/mesh/hyperschema/index.js +223 -10
- package/dist-lib/spec/mesh/hyperschema/schema.json +128 -1
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +34 -2
- package/package.json +14 -19
- package/tui/README.md +7 -1
- package/tui/app.mjs +366 -78
- package/tui/boot.mjs +1 -0
- package/tui/index.mjs +1 -0
- package/tui/lib/fork-command.mjs +43 -0
- package/tui/lib/render.mjs +25 -1
- package/worker/assistant-sidecar.mjs +5 -2
- package/dist-lib/lib/core/device-invites.d.ts +0 -11
- package/dist-lib/lib/core/device-invites.js +0 -63
- package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
- package/dist-lib/lib/core/grant-expiry.js +0 -66
- package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
- package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
- package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
- package/dist-lib/lib/oauth/loopback.stub.js +0 -6
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type ChatListing } from './chat.ts';
|
|
2
|
+
import type { Mesh } from './mesh.ts';
|
|
3
|
+
export declare const NO_SUCH_TURN = "no turn of this chat has that run id";
|
|
4
|
+
export declare const TURN_RUNNING = "a turn up to the fork point is still running; fork once it has finished";
|
|
5
|
+
export declare const NOTHING_BEFORE = "nothing comes before the first turn, so there is nothing to fork";
|
|
6
|
+
export declare const AGENT_REQUIRED = "a chat forked into another mesh needs an agent of that mesh";
|
|
7
|
+
export declare const FILE_ARRIVING = "a file in this chat is still arriving; fork once it has landed";
|
|
8
|
+
export declare const FILE_NOT_HERE = "a file in this chat is not on this device yet; open it here first";
|
|
9
|
+
export interface ForkMeshes {
|
|
10
|
+
readonly from: Mesh;
|
|
11
|
+
readonly into: Mesh;
|
|
12
|
+
}
|
|
13
|
+
export interface ForkChatInput {
|
|
14
|
+
readonly chatId: string;
|
|
15
|
+
readonly runId?: string | null;
|
|
16
|
+
readonly exclusive?: boolean;
|
|
17
|
+
readonly agentId?: string | null;
|
|
18
|
+
readonly title?: string | null;
|
|
19
|
+
readonly originDeviceId: Buffer;
|
|
20
|
+
}
|
|
21
|
+
export declare function forkChat(meshes: ForkMeshes, input: ForkChatInput): Promise<ChatListing>;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import b4a from 'b4a';
|
|
2
|
+
import { mustGetActiveAgent } from './agent.js';
|
|
3
|
+
import { TERMINAL_RUN_STATES, enrichChat, listSidebar } from './chat.js';
|
|
4
|
+
import { copyBlob, removedReference, stripped } from './chat-copy.js';
|
|
5
|
+
import { COMPUTE_MESH, NOT_A_WRITER } from './chat-move.js';
|
|
6
|
+
import { chatSortKey } from './chat-order.js';
|
|
7
|
+
import { isPendingBlobReference } from './files/index.js';
|
|
8
|
+
import { groupAttachmentsByMessage } from './files/records/linkage.js';
|
|
9
|
+
import { derivedId, randomId } from './id.js';
|
|
10
|
+
import { rankOnTop } from './rank.js';
|
|
11
|
+
import { CHATS, CHUNKS_BY_CHAT, KNOWLEDGE_BY_CHAT } from './collections.js';
|
|
12
|
+
export const NO_SUCH_TURN = 'no turn of this chat has that run id';
|
|
13
|
+
export const TURN_RUNNING = 'a turn up to the fork point is still running; fork once it has finished';
|
|
14
|
+
export const NOTHING_BEFORE = 'nothing comes before the first turn, so there is nothing to fork';
|
|
15
|
+
export const AGENT_REQUIRED = 'a chat forked into another mesh needs an agent of that mesh';
|
|
16
|
+
export const FILE_ARRIVING = 'a file in this chat is still arriving; fork once it has landed';
|
|
17
|
+
export const FILE_NOT_HERE = 'a file in this chat is not on this device yet; open it here first';
|
|
18
|
+
export async function forkChat(meshes, input) {
|
|
19
|
+
const { into } = meshes;
|
|
20
|
+
if (meshes.from.compute || into.compute) {
|
|
21
|
+
throw new Error(COMPUTE_MESH);
|
|
22
|
+
}
|
|
23
|
+
if (!into.writable) {
|
|
24
|
+
throw new Error(NOT_A_WRITER);
|
|
25
|
+
}
|
|
26
|
+
const plan = await forkPlan(meshes, input);
|
|
27
|
+
const batch = into.batch();
|
|
28
|
+
batch.dispatch('put-chat', { chat: plan.chat });
|
|
29
|
+
for (const chunk of plan.chunks) {
|
|
30
|
+
batch.dispatch('append-chunk', chunk);
|
|
31
|
+
}
|
|
32
|
+
for (const knowledge of plan.knowledge) {
|
|
33
|
+
batch.dispatch('add-knowledge', { knowledge });
|
|
34
|
+
}
|
|
35
|
+
await batch.flush();
|
|
36
|
+
await into.fileIndex?.record(plan.chunks.filter((chunk) => chunk.attachment));
|
|
37
|
+
return enrichChat(into, plan.chat);
|
|
38
|
+
}
|
|
39
|
+
async function forkPlan(meshes, input) {
|
|
40
|
+
const snap = meshes.from.view.snapshot();
|
|
41
|
+
try {
|
|
42
|
+
const source = await snap.get(CHATS, { id: input.chatId });
|
|
43
|
+
if (!source || source.deletedAt) {
|
|
44
|
+
throw new Error(`chat not found: ${input.chatId}`);
|
|
45
|
+
}
|
|
46
|
+
const all = await chunksOf(snap, source.id);
|
|
47
|
+
const cut = forkCut(all, input);
|
|
48
|
+
const documents = await documentsOf(snap, { source, all, cut });
|
|
49
|
+
const ctx = contextOf(meshes, { cut, documents, originDeviceId: input.originDeviceId });
|
|
50
|
+
const agentId = await forkAgentId(ctx, { source, agentId: input.agentId });
|
|
51
|
+
const chunks = await forkedChunks(ctx, cut.chunks);
|
|
52
|
+
const knowledge = await forkedDocuments(ctx, documents);
|
|
53
|
+
const chat = await forkedChat(ctx, { source, cut, agentId, title: input.title, chunks });
|
|
54
|
+
return { chat, chunks, knowledge };
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
await snap.close();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function forkCut(chunks, input) {
|
|
61
|
+
const requests = chunks.filter((chunk) => chunk.type === 'run-request' && chunk.runId);
|
|
62
|
+
const finished = finishedRuns(chunks);
|
|
63
|
+
const end = input.runId ? endAt(requests, input) : settledEnd(requests, finished);
|
|
64
|
+
const included = requests.slice(0, end);
|
|
65
|
+
if (included.some((request) => !finished.has(request.runId ?? ''))) {
|
|
66
|
+
throw new Error(TURN_RUNNING);
|
|
67
|
+
}
|
|
68
|
+
const turns = new Set(requests.map((request) => request.runId));
|
|
69
|
+
const runs = new Set(included.map((request) => request.runId));
|
|
70
|
+
const next = requests[end];
|
|
71
|
+
const from = next ? turnStart(chunks, { next, included }) : Infinity;
|
|
72
|
+
const kept = [];
|
|
73
|
+
let before = Infinity;
|
|
74
|
+
for (const chunk of chunks) {
|
|
75
|
+
if (turns.has(chunk.runId) ? runs.has(chunk.runId) : chunk.seq < from) {
|
|
76
|
+
kept.push(chunk);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
before = Math.min(before, chunk.createdAt);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return { chunks: kept, before, runId: included.at(-1)?.runId ?? undefined };
|
|
83
|
+
}
|
|
84
|
+
function endAt(requests, { runId, exclusive }) {
|
|
85
|
+
const at = requests.findIndex((request) => request.runId === runId);
|
|
86
|
+
if (at === -1) {
|
|
87
|
+
throw new Error(NO_SUCH_TURN);
|
|
88
|
+
}
|
|
89
|
+
const end = exclusive ? at : at + 1;
|
|
90
|
+
if (end === 0) {
|
|
91
|
+
throw new Error(NOTHING_BEFORE);
|
|
92
|
+
}
|
|
93
|
+
return end;
|
|
94
|
+
}
|
|
95
|
+
function settledEnd(requests, finished) {
|
|
96
|
+
const running = requests.findIndex((request) => !finished.has(request.runId ?? ''));
|
|
97
|
+
if (running === 0) {
|
|
98
|
+
throw new Error(TURN_RUNNING);
|
|
99
|
+
}
|
|
100
|
+
return running === -1 ? requests.length : running;
|
|
101
|
+
}
|
|
102
|
+
function finishedRuns(chunks) {
|
|
103
|
+
const runs = new Set();
|
|
104
|
+
for (const chunk of chunks) {
|
|
105
|
+
if (chunk.runId && TERMINAL_RUN_STATES.has(chunk.runStatus?.state ?? '')) {
|
|
106
|
+
runs.add(chunk.runId);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return runs;
|
|
110
|
+
}
|
|
111
|
+
function turnStart(chunks, { next, included }) {
|
|
112
|
+
const messages = chunks.filter((chunk) => chunk.type === 'user-message' && !chunk.runId);
|
|
113
|
+
const message = messages.findLast((chunk) => chunk.seq < next.seq);
|
|
114
|
+
const answered = (included.at(-1)?.seq ?? -1) > (message?.seq ?? -1);
|
|
115
|
+
if (!message || answered) {
|
|
116
|
+
return next.seq;
|
|
117
|
+
}
|
|
118
|
+
const attachments = chunks.filter((chunk) => chunk.type === 'attachment');
|
|
119
|
+
const bound = groupAttachmentsByMessage(attachments, messages).get(message.id) ?? [];
|
|
120
|
+
return Math.min(message.seq, ...bound.map((chunk) => chunk.seq));
|
|
121
|
+
}
|
|
122
|
+
async function documentsOf(snap, { source, all, cut }) {
|
|
123
|
+
const inChat = { gte: { chatId: source.id }, lte: { chatId: source.id } };
|
|
124
|
+
const rows = await snap.find(KNOWLEDGE_BY_CHAT, inChat).toArray();
|
|
125
|
+
const attached = new Set(all.map(fileIdOf));
|
|
126
|
+
const carried = new Set(cut.chunks.map(fileIdOf));
|
|
127
|
+
return rows.filter((row) => attached.has(row.id) ? carried.has(row.id) : row.createdAt < cut.before);
|
|
128
|
+
}
|
|
129
|
+
function fileIdOf(chunk) {
|
|
130
|
+
return chunk.attachment?.id;
|
|
131
|
+
}
|
|
132
|
+
function contextOf(meshes, { cut, documents, originDeviceId }) {
|
|
133
|
+
const files = cut.chunks.map(fileIdOf).filter((id) => id !== undefined);
|
|
134
|
+
return {
|
|
135
|
+
...meshes,
|
|
136
|
+
same: b4a.equals(meshes.from.key, meshes.into.key),
|
|
137
|
+
chatId: randomId(),
|
|
138
|
+
kept: new Set(cut.chunks.map((chunk) => chunk.id)),
|
|
139
|
+
files: new Set([...files, ...documents.map((row) => row.id)]),
|
|
140
|
+
originDeviceId,
|
|
141
|
+
references: new Map()
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
async function forkAgentId(ctx, { source, agentId }) {
|
|
145
|
+
if (agentId) {
|
|
146
|
+
return (await mustGetActiveAgent(ctx.into, agentId)).id;
|
|
147
|
+
}
|
|
148
|
+
if (!ctx.same) {
|
|
149
|
+
throw new Error(AGENT_REQUIRED);
|
|
150
|
+
}
|
|
151
|
+
return source.agentId;
|
|
152
|
+
}
|
|
153
|
+
function forkedId(ctx, id) {
|
|
154
|
+
return derivedId(id, ctx.chatId);
|
|
155
|
+
}
|
|
156
|
+
async function forkedChunks(ctx, chunks) {
|
|
157
|
+
const forked = [];
|
|
158
|
+
for (const chunk of chunks) {
|
|
159
|
+
const through = chunk.compaction?.throughId;
|
|
160
|
+
if (through && !ctx.kept.has(through)) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
forked.push(await forkedChunk(ctx, chunk));
|
|
164
|
+
}
|
|
165
|
+
return forked;
|
|
166
|
+
}
|
|
167
|
+
async function forkedChunk(ctx, chunk) {
|
|
168
|
+
const { seq: _seq, updatedAt: _updatedAt, ...op } = stripped(chunk);
|
|
169
|
+
const forked = {
|
|
170
|
+
...op,
|
|
171
|
+
id: forkedId(ctx, chunk.id),
|
|
172
|
+
chatId: ctx.chatId,
|
|
173
|
+
runId: chunk.runId && forkedId(ctx, chunk.runId),
|
|
174
|
+
parentId: chunk.parentId && ctx.kept.has(chunk.parentId) ? forkedId(ctx, chunk.parentId) : null,
|
|
175
|
+
compaction: chunk.compaction && {
|
|
176
|
+
...chunk.compaction,
|
|
177
|
+
throughId: forkedId(ctx, chunk.compaction.throughId)
|
|
178
|
+
},
|
|
179
|
+
sources: chunk.sources && {
|
|
180
|
+
...chunk.sources,
|
|
181
|
+
sources: chunk.sources.sources.map((source) => forkedSource(ctx, source))
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
if (!chunk.attachment) {
|
|
185
|
+
return forked;
|
|
186
|
+
}
|
|
187
|
+
const blobId = await forkedReference(ctx, chunk.attachment);
|
|
188
|
+
const attachment = { ...chunk.attachment, id: forkedId(ctx, chunk.attachment.id), blobId };
|
|
189
|
+
return { ...forked, attachment, originDeviceId: ctx.originDeviceId };
|
|
190
|
+
}
|
|
191
|
+
function forkedSource(ctx, source) {
|
|
192
|
+
const file = (id) => (id && ctx.files.has(id) ? forkedId(ctx, id) : id);
|
|
193
|
+
return { ...source, documentId: file(source.documentId), attachmentId: file(source.attachmentId) };
|
|
194
|
+
}
|
|
195
|
+
async function forkedDocuments(ctx, rows) {
|
|
196
|
+
const forked = [];
|
|
197
|
+
for (const row of rows) {
|
|
198
|
+
const blobId = await forkedReference(ctx, row);
|
|
199
|
+
forked.push({ ...stripped(row), id: forkedId(ctx, row.id), chatId: ctx.chatId, blobId });
|
|
200
|
+
}
|
|
201
|
+
return forked;
|
|
202
|
+
}
|
|
203
|
+
async function forkedReference(ctx, file) {
|
|
204
|
+
if (file.deletedAt) {
|
|
205
|
+
return removedReference(file);
|
|
206
|
+
}
|
|
207
|
+
if (isPendingBlobReference(file.blobId)) {
|
|
208
|
+
throw new Error(FILE_ARRIVING);
|
|
209
|
+
}
|
|
210
|
+
const known = ctx.references.get(file.blobId);
|
|
211
|
+
if (known) {
|
|
212
|
+
return known;
|
|
213
|
+
}
|
|
214
|
+
const copied = await copyBlob(ctx.from, ctx.into, file.blobId);
|
|
215
|
+
if (!copied) {
|
|
216
|
+
throw new Error(FILE_NOT_HERE);
|
|
217
|
+
}
|
|
218
|
+
ctx.references.set(file.blobId, copied);
|
|
219
|
+
return copied;
|
|
220
|
+
}
|
|
221
|
+
async function forkedChat(ctx, input) {
|
|
222
|
+
const { source, cut } = input;
|
|
223
|
+
const now = Date.now();
|
|
224
|
+
const groupId = ctx.same ? source.groupId : undefined;
|
|
225
|
+
const { chats } = await listSidebar(ctx.into);
|
|
226
|
+
const siblings = chats.filter((chat) => (chat.groupId || undefined) === (groupId || undefined));
|
|
227
|
+
const top = siblings.length > 0 ? Math.max(...siblings.map(chatSortKey)) : undefined;
|
|
228
|
+
return {
|
|
229
|
+
...stripped(source),
|
|
230
|
+
id: ctx.chatId,
|
|
231
|
+
agentId: input.agentId,
|
|
232
|
+
title: input.title || source.title,
|
|
233
|
+
createdAt: now,
|
|
234
|
+
updatedAt: now,
|
|
235
|
+
deletedAt: undefined,
|
|
236
|
+
groupId,
|
|
237
|
+
rank: rankOnTop(top, now),
|
|
238
|
+
rankedAt: now,
|
|
239
|
+
readThrough: input.chunks.length || undefined,
|
|
240
|
+
forkedFrom: { meshId: b4a.toHex(ctx.from.key), chatId: source.id, runId: cut.runId }
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function chunksOf(snap, chatId) {
|
|
244
|
+
return snap.find(CHUNKS_BY_CHAT, { gte: { chatId }, lte: { chatId } }).toArray();
|
|
245
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
|
-
import crypto from 'hypercore-crypto';
|
|
3
2
|
import { AbortController } from 'bare-abort-controller';
|
|
4
3
|
import { AbortedError } from '@qvac/harness/race-abort';
|
|
5
4
|
import { getAgent, getDefaultAgent, mustGetActiveAgent } from './agent.js';
|
|
6
5
|
import { cancelActiveRuns, deleteChat, getChat, mustGetActiveChat } from './chat.js';
|
|
7
6
|
import { CHUNK_IN_ANOTHER_CHAT, copyMovedChunks, copyMovedKnowledge, movedBlob, stripped } from './chat-copy.js';
|
|
8
7
|
import { appendChunk, getChunk, listChunks, updateOp } from './chunk.js';
|
|
8
|
+
import { derivedId } from './id.js';
|
|
9
9
|
import { isPendingBlobReference } from './files/index.js';
|
|
10
10
|
import { errorMessage } from '../guards.js';
|
|
11
11
|
import { CHATS, DEVICES, KNOWLEDGE, KNOWLEDGE_BY_CHAT } from './collections.js';
|
|
@@ -516,9 +516,6 @@ export async function movedChatId(target, chatId) {
|
|
|
516
516
|
}
|
|
517
517
|
}
|
|
518
518
|
}
|
|
519
|
-
function derivedId(...parts) {
|
|
520
|
-
return b4a.toHex(crypto.hash(parts.map((part) => b4a.from(part))).subarray(0, 16));
|
|
521
|
-
}
|
|
522
519
|
function hex(key) {
|
|
523
520
|
return b4a.toHex(key);
|
|
524
521
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type HyperDB from 'hyperdb';
|
|
2
2
|
import type { QvacChat, QvacChatGroup } from '../../spec/mesh/hyperschema/types.d.ts';
|
|
3
3
|
import type { Mesh } from './mesh.ts';
|
|
4
|
+
export declare const TERMINAL_RUN_STATES: ReadonlySet<string>;
|
|
4
5
|
export interface ChatListing extends QvacChat {
|
|
5
6
|
readonly lastActivityAt: number;
|
|
6
7
|
}
|
|
@@ -8,7 +8,7 @@ import { place } from './rank.js';
|
|
|
8
8
|
import { chatSortKey } from './chat-order.js';
|
|
9
9
|
import b4a from 'b4a';
|
|
10
10
|
import { ACTIVE_CHATS, ACTIVE_CHATS_BY_AGENT, ACTIVE_CHAT_GROUPS, CHATS, CHAT_GROUPS, CHUNK_COUNTERS } from './collections.js';
|
|
11
|
-
const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
|
|
11
|
+
export const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
|
|
12
12
|
function byChatOrder(a, b) {
|
|
13
13
|
return chatSortKey(a) - chatSortKey(b) || (a.id < b.id ? -1 : 1);
|
|
14
14
|
}
|
|
@@ -70,3 +70,4 @@ export declare const MESH_INVITE_GRANTS = "@qvac/mesh-invite-grants";
|
|
|
70
70
|
export declare const MESH_FOUNDERS = "@qvac/mesh-founders";
|
|
71
71
|
export declare const MESH_KINDS = "@qvac/mesh-kinds";
|
|
72
72
|
export declare const MESH_NAMES = "@qvac/mesh-names";
|
|
73
|
+
export declare const MESH_SEEDS = "@qvac/mesh-seeds";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
|
|
2
|
-
declare const DROPPED: readonly ['@qvac/append-chunk', '@qvac/update-chunk', '@qvac/resolve-approval', '@qvac/put-agent', '@qvac/put-approval-rule', '@qvac/seed-defaults', '@qvac/add-knowledge', '@qvac/remove-knowledge', '@qvac/add-knowledge-artifact', '@qvac/claim-indexing', '@qvac/resolve-indexing', '@qvac/put-chat', '@qvac/put-tool-configs', '@qvac/append-tool-activity', '@qvac/migrate-agent-catalog', '@qvac/reindex-agents', '@qvac/put-chat-group', '@qvac/put-joined-mesh', '@qvac/purge-chat', '@qvac/name-mesh'];
|
|
2
|
+
declare const DROPPED: readonly ['@qvac/append-chunk', '@qvac/update-chunk', '@qvac/resolve-approval', '@qvac/put-agent', '@qvac/put-approval-rule', '@qvac/seed-defaults', '@qvac/add-knowledge', '@qvac/remove-knowledge', '@qvac/add-knowledge-artifact', '@qvac/claim-indexing', '@qvac/resolve-indexing', '@qvac/put-chat', '@qvac/put-tool-configs', '@qvac/append-tool-activity', '@qvac/migrate-agent-catalog', '@qvac/reindex-agents', '@qvac/put-chat-group', '@qvac/put-joined-mesh', '@qvac/purge-chat', '@qvac/name-mesh', '@qvac/put-mesh-seed'];
|
|
3
3
|
export type DroppedComputeRoute = (typeof DROPPED)[number];
|
|
4
4
|
export declare function droppedComputeRoutes(): ReadonlyArray<DroppedComputeRoute>;
|
|
5
5
|
export declare class ComputeRouteDropped extends Error {
|
|
@@ -13,6 +13,8 @@ import { registerJoinedMeshRoutes } from './joined-meshes.js';
|
|
|
13
13
|
import { registerMemberRoutes } from './members.js';
|
|
14
14
|
import { registerMeshKindRoutes } from './mesh-kind.js';
|
|
15
15
|
import { registerMeshNameRoutes } from './mesh-name.js';
|
|
16
|
+
import { registerMeshSeedRoutes } from './mesh-seeds.js';
|
|
17
|
+
import { registerOpenJoinRoutes } from './open-join.js';
|
|
16
18
|
import { registerSeedDefaultsRoutes } from './seed-defaults.js';
|
|
17
19
|
import { registerToolActivityRoutes } from './tool-activity.js';
|
|
18
20
|
import { registerToolConfigsRoutes } from './tool-configs.js';
|
|
@@ -22,7 +24,8 @@ const OPTIMISTIC_ROUTES = new Set([
|
|
|
22
24
|
'@qvac/claim-invite',
|
|
23
25
|
'@qvac/recover-device',
|
|
24
26
|
'@qvac/attest-device',
|
|
25
|
-
'@qvac/join-member'
|
|
27
|
+
'@qvac/join-member',
|
|
28
|
+
'@qvac/open-join'
|
|
26
29
|
]);
|
|
27
30
|
function writersOnly(router) {
|
|
28
31
|
return {
|
|
@@ -56,6 +59,8 @@ export function registerRoutes(target, { compute = false } = {}) {
|
|
|
56
59
|
registerInviteRoutes(router, { compute });
|
|
57
60
|
registerMeshKindRoutes(router, { compute });
|
|
58
61
|
registerMeshNameRoutes(router);
|
|
62
|
+
registerOpenJoinRoutes(router);
|
|
63
|
+
registerMeshSeedRoutes(router);
|
|
59
64
|
if (compute) {
|
|
60
65
|
registerComputeDrops(router);
|
|
61
66
|
}
|
|
@@ -1,17 +1,7 @@
|
|
|
1
1
|
import type { Router } from '../../../spec/mesh/hyperdispatch/index.js';
|
|
2
2
|
import type HyperDB from 'hyperdb';
|
|
3
|
-
import type { QvacDevice
|
|
4
|
-
export declare function memberStanding(tx: HyperDB, memberId: Buffer, grant: QvacMeshInviteGrant): Promise<'present' | 'removed' | null>;
|
|
5
|
-
interface InviteStandingOptions {
|
|
6
|
-
readonly device: QvacDevice | null;
|
|
7
|
-
readonly memberId: Buffer;
|
|
8
|
-
readonly grant: QvacMeshInviteGrant;
|
|
9
|
-
readonly compute?: boolean;
|
|
10
|
-
}
|
|
11
|
-
export declare function inviteStanding(tx: HyperDB, { device, memberId, grant, compute }: InviteStandingOptions): Promise<'present' | 'removed' | null>;
|
|
12
|
-
export declare function belongsToAnotherMember(tx: HyperDB, device: QvacDevice | null, memberId: Buffer): Promise<boolean>;
|
|
3
|
+
import type { QvacDevice } from '../../../spec/mesh/hyperschema/types.d.ts';
|
|
13
4
|
export declare function removedBy(tx: HyperDB, device: QvacDevice): Promise<'itself' | 'own-member' | 'another-member'>;
|
|
14
5
|
export declare function registerInviteRoutes(router: Router, { compute }?: {
|
|
15
6
|
compute?: boolean | undefined;
|
|
16
7
|
}): void;
|
|
17
|
-
export {};
|
|
@@ -24,7 +24,7 @@ async function kickedFromOutside(tx, device, memberId) {
|
|
|
24
24
|
const kickerMember = kicker.memberId ?? kicker.claimedMemberId;
|
|
25
25
|
return !kickerMember || !b4a.equals(kickerMember, memberId);
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
async function memberStanding(tx, memberId, grant) {
|
|
28
28
|
if (await removedSinceMint(tx, memberId, grant))
|
|
29
29
|
return 'removed';
|
|
30
30
|
for await (const device of tx.find(DEVICES)) {
|
|
@@ -37,7 +37,7 @@ export async function memberStanding(tx, memberId, grant) {
|
|
|
37
37
|
}
|
|
38
38
|
return null;
|
|
39
39
|
}
|
|
40
|
-
|
|
40
|
+
async function inviteStanding(tx, { device, memberId, grant, compute = false }) {
|
|
41
41
|
const standing = await memberStanding(tx, memberId, grant);
|
|
42
42
|
if (standing !== 'present') {
|
|
43
43
|
return standing;
|
|
@@ -50,7 +50,7 @@ export async function inviteStanding(tx, { device, memberId, grant, compute = fa
|
|
|
50
50
|
}
|
|
51
51
|
return (await removedBy(tx, device)) === 'own-member' ? 'present' : null;
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
async function belongsToAnotherMember(tx, device, memberId) {
|
|
54
54
|
if (!device?.memberId || b4a.equals(device.memberId, memberId))
|
|
55
55
|
return false;
|
|
56
56
|
return !device.revoked || (await removedBy(tx, device)) === 'another-member';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { liveDeviceForWriter, trackActivity } from './activity.js';
|
|
2
|
+
import b4a from 'b4a';
|
|
3
|
+
import { MESH_SEEDS } from '../collections.js';
|
|
4
|
+
import { meshKind } from './mesh-kind.js';
|
|
5
|
+
export function registerMeshSeedRoutes(router) {
|
|
6
|
+
router.add('@qvac/put-mesh-seed', trackActivity(async ({ meshKey, seed }, { tx, node, log }) => {
|
|
7
|
+
const ids = { meshKey: b4a.toHex(meshKey).slice(0, 8) };
|
|
8
|
+
if ((await meshKind(tx)) === 'shared') {
|
|
9
|
+
log?.warn('contract', 'put-mesh-seed dropped: seeds live in the account alone', ids);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const author = await liveDeviceForWriter(tx, node.key);
|
|
13
|
+
if (!author?.memberId) {
|
|
14
|
+
log?.warn('contract', 'put-mesh-seed dropped: the author speaks for no member here', ids);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const held = await tx.get(MESH_SEEDS, { meshKey });
|
|
18
|
+
if (held && b4a.equals(held.seed, seed)) {
|
|
19
|
+
log?.debug('contract', 'put-mesh-seed dropped: the account already holds it', ids);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const row = { meshKey, seed };
|
|
23
|
+
await tx.insert(MESH_SEEDS, row);
|
|
24
|
+
return row;
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import crypto from 'hypercore-crypto';
|
|
2
|
+
import { trackActivity } from './activity.js';
|
|
3
|
+
import { linkMember, provenMember } from './members.js';
|
|
4
|
+
import { admitDevice } from './writers.js';
|
|
5
|
+
import b4a from 'b4a';
|
|
6
|
+
import { DEVICES } from '../collections.js';
|
|
7
|
+
export function registerOpenJoinRoutes(router) {
|
|
8
|
+
router.add('@qvac/open-join', trackActivity(async (payload, context) => {
|
|
9
|
+
const { node, host, meshPublicKey, log } = context;
|
|
10
|
+
let valid = false;
|
|
11
|
+
try {
|
|
12
|
+
valid = crypto.verify(node.key, payload.signature, meshPublicKey);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
valid = false;
|
|
16
|
+
}
|
|
17
|
+
if (!valid) {
|
|
18
|
+
log?.warn('contract', 'open-join dropped: invalid writer-claim signature');
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const id = payload.deviceId;
|
|
22
|
+
if (!id) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const admission = await admitDevice(context, payload.device, {
|
|
26
|
+
id,
|
|
27
|
+
writerKey: node.key,
|
|
28
|
+
claim: { deviceSignature: payload.deviceSignature }
|
|
29
|
+
});
|
|
30
|
+
if (admission !== 'admitted') {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
await host.addWriter(node.key);
|
|
34
|
+
await linkClaimedMember(payload, context, id);
|
|
35
|
+
return payload.device;
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
async function linkClaimedMember({ member, memberClaim }, { tx, node, meshPublicKey, log }, deviceId) {
|
|
39
|
+
if (!member || !memberClaim)
|
|
40
|
+
return;
|
|
41
|
+
const proof = await provenMember(meshPublicKey, {
|
|
42
|
+
deviceId,
|
|
43
|
+
writerKey: node.key,
|
|
44
|
+
memberId: member.id,
|
|
45
|
+
claim: memberClaim
|
|
46
|
+
});
|
|
47
|
+
if (!proof) {
|
|
48
|
+
log?.warn('contract', 'open-join: the member claim does not bind this writer', {
|
|
49
|
+
device: b4a.toHex(deviceId).slice(0, 16)
|
|
50
|
+
});
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const admitted = await tx.get(DEVICES, { id: deviceId });
|
|
54
|
+
if (admitted) {
|
|
55
|
+
await linkMember(tx, admitted, { member, proof });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -3,7 +3,7 @@ import type Hyperswarm from 'hyperswarm';
|
|
|
3
3
|
import type Wakeup from 'protomux-wakeup';
|
|
4
4
|
import type { Logger } from '../../index.ts';
|
|
5
5
|
import type { LocalMeshDeparture } from '../../spec/local/hyperschema/types.d.ts';
|
|
6
|
-
import type { BlindPeeringOpts } from './blind-peers.ts';
|
|
6
|
+
import type { BlindPeeringOpts, StartBlindPeeringOpts } from './blind-peers.ts';
|
|
7
7
|
interface DepartureRows {
|
|
8
8
|
departures(): Promise<LocalMeshDeparture[]>;
|
|
9
9
|
deleteDeparture(key: Buffer): Promise<void>;
|
|
@@ -11,9 +11,11 @@ interface DepartureRows {
|
|
|
11
11
|
export interface PendingDeparturesOptions {
|
|
12
12
|
swarm: Hyperswarm;
|
|
13
13
|
isPooled: (key: Buffer) => boolean;
|
|
14
|
+
deviceId: () => Buffer | null;
|
|
14
15
|
logger?: Logger | null;
|
|
15
16
|
blindPeerKeys?: ReadonlyArray<Buffer>;
|
|
16
17
|
blindPeeringOpts?: BlindPeeringOpts;
|
|
18
|
+
blindPeering?: boolean;
|
|
17
19
|
wakeup?: Wakeup;
|
|
18
20
|
}
|
|
19
21
|
export declare class PendingDepartures {
|
|
@@ -25,17 +27,22 @@ export declare class PendingDepartures {
|
|
|
25
27
|
private readonly _blindPeeringOpts;
|
|
26
28
|
private readonly _wakeup;
|
|
27
29
|
private readonly _isPooled;
|
|
30
|
+
private readonly _deviceId;
|
|
28
31
|
private readonly _active;
|
|
29
32
|
private readonly _parked;
|
|
30
33
|
private readonly _lock;
|
|
31
34
|
private _closing;
|
|
35
|
+
private _blindPeering;
|
|
32
36
|
constructor(store: Corestore, local: DepartureRows, opts: PendingDeparturesOptions);
|
|
33
37
|
flush(): void;
|
|
34
38
|
private _flush;
|
|
35
39
|
private _deliver;
|
|
40
|
+
private _announce;
|
|
36
41
|
park(key: Buffer): Promise<void>;
|
|
37
42
|
unpark(key: Buffer): void;
|
|
38
43
|
drop(key: Buffer): Promise<void>;
|
|
44
|
+
startBlindPeering(opts?: StartBlindPeeringOpts): void;
|
|
45
|
+
stopBlindPeering(): Promise<void>;
|
|
39
46
|
suspend(): Promise<void>;
|
|
40
47
|
resume(): Promise<void>;
|
|
41
48
|
close(): Promise<void>;
|
|
@@ -11,19 +11,23 @@ export class PendingDepartures {
|
|
|
11
11
|
_blindPeeringOpts;
|
|
12
12
|
_wakeup;
|
|
13
13
|
_isPooled;
|
|
14
|
+
_deviceId;
|
|
14
15
|
_active = new Map();
|
|
15
16
|
_parked = new Set();
|
|
16
17
|
_lock = new ScopeLock();
|
|
17
18
|
_closing = false;
|
|
19
|
+
_blindPeering;
|
|
18
20
|
constructor(store, local, opts) {
|
|
19
21
|
this._store = store;
|
|
20
22
|
this._local = local;
|
|
21
23
|
this._swarm = opts.swarm;
|
|
22
24
|
this._isPooled = opts.isPooled;
|
|
25
|
+
this._deviceId = opts.deviceId;
|
|
23
26
|
this._log = opts.logger ?? null;
|
|
24
27
|
this._blindPeerKeys = opts.blindPeerKeys ?? [];
|
|
25
28
|
this._blindPeeringOpts = opts.blindPeeringOpts;
|
|
26
29
|
this._wakeup = opts.wakeup;
|
|
30
|
+
this._blindPeering = opts.blindPeering ?? true;
|
|
27
31
|
}
|
|
28
32
|
flush() {
|
|
29
33
|
void this._flush().catch((err) => this._log?.warn(SCOPE, 'departure flush failed', err));
|
|
@@ -45,6 +49,7 @@ export class PendingDepartures {
|
|
|
45
49
|
seed: row.seed,
|
|
46
50
|
key: row.creator ? null : row.key,
|
|
47
51
|
writerSeed: row.writerSeed ?? undefined,
|
|
52
|
+
deviceId: this._deviceId() ?? undefined,
|
|
48
53
|
logger: this._log,
|
|
49
54
|
blindPeerKeys: this._blindPeerKeys,
|
|
50
55
|
blindPeeringOpts: this._blindPeeringOpts,
|
|
@@ -68,9 +73,12 @@ export class PendingDepartures {
|
|
|
68
73
|
return;
|
|
69
74
|
}
|
|
70
75
|
this._swarm.join(mesh.discoveryKey, { server: true, client: true });
|
|
71
|
-
|
|
76
|
+
if (this._blindPeering) {
|
|
77
|
+
mesh.startBlindPeering(this._swarm.dht);
|
|
78
|
+
}
|
|
72
79
|
this._log?.info(scope, 'reopened a departed mesh to deliver its removal');
|
|
73
|
-
|
|
80
|
+
const announced = await this._announce(mesh);
|
|
81
|
+
if (announced === 'closed' || (announced === 'sent' && !(await waitForDelivery(mesh)))) {
|
|
74
82
|
return;
|
|
75
83
|
}
|
|
76
84
|
await this._local.deleteDeparture(row.key);
|
|
@@ -81,6 +89,23 @@ export class PendingDepartures {
|
|
|
81
89
|
await mesh.close().catch(noop);
|
|
82
90
|
}
|
|
83
91
|
}
|
|
92
|
+
async _announce(mesh) {
|
|
93
|
+
if (mesh.localLength === 0)
|
|
94
|
+
return 'moot';
|
|
95
|
+
const deviceId = this._deviceId();
|
|
96
|
+
if (!deviceId || (await mesh.checkResumedRevocation())) {
|
|
97
|
+
return 'sent';
|
|
98
|
+
}
|
|
99
|
+
if (!(await admitted(mesh))) {
|
|
100
|
+
return mesh.closing ? 'closed' : 'moot';
|
|
101
|
+
}
|
|
102
|
+
await mesh.dispatch('remove-writer', {
|
|
103
|
+
writerKey: mesh.localWriterKey,
|
|
104
|
+
deviceId,
|
|
105
|
+
revokedAt: Date.now()
|
|
106
|
+
});
|
|
107
|
+
return 'sent';
|
|
108
|
+
}
|
|
84
109
|
async park(key) {
|
|
85
110
|
const hex = b4a.toHex(key);
|
|
86
111
|
await this._lock.lock();
|
|
@@ -103,6 +128,16 @@ export class PendingDepartures {
|
|
|
103
128
|
drop(key) {
|
|
104
129
|
return this._local.deleteDeparture(key);
|
|
105
130
|
}
|
|
131
|
+
startBlindPeering(opts = {}) {
|
|
132
|
+
this._blindPeering = true;
|
|
133
|
+
for (const { mesh } of this._active.values()) {
|
|
134
|
+
mesh.startBlindPeering(this._swarm.dht, opts);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async stopBlindPeering() {
|
|
138
|
+
this._blindPeering = false;
|
|
139
|
+
await Promise.all([...this._active.values()].map(({ mesh }) => mesh.stopBlindPeering()));
|
|
140
|
+
}
|
|
106
141
|
async suspend() {
|
|
107
142
|
for (const { mesh } of this._active.values()) {
|
|
108
143
|
await mesh.suspend();
|
|
@@ -121,6 +156,28 @@ export class PendingDepartures {
|
|
|
121
156
|
}));
|
|
122
157
|
}
|
|
123
158
|
}
|
|
159
|
+
async function admitted(mesh) {
|
|
160
|
+
if (mesh.writable)
|
|
161
|
+
return true;
|
|
162
|
+
if (await mesh.ownWriterRefused())
|
|
163
|
+
return false;
|
|
164
|
+
return new Promise((resolve) => {
|
|
165
|
+
const settle = (ok) => {
|
|
166
|
+
mesh.autobee.off('writable', onWritable);
|
|
167
|
+
mesh.syncActivity.unwatch(check);
|
|
168
|
+
mesh.off('close', onClose);
|
|
169
|
+
resolve(ok);
|
|
170
|
+
};
|
|
171
|
+
const onWritable = () => settle(true);
|
|
172
|
+
const onClose = () => settle(false);
|
|
173
|
+
const check = () => {
|
|
174
|
+
void mesh.ownWriterRefused().then((refused) => refused && settle(false), noop);
|
|
175
|
+
};
|
|
176
|
+
mesh.autobee.on('writable', onWritable);
|
|
177
|
+
mesh.syncActivity.watch(check);
|
|
178
|
+
mesh.on('close', onClose);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
124
181
|
function waitForDelivery(mesh) {
|
|
125
182
|
const local = mesh.autobee.local;
|
|
126
183
|
let uploaded = 0;
|