@chatpanel/events 0.102.0 → 0.103.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 (138) hide show
  1. package/adapters.js +1 -83
  2. package/adaptive-tool-policy.js +1 -45
  3. package/agent.js +2 -255
  4. package/attribution.js +1 -132
  5. package/backup-envelope.js +1 -221
  6. package/board-tool.js +2 -165
  7. package/budget.js +1 -116
  8. package/capability.js +1 -126
  9. package/citations.js +5 -127
  10. package/client-prefs.js +1 -148
  11. package/context-attachments.js +8 -144
  12. package/cowriter-router.js +1 -83
  13. package/cowriter-writer.js +19 -193
  14. package/cowriter.js +1 -190
  15. package/curate.js +3 -509
  16. package/distance.js +1 -124
  17. package/engine.js +1 -131
  18. package/entitlement.js +1 -332
  19. package/entity.js +1 -354
  20. package/event.js +1 -175
  21. package/extraction.js +4 -293
  22. package/failover.js +1 -93
  23. package/find-tool.js +1 -66
  24. package/flowchart.js +2 -644
  25. package/gate.js +1 -74
  26. package/harness.js +4 -101
  27. package/index.js +1 -345
  28. package/invariants.js +1 -174
  29. package/job.js +3 -147
  30. package/kernel.js +1 -255
  31. package/knowledge-derive.js +1 -267
  32. package/knowledge.js +3 -230
  33. package/library.js +11 -280
  34. package/loop.js +1 -132
  35. package/manifest.js +1 -107
  36. package/markdown-authoring.js +11 -288
  37. package/markdown-render.js +0 -0
  38. package/mcp-client.js +8 -358
  39. package/mcp-dispatch.js +1 -52
  40. package/mcp-errors.js +1 -106
  41. package/mcp-manager.js +1 -127
  42. package/media-transcript.js +8 -531
  43. package/meeting-analyzers.js +1 -83
  44. package/meeting-insights.js +7 -164
  45. package/meeting-shape.js +1 -282
  46. package/meeting-text.js +5 -94
  47. package/memory.js +2 -638
  48. package/model-candidates.js +1 -358
  49. package/model-health.js +1 -182
  50. package/model-ledger.js +1 -228
  51. package/model-picker.js +1 -188
  52. package/note-actions.js +15 -246
  53. package/note-graph.js +1 -158
  54. package/note-links.js +1 -68
  55. package/note-mentions.js +8 -114
  56. package/note-plan.js +9 -107
  57. package/note-research.js +1 -135
  58. package/observability.js +1 -119
  59. package/omni.js +1 -125
  60. package/order.js +1 -78
  61. package/package.json +1 -4
  62. package/pdf-layout.js +7 -253
  63. package/project-run.js +20 -444
  64. package/project.js +1 -170
  65. package/promotion.js +1 -171
  66. package/queue.js +1 -98
  67. package/reach.js +1 -23
  68. package/recipe-tool.js +2 -161
  69. package/recipe.js +1 -264
  70. package/record-list.js +2 -110
  71. package/recruit.js +3 -456
  72. package/redaction-tokens.js +1 -61
  73. package/ref.js +1 -55
  74. package/registry.js +1 -240
  75. package/route-graph.js +1 -115
  76. package/route-strategies.js +2 -232
  77. package/router.js +2 -880
  78. package/rrf.js +1 -89
  79. package/rules.js +1 -142
  80. package/schedule.js +1 -663
  81. package/scm-connection.js +1 -180
  82. package/scopes.js +1 -12
  83. package/scorecard.js +1 -335
  84. package/search-engines.js +1 -81
  85. package/skill-manifest.js +1 -330
  86. package/skill-scan.js +3 -209
  87. package/skill-sources.js +1 -162
  88. package/skill-vars.js +2 -252
  89. package/slash-commands.js +1 -158
  90. package/source-gate.js +1 -92
  91. package/sources-retrieval.js +5 -186
  92. package/sources.js +1 -273
  93. package/store.js +1 -171
  94. package/structured.js +4 -901
  95. package/subject-kinds.js +1 -5
  96. package/subject-name.js +1 -96
  97. package/sync-plan.js +1 -213
  98. package/synthesis.js +3 -123
  99. package/tags.js +1 -203
  100. package/team-board.js +6 -337
  101. package/team-cache.js +2 -63
  102. package/team-observe.js +1 -118
  103. package/team-org.js +1 -442
  104. package/team-plan.js +4 -131
  105. package/team-record.js +1 -225
  106. package/team-run.js +9 -652
  107. package/team-subtask.js +2 -191
  108. package/team-task.js +2 -124
  109. package/team-tool.js +2 -196
  110. package/team-trail.js +2 -73
  111. package/team-worklog.js +2 -149
  112. package/team.js +1 -302
  113. package/text-search.js +1 -155
  114. package/theme.js +17 -154
  115. package/titles.js +4 -285
  116. package/tool-discovery.js +1 -87
  117. package/tool-dispatch.js +5 -230
  118. package/tool-groups.js +1 -81
  119. package/tool-hints.js +10 -185
  120. package/tool-loop-guard.js +4 -182
  121. package/tool-need.js +2 -124
  122. package/tool-result.js +2 -438
  123. package/tool-round.js +0 -0
  124. package/tool-schema.js +1 -155
  125. package/tool-traits.js +1 -182
  126. package/toolset.js +2 -88
  127. package/trajectory.js +1 -530
  128. package/turn-loop.js +4 -440
  129. package/upcast.js +1 -37
  130. package/vault.js +2 -250
  131. package/view.js +1 -88
  132. package/voice-intents.js +2 -1488
  133. package/voice-speaker.js +1 -98
  134. package/weather-tool.js +1 -65
  135. package/weather.js +2 -211
  136. package/web-search-tool.js +14 -102
  137. package/web-search.js +1 -160
  138. package/widget.js +1 -153
package/structured.js CHANGED
@@ -1,901 +1,4 @@
1
- // Structured output one schema, one prompt, one parser, everywhere.
2
- //
3
- // A dozen places in ChatPanel ask a model for a small typed answer: what was this person
4
- // actually asking for, what are the topics of this note, which entities in this text are
5
- // PII, what should this meeting be called. Every one of them was written the same way and
6
- // none of them shared a line of code:
7
- //
8
- // const prompt = 'Return ONLY a JSON object: {"kind":"...","name":"..."}'; // hand-typed
9
- // const obj = JSON.parse(text.slice(text.indexOf('{'), text.lastIndexOf('}') + 1));
10
- //
11
- // That shape has three faults, and each of them cost a real bug:
12
- //
13
- // 1. THE SHAPE IS TYPED TWICE — once in the prompt string and once in the parser, thirty
14
- // lines apart, with nothing making them agree. Add a field to one and the other
15
- // silently ignores it forever.
16
- // 2. THE REPAIRS DO NOT PROPAGATE. voice-intents learned in production that a small model
17
- // answers "none" as prose with no JSON at all; topic-extraction learned separately that
18
- // models wrap answers in code fences; suggestions learned separately that they emit a
19
- // markdown list instead of an array. Three files, three lessons, no sharing — and the
20
- // fourth site starts from zero and re-earns all three.
21
- // 3. indexOf('{') … lastIndexOf('}') IS NOT A PARSER. It breaks on a brace inside a
22
- // string, on prose that mentions JSON, and on every truncated response — which is
23
- // every response, while it is still streaming.
24
- //
25
- // So: describe the answer ONCE as a schema. The prompt is rendered from it, the JSON Schema
26
- // for models that support structured output is derived from it, and the parser coerces onto
27
- // it. A field cannot drift from its own description.
28
- //
29
- // WHY NOT A LIBRARY. The obvious answer is BAML, which is right about the two ideas here —
30
- // schema-aligned parsing and deriving the prompt from the schema. Its runtime is a Rust core
31
- // behind a Node native addon (eight platform binaries) driven by codegen. This package is
32
- // vendored into the extension by file copy, loads as raw ES modules under MV3 CSP with no
33
- // bundler, and must also run in the gateway, the bridge and a mobile JS runtime. A native
34
- // addon cannot go here. The ideas can, and they are small.
35
- //
36
- // WHY IT LIVES IN THE SHARED PACKAGE. Ask the test from CLAUDE.md: could a mobile client
37
- // need it? It is the only thing standing between a 3B local model and a usable answer, so
38
- // yes — every client needs it, including the ones that do not exist yet. Pure input → output,
39
- // no clock, no network, no platform API.
40
- //
41
- // THE TWO AUDIENCES, one schema:
42
- //
43
- // • A CAPABLE MODEL over an OpenAI-compatible endpoint gets `responseFormat(schema)` and is
44
- // constrained by the server — the answer arrives well-formed and `coerce` is a formality.
45
- // • A SMALL LOCAL MODEL, or an agent CLI (Claude Code, Codex) which has no response_format
46
- // at all, gets `describe(schema)` in the prompt and everything it emits goes through the
47
- // repair pass. This is the path that actually needed building, and it is why the parser
48
- // is generous rather than strict.
49
- //
50
- // AND IT STREAMS. `createStructuredStream` re-reads the buffer as tokens arrive and hands
51
- // back the object so far plus the set of fields that are FINISHED — so a panel can render a
52
- // request as it is being written and only commit an enum once the model has closed it.
53
- // Truncated JSON is the normal case mid-stream, not an error, which is why the repair pass
54
- // treats "close whatever is open" as a first-class operation.
55
-
56
- export class StructuredError extends Error {
57
- constructor(code, message) { super(message); this.name = 'StructuredError'; this.code = code; }
58
- }
59
-
60
- /** Field types a schema may declare. Deliberately small — this is for SMALL answers. */
61
- export const FIELD_TYPES = Object.freeze([
62
- 'string', 'number', 'integer', 'boolean', 'enum', 'string[]', 'number[]', 'object[]',
63
- ]);
64
-
65
- // Whole-answer prose a model emits INSTEAD of JSON when the honest answer is "nothing".
66
- // Told to return JSON and having nothing to report, small models very often just say the
67
- // word. Reading that as unparseable is worse than useless: the caller falls back to its
68
- // deterministic reading and acts on something the model has just said was not there.
69
- const NOTHING = /^(?:none|n\/a|na|nothing|no|null|nil|empty|no results?|nothing found|no request|-{1,3}|\.)\s*[.!]?$/i;
70
-
71
- // Prose a model puts in FRONT of the JSON. Stripped only when a structural character
72
- // follows, so a legitimate answer that happens to start with "Sure" is never touched.
73
- const LEAD_IN = /^(?:(?:sure|certainly|of course|okay|ok|got it|here(?:'s| is)(?: the)?(?: \w+)?|the (?:json|answer|result|output)(?: is)?|json|output|answer|result)\s*[:.!,-]*\s*)+/i;
74
-
75
- const isObj = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
76
- const clip = (s, n) => (s.length > n ? `${s.slice(0, n)}` : s);
77
-
78
- // ---------------------------------------------------------------------------
79
- // Defining a schema
80
- // ---------------------------------------------------------------------------
81
-
82
- /**
83
- * Declare the answer once.
84
- *
85
- * defineSchema({
86
- * name: 'refinement',
87
- * purpose: 'what the speaker actually wants done',
88
- * fields: {
89
- * request: { type: 'string', required: true, max: 200, describe: 'one sentence, their words' },
90
- * kind: { type: 'enum', values: ['question','monitor','note'], default: 'question' },
91
- * },
92
- * })
93
- *
94
- * `fields` is ORDERED — the prompt lists them in declaration order, and models answer in the
95
- * order they are asked, which is what makes the streaming `settled` set arrive in a useful
96
- * sequence rather than at random.
97
- */
98
- export function defineSchema(spec = {}) {
99
- const name = String(spec.name || '').trim();
100
- if (!name) throw new StructuredError('no-name', 'a schema needs a name');
101
- if (!isObj(spec.fields) || !Object.keys(spec.fields).length) {
102
- throw new StructuredError('no-fields', `schema ${name} declares no fields`);
103
- }
104
- const fields = {};
105
- for (const [key, raw] of Object.entries(spec.fields)) fields[key] = defineField(name, key, raw);
106
- if (spec.nothingIf != null && !(spec.nothingIf instanceof RegExp)) {
107
- throw new StructuredError('bad-nothing', `schema ${name}: nothingIf must be a RegExp`);
108
- }
109
- if (spec.fallback != null && spec.fallback !== 'lines' && typeof spec.fallback !== 'function') {
110
- throw new StructuredError('bad-fallback', `schema ${name}: fallback must be 'lines' or a function`);
111
- }
112
- return Object.freeze({
113
- name,
114
- purpose: String(spec.purpose || '').trim(),
115
- fields: Object.freeze(fields),
116
- order: Object.freeze(Object.keys(fields)),
117
- // WHAT "THE MODEL REPORTED NOTHING" LOOKS LIKE, as a value the caller can act on.
118
- //
119
- // Two different answers mean it and neither is a parse failure: the whole reply is the
120
- // word ("none", "N/A", "nothing found"), or the reply is valid JSON whose required field
121
- // is that word. Both were read as unparseable before, so the caller fell back to its
122
- // deterministic reading and acted on something the model had just said was not there —
123
- // which is how a user ended up with a chat message reading "none".
124
- //
125
- // A schema that declares no `nothing` gets null for both, which is the honest answer when
126
- // there is no meaningful empty value to hand back.
127
- nothing: spec.nothing ?? null,
128
- nothingIf: spec.nothingIf ?? null,
129
- // How to read an answer that never contained JSON at all. 'lines' handles the case every
130
- // list-shaped schema hits — the model replied with a markdown list — and is only legal
131
- // when there is exactly one array field to put the lines into.
132
- fallback: spec.fallback ?? null,
133
- // Reject the whole answer when a required field is missing, rather than handing back a
134
- // half-object the caller has to re-validate. A caller that wants the half-object asks
135
- // for `partial`.
136
- strictRequired: spec.strictRequired !== false,
137
- });
138
- }
139
-
140
- function defineField(schemaName, key, raw) {
141
- const spec = typeof raw === 'string' ? { type: raw } : { ...(raw || {}) };
142
- const type = String(spec.type || 'string');
143
- if (!FIELD_TYPES.includes(type)) {
144
- throw new StructuredError('bad-type', `schema ${schemaName}.${key}: unknown type ${JSON.stringify(type)}`);
145
- }
146
- if (type === 'enum') {
147
- const values = (spec.values || []).map((v) => String(v));
148
- if (!values.length) throw new StructuredError('bad-enum', `schema ${schemaName}.${key}: enum with no values`);
149
- if (spec.default != null && !values.includes(String(spec.default))) {
150
- throw new StructuredError('bad-default', `schema ${schemaName}.${key}: default ${JSON.stringify(spec.default)} is not one of its values`);
151
- }
152
- spec.values = Object.freeze(values);
153
- // Lower-cased alias → canonical value. Models answer "Question", "a question" and
154
- // "question." for the same enum; an unknown value falling through to the default is a
155
- // silently wrong answer, so near-misses are mapped rather than discarded.
156
- const aliases = new Map();
157
- for (const v of values) aliases.set(v.toLowerCase(), v);
158
- for (const [from, to] of Object.entries(spec.aliases || {})) {
159
- if (!values.includes(String(to))) throw new StructuredError('bad-alias', `schema ${schemaName}.${key}: alias → ${to}, which is not a value`);
160
- aliases.set(String(from).toLowerCase(), String(to));
161
- }
162
- spec.aliasMap = aliases;
163
- }
164
- if (type === 'object[]') {
165
- if (!isObj(spec.fields)) throw new StructuredError('bad-items', `schema ${schemaName}.${key}: object[] needs \`fields\``);
166
- const sub = {};
167
- for (const [k, v] of Object.entries(spec.fields)) sub[k] = defineField(`${schemaName}.${key}[]`, k, v);
168
- spec.fields = Object.freeze(sub);
169
- spec.order = Object.freeze(Object.keys(sub));
170
- }
171
- if (spec.emptyIf != null && !(spec.emptyIf instanceof RegExp)) {
172
- throw new StructuredError('bad-emptyif', `schema ${schemaName}.${key}: emptyIf must be a RegExp`);
173
- }
174
- spec.type = type;
175
- spec.describe = String(spec.describe || '').trim();
176
- return Object.freeze(spec);
177
- }
178
-
179
- const isListType = (t) => t === 'string[]' || t === 'number[]' || t === 'object[]';
180
-
181
- /** The single array field of a schema, when there is exactly one. Null otherwise. */
182
- function soleListField(schema) {
183
- const lists = schema.order.filter((k) => isListType(schema.fields[k].type));
184
- return lists.length === 1 ? lists[0] : null;
185
- }
186
-
187
- // ---------------------------------------------------------------------------
188
- // Rendering the prompt
189
- // ---------------------------------------------------------------------------
190
-
191
- /**
192
- * The instruction block, rendered FROM the schema — so the shape a model is shown and the
193
- * shape the parser expects cannot disagree.
194
- *
195
- * Kept short on purpose. These calls run on fast models while a meeting is happening; the
196
- * schema block is paid for on every one of them, and a paragraph per field would cost more
197
- * than the answer. A field's `describe` is the one place to spend words.
198
- */
199
- export function describeSchema(schema, { fences = false } = {}) {
200
- const shape = schema.order.map((k) => `${JSON.stringify(k)}: ${shapeOf(schema.fields[k])}`).join(', ');
201
- const notes = [];
202
- for (const key of schema.order) {
203
- const f = schema.fields[key];
204
- const bits = [];
205
- if (f.describe) bits.push(f.describe);
206
- if (f.required) bits.push('required');
207
- if (f.default != null && !f.required) bits.push(`defaults to ${JSON.stringify(f.default)}`);
208
- if (f.type === 'string' && f.max) bits.push(`at most ${f.max} characters`);
209
- if (isListType(f.type) && f.maxItems) bits.push(`at most ${f.maxItems} items`);
210
- if (bits.length) notes.push(`- ${key} — ${bits.join('; ')}.`);
211
- }
212
- return [
213
- schema.purpose ? `${schema.purpose}` : '',
214
- schema.purpose ? '' : '',
215
- fences
216
- ? 'Return a single JSON object in a ```json code fence and nothing else:'
217
- : 'Return ONLY a JSON object — no prose, no code fences, no explanation:',
218
- `{${shape}}`,
219
- notes.length ? '' : '',
220
- ...notes,
221
- ].filter((l) => l !== '').join('\n');
222
- }
223
-
224
- function shapeOf(f) {
225
- switch (f.type) {
226
- case 'enum': return f.values.map((v) => JSON.stringify(v)).join('|');
227
- case 'string[]': return '[string, …]';
228
- case 'number[]': return '[number, …]';
229
- case 'object[]': return `[{${f.order.map((k) => `${JSON.stringify(k)}: ${shapeOf(f.fields[k])}`).join(', ')}}, …]`;
230
- case 'integer': return 'integer';
231
- case 'number': return 'number';
232
- case 'boolean': return 'true|false';
233
- default: return 'string';
234
- }
235
- }
236
-
237
- // ---------------------------------------------------------------------------
238
- // The same schema, as JSON Schema
239
- // ---------------------------------------------------------------------------
240
-
241
- /**
242
- * JSON Schema for the endpoints that can enforce it — OpenAI `json_schema` response format,
243
- * and tool/function parameters.
244
- *
245
- * `strict` mode on OpenAI-compatible servers requires that EVERY property is listed in
246
- * `required` and that `additionalProperties` is false, so optional fields are expressed as a
247
- * union with null rather than by omission. That is the server's rule, not ours; `coerce`
248
- * still treats a null there as absent.
249
- */
250
- export function toJsonSchema(schema, { strict = true } = {}) {
251
- const properties = {};
252
- for (const key of schema.order) properties[key] = jsonSchemaField(schema.fields[key]);
253
- const required = strict
254
- ? schema.order.slice()
255
- : schema.order.filter((k) => schema.fields[k].required);
256
- return {
257
- type: 'object',
258
- properties,
259
- required,
260
- additionalProperties: false,
261
- };
262
- }
263
-
264
- function jsonSchemaField(f) {
265
- const base = f.describe ? { description: f.describe } : {};
266
- switch (f.type) {
267
- case 'enum': return { ...base, type: 'string', enum: f.values.slice() };
268
- case 'integer': return { ...base, type: 'integer' };
269
- case 'number': return { ...base, type: 'number' };
270
- case 'boolean': return { ...base, type: 'boolean' };
271
- case 'string[]': return { ...base, type: 'array', items: { type: 'string' } };
272
- case 'number[]': return { ...base, type: 'array', items: { type: 'number' } };
273
- case 'object[]': return {
274
- ...base,
275
- type: 'array',
276
- items: {
277
- type: 'object',
278
- properties: Object.fromEntries(f.order.map((k) => [k, jsonSchemaField(f.fields[k])])),
279
- required: f.order.slice(),
280
- additionalProperties: false,
281
- },
282
- };
283
- default: return { ...base, type: 'string' };
284
- }
285
- }
286
-
287
- /**
288
- * The request body fragment that makes a server do the work for us.
289
- *
290
- * `mode: 'schema'` is the strongest and the narrowest — real grammar-constrained decoding,
291
- * supported by OpenAI and a growing set of compatible servers. `mode: 'object'` is the older,
292
- * near-universal JSON mode, which guarantees only that the answer parses. `mode: 'none'`
293
- * returns null, which is what an agent CLI (Claude Code, Codex) and llama.cpp-era endpoints
294
- * get: no server-side constraint at all, the prompt and the repair pass carry it.
295
- *
296
- * Callers should DEGRADE, not branch: try 'schema', fall back to 'object', then to null —
297
- * because `coerce` produces the same answer from all three, the only thing that changes is
298
- * how often it has to work for it. That is exactly what providers.js already does by hand.
299
- */
300
- export function responseFormat(schema, { mode = 'schema', strict = true } = {}) {
301
- if (mode === 'none') return null;
302
- if (mode === 'object') return { response_format: { type: 'json_object' } };
303
- return {
304
- response_format: {
305
- type: 'json_schema',
306
- json_schema: {
307
- name: schema.name.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64) || 'answer',
308
- strict,
309
- schema: toJsonSchema(schema, { strict }),
310
- },
311
- },
312
- };
313
- }
314
-
315
- /** The degradation ladder, in order. A caller retries down it on a 4xx from the server. */
316
- export const RESPONSE_MODES = Object.freeze(['schema', 'object', 'none']);
317
-
318
- // ---------------------------------------------------------------------------
319
- // Finding the JSON
320
- // ---------------------------------------------------------------------------
321
-
322
- /** Strip code fences and the "Sure, here's the JSON:" preamble. Never touches the inside. */
323
- export function unfence(text) {
324
- let t = String(text ?? '').trim();
325
- // A fenced block anywhere in the answer wins over the prose around it.
326
- const fenced = t.match(/```(?:json|jsonc|json5)?\s*\n?([\s\S]*?)(?:```|$)/i);
327
- if (fenced && fenced[1].trim()) t = fenced[1].trim();
328
- const led = t.replace(LEAD_IN, '');
329
- // Only when a structure follows — otherwise "OK" as a whole answer becomes ''.
330
- if (led !== t && /^[[{"]/.test(led)) t = led;
331
- return t.trim();
332
- }
333
-
334
- /**
335
- * Locate the JSON value inside an answer, honestly.
336
- *
337
- * The pattern this replaces — `slice(indexOf('{'), lastIndexOf('}') + 1)` — is wrong in three
338
- * ways that all happen: a `}` inside a string ends the slice early, prose after the object
339
- * that mentions a brace extends it past the end, and a response still arriving has no closing
340
- * brace at all so the slice is empty. This scans with string and escape awareness and
341
- * reports what it found, including how deep it was when the text ran out.
342
- *
343
- * @returns { text, start, end, complete } | null
344
- */
345
- export function findJson(text) {
346
- const src = String(text ?? '');
347
- let best = null;
348
- for (let i = 0; i < src.length; i++) {
349
- const c = src[i];
350
- if (c !== '{' && c !== '[') continue;
351
- const scan = scanFrom(src, i);
352
- if (scan.complete) return { text: src.slice(i, scan.end), start: i, end: scan.end, complete: true };
353
- // Incomplete: remember the FIRST opener and keep looking for a complete one later in the
354
- // answer — a model that writes a broken example and then the real object is common enough
355
- // to survive, and mid-stream there is only ever the one.
356
- if (!best) best = { text: src.slice(i), start: i, end: src.length, complete: false };
357
- }
358
- return best;
359
- }
360
-
361
- /** Walk from an opener, tracking strings and escapes. Returns where it closed, or how deep it still is. */
362
- function scanFrom(src, start) {
363
- const stack = [];
364
- let inStr = false;
365
- let quote = '"';
366
- for (let i = start; i < src.length; i++) {
367
- const c = src[i];
368
- if (inStr) {
369
- if (c === '\\') { i++; continue; }
370
- if (c === quote) inStr = false;
371
- continue;
372
- }
373
- if (QUOTE_PAIRS.has(c)) { inStr = true; quote = QUOTE_PAIRS.get(c); continue; }
374
- if (c === '{' || c === '[') { stack.push(c); continue; }
375
- if (c === '}' || c === ']') {
376
- stack.pop();
377
- if (!stack.length) return { end: i + 1, complete: true, depth: 0, inStr: false };
378
- }
379
- }
380
- return { end: src.length, complete: false, depth: stack.length, inStr };
381
- }
382
-
383
- // ---------------------------------------------------------------------------
384
- // Repairing it
385
- // ---------------------------------------------------------------------------
386
-
387
- const LITERALS = new Map([
388
- ['true', 'true'], ['false', 'false'], ['null', 'null'],
389
- ['True', 'true'], ['False', 'false'], ['None', 'null'], // a model that has read Python
390
- ['TRUE', 'true'], ['FALSE', 'false'], ['NULL', 'null'],
391
- ['yes', 'true'], ['no', 'false'], ['Yes', 'true'], ['No', 'false'],
392
- ['undefined', 'null'], ['NaN', 'null'], ['Infinity', 'null'],
393
- ]);
394
-
395
- // A curly quote opens a string that closes with its PARTNER, never with itself — mapping the
396
- // opener to '"' before reading the string is how the closing '”' got missed and the rest of the
397
- // answer was swallowed into one giant value.
398
- const QUOTE_PAIRS = new Map([['"', '"'], ["'", "'"], ['“', '”'], ['”', '”'], ['‘', '’'], ['’', '’']]);
399
- const isQuote = (c) => QUOTE_PAIRS.has(c);
400
-
401
- /**
402
- * Rewrite almost-JSON into JSON.
403
- *
404
- * Everything here is something a model has actually emitted while being told to return JSON:
405
- * single quotes, unquoted keys, `//` comments, trailing commas, Python literals, curly quotes
406
- * from a model that has been trained on prose, a raw newline inside a string — and, on every
407
- * single response that is still arriving, an ending that simply is not there yet.
408
- *
409
- * `partial: true` closes what is open: an unterminated string gets its quote, a key with no
410
- * value yet is dropped rather than invented, and the container stack is closed in order. That
411
- * is what makes a half-received answer renderable instead of an error.
412
- *
413
- * Also reports which TOP-LEVEL keys finished on their own — the streaming contract. A field
414
- * is `settled` only if its value ended because the model ended it, never because we closed it.
415
- */
416
- export function rewriteJson(src, { partial = false } = {}) {
417
- const text = String(src ?? '');
418
- let out = '';
419
- const stack = []; // { kind: '{'|'[', expect: 'key'|'colon'|'value'|'comma' }
420
- const settled = new Set();
421
- let pendingComma = false;
422
- let currentKey = null; // the key whose value we are inside, at depth 1
423
- let keyMark = -1; // where in `out` the pending key started, for rollback
424
- let commaMark = -1; // where in `out` the comma before it started
425
- let truncated = false;
426
-
427
- const top = () => stack[stack.length - 1] || null;
428
- const flushComma = () => { if (pendingComma) { commaMark = out.length; out += ','; pendingComma = false; } };
429
- // Depth 1 means "a value of the root object", which is the only level the streaming
430
- // contract talks about. Nested settling is not reported — a caller that needs it wants a
431
- // different schema, not a deeper report.
432
- const settleValue = () => {
433
- const t = top();
434
- if (t && t.kind === '{') { if (stack.length === 1 && currentKey) settled.add(currentKey); t.expect = 'comma'; }
435
- else if (t) t.expect = 'comma';
436
- };
437
-
438
- for (let i = 0; i < text.length; i++) {
439
- const c = text[i];
440
-
441
- if (/\s/.test(c)) { if (out && !/\s$/.test(out)) out += ' '; continue; }
442
-
443
- // Comments — a model asked for JSON explains itself in it surprisingly often.
444
- if (c === '/' && (text[i + 1] === '/' || text[i + 1] === '*')) {
445
- if (text[i + 1] === '/') { while (i < text.length && text[i] !== '\n') i++; }
446
- else { const close = text.indexOf('*/', i + 2); i = close < 0 ? text.length : close + 1; }
447
- continue;
448
- }
449
-
450
- if (c === '{' || c === '[') {
451
- flushComma();
452
- stack.push({ kind: c, expect: c === '{' ? 'key' : 'value' });
453
- out += c;
454
- continue;
455
- }
456
-
457
- if (c === '}' || c === ']') {
458
- pendingComma = false; // a trailing comma before a closer, dropped
459
- const t = top();
460
- if (!t) continue; // a closer with nothing open: noise, skipped
461
- // A key with no value: `{"a":"x","b"}`. Roll the dangling key back out.
462
- if (t.kind === '{' && (t.expect === 'colon' || (t.expect === 'value' && out.endsWith(':')))) rollbackKey();
463
- stack.pop();
464
- out += t.kind === '{' ? '}' : ']'; // the closer the STACK says, not the one written
465
- settleValue();
466
- continue;
467
- }
468
-
469
- if (c === ':') { out += ':'; const t = top(); if (t) t.expect = 'value'; continue; }
470
-
471
- if (c === ',') {
472
- // Buffered rather than emitted, so the next thing gets to decide whether it was a
473
- // separator or a trailing comma. `,]` and `,}` are both routine.
474
- pendingComma = true;
475
- const t = top();
476
- if (t) t.expect = t.kind === '{' ? 'key' : 'value';
477
- continue;
478
- }
479
-
480
- if (isQuote(c)) {
481
- flushComma();
482
- const t = top();
483
- const isKey = !!t && t.kind === '{' && t.expect !== 'value';
484
- if (isKey) { keyMark = out.length; }
485
- const str = readString(text, i, c);
486
- i = str.end;
487
- if (!str.closed) {
488
- truncated = true;
489
- if (!partial) return { json: null, complete: false, settled, truncated: true };
490
- // Mid-stream: a half-written KEY names nothing, so it goes; a half-written VALUE is
491
- // the thing the user is watching appear, so it stays and gets its quote.
492
- if (isKey) { rollbackKey(); break; }
493
- out += `${JSON.stringify(str.value)}`;
494
- if (stack.length === 1 && currentKey) { /* deliberately NOT settled — we closed it */ }
495
- break;
496
- }
497
- out += JSON.stringify(str.value);
498
- if (isKey) { if (stack.length === 1) currentKey = str.value; if (t) t.expect = 'colon'; }
499
- else settleValue();
500
- continue;
501
- }
502
-
503
- // A bare word or number.
504
- const word = readBare(text, i);
505
- if (!word.value) continue; // a character that is not JSON at all
506
- i = word.end;
507
- flushComma();
508
- const t = top();
509
- const wantsKey = !!t && t.kind === '{' && t.expect !== 'value';
510
- if (wantsKey) {
511
- keyMark = out.length;
512
- out += JSON.stringify(word.value); // unquoted key
513
- if (stack.length === 1) currentKey = word.value;
514
- t.expect = 'colon';
515
- continue;
516
- }
517
- const lit = LITERALS.get(word.value);
518
- if (lit) out += lit;
519
- else if (/^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/.test(word.value)) out += word.value;
520
- else out += JSON.stringify(word.value); // an unquoted string value
521
- // A bare word at the very end may still be growing ("questi" → "question"), so mid-stream
522
- // it is never settled. Complete text settles it normally.
523
- if (!(partial && word.end >= text.length - 1)) settleValue();
524
- else truncated = true;
525
- }
526
-
527
- function rollbackKey() {
528
- if (keyMark < 0) return;
529
- out = out.slice(0, commaMark >= 0 && commaMark >= keyMark - 1 ? commaMark : keyMark);
530
- if (out.endsWith(',')) out = out.slice(0, -1);
531
- keyMark = -1;
532
- if (stack.length === 1) currentKey = null;
533
- }
534
-
535
- if (stack.length) {
536
- truncated = true;
537
- if (!partial) return { json: null, complete: false, settled, truncated: true };
538
- // `{"request"` — a key arrived and its colon has not. The key names nothing yet, so it
539
- // goes; keeping it would produce `{"request"}`, which is the one shape JSON.parse cannot
540
- // be talked into accepting, and every streamed answer passes through it.
541
- const t = top();
542
- if (t && t.kind === '{' && t.expect === 'colon') rollbackKey();
543
- // `{"request":` — the value has not been written. Null, never a guess: an invented value
544
- // is indistinguishable downstream from one the model actually chose.
545
- if (out.trimEnd().endsWith(':')) out += 'null';
546
- if (out.trimEnd().endsWith(',')) out = out.trimEnd().slice(0, -1);
547
- while (stack.length) out += stack.pop().kind === '{' ? '}' : ']';
548
- }
549
-
550
- return { json: out.trim(), complete: !truncated, settled, truncated };
551
- }
552
-
553
- /** Read one string literal, tolerating raw newlines and either quote style. */
554
- function readString(text, start, opener) {
555
- const quote = QUOTE_PAIRS.get(opener) || '"';
556
- let value = '';
557
- for (let i = start + 1; i < text.length; i++) {
558
- const c = text[i];
559
- if (c === '\\') {
560
- const n = text[i + 1];
561
- if (n === undefined) return { value, end: text.length, closed: false };
562
- // Keep real escapes, unescape a quote that only needed escaping in the other style.
563
- if (n === 'n') value += '\n';
564
- else if (n === 't') value += '\t';
565
- else if (n === 'r') value += '\r';
566
- else if (n === 'u' && /^[0-9a-fA-F]{4}$/.test(text.slice(i + 2, i + 6))) {
567
- value += String.fromCharCode(parseInt(text.slice(i + 2, i + 6), 16));
568
- i += 4;
569
- } else value += n;
570
- i++;
571
- continue;
572
- }
573
- if (c === quote) return { value, end: i, closed: true };
574
- value += c; // a raw newline inside a string, kept
575
- }
576
- return { value, end: text.length, closed: false };
577
- }
578
-
579
- /** Read one unquoted token — a key, a number, or a bare word a model forgot to quote. */
580
- function readBare(text, start) {
581
- let end = start;
582
- while (end < text.length && !/[\s,:{}[\]"']/.test(text[end])) end++;
583
- return { value: text.slice(start, end), end: end - 1 };
584
- }
585
-
586
- /** Almost-JSON in, JSON text out (or null when it cannot be made to parse). */
587
- export function repairJson(text, { partial = false } = {}) {
588
- const found = findJson(unfence(text));
589
- if (!found) return null;
590
- return rewriteJson(found.text, { partial }).json;
591
- }
592
-
593
- // ---------------------------------------------------------------------------
594
- // Coercing onto the schema
595
- // ---------------------------------------------------------------------------
596
-
597
- /**
598
- * Read a model's answer as the schema says it should be.
599
- *
600
- * @returns {{ value, complete, settled: string[], source: 'json'|'sentinel'|'fallback' }|null}
601
- * null means "nothing usable" — the caller falls back to its deterministic reading
602
- * rather than acting on a guess. That distinction is the whole point of the return
603
- * shape: an empty ANSWER and an unreadable one lead to different behaviour.
604
- */
605
- export function coerce(text, schema, { partial = false } = {}) {
606
- const raw = String(text ?? '').trim();
607
- if (!raw) return null;
608
- const body = unfence(raw);
609
-
610
- // "none", on its own, is an answer.
611
- if (NOTHING.test(body) || schema.nothingIf?.test(body)) return nothingResult(schema);
612
-
613
- const found = findJson(body);
614
- let parsed = null;
615
- let complete = false;
616
- let settled = new Set();
617
- if (found) {
618
- const rewritten = rewriteJson(found.text, { partial });
619
- complete = rewritten.complete && found.complete;
620
- settled = rewritten.settled;
621
- if (rewritten.json) { try { parsed = JSON.parse(rewritten.json); } catch { parsed = null; } }
622
- }
623
-
624
- if (parsed == null) {
625
- const fb = runFallback(body, schema);
626
- if (fb) {
627
- const v = coerceObject(fb, schema, { partial });
628
- if (v === NOTHING_MARK) return nothingResult(schema);
629
- if (v) return { value: v, complete: true, settled: schema.order.slice(), source: 'fallback' };
630
- }
631
- return null;
632
- }
633
-
634
- // A bare array against a schema with exactly one list field IS that field. Models do this
635
- // constantly — asked for {"topics":[…]} they return […] — and it is unambiguous, so it is
636
- // read rather than rejected.
637
- if (Array.isArray(parsed)) {
638
- const sole = soleListField(schema);
639
- if (!sole) return null;
640
- parsed = { [sole]: parsed };
641
- }
642
- if (!isObj(parsed)) return null;
643
-
644
- const value = coerceObject(parsed, schema, { partial });
645
- // Valid JSON whose required field is the word "none" is the model saying nothing, in the
646
- // shape it was asked to say it in. Same answer as the prose form, and not a failure.
647
- if (value === NOTHING_MARK) return nothingResult(schema);
648
- if (value == null) return null;
649
- return { value, complete, settled: [...settled].filter((k) => schema.order.includes(k)), source: 'json' };
650
- }
651
-
652
- /** Distinguishable from both null (unreadable) and an object (an answer). Never escapes. */
653
- const NOTHING_MARK = Symbol('nothing');
654
-
655
- function nothingResult(schema) {
656
- if (schema.nothing == null) return null;
657
- return { value: schema.nothing, complete: true, settled: schema.order.slice(), source: 'nothing' };
658
- }
659
-
660
- /** The common case: the object, or null. */
661
- export function parseStructured(text, schema, opts) {
662
- return coerce(text, schema, opts)?.value ?? null;
663
- }
664
-
665
- function runFallback(body, schema) {
666
- if (typeof schema.fallback === 'function') {
667
- try { const v = schema.fallback(body); return isObj(v) ? v : null; } catch { return null; }
668
- }
669
- if (schema.fallback !== 'lines') return null;
670
- const sole = soleListField(schema);
671
- if (!sole) return null;
672
- // The markdown list a model writes when it ignores "return JSON". Bullets if there are any,
673
- // otherwise every line — a bare list of lines is the other half of the same mistake.
674
- const lines = body.split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
675
- const bullets = lines.filter((l) => /^(?:[-*+•]|\d+[.)])\s+/.test(l));
676
- // ONE line with no list marker is a SENTENCE, not a list of one. "I could not determine any
677
- // topics." read as an item is worse than reading nothing: it becomes a tag on the note.
678
- if (!bullets.length && lines.length < 2) return null;
679
- const items = (bullets.length ? bullets : lines)
680
- .map((l) => l.replace(/^(?:[-*+•]|\d+[.)])\s*/, '').replace(/^["'`]|["'`]$/g, '').trim())
681
- .filter(Boolean);
682
- return items.length ? { [sole]: items } : null;
683
- }
684
-
685
- function coerceObject(obj, schema, { partial }) {
686
- const out = {};
687
- let missingRequired = false;
688
- let emptiedRequired = false;
689
- let any = false;
690
- for (const key of schema.order) {
691
- const f = schema.fields[key];
692
- const present = pick(obj, key);
693
- const v = coerceValue(present, f);
694
- if (v === undefined) {
695
- if (f.required) missingRequired = true;
696
- if (f.default !== undefined) out[key] = f.default;
697
- else if (!partial) out[key] = emptyFor(f);
698
- continue;
699
- }
700
- any = true;
701
- out[key] = v;
702
- // A required field ANSWERED as empty is different from one never answered: the model
703
- // filled the key it was told it had to fill, with nothing. That is a "nothing" answer,
704
- // not a malformed one — and the two need different handling by the caller.
705
- if (f.required && isEmptyValue(v)) emptiedRequired = true;
706
- }
707
- if (!any) return null;
708
- // Mid-stream a required field is simply not written yet, which is not the same as a model
709
- // that finished and left it out.
710
- if (partial) return out;
711
- if (emptiedRequired) return NOTHING_MARK;
712
- if (missingRequired && schema.strictRequired) return null;
713
- return out;
714
- }
715
-
716
- const isEmptyValue = (v) => v === '' || v === null || (Array.isArray(v) && v.length === 0);
717
-
718
- /**
719
- * Find a key however the model spelled it. `actionItems`, `action_items`, `Action Items` and
720
- * `action-items` are one key, and losing a field to casing is a silent, total failure of the
721
- * call — the model answered correctly and we threw it away.
722
- */
723
- function pick(obj, key) {
724
- if (key in obj) return obj[key];
725
- const want = key.toLowerCase().replace(/[^a-z0-9]/g, '');
726
- for (const k of Object.keys(obj)) {
727
- if (k.toLowerCase().replace(/[^a-z0-9]/g, '') === want) return obj[k];
728
- }
729
- return undefined;
730
- }
731
-
732
- function emptyFor(f) {
733
- if (isListType(f.type)) return [];
734
- if (f.type === 'boolean') return false;
735
- if (f.type === 'number' || f.type === 'integer') return null;
736
- if (f.type === 'enum') return f.default ?? f.values[0];
737
- return '';
738
- }
739
-
740
- function coerceValue(v, f) {
741
- if (v === undefined || v === null) return undefined;
742
- switch (f.type) {
743
- case 'string': {
744
- const s = coerceString(v, f);
745
- return s === undefined ? undefined : s;
746
- }
747
- case 'enum': {
748
- const s = String(typeof v === 'string' ? v : (v?.value ?? v)).trim().toLowerCase().replace(/[.!]+$/, '');
749
- const hit = f.aliasMap.get(s) ?? f.aliasMap.get(s.replace(/^(?:a|an|the)\s+/, ''));
750
- if (hit) return hit;
751
- // An unrecognised enum is NOT the default by accident — the default is a deliberate
752
- // "when in doubt, do the least surprising thing", and a schema that has not declared one
753
- // would rather the caller knew the answer was unusable.
754
- return f.default !== undefined ? f.default : undefined;
755
- }
756
- case 'number':
757
- case 'integer': {
758
- const n = typeof v === 'number' ? v : Number(String(v).replace(/[^0-9.eE+-]/g, ''));
759
- if (!Number.isFinite(n)) return undefined;
760
- const r = f.type === 'integer' ? Math.round(n) : n;
761
- if (f.min != null && r < f.min) return f.min;
762
- if (f.max != null && r > f.max) return f.max;
763
- return r;
764
- }
765
- case 'boolean': {
766
- if (typeof v === 'boolean') return v;
767
- const s = String(v).trim().toLowerCase();
768
- if (/^(?:true|yes|y|1)$/.test(s)) return true;
769
- if (/^(?:false|no|n|0)$/.test(s)) return false;
770
- return undefined;
771
- }
772
- case 'string[]':
773
- case 'number[]':
774
- case 'object[]': {
775
- // A single value where a list was asked for is a list of one — a routine answer when
776
- // there happens to be only one thing to report, and rejecting it loses that one thing.
777
- const arr = Array.isArray(v) ? v : (v === '' ? [] : [v]);
778
- const item = f.type === 'string[]'
779
- ? (x) => coerceString(x, { max: f.itemMax, emptyIf: f.itemEmptyIf })
780
- : f.type === 'number[]'
781
- ? (x) => coerceValue(x, { type: 'number', min: f.min, max: f.max })
782
- : (x) => (isObj(x) ? coerceObject(x, { order: f.order, fields: f.fields, strictRequired: true }, { partial: false }) : undefined);
783
- const out = [];
784
- const seen = new Set();
785
- for (const x of arr) {
786
- const c = item(x);
787
- // NOTHING_MARK here means the item's own required field came back empty — an entity
788
- // with no value, a topic with no text. There is nothing to keep, so it is dropped
789
- // rather than turning the whole list into a "nothing" answer.
790
- if (c === undefined || c === null || c === '' || c === NOTHING_MARK) continue;
791
- if (f.dedupe !== false) {
792
- const k = typeof c === 'object' ? JSON.stringify(c) : String(c).toLowerCase();
793
- if (seen.has(k)) continue;
794
- seen.add(k);
795
- }
796
- out.push(c);
797
- if (f.maxItems && out.length >= f.maxItems) break;
798
- }
799
- return out;
800
- }
801
- default: return undefined;
802
- }
803
- }
804
-
805
- function coerceString(v, f = {}) {
806
- if (v === undefined || v === null) return undefined;
807
- // An object where a string was asked for usually carries it under an obvious key.
808
- const raw = typeof v === 'string' ? v : (isObj(v) ? String(v.value ?? v.text ?? v.name ?? '') : String(v));
809
- let s = raw.replace(/\s+/g, ' ').trim();
810
- if (f.emptyIf && f.emptyIf.test(s)) return '';
811
- // "none" written INTO a field, which is how a model says "not this one" when it has been
812
- // told it must fill every key. Anchored, so a real answer that contains the word survives.
813
- if (NOTHING.test(s)) return '';
814
- s = s.replace(/^["'`]+|["'`]+$/g, '').trim();
815
- if (f.max && s.length > f.max) {
816
- // Clip on a word boundary — a label cut mid-word reads as a bug, not as a limit.
817
- const cut = s.slice(0, f.max);
818
- s = /\s/.test(cut) ? cut.replace(/\s+\S*$/, '') : cut;
819
- }
820
- return s;
821
- }
822
-
823
- /** True when nothing was said — the prose form, exported because callers check it too. */
824
- export function isNothing(text) { return NOTHING.test(String(text ?? '').trim()); }
825
-
826
- // ---------------------------------------------------------------------------
827
- // Streaming
828
- // ---------------------------------------------------------------------------
829
-
830
- // Re-reading the whole buffer on every token is O(n²). These answers are a few hundred
831
- // characters, so it does not matter in practice — but a schema with a long string field
832
- // streamed token by token is exactly the case where it would start to, so a re-read is
833
- // earned rather than automatic: it happens when the shape may have changed (a structural
834
- // character arrived) or when enough new text has accumulated to be worth showing.
835
- const STRUCTURAL = /[{}[\]",:]/;
836
- const GROWTH_BEFORE_REPARSE = 12;
837
-
838
- /**
839
- * Read a structured answer AS IT ARRIVES.
840
- *
841
- * The reason this exists rather than "wait for the end and parse once": these calls are made
842
- * while a person is waiting — mid-meeting, mid-turn — and the standing UX rule is that every
843
- * model output streams with visible progress. A structured answer had no way to do that, so
844
- * structured calls were the one place in the product that showed a spinner.
845
- *
846
- * const s = createStructuredStream(REFINEMENT);
847
- * await dispatchStream({ …, onDelta: (d) => { const { value, settled } = s.push(d); render(value, settled); } });
848
- * const final = s.end();
849
- *
850
- * `settled` is the contract that makes this safe to render: a field in it is FINISHED — the
851
- * model closed it — so committing to it (starting the monitor, choosing the branch) is sound.
852
- * A field not in it is still being written and must only ever be displayed.
853
- */
854
- export function createStructuredStream(schema, { onChange = null } = {}) {
855
- let buffer = '';
856
- let sinceParse = 0;
857
- let last = null; // the most recent successful read
858
- let lastJson = ''; // for change detection, so onChange is not called per token
859
-
860
- const read = (partial) => {
861
- const got = coerce(buffer, schema, { partial });
862
- if (got) {
863
- last = got;
864
- const json = JSON.stringify(got.value);
865
- if (json !== lastJson) { lastJson = json; onChange?.(got.value, got.settled); }
866
- }
867
- sinceParse = 0;
868
- return last;
869
- };
870
-
871
- return {
872
- /** Feed a delta. Returns the answer so far — never throws, never blocks. */
873
- push(chunk) {
874
- const s = String(chunk ?? '');
875
- if (s) { buffer += s; sinceParse += s.length; }
876
- if (s && (STRUCTURAL.test(s) || sinceParse >= GROWTH_BEFORE_REPARSE)) read(true);
877
- return this.snapshot();
878
- },
879
- /** No more deltas. Re-reads once strictly, so a truncation that never resolved is caught. */
880
- end() {
881
- read(false);
882
- // Nothing parsed strictly, but something parsed while it was arriving: the answer was
883
- // cut off. Better a partial object than none — the caller decides with `complete`.
884
- if (!last) read(true);
885
- return this.snapshot();
886
- },
887
- /** What we know right now. */
888
- snapshot() {
889
- return {
890
- value: last?.value ?? null,
891
- settled: last?.settled ?? [],
892
- complete: !!last?.complete,
893
- source: last?.source ?? null,
894
- text: buffer,
895
- };
896
- },
897
- /** Throw away the buffer and start again — one stream object per call site, reused. */
898
- reset() { buffer = ''; sinceParse = 0; last = null; lastJson = ''; },
899
- get text() { return buffer; },
900
- };
901
- }
1
+ class m extends Error{constructor(t,r){super(r),this.name="StructuredError",this.code=t}}const P=Object.freeze(["string","number","integer","boolean","enum","string[]","number[]","object[]"]),E=/^(?:none|n\/a|na|nothing|no|null|nil|empty|no results?|nothing found|no request|-{1,3}|\.)\s*[.!]?$/i,K=/^(?:(?:sure|certainly|of course|okay|ok|got it|here(?:'s| is)(?: the)?(?: \w+)?|the (?:json|answer|result|output)(?: is)?|json|output|answer|result)\s*[:.!,-]*\s*)+/i,b=e=>!!e&&typeof e=="object"&&!Array.isArray(e),ne=(e,t)=>e.length>t?`${e.slice(0,t)}`:e;function re(e={}){const t=String(e.name||"").trim();if(!t)throw new m("no-name","a schema needs a name");if(!b(e.fields)||!Object.keys(e.fields).length)throw new m("no-fields",`schema ${t} declares no fields`);const r={};for(const[n,i]of Object.entries(e.fields))r[n]=L(t,n,i);if(e.nothingIf!=null&&!(e.nothingIf instanceof RegExp))throw new m("bad-nothing",`schema ${t}: nothingIf must be a RegExp`);if(e.fallback!=null&&e.fallback!=="lines"&&typeof e.fallback!="function")throw new m("bad-fallback",`schema ${t}: fallback must be 'lines' or a function`);return Object.freeze({name:t,purpose:String(e.purpose||"").trim(),fields:Object.freeze(r),order:Object.freeze(Object.keys(r)),nothing:e.nothing??null,nothingIf:e.nothingIf??null,fallback:e.fallback??null,strictRequired:e.strictRequired!==!1})}function L(e,t,r){const n=typeof r=="string"?{type:r}:{...r||{}},i=String(n.type||"string");if(!P.includes(i))throw new m("bad-type",`schema ${e}.${t}: unknown type ${JSON.stringify(i)}`);if(i==="enum"){const l=(n.values||[]).map(o=>String(o));if(!l.length)throw new m("bad-enum",`schema ${e}.${t}: enum with no values`);if(n.default!=null&&!l.includes(String(n.default)))throw new m("bad-default",`schema ${e}.${t}: default ${JSON.stringify(n.default)} is not one of its values`);n.values=Object.freeze(l);const s=new Map;for(const o of l)s.set(o.toLowerCase(),o);for(const[o,u]of Object.entries(n.aliases||{})){if(!l.includes(String(u)))throw new m("bad-alias",`schema ${e}.${t}: alias → ${u}, which is not a value`);s.set(String(o).toLowerCase(),String(u))}n.aliasMap=s}if(i==="object[]"){if(!b(n.fields))throw new m("bad-items",`schema ${e}.${t}: object[] needs \`fields\``);const l={};for(const[s,o]of Object.entries(n.fields))l[s]=L(`${e}.${t}[]`,s,o);n.fields=Object.freeze(l),n.order=Object.freeze(Object.keys(l))}if(n.emptyIf!=null&&!(n.emptyIf instanceof RegExp))throw new m("bad-emptyif",`schema ${e}.${t}: emptyIf must be a RegExp`);return n.type=i,n.describe=String(n.describe||"").trim(),Object.freeze(n)}const I=e=>e==="string[]"||e==="number[]"||e==="object[]";function q(e){const t=e.order.filter(r=>I(e.fields[r].type));return t.length===1?t[0]:null}function ie(e,{fences:t=!1}={}){const r=e.order.map(i=>`${JSON.stringify(i)}: ${A(e.fields[i])}`).join(", "),n=[];for(const i of e.order){const l=e.fields[i],s=[];l.describe&&s.push(l.describe),l.required&&s.push("required"),l.default!=null&&!l.required&&s.push(`defaults to ${JSON.stringify(l.default)}`),l.type==="string"&&l.max&&s.push(`at most ${l.max} characters`),I(l.type)&&l.maxItems&&s.push(`at most ${l.maxItems} items`),s.length&&n.push(`- ${i} — ${s.join("; ")}.`)}return[e.purpose?`${e.purpose}`:"",(e.purpose,""),t?"Return a single JSON object in a ```json code fence and nothing else:":"Return ONLY a JSON object — no prose, no code fences, no explanation:",`{${r}}`,(n.length,""),...n].filter(i=>i!=="").join(`
2
+ `)}function A(e){switch(e.type){case"enum":return e.values.map(t=>JSON.stringify(t)).join("|");case"string[]":return"[string, …]";case"number[]":return"[number, …]";case"object[]":return`[{${e.order.map(t=>`${JSON.stringify(t)}: ${A(e.fields[t])}`).join(", ")}}, …]`;case"integer":return"integer";case"number":return"number";case"boolean":return"true|false";default:return"string"}}function U(e,{strict:t=!0}={}){const r={};for(const i of e.order)r[i]=_(e.fields[i]);const n=t?e.order.slice():e.order.filter(i=>e.fields[i].required);return{type:"object",properties:r,required:n,additionalProperties:!1}}function _(e){const t=e.describe?{description:e.describe}:{};switch(e.type){case"enum":return{...t,type:"string",enum:e.values.slice()};case"integer":return{...t,type:"integer"};case"number":return{...t,type:"number"};case"boolean":return{...t,type:"boolean"};case"string[]":return{...t,type:"array",items:{type:"string"}};case"number[]":return{...t,type:"array",items:{type:"number"}};case"object[]":return{...t,type:"array",items:{type:"object",properties:Object.fromEntries(e.order.map(r=>[r,_(e.fields[r])])),required:e.order.slice(),additionalProperties:!1}};default:return{...t,type:"string"}}}function se(e,{mode:t="schema",strict:r=!0}={}){return t==="none"?null:t==="object"?{response_format:{type:"json_object"}}:{response_format:{type:"json_schema",json_schema:{name:e.name.replace(/[^a-zA-Z0-9_-]/g,"_").slice(0,64)||"answer",strict:r,schema:U(e,{strict:r})}}}}const le=Object.freeze(["schema","object","none"]);function v(e){let t=String(e??"").trim();const r=t.match(/```(?:json|jsonc|json5)?\s*\n?([\s\S]*?)(?:```|$)/i);r&&r[1].trim()&&(t=r[1].trim());const n=t.replace(K,"");return n!==t&&/^[[{"]/.test(n)&&(t=n),t.trim()}function F(e){const t=String(e??"");let r=null;for(let n=0;n<t.length;n++){const i=t[n];if(i!=="{"&&i!=="[")continue;const l=W(t,n);if(l.complete)return{text:t.slice(n,l.end),start:n,end:l.end,complete:!0};r||(r={text:t.slice(n),start:n,end:t.length,complete:!1})}return r}function W(e,t){const r=[];let n=!1,i='"';for(let l=t;l<e.length;l++){const s=e[l];if(n){if(s==="\\"){l++;continue}s===i&&(n=!1);continue}if(j.has(s)){n=!0,i=j.get(s);continue}if(s==="{"||s==="["){r.push(s);continue}if((s==="}"||s==="]")&&(r.pop(),!r.length))return{end:l+1,complete:!0,depth:0,inStr:!1}}return{end:e.length,complete:!1,depth:r.length,inStr:n}}const B=new Map([["true","true"],["false","false"],["null","null"],["True","true"],["False","false"],["None","null"],["TRUE","true"],["FALSE","false"],["NULL","null"],["yes","true"],["no","false"],["Yes","true"],["No","false"],["undefined","null"],["NaN","null"],["Infinity","null"]]),j=new Map([['"','"'],["'","'"],["“","”"],["”","”"],["‘","’"],["’","’"]]),D=e=>j.has(e);function z(e,{partial:t=!1}={}){const r=String(e??"");let n="";const i=[],l=new Set;let s=!1,o=null,u=-1,d=-1,f=!1;const g=()=>i[i.length-1]||null,x=()=>{s&&(d=n.length,n+=",",s=!1)},$=()=>{const c=g();c&&c.kind==="{"?(i.length===1&&o&&l.add(o),c.expect="comma"):c&&(c.expect="comma")};for(let c=0;c<r.length;c++){const p=r[c];if(/\s/.test(p)){n&&!/\s$/.test(n)&&(n+=" ");continue}if(p==="/"&&(r[c+1]==="/"||r[c+1]==="*")){if(r[c+1]==="/")for(;c<r.length&&r[c]!==`
3
+ `;)c++;else{const a=r.indexOf("*/",c+2);c=a<0?r.length:a+1}continue}if(p==="{"||p==="["){x(),i.push({kind:p,expect:p==="{"?"key":"value"}),n+=p;continue}if(p==="}"||p==="]"){s=!1;const a=g();if(!a)continue;a.kind==="{"&&(a.expect==="colon"||a.expect==="value"&&n.endsWith(":"))&&O(),i.pop(),n+=a.kind==="{"?"}":"]",$();continue}if(p===":"){n+=":";const a=g();a&&(a.expect="value");continue}if(p===","){s=!0;const a=g();a&&(a.expect=a.kind==="{"?"key":"value");continue}if(D(p)){x();const a=g(),k=!!a&&a.kind==="{"&&a.expect!=="value";k&&(u=n.length);const y=G(r,c,p);if(c=y.end,!y.closed){if(f=!0,!t)return{json:null,complete:!1,settled:l,truncated:!0};if(k){O();break}n+=`${JSON.stringify(y.value)}`,i.length;break}n+=JSON.stringify(y.value),k?(i.length===1&&(o=y.value),a&&(a.expect="colon")):$();continue}const h=H(r,c);if(!h.value)continue;c=h.end,x();const S=g();if(!!S&&S.kind==="{"&&S.expect!=="value"){u=n.length,n+=JSON.stringify(h.value),i.length===1&&(o=h.value),S.expect="colon";continue}const J=B.get(h.value);J?n+=J:/^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/.test(h.value)?n+=h.value:n+=JSON.stringify(h.value),t&&h.end>=r.length-1?f=!0:$()}function O(){u<0||(n=n.slice(0,d>=0&&d>=u-1?d:u),n.endsWith(",")&&(n=n.slice(0,-1)),u=-1,i.length===1&&(o=null))}if(i.length){if(f=!0,!t)return{json:null,complete:!1,settled:l,truncated:!0};const c=g();for(c&&c.kind==="{"&&c.expect==="colon"&&O(),n.trimEnd().endsWith(":")&&(n+="null"),n.trimEnd().endsWith(",")&&(n=n.trimEnd().slice(0,-1));i.length;)n+=i.pop().kind==="{"?"}":"]"}return{json:n.trim(),complete:!f,settled:l,truncated:f}}function G(e,t,r){const n=j.get(r)||'"';let i="";for(let l=t+1;l<e.length;l++){const s=e[l];if(s==="\\"){const o=e[l+1];if(o===void 0)return{value:i,end:e.length,closed:!1};o==="n"?i+=`
4
+ `:o==="t"?i+=" ":o==="r"?i+="\r":o==="u"&&/^[0-9a-fA-F]{4}$/.test(e.slice(l+2,l+6))?(i+=String.fromCharCode(parseInt(e.slice(l+2,l+6),16)),l+=4):i+=o,l++;continue}if(s===n)return{value:i,end:l,closed:!0};i+=s}return{value:i,end:e.length,closed:!1}}function H(e,t){let r=t;for(;r<e.length&&!/[\s,:{}[\]"']/.test(e[r]);)r++;return{value:e.slice(t,r),end:r-1}}function oe(e,{partial:t=!1}={}){const r=F(v(e));return r?z(r.text,{partial:t}).json:null}function C(e,t,{partial:r=!1}={}){const n=String(e??"").trim();if(!n)return null;const i=v(n);if(E.test(i)||t.nothingIf?.test(i))return N(t);const l=F(i);let s=null,o=!1,u=new Set;if(l){const f=z(l.text,{partial:r});if(o=f.complete&&l.complete,u=f.settled,f.json)try{s=JSON.parse(f.json)}catch{s=null}}if(s==null){const f=V(i,t);if(f){const g=R(f,t,{partial:r});if(g===w)return N(t);if(g)return{value:g,complete:!0,settled:t.order.slice(),source:"fallback"}}return null}if(Array.isArray(s)){const f=q(t);if(!f)return null;s={[f]:s}}if(!b(s))return null;const d=R(s,t,{partial:r});return d===w?N(t):d==null?null:{value:d,complete:o,settled:[...u].filter(f=>t.order.includes(f)),source:"json"}}const w=Symbol("nothing");function N(e){return e.nothing==null?null:{value:e.nothing,complete:!0,settled:e.order.slice(),source:"nothing"}}function ue(e,t,r){return C(e,t,r)?.value??null}function V(e,t){if(typeof t.fallback=="function")try{const s=t.fallback(e);return b(s)?s:null}catch{return null}if(t.fallback!=="lines")return null;const r=q(t);if(!r)return null;const n=e.split(/\r?\n/).map(s=>s.trim()).filter(Boolean),i=n.filter(s=>/^(?:[-*+•]|\d+[.)])\s+/.test(s));if(!i.length&&n.length<2)return null;const l=(i.length?i:n).map(s=>s.replace(/^(?:[-*+•]|\d+[.)])\s*/,"").replace(/^["'`]|["'`]$/g,"").trim()).filter(Boolean);return l.length?{[r]:l}:null}function R(e,t,{partial:r}){const n={};let i=!1,l=!1,s=!1;for(const o of t.order){const u=t.fields[o],d=Q(e,o),f=M(d,u);if(f===void 0){u.required&&(i=!0),u.default!==void 0?n[o]=u.default:r||(n[o]=Z(u));continue}s=!0,n[o]=f,u.required&&Y(f)&&(l=!0)}return s?r?n:l?w:i&&t.strictRequired?null:n:null}const Y=e=>e===""||e===null||Array.isArray(e)&&e.length===0;function Q(e,t){if(t in e)return e[t];const r=t.toLowerCase().replace(/[^a-z0-9]/g,"");for(const n of Object.keys(e))if(n.toLowerCase().replace(/[^a-z0-9]/g,"")===r)return e[n]}function Z(e){return I(e.type)?[]:e.type==="boolean"?!1:e.type==="number"||e.type==="integer"?null:e.type==="enum"?e.default??e.values[0]:""}function M(e,t){if(e!=null)switch(t.type){case"string":{const r=T(e,t);return r===void 0?void 0:r}case"enum":{const r=String(typeof e=="string"?e:e?.value??e).trim().toLowerCase().replace(/[.!]+$/,""),n=t.aliasMap.get(r)??t.aliasMap.get(r.replace(/^(?:a|an|the)\s+/,""));return n||(t.default!==void 0?t.default:void 0)}case"number":case"integer":{const r=typeof e=="number"?e:Number(String(e).replace(/[^0-9.eE+-]/g,""));if(!Number.isFinite(r))return;const n=t.type==="integer"?Math.round(r):r;return t.min!=null&&n<t.min?t.min:t.max!=null&&n>t.max?t.max:n}case"boolean":{if(typeof e=="boolean")return e;const r=String(e).trim().toLowerCase();return/^(?:true|yes|y|1)$/.test(r)?!0:/^(?:false|no|n|0)$/.test(r)?!1:void 0}case"string[]":case"number[]":case"object[]":{const r=Array.isArray(e)?e:e===""?[]:[e],n=t.type==="string[]"?s=>T(s,{max:t.itemMax,emptyIf:t.itemEmptyIf}):t.type==="number[]"?s=>M(s,{type:"number",min:t.min,max:t.max}):s=>b(s)?R(s,{order:t.order,fields:t.fields,strictRequired:!0},{partial:!1}):void 0,i=[],l=new Set;for(const s of r){const o=n(s);if(!(o==null||o===""||o===w)){if(t.dedupe!==!1){const u=typeof o=="object"?JSON.stringify(o):String(o).toLowerCase();if(l.has(u))continue;l.add(u)}if(i.push(o),t.maxItems&&i.length>=t.maxItems)break}}return i}default:return}}function T(e,t={}){if(e==null)return;let n=(typeof e=="string"?e:b(e)?String(e.value??e.text??e.name??""):String(e)).replace(/\s+/g," ").trim();if(t.emptyIf&&t.emptyIf.test(n)||E.test(n))return"";if(n=n.replace(/^["'`]+|["'`]+$/g,"").trim(),t.max&&n.length>t.max){const i=n.slice(0,t.max);n=/\s/.test(i)?i.replace(/\s+\S*$/,""):i}return n}function ce(e){return E.test(String(e??"").trim())}const X=/[{}[\]",:]/,ee=12;function fe(e,{onChange:t=null}={}){let r="",n=0,i=null,l="";const s=o=>{const u=C(r,e,{partial:o});if(u){i=u;const d=JSON.stringify(u.value);d!==l&&(l=d,t?.(u.value,u.settled))}return n=0,i};return{push(o){const u=String(o??"");return u&&(r+=u,n+=u.length),u&&(X.test(u)||n>=ee)&&s(!0),this.snapshot()},end(){return s(!1),i||s(!0),this.snapshot()},snapshot(){return{value:i?.value??null,settled:i?.settled??[],complete:!!i?.complete,source:i?.source??null,text:r}},reset(){r="",n=0,i=null,l=""},get text(){return r}}}export{P as FIELD_TYPES,le as RESPONSE_MODES,m as StructuredError,C as coerce,fe as createStructuredStream,re as defineSchema,ie as describeSchema,F as findJson,ce as isNothing,ue as parseStructured,oe as repairJson,se as responseFormat,z as rewriteJson,U as toJsonSchema,v as unfence};