llmz 0.8.0 → 1.0.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 (224) hide show
  1. package/DOCS.md +230 -1796
  2. package/ERRORS.md +98 -0
  3. package/README.md +96 -548
  4. package/dist/chat/chat.d.ts +63 -0
  5. package/dist/chat/component.d.ts +31 -0
  6. package/dist/chat/component.default.d.ts +58 -0
  7. package/dist/chat/response.d.ts +15 -0
  8. package/dist/compiler/compiler.d.ts +2 -7
  9. package/dist/compiler/plugins/dynamic-code.d.ts +7 -0
  10. package/dist/compiler/plugins/replace-comment.d.ts +2 -3
  11. package/dist/compiler/plugins/return-async.d.ts +5 -4
  12. package/dist/compiler/plugins/termination.d.ts +8 -0
  13. package/dist/compiler/plugins/tool-resolution.d.ts +4 -0
  14. package/dist/compiler/plugins/variable-extraction.d.ts +2 -8
  15. package/dist/component-BpMVZYOa.cjs +649 -0
  16. package/dist/component-C27qu6Zv.js +614 -0
  17. package/dist/context.d.ts +74 -385
  18. package/dist/custom-client-BDkgBR2w.js +1631 -0
  19. package/dist/custom-client-MytucGI-.cjs +1708 -0
  20. package/dist/custom-client.d.ts +12 -2
  21. package/dist/errors/catalog.d.ts +287 -0
  22. package/dist/errors/hooks.d.ts +2 -0
  23. package/dist/errors-H4JJFH3S.js +620 -0
  24. package/dist/errors-IhnTKUp7.cjs +871 -0
  25. package/dist/errors.d.ts +18 -36
  26. package/dist/execute-BYI_U5sR.js +1943 -0
  27. package/dist/execute-DLas4V2n.cjs +1944 -0
  28. package/dist/{exit-DUM96DN1.cjs → exit-BUgJ5qi5.cjs} +38 -28
  29. package/dist/{exit-CDdxDBRT.js → exit-CPNk3vbz.js} +37 -27
  30. package/dist/exit.d.ts +19 -16
  31. package/dist/index.cjs +181 -999
  32. package/dist/index.d.ts +25 -28
  33. package/dist/index.js +141 -974
  34. package/dist/index.workerd.js +7725 -9531
  35. package/dist/inspect.d.ts +19 -3
  36. package/dist/inspection.d.ts +38 -0
  37. package/dist/native-Bm6L9zOb.cjs +212 -0
  38. package/dist/native-DvTH9vFe.js +177 -0
  39. package/dist/objects.d.ts +2 -57
  40. package/dist/prompts/native.d.ts +2 -0
  41. package/dist/prompts/prompt.d.ts +10 -98
  42. package/dist/quickjs-variant-DJwYDSsb.cjs +1073 -0
  43. package/dist/quickjs-variant-DlPi9-Sx.js +984 -0
  44. package/dist/result.d.ts +30 -861
  45. package/dist/runtime/execute.d.ts +3 -2
  46. package/dist/runtime/execution-activity.d.ts +11 -0
  47. package/dist/runtime/execution-activity.test.d.ts +1 -0
  48. package/dist/runtime/execution-diagnostics.d.ts +11 -0
  49. package/dist/runtime/execution-diagnostics.test.d.ts +1 -0
  50. package/dist/runtime/execution-report.d.ts +48 -0
  51. package/dist/runtime/execution-report.test.d.ts +1 -0
  52. package/dist/runtime/execution-safety.test.d.ts +1 -0
  53. package/dist/runtime/exit-control-flow.test.d.ts +1 -0
  54. package/dist/runtime/fixtures/chat.d.ts +8 -0
  55. package/dist/runtime/fixtures/long-search.d.ts +19 -0
  56. package/dist/runtime/fixtures/long-search.test.d.ts +1 -0
  57. package/dist/runtime/fixtures/native-client.d.ts +33 -0
  58. package/dist/runtime/fixtures/protocol-languages.d.ts +74 -0
  59. package/dist/runtime/generate.d.ts +23 -27
  60. package/dist/runtime/generate.test.d.ts +1 -0
  61. package/dist/runtime/hook-provenance.test.d.ts +1 -0
  62. package/dist/runtime/inspection-hook.test.d.ts +1 -0
  63. package/dist/runtime/inspection-values.d.ts +14 -0
  64. package/dist/runtime/inspection-values.test.d.ts +1 -0
  65. package/dist/runtime/interruption-control-flow.test.d.ts +1 -0
  66. package/dist/runtime/interruption-settings.test.d.ts +1 -0
  67. package/dist/runtime/javascript-api.d.ts +59 -0
  68. package/dist/runtime/javascript-api.test.d.ts +1 -0
  69. package/dist/runtime/native-review.test.d.ts +1 -0
  70. package/dist/runtime/native-tools.d.ts +18 -0
  71. package/dist/runtime/native-tools.test.d.ts +1 -0
  72. package/dist/runtime/object-memory.test.d.ts +1 -0
  73. package/dist/runtime/protocol-adherence.test.d.ts +1 -0
  74. package/dist/runtime/single-tool.test.d.ts +1 -0
  75. package/dist/runtime/token-budget.d.ts +22 -0
  76. package/dist/runtime/token-budget.test.d.ts +1 -0
  77. package/dist/runtime/tool-wrapper.d.ts +4 -5
  78. package/dist/runtime/traces.test.d.ts +1 -0
  79. package/dist/runtime/truncation.test.d.ts +1 -0
  80. package/dist/runtime/types.d.ts +41 -36
  81. package/dist/runtime/variable-recovery.test.d.ts +1 -0
  82. package/dist/runtime/vm-context.d.ts +6 -1
  83. package/dist/runtime/worker-completion.test.d.ts +1 -0
  84. package/dist/runtime-names.d.ts +4 -0
  85. package/dist/schema-Aer_W7Al.js +45 -0
  86. package/dist/schema-DBkjo0b7.cjs +68 -0
  87. package/dist/schema.d.ts +10 -0
  88. package/dist/schema.test.d.ts +1 -0
  89. package/dist/session/compactor.d.ts +51 -0
  90. package/dist/session/compactor.test.d.ts +1 -0
  91. package/dist/session/history.d.ts +36 -0
  92. package/dist/session/history.test.d.ts +1 -0
  93. package/dist/session/json.d.ts +3 -0
  94. package/dist/session/json.test.d.ts +1 -0
  95. package/dist/session/memory-codec.d.ts +9 -0
  96. package/dist/session/memory-codec.test.d.ts +1 -0
  97. package/dist/session/memory-render.d.ts +15 -0
  98. package/dist/session/memory-render.test.d.ts +1 -0
  99. package/dist/session/memory.d.ts +112 -0
  100. package/dist/session/memory.test.d.ts +1 -0
  101. package/dist/session/messages.d.ts +17 -0
  102. package/dist/session/messages.test.d.ts +1 -0
  103. package/dist/session/serialization.d.ts +34 -0
  104. package/dist/session/serialization.test.d.ts +1 -0
  105. package/dist/session/session-memory.test.d.ts +1 -0
  106. package/dist/session/session.d.ts +117 -0
  107. package/dist/session/session.test.d.ts +1 -0
  108. package/dist/{transcript.d.ts → session/transcript.d.ts} +4 -8
  109. package/dist/session/transcript.test.d.ts +1 -0
  110. package/dist/{tool-Bv8N4IPj.js → tool--gK91eez.js} +144 -92
  111. package/dist/{tool-Dy6AJpJH.cjs → tool-Da_F-N-5.cjs} +146 -94
  112. package/dist/tool.d.ts +25 -17
  113. package/dist/truncate-BSoH2-nY.js +79 -0
  114. package/dist/truncate-nV3Mxr6-.cjs +102 -0
  115. package/dist/truncate.d.ts +24 -0
  116. package/dist/truncate.test.d.ts +1 -0
  117. package/dist/types.d.ts +27 -9
  118. package/dist/{typings-3lsqCbkn.cjs → typings-C1ihexWE.cjs} +38 -193
  119. package/dist/{typings-CrBa7Tyy.js → typings-D9_aC8aq.js} +38 -175
  120. package/dist/typings-formatter.d.ts +1 -1
  121. package/dist/typings.d.ts +3 -0
  122. package/dist/{uniq-BYQ7GWwb.js → uniq-DfUeSNaX.js} +2 -2
  123. package/dist/{uniq-G872RWmE.cjs → uniq-DiXgysNV.cjs} +6 -18
  124. package/dist/{_baseIteratee-P_firB7b.js → utils-BOFzb8Ok.js} +1134 -16
  125. package/dist/{_baseIteratee-CF4qP2a8.cjs → utils-CTlsXvdj.cjs} +1281 -84
  126. package/dist/vm/drivers/node.d.ts +1 -1
  127. package/dist/vm/drivers/quickjs.d.ts +1 -1
  128. package/dist/vm/errors.d.ts +4 -4
  129. package/dist/vm/index.d.ts +3 -4
  130. package/dist/vm/instrument.d.ts +14 -6
  131. package/dist/vm/member-proxy.d.ts +4 -0
  132. package/dist/vm/member-proxy.test.d.ts +1 -0
  133. package/dist/vm/memory.test.d.ts +1 -0
  134. package/dist/vm/return-semantics.test.d.ts +1 -0
  135. package/dist/vm/types.d.ts +18 -3
  136. package/dist/{vm-BLsHw1da.js → vm-8jxt8PTK.js} +1162 -180
  137. package/dist/{vm-D6s1zF6M.cjs → vm-XuVPuW3Y.cjs} +1195 -207
  138. package/package.json +13 -6
  139. package/dist/_baseForOwn-BfWDTL8L.cjs +0 -53
  140. package/dist/_baseForOwn-CFhWe_W6.js +0 -48
  141. package/dist/_getPrototype-CG7Dxrrz.js +0 -6
  142. package/dist/_getPrototype-DQDlNVVM.cjs +0 -10
  143. package/dist/_overArg-C5Hy7oCi.cjs +0 -196
  144. package/dist/_overArg-CRYbj1NH.js +0 -155
  145. package/dist/chat.d.ts +0 -401
  146. package/dist/common-DdArNQhl.cjs +0 -643
  147. package/dist/common-pgJjFu2Y.js +0 -596
  148. package/dist/compiler/plugins/track-tool-calls.d.ts +0 -33
  149. package/dist/compiler-C-OI9sim.cjs +0 -547
  150. package/dist/compiler-CW1VjQsn.js +0 -473
  151. package/dist/component-BXS8vAwO.cjs +0 -73
  152. package/dist/component-Dq7ZLdvy.js +0 -44
  153. package/dist/component.d.ts +0 -88
  154. package/dist/component.default.d.ts +0 -268
  155. package/dist/custom-client-2V9lmzUf.js +0 -2655
  156. package/dist/custom-client-HkM1Sdzv.cjs +0 -2726
  157. package/dist/dual-modes-Cb7ThvoV.cjs +0 -3020
  158. package/dist/dual-modes-Cy0_W4O2.js +0 -2930
  159. package/dist/errors-C962uJdS.cjs +0 -220
  160. package/dist/errors-Cg1G0Awu.js +0 -161
  161. package/dist/example.d.ts +0 -45
  162. package/dist/execute-Aw4cbIx-.js +0 -1080
  163. package/dist/execute-COIHOIKR.cjs +0 -1081
  164. package/dist/exit-parser.d.ts +0 -39
  165. package/dist/handlers.d.ts +0 -10
  166. package/dist/hoist.d.ts +0 -2
  167. package/dist/isEmpty-BhJRCqIF.js +0 -52
  168. package/dist/isEmpty-LY533Wmh.cjs +0 -57
  169. package/dist/jsx-B0iYyvfr.js +0 -100
  170. package/dist/jsx-D5Y2cPo-.cjs +0 -129
  171. package/dist/jsx.d.ts +0 -11
  172. package/dist/message-stream/index.d.ts +0 -9
  173. package/dist/message-stream/instructions.d.ts +0 -22
  174. package/dist/message-stream/normalizer.d.ts +0 -21
  175. package/dist/message-stream/parser.d.ts +0 -83
  176. package/dist/message-stream/registry.d.ts +0 -14
  177. package/dist/message-stream/replayable.d.ts +0 -27
  178. package/dist/message-stream/sanitize.d.ts +0 -10
  179. package/dist/message-stream/stream.d.ts +0 -54
  180. package/dist/message-stream/types.d.ts +0 -96
  181. package/dist/message-stream/validator.d.ts +0 -23
  182. package/dist/prompts/chat-mode/system.d.ts +0 -2
  183. package/dist/prompts/chat-mode/system.md.d.ts +0 -2
  184. package/dist/prompts/chat-mode/user.d.ts +0 -2
  185. package/dist/prompts/chat-mode/user.md.d.ts +0 -2
  186. package/dist/prompts/common.d.ts +0 -6
  187. package/dist/prompts/dual-modes.d.ts +0 -2
  188. package/dist/prompts/execution-history.d.ts +0 -6
  189. package/dist/prompts/execution-state.d.ts +0 -3
  190. package/dist/prompts/protocol.d.ts +0 -18
  191. package/dist/prompts/worker-mode/system.d.ts +0 -2
  192. package/dist/prompts/worker-mode/system.md.d.ts +0 -2
  193. package/dist/prompts/worker-mode/user.d.ts +0 -2
  194. package/dist/prompts/worker-mode/user.md.d.ts +0 -2
  195. package/dist/quickjs-variant-BYhMBpwU.js +0 -45
  196. package/dist/quickjs-variant-CsIwTVFC.cjs +0 -56
  197. package/dist/rolldown-runtime-EAsCxrDo.js +0 -14
  198. package/dist/runtime/interpret-result.d.ts +0 -22
  199. package/dist/snapshots.d.ts +0 -249
  200. package/dist/stack-traces-BIE_5Xwf.js +0 -21
  201. package/dist/stack-traces-ntw_qhoy.cjs +0 -26
  202. package/dist/truncator-CLkouTCO.cjs +0 -276
  203. package/dist/truncator-DZqEqnpD.js +0 -253
  204. package/dist/truncator.d.ts +0 -118
  205. package/dist/utils-3ESwP3yg.cjs +0 -1075
  206. package/dist/utils-UBeAFeEI.js +0 -950
  207. /package/dist/{citations.d.ts → chat/citations.d.ts} +0 -0
  208. /package/dist/{citations.test.d.ts → chat/citations.test.d.ts} +0 -0
  209. /package/dist/{component.default.test.d.ts → chat/component.default.test.d.ts} +0 -0
  210. /package/dist/{compiler/plugins/track-tool-calls.test.d.ts → chat/component.test.d.ts} +0 -0
  211. /package/dist/{example.test.d.ts → chat/response.test.d.ts} +0 -0
  212. /package/dist/{exit-parser.test.d.ts → compiler/plugins/dynamic-code.test.d.ts} +0 -0
  213. /package/dist/{handlers.test.d.ts → compiler/plugins/tool-resolution.test.d.ts} +0 -0
  214. /package/dist/{hoist.test.d.ts → errors/catalog.test.d.ts} +0 -0
  215. /package/dist/{message-stream/instructions.test.d.ts → exit.test.d.ts} +0 -0
  216. /package/dist/{message-stream/normalizer.test.d.ts → inspection.test.d.ts} +0 -0
  217. /package/dist/{message-stream/parser.test.d.ts → prompts/native.test.d.ts} +0 -0
  218. /package/dist/{message-stream/stream.test.d.ts → result.test.d.ts} +0 -0
  219. /package/dist/{message-stream/validator.test.d.ts → runtime/chat-delivery.test.d.ts} +0 -0
  220. /package/dist/{prompts/common.test.d.ts → runtime/citations.test.d.ts} +0 -0
  221. /package/dist/{prompts/protocol.test.d.ts → runtime/compatibility-regressions.test.d.ts} +0 -0
  222. /package/dist/runtime/{interpret-result.test.d.ts → context-registration.test.d.ts} +0 -0
  223. /package/dist/{transcript.test.d.ts → runtime/error-recovery.test.d.ts} +0 -0
  224. /package/dist/{truncator.test.d.ts → runtime/errors.test.d.ts} +0 -0
@@ -1,643 +0,0 @@
1
- const require_compiler = require("./compiler-C-OI9sim.cjs");
2
- let json5 = require("json5");
3
- json5 = require_compiler.__toESM(json5, 1);
4
- let jsonrepair = require("jsonrepair");
5
- //#region src/message-stream/types.ts
6
- /**
7
- * Reserved protocol symbol. Every block of an LLM response starts with `■`:
8
- *
9
- * ■send=<component> { props? }
10
- * streaming body here
11
- *
12
- * ■run
13
- * // code here
14
- *
15
- * ■next=<name> { props? }
16
- *
17
- * The model must never emit `■` inside props or body content.
18
- */
19
- const MARKER = "■";
20
- /** Component and exit names are normalized to lowercase (kebab or snake case). */
21
- const NAME_REGEX = /^[a-z][a-z0-9_-]*$/;
22
- //#endregion
23
- //#region src/message-stream/parser.ts
24
- const isWhitespace = (char) => /\s/.test(char);
25
- /**
26
- * Lenient JSON parsing: JSON5 first (unquoted keys, single quotes, trailing
27
- * commas), then jsonrepair for anything else the model got slightly wrong.
28
- */
29
- const tryParseJson = (text) => {
30
- try {
31
- return json5.default.parse(text);
32
- } catch {
33
- try {
34
- return json5.default.parse((0, jsonrepair.jsonrepair)(text));
35
- } catch {
36
- const trimmed = text.trim();
37
- if (/^\{\s*\{/.test(trimmed) && /\}\s*\}$/.test(trimmed)) try {
38
- return json5.default.parse(trimmed.slice(1, -1));
39
- } catch {}
40
- return;
41
- }
42
- }
43
- };
44
- /**
45
- * Incremental, chunk-boundary-agnostic parser for the message-stream protocol.
46
- *
47
- * The parser is purely syntactic: it knows the reserved `■` symbol and the
48
- * directive grammar, but nothing about registered components, exits or their
49
- * schemas. Semantic validation is a separate step (see `validator.ts`).
50
- * Text outside protocol blocks is always discarded with a diagnostic; only
51
- * explicit `■send` blocks can produce messages.
52
- *
53
- * Pushing the same text split across arbitrary chunk boundaries always produces
54
- * the same items and the same concatenated body deltas. For a higher-level
55
- * `AsyncIterable` API, see `stream.ts`.
56
- */
57
- var StreamingMessageParser = class {
58
- _maxPropsLength;
59
- _state = "idle";
60
- _items = [];
61
- _diagnostics = [];
62
- _current;
63
- _currentReady = false;
64
- _counter = 0;
65
- _finished = false;
66
- _directiveBuffer = "";
67
- _nameBuffer = "";
68
- _propsBuffer = "";
69
- _propsDepth = 0;
70
- _quoteChar;
71
- _escaped = false;
72
- _propsBroken = false;
73
- _bodyDelta = "";
74
- _pendingWhitespace = "";
75
- _preambleHeader;
76
- _closingFenceTicks;
77
- constructor(options = {}) {
78
- this._maxPropsLength = options.maxPropsLength ?? 1e5;
79
- }
80
- /** All items parsed so far, in order of appearance. */
81
- get items() {
82
- return [...this._items];
83
- }
84
- /** All syntax diagnostics, including discarded text that has no protocol item. */
85
- get diagnostics() {
86
- return [...this._diagnostics];
87
- }
88
- push(chunk) {
89
- if (this._finished) throw new Error("Parser has already finished. Call reset() before parsing a new stream.");
90
- const events = [];
91
- for (const char of chunk) this._processChar(char, events);
92
- this._flushBodyDelta(events);
93
- return events;
94
- }
95
- /**
96
- * Signals the end of the transport stream.
97
- * Pass `'interrupted'` when the stream was cut short (aborted request, network
98
- * error) so the active item is marked `interrupted` instead of `complete`.
99
- * All received content is preserved either way.
100
- */
101
- finish(reason = "end") {
102
- if (this._finished) return [];
103
- this._finished = true;
104
- const events = [];
105
- if (this._closingFenceTicks !== void 0 && this._closingFenceTicks !== 3) this._skipUnexpectedText(events);
106
- this._closingFenceTicks = void 0;
107
- const status = reason === "interrupted" ? "interrupted" : void 0;
108
- if (this._state === "skip" && this._preambleHeader) this._recoverPreambleHeader(events);
109
- switch (this._state) {
110
- case "directive":
111
- this._endDirective(events, void 0);
112
- this._completeCurrent(events, status);
113
- break;
114
- case "name":
115
- this._endName(events);
116
- this._completeCurrent(events, status);
117
- break;
118
- case "header":
119
- case "body-wait":
120
- this._completeCurrent(events, status);
121
- break;
122
- case "props":
123
- this._endPartialProps(events);
124
- this._completeCurrent(events, "interrupted");
125
- break;
126
- case "body":
127
- this._pendingWhitespace = "";
128
- this._flushBodyDelta(events);
129
- this._completeCurrent(events, status);
130
- }
131
- return events;
132
- }
133
- reset() {
134
- this._state = "idle";
135
- this._items = [];
136
- this._diagnostics = [];
137
- this._current = void 0;
138
- this._currentReady = false;
139
- this._counter = 0;
140
- this._finished = false;
141
- this._directiveBuffer = "";
142
- this._nameBuffer = "";
143
- this._propsBuffer = "";
144
- this._propsDepth = 0;
145
- this._quoteChar = void 0;
146
- this._escaped = false;
147
- this._propsBroken = false;
148
- this._bodyDelta = "";
149
- this._pendingWhitespace = "";
150
- this._preambleHeader = void 0;
151
- this._closingFenceTicks = void 0;
152
- }
153
- _processChar(char, events) {
154
- if (this._closingFenceTicks !== void 0) {
155
- if (char === "`" && this._closingFenceTicks < 3) {
156
- this._closingFenceTicks++;
157
- return;
158
- }
159
- if (isWhitespace(char) && this._closingFenceTicks === 3) return;
160
- this._closingFenceTicks = void 0;
161
- this._skipUnexpectedText(events);
162
- }
163
- switch (this._state) {
164
- case "idle":
165
- if (char === "■") this._beginItem();
166
- else if (!isWhitespace(char)) {
167
- const last = this._items.at(-1);
168
- if (char === "`" && (last === null || last === void 0 ? void 0 : last.kind) === "next" && last.status === "complete") {
169
- this._closingFenceTicks = 1;
170
- return;
171
- }
172
- this._skipUnexpectedText(events);
173
- if (!this._items.length) this._preambleHeader = "";
174
- }
175
- return;
176
- case "directive":
177
- if (char === "=") this._endDirective(events, "=");
178
- else if (char === "■") {
179
- this._endDirective(events, void 0);
180
- this._completeCurrent(events);
181
- this._beginItem();
182
- } else if (char === "\n") this._endDirective(events, "\n");
183
- else if (isWhitespace(char)) this._endDirective(events, " ");
184
- else if (char === "{") this._endDirective(events, "{");
185
- else this._directiveBuffer += char;
186
- return;
187
- case "name":
188
- if (char === "■") {
189
- this._endName(events);
190
- this._completeCurrent(events);
191
- this._beginItem();
192
- } else if (char === "{") {
193
- this._endName(events);
194
- this._afterHeaderChar(char, events);
195
- } else if (char === "\n") {
196
- this._endName(events);
197
- this._afterHeaderChar(char, events);
198
- } else if (isWhitespace(char)) {
199
- this._endName(events);
200
- this._state = "header";
201
- } else this._nameBuffer += char;
202
- return;
203
- case "header":
204
- this._afterHeaderChar(char, events);
205
- return;
206
- case "props":
207
- this._processPropsChar(char, events);
208
- return;
209
- case "body-wait":
210
- if (char === "■") {
211
- this._completeCurrent(events);
212
- this._beginItem();
213
- } else if (!isWhitespace(char)) {
214
- this._startBody(events);
215
- this._appendBody(char);
216
- }
217
- return;
218
- case "body":
219
- if (char === "■") {
220
- this._pendingWhitespace = "";
221
- this._flushBodyDelta(events);
222
- this._completeCurrent(events);
223
- this._beginItem();
224
- } else this._appendBody(char);
225
- return;
226
- case "skip":
227
- if (this._preambleHeader !== void 0) {
228
- if (char === "■") this._preambleHeader = "■";
229
- else if (this._preambleHeader) {
230
- this._preambleHeader += char;
231
- if (char === "\n") this._recoverPreambleHeader(events);
232
- else if (this._preambleHeader.length > this._maxPropsLength) this._preambleHeader = "";
233
- }
234
- return;
235
- }
236
- if (char === "■") this._beginItem();
237
- return;
238
- }
239
- }
240
- _recoverPreambleHeader(events) {
241
- const header = this._preambleHeader ?? "";
242
- if (/^■(?:run|(?:send|next)=[a-z][a-z0-9_-]*(?:[ \t]*\{[^\r\n]*\})?)[ \t]*\r?\n?$/i.test(header)) {
243
- this._preambleHeader = void 0;
244
- this._state = "idle";
245
- for (const char of header) this._processChar(char, events);
246
- } else this._preambleHeader = "";
247
- }
248
- /** Handles a character on the header line, after the directive and name have been parsed. */
249
- _afterHeaderChar(char, events) {
250
- const item = this._current;
251
- if (char === "■") {
252
- this._completeCurrent(events);
253
- this._beginItem();
254
- return;
255
- }
256
- if (char === "{" && (item.kind === "send" || item.kind === "next")) {
257
- this._startProps(char);
258
- return;
259
- }
260
- if (char === "\n") {
261
- if (item.kind === "next") {
262
- this._completeCurrent(events);
263
- this._state = "idle";
264
- } else this._state = "body-wait";
265
- return;
266
- }
267
- if (isWhitespace(char)) return;
268
- if (item.kind === "next") {
269
- this._completeCurrent(events);
270
- this._skipUnexpectedText(events);
271
- return;
272
- }
273
- this._startBody(events);
274
- this._appendBody(char);
275
- }
276
- _processPropsChar(char, events) {
277
- this._propsBuffer += char;
278
- if (this._propsBuffer.length > this._maxPropsLength) {
279
- this._failProps(events, `props exceeded the maximum length of ${this._maxPropsLength} characters`, "props-too-long");
280
- this._completeCurrent(events);
281
- this._state = "skip";
282
- return;
283
- }
284
- if (this._escaped) {
285
- this._escaped = false;
286
- return;
287
- }
288
- if (this._quoteChar) {
289
- if (char === "\\") this._escaped = true;
290
- else if (char === this._quoteChar) this._quoteChar = void 0;
291
- else if (char === "\n" || char === "\r") this._propsBroken = true;
292
- else if (char === "■" && this._propsBroken) {
293
- this._failProps(events, "props were interrupted by a new block");
294
- this._completeCurrent(events);
295
- this._beginItem();
296
- }
297
- return;
298
- }
299
- if (char === "\"" || char === "'") this._quoteChar = char;
300
- else if (char === "■") {
301
- this._failProps(events, "props were interrupted by a new block");
302
- this._completeCurrent(events);
303
- this._beginItem();
304
- } else if (char === "{" || char === "[") this._propsDepth++;
305
- else if (char === "}" || char === "]") {
306
- this._propsDepth--;
307
- if (this._propsDepth <= 0) this._endProps(events);
308
- }
309
- }
310
- _beginItem() {
311
- const item = {
312
- id: `item-${this._counter++}`,
313
- kind: "unknown",
314
- name: "",
315
- props: {},
316
- status: "pending",
317
- diagnostics: []
318
- };
319
- this._items.push(item);
320
- this._current = item;
321
- this._currentReady = false;
322
- this._directiveBuffer = "";
323
- this._nameBuffer = "";
324
- this._state = "directive";
325
- }
326
- /**
327
- * Finalizes the directive token. `terminator` tells us what ended it:
328
- * '=' (a name follows), whitespace/newline/'{' (no name), or undefined (■ or end of stream).
329
- */
330
- _endDirective(events, terminator) {
331
- const item = this._current;
332
- const directive = this._directiveBuffer.toLowerCase();
333
- if (directive === "send" || directive === "next" || directive === "run") item.kind = directive;
334
- else {
335
- item.kind = "unknown";
336
- item.status = "invalid";
337
- this._diagnostic(events, {
338
- code: "invalid-directive",
339
- message: `Unknown directive "■${this._directiveBuffer}". Expected ■send, ■run or ■next.`,
340
- itemId: item.id
341
- });
342
- events.push({
343
- type: "item-start",
344
- item
345
- });
346
- this._completeCurrent(events);
347
- this._state = "skip";
348
- return;
349
- }
350
- if (item.kind === "run") {
351
- events.push({
352
- type: "item-start",
353
- item
354
- });
355
- if (terminator === "=") {
356
- this._diagnostic(events, {
357
- code: "invalid-directive",
358
- message: "■run does not take a name",
359
- itemId: item.id
360
- });
361
- this._state = "header";
362
- } else if (terminator === "\n") this._state = "body-wait";
363
- else if (terminator === "{" || terminator === " ") {
364
- this._state = "header";
365
- if (terminator === "{") {
366
- this._startBody(events);
367
- this._appendBody("{");
368
- }
369
- }
370
- return;
371
- }
372
- if (terminator === "=") {
373
- this._nameBuffer = "";
374
- this._state = "name";
375
- return;
376
- }
377
- item.status = "invalid";
378
- this._diagnostic(events, {
379
- code: "invalid-name",
380
- message: `■${directive} requires a name (e.g. ■${directive}=example)`,
381
- itemId: item.id
382
- });
383
- events.push({
384
- type: "item-start",
385
- item
386
- });
387
- if (terminator === "\n") {
388
- this._state = item.kind === "next" ? "idle" : "body-wait";
389
- if (item.kind === "next") this._completeCurrent(events);
390
- } else if (terminator === "{") this._startProps("{");
391
- else this._state = "header";
392
- }
393
- _endName(events) {
394
- const item = this._current;
395
- const name = this._nameBuffer.toLowerCase();
396
- item.name = name;
397
- if (!NAME_REGEX.test(name)) {
398
- item.status = "invalid";
399
- this._diagnostic(events, {
400
- code: "invalid-name",
401
- message: `Invalid name "${this._nameBuffer}" for ■${item.kind}`,
402
- itemId: item.id
403
- });
404
- }
405
- events.push({
406
- type: "item-start",
407
- item
408
- });
409
- }
410
- _startProps(char) {
411
- this._propsBuffer = char;
412
- this._propsDepth = 1;
413
- this._quoteChar = void 0;
414
- this._escaped = false;
415
- this._propsBroken = false;
416
- this._state = "props";
417
- }
418
- _endProps(events) {
419
- const item = this._current;
420
- const parsed = tryParseJson(this._propsBuffer);
421
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) this._failProps(events, "props are not a valid JSON object");
422
- else {
423
- item.props = parsed;
424
- this._ready(events);
425
- }
426
- if (item.kind === "next") {
427
- this._completeCurrent(events);
428
- this._state = "idle";
429
- } else this._state = "body-wait";
430
- }
431
- /** Attempts to salvage props that were cut off by the end of the stream. */
432
- _endPartialProps(events) {
433
- const item = this._current;
434
- const repaired = tryParseJson(this._propsBuffer);
435
- this._diagnostic(events, {
436
- code: "interrupted",
437
- message: `Props of ■${item.kind} were interrupted by the end of the stream`,
438
- itemId: item.id
439
- });
440
- if (repaired && typeof repaired === "object" && !Array.isArray(repaired)) item.props = repaired;
441
- else {
442
- item.status = "invalid";
443
- this._diagnostic(events, {
444
- code: "invalid-props",
445
- message: `Invalid props for ■${item.kind}: props are not a valid JSON object`,
446
- itemId: item.id
447
- });
448
- }
449
- }
450
- _failProps(events, detail, code = "invalid-props") {
451
- const item = this._current;
452
- item.status = "invalid";
453
- item.props = {};
454
- this._diagnostic(events, {
455
- code,
456
- message: `Invalid props for ■${item.kind}${item.name ? `=${item.name}` : ""}: ${detail}`,
457
- itemId: item.id
458
- });
459
- }
460
- _startBody(events) {
461
- const item = this._current;
462
- this._ready(events);
463
- item.body ??= "";
464
- this._pendingWhitespace = "";
465
- events.push({
466
- type: "body-start",
467
- itemId: item.id
468
- });
469
- this._state = "body";
470
- }
471
- _appendBody(char) {
472
- if (isWhitespace(char)) {
473
- this._pendingWhitespace += char;
474
- return;
475
- }
476
- if (this._pendingWhitespace) {
477
- this._emitBodyText(this._pendingWhitespace);
478
- this._pendingWhitespace = "";
479
- }
480
- this._emitBodyText(char);
481
- }
482
- _emitBodyText(text) {
483
- this._current.body += text;
484
- this._bodyDelta += text;
485
- }
486
- _flushBodyDelta(events) {
487
- if (this._bodyDelta && this._current) events.push({
488
- type: "body-delta",
489
- itemId: this._current.id,
490
- delta: this._bodyDelta
491
- });
492
- this._bodyDelta = "";
493
- }
494
- _ready(events) {
495
- const item = this._current;
496
- if (!item || this._currentReady) return;
497
- this._currentReady = true;
498
- if (item.status !== "invalid") item.status = "streaming";
499
- events.push({
500
- type: "item-ready",
501
- item
502
- });
503
- }
504
- _completeCurrent(events, forcedStatus) {
505
- const item = this._current;
506
- if (!item) return;
507
- this._ready(events);
508
- if (item.status !== "invalid") item.status = forcedStatus ?? "complete";
509
- events.push({
510
- type: "item-complete",
511
- item
512
- });
513
- this._current = void 0;
514
- this._currentReady = false;
515
- }
516
- _skipUnexpectedText(events) {
517
- this._diagnostic(events, {
518
- code: "unexpected-text",
519
- message: "Encountered text outside of a ■ block"
520
- });
521
- this._state = "skip";
522
- }
523
- _diagnostic(events, diagnostic) {
524
- this._diagnostics.push(diagnostic);
525
- if (diagnostic.itemId) {
526
- const item = this._items.find((i) => i.id === diagnostic.itemId);
527
- item === null || item === void 0 || item.diagnostics.push(diagnostic);
528
- }
529
- events.push({
530
- type: "diagnostic",
531
- diagnostic
532
- });
533
- }
534
- };
535
- //#endregion
536
- //#region src/prompts/common.ts
537
- var common_exports = /* @__PURE__ */ require_compiler.__exportAll({
538
- parseAssistantResponse: () => parseAssistantResponse,
539
- replacePlaceholders: () => replacePlaceholders,
540
- toParsedAssistantResponse: () => toParsedAssistantResponse
541
- });
542
- /** Strips wrapping code fences the model may have added around the whole response. */
543
- const stripWrappingFences = (text) => text.trim().split("\n").filter((line, index, arr) => {
544
- if ((index === 0 || index === arr.length - 1) && line.trim().startsWith("```")) return false;
545
- return true;
546
- }).join("\n");
547
- /** Builds a {@link ParsedAssistantResponse} from parsed protocol items. */
548
- const toParsedAssistantResponse = (items, raw, diagnostics = []) => {
549
- var _run$body;
550
- const runIndex = items.findIndex((item) => item.kind === "run");
551
- const run = items[runIndex];
552
- const awaitsResult = !!(run === null || run === void 0 ? void 0 : run.body) && require_compiler.hasTopLevelReturn(run.body);
553
- const prematureSends = awaitsResult ? items.slice(runIndex + 1).filter((item) => item.kind === "send") : [];
554
- const sends = items.filter((item, index) => item.kind === "send" && (!awaitsResult || index < runIndex)).map((item) => ({
555
- name: item.name,
556
- props: item.props,
557
- body: item.body
558
- }));
559
- const next = items.find((item) => item.kind === "next" && item.status !== "invalid");
560
- return {
561
- raw,
562
- items,
563
- diagnostics: [...diagnostics, ...prematureSends.map((item) => ({
564
- code: "send-after-run",
565
- itemId: item.id,
566
- message: "Discarded a message generated after code returning a result, before that result was available to the model."
567
- }))],
568
- sends,
569
- code: (run === null || run === void 0 || (_run$body = run.body) === null || _run$body === void 0 ? void 0 : _run$body.trim()) || void 0,
570
- next: next ? {
571
- name: next.name,
572
- props: next.props
573
- } : void 0
574
- };
575
- };
576
- const parseAssistantResponse = (response) => {
577
- const parser = new StreamingMessageParser();
578
- parser.push(stripWrappingFences(response));
579
- parser.finish();
580
- return toParsedAssistantResponse(parser.items, response, parser.diagnostics);
581
- };
582
- const replacePlaceholders = (prompt, values) => {
583
- const regex = /* @__PURE__ */ new RegExp("■■■([A-Z0-9_\\.-]+)■■■", "gi");
584
- const obj = Object.assign({}, values);
585
- const replaced = prompt.replace(regex, (_match, name) => {
586
- if (name in values) {
587
- delete obj[name];
588
- return typeof values[name] === "string" ? values[name] : JSON.stringify(values[name]);
589
- } else throw new Error(`Placeholder not found: ${name}`);
590
- });
591
- const remaining = Object.keys(obj).filter((key) => key !== "is_message_enabled" && key !== "exits" && key !== "components" && key !== "transcript");
592
- if (remaining.length) throw new Error(`Missing placeholders: ${remaining.join(", ")}`);
593
- return replaced.replace(/\n{3,}/g, "\n\n").trim();
594
- };
595
- //#endregion
596
- Object.defineProperty(exports, "MARKER", {
597
- enumerable: true,
598
- get: function() {
599
- return MARKER;
600
- }
601
- });
602
- Object.defineProperty(exports, "NAME_REGEX", {
603
- enumerable: true,
604
- get: function() {
605
- return NAME_REGEX;
606
- }
607
- });
608
- Object.defineProperty(exports, "StreamingMessageParser", {
609
- enumerable: true,
610
- get: function() {
611
- return StreamingMessageParser;
612
- }
613
- });
614
- Object.defineProperty(exports, "common_exports", {
615
- enumerable: true,
616
- get: function() {
617
- return common_exports;
618
- }
619
- });
620
- Object.defineProperty(exports, "parseAssistantResponse", {
621
- enumerable: true,
622
- get: function() {
623
- return parseAssistantResponse;
624
- }
625
- });
626
- Object.defineProperty(exports, "replacePlaceholders", {
627
- enumerable: true,
628
- get: function() {
629
- return replacePlaceholders;
630
- }
631
- });
632
- Object.defineProperty(exports, "toParsedAssistantResponse", {
633
- enumerable: true,
634
- get: function() {
635
- return toParsedAssistantResponse;
636
- }
637
- });
638
- Object.defineProperty(exports, "tryParseJson", {
639
- enumerable: true,
640
- get: function() {
641
- return tryParseJson;
642
- }
643
- });