@serene-pub/sdk 0.6.0-pr-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +17 -0
  3. package/README.md +368 -0
  4. package/dist/actions.d.ts +572 -0
  5. package/dist/actions.js +829 -0
  6. package/dist/annexFields.d.ts +234 -0
  7. package/dist/annexFields.js +413 -0
  8. package/dist/announce.d.ts +464 -0
  9. package/dist/announce.js +426 -0
  10. package/dist/attributes.d.ts +670 -0
  11. package/dist/attributes.js +904 -0
  12. package/dist/bands.d.ts +125 -0
  13. package/dist/bands.js +256 -0
  14. package/dist/builder.d.ts +683 -0
  15. package/dist/builder.js +677 -0
  16. package/dist/candidates.d.ts +109 -0
  17. package/dist/candidates.js +118 -0
  18. package/dist/capabilities.d.ts +744 -0
  19. package/dist/capabilities.js +679 -0
  20. package/dist/channels.d.ts +106 -0
  21. package/dist/channels.js +129 -0
  22. package/dist/clauses.d.ts +40 -0
  23. package/dist/clauses.js +43 -0
  24. package/dist/component.d.ts +33 -0
  25. package/dist/component.js +27 -0
  26. package/dist/componentBuild.d.ts +48 -0
  27. package/dist/componentBuild.js +84 -0
  28. package/dist/componentClient.d.ts +100 -0
  29. package/dist/componentClient.js +13 -0
  30. package/dist/componentImports.d.ts +51 -0
  31. package/dist/componentImports.js +106 -0
  32. package/dist/componentModule.d.ts +8 -0
  33. package/dist/componentModule.js +270 -0
  34. package/dist/componentWire.d.ts +130 -0
  35. package/dist/componentWire.js +280 -0
  36. package/dist/config.d.ts +295 -0
  37. package/dist/config.js +231 -0
  38. package/dist/connections.d.ts +89 -0
  39. package/dist/connections.js +131 -0
  40. package/dist/declarations.d.ts +290 -0
  41. package/dist/declarations.js +794 -0
  42. package/dist/descriptors.d.ts +1774 -0
  43. package/dist/descriptors.js +1329 -0
  44. package/dist/dev.d.ts +136 -0
  45. package/dist/dev.js +161 -0
  46. package/dist/document.d.ts +202 -0
  47. package/dist/document.js +312 -0
  48. package/dist/engines.d.ts +117 -0
  49. package/dist/engines.js +203 -0
  50. package/dist/eventMapLaws.d.ts +63 -0
  51. package/dist/eventMapLaws.js +128 -0
  52. package/dist/events.d.ts +554 -0
  53. package/dist/events.js +633 -0
  54. package/dist/executor.d.ts +771 -0
  55. package/dist/executor.js +1829 -0
  56. package/dist/extension.d.ts +448 -0
  57. package/dist/extension.js +315 -0
  58. package/dist/facets.d.ts +53 -0
  59. package/dist/facets.js +60 -0
  60. package/dist/foldedSections.d.ts +74 -0
  61. package/dist/foldedSections.js +161 -0
  62. package/dist/genres.d.ts +507 -0
  63. package/dist/genres.js +633 -0
  64. package/dist/guardedConnection.d.ts +138 -0
  65. package/dist/guardedConnection.js +341 -0
  66. package/dist/handlebarsReach.d.ts +122 -0
  67. package/dist/handlebarsReach.js +222 -0
  68. package/dist/hash.d.ts +146 -0
  69. package/dist/hash.js +166 -0
  70. package/dist/hookGrants.d.ts +47 -0
  71. package/dist/hookGrants.js +68 -0
  72. package/dist/hooks.d.ts +290 -0
  73. package/dist/hooks.js +113 -0
  74. package/dist/hostElements.d.ts +447 -0
  75. package/dist/hostElements.js +702 -0
  76. package/dist/i18n.d.ts +94 -0
  77. package/dist/i18n.js +123 -0
  78. package/dist/identity.d.ts +93 -0
  79. package/dist/identity.js +135 -0
  80. package/dist/index.d.ts +85 -0
  81. package/dist/index.js +176 -0
  82. package/dist/layout.d.ts +194 -0
  83. package/dist/layout.js +40 -0
  84. package/dist/media.d.ts +202 -0
  85. package/dist/media.js +70 -0
  86. package/dist/messageBlocks.d.ts +487 -0
  87. package/dist/messageBlocks.js +683 -0
  88. package/dist/migration.d.ts +128 -0
  89. package/dist/migration.js +261 -0
  90. package/dist/names.d.ts +78 -0
  91. package/dist/names.js +136 -0
  92. package/dist/nodeInput.d.ts +399 -0
  93. package/dist/nodeInput.js +178 -0
  94. package/dist/participants.d.ts +291 -0
  95. package/dist/participants.js +252 -0
  96. package/dist/personGate.d.ts +62 -0
  97. package/dist/personGate.js +77 -0
  98. package/dist/pick.d.ts +84 -0
  99. package/dist/pick.js +103 -0
  100. package/dist/pluginHarness.d.ts +235 -0
  101. package/dist/pluginHarness.js +558 -0
  102. package/dist/pluginPermissions.d.ts +25 -0
  103. package/dist/pluginPermissions.js +97 -0
  104. package/dist/pluginRuleRef.d.ts +13 -0
  105. package/dist/pluginRuleRef.js +10 -0
  106. package/dist/predicates.d.ts +182 -0
  107. package/dist/predicates.js +249 -0
  108. package/dist/preview.d.ts +120 -0
  109. package/dist/preview.js +82 -0
  110. package/dist/promptBlocks.d.ts +130 -0
  111. package/dist/promptBlocks.js +197 -0
  112. package/dist/rankingDecisions.d.ts +60 -0
  113. package/dist/rankingDecisions.js +48 -0
  114. package/dist/receipt.d.ts +313 -0
  115. package/dist/receipt.js +60 -0
  116. package/dist/receiverRules.d.ts +24 -0
  117. package/dist/receiverRules.js +94 -0
  118. package/dist/refs.d.ts +117 -0
  119. package/dist/refs.js +137 -0
  120. package/dist/registry.d.ts +230 -0
  121. package/dist/registry.js +273 -0
  122. package/dist/review.d.ts +108 -0
  123. package/dist/review.js +97 -0
  124. package/dist/sampling.d.ts +152 -0
  125. package/dist/sampling.js +712 -0
  126. package/dist/scope.d.ts +65 -0
  127. package/dist/scope.js +128 -0
  128. package/dist/scripts.d.ts +243 -0
  129. package/dist/scripts.js +326 -0
  130. package/dist/sessionLayout.d.ts +339 -0
  131. package/dist/sessionLayout.js +503 -0
  132. package/dist/sessionSettings.d.ts +70 -0
  133. package/dist/sessionSettings.js +2 -0
  134. package/dist/settings.d.ts +501 -0
  135. package/dist/settings.js +604 -0
  136. package/dist/settingsSlot.d.ts +108 -0
  137. package/dist/settingsSlot.js +166 -0
  138. package/dist/shapes.d.ts +373 -0
  139. package/dist/shapes.js +408 -0
  140. package/dist/sheets.d.ts +165 -0
  141. package/dist/sheets.js +251 -0
  142. package/dist/statShapes.d.ts +96 -0
  143. package/dist/statShapes.js +112 -0
  144. package/dist/status.d.ts +103 -0
  145. package/dist/status.js +125 -0
  146. package/dist/storage.d.ts +145 -0
  147. package/dist/storage.js +33 -0
  148. package/dist/storyTime.d.ts +308 -0
  149. package/dist/storyTime.js +560 -0
  150. package/dist/surfaces.d.ts +501 -0
  151. package/dist/surfaces.js +292 -0
  152. package/dist/template.d.ts +312 -0
  153. package/dist/template.js +554 -0
  154. package/dist/templateCheck.d.ts +60 -0
  155. package/dist/templateCheck.js +516 -0
  156. package/dist/templateFit.d.ts +157 -0
  157. package/dist/templateFit.js +349 -0
  158. package/dist/templateIds.d.ts +138 -0
  159. package/dist/templateIds.js +144 -0
  160. package/dist/templateScopeAt.d.ts +131 -0
  161. package/dist/templateScopeAt.js +354 -0
  162. package/dist/testing.d.ts +177 -0
  163. package/dist/testing.js +383 -0
  164. package/dist/tokenizers.d.ts +113 -0
  165. package/dist/tokenizers.js +184 -0
  166. package/dist/turnLock.d.ts +96 -0
  167. package/dist/turnLock.js +79 -0
  168. package/dist/turnOrder.d.ts +143 -0
  169. package/dist/turnOrder.js +128 -0
  170. package/dist/validate.d.ts +74 -0
  171. package/dist/validate.js +1399 -0
  172. package/dist/values.d.ts +212 -0
  173. package/dist/values.js +390 -0
  174. package/dist/variables.d.ts +493 -0
  175. package/dist/variables.js +662 -0
  176. package/dist/verdicts.d.ts +275 -0
  177. package/dist/verdicts.js +348 -0
  178. package/dist/widgetDecls.d.ts +88 -0
  179. package/dist/widgetDecls.js +119 -0
  180. package/dist/widgets.d.ts +1527 -0
  181. package/dist/widgets.js +208 -0
  182. package/dist/wire.d.ts +126 -0
  183. package/dist/wire.js +140 -0
  184. package/dist/worldinfo.d.ts +150 -0
  185. package/dist/worldinfo.js +212 -0
  186. package/host-elements.d.ts +914 -0
  187. package/package.json +67 -0
@@ -0,0 +1,679 @@
1
+ /**
2
+ * What a connection can do, and what a node needs from one.
3
+ *
4
+ * A connection used to carry a single `modality` — one scalar saying "this is a
5
+ * text connection" or "this is an image connection" — and adapters were split
6
+ * into two families by it. That model cannot describe a real backend. KoboldCPP
7
+ * does text, images, vision, speech and transcription from one process and
8
+ * already reports so. An OpenAI-compatible endpoint may or may not do images
9
+ * depending entirely on who is behind it. And the model was already violated in
10
+ * practice: rows carrying `modality: 'embeddings'` existed and matched no family.
11
+ *
12
+ * The replacement has two axes, deliberately kept apart:
13
+ *
14
+ * - **Transforms** say what can *flow*: text in, an image out. They are the
15
+ * modality question, asked properly — as a relation rather than a label.
16
+ * - **Features** say how a request can be *constrained*: JSON schema, a
17
+ * grammar, tool calls. Orthogonal to transforms; a backend that generates
18
+ * text may or may not be able to promise the shape of it.
19
+ *
20
+ * Mixing the two into one list is what makes a capability screen unreadable:
21
+ * "Chat" and "Strict JSON schema" are not the same kind of fact and should not
22
+ * sit in the same column.
23
+ *
24
+ * ## Who decides
25
+ *
26
+ * Four layers, each narrowing the last, resolved once by `resolveCapabilities`:
27
+ *
28
+ * adapter (what the WIRE FORMAT can express — gates the key space entirely)
29
+ * → preset (what this named service defaults to)
30
+ * → probe (what the live backend actually answered)
31
+ * → user (the final toggle)
32
+ *
33
+ * Those four collapse into two INDEPENDENT gradings, and the composite is their
34
+ * floor: the adapter grades the API (what the format can express), while the
35
+ * preset, probe and person grade the MODEL (what the thing answering actually
36
+ * does — vision is a property of a model, never of a format). See `composite`.
37
+ *
38
+ * The adapter layer is a gate, not a default: a capability absent from
39
+ * `supports` can never be turned on by any later layer, because the protocol has
40
+ * no way to say it. That is the whole reason an adapter is scoped by API format
41
+ * rather than by vendor — "Claude over an OpenAI-compatible endpoint" and
42
+ * "Claude over the native Anthropic API" differ in exactly this.
43
+ */
44
+ import { MEDIA_KINDS } from './media.js';
45
+ // ── Kinds ───────────────────────────────────────────────────────────────────
46
+ /**
47
+ * The endpoints of a transform.
48
+ *
49
+ * A superset of `MediaKind`, and the difference is the point. In
50
+ * `MediaCapability`, text is the *implicit* kind — `accepts: ['image']` means
51
+ * "images as well as text", and there is no way to write "an image and text
52
+ * together, both required". That is precisely the thing vision needs to say, so
53
+ * text is a first-class member here.
54
+ *
55
+ * `embedding` is out-only: a vector is never stored as media and never travels
56
+ * as a `MediaRef`. It is here because embedding connections exist and had
57
+ * nowhere to live under the old one-modality model.
58
+ *
59
+ * `entities` is out-only for the same reason: a named-entity span is a reading
60
+ * OF text, not a medium anything is stored or sent in. It is a kind rather than
61
+ * a feature because the thing producing it is a different model on a different
62
+ * connection, which is exactly what a transform's output side names.
63
+ *
64
+ * ⚠ **Append, never insert.** `side()` orders a transform's kinds by position
65
+ * here, so moving an existing member rewrites ids that are stored as primary
66
+ * keys (`connection_defaults`) and compared as strings everywhere else.
67
+ * @experimental
68
+ */
69
+ export const IO_KINDS = ['text', ...MEDIA_KINDS, 'embedding', 'entities'];
70
+ const KIND_ORDER = new Map(IO_KINDS.map((k, i) => [k, i]));
71
+ const side = (kinds) => [...new Set(kinds)]
72
+ .sort((a, b) => (KIND_ORDER.get(a) ?? 99) - (KIND_ORDER.get(b) ?? 99))
73
+ .join('+');
74
+ /** Read an id back apart, for a UI that wants the kinds rather than the string. @internal */
75
+ export function parseTransform(id) {
76
+ const [inS = '', outS = ''] = id.split('->');
77
+ return {
78
+ in: inS.split('+').filter(Boolean),
79
+ out: outS.split('+').filter(Boolean),
80
+ };
81
+ }
82
+ // ── Authoring a transform id ────────────────────────────────────────────────
83
+ /**
84
+ * The kinds, as values.
85
+ *
86
+ * `IoKinds.image` autocompletes; `IoKinds.imgae` is a compile error. That is the
87
+ * whole job — a JS/TS-defined node names its combinations with objects and enum
88
+ * members rather than by spelling a string, because `'text+imgae->text'` is a
89
+ * perfectly good `TransformId` today and fails at *run* time, on the machine of
90
+ * whoever installed the plugin, as a capability nothing can ever satisfy.
91
+ *
92
+ * ⚠ Named `IoKinds`, NOT `Kind`. `descriptors.ts` has exported `Kind` — the node
93
+ * kind, `'inlet' | 'query' | 'task' | 'oracle' | 'outlet'` — since long
94
+ * before this existed, and both names would meet inside a single descriptor
95
+ * literal that already says `kind: 'oracle'`. `IoKinds` also matches the two
96
+ * spellings already here, `IoKind` and `IO_KINDS`.
97
+ *
98
+ * `satisfies Record<IoKind, IoKind>` is load-bearing in both directions: adding
99
+ * a kind to `IO_KINDS` without adding it here stops compiling, and so does a
100
+ * member here whose value is not its own name.
101
+ * @experimental
102
+ */
103
+ export const IoKinds = {
104
+ text: 'text',
105
+ image: 'image',
106
+ audio: 'audio',
107
+ video: 'video',
108
+ document: 'document',
109
+ embedding: 'embedding',
110
+ entities: 'entities',
111
+ };
112
+ /**
113
+ * A transform id, from the kinds rather than from a string.
114
+ *
115
+ * Returns the EXACT literal — `tf({ in: [IoKinds.text, IoKinds.image], out: [IoKinds.text] })`
116
+ * has type `'text+image->text'`, not `TransformId` and not `string`. So a
117
+ * `requires:` entry written this way is checkable against `TRANSFORMS` at the
118
+ * call site by anything that cares to, and a mistyped kind is a compile error
119
+ * where it was written.
120
+ *
121
+ * The canonical STRING is still what gets stored, keyed on and compared — this
122
+ * is an authoring surface over `transformId`'s machinery, not a second
123
+ * representation. There is one body, `build`; the widened face over it is
124
+ * `transformId`, and see there for why the two are separate functions rather
125
+ * than an overload pair.
126
+ *
127
+ * A novel combination is deliberately still expressible: `tf({ in: [IoKinds.audio],
128
+ * out: [IoKinds.image] })` compiles and yields `'audio->image'`, which `TRANSFORMS`
129
+ * does not name. Core's table names what core ships; a plugin may introduce a
130
+ * transform, and closing that door is a separate ruling nobody has taken.
131
+ * @experimental
132
+ */
133
+ export function tf(t) {
134
+ return build(t);
135
+ }
136
+ /** The one body. Both public faces are this, typed differently. */
137
+ const build = (t) => `${side(t.in)}->${side(t.out)}`;
138
+ /**
139
+ * The widened form, over the same body.
140
+ *
141
+ * Kept because the runtime path has kinds in hand as `readonly IoKind[]` — a
142
+ * parsed id, a probe result — where there is no literal type to preserve and
143
+ * `TransformId` is the honest answer.
144
+ *
145
+ * ⚠ Deliberately a SEPARATE function rather than a second overload on `tf`. As
146
+ * an overload it silently rescues everything the narrow signature is there to
147
+ * reject: `tf({ in: [], out: [IoKinds.text] })` fails the non-empty `Kinds`
148
+ * check, falls through to `readonly IoKind[]`, and compiles as `TransformId`.
149
+ * Two faces over one body keeps `tf` strict and this one honest.
150
+ * @internal
151
+ */
152
+ export const transformId = (t) => build(t);
153
+ /**
154
+ * Human names for the transforms core knows about.
155
+ *
156
+ * A closed table over an open id space: a plugin may introduce a transform this
157
+ * does not name, and everything keeps working — it simply shows as its id until
158
+ * somebody names it. Never render a raw `text->image` to a person; that is what
159
+ * `transformLabel` is for.
160
+ * @internal
161
+ */
162
+ export const TRANSFORMS = {
163
+ 'text->text': {
164
+ i18n: { en: 'Chat' },
165
+ tagline: { en: 'Writes replies.' },
166
+ disclosure: 'basic',
167
+ },
168
+ 'text+image->text': {
169
+ i18n: { en: 'Vision' },
170
+ tagline: { en: 'Can look at pictures you send it.' },
171
+ },
172
+ 'text+document->text': {
173
+ i18n: { en: 'Document reading' },
174
+ tagline: { en: 'Can read files you attach.' },
175
+ },
176
+ 'text->image': {
177
+ i18n: { en: 'Image generation' },
178
+ tagline: { en: 'Draws pictures from a description.' },
179
+ },
180
+ 'text+image->image': {
181
+ i18n: { en: 'Image editing' },
182
+ tagline: { en: 'Changes a picture you give it.' },
183
+ },
184
+ 'image->image': {
185
+ i18n: { en: 'Image transform' },
186
+ tagline: { en: 'Upscales or restyles, with no prompt.' },
187
+ },
188
+ 'audio->text': {
189
+ i18n: { en: 'Transcription' },
190
+ tagline: { en: 'Turns speech into text.' },
191
+ },
192
+ 'text->audio': {
193
+ i18n: { en: 'Speech' },
194
+ tagline: { en: 'Reads text aloud.' },
195
+ },
196
+ 'text->embedding': {
197
+ i18n: { en: 'Embeddings' },
198
+ tagline: { en: 'Turns text into vectors, for search.' },
199
+ },
200
+ 'text->entities': {
201
+ i18n: { en: 'Named entities' },
202
+ tagline: { en: 'Finds the people, places and things a passage names.' },
203
+ },
204
+ };
205
+ // ── Features ────────────────────────────────────────────────────────────────
206
+ /**
207
+ * How a request can be constrained or SHAPED, as opposed to what flows through
208
+ * it.
209
+ *
210
+ * Every one of these is already implemented somewhere in the app and merely
211
+ * unnamed: `json_object` and `json_schema` are OpenAI's two `response_format`
212
+ * modes, `grammar` is the GBNF path the llama.cpp family takes, and `tools` is
213
+ * the four-valued `toolUse` field that sat on every adapter with no consumers.
214
+ *
215
+ * Two kinds live here, and the split is worth naming because it is not obvious
216
+ * from the list:
217
+ *
218
+ * - **Constraints** on one request — the five above, plus `streaming`, which
219
+ * constrains how the answer arrives rather than what it says.
220
+ * - **Behaviours** the connection can be asked for — `continue_reply`, and the
221
+ * two `wire_*` ids. These are not fields on a request; they say what kind of
222
+ * request can be made at all.
223
+ *
224
+ * The axis they share, and the reason none of them is a transform, is that every
225
+ * one qualifies a call whose in and out kinds are already settled. `text->text`
226
+ * either way; these say HOW.
227
+ * @experimental
228
+ */
229
+ export const FEATURES = [
230
+ 'json_object',
231
+ 'json_schema',
232
+ 'strict_schema',
233
+ 'grammar',
234
+ 'tools',
235
+ 'streaming',
236
+ /**
237
+ * The connection can resume an assistant message from a partial, as a TRUE
238
+ * PREFILL: the model is handed the text so far inside an OPEN assistant turn
239
+ * and writes the next characters of it, rather than being asked in a fresh
240
+ * turn to carry on.
241
+ *
242
+ * A behaviour rather than a wire, which is why it is not spelled `wire_*`. It
243
+ * is nonetheless answerable only alongside one — a completion wire prefills by
244
+ * leaving the seed block unclosed, and a chat wire can only prefill where the
245
+ * protocol itself accepts a trailing assistant turn — so the app's manifest
246
+ * records which of a type's wire modes actually carries it, and the effective
247
+ * answer is the pair. The SDK names the capability; it does not own that pair,
248
+ * because which endpoint an adapter speaks is not a fact this package can see.
249
+ *
250
+ * ⚠ The distinction it draws is the whole reason it exists. Appending a
251
+ * "please continue" user turn, or sending the partial as a closed assistant
252
+ * turn and hoping, both LOOK like a continuation and neither is one: the model
253
+ * starts a new reply, the join then glues two beginnings together, and nothing
254
+ * reports it. A connection that can only do that does not hold this.
255
+ */
256
+ 'continue_reply',
257
+ 'wire_chat',
258
+ 'wire_completion',
259
+ ];
260
+ /** Transforms contain `->`; features never do. The two spaces cannot collide. @internal */
261
+ export const isTransformId = (id) => id.includes('->');
262
+ /**
263
+ * Strengthenings: holding the key implies holding the values, at the same BAND.
264
+ *
265
+ * A backend that can enforce a strict schema can obviously enforce a loose one.
266
+ * Declaring the implication once means an adapter states the strongest thing it
267
+ * does and the rest follows, rather than seven adapters each remembering to list
268
+ * the weaker forms.
269
+ * @internal
270
+ */
271
+ export const IMPLIES = {
272
+ strict_schema: ['json_schema'],
273
+ json_schema: ['json_object'],
274
+ };
275
+ /**
276
+ * X is available at its `emulated` band when any of the values is at ITS top.
277
+ *
278
+ * This is where "Serene Pub provides it to a backend that never heard of it"
279
+ * becomes data instead of a comment. `jsonSchemaToGbnf` is literally
280
+ * "provide `json_schema` by compiling it to a `grammar`" — declared here once,
281
+ * rather than re-derived inside each adapter that happens to have a grammar.
282
+ * @internal
283
+ */
284
+ export const EMULATABLE_VIA = {
285
+ json_object: ['json_schema', 'grammar'],
286
+ json_schema: ['grammar'],
287
+ tools: ['json_schema', 'grammar'],
288
+ };
289
+ /**
290
+ * The band names core knows: what a grade MEANS, once you know the capability.
291
+ *
292
+ * A CLOSED vocabulary, while the grade scale is open, and the asymmetry is
293
+ * deliberate. These are the names an author writes, so closing them makes
294
+ * `"nativ"` a compile error at the place it was typed. The openness that matters
295
+ * is `bandsFor`'s fallback — a plugin's own transform grades correctly without
296
+ * anybody editing this file — and that needs no new band NAMES to work.
297
+ * @experimental
298
+ */
299
+ export const BAND = {
300
+ none: 'none',
301
+ emulated: 'emulated',
302
+ native: 'native',
303
+ };
304
+ /**
305
+ * Weakest to strongest, independently of any one capability's own table.
306
+ *
307
+ * Consulted when a band a capability does not HAVE is asked for: `emulated`
308
+ * asked of `text->image` walks down to `none`, because a claim is never stronger
309
+ * than what permits it and nothing supplies an emulated picture.
310
+ * @internal
311
+ */
312
+ export const BAND_ORDER = [BAND.none, BAND.emulated, BAND.native];
313
+ /**
314
+ * Per capability, what its grades mean — the index IS the grade.
315
+ *
316
+ * Only capabilities whose shape differs from binary are listed. Everything else
317
+ * falls back to `DEFAULT_BANDS` through `bandsFor`, which is the half that keeps
318
+ * this table from having to name a capability in order for that capability to
319
+ * grade correctly: a plugin introduces a transform, and it is binary until
320
+ * somebody says otherwise.
321
+ *
322
+ * The three listed here are exactly the keys of `EMULATABLE_VIA`, and that is
323
+ * not a coincidence. An `emulated` band exists precisely where Serene Pub can
324
+ * supply the capability itself over a backend that cannot, which is what that
325
+ * table records. `closure` raises through `gradeOf(id, BAND.emulated)`, so a
326
+ * capability with no such band cannot be emulated upward at all — the honest
327
+ * answer, arrived at without a second list stating it.
328
+ * @experimental
329
+ */
330
+ export const BANDS = {
331
+ json_object: [BAND.none, BAND.emulated, BAND.native],
332
+ json_schema: [BAND.none, BAND.emulated, BAND.native],
333
+ tools: [BAND.none, BAND.emulated, BAND.native],
334
+ };
335
+ /** A capability nobody gave bands: it either works or it does not. @experimental */
336
+ export const DEFAULT_BANDS = [BAND.none, BAND.native];
337
+ /** @internal */
338
+ export const bandsFor = (id) => BANDS[id] ?? DEFAULT_BANDS;
339
+ /** The best this capability can be — what `native` means FOR IT. @experimental */
340
+ export const topGrade = (id) => bandsFor(id).length - 1;
341
+ /** Which band a grade lands in, or `undefined` for a grade off the scale. @internal */
342
+ export const bandOf = (id, grade) => bandsFor(id)[grade];
343
+ /**
344
+ * A spec as a grade, on this capability's own scale.
345
+ *
346
+ * Total, and never stronger than the scale allows: a number is clamped into
347
+ * range, and a band the capability does not have walks DOWN `BAND_ORDER`. Both
348
+ * failures are authoring bugs rather than data, so this stays pure and the
349
+ * manifest conformance test is what fails the build over one.
350
+ * @internal
351
+ */
352
+ export function gradeOf(id, spec) {
353
+ if (typeof spec === 'number')
354
+ return Math.max(0, Math.min(topGrade(id), Math.trunc(spec)));
355
+ const bands = bandsFor(id);
356
+ for (let i = BAND_ORDER.indexOf(spec); i >= 0; i--) {
357
+ const at = bands.indexOf(BAND_ORDER[i]);
358
+ if (at >= 0)
359
+ return at;
360
+ }
361
+ return 0;
362
+ }
363
+ /**
364
+ * The same BAND, read against another capability's scale.
365
+ *
366
+ * `IMPLIES` propagates strength BETWEEN capabilities, and propagating the raw
367
+ * number would be wrong wherever two shapes differ. `strict_schema` is binary,
368
+ * so its native grade is 1; writing that 1 into three-band `json_schema` would
369
+ * say EMULATED — quietly demoting the strongest claim available into the weakest
370
+ * supported one, which is the exact failure a flat enum could not have. The band
371
+ * is the thing that carries across; the number is local.
372
+ */
373
+ const rebase = (from, to, grade) => {
374
+ const band = bandOf(from, grade);
375
+ return band === undefined ? 0 : gradeOf(to, band);
376
+ };
377
+ /**
378
+ * The composite of the two independent gradings: their floor.
379
+ *
380
+ * The ONE comparator in this file, and it is `Math.min` because a grade is a
381
+ * number. Three hardcoded rank tables used to do this — in `closure`, in
382
+ * `resolveCapabilities`, and in a `weakest` helper — and the fourth would have
383
+ * been written by whoever next needed to compare two.
384
+ *
385
+ * The two arguments are what the four layers actually produce, named:
386
+ *
387
+ * - the API grade, from the adapter/manifest layer — what this WIRE FORMAT can
388
+ * express. Also an absolute gate on the key space, which is why it is the
389
+ * ceiling every layer below is taken against rather than merely a default.
390
+ * - the MODEL grade, from the preset, the probe and the person — what the
391
+ * thing actually answering does. Inherently per-model: vision is a property
392
+ * of a model, not of a format, which is why no preset can settle it.
393
+ *
394
+ * Neither can promise past the other, so the composite is the floor — and the
395
+ * two need no second column, table or stored field to be graded independently.
396
+ * @experimental
397
+ */
398
+ export const composite = (api, model) => Math.min(api, model);
399
+ const isUnproven = (d) => typeof d === 'object' && d !== null;
400
+ /**
401
+ * The API grade: the best this capability could be, if something asserts it.
402
+ *
403
+ * For a plain declaration that is the declaration itself. For an unproven one it
404
+ * is the capability's TOP band: the point of probing is that the protocol *can*
405
+ * do it and we do not yet know whether this model does — so an answer, from a
406
+ * preset or a live probe, is allowed to be a full-strength one. Per capability,
407
+ * because "full strength" is 2 for `tools` and 1 for `text->image`.
408
+ */
409
+ const ceiling = (id, d) => isUnproven(d) ? topGrade(id) : gradeOf(id, d);
410
+ /**
411
+ * What to assume when nobody has asserted anything.
412
+ *
413
+ * Always at or below `ceiling`, by construction: a plain declaration is both,
414
+ * and `until` is clamped to the same scale.
415
+ */
416
+ const floor = (id, d) => gradeOf(id, isUnproven(d) ? d.until : d);
417
+ /**
418
+ * The grade as a letter, relative to THIS capability's own top band.
419
+ *
420
+ * Derived at render and nowhere else: no letter is ever stored, sent on the wire
421
+ * or compared. `A` is the best this capability can be, so a two-band
422
+ * capability's grade 1 is an `A` — which is the entire reason grades are read
423
+ * per capability rather than against one global scale.
424
+ *
425
+ * Grade 0 has no letter, because 0 is not a quality but an absence; the caller
426
+ * says "Off". Anything past `Z` clamps rather than walking out of the alphabet.
427
+ * @internal
428
+ */
429
+ export function gradeLetter(id, grade) {
430
+ if (typeof grade !== 'number' || grade <= 0)
431
+ return undefined;
432
+ const down = Math.min(25, Math.max(0, topGrade(id) - Math.trunc(grade)));
433
+ return String.fromCharCode(65 + down);
434
+ }
435
+ // ── Closure ─────────────────────────────────────────────────────────────────
436
+ /**
437
+ * Apply `IMPLIES` and `EMULATABLE_VIA` until nothing changes.
438
+ *
439
+ * Run once at the end of resolution rather than at every read, so `satisfies()`
440
+ * stays a lookup. The fixpoint loop is bounded by the feature count; it exists
441
+ * because the two relations chain — a native `grammar` gives `json_schema` at
442
+ * `emulated`, which in turn implies `json_object`.
443
+ * @experimental
444
+ */
445
+ export function closure(set) {
446
+ const out = { ...set };
447
+ const raise = (id, grade) => {
448
+ const have = out[id];
449
+ if (have === undefined || grade > have) {
450
+ out[id] = grade;
451
+ return true;
452
+ }
453
+ return false;
454
+ };
455
+ for (let pass = 0; pass < FEATURES.length + 1; pass++) {
456
+ let moved = false;
457
+ for (const [id, implied] of Object.entries(IMPLIES)) {
458
+ const source = id;
459
+ const grade = out[source];
460
+ // 0 and absent alike: neither is something to propagate.
461
+ if (!grade)
462
+ continue;
463
+ // By BAND, not by number — see `rebase`. `strict_schema` is binary and
464
+ // `json_schema` is not, so the two scales genuinely disagree here.
465
+ for (const weaker of implied)
466
+ moved = raise(weaker, rebase(source, weaker, grade)) || moved;
467
+ }
468
+ for (const [id, via] of Object.entries(EMULATABLE_VIA)) {
469
+ const feature = id;
470
+ // Already at its own best; nothing to add.
471
+ if (out[feature] === topGrade(feature))
472
+ continue;
473
+ // "Native" per provider, which is its OWN top band: `grammar` is binary
474
+ // and tops out at 1, `json_schema` tops out at 2.
475
+ const can = via.some((v) => out[v] === topGrade(v));
476
+ if (can)
477
+ moved = raise(feature, gradeOf(feature, BAND.emulated)) || moved;
478
+ }
479
+ if (!moved)
480
+ break;
481
+ }
482
+ return out;
483
+ }
484
+ /**
485
+ * The four layers, collapsed to what a connection stores.
486
+ *
487
+ * Pure and total: no I/O, and every input optional, so a connection that has
488
+ * never been tested still resolves to something coherent rather than to nothing.
489
+ * @internal
490
+ */
491
+ export function resolveCapabilities(args) {
492
+ const { adapter, preset, probe, overrides } = args;
493
+ const out = {};
494
+ const defaults = new Set(adapter.defaults ?? []);
495
+ for (const [key, declared] of Object.entries(adapter.supports)) {
496
+ const id = key;
497
+ if (declared === undefined)
498
+ continue;
499
+ // The API grade, graded once and independently: what this WIRE FORMAT can
500
+ // express. Also the gate — every layer below is taken against it, so a
501
+ // toggle can never make a protocol able to do something it has no field
502
+ // for. Taken ONCE at the end rather than at each layer, which is the same
503
+ // answer (every layer already assigned at or below it) and says the
504
+ // api/model split out loud.
505
+ const api = ceiling(id, declared);
506
+ // The MODEL grade, from the three layers that speak about the thing on the
507
+ // other end. Layer 1 — the adapter's own position: its honest assumption,
508
+ // if on.
509
+ let model = defaults.has(id) ? floor(id, declared) : 0;
510
+ // Layer 2 — the preset. A named service knows more than the generic
511
+ // protocol does, so `true` here is an ASSERTION and resolves to the
512
+ // ceiling, not to the adapter's pessimistic fallback. Without that, a
513
+ // preset could never switch on a probed capability at all.
514
+ const p = preset?.[id];
515
+ if (p !== undefined) {
516
+ if (p === true)
517
+ model = api;
518
+ else if (p === false)
519
+ model = 0;
520
+ else
521
+ model = gradeOf(id, p);
522
+ }
523
+ // Layer 3 — what the backend actually answered. Only for a capability the
524
+ // adapter said to ask about: a probe cannot answer a question that was
525
+ // never open, so it cannot contradict an outright declaration. This is the
526
+ // per-MODEL grading; the adapter above is the per-format one.
527
+ if (isUnproven(declared) && probe && probe[id] !== undefined)
528
+ model = gradeOf(id, probe[id]);
529
+ // Layer 4 — the person. Final, and still clamped by `composite` below.
530
+ const o = overrides?.[id];
531
+ if (o !== undefined)
532
+ model = o === false ? 0 : gradeOf(id, o);
533
+ const grade = composite(api, model);
534
+ if (grade > 0)
535
+ out[id] = grade;
536
+ }
537
+ return closure(out);
538
+ }
539
+ /** @experimental */
540
+ export function satisfies(need, have) {
541
+ const missing = [];
542
+ const degraded = [];
543
+ const absent = [];
544
+ const grades = {};
545
+ // `typeof` and not merely `> 0`, because a `CapabilitySet` routinely arrives
546
+ // from a loose JSON column that no type checked on the way in. A non-number
547
+ // there reads as UNSUPPORTED, which is the safe direction: `!"native"` is
548
+ // false, so a truthiness test alone would grant a capability on the strength
549
+ // of an unmigrated string.
550
+ const held = (id) => {
551
+ const grade = have[id];
552
+ return typeof grade === 'number' && grade > 0 ? grade : undefined;
553
+ };
554
+ for (const id of need.requires ?? []) {
555
+ const grade = held(id);
556
+ if (grade === undefined)
557
+ missing.push(id);
558
+ else {
559
+ grades[id] = grade;
560
+ if (grade < topGrade(id))
561
+ degraded.push(id);
562
+ }
563
+ }
564
+ for (const id of need.optional ?? []) {
565
+ const grade = held(id);
566
+ if (grade === undefined)
567
+ absent.push(id);
568
+ else
569
+ grades[id] = grade;
570
+ }
571
+ return { ok: missing.length === 0, missing, degraded, absent, grades };
572
+ }
573
+ /**
574
+ * The capability each mode is requested as.
575
+ *
576
+ * Features rather than transforms, and deliberately: what flows is `text->text`
577
+ * in both modes. These qualify HOW the request is made, which is exactly what
578
+ * the feature axis is for (`json_schema` is a field on a request, `grammar` is a
579
+ * decoder constraint, and these two are the shape of the prompt field itself).
580
+ *
581
+ * Two ids rather than one enum-valued key because a backend routinely offers
582
+ * BOTH, and a single key could not say so — nor be graded, presetted, probed and
583
+ * hand-overridden independently, which is the entire four-layer model.
584
+ * @internal
585
+ */
586
+ export const WIRE_CAPABILITY = {
587
+ chat: 'wire_chat',
588
+ completion: 'wire_completion',
589
+ };
590
+ /**
591
+ * The tie-break, as data: which mode wins when a connection offers both.
592
+ *
593
+ * **Chat first.** Three reasons, in the order they matter:
594
+ *
595
+ * 1. It is the mode that LOSES NOTHING. Roles reach the model as roles; a flat
596
+ * completion has to encode them as delimiters the model must have been
597
+ * trained to read. Going the other way is always possible (a flat prompt can
598
+ * be rebuilt from messages) and going this way is not, so the recoverable
599
+ * direction is the safe default.
600
+ * 2. Completion mode carries a SECOND choice — which completion template — and
601
+ * getting it wrong produces run-on generation with no error attached. An
602
+ * "either" that silently required a second correct decision would not be an
603
+ * either.
604
+ * 3. It is what every adapter offering both already defaulted to before wire
605
+ * mode existed (`extraJson.useSession ?? true`, `prerenderPrompt ?? false`),
606
+ * so an existing connection keeps sending what it sent yesterday.
607
+ *
608
+ * A caller that genuinely requires one mode does not consult this at all — it
609
+ * asks for that mode's capability by id, like any other.
610
+ * @internal
611
+ */
612
+ export const WIRE_MODE_ORDER = ['chat', 'completion'];
613
+ /**
614
+ * The mode a resolved capability set prefers, or `undefined` if it holds neither.
615
+ *
616
+ * The "either" default: a caller that does not care hands over what the four
617
+ * layers resolved and is told which method to use. `undefined` is a real answer
618
+ * — a set that names neither wire capability has not been resolved against an
619
+ * adapter that declares one — and is deliberately not defaulted here, because
620
+ * the honest fallback depends on what the caller knows about the connection.
621
+ * @internal
622
+ */
623
+ export function wireModeOf(have) {
624
+ for (const mode of WIRE_MODE_ORDER) {
625
+ const grade = have[WIRE_CAPABILITY[mode]];
626
+ if (typeof grade === 'number' && grade > 0)
627
+ return mode;
628
+ }
629
+ return undefined;
630
+ }
631
+ // ── Naming ──────────────────────────────────────────────────────────────────
632
+ const FEATURE_LABELS = {
633
+ json_object: 'JSON output',
634
+ json_schema: 'JSON schema',
635
+ strict_schema: 'Strict JSON schema',
636
+ grammar: 'Grammar constraints',
637
+ tools: 'Tool calling',
638
+ streaming: 'Streaming',
639
+ // Plain language, and deliberately the USER's word for the button rather
640
+ // than the mechanism's ("prefill", "assistant prefill"): the row answers
641
+ // "can I press Continue on a reply from this connection?".
642
+ continue_reply: 'Continue a reply',
643
+ // The two wire modes, named by what the backend RECEIVES rather than by the
644
+ // endpoint it is posted to — `prerenderPrompt` on an OpenAI-compatible
645
+ // connection still posts to /v1/chat/completions, and what makes it a
646
+ // completion is that the model is handed one formatted string.
647
+ wire_chat: 'Chat messages',
648
+ wire_completion: 'Text completion',
649
+ };
650
+ /**
651
+ * What to show a person for a capability id.
652
+ *
653
+ * Never render the id itself: `text+image->text` is an address, not a name, and
654
+ * a capability screen full of arrows is the thing rulings about newcomers are
655
+ * trying to prevent. An unknown id falls back to the id so a plugin's capability
656
+ * is legible rather than blank.
657
+ * @internal
658
+ */
659
+ export function capabilityLabel(id) {
660
+ if (isTransformId(id)) {
661
+ const meta = TRANSFORMS[id];
662
+ const label = meta?.i18n;
663
+ if (!label)
664
+ return id;
665
+ return typeof label === 'string' ? label : label.en;
666
+ }
667
+ return FEATURE_LABELS[id] ?? id;
668
+ }
669
+ /** The one-liner under the name, when there is one. @internal */
670
+ export function capabilityTagline(id) {
671
+ if (!isTransformId(id))
672
+ return undefined;
673
+ const t = TRANSFORMS[id]?.tagline;
674
+ return typeof t === 'string' ? t : t?.en;
675
+ }
676
+ /** Whether a first-timer should see this without opening anything. @internal */
677
+ export const isBasicCapability = (id) => isTransformId(id) &&
678
+ TRANSFORMS[id]?.disclosure === 'basic';
679
+ //# sourceMappingURL=capabilities.js.map