@hydranium/protocol 1.0.0-next.23 → 1.0.0-next.230

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 (246) hide show
  1. package/README.md +40 -3
  2. package/lib/abstract-logger.d.ts +5 -0
  3. package/lib/abstract-logger.d.ts.map +1 -1
  4. package/lib/abstract-logger.js +7 -0
  5. package/lib/abstract-logger.js.map +1 -1
  6. package/lib/client/data-connection.d.ts +236 -0
  7. package/lib/client/data-connection.d.ts.map +1 -0
  8. package/lib/client/data-connection.js +404 -0
  9. package/lib/client/data-connection.js.map +1 -0
  10. package/lib/client/data-events.d.ts +13 -1
  11. package/lib/client/data-events.d.ts.map +1 -1
  12. package/lib/client/data-events.js +21 -0
  13. package/lib/client/data-events.js.map +1 -1
  14. package/lib/client/data-port.d.ts +27 -22
  15. package/lib/client/data-port.d.ts.map +1 -1
  16. package/lib/client/data-session.d.ts +473 -81
  17. package/lib/client/data-session.d.ts.map +1 -1
  18. package/lib/client/data-session.js +743 -108
  19. package/lib/client/data-session.js.map +1 -1
  20. package/lib/client/index.d.ts +14 -9
  21. package/lib/client/index.d.ts.map +1 -1
  22. package/lib/client/index.js +14 -9
  23. package/lib/client/index.js.map +1 -1
  24. package/lib/client/message-relay.d.ts +9 -3
  25. package/lib/client/message-relay.d.ts.map +1 -1
  26. package/lib/client/message-relay.js +11 -5
  27. package/lib/client/message-relay.js.map +1 -1
  28. package/lib/client/post-message-transport.d.ts +64 -3
  29. package/lib/client/post-message-transport.d.ts.map +1 -1
  30. package/lib/client/post-message-transport.js +175 -1
  31. package/lib/client/post-message-transport.js.map +1 -1
  32. package/lib/client/rpc-connection.d.ts +157 -0
  33. package/lib/client/rpc-connection.d.ts.map +1 -0
  34. package/lib/client/rpc-connection.js +214 -0
  35. package/lib/client/rpc-connection.js.map +1 -0
  36. package/lib/client-ids.d.ts +45 -0
  37. package/lib/client-ids.d.ts.map +1 -0
  38. package/lib/client-ids.js +48 -0
  39. package/lib/client-ids.js.map +1 -0
  40. package/lib/clock.d.ts +38 -0
  41. package/lib/clock.d.ts.map +1 -1
  42. package/lib/clock.js +36 -1
  43. package/lib/clock.js.map +1 -1
  44. package/lib/console-logger.d.ts +23 -0
  45. package/lib/console-logger.d.ts.map +1 -0
  46. package/lib/console-logger.js +39 -0
  47. package/lib/console-logger.js.map +1 -0
  48. package/lib/data/data-protocol-methods.d.ts +4 -4
  49. package/lib/data/data-protocol-methods.d.ts.map +1 -1
  50. package/lib/data/data-protocol-methods.js +12 -1
  51. package/lib/data/data-protocol-methods.js.map +1 -1
  52. package/lib/data/data-server-protocol.d.ts +132 -41
  53. package/lib/data/data-server-protocol.d.ts.map +1 -1
  54. package/lib/data/events.d.ts +117 -21
  55. package/lib/data/events.d.ts.map +1 -1
  56. package/lib/data/requests.d.ts +69 -11
  57. package/lib/data/requests.d.ts.map +1 -1
  58. package/lib/debouncer.d.ts.map +1 -1
  59. package/lib/debouncer.js.map +1 -1
  60. package/lib/errors.d.ts +187 -29
  61. package/lib/errors.d.ts.map +1 -1
  62. package/lib/errors.js +270 -29
  63. package/lib/errors.js.map +1 -1
  64. package/lib/glsp-request-model-args.d.ts +16 -0
  65. package/lib/glsp-request-model-args.d.ts.map +1 -0
  66. package/lib/glsp-request-model-args.js +19 -0
  67. package/lib/glsp-request-model-args.js.map +1 -0
  68. package/lib/glsp-save-model-actions.d.ts +50 -0
  69. package/lib/glsp-save-model-actions.d.ts.map +1 -0
  70. package/lib/glsp-save-model-actions.js +28 -0
  71. package/lib/glsp-save-model-actions.js.map +1 -0
  72. package/lib/index.d.ts +7 -0
  73. package/lib/index.d.ts.map +1 -1
  74. package/lib/index.js +10 -0
  75. package/lib/index.js.map +1 -1
  76. package/lib/latency-collector.d.ts +8 -4
  77. package/lib/latency-collector.d.ts.map +1 -1
  78. package/lib/latency-collector.js.map +1 -1
  79. package/lib/logger.d.ts +22 -1
  80. package/lib/logger.d.ts.map +1 -1
  81. package/lib/logger.js +31 -3
  82. package/lib/logger.js.map +1 -1
  83. package/lib/messages/index.d.ts +30 -0
  84. package/lib/messages/index.d.ts.map +1 -0
  85. package/lib/messages/index.js +62 -0
  86. package/lib/messages/index.js.map +1 -0
  87. package/lib/messages/primitives.d.ts +188 -0
  88. package/lib/messages/primitives.d.ts.map +1 -0
  89. package/lib/messages/primitives.js +161 -0
  90. package/lib/messages/primitives.js.map +1 -0
  91. package/lib/model-server.d.ts +60 -13
  92. package/lib/model-server.d.ts.map +1 -1
  93. package/lib/model-server.js +4 -2
  94. package/lib/model-server.js.map +1 -1
  95. package/lib/model-service/base-version.d.ts +64 -0
  96. package/lib/model-service/base-version.d.ts.map +1 -0
  97. package/lib/model-service/base-version.js +43 -0
  98. package/lib/model-service/base-version.js.map +1 -0
  99. package/lib/model-service/index.d.ts +1 -1
  100. package/lib/model-service/index.d.ts.map +1 -1
  101. package/lib/model-service/index.js +4 -5
  102. package/lib/model-service/index.js.map +1 -1
  103. package/lib/model-service/reference-candidate.d.ts +5 -3
  104. package/lib/model-service/reference-candidate.d.ts.map +1 -1
  105. package/lib/{model-service/args.js → node/index.d.ts} +2 -3
  106. package/lib/node/index.d.ts.map +1 -0
  107. package/lib/node/index.js +29 -0
  108. package/lib/node/index.js.map +1 -0
  109. package/lib/node/process-memory.d.ts +66 -0
  110. package/lib/node/process-memory.d.ts.map +1 -0
  111. package/lib/node/process-memory.js +291 -0
  112. package/lib/node/process-memory.js.map +1 -0
  113. package/lib/noop-logger.d.ts.map +1 -1
  114. package/lib/noop-logger.js.map +1 -1
  115. package/lib/observable-value.js.map +1 -1
  116. package/lib/patch-merge.d.ts +35 -32
  117. package/lib/patch-merge.d.ts.map +1 -1
  118. package/lib/patch-merge.js +67 -23
  119. package/lib/patch-merge.js.map +1 -1
  120. package/lib/profile-session.d.ts +8 -4
  121. package/lib/profile-session.d.ts.map +1 -1
  122. package/lib/profile-session.js.map +1 -1
  123. package/lib/random-uuid.d.ts +14 -0
  124. package/lib/random-uuid.d.ts.map +1 -0
  125. package/lib/random-uuid.js +24 -0
  126. package/lib/random-uuid.js.map +1 -0
  127. package/lib/reconcile-write.d.ts +65 -0
  128. package/lib/reconcile-write.d.ts.map +1 -0
  129. package/lib/reconcile-write.js +67 -0
  130. package/lib/reconcile-write.js.map +1 -0
  131. package/lib/rpc/bind-rpc-methods.d.ts +33 -3
  132. package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
  133. package/lib/rpc/bind-rpc-methods.js +32 -3
  134. package/lib/rpc/bind-rpc-methods.js.map +1 -1
  135. package/lib/rpc/create-rpc-proxy.d.ts +10 -0
  136. package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
  137. package/lib/rpc/create-rpc-proxy.js +12 -2
  138. package/lib/rpc/create-rpc-proxy.js.map +1 -1
  139. package/lib/rpc/index.d.ts +1 -0
  140. package/lib/rpc/index.d.ts.map +1 -1
  141. package/lib/rpc/index.js +1 -0
  142. package/lib/rpc/index.js.map +1 -1
  143. package/lib/rpc/send-by-method-name.d.ts +76 -0
  144. package/lib/rpc/send-by-method-name.d.ts.map +1 -0
  145. package/lib/rpc/send-by-method-name.js +120 -0
  146. package/lib/rpc/send-by-method-name.js.map +1 -0
  147. package/lib/rpc/wire-prefix.js.map +1 -1
  148. package/lib/testing/catalogue-audit.d.ts +80 -0
  149. package/lib/testing/catalogue-audit.d.ts.map +1 -0
  150. package/lib/testing/catalogue-audit.js +94 -0
  151. package/lib/testing/catalogue-audit.js.map +1 -0
  152. package/lib/testing/data-doubles.d.ts +42 -15
  153. package/lib/testing/data-doubles.d.ts.map +1 -1
  154. package/lib/testing/data-doubles.js +58 -10
  155. package/lib/testing/data-doubles.js.map +1 -1
  156. package/lib/testing/fake-clock.d.ts +9 -1
  157. package/lib/testing/fake-clock.d.ts.map +1 -1
  158. package/lib/testing/fake-clock.js +54 -45
  159. package/lib/testing/fake-clock.js.map +1 -1
  160. package/lib/testing/index.d.ts +1 -0
  161. package/lib/testing/index.d.ts.map +1 -1
  162. package/lib/testing/index.js +5 -2
  163. package/lib/testing/index.js.map +1 -1
  164. package/lib/testing/node/duplex-connection.d.ts.map +1 -1
  165. package/lib/testing/node/duplex-connection.js +3 -2
  166. package/lib/testing/node/duplex-connection.js.map +1 -1
  167. package/lib/testing/node/duplex-stream.js.map +1 -1
  168. package/lib/testing/node/index.d.ts +1 -0
  169. package/lib/testing/node/index.d.ts.map +1 -1
  170. package/lib/testing/node/index.js +2 -2
  171. package/lib/testing/node/index.js.map +1 -1
  172. package/lib/testing/node/message-port-pair.d.ts +25 -0
  173. package/lib/testing/node/message-port-pair.d.ts.map +1 -0
  174. package/lib/testing/node/message-port-pair.js +26 -0
  175. package/lib/testing/node/message-port-pair.js.map +1 -0
  176. package/lib/testing/wait-for.js.map +1 -1
  177. package/lib/tracer.d.ts.map +1 -1
  178. package/lib/tracer.js.map +1 -1
  179. package/lib/transfer-diagnostic.d.ts +33 -0
  180. package/lib/transfer-diagnostic.d.ts.map +1 -1
  181. package/lib/transfer-diagnostic.js +23 -0
  182. package/lib/transfer-diagnostic.js.map +1 -1
  183. package/lib/transfer-document.d.ts +70 -32
  184. package/lib/transfer-document.d.ts.map +1 -1
  185. package/lib/transfer-document.js +17 -9
  186. package/lib/transfer-document.js.map +1 -1
  187. package/lib/uri.d.ts.map +1 -1
  188. package/lib/uri.js.map +1 -1
  189. package/lib/util.d.ts +8 -0
  190. package/lib/util.d.ts.map +1 -1
  191. package/lib/util.js +32 -0
  192. package/lib/util.js.map +1 -1
  193. package/package.json +29 -37
  194. package/src/abstract-logger.ts +8 -0
  195. package/src/client/data-connection.ts +502 -0
  196. package/src/client/data-events.ts +33 -1
  197. package/src/client/data-port.ts +29 -23
  198. package/src/client/data-session.ts +951 -126
  199. package/src/client/index.ts +14 -9
  200. package/src/client/message-relay.ts +29 -7
  201. package/src/client/post-message-transport.ts +219 -4
  202. package/src/client/rpc-connection.ts +281 -0
  203. package/src/client-ids.ts +49 -0
  204. package/src/clock.ts +56 -0
  205. package/src/console-logger.ts +39 -0
  206. package/src/data/data-protocol-methods.ts +13 -4
  207. package/src/data/data-server-protocol.ts +157 -41
  208. package/src/data/events.ts +123 -21
  209. package/src/data/requests.ts +74 -11
  210. package/src/errors.ts +322 -36
  211. package/src/glsp-request-model-args.ts +16 -0
  212. package/src/glsp-save-model-actions.ts +59 -0
  213. package/src/index.ts +10 -0
  214. package/src/latency-collector.ts +8 -3
  215. package/src/logger.ts +28 -2
  216. package/src/messages/index.ts +37 -0
  217. package/src/messages/primitives.ts +271 -0
  218. package/src/model-server.ts +63 -18
  219. package/src/model-service/base-version.ts +72 -0
  220. package/src/model-service/index.ts +4 -5
  221. package/src/model-service/reference-candidate.ts +5 -3
  222. package/src/node/index.ts +14 -0
  223. package/src/node/process-memory.ts +299 -0
  224. package/src/patch-merge.ts +97 -42
  225. package/src/profile-session.ts +9 -4
  226. package/src/random-uuid.ts +21 -0
  227. package/src/reconcile-write.ts +124 -0
  228. package/src/rpc/README.md +4 -5
  229. package/src/rpc/bind-rpc-methods.ts +59 -4
  230. package/src/rpc/create-rpc-proxy.ts +20 -2
  231. package/src/rpc/index.ts +1 -0
  232. package/src/rpc/send-by-method-name.ts +140 -0
  233. package/src/testing/catalogue-audit.ts +111 -0
  234. package/src/testing/data-doubles.ts +145 -25
  235. package/src/testing/fake-clock.ts +62 -47
  236. package/src/testing/index.ts +5 -2
  237. package/src/testing/node/duplex-connection.ts +3 -2
  238. package/src/testing/node/index.ts +2 -2
  239. package/src/testing/node/message-port-pair.ts +40 -0
  240. package/src/transfer-diagnostic.ts +40 -0
  241. package/src/transfer-document.ts +87 -34
  242. package/src/util.ts +33 -0
  243. package/lib/model-service/args.d.ts +0 -64
  244. package/lib/model-service/args.d.ts.map +0 -1
  245. package/lib/model-service/args.js.map +0 -1
  246. 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 for the build that follows a document's release, once no client holds
39
+ * it; by default that build reverts the document to its disk content,
40
+ * discarding unsaved edits. Not a real client: it lets consumers tell the
41
+ * release broadcast from client-authored updates.
42
+ */
43
+ export const DOCUMENT_RELEASE_CLIENT_ID = 'document-release';
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, DOCUMENT_RELEASE_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
  }
@@ -0,0 +1,39 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 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
+ import { AbstractLogger } from './abstract-logger';
11
+ import { type LogLevel, type Logger } from './logger';
12
+
13
+ /** `console.trace` prints a stack with every line, so `trace` goes out at `debug`. */
14
+ const CONSOLE_METHODS: Record<LogLevel, 'error' | 'warn' | 'info' | 'debug'> = {
15
+ error: 'error',
16
+ warn: 'warn',
17
+ info: 'info',
18
+ debug: 'debug',
19
+ trace: 'debug'
20
+ };
21
+
22
+ /**
23
+ * {@link Logger} that writes each line to `console`, at the method matching its
24
+ * level: the framework's `[Label - time] [component]` prefix and the message,
25
+ * then any trailing arguments as they are, so a browser console can expand
26
+ * them. For a browser page, webview or extension host, which has no other sink
27
+ * to hand. On Node, `info` and `debug` reach stdout, so a process whose stdout
28
+ * carries a protocol stream must not use it.
29
+ */
30
+ export class ConsoleLogger extends AbstractLogger implements Logger {
31
+ protected emit(level: LogLevel, label: string, message: string, args: readonly unknown[]): void {
32
+ console[CONSOLE_METHODS[level]](`${this.formatLinePrefix(label)} ${message}`, ...args);
33
+ }
34
+
35
+ protected derive(component: string): this {
36
+ const Subclass = this.constructor as new (component?: string) => this;
37
+ return new Subclass(component);
38
+ }
39
+ }
@@ -20,11 +20,16 @@ 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',
26
30
  'updateModelDocument',
27
31
  'saveModelDocument',
32
+ 'persistModelDocument',
28
33
  'watchModelDocument',
29
34
  'unwatchModelDocument',
30
35
  'waitForReady'
@@ -56,12 +61,16 @@ export const REFERENCE_SERVER_PROTOCOL_METHODS = [
56
61
  'findReferenceCandidates',
57
62
  'resolveReference',
58
63
  'findNextName'
59
- ] as const satisfies ReadonlyArray<keyof ReferenceServerProtocol<TransferElement> & string>;
64
+ ] as const satisfies ReadonlyArray<keyof ReferenceServerProtocol & string>;
60
65
 
61
66
  /** 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
- >;
67
+ export const DOCUMENT_CLIENT_PROTOCOL_METHODS = [
68
+ 'onDocumentUpdated',
69
+ 'onDocumentSaved',
70
+ 'onDocumentDirtyChanged',
71
+ 'onDocumentDeleted',
72
+ 'onDocumentsBuilt'
73
+ ] as const satisfies ReadonlyArray<keyof DocumentClientProtocol<TransferElement> & string>;
65
74
 
66
75
  /** Notification-method names on {@link ProjectClientProtocol}. */
67
76
  export const PROJECT_CLIENT_PROTOCOL_METHODS = ['onProjectsChanged'] as const satisfies ReadonlyArray<keyof ProjectClientProtocol & string>;
@@ -10,16 +10,28 @@
10
10
  import type { TransferDiagnostic } from '../transfer-diagnostic';
11
11
  import type { TransferElement } from '../transfer-element';
12
12
  import type { Project } from '../project';
13
- import type { TransferDocument } from '../transfer-document';
13
+ import type { TransferDocument, TransferSavedDocument } 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,
30
+ TransferPersistDocumentArgs,
20
31
  TransferSaveDocumentArgs,
21
32
  WatchModelDocumentArgs,
22
- TransferUpdateDocumentArgs
33
+ TransferUpdateDocumentArgs,
34
+ TransferUpdateDocumentsArgs
23
35
  } from './requests';
24
36
 
25
37
  /**
@@ -40,35 +52,74 @@ import type {
40
52
  */
41
53
  export interface DocumentServerProtocol<TTransfer extends TransferElement, TDiagnostic extends TransferDiagnostic = TransferDiagnostic> {
42
54
  /**
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.
55
+ * Register `args.clientId` as a client session owned by this connection.
56
+ * Rejects with a `DuplicateClientIdError` code when the id is live anywhere
57
+ * in the server process, and with a `ReservedClientIdError` code when the
58
+ * framework reserves it.
59
+ *
60
+ * A request carrying a registered id acts as that session: it writes only
61
+ * what the session has open, failing with a `DocumentNotOpenError` code
62
+ * otherwise, and opens nothing implicitly; its open reads the file and takes
63
+ * no `languageId`, `version` or `text` seed. The session ends with
64
+ * {@link closeSession} or when the connection closes, and either closes
65
+ * every document it has open.
54
66
  *
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).
67
+ * A registration carrying the `resumeToken` an earlier registration of the
68
+ * same id carried ends that session first, from any connection, so a client
69
+ * whose connection dropped can register again before the server notices.
70
+ */
71
+ createSession(args: CreateSessionArgs): Promise<void>;
72
+
73
+ /**
74
+ * End a session this connection registered: close every document it has
75
+ * open, drop its watches, and free its id. A no-op for an id this
76
+ * connection did not register.
59
77
  */
60
- openModelDocument(args: OpenModelArgs): Promise<TransferDocument<TTransfer, TDiagnostic>>;
78
+ closeSession(args: CloseSessionArgs): Promise<void>;
61
79
 
62
80
  /**
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.
81
+ * Create a document with `args.text`, open for the session, and return it
82
+ * as {@link openModelDocument} does. It reaches disk with the first save.
83
+ * Fails when the file exists or any client has the URI open, and with a
84
+ * `SessionClosedError` code when `args.clientId` is not a session
85
+ * registered on this connection.
86
+ */
87
+ createModelDocument(args: CreateModelDocumentArgs): Promise<TransferDocument<TTransfer, TDiagnostic>>;
88
+
89
+ /**
90
+ * Write several documents the session has open, all or none: a conflict or
91
+ * a document not open fails the whole set before any text applies.
92
+ * Resolves to the documents in the order given. Only for a session
93
+ * registered on this connection.
94
+ */
95
+ updateModelDocuments(args: TransferUpdateDocumentsArgs<TTransfer>): Promise<TransferDocument<TTransfer, TDiagnostic>[]>;
96
+
97
+ /**
98
+ * Open a document for the client session `clientId` and return its current
99
+ * state. `clientId` must be a session this connection registered with
100
+ * {@link createSession}; any other id fails with the closed-session code,
101
+ * as every document request here does. The document is read from disk
102
+ * unless a client has it open already, so the request carries no
103
+ * `languageId`, `version` or `text` seed. Returns the document at
104
+ * the server's configured target phase, as {@link getModelDocument} does.
105
+ * A repeat open changes nothing.
106
+ *
107
+ * Pair with {@link watchModelDocument} to receive subsequent build-phase
108
+ * events (open returns a one-shot snapshot; later validation diagnostics
109
+ * arrive on the watch channel).
110
+ */
111
+ openModelDocument(args: Pick<OpenModelArgs, 'uri' | 'clientId' | 'options'>): Promise<TransferDocument<TTransfer, TDiagnostic>>;
112
+
113
+ /**
114
+ * Close the session's open of `uri`. Counterpart to
115
+ * {@link openModelDocument}; the document stays open until every client
116
+ * has closed it, and then the server releases it, by default reverting it
117
+ * 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,19 +132,30 @@ 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
92
- * state (matches the file on disk). Save semantics depend on the
93
- * filesystem-provider wiring; rejection paths surface as awaited
94
- * promise rejections.
142
+ * Write a document as the session `clientId`, which must have it open, and
143
+ * persist it to disk. The response is the latest build of the document,
144
+ * with the version of the text written in `persisted`. Save semantics
145
+ * depend on the filesystem-provider wiring; rejection paths surface as
146
+ * awaited promise rejections.
147
+ */
148
+ saveModelDocument(args: TransferSaveDocumentArgs<TTransfer>): Promise<TransferSavedDocument<TTransfer, TDiagnostic>>;
149
+
150
+ /**
151
+ * Persist the text the server holds for a document the session `clientId`
152
+ * has open, as it is: no update and no serialisation, so another client's
153
+ * unsaved text keeps its formatting. `baseVersion` is checked in the step
154
+ * that takes the text, so text that moved on fails with a `ConflictError`
155
+ * code. The saved event names this session, whoever wrote the text.
156
+ * Responds as {@link saveModelDocument} does.
95
157
  */
96
- saveModelDocument(args: TransferSaveDocumentArgs<TTransfer>): Promise<TransferDocument<TTransfer, TDiagnostic>>;
158
+ persistModelDocument(args: TransferPersistDocumentArgs): Promise<TransferSavedDocument<TTransfer, TDiagnostic>>;
97
159
 
98
160
  /**
99
161
  * Start watching `(uri, clientId)`. The server starts dispatching
@@ -194,6 +256,22 @@ export interface DataServerProtocol<
194
256
  >
195
257
  extends DocumentServerProtocol<TTransfer, TDiagnostic>, ProjectServerProtocol<TProject> {}
196
258
 
259
+ /**
260
+ * The diagnostic shape a server answers with, read back off its own
261
+ * declaration.
262
+ *
263
+ * This is what lets a client tier stay generic over the server alone and still
264
+ * describe what it hands back. Both parameters must be inferred: naming
265
+ * {@link TransferElement} for the transfer instead fails to match while that
266
+ * parameter is still generic, and a default declared against the constraint
267
+ * then stops satisfying it.
268
+ */
269
+ export type DiagnosticOf<TServer> =
270
+ TServer extends DocumentServerProtocol<infer _TTransfer, infer TDiagnostic> ? TDiagnostic : TransferDiagnostic;
271
+
272
+ /** The project shape a server answers with, read back off its own declaration. */
273
+ export type ProjectOf<TServer> = TServer extends ProjectServerProtocol<infer TProject> ? TProject : Project;
274
+
197
275
  /**
198
276
  * Cross-reference / naming slice of the server surface — scope-aware
199
277
  * queries that resolve against the language's reference services. NOT part
@@ -204,11 +282,11 @@ export interface DataServerProtocol<
204
282
  * source/URI (an `ElementSource` with no URI resolves to the sole
205
283
  * registered language; a multi-language workspace overrides).
206
284
  *
207
- * Generic over the transfer root type only — the resolution result
208
- * ({@link ReferenceTarget}) carries the resolved node's encoded subtree;
209
- * candidates and names are diagnostic-free.
285
+ * Not generic over a server's root type: {@link resolveReference} answers with
286
+ * a node a reference names, which is no document root, and candidates and
287
+ * names are diagnostic-free.
210
288
  */
211
- export interface ReferenceServerProtocol<TTransfer extends TransferElement> {
289
+ export interface ReferenceServerProtocol {
212
290
  /**
213
291
  * List the reference candidates reachable for the property named in `ctx`
214
292
  * from its (possibly synthetic) source. Backed by the language's
@@ -221,8 +299,13 @@ export interface ReferenceServerProtocol<TTransfer extends TransferElement> {
221
299
  * target's document URI, display fields, and the resolved node's encoded
222
300
  * transfer subtree. Resolves via the language's reference services;
223
301
  * `undefined` when the reference does not resolve.
302
+ *
303
+ * `TElement` is the caller's claim about the target node's type; nothing
304
+ * checks it, so leave it at the default unless the reference names one type.
224
305
  */
225
- resolveReference(ref: ReferenceRequest): Promise<ReferenceTarget<TTransfer> | undefined>;
306
+ resolveReference<TElement extends TransferElement = TransferElement>(
307
+ ref: ReferenceRequest
308
+ ): Promise<ReferenceTarget<TElement> | undefined>;
226
309
 
227
310
  /**
228
311
  * Compute the next free name for a new element of `args.type` based on
@@ -262,6 +345,39 @@ export interface DocumentClientProtocol<TTransfer extends TransferElement, TDiag
262
345
  * codepath, not a subscription codepath).
263
346
  */
264
347
  onDocumentSaved(event: TransferDocumentSavedEvent<TTransfer, TDiagnostic>): void;
348
+
349
+ /**
350
+ * Delivered when a subscribed document's text starts or stops differing
351
+ * from its file, as the server last knew it. Gated like
352
+ * {@link onDocumentSaved}; the answer at any one moment is the `dirty` of
353
+ * the documents the server sends. After a reconnect, a `DataSession`
354
+ * delivers here the answer of each document it restored, when that differs
355
+ * from the last this client was told: the flips while the connection was
356
+ * down reached no one.
357
+ */
358
+ onDocumentDirtyChanged(event: TransferDocumentDirtyChangedEvent): void;
359
+
360
+ /**
361
+ * Delivered when a document's backing file was removed. Separate from
362
+ * {@link onDocumentUpdated} for the same reason as {@link onDocumentSaved},
363
+ * and more strongly: there is no built state to deliver, and the build-phase
364
+ * path cannot report a deletion at all.
365
+ *
366
+ * Ungated, because on a browser host — where the workspace lives behind the
367
+ * head — no other source can observe a file disappearing. Recipients that
368
+ * only care about their own documents filter by URI.
369
+ */
370
+ onDocumentDeleted(event: TransferDocumentDeletedEvent): void;
371
+
372
+ /**
373
+ * Delivered once per build, naming the documents that reached the
374
+ * subscription phase and that
375
+ * nobody on this connection watches — chiefly the ones rebuilt as a cascade
376
+ * from a dependency's change, which no filesystem watcher can see because
377
+ * their own files did not change. The complement of
378
+ * {@link onDocumentUpdated}; carries URIs and no documents.
379
+ */
380
+ onDocumentsBuilt(event: TransferDocumentsBuiltEvent): void;
265
381
  }
266
382
 
267
383
  /**