@agentshouse/mdruntime-adapter 0.1.0-cd59fd8945dcb → 0.1.0-cda4dff0d1d23

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 (62) hide show
  1. package/README.md +2 -2
  2. package/dist/actions.d.ts +21 -5
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +100 -113
  5. package/dist/adapter.d.ts +1 -0
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +346 -100
  8. package/dist/app-reference.d.ts +2 -0
  9. package/dist/app-reference.d.ts.map +1 -0
  10. package/dist/app-reference.js +2 -0
  11. package/dist/crockford.d.ts +4 -0
  12. package/dist/crockford.d.ts.map +1 -0
  13. package/dist/crockford.js +3 -0
  14. package/dist/discovery.d.ts +7 -10
  15. package/dist/discovery.d.ts.map +1 -1
  16. package/dist/discovery.js +43 -34
  17. package/dist/gather.d.ts +1 -1
  18. package/dist/gather.d.ts.map +1 -1
  19. package/dist/gather.js +4 -14
  20. package/dist/house-model.d.ts +2 -0
  21. package/dist/house-model.d.ts.map +1 -0
  22. package/dist/house-model.js +443 -0
  23. package/dist/index.d.ts +7 -2
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +6 -1
  26. package/dist/model.d.ts +5 -2
  27. package/dist/model.d.ts.map +1 -1
  28. package/dist/model.js +10 -15
  29. package/dist/part.d.ts +15 -0
  30. package/dist/part.d.ts.map +1 -0
  31. package/dist/part.js +82 -0
  32. package/dist/read.d.ts +77 -23
  33. package/dist/read.d.ts.map +1 -1
  34. package/dist/read.js +358 -144
  35. package/dist/refusal.d.ts.map +1 -1
  36. package/dist/refusal.js +2 -1
  37. package/dist/room-types.d.ts +7 -0
  38. package/dist/room-types.d.ts.map +1 -0
  39. package/dist/room-types.js +124 -0
  40. package/dist/site.d.ts +1 -1
  41. package/dist/site.d.ts.map +1 -1
  42. package/dist/site.js +160 -59
  43. package/dist/stream.d.ts +5 -4
  44. package/dist/stream.d.ts.map +1 -1
  45. package/dist/stream.js +356 -43
  46. package/dist/transport.d.ts +67 -10
  47. package/dist/transport.d.ts.map +1 -1
  48. package/dist/transport.js +3 -0
  49. package/dist/upload.d.ts +12 -0
  50. package/dist/upload.d.ts.map +1 -0
  51. package/dist/upload.js +63 -0
  52. package/dist/version.d.ts +1 -0
  53. package/dist/version.d.ts.map +1 -1
  54. package/dist/version.js +1 -1
  55. package/dist/write.d.ts +13 -9
  56. package/dist/write.d.ts.map +1 -1
  57. package/dist/write.js +130 -40
  58. package/package.json +8 -5
  59. package/AGENTS.md +0 -290
  60. package/dist/links.d.ts +0 -20
  61. package/dist/links.d.ts.map +0 -1
  62. package/dist/links.js +0 -104
package/AGENTS.md DELETED
@@ -1,290 +0,0 @@
1
- # Instructions for the App Creator's agent
2
-
3
- These instructions address an agent in an environment with Git. Obtain the
4
- App Repository, change it, build, and push. They are documentation, not
5
- enforcement.
6
-
7
- ## The MD Runtime API
8
-
9
- App code reaches the MD Runtime through the remote client. The surfaces are
10
- Rooms, Query, Search, Observe, Types, Room Draft, Save, History, Call, Reached, Log,
11
- the reported persistence answer, reauthorization, conversation, the chat draft,
12
- and Presentation State. Sync Status, the Service Mode, the storage-loss
13
- statement, the deployment facts and the signed-in member are observations only.
14
-
15
- The host attaches each port with its reach, and every member and observation
16
- answers within it: a Room outside it answers as one the replica does not hold,
17
- Rooms lists only the Rooms reached whole, a Room reached for single Documents
18
- shows only those Documents and the Types of the models that govern them, and
19
- without conversations the conversation members, the chat draft and the
20
- conversation observation hold none.
21
- A change of the reach reaches every open observation as its new value, a query
22
- keeping every unchanged Document the same object.
23
-
24
- Every `observe` resolves, once its first value has arrived, to a handle:
25
- `current()` is the latest complete value, `subscribe(listener)` hears each later
26
- value whole and returns its unsubscribe, and `close()` ends the observation.
27
- Closing the port ends every handle and rejects every pending call and
28
- observation; a client that reconnects observes again.
29
-
30
- `observe('agents')` answers every Agent a new conversation can go to, each
31
- `{ identity, name }`, whether or not it has a conversation yet.
32
- Each conversation the Provider's conversation list names is held complete in the
33
- browser. `conversation.list()` and `observe('conversations')` answer each with its
34
- Agent's identity and name, its name, created and finished times, last outcome and live
35
- `state`: `asking` while a question waits, `working` while a turn runs, else `idle`,
36
- and `started`, when the running turn began, or `null`.
37
- Observe one with `observe('conversation', id, { limit })`: the view
38
- holds its latest `limit` messages, or all of them, oldest first, its chat
39
- draft, and what the Provider streams for it whether or not anything views it:
40
- the running `turn` with its message parts and its context in use and window, the
41
- open questions, the process state, and the CLI's commands and options. These live
42
- only in memory: the message accepted under the turn's id replaces the running turn,
43
- and none of them is stored or searchable.
44
- `chatDraft.write` and `chatDraft.discard` keep what the App Creator composes;
45
- sending it is a Provider Action that needs the Provider reachable and is never
46
- queued.
47
-
48
- Each Room in Rooms lists its `attachments`, the files the Provider holds for it
49
- that its Documents link by the Provider's reference, ordered by `reference`,
50
- each with `name`, `byteLength`, `mediaType` and the Provider's own entry as
51
- `entry`. They are as the Provider last declared them and hold no bytes;
52
- open one through the Provider Action that serves it.
53
-
54
- Rooms, Query, Search, Observe, Types, Room Draft, Save, and Reached never
55
- touch the network.
56
- History and Call go to the Provider through the Adapter. Call takes the Action names
57
- the Adapter declares; any other name answers `undeclared` without reaching the
58
- Provider. A Call result whose
59
- effect the Provider places in a Room carries `at`, that Room and Room Version;
60
- `reached(room, roomVersion)` answers whether the Room's confirmed state has
61
- reached it, and the MD Runtime keeps refreshing that Room until it has;
62
- `reached(room, roomVersion, path)` answers whether the held confirmed copy of
63
- that document was written at a later Room Version. Each History revision
64
- names its author, `{ kind: 'user', id }` or `{ kind: 'site', id, name }`.
65
- History of a Path no current document holds answers the removed document's
66
- revisions with `deleted: true`, newest first; that revision is what a link to
67
- the Path opens. Every
68
- member of the
69
- client is asynchronous. Results are detached snapshots. Nothing in this
70
- surface returns the authorization, performs a request the App's code
71
- composes, or exposes the browser store.
72
-
73
- Every refusal and failure carries `code` and `retryable`: each Call and History answer
74
- other than its result, a refused Save or Room Draft write, a returned Room Draft's
75
- reason, a Room's refusal and the Connection Refusal, and every error a member rejects
76
- with, beside its message. A Provider refusal carries the Provider's own `retryable`.
77
- Every code and every value the API answers is open: handle one you do not know by its
78
- `code` and `retryable`.
79
-
80
- Read the deployment facts with `observe('deployment-facts')`. Interpret the
81
- opaque value using the host's contract.
82
-
83
- `observe('member')` answers who uses the App: the User the Provider signed in,
84
- `{ kind: 'user', id, name }` in the shape of a change's `author`, so a Document
85
- whose `change.author.id` equals its `id` is that User's own. It is `null` until
86
- the Provider first names one, and where it names none, such as a Site. The
87
- MD Runtime keeps it with the Local Replicas, so it answers offline once known,
88
- and every port hears the next member the Provider names; signing out answers
89
- `null`, then ends the observation with the authorization. It is the only way App code learns who
90
- uses it.
91
-
92
- The in-memory Adapter at `@agentshouse/mdruntime/memory` is for tests and
93
- ships in no bundle.
94
-
95
- ## Local Query rules
96
-
97
- `query(q)` answers once from local state; `observe('query', q)` holds the whole
98
- result and hears it again only when it changes, keeping every unchanged Document
99
- the same object, so a component can tell what changed by identity. A query is
100
- data:
101
-
102
- - **Filter.** `rooms` names Room locators and defaults to every Room. `types`
103
- names MD Types, and `any` names untyped Markdown Documents. `pathPrefix` is a
104
- directory: `notes` keeps `notes/a.md` and not `notes2/a.md`.
105
- - **Fields.** Every frontmatter value is named by JSON Pointer: `/status`,
106
- `/shipping/city`, `~1` for a `/` and `~0` for a `~` in a key. A segment that
107
- meets a list applies to every item and a number names one item. `where` names
108
- pointers, ANDs them, and nests `$and` and `$or`; a pointer through a list holds
109
- when any item satisfies it. A bare value is equality; the operators are `$eq`,
110
- `$ne`, `$in`, `$nin`, `$gt`, `$gte`, `$lt`, `$lte`, `$all`, `$size` and
111
- `$exists`. Equality on a list asks whether it contains the value. Missing, null and empty differ:
112
- `$exists: false` keeps a missing field, `null` an explicit null, `''` empty
113
- text and `$size: 0` an empty list. A date compares in calendar order and never
114
- with a date-time. A reference compares by the Room Path it resolves to.
115
- Untyped frontmatter, and a value under `oneOf` or a conditional, is plain
116
- values. There is no text containment or regular
117
- expression.
118
- - **Relations.** `hop: { field, types, where, pathPrefix }` keeps Documents whose
119
- reference at pointer `field`, at any depth, points to a matching Document of the
120
- same Room. `backlinks: { field, types, where, pathPrefix }` keeps Documents that a
121
- matching Document of the same Room references. Each keeps a Document once.
122
- - **Parts.** A Document query must `select`: `'all'`, or `fields`, `sections`,
123
- `preamble`, `markdown`, `backlinks` and `change`, where `fields` is `'all'` or a
124
- list of pointers and `sections` `'all'` or a list of names. Selected pointers
125
- return the frontmatter holding only them, nested as written. A backlink names
126
- the referring Path, MD Type and the pointer it came from as `field`. Room and Path always return; a Markdown
127
- Document also returns its MD Type. A non-Markdown Document or binary link
128
- target returns only Room and Path. An unselected part is absent. Select only
129
- what a screen shows: a list that selects the Markdown hears every edit of it.
130
- - **Change.** `change` is `{ author, time }` of the Document's head revision as
131
- the Provider last delivered it: `author` is `{ kind: 'user' | 'site', id, name }`
132
- or `null` for a write the Provider made itself, and `time` is the Provider's
133
- ISO 8601 change time. It is `null` where the Provider delivered no time, as at
134
- a Site. An unsent edit or move keeps it, an unsent creation has none, and a
135
- saved write takes the Provider's own once it is Accepted. A
136
- Room's last change is its Document with the latest `time`.
137
- - **Order and groups.** `orderBy` sorts by `room`, `path`, `type` or `{ field }`
138
- with TanStack DB's own options, and ties end with Room, then Path. `groupBy` by
139
- `room`, `type` or `{ field }` answers `{ groups: [{ key, count }] }`, counted
140
- over every match and ordered by `count` or a key. A sort or group takes only a
141
- pointer that holds one value. `limit` bounds the Documents or the groups.
142
- - **Results and errors.** A result carries no Room Version: read freshness from
143
- Rooms and Sync Status. An invalid query fails with the `code`
144
- `invalid-query`, `unknown-type`, `unknown-field` or `wrong-kind`, naming where,
145
- and never answers an empty result in its place.
146
-
147
- ## Local Search rules
148
-
149
- `search({ text, documents, conversations, limit })` answers once, by words, from
150
- local state, with MiniSearch at its default configuration. There is no observed
151
- search.
152
-
153
- - **What is found.** A Document by its field values, its sections' headings and
154
- text, its preamble and its Path; a message by its text. Frontmatter keys, Room
155
- Drafts, chat drafts and a running turn's parts are never found. There are no phrases,
156
- word forms, typo tolerance or prefix matching.
157
- - **Narrowing.** `documents` is a Local Query filter: `rooms`, `types`, `where`,
158
- `pathPrefix`, `hop` and `backlinks`. `conversations` names conversations. Both
159
- apply before `limit`, which bounds each list; without it every hit returns.
160
- - **Results.** `{ documents, messages }`, each ranked by `score`, which compares
161
- hits of one list of one search. A Document hit names its `room`, `path` and
162
- `type`, a message hit its `conversation` and `identity`, and `matches` maps each
163
- matched word to the parts it matched in: `fields`, `sections`, `preamble` or
164
- `path`, or `text`. A hit carries no position; mark the words in the text you
165
- show. An invalid filter fails with its Local Query error.
166
-
167
- ## Room Draft, Save, and outcome rules
168
-
169
- The MD Runtime owns one Room Draft per Room and Path, shared by every App and
170
- tab. It is durable: it survives reload and navigation. A Room that Discovery
171
- stops listing takes its Room Drafts and its queue with it.
172
- Write a Room Draft with `roomDraft.write(room, path, revision, parts)`, naming
173
- the revision you read, or `null` when there was none, and every part it should
174
- now change: fields, sections and the preamble, or the whole Markdown, a
175
- creation, a move, or a removal. A part new to the Room Draft names its base,
176
- the value you showed when it was opened; the MD Runtime keeps a part's first
177
- base. A binary link target has a Room Draft too, for a move or a removal, with
178
- `null` as its base. A write or a discard from an older revision is refused as `stale` with
179
- the current Room Draft; read it and write again.
180
-
181
- Save takes one Room and the Room Drafts of it to save, each with its revision,
182
- validates locally, and on success appends one Change Batch to that Room's
183
- queue and removes those Room Drafts. It is refused with the `code` `stale`,
184
- `changed-underneath` naming each part whose base no longer matches the visible
185
- state, or a local validation rule, and then creates nothing and leaves every
186
- Room Draft in place. A directory move or removal is a move or removal part on
187
- each Document and binary link target beneath it, saved together.
188
-
189
- Outcomes follow the Provider's answer. An Accepted Change Batch stays in the
190
- queue, drawn over the confirmed layer, until the confirmed layer reaches the
191
- Room Version it was accepted at, and then leaves it. Conflicted and Rejected, an expired operation, and a change that no
192
- longer applies over the Room each leave the queue and return as Room Drafts on
193
- the Documents they changed, marked with why: the Provider's structured refusal
194
- code and fields as returned, not known to have applied, or withdrawn locally.
195
- The rest of the queue keeps synchronizing, and Sync Status requires attention
196
- until each returned Room Draft is saved again or discarded. A Change Batch
197
- keeps no outcome once it leaves the queue; `observe('queue', room)` shows each
198
- Pending one, and a returned Room Draft says why it returned. Transport
199
- failure and Provider unavailability keep work Pending and do not infer a
200
- rejection. A refusal the Provider marks retryable stays Pending until the reset
201
- it names; the MD Runtime retries the same Change Batch identity and payload and
202
- never infers an outcome from transport uncertainty. The MD Runtime never
203
- combines, reorders, or rebases.
204
-
205
- On a Site, every Save stays in the visitor's browser. Write grants and
206
- revisions do not refuse it, and its Pending Change shows in every query and
207
- across reloads until the browser's storage is cleared. It is never sent:
208
- `observe('queue', room)` shows it `local`, and Sync Status never waits on it.
209
- Once the Room changes so that it no longer applies, it leaves the queue and
210
- returns no Room Draft.
211
-
212
- `observe('service-mode')` answers the Provider's Service
213
- Mode, `open` or `write-halted`. While writes are halted, Save still succeeds,
214
- its Change Batch waits unsent, and the queue is submitted in order once the
215
- Provider reopens writes. An Action is never held: a write the Provider refuses
216
- for the halt returns that refusal to its caller. Show the Provider as read-only
217
- instead of retrying.
218
-
219
- ## The neutrality rule
220
-
221
- No module outside an Adapter package imports, names, or special-cases a
222
- Provider. MD UI and every App reach data only through the client entry.
223
-
224
- ## Content-safety rules
225
-
226
- The Provider authorization belongs to the Runtime host and is never readable
227
- by the App's code. The App's code reaches the network only at the external
228
- origins its manifest declares, empty by default, and the Provider enforces
229
- that set. Every request to the Provider itself goes through the MD Runtime.
230
- Rendered Markdown never executes scripts, renders raw HTML only for the
231
- details and summary elements, and loads images and other resources only from
232
- origins the Provider's policy allows.
233
-
234
- ## Launch promises MD UI keeps
235
-
236
- MD UI uses semantic HTML, is fully operable from a keyboard with visible
237
- focus and labelled controls, and lays out from a 360 pixel wide viewport to
238
- a desktop. Its strings are English only, dates and numbers follow the
239
- browser locale, and content is never translated. Light and dark themes come
240
- from CSS custom properties with the browser's preference as the default and
241
- an App's override respected. What an App Creator's agent adds carries none
242
- of these promises by itself. Reading, querying and searching never wait for
243
- the network. Data lives only in the App Creator's browser and with the Provider.
244
-
245
- ## App code takes the client its host hands it and never constructs a Runtime
246
-
247
- App code takes the client its host hands it: in House,
248
- `houseBridge().runtime` from `@agentshouse/app-bridge` resolves to the
249
- client House built into the page. It never constructs a Runtime or a client.
250
- It imports only types from `@agentshouse/mdruntime/client`, does not call
251
- `connectMdRuntime` or `serveMdRuntime`, and does not import the package main,
252
- the host entry, or the Adapter package. Hosts and tests use those entries; the
253
- served App does not.
254
-
255
- A React App may observe and save through `@agentshouse/mdruntime/react`, which
256
- names React as a peer dependency. `useObserve(client, topic, ...input)` takes
257
- the arguments of `observe` and returns `{ state: 'loading' }` until the topic's
258
- first value arrives, then `{ state: 'ready', value }`, re-rendering on each
259
- change; a `null` client observes nothing and stays loading. `useSave(client)` returns `busy`, the last `outcome` or `failure`, and
260
- `save(room, roomDrafts)`, which writes each Path's parts onto the Room Draft
261
- already there, keeping the bases its parts have, or onto a new one, and saves
262
- them together. A refused Save leaves the Room Drafts for the App to save again
263
- or discard; a failure is recorded and rejects. `saveRoomDrafts(client, room,
264
- roomDrafts)` is that save outside a component. A react-admin or Refine App
265
- reads and writes its Rooms through the data providers of
266
- `@agentshouse/mdruntime-families`.
267
-
268
- ## The Chat
269
-
270
- `@agentshouse/mdruntime/chat` holds the Chat: one data layer over the
271
- conversations, the same for every App. React and `@assistant-ui/react` are
272
- required peers of this entry, so every App that uses it adds React as a
273
- dependency, also an App not written in React. Wrap your own assistant-ui
274
- primitives in `<ChatProvider client={runtime} conversation={id} agent={agent}>`:
275
- `conversation` is the one it opens, `null` for a new chat, `agent` the Agent a
276
- new chat goes to, and `onOpen` hears each conversation it opens. Thread, Messages,
277
- Composer, its attachments and the thread list run on it: sending with files, a
278
- message sent mid-turn waiting in the queue, Stop, rename, Done as archive and
279
- delete. `useChat()` gives the rest: `send`, through the same queue, the
280
- conversations, the Agents, the open
281
- questions to `answer`, the CLI's `options` to `choose` and `commands`, the queue
282
- to `edit` or `unqueue`, `kill`, and each act's outcome. A sent message shows at
283
- once until House keeps it, and the unsent text stays in the chat draft, the same
284
- in every composer over that conversation. A secret
285
- question is never answered here. Never replace its messages, sending or store.
286
-
287
- ## What an agent must never do
288
-
289
- An agent must never read the authorization, load code from a Room, call the
290
- Provider directly, or rebase a refused Change Batch.
package/dist/links.d.ts DELETED
@@ -1,20 +0,0 @@
1
- import type { HeldRoom, MutationResult, RoomAttachment } from '@agentshouse/mdruntime';
2
- import type { HouseScope, HouseView } from './read.js';
3
- import type { StreamedAdvancement } from './stream.js';
4
- export type RoomLinks = {
5
- roomVersion: string;
6
- handed: ReadonlyMap<string, RoomAttachment>;
7
- paths: ReadonlyMap<string, ReadonlySet<string>>;
8
- revisions: ReadonlyMap<string, string>;
9
- };
10
- export declare function scopeLinks(scope: HouseScope): RoomLinks;
11
- export declare function heldLinks(held: HeldRoom): RoomLinks;
12
- export declare function acceptedLinks(held: RoomLinks, accepted: Extract<MutationResult, {
13
- kind: 'accepted';
14
- }>, roomVersion: string, originals: readonly RoomAttachment[]): {
15
- links: RoomLinks;
16
- attachments?: readonly RoomAttachment[];
17
- };
18
- export declare function enumeratedLinks(view: HouseView, earlier: RoomLinks | undefined): RoomLinks;
19
- export declare function streamedLinks(held: RoomLinks, advanced: StreamedAdvancement): RoomLinks | null;
20
- //# sourceMappingURL=links.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"links.d.ts","sourceRoot":"","sources":["../src/links.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACvF,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAOvD,MAAM,MAAM,SAAS,GAAG;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IAC5C,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;IAChD,SAAS,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACxC,CAAC;AAcF,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,CAWvD;AAOD,wBAAgB,SAAS,CAAC,IAAI,EAAE,QAAQ,GAAG,SAAS,CAOnD;AAED,wBAAgB,aAAa,CAC3B,IAAI,EAAE,SAAS,EACf,QAAQ,EAAE,OAAO,CAAC,cAAc,EAAE;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,EACvD,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,SAAS,cAAc,EAAE,GACnC;IAAE,KAAK,EAAE,SAAS,CAAC;IAAC,WAAW,CAAC,EAAE,SAAS,cAAc,EAAE,CAAA;CAAE,CA2B/D;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,CAgB1F;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,mBAAmB,GAAG,SAAS,GAAG,IAAI,CAoB9F"}
package/dist/links.js DELETED
@@ -1,104 +0,0 @@
1
- import { roomVersion } from './version.js';
2
- const ATTACHMENT_REFERENCE = /at_[1-9a-hjkmnp-tv-z][0-9a-hjkmnp-tv-z]*\.[0-9a-hjkmnp-tv-z]/g;
3
- const NONE = new Set();
4
- function linkedBy(text) {
5
- if (text === null)
6
- return NONE;
7
- const references = new Set(Array.from(text.matchAll(ATTACHMENT_REFERENCE), ([found]) => found));
8
- return references.size === 0 ? NONE : references;
9
- }
10
- function handedOf(attachments) {
11
- return new Map((attachments ?? []).map((attachment) => [attachment.reference, attachment]));
12
- }
13
- export function scopeLinks(scope) {
14
- return {
15
- roomVersion: roomVersion(scope),
16
- handed: handedOf(scope.attachments),
17
- paths: new Map(scope.contents.map(({ path, content }) => [path, linkedBy(content)])),
18
- revisions: new Map(scope.files.flatMap(({ path, revision }) => revision === undefined ? [] : [[path, revision]])),
19
- };
20
- }
21
- function revised(revisions, path, revision) {
22
- if (revision === undefined)
23
- revisions.delete(path);
24
- else
25
- revisions.set(path, revision);
26
- }
27
- export function heldLinks(held) {
28
- return {
29
- roomVersion: held.roomVersion,
30
- handed: handedOf(held.attachments),
31
- paths: new Map(Object.entries(held.documents).map(([path, text]) => [path, linkedBy(text)])),
32
- revisions: new Map(Object.entries(held.revisions)),
33
- };
34
- }
35
- export function acceptedLinks(held, accepted, roomVersion, originals) {
36
- const paths = new Map(held.paths);
37
- const revisions = new Map(held.revisions);
38
- const handed = new Map(held.handed);
39
- const answered = handedOf(originals);
40
- for (const [path, text] of Object.entries(accepted.documents)) {
41
- const linked = linkedBy(text);
42
- for (const reference of linked) {
43
- const original = answered.get(reference);
44
- if (!handed.has(reference) && original !== undefined)
45
- handed.set(reference, original);
46
- }
47
- paths.set(path, linked);
48
- }
49
- for (const [path, revision] of Object.entries(accepted.revisions))
50
- revisions.set(path, revision);
51
- for (const path of accepted.removals) {
52
- paths.delete(path);
53
- revisions.delete(path);
54
- }
55
- const linked = new Set([...paths.values()].flatMap((references) => [...references]));
56
- for (const reference of held.handed.keys()) {
57
- const before = [...held.paths.values()].some((references) => references.has(reference));
58
- if (before && !linked.has(reference))
59
- handed.delete(reference);
60
- }
61
- const links = { roomVersion, handed, paths, revisions };
62
- const changed = handed.size !== held.handed.size || [...handed.keys()].some((key) => !held.handed.has(key));
63
- return changed ? { links, attachments: [...handed.values()] } : { links };
64
- }
65
- export function enumeratedLinks(view, earlier) {
66
- const paths = new Map(earlier?.paths);
67
- const revisions = new Map(earlier?.revisions);
68
- for (const change of view.changes) {
69
- paths.set(change.path, linkedBy('content' in change ? change.content : null));
70
- revised(revisions, change.path, 'removed' in change
71
- ? undefined
72
- : 'reference' in change
73
- ? change.reference.revision
74
- : change.revision);
75
- }
76
- return { roomVersion: roomVersion(view), handed: handedOf(view.attachments), paths, revisions };
77
- }
78
- export function streamedLinks(held, advanced) {
79
- const paths = new Map(held.paths);
80
- const revisions = new Map(held.revisions);
81
- const dropped = new Set();
82
- for (const entry of advanced.entries) {
83
- revised(revisions, entry.path, entry.kind === 'removed' ? undefined : entry.revision);
84
- const text = entry.kind === 'removed' ? null : entry.text;
85
- if (text === undefined)
86
- return null;
87
- const linked = linkedBy(text);
88
- for (const reference of linked)
89
- if (!held.handed.has(reference))
90
- return null;
91
- for (const reference of paths.get(entry.path) ?? held.handed.keys()) {
92
- if (held.handed.has(reference) && !linked.has(reference))
93
- dropped.add(reference);
94
- }
95
- paths.set(entry.path, linked);
96
- }
97
- for (const references of paths.values()) {
98
- for (const reference of references)
99
- dropped.delete(reference);
100
- }
101
- if (dropped.size > 0)
102
- return null;
103
- return { roomVersion: advanced.page.roomVersion, handed: held.handed, paths, revisions };
104
- }