agentfootprint 9.114.0 → 9.114.2

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 (33) hide show
  1. package/CAPABILITIES.md +82 -0
  2. package/ai-instructions/setup.sh +4 -4
  3. package/dist/core/agent/findings/answerText.js +847 -0
  4. package/dist/core/agent/findings/answerText.js.map +1 -0
  5. package/dist/core/agent/findings/reserved.js +55 -16
  6. package/dist/core/agent/findings/reserved.js.map +1 -1
  7. package/dist/core/agent/stages/callLLM.js +33 -6
  8. package/dist/core/agent/stages/callLLM.js.map +1 -1
  9. package/dist/core/agent/stages/route.js +95 -55
  10. package/dist/core/agent/stages/route.js.map +1 -1
  11. package/dist/esm/core/agent/findings/answerText.d.ts +78 -0
  12. package/dist/esm/core/agent/findings/answerText.js +842 -0
  13. package/dist/esm/core/agent/findings/answerText.js.map +1 -0
  14. package/dist/esm/core/agent/findings/reserved.d.ts +18 -5
  15. package/dist/esm/core/agent/findings/reserved.js +55 -16
  16. package/dist/esm/core/agent/findings/reserved.js.map +1 -1
  17. package/dist/esm/core/agent/stages/callLLM.d.ts +7 -4
  18. package/dist/esm/core/agent/stages/callLLM.js +33 -6
  19. package/dist/esm/core/agent/stages/callLLM.js.map +1 -1
  20. package/dist/esm/core/agent/stages/route.d.ts +6 -4
  21. package/dist/esm/core/agent/stages/route.js +95 -55
  22. package/dist/esm/core/agent/stages/route.js.map +1 -1
  23. package/dist/types/core/agent/findings/answerText.d.ts +79 -0
  24. package/dist/types/core/agent/findings/answerText.d.ts.map +1 -0
  25. package/dist/types/core/agent/findings/reserved.d.ts +18 -5
  26. package/dist/types/core/agent/findings/reserved.d.ts.map +1 -1
  27. package/dist/types/core/agent/stages/callLLM.d.ts +7 -4
  28. package/dist/types/core/agent/stages/callLLM.d.ts.map +1 -1
  29. package/dist/types/core/agent/stages/route.d.ts +6 -4
  30. package/dist/types/core/agent/stages/route.d.ts.map +1 -1
  31. package/package.json +10 -3
  32. package/CHANGELOG.md +0 -24738
  33. package/CLAUDE.md +0 -217
@@ -0,0 +1,842 @@
1
+ /**
2
+ * findings/answerText — the ONE reader of the reserved key in an answer's
3
+ * TEXT (9.114.2).
4
+ *
5
+ * Pattern: an incremental scanner — a character state machine with no scope
6
+ * and no I/O, fed chunk by chunk (`reservedMemberFilter`) or whole
7
+ * (`withoutReservedMembers`), with the same bytes out either way.
8
+ * Role: core/ layer leaf. `stages/callLLM.ts` feeds it the provider's
9
+ * stream under the arm, so `agentfootprint.stream.token` never
10
+ * carries the model's notes; `reserved.ts · peelAnswerFindings` feeds
11
+ * it the whole answer, so the answer the run returns is exactly the
12
+ * text the stream showed.
13
+ *
14
+ * THE RULE
15
+ * Every JSON object written in the text — the whole answer, one in a code
16
+ * block, one in the prose, one in a list — loses its own `_findings` member:
17
+ * the key, the value and the separator that joined it to its neighbours,
18
+ * and nothing else. A `_findings` inside another object's VALUE is data and
19
+ * stays. Each removed value is handed back (`removed`), as written, for the
20
+ * peel to read as the answer's declaration.
21
+ *
22
+ * - An object left with nothing in it goes whole — its line too — when it
23
+ * stood on a line of its own outside any list: the whole answer, a
24
+ * paragraph of its own, a code block of its own. A code block left empty
25
+ * goes with its fences. The removed line's indentation and the whole
26
+ * blank lines after it go; the next line keeps its own indentation. No
27
+ * blank lines are left at either end of the text. The whole answer, left with nothing, is `{}` (a JSON answer stays
28
+ * JSON). Anywhere else an emptied object stays as `{}`, so the JSON or
29
+ * code around it keeps its shape. A string inside a list is read as a
30
+ * string, so a `]` or `{` in it neither ends the list nor opens an object.
31
+ * - An answer that held nothing but notes — a code block of them, or
32
+ * several objects of them — is left empty; only an answer that was one
33
+ * bare JSON object becomes `{}`.
34
+ * - Text that stops being JSON part way (`{see below}`, a stray quote or a
35
+ * raw line break inside a `_findings` value) is given back exactly as it
36
+ * was written; nothing is removed from it except a `_findings` member
37
+ * that was already complete. An object that held nothing but such
38
+ * complete notes when the text stopped being JSON (a model one closing
39
+ * brace short) ends there, and goes as an emptied object does. Keys and
40
+ * values are read by JSON's own grammar (`JsonValue`), so the first
41
+ * character JSON cannot take decides it — not a count of brackets, which
42
+ * prose may never balance.
43
+ * - Text that ends inside a `_findings` value that is JSON so far (a
44
+ * cut-off stream) keeps it hidden and reports it `complete: false`.
45
+ * - Nothing removed ⇒ the text comes back byte for byte, whatever the
46
+ * chunking. What is held back while undecided is bounded by a line:
47
+ * whitespace, a key (a raw line break ends one — JSON allows none), `{`
48
+ * until its first key is read, a separator, a code fence line, the rest
49
+ * of an emptied object's line. The one exception is a `_findings` value:
50
+ * it is never shown while it is JSON so far, and one that stops being
51
+ * JSON is held until the character that breaks it and then given back as
52
+ * written — so that hold can span as many lines as the notes do.
53
+ */
54
+ import { RESERVED_ANSWER_KEY } from './types.js';
55
+ /** A fresh scanner for one text (one provider call's content). */
56
+ export function reservedMemberFilter(key = RESERVED_ANSWER_KEY) {
57
+ return new Scanner(key);
58
+ }
59
+ /** The whole-text form: the text without its reserved members, and what was removed. */
60
+ export function withoutReservedMembers(text, key = RESERVED_ANSWER_KEY) {
61
+ const scanner = new Scanner(key);
62
+ const out = scanner.push(text) + scanner.end();
63
+ return { text: out, removed: scanner.removed };
64
+ }
65
+ // ─── The machine ───────────────────────────────────────────────────────
66
+ const WS = new Set([' ', '\t', '\n', '\r']);
67
+ /** A JSON number or literal — what a value that is not a string, object or array must be. */
68
+ const SCALAR = /^(?:-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null)$/;
69
+ /** The start of one: a scalar that stops matching this cannot become JSON. */
70
+ const SCALAR_PREFIX = /^(?:-|-?(?:0|[1-9]\d*)(?:\.(?:\d+(?:[eE][+-]?\d*)?)?|[eE][+-]?\d*)?|t(?:r(?:ue?)?)?|f(?:a(?:l(?:se?)?)?)?|n(?:u(?:ll?)?)?)$/;
71
+ /** Characters that can continue a scalar; anything else ends it. */
72
+ const SCALAR_CHAR = /[0-9a-zA-Z.+-]/;
73
+ /** What may follow `\` in a JSON string (`u` takes four hex digits). */
74
+ const ESCAPES = new Set(['"', '\\', '/', 'b', 'f', 'n', 'r', 't']);
75
+ const HEX = /^[0-9a-fA-F]$/;
76
+ /**
77
+ * One character of a JSON string after its opening quote. `end` at the
78
+ * closing quote; `bad` where JSON cannot continue — a raw newline or other
79
+ * control character, or an escape JSON does not have.
80
+ */
81
+ function stringChar(s, ch) {
82
+ if (ch < ' ')
83
+ return 'bad';
84
+ if (s.esc === -1) {
85
+ if (ch === 'u')
86
+ s.esc = 4;
87
+ else if (ESCAPES.has(ch))
88
+ s.esc = 0;
89
+ else
90
+ return 'bad';
91
+ return 'more';
92
+ }
93
+ if (s.esc > 0) {
94
+ if (!HEX.test(ch))
95
+ return 'bad';
96
+ s.esc -= 1;
97
+ return 'more';
98
+ }
99
+ if (ch === '\\') {
100
+ s.esc = -1;
101
+ return 'more';
102
+ }
103
+ return ch === '"' ? 'end' : 'more';
104
+ }
105
+ /**
106
+ * One JSON value read character by character by JSON's own grammar, so a
107
+ * value that stops being JSON is known at its first wrong character — not
108
+ * only when its brackets fail to balance, which may never happen.
109
+ */
110
+ class JsonValue {
111
+ frames = [];
112
+ str = { esc: 0 };
113
+ inString = false;
114
+ scalar = '';
115
+ step(ch) {
116
+ if (this.inString)
117
+ return this.stringStep(ch);
118
+ if (this.scalar !== '') {
119
+ if (SCALAR_CHAR.test(ch))
120
+ return this.scalarStep(ch);
121
+ if (!SCALAR.test(this.scalar))
122
+ return 'bad';
123
+ this.scalar = '';
124
+ if (this.frames.length === 0)
125
+ return 'after';
126
+ }
127
+ const top = this.frames[this.frames.length - 1];
128
+ // Nothing open: this is the value's first character.
129
+ if (top === undefined)
130
+ return this.startValue(ch);
131
+ if (WS.has(ch))
132
+ return 'take';
133
+ if (ch === ',')
134
+ return this.expecting(top, 'commaOrClose', top.close === '}' ? 'key' : 'value');
135
+ if (ch === ':')
136
+ return this.expecting(top, 'colon', 'value');
137
+ if (ch === '}' || ch === ']')
138
+ return this.close(top, ch);
139
+ if (ch === '"' && (top.expect === 'keyOrClose' || top.expect === 'key')) {
140
+ top.expect = 'colon';
141
+ return this.openString();
142
+ }
143
+ return this.startValue(ch);
144
+ }
145
+ stringStep(ch) {
146
+ const r = stringChar(this.str, ch);
147
+ if (r !== 'end')
148
+ return r === 'bad' ? 'bad' : 'take';
149
+ this.inString = false;
150
+ return this.frames.length === 0 ? 'end' : 'take';
151
+ }
152
+ scalarStep(ch) {
153
+ if (!SCALAR_PREFIX.test(this.scalar + ch))
154
+ return 'bad';
155
+ this.scalar += ch;
156
+ return 'take';
157
+ }
158
+ expecting(top, want, next) {
159
+ if (top.expect !== want)
160
+ return 'bad';
161
+ top.expect = next;
162
+ return 'take';
163
+ }
164
+ close(top, ch) {
165
+ const closable = top.expect === 'keyOrClose' || top.expect === 'valueOrClose' || top.expect === 'commaOrClose';
166
+ if (top.close !== ch || !closable)
167
+ return 'bad';
168
+ this.frames.pop();
169
+ return this.frames.length === 0 ? 'end' : 'take';
170
+ }
171
+ openString() {
172
+ this.inString = true;
173
+ this.str.esc = 0;
174
+ return 'take';
175
+ }
176
+ /** A value begins — only where one is expected. */
177
+ startValue(ch) {
178
+ const top = this.frames[this.frames.length - 1];
179
+ if (top !== undefined) {
180
+ if (top.expect !== 'value' && top.expect !== 'valueOrClose')
181
+ return 'bad';
182
+ top.expect = 'commaOrClose';
183
+ }
184
+ if (ch === '"')
185
+ return this.openString();
186
+ if (ch === '{' || ch === '[') {
187
+ const obj = ch === '{';
188
+ this.frames.push({ close: obj ? '}' : ']', expect: obj ? 'keyOrClose' : 'valueOrClose' });
189
+ return 'take';
190
+ }
191
+ if (!SCALAR_PREFIX.test(ch))
192
+ return 'bad';
193
+ this.scalar = ch;
194
+ return 'take';
195
+ }
196
+ }
197
+ class Scanner {
198
+ key;
199
+ removed = [];
200
+ out = '';
201
+ ended = false;
202
+ mode = 'prose';
203
+ // Prose.
204
+ pendingWs = '';
205
+ /** A block was removed after `pendingWs` began: that whitespace is kept only if more text follows. */
206
+ pendingBeforeRemoved = false;
207
+ /** Line breaks dropped after the last removed block (`dropWs`). */
208
+ droppedNewlines = 0;
209
+ /** The whitespace right after a removed block is dropped — whole blank lines only. */
210
+ dropWs = false;
211
+ /**
212
+ * While `dropWs` is on: the spaces and tabs since the last newline — the
213
+ * next line's own indentation, which is not the removed block's to take.
214
+ */
215
+ indentWs = '';
216
+ lineStart = true;
217
+ textStart = true;
218
+ /** `[` written in the text and not yet closed — an object inside a list keeps its place. */
219
+ listDepth = 0;
220
+ /** Inside a string in a list (`listDepth > 0`). */
221
+ listString = false;
222
+ listStr = { esc: 0 };
223
+ /** The text began with an object that went whole — `{}` if nothing else follows. */
224
+ emptiedInitial = false;
225
+ // Code blocks.
226
+ inFence = false;
227
+ fenceTicks = 0;
228
+ ticks = '';
229
+ opener = '';
230
+ tail = '';
231
+ // Objects.
232
+ obj;
233
+ /** The emptied object whose line is being read (`lineRest`). */
234
+ emptied;
235
+ objects = 0;
236
+ constructor(key) {
237
+ this.key = key;
238
+ }
239
+ push(chunk) {
240
+ if (this.ended)
241
+ return '';
242
+ for (const ch of chunk)
243
+ this.step(ch);
244
+ return this.flush();
245
+ }
246
+ end() {
247
+ if (this.ended)
248
+ return '';
249
+ this.ended = true;
250
+ switch (this.mode) {
251
+ case 'ticks':
252
+ this.mode = 'prose';
253
+ this.text(this.takeTicks());
254
+ break;
255
+ case 'fenceInfo':
256
+ case 'fenceStart':
257
+ this.releaseWs();
258
+ this.emit(this.opener);
259
+ this.opener = '';
260
+ break;
261
+ case 'object':
262
+ this.endInObject();
263
+ break;
264
+ case 'lineRest': {
265
+ // The emptied object's line ended with the text: it stood alone — and
266
+ // a code block it began never closed, so the block goes too.
267
+ const o = this.emptied;
268
+ this.goesWhole(o);
269
+ if (o.fence !== undefined)
270
+ this.removeFencedBlock();
271
+ break;
272
+ }
273
+ case 'fenceAfterTicks':
274
+ if (this.ticks.length >= this.fenceTicks)
275
+ this.removeFencedBlock();
276
+ else
277
+ this.keepFencedBlock(this.takeTicks());
278
+ break;
279
+ case 'fenceAfter':
280
+ // The block never closed and held only the removed object.
281
+ this.removeFencedBlock();
282
+ break;
283
+ default:
284
+ break;
285
+ }
286
+ if (this.emptiedInitial) {
287
+ this.dropHeldWs();
288
+ this.emit('{}');
289
+ }
290
+ else if (this.pendingBeforeRemoved) {
291
+ this.dropHeldWs();
292
+ }
293
+ else {
294
+ this.releaseWs();
295
+ }
296
+ return this.flush();
297
+ }
298
+ // ─── Output ────────────────────────────────────────────────────────
299
+ flush() {
300
+ const out = this.out;
301
+ this.out = '';
302
+ return out;
303
+ }
304
+ emit(s) {
305
+ this.out += s;
306
+ }
307
+ /** Whitespace held before now goes out. */
308
+ releaseWs() {
309
+ // After a removed line, the blank line that followed it still keeps the
310
+ // text on either side apart — at most one blank line, and only where text
311
+ // came before the removed line.
312
+ let breaks = '';
313
+ if (this.pendingBeforeRemoved && this.pendingWs.includes('\n')) {
314
+ const have = this.pendingWs.split('\n').length - 1;
315
+ breaks = '\n'.repeat(Math.min(this.droppedNewlines, Math.max(0, 2 - have)));
316
+ }
317
+ this.emit(this.pendingWs + breaks + this.indentWs);
318
+ this.pendingWs = '';
319
+ this.indentWs = '';
320
+ this.droppedNewlines = 0;
321
+ this.pendingBeforeRemoved = false;
322
+ this.dropWs = false;
323
+ }
324
+ /** The text ended after a removed block: the whitespace held goes with it. */
325
+ dropHeldWs() {
326
+ this.pendingWs = '';
327
+ this.indentWs = '';
328
+ }
329
+ /**
330
+ * A removed line's indentation goes with it, so it is not carried onto the
331
+ * next line (four spaces there would make it a code block). The held
332
+ * whitespace is cut back to its last newline.
333
+ */
334
+ dropRemovedIndent() {
335
+ this.pendingWs = this.pendingWs.slice(0, this.pendingWs.lastIndexOf('\n') + 1);
336
+ this.indentWs = '';
337
+ }
338
+ /** Visible text: everything held before it goes out first. */
339
+ text(s) {
340
+ this.releaseWs();
341
+ this.emit(s);
342
+ this.lineStart = false;
343
+ this.textStart = false;
344
+ this.emptiedInitial = false;
345
+ }
346
+ takeTicks() {
347
+ const t = this.ticks;
348
+ this.ticks = '';
349
+ return t;
350
+ }
351
+ blockRemoved() {
352
+ this.pendingBeforeRemoved = true;
353
+ this.dropWs = true;
354
+ this.droppedNewlines = 0;
355
+ }
356
+ // ─── Dispatch ──────────────────────────────────────────────────────
357
+ step(ch) {
358
+ switch (this.mode) {
359
+ case 'prose':
360
+ return this.prose(ch);
361
+ case 'ticks':
362
+ return this.ticksStep(ch);
363
+ case 'fenceInfo':
364
+ return this.fenceInfo(ch);
365
+ case 'fenceStart':
366
+ return this.fenceStart(ch);
367
+ case 'object':
368
+ return this.objectStep(ch);
369
+ case 'lineRest':
370
+ return this.lineRest(ch);
371
+ case 'fenceAfter':
372
+ return this.fenceAfter(ch);
373
+ case 'fenceAfterTicks':
374
+ return this.fenceAfterTicks(ch);
375
+ }
376
+ }
377
+ // ─── Prose (a code block's body included) ──────────────────────────
378
+ prose(ch) {
379
+ if (this.listString)
380
+ return this.listStringStep(ch);
381
+ if (WS.has(ch)) {
382
+ if (ch === '\n')
383
+ this.lineStart = true;
384
+ if (!this.dropWs)
385
+ this.pendingWs += ch;
386
+ else if (ch === '\n' || ch === '\r') {
387
+ this.indentWs = '';
388
+ if (ch === '\n')
389
+ this.droppedNewlines += 1;
390
+ }
391
+ else if (this.lineStart)
392
+ this.indentWs += ch;
393
+ return;
394
+ }
395
+ if (ch === '`' && this.lineStart) {
396
+ this.mode = 'ticks';
397
+ this.ticks = ch;
398
+ this.textStart = false;
399
+ this.emptiedInitial = false;
400
+ return;
401
+ }
402
+ if (ch === '{')
403
+ return this.openObject(undefined);
404
+ if (ch === '[')
405
+ this.listDepth += 1;
406
+ else if (ch === ']' && this.listDepth > 0)
407
+ this.listDepth -= 1;
408
+ else if (ch === '"' && this.listDepth > 0) {
409
+ this.listString = true;
410
+ this.listStr.esc = 0;
411
+ }
412
+ this.text(ch);
413
+ }
414
+ /**
415
+ * A string inside a list: its `[`, `]` and `{` are words, not structure, so
416
+ * the list keeps its depth. A character no JSON string can hold (a raw
417
+ * newline) means this was never a JSON list: it is read as prose from here.
418
+ */
419
+ listStringStep(ch) {
420
+ const r = stringChar(this.listStr, ch);
421
+ if (r === 'bad') {
422
+ this.listString = false;
423
+ this.listDepth = 0;
424
+ return this.prose(ch);
425
+ }
426
+ if (r === 'end')
427
+ this.listString = false;
428
+ this.text(ch);
429
+ }
430
+ /** A run of backticks at the start of a line: a fence opens (or closes, inside one). */
431
+ ticksStep(ch) {
432
+ if (ch === '`') {
433
+ this.ticks += ch;
434
+ return;
435
+ }
436
+ const run = this.takeTicks();
437
+ this.mode = 'prose';
438
+ if (this.inFence) {
439
+ this.text(run);
440
+ if (run.length >= this.fenceTicks && WS.has(ch)) {
441
+ this.inFence = false;
442
+ this.fenceTicks = 0;
443
+ }
444
+ return this.prose(ch);
445
+ }
446
+ if (run.length >= 3) {
447
+ // A code block opens; its opening line is held until its first character.
448
+ this.fenceTicks = run.length;
449
+ this.opener = run;
450
+ this.mode = 'fenceInfo';
451
+ return this.fenceInfo(ch);
452
+ }
453
+ this.text(run);
454
+ this.prose(ch);
455
+ }
456
+ fenceInfo(ch) {
457
+ this.opener += ch;
458
+ if (ch === '\n')
459
+ this.mode = 'fenceStart';
460
+ }
461
+ fenceStart(ch) {
462
+ if (WS.has(ch)) {
463
+ this.opener += ch;
464
+ return;
465
+ }
466
+ if (ch === '{') {
467
+ const opener = this.opener;
468
+ this.opener = '';
469
+ this.lineStart = true;
470
+ return this.openObject(opener);
471
+ }
472
+ // The block does not open with an object: its opening line goes out as
473
+ // written, and its body is read as text.
474
+ this.releaseWs();
475
+ this.emit(this.opener);
476
+ this.opener = '';
477
+ this.inFence = true;
478
+ this.mode = 'prose';
479
+ this.lineStart = true;
480
+ this.prose(ch);
481
+ }
482
+ // ─── After an emptied object ───────────────────────────────────────
483
+ /** The rest of an emptied object's line: only whitespace up to the newline ⇒ it goes whole. */
484
+ lineRest(ch) {
485
+ const o = this.emptied;
486
+ if (ch === '\n') {
487
+ // Its line goes with it, newline and all.
488
+ this.goesWhole(o);
489
+ this.lineStart = true;
490
+ return;
491
+ }
492
+ if (WS.has(ch)) {
493
+ this.tail += ch;
494
+ return;
495
+ }
496
+ // Something else shares its line: it stays, as `{}`.
497
+ this.staysEmpty(o);
498
+ this.step(ch);
499
+ }
500
+ /** The emptied object stood alone on its line: it goes, and its line with it. */
501
+ goesWhole(o) {
502
+ this.emptied = undefined;
503
+ this.tail = '';
504
+ if (o.fence !== undefined) {
505
+ // The block's opening line stays held — without the removed line's indentation.
506
+ this.opener = o.fence.slice(0, o.fence.lastIndexOf('\n') + 1);
507
+ this.mode = 'fenceAfter';
508
+ this.lineStart = false;
509
+ return;
510
+ }
511
+ this.dropRemovedIndent();
512
+ if (o.initial)
513
+ this.emptiedInitial = true;
514
+ this.mode = 'prose';
515
+ this.lineStart = false;
516
+ this.blockRemoved();
517
+ }
518
+ /** The emptied object shares its line or sits in a list: it stays as `{}`. */
519
+ staysEmpty(o) {
520
+ this.emptied = undefined;
521
+ this.releaseWs();
522
+ if (o.fence !== undefined) {
523
+ this.emit(o.fence);
524
+ this.inFence = true;
525
+ }
526
+ this.emit('{}' + this.tail);
527
+ this.tail = '';
528
+ this.mode = 'prose';
529
+ this.lineStart = false;
530
+ this.textStart = false;
531
+ this.emptiedInitial = false;
532
+ }
533
+ fenceAfter(ch) {
534
+ if (WS.has(ch)) {
535
+ this.tail += ch;
536
+ if (ch === '\n')
537
+ this.lineStart = true;
538
+ return;
539
+ }
540
+ if (ch === '`' && this.lineStart) {
541
+ this.mode = 'fenceAfterTicks';
542
+ this.ticks = ch;
543
+ return;
544
+ }
545
+ if (ch === '{') {
546
+ // Another object in the same block: the block's opening line stays held
547
+ // with it, so a block of nothing but notes goes whole, fences and all.
548
+ const fence = this.opener + this.tail;
549
+ this.opener = '';
550
+ this.tail = '';
551
+ return this.openObject(fence);
552
+ }
553
+ this.keepFencedBlock('');
554
+ this.prose(ch);
555
+ }
556
+ fenceAfterTicks(ch) {
557
+ if (ch === '`') {
558
+ this.ticks += ch;
559
+ return;
560
+ }
561
+ if (this.ticks.length >= this.fenceTicks && WS.has(ch)) {
562
+ this.ticks = '';
563
+ this.removeFencedBlock();
564
+ return this.prose(ch);
565
+ }
566
+ this.keepFencedBlock(this.takeTicks());
567
+ this.prose(ch);
568
+ }
569
+ /** The code block held only the removed object: it goes, fences and all. */
570
+ removeFencedBlock() {
571
+ this.dropRemovedIndent();
572
+ this.opener = '';
573
+ this.tail = '';
574
+ this.inFence = false;
575
+ this.fenceTicks = 0;
576
+ this.mode = 'prose';
577
+ this.lineStart = false;
578
+ this.blockRemoved();
579
+ }
580
+ /** The code block holds more than the removed object's line: it stays, and is read on. */
581
+ keepFencedBlock(ticks) {
582
+ this.releaseWs();
583
+ this.emit(this.opener + this.tail + ticks);
584
+ this.opener = '';
585
+ this.tail = '';
586
+ this.inFence = true;
587
+ this.mode = 'prose';
588
+ this.lineStart = ticks === '' && this.lineStart;
589
+ this.textStart = false;
590
+ }
591
+ // ─── Objects ───────────────────────────────────────────────────────
592
+ openObject(fence) {
593
+ this.obj = {
594
+ index: this.objects++,
595
+ initial: this.textStart && fence === undefined,
596
+ ownLine: this.lineStart && this.listDepth === 0,
597
+ fence,
598
+ open: '{',
599
+ openHeld: true,
600
+ kept: 0,
601
+ removed: 0,
602
+ state: 'key?',
603
+ sep: '',
604
+ key: '',
605
+ keyStr: { esc: 0 },
606
+ reserved: false,
607
+ raw: '',
608
+ rawSep: 0,
609
+ value: '',
610
+ reader: undefined,
611
+ };
612
+ this.mode = 'object';
613
+ this.textStart = false;
614
+ this.lineStart = false;
615
+ this.emptiedInitial = false;
616
+ }
617
+ /** `{`, and the code block's opening line before it, go out. */
618
+ releaseOpen(o) {
619
+ if (!o.openHeld)
620
+ return;
621
+ this.releaseWs();
622
+ if (o.fence !== undefined) {
623
+ this.emit(o.fence);
624
+ this.inFence = true;
625
+ }
626
+ this.emit(o.open);
627
+ o.openHeld = false;
628
+ }
629
+ /** A character of the current member: shown when it is kept, held when it is reserved. */
630
+ member(o, ch, value) {
631
+ if (!o.reserved)
632
+ return this.emit(ch);
633
+ o.raw += ch;
634
+ if (value)
635
+ o.value += ch;
636
+ }
637
+ objectStep(ch) {
638
+ const o = this.obj;
639
+ switch (o.state) {
640
+ case 'key?':
641
+ if (WS.has(ch)) {
642
+ if (o.kept + o.removed === 0)
643
+ o.open += ch;
644
+ else
645
+ o.sep += ch;
646
+ return;
647
+ }
648
+ if (ch === '"') {
649
+ o.key = ch;
650
+ o.keyStr.esc = 0;
651
+ o.state = 'key';
652
+ return;
653
+ }
654
+ if (ch === '}' && o.kept + o.removed === 0) {
655
+ // `{}` — nothing to read, and nothing to remove.
656
+ this.releaseOpen(o);
657
+ this.emit(ch);
658
+ return this.finishObject();
659
+ }
660
+ return this.abort(o, ch);
661
+ case 'key': {
662
+ const r = stringChar(o.keyStr, ch);
663
+ if (r === 'bad')
664
+ return this.abort(o, ch);
665
+ o.key += ch;
666
+ if (r === 'end')
667
+ this.resolveKey(o);
668
+ return;
669
+ }
670
+ case 'colon':
671
+ if (WS.has(ch))
672
+ return this.member(o, ch, false);
673
+ if (ch === ':') {
674
+ this.member(o, ch, false);
675
+ o.state = 'value?';
676
+ return;
677
+ }
678
+ return this.abort(o, ch);
679
+ case 'value?':
680
+ if (WS.has(ch))
681
+ return this.member(o, ch, false);
682
+ return this.startValue(o, ch);
683
+ case 'value':
684
+ return this.valueStep(o, ch);
685
+ case 'after':
686
+ if (WS.has(ch)) {
687
+ o.sep += ch;
688
+ return;
689
+ }
690
+ if (ch === ',') {
691
+ o.sep += ch;
692
+ o.state = 'key?';
693
+ return;
694
+ }
695
+ if (ch === '}')
696
+ return this.closeObject(o);
697
+ return this.abort(o, ch);
698
+ }
699
+ }
700
+ resolveKey(o) {
701
+ let name;
702
+ try {
703
+ name = JSON.parse(o.key);
704
+ }
705
+ catch {
706
+ name = undefined;
707
+ }
708
+ if (name === this.key) {
709
+ // The separator that joined it to the member before goes with it.
710
+ o.reserved = true;
711
+ o.removed += 1;
712
+ o.raw = o.sep + o.key;
713
+ o.rawSep = o.sep.length;
714
+ o.value = '';
715
+ }
716
+ else {
717
+ o.reserved = false;
718
+ // The first member kept takes `{` and the whitespace after it; a
719
+ // separator left behind by removed members before it goes.
720
+ if (o.kept === 0)
721
+ this.releaseOpen(o);
722
+ else
723
+ this.emit(o.sep);
724
+ o.kept += 1;
725
+ this.emit(o.key);
726
+ }
727
+ o.sep = '';
728
+ o.key = '';
729
+ o.state = 'colon';
730
+ }
731
+ startValue(o, ch) {
732
+ o.reader = new JsonValue();
733
+ o.state = 'value';
734
+ this.valueStep(o, ch);
735
+ }
736
+ /** A character of a member's value, read by JSON's grammar: the first one JSON cannot take gives the text back. */
737
+ valueStep(o, ch) {
738
+ const r = o.reader.step(ch);
739
+ if (r === 'bad')
740
+ return this.abort(o, ch);
741
+ if (r === 'after') {
742
+ this.endValue(o);
743
+ return this.objectStep(ch);
744
+ }
745
+ this.member(o, ch, true);
746
+ if (r === 'end')
747
+ this.endValue(o);
748
+ }
749
+ endValue(o) {
750
+ if (o.reserved) {
751
+ this.removed.push({ object: o.index, valueText: o.value, complete: true });
752
+ o.reserved = false;
753
+ o.raw = '';
754
+ o.value = '';
755
+ }
756
+ o.reader = undefined;
757
+ o.state = 'after';
758
+ }
759
+ closeObject(o) {
760
+ if (o.kept > 0) {
761
+ this.emit(o.sep);
762
+ this.emit('}');
763
+ return this.finishObject();
764
+ }
765
+ // Every member was `_findings`: the object is empty now.
766
+ this.emptyObjectEnded(o);
767
+ }
768
+ /** An object that held nothing but notes is over: whole if it stood on its own line, else `{}`. */
769
+ emptyObjectEnded(o) {
770
+ this.obj = undefined;
771
+ this.lineStart = false;
772
+ this.tail = '';
773
+ if (o.ownLine) {
774
+ // Whether it goes whole depends on the rest of its line.
775
+ this.emptied = o;
776
+ this.mode = 'lineRest';
777
+ return;
778
+ }
779
+ this.staysEmpty(o);
780
+ }
781
+ finishObject() {
782
+ this.obj = undefined;
783
+ this.mode = 'prose';
784
+ this.lineStart = false;
785
+ }
786
+ /** Not JSON after all: everything held goes out as written, and the character is read again. */
787
+ abort(o, ch) {
788
+ // A `"` straight after a member is a missing comma before the next key,
789
+ // not a missing closing brace: that object goes on, so it is given back.
790
+ const missingComma = o.state === 'after' && ch === '"';
791
+ if (o.kept === 0 && o.removed > 0 && !o.reserved && !missingComma) {
792
+ // It held nothing but complete notes when the text stopped being JSON —
793
+ // a model a closing brace short. The notes object ends here and goes as
794
+ // an emptied one does; a trailing comma was its own, and the whitespace
795
+ // after it (and a key it had begun) is read as the text that follows.
796
+ const after = o.sep.slice(o.sep.lastIndexOf(',') + 1) + (o.state === 'key' ? o.key : '');
797
+ this.emptyObjectEnded(o);
798
+ for (const c of after)
799
+ this.step(c);
800
+ return this.step(ch);
801
+ }
802
+ this.releaseOpen(o);
803
+ // A separator after removed members with nothing kept before it is theirs
804
+ // (the rule `resolveKey` applies to the first kept member).
805
+ if (o.kept > 0)
806
+ this.emit(o.sep);
807
+ if (o.state === 'key')
808
+ this.emit(o.key);
809
+ // A reserved member given back: with nothing kept before it, the
810
+ // separator before it was a removed member's (the `resolveKey` rule).
811
+ if (o.reserved)
812
+ this.emit(o.kept > 0 ? o.raw : o.raw.slice(o.rawSep));
813
+ this.finishObject();
814
+ this.step(ch);
815
+ }
816
+ /** The text ended inside an object. */
817
+ endInObject() {
818
+ const o = this.obj;
819
+ this.obj = undefined;
820
+ this.mode = 'prose';
821
+ if (o.reserved) {
822
+ // Cut off inside `_findings`: its words stay hidden.
823
+ this.removed.push({ object: o.index, valueText: o.value, complete: false });
824
+ }
825
+ if (o.kept === 0 && o.removed > 0 && o.state !== 'key') {
826
+ // Nothing kept, something removed: as if it had closed there.
827
+ if (!o.ownLine)
828
+ return this.staysEmpty(o);
829
+ this.goesWhole(o);
830
+ if (o.fence !== undefined)
831
+ this.removeFencedBlock();
832
+ return;
833
+ }
834
+ // Everything else held goes out as written.
835
+ this.releaseOpen(o);
836
+ if (o.kept > 0)
837
+ this.emit(o.sep);
838
+ if (o.state === 'key')
839
+ this.emit(o.key);
840
+ }
841
+ }
842
+ //# sourceMappingURL=answerText.js.map