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