llmz 0.9.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 (231) hide show
  1. package/DOCS.md +230 -1796
  2. package/ERRORS.md +98 -0
  3. package/README.md +96 -582
  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 +70 -377
  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 +176 -1237
  32. package/dist/index.d.ts +25 -28
  33. package/dist/index.js +137 -1213
  34. package/dist/index.workerd.js +8427 -10771
  35. package/dist/inspect.d.ts +18 -4
  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 -101
  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-diagnostics.d.ts +11 -0
  48. package/dist/runtime/execution-report.d.ts +48 -0
  49. package/dist/runtime/fixtures/chat.d.ts +8 -0
  50. package/dist/runtime/fixtures/long-search.d.ts +1 -1
  51. package/dist/runtime/fixtures/native-client.d.ts +33 -0
  52. package/dist/runtime/fixtures/protocol-languages.d.ts +1 -1
  53. package/dist/runtime/generate.d.ts +23 -20
  54. package/dist/runtime/inspection-values.d.ts +14 -0
  55. package/dist/runtime/inspection-values.test.d.ts +1 -0
  56. package/dist/runtime/interruption-control-flow.test.d.ts +1 -0
  57. package/dist/runtime/interruption-settings.test.d.ts +1 -0
  58. package/dist/runtime/javascript-api.d.ts +59 -0
  59. package/dist/runtime/javascript-api.test.d.ts +1 -0
  60. package/dist/runtime/native-review.test.d.ts +1 -0
  61. package/dist/runtime/native-tools.d.ts +18 -0
  62. package/dist/runtime/native-tools.test.d.ts +1 -0
  63. package/dist/runtime/object-memory.test.d.ts +1 -0
  64. package/dist/runtime/single-tool.test.d.ts +1 -0
  65. package/dist/runtime/token-budget.d.ts +22 -0
  66. package/dist/runtime/token-budget.test.d.ts +1 -0
  67. package/dist/runtime/tool-wrapper.d.ts +4 -5
  68. package/dist/runtime/traces.test.d.ts +1 -0
  69. package/dist/runtime/truncation.test.d.ts +1 -0
  70. package/dist/runtime/types.d.ts +39 -31
  71. package/dist/runtime/variable-recovery.test.d.ts +1 -0
  72. package/dist/runtime/vm-context.d.ts +6 -1
  73. package/dist/runtime/worker-completion.test.d.ts +1 -0
  74. package/dist/runtime-names.d.ts +4 -0
  75. package/dist/schema-Aer_W7Al.js +45 -0
  76. package/dist/schema-DBkjo0b7.cjs +68 -0
  77. package/dist/schema.d.ts +10 -0
  78. package/dist/schema.test.d.ts +1 -0
  79. package/dist/session/compactor.d.ts +51 -0
  80. package/dist/session/compactor.test.d.ts +1 -0
  81. package/dist/session/history.d.ts +36 -0
  82. package/dist/session/history.test.d.ts +1 -0
  83. package/dist/session/json.d.ts +3 -0
  84. package/dist/session/json.test.d.ts +1 -0
  85. package/dist/session/memory-codec.d.ts +9 -0
  86. package/dist/session/memory-codec.test.d.ts +1 -0
  87. package/dist/session/memory-render.d.ts +15 -0
  88. package/dist/session/memory-render.test.d.ts +1 -0
  89. package/dist/session/memory.d.ts +112 -0
  90. package/dist/session/memory.test.d.ts +1 -0
  91. package/dist/session/messages.d.ts +17 -0
  92. package/dist/session/messages.test.d.ts +1 -0
  93. package/dist/session/serialization.d.ts +34 -0
  94. package/dist/session/serialization.test.d.ts +1 -0
  95. package/dist/session/session-memory.test.d.ts +1 -0
  96. package/dist/session/session.d.ts +117 -0
  97. package/dist/session/session.test.d.ts +1 -0
  98. package/dist/{transcript.d.ts → session/transcript.d.ts} +4 -8
  99. package/dist/session/transcript.test.d.ts +1 -0
  100. package/dist/{tool-Bv8N4IPj.js → tool--gK91eez.js} +144 -92
  101. package/dist/{tool-Dy6AJpJH.cjs → tool-Da_F-N-5.cjs} +146 -94
  102. package/dist/tool.d.ts +25 -17
  103. package/dist/truncate-BSoH2-nY.js +79 -0
  104. package/dist/truncate-nV3Mxr6-.cjs +102 -0
  105. package/dist/truncate.d.ts +24 -0
  106. package/dist/truncate.test.d.ts +1 -0
  107. package/dist/types.d.ts +27 -9
  108. package/dist/{typings-3lsqCbkn.cjs → typings-C1ihexWE.cjs} +38 -193
  109. package/dist/{typings-CrBa7Tyy.js → typings-D9_aC8aq.js} +38 -175
  110. package/dist/typings-formatter.d.ts +1 -1
  111. package/dist/typings.d.ts +3 -0
  112. package/dist/{uniq-BYQ7GWwb.js → uniq-DfUeSNaX.js} +2 -2
  113. package/dist/{uniq-G872RWmE.cjs → uniq-DiXgysNV.cjs} +6 -18
  114. package/dist/{_baseIteratee-P_firB7b.js → utils-BOFzb8Ok.js} +1134 -16
  115. package/dist/{_baseIteratee-CF4qP2a8.cjs → utils-CTlsXvdj.cjs} +1281 -84
  116. package/dist/vm/drivers/node.d.ts +1 -1
  117. package/dist/vm/drivers/quickjs.d.ts +1 -1
  118. package/dist/vm/errors.d.ts +4 -4
  119. package/dist/vm/index.d.ts +3 -4
  120. package/dist/vm/instrument.d.ts +14 -6
  121. package/dist/vm/member-proxy.d.ts +4 -0
  122. package/dist/vm/member-proxy.test.d.ts +1 -0
  123. package/dist/vm/memory.test.d.ts +1 -0
  124. package/dist/vm/return-semantics.test.d.ts +1 -0
  125. package/dist/vm/types.d.ts +18 -3
  126. package/dist/{vm-BLsHw1da.js → vm-8jxt8PTK.js} +1162 -180
  127. package/dist/{vm-D6s1zF6M.cjs → vm-XuVPuW3Y.cjs} +1195 -207
  128. package/package.json +13 -6
  129. package/dist/_baseForOwn-BfWDTL8L.cjs +0 -53
  130. package/dist/_baseForOwn-CFhWe_W6.js +0 -48
  131. package/dist/_getPrototype-CG7Dxrrz.js +0 -6
  132. package/dist/_getPrototype-DQDlNVVM.cjs +0 -10
  133. package/dist/_overArg-C5Hy7oCi.cjs +0 -196
  134. package/dist/_overArg-CRYbj1NH.js +0 -155
  135. package/dist/chat.d.ts +0 -401
  136. package/dist/common-BlRFtdf2.js +0 -807
  137. package/dist/common-BnvrDnIT.cjs +0 -860
  138. package/dist/compiler/plugins/track-tool-calls.d.ts +0 -33
  139. package/dist/compiler-C-OI9sim.cjs +0 -547
  140. package/dist/compiler-CW1VjQsn.js +0 -473
  141. package/dist/component-C6Agl2ns.cjs +0 -73
  142. package/dist/component-DGG7d_Mz.js +0 -44
  143. package/dist/component.d.ts +0 -88
  144. package/dist/component.default.d.ts +0 -278
  145. package/dist/custom-client-ChuZ63zp.js +0 -2658
  146. package/dist/custom-client-DAsGRnYc.cjs +0 -2729
  147. package/dist/dual-modes-DDkoCp3R.js +0 -3006
  148. package/dist/dual-modes-DPXiuvNe.cjs +0 -3090
  149. package/dist/errors-C962uJdS.cjs +0 -220
  150. package/dist/errors-Cg1G0Awu.js +0 -161
  151. package/dist/example-format.d.ts +0 -8
  152. package/dist/example.d.ts +0 -45
  153. package/dist/execute-BWcdv9so.js +0 -1059
  154. package/dist/execute-DM2-7y86.cjs +0 -1060
  155. package/dist/exit-parser.d.ts +0 -39
  156. package/dist/handlers.d.ts +0 -10
  157. package/dist/hoist.d.ts +0 -2
  158. package/dist/isEmpty-BhJRCqIF.js +0 -52
  159. package/dist/isEmpty-LY533Wmh.cjs +0 -57
  160. package/dist/isPlainObject-CSxXVKCk.cjs +0 -56
  161. package/dist/isPlainObject-D39AzPO2.js +0 -51
  162. package/dist/jsx-CIgdGGVs.js +0 -52
  163. package/dist/jsx-owqxrquU.cjs +0 -75
  164. package/dist/jsx.d.ts +0 -11
  165. package/dist/message-stream/index.d.ts +0 -9
  166. package/dist/message-stream/instructions.d.ts +0 -30
  167. package/dist/message-stream/normalizer.d.ts +0 -21
  168. package/dist/message-stream/parser.d.ts +0 -89
  169. package/dist/message-stream/registry.d.ts +0 -14
  170. package/dist/message-stream/replayable.d.ts +0 -27
  171. package/dist/message-stream/response-parser.d.ts +0 -24
  172. package/dist/message-stream/sanitize.d.ts +0 -10
  173. package/dist/message-stream/stream.d.ts +0 -54
  174. package/dist/message-stream/types.d.ts +0 -97
  175. package/dist/message-stream/validator.d.ts +0 -23
  176. package/dist/prompts/chat-mode/protocol.d.ts +0 -2
  177. package/dist/prompts/chat-mode/system.d.ts +0 -2
  178. package/dist/prompts/chat-mode/system.md.d.ts +0 -2
  179. package/dist/prompts/chat-mode/user.d.ts +0 -2
  180. package/dist/prompts/chat-mode/user.md.d.ts +0 -2
  181. package/dist/prompts/code-examples.d.ts +0 -2
  182. package/dist/prompts/common.d.ts +0 -7
  183. package/dist/prompts/dual-modes.d.ts +0 -2
  184. package/dist/prompts/execution-history.d.ts +0 -6
  185. package/dist/prompts/execution-state.d.ts +0 -3
  186. package/dist/prompts/protocol-basics.d.ts +0 -5
  187. package/dist/prompts/protocol.d.ts +0 -31
  188. package/dist/prompts/worker-mode/protocol.d.ts +0 -2
  189. package/dist/prompts/worker-mode/system.d.ts +0 -2
  190. package/dist/prompts/worker-mode/system.md.d.ts +0 -2
  191. package/dist/prompts/worker-mode/user.d.ts +0 -2
  192. package/dist/prompts/worker-mode/user.md.d.ts +0 -2
  193. package/dist/quickjs-variant-BYhMBpwU.js +0 -45
  194. package/dist/quickjs-variant-CsIwTVFC.cjs +0 -56
  195. package/dist/rolldown-runtime-EAsCxrDo.js +0 -14
  196. package/dist/runtime/fixtures/qwen-protocol-failures.d.ts +0 -29
  197. package/dist/runtime/interpret-result.d.ts +0 -22
  198. package/dist/snapshots.d.ts +0 -249
  199. package/dist/stack-traces-BIE_5Xwf.js +0 -21
  200. package/dist/stack-traces-ntw_qhoy.cjs +0 -26
  201. package/dist/truncator-CLkouTCO.cjs +0 -276
  202. package/dist/truncator-DZqEqnpD.js +0 -253
  203. package/dist/truncator.d.ts +0 -118
  204. package/dist/utils-3ESwP3yg.cjs +0 -1075
  205. package/dist/utils-UBeAFeEI.js +0 -950
  206. /package/dist/{citations.d.ts → chat/citations.d.ts} +0 -0
  207. /package/dist/{citations.test.d.ts → chat/citations.test.d.ts} +0 -0
  208. /package/dist/{component.default.test.d.ts → chat/component.default.test.d.ts} +0 -0
  209. /package/dist/{compiler/plugins/track-tool-calls.test.d.ts → chat/component.test.d.ts} +0 -0
  210. /package/dist/{example.test.d.ts → chat/response.test.d.ts} +0 -0
  211. /package/dist/{exit-parser.test.d.ts → compiler/plugins/dynamic-code.test.d.ts} +0 -0
  212. /package/dist/{handlers.test.d.ts → compiler/plugins/tool-resolution.test.d.ts} +0 -0
  213. /package/dist/{hoist.test.d.ts → errors/catalog.test.d.ts} +0 -0
  214. /package/dist/{message-stream/instructions.test.d.ts → exit.test.d.ts} +0 -0
  215. /package/dist/{message-stream/normalizer.test.d.ts → inspection.test.d.ts} +0 -0
  216. /package/dist/{message-stream/parser.test.d.ts → prompts/native.test.d.ts} +0 -0
  217. /package/dist/{message-stream/response-parser.test.d.ts → result.test.d.ts} +0 -0
  218. /package/dist/{message-stream/stream.test.d.ts → runtime/chat-delivery.test.d.ts} +0 -0
  219. /package/dist/{message-stream/validator.test.d.ts → runtime/compatibility-regressions.test.d.ts} +0 -0
  220. /package/dist/{prompts/adherence.test.d.ts → runtime/context-registration.test.d.ts} +0 -0
  221. /package/dist/{prompts/catalogue-examples.test.d.ts → runtime/error-recovery.test.d.ts} +0 -0
  222. /package/dist/{prompts/code-examples.test.d.ts → runtime/errors.test.d.ts} +0 -0
  223. /package/dist/{prompts/common.test.d.ts → runtime/execution-activity.test.d.ts} +0 -0
  224. /package/dist/{prompts/concrete-patterns.test.d.ts → runtime/execution-diagnostics.test.d.ts} +0 -0
  225. /package/dist/{prompts/long-search.test.d.ts → runtime/execution-report.test.d.ts} +0 -0
  226. /package/dist/{prompts/protocol-simple.test.d.ts → runtime/execution-safety.test.d.ts} +0 -0
  227. /package/dist/{prompts/protocol.test.d.ts → runtime/exit-control-flow.test.d.ts} +0 -0
  228. /package/dist/{prompts/sections.test.d.ts → runtime/fixtures/long-search.test.d.ts} +0 -0
  229. /package/dist/runtime/{interpret-result.test.d.ts → generate.test.d.ts} +0 -0
  230. /package/dist/{transcript.test.d.ts → runtime/hook-provenance.test.d.ts} +0 -0
  231. /package/dist/{truncator.test.d.ts → runtime/inspection-hook.test.d.ts} +0 -0
@@ -1,807 +0,0 @@
1
- import { n as __exportAll } from "./rolldown-runtime-EAsCxrDo.js";
2
- import { r as hasTopLevelReturn } from "./compiler-CW1VjQsn.js";
3
- import JSON5 from "json5";
4
- import { jsonrepair } from "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.parse(text);
32
- } catch {
33
- try {
34
- return JSON5.parse(jsonrepair(text));
35
- } catch {
36
- const trimmed = text.trim();
37
- if (/^\{\s*\{/.test(trimmed) && /\}\s*\}$/.test(trimmed)) try {
38
- return JSON5.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
- _ended = false;
67
- _directiveBuffer = "";
68
- _nameBuffer = "";
69
- _propsBuffer = "";
70
- _propsDepth = 0;
71
- _quoteChar;
72
- _escaped = false;
73
- _propsBroken = false;
74
- _bodyDelta = "";
75
- _pendingWhitespace = "";
76
- _preambleHeader;
77
- _closingFenceTicks;
78
- _lineStart = true;
79
- _exampleDelimiter = "";
80
- constructor(options = {}) {
81
- this._maxPropsLength = options.maxPropsLength ?? 1e5;
82
- }
83
- /** All items parsed so far, in order of appearance. */
84
- get items() {
85
- return [...this._items];
86
- }
87
- /** All syntax diagnostics, including discarded text that has no protocol item. */
88
- get diagnostics() {
89
- return [...this._diagnostics];
90
- }
91
- push(chunk) {
92
- if (this._finished) throw new Error("Parser has already finished. Call reset() before parsing a new stream.");
93
- const events = [];
94
- for (const char of chunk) this._processOutputChar(char, events);
95
- this._flushBodyDelta(events);
96
- return events;
97
- }
98
- /**
99
- * Signals the end of the transport stream.
100
- * Pass `'interrupted'` when the stream was cut short (aborted request, network
101
- * error) so the active item is marked `interrupted` instead of `complete`.
102
- * All received content is preserved either way.
103
- */
104
- finish(reason = "end") {
105
- if (this._finished) return [];
106
- this._finished = true;
107
- const events = [];
108
- if (/^"""\s*$/.test(this._exampleDelimiter)) this._discardExampleDelimiter(events);
109
- else if (reason === "end") this._flushExampleDelimiter(events);
110
- else this._exampleDelimiter = "";
111
- if (this._closingFenceTicks !== void 0 && this._closingFenceTicks !== 3) this._skipUnexpectedText(events);
112
- this._closingFenceTicks = void 0;
113
- const status = reason === "interrupted" ? "interrupted" : void 0;
114
- if (this._state === "skip" && this._preambleHeader) this._recoverPreambleHeader(events);
115
- switch (this._state) {
116
- case "directive":
117
- this._endDirective(events, void 0);
118
- this._completeCurrent(events, status);
119
- break;
120
- case "name":
121
- this._endName(events);
122
- this._completeCurrent(events, status);
123
- break;
124
- case "header":
125
- case "body-wait":
126
- this._completeCurrent(events, status);
127
- break;
128
- case "props":
129
- this._endPartialProps(events);
130
- this._completeCurrent(events, "interrupted");
131
- break;
132
- case "body":
133
- this._pendingWhitespace = "";
134
- this._flushBodyDelta(events);
135
- this._completeCurrent(events, status);
136
- }
137
- return events;
138
- }
139
- reset() {
140
- this._state = "idle";
141
- this._items = [];
142
- this._diagnostics = [];
143
- this._current = void 0;
144
- this._currentReady = false;
145
- this._counter = 0;
146
- this._finished = false;
147
- this._ended = false;
148
- this._directiveBuffer = "";
149
- this._nameBuffer = "";
150
- this._propsBuffer = "";
151
- this._propsDepth = 0;
152
- this._quoteChar = void 0;
153
- this._escaped = false;
154
- this._propsBroken = false;
155
- this._bodyDelta = "";
156
- this._pendingWhitespace = "";
157
- this._preambleHeader = void 0;
158
- this._closingFenceTicks = void 0;
159
- this._lineStart = true;
160
- this._exampleDelimiter = "";
161
- }
162
- _processOutputChar(char, events) {
163
- const lineStart = this._lineStart;
164
- this._lineStart = char === "\n" || lineStart && (char === " " || char === " " || char === "\r");
165
- if (this._exampleDelimiter) {
166
- if (this._exampleDelimiter.includes("\n")) {
167
- if (isWhitespace(char)) {
168
- this._exampleDelimiter += char;
169
- return;
170
- }
171
- if (char === "■") this._discardExampleDelimiter(events);
172
- else this._flushExampleDelimiter(events);
173
- } else {
174
- const candidate = this._exampleDelimiter + char;
175
- if (/^(?:"{1,2}|"""[ \t]*\r?)$/.test(candidate)) {
176
- this._exampleDelimiter = candidate;
177
- return;
178
- }
179
- if (/^"""[ \t]*\r?\n$/.test(candidate)) {
180
- this._exampleDelimiter = candidate;
181
- if (this._state !== "body") this._discardExampleDelimiter(events);
182
- return;
183
- }
184
- this._flushExampleDelimiter(events);
185
- }
186
- }
187
- if (lineStart && char === "\"" && (this._state === "idle" || this._state === "skip" || this._state === "body-wait" || this._state === "body")) {
188
- this._exampleDelimiter = char;
189
- return;
190
- }
191
- this._processChar(char, events);
192
- }
193
- _flushExampleDelimiter(events) {
194
- for (const char of this._exampleDelimiter) this._processChar(char, events);
195
- this._exampleDelimiter = "";
196
- }
197
- _discardExampleDelimiter(events) {
198
- this._exampleDelimiter = "";
199
- this._diagnostic(events, {
200
- code: "example-delimiter",
201
- message: "Discarded a triple-quote example delimiter"
202
- });
203
- }
204
- _processChar(char, events) {
205
- if (this._closingFenceTicks !== void 0) {
206
- if (char === "`" && this._closingFenceTicks < 3) {
207
- this._closingFenceTicks++;
208
- return;
209
- }
210
- if (isWhitespace(char) && this._closingFenceTicks === 3) return;
211
- this._closingFenceTicks = void 0;
212
- this._skipUnexpectedText(events);
213
- }
214
- if (this._ended) {
215
- if (this._state === "skip" || isWhitespace(char)) return;
216
- if (char === "`") {
217
- this._closingFenceTicks = 1;
218
- return;
219
- }
220
- this._skipUnexpectedText(events);
221
- return;
222
- }
223
- switch (this._state) {
224
- case "idle":
225
- if (char === "■") this._beginItem(events);
226
- else if (!isWhitespace(char)) {
227
- const last = this._items.at(-1);
228
- if (char === "`" && (last === null || last === void 0 ? void 0 : last.kind) === "next" && last.status === "complete") {
229
- this._closingFenceTicks = 1;
230
- return;
231
- }
232
- this._skipUnexpectedText(events);
233
- if (!this._items.length) this._preambleHeader = "";
234
- }
235
- return;
236
- case "directive":
237
- if (char === "=") this._endDirective(events, "=");
238
- else if (char === "■") {
239
- this._endDirective(events, void 0);
240
- this._completeCurrent(events);
241
- this._beginItem(events);
242
- } else if (char === "\n") this._endDirective(events, "\n");
243
- else if (isWhitespace(char)) this._endDirective(events, " ");
244
- else if (char === "{") this._endDirective(events, "{");
245
- else this._directiveBuffer += char;
246
- return;
247
- case "name":
248
- if (char === "■") {
249
- this._endName(events);
250
- this._completeCurrent(events);
251
- this._beginItem(events);
252
- } else if (char === "{") {
253
- this._endName(events);
254
- this._afterHeaderChar(char, events);
255
- } else if (char === "\n") {
256
- this._endName(events);
257
- this._afterHeaderChar(char, events);
258
- } else if (isWhitespace(char)) {
259
- this._endName(events);
260
- this._state = "header";
261
- } else this._nameBuffer += char;
262
- return;
263
- case "header":
264
- this._afterHeaderChar(char, events);
265
- return;
266
- case "props":
267
- this._processPropsChar(char, events);
268
- return;
269
- case "body-wait":
270
- if (char === "■") {
271
- this._completeCurrent(events);
272
- this._beginItem(events);
273
- } else if (!isWhitespace(char)) {
274
- this._startBody(events);
275
- this._appendBody(char);
276
- }
277
- return;
278
- case "body":
279
- if (char === "■") {
280
- this._pendingWhitespace = "";
281
- this._flushBodyDelta(events);
282
- this._completeCurrent(events);
283
- this._beginItem(events);
284
- } else this._appendBody(char);
285
- return;
286
- case "skip":
287
- if (this._preambleHeader !== void 0) {
288
- if (char === "■") this._preambleHeader = "■";
289
- else if (this._preambleHeader) {
290
- this._preambleHeader += char;
291
- if (char === "\n") this._recoverPreambleHeader(events);
292
- else if (this._preambleHeader.length > this._maxPropsLength) this._preambleHeader = "";
293
- }
294
- return;
295
- }
296
- if (char === "■") this._beginItem(events);
297
- return;
298
- }
299
- }
300
- _recoverPreambleHeader(events) {
301
- const header = this._preambleHeader ?? "";
302
- if (/^■(?:run|(?:send|next)=[a-z][a-z0-9_-]*(?:[ \t]*\{[^\r\n]*\})?)[ \t]*\r?\n?$/i.test(header)) {
303
- this._preambleHeader = void 0;
304
- this._state = "idle";
305
- for (const char of header) this._processChar(char, events);
306
- } else this._preambleHeader = "";
307
- }
308
- /** Handles a character on the header line, after the directive and name have been parsed. */
309
- _afterHeaderChar(char, events) {
310
- const item = this._current;
311
- if (char === "■") {
312
- this._completeCurrent(events);
313
- this._beginItem(events);
314
- return;
315
- }
316
- if (char === "{" && (item.kind === "send" || item.kind === "next")) {
317
- this._startProps(char);
318
- return;
319
- }
320
- if (char === "\n") {
321
- if (item.kind === "next") {
322
- this._completeCurrent(events);
323
- this._state = "idle";
324
- } else this._state = "body-wait";
325
- return;
326
- }
327
- if (isWhitespace(char)) return;
328
- if (item.kind === "next") {
329
- this._completeCurrent(events);
330
- this._skipUnexpectedText(events);
331
- return;
332
- }
333
- this._startBody(events);
334
- this._appendBody(char);
335
- }
336
- _processPropsChar(char, events) {
337
- this._propsBuffer += char;
338
- if (this._propsBuffer.length > this._maxPropsLength) {
339
- this._failProps(events, `props exceeded the maximum length of ${this._maxPropsLength} characters`, "props-too-long");
340
- this._completeCurrent(events);
341
- this._state = "skip";
342
- return;
343
- }
344
- if (this._escaped) {
345
- this._escaped = false;
346
- return;
347
- }
348
- if (this._quoteChar) {
349
- if (char === "\\") this._escaped = true;
350
- else if (char === this._quoteChar) this._quoteChar = void 0;
351
- else if (char === "\n" || char === "\r") this._propsBroken = true;
352
- else if (char === "■" && this._propsBroken) {
353
- this._failProps(events, "props were interrupted by a new block");
354
- this._completeCurrent(events);
355
- this._beginItem(events);
356
- }
357
- return;
358
- }
359
- if (char === "\"" || char === "'") this._quoteChar = char;
360
- else if (char === "■") {
361
- this._failProps(events, "props were interrupted by a new block");
362
- this._completeCurrent(events);
363
- this._beginItem(events);
364
- } else if (char === "{" || char === "[") this._propsDepth++;
365
- else if (char === "}" || char === "]") {
366
- this._propsDepth--;
367
- if (this._propsDepth <= 0) this._endProps(events);
368
- }
369
- }
370
- _beginItem(events) {
371
- if (this._ended) {
372
- this._skipUnexpectedText(events);
373
- return;
374
- }
375
- const item = {
376
- id: `item-${this._counter++}`,
377
- kind: "unknown",
378
- name: "",
379
- props: {},
380
- status: "pending",
381
- diagnostics: []
382
- };
383
- this._items.push(item);
384
- this._current = item;
385
- this._currentReady = false;
386
- this._directiveBuffer = "";
387
- this._nameBuffer = "";
388
- this._state = "directive";
389
- }
390
- /**
391
- * Finalizes the directive token. `terminator` tells us what ended it:
392
- * '=' (a name follows), whitespace/newline/'{' (no name), or undefined (■ or end of stream).
393
- */
394
- _endDirective(events, terminator) {
395
- const item = this._current;
396
- const directive = this._directiveBuffer.toLowerCase();
397
- if (directive === "send" || directive === "next" || directive === "run") item.kind = directive;
398
- else {
399
- item.kind = "unknown";
400
- item.status = "invalid";
401
- this._diagnostic(events, {
402
- code: "invalid-directive",
403
- message: `Unknown directive "■${this._directiveBuffer}". Expected ■send, ■run or ■next.`,
404
- itemId: item.id
405
- });
406
- events.push({
407
- type: "item-start",
408
- item
409
- });
410
- this._completeCurrent(events);
411
- this._state = "skip";
412
- return;
413
- }
414
- if (item.kind === "run") {
415
- events.push({
416
- type: "item-start",
417
- item
418
- });
419
- if (terminator === "=") {
420
- this._diagnostic(events, {
421
- code: "invalid-directive",
422
- message: "■run does not take a name",
423
- itemId: item.id
424
- });
425
- this._state = "header";
426
- } else if (terminator === "\n") this._state = "body-wait";
427
- else if (terminator === "{" || terminator === " ") {
428
- this._state = "header";
429
- if (terminator === "{") {
430
- this._startBody(events);
431
- this._appendBody("{");
432
- }
433
- }
434
- return;
435
- }
436
- if (terminator === "=") {
437
- this._nameBuffer = "";
438
- this._state = "name";
439
- return;
440
- }
441
- item.status = "invalid";
442
- this._diagnostic(events, {
443
- code: "invalid-name",
444
- message: `■${directive} requires a name (e.g. ■${directive}=example)`,
445
- itemId: item.id
446
- });
447
- events.push({
448
- type: "item-start",
449
- item
450
- });
451
- if (terminator === "\n") {
452
- this._state = item.kind === "next" ? "idle" : "body-wait";
453
- if (item.kind === "next") this._completeCurrent(events);
454
- } else if (terminator === "{") this._startProps("{");
455
- else this._state = "header";
456
- }
457
- _endName(events) {
458
- const item = this._current;
459
- const name = this._nameBuffer.toLowerCase();
460
- item.name = name;
461
- if (!NAME_REGEX.test(name)) {
462
- item.status = "invalid";
463
- this._diagnostic(events, {
464
- code: "invalid-name",
465
- message: `Invalid name "${this._nameBuffer}" for ■${item.kind}`,
466
- itemId: item.id
467
- });
468
- }
469
- events.push({
470
- type: "item-start",
471
- item
472
- });
473
- }
474
- _startProps(char) {
475
- this._propsBuffer = char;
476
- this._propsDepth = 1;
477
- this._quoteChar = void 0;
478
- this._escaped = false;
479
- this._propsBroken = false;
480
- this._state = "props";
481
- }
482
- _endProps(events) {
483
- const item = this._current;
484
- const parsed = tryParseJson(this._propsBuffer);
485
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) this._failProps(events, "props are not a valid JSON object");
486
- else {
487
- item.props = parsed;
488
- this._ready(events);
489
- }
490
- if (item.kind === "next") {
491
- this._completeCurrent(events);
492
- this._state = "idle";
493
- } else this._state = "body-wait";
494
- }
495
- /** Attempts to salvage props that were cut off by the end of the stream. */
496
- _endPartialProps(events) {
497
- const item = this._current;
498
- const repaired = tryParseJson(this._propsBuffer);
499
- this._diagnostic(events, {
500
- code: "interrupted",
501
- message: `Props of ■${item.kind} were interrupted by the end of the stream`,
502
- itemId: item.id
503
- });
504
- if (repaired && typeof repaired === "object" && !Array.isArray(repaired)) item.props = repaired;
505
- else {
506
- item.status = "invalid";
507
- this._diagnostic(events, {
508
- code: "invalid-props",
509
- message: `Invalid props for ■${item.kind}: props are not a valid JSON object`,
510
- itemId: item.id
511
- });
512
- }
513
- }
514
- _failProps(events, detail, code = "invalid-props") {
515
- const item = this._current;
516
- item.status = "invalid";
517
- item.props = {};
518
- this._diagnostic(events, {
519
- code,
520
- message: `Invalid props for ■${item.kind}${item.name ? `=${item.name}` : ""}: ${detail}`,
521
- itemId: item.id
522
- });
523
- }
524
- _startBody(events) {
525
- const item = this._current;
526
- this._ready(events);
527
- item.body ??= "";
528
- this._pendingWhitespace = "";
529
- events.push({
530
- type: "body-start",
531
- itemId: item.id
532
- });
533
- this._state = "body";
534
- }
535
- _appendBody(char) {
536
- if (isWhitespace(char)) {
537
- this._pendingWhitespace += char;
538
- return;
539
- }
540
- if (this._pendingWhitespace) {
541
- this._emitBodyText(this._pendingWhitespace);
542
- this._pendingWhitespace = "";
543
- }
544
- this._emitBodyText(char);
545
- }
546
- _emitBodyText(text) {
547
- this._current.body += text;
548
- this._bodyDelta += text;
549
- }
550
- _flushBodyDelta(events) {
551
- if (this._bodyDelta && this._current) events.push({
552
- type: "body-delta",
553
- itemId: this._current.id,
554
- delta: this._bodyDelta
555
- });
556
- this._bodyDelta = "";
557
- }
558
- _ready(events) {
559
- const item = this._current;
560
- if (!item || this._currentReady) return;
561
- this._currentReady = true;
562
- if (item.status !== "invalid") item.status = "streaming";
563
- events.push({
564
- type: "item-ready",
565
- item
566
- });
567
- }
568
- _completeCurrent(events, forcedStatus) {
569
- const item = this._current;
570
- if (!item) return;
571
- this._ready(events);
572
- if (item.status !== "invalid") item.status = forcedStatus ?? "complete";
573
- if (item.kind === "next" && item.status === "complete") this._ended = true;
574
- events.push({
575
- type: "item-complete",
576
- item
577
- });
578
- this._current = void 0;
579
- this._currentReady = false;
580
- }
581
- _skipUnexpectedText(events) {
582
- this._diagnostic(events, {
583
- code: "unexpected-text",
584
- message: this._ended ? "Discarded content after terminal ■next" : "Encountered text outside of a ■ block"
585
- });
586
- this._state = "skip";
587
- }
588
- _diagnostic(events, diagnostic) {
589
- this._diagnostics.push(diagnostic);
590
- if (diagnostic.itemId) {
591
- const item = this._items.find((i) => i.id === diagnostic.itemId);
592
- item === null || item === void 0 || item.diagnostics.push(diagnostic);
593
- }
594
- events.push({
595
- type: "diagnostic",
596
- diagnostic
597
- });
598
- }
599
- };
600
- //#endregion
601
- //#region src/message-stream/response-parser.ts
602
- /** One wire format: a complete ■start … ■end envelope containing valid blocks. */
603
- var ResponseParser = class {
604
- _parser = new StreamingMessageParser();
605
- _mode = "detect";
606
- _discardPrefixLine = false;
607
- _prefixDiagnostics = [];
608
- _prefix = "";
609
- _boundary = "";
610
- _closed = false;
611
- _finished = false;
612
- _failure;
613
- _trailer = "";
614
- get framed() {
615
- return this._mode === "framed";
616
- }
617
- get valid() {
618
- return this._finished && this._closed && !this._failure;
619
- }
620
- get items() {
621
- return this.valid ? this._parser.items : [];
622
- }
623
- get diagnostics() {
624
- return [
625
- ...this._prefixDiagnostics,
626
- ...this._parser.diagnostics,
627
- ...this._trailingDiagnostics,
628
- ...this._failure ? [this._failure] : []
629
- ];
630
- }
631
- push(chunk) {
632
- if (this._finished) throw new Error("Response parser has already finished");
633
- const events = [];
634
- let forward = "";
635
- const flush = () => {
636
- if (forward) events.push(...this._parser.push(forward));
637
- forward = "";
638
- };
639
- for (const char of chunk) {
640
- if (this._mode === "detect") {
641
- if (this._discardPrefixLine) {
642
- if (char === "\n") this._discardPrefixLine = false;
643
- continue;
644
- }
645
- if (!this._prefix && /\s/.test(char)) continue;
646
- this._prefix += char;
647
- const trimmed = this._prefix.trimEnd();
648
- if (char === "\n" && trimmed === "\"\"\"") {
649
- events.push(...this._parser.push(this._prefix));
650
- this._prefix = "";
651
- } else if (char === "\n" && trimmed === "■start") {
652
- this._mode = "framed";
653
- this._prefix = "";
654
- } else if (!"■start".startsWith(this._prefix) && !/^■start[ \t\r]*$/.test(this._prefix) && !/^"{1,3}[ \t\r]*$/.test(this._prefix)) {
655
- if (!this._prefixDiagnostics.length) {
656
- const diagnostic = {
657
- code: "unexpected-text",
658
- message: "Discarded text before ■start"
659
- };
660
- this._prefixDiagnostics.push(diagnostic);
661
- events.push({
662
- type: "diagnostic",
663
- diagnostic
664
- });
665
- }
666
- this._prefix = "";
667
- this._discardPrefixLine = char !== "\n";
668
- }
669
- continue;
670
- }
671
- if (this._closed) {
672
- this._trailer += char;
673
- continue;
674
- }
675
- if (this._boundary || char === "■") {
676
- this._boundary += char;
677
- if (this._boundary === "■end") {
678
- flush();
679
- events.push(...this._parser.finish());
680
- this._boundary = "";
681
- this._closed = true;
682
- } else if (!"■end".startsWith(this._boundary)) {
683
- forward += this._boundary;
684
- this._boundary = "";
685
- }
686
- } else forward += char;
687
- }
688
- flush();
689
- return events;
690
- }
691
- finish(stopReason) {
692
- if (this._finished) return [];
693
- this._finished = true;
694
- const events = [];
695
- if (this._mode === "detect") {
696
- if (this._prefix.trim() === "■start") this._mode = "framed";
697
- else this._fail("Missing ■start: every response must begin with the response envelope");
698
- this._prefix = "";
699
- }
700
- if (this.framed) {
701
- if (!this._boundary && stopReason === "stop") this._closed = true;
702
- else if (this._boundary) events.push(...this._parser.push(this._boundary));
703
- events.push(...this._parser.finish());
704
- const trailer = this._trailer.trim();
705
- if (!this._closed) this._fail("Missing ■end: the response envelope is incomplete");
706
- else if (this._parser.diagnostics.some((d) => d.code !== "example-delimiter")) this._fail("The response envelope contains malformed protocol blocks");
707
- if (!this._failure) this._validateBlocks();
708
- if (trailer === "\"\"\"") {
709
- const parser = new StreamingMessageParser();
710
- events.push(...parser.push(trailer), ...parser.finish());
711
- this._trailingDiagnostics = parser.diagnostics;
712
- } else if (trailer) {
713
- const diagnostic = {
714
- code: "unexpected-text",
715
- message: "Discarded content after ■end"
716
- };
717
- this._trailingDiagnostics.push(diagnostic);
718
- events.push({
719
- type: "diagnostic",
720
- diagnostic
721
- });
722
- }
723
- if (this._failure) events.push({
724
- type: "diagnostic",
725
- diagnostic: this._failure
726
- });
727
- } else if (this._failure) events.push({
728
- type: "diagnostic",
729
- diagnostic: this._failure
730
- });
731
- return events;
732
- }
733
- _trailingDiagnostics = [];
734
- _validateBlocks() {
735
- const items = this._parser.items;
736
- const runs = items.filter((item) => item.kind === "run");
737
- const next = items.findIndex((item) => item.kind === "next");
738
- const run = items.findIndex((item) => item.kind === "run");
739
- if (runs.length > 1 || run >= 0 && items.slice(run + 1).some((item) => item.kind === "send")) this._fail("Use at most one ■run, after all messages; never send a message after code");
740
- else if (run >= 0 && hasTopLevelReturn(items[run].body ?? "")) {
741
- if (run !== items.length - 1) this._fail("After code returning a result, write ■end and wait for the result");
742
- } else if (next < 0 && run < 0) this._fail("Finish with ■next, or return a result from ■run for the next iteration");
743
- }
744
- _fail(message) {
745
- this._failure = {
746
- code: "invalid-envelope",
747
- message
748
- };
749
- }
750
- };
751
- //#endregion
752
- //#region src/prompts/common.ts
753
- var common_exports = /* @__PURE__ */ __exportAll({
754
- parseAssistantResponse: () => parseAssistantResponse,
755
- replacePlaceholders: () => replacePlaceholders,
756
- toParsedAssistantResponse: () => toParsedAssistantResponse
757
- });
758
- /** Builds a {@link ParsedAssistantResponse} from parsed protocol items. */
759
- const toParsedAssistantResponse = (items, raw, diagnostics = []) => {
760
- var _run$body;
761
- const runIndex = items.findIndex((item) => item.kind === "run");
762
- const run = items[runIndex];
763
- const awaitsResult = !!(run === null || run === void 0 ? void 0 : run.body) && hasTopLevelReturn(run.body);
764
- const prematureSends = awaitsResult ? items.slice(runIndex + 1).filter((item) => item.kind === "send") : [];
765
- const sends = items.filter((item, index) => item.kind === "send" && (!awaitsResult || index < runIndex)).map((item) => ({
766
- name: item.name,
767
- props: item.props,
768
- body: item.body
769
- }));
770
- const next = items.find((item) => item.kind === "next" && item.status !== "invalid");
771
- return {
772
- raw,
773
- items,
774
- diagnostics: [...diagnostics, ...prematureSends.map((item) => ({
775
- code: "send-after-run",
776
- itemId: item.id,
777
- message: "Discarded a message generated after code returning a result, before that result was available to the model."
778
- }))],
779
- sends,
780
- code: (run === null || run === void 0 || (_run$body = run.body) === null || _run$body === void 0 ? void 0 : _run$body.trim()) || void 0,
781
- next: next ? {
782
- name: next.name,
783
- props: next.props
784
- } : void 0
785
- };
786
- };
787
- const parseAssistantResponse = (response, stopReason) => {
788
- const parser = new ResponseParser();
789
- parser.push(response);
790
- parser.finish(stopReason);
791
- return toParsedAssistantResponse(parser.items, response, parser.diagnostics);
792
- };
793
- const replacePlaceholders = (prompt, values) => {
794
- const regex = /* @__PURE__ */ new RegExp("■■■([A-Z0-9_\\.-]+)■■■", "gi");
795
- const obj = Object.assign({}, values);
796
- const replaced = prompt.replace(regex, (_match, name) => {
797
- if (name in values) {
798
- delete obj[name];
799
- return typeof values[name] === "string" ? values[name] : JSON.stringify(values[name]);
800
- } else throw new Error(`Placeholder not found: ${name}`);
801
- });
802
- const remaining = Object.keys(obj).filter((key) => key !== "is_message_enabled" && key !== "exits" && key !== "components" && key !== "transcript");
803
- if (remaining.length) throw new Error(`Missing placeholders: ${remaining.join(", ")}`);
804
- return replaced.replace(/\n{3,}/g, "\n\n").trim();
805
- };
806
- //#endregion
807
- export { ResponseParser as a, MARKER as c, toParsedAssistantResponse as i, NAME_REGEX as l, parseAssistantResponse as n, StreamingMessageParser as o, replacePlaceholders as r, tryParseJson as s, common_exports as t };