@qvac/core 1.0.0-rc.12 → 1.0.0-rc.13
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 +19 -1
- package/dist-lib/lib/assistant.js +2 -1
- package/dist-lib/lib/core/archived-mesh.js +7 -3
- package/dist-lib/lib/core/chat-copy.d.ts +18 -3
- package/dist-lib/lib/core/chat-copy.js +73 -23
- package/dist-lib/lib/core/chat-group.js +14 -9
- 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.js +21 -8
- 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/activity.js +2 -2
- 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/indexing.js +4 -2
- package/dist-lib/lib/core/contract/knowledge.js +5 -4
- package/dist-lib/lib/core/contract/members.js +2 -2
- package/dist-lib/lib/core/contract/projects.d.ts +5 -0
- package/dist-lib/lib/core/contract/projects.js +12 -4
- package/dist-lib/lib/core/contract/roles.js +30 -4
- package/dist-lib/lib/core/contract/run-requests.js +2 -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/files/attachments.js +4 -1
- package/dist-lib/lib/core/local/index.d.ts +6 -2
- package/dist-lib/lib/core/local/index.js +12 -3
- package/dist-lib/lib/core/mesh.d.ts +5 -0
- package/dist-lib/lib/core/mesh.js +3 -0
- package/dist-lib/lib/core/migrations/workspace-from-0-21/index.d.ts +85 -0
- package/dist-lib/lib/core/migrations/workspace-from-0-21/index.js +690 -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 +2 -0
- package/dist-lib/lib/core/public-mesh.js +5 -0
- 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 +27 -2
- package/dist-lib/lib/core/workspace-upgrade.d.ts +6 -0
- package/dist-lib/lib/core/workspace-upgrade.js +26 -16
- package/dist-lib/lib/core.d.ts +4 -32
- package/dist-lib/lib/core.js +117 -469
- package/dist-lib/lib/run-transcript.d.ts +2 -1
- package/dist-lib/lib/run-transcript.js +11 -5
- package/dist-lib/schema/qvac.js +26 -1
- package/dist-lib/spec/mesh/hyperdb/db.json +15 -0
- package/dist-lib/spec/mesh/hyperdb/index.js +70 -1
- package/dist-lib/spec/mesh/hyperdb/messages.js +806 -438
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +18 -0
- package/dist-lib/spec/mesh/hyperdispatch/index.js +52 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +303 -3
- package/dist-lib/spec/mesh/hyperschema/index.js +303 -3
- package/dist-lib/spec/mesh/hyperschema/schema.json +146 -0
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +39 -0
- package/package.json +6 -4
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
import b4a from 'b4a';
|
|
2
|
+
import crypto from 'hypercore-crypto';
|
|
3
|
+
import { projectName } from '../../../api/projects.js';
|
|
4
|
+
import { cancelActiveRuns } from '../../chat.js';
|
|
5
|
+
import { listChunks } from '../../chunk.js';
|
|
6
|
+
import { copyContent, isCopiedFields, stripped, UNCOPIED } from '../../chat-copy.js';
|
|
7
|
+
import { CHAT_MOVED } from '../../chat-move.js';
|
|
8
|
+
import { CHATS, CHUNKS, CHUNKS_BY_CHAT, JOINED_MESHES, KNOWLEDGE, KNOWLEDGE_BY_CHAT, MOVED_OUT_CHATS, PROJECTS, TOOL_ACTIVITY } from '../../collections.js';
|
|
9
|
+
import { FEATURES, isFeatureSupported } from '../../contract/features.js';
|
|
10
|
+
import { meshKind } from '../../contract/mesh-kind.js';
|
|
11
|
+
import { bornUnderRules } from '../../contract/mesh-rules.js';
|
|
12
|
+
import { bornMembers, inProject, listedProject, listsMember, projectOfGroup, readProject, UNGROUPED } from '../../contract/projects.js';
|
|
13
|
+
import { countedMembers, isWorkspace, liveMemberIds } from '../../contract/workspace.js';
|
|
14
|
+
import { openLocalStore, recordedRole } from '../../local/index.js';
|
|
15
|
+
import { oldChats, ourRows } from '../../public-mesh.js';
|
|
16
|
+
import { foldAgents, ownCopies } from '../../upgrade-agents.js';
|
|
17
|
+
import { FOLD_PENDING, GROUPS_PENDING, editedChats, historyEditsOf, touches, agentsFoldKey, groupBuckets, groupRecordKey, groupRecordsOf, isGroupRecord, liveGroups, projectsByGroup, upgradeCheckedKey } from '../../workspace-upgrade.js';
|
|
18
|
+
const REMOVAL_BATCH = 256;
|
|
19
|
+
export class WorkspaceUpgrade {
|
|
20
|
+
_host;
|
|
21
|
+
_upgrading = new Set();
|
|
22
|
+
_groupPasses = new WeakSet();
|
|
23
|
+
_groupPassesDue = new WeakSet();
|
|
24
|
+
_publicMigration = null;
|
|
25
|
+
_copyAgain = false;
|
|
26
|
+
_accountCopied = null;
|
|
27
|
+
constructor(host) {
|
|
28
|
+
this._host = host;
|
|
29
|
+
}
|
|
30
|
+
get accountCopy() {
|
|
31
|
+
return this._publicMigration;
|
|
32
|
+
}
|
|
33
|
+
get accountUnread() {
|
|
34
|
+
return this._accountCopied === null;
|
|
35
|
+
}
|
|
36
|
+
async followShared(mesh) {
|
|
37
|
+
const meshId = this._host.meshIdOf(mesh);
|
|
38
|
+
if (this._host.closing || this._upgrading.has(meshId)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
this._upgrading.add(meshId);
|
|
42
|
+
try {
|
|
43
|
+
let kind = await meshKind(mesh.view);
|
|
44
|
+
if (kind === 'shared' && (await this._upgradable(mesh))) {
|
|
45
|
+
await mesh.dispatch('upgrade-to-workspace', { upgradedAt: Date.now() });
|
|
46
|
+
kind = await meshKind(mesh.view);
|
|
47
|
+
}
|
|
48
|
+
if (!this._host.isPooled(mesh)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (kind === 'workspace' && mesh.writable) {
|
|
52
|
+
await this._host.local.set(upgradeCheckedKey(mesh.key), GROUPS_PENDING);
|
|
53
|
+
await this._markFoldDue(mesh.key);
|
|
54
|
+
await this._host.reopenInRole(mesh, 'workspace');
|
|
55
|
+
await this._markJoinedWorkspace(mesh.key);
|
|
56
|
+
}
|
|
57
|
+
else if (kind !== 'project' && (kind !== 'shared' || (await bornUnderRules(mesh.view)))) {
|
|
58
|
+
await this._markUpgradeChecked(mesh.key);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
if (!this._host.closing && !mesh.closing) {
|
|
63
|
+
this._host.log?.warn(['p2p', 'mesh'], 'upgrading a shared mesh to a workspace failed', err);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
this._upgrading.delete(meshId);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
follow(workspace) {
|
|
71
|
+
if (this._host.closing || !workspace.writable) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (this._groupPasses.has(workspace)) {
|
|
75
|
+
this._groupPassesDue.add(workspace);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this._groupPasses.add(workspace);
|
|
79
|
+
void this._followPours(workspace)
|
|
80
|
+
.then(() => this._passGroups(workspace))
|
|
81
|
+
.catch((err) => {
|
|
82
|
+
if (!this._host.closing && this._host.isPooled(workspace)) {
|
|
83
|
+
this._host.log?.warn(['p2p', 'mesh'], 'moving chat-groups into projects failed', err);
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
.finally(() => {
|
|
87
|
+
this._groupPasses.delete(workspace);
|
|
88
|
+
if (this._groupPassesDue.delete(workspace)) {
|
|
89
|
+
this.follow(workspace);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
async _passGroups(workspace) {
|
|
94
|
+
const checked = await this._host.local.get(upgradeCheckedKey(workspace.key));
|
|
95
|
+
const prefix = historyEditsOf(workspace.key);
|
|
96
|
+
const entries = await this._host.local.listPrefix(prefix);
|
|
97
|
+
const edits = entries.map(({ key }) => key);
|
|
98
|
+
if (checked === true && edits.length === 0) {
|
|
99
|
+
return this._passAgents(workspace);
|
|
100
|
+
}
|
|
101
|
+
if (checked !== GROUPS_PENDING && checked !== true) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const ours = workspace.ourMemberId ?? (await workspace.refreshOurMember())?.memberId;
|
|
105
|
+
if (!ours || !(await isWorkspace(workspace.view))) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const buckets = await this._groupBuckets(workspace, edits.length > 0);
|
|
109
|
+
const rows = await projectsByGroup(workspace.view);
|
|
110
|
+
const edited = editedChats(prefix, edits);
|
|
111
|
+
const passes = await Promise.allSettled(buckets.map((bucket) => this._passGroup(workspace, ours, { bucket, row: rows.get(bucket.groupId) ?? null, edited })));
|
|
112
|
+
const settled = [];
|
|
113
|
+
for (const pass of passes) {
|
|
114
|
+
if (pass.status === 'rejected') {
|
|
115
|
+
throw pass.reason;
|
|
116
|
+
}
|
|
117
|
+
settled.push(pass.value);
|
|
118
|
+
}
|
|
119
|
+
await this._host.local.change(async (get) => {
|
|
120
|
+
const current = await Promise.all(entries.map(({ key }) => get(key)));
|
|
121
|
+
const carried = entries.filter(({ value }, i) => current[i] === value);
|
|
122
|
+
return { result: undefined, set: [], delete: carried.map(({ key }) => key) };
|
|
123
|
+
});
|
|
124
|
+
if (settled.every((state) => state === 'done') && this._host.isPooled(workspace)) {
|
|
125
|
+
await this._host.local.set(upgradeCheckedKey(workspace.key), true);
|
|
126
|
+
}
|
|
127
|
+
if (!settled.includes('pending')) {
|
|
128
|
+
await this._passAgents(workspace);
|
|
129
|
+
}
|
|
130
|
+
await this._rehome(workspace, buckets);
|
|
131
|
+
}
|
|
132
|
+
async _rehome(workspace, buckets) {
|
|
133
|
+
if (buckets.every((bucket) => bucket.chats.size === 0)) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const ours = workspace.ourMemberId;
|
|
137
|
+
const held = new Map();
|
|
138
|
+
for (const row of (await projectsByGroup(workspace.view)).values()) {
|
|
139
|
+
const project = !row.deletedAt && ours && listedProject(row, ours);
|
|
140
|
+
const mesh = project ? await this._host.heldMesh(row.id) : null;
|
|
141
|
+
if (mesh?.writable && row.groupId) {
|
|
142
|
+
held.set(row.groupId, mesh);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
for (const { groupId, chats } of buckets) {
|
|
146
|
+
const home = held.get(groupId);
|
|
147
|
+
const stale = [...held.values()].filter((mesh) => mesh !== home);
|
|
148
|
+
if (home) {
|
|
149
|
+
await this._rehomeInto({ home, stale, chats });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async _rehomeInto({ home, stale, chats }) {
|
|
154
|
+
for (const chatId of chats) {
|
|
155
|
+
for (const from of stale) {
|
|
156
|
+
await this._carryHome(from, home, chatId);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async _carryHome(from, home, chatId) {
|
|
161
|
+
const copy = await from.view.get(CHATS, { id: chatId });
|
|
162
|
+
const [late] = copy ? [] : await listChunks(from, { chatId, limit: 1 });
|
|
163
|
+
if (copy?.deletedAt || (!copy && !late)) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const held = await home.view.get(CHATS, { id: chatId });
|
|
167
|
+
if (held && !held.deletedAt) {
|
|
168
|
+
await home.dispatch('patch-chat', { chat: { ...stripped(held), updatedAt: Date.now() } });
|
|
169
|
+
}
|
|
170
|
+
const deviceId = this._host.requireDeviceKeyPair().publicKey;
|
|
171
|
+
await cancelActiveRuns(from, chatId, { deviceId, reason: CHAT_MOVED });
|
|
172
|
+
await copyContent({ from, into: home, deviceId, chats: new Set([chatId]), historyOnly: !copy });
|
|
173
|
+
const landed = await home.view.get(CHATS, { id: chatId });
|
|
174
|
+
if (landed && !landed.deletedAt) {
|
|
175
|
+
await this._removeCarried(from, home, { chatId, updatedAt: copy?.updatedAt ?? 0 });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async _removeCarried(from, home, read) {
|
|
179
|
+
const { chatId } = read;
|
|
180
|
+
const holds = async (collection, id) => !!(await home.view.get(collection, { id }));
|
|
181
|
+
const chunkIds = [];
|
|
182
|
+
for (const chunk of await from.view.find(CHUNKS_BY_CHAT, inChat(chatId)).toArray()) {
|
|
183
|
+
if (UNCOPIED.has(chunk.type) || (await holds(CHUNKS, chunk.id))) {
|
|
184
|
+
chunkIds.push(chunk.id);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const knowledgeIds = [];
|
|
188
|
+
for (const row of await from.view
|
|
189
|
+
.find(KNOWLEDGE_BY_CHAT, inChat(chatId))
|
|
190
|
+
.toArray()) {
|
|
191
|
+
if (await holds(KNOWLEDGE, row.id)) {
|
|
192
|
+
knowledgeIds.push(row.id);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const toolActivityIds = [];
|
|
196
|
+
for (const row of await from.view.find(TOOL_ACTIVITY).toArray()) {
|
|
197
|
+
if (row.chatId === chatId && (await holds(TOOL_ACTIVITY, row.id))) {
|
|
198
|
+
toolActivityIds.push(row.id);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
let named = { ...read, knowledgeIds, toolActivityIds };
|
|
202
|
+
for (let at = 0; at === 0 || at < chunkIds.length; at += REMOVAL_BATCH) {
|
|
203
|
+
await from.dispatch('remove-chat-copy', {
|
|
204
|
+
...named,
|
|
205
|
+
chunkIds: chunkIds.slice(at, at + REMOVAL_BATCH)
|
|
206
|
+
});
|
|
207
|
+
named = { ...read, knowledgeIds: [], toolActivityIds: [] };
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async _groupBuckets(workspace, edited) {
|
|
211
|
+
const named = workspace.name?.name?.trim() || 'Workspace';
|
|
212
|
+
const groups = await liveGroups(workspace.view);
|
|
213
|
+
const ids = [...groups.map((group) => group.id), UNGROUPED];
|
|
214
|
+
const records = await Promise.all(ids.map((id) => this._groupRecord(workspace, id)));
|
|
215
|
+
if (edited || !records.every((record) => record.copied || record.done)) {
|
|
216
|
+
return this._scanBuckets(workspace, named);
|
|
217
|
+
}
|
|
218
|
+
return ids.map((groupId, idx) => ({
|
|
219
|
+
groupId,
|
|
220
|
+
name: groups[idx]?.name ?? named,
|
|
221
|
+
chats: new Set()
|
|
222
|
+
}));
|
|
223
|
+
}
|
|
224
|
+
async _markFoldDue(workspaceKey) {
|
|
225
|
+
const key = agentsFoldKey(workspaceKey);
|
|
226
|
+
if ((await this._host.local.get(key)) !== true) {
|
|
227
|
+
await this._host.local.set(key, FOLD_PENDING);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
async _passAgents(workspace) {
|
|
231
|
+
if ((await this._host.local.get(agentsFoldKey(workspace.key))) !== FOLD_PENDING) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const copies = await ownCopies(workspace, this._host.requireDeviceKeyPair().publicKey);
|
|
235
|
+
if (copies.length > 0) {
|
|
236
|
+
const account = await this._host.writableAccount();
|
|
237
|
+
const projects = await this._heldProjects(workspace);
|
|
238
|
+
if (!account || !projects) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
await foldAgents({ account, workspace, projects, picks: this._host.agentPicks() }, copies);
|
|
242
|
+
}
|
|
243
|
+
if (this._host.isPooled(workspace)) {
|
|
244
|
+
await this._host.local.set(agentsFoldKey(workspace.key), true);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async _heldProjects(workspace) {
|
|
248
|
+
const ours = workspace.ourMemberId;
|
|
249
|
+
const held = [];
|
|
250
|
+
for (const row of await workspace.view.find(PROJECTS).toArray()) {
|
|
251
|
+
if (row.deletedAt || !ours || !listedProject(row, ours))
|
|
252
|
+
continue;
|
|
253
|
+
const project = await this._host.heldMesh(row.id);
|
|
254
|
+
if (!project?.writable) {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
held.push(project);
|
|
258
|
+
}
|
|
259
|
+
return held;
|
|
260
|
+
}
|
|
261
|
+
async _groupRecord(workspace, groupId) {
|
|
262
|
+
const stored = await this._host.local.get(groupRecordKey(workspace.key, groupId));
|
|
263
|
+
return isGroupRecord(stored) ? stored : {};
|
|
264
|
+
}
|
|
265
|
+
async _scanBuckets(workspace, named) {
|
|
266
|
+
const buckets = await groupBuckets(workspace.view, named);
|
|
267
|
+
if (buckets.some((bucket) => bucket.groupId === UNGROUPED)) {
|
|
268
|
+
return buckets;
|
|
269
|
+
}
|
|
270
|
+
const record = await this._groupRecord(workspace, UNGROUPED);
|
|
271
|
+
if (!record.key) {
|
|
272
|
+
await this._host.local.set(groupRecordKey(workspace.key, UNGROUPED), { done: true });
|
|
273
|
+
return buckets;
|
|
274
|
+
}
|
|
275
|
+
const emptied = { groupId: UNGROUPED, name: named, chats: new Set() };
|
|
276
|
+
return record.done ? buckets : [...buckets, emptied];
|
|
277
|
+
}
|
|
278
|
+
async _passGroup(workspace, ours, { bucket, row: listed, edited }) {
|
|
279
|
+
const recordKey = groupRecordKey(workspace.key, bucket.groupId);
|
|
280
|
+
let record = await this._groupRecord(workspace, bucket.groupId);
|
|
281
|
+
if (touches(bucket, edited) && (record.copied || record.done)) {
|
|
282
|
+
record = { key: record.key, listedMembers: record.listedMembers, into: record.into };
|
|
283
|
+
await this._host.local.set(recordKey, record);
|
|
284
|
+
await this._host.local.set(upgradeCheckedKey(workspace.key), GROUPS_PENDING);
|
|
285
|
+
}
|
|
286
|
+
if (record.done) {
|
|
287
|
+
return 'done';
|
|
288
|
+
}
|
|
289
|
+
let row = listed;
|
|
290
|
+
if (!row) {
|
|
291
|
+
record = await this._foundGroupProject(workspace, bucket, { recordKey, record });
|
|
292
|
+
row = await projectOfGroup(workspace.view, bucket.groupId);
|
|
293
|
+
}
|
|
294
|
+
const live = !!row && !row.deletedAt;
|
|
295
|
+
if (row && live && record.key === b4a.toHex(row.id)) {
|
|
296
|
+
const project = record.copied ? null : await this._host.heldMesh(row.id);
|
|
297
|
+
if (project) {
|
|
298
|
+
await this._copyGroup(workspace, project, bucket);
|
|
299
|
+
record = { ...record, copied: true };
|
|
300
|
+
await this._host.local.set(recordKey, record);
|
|
301
|
+
}
|
|
302
|
+
record = await this._settleListed(workspace, { row, recordKey, record });
|
|
303
|
+
await this._listNewMembers(workspace, { row, recordKey, record });
|
|
304
|
+
return 'won';
|
|
305
|
+
}
|
|
306
|
+
if (row && live && !(await this._mergeGroupInto(workspace, ours, { row, bucket, record }))) {
|
|
307
|
+
return 'pending';
|
|
308
|
+
}
|
|
309
|
+
if (record.key) {
|
|
310
|
+
await this._host.purgeProject(b4a.from(record.key, 'hex'));
|
|
311
|
+
}
|
|
312
|
+
const lost = record.into && !(await readProject(workspace.view, b4a.from(record.into, 'hex')));
|
|
313
|
+
const merged = row && live ? b4a.toHex(row.id) : undefined;
|
|
314
|
+
const into = lost ? record.into : merged;
|
|
315
|
+
await this._host.local.set(recordKey, { ...record, done: true, into });
|
|
316
|
+
return 'done';
|
|
317
|
+
}
|
|
318
|
+
async _followPours(workspace) {
|
|
319
|
+
const prefix = groupRecordsOf(workspace.key);
|
|
320
|
+
for (const { key, value } of await this._host.local.listPrefix(prefix)) {
|
|
321
|
+
const record = isGroupRecord(value) ? value : {};
|
|
322
|
+
const lost = record.into ? b4a.from(record.into, 'hex') : null;
|
|
323
|
+
if (!lost || (await readProject(workspace.view, lost))) {
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
const ours = workspace.ourMemberId;
|
|
327
|
+
const row = await projectOfGroup(workspace.view, key.slice(prefix.length));
|
|
328
|
+
const listed = row && !row.deletedAt && ours && listsMember(row, ours);
|
|
329
|
+
const winner = listed ? await this._winnerOf(workspace, row) : null;
|
|
330
|
+
if (!row || !winner) {
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
const held = await this._host.heldMesh(lost);
|
|
334
|
+
if (held) {
|
|
335
|
+
const deviceId = this._host.requireDeviceKeyPair().publicKey;
|
|
336
|
+
await copyContent({ from: held, into: winner, deviceId });
|
|
337
|
+
await this._host.purgeProject(lost);
|
|
338
|
+
}
|
|
339
|
+
await this._host.local.set(key, { ...record, into: b4a.toHex(row.id) });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
async _foundGroupProject(workspace, bucket, { recordKey, record }) {
|
|
343
|
+
const held = record.key ? await this._host.heldMesh(b4a.from(record.key, 'hex')) : null;
|
|
344
|
+
const name = projectName(bucket.name.trim() || 'Project');
|
|
345
|
+
const project = held ?? (await this._host.openNewProject(workspace, { name }));
|
|
346
|
+
let next = { ...record, key: this._host.meshIdOf(project) };
|
|
347
|
+
await this._host.local.set(recordKey, next);
|
|
348
|
+
await this._addWorkspaceMembers(workspace, project);
|
|
349
|
+
if (!next.copied) {
|
|
350
|
+
await this._copyGroup(workspace, project, bucket);
|
|
351
|
+
next = { ...next, copied: true };
|
|
352
|
+
await this._host.local.set(recordKey, next);
|
|
353
|
+
}
|
|
354
|
+
const ours = workspace.ourMemberId;
|
|
355
|
+
const { groupId } = bucket;
|
|
356
|
+
const memberIds = ours ? await bornMembers(workspace.view, { signer: ours, groupId }) : [];
|
|
357
|
+
next = { ...next, listedMembers: memberIds.map((id) => b4a.toHex(id)) };
|
|
358
|
+
await this._host.local.set(recordKey, next);
|
|
359
|
+
const now = Date.now();
|
|
360
|
+
await this._host.listProject(workspace, project, {
|
|
361
|
+
createdAt: now,
|
|
362
|
+
updatedAt: now,
|
|
363
|
+
groupId,
|
|
364
|
+
memberIds
|
|
365
|
+
});
|
|
366
|
+
this._host.notifyStatus();
|
|
367
|
+
return next;
|
|
368
|
+
}
|
|
369
|
+
async _mergeGroupInto(workspace, ours, { row, bucket, record }) {
|
|
370
|
+
const winner = listsMember(row, ours) ? await this._winnerOf(workspace, row) : null;
|
|
371
|
+
if (!winner) {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
await this._addWorkspaceMembers(workspace, winner);
|
|
375
|
+
await this._listWorkspaceMembers(workspace, row);
|
|
376
|
+
const own = record.key ? await this._host.heldMesh(b4a.from(record.key, 'hex')) : null;
|
|
377
|
+
if (own) {
|
|
378
|
+
await copyContent({
|
|
379
|
+
from: own,
|
|
380
|
+
into: winner,
|
|
381
|
+
deviceId: this._host.requireDeviceKeyPair().publicKey
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
await this._copyGroup(workspace, winner, bucket);
|
|
385
|
+
this._host.log?.info(['p2p', 'mesh'], 'merged a chat-group into the project that won it', {
|
|
386
|
+
project: b4a.toHex(row.id).slice(0, 8)
|
|
387
|
+
});
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
async _winnerOf(workspace, row) {
|
|
391
|
+
const projectId = b4a.toHex(row.id);
|
|
392
|
+
if (!this._host.pooled(projectId) && !(await this._host.local.readMeshSession(row.id))) {
|
|
393
|
+
await this._host.followProjects(workspace);
|
|
394
|
+
await this._host.following(projectId);
|
|
395
|
+
}
|
|
396
|
+
const winner = this._host.pooled(projectId) ?? (await this._host.heldMesh(row.id));
|
|
397
|
+
return winner?.writable ? winner : null;
|
|
398
|
+
}
|
|
399
|
+
async _copyGroup(workspace, project, { chats }) {
|
|
400
|
+
const deviceId = this._host.requireDeviceKeyPair().publicKey;
|
|
401
|
+
const baseline = this._copyBaseline(project);
|
|
402
|
+
await copyContent({
|
|
403
|
+
from: workspace,
|
|
404
|
+
into: project,
|
|
405
|
+
deviceId,
|
|
406
|
+
chats,
|
|
407
|
+
endsOpenRuns: true,
|
|
408
|
+
baseline
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
_copyBaseline(into) {
|
|
412
|
+
const prefix = `copied/${this._host.meshIdOf(into)}/`;
|
|
413
|
+
return {
|
|
414
|
+
get: async (key) => {
|
|
415
|
+
const fields = await this._host.local.get(`${prefix}${key}`);
|
|
416
|
+
return isCopiedFields(fields) ? fields : null;
|
|
417
|
+
},
|
|
418
|
+
set: (rows) => this._host.local.setAll(rows.map(({ key, fields }) => ({ key: `${prefix}${key}`, value: fields })))
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
async _addWorkspaceMembers(workspace, project) {
|
|
422
|
+
for (const memberId of await countedMembers(workspace.view)) {
|
|
423
|
+
if (!(await inProject(project.view, memberId))) {
|
|
424
|
+
await project.dispatch('put-project-member', { memberId });
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
async _settleListed(workspace, { row, recordKey, record }) {
|
|
429
|
+
if (record.listedSettled || !this._host.isPooled(workspace)) {
|
|
430
|
+
return record;
|
|
431
|
+
}
|
|
432
|
+
const live = await liveMemberIds(workspace.view);
|
|
433
|
+
const listedMembers = (record.listedMembers ?? []).filter((hex) => live.has(hex) || listsMember(row, b4a.from(hex, 'hex')));
|
|
434
|
+
const settled = { ...record, listedMembers, listedSettled: true };
|
|
435
|
+
await this._host.local.set(recordKey, settled);
|
|
436
|
+
return settled;
|
|
437
|
+
}
|
|
438
|
+
async _listNewMembers(workspace, { row, recordKey, record }) {
|
|
439
|
+
const listed = new Set(record.listedMembers ?? []);
|
|
440
|
+
const members = await countedMembers(workspace.view);
|
|
441
|
+
const added = members.filter((id) => !listed.has(b4a.toHex(id)));
|
|
442
|
+
const project = added.length > 0 ? await this._host.heldMesh(row.id) : null;
|
|
443
|
+
for (const memberId of added) {
|
|
444
|
+
if (project && !(await inProject(project.view, memberId))) {
|
|
445
|
+
await project.dispatch('put-project-member', { memberId });
|
|
446
|
+
}
|
|
447
|
+
if (!listsMember(row, memberId)) {
|
|
448
|
+
await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (added.length > 0 && this._host.isPooled(workspace)) {
|
|
452
|
+
const listedMembers = [...listed, ...added.map((id) => b4a.toHex(id))];
|
|
453
|
+
await this._host.local.set(recordKey, { ...record, listedMembers });
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
async _listWorkspaceMembers(workspace, row) {
|
|
457
|
+
for (const memberId of await countedMembers(workspace.view)) {
|
|
458
|
+
if (!listsMember(row, memberId)) {
|
|
459
|
+
await workspace.dispatch('set-project-listing', { projectId: row.id, memberId });
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
async boot() {
|
|
464
|
+
const account = await this._host.storedAccountKey();
|
|
465
|
+
if (this._host.closing) {
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
for (const row of await this._host.local.listMeshes()) {
|
|
469
|
+
if (this._host.closing) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
const skip = row.workspaceKey || (account && b4a.equals(row.key, account));
|
|
473
|
+
const role = recordedRole(row);
|
|
474
|
+
if (skip || (role !== 'project' && role !== 'workspace')) {
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
const checked = await this._host.local.get(upgradeCheckedKey(row.key));
|
|
478
|
+
if (this._host.closing) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const due = role === 'workspace'
|
|
482
|
+
? checked === GROUPS_PENDING ||
|
|
483
|
+
(await this._host.local.get(agentsFoldKey(row.key))) === FOLD_PENDING
|
|
484
|
+
: checked !== true;
|
|
485
|
+
if (this._host.closing) {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
if (!due) {
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
await this._host.openPooledMesh(b4a.toHex(row.key)).catch((err) => {
|
|
492
|
+
if (!this._host.closing) {
|
|
493
|
+
this._host.log?.warn(['p2p', 'mesh'], 'opening a shared mesh to upgrade it failed', err);
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
async _markUpgradeChecked(meshKey) {
|
|
499
|
+
const key = upgradeCheckedKey(meshKey);
|
|
500
|
+
if ((await this._host.local.get(key)) !== true) {
|
|
501
|
+
await this._host.local.set(key, true);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
async _upgradable(mesh) {
|
|
505
|
+
if (!mesh.writable || !(await isFeatureSupported(mesh.view, FEATURES.WORKSPACE_UPGRADE))) {
|
|
506
|
+
return false;
|
|
507
|
+
}
|
|
508
|
+
return !(await bornUnderRules(mesh.view));
|
|
509
|
+
}
|
|
510
|
+
async _markJoinedWorkspace(meshKey) {
|
|
511
|
+
const registry = await this._host.writableAccount();
|
|
512
|
+
const joined = await registry?.view.get(JOINED_MESHES, { meshKey });
|
|
513
|
+
if (!registry || !joined || joined.workspace || joined.leftAt) {
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
await registry.dispatch('put-joined-mesh', {
|
|
517
|
+
room: {
|
|
518
|
+
meshKey,
|
|
519
|
+
memberId: joined.memberId,
|
|
520
|
+
joinedAt: joined.joinedAt,
|
|
521
|
+
label: joined.label,
|
|
522
|
+
labeledAt: joined.labeledAt,
|
|
523
|
+
shared: joined.shared,
|
|
524
|
+
sharedAt: joined.sharedAt,
|
|
525
|
+
workspace: true
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
copyAccount(account) {
|
|
530
|
+
if (this._publicMigration) {
|
|
531
|
+
this._copyAgain = true;
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
this._publicMigration = (async () => {
|
|
535
|
+
const chats = await oldChats(account);
|
|
536
|
+
const targets = await this._copyTargets(account);
|
|
537
|
+
const copied = `${this._host.meshIdOf(account)}:${chats?.mark}:${targets.join()}`;
|
|
538
|
+
if (this._accountCopied === copied) {
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
let done = true;
|
|
542
|
+
for (const workspaceId of targets) {
|
|
543
|
+
done = (await this._copyAccountInto(account, workspaceId, chats)) && done;
|
|
544
|
+
}
|
|
545
|
+
if (done) {
|
|
546
|
+
this._accountCopied = copied;
|
|
547
|
+
}
|
|
548
|
+
})()
|
|
549
|
+
.catch((err) => {
|
|
550
|
+
if (!this._host.closing) {
|
|
551
|
+
this._host.log?.warn(['p2p', 'mesh'], 'copying the account into its public mesh failed', err);
|
|
552
|
+
}
|
|
553
|
+
})
|
|
554
|
+
.finally(() => {
|
|
555
|
+
this._publicMigration = null;
|
|
556
|
+
if (this._copyAgain && !this._host.closing) {
|
|
557
|
+
this._copyAgain = false;
|
|
558
|
+
this.copyAccount(account);
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
async _copyTargets(account) {
|
|
563
|
+
const targets = new Set();
|
|
564
|
+
for (const row of await ourRows(account)) {
|
|
565
|
+
if (row.initial && !row.leftAt && !row.kicked) {
|
|
566
|
+
targets.add(b4a.toHex(row.meshKey));
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const own = this._host.ownWorkspaceId;
|
|
570
|
+
if (own && !targets.has(own) && (await this._host.heldMesh(b4a.from(own, 'hex')))?.minted) {
|
|
571
|
+
targets.add(own);
|
|
572
|
+
}
|
|
573
|
+
return [...targets];
|
|
574
|
+
}
|
|
575
|
+
async _copyAccountInto(account, workspaceId, chats) {
|
|
576
|
+
const workspaceKey = b4a.from(workspaceId, 'hex');
|
|
577
|
+
const into = this._host.publicMesh(workspaceId);
|
|
578
|
+
if (!into?.writable || !this._host.isAccount(account)) {
|
|
579
|
+
return false;
|
|
580
|
+
}
|
|
581
|
+
if (chats && (await this._host.publicRecord(account, workspaceKey))?.copied !== chats.mark) {
|
|
582
|
+
await copyContent({
|
|
583
|
+
from: account,
|
|
584
|
+
into,
|
|
585
|
+
deviceId: this._host.requireDeviceKeyPair().publicKey,
|
|
586
|
+
endsOpenRuns: true,
|
|
587
|
+
baseline: this._copyBaseline(into)
|
|
588
|
+
});
|
|
589
|
+
if (!this._host.isAccount(account) || !this._host.isPooled(into)) {
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
this._host.log?.info(['p2p', 'mesh'], 'copied the account into its public mesh', {
|
|
593
|
+
mesh: this._host.meshIdOf(into).slice(0, 8)
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
await this._recordCopied(account, workspaceKey, chats?.mark);
|
|
597
|
+
return !chats || this._markCopied(account, workspaceKey);
|
|
598
|
+
}
|
|
599
|
+
async refill(into) {
|
|
600
|
+
const account = this._host.accountMesh();
|
|
601
|
+
const workspaceKey = into.workspaceKey;
|
|
602
|
+
if (this._host.closing || !account || !workspaceKey || !this._host.publicWriter(into)) {
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
while (this._publicMigration) {
|
|
606
|
+
await this._publicMigration;
|
|
607
|
+
}
|
|
608
|
+
const record = await this._host.publicRecord(account, workspaceKey);
|
|
609
|
+
if (record?.copied) {
|
|
610
|
+
await this._host.savePublicRecord(account, workspaceKey, { ...record, copied: undefined });
|
|
611
|
+
}
|
|
612
|
+
this._accountCopied = null;
|
|
613
|
+
this.copyAccount(account);
|
|
614
|
+
}
|
|
615
|
+
async _recordCopied(account, workspaceKey, copied) {
|
|
616
|
+
const record = await this._host.publicRecord(account, workspaceKey);
|
|
617
|
+
const minted = !!(await this._host.heldMesh(workspaceKey))?.minted;
|
|
618
|
+
const migrated = !!record?.migrated || minted;
|
|
619
|
+
if (record && (migrated !== record.migrated || (copied && record.copied !== copied))) {
|
|
620
|
+
await this._host.savePublicRecord(account, workspaceKey, {
|
|
621
|
+
...record,
|
|
622
|
+
migrated,
|
|
623
|
+
copied: copied ?? record.copied
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
async _markCopied(account, workspaceKey) {
|
|
628
|
+
const row = await account.view.get(JOINED_MESHES, { meshKey: workspaceKey });
|
|
629
|
+
if (!row || !account.writable) {
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
if (!row.initial) {
|
|
633
|
+
const { meshKey, memberId, joinedAt } = row;
|
|
634
|
+
await account.dispatch('put-joined-mesh', {
|
|
635
|
+
room: { meshKey, memberId, joinedAt, initial: true }
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
return true;
|
|
639
|
+
}
|
|
640
|
+
history(mesh) {
|
|
641
|
+
return {
|
|
642
|
+
edited: async (chatIds) => {
|
|
643
|
+
const held = mesh();
|
|
644
|
+
if (held.role === 'workspace') {
|
|
645
|
+
await this._noteEdits(held, chatIds);
|
|
646
|
+
}
|
|
647
|
+
else if (held.role === 'project') {
|
|
648
|
+
await this._noteLateWrites(held, chatIds);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
async _noteEdits(workspace, chatIds) {
|
|
654
|
+
const checked = await this._host.local.get(upgradeCheckedKey(workspace.key));
|
|
655
|
+
if (checked !== GROUPS_PENDING && checked !== true) {
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const prefix = historyEditsOf(workspace.key);
|
|
659
|
+
const keys = chatIds ? [...chatIds].map((chatId) => `${prefix}${chatId}`) : [prefix];
|
|
660
|
+
const stamp = b4a.toHex(crypto.randomBytes(8));
|
|
661
|
+
await this._host.local.setAll(keys.map((key) => ({ key, value: stamp })));
|
|
662
|
+
this.follow(workspace);
|
|
663
|
+
}
|
|
664
|
+
async _noteLateWrites(project, chatIds) {
|
|
665
|
+
const late = await movedOutOf(project, chatIds);
|
|
666
|
+
const record = late.size > 0 ? await this._host.local.meshRecord(project.key) : null;
|
|
667
|
+
const workspace = record?.workspaceKey
|
|
668
|
+
? this._host.pooled(b4a.toHex(record.workspaceKey))
|
|
669
|
+
: null;
|
|
670
|
+
if (workspace?.role === 'workspace') {
|
|
671
|
+
await this._noteEdits(workspace, late);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
async function movedOutOf(project, chatIds) {
|
|
676
|
+
if (!chatIds) {
|
|
677
|
+
const rows = await project.view.find(MOVED_OUT_CHATS).toArray();
|
|
678
|
+
return new Set(rows.map((row) => row.chatId));
|
|
679
|
+
}
|
|
680
|
+
const late = new Set();
|
|
681
|
+
for (const chatId of chatIds) {
|
|
682
|
+
if (await project.view.get(MOVED_OUT_CHATS, { chatId })) {
|
|
683
|
+
late.add(chatId);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return late;
|
|
687
|
+
}
|
|
688
|
+
function inChat(chatId) {
|
|
689
|
+
return { gte: { chatId }, lte: { chatId } };
|
|
690
|
+
}
|