llmz 0.0.11 → 0.0.12
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.
- package/dist/chat.d.ts +17 -0
- package/dist/chunk-276Q6EWP.cjs +224 -0
- package/dist/chunk-4L6D2A6O.cjs +283 -0
- package/dist/chunk-4MNIJGK6.js +224 -0
- package/dist/chunk-7WRN4E42.js +3087 -0
- package/dist/chunk-BEPRLBPK.cjs +3123 -0
- package/dist/chunk-C6WNNTEV.cjs +212 -0
- package/dist/chunk-D3ESDRLH.js +3123 -0
- package/dist/chunk-GGWM6X2K.js +184 -0
- package/dist/chunk-GWFYZDUR.cjs +105 -0
- package/dist/chunk-HJKOSEH2.cjs +722 -0
- package/dist/chunk-IH2WQFO5.js +283 -0
- package/dist/chunk-IKSIOIIP.cjs +28 -0
- package/dist/chunk-JAGB2AOU.js +212 -0
- package/dist/chunk-JDABP4SD.cjs +161 -0
- package/dist/chunk-JGVAZO4X.cjs +152 -0
- package/dist/chunk-JKVVQN2P.js +161 -0
- package/dist/chunk-JMSZKB4T.js +105 -0
- package/dist/chunk-JQBT7UWN.js +28 -0
- package/dist/chunk-KH6JQYQA.js +796 -0
- package/dist/chunk-KMZDFWYZ.cjs +33 -0
- package/dist/chunk-ORQP26SZ.js +33 -0
- package/dist/chunk-PRVFVXT4.js +722 -0
- package/dist/chunk-SHJDRZF5.cjs +796 -0
- package/dist/chunk-SNDVQU5A.js +152 -0
- package/dist/chunk-UQOBUJIQ.cjs +3087 -0
- package/dist/chunk-ZRCU35UV.cjs +184 -0
- package/dist/citations.d.ts +63 -0
- package/dist/compiler/compiler.d.ts +28 -0
- package/dist/compiler/index.d.ts +2 -0
- package/dist/compiler/plugins/async-iterator.d.ts +4 -0
- package/dist/compiler/plugins/braces-tsx.d.ts +3 -0
- package/dist/compiler/plugins/jsx-preserve-newlines.d.ts +5 -0
- package/dist/compiler/plugins/line-tracking.d.ts +5 -0
- package/dist/compiler/plugins/replace-comment.d.ts +5 -0
- package/dist/compiler/plugins/return-async.d.ts +4 -0
- package/dist/compiler/plugins/track-tool-calls.d.ts +16 -0
- package/dist/compiler/plugins/variable-extraction.d.ts +5 -0
- package/dist/component-R4WTW6DZ.cjs +18 -0
- package/dist/component-WFVDVSDK.js +18 -0
- package/dist/component.d.ts +66 -0
- package/dist/component.default.d.ts +316 -0
- package/dist/context.d.ts +176 -0
- package/dist/dual-modes-T53P72CH.js +12 -0
- package/dist/dual-modes-VLIGPIHX.cjs +12 -0
- package/dist/errors.d.ts +64 -0
- package/dist/exit-TRXEU4OU.cjs +8 -0
- package/dist/exit-YORW76T3.js +8 -0
- package/dist/exit.d.ts +26 -0
- package/dist/formatting.d.ts +5 -0
- package/dist/getter.d.ts +2 -0
- package/dist/handlers.d.ts +10 -0
- package/dist/hoist.d.ts +2 -0
- package/dist/index.cjs +921 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +921 -0
- package/dist/inspect.d.ts +6 -0
- package/dist/jsx-AEHVFB3L.js +13 -0
- package/dist/jsx-AJAXBWFE.cjs +13 -0
- package/dist/jsx.d.ts +11 -0
- package/dist/llmz-QLZBDG2Z.cjs +576 -0
- package/dist/llmz-ROOX7RYI.js +576 -0
- package/dist/llmz.d.ts +40 -0
- package/dist/objects.d.ts +24 -0
- package/dist/prompts/chat-mode/system.md.d.ts +2 -0
- package/dist/prompts/chat-mode/user.md.d.ts +2 -0
- package/dist/prompts/common.d.ts +6 -0
- package/dist/prompts/dual-modes.d.ts +2 -0
- package/dist/prompts/prompt.d.ts +55 -0
- package/dist/prompts/worker-mode/system.md.d.ts +2 -0
- package/dist/prompts/worker-mode/user.md.d.ts +2 -0
- package/dist/result.d.ts +33 -0
- package/dist/snapshots.d.ts +69 -0
- package/dist/stack-traces.d.ts +1 -0
- package/dist/tool-N6ODRRGH.js +11 -0
- package/dist/tool-QP4MVRWI.cjs +11 -0
- package/dist/tool.d.ts +56 -0
- package/dist/transcript.d.ts +36 -0
- package/dist/truncator-DUMWEGQO.cjs +10 -0
- package/dist/truncator-IY2MXOMC.js +10 -0
- package/dist/truncator.d.ts +20 -0
- package/dist/types.d.ts +105 -0
- package/dist/typings-2CPHOFDN.cjs +10 -0
- package/dist/typings-GDMY6VY2.js +10 -0
- package/dist/typings.d.ts +5 -0
- package/dist/utils-A7WNEFTA.cjs +39 -0
- package/dist/utils-N24IHDFA.js +39 -0
- package/dist/utils.d.ts +23 -0
- package/dist/vm-2DLG7V4G.cjs +12 -0
- package/dist/vm-FLBMZUA2.js +12 -0
- package/dist/vm.d.ts +3 -0
- package/package.json +1 -1
package/dist/chat.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Component, RenderedComponent } from './component.js';
|
|
2
|
+
import { Context } from './context.js';
|
|
3
|
+
import { ValueOrGetter } from './getter.js';
|
|
4
|
+
import { ExecutionResult } from './result.js';
|
|
5
|
+
import { TranscriptArray, Transcript } from './transcript.js';
|
|
6
|
+
export type MessageHandler = (input: RenderedComponent) => Promise<void> | void;
|
|
7
|
+
export declare class Chat {
|
|
8
|
+
readonly handler: MessageHandler;
|
|
9
|
+
readonly transcript: ValueOrGetter<TranscriptArray, Context>;
|
|
10
|
+
readonly components: ValueOrGetter<Component[], Context>;
|
|
11
|
+
constructor(props: {
|
|
12
|
+
handler: MessageHandler;
|
|
13
|
+
components: ValueOrGetter<Component[], Context>;
|
|
14
|
+
transcript?: ValueOrGetter<Transcript.Message[], Context>;
|
|
15
|
+
});
|
|
16
|
+
onExecutionDone(_result: ExecutionResult): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
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 _chunkUQOBUJIQcjs = require('./chunk-UQOBUJIQ.cjs');
|
|
8
|
+
|
|
9
|
+
// src/utils.ts
|
|
10
|
+
var _thicktoken = require('@bpinternal/thicktoken');
|
|
11
|
+
var _zui = require('@bpinternal/zui');
|
|
12
|
+
var tokenizer = null;
|
|
13
|
+
var init = async () => {
|
|
14
|
+
if (tokenizer) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
while (typeof _thicktoken.getWasmTokenizer !== "function") {
|
|
18
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
19
|
+
}
|
|
20
|
+
tokenizer = await _thicktoken.getWasmTokenizer.call(void 0, );
|
|
21
|
+
};
|
|
22
|
+
var getTokenizer = () => {
|
|
23
|
+
if (!tokenizer) {
|
|
24
|
+
throw new Error("Tokenizer not initialized, make sure to call init() first and await it");
|
|
25
|
+
}
|
|
26
|
+
return tokenizer;
|
|
27
|
+
};
|
|
28
|
+
var Tokens = (min, max) => {
|
|
29
|
+
return _zui.z.string().superRefine((value, ctx) => {
|
|
30
|
+
const tokens = tokenizer.count(value);
|
|
31
|
+
if (value.length < min) {
|
|
32
|
+
ctx.addIssue({
|
|
33
|
+
code: "too_small",
|
|
34
|
+
minimum: min,
|
|
35
|
+
type: "string",
|
|
36
|
+
message: `Text is ${tokens} but expected at least ${min} tokens`,
|
|
37
|
+
inclusive: true
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (value.length > max) {
|
|
41
|
+
ctx.addIssue({
|
|
42
|
+
code: "too_big",
|
|
43
|
+
maximum: max,
|
|
44
|
+
type: "string",
|
|
45
|
+
message: `Text is ${tokens} tokens but expected at most ${max} tokens`,
|
|
46
|
+
inclusive: true
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
var Identifier = _zui.z.string().min(1).max(100).regex(
|
|
53
|
+
/^([a-zA-Z_][a-zA-Z0-9_]*)$/,
|
|
54
|
+
"Identifiers must start with a letter and can only contain letters, numbers and underscores"
|
|
55
|
+
);
|
|
56
|
+
function escapeString(str) {
|
|
57
|
+
if (typeof str !== "string") {
|
|
58
|
+
return "";
|
|
59
|
+
}
|
|
60
|
+
const rawStr = String.raw`${str}`;
|
|
61
|
+
const escapedStr = rawStr.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/"/g, '\\"').replace(/'/g, "\\'");
|
|
62
|
+
if (escapedStr.includes("`")) {
|
|
63
|
+
return `"${escapedStr}"`;
|
|
64
|
+
} else if (escapedStr.includes("'")) {
|
|
65
|
+
return `"${escapedStr}"`;
|
|
66
|
+
} else {
|
|
67
|
+
return `'${escapedStr}'`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
var toPropertyKey = (key) => {
|
|
71
|
+
if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key)) {
|
|
72
|
+
return key;
|
|
73
|
+
}
|
|
74
|
+
return escapeString(key);
|
|
75
|
+
};
|
|
76
|
+
var trimEmptyLines = (lines) => {
|
|
77
|
+
var _a;
|
|
78
|
+
while (lines.length && !((_a = lines[0]) == null ? void 0 : _a.trim())) {
|
|
79
|
+
lines.shift();
|
|
80
|
+
}
|
|
81
|
+
while (lines.length && !lines[lines.length - 1].trim()) {
|
|
82
|
+
lines.pop();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var getMultilineComment = (description) => {
|
|
86
|
+
description = description == null ? void 0 : description.replace(/(\n(\s*)?){3,}/g, "\n\n");
|
|
87
|
+
const ensureLineStartsWithAsterisk = (line) => line.startsWith("* ") ? ` ${line}` : ` * ${line}`;
|
|
88
|
+
const escapeCommentEnd = (line) => line.replace(/\*\//g, "*\\/");
|
|
89
|
+
const descLines = (_nullishCoalesce(description, () => ( ""))).split("\n").map((line) => line.trim());
|
|
90
|
+
trimEmptyLines(descLines);
|
|
91
|
+
if (descLines.length) {
|
|
92
|
+
descLines[0] = descLines[0].replace(/^\/\*\*?/, "");
|
|
93
|
+
descLines[descLines.length - 1] = descLines[descLines.length - 1].replace(/\*\/$/, "");
|
|
94
|
+
}
|
|
95
|
+
trimEmptyLines(descLines);
|
|
96
|
+
return descLines.length === 0 ? "" : descLines.length === 1 ? `/** ${escapeCommentEnd(descLines[0])} */` : `/**
|
|
97
|
+
${descLines.map(ensureLineStartsWithAsterisk).map(escapeCommentEnd).join("\n")}
|
|
98
|
+
*/`;
|
|
99
|
+
};
|
|
100
|
+
var toValidFunctionName = (str) => {
|
|
101
|
+
let name = _chunkUQOBUJIQcjs.deburr_default.call(void 0, str);
|
|
102
|
+
name = name.replace(/[^a-zA-Z0-9_$]/g, "");
|
|
103
|
+
if (!/^[a-zA-Z_$]/.test(name)) {
|
|
104
|
+
name = `_${name}`;
|
|
105
|
+
}
|
|
106
|
+
return _chunkUQOBUJIQcjs.camelCase_default.call(void 0, name);
|
|
107
|
+
};
|
|
108
|
+
var awaitObject = async (obj) => {
|
|
109
|
+
const newObj = { ...obj };
|
|
110
|
+
const promises = [];
|
|
111
|
+
const setProp = async (key, value) => {
|
|
112
|
+
newObj[key] = await value;
|
|
113
|
+
};
|
|
114
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
115
|
+
if (value instanceof Promise) {
|
|
116
|
+
promises.push(setProp(key, value));
|
|
117
|
+
} else if (_chunkUQOBUJIQcjs.isPlainObject_default.call(void 0, value)) {
|
|
118
|
+
promises.push(setProp(key, await awaitObject(value)));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
await Promise.all(promises);
|
|
122
|
+
return newObj;
|
|
123
|
+
};
|
|
124
|
+
var toValidObjectName = (str) => {
|
|
125
|
+
let name = _chunkUQOBUJIQcjs.deburr_default.call(void 0, str);
|
|
126
|
+
name = name.split(/[^a-zA-Z0-9_$]/g).map(_chunkUQOBUJIQcjs.startCase_default).join("");
|
|
127
|
+
if (!/^[a-zA-Z_$]/.test(name)) {
|
|
128
|
+
name = `_${name}`;
|
|
129
|
+
}
|
|
130
|
+
return name.replaceAll(" ", "");
|
|
131
|
+
};
|
|
132
|
+
var stripInvalidIdentifiers = (object) => {
|
|
133
|
+
if (typeof object !== "object") {
|
|
134
|
+
return object;
|
|
135
|
+
}
|
|
136
|
+
if (Array.isArray(object)) {
|
|
137
|
+
return object.map(stripInvalidIdentifiers);
|
|
138
|
+
}
|
|
139
|
+
return _chunkUQOBUJIQcjs.pickBy_default.call(void 0, object, (__, key) => Identifier.safeParse(key).success);
|
|
140
|
+
};
|
|
141
|
+
var convertObjectToZuiLiterals = (obj, nested = false) => {
|
|
142
|
+
if (typeof obj === "string") {
|
|
143
|
+
return _zui.z.literal(obj).catch(() => obj);
|
|
144
|
+
}
|
|
145
|
+
if (typeof obj === "number") {
|
|
146
|
+
return _zui.z.literal(obj).catch(() => obj);
|
|
147
|
+
}
|
|
148
|
+
if (typeof obj === "boolean") {
|
|
149
|
+
return _zui.z.literal(obj).catch(() => obj);
|
|
150
|
+
}
|
|
151
|
+
if (Array.isArray(obj)) {
|
|
152
|
+
if (obj.length === 0) {
|
|
153
|
+
return _zui.z.tuple([]).catch(() => obj);
|
|
154
|
+
}
|
|
155
|
+
return _zui.z.tuple([
|
|
156
|
+
// the tuple needs to have at least one element
|
|
157
|
+
convertObjectToZuiLiterals(obj[0]),
|
|
158
|
+
...obj.slice(1).map((child) => convertObjectToZuiLiterals(child))
|
|
159
|
+
]).catch(() => obj);
|
|
160
|
+
}
|
|
161
|
+
if (obj !== null && typeof obj === "object") {
|
|
162
|
+
const shape = {};
|
|
163
|
+
for (const key in obj) {
|
|
164
|
+
shape[key] = convertObjectToZuiLiterals(obj[key], true);
|
|
165
|
+
}
|
|
166
|
+
if (nested) {
|
|
167
|
+
return _zui.z.object(shape).catch(() => shape);
|
|
168
|
+
}
|
|
169
|
+
return shape;
|
|
170
|
+
}
|
|
171
|
+
if (obj === void 0) {
|
|
172
|
+
return _zui.z.undefined().catch(() => void 0);
|
|
173
|
+
}
|
|
174
|
+
if (obj === null) {
|
|
175
|
+
return _zui.z.null().catch(() => null);
|
|
176
|
+
}
|
|
177
|
+
throw new Error(`Unsupported object type ${typeof obj}, ${obj})`);
|
|
178
|
+
};
|
|
179
|
+
var isValidSchema = (schema) => {
|
|
180
|
+
try {
|
|
181
|
+
_zui.transforms.fromJSONSchemaLegacy(schema);
|
|
182
|
+
return typeof schema.type === "string";
|
|
183
|
+
} catch (e) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
function isZuiSchema(schema) {
|
|
188
|
+
return !!schema && typeof schema === "object" && schema.__type__ === "ZuiType";
|
|
189
|
+
}
|
|
190
|
+
function isJsonSchema(schema) {
|
|
191
|
+
return !!schema && typeof schema === "object" && ("$schema" in schema || "type" in schema || "properties" in schema);
|
|
192
|
+
}
|
|
193
|
+
function isValidIdentifier(name) {
|
|
194
|
+
if (typeof name !== "string") {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
return /^[A-Z]{1,}[A-Z0-9_]{0,50}$/i.test(name);
|
|
198
|
+
}
|
|
199
|
+
function isValidMessageName(name) {
|
|
200
|
+
if (typeof name !== "string") {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
return /^[A-Z]{1,50}$/i.test(name);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
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.convertObjectToZuiLiterals = convertObjectToZuiLiterals; exports.isValidSchema = isValidSchema; exports.isZuiSchema = isZuiSchema; exports.isJsonSchema = isJsonSchema; exports.isValidIdentifier = isValidIdentifier; exports.isValidMessageName = isValidMessageName;
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
+
|
|
3
|
+
var _chunkJDABP4SDcjs = require('./chunk-JDABP4SD.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunk276Q6EWPcjs = require('./chunk-276Q6EWP.cjs');
|
|
9
|
+
|
|
10
|
+
// src/typings.ts
|
|
11
|
+
var _zui = require('@bpinternal/zui');
|
|
12
|
+
|
|
13
|
+
// src/formatting.ts
|
|
14
|
+
var _lrucache = require('lru-cache');
|
|
15
|
+
var _babel = require('prettier/plugins/babel'); var _babel2 = _interopRequireDefault(_babel);
|
|
16
|
+
var _estree = require('prettier/plugins/estree'); var _estree2 = _interopRequireDefault(_estree);
|
|
17
|
+
var _typescript = require('prettier/plugins/typescript'); var _typescript2 = _interopRequireDefault(_typescript);
|
|
18
|
+
var _standalone = require('prettier/standalone');
|
|
19
|
+
var cache = new (0, _lrucache.LRUCache)({ max: 1e3 });
|
|
20
|
+
async function formatTypings(typings, options) {
|
|
21
|
+
if (cache.has(typings)) {
|
|
22
|
+
return cache.get(typings);
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
options ??= {};
|
|
26
|
+
options.throwOnError ??= true;
|
|
27
|
+
const result = (await _standalone.format.call(void 0, typings, {
|
|
28
|
+
singleAttributePerLine: true,
|
|
29
|
+
bracketSameLine: true,
|
|
30
|
+
semi: false,
|
|
31
|
+
...options,
|
|
32
|
+
embeddedLanguageFormatting: "off",
|
|
33
|
+
plugins: [_estree2.default, _babel2.default, _typescript2.default],
|
|
34
|
+
parser: "typescript",
|
|
35
|
+
filepath: "tools.d.ts"
|
|
36
|
+
})).trim();
|
|
37
|
+
cache.set(typings, result);
|
|
38
|
+
return result;
|
|
39
|
+
} catch (err) {
|
|
40
|
+
if (options == null ? void 0 : options.throwOnError) {
|
|
41
|
+
throw new (0, _chunkJDABP4SDcjs.CodeFormattingError)(err instanceof Error ? err.message : _nullishCoalesce((err == null ? void 0 : err.toString()), () => ( "Unknown Error")), typings);
|
|
42
|
+
}
|
|
43
|
+
return typings;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/typings.ts
|
|
48
|
+
var Primitives = [
|
|
49
|
+
"string",
|
|
50
|
+
"number",
|
|
51
|
+
"boolean",
|
|
52
|
+
"unknown",
|
|
53
|
+
"void",
|
|
54
|
+
"any",
|
|
55
|
+
"null",
|
|
56
|
+
"undefined",
|
|
57
|
+
"never",
|
|
58
|
+
"bigint",
|
|
59
|
+
"symbol",
|
|
60
|
+
"object"
|
|
61
|
+
];
|
|
62
|
+
var LARGE_DECLARATION_LINES = 5;
|
|
63
|
+
var isPrimitive = (type) => Primitives.includes(type);
|
|
64
|
+
var isArrayOfPrimitives = (type) => Primitives.map((p) => `${p}[]`).includes(type);
|
|
65
|
+
var stripSpaces = (typings) => typings.replace(/ +/g, " ").trim();
|
|
66
|
+
var KeyValue = class {
|
|
67
|
+
constructor(key, value) {
|
|
68
|
+
this.key = key;
|
|
69
|
+
this.value = value;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
var FnParameters = class {
|
|
73
|
+
constructor(schema) {
|
|
74
|
+
this.schema = schema;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
var FnReturn = class {
|
|
78
|
+
constructor(schema) {
|
|
79
|
+
this.schema = schema;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var Declaration = class {
|
|
83
|
+
constructor(schema, identifier) {
|
|
84
|
+
this.schema = schema;
|
|
85
|
+
this.identifier = identifier;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
async function getTypings(schema, options) {
|
|
89
|
+
options ??= {};
|
|
90
|
+
options.declaration ??= false;
|
|
91
|
+
let wrappedSchema = schema;
|
|
92
|
+
if ((options == null ? void 0 : options.declaration) && schema instanceof _zui.z.Schema) {
|
|
93
|
+
const title = "title" in schema.ui ? schema.ui.title : null;
|
|
94
|
+
if (!title) {
|
|
95
|
+
throw new Error('Only schemas with "title" Zui property can be declared.');
|
|
96
|
+
}
|
|
97
|
+
wrappedSchema = new Declaration(schema, title);
|
|
98
|
+
}
|
|
99
|
+
let dts = await sUnwrapZodRecursive(wrappedSchema, { ...options });
|
|
100
|
+
dts = await formatTypings(dts, { throwOnError: false });
|
|
101
|
+
return dts;
|
|
102
|
+
}
|
|
103
|
+
async function sUnwrapZodRecursive(schema, options) {
|
|
104
|
+
return sUnwrapZod(schema, options);
|
|
105
|
+
}
|
|
106
|
+
async function sUnwrapZod(schema, options) {
|
|
107
|
+
var _a, _b;
|
|
108
|
+
const newOptions = {
|
|
109
|
+
...options,
|
|
110
|
+
declaration: false,
|
|
111
|
+
parent: schema
|
|
112
|
+
};
|
|
113
|
+
if (schema instanceof Declaration) {
|
|
114
|
+
const description = _chunk276Q6EWPcjs.getMultilineComment.call(void 0, schema.schema.description);
|
|
115
|
+
const withoutDesc = schema.schema.describe("");
|
|
116
|
+
const typings = await sUnwrapZodRecursive(withoutDesc, { ...newOptions, declaration: true });
|
|
117
|
+
const isLargeDeclaration = typings.split("\n").length >= LARGE_DECLARATION_LINES;
|
|
118
|
+
const closingTag = isLargeDeclaration ? `// end of ${schema.identifier}` : "";
|
|
119
|
+
if (schema.schema instanceof _zui.z.ZodFunction) {
|
|
120
|
+
return stripSpaces(`${description}
|
|
121
|
+
declare function ${schema.identifier}${typings};${closingTag}`);
|
|
122
|
+
}
|
|
123
|
+
return stripSpaces(`${description}
|
|
124
|
+
declare const ${schema.identifier}: ${typings};${closingTag}`);
|
|
125
|
+
}
|
|
126
|
+
if (schema instanceof KeyValue) {
|
|
127
|
+
if (schema.value instanceof _zui.z.ZodOptional) {
|
|
128
|
+
let innerType = schema.value._def.innerType;
|
|
129
|
+
if (innerType instanceof _zui.z.Schema && !innerType.description && schema.value.description) {
|
|
130
|
+
innerType = innerType == null ? void 0 : innerType.describe(schema.value.description);
|
|
131
|
+
}
|
|
132
|
+
const optionalToken = schema.key.endsWith("?") ? "" : "?";
|
|
133
|
+
return sUnwrapZodRecursive(new KeyValue(schema.key + optionalToken, innerType), newOptions);
|
|
134
|
+
}
|
|
135
|
+
const description = _chunk276Q6EWPcjs.getMultilineComment.call(void 0, schema.value._def.description || schema.value.description);
|
|
136
|
+
const delimiter = (description == null ? void 0 : description.trim().length) > 0 ? "\n" : "";
|
|
137
|
+
const withoutDesc = schema.value.describe("");
|
|
138
|
+
return `${delimiter}${description}${delimiter}${schema.key}: ${await sUnwrapZodRecursive(withoutDesc, newOptions)}${delimiter}`;
|
|
139
|
+
}
|
|
140
|
+
if (schema instanceof FnParameters) {
|
|
141
|
+
if (schema.schema instanceof _zui.z.ZodTuple) {
|
|
142
|
+
let args = "";
|
|
143
|
+
for (let i = 0; i < schema.schema.items.length; i++) {
|
|
144
|
+
const argName = _nullishCoalesce(((_b = (_a = schema.schema.items[i]) == null ? void 0 : _a.ui) == null ? void 0 : _b.title), () => ( `arg${i}`));
|
|
145
|
+
const item = schema.schema.items[i];
|
|
146
|
+
args += `${await sUnwrapZodRecursive(new KeyValue(_chunk276Q6EWPcjs.toPropertyKey.call(void 0, argName), item), newOptions)}, `;
|
|
147
|
+
}
|
|
148
|
+
return args;
|
|
149
|
+
}
|
|
150
|
+
const isLiteral = schema.schema.naked() instanceof _zui.z.ZodLiteral;
|
|
151
|
+
const typings = (await sUnwrapZodRecursive(schema.schema, newOptions)).trim();
|
|
152
|
+
const startsWithPairs = typings.startsWith("{") && typings.endsWith("}") || typings.startsWith("[") && typings.endsWith("]") || typings.startsWith("(") && typings.endsWith(")") || typings.startsWith("Array<") && typings.endsWith(">") || typings.startsWith("Record<") && typings.endsWith(">") || isArrayOfPrimitives(typings);
|
|
153
|
+
if (startsWithPairs || isLiteral) {
|
|
154
|
+
return `args: ${typings}`;
|
|
155
|
+
} else {
|
|
156
|
+
return typings;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (schema instanceof FnReturn) {
|
|
160
|
+
if (schema.schema instanceof _zui.z.ZodOptional) {
|
|
161
|
+
return `${await sUnwrapZodRecursive(schema.schema.unwrap(), newOptions)} | undefined`;
|
|
162
|
+
}
|
|
163
|
+
return sUnwrapZodRecursive(schema.schema, newOptions);
|
|
164
|
+
}
|
|
165
|
+
if (schema instanceof _zui.z.ZodDefault) {
|
|
166
|
+
return sUnwrapZodRecursive(schema._def.innerType, options);
|
|
167
|
+
}
|
|
168
|
+
if (schema instanceof _zui.z.ZodVoid) {
|
|
169
|
+
return "void";
|
|
170
|
+
}
|
|
171
|
+
if (schema instanceof _zui.z.ZodUnknown) {
|
|
172
|
+
return "unknown";
|
|
173
|
+
}
|
|
174
|
+
if (schema instanceof _zui.z.ZodAny) {
|
|
175
|
+
return "any";
|
|
176
|
+
}
|
|
177
|
+
if (schema instanceof _zui.z.ZodPromise) {
|
|
178
|
+
return `Promise<${await sUnwrapZodRecursive(schema.unwrap(), newOptions)}>`;
|
|
179
|
+
}
|
|
180
|
+
if (schema instanceof _zui.z.ZodFunction) {
|
|
181
|
+
const description = _chunk276Q6EWPcjs.getMultilineComment.call(void 0, schema._def.description);
|
|
182
|
+
const input = await sUnwrapZodRecursive(new FnParameters(schema._def.args), newOptions);
|
|
183
|
+
const output = await sUnwrapZodRecursive(new FnReturn(schema._def.returns), newOptions);
|
|
184
|
+
if (options == null ? void 0 : options.declaration) {
|
|
185
|
+
return `${description}
|
|
186
|
+
(${input}): ${output}`;
|
|
187
|
+
}
|
|
188
|
+
return `${description}
|
|
189
|
+
(${input}) => ${output}`;
|
|
190
|
+
}
|
|
191
|
+
if (schema instanceof _zui.z.ZodArray) {
|
|
192
|
+
const item = await sUnwrapZodRecursive(schema._def.type, newOptions);
|
|
193
|
+
if (isPrimitive(item)) {
|
|
194
|
+
return `${item}[]`;
|
|
195
|
+
}
|
|
196
|
+
return `Array<${item}>`;
|
|
197
|
+
}
|
|
198
|
+
if (schema instanceof _zui.z.ZodEnum) {
|
|
199
|
+
const values = schema._def.values.map(_chunk276Q6EWPcjs.escapeString);
|
|
200
|
+
return values.join(" | ");
|
|
201
|
+
}
|
|
202
|
+
if (schema instanceof _zui.z.ZodTuple) {
|
|
203
|
+
if (schema.items.length === 0) {
|
|
204
|
+
return "[]";
|
|
205
|
+
}
|
|
206
|
+
const items = await Promise.all(schema.items.map((i) => sUnwrapZodRecursive(i, newOptions)));
|
|
207
|
+
return `[${items.join(", ")}]`;
|
|
208
|
+
}
|
|
209
|
+
if (schema instanceof _zui.z.ZodNullable) {
|
|
210
|
+
return `${await sUnwrapZodRecursive(schema.unwrap(), options)} | null`;
|
|
211
|
+
}
|
|
212
|
+
if (schema instanceof _zui.z.ZodOptional) {
|
|
213
|
+
if ((options == null ? void 0 : options.declaration) || (options == null ? void 0 : options.parent) instanceof _zui.z.ZodRecord) {
|
|
214
|
+
return `${await sUnwrapZodRecursive(schema._def.innerType, newOptions)} | undefined`;
|
|
215
|
+
}
|
|
216
|
+
const optionalToken = options.parent instanceof KeyValue ? "| undefined" : "";
|
|
217
|
+
const val = `${await sUnwrapZodRecursive(schema._def.innerType, newOptions)}${optionalToken}`;
|
|
218
|
+
return val;
|
|
219
|
+
}
|
|
220
|
+
if (schema instanceof _zui.z.ZodObject) {
|
|
221
|
+
const props = await Promise.all(
|
|
222
|
+
Object.entries(schema.shape).map(async ([key, value]) => {
|
|
223
|
+
if (value instanceof _zui.z.Schema) {
|
|
224
|
+
return sUnwrapZodRecursive(new KeyValue(_chunk276Q6EWPcjs.toPropertyKey.call(void 0, key), value), newOptions);
|
|
225
|
+
}
|
|
226
|
+
return `${key}: unknown`;
|
|
227
|
+
})
|
|
228
|
+
);
|
|
229
|
+
return `{ ${props.join("; ")} }`;
|
|
230
|
+
}
|
|
231
|
+
if (schema instanceof _zui.z.ZodString) {
|
|
232
|
+
const description = _chunk276Q6EWPcjs.getMultilineComment.call(void 0, schema._def.description);
|
|
233
|
+
return `${description} string`.trim();
|
|
234
|
+
}
|
|
235
|
+
if (schema instanceof _zui.z.ZodUnion) {
|
|
236
|
+
const description = _chunk276Q6EWPcjs.getMultilineComment.call(void 0, schema._def.description);
|
|
237
|
+
const options2 = await Promise.all(
|
|
238
|
+
schema.options.map(async (option) => {
|
|
239
|
+
return sUnwrapZodRecursive(option, newOptions);
|
|
240
|
+
})
|
|
241
|
+
);
|
|
242
|
+
return `${description}
|
|
243
|
+
${options2.join(" | ")}`;
|
|
244
|
+
}
|
|
245
|
+
if (schema instanceof _zui.z.ZodLiteral) {
|
|
246
|
+
const description = _chunk276Q6EWPcjs.getMultilineComment.call(void 0, schema._def.description);
|
|
247
|
+
return `${description}
|
|
248
|
+
${typeof schema.value === "string" ? _chunk276Q6EWPcjs.escapeString.call(void 0, schema.value) : schema.value}`.trim();
|
|
249
|
+
}
|
|
250
|
+
if (schema instanceof _zui.z.ZodNumber) {
|
|
251
|
+
const description = _chunk276Q6EWPcjs.getMultilineComment.call(void 0, schema._def.description);
|
|
252
|
+
return `${description} number`.trim();
|
|
253
|
+
}
|
|
254
|
+
if (schema instanceof _zui.z.ZodBoolean) {
|
|
255
|
+
const description = _chunk276Q6EWPcjs.getMultilineComment.call(void 0, schema._def.description);
|
|
256
|
+
return `${description} boolean`.trim();
|
|
257
|
+
}
|
|
258
|
+
if (schema instanceof _zui.z.ZodCatch) {
|
|
259
|
+
return sUnwrapZodRecursive(schema.removeCatch(), newOptions);
|
|
260
|
+
}
|
|
261
|
+
if (schema instanceof _zui.z.ZodLazy) {
|
|
262
|
+
return sUnwrapZodRecursive(schema._def.getter(), newOptions);
|
|
263
|
+
}
|
|
264
|
+
if (schema instanceof _zui.z.ZodRecord) {
|
|
265
|
+
const description = _chunk276Q6EWPcjs.getMultilineComment.call(void 0, schema._def.description);
|
|
266
|
+
const keyType = await sUnwrapZodRecursive(schema._def.keyType, newOptions);
|
|
267
|
+
const valueType = await sUnwrapZodRecursive(schema._def.valueType, newOptions);
|
|
268
|
+
return `${description} { [key: (${keyType})]: (${valueType}) }`;
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
let typings = schema == null ? void 0 : schema.toTypescriptType();
|
|
272
|
+
typings ??= "unknown";
|
|
273
|
+
return stripSpaces(typings);
|
|
274
|
+
} catch (error) {
|
|
275
|
+
console.error("Error in sUnwrapZod", { error, schema, parent: options == null ? void 0 : options.parent });
|
|
276
|
+
return "unknown";
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
exports.formatTypings = formatTypings; exports.getTypings = getTypings;
|