@foldrun/core 24.4.2 → 24.6.0
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/src/engine-loop.d.ts +9 -0
- package/dist/src/engine-loop.js +26 -2
- package/package.json +3 -2
- package/src/engine-loop.ts +28 -2
|
@@ -22,6 +22,9 @@ type CanUseTool = (name: string, input: Json, extra: {
|
|
|
22
22
|
interface LoopOptions {
|
|
23
23
|
cwd: string;
|
|
24
24
|
env: Record<string, string | undefined>;
|
|
25
|
+
/** What the tools (Bash) get; `env` when not given. foldrun code passes
|
|
26
|
+
* the person's own environment here, so the model key stays with the loop. */
|
|
27
|
+
shellEnv: Record<string, string | undefined>;
|
|
25
28
|
model: string;
|
|
26
29
|
systemPrompt: string;
|
|
27
30
|
tools: EngineTool[];
|
|
@@ -31,6 +34,12 @@ interface LoopOptions {
|
|
|
31
34
|
post: HookFn[];
|
|
32
35
|
canUseTool: CanUseTool | null;
|
|
33
36
|
outer: AbortSignal | null;
|
|
37
|
+
/** The conversation so far, before this prompt: earlier user turns, the
|
|
38
|
+
* model's replies and tool results, as the loop yielded them. */
|
|
39
|
+
history: {
|
|
40
|
+
role: string;
|
|
41
|
+
content: Block[];
|
|
42
|
+
}[];
|
|
34
43
|
}
|
|
35
44
|
/** The request body for one turn. Cache marks go on the three places a
|
|
36
45
|
* prefix ends: the tools, the system prompt, and the newest message — so
|
package/dist/src/engine-loop.js
CHANGED
|
@@ -78,6 +78,7 @@ function readOptions(options) {
|
|
|
78
78
|
return {
|
|
79
79
|
cwd: String(options.cwd ?? process.cwd()),
|
|
80
80
|
env,
|
|
81
|
+
shellEnv: options.shellEnv && typeof options.shellEnv === "object" ? options.shellEnv : env,
|
|
81
82
|
model: env[`ANTHROPIC_DEFAULT_${model.toUpperCase()}_MODEL`] || TIER_DEFAULTS[model] || model,
|
|
82
83
|
systemPrompt: typeof options.systemPrompt === "string" ? options.systemPrompt : "",
|
|
83
84
|
tools: names.map((n) => ENGINE_TOOLS[n]),
|
|
@@ -87,8 +88,27 @@ function readOptions(options) {
|
|
|
87
88
|
post: hookFns(options.hooks, "PostToolUse"),
|
|
88
89
|
canUseTool: typeof options.canUseTool === "function" ? options.canUseTool : null,
|
|
89
90
|
outer: options.abortController?.signal ?? null,
|
|
91
|
+
history: readHistory(options.history),
|
|
90
92
|
};
|
|
91
93
|
}
|
|
94
|
+
/** An earlier conversation, checked: user and assistant turns that
|
|
95
|
+
* alternate, starting with the user's, each a list of content blocks. A
|
|
96
|
+
* turn that is not one is refused, not sent half-right to the model. */
|
|
97
|
+
function readHistory(raw) {
|
|
98
|
+
if (raw == null)
|
|
99
|
+
return [];
|
|
100
|
+
if (!Array.isArray(raw))
|
|
101
|
+
throw new Error("history must be a list of turns");
|
|
102
|
+
return raw.map((t, i) => {
|
|
103
|
+
const role = t?.role;
|
|
104
|
+
const content = t?.content;
|
|
105
|
+
if (role !== (i % 2 === 0 ? "user" : "assistant"))
|
|
106
|
+
throw new Error(`history turn ${i + 1} must be the ${i % 2 === 0 ? "user's" : "model's"}`);
|
|
107
|
+
if (!Array.isArray(content) || !content.every((b) => b && typeof b.type === "string"))
|
|
108
|
+
throw new Error(`history turn ${i + 1} has no content blocks`);
|
|
109
|
+
return { role: String(role), content: content };
|
|
110
|
+
});
|
|
111
|
+
}
|
|
92
112
|
function requestHeaders(env) {
|
|
93
113
|
const headers = { "content-type": "application/json", "anthropic-version": API_VERSION };
|
|
94
114
|
if (env.ANTHROPIC_API_KEY)
|
|
@@ -326,8 +346,12 @@ async function* run(prompt, o, signal, interrupted) {
|
|
|
326
346
|
const sessionId = crypto.randomUUID();
|
|
327
347
|
const startedAt = Date.now();
|
|
328
348
|
const byName = new Map(o.tools.map((t) => [t.name, t]));
|
|
329
|
-
const ctx = { cwd: o.cwd, env: o.
|
|
330
|
-
|
|
349
|
+
const ctx = { cwd: o.cwd, env: o.shellEnv, signal, reads: new Map(), groups: new Set() };
|
|
350
|
+
// An earlier conversation comes first (foldrun code's local turns carry
|
|
351
|
+
// theirs, 19 Nov 2026); the prompt is always the newest user turn.
|
|
352
|
+
if (o.history.length % 2 !== 0)
|
|
353
|
+
throw new Error("history must end with the model's turn, so the prompt is the next user turn");
|
|
354
|
+
const messages = [...o.history, { role: "user", content: [{ type: "text", text: prompt }] }];
|
|
331
355
|
const total = emptyUsage();
|
|
332
356
|
let turns = 0;
|
|
333
357
|
let lastText = "";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foldrun/core",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.6.0",
|
|
4
4
|
"description": "Agents are just folders. The format, the runner, the checks.",
|
|
5
5
|
"license": "FSL-1.1-ALv2",
|
|
6
6
|
"type": "module",
|
|
@@ -34,7 +34,8 @@
|
|
|
34
34
|
"./subagents",
|
|
35
35
|
"./tool-names",
|
|
36
36
|
"./scan",
|
|
37
|
-
"./snapshot-archive"
|
|
37
|
+
"./snapshot-archive",
|
|
38
|
+
"./engine-loop"
|
|
38
39
|
]
|
|
39
40
|
},
|
|
40
41
|
"main": "./dist/index.js",
|
package/src/engine-loop.ts
CHANGED
|
@@ -71,6 +71,9 @@ interface Usage {
|
|
|
71
71
|
interface LoopOptions {
|
|
72
72
|
cwd: string;
|
|
73
73
|
env: Record<string, string | undefined>;
|
|
74
|
+
/** What the tools (Bash) get; `env` when not given. foldrun code passes
|
|
75
|
+
* the person's own environment here, so the model key stays with the loop. */
|
|
76
|
+
shellEnv: Record<string, string | undefined>;
|
|
74
77
|
model: string;
|
|
75
78
|
systemPrompt: string;
|
|
76
79
|
tools: EngineTool[];
|
|
@@ -80,6 +83,9 @@ interface LoopOptions {
|
|
|
80
83
|
post: HookFn[];
|
|
81
84
|
canUseTool: CanUseTool | null;
|
|
82
85
|
outer: AbortSignal | null;
|
|
86
|
+
/** The conversation so far, before this prompt: earlier user turns, the
|
|
87
|
+
* model's replies and tool results, as the loop yielded them. */
|
|
88
|
+
history: { role: string; content: Block[] }[];
|
|
83
89
|
}
|
|
84
90
|
|
|
85
91
|
function hookFns(hooks: unknown, event: string): HookFn[] {
|
|
@@ -113,6 +119,7 @@ function readOptions(options: Json): LoopOptions {
|
|
|
113
119
|
return {
|
|
114
120
|
cwd: String(options.cwd ?? process.cwd()),
|
|
115
121
|
env,
|
|
122
|
+
shellEnv: options.shellEnv && typeof options.shellEnv === "object" ? (options.shellEnv as Record<string, string | undefined>) : env,
|
|
116
123
|
model: env[`ANTHROPIC_DEFAULT_${model.toUpperCase()}_MODEL`] || TIER_DEFAULTS[model] || model,
|
|
117
124
|
systemPrompt: typeof options.systemPrompt === "string" ? options.systemPrompt : "",
|
|
118
125
|
tools: names.map((n) => ENGINE_TOOLS[n]),
|
|
@@ -122,9 +129,25 @@ function readOptions(options: Json): LoopOptions {
|
|
|
122
129
|
post: hookFns(options.hooks, "PostToolUse"),
|
|
123
130
|
canUseTool: typeof options.canUseTool === "function" ? (options.canUseTool as CanUseTool) : null,
|
|
124
131
|
outer: (options.abortController as AbortController | undefined)?.signal ?? null,
|
|
132
|
+
history: readHistory(options.history),
|
|
125
133
|
};
|
|
126
134
|
}
|
|
127
135
|
|
|
136
|
+
/** An earlier conversation, checked: user and assistant turns that
|
|
137
|
+
* alternate, starting with the user's, each a list of content blocks. A
|
|
138
|
+
* turn that is not one is refused, not sent half-right to the model. */
|
|
139
|
+
function readHistory(raw: unknown): { role: string; content: Block[] }[] {
|
|
140
|
+
if (raw == null) return [];
|
|
141
|
+
if (!Array.isArray(raw)) throw new Error("history must be a list of turns");
|
|
142
|
+
return raw.map((t, i) => {
|
|
143
|
+
const role = (t as Json)?.role;
|
|
144
|
+
const content = (t as Json)?.content;
|
|
145
|
+
if (role !== (i % 2 === 0 ? "user" : "assistant")) throw new Error(`history turn ${i + 1} must be the ${i % 2 === 0 ? "user's" : "model's"}`);
|
|
146
|
+
if (!Array.isArray(content) || !content.every((b) => b && typeof (b as Json).type === "string")) throw new Error(`history turn ${i + 1} has no content blocks`);
|
|
147
|
+
return { role: String(role), content: content as Block[] };
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
128
151
|
function requestHeaders(env: Record<string, string | undefined>): Record<string, string> {
|
|
129
152
|
const headers: Record<string, string> = { "content-type": "application/json", "anthropic-version": API_VERSION };
|
|
130
153
|
if (env.ANTHROPIC_API_KEY) headers["x-api-key"] = env.ANTHROPIC_API_KEY;
|
|
@@ -345,8 +368,11 @@ async function* run(prompt: string, o: LoopOptions, signal: AbortSignal, interru
|
|
|
345
368
|
const sessionId = crypto.randomUUID();
|
|
346
369
|
const startedAt = Date.now();
|
|
347
370
|
const byName = new Map(o.tools.map((t) => [t.name, t]));
|
|
348
|
-
const ctx: ToolContext = { cwd: o.cwd, env: o.
|
|
349
|
-
|
|
371
|
+
const ctx: ToolContext = { cwd: o.cwd, env: o.shellEnv, signal, reads: new Map(), groups: new Set() };
|
|
372
|
+
// An earlier conversation comes first (foldrun code's local turns carry
|
|
373
|
+
// theirs, 19 Nov 2026); the prompt is always the newest user turn.
|
|
374
|
+
if (o.history.length % 2 !== 0) throw new Error("history must end with the model's turn, so the prompt is the next user turn");
|
|
375
|
+
const messages: { role: string; content: Block[] }[] = [...o.history, { role: "user", content: [{ type: "text", text: prompt }] }];
|
|
350
376
|
const total = emptyUsage();
|
|
351
377
|
let turns = 0;
|
|
352
378
|
let lastText = "";
|