llmz 0.0.6 → 0.0.7
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/README.md +0 -4
- package/package.json +5 -3
- package/dist/chat.d.ts +0 -17
- package/dist/chunk-4I7UPBLN.cjs +0 -104
- package/dist/chunk-4QTK5ALH.cjs +0 -211
- package/dist/chunk-5TRUJES5.js +0 -220
- package/dist/chunk-6WT5VZBG.cjs +0 -755
- package/dist/chunk-7WRN4E42.js +0 -3087
- package/dist/chunk-BEPRLBPK.cjs +0 -3123
- package/dist/chunk-CH5T6SPC.cjs +0 -694
- package/dist/chunk-D3ESDRLH.js +0 -3123
- package/dist/chunk-EJRCDWBA.cjs +0 -153
- package/dist/chunk-FIVFS4HG.cjs +0 -152
- package/dist/chunk-HP7RKM25.js +0 -153
- package/dist/chunk-HYVKY3W6.cjs +0 -283
- package/dist/chunk-IKSIOIIP.cjs +0 -28
- package/dist/chunk-JDABP4SD.cjs +0 -161
- package/dist/chunk-JKVVQN2P.js +0 -161
- package/dist/chunk-JQBT7UWN.js +0 -28
- package/dist/chunk-KMZDFWYZ.cjs +0 -33
- package/dist/chunk-ORQP26SZ.js +0 -33
- package/dist/chunk-P7J2WCBB.cjs +0 -220
- package/dist/chunk-QBXIE6EY.js +0 -211
- package/dist/chunk-QT4QF3YA.js +0 -152
- package/dist/chunk-S6FOL2HY.js +0 -104
- package/dist/chunk-UNMKB2DA.js +0 -283
- package/dist/chunk-UQOBUJIQ.cjs +0 -3087
- package/dist/chunk-WLUVY5QU.js +0 -755
- package/dist/chunk-XJNRORSK.js +0 -694
- package/dist/citations.d.ts +0 -63
- package/dist/compiler/compiler.d.ts +0 -28
- package/dist/compiler/index.d.ts +0 -2
- package/dist/compiler/plugins/async-iterator.d.ts +0 -4
- package/dist/compiler/plugins/braces-tsx.d.ts +0 -3
- package/dist/compiler/plugins/jsx-preserve-newlines.d.ts +0 -5
- package/dist/compiler/plugins/line-tracking.d.ts +0 -5
- package/dist/compiler/plugins/replace-comment.d.ts +0 -5
- package/dist/compiler/plugins/return-async.d.ts +0 -4
- package/dist/compiler/plugins/track-tool-calls.d.ts +0 -16
- package/dist/compiler/plugins/variable-extraction.d.ts +0 -5
- package/dist/component-R2Y74VUI.js +0 -16
- package/dist/component-TSNW3SC7.cjs +0 -16
- package/dist/component.d.ts +0 -60
- package/dist/component.default.d.ts +0 -316
- package/dist/context.d.ts +0 -177
- package/dist/dual-modes-UHNDHNIF.cjs +0 -12
- package/dist/dual-modes-ZUQKPJFH.js +0 -12
- package/dist/errors.d.ts +0 -64
- package/dist/exit-KJ4COC5N.cjs +0 -8
- package/dist/exit-OIYZLBVJ.js +0 -8
- package/dist/exit.d.ts +0 -26
- package/dist/formatting.d.ts +0 -5
- package/dist/getter.d.ts +0 -2
- package/dist/handlers.d.ts +0 -10
- package/dist/hoist.d.ts +0 -2
- package/dist/index.cjs +0 -919
- package/dist/index.d.ts +0 -22
- package/dist/index.js +0 -919
- package/dist/inspect.d.ts +0 -6
- package/dist/jsx-AEHVFB3L.js +0 -13
- package/dist/jsx-AJAXBWFE.cjs +0 -13
- package/dist/jsx.d.ts +0 -11
- package/dist/llmz-CPMVYIOJ.cjs +0 -581
- package/dist/llmz-LAN4WDFW.js +0 -581
- package/dist/llmz.d.ts +0 -40
- package/dist/objects.d.ts +0 -24
- package/dist/openai.d.ts +0 -8
- package/dist/prompts/chat-mode/system.md.d.ts +0 -2
- package/dist/prompts/chat-mode/user.md.d.ts +0 -2
- package/dist/prompts/common.d.ts +0 -6
- package/dist/prompts/dual-modes.d.ts +0 -2
- package/dist/prompts/prompt.d.ts +0 -50
- package/dist/prompts/worker-mode/system.md.d.ts +0 -2
- package/dist/prompts/worker-mode/user.md.d.ts +0 -2
- package/dist/result.d.ts +0 -33
- package/dist/snapshots.d.ts +0 -69
- package/dist/stack-traces.d.ts +0 -1
- package/dist/tool-LY77IWV2.js +0 -11
- package/dist/tool-SWJYOR2Z.cjs +0 -11
- package/dist/tool.d.ts +0 -55
- package/dist/transcript.d.ts +0 -9
- package/dist/truncator-NYN7BGKJ.cjs +0 -10
- package/dist/truncator-WAL2GCUY.js +0 -10
- package/dist/truncator.d.ts +0 -21
- package/dist/types.d.ts +0 -105
- package/dist/typings-AIV2OASX.js +0 -10
- package/dist/typings-XR6CYHW4.cjs +0 -10
- package/dist/typings.d.ts +0 -5
- package/dist/utils-L5GLCS3C.js +0 -37
- package/dist/utils-QC4I2L6R.cjs +0 -37
- package/dist/utils.d.ts +0 -21
- package/dist/vm-2DLG7V4G.cjs +0 -12
- package/dist/vm-FLBMZUA2.js +0 -12
- package/dist/vm.d.ts +0 -3
package/dist/chunk-XJNRORSK.js
DELETED
|
@@ -1,694 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DualModePrompt,
|
|
3
|
-
extractType,
|
|
4
|
-
inspect
|
|
5
|
-
} from "./chunk-WLUVY5QU.js";
|
|
6
|
-
import {
|
|
7
|
-
Tool
|
|
8
|
-
} from "./chunk-QBXIE6EY.js";
|
|
9
|
-
import {
|
|
10
|
-
LoopExceededError
|
|
11
|
-
} from "./chunk-JKVVQN2P.js";
|
|
12
|
-
import {
|
|
13
|
-
Exit
|
|
14
|
-
} from "./chunk-S6FOL2HY.js";
|
|
15
|
-
import {
|
|
16
|
-
assertValidComponent
|
|
17
|
-
} from "./chunk-HP7RKM25.js";
|
|
18
|
-
import {
|
|
19
|
-
wrapContent
|
|
20
|
-
} from "./chunk-QT4QF3YA.js";
|
|
21
|
-
import {
|
|
22
|
-
cloneDeep_default,
|
|
23
|
-
isPlainObject_default
|
|
24
|
-
} from "./chunk-7WRN4E42.js";
|
|
25
|
-
|
|
26
|
-
// src/snapshots.ts
|
|
27
|
-
import { ulid } from "ulid";
|
|
28
|
-
var MAX_SNAPSHOT_SIZE_BYTES = 4e3;
|
|
29
|
-
var Snapshot = class _Snapshot {
|
|
30
|
-
id;
|
|
31
|
-
reason;
|
|
32
|
-
stack;
|
|
33
|
-
variables;
|
|
34
|
-
toolCall;
|
|
35
|
-
#status;
|
|
36
|
-
get status() {
|
|
37
|
-
return Object.freeze({ ...this.#status });
|
|
38
|
-
}
|
|
39
|
-
constructor(props) {
|
|
40
|
-
this.id = props.id;
|
|
41
|
-
this.stack = props.stack;
|
|
42
|
-
this.reason = props.reason;
|
|
43
|
-
this.variables = props.variables;
|
|
44
|
-
this.toolCall = props.toolCall;
|
|
45
|
-
this.#status = props.status;
|
|
46
|
-
}
|
|
47
|
-
static fromSignal(signal) {
|
|
48
|
-
const variables = Object.entries(signal.variables).map(([name, value]) => {
|
|
49
|
-
const type = extractType(value);
|
|
50
|
-
const bytes = JSON.stringify(value || "").length;
|
|
51
|
-
const truncated = bytes > MAX_SNAPSHOT_SIZE_BYTES;
|
|
52
|
-
return truncated ? { name, type, bytes, truncated: true, preview: inspect(value, name) ?? "N/A" } : { name, type, bytes, truncated: false, value };
|
|
53
|
-
});
|
|
54
|
-
return new _Snapshot({
|
|
55
|
-
id: "snapshot_" + ulid(),
|
|
56
|
-
reason: signal.message,
|
|
57
|
-
stack: signal.truncatedCode,
|
|
58
|
-
variables,
|
|
59
|
-
toolCall: signal.toolCall,
|
|
60
|
-
status: { type: "pending" }
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
toJSON() {
|
|
64
|
-
return {
|
|
65
|
-
id: this.id,
|
|
66
|
-
reason: this.reason,
|
|
67
|
-
stack: this.stack,
|
|
68
|
-
variables: this.variables,
|
|
69
|
-
toolCall: this.toolCall,
|
|
70
|
-
status: this.#status
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
static fromJSON(json) {
|
|
74
|
-
return new _Snapshot({
|
|
75
|
-
id: json.id,
|
|
76
|
-
reason: json.reason,
|
|
77
|
-
stack: json.stack,
|
|
78
|
-
variables: json.variables,
|
|
79
|
-
toolCall: json.toolCall,
|
|
80
|
-
status: json.status
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
clone() {
|
|
84
|
-
return new _Snapshot({
|
|
85
|
-
id: this.id,
|
|
86
|
-
reason: this.reason,
|
|
87
|
-
stack: this.stack,
|
|
88
|
-
variables: this.variables,
|
|
89
|
-
toolCall: this.toolCall,
|
|
90
|
-
status: this.#status
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
reset() {
|
|
94
|
-
this.#status = { type: "pending" };
|
|
95
|
-
}
|
|
96
|
-
resolve(value) {
|
|
97
|
-
if (this.#status.type !== "pending") {
|
|
98
|
-
throw new Error(`Cannot resolve snapshot because it is already settled: ${this.#status.type}`);
|
|
99
|
-
}
|
|
100
|
-
this.#status = { type: "resolved", value };
|
|
101
|
-
}
|
|
102
|
-
reject(error) {
|
|
103
|
-
if (this.#status.type !== "pending") {
|
|
104
|
-
throw new Error(`Cannot reject snapshot because it is already settled: ${this.#status.type}`);
|
|
105
|
-
}
|
|
106
|
-
this.#status = { type: "rejected", error };
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
// src/result.ts
|
|
111
|
-
var ExecutionResult = class {
|
|
112
|
-
status;
|
|
113
|
-
context;
|
|
114
|
-
constructor(status, context) {
|
|
115
|
-
this.status = status;
|
|
116
|
-
this.context = context;
|
|
117
|
-
}
|
|
118
|
-
isSuccess() {
|
|
119
|
-
return this.status === "success" && this instanceof SuccessExecutionResult;
|
|
120
|
-
}
|
|
121
|
-
isError() {
|
|
122
|
-
return this.status === "error" && this instanceof ErrorExecutionResult;
|
|
123
|
-
}
|
|
124
|
-
isInterrupted() {
|
|
125
|
-
return this.status === "interrupted" && this instanceof PartialExecutionResult;
|
|
126
|
-
}
|
|
127
|
-
is(exit) {
|
|
128
|
-
return this.status === "success" && this instanceof SuccessExecutionResult && this.result.exit === exit;
|
|
129
|
-
}
|
|
130
|
-
get output() {
|
|
131
|
-
return this.isSuccess() ? this.result.result : null;
|
|
132
|
-
}
|
|
133
|
-
get iteration() {
|
|
134
|
-
return this.context.iterations.at(-1) || null;
|
|
135
|
-
}
|
|
136
|
-
get iterations() {
|
|
137
|
-
return this.context.iterations ?? [];
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
var SuccessExecutionResult = class extends ExecutionResult {
|
|
141
|
-
result;
|
|
142
|
-
constructor(context, result) {
|
|
143
|
-
super("success", context);
|
|
144
|
-
this.result = result;
|
|
145
|
-
}
|
|
146
|
-
get output() {
|
|
147
|
-
return this.result.result;
|
|
148
|
-
}
|
|
149
|
-
get iteration() {
|
|
150
|
-
return this.context.iterations.at(-1);
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
var ErrorExecutionResult = class extends ExecutionResult {
|
|
154
|
-
error;
|
|
155
|
-
constructor(context, error) {
|
|
156
|
-
super("error", context);
|
|
157
|
-
this.error = error;
|
|
158
|
-
}
|
|
159
|
-
get output() {
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
var PartialExecutionResult = class extends ExecutionResult {
|
|
164
|
-
signal;
|
|
165
|
-
snapshot;
|
|
166
|
-
constructor(context, signal, snapshot) {
|
|
167
|
-
super("interrupted", context);
|
|
168
|
-
this.signal = signal;
|
|
169
|
-
this.snapshot = snapshot;
|
|
170
|
-
}
|
|
171
|
-
get output() {
|
|
172
|
-
return null;
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
// src/getter.ts
|
|
177
|
-
var getValue = async (valueOrGetter, ctx) => {
|
|
178
|
-
if (typeof valueOrGetter === "function") {
|
|
179
|
-
try {
|
|
180
|
-
return await valueOrGetter(ctx);
|
|
181
|
-
} catch (e) {
|
|
182
|
-
throw new Error(`Error while getting value for ${valueOrGetter}: ${e}`);
|
|
183
|
-
}
|
|
184
|
-
} else {
|
|
185
|
-
return valueOrGetter;
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
// src/context.ts
|
|
190
|
-
import { z } from "@bpinternal/zui";
|
|
191
|
-
import { ulid as ulid2 } from "ulid";
|
|
192
|
-
|
|
193
|
-
// src/handlers.ts
|
|
194
|
-
var Emitter = class {
|
|
195
|
-
_handlers = [];
|
|
196
|
-
subscribe = (fn) => {
|
|
197
|
-
this._handlers.push(fn);
|
|
198
|
-
return () => {
|
|
199
|
-
this._handlers = this._handlers.filter((handler) => handler !== fn);
|
|
200
|
-
};
|
|
201
|
-
};
|
|
202
|
-
emit(event) {
|
|
203
|
-
this._handlers.forEach((handler) => handler(event));
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
var HookedArray = class extends Array {
|
|
207
|
-
#listeners = new Emitter();
|
|
208
|
-
constructor(...items) {
|
|
209
|
-
super(...items);
|
|
210
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
211
|
-
}
|
|
212
|
-
push(...items) {
|
|
213
|
-
try {
|
|
214
|
-
this.#listeners.emit(items);
|
|
215
|
-
} finally {
|
|
216
|
-
return super.push(...items);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
onPush(fn) {
|
|
220
|
-
return this.#listeners.subscribe(fn);
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
// src/transcript.ts
|
|
225
|
-
var MAX_MESSAGE_LENGTH = 5e3;
|
|
226
|
-
var TranscriptArray = class extends Array {
|
|
227
|
-
constructor(items = []) {
|
|
228
|
-
items = Array.isArray(items) ? items : [];
|
|
229
|
-
super(...items);
|
|
230
|
-
items.forEach((item) => {
|
|
231
|
-
if (!["user", "assistant"].includes(item.role)) {
|
|
232
|
-
throw new Error(`Invalid role "${item.role}" in transcript message`);
|
|
233
|
-
}
|
|
234
|
-
if (item.name && typeof item.name !== "string") {
|
|
235
|
-
throw new Error(`Invalid name for transcript message. Expected a string, but got type "${typeof item.name}"`);
|
|
236
|
-
}
|
|
237
|
-
if (typeof item.content !== "string") {
|
|
238
|
-
throw new Error(
|
|
239
|
-
`Invalid content for transcript message. Expected a string, but got type "${typeof item.content}"`
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
244
|
-
}
|
|
245
|
-
toString() {
|
|
246
|
-
if (!this.length) {
|
|
247
|
-
return "";
|
|
248
|
-
}
|
|
249
|
-
return this.map((item, idx) => {
|
|
250
|
-
var _a;
|
|
251
|
-
const msgIdx = "message-" + String(idx + 1).padStart(3, "0");
|
|
252
|
-
const shouldTruncate = item.content.length > MAX_MESSAGE_LENGTH;
|
|
253
|
-
const preview = shouldTruncate ? item.content.slice(0, MAX_MESSAGE_LENGTH) + "\n... (truncated)" : item.content;
|
|
254
|
-
const tags = [];
|
|
255
|
-
tags.push({ key: "role", value: item.role });
|
|
256
|
-
if ((_a = item.name) == null ? void 0 : _a.length) {
|
|
257
|
-
tags.push({ key: "name", value: item.name });
|
|
258
|
-
}
|
|
259
|
-
const tagsString = tags.map(({ key, value }) => `${key}="${value}"`).join(" ");
|
|
260
|
-
return `<${msgIdx} ${tagsString}>
|
|
261
|
-
${preview.trim()}
|
|
262
|
-
</${msgIdx}>`;
|
|
263
|
-
}).join("\n");
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
// src/context.ts
|
|
268
|
-
var ThinkExit = new Exit({
|
|
269
|
-
name: "think",
|
|
270
|
-
description: "Think about the current situation and provide a response"
|
|
271
|
-
});
|
|
272
|
-
var ListenExit = new Exit({
|
|
273
|
-
name: "listen",
|
|
274
|
-
description: "Listen to the user and provide a response"
|
|
275
|
-
});
|
|
276
|
-
var DefaultExit = new Exit({
|
|
277
|
-
name: "done",
|
|
278
|
-
description: "When the execution is sucessfully completed or when error recovery is not possible",
|
|
279
|
-
schema: z.discriminatedUnion("success", [
|
|
280
|
-
z.object({
|
|
281
|
-
success: z.literal(true),
|
|
282
|
-
result: z.any().describe("The result of the execution")
|
|
283
|
-
}),
|
|
284
|
-
z.object({
|
|
285
|
-
success: z.literal(false),
|
|
286
|
-
error: z.string().describe("The error message if the execution failed")
|
|
287
|
-
})
|
|
288
|
-
])
|
|
289
|
-
});
|
|
290
|
-
var Iteration = class {
|
|
291
|
-
id;
|
|
292
|
-
messages;
|
|
293
|
-
code;
|
|
294
|
-
traces;
|
|
295
|
-
variables;
|
|
296
|
-
started_ts;
|
|
297
|
-
ended_ts;
|
|
298
|
-
status;
|
|
299
|
-
_mutations;
|
|
300
|
-
get mutations() {
|
|
301
|
-
return [...this._mutations.values()];
|
|
302
|
-
}
|
|
303
|
-
trackMutation(mutation) {
|
|
304
|
-
this._mutations.set(`${mutation.object ?? "global"}:${mutation.property}`, mutation);
|
|
305
|
-
}
|
|
306
|
-
_parameters;
|
|
307
|
-
get transcript() {
|
|
308
|
-
return this._parameters.transcript;
|
|
309
|
-
}
|
|
310
|
-
get tools() {
|
|
311
|
-
return this._parameters.tools;
|
|
312
|
-
}
|
|
313
|
-
get objects() {
|
|
314
|
-
return this._parameters.objects;
|
|
315
|
-
}
|
|
316
|
-
get exits() {
|
|
317
|
-
const exits = [...this._parameters.exits, ThinkExit];
|
|
318
|
-
if (this.isChatEnabled) {
|
|
319
|
-
exits.push(ListenExit);
|
|
320
|
-
}
|
|
321
|
-
return exits;
|
|
322
|
-
}
|
|
323
|
-
get instructions() {
|
|
324
|
-
return this._parameters.instructions;
|
|
325
|
-
}
|
|
326
|
-
llm;
|
|
327
|
-
hasExited() {
|
|
328
|
-
return ["exit_success"].includes(this.status.type);
|
|
329
|
-
}
|
|
330
|
-
hasExitedWith(exit) {
|
|
331
|
-
return this.status.type === "exit_success" && this.status.exit_success.exit_name === exit.name;
|
|
332
|
-
}
|
|
333
|
-
isSuccessful() {
|
|
334
|
-
return ["callback_requested", "exit_success", "thinking_requested"].includes(
|
|
335
|
-
this.status.type
|
|
336
|
-
);
|
|
337
|
-
}
|
|
338
|
-
isFailed() {
|
|
339
|
-
return [
|
|
340
|
-
"generation_error",
|
|
341
|
-
"invalid_code_error",
|
|
342
|
-
"execution_error",
|
|
343
|
-
"exit_error",
|
|
344
|
-
"aborted"
|
|
345
|
-
].includes(this.status.type);
|
|
346
|
-
}
|
|
347
|
-
get duration() {
|
|
348
|
-
const ms = (this.ended_ts ?? Date.now()) - this.started_ts;
|
|
349
|
-
const trailing = this.ended_ts ? "" : " (still running)";
|
|
350
|
-
return ms.toLocaleString("en-US", { style: "unit", unit: "millisecond" }) + trailing;
|
|
351
|
-
}
|
|
352
|
-
get error() {
|
|
353
|
-
if (this.status.type === "generation_error") {
|
|
354
|
-
return `CodeGenerationError: ${this.status.generation_error.message}`;
|
|
355
|
-
}
|
|
356
|
-
if (this.status.type === "invalid_code_error") {
|
|
357
|
-
return `InvalidCodeError: ${this.status.invalid_code_error.message}`;
|
|
358
|
-
}
|
|
359
|
-
if (this.status.type === "execution_error") {
|
|
360
|
-
return `CodeExecutionError: ${this.status.execution_error.message}`;
|
|
361
|
-
}
|
|
362
|
-
if (this.status.type === "exit_error") {
|
|
363
|
-
return `ExitError: ${this.status.exit_error.message}`;
|
|
364
|
-
}
|
|
365
|
-
if (this.status.type === "aborted") {
|
|
366
|
-
return `Aborted: ${this.status.aborted.reason}`;
|
|
367
|
-
}
|
|
368
|
-
return null;
|
|
369
|
-
}
|
|
370
|
-
get isChatEnabled() {
|
|
371
|
-
return this._parameters.tools.find((x) => x.name.toLowerCase() === "message") !== void 0;
|
|
372
|
-
}
|
|
373
|
-
constructor(props) {
|
|
374
|
-
this.id = props.id;
|
|
375
|
-
this.status = { type: "pending" };
|
|
376
|
-
this.traces = new HookedArray();
|
|
377
|
-
this._mutations = /* @__PURE__ */ new Map();
|
|
378
|
-
this.messages = props.messages;
|
|
379
|
-
this.variables = props.variables;
|
|
380
|
-
this._parameters = props.parameters;
|
|
381
|
-
this.started_ts = Date.now();
|
|
382
|
-
}
|
|
383
|
-
end(status) {
|
|
384
|
-
if (this.status.type !== "pending") {
|
|
385
|
-
throw new Error(`Iteration ${this.id} has already ended with status ${this.status.type}`);
|
|
386
|
-
}
|
|
387
|
-
this.ended_ts = Date.now();
|
|
388
|
-
this.status = status;
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
var Context = class {
|
|
392
|
-
id;
|
|
393
|
-
chat;
|
|
394
|
-
instructions;
|
|
395
|
-
objects;
|
|
396
|
-
tools;
|
|
397
|
-
exits;
|
|
398
|
-
version = DualModePrompt;
|
|
399
|
-
timeout = 6e4;
|
|
400
|
-
// Default timeout of 60 seconds
|
|
401
|
-
loop;
|
|
402
|
-
temperature;
|
|
403
|
-
model;
|
|
404
|
-
metadata;
|
|
405
|
-
snapshot;
|
|
406
|
-
iteration = 0;
|
|
407
|
-
iterations;
|
|
408
|
-
async nextIteration() {
|
|
409
|
-
if (this.iterations.length >= this.loop) {
|
|
410
|
-
throw new LoopExceededError();
|
|
411
|
-
}
|
|
412
|
-
if (this.snapshot && this.snapshot.status.type === "pending") {
|
|
413
|
-
throw new Error(
|
|
414
|
-
`Cannot resume execution from a snapshot that is still pending: ${this.snapshot.id}. Please resolve() or reject() it first.`
|
|
415
|
-
);
|
|
416
|
-
}
|
|
417
|
-
const parameters = await this._refreshIterationParameters();
|
|
418
|
-
const messages = await this._getIterationMessages(parameters);
|
|
419
|
-
const iteration = new Iteration({
|
|
420
|
-
id: `${this.id}_${this.iterations.length + 1}`,
|
|
421
|
-
variables: this._getIterationVariables(),
|
|
422
|
-
parameters,
|
|
423
|
-
messages
|
|
424
|
-
});
|
|
425
|
-
this.iterations.push(iteration);
|
|
426
|
-
this.iteration = this.iterations.length;
|
|
427
|
-
this.snapshot = void 0;
|
|
428
|
-
return iteration;
|
|
429
|
-
}
|
|
430
|
-
_getIterationVariables() {
|
|
431
|
-
const lastIteration = this.iterations.at(-1);
|
|
432
|
-
const variables = {};
|
|
433
|
-
if ((lastIteration == null ? void 0 : lastIteration.status.type) === "thinking_requested") {
|
|
434
|
-
const lastThinkingVariables = lastIteration.status.thinking_requested.variables;
|
|
435
|
-
if (isPlainObject_default(lastThinkingVariables)) {
|
|
436
|
-
Object.assign(variables, cloneDeep_default(lastThinkingVariables));
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
if (isPlainObject_default(lastIteration == null ? void 0 : lastIteration.variables)) {
|
|
440
|
-
Object.assign(variables, cloneDeep_default((lastIteration == null ? void 0 : lastIteration.variables) ?? {}));
|
|
441
|
-
}
|
|
442
|
-
return variables;
|
|
443
|
-
}
|
|
444
|
-
async _getIterationMessages(parameters) {
|
|
445
|
-
var _a, _b, _c, _d, _e, _f;
|
|
446
|
-
const lastIteration = this.iterations.at(-1);
|
|
447
|
-
if (((_a = this.snapshot) == null ? void 0 : _a.status.type) === "resolved") {
|
|
448
|
-
return [
|
|
449
|
-
await this.version.getSystemMessage({
|
|
450
|
-
globalTools: parameters.tools,
|
|
451
|
-
objects: parameters.objects,
|
|
452
|
-
instructions: parameters.instructions,
|
|
453
|
-
transcript: parameters.transcript,
|
|
454
|
-
exits: parameters.exits,
|
|
455
|
-
components: parameters.components
|
|
456
|
-
}),
|
|
457
|
-
this.version.getSnapshotResolvedMessage({
|
|
458
|
-
snapshot: this.snapshot
|
|
459
|
-
})
|
|
460
|
-
];
|
|
461
|
-
}
|
|
462
|
-
if (((_b = this.snapshot) == null ? void 0 : _b.status.type) === "rejected") {
|
|
463
|
-
return [
|
|
464
|
-
await this.version.getSystemMessage({
|
|
465
|
-
globalTools: parameters.tools,
|
|
466
|
-
objects: parameters.objects,
|
|
467
|
-
instructions: parameters.instructions,
|
|
468
|
-
transcript: parameters.transcript,
|
|
469
|
-
exits: parameters.exits,
|
|
470
|
-
components: parameters.components
|
|
471
|
-
}),
|
|
472
|
-
this.version.getSnapshotRejectedMessage({
|
|
473
|
-
snapshot: this.snapshot
|
|
474
|
-
})
|
|
475
|
-
];
|
|
476
|
-
}
|
|
477
|
-
if (!lastIteration) {
|
|
478
|
-
return [
|
|
479
|
-
await this.version.getSystemMessage({
|
|
480
|
-
globalTools: parameters.tools,
|
|
481
|
-
objects: parameters.objects,
|
|
482
|
-
instructions: parameters.instructions,
|
|
483
|
-
transcript: parameters.transcript,
|
|
484
|
-
exits: parameters.exits,
|
|
485
|
-
components: parameters.components
|
|
486
|
-
}),
|
|
487
|
-
await this.version.getInitialUserMessage({
|
|
488
|
-
globalTools: parameters.tools,
|
|
489
|
-
objects: parameters.objects,
|
|
490
|
-
instructions: parameters.instructions,
|
|
491
|
-
transcript: parameters.transcript,
|
|
492
|
-
exits: parameters.exits,
|
|
493
|
-
components: parameters.components
|
|
494
|
-
})
|
|
495
|
-
];
|
|
496
|
-
}
|
|
497
|
-
const lastIterationMessages = [
|
|
498
|
-
await this.version.getSystemMessage({
|
|
499
|
-
globalTools: parameters.tools,
|
|
500
|
-
objects: parameters.objects,
|
|
501
|
-
instructions: parameters.instructions,
|
|
502
|
-
transcript: parameters.transcript,
|
|
503
|
-
exits: parameters.exits,
|
|
504
|
-
components: parameters.components
|
|
505
|
-
}),
|
|
506
|
-
...lastIteration.messages.filter((x) => x.role !== "system")
|
|
507
|
-
];
|
|
508
|
-
if ((lastIteration == null ? void 0 : lastIteration.status.type) === "thinking_requested") {
|
|
509
|
-
return [
|
|
510
|
-
...lastIterationMessages,
|
|
511
|
-
{
|
|
512
|
-
role: "assistant",
|
|
513
|
-
content: wrapContent(((_c = lastIteration.llm) == null ? void 0 : _c.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
|
|
514
|
-
},
|
|
515
|
-
await this.version.getThinkingMessage({
|
|
516
|
-
reason: lastIteration.status.thinking_requested.reason,
|
|
517
|
-
variables: lastIteration.status.thinking_requested.variables
|
|
518
|
-
})
|
|
519
|
-
];
|
|
520
|
-
}
|
|
521
|
-
if ((lastIteration == null ? void 0 : lastIteration.status.type) === "exit_error") {
|
|
522
|
-
return [
|
|
523
|
-
...lastIterationMessages,
|
|
524
|
-
{
|
|
525
|
-
role: "assistant",
|
|
526
|
-
content: wrapContent(((_d = lastIteration.llm) == null ? void 0 : _d.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
|
|
527
|
-
},
|
|
528
|
-
await this.version.getInvalidCodeMessage({
|
|
529
|
-
code: lastIteration.code ?? "// No code generated",
|
|
530
|
-
message: `Invalid return statement (action: ${lastIteration.status.exit_error.exit}): ${lastIteration.status.exit_error.message}`
|
|
531
|
-
})
|
|
532
|
-
];
|
|
533
|
-
}
|
|
534
|
-
if ((lastIteration == null ? void 0 : lastIteration.status.type) === "invalid_code_error") {
|
|
535
|
-
return [
|
|
536
|
-
...lastIterationMessages,
|
|
537
|
-
{
|
|
538
|
-
role: "assistant",
|
|
539
|
-
content: wrapContent(((_e = lastIteration.llm) == null ? void 0 : _e.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
|
|
540
|
-
},
|
|
541
|
-
await this.version.getInvalidCodeMessage({
|
|
542
|
-
code: lastIteration.code ?? "// No code generated",
|
|
543
|
-
message: lastIteration.status.invalid_code_error.message
|
|
544
|
-
})
|
|
545
|
-
];
|
|
546
|
-
}
|
|
547
|
-
if ((lastIteration == null ? void 0 : lastIteration.status.type) === "execution_error") {
|
|
548
|
-
return [
|
|
549
|
-
...lastIterationMessages,
|
|
550
|
-
{
|
|
551
|
-
role: "assistant",
|
|
552
|
-
content: wrapContent(((_f = lastIteration.llm) == null ? void 0 : _f.output) ?? "", { preserve: "top", flex: 4, minTokens: 25 })
|
|
553
|
-
},
|
|
554
|
-
await this.version.getCodeExecutionErrorMessage({
|
|
555
|
-
message: lastIteration.status.execution_error.message,
|
|
556
|
-
stacktrace: lastIteration.status.execution_error.stack
|
|
557
|
-
})
|
|
558
|
-
];
|
|
559
|
-
}
|
|
560
|
-
throw new Error(
|
|
561
|
-
`Unexpected iteration status: ${lastIteration == null ? void 0 : lastIteration.status.type}. This is likely a bug, please report it.`
|
|
562
|
-
);
|
|
563
|
-
}
|
|
564
|
-
async _refreshIterationParameters() {
|
|
565
|
-
var _a, _b;
|
|
566
|
-
const instructions = await getValue(this.instructions, this);
|
|
567
|
-
const transcript = new TranscriptArray(await getValue(((_a = this.chat) == null ? void 0 : _a.transcript) ?? [], this));
|
|
568
|
-
const tools = Tool.withUniqueNames(await getValue(this.tools, this) ?? []);
|
|
569
|
-
const objects = await getValue(this.objects, this) ?? [];
|
|
570
|
-
const exits = await getValue(this.exits, this) ?? [];
|
|
571
|
-
const components = await getValue(((_b = this.chat) == null ? void 0 : _b.components) ?? [], this);
|
|
572
|
-
if (objects && objects.length > 100) {
|
|
573
|
-
throw new Error("Too many objects. Expected at most 100 objects.");
|
|
574
|
-
}
|
|
575
|
-
if (tools && tools.length > 100) {
|
|
576
|
-
throw new Error("Too many tools. Expected at most 100 tools.");
|
|
577
|
-
}
|
|
578
|
-
for (const component of components) {
|
|
579
|
-
assertValidComponent(component.definition);
|
|
580
|
-
}
|
|
581
|
-
const ReservedToolNames = [
|
|
582
|
-
"think",
|
|
583
|
-
"listen",
|
|
584
|
-
"return",
|
|
585
|
-
"exit",
|
|
586
|
-
"action",
|
|
587
|
-
"function",
|
|
588
|
-
"callback",
|
|
589
|
-
"code",
|
|
590
|
-
"execute",
|
|
591
|
-
"jsx",
|
|
592
|
-
"object",
|
|
593
|
-
"string",
|
|
594
|
-
"number",
|
|
595
|
-
"boolean",
|
|
596
|
-
"array"
|
|
597
|
-
];
|
|
598
|
-
const MessageTool = this.chat && components.length ? new Tool({
|
|
599
|
-
name: "Message",
|
|
600
|
-
description: "Send a message to the user",
|
|
601
|
-
aliases: Array.from(
|
|
602
|
-
/* @__PURE__ */ new Set(["message", ...components.flatMap((x) => [x.definition.name, ...x.definition.aliases ?? []])])
|
|
603
|
-
),
|
|
604
|
-
handler: async (message) => {
|
|
605
|
-
var _a2, _b2;
|
|
606
|
-
return await ((_b2 = (_a2 = this.chat) == null ? void 0 : _a2.handler) == null ? void 0 : _b2.call(_a2, message));
|
|
607
|
-
}
|
|
608
|
-
}) : null;
|
|
609
|
-
const allTools = MessageTool ? [MessageTool, ...tools] : tools;
|
|
610
|
-
for (const tool of tools) {
|
|
611
|
-
for (let name of [...tool.aliases, tool.name]) {
|
|
612
|
-
name = name.toLowerCase();
|
|
613
|
-
if (ReservedToolNames.includes(name)) {
|
|
614
|
-
throw new Error(`Tool name "${name}" (${tool.name}) is reserved. Please choose a different name.`);
|
|
615
|
-
}
|
|
616
|
-
if (components.find(
|
|
617
|
-
(x) => {
|
|
618
|
-
var _a2;
|
|
619
|
-
return x.definition.name.toLowerCase() === name || ((_a2 = x.definition.aliases) == null ? void 0 : _a2.map((x2) => x2.toLowerCase()).includes(name));
|
|
620
|
-
}
|
|
621
|
-
)) {
|
|
622
|
-
throw new Error(
|
|
623
|
-
`Tool name "${name}" (${tool.name}) is already used by a component. Please choose a different name.`
|
|
624
|
-
);
|
|
625
|
-
}
|
|
626
|
-
if (exits.find((x) => x.name.toLowerCase() === name) || exits.find((x) => {
|
|
627
|
-
var _a2;
|
|
628
|
-
return (_a2 = x.aliases) == null ? void 0 : _a2.map((x2) => x2.toLowerCase()).includes(name);
|
|
629
|
-
})) {
|
|
630
|
-
throw new Error(
|
|
631
|
-
`Tool name "${name}" (${tool.name}) is already used by an exit. Please choose a different name.`
|
|
632
|
-
);
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
if (exits && exits.length > 100) {
|
|
637
|
-
throw new Error("Too many exits. Expected at most 100 exits.");
|
|
638
|
-
}
|
|
639
|
-
if (components && components.length > 100) {
|
|
640
|
-
throw new Error("Too many components. Expected at most 100 components.");
|
|
641
|
-
}
|
|
642
|
-
if (instructions && instructions.length > 1e6) {
|
|
643
|
-
throw new Error("Instructions are too long. Expected at most 1,000,000 characters.");
|
|
644
|
-
}
|
|
645
|
-
if (transcript && transcript.length > 250) {
|
|
646
|
-
throw new Error("Too many transcript messages. Expected at most 250 messages.");
|
|
647
|
-
}
|
|
648
|
-
if (!components.length && !exits.length) {
|
|
649
|
-
exits.push(DefaultExit);
|
|
650
|
-
}
|
|
651
|
-
return {
|
|
652
|
-
transcript,
|
|
653
|
-
tools: allTools,
|
|
654
|
-
objects,
|
|
655
|
-
exits,
|
|
656
|
-
instructions,
|
|
657
|
-
components
|
|
658
|
-
};
|
|
659
|
-
}
|
|
660
|
-
constructor(props) {
|
|
661
|
-
this.id = `llmz_${ulid2()}`;
|
|
662
|
-
this.instructions = props.instructions;
|
|
663
|
-
this.objects = props.objects;
|
|
664
|
-
this.tools = props.tools;
|
|
665
|
-
this.exits = props.exits;
|
|
666
|
-
this.chat = props.chat;
|
|
667
|
-
this.timeout = Math.min(999999999, Math.max(0, props.timeout ?? 6e4));
|
|
668
|
-
this.loop = props.loop ?? 3;
|
|
669
|
-
this.temperature = props.temperature ?? 0.7;
|
|
670
|
-
this.model = props.model;
|
|
671
|
-
this.iterations = [];
|
|
672
|
-
this.metadata = props.metadata ?? {};
|
|
673
|
-
this.snapshot = props.snapshot;
|
|
674
|
-
if (this.loop < 1 || this.loop > 100) {
|
|
675
|
-
throw new Error("Invalid loop. Expected a number between 1 and 100.");
|
|
676
|
-
}
|
|
677
|
-
if (this.temperature < 0 || this.temperature > 2) {
|
|
678
|
-
throw new Error("Invalid temperature. Expected a number between 0 and 2.");
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
export {
|
|
684
|
-
Snapshot,
|
|
685
|
-
ExecutionResult,
|
|
686
|
-
SuccessExecutionResult,
|
|
687
|
-
ErrorExecutionResult,
|
|
688
|
-
PartialExecutionResult,
|
|
689
|
-
getValue,
|
|
690
|
-
ThinkExit,
|
|
691
|
-
ListenExit,
|
|
692
|
-
DefaultExit,
|
|
693
|
-
Context
|
|
694
|
-
};
|