llmz 0.6.0 → 0.6.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 (110) hide show
  1. package/DOCS.md +1 -3
  2. package/dist/_baseForOwn-CFhWe_W6.js +48 -0
  3. package/dist/_baseForOwn-buOZDFbK.cjs +103 -0
  4. package/dist/_baseIteratee-CF4qP2a8.cjs +2377 -0
  5. package/dist/_baseIteratee-P_firB7b.js +2138 -0
  6. package/dist/_getPrototype-CG7Dxrrz.js +6 -0
  7. package/dist/_getPrototype-DQDlNVVM.cjs +10 -0
  8. package/dist/_overArg-C5Hy7oCi.cjs +196 -0
  9. package/dist/_overArg-CRYbj1NH.js +155 -0
  10. package/dist/citations.d.ts +11 -11
  11. package/dist/component-BwixNVH8.cjs +73 -0
  12. package/dist/component-DGG7d_Mz.js +44 -0
  13. package/dist/context.d.ts +8 -8
  14. package/dist/custom-client-BPmEoWKu.cjs +2618 -0
  15. package/dist/custom-client-NUoxrFQh.js +2547 -0
  16. package/dist/dual-modes-D_p-jedw.js +2750 -0
  17. package/dist/dual-modes-wAOAlaBS.cjs +2840 -0
  18. package/dist/errors-C962uJdS.cjs +220 -0
  19. package/dist/errors-Cg1G0Awu.js +161 -0
  20. package/dist/execute-BEaq3jkV.cjs +1003 -0
  21. package/dist/execute-DYvFRs54.js +1001 -0
  22. package/dist/exit-BGi0Kk0y.cjs +428 -0
  23. package/dist/exit-CDdxDBRT.js +417 -0
  24. package/dist/index.cjs +1786 -1489
  25. package/dist/index.d.ts +10 -9
  26. package/dist/index.js +1721 -1479
  27. package/dist/index.workerd.js +17534 -16875
  28. package/dist/isEmpty-BhJRCqIF.js +52 -0
  29. package/dist/isEmpty-LY533Wmh.cjs +57 -0
  30. package/dist/isPlainObject-CSxXVKCk.cjs +56 -0
  31. package/dist/isPlainObject-D39AzPO2.js +51 -0
  32. package/dist/jsx-BWvT6ukg.cjs +75 -0
  33. package/dist/jsx-CIgdGGVs.js +52 -0
  34. package/dist/jsx.d.ts +2 -2
  35. package/dist/prompts/chat-mode/system.md.d.ts +1 -1
  36. package/dist/prompts/chat-mode/user.md.d.ts +1 -1
  37. package/dist/prompts/worker-mode/system.md.d.ts +1 -1
  38. package/dist/prompts/worker-mode/user.md.d.ts +1 -1
  39. package/dist/quickjs-variant-BYhMBpwU.js +45 -0
  40. package/dist/quickjs-variant-CsIwTVFC.cjs +56 -0
  41. package/dist/quickjs-variant.workerd.d.ts +1 -1
  42. package/dist/result.d.ts +12 -12
  43. package/dist/rolldown-runtime-EAsCxrDo.js +14 -0
  44. package/dist/runtime/interpret-result.d.ts +1 -1
  45. package/dist/snapshots.d.ts +5 -5
  46. package/dist/stack-traces-BIE_5Xwf.js +21 -0
  47. package/dist/stack-traces-ntw_qhoy.cjs +26 -0
  48. package/dist/tool-CAo-BQHs.js +643 -0
  49. package/dist/tool-DdPZb0JW.cjs +654 -0
  50. package/dist/tool.d.ts +3 -3
  51. package/dist/truncator-B6aaJaN1.cjs +276 -0
  52. package/dist/truncator-DZqEqnpD.js +253 -0
  53. package/dist/typings-BwAz_TM9.cjs +784 -0
  54. package/dist/typings-CrBa7Tyy.js +743 -0
  55. package/dist/uniq-BYQ7GWwb.js +183 -0
  56. package/dist/uniq-G872RWmE.cjs +200 -0
  57. package/dist/utils-UBeAFeEI.js +950 -0
  58. package/dist/utils-ZpJm00ed.cjs +1075 -0
  59. package/dist/vm/errors.d.ts +2 -2
  60. package/dist/vm-BjkJqSQH.cjs +3438 -0
  61. package/dist/vm-C5vf1LH3.js +3436 -0
  62. package/package.json +7 -7
  63. package/dist/chunk-34GJUS6U.js +0 -3266
  64. package/dist/chunk-3UZKLXAD.cjs +0 -3249
  65. package/dist/chunk-4POZJQXN.js +0 -1530
  66. package/dist/chunk-5WBNTV42.cjs +0 -1530
  67. package/dist/chunk-5ZF6NTYI.cjs +0 -33
  68. package/dist/chunk-72ZWBNZV.js +0 -173
  69. package/dist/chunk-DDSJTR6Q.cjs +0 -257
  70. package/dist/chunk-GDJTKI7J.js +0 -792
  71. package/dist/chunk-GLXADNTR.js +0 -199
  72. package/dist/chunk-GOWPACSK.cjs +0 -199
  73. package/dist/chunk-HB2G32AK.js +0 -257
  74. package/dist/chunk-HC2QZTVT.cjs +0 -3266
  75. package/dist/chunk-HEAQAEFZ.cjs +0 -173
  76. package/dist/chunk-HMMM32RG.cjs +0 -792
  77. package/dist/chunk-I56MPOVG.cjs +0 -62
  78. package/dist/chunk-KIN7Y247.cjs +0 -32
  79. package/dist/chunk-KTDBAJBB.cjs +0 -1685
  80. package/dist/chunk-KWQTYXAT.js +0 -62
  81. package/dist/chunk-LOX7GOR6.js +0 -1685
  82. package/dist/chunk-PK72FAKD.cjs +0 -175
  83. package/dist/chunk-PZE6PIPG.cjs +0 -621
  84. package/dist/chunk-RGUG3UMF.cjs +0 -26
  85. package/dist/chunk-TDALRNGL.js +0 -621
  86. package/dist/chunk-UGHFNCPD.js +0 -26
  87. package/dist/chunk-WSVDMGMR.js +0 -175
  88. package/dist/chunk-XTEHT2TK.js +0 -33
  89. package/dist/chunk-YEAWWJSJ.js +0 -32
  90. package/dist/chunk-YRS7KRKP.js +0 -3249
  91. package/dist/component-2NYFY6CW.cjs +0 -14
  92. package/dist/component-ASNZHQUR.js +0 -14
  93. package/dist/dual-modes-S5TJ5JVJ.js +0 -10
  94. package/dist/dual-modes-TCMWBZIH.cjs +0 -10
  95. package/dist/execute-FF7D46TY.js +0 -1109
  96. package/dist/execute-IK6O4SU2.cjs +0 -1109
  97. package/dist/exit-MX56VN2C.cjs +0 -8
  98. package/dist/exit-UQ3EUX5D.js +0 -8
  99. package/dist/jsx-DDZ63UUF.js +0 -13
  100. package/dist/jsx-ULPWX5TW.cjs +0 -13
  101. package/dist/tool-KD7FL45V.js +0 -11
  102. package/dist/tool-VE2YHIY2.cjs +0 -11
  103. package/dist/truncator-73RCL52Y.js +0 -12
  104. package/dist/truncator-FZNLYSDL.cjs +0 -12
  105. package/dist/typings-E2RROCLT.js +0 -10
  106. package/dist/typings-LQOBZXSO.cjs +0 -10
  107. package/dist/utils-IATCUNKF.js +0 -43
  108. package/dist/utils-ITB4NEL7.cjs +0 -43
  109. package/dist/vm-6D3IIIU7.cjs +0 -12
  110. package/dist/vm-QO5FSGPN.js +0 -12
@@ -1,199 +0,0 @@
1
- import {
2
- camelCase_default,
3
- deburr_default,
4
- isPlainObject_default,
5
- pickBy_default,
6
- startCase_default
7
- } from "./chunk-34GJUS6U.js";
8
-
9
- // src/utils.ts
10
- import { getWasmTokenizer } from "@bpinternal/thicktoken";
11
- import { z, transforms } from "@bpinternal/zui";
12
- var tokenizer = null;
13
- var configureTokenizer = (customTokenizer) => {
14
- tokenizer = customTokenizer;
15
- };
16
- var init = async () => {
17
- if (tokenizer) {
18
- return;
19
- }
20
- while (typeof getWasmTokenizer !== "function") {
21
- await new Promise((resolve) => setTimeout(resolve, 50));
22
- }
23
- tokenizer = await getWasmTokenizer();
24
- };
25
- var getTokenizer = () => {
26
- if (!tokenizer) {
27
- throw new Error("Tokenizer not initialized, make sure to call init() first and await it");
28
- }
29
- return tokenizer;
30
- };
31
- var Tokens = (min, max) => {
32
- return z.string().superRefine((value, ctx) => {
33
- const tokens = tokenizer.count(value);
34
- if (value.length < min) {
35
- ctx.addIssue({
36
- code: "too_small",
37
- minimum: min,
38
- type: "string",
39
- message: `Text is ${tokens} but expected at least ${min} tokens`,
40
- inclusive: true
41
- });
42
- }
43
- if (value.length > max) {
44
- ctx.addIssue({
45
- code: "too_big",
46
- maximum: max,
47
- type: "string",
48
- message: `Text is ${tokens} tokens but expected at most ${max} tokens`,
49
- inclusive: true
50
- });
51
- }
52
- return value;
53
- });
54
- };
55
- var Identifier = z.string().min(1).max(100).regex(
56
- /^([a-zA-Z_][a-zA-Z0-9_]*)$/,
57
- "Identifiers must start with a letter and can only contain letters, numbers and underscores"
58
- );
59
- function escapeString(str) {
60
- if (typeof str !== "string") {
61
- return "";
62
- }
63
- const rawStr = String.raw`${str}`;
64
- const escapedStr = rawStr.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/"/g, '\\"').replace(/'/g, "\\'");
65
- if (escapedStr.includes("`")) {
66
- return `"${escapedStr}"`;
67
- } else if (escapedStr.includes("'")) {
68
- return `"${escapedStr}"`;
69
- } else {
70
- return `'${escapedStr}'`;
71
- }
72
- }
73
- var toPropertyKey = (key) => {
74
- if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key)) {
75
- return key;
76
- }
77
- return escapeString(key);
78
- };
79
- var trimEmptyLines = (lines) => {
80
- var _a;
81
- while (lines.length && !((_a = lines[0]) == null ? void 0 : _a.trim())) {
82
- lines.shift();
83
- }
84
- while (lines.length && !lines[lines.length - 1].trim()) {
85
- lines.pop();
86
- }
87
- };
88
- var getMultilineComment = (description) => {
89
- description = description == null ? void 0 : description.replace(/(\n(\s*)?){3,}/g, "\n\n");
90
- const ensureLineStartsWithAsterisk = (line) => line.startsWith("* ") ? ` ${line}` : ` * ${line}`;
91
- const escapeCommentEnd = (line) => line.replace(/\*\//g, "*\\/");
92
- const descLines = (description ?? "").split("\n").map((line) => line.trim());
93
- trimEmptyLines(descLines);
94
- if (descLines.length) {
95
- descLines[0] = descLines[0].replace(/^\/\*\*?/, "");
96
- descLines[descLines.length - 1] = descLines[descLines.length - 1].replace(/\*\/$/, "");
97
- }
98
- trimEmptyLines(descLines);
99
- return descLines.length === 0 ? "" : descLines.length === 1 ? `/** ${escapeCommentEnd(descLines[0])} */` : `/**
100
- ${descLines.map(ensureLineStartsWithAsterisk).map(escapeCommentEnd).join("\n")}
101
- */`;
102
- };
103
- var toValidFunctionName = (str) => {
104
- let name = deburr_default(str);
105
- name = name.replace(/[^a-zA-Z0-9_$]/g, "");
106
- if (!/^[a-zA-Z_$]/.test(name)) {
107
- name = `_${name}`;
108
- }
109
- return camelCase_default(name);
110
- };
111
- var awaitObject = async (obj) => {
112
- const newObj = { ...obj };
113
- const promises = [];
114
- const setProp = async (key, value) => {
115
- newObj[key] = await value;
116
- };
117
- for (const [key, value] of Object.entries(obj)) {
118
- if (value instanceof Promise) {
119
- promises.push(setProp(key, value));
120
- } else if (isPlainObject_default(value)) {
121
- promises.push(setProp(key, await awaitObject(value)));
122
- }
123
- }
124
- await Promise.all(promises);
125
- return newObj;
126
- };
127
- var toValidObjectName = (str) => {
128
- let name = deburr_default(str);
129
- name = name.split(/[^a-zA-Z0-9_$]/g).map(startCase_default).join("");
130
- if (!/^[a-zA-Z_$]/.test(name)) {
131
- name = `_${name}`;
132
- }
133
- return name.replaceAll(" ", "");
134
- };
135
- var stripInvalidIdentifiers = (object) => {
136
- if (typeof object !== "object") {
137
- return object;
138
- }
139
- if (Array.isArray(object)) {
140
- return object.map(stripInvalidIdentifiers);
141
- }
142
- return pickBy_default(object, (__, key) => Identifier.safeParse(key).success);
143
- };
144
- var fromJSONSchemaCompat = (schema) => {
145
- try {
146
- return transforms.fromJSONSchemaLegacy(schema);
147
- } catch {
148
- return transforms.fromJSONSchema(schema);
149
- }
150
- };
151
- var isValidSchema = (schema) => {
152
- try {
153
- fromJSONSchemaCompat(schema);
154
- return typeof schema.type === "string";
155
- } catch {
156
- return false;
157
- }
158
- };
159
- function isZuiSchema(schema) {
160
- return !!schema && typeof schema === "object" && schema.__type__ === "ZuiType";
161
- }
162
- function isJsonSchema(schema) {
163
- return !!schema && typeof schema === "object" && ("$schema" in schema || "type" in schema || "properties" in schema);
164
- }
165
- function isValidIdentifier(name) {
166
- if (typeof name !== "string") {
167
- return false;
168
- }
169
- return /^[A-Z]{1,}[A-Z0-9_]{0,50}$/i.test(name);
170
- }
171
- function isValidMessageName(name) {
172
- if (typeof name !== "string") {
173
- return false;
174
- }
175
- return /^[A-Z]{1,50}$/i.test(name);
176
- }
177
- var getErrorMessage = (err) => err instanceof Error ? err.message : JSON.stringify(err);
178
-
179
- export {
180
- configureTokenizer,
181
- init,
182
- getTokenizer,
183
- Tokens,
184
- Identifier,
185
- escapeString,
186
- toPropertyKey,
187
- getMultilineComment,
188
- toValidFunctionName,
189
- awaitObject,
190
- toValidObjectName,
191
- stripInvalidIdentifiers,
192
- fromJSONSchemaCompat,
193
- isValidSchema,
194
- isZuiSchema,
195
- isJsonSchema,
196
- isValidIdentifier,
197
- isValidMessageName,
198
- getErrorMessage
199
- };
@@ -1,199 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
-
3
-
4
-
5
-
6
-
7
- var _chunkHC2QZTVTcjs = require('./chunk-HC2QZTVT.cjs');
8
-
9
- // src/utils.ts
10
- var _thicktoken = require('@bpinternal/thicktoken');
11
- var _zui = require('@bpinternal/zui');
12
- var tokenizer = null;
13
- var configureTokenizer = (customTokenizer) => {
14
- tokenizer = customTokenizer;
15
- };
16
- var init = async () => {
17
- if (tokenizer) {
18
- return;
19
- }
20
- while (typeof _thicktoken.getWasmTokenizer !== "function") {
21
- await new Promise((resolve) => setTimeout(resolve, 50));
22
- }
23
- tokenizer = await _thicktoken.getWasmTokenizer.call(void 0, );
24
- };
25
- var getTokenizer = () => {
26
- if (!tokenizer) {
27
- throw new Error("Tokenizer not initialized, make sure to call init() first and await it");
28
- }
29
- return tokenizer;
30
- };
31
- var Tokens = (min, max) => {
32
- return _zui.z.string().superRefine((value, ctx) => {
33
- const tokens = tokenizer.count(value);
34
- if (value.length < min) {
35
- ctx.addIssue({
36
- code: "too_small",
37
- minimum: min,
38
- type: "string",
39
- message: `Text is ${tokens} but expected at least ${min} tokens`,
40
- inclusive: true
41
- });
42
- }
43
- if (value.length > max) {
44
- ctx.addIssue({
45
- code: "too_big",
46
- maximum: max,
47
- type: "string",
48
- message: `Text is ${tokens} tokens but expected at most ${max} tokens`,
49
- inclusive: true
50
- });
51
- }
52
- return value;
53
- });
54
- };
55
- var Identifier = _zui.z.string().min(1).max(100).regex(
56
- /^([a-zA-Z_][a-zA-Z0-9_]*)$/,
57
- "Identifiers must start with a letter and can only contain letters, numbers and underscores"
58
- );
59
- function escapeString(str) {
60
- if (typeof str !== "string") {
61
- return "";
62
- }
63
- const rawStr = String.raw`${str}`;
64
- const escapedStr = rawStr.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/"/g, '\\"').replace(/'/g, "\\'");
65
- if (escapedStr.includes("`")) {
66
- return `"${escapedStr}"`;
67
- } else if (escapedStr.includes("'")) {
68
- return `"${escapedStr}"`;
69
- } else {
70
- return `'${escapedStr}'`;
71
- }
72
- }
73
- var toPropertyKey = (key) => {
74
- if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key)) {
75
- return key;
76
- }
77
- return escapeString(key);
78
- };
79
- var trimEmptyLines = (lines) => {
80
- var _a;
81
- while (lines.length && !((_a = lines[0]) == null ? void 0 : _a.trim())) {
82
- lines.shift();
83
- }
84
- while (lines.length && !lines[lines.length - 1].trim()) {
85
- lines.pop();
86
- }
87
- };
88
- var getMultilineComment = (description) => {
89
- description = description == null ? void 0 : description.replace(/(\n(\s*)?){3,}/g, "\n\n");
90
- const ensureLineStartsWithAsterisk = (line) => line.startsWith("* ") ? ` ${line}` : ` * ${line}`;
91
- const escapeCommentEnd = (line) => line.replace(/\*\//g, "*\\/");
92
- const descLines = (_nullishCoalesce(description, () => ( ""))).split("\n").map((line) => line.trim());
93
- trimEmptyLines(descLines);
94
- if (descLines.length) {
95
- descLines[0] = descLines[0].replace(/^\/\*\*?/, "");
96
- descLines[descLines.length - 1] = descLines[descLines.length - 1].replace(/\*\/$/, "");
97
- }
98
- trimEmptyLines(descLines);
99
- return descLines.length === 0 ? "" : descLines.length === 1 ? `/** ${escapeCommentEnd(descLines[0])} */` : `/**
100
- ${descLines.map(ensureLineStartsWithAsterisk).map(escapeCommentEnd).join("\n")}
101
- */`;
102
- };
103
- var toValidFunctionName = (str) => {
104
- let name = _chunkHC2QZTVTcjs.deburr_default.call(void 0, str);
105
- name = name.replace(/[^a-zA-Z0-9_$]/g, "");
106
- if (!/^[a-zA-Z_$]/.test(name)) {
107
- name = `_${name}`;
108
- }
109
- return _chunkHC2QZTVTcjs.camelCase_default.call(void 0, name);
110
- };
111
- var awaitObject = async (obj) => {
112
- const newObj = { ...obj };
113
- const promises = [];
114
- const setProp = async (key, value) => {
115
- newObj[key] = await value;
116
- };
117
- for (const [key, value] of Object.entries(obj)) {
118
- if (value instanceof Promise) {
119
- promises.push(setProp(key, value));
120
- } else if (_chunkHC2QZTVTcjs.isPlainObject_default.call(void 0, value)) {
121
- promises.push(setProp(key, await awaitObject(value)));
122
- }
123
- }
124
- await Promise.all(promises);
125
- return newObj;
126
- };
127
- var toValidObjectName = (str) => {
128
- let name = _chunkHC2QZTVTcjs.deburr_default.call(void 0, str);
129
- name = name.split(/[^a-zA-Z0-9_$]/g).map(_chunkHC2QZTVTcjs.startCase_default).join("");
130
- if (!/^[a-zA-Z_$]/.test(name)) {
131
- name = `_${name}`;
132
- }
133
- return name.replaceAll(" ", "");
134
- };
135
- var stripInvalidIdentifiers = (object) => {
136
- if (typeof object !== "object") {
137
- return object;
138
- }
139
- if (Array.isArray(object)) {
140
- return object.map(stripInvalidIdentifiers);
141
- }
142
- return _chunkHC2QZTVTcjs.pickBy_default.call(void 0, object, (__, key) => Identifier.safeParse(key).success);
143
- };
144
- var fromJSONSchemaCompat = (schema) => {
145
- try {
146
- return _zui.transforms.fromJSONSchemaLegacy(schema);
147
- } catch (e) {
148
- return _zui.transforms.fromJSONSchema(schema);
149
- }
150
- };
151
- var isValidSchema = (schema) => {
152
- try {
153
- fromJSONSchemaCompat(schema);
154
- return typeof schema.type === "string";
155
- } catch (e2) {
156
- return false;
157
- }
158
- };
159
- function isZuiSchema(schema) {
160
- return !!schema && typeof schema === "object" && schema.__type__ === "ZuiType";
161
- }
162
- function isJsonSchema(schema) {
163
- return !!schema && typeof schema === "object" && ("$schema" in schema || "type" in schema || "properties" in schema);
164
- }
165
- function isValidIdentifier(name) {
166
- if (typeof name !== "string") {
167
- return false;
168
- }
169
- return /^[A-Z]{1,}[A-Z0-9_]{0,50}$/i.test(name);
170
- }
171
- function isValidMessageName(name) {
172
- if (typeof name !== "string") {
173
- return false;
174
- }
175
- return /^[A-Z]{1,50}$/i.test(name);
176
- }
177
- var getErrorMessage = (err) => err instanceof Error ? err.message : JSON.stringify(err);
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
- exports.configureTokenizer = configureTokenizer; exports.init = init; exports.getTokenizer = getTokenizer; exports.Tokens = Tokens; exports.Identifier = Identifier; exports.escapeString = escapeString; exports.toPropertyKey = toPropertyKey; exports.getMultilineComment = getMultilineComment; exports.toValidFunctionName = toValidFunctionName; exports.awaitObject = awaitObject; exports.toValidObjectName = toValidObjectName; exports.stripInvalidIdentifiers = stripInvalidIdentifiers; exports.fromJSONSchemaCompat = fromJSONSchemaCompat; exports.isValidSchema = isValidSchema; exports.isZuiSchema = isZuiSchema; exports.isJsonSchema = isJsonSchema; exports.isValidIdentifier = isValidIdentifier; exports.isValidMessageName = isValidMessageName; exports.getErrorMessage = getErrorMessage;
@@ -1,257 +0,0 @@
1
- import {
2
- fromJSONSchemaCompat,
3
- isJsonSchema,
4
- isValidIdentifier,
5
- isZuiSchema
6
- } from "./chunk-GLXADNTR.js";
7
- import {
8
- uniq_default
9
- } from "./chunk-34GJUS6U.js";
10
-
11
- // src/exit.ts
12
- import { transforms } from "@bpinternal/zui";
13
- var Exit = class _Exit {
14
- /** The primary name of the exit (used in return statements) */
15
- name;
16
- /** Alternative names that can be used to reference this exit */
17
- aliases = [];
18
- /** Human-readable description of when this exit should be used */
19
- description;
20
- /** Additional metadata for orchestration and custom logic */
21
- metadata;
22
- /** JSON Schema for validating exit result data */
23
- schema;
24
- /**
25
- * Returns the Zod schema equivalent of the JSON schema (if available).
26
- * Used internally for validation and type inference.
27
- */
28
- get zSchema() {
29
- return this.schema ? fromJSONSchemaCompat(this.schema) : void 0;
30
- }
31
- /**
32
- * Renames the exit and updates aliases accordingly.
33
- *
34
- * @param name - The new name for the exit (must be a valid identifier)
35
- * @returns This exit instance for chaining
36
- *
37
- * @example
38
- * ```typescript
39
- * const exit = new Exit({ name: 'old_name', description: 'Test exit' })
40
- * exit.rename('new_name')
41
- * console.log(exit.name) // 'new_name'
42
- * ```
43
- */
44
- rename(name) {
45
- const before = this.name;
46
- if (!isValidIdentifier(name)) {
47
- throw new Error(
48
- `Invalid name for exit ${name}. An exit name must start with a letter and contain only letters, numbers, and underscores. It must be 1-50 characters long.`
49
- );
50
- }
51
- this.name = name;
52
- this.aliases = uniq_default([name, ...this.aliases.map((alias) => alias === before ? name : alias)]);
53
- return this;
54
- }
55
- /**
56
- * Creates a deep copy of this exit.
57
- *
58
- * The clone is completely independent and can be modified without affecting
59
- * the original exit. This is useful for creating variations of existing exits.
60
- *
61
- * @returns A new Exit instance with the same configuration
62
- *
63
- * @example
64
- * ```typescript
65
- * const originalExit = new Exit({
66
- * name: 'base',
67
- * description: 'Base exit',
68
- * schema: z.object({ status: z.string() }),
69
- * })
70
- *
71
- * const customExit = originalExit.clone().rename('custom')
72
- * // customExit is independent of originalExit
73
- * ```
74
- */
75
- clone() {
76
- return new _Exit({
77
- name: this.name,
78
- aliases: [...this.aliases],
79
- description: this.description,
80
- metadata: JSON.parse(JSON.stringify(this.metadata)),
81
- schema: this.zSchema
82
- });
83
- }
84
- /**
85
- * Type guard to check if this exit matches another exit by name.
86
- *
87
- * Used internally for type narrowing and exit comparison.
88
- *
89
- * @param exit - The exit to compare against
90
- * @returns True if the exits have the same name
91
- */
92
- is(exit) {
93
- return this.name === exit.name;
94
- }
95
- /**
96
- * Type guard to check if an ExitResult matches this exit.
97
- *
98
- * @param result - The exit result to check
99
- * @returns True if the result was created by this exit
100
- */
101
- match(result) {
102
- return result.exit instanceof _Exit && this.name === result.exit.name;
103
- }
104
- /**
105
- * Serializes this exit to a JSON-compatible object.
106
- *
107
- * @returns JSON representation of the exit
108
- *
109
- * @example
110
- * ```typescript
111
- * const exit = new Exit({
112
- * name: 'complete',
113
- * description: 'Task completed successfully',
114
- * })
115
- *
116
- * console.log(exit.toJSON())
117
- * // { name: 'complete', aliases: [], description: 'Task completed successfully', metadata: {}, schema: undefined }
118
- * ```
119
- */
120
- toJSON() {
121
- return {
122
- name: this.name,
123
- aliases: [...this.aliases],
124
- description: this.description,
125
- metadata: { ...this.metadata },
126
- schema: this.schema
127
- };
128
- }
129
- /**
130
- * Creates a new Exit instance.
131
- *
132
- * @param props - Exit configuration
133
- * @param props.name - Primary name for the exit (must be valid identifier)
134
- * @param props.description - Human-readable description of the exit's purpose
135
- * @param props.aliases - Alternative names that can be used to reference this exit
136
- * @param props.metadata - Additional data for orchestration and custom logic
137
- * @param props.schema - Zod schema for validating exit result data
138
- *
139
- * @example
140
- * ```typescript
141
- * // Simple exit without data validation
142
- * const exit = new Exit({
143
- * name: 'complete',
144
- * description: 'Task completed successfully',
145
- * })
146
- * ```
147
- *
148
- * @example
149
- * ```typescript
150
- * // Exit with typed result data
151
- * const approval = new Exit({
152
- * name: 'approved',
153
- * description: 'Request approved by system',
154
- * schema: z.object({
155
- * amount: z.number().positive(),
156
- * reference: z.string().min(1),
157
- * timestamp: z.date().default(() => new Date()),
158
- * }),
159
- * })
160
- * ```
161
- *
162
- * @example
163
- * ```typescript
164
- * // Exit with aliases and metadata
165
- * const handoff = new Exit({
166
- * name: 'handoff_support',
167
- * aliases: ['escalate', 'transfer'],
168
- * description: 'Transfer to human support agent',
169
- * metadata: {
170
- * department: 'customer_service',
171
- * priority: 'high',
172
- * },
173
- * schema: z.object({
174
- * reason: z.string(),
175
- * customerData: z.record(z.any()),
176
- * }),
177
- * })
178
- * ```
179
- */
180
- constructor(props) {
181
- if (!isValidIdentifier(props.name)) {
182
- throw new Error(
183
- `Invalid name for exit ${props.name}. A exit name must start with a letter and contain only letters, numbers, and underscores. It must be 1-50 characters long.`
184
- );
185
- }
186
- if (typeof props.description !== "string" || props.description.trim().length === 0) {
187
- throw new Error(
188
- `Invalid description for exit ${props.name}. Expected a non-empty string, but got type "${typeof props.description}"`
189
- );
190
- }
191
- if (props.metadata !== void 0 && typeof props.metadata !== "object") {
192
- throw new Error(
193
- `Invalid metadata for exit ${props.name}. Expected an object, but got type "${typeof props.metadata}"`
194
- );
195
- }
196
- if (props.aliases !== void 0 && !Array.isArray(props.aliases)) {
197
- throw new Error(
198
- `Invalid aliases for exit ${props.name}. Expected an array, but got type "${typeof props.aliases}"`
199
- );
200
- }
201
- if (props.aliases && props.aliases.some((alias) => !isValidIdentifier(alias))) {
202
- throw new Error(`Invalid aliases for exit ${props.name}. Expected an array of valid identifiers.`);
203
- }
204
- if (typeof props.schema !== "undefined") {
205
- if (isZuiSchema(props.schema)) {
206
- this.schema = transforms.toJSONSchemaLegacy(props.schema);
207
- } else if (isJsonSchema(props.schema)) {
208
- this.schema = props.schema;
209
- } else {
210
- throw new Error(
211
- `Invalid input schema for exit ${props.name}. Expected a ZodType or JSONSchema, but got type "${typeof props.schema}"`
212
- );
213
- }
214
- }
215
- this.name = props.name;
216
- this.aliases = uniq_default([props.name, ...props.aliases ?? []]);
217
- this.description = props.description;
218
- this.metadata = props.metadata ?? {};
219
- }
220
- /**
221
- * Ensures all exits in an array have unique names by renaming duplicates.
222
- *
223
- * When multiple exits have the same name, this method appends numbers to
224
- * create unique names (e.g., 'exit1', 'exit2'). This prevents naming conflicts
225
- * in execution contexts with multiple exits.
226
- *
227
- * @param exits - Array of exits that may have duplicate names
228
- * @returns Array of exits with guaranteed unique names
229
- *
230
- * @example
231
- * ```typescript
232
- * const exit1 = new Exit({ name: 'done', description: 'First done' })
233
- * const exit2 = new Exit({ name: 'done', description: 'Second done' })
234
- *
235
- * const uniqueExits = Exit.withUniqueNames([exit1, exit2])
236
- * // Result: [{ name: 'done' }, { name: 'done1' }]
237
- * ```
238
- */
239
- static withUniqueNames = (exits) => {
240
- const names = /* @__PURE__ */ new Set();
241
- return exits.map((exit) => {
242
- if (exits.filter((t) => t.name === exit.name).length === 1) {
243
- return exit;
244
- }
245
- let counter = 1;
246
- let exitName = exit.name + counter;
247
- while (names.has(exitName)) {
248
- exitName = `${exit.name}${++counter}`;
249
- }
250
- return exit.rename(exitName);
251
- });
252
- };
253
- };
254
-
255
- export {
256
- Exit
257
- };