@hydranium/protocol 1.0.0-next.24 → 1.0.0-next.242

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 (250) 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 +245 -0
  7. package/lib/client/data-connection.d.ts.map +1 -0
  8. package/lib/client/data-connection.js +425 -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 +44 -27
  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 +10 -4
  25. package/lib/client/message-relay.d.ts.map +1 -1
  26. package/lib/client/message-relay.js +12 -6
  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 +46 -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.d.ts +3 -2
  177. package/lib/testing/wait-for.d.ts.map +1 -1
  178. package/lib/testing/wait-for.js +40 -10
  179. package/lib/testing/wait-for.js.map +1 -1
  180. package/lib/tracer.d.ts.map +1 -1
  181. package/lib/tracer.js.map +1 -1
  182. package/lib/transfer-diagnostic.d.ts +33 -0
  183. package/lib/transfer-diagnostic.d.ts.map +1 -1
  184. package/lib/transfer-diagnostic.js +23 -0
  185. package/lib/transfer-diagnostic.js.map +1 -1
  186. package/lib/transfer-document.d.ts +70 -32
  187. package/lib/transfer-document.d.ts.map +1 -1
  188. package/lib/transfer-document.js +17 -9
  189. package/lib/transfer-document.js.map +1 -1
  190. package/lib/uri.d.ts.map +1 -1
  191. package/lib/uri.js.map +1 -1
  192. package/lib/util.d.ts +8 -0
  193. package/lib/util.d.ts.map +1 -1
  194. package/lib/util.js +32 -0
  195. package/lib/util.js.map +1 -1
  196. package/package.json +29 -37
  197. package/src/abstract-logger.ts +8 -0
  198. package/src/client/data-connection.ts +520 -0
  199. package/src/client/data-events.ts +33 -1
  200. package/src/client/data-port.ts +46 -28
  201. package/src/client/data-session.ts +951 -126
  202. package/src/client/index.ts +14 -9
  203. package/src/client/message-relay.ts +30 -8
  204. package/src/client/post-message-transport.ts +219 -4
  205. package/src/client/rpc-connection.ts +281 -0
  206. package/src/client-ids.ts +49 -0
  207. package/src/clock.ts +56 -0
  208. package/src/console-logger.ts +39 -0
  209. package/src/data/data-protocol-methods.ts +13 -4
  210. package/src/data/data-server-protocol.ts +157 -41
  211. package/src/data/events.ts +123 -21
  212. package/src/data/requests.ts +74 -11
  213. package/src/errors.ts +322 -36
  214. package/src/glsp-request-model-args.ts +16 -0
  215. package/src/glsp-save-model-actions.ts +59 -0
  216. package/src/index.ts +10 -0
  217. package/src/latency-collector.ts +8 -3
  218. package/src/logger.ts +28 -2
  219. package/src/messages/index.ts +37 -0
  220. package/src/messages/primitives.ts +271 -0
  221. package/src/model-server.ts +63 -18
  222. package/src/model-service/base-version.ts +72 -0
  223. package/src/model-service/index.ts +4 -5
  224. package/src/model-service/reference-candidate.ts +5 -3
  225. package/src/node/index.ts +14 -0
  226. package/src/node/process-memory.ts +299 -0
  227. package/src/patch-merge.ts +97 -42
  228. package/src/profile-session.ts +9 -4
  229. package/src/random-uuid.ts +21 -0
  230. package/src/reconcile-write.ts +124 -0
  231. package/src/rpc/README.md +4 -5
  232. package/src/rpc/bind-rpc-methods.ts +59 -4
  233. package/src/rpc/create-rpc-proxy.ts +20 -2
  234. package/src/rpc/index.ts +1 -0
  235. package/src/rpc/send-by-method-name.ts +140 -0
  236. package/src/testing/catalogue-audit.ts +111 -0
  237. package/src/testing/data-doubles.ts +149 -25
  238. package/src/testing/fake-clock.ts +62 -47
  239. package/src/testing/index.ts +5 -2
  240. package/src/testing/node/duplex-connection.ts +3 -2
  241. package/src/testing/node/index.ts +2 -2
  242. package/src/testing/node/message-port-pair.ts +40 -0
  243. package/src/testing/wait-for.ts +38 -11
  244. package/src/transfer-diagnostic.ts +40 -0
  245. package/src/transfer-document.ts +87 -34
  246. package/src/util.ts +33 -0
  247. package/lib/model-service/args.d.ts +0 -64
  248. package/lib/model-service/args.d.ts.map +0 -1
  249. package/lib/model-service/args.js.map +0 -1
  250. package/src/model-service/args.ts +0 -67
@@ -0,0 +1,271 @@
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
+ import { ResponseError } from 'vscode-jsonrpc';
11
+
12
+ /**
13
+ * The framework externalizes user-facing strings and SELECTS no locale: it
14
+ * relays the one its client declared and renders with whatever templates the
15
+ * adopter installed, defaulting to its English. Every such string carries a
16
+ * stable code beside that English, and exactly one side renders it — the side
17
+ * that knows the reading user's language.
18
+ *
19
+ * Which side that is depends on the message, not on the package. A server
20
+ * message is rendered by the server, at the one seam every carrier passes
21
+ * through, in the locale it was handed at init. A message the client tier raises
22
+ * is rendered there, because those fire when the server is unreachable. Nothing
23
+ * is rendered twice: two renders of one sentence are two authorities over it,
24
+ * and they diverge on the first reword.
25
+ *
26
+ * Codes are `hydranium/<unscoped-package>/<name>`. The package segment locates
27
+ * the declaration, so a message is declared in the package that raises it and a
28
+ * code never names a package it does not live in. `.` and `:` are forbidden in a
29
+ * segment: they are i18next's default key and namespace separators, where either
30
+ * silently becomes a nested lookup that misses.
31
+ */
32
+ export type MessageParams = Readonly<Record<string, string | number>>;
33
+
34
+ type Placeholder<S extends string> = S extends `${string}{${infer Name}}${infer Rest}` ? Name | Placeholder<Rest> : never;
35
+
36
+ export type ParamsOf<S extends string> = [Placeholder<S>] extends [never]
37
+ ? Record<never, never>
38
+ : Readonly<Record<Placeholder<S>, string | number>>;
39
+
40
+ /** Required exactly when the text has placeholders, absent when it does not. */
41
+ export type ParamsArg<S extends string> = [Placeholder<S>] extends [never] ? [] : [params: ParamsOf<S>];
42
+
43
+ /**
44
+ * Rejects a text argument already widened to `string`. Load-bearing rather than
45
+ * defensive: the whole compile-time guarantee is conditional on `S` inferring a
46
+ * literal, and for a concatenated or pre-widened text the placeholder set
47
+ * silently becomes empty, `format()` accepts no arguments, and the missing
48
+ * substitution surfaces only at runtime.
49
+ */
50
+ export type LiteralText<S extends string> = string extends S ? never : S;
51
+
52
+ export interface MessageDefinition<S extends string> {
53
+ readonly code: string;
54
+ readonly text: S;
55
+ format(...args: ParamsArg<S>): string;
56
+ }
57
+
58
+ /**
59
+ * Declare a message. Placeholder names are inferred from `text` rather than
60
+ * declared again in a type argument: a second spelling of every name is the
61
+ * repetition that drifts, since adding a placeholder to the sentence and not to
62
+ * the type compiles.
63
+ */
64
+ export function defineMessage<S extends string>(code: string, text: LiteralText<S>): MessageDefinition<S> {
65
+ const literal = text as S;
66
+ return { code, text: literal, format: (...args) => interpolate(literal, args[0] ?? {}) };
67
+ }
68
+
69
+ const PLACEHOLDER = /\{([^}]+)\}/g;
70
+
71
+ /**
72
+ * Substitute `{name}` tokens, leaving an unfilled token in place.
73
+ *
74
+ * It must not throw. This runs over an adopter's translation as well as our own
75
+ * text, so a typo in a foreign catalogue has to degrade to a slightly wrong
76
+ * sentence rather than raise inside a toast render. Re-scanning the result to
77
+ * detect an unfilled token is what an earlier form did, and it cannot work:
78
+ * `String.replace` does not rescan replacement text, so the check could not tell
79
+ * an unfilled placeholder from user data shaped like one — an element literally
80
+ * named `{separator}` crashed at the authoring site.
81
+ */
82
+ export function interpolate(template: string, params: MessageParams): string {
83
+ // Indexed rather than `key in params`: a hand-built or version-skewed
84
+ // identity can arrive with no params at all, and `in` throws on a non-object
85
+ // where a lookup degrades.
86
+ const lookup = params as Record<string, string | number | undefined> | undefined;
87
+ return template.replace(PLACEHOLDER, (match, key: string) => {
88
+ const value = lookup?.[key];
89
+ return value === undefined ? match : String(value);
90
+ });
91
+ }
92
+
93
+ export interface MessageIdentity {
94
+ readonly code: string;
95
+ readonly params: MessageParams;
96
+ }
97
+
98
+ /**
99
+ * Envelope for a protocol `data` field. Namespaced under one key so it co-exists
100
+ * with a carrier's own `data` conventions rather than occupying `data` itself.
101
+ */
102
+ export interface HydraniumMessageData {
103
+ readonly hydranium: MessageIdentity;
104
+ }
105
+
106
+ /** An identity plus its resolved English — everything a renderer needs, on any carrier. */
107
+ export interface ResolvedMessage extends MessageIdentity {
108
+ readonly text: string;
109
+ }
110
+
111
+ export function messageData<S extends string>(message: MessageDefinition<S>, ...args: ParamsArg<S>): HydraniumMessageData {
112
+ return { hydranium: { code: message.code, params: args[0] ?? {} } };
113
+ }
114
+
115
+ /**
116
+ * Identity plus resolved English, for a hand-off carrying a value rather than a
117
+ * protocol field. The result is structured-clone safe, so it survives a process
118
+ * hop where one intervenes and costs nothing where none does.
119
+ */
120
+ export function resolve<S extends string>(message: MessageDefinition<S>, ...args: ParamsArg<S>): ResolvedMessage {
121
+ return { code: message.code, text: message.format(...args), params: args[0] ?? {} };
122
+ }
123
+
124
+ /**
125
+ * Validates every field {@link MessageIdentity} declares, `params` included.
126
+ * A guard over foreign input that checks only `code` while declaring `params`
127
+ * non-optional hands `undefined` to the renderer, which fails with the worst
128
+ * polarity available: invisible in English, crashing only once a translation is
129
+ * loaded.
130
+ */
131
+ export function hasMessageIdentity(data: unknown): data is HydraniumMessageData {
132
+ if (typeof data !== 'object' || data === null || Array.isArray(data) || !('hydranium' in data)) {
133
+ return false;
134
+ }
135
+ const identity = (data as { hydranium: unknown }).hydranium;
136
+ if (typeof identity !== 'object' || identity === null || Array.isArray(identity)) {
137
+ return false;
138
+ }
139
+ const candidate = identity as Partial<MessageIdentity>;
140
+ return typeof candidate.code === 'string' && typeof candidate.params === 'object' && candidate.params !== null;
141
+ }
142
+
143
+ /**
144
+ * A type alias rather than a subclass. Only `code`, `message` and `data` cross
145
+ * the wire, so a subclass buys nothing there: `instanceof` does not survive
146
+ * reconstruction, and the subclass costs an `Object.setPrototypeOf` in every
147
+ * constructor purely to undo what `ResponseError`'s own constructor does.
148
+ */
149
+ export type HydraniumResponseError = ResponseError<HydraniumMessageData>;
150
+
151
+ /**
152
+ * The numeric `code` and the message's catalogue code are unrelated and both are
153
+ * needed: `ResponseError.code` is an `integer`, so it cannot hold a
154
+ * `hydranium/…` key, and it is what a caller switches on after reconstruction.
155
+ */
156
+ export function messageError<S extends string>(
157
+ code: number,
158
+ message: MessageDefinition<S>,
159
+ ...params: ParamsArg<S>
160
+ ): HydraniumResponseError {
161
+ return new ResponseError(code, message.format(...params), messageData(message, ...params));
162
+ }
163
+
164
+ /**
165
+ * The reading user's language, as the tag a client declared.
166
+ *
167
+ * Neither validated nor normalised anywhere: rejecting an unfamiliar tag would
168
+ * be selecting a locale, which the framework does not do.
169
+ *
170
+ * NOT a Langium language id, which names a grammar. The two are strings of the
171
+ * same shape reachable from the same services, so one used where the other
172
+ * belongs misses every lookup rather than failing.
173
+ */
174
+ export type Locale = string;
175
+
176
+ /**
177
+ * A host's templates for one locale, keyed by the whole message code.
178
+ *
179
+ * **Flat, not nested.** A code is one key here, `/` separators included; a host
180
+ * whose own catalogue format nests — Theia's does — flattens on the way in. A
181
+ * map that nests instead misses every lookup while type-checking, because the
182
+ * value this is indexed by is `ResolvedMessage.code`.
183
+ *
184
+ * **Partial by nature rather than by convention.** A code with no entry renders
185
+ * its English default, so a catalogue covering none of a package's codes is a
186
+ * valid catalogue and not a broken one — which is what lets an adopter translate
187
+ * as much or as little as they like.
188
+ *
189
+ * Deliberately NOT narrowed to the codes that exist, and a `Code extends string`
190
+ * parameter would not buy what it appears to: a catalogue is loaded as JSON, so
191
+ * it reaches a checked position as a variable rather than as an object literal,
192
+ * and excess-property checking — the only thing that would reject a mistyped
193
+ * key — does not run there. The gain is a compile error for a catalogue with no
194
+ * correct key at all; the cost is a code union to maintain by hand, since a
195
+ * declaration does not carry its code as a literal type.
196
+ */
197
+ export type MessageCatalogue = Readonly<Record<string, string>>;
198
+
199
+ export namespace MessageCatalogue {
200
+ /**
201
+ * One catalogue from several, for a renderer whose override supplies entries
202
+ * over the ones it inherits.
203
+ *
204
+ * **Later wins**, so a caller passes the inherited answer before its own.
205
+ *
206
+ * **No source at all answers `undefined`, not `{}`.** A renderer holds that
207
+ * answer as the locale having no catalogue and renders straight through; an
208
+ * empty object is a catalogue that misses every lookup, so returning one
209
+ * turns the opt-out into a per-message search that yields the same English.
210
+ */
211
+ export function merge(...catalogues: ReadonlyArray<MessageCatalogue | undefined>): MessageCatalogue | undefined {
212
+ const present = catalogues.filter((catalogue): catalogue is MessageCatalogue => catalogue !== undefined);
213
+ if (present.length <= 1) {
214
+ return present[0];
215
+ }
216
+ return Object.assign({}, ...present) as MessageCatalogue;
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Render on the side that knows the reading user's locale. Omitting
222
+ * `translations` is how an adopter without i18n opts out, and yields the
223
+ * English.
224
+ */
225
+ export function renderFrameworkMessage(message: ResolvedMessage, translations?: MessageCatalogue): string {
226
+ const template = translations?.[message.code];
227
+ return template ? interpolate(template, message.params) : message.text;
228
+ }
229
+
230
+ export function resolvedFromResponseError(error: ResponseError<unknown>): ResolvedMessage | undefined {
231
+ return hasMessageIdentity(error.data) ? { ...error.data.hydranium, text: error.message } : undefined;
232
+ }
233
+
234
+ /**
235
+ * The detail half of a `{detail}` placeholder. A technical error string is safe
236
+ * to pass as a parameter for the same reason a number is: it is not itself
237
+ * translatable text, so it needs no code of its own. A PROSE fragment is not,
238
+ * and must become one code per value instead.
239
+ */
240
+ export function describeError(error: unknown): string {
241
+ return error instanceof Error ? error.message : String(error);
242
+ }
243
+
244
+ /**
245
+ * Recognises a declaration among a barrel's exports. The `format` check is what
246
+ * discriminates: a `code` + `text` pair alone admits any object that happens to
247
+ * carry both.
248
+ */
249
+ export function isMessageDeclaration(value: unknown): value is MessageDefinition<string> {
250
+ const candidate = value as { code?: unknown; text?: unknown; format?: unknown } | null;
251
+ return (
252
+ typeof candidate === 'object' &&
253
+ candidate !== null &&
254
+ typeof candidate.code === 'string' &&
255
+ typeof candidate.text === 'string' &&
256
+ typeof candidate.format === 'function'
257
+ );
258
+ }
259
+
260
+ /**
261
+ * Every declaration a `./messages` barrel exports.
262
+ *
263
+ * A caller cannot get there with `Object.values(barrel).filter(isMessageDeclaration)`:
264
+ * a barrel's value type is a union of its declarations AND its functions, and
265
+ * `filter` will not narrow a function type down to a `MessageDefinition`, so the
266
+ * result stays the union and reading `.code` off it does not compile. Taking the
267
+ * barrel as an opaque object is what makes the one-liner work.
268
+ */
269
+ export function collectMessages(barrel: object): MessageDefinition<string>[] {
270
+ return (Object.values(barrel) as unknown[]).filter(isMessageDeclaration);
271
+ }
@@ -14,17 +14,19 @@
14
14
  * over `TRoot`.
15
15
  */
16
16
 
17
- import type { TransferClientArgs } from './model-service/args';
18
17
  import type { ReferenceCandidate } from './model-service/reference-candidate';
19
18
 
20
19
  // ---------------------------------------------------------------------------
21
20
  // Client / server arguments
22
21
  // ---------------------------------------------------------------------------
23
22
 
24
- // `TransferClientArgs` / `TransferUpdateArgs` / `TransferSaveArgs` live in
25
- // `./model-service/args.ts` and are re-exported by the main barrel via
26
- // `./model-service`. The legacy LSP-style protocol (Open / Close / events)
27
- // defined here only consumes them as a supertype.
23
+ /** Identifies a client-document binding. */
24
+ export interface TransferClientArgs {
25
+ /** Document URI. */
26
+ uri: string;
27
+ /** Stable identifier for the client invoking the operation. */
28
+ clientId: string;
29
+ }
28
30
 
29
31
  /** Open a document on behalf of a client. */
30
32
  export interface OpenModelArgs extends TransferClientArgs {
@@ -47,10 +49,18 @@ export interface OpenModelArgs extends TransferClientArgs {
47
49
  * means read from the filesystem, which is the normal case.
48
50
  *
49
51
  * Honoured only on the first open of a URI: opening an already-open document
50
- * is a no-op, so passing `text` for one silently changes nothing. Write
51
- * through the update path instead.
52
+ * attaches the caller to the existing shared entry and does not replace its
53
+ * content with this seed. Write through the update path instead. The open
54
+ * response reads the current shared content after registration; a textual
55
+ * `didOpen` attach may separately refresh the build.
52
56
  */
53
57
  text?: string;
58
+ /**
59
+ * Kept on the server for this client's open until it closes, for the
60
+ * server's own open path to read. Only a client session's open keeps them,
61
+ * and a repeat open keeps the first open's.
62
+ */
63
+ options?: object;
54
64
  }
55
65
 
56
66
  /** Close a previously-opened document for the client. */
@@ -63,9 +73,25 @@ export interface CloseModelArgs extends TransferClientArgs {}
63
73
 
64
74
  export interface TransferUpdatedEvent<TDocument> {
65
75
  document: TDocument;
76
+ /**
77
+ * The client whose write this event echoes: the author of the version on a
78
+ * `'changed'`, and the unknown-client id on a `'rebuilt'`, which echoes no
79
+ * write. A recipient compares it against its own id to recognise its echo.
80
+ */
66
81
  sourceClientId: string;
67
- /** See `ModelDocumentUpdateReason` in `./data/events` for the canonical reason set + semantics. */
68
- reason: 'changed' | 'deleted' | 'rebuilt' | 'saved';
82
+ /** See `TransferDocumentUpdateReason` in `./data/events` for the canonical reason set + semantics. */
83
+ reason: 'changed' | 'rebuilt' | 'saved';
84
+ /**
85
+ * The client whose write caused the build that produced this event, or the
86
+ * unknown-client id when no single client's did. On a `'changed'` it is the
87
+ * version's author. A `'rebuilt'` names the writer of another document, so
88
+ * it is a cause, never an echo: its diagnostics and references are new to
89
+ * that writer too.
90
+ *
91
+ * In-process only: the data head does not send it, and an event built
92
+ * elsewhere may leave it out, which a reader takes as "not known".
93
+ */
94
+ causedBy?: string;
69
95
  }
70
96
 
71
97
  export interface TransferSavedEvent<TDocument> {
@@ -147,10 +173,10 @@ export function isElementSource(object: unknown): object is ElementSource {
147
173
  /** An element of a document that does not yet exist on disk — used during element creation flows. */
148
174
  export interface SyntheticSource {
149
175
  /**
150
- * The document the element would belong to. That document must already be
151
- * LOADED — the default resolution takes its parse root as the synthetic
152
- * node's container and answers `undefined` when it is not, so a URI for a
153
- * file that exists on disk but was never opened resolves to nothing.
176
+ * The document the element would belong to. It need not be loaded, or
177
+ * exist — an unloaded URI materialises a transient empty document, so a
178
+ * folder is a valid anchor. A URI that IS loaded contributes that
179
+ * document's parse root as the container, putting its contents in scope.
154
180
  */
155
181
  uri: string;
156
182
  /**
@@ -159,6 +185,16 @@ export interface SyntheticSource {
159
185
  * it is what scoping filters candidates against.
160
186
  */
161
187
  type: string;
188
+ /**
189
+ * The grammar the element will belong to, as a language id.
190
+ *
191
+ * Optional because `uri` and `type` each answer on their own when
192
+ * unambiguous. Set it when neither is: a URI naming no file carries no
193
+ * extension to route on, and a `type` two grammars can produce identifies
194
+ * neither. Unset, such a source reaches the server's own policy, which
195
+ * answers the same way for every caller.
196
+ */
197
+ language?: string;
162
198
  }
163
199
 
164
200
  export function isSyntheticSource(object: unknown): object is SyntheticSource {
@@ -183,8 +219,10 @@ export namespace ReferenceSource {
183
219
  export function element(name: string, type?: string): ElementSource {
184
220
  return { name, type };
185
221
  }
186
- export function synthetic(uri: string, type: string): SyntheticSource {
187
- return { uri, type };
222
+ export function synthetic(uri: string, type: string, language?: string): SyntheticSource {
223
+ // The key is omitted rather than set to `undefined`, so a source built
224
+ // without a language stays deep-equal to the two-argument form.
225
+ return language === undefined ? { uri, type } : { uri, type, language };
188
226
  }
189
227
  }
190
228
 
@@ -457,9 +495,10 @@ export interface FindNextNameArgs {
457
495
  */
458
496
  uri: string;
459
497
  /**
460
- * The AST `$type` of the element being named. Collisions are only looked for
461
- * among elements of that same type, so the returned name may still be taken
462
- * by an element of another type.
498
+ * The AST `$type` of the element being named. Collisions are looked for
499
+ * among elements of that type and of any subtype of it, so a supertype
500
+ * names a uniqueness scope its concrete types share; the returned name may
501
+ * still be taken by an element outside that hierarchy.
463
502
  */
464
503
  type: string;
465
504
  /**
@@ -476,4 +515,10 @@ export interface FindNextNameArgs {
476
515
  * See {@link NameTier} for what each tier covers.
477
516
  */
478
517
  tier?: NameTier;
518
+ /**
519
+ * The grammar to name under, routed as {@link SyntheticSource.language} —
520
+ * this call builds one. A caller that sets it there must set it here, or
521
+ * candidates and the name it proposes come from different grammars.
522
+ */
523
+ language?: string;
479
524
  }
@@ -0,0 +1,72 @@
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
+ declare const snapshotMarker: unique symbol;
11
+
12
+ /**
13
+ * The version of the text a model was parsed from: a snapshot of the store's
14
+ * {@link TextVersion}, not a counter of its own. A document envelope carries
15
+ * it, and it cannot move once the envelope exists.
16
+ *
17
+ * A plain number at runtime, usable anywhere a number is, and it travels the
18
+ * wire as one. The marker exists only so the compiler can tell it apart from a
19
+ * version read off a LIVE document, which is the same number type and is the
20
+ * defect the conflict gate exists to prevent: read at write time it is whatever
21
+ * the server is at now, which is the number the gate is about to compare it
22
+ * against, so the gate passes unconditionally and a concurrent edit is
23
+ * overwritten with nothing logged.
24
+ */
25
+ export type ModelVersion = number & { readonly [snapshotMarker]: true };
26
+
27
+ /**
28
+ * The store's version of a document's text, which moves with every change of it.
29
+ * Unbranded, so a write refuses it as `baseVersion`: it can name text newer than
30
+ * the caller's model, and a write based on it would pass the gate with older content.
31
+ */
32
+ export type TextVersion = number;
33
+
34
+ /**
35
+ * The version a write was authored against, from a snapshot read, or `'any'`: a
36
+ * precondition that always holds, so the write overwrites whatever the server has.
37
+ * `'any'` is a lie for a write authored against a document a read handed over.
38
+ */
39
+ export type BaseVersion = ModelVersion | 'any';
40
+
41
+ /** Whether `baseVersion` names a version, and so arms the gate. */
42
+ export function isModelVersion(baseVersion: BaseVersion): baseVersion is ModelVersion {
43
+ return typeof baseVersion === 'number';
44
+ }
45
+
46
+ /**
47
+ * Mark a version as having come from a snapshot read.
48
+ *
49
+ * **For the envelope constructors, not for callers.** Every door that builds a
50
+ * document envelope applies it on the way in, so anything a read returns
51
+ * already carries it and a writer never needs this. A caller reaching for it is
52
+ * asserting a provenance the compiler was about to deny — the read-late defect
53
+ * written out where a reviewer can see it.
54
+ */
55
+ export function asModelVersion(version: number): ModelVersion {
56
+ return version as ModelVersion;
57
+ }
58
+
59
+ /**
60
+ * The model version of a root no document factory path built, so nothing
61
+ * records the text it came from. No write matches it, so a write based on it
62
+ * conflicts.
63
+ */
64
+ export const UNRECORDED_VERSION: ModelVersion = asModelVersion(-2);
65
+
66
+ /**
67
+ * The model version of a root parsed from text the store holds under no
68
+ * version: the file, read while the store held the document open with other
69
+ * text. Below every text version, so the model reads as behind and a write
70
+ * based on it conflicts.
71
+ */
72
+ export const STALE_VERSION: ModelVersion = asModelVersion(-1);
@@ -7,10 +7,9 @@
7
7
  * SPDX-License-Identifier: MIT
8
8
  ********************************************************************************/
9
9
 
10
- // Subpath barrel for the in-process `ModelService` facade contract. The
11
- // implementation lives in `@hydranium/core`; this package owns the
12
- // argument types so both the framework facade and the data-server wire
13
- // protocol can structurally agree on the lifecycle shape.
10
+ // Generic payload types the data protocol shares with core: the base
11
+ // version, which the server's model service checks writes against, and the
12
+ // reference candidates, which core's scope and completion code produce.
14
13
 
15
- export * from './args';
14
+ export * from './base-version';
16
15
  export * from './reference-candidate';
@@ -50,9 +50,11 @@ export interface ReferenceCandidate {
50
50
  * node's subtree, NOT the whole document root — callers that want the full
51
51
  * document fetch it via `getModelDocument(uri)`.
52
52
  *
53
- * Generic over the transfer root type so adopters get their typed overlay.
53
+ * `TElement` is the type of the target node, not of a document root. A server
54
+ * answers with the structural base, since a reference can name any node; a
55
+ * caller that knows what the reference targets may narrow it.
54
56
  */
55
- export interface ReferenceTarget<TTransfer extends TransferElement = TransferElement> extends ReferenceCandidate {
57
+ export interface ReferenceTarget<TElement extends TransferElement = TransferElement> extends ReferenceCandidate {
56
58
  /** The resolved target node, encoded as a transfer subtree. */
57
- element: TTransfer;
59
+ element: TElement;
58
60
  }
@@ -0,0 +1,14 @@
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
+ // Subpath barrel for `@hydranium/protocol/node` — the diagnostics helpers that
11
+ // need a Node runtime, kept out of the root barrel so the root stays
12
+ // bundleable for a browser.
13
+
14
+ export * from './process-memory';