@hydranium/protocol 1.0.0-next.16 → 1.0.0-next.160

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 (177) hide show
  1. package/README.md +35 -1
  2. package/lib/client/data-connection.d.ts +157 -0
  3. package/lib/client/data-connection.d.ts.map +1 -0
  4. package/lib/client/data-connection.js +236 -0
  5. package/lib/client/data-connection.js.map +1 -0
  6. package/lib/client/data-events.d.ts +13 -1
  7. package/lib/client/data-events.d.ts.map +1 -1
  8. package/lib/client/data-events.js +21 -0
  9. package/lib/client/data-events.js.map +1 -1
  10. package/lib/client/data-port.d.ts +17 -22
  11. package/lib/client/data-port.d.ts.map +1 -1
  12. package/lib/client/data-session.d.ts +440 -78
  13. package/lib/client/data-session.d.ts.map +1 -1
  14. package/lib/client/data-session.js +697 -106
  15. package/lib/client/data-session.js.map +1 -1
  16. package/lib/client/index.d.ts +13 -8
  17. package/lib/client/index.d.ts.map +1 -1
  18. package/lib/client/index.js +13 -8
  19. package/lib/client/index.js.map +1 -1
  20. package/lib/client/message-relay.d.ts +9 -3
  21. package/lib/client/message-relay.d.ts.map +1 -1
  22. package/lib/client/message-relay.js +11 -5
  23. package/lib/client/message-relay.js.map +1 -1
  24. package/lib/client/post-message-transport.d.ts +64 -3
  25. package/lib/client/post-message-transport.d.ts.map +1 -1
  26. package/lib/client/post-message-transport.js +175 -1
  27. package/lib/client/post-message-transport.js.map +1 -1
  28. package/lib/client/rpc-connection.d.ts +149 -0
  29. package/lib/client/rpc-connection.d.ts.map +1 -0
  30. package/lib/client/rpc-connection.js +179 -0
  31. package/lib/client/rpc-connection.js.map +1 -0
  32. package/lib/client-ids.d.ts +45 -0
  33. package/lib/client-ids.d.ts.map +1 -0
  34. package/lib/client-ids.js +48 -0
  35. package/lib/client-ids.js.map +1 -0
  36. package/lib/clock.d.ts +38 -0
  37. package/lib/clock.d.ts.map +1 -1
  38. package/lib/clock.js +36 -1
  39. package/lib/clock.js.map +1 -1
  40. package/lib/data/data-protocol-methods.d.ts +4 -4
  41. package/lib/data/data-protocol-methods.d.ts.map +1 -1
  42. package/lib/data/data-protocol-methods.js +11 -1
  43. package/lib/data/data-protocol-methods.js.map +1 -1
  44. package/lib/data/data-server-protocol.d.ts +111 -31
  45. package/lib/data/data-server-protocol.d.ts.map +1 -1
  46. package/lib/data/events.d.ts +103 -14
  47. package/lib/data/events.d.ts.map +1 -1
  48. package/lib/data/requests.d.ts +67 -11
  49. package/lib/data/requests.d.ts.map +1 -1
  50. package/lib/errors.d.ts +163 -17
  51. package/lib/errors.d.ts.map +1 -1
  52. package/lib/errors.js +219 -22
  53. package/lib/errors.js.map +1 -1
  54. package/lib/glsp-save-model-actions.d.ts +50 -0
  55. package/lib/glsp-save-model-actions.d.ts.map +1 -0
  56. package/lib/glsp-save-model-actions.js +28 -0
  57. package/lib/glsp-save-model-actions.js.map +1 -0
  58. package/lib/index.d.ts +3 -0
  59. package/lib/index.d.ts.map +1 -1
  60. package/lib/index.js +6 -0
  61. package/lib/index.js.map +1 -1
  62. package/lib/latency-collector.d.ts +8 -4
  63. package/lib/latency-collector.d.ts.map +1 -1
  64. package/lib/latency-collector.js.map +1 -1
  65. package/lib/messages/index.d.ts +29 -0
  66. package/lib/messages/index.d.ts.map +1 -0
  67. package/lib/messages/index.js +58 -0
  68. package/lib/messages/index.js.map +1 -0
  69. package/lib/messages/primitives.d.ts +188 -0
  70. package/lib/messages/primitives.d.ts.map +1 -0
  71. package/lib/messages/primitives.js +161 -0
  72. package/lib/messages/primitives.js.map +1 -0
  73. package/lib/model-server.d.ts +60 -13
  74. package/lib/model-server.d.ts.map +1 -1
  75. package/lib/model-server.js +4 -2
  76. package/lib/model-server.js.map +1 -1
  77. package/lib/model-service/based-on.d.ts +57 -0
  78. package/lib/model-service/based-on.d.ts.map +1 -0
  79. package/lib/model-service/based-on.js +37 -0
  80. package/lib/model-service/based-on.js.map +1 -0
  81. package/lib/model-service/index.d.ts +1 -1
  82. package/lib/model-service/index.d.ts.map +1 -1
  83. package/lib/model-service/index.js +4 -5
  84. package/lib/model-service/index.js.map +1 -1
  85. package/lib/patch-merge.d.ts +28 -27
  86. package/lib/patch-merge.d.ts.map +1 -1
  87. package/lib/patch-merge.js +66 -22
  88. package/lib/patch-merge.js.map +1 -1
  89. package/lib/profile-session.d.ts +8 -4
  90. package/lib/profile-session.d.ts.map +1 -1
  91. package/lib/profile-session.js.map +1 -1
  92. package/lib/rpc/bind-rpc-methods.d.ts +29 -3
  93. package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
  94. package/lib/rpc/bind-rpc-methods.js +22 -3
  95. package/lib/rpc/bind-rpc-methods.js.map +1 -1
  96. package/lib/rpc/create-rpc-proxy.d.ts +7 -0
  97. package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
  98. package/lib/rpc/create-rpc-proxy.js +6 -1
  99. package/lib/rpc/create-rpc-proxy.js.map +1 -1
  100. package/lib/testing/catalogue-audit.d.ts +80 -0
  101. package/lib/testing/catalogue-audit.d.ts.map +1 -0
  102. package/lib/testing/catalogue-audit.js +94 -0
  103. package/lib/testing/catalogue-audit.js.map +1 -0
  104. package/lib/testing/data-doubles.d.ts +13 -12
  105. package/lib/testing/data-doubles.d.ts.map +1 -1
  106. package/lib/testing/data-doubles.js +19 -5
  107. package/lib/testing/data-doubles.js.map +1 -1
  108. package/lib/testing/fake-clock.d.ts +9 -1
  109. package/lib/testing/fake-clock.d.ts.map +1 -1
  110. package/lib/testing/fake-clock.js +54 -45
  111. package/lib/testing/fake-clock.js.map +1 -1
  112. package/lib/testing/index.d.ts +1 -0
  113. package/lib/testing/index.d.ts.map +1 -1
  114. package/lib/testing/index.js +4 -1
  115. package/lib/testing/index.js.map +1 -1
  116. package/lib/testing/node/index.d.ts +1 -0
  117. package/lib/testing/node/index.d.ts.map +1 -1
  118. package/lib/testing/node/index.js +2 -2
  119. package/lib/testing/node/index.js.map +1 -1
  120. package/lib/testing/node/message-port-pair.d.ts +25 -0
  121. package/lib/testing/node/message-port-pair.d.ts.map +1 -0
  122. package/lib/testing/node/message-port-pair.js +26 -0
  123. package/lib/testing/node/message-port-pair.js.map +1 -0
  124. package/lib/transfer-diagnostic.d.ts +33 -0
  125. package/lib/transfer-diagnostic.d.ts.map +1 -1
  126. package/lib/transfer-diagnostic.js +23 -0
  127. package/lib/transfer-diagnostic.js.map +1 -1
  128. package/lib/transfer-document.d.ts +34 -5
  129. package/lib/transfer-document.d.ts.map +1 -1
  130. package/lib/transfer-document.js +14 -1
  131. package/lib/transfer-document.js.map +1 -1
  132. package/lib/util.d.ts +8 -0
  133. package/lib/util.d.ts.map +1 -1
  134. package/lib/util.js +32 -0
  135. package/lib/util.js.map +1 -1
  136. package/package.json +11 -2
  137. package/src/client/data-connection.ts +314 -0
  138. package/src/client/data-events.ts +33 -1
  139. package/src/client/data-port.ts +17 -23
  140. package/src/client/data-session.ts +880 -123
  141. package/src/client/index.ts +13 -8
  142. package/src/client/message-relay.ts +29 -7
  143. package/src/client/post-message-transport.ts +219 -4
  144. package/src/client/rpc-connection.ts +244 -0
  145. package/src/client-ids.ts +49 -0
  146. package/src/clock.ts +56 -0
  147. package/src/data/data-protocol-methods.ts +11 -3
  148. package/src/data/data-server-protocol.ts +132 -31
  149. package/src/data/events.ts +109 -14
  150. package/src/data/requests.ts +71 -11
  151. package/src/errors.ts +259 -24
  152. package/src/glsp-save-model-actions.ts +59 -0
  153. package/src/index.ts +6 -0
  154. package/src/latency-collector.ts +8 -3
  155. package/src/messages/index.ts +36 -0
  156. package/src/messages/primitives.ts +271 -0
  157. package/src/model-server.ts +63 -18
  158. package/src/model-service/based-on.ts +63 -0
  159. package/src/model-service/index.ts +4 -5
  160. package/src/patch-merge.ts +90 -37
  161. package/src/profile-session.ts +9 -4
  162. package/src/rpc/bind-rpc-methods.ts +49 -4
  163. package/src/rpc/create-rpc-proxy.ts +14 -1
  164. package/src/testing/catalogue-audit.ts +111 -0
  165. package/src/testing/data-doubles.ts +42 -17
  166. package/src/testing/fake-clock.ts +62 -47
  167. package/src/testing/index.ts +4 -1
  168. package/src/testing/node/index.ts +2 -2
  169. package/src/testing/node/message-port-pair.ts +40 -0
  170. package/src/transfer-diagnostic.ts +40 -0
  171. package/src/transfer-document.ts +41 -6
  172. package/src/util.ts +33 -0
  173. package/lib/model-service/args.d.ts +0 -64
  174. package/lib/model-service/args.d.ts.map +0 -1
  175. package/lib/model-service/args.js +0 -11
  176. package/lib/model-service/args.js.map +0 -1
  177. package/src/model-service/args.ts +0 -67
@@ -0,0 +1,49 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 CrossBreeze, EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the MIT License which is available in the project root.
6
+ *
7
+ * SPDX-License-Identifier: MIT
8
+ ********************************************************************************/
9
+
10
+ /**
11
+ * Well-known values of the wire's `clientId` / `sourceClientId` field.
12
+ *
13
+ * The framework tracks each document as belonging to one or more "clients" —
14
+ * typically an LSP-text client (Monaco, VS Code, …), a GLSP diagram client,
15
+ * and/or a structured-form client — and keys its holds and watches per
16
+ * `(uri, clientId)`. These three values are not real participants, so a client
17
+ * minting its own id must avoid them.
18
+ *
19
+ * **Here rather than in the server package, because the side that has to
20
+ * RECOGNISE them is the client.** An inbound `onDocumentUpdated` carries
21
+ * `sourceClientId`, and a consumer deciding what to do with it is asking which
22
+ * of these it is — so a frontend would otherwise have to depend on the server
23
+ * tier to name three strings, and in practice retypes the literal instead.
24
+ */
25
+
26
+ /** Identifier for an LSP-text client (Monaco, VS Code, …). */
27
+ export const LANGUAGE_CLIENT_ID = 'language-client';
28
+
29
+ /**
30
+ * The id an event names when no single client is behind it: the source of a
31
+ * `'rebuilt'` event, a document no client authored, or a build with more than
32
+ * one cause.
33
+ */
34
+ export const UNKNOWN_CLIENT_ID = 'unknown';
35
+
36
+ /**
37
+ * Synthetic author id on the `onDocumentUpdated` broadcast a data-server
38
+ * emits after the LAST client closed a document and the framework rebuilt it
39
+ * from its disk content (discarding unsaved in-session edits). Not a real
40
+ * client: it lets consumers distinguish the revert broadcast from
41
+ * client-authored updates.
42
+ */
43
+ export const REVERT_ON_CLOSE_CLIENT_ID = 'revert-on-close';
44
+
45
+ /**
46
+ * Every id the framework reserves, for a client checking that the identity it
47
+ * is about to mint collides with none of them.
48
+ */
49
+ export const FRAMEWORK_CLIENT_IDS: readonly string[] = [LANGUAGE_CLIENT_ID, UNKNOWN_CLIENT_ID, REVERT_ON_CLOSE_CLIENT_ID];
package/src/clock.ts CHANGED
@@ -76,8 +76,30 @@ export interface Clock {
76
76
  */
77
77
  measure<T>(callback: () => Promise<T>): Promise<Timed<T>>;
78
78
  measure<T>(callback: () => T): Timed<T>;
79
+ /**
80
+ * Settle with `promise`, or with {@link TIMED_OUT} once `ms` have elapsed
81
+ * first. A promise that rejects first rejects this one; one that rejects
82
+ * after the timer is ignored. The timer is disposed the moment `promise`
83
+ * settles, so a won race leaves nothing scheduled, but `promise` itself
84
+ * runs on: nothing here can cancel it.
85
+ *
86
+ * Unlike VS Code's `raceTimeout`, this settles with a sentinel rather than
87
+ * `undefined`, so a promise resolving to `undefined` is not taken for a
88
+ * timeout; the caller branches on the result instead of passing an
89
+ * `onTimeout`.
90
+ *
91
+ * A custom implementation extends {@link SystemClock}, whose race runs over
92
+ * the subclass's {@link setTimer}.
93
+ */
94
+ raceTimer<T>(promise: Promise<T>, ms: number): Promise<T | typeof TIMED_OUT>;
79
95
  }
80
96
 
97
+ /**
98
+ * What {@link Clock.raceTimer} settles with when its timer fires first.
99
+ * Registered under a global key, so two copies of this package agree on it.
100
+ */
101
+ export const TIMED_OUT = Symbol.for('hydranium/protocol/timed-out');
102
+
81
103
  /**
82
104
  * The result of {@link Clock.measure}: a callback's return value paired with
83
105
  * its elapsed wall-time in milliseconds. A plain value object — no methods, no
@@ -144,6 +166,10 @@ function unrefTimer(handle: { unref?: () => void }): void {
144
166
  * Default {@link Clock}: delegates to the platform's `Date.now`,
145
167
  * `performance.now`, and `setTimeout`/`clearTimeout`. Bound everywhere in
146
168
  * production; swapped for `makeFakeClock` in tests.
169
+ *
170
+ * {@link measure} and {@link raceTimer} run over {@link stopwatch} and
171
+ * {@link setTimer}, so a clock that extends this one and replaces those
172
+ * measures and races on its own time, as the test clock does.
147
173
  */
148
174
  export class SystemClock implements Clock {
149
175
  now(): number {
@@ -170,4 +196,34 @@ export class SystemClock implements Clock {
170
196
  }
171
197
  return { result: result as T, elapsedMs: stopwatch.elapsedMs };
172
198
  }
199
+
200
+ /**
201
+ * The timer settles the race one microtask after it fires. A real timer
202
+ * runs only once pending resolutions are delivered, while a fake one fires
203
+ * inside its `advance`, so settling at once would let a promise already
204
+ * resolved when `advance` runs lose to a timer it beats in production. The
205
+ * deferral covers only that promise: one that settles through further
206
+ * chained reactions, such as a `.then` chain, an async function or
207
+ * `Promise.allSettled`, can still lose to an `advance` in the same turn,
208
+ * while it wins on the platform's timer. The handlers go on `promise`
209
+ * before {@link setTimer} runs, so a `setTimer` that throws still leaves
210
+ * `promise` observed.
211
+ */
212
+ raceTimer<T>(promise: Promise<T>, ms: number): Promise<T | typeof TIMED_OUT> {
213
+ return new Promise<T | typeof TIMED_OUT>((resolve, reject) => {
214
+ // eslint-disable-next-line prefer-const -- with a `const`, a `setTimer` that throws leaves it uninitialised, and the handlers' later read of it is an unhandled ReferenceError
215
+ let timer: Disposable | undefined;
216
+ promise.then(
217
+ value => {
218
+ timer?.dispose();
219
+ resolve(value);
220
+ },
221
+ (err: unknown) => {
222
+ timer?.dispose();
223
+ reject(err);
224
+ }
225
+ );
226
+ timer = this.setTimer(() => void Promise.resolve().then(() => resolve(TIMED_OUT)), ms);
227
+ });
228
+ }
173
229
  }
@@ -20,6 +20,10 @@ import type {
20
20
 
21
21
  /** Request-method names on {@link DocumentServerProtocol}. */
22
22
  export const DOCUMENT_SERVER_PROTOCOL_METHODS = [
23
+ 'createSession',
24
+ 'closeSession',
25
+ 'createModelDocument',
26
+ 'updateModelDocuments',
23
27
  'openModelDocument',
24
28
  'closeModelDocument',
25
29
  'getModelDocument',
@@ -59,9 +63,13 @@ export const REFERENCE_SERVER_PROTOCOL_METHODS = [
59
63
  ] as const satisfies ReadonlyArray<keyof ReferenceServerProtocol<TransferElement> & string>;
60
64
 
61
65
  /** Notification-method names on {@link DocumentClientProtocol}. */
62
- export const DOCUMENT_CLIENT_PROTOCOL_METHODS = ['onDocumentUpdated', 'onDocumentSaved'] as const satisfies ReadonlyArray<
63
- keyof DocumentClientProtocol<TransferElement> & string
64
- >;
66
+ export const DOCUMENT_CLIENT_PROTOCOL_METHODS = [
67
+ 'onDocumentUpdated',
68
+ 'onDocumentSaved',
69
+ 'onDocumentDirtyChanged',
70
+ 'onDocumentDeleted',
71
+ 'onDocumentsBuilt'
72
+ ] as const satisfies ReadonlyArray<keyof DocumentClientProtocol<TransferElement> & string>;
65
73
 
66
74
  /** Notification-method names on {@link ProjectClientProtocol}. */
67
75
  export const PROJECT_CLIENT_PROTOCOL_METHODS = ['onProjectsChanged'] as const satisfies ReadonlyArray<keyof ProjectClientProtocol & string>;
@@ -13,13 +13,24 @@ import type { Project } from '../project';
13
13
  import type { TransferDocument } from '../transfer-document';
14
14
  import type { CloseModelArgs, FindNextNameArgs, OpenModelArgs, ReferenceContext, ReferenceRequest } from '../model-server';
15
15
  import type { ReferenceCandidate, ReferenceTarget } from '../model-service/reference-candidate';
16
- import type { ProjectsChangedEvent, TransferDocumentSavedEvent, TransferDocumentUpdatedEvent } from './events';
17
16
  import type {
17
+ ProjectsChangedEvent,
18
+ TransferDocumentDeletedEvent,
19
+ TransferDocumentDirtyChangedEvent,
20
+ TransferDocumentSavedEvent,
21
+ TransferDocumentsBuiltEvent,
22
+ TransferDocumentUpdatedEvent
23
+ } from './events';
24
+ import type {
25
+ CloseSessionArgs,
26
+ CreateModelDocumentArgs,
27
+ CreateSessionArgs,
18
28
  GetModelDocumentArgs,
19
29
  GetProjectForUriArgs,
20
30
  TransferSaveDocumentArgs,
21
31
  WatchModelDocumentArgs,
22
- TransferUpdateDocumentArgs
32
+ TransferUpdateDocumentArgs,
33
+ TransferUpdateDocumentsArgs
23
34
  } from './requests';
24
35
 
25
36
  /**
@@ -40,35 +51,75 @@ import type {
40
51
  */
41
52
  export interface DocumentServerProtocol<TTransfer extends TransferElement, TDiagnostic extends TransferDiagnostic = TransferDiagnostic> {
42
53
  /**
43
- * Open a document for an editor session and return its current state.
44
- * Registers `(uri, clientId)` with the multi-client document manager
45
- * (so concurrent editors share one built document) and returns the
46
- * document at the server's configured target phase — the same shape
47
- * {@link getModelDocument} returns, except that `version` is taken from
48
- * the text-document store so the caller's first `baseVersion` write
49
- * cannot self-conflict. Idempotent in registration terms: opening an
50
- * already-open document refreshes the client registration. Note it does
51
- * NOT avoid a rebuild — a second client attaching triggers
52
- * `refreshContent`, which fires a change event, so mounting a form on a
53
- * document already open in a text editor rebuilds it.
54
+ * Register `args.clientId` as a client session owned by this connection.
55
+ * Rejects with a `DuplicateClientIdError` code when the id is live anywhere
56
+ * in the server process, and with a `ReservedClientIdError` code when the
57
+ * framework reserves it.
58
+ *
59
+ * A request carrying a registered id acts as that session: it writes only
60
+ * what the session has open, failing with a `DocumentNotOpenError` code
61
+ * otherwise, and opens nothing implicitly; its open reads the file and takes
62
+ * no `languageId`, `version` or `text` seed. The session ends with
63
+ * {@link closeSession} or when the connection closes, and either closes
64
+ * every document it has open.
65
+ *
66
+ * A registration carrying the `resumeToken` an earlier registration of the
67
+ * same id carried ends that session first, from any connection, so a client
68
+ * whose connection dropped can register again before the server notices.
69
+ */
70
+ createSession(args: CreateSessionArgs): Promise<void>;
71
+
72
+ /**
73
+ * End a session this connection registered: close every document it has
74
+ * open, drop its watches, and free its id. A no-op for an id this
75
+ * connection did not register.
76
+ */
77
+ closeSession(args: CloseSessionArgs): Promise<void>;
78
+
79
+ /**
80
+ * Create a document with `args.text`, open for the session, and return it
81
+ * as {@link openModelDocument} does. It reaches disk with the first save.
82
+ * Fails when the file exists or any client has the URI open, and with a
83
+ * `SessionClosedError` code when `args.clientId` is not a session
84
+ * registered on this connection.
85
+ */
86
+ createModelDocument(args: CreateModelDocumentArgs): Promise<TransferDocument<TTransfer, TDiagnostic>>;
87
+
88
+ /**
89
+ * Write several documents the session has open, all or none: a conflict or
90
+ * a document not open fails the whole set before any text applies.
91
+ * Resolves to the documents in the order given. Only for a session
92
+ * registered on this connection.
93
+ */
94
+ updateModelDocuments(args: TransferUpdateDocumentsArgs<TTransfer>): Promise<TransferDocument<TTransfer, TDiagnostic>[]>;
95
+
96
+ /**
97
+ * Open a document for the client session `clientId` and return its current
98
+ * state. `clientId` must be a session this connection registered with
99
+ * {@link createSession}; any other id fails with the closed-session code,
100
+ * as every document request here does. The document is read from disk
101
+ * unless a client has it open already, so the request carries no
102
+ * `languageId`, `version` or `text` seed. Returns the document at
103
+ * the server's configured target phase — the same shape
104
+ * {@link getModelDocument} returns, except that `version` is taken from the
105
+ * text-document store so the caller's first `basedOn` write cannot
106
+ * self-conflict. A repeat open changes nothing.
54
107
  *
55
- * The default `DataServer` impl delegates to `ModelService.open` then
56
- * reads the built state. Pair with {@link watchModelDocument} to receive
57
- * subsequent build-phase events (open returns a one-shot snapshot;
58
- * later validation diagnostics arrive on the watch channel).
108
+ * Pair with {@link watchModelDocument} to receive subsequent build-phase
109
+ * events (open returns a one-shot snapshot; later validation diagnostics
110
+ * arrive on the watch channel).
59
111
  */
60
- openModelDocument(args: OpenModelArgs): Promise<TransferDocument<TTransfer, TDiagnostic>>;
112
+ openModelDocument(args: Pick<OpenModelArgs, 'uri' | 'clientId' | 'options'>): Promise<TransferDocument<TTransfer, TDiagnostic>>;
61
113
 
62
114
  /**
63
- * Close an editor session for `(uri, clientId)`. Counterpart to
64
- * {@link openModelDocument}; the underlying document stays built until
65
- * every registered client has closed. The default `DataServer` impl
66
- * delegates to `ModelService.close` and ALSO releases any watch for the
67
- * same `(uri, clientId)` (closing a session frees its own watch — a
68
- * forgotten {@link unwatchModelDocument} would otherwise leak dispatch;
69
- * the implicit unwatch is idempotent). `watchModelDocument` is NOT
70
- * coupled the other way: opening does not force a watch, so a snapshot
71
- * reader can open without streaming.
115
+ * Close the session's open of `uri`. Counterpart to
116
+ * {@link openModelDocument}; the document stays open until every client
117
+ * has closed it, and the last close reverts it to disk. The default
118
+ * `DataServer` impl ALSO releases any watch for the same
119
+ * `(uri, clientId)` (a forgotten {@link unwatchModelDocument} would
120
+ * otherwise leak dispatch; the implicit unwatch is idempotent).
121
+ * `watchModelDocument` is NOT coupled the other way: opening does not force
122
+ * a watch, so a snapshot reader can open without streaming.
72
123
  */
73
124
  closeModelDocument(args: CloseModelArgs): Promise<void>;
74
125
 
@@ -81,14 +132,15 @@ export interface DocumentServerProtocol<TTransfer extends TransferElement, TDiag
81
132
  getModelDocument(args: GetModelDocumentArgs): Promise<TransferDocument<TTransfer, TDiagnostic>>;
82
133
 
83
134
  /**
84
- * Update a document's content. The response carries the latest built
85
- * state including diagnostics; callers observe convergence via that
86
- * response.
135
+ * Update a document's content as the session `clientId`, which must have
136
+ * it open. The response carries the latest built state including
137
+ * diagnostics; callers observe convergence via that response.
87
138
  */
88
139
  updateModelDocument(args: TransferUpdateDocumentArgs<TTransfer>): Promise<TransferDocument<TTransfer, TDiagnostic>>;
89
140
 
90
141
  /**
91
- * Persist a document to disk. The response is the post-save document
142
+ * Write a document as the session `clientId`, which must have it open, and
143
+ * persist it to disk. The response is the post-save document
92
144
  * state (matches the file on disk). Save semantics depend on the
93
145
  * filesystem-provider wiring; rejection paths surface as awaited
94
146
  * promise rejections.
@@ -194,6 +246,22 @@ export interface DataServerProtocol<
194
246
  >
195
247
  extends DocumentServerProtocol<TTransfer, TDiagnostic>, ProjectServerProtocol<TProject> {}
196
248
 
249
+ /**
250
+ * The diagnostic shape a server answers with, read back off its own
251
+ * declaration.
252
+ *
253
+ * This is what lets a client tier stay generic over the server alone and still
254
+ * describe what it hands back. Both parameters must be inferred: naming
255
+ * {@link TransferElement} for the transfer instead fails to match while that
256
+ * parameter is still generic, and a default declared against the constraint
257
+ * then stops satisfying it.
258
+ */
259
+ export type DiagnosticOf<TServer> =
260
+ TServer extends DocumentServerProtocol<infer _TTransfer, infer TDiagnostic> ? TDiagnostic : TransferDiagnostic;
261
+
262
+ /** The project shape a server answers with, read back off its own declaration. */
263
+ export type ProjectOf<TServer> = TServer extends ProjectServerProtocol<infer TProject> ? TProject : Project;
264
+
197
265
  /**
198
266
  * Cross-reference / naming slice of the server surface — scope-aware
199
267
  * queries that resolve against the language's reference services. NOT part
@@ -262,6 +330,39 @@ export interface DocumentClientProtocol<TTransfer extends TransferElement, TDiag
262
330
  * codepath, not a subscription codepath).
263
331
  */
264
332
  onDocumentSaved(event: TransferDocumentSavedEvent<TTransfer, TDiagnostic>): void;
333
+
334
+ /**
335
+ * Delivered when a subscribed document's text starts or stops differing
336
+ * from its file, as the server last knew it. Gated like
337
+ * {@link onDocumentSaved}; the answer at any one moment is the `dirty` of
338
+ * the documents the server sends. After a reconnect, a `DataSession`
339
+ * delivers here the answer of each document it restored, when that differs
340
+ * from the last this client was told: the flips while the connection was
341
+ * down reached no one.
342
+ */
343
+ onDocumentDirtyChanged(event: TransferDocumentDirtyChangedEvent): void;
344
+
345
+ /**
346
+ * Delivered when a document's backing file was removed. Separate from
347
+ * {@link onDocumentUpdated} for the same reason as {@link onDocumentSaved},
348
+ * and more strongly: there is no built state to deliver, and the build-phase
349
+ * path cannot report a deletion at all.
350
+ *
351
+ * Ungated, because on a browser host — where the workspace lives behind the
352
+ * head — no other source can observe a file disappearing. Recipients that
353
+ * only care about their own documents filter by URI.
354
+ */
355
+ onDocumentDeleted(event: TransferDocumentDeletedEvent): void;
356
+
357
+ /**
358
+ * Delivered once per build, naming the documents that reached the
359
+ * subscription phase and that
360
+ * nobody on this connection watches — chiefly the ones rebuilt as a cascade
361
+ * from a dependency's change, which no filesystem watcher can see because
362
+ * their own files did not change. The complement of
363
+ * {@link onDocumentUpdated}; carries URIs and no documents.
364
+ */
365
+ onDocumentsBuilt(event: TransferDocumentsBuiltEvent): void;
265
366
  }
266
367
 
267
368
  /**
@@ -16,24 +16,30 @@ import type { TransferDocument } from '../transfer-document';
16
16
  * Why an update event fired. Subscribers filter on reason for behaviour
17
17
  * decisions (e.g. dirty-flag handling, undo-history grouping, telemetry):
18
18
  *
19
- * - `'changed'` — the URI appeared in `DocumentBuilder.onUpdate`'s
20
- * `changed` list. The framework's underlying primitive is "this URI was
21
- * passed to `documentBuilder.update(changed, deleted)`", which spans
22
- * `didChange` text-document events, `notifyDidChangeTextDocument` calls,
23
- * and any programmatic `documentBuilder.update([uri], [])` invocation.
24
- * The name matches Langium's own `changed` parameter — it's vague-on-
25
- * purpose because the underlying primitive is.
26
- * - `'rebuilt'` — the URI was rebuilt as a cascade from another URI's
27
- * build (dependency graph re-derivation), without itself being passed to
28
- * `documentBuilder.update`. The complement of `'changed'`.
19
+ * - `'changed'` — the server's first update event for the document's current
20
+ * version: its content changed since the last one. A write whose build a
21
+ * later write cancelled is still `'changed'` in the build that takes over.
22
+ * - `'rebuilt'` — a later event for a version the server already delivered,
23
+ * whether or not any client watched it then: something the document depends
24
+ * on changed, or the document was built again with the same content. The
25
+ * complement of `'changed'`.
29
26
  * - `'saved'` — emitted by adopters that synthesise a unified update stream
30
27
  * from both `onDocumentUpdated` and `onDocumentSaved`. The framework's own
31
28
  * `dispatchPhaseEvent` does NOT emit `'saved'` — saves take the dedicated
32
29
  * `DataClientProtocol.onDocumentSaved` channel.
33
- * - `'deleted'` — the URI appeared in `DocumentBuilder.onUpdate`'s
34
- * `deleted` list. The backing file was removed.
30
+ *
31
+ * The version rule needs a version the server keeps and builds that validate.
32
+ * For a document no client has opened, or when rebuilds do not validate,
33
+ * `'changed'` means the URI was passed to `DocumentBuilder.update` for this
34
+ * build. A document that a validating build skips is `'changed'` every time.
35
+ *
36
+ * Deletion is deliberately NOT a member. An update event carries a built
37
+ * document, which a deleted one has none of, and the phase-driven path that
38
+ * produces these events never runs for a deleted URI — the builder drops the
39
+ * document before deriving the rebuild set. It travels as
40
+ * {@link TransferDocumentDeletedEvent} on its own channel instead.
35
41
  */
36
- export type TransferDocumentUpdateReason = 'changed' | 'rebuilt' | 'saved' | 'deleted';
42
+ export type TransferDocumentUpdateReason = 'changed' | 'rebuilt' | 'saved';
37
43
 
38
44
  /**
39
45
  * Delivered on the data-server when a document's content (or existence)
@@ -47,7 +53,13 @@ export interface TransferDocumentUpdatedEvent<
47
53
  TDiagnostic extends TransferDiagnostic = TransferDiagnostic
48
54
  > {
49
55
  document: TransferDocument<TTransfer, TDiagnostic>;
50
- /** Stable identifier of the client that triggered the update. */
56
+ /**
57
+ * The client whose write this event echoes: the author of the version on a
58
+ * `'changed'`, and the unknown-client id on a `'rebuilt'`, which echoes no
59
+ * write. A recipient compares it against its own id to recognise its echo.
60
+ * The rebuild that reverts a document to disk after its last close names
61
+ * the revert-on-close id instead.
62
+ */
51
63
  sourceClientId: string;
52
64
  reason: TransferDocumentUpdateReason;
53
65
  }
@@ -91,6 +103,89 @@ export type TransferDocumentSavedListener<
91
103
  TDiagnostic extends TransferDiagnostic = TransferDiagnostic
92
104
  > = (event: TransferDocumentSavedEvent<TTransfer, TDiagnostic>) => void;
93
105
 
106
+ /**
107
+ * Delivered on the data-server when a watched document's text starts or stops
108
+ * differing from its file, as the server last knew the file. The current
109
+ * answer rides on every transfer document the server sends as `dirty`; this
110
+ * carries only its changes, so a client that follows it needs no rebuild to
111
+ * learn of a save.
112
+ */
113
+ export interface TransferDocumentDirtyChangedEvent {
114
+ /** Canonical URI, keyed as the subscription is. */
115
+ readonly uri: string;
116
+ readonly dirty: boolean;
117
+ }
118
+
119
+ /** Callback shape for `DataClientProtocol.onDocumentDirtyChanged`. */
120
+ export type TransferDocumentDirtyChangedListener = (event: TransferDocumentDirtyChangedEvent) => void;
121
+
122
+ /**
123
+ * Delivered on the data-server when a document's backing file was removed.
124
+ * Carries no document, and cannot: the state a
125
+ * {@link TransferDocumentUpdatedEvent} would have to carry no longer exists by
126
+ * the time anyone can be told. That is also why deletion is not a `reason` on
127
+ * the update stream — `DocumentBuilder.update` drops the document before
128
+ * deriving the rebuild set, so the phase-driven path that produces update
129
+ * events never runs for it.
130
+ *
131
+ * **Delivered for EVERY document, not only watched ones**, unlike
132
+ * `onDocumentUpdated` and `onDocumentSaved`. The test that decides which
133
+ * channels are gated is whether any OTHER source can observe the fact on the
134
+ * least capable host: a browser-hosted client's workspace lives behind the
135
+ * head, so nothing there can see a file disappear, and gating the notification
136
+ * would leave it blind. (A Theia frontend's filesystem watcher would cover it,
137
+ * which is why this is a host argument and not a structure-versus-content one.)
138
+ * A watcher is told about its own document's deletion here too, the update
139
+ * channel being silent for deletions by construction. Filter on {@link uri} if
140
+ * the receiver only cares about documents it opened.
141
+ *
142
+ * A watch survives the deletion, so a file that comes back resumes delivering
143
+ * `onDocumentUpdated` to the same subscribers with no re-subscription. A
144
+ * client that responds by closing its editor releases the watch through
145
+ * `closeModelDocument` as usual.
146
+ */
147
+ export interface TransferDocumentDeletedEvent {
148
+ /** Canonical URI of the removed document, keyed as the subscription is. */
149
+ readonly uri: string;
150
+ }
151
+
152
+ /** Callback shape for `DataClientProtocol.onDocumentDeleted`. */
153
+ export type TransferDocumentDeletedListener = (event: TransferDocumentDeletedEvent) => void;
154
+
155
+ /**
156
+ * Delivered on the data-server once per build, naming the documents that reached
157
+ * the configured subscription phase (`DataServerOptions.subscriptionPhase`,
158
+ * `Validated` by default) and that NO client on the connection is watching. Not
159
+ * the integrity-settled landmark, which is a different point and a different
160
+ * word in this framework.
161
+ *
162
+ * The complement of {@link TransferDocumentUpdatedEvent}, which is gated per
163
+ * URI: together the two cover every document a build touched. This one exists
164
+ * for the case no source outside the server can observe — a document rebuilt
165
+ * because something it DEPENDS ON changed. Its own file never changed, so a
166
+ * filesystem watcher cannot see it, and it has no subscriber, so the update
167
+ * channel does not report it. A consumer showing data derived from such a
168
+ * document (a tree label, a decorator) would otherwise hold a stale value with
169
+ * nothing to invalidate it.
170
+ *
171
+ * Carries URIs and no documents: a recipient re-reads what it displays, through
172
+ * `getModelDocument` or its own request. That keeps the bandwidth property the
173
+ * per-URI subscription exists for, without gating the message.
174
+ *
175
+ * Not sent when the set is empty, which is the normal case while editing — the
176
+ * document being edited is watched by its own editor and therefore excluded.
177
+ * Workspace initialisation sends nothing either: it does not build to the
178
+ * subscription phase. The largest message a workspace can produce is therefore
179
+ * a whole-workspace rebuild at that phase, which is one message of URIs.
180
+ */
181
+ export interface TransferDocumentsBuiltEvent {
182
+ /** Canonical URIs, keyed as subscriptions are. Never empty. */
183
+ readonly uris: readonly string[];
184
+ }
185
+
186
+ /** Callback shape for `DataClientProtocol.onDocumentsBuilt`. */
187
+ export type TransferDocumentsBuiltListener = (event: TransferDocumentsBuiltEvent) => void;
188
+
94
189
  /**
95
190
  * Why a project-change event fired. `added` — the project was newly
96
191
  * registered (descriptor discovered); `updated` — the project's
@@ -7,7 +7,7 @@
7
7
  * SPDX-License-Identifier: MIT
8
8
  ********************************************************************************/
9
9
 
10
- import type { TransferSaveArgs, TransferUpdateArgs } from '../model-service/args';
10
+ import type { BasedOn } from '../model-service/based-on';
11
11
 
12
12
  /** Get the current state of a single document. The server returns the latest built version. */
13
13
  export interface GetModelDocumentArgs {
@@ -41,24 +41,84 @@ export interface GetProjectForUriArgs {
41
41
  }
42
42
 
43
43
  /**
44
- * Update a document's content. Wire-side projection of the facade's
45
- * {@link TransferUpdateArgs}; structurally identical so the data-server RPC
46
- * handler can forward straight to the in-process `ModelService.update`
47
- * without an args mapping.
44
+ * Update a document the session `clientId` has open. Generic over `TTransfer`
45
+ * so adopters parameterise the structured shape against their grammar's
46
+ * transfer-model overlay; passing a string is always allowed.
48
47
  */
49
- export type TransferUpdateDocumentArgs<TTransfer> = TransferUpdateArgs<TTransfer>;
48
+ export interface TransferUpdateDocumentArgs<TTransfer> {
49
+ /** Document URI. */
50
+ uri: string;
51
+ /** The id of a live session registered on this connection. */
52
+ clientId: string;
53
+ /** The whole structured model root, or its serialised textual form. */
54
+ model: TTransfer | string;
55
+ /**
56
+ * What this write was authored against. A `SnapshotVersion` is compared
57
+ * against the server's current text-document version for `uri` and throws
58
+ * `ConflictError` on mismatch; `'anything'` writes unconditionally.
59
+ *
60
+ * **Required so that an ungated write is a decision rather than an
61
+ * omission.** An optional gate is indistinguishable from a forgotten one at
62
+ * the call site, and a file of twenty writes hides the one that lost the
63
+ * field. Nothing else here can see that, since the defect is an absence.
64
+ */
65
+ basedOn: BasedOn;
66
+ }
67
+
68
+ /** Update a document the session `clientId` has open, then persist it to disk. */
69
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
70
+ export interface TransferSaveDocumentArgs<TTransfer> extends TransferUpdateDocumentArgs<TTransfer> {}
71
+
72
+ /** Register a client session on the connection. */
73
+ export interface CreateSessionArgs {
74
+ /** The session's id, minted by the client; unique in the server process while the session is live. */
75
+ clientId: string;
76
+ /** What the participant is; kept as the server session's `label`. */
77
+ label?: string;
78
+ /**
79
+ * A token the client keeps for the session's lifetime. It is no secret: the
80
+ * wire carries no authentication. A later `createSession` for the same id
81
+ * carrying the same token, from any connection, ends the session it names
82
+ * and registers the id afresh, so a client whose connection dropped before
83
+ * the server noticed can register again. Without it the id stays refused
84
+ * until the server notices.
85
+ */
86
+ resumeToken?: string;
87
+ }
88
+
89
+ /** End a client session registered on the connection. */
90
+ export interface CloseSessionArgs {
91
+ clientId: string;
92
+ }
93
+
94
+ /** Create a document that exists nowhere yet, open for the session creating it. */
95
+ export interface CreateModelDocumentArgs {
96
+ uri: string;
97
+ /** The id of a live session registered on this connection. */
98
+ clientId: string;
99
+ /** The document's initial content; it reaches disk with the first save. */
100
+ text: string;
101
+ }
50
102
 
51
- /** Persist a document to disk. Wire-side projection of {@link TransferSaveArgs}. */
52
- export type TransferSaveDocumentArgs<TTransfer> = TransferSaveArgs<TTransfer>;
103
+ /**
104
+ * Write several documents a session has open, all or none. `clientId` is on
105
+ * the set rather than on each update: an id per update would allow a set
106
+ * mixing clients, which the server would have to refuse.
107
+ */
108
+ export interface TransferUpdateDocumentsArgs<TTransfer> {
109
+ /** The id of a live session registered on this connection. */
110
+ clientId: string;
111
+ updates: Omit<TransferUpdateDocumentArgs<TTransfer>, 'clientId'>[];
112
+ }
53
113
 
54
114
  /**
55
115
  * Identifies a per-document watch on the data server. Shared by both
56
116
  * `watchModelDocument` and `unwatchModelDocument` — the `(uri, clientId)`
57
117
  * pair is the watch key, so unwatching names the same watch that was
58
118
  * started. The `clientId` identifies the originator the same way it does
59
- * on facade-side mutations (`TransferUpdateArgs.clientId`,
60
- * `TransferSaveArgs.clientId`): it keys the per-`(uri, clientId)` watch
61
- * bucket so multiple watchers on the same wire stay distinct, AND it lets
119
+ * on the document writes (`TransferUpdateDocumentArgs.clientId`,
120
+ * `TransferSaveDocumentArgs.clientId`): it keys the per-`(uri, clientId)`
121
+ * watch bucket so multiple watchers on the same wire stay distinct, AND it lets
62
122
  * each watcher recognise its own echo on inbound `onDocumentUpdated` events
63
123
  * (the wire shape's `sourceClientId` carries the originating mutation's
64
124
  * `clientId`).