@trama-dev/runtime 0.7.0 → 0.8.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/agent-backend.d.ts +49 -0
- package/dist/agent-hosts.d.ts +8 -0
- package/dist/agent-hosts.js +1 -0
- package/dist/agent.d.ts +3 -1
- package/dist/agent.js +1 -0
- package/dist/bridge-client.js +1 -0
- package/dist/budget.d.ts +6 -0
- package/dist/budget.js +1 -0
- package/dist/client.d.ts +3 -2
- package/dist/client.js +20 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -1
- package/dist/ipc-server.js +6 -1
- package/dist/journal-view.d.ts +8 -0
- package/dist/journal-view.js +45 -3
- package/dist/journal.js +45 -1
- package/dist/model-relay.d.ts +19 -0
- package/dist/model-relay.js +56 -4
- package/dist/model-request.d.ts +9 -0
- package/dist/model-request.js +185 -0
- package/dist/pi-adapter.d.ts +12 -11
- package/dist/pi-adapter.js +51 -5
- package/dist/project.d.ts +8 -0
- package/dist/project.js +18 -1
- package/dist/runner-backend.js +31 -7
- package/dist/runner-types.d.ts +18 -0
- package/dist/runner.d.ts +2 -2
- package/dist/runner.js +92 -9
- package/dist/schema.d.ts +7 -0
- package/dist/schema.js +9 -0
- package/dist/tools.d.ts +8 -1
- package/dist/tools.js +5 -3
- package/dist/types.d.ts +128 -0
- package/dist/workspace.d.ts +8 -1
- package/dist/workspace.js +12 -1
- package/package.json +1 -1
package/dist/runner.js
CHANGED
|
@@ -18,7 +18,7 @@ import { removeTempDir, removeWithThisProcess, requireWatchdog, stopGroups, stop
|
|
|
18
18
|
import { createIPCServer, listen, closeServer } from "./ipc-server.js";
|
|
19
19
|
import { journalEntriesOf, openJournal } from "./journal.js";
|
|
20
20
|
import { messageOf } from "./errors.js";
|
|
21
|
-
import { checkedRepairAttempts, checkedTimeout, ensureProjectScaffold, loadProjectConfig, loadState, saveState } from "./project.js";
|
|
21
|
+
import { checkedRepairAttempts, checkedTimeout, ensureProjectScaffold, loadProjectConfig, loadState, projectNamed, saveState } from "./project.js";
|
|
22
22
|
import { createBackend, saysOnce } from "./runner-backend.js";
|
|
23
23
|
import { copyToHistory, recordVersion } from "./history.js";
|
|
24
24
|
/**
|
|
@@ -43,6 +43,8 @@ export const RUNTIME_TYPES = [
|
|
|
43
43
|
"export declare const agent: Agent;",
|
|
44
44
|
"export declare const tools: Tools;",
|
|
45
45
|
"export declare const workspace: Workspace;",
|
|
46
|
+
"/** What a model called a tool with, held to the parameters the tool declared (see AgentTool): the declared fields and no others — or it throws what is wrong, in words the model can be handed. */",
|
|
47
|
+
"export declare function heldTo(parameters: Schema | undefined, given: unknown): Record<string, unknown>;",
|
|
46
48
|
].join("\n"),
|
|
47
49
|
].join("\n\n");
|
|
48
50
|
// --- Staging: run against a copy, merge back what worked (see tree.ts) ---
|
|
@@ -380,8 +382,9 @@ async function runProgramOnce(options) {
|
|
|
380
382
|
const workspaces = new Map();
|
|
381
383
|
const workspaceAt = (dir) => {
|
|
382
384
|
let workspace = workspaces.get(dir);
|
|
385
|
+
// (Its shell is this run's: what a command there starts is stopped with the run, and has the run's variables.)
|
|
383
386
|
if (!workspace)
|
|
384
|
-
workspaces.set(dir, workspace = createWorkspace(dir, runEnded.signal));
|
|
387
|
+
workspaces.set(dir, workspace = createWorkspace(dir, runEnded.signal, (root, command, given) => tools.shellIn(root, command, given)));
|
|
385
388
|
return workspace;
|
|
386
389
|
};
|
|
387
390
|
// The workspace of the session this run is part of, if it is part of one.
|
|
@@ -550,6 +553,16 @@ async function runProgramOnce(options) {
|
|
|
550
553
|
await closeServer(server);
|
|
551
554
|
}
|
|
552
555
|
}
|
|
556
|
+
/** How whoever is asked for a fix begins an answer that says there is none (see `NoFix`). */
|
|
557
|
+
const NO_FIX = "NO FIX:";
|
|
558
|
+
/**
|
|
559
|
+
* There is no fix, by whoever was asked for one: the program cannot be made
|
|
560
|
+
* to run and still be the program it was. Not a fix that failed to come —
|
|
561
|
+
* an answer, and the last: nobody is asked again for what was said not to
|
|
562
|
+
* be had.
|
|
563
|
+
*/
|
|
564
|
+
class NoFix extends Error {
|
|
565
|
+
}
|
|
553
566
|
/** The instructions a request for a fix is made under. (Exported so the tests' stand-in LLM can tell one from any other call.) */
|
|
554
567
|
export const REPAIR_SYSTEM_PROMPT = "You are a TypeScript repair tool.";
|
|
555
568
|
const repairPrompt = (input) => `Fix this program so it runs.\n\n` +
|
|
@@ -559,6 +572,7 @@ const repairPrompt = (input) => `Fix this program so it runs.\n\n` +
|
|
|
559
572
|
`what you change in this directory is kept, with the program you answer with, if that program then runs. If it does not, none of it is.\n`
|
|
560
573
|
: `Nothing you change in this directory is kept: the program you answer with is all that is taken from you.\n`) +
|
|
561
574
|
(input.journal ? `The record of the run that failed — each call the program made, and what came back — is at ${input.journal}, a line of JSON for each.\n` : "") +
|
|
575
|
+
`If the program cannot be made to run without having it say what is not so, or leave out what it is there to do — a check taken out, a failure handed back as if it were a result — then there is no fix, and you are not to hand over such a program: answer instead with one line that begins "${NO_FIX}" and says why.\n` +
|
|
562
576
|
`\nRuntime types:\n${RUNTIME_TYPES}\n\n` +
|
|
563
577
|
`Broken program:\n${input.programSource}\n\n` +
|
|
564
578
|
`Error:\n${input.error}\n\n` +
|
|
@@ -623,6 +637,14 @@ export const shownPaths = (paths) => paths.slice(0, MAX_PATHS_SHOWN).join(", ")
|
|
|
623
637
|
*/
|
|
624
638
|
async function requestRepair(options, input, privateDir) {
|
|
625
639
|
options.journal.note("fix-request", { error: input.error });
|
|
640
|
+
// What a project that is asked for the fix uses is on its own record, as any run's is — and is said here too,
|
|
641
|
+
// with the fix: what a fix took is then read where the fix was asked for, whoever made it. (What pi's agent
|
|
642
|
+
// uses for one is on this record anyway, as `usage`.) Counted as the run's count grew while it was asked.
|
|
643
|
+
const before = options.repairWith !== undefined ? options.meter?.counted() : undefined;
|
|
644
|
+
const usedByAProject = () => {
|
|
645
|
+
const now = before && options.meter?.counted();
|
|
646
|
+
return before && now ? { by: options.repairWith, used: { calls: now.calls - before.calls, tokens: now.tokens - before.tokens, cost: now.cost - before.cost } } : {};
|
|
647
|
+
};
|
|
626
648
|
const timedOut = new AbortController();
|
|
627
649
|
const limit = createAllowance(options.timeout);
|
|
628
650
|
const signal = options.signal ? AbortSignal.any([timedOut.signal, options.signal]) : timedOut.signal;
|
|
@@ -630,16 +652,27 @@ async function requestRepair(options, input, privateDir) {
|
|
|
630
652
|
const kept = options.agentChanges !== "discard" && !options.validating;
|
|
631
653
|
const staged = stageProject(options.projectDir, privateDir, input.programSource);
|
|
632
654
|
limit.start(() => timedOut.abort());
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
655
|
+
// Who is asked: a project, where one is named — or pi's agent, at work in the copy with its own tools.
|
|
656
|
+
let fixed;
|
|
657
|
+
if (options.repairWith !== undefined) {
|
|
658
|
+
fixed = await fixedBy(options.repairWith, options, { ...input, kept, copy: staged.copy }, signal);
|
|
659
|
+
}
|
|
660
|
+
else {
|
|
661
|
+
const answer = await options.backend.ask(repairPrompt({ ...input, journal: options.journal.path, kept }), {
|
|
662
|
+
cwd: staged.copy, system: REPAIR_SYSTEM_PROMPT, signal,
|
|
663
|
+
used: (usage) => options.journal.used("fix", usage),
|
|
664
|
+
});
|
|
665
|
+
// (An answer that says there is no fix is that, and no program: it is not installed and tried as one.)
|
|
666
|
+
if (answer.trimStart().startsWith(NO_FIX))
|
|
667
|
+
throw new NoFix(answer.trimStart().slice(NO_FIX.length).trim() || "no reason was given");
|
|
668
|
+
fixed = stripCodeFences(answer);
|
|
669
|
+
}
|
|
637
670
|
if (fixed.trim() === "")
|
|
638
671
|
throw new Error("The repair returned an empty program.");
|
|
639
672
|
if (!kept) {
|
|
640
673
|
if (fixed === input.programSource)
|
|
641
674
|
throw new Error("The repair returned the program unchanged.");
|
|
642
|
-
options.journal.note("fix", { ok: true, source: fixed });
|
|
675
|
+
options.journal.note("fix", { ok: true, ...usedByAProject(), source: fixed });
|
|
643
676
|
return { fixed };
|
|
644
677
|
}
|
|
645
678
|
let changed = [], unusable;
|
|
@@ -658,6 +691,7 @@ async function requestRepair(options, input, privateDir) {
|
|
|
658
691
|
}
|
|
659
692
|
options.journal.note("fix", {
|
|
660
693
|
ok: true,
|
|
694
|
+
...usedByAProject(),
|
|
661
695
|
...(changed.length > 0 ? { changed: changed.slice(0, MAX_PATHS_NOTED) } : {}),
|
|
662
696
|
...(changed.length > MAX_PATHS_NOTED ? { changedInAll: changed.length } : {}),
|
|
663
697
|
alone: unusable,
|
|
@@ -666,14 +700,54 @@ async function requestRepair(options, input, privateDir) {
|
|
|
666
700
|
return unusable === undefined ? { fixed, staged } : { fixed, unusable };
|
|
667
701
|
}
|
|
668
702
|
catch (err) {
|
|
669
|
-
const error = messageOf(err);
|
|
670
|
-
options.journal.note("fix", { ok: false, error });
|
|
703
|
+
const error = err instanceof NoFix ? `It was said that there is none to be had: ${err.message}` : messageOf(err);
|
|
704
|
+
options.journal.note("fix", { ok: false, ...(err instanceof NoFix && { none: true }), ...usedByAProject(), error });
|
|
671
705
|
return { error };
|
|
672
706
|
}
|
|
673
707
|
finally {
|
|
674
708
|
limit.cancel();
|
|
675
709
|
}
|
|
676
710
|
}
|
|
711
|
+
/**
|
|
712
|
+
* Ask a project for a fix (see RunOptions.repairWith): its program is run
|
|
713
|
+
* with the copy as its workspace, and what it ends with is the answer. It is
|
|
714
|
+
* a run of the failing run's own — under its count, stopped with it, started
|
|
715
|
+
* with its variables, on its record — and a run like any otherwise, with a
|
|
716
|
+
* journal of its own in its own project. Not one that is fixed where it
|
|
717
|
+
* fails: what fixes would then be asked to fix what fixes.
|
|
718
|
+
*/
|
|
719
|
+
async function fixedBy(project, options, asked, signal) {
|
|
720
|
+
if (realRootOf(project) === realRootOf(options.projectDir)) {
|
|
721
|
+
throw new Error("A project is not asked to fix itself: repairWith names the project that failed.");
|
|
722
|
+
}
|
|
723
|
+
const { journal } = options;
|
|
724
|
+
const result = await runProgram({
|
|
725
|
+
projectDir: project,
|
|
726
|
+
workspaceDir: asked.copy,
|
|
727
|
+
maxRepairAttempts: 0,
|
|
728
|
+
timeout: options.timeout,
|
|
729
|
+
label: options.label ? `${options.label}: fix` : "fix",
|
|
730
|
+
env: options.env,
|
|
731
|
+
signal,
|
|
732
|
+
args: { program: asked.programSource, error: asked.error, journal: journal.path, kept: asked.kept, types: RUNTIME_TYPES },
|
|
733
|
+
}, {
|
|
734
|
+
depth: options.depth + 1,
|
|
735
|
+
parent: { run: journal.run, journal: journal.path },
|
|
736
|
+
started: (run, path) => { if (run)
|
|
737
|
+
journal.note("child", { run, journal: path, purpose: "fix" }); },
|
|
738
|
+
above: options.meter,
|
|
739
|
+
});
|
|
740
|
+
if (result.status !== "done") {
|
|
741
|
+
throw new Error(`The project asked for the fix ${result.status === "failed" ? "failed" : "did not end its work"}: ${result.reason ?? result.status}`);
|
|
742
|
+
}
|
|
743
|
+
const { program, unfixable } = (result.result ?? {});
|
|
744
|
+
if (typeof unfixable === "string" && program === undefined)
|
|
745
|
+
throw new NoFix(unfixable.trim() || "no reason was given");
|
|
746
|
+
if (typeof program !== "string") {
|
|
747
|
+
throw new Error("The project asked for the fix answered with no program: its result is to be { program }, the whole of the fixed program — or { unfixable }, why there is no fix.");
|
|
748
|
+
}
|
|
749
|
+
return program;
|
|
750
|
+
}
|
|
677
751
|
/**
|
|
678
752
|
* Try a proposed fix, in a copy of the project that has it for its program.
|
|
679
753
|
*
|
|
@@ -1177,6 +1251,13 @@ async function runProject(options, depth, askedFor, making) {
|
|
|
1177
1251
|
const timeout = checkedTimeout(options.timeout ?? config.defaultTimeout);
|
|
1178
1252
|
const maxRepairAttempts = checkedRepairAttempts(options.maxRepairAttempts ?? config.maxRepairAttempts);
|
|
1179
1253
|
const env = options.env == null ? undefined : checkedEnv(options.env);
|
|
1254
|
+
// Who is asked for a fix, where it is a project: found now, and not when the program has failed — a run that
|
|
1255
|
+
// could not have been fixed as it was to be is not begun. (Where no fix is to be asked for, nobody is looked for.)
|
|
1256
|
+
const namedToFix = options.repairWith ?? config.repairWith;
|
|
1257
|
+
const repairWith = namedToFix !== undefined && maxRepairAttempts > 0 ? projectNamed(namedToFix) : undefined;
|
|
1258
|
+
if (repairWith !== undefined && !existsSync(join(repairWith, "meta.json"))) {
|
|
1259
|
+
throw new Error(`repairWith names no project: there is none at ${repairWith}`);
|
|
1260
|
+
}
|
|
1180
1261
|
// Its own budget — the config's, for a run nobody's program asked for and that was given none — under the budgets above it.
|
|
1181
1262
|
const budget = options.budget != null ? checkedBudget(options.budget) : askedFor ? undefined : config.budget;
|
|
1182
1263
|
const meter = createMeter(budget, askedFor?.above);
|
|
@@ -1193,6 +1274,7 @@ async function runProject(options, depth, askedFor, making) {
|
|
|
1193
1274
|
workspace: options.workspaceDir,
|
|
1194
1275
|
timeout,
|
|
1195
1276
|
maxRepairAttempts,
|
|
1277
|
+
repairWith,
|
|
1196
1278
|
budget,
|
|
1197
1279
|
// (The names: what the variables hold is not for a journal.)
|
|
1198
1280
|
env: env && Object.keys(env),
|
|
@@ -1213,6 +1295,7 @@ async function runProject(options, depth, askedFor, making) {
|
|
|
1213
1295
|
label: options.label,
|
|
1214
1296
|
env,
|
|
1215
1297
|
agentChanges: config.agentChanges,
|
|
1298
|
+
repairWith,
|
|
1216
1299
|
signal: options.signal,
|
|
1217
1300
|
journal,
|
|
1218
1301
|
}));
|
package/dist/schema.d.ts
CHANGED
|
@@ -35,4 +35,11 @@ export declare const hasList: (shape: Shape) => boolean;
|
|
|
35
35
|
* it holding the declared fields and no others.
|
|
36
36
|
*/
|
|
37
37
|
export declare function conform(value: unknown, shape: Shape, where?: Where): unknown;
|
|
38
|
+
/**
|
|
39
|
+
* What a model called a tool with, held to the parameters the tool declared:
|
|
40
|
+
* for a program that runs its own tools, as the runner holds them for an
|
|
41
|
+
* agent's (see argumentsOf). Returns the declared fields and no others, or
|
|
42
|
+
* throws what is wrong — in words a model can be handed, to try again by.
|
|
43
|
+
*/
|
|
44
|
+
export declare function heldTo(parameters: unknown, given: unknown): Record<string, unknown>;
|
|
38
45
|
export {};
|
package/dist/schema.js
CHANGED
|
@@ -102,3 +102,12 @@ export function conform(value, shape, where = null) {
|
|
|
102
102
|
return [key, conform(value[key], field, fieldOf(where, key))];
|
|
103
103
|
}));
|
|
104
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* What a model called a tool with, held to the parameters the tool declared:
|
|
107
|
+
* for a program that runs its own tools, as the runner holds them for an
|
|
108
|
+
* agent's (see argumentsOf). Returns the declared fields and no others, or
|
|
109
|
+
* throws what is wrong — in words a model can be handed, to try again by.
|
|
110
|
+
*/
|
|
111
|
+
export function heldTo(parameters, given) {
|
|
112
|
+
return argumentsOf(parameters ?? {}).held(given);
|
|
113
|
+
}
|
package/dist/tools.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import type { Tools } from "./types.js";
|
|
1
|
+
import type { Tools, ShellResult } from "./types.js";
|
|
2
2
|
export interface ToolsWithCleanup extends Tools {
|
|
3
|
+
/**
|
|
4
|
+
* The shell, in another directory than the project: where a command begins
|
|
5
|
+
* is `root`, or a path within it. It is the same shell otherwise — its
|
|
6
|
+
* variables, what is kept of the commands it started, how they are stopped.
|
|
7
|
+
* (For a workspace: see Workspace.shell.)
|
|
8
|
+
*/
|
|
9
|
+
shellIn(root: string, command: string, options?: Parameters<Tools["shell"]>[1]): Promise<ShellResult>;
|
|
3
10
|
/** Stop every shell command still running, and anything they left in the background. Resolves when they are gone. */
|
|
4
11
|
stopActiveShells(): Promise<void>;
|
|
5
12
|
}
|
package/dist/tools.js
CHANGED
|
@@ -99,7 +99,7 @@ export function createTools(projectDir, signal, env) {
|
|
|
99
99
|
sweeper.unref();
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
|
-
|
|
102
|
+
const made = {
|
|
103
103
|
async stopActiveShells() {
|
|
104
104
|
const pgids = [...groups.keys()];
|
|
105
105
|
await stopGroups(pgids);
|
|
@@ -127,8 +127,9 @@ export function createTools(projectDir, signal, env) {
|
|
|
127
127
|
makeDirsIn(normalizedProjectDir, dirname(target));
|
|
128
128
|
writeFileSync(target, content);
|
|
129
129
|
},
|
|
130
|
-
|
|
131
|
-
|
|
130
|
+
shell: (command, options) => made.shellIn(normalizedProjectDir, command, options),
|
|
131
|
+
async shellIn(root, command, options = {}) {
|
|
132
|
+
const cwd = options.cwd ? guardPath(root, options.cwd) : root;
|
|
132
133
|
const env = options.env == null ? shellEnv : { ...shellEnv, ...checkedEnv(options.env) };
|
|
133
134
|
// The shell is started from where the runner finds it, and is `sh` to itself and to whoever it tells of
|
|
134
135
|
// itself, as it was when the system did the finding: in "$0", in what it says of a command it cannot find,
|
|
@@ -220,4 +221,5 @@ export function createTools(projectDir, signal, env) {
|
|
|
220
221
|
return { status: response.status, body, headers };
|
|
221
222
|
}
|
|
222
223
|
};
|
|
224
|
+
return made;
|
|
223
225
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -204,6 +204,123 @@ export interface Agent {
|
|
|
204
204
|
prompt: string;
|
|
205
205
|
schema: Schema;
|
|
206
206
|
}): Promise<T>;
|
|
207
|
+
/**
|
|
208
|
+
* Ask the model once, and have its one reply: no agent is at work, no
|
|
209
|
+
* tool is run, and nothing is asked again. For a program that is its own
|
|
210
|
+
* harness — it keeps the conversation, runs the tools the model calls,
|
|
211
|
+
* and decides when to ask again and when to stop:
|
|
212
|
+
*
|
|
213
|
+
* const messages: ModelMessage[] = [{ role: "user", content: "…" }];
|
|
214
|
+
* for (;;) {
|
|
215
|
+
* const reply = await agent.complete({ system, messages, tools });
|
|
216
|
+
* messages.push(reply); // a reply goes back as it came
|
|
217
|
+
* if (reply.stopReason !== "toolUse") break; // read why it stopped: see ModelReply
|
|
218
|
+
* for (const part of reply.content) {
|
|
219
|
+
* if (part.type !== "toolCall") continue;
|
|
220
|
+
* messages.push({ role: "toolResult", toolCallId: part.id, toolName: part.name, content: await mine(part) });
|
|
221
|
+
* }
|
|
222
|
+
* }
|
|
223
|
+
*
|
|
224
|
+
* Each is a call to a run's budget, counted for what its reply used: a
|
|
225
|
+
* loop is better held by a cap on tokens or cost than on calls.
|
|
226
|
+
*/
|
|
227
|
+
complete(request: ModelRequest): Promise<ModelReply>;
|
|
228
|
+
}
|
|
229
|
+
/** A part of a message that is text. */
|
|
230
|
+
export interface TextPart {
|
|
231
|
+
type: "text";
|
|
232
|
+
text: string;
|
|
233
|
+
}
|
|
234
|
+
/** An image in a message: its kind — image/png, image/jpeg, image/gif or image/webp — and its bytes in base64. */
|
|
235
|
+
export interface ImagePart {
|
|
236
|
+
type: "image";
|
|
237
|
+
mimeType: string;
|
|
238
|
+
data: string;
|
|
239
|
+
}
|
|
240
|
+
/** What a model thought before it answered, where it shows that. */
|
|
241
|
+
export interface ThinkingPart {
|
|
242
|
+
type: "thinking";
|
|
243
|
+
thinking: string;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* A model's call of a tool it was told of: to be answered by a message of
|
|
247
|
+
* role "toolResult" with its `id`, before the model is asked again. Its
|
|
248
|
+
* arguments are as the model wrote them — held to nothing: where they were
|
|
249
|
+
* no JSON at all, `argumentsError` says so, and they are `{}`.
|
|
250
|
+
*/
|
|
251
|
+
export interface ToolCallPart {
|
|
252
|
+
type: "toolCall";
|
|
253
|
+
id: string;
|
|
254
|
+
name: string;
|
|
255
|
+
arguments: Record<string, unknown>;
|
|
256
|
+
argumentsError?: string;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* A model's reply to one request. What is declared here is what a program
|
|
260
|
+
* may read. There is more on it — what its provider wants to see again when
|
|
261
|
+
* the conversation goes on: so a reply goes back into `messages` as it came,
|
|
262
|
+
* all of it. (It is plain data, and can be kept in state.)
|
|
263
|
+
*/
|
|
264
|
+
export interface ModelReply {
|
|
265
|
+
role: "assistant";
|
|
266
|
+
content: Array<TextPart | ThinkingPart | ToolCallPart>;
|
|
267
|
+
/**
|
|
268
|
+
* Why the model stopped. "stop": it has answered. "toolUse": it called
|
|
269
|
+
* tools, and waits for what they say. "length": it was cut off at its
|
|
270
|
+
* output limit — what there is of the reply is here. "error": the
|
|
271
|
+
* provider failed, and `errorMessage` says how; whether to ask again is
|
|
272
|
+
* the program's to decide.
|
|
273
|
+
*/
|
|
274
|
+
stopReason: "stop" | "toolUse" | "length" | "error";
|
|
275
|
+
errorMessage?: string;
|
|
276
|
+
/** The model that answered, as its provider names it. */
|
|
277
|
+
model: string;
|
|
278
|
+
/** What the reply used: tokens read and written, from a cache and to one, and what pi's price list makes of them in US dollars. */
|
|
279
|
+
usage: {
|
|
280
|
+
input: number;
|
|
281
|
+
output: number;
|
|
282
|
+
cacheRead: number;
|
|
283
|
+
cacheWrite: number;
|
|
284
|
+
cost: {
|
|
285
|
+
total: number;
|
|
286
|
+
};
|
|
287
|
+
};
|
|
288
|
+
[carried: string]: unknown;
|
|
289
|
+
}
|
|
290
|
+
/** A message of the conversation a model is sent: the user's, a reply as it came, or what a tool the model called said. */
|
|
291
|
+
export type ModelMessage = {
|
|
292
|
+
role: "user";
|
|
293
|
+
content: string | Array<TextPart | ImagePart>;
|
|
294
|
+
} | ModelReply | {
|
|
295
|
+
role: "toolResult";
|
|
296
|
+
toolCallId: string;
|
|
297
|
+
toolName: string;
|
|
298
|
+
content: string | Array<TextPart | ImagePart>;
|
|
299
|
+
isError?: boolean;
|
|
300
|
+
};
|
|
301
|
+
export interface ModelRequest {
|
|
302
|
+
/** The conversation so far, oldest first. It is the program's to keep: nothing of it is remembered between requests. */
|
|
303
|
+
messages: ModelMessage[];
|
|
304
|
+
/** What the model is told to be, before the conversation. */
|
|
305
|
+
system?: string;
|
|
306
|
+
/**
|
|
307
|
+
* The tools the model may call, each declared as a tool of the program's
|
|
308
|
+
* is to an agent (see `AgentTool`): its name, description and parameters.
|
|
309
|
+
* Nothing runs one — the model's call comes back in the reply, and the
|
|
310
|
+
* program does with it what it will; so a tool's `run` may be left out.
|
|
311
|
+
*/
|
|
312
|
+
tools?: Array<Pick<AgentTool, "name" | "description" | "parameters"> & Partial<AgentTool>>;
|
|
313
|
+
/** The model, as "provider/model". Without it, the project's — which is then one pi has, or the call fails. */
|
|
314
|
+
model?: string;
|
|
315
|
+
/** How much the model is to think. A level the model has not got is an error. */
|
|
316
|
+
thinking?: Thinking;
|
|
317
|
+
/** The most the reply may come to, in tokens. */
|
|
318
|
+
maxTokens?: number;
|
|
319
|
+
temperature?: number;
|
|
320
|
+
/** "none": the model answers without calling a tool, though it is told of them. */
|
|
321
|
+
toolChoice?: "auto" | "none";
|
|
322
|
+
/** How long the provider is waited for, in milliseconds: five minutes without it. */
|
|
323
|
+
timeout?: number;
|
|
207
324
|
}
|
|
208
325
|
export interface ShellResult {
|
|
209
326
|
exitCode: number;
|
|
@@ -258,4 +375,15 @@ export interface Workspace {
|
|
|
258
375
|
expect?: number;
|
|
259
376
|
timeout?: number;
|
|
260
377
|
}): Promise<ObserveResult>;
|
|
378
|
+
/**
|
|
379
|
+
* Execute a shell command in the workspace: as `tools.shell`, but it
|
|
380
|
+
* begins in the workspace's directory — or, with `cwd`, in a directory
|
|
381
|
+
* of it. For a program whose work is in a directory that is not its own:
|
|
382
|
+
* one that was handed another project to work on, say.
|
|
383
|
+
*/
|
|
384
|
+
shell(command: string, options?: {
|
|
385
|
+
cwd?: string;
|
|
386
|
+
timeout?: number;
|
|
387
|
+
env?: Record<string, string | undefined>;
|
|
388
|
+
}): Promise<ShellResult>;
|
|
261
389
|
}
|
package/dist/workspace.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Workspace } from "./types.js";
|
|
2
|
+
import { type ToolsWithCleanup } from "./tools.js";
|
|
2
3
|
/**
|
|
3
4
|
* Read a simple glob pattern for matching relative paths: `*` is any run of
|
|
4
5
|
* characters within a segment, `?` one of them, `**` any run of characters
|
|
@@ -26,4 +27,10 @@ export declare function compileGlob(pattern: string): {
|
|
|
26
27
|
* is not there yet.
|
|
27
28
|
*/
|
|
28
29
|
export declare function ensureWorkspaceDir(workspaceDir: string): string;
|
|
29
|
-
|
|
30
|
+
/**
|
|
31
|
+
* The workspace in a directory. `shellIn`: the shell its commands are run by
|
|
32
|
+
* — a run's own, so that they are the run's to stop, and have its variables.
|
|
33
|
+
* Without it the workspace has a shell of its own: commands it started end
|
|
34
|
+
* with this process.
|
|
35
|
+
*/
|
|
36
|
+
export declare function createWorkspace(workspaceDir: string, signal?: AbortSignal, shellIn?: ToolsWithCleanup["shellIn"]): Workspace;
|
package/dist/workspace.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFileSync, mkdirSync, readdirSync, statSync } from "fs";
|
|
2
2
|
import { resolve, relative, sep } from "path";
|
|
3
|
+
import { createTools } from "./tools.js";
|
|
3
4
|
import { onAbort } from "./abort.js";
|
|
4
5
|
import { guardPath, realRootOf } from "./path-guard.js";
|
|
5
6
|
import { isStagedName, stageFile } from "./replace-file.js";
|
|
@@ -166,8 +167,15 @@ export function ensureWorkspaceDir(workspaceDir) {
|
|
|
166
167
|
mkdirSync(realRootOf(root), { recursive: true });
|
|
167
168
|
return root;
|
|
168
169
|
}
|
|
169
|
-
|
|
170
|
+
/**
|
|
171
|
+
* The workspace in a directory. `shellIn`: the shell its commands are run by
|
|
172
|
+
* — a run's own, so that they are the run's to stop, and have its variables.
|
|
173
|
+
* Without it the workspace has a shell of its own: commands it started end
|
|
174
|
+
* with this process.
|
|
175
|
+
*/
|
|
176
|
+
export function createWorkspace(workspaceDir, signal, shellIn) {
|
|
170
177
|
const root = ensureWorkspaceDir(workspaceDir);
|
|
178
|
+
let ownShell;
|
|
171
179
|
/** All artifact paths matching a glob, sorted. */
|
|
172
180
|
const listMatching = (pattern) => {
|
|
173
181
|
const re = compileGlob(pattern);
|
|
@@ -204,6 +212,9 @@ export function createWorkspace(workspaceDir, signal) {
|
|
|
204
212
|
async list(pattern) {
|
|
205
213
|
return listMatching(pattern);
|
|
206
214
|
},
|
|
215
|
+
async shell(command, options) {
|
|
216
|
+
return (shellIn ?? (ownShell ??= createTools(root, signal)).shellIn)(root, command, options);
|
|
217
|
+
},
|
|
207
218
|
async observe(pattern, options = {}) {
|
|
208
219
|
const { expect, timeout } = options;
|
|
209
220
|
if (expect !== undefined && (!Number.isInteger(expect) || expect < 1)) {
|