llmz 0.0.16 → 0.0.18
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/{chunk-BEPRLBPK.cjs → chunk-M2MYALUN.cjs} +28 -14
- package/dist/{chunk-D3ESDRLH.js → chunk-VIMCBE6B.js} +26 -12
- package/dist/index.cjs +6 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.js +6 -4
- package/dist/{llmz-T4DEP7OD.js → llmz-D2R3BIFW.js} +1 -1
- package/dist/{llmz-EUESEPB7.cjs → llmz-HVMGCGKR.cjs} +2 -2
- package/dist/truncator.d.ts +96 -0
- package/dist/{vm-2DLG7V4G.cjs → vm-6NC73N5B.cjs} +2 -2
- package/dist/{vm-FLBMZUA2.js → vm-DNS3KON5.js} +3 -3
- package/dist/vm.d.ts +1 -1
- package/package.json +4 -2
|
@@ -2616,8 +2616,7 @@ var _a;
|
|
|
2616
2616
|
var IS_CI = typeof process !== "undefined" && !!((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.CI);
|
|
2617
2617
|
var _a2;
|
|
2618
2618
|
var VM_DRIVER = _nullishCoalesce((typeof process !== "undefined" && ((_a2 = process == null ? void 0 : process.env) == null ? void 0 : _a2.VM_DRIVER)), () => ( (IS_CI ? "node" : "isolated-vm")));
|
|
2619
|
-
var
|
|
2620
|
-
var LINE_OFFSET = USE_ISOLATED_VM ? 3 : 1;
|
|
2619
|
+
var CAN_USE_ISOLATED_VM = IS_NODE && VM_DRIVER === "isolated-vm";
|
|
2621
2620
|
var MAX_VM_EXECUTION_TIME = 6e4;
|
|
2622
2621
|
var requireEsm = async (id) => {
|
|
2623
2622
|
if (typeof globalThis.window === "undefined" && typeof globalThis.require !== "undefined") {
|
|
@@ -2723,7 +2722,19 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
|
|
|
2723
2722
|
traces.push({ type: "yield", value, started_at: startedAt, ended_at: Date.now() });
|
|
2724
2723
|
}
|
|
2725
2724
|
};
|
|
2726
|
-
|
|
2725
|
+
let DRIVER = CAN_USE_ISOLATED_VM ? "isolated-vm" : "node";
|
|
2726
|
+
let isolatedVm;
|
|
2727
|
+
if (DRIVER === "isolated-vm") {
|
|
2728
|
+
try {
|
|
2729
|
+
isolatedVm = await getIsolatedVm();
|
|
2730
|
+
} catch (e3) {
|
|
2731
|
+
console.warn(
|
|
2732
|
+
"LLMZ: 'isolated-vm' is not available, falling back to node driver. LLMZ requires 'isolated-vm' packager to run in a sandboxed environment. The code generted by the LLM will run in the NodeJS environment, which is not sandboxed and may have access to the file system, network, and other resources that could lead to security issues."
|
|
2733
|
+
);
|
|
2734
|
+
DRIVER = "node";
|
|
2735
|
+
}
|
|
2736
|
+
}
|
|
2737
|
+
if (DRIVER === "node") {
|
|
2727
2738
|
const AsyncFunction = async function* () {
|
|
2728
2739
|
}.constructor;
|
|
2729
2740
|
return await (async () => {
|
|
@@ -2758,16 +2769,18 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
|
|
|
2758
2769
|
lines_executed: Array.from(lines_executed),
|
|
2759
2770
|
return_value: res
|
|
2760
2771
|
};
|
|
2761
|
-
}).catch((err) => handleError(err, code, consumer, traces, variables, lines_executed, currentToolCall)).catch((err) => handleCatch(err, traces, variables, lines_executed));
|
|
2772
|
+
}).catch((err) => handleError(err, code, consumer, traces, variables, lines_executed, currentToolCall, DRIVER)).catch((err) => handleCatch(err, traces, variables, lines_executed));
|
|
2773
|
+
}
|
|
2774
|
+
if (!isolatedVm) {
|
|
2775
|
+
throw new Error("isolated-vm is not available");
|
|
2762
2776
|
}
|
|
2763
|
-
const isolatedVm = await getIsolatedVm();
|
|
2764
2777
|
const isolate = new isolatedVm.Isolate({ memoryLimit: 128 });
|
|
2765
2778
|
const isolatedContext = await isolate.createContext();
|
|
2766
2779
|
const jail = isolatedContext.global;
|
|
2767
2780
|
const trackedProperties = /* @__PURE__ */ new Set();
|
|
2768
2781
|
const referenceProperties = /* @__PURE__ */ new Set();
|
|
2769
2782
|
const abort = () => {
|
|
2770
|
-
if (
|
|
2783
|
+
if (DRIVER === "isolated-vm") {
|
|
2771
2784
|
isolate.dispose();
|
|
2772
2785
|
isolatedContext.release();
|
|
2773
2786
|
}
|
|
@@ -3012,7 +3025,7 @@ do {
|
|
|
3012
3025
|
};
|
|
3013
3026
|
},
|
|
3014
3027
|
(err) => {
|
|
3015
|
-
return handleError(err, code, consumer, traces, variables, lines_executed, currentToolCall);
|
|
3028
|
+
return handleError(err, code, consumer, traces, variables, lines_executed, currentToolCall, DRIVER);
|
|
3016
3029
|
}
|
|
3017
3030
|
).catch((err) => {
|
|
3018
3031
|
if (signal == null ? void 0 : signal.aborted) {
|
|
@@ -3025,21 +3038,22 @@ do {
|
|
|
3025
3038
|
signal == null ? void 0 : signal.removeEventListener("abort", abort);
|
|
3026
3039
|
try {
|
|
3027
3040
|
isolate.dispose();
|
|
3028
|
-
} catch (
|
|
3041
|
+
} catch (e4) {
|
|
3029
3042
|
}
|
|
3030
3043
|
try {
|
|
3031
3044
|
isolatedContext.release();
|
|
3032
|
-
} catch (
|
|
3045
|
+
} catch (e5) {
|
|
3033
3046
|
}
|
|
3034
3047
|
return final;
|
|
3035
3048
|
}
|
|
3036
|
-
var handleError = (err, code, consumer, traces, variables, lines_executed, currentToolCall) => {
|
|
3049
|
+
var handleError = (err, code, consumer, traces, variables, lines_executed, currentToolCall, driver = "isolated-vm") => {
|
|
3037
3050
|
var _a3, _b, _c;
|
|
3038
3051
|
err = _chunkJDABP4SDcjs.Signals.maybeDeserializeError(err);
|
|
3039
3052
|
const lines = code.split("\n");
|
|
3040
3053
|
const stackTrace = err.stack || "";
|
|
3054
|
+
const LINE_OFFSET = driver === "isolated-vm" ? 3 : 1;
|
|
3041
3055
|
let regex = /\(<isolated-vm>:(\d+):(\d+)/g;
|
|
3042
|
-
if (
|
|
3056
|
+
if (driver === "node") {
|
|
3043
3057
|
regex = /<anonymous>:(\d+):(\d+)/g;
|
|
3044
3058
|
}
|
|
3045
3059
|
const matches = [...stackTrace.matchAll(regex)].map((x) => {
|
|
@@ -3067,8 +3081,8 @@ var handleError = (err, code, consumer, traces, variables, lines_executed, curre
|
|
|
3067
3081
|
}
|
|
3068
3082
|
};
|
|
3069
3083
|
for (let i = 0; i < lines.length; i++) {
|
|
3070
|
-
const VM_OFFSET =
|
|
3071
|
-
const DISPLAY_OFFSET =
|
|
3084
|
+
const VM_OFFSET = driver === "isolated-vm" ? 2 : 2;
|
|
3085
|
+
const DISPLAY_OFFSET = driver === "isolated-vm" ? 2 : 0;
|
|
3072
3086
|
const line = lines[i];
|
|
3073
3087
|
const correctedStackLineIndex = i + LINE_OFFSET + VM_OFFSET;
|
|
3074
3088
|
const match = matches.find((x) => x.line + VM_OFFSET === correctedStackLineIndex);
|
|
@@ -3120,4 +3134,4 @@ var handleCatch = (err, traces, variables, lines_executed) => {
|
|
|
3120
3134
|
|
|
3121
3135
|
|
|
3122
3136
|
|
|
3123
|
-
exports.
|
|
3137
|
+
exports.CAN_USE_ISOLATED_VM = CAN_USE_ISOLATED_VM; exports.runAsyncFunction = runAsyncFunction;
|
|
@@ -2616,8 +2616,7 @@ var _a;
|
|
|
2616
2616
|
var IS_CI = typeof process !== "undefined" && !!((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.CI);
|
|
2617
2617
|
var _a2;
|
|
2618
2618
|
var VM_DRIVER = (typeof process !== "undefined" && ((_a2 = process == null ? void 0 : process.env) == null ? void 0 : _a2.VM_DRIVER)) ?? (IS_CI ? "node" : "isolated-vm");
|
|
2619
|
-
var
|
|
2620
|
-
var LINE_OFFSET = USE_ISOLATED_VM ? 3 : 1;
|
|
2619
|
+
var CAN_USE_ISOLATED_VM = IS_NODE && VM_DRIVER === "isolated-vm";
|
|
2621
2620
|
var MAX_VM_EXECUTION_TIME = 6e4;
|
|
2622
2621
|
var requireEsm = async (id) => {
|
|
2623
2622
|
if (typeof globalThis.window === "undefined" && typeof globalThis.require !== "undefined") {
|
|
@@ -2723,7 +2722,19 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
|
|
|
2723
2722
|
traces.push({ type: "yield", value, started_at: startedAt, ended_at: Date.now() });
|
|
2724
2723
|
}
|
|
2725
2724
|
};
|
|
2726
|
-
|
|
2725
|
+
let DRIVER = CAN_USE_ISOLATED_VM ? "isolated-vm" : "node";
|
|
2726
|
+
let isolatedVm;
|
|
2727
|
+
if (DRIVER === "isolated-vm") {
|
|
2728
|
+
try {
|
|
2729
|
+
isolatedVm = await getIsolatedVm();
|
|
2730
|
+
} catch {
|
|
2731
|
+
console.warn(
|
|
2732
|
+
"LLMZ: 'isolated-vm' is not available, falling back to node driver. LLMZ requires 'isolated-vm' packager to run in a sandboxed environment. The code generted by the LLM will run in the NodeJS environment, which is not sandboxed and may have access to the file system, network, and other resources that could lead to security issues."
|
|
2733
|
+
);
|
|
2734
|
+
DRIVER = "node";
|
|
2735
|
+
}
|
|
2736
|
+
}
|
|
2737
|
+
if (DRIVER === "node") {
|
|
2727
2738
|
const AsyncFunction = async function* () {
|
|
2728
2739
|
}.constructor;
|
|
2729
2740
|
return await (async () => {
|
|
@@ -2758,16 +2769,18 @@ async function runAsyncFunction(context, code, traces = [], signal = null, timeo
|
|
|
2758
2769
|
lines_executed: Array.from(lines_executed),
|
|
2759
2770
|
return_value: res
|
|
2760
2771
|
};
|
|
2761
|
-
}).catch((err) => handleError(err, code, consumer, traces, variables, lines_executed, currentToolCall)).catch((err) => handleCatch(err, traces, variables, lines_executed));
|
|
2772
|
+
}).catch((err) => handleError(err, code, consumer, traces, variables, lines_executed, currentToolCall, DRIVER)).catch((err) => handleCatch(err, traces, variables, lines_executed));
|
|
2773
|
+
}
|
|
2774
|
+
if (!isolatedVm) {
|
|
2775
|
+
throw new Error("isolated-vm is not available");
|
|
2762
2776
|
}
|
|
2763
|
-
const isolatedVm = await getIsolatedVm();
|
|
2764
2777
|
const isolate = new isolatedVm.Isolate({ memoryLimit: 128 });
|
|
2765
2778
|
const isolatedContext = await isolate.createContext();
|
|
2766
2779
|
const jail = isolatedContext.global;
|
|
2767
2780
|
const trackedProperties = /* @__PURE__ */ new Set();
|
|
2768
2781
|
const referenceProperties = /* @__PURE__ */ new Set();
|
|
2769
2782
|
const abort = () => {
|
|
2770
|
-
if (
|
|
2783
|
+
if (DRIVER === "isolated-vm") {
|
|
2771
2784
|
isolate.dispose();
|
|
2772
2785
|
isolatedContext.release();
|
|
2773
2786
|
}
|
|
@@ -3012,7 +3025,7 @@ do {
|
|
|
3012
3025
|
};
|
|
3013
3026
|
},
|
|
3014
3027
|
(err) => {
|
|
3015
|
-
return handleError(err, code, consumer, traces, variables, lines_executed, currentToolCall);
|
|
3028
|
+
return handleError(err, code, consumer, traces, variables, lines_executed, currentToolCall, DRIVER);
|
|
3016
3029
|
}
|
|
3017
3030
|
).catch((err) => {
|
|
3018
3031
|
if (signal == null ? void 0 : signal.aborted) {
|
|
@@ -3033,13 +3046,14 @@ do {
|
|
|
3033
3046
|
}
|
|
3034
3047
|
return final;
|
|
3035
3048
|
}
|
|
3036
|
-
var handleError = (err, code, consumer, traces, variables, lines_executed, currentToolCall) => {
|
|
3049
|
+
var handleError = (err, code, consumer, traces, variables, lines_executed, currentToolCall, driver = "isolated-vm") => {
|
|
3037
3050
|
var _a3, _b, _c;
|
|
3038
3051
|
err = Signals.maybeDeserializeError(err);
|
|
3039
3052
|
const lines = code.split("\n");
|
|
3040
3053
|
const stackTrace = err.stack || "";
|
|
3054
|
+
const LINE_OFFSET = driver === "isolated-vm" ? 3 : 1;
|
|
3041
3055
|
let regex = /\(<isolated-vm>:(\d+):(\d+)/g;
|
|
3042
|
-
if (
|
|
3056
|
+
if (driver === "node") {
|
|
3043
3057
|
regex = /<anonymous>:(\d+):(\d+)/g;
|
|
3044
3058
|
}
|
|
3045
3059
|
const matches = [...stackTrace.matchAll(regex)].map((x) => {
|
|
@@ -3067,8 +3081,8 @@ var handleError = (err, code, consumer, traces, variables, lines_executed, curre
|
|
|
3067
3081
|
}
|
|
3068
3082
|
};
|
|
3069
3083
|
for (let i = 0; i < lines.length; i++) {
|
|
3070
|
-
const VM_OFFSET =
|
|
3071
|
-
const DISPLAY_OFFSET =
|
|
3084
|
+
const VM_OFFSET = driver === "isolated-vm" ? 2 : 2;
|
|
3085
|
+
const DISPLAY_OFFSET = driver === "isolated-vm" ? 2 : 0;
|
|
3072
3086
|
const line = lines[i];
|
|
3073
3087
|
const correctedStackLineIndex = i + LINE_OFFSET + VM_OFFSET;
|
|
3074
3088
|
const match = matches.find((x) => x.line + VM_OFFSET === correctedStackLineIndex);
|
|
@@ -3118,6 +3132,6 @@ var handleCatch = (err, traces, variables, lines_executed) => {
|
|
|
3118
3132
|
};
|
|
3119
3133
|
|
|
3120
3134
|
export {
|
|
3121
|
-
|
|
3135
|
+
CAN_USE_ISOLATED_VM,
|
|
3122
3136
|
runAsyncFunction
|
|
3123
3137
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -37,6 +37,7 @@ var _chunkZRCU35UVcjs = require('./chunk-ZRCU35UV.cjs');
|
|
|
37
37
|
require('./chunk-KMZDFWYZ.cjs');
|
|
38
38
|
|
|
39
39
|
|
|
40
|
+
|
|
40
41
|
var _chunkGZPN7RGHcjs = require('./chunk-GZPN7RGH.cjs');
|
|
41
42
|
|
|
42
43
|
|
|
@@ -1098,19 +1099,20 @@ var Chat = class {
|
|
|
1098
1099
|
var utils = {
|
|
1099
1100
|
toValidObjectName: _chunkWHNOR4ZUcjs.toValidObjectName,
|
|
1100
1101
|
toValidFunctionName: _chunkWHNOR4ZUcjs.toValidFunctionName,
|
|
1101
|
-
wrapContent: _chunkGZPN7RGHcjs.wrapContent
|
|
1102
|
+
wrapContent: _chunkGZPN7RGHcjs.wrapContent,
|
|
1103
|
+
truncateWrappedContent: _chunkGZPN7RGHcjs.truncateWrappedContent
|
|
1102
1104
|
};
|
|
1103
1105
|
var execute = async (props) => {
|
|
1104
|
-
const { executeContext } = await Promise.resolve().then(() => _interopRequireWildcard(require("./llmz-
|
|
1106
|
+
const { executeContext } = await Promise.resolve().then(() => _interopRequireWildcard(require("./llmz-HVMGCGKR.cjs")));
|
|
1105
1107
|
return executeContext(props);
|
|
1106
1108
|
};
|
|
1107
1109
|
var init = async () => {
|
|
1108
|
-
await Promise.resolve().then(() => _interopRequireWildcard(require("./llmz-
|
|
1110
|
+
await Promise.resolve().then(() => _interopRequireWildcard(require("./llmz-HVMGCGKR.cjs")));
|
|
1109
1111
|
await Promise.resolve().then(() => _interopRequireWildcard(require("./component-R4WTW6DZ.cjs")));
|
|
1110
1112
|
await Promise.resolve().then(() => _interopRequireWildcard(require("./tool-O4SFRIE4.cjs")));
|
|
1111
1113
|
await Promise.resolve().then(() => _interopRequireWildcard(require("./exit-XAYKJ6TR.cjs")));
|
|
1112
1114
|
await Promise.resolve().then(() => _interopRequireWildcard(require("./jsx-AJAXBWFE.cjs")));
|
|
1113
|
-
await Promise.resolve().then(() => _interopRequireWildcard(require("./vm-
|
|
1115
|
+
await Promise.resolve().then(() => _interopRequireWildcard(require("./vm-6NC73N5B.cjs")));
|
|
1114
1116
|
await Promise.resolve().then(() => _interopRequireWildcard(require("./utils-L5QAQXV2.cjs")));
|
|
1115
1117
|
await Promise.resolve().then(() => _interopRequireWildcard(require("./truncator-W3NXBLYJ.cjs")));
|
|
1116
1118
|
await Promise.resolve().then(() => _interopRequireWildcard(require("./typings-Y45GMPZT.cjs")));
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export { Snapshot } from './snapshots.js';
|
|
|
9
9
|
export { Chat, type MessageHandler } from './chat.js';
|
|
10
10
|
import { type ExecutionProps } from './llmz.js';
|
|
11
11
|
import { ExecutionResult } from './result.js';
|
|
12
|
-
import { wrapContent } from './truncator.js';
|
|
12
|
+
import { truncateWrappedContent, wrapContent } from './truncator.js';
|
|
13
13
|
export { Transcript } from './transcript.js';
|
|
14
14
|
export { ErrorExecutionResult, ExecutionResult, PartialExecutionResult, SuccessExecutionResult } from './result.js';
|
|
15
15
|
export { type Trace, type Traces } from './types.js';
|
|
@@ -21,6 +21,7 @@ export declare const utils: {
|
|
|
21
21
|
toValidObjectName: (str: string) => string;
|
|
22
22
|
toValidFunctionName: (str: string) => string;
|
|
23
23
|
wrapContent: typeof wrapContent;
|
|
24
|
+
truncateWrappedContent: typeof truncateWrappedContent;
|
|
24
25
|
};
|
|
25
26
|
/**
|
|
26
27
|
* Executes an LLMz agent in either Chat Mode or Worker Mode.
|
package/dist/index.js
CHANGED
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
} from "./chunk-GGWM6X2K.js";
|
|
37
37
|
import "./chunk-ORQP26SZ.js";
|
|
38
38
|
import {
|
|
39
|
+
truncateWrappedContent,
|
|
39
40
|
wrapContent
|
|
40
41
|
} from "./chunk-3JYCCI4S.js";
|
|
41
42
|
import {
|
|
@@ -1098,19 +1099,20 @@ var Chat = class {
|
|
|
1098
1099
|
var utils = {
|
|
1099
1100
|
toValidObjectName,
|
|
1100
1101
|
toValidFunctionName,
|
|
1101
|
-
wrapContent
|
|
1102
|
+
wrapContent,
|
|
1103
|
+
truncateWrappedContent
|
|
1102
1104
|
};
|
|
1103
1105
|
var execute = async (props) => {
|
|
1104
|
-
const { executeContext } = await import("./llmz-
|
|
1106
|
+
const { executeContext } = await import("./llmz-D2R3BIFW.js");
|
|
1105
1107
|
return executeContext(props);
|
|
1106
1108
|
};
|
|
1107
1109
|
var init = async () => {
|
|
1108
|
-
await import("./llmz-
|
|
1110
|
+
await import("./llmz-D2R3BIFW.js");
|
|
1109
1111
|
await import("./component-WFVDVSDK.js");
|
|
1110
1112
|
await import("./tool-PCOYOCRH.js");
|
|
1111
1113
|
await import("./exit-YLO7BY7Z.js");
|
|
1112
1114
|
await import("./jsx-AEHVFB3L.js");
|
|
1113
|
-
await import("./vm-
|
|
1115
|
+
await import("./vm-DNS3KON5.js");
|
|
1114
1116
|
await import("./utils-RQHQ2KOG.js");
|
|
1115
1117
|
await import("./truncator-BSP6PQPC.js");
|
|
1116
1118
|
await import("./typings-WYHEFCYB.js");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
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
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkM2MYALUNcjs = require('./chunk-M2MYALUN.cjs');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
@@ -361,7 +361,7 @@ var executeIteration = async ({
|
|
|
361
361
|
});
|
|
362
362
|
}
|
|
363
363
|
startedAt = Date.now();
|
|
364
|
-
const result = await
|
|
364
|
+
const result = await _chunkM2MYALUNcjs.runAsyncFunction.call(void 0,
|
|
365
365
|
vmContext,
|
|
366
366
|
iteration.code,
|
|
367
367
|
traces,
|
package/dist/truncator.d.ts
CHANGED
|
@@ -15,6 +15,102 @@ type TruncateOptions = {
|
|
|
15
15
|
/** If provided, the message will never truncate below that number */
|
|
16
16
|
minTokens: number;
|
|
17
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* Wraps content with truncation tags to mark it as truncatable when using `truncateWrappedContent`.
|
|
20
|
+
*
|
|
21
|
+
* This function encases the provided content within special truncation tags that contain metadata
|
|
22
|
+
* about how the content should be truncated. The wrapped content becomes eligible for intelligent
|
|
23
|
+
* truncation while preserving non-wrapped content intact.
|
|
24
|
+
*
|
|
25
|
+
* @param content - The string content to wrap with truncation tags
|
|
26
|
+
* @param options - Optional truncation configuration
|
|
27
|
+
* @param options.preserve - Which part of the content to preserve when truncating:
|
|
28
|
+
* - 'top': Keep the beginning, remove from the end (default)
|
|
29
|
+
* - 'bottom': Keep the end, remove from the beginning
|
|
30
|
+
* - 'both': Keep both ends, remove from the middle
|
|
31
|
+
* @param options.flex - Priority factor for truncation (default: 1). Higher values make this
|
|
32
|
+
* content more likely to be truncated. A flex of 2 means this content can shrink twice as
|
|
33
|
+
* much as content with flex of 1.
|
|
34
|
+
* @param options.minTokens - Minimum number of tokens to preserve (default: 0). Content will
|
|
35
|
+
* never be truncated below this threshold.
|
|
36
|
+
*
|
|
37
|
+
* @returns The content wrapped with truncation tags and embedded metadata
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* // Basic usage - content will be truncated from the end if needed
|
|
42
|
+
* const wrapped = wrapContent("This is some long content that might need truncation")
|
|
43
|
+
*
|
|
44
|
+
* // Preserve the end of the content
|
|
45
|
+
* const bottomPreserved = wrapContent("Error log: ... important error details", {
|
|
46
|
+
* preserve: 'bottom'
|
|
47
|
+
* })
|
|
48
|
+
*
|
|
49
|
+
* // High priority for truncation with minimum preservation
|
|
50
|
+
* const flexible = wrapContent("Optional context information", {
|
|
51
|
+
* flex: 3,
|
|
52
|
+
* minTokens: 50
|
|
53
|
+
* })
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
18
56
|
export declare function wrapContent(content: string, options?: Partial<TruncateOptions>): string;
|
|
57
|
+
/**
|
|
58
|
+
* Intelligently truncates message content to fit within a token limit while preserving important parts.
|
|
59
|
+
*
|
|
60
|
+
* This function processes an array of messages and reduces their total token count to fit within the
|
|
61
|
+
* specified limit. It only truncates content that has been wrapped with `wrapContent()`, leaving
|
|
62
|
+
* unwrapped content completely intact. The truncation algorithm prioritizes content based on flex
|
|
63
|
+
* values and respects preservation preferences and minimum token requirements.
|
|
64
|
+
*
|
|
65
|
+
* ## How it works:
|
|
66
|
+
* 1. **Parsing**: Scans each message for wrapped content sections and unwrapped sections
|
|
67
|
+
* 2. **Token counting**: Calculates tokens for each section using the configured tokenizer
|
|
68
|
+
* 3. **Prioritization**: Identifies the largest truncatable sections based on flex values
|
|
69
|
+
* 4. **Intelligent truncation**: Removes content according to preservation preferences
|
|
70
|
+
* 5. **Reconstruction**: Rebuilds messages with truncated content and removes wrapper tags
|
|
71
|
+
*
|
|
72
|
+
* ## Truncation strategy:
|
|
73
|
+
* - **Priority**: Higher flex values = higher truncation priority
|
|
74
|
+
* - **Minimum tokens**: Content is never truncated below its `minTokens` threshold
|
|
75
|
+
* - **Preservation modes**:
|
|
76
|
+
* - `'top'`: Removes from the end, keeps the beginning
|
|
77
|
+
* - `'bottom'`: Removes from the beginning, keeps the end
|
|
78
|
+
* - `'both'`: Removes from the middle, keeps both ends
|
|
79
|
+
*
|
|
80
|
+
* @template T - Type extending MessageLike (must have a content property)
|
|
81
|
+
* @param options - Configuration object
|
|
82
|
+
* @param options.messages - Array of messages to truncate
|
|
83
|
+
* @param options.tokenLimit - Maximum total tokens allowed across all messages
|
|
84
|
+
* @param options.throwOnFailure - Whether to throw an error if truncation fails (default: true).
|
|
85
|
+
* If false, returns the best effort result even if over the token limit.
|
|
86
|
+
*
|
|
87
|
+
* @returns Array of messages with content truncated to fit the token limit
|
|
88
|
+
*
|
|
89
|
+
* @throws Error if unable to truncate enough content to meet the token limit (when throwOnFailure is true)
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const messages = [
|
|
94
|
+
* {
|
|
95
|
+
* role: 'system',
|
|
96
|
+
* content: 'You are a helpful assistant. ' + wrapContent('Here is some background info...', { flex: 2 })
|
|
97
|
+
* },
|
|
98
|
+
* {
|
|
99
|
+
* role: 'user',
|
|
100
|
+
* content: 'Please help me with: ' + wrapContent('detailed context and examples', { preserve: 'both' })
|
|
101
|
+
* }
|
|
102
|
+
* ]
|
|
103
|
+
*
|
|
104
|
+
* // Truncate to fit within 1000 tokens
|
|
105
|
+
* const truncated = truncateWrappedContent({
|
|
106
|
+
* messages,
|
|
107
|
+
* tokenLimit: 1000,
|
|
108
|
+
* throwOnFailure: false
|
|
109
|
+
* })
|
|
110
|
+
*
|
|
111
|
+
* // The system message background info will be truncated first (higher flex),
|
|
112
|
+
* // and user context will be truncated from the middle if needed (preserve: 'both')
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
19
115
|
export declare function truncateWrappedContent<T extends MessageLike>({ messages, tokenLimit, throwOnFailure, }: Options<T>): T[];
|
|
20
116
|
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var
|
|
4
|
+
var _chunkM2MYALUNcjs = require('./chunk-M2MYALUN.cjs');
|
|
5
5
|
require('./chunk-JDABP4SD.cjs');
|
|
6
6
|
require('./chunk-IKSIOIIP.cjs');
|
|
7
7
|
require('./chunk-KMZDFWYZ.cjs');
|
|
@@ -9,4 +9,4 @@ require('./chunk-UQOBUJIQ.cjs');
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
exports.
|
|
12
|
+
exports.CAN_USE_ISOLATED_VM = _chunkM2MYALUNcjs.CAN_USE_ISOLATED_VM; exports.runAsyncFunction = _chunkM2MYALUNcjs.runAsyncFunction;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
CAN_USE_ISOLATED_VM,
|
|
3
3
|
runAsyncFunction
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-VIMCBE6B.js";
|
|
5
5
|
import "./chunk-JKVVQN2P.js";
|
|
6
6
|
import "./chunk-JQBT7UWN.js";
|
|
7
7
|
import "./chunk-ORQP26SZ.js";
|
|
8
8
|
import "./chunk-7WRN4E42.js";
|
|
9
9
|
export {
|
|
10
|
-
|
|
10
|
+
CAN_USE_ISOLATED_VM,
|
|
11
11
|
runAsyncFunction
|
|
12
12
|
};
|
package/dist/vm.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Trace, VMExecutionResult } from './types.js';
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const CAN_USE_ISOLATED_VM: boolean;
|
|
3
3
|
export declare function runAsyncFunction(context: any, code: string, traces?: Trace[], signal?: AbortSignal | null, timeout?: number): Promise<VMExecutionResult>;
|
package/package.json
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
"name": "llmz",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"description": "LLMz – An LLM-native Typescript VM built on top of Zui",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.18",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
7
9
|
"exports": {
|
|
8
10
|
".": {
|
|
9
11
|
"import": "./dist/index.js",
|
|
@@ -32,7 +34,7 @@
|
|
|
32
34
|
"@babel/standalone": "^7.26.4",
|
|
33
35
|
"@babel/traverse": "^7.26.4",
|
|
34
36
|
"@babel/types": "^7.26.3",
|
|
35
|
-
"@botpress/client": "1.
|
|
37
|
+
"@botpress/client": "1.21.0",
|
|
36
38
|
"bytes": "^3.1.2",
|
|
37
39
|
"exponential-backoff": "^3.1.1",
|
|
38
40
|
"handlebars": "^4.7.8",
|