@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
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// One request to a model, as a program makes it — held to its shape.
|
|
2
|
+
//
|
|
3
|
+
// A program that is its own harness keeps the conversation and asks a model
|
|
4
|
+
// one reply at a time (`agent.complete`). What it asks with comes over the
|
|
5
|
+
// bridge as JSON, and nothing behind the bridge holds it to anything: pi sends
|
|
6
|
+
// a provider what it is handed, and a conversation that is none comes back as
|
|
7
|
+
// the provider's complaint, or as pi's own failure half-way through reading
|
|
8
|
+
// it. So it is read here first, and what is wrong with it is said in the
|
|
9
|
+
// program's words — which message, which part, what was expected.
|
|
10
|
+
//
|
|
11
|
+
// What a program writes is three kinds of message: the user's, a tool's
|
|
12
|
+
// result, and a reply — the last as `complete` returned it, and sent back as
|
|
13
|
+
// it came: a reply holds what its provider needs to see again (a signature on
|
|
14
|
+
// what the model thought, an id of its own), and none of that is a program's
|
|
15
|
+
// to know or to mend. Of a reply only what could not be one is refused.
|
|
16
|
+
import { IMAGE_KINDS } from "./agent-backend.js";
|
|
17
|
+
import { messageOf } from "./errors.js";
|
|
18
|
+
import { toolsOf } from "./own-tools.js";
|
|
19
|
+
import { isJsonObject } from "./serializable.js";
|
|
20
|
+
/** What a request may hold, as a program names it. */
|
|
21
|
+
export const REQUEST_FIELDS = ["messages", "system", "tools", "model", "thinking", "maxTokens", "temperature", "toolChoice", "timeout"];
|
|
22
|
+
/** A value as it is said back to whoever gave it: what it is, where that is short to say — or what kind of thing. */
|
|
23
|
+
function got(value) {
|
|
24
|
+
if (typeof value === "string")
|
|
25
|
+
return JSON.stringify(value.length > 60 ? `${value.slice(0, 60)}…` : value);
|
|
26
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
27
|
+
return String(value);
|
|
28
|
+
if (Array.isArray(value))
|
|
29
|
+
return value.length === 0 ? "an empty list" : "a list";
|
|
30
|
+
return value === null ? "null" : typeof value === "object" ? "an object" : typeof value;
|
|
31
|
+
}
|
|
32
|
+
/** A field a thing does not take, if it has one. */
|
|
33
|
+
const beside = (given, takes) => Object.keys(given).find(field => !takes.includes(field));
|
|
34
|
+
const IMAGES = new Set(IMAGE_KINDS);
|
|
35
|
+
const BASE64 = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
36
|
+
/** What a user's message says, or a tool's result: text, or parts of text and images — as it is to be sent. */
|
|
37
|
+
function saidOf(content, at) {
|
|
38
|
+
if (typeof content === "string")
|
|
39
|
+
return content;
|
|
40
|
+
if (!Array.isArray(content) || content.length === 0)
|
|
41
|
+
throw new Error(`${at} is text, or a list of parts — text and images: got ${got(content)}`);
|
|
42
|
+
return content.map((part, index) => {
|
|
43
|
+
const here = `${at}[${index}]`;
|
|
44
|
+
const isPart = isJsonObject(part) && (part.type === "text" || part.type === "image");
|
|
45
|
+
if (!isPart) {
|
|
46
|
+
throw new Error(`${here} is a part — { type: "text", text } or { type: "image", mimeType, data }: got ${isJsonObject(part) && part.type !== undefined ? `one of type ${got(part.type)}` : got(part)}`);
|
|
47
|
+
}
|
|
48
|
+
if (part.type === "text") {
|
|
49
|
+
if (typeof part.text !== "string")
|
|
50
|
+
throw new Error(`${here}.text is text: got ${got(part.text)}`);
|
|
51
|
+
const more = beside(part, ["type", "text"]);
|
|
52
|
+
if (more !== undefined)
|
|
53
|
+
throw new Error(`${here} has a field a part of text does not take: ${more} (it takes: type, text)`);
|
|
54
|
+
return { type: "text", text: part.text };
|
|
55
|
+
}
|
|
56
|
+
const { mimeType, data } = part;
|
|
57
|
+
if (typeof mimeType !== "string" || !IMAGES.has(mimeType) || typeof data !== "string" || !BASE64.test(data) || beside(part, ["type", "mimeType", "data"]) !== undefined) {
|
|
58
|
+
throw new Error(`${here} is an image of a kind a model is shown — ${IMAGE_KINDS.join(", ")} — with its data in base64`);
|
|
59
|
+
}
|
|
60
|
+
return { type: "image", mimeType, data };
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/** A reply, as it came: held to nothing but what every reply is. */
|
|
64
|
+
function replyOf(message, at) {
|
|
65
|
+
const { content } = message;
|
|
66
|
+
if (!Array.isArray(content))
|
|
67
|
+
throw new Error(`${at}.content is a reply's parts, as the reply came: got ${got(content)}`);
|
|
68
|
+
for (const [index, part] of content.entries()) {
|
|
69
|
+
const here = `${at}.content[${index}]`;
|
|
70
|
+
if (!isJsonObject(part))
|
|
71
|
+
throw new Error(`${here} is a part of a reply, as the reply came: got ${got(part)}`);
|
|
72
|
+
if (typeof part.type !== "string")
|
|
73
|
+
throw new Error(`${here} is a part of a reply, as the reply came: it has no type`);
|
|
74
|
+
if (part.type === "toolCall" && (typeof part.id !== "string" || typeof part.name !== "string" || !isJsonObject(part.arguments))) {
|
|
75
|
+
throw new Error(`${here} is a tool's call — its id, the tool's name, and its arguments — as the reply came`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return message;
|
|
79
|
+
}
|
|
80
|
+
const ROLES = `"user", "assistant" or "toolResult"`;
|
|
81
|
+
function messageOfAConversation(message, at) {
|
|
82
|
+
if (!isJsonObject(message))
|
|
83
|
+
throw new Error(`${at} is a message — from the user, a reply as it came, or a tool's result: got ${got(message)}`);
|
|
84
|
+
const { role } = message;
|
|
85
|
+
if (role === "user") {
|
|
86
|
+
const content = saidOf(message.content, `${at}.content`);
|
|
87
|
+
const more = beside(message, ["role", "content"]);
|
|
88
|
+
if (more !== undefined)
|
|
89
|
+
throw new Error(`${at} has a field a user's message does not take: ${more} (it takes: role, content)`);
|
|
90
|
+
return { role, content };
|
|
91
|
+
}
|
|
92
|
+
if (role === "assistant")
|
|
93
|
+
return replyOf(message, at);
|
|
94
|
+
if (role === "toolResult") {
|
|
95
|
+
const { toolCallId, toolName, isError } = message;
|
|
96
|
+
if (typeof toolCallId !== "string" || toolCallId === "")
|
|
97
|
+
throw new Error(`${at}.toolCallId is the id of the call it answers, as the reply had it: got ${got(toolCallId)}`);
|
|
98
|
+
if (typeof toolName !== "string" || toolName === "")
|
|
99
|
+
throw new Error(`${at}.toolName is the name of the tool that was called: got ${got(toolName)}`);
|
|
100
|
+
const said = saidOf(message.content, `${at}.content`);
|
|
101
|
+
if (isError != null && typeof isError !== "boolean")
|
|
102
|
+
throw new Error(`${at}.isError is true or false: got ${got(isError)}`);
|
|
103
|
+
const more = beside(message, ["role", "toolCallId", "toolName", "content", "isError"]);
|
|
104
|
+
if (more !== undefined)
|
|
105
|
+
throw new Error(`${at} has a field a tool's result does not take: ${more} (it takes: role, toolCallId, toolName, content, isError)`);
|
|
106
|
+
// (As a provider is sent one: its words as parts, and whether it failed said either way.)
|
|
107
|
+
return { role, toolCallId, toolName, content: typeof said === "string" ? [{ type: "text", text: said }] : said, isError: isError === true };
|
|
108
|
+
}
|
|
109
|
+
throw new Error(`${at}: a message's role is ${ROLES}, not ${got(role)}${role === "system" ? " (what a model is told to be is the request's `system`)" : ""}`);
|
|
110
|
+
}
|
|
111
|
+
/** The tools a model is told of: each declared, as a program declares a tool of its own to an agent — and none named. */
|
|
112
|
+
function declaredOf(tools) {
|
|
113
|
+
if (!Array.isArray(tools))
|
|
114
|
+
throw new Error(`tools is a list of tools, each declared: got ${got(tools)}`);
|
|
115
|
+
const named = tools.findIndex(tool => typeof tool === "string");
|
|
116
|
+
if (named !== -1) {
|
|
117
|
+
throw new Error(`tools[${named}]: a tool is declared here — its name, its description, its parameters — and not named: no agent is asked, whose tool ${JSON.stringify(tools[named])} would be`);
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
// (Whether one may run beside another, or ends anything, is a question for whoever runs tools: here nobody does.)
|
|
121
|
+
return toolsOf(tools).declared.map(({ name, description, parameters }) => ({ name, description, parameters }));
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
throw new Error(`tools: ${messageOf(err)}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* A request as a program gave it, held to what one is: returns it as a
|
|
129
|
+
* backend is handed it. Throws with what is wrong, in words for whoever
|
|
130
|
+
* wrote it. (What is given as null is not given: it is how JSON says so.)
|
|
131
|
+
*/
|
|
132
|
+
export function requestOf(given) {
|
|
133
|
+
if (!isJsonObject(given))
|
|
134
|
+
throw new Error(`a request is an object — the conversation so far (messages), and what goes with it: got ${got(given)}`);
|
|
135
|
+
const unknown = beside(given, REQUEST_FIELDS);
|
|
136
|
+
if (unknown !== undefined)
|
|
137
|
+
throw new Error(`a request has no ${unknown}: it has ${REQUEST_FIELDS.join(", ")}`);
|
|
138
|
+
const { messages, system, tools, model, thinking, maxTokens, temperature, toolChoice, timeout } = given;
|
|
139
|
+
if (!Array.isArray(messages) || messages.length === 0)
|
|
140
|
+
throw new Error(`messages is the conversation so far, a list of messages with at least one in it: got ${got(messages)}`);
|
|
141
|
+
const context = { messages: messages.map((message, index) => messageOfAConversation(message, `messages[${index}]`)) };
|
|
142
|
+
if (system != null) {
|
|
143
|
+
if (typeof system !== "string")
|
|
144
|
+
throw new Error(`system is text: got ${got(system)}`);
|
|
145
|
+
context.systemPrompt = system;
|
|
146
|
+
}
|
|
147
|
+
if (tools != null) {
|
|
148
|
+
const declared = declaredOf(tools);
|
|
149
|
+
if (declared.length > 0)
|
|
150
|
+
context.tools = declared;
|
|
151
|
+
}
|
|
152
|
+
const asked = { context };
|
|
153
|
+
if (model != null) {
|
|
154
|
+
// (Which of its strokes parts the provider from the model is for whoever has the models to say.)
|
|
155
|
+
if (typeof model !== "string" || !/^[^/]+\/./.test(model))
|
|
156
|
+
throw new Error(`model is a model by name, "provider/model": got ${got(model)}`);
|
|
157
|
+
asked.model = model;
|
|
158
|
+
}
|
|
159
|
+
if (thinking != null) {
|
|
160
|
+
if (typeof thinking !== "string" || thinking === "")
|
|
161
|
+
throw new Error(`thinking is a level by name: got ${got(thinking)}`);
|
|
162
|
+
asked.thinking = thinking;
|
|
163
|
+
}
|
|
164
|
+
if (maxTokens != null) {
|
|
165
|
+
if (typeof maxTokens !== "number" || !Number.isInteger(maxTokens) || maxTokens <= 0)
|
|
166
|
+
throw new Error(`maxTokens is a whole number above 0: got ${got(maxTokens)}`);
|
|
167
|
+
asked.maxTokens = maxTokens;
|
|
168
|
+
}
|
|
169
|
+
if (temperature != null) {
|
|
170
|
+
if (typeof temperature !== "number" || !Number.isFinite(temperature) || temperature < 0)
|
|
171
|
+
throw new Error(`temperature is a number that is not negative: got ${got(temperature)}`);
|
|
172
|
+
asked.temperature = temperature;
|
|
173
|
+
}
|
|
174
|
+
if (toolChoice != null) {
|
|
175
|
+
if (toolChoice !== "auto" && toolChoice !== "none")
|
|
176
|
+
throw new Error(`toolChoice is "auto" or "none": got ${got(toolChoice)}`);
|
|
177
|
+
asked.toolChoice = toolChoice;
|
|
178
|
+
}
|
|
179
|
+
if (timeout != null) {
|
|
180
|
+
if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0)
|
|
181
|
+
throw new Error(`timeout is milliseconds, a number above 0: got ${got(timeout)}`);
|
|
182
|
+
asked.timeout = timeout;
|
|
183
|
+
}
|
|
184
|
+
return asked;
|
|
185
|
+
}
|
package/dist/pi-adapter.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type AgentBackend, type AgentCall } from "./agent-backend.js";
|
|
1
|
+
import { type AgentBackend, type AgentCall, type ModelAsk, type ModelCall } from "./agent-backend.js";
|
|
2
2
|
import type { ModelConfig } from "./runner-types.js";
|
|
3
3
|
/** The pi version this runtime is built against (recorded in each project's meta.json). */
|
|
4
4
|
export declare const piVersion: () => Promise<string>;
|
|
@@ -24,25 +24,26 @@ export declare const HOOKS: string[];
|
|
|
24
24
|
* is served, until it is named here.
|
|
25
25
|
*/
|
|
26
26
|
export declare const LOOPS: string[];
|
|
27
|
-
/**
|
|
28
|
-
* The pi coding agent: a fresh in-memory session for every call — in this
|
|
29
|
-
* process, or in a process of its own (see agent-host.ts). A call that has
|
|
30
|
-
* extensions is served in one: an extension is somebody's code, and where it
|
|
31
|
-
* runs it can hold up, or bring down, everything else that runs there. And
|
|
32
|
-
* so is every call of an adapter that was given variables (`env`: a run's):
|
|
33
|
-
* what an agent finds of pi, which key it has for a provider, and what the
|
|
34
|
-
* commands it runs are given, are a process's — and this process is the
|
|
35
|
-
* runner's, whose own are not a run's to change.
|
|
36
|
-
*/
|
|
37
27
|
export declare class PiAdapter implements AgentBackend {
|
|
38
28
|
private config;
|
|
39
29
|
/** Whether every call is served in a process of its own: where those processes are given variables this one has not. */
|
|
40
30
|
private apart;
|
|
41
31
|
/** The processes that serve this adapter's calls. */
|
|
42
32
|
private hosts;
|
|
33
|
+
/** Who asks a model for a request that is no call of an agent's, where that is this process: made when one first comes. */
|
|
34
|
+
private asks?;
|
|
43
35
|
constructor(config: ModelConfig, env?: Record<string, string>);
|
|
44
36
|
/** Have a call served: here, or by a host. */
|
|
45
37
|
ask(prompt: string, call: AgentCall): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Ask a model once (see AgentBackend.complete): by pi's own models, as pi
|
|
40
|
+
* hands a request to a provider — no session of pi's, and so none of what
|
|
41
|
+
* a session does: no tool is run, nothing is asked again, nothing summed
|
|
42
|
+
* up. Asked where this adapter's calls have their models asked for them:
|
|
43
|
+
* here — or, for an adapter that was given variables, by the process
|
|
44
|
+
* that was started with them, where a key that is among them is the key.
|
|
45
|
+
*/
|
|
46
|
+
complete(request: ModelAsk, call: ModelCall): Promise<Record<string, unknown>>;
|
|
46
47
|
/** End the processes that served calls: what this adapter was for is over. (It can be asked again: it starts them anew.) */
|
|
47
48
|
close(): Promise<void>;
|
|
48
49
|
private createSession;
|
package/dist/pi-adapter.js
CHANGED
|
@@ -4,7 +4,7 @@ import { createAgentHosts } from "./agent-hosts.js";
|
|
|
4
4
|
import { CutOff, NOT_WAITED_FOR, addReply, atLeast, nothingYet } from "./agent-backend.js";
|
|
5
5
|
import * as http from "http";
|
|
6
6
|
import * as https from "https";
|
|
7
|
-
import { dataOf, isData, notAsked, usageAtEnd, wholeOf } from "./model-relay.js";
|
|
7
|
+
import { createModelRelay, dataOf, isData, modelNamed, notAsked, usageAtEnd, wholeOf } from "./model-relay.js";
|
|
8
8
|
/**
|
|
9
9
|
* pi is loaded when it is first needed, not when this file is: it is most of
|
|
10
10
|
* what the runtime weighs, and listing projects, reading logs, or running a
|
|
@@ -749,12 +749,16 @@ function answerOf(session, taken = false) {
|
|
|
749
749
|
* commands it runs are given, are a process's — and this process is the
|
|
750
750
|
* runner's, whose own are not a run's to change.
|
|
751
751
|
*/
|
|
752
|
+
/** How long a provider is waited for, for one request to a model that does not say: as pi's agent waits for one. */
|
|
753
|
+
const MODEL_TIMEOUT_MS = 300_000;
|
|
752
754
|
export class PiAdapter {
|
|
753
755
|
config;
|
|
754
756
|
/** Whether every call is served in a process of its own: where those processes are given variables this one has not. */
|
|
755
757
|
apart;
|
|
756
758
|
/** The processes that serve this adapter's calls. */
|
|
757
759
|
hosts;
|
|
760
|
+
/** Who asks a model for a request that is no call of an agent's, where that is this process: made when one first comes. */
|
|
761
|
+
asks;
|
|
758
762
|
constructor(config, env) {
|
|
759
763
|
this.config = config;
|
|
760
764
|
// (Variables that are none are none given: such a run's calls are served as any run's.)
|
|
@@ -765,9 +769,53 @@ export class PiAdapter {
|
|
|
765
769
|
async ask(prompt, call) {
|
|
766
770
|
return call.extensions || this.apart ? this.hosts.ask(this.config, prompt, call) : this.serve(prompt, call);
|
|
767
771
|
}
|
|
772
|
+
/**
|
|
773
|
+
* Ask a model once (see AgentBackend.complete): by pi's own models, as pi
|
|
774
|
+
* hands a request to a provider — no session of pi's, and so none of what
|
|
775
|
+
* a session does: no tool is run, nothing is asked again, nothing summed
|
|
776
|
+
* up. Asked where this adapter's calls have their models asked for them:
|
|
777
|
+
* here — or, for an adapter that was given variables, by the process
|
|
778
|
+
* that was started with them, where a key that is among them is the key.
|
|
779
|
+
*/
|
|
780
|
+
async complete(request, call) {
|
|
781
|
+
throwIfAborted(call.signal);
|
|
782
|
+
const { context, model, thinking, timeout, ...sampled } = request;
|
|
783
|
+
// (Without a name of its own, the project's model — by name too: which of pi's that is, is for whoever asks it to say.)
|
|
784
|
+
const named = model ?? `${this.config.provider}/${this.config.model}`;
|
|
785
|
+
const stroke = named.indexOf("/");
|
|
786
|
+
const relay = this.apart ? this.hosts.asks : this.asks ??= createModelRelay();
|
|
787
|
+
const events = await relay.ask({
|
|
788
|
+
named, model: { provider: named.slice(0, stroke), id: named.slice(stroke + 1) }, context,
|
|
789
|
+
// (As pi's agent has a provider waited for, where the request does not say.)
|
|
790
|
+
options: { ...sampled, ...(thinking !== undefined && { reasoning: thinking }), timeoutMs: timeout ?? MODEL_TIMEOUT_MS },
|
|
791
|
+
}, call.signal);
|
|
792
|
+
if (!events)
|
|
793
|
+
throw new Error("The model could not be asked: pi was not to be had to ask it by.");
|
|
794
|
+
let last;
|
|
795
|
+
for await (const event of events)
|
|
796
|
+
last = event;
|
|
797
|
+
const reply = last?.type === "done" ? last.message : last?.type === "error" ? last.error : undefined;
|
|
798
|
+
if (!last || !isData(reply) || typeof reply !== "object" || reply === null || Array.isArray(reply))
|
|
799
|
+
throw new Error("The model could not be asked: nothing came of asking it.");
|
|
800
|
+
const said = reply;
|
|
801
|
+
// What it used, however it ended — where it used anything: a reply that failed half-way was paid for as far as it came.
|
|
802
|
+
const usage = usageAtEnd(last);
|
|
803
|
+
if (usage && usage.input + usage.output + usage.cacheRead + usage.cacheWrite > 0) {
|
|
804
|
+
call.used?.({ model: `${String(said.provider)}/${String(said.model)}`, input: usage.input, output: usage.output, cacheRead: usage.cacheRead, cacheWrite: usage.cacheWrite, cost: usage.cost.total });
|
|
805
|
+
}
|
|
806
|
+
throwIfAborted(call.signal);
|
|
807
|
+
const kind = last.unasked;
|
|
808
|
+
if (kind !== undefined) {
|
|
809
|
+
throw new Error(kind === "model" && model === undefined
|
|
810
|
+
? `The project's model, ${named}, is not one pi has. A request to a model is answered by the model it names, or by the project's: there is no agent here whose own choice it could fall back on.`
|
|
811
|
+
: String(said.errorMessage));
|
|
812
|
+
}
|
|
813
|
+
return said;
|
|
814
|
+
}
|
|
768
815
|
/** End the processes that served calls: what this adapter was for is over. (It can be asked again: it starts them anew.) */
|
|
769
816
|
async close() {
|
|
770
|
-
await this.hosts.close();
|
|
817
|
+
await Promise.all([this.hosts.close(), this.asks?.close()]);
|
|
818
|
+
this.asks = undefined;
|
|
771
819
|
}
|
|
772
820
|
async createSession(cwd, systemPrompt, signal, asked = {}) {
|
|
773
821
|
// Each step below is given up on the moment the call is cancelled.
|
|
@@ -877,11 +925,9 @@ export class PiAdapter {
|
|
|
877
925
|
const modelRuntime = await raceAbort(ModelRuntime.create({ signal }));
|
|
878
926
|
const asking = { models: modelRuntime, seen: nothingYet(), answering: 0, readying: 0, cut: new AbortController(), over: false, gone: false, refusal: new AbortController() };
|
|
879
927
|
watch(modelRuntime, asking);
|
|
880
|
-
// (A call names it "provider/model": the provider's name ends at a stroke, and either name may have more of them.)
|
|
881
|
-
const strokes = [...(asked.model ?? "").matchAll(/\//g)].map(stroke => stroke.index);
|
|
882
928
|
const model = asked.model === undefined
|
|
883
929
|
? modelRuntime.getModel(this.config.provider, this.config.model)
|
|
884
|
-
:
|
|
930
|
+
: modelNamed(modelRuntime, asked.model);
|
|
885
931
|
if (asked.model !== undefined && !model) {
|
|
886
932
|
throw new Error(`Unknown model "${asked.model}". A model is named "provider/model", as pi's model catalog has it.`);
|
|
887
933
|
}
|
package/dist/project.d.ts
CHANGED
|
@@ -7,6 +7,14 @@ import type { TramaConfig } from "./runner-types.js";
|
|
|
7
7
|
*/
|
|
8
8
|
export declare function ensureProjectScaffold(projectDir: string): void;
|
|
9
9
|
export declare function loadConfig(): TramaConfig;
|
|
10
|
+
/**
|
|
11
|
+
* Where a project is, that is named as `repairWith` names one: by a path —
|
|
12
|
+
* what has a stroke in it, or begins with a dot or with "~" for the user's
|
|
13
|
+
* home — or else by its name among the user's projects.
|
|
14
|
+
*/
|
|
15
|
+
export declare function projectNamed(given: string): string;
|
|
16
|
+
/** Whether what names a project names it by a path, and not by its name among the user's projects. */
|
|
17
|
+
export declare const namesAPath: (given: string) => boolean;
|
|
10
18
|
/** The time a run is given, held to what a time is. */
|
|
11
19
|
export declare function checkedTimeout(timeout: number): number;
|
|
12
20
|
/** How many fixes a run may be tried with, held to what a count is. */
|
package/dist/project.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// keeps around a program.ts, the config a run of it goes by, and the state
|
|
3
3
|
// its last run left.
|
|
4
4
|
import { readFileSync, writeFileSync, mkdirSync, existsSync, symlinkSync, rmSync, lstatSync, readlinkSync, accessSync, constants, } from "fs";
|
|
5
|
-
import { join, dirname } from "path";
|
|
5
|
+
import { join, dirname, resolve, sep } from "path";
|
|
6
6
|
import { homedir } from "os";
|
|
7
7
|
import { fileURLToPath } from "url";
|
|
8
8
|
import { checkedBudget } from "./budget.js";
|
|
@@ -158,6 +158,9 @@ function checkedConfig(merged, source) {
|
|
|
158
158
|
[Number.isInteger(merged.journalRuns) && merged.journalRuns >= 0, `"journalRuns" must be a non-negative integer (got ${merged.journalRuns})`],
|
|
159
159
|
[merged.agentChanges === "keep" || merged.agentChanges === "discard", `"agentChanges" must be "keep" or "discard" (got ${JSON.stringify(merged.agentChanges)})`],
|
|
160
160
|
];
|
|
161
|
+
if (merged.repairWith !== undefined) {
|
|
162
|
+
checks.push([s(merged.repairWith), `"repairWith" must be a project — its name, or a path to it (got ${JSON.stringify(merged.repairWith)})`]);
|
|
163
|
+
}
|
|
161
164
|
for (const [ok, msg] of checks) {
|
|
162
165
|
if (!ok)
|
|
163
166
|
throw new Error(`Invalid ${source}: ${msg}`);
|
|
@@ -171,6 +174,20 @@ function checkedConfig(merged, source) {
|
|
|
171
174
|
throw new Error(`Invalid ${source}: ${messageOf(err)}`);
|
|
172
175
|
}
|
|
173
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Where a project is, that is named as `repairWith` names one: by a path —
|
|
179
|
+
* what has a stroke in it, or begins with a dot or with "~" for the user's
|
|
180
|
+
* home — or else by its name among the user's projects.
|
|
181
|
+
*/
|
|
182
|
+
export function projectNamed(given) {
|
|
183
|
+
if (given === "~" || given.startsWith("~/"))
|
|
184
|
+
return join(homedir(), given.slice(2));
|
|
185
|
+
if (namesAPath(given))
|
|
186
|
+
return resolve(given);
|
|
187
|
+
return join(homedir(), ".trama", "projects", given);
|
|
188
|
+
}
|
|
189
|
+
/** Whether what names a project names it by a path, and not by its name among the user's projects. */
|
|
190
|
+
export const namesAPath = (given) => given.includes("/") || given.includes(sep) || given.startsWith(".") || given.startsWith("~");
|
|
174
191
|
/** The time a run is given, held to what a time is. */
|
|
175
192
|
export function checkedTimeout(timeout) {
|
|
176
193
|
if (!Number.isFinite(timeout) || timeout <= 0)
|
package/dist/runner-backend.js
CHANGED
|
@@ -29,20 +29,44 @@ export function createBackend(config, meter = createMeter(), spent = () => { },
|
|
|
29
29
|
refused ??= why;
|
|
30
30
|
return new Error(why);
|
|
31
31
|
};
|
|
32
|
+
/** Take a call on, in the run's count — or refuse it, with why, where nothing more may be asked. */
|
|
33
|
+
const admitted = () => {
|
|
34
|
+
try {
|
|
35
|
+
return meter.admit();
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
throw overBudget(messageOf(err));
|
|
39
|
+
}
|
|
40
|
+
};
|
|
32
41
|
return {
|
|
33
42
|
spent: () => meter.spent(),
|
|
34
43
|
refused: () => refused,
|
|
35
44
|
counts: () => meter.capped(),
|
|
45
|
+
// One request to a model is a call too: taken on or refused as an agent's is, and counted for what its
|
|
46
|
+
// reply used. There is nothing in it to stop half-way — it is asked once, and a reply that is coming is
|
|
47
|
+
// let come, as an agent's is.
|
|
48
|
+
async complete(request, call) {
|
|
49
|
+
const account = admitted();
|
|
50
|
+
const work = backend.complete(request, {
|
|
51
|
+
...call,
|
|
52
|
+
used(usage) {
|
|
53
|
+
account.sofar(usage);
|
|
54
|
+
call.used?.(usage);
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
const off = onAbort(call.signal, () => { cutOff.add(work); });
|
|
58
|
+
const settled = () => {
|
|
59
|
+
off();
|
|
60
|
+
account.close();
|
|
61
|
+
cutOff.delete(work);
|
|
62
|
+
};
|
|
63
|
+
work.then(settled, settled);
|
|
64
|
+
return rejectOnAbort(work, call.signal);
|
|
65
|
+
},
|
|
36
66
|
// The interface promises that a cancelled call rejects, and a run's end
|
|
37
67
|
// waits on that. Every backend is held to it here, rather than trusted with it.
|
|
38
68
|
async ask(prompt, call) {
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
account = meter.admit();
|
|
42
|
-
}
|
|
43
|
-
catch (err) {
|
|
44
|
-
throw overBudget(messageOf(err));
|
|
45
|
-
}
|
|
69
|
+
const account = admitted();
|
|
46
70
|
// Stops the call where the budget is reached while it is under way.
|
|
47
71
|
const over = new AbortController();
|
|
48
72
|
let why;
|
package/dist/runner-types.d.ts
CHANGED
|
@@ -46,6 +46,22 @@ export interface RunOptions {
|
|
|
46
46
|
* program asked for has the config's `budget`, if that has one.
|
|
47
47
|
*/
|
|
48
48
|
budget?: Budget;
|
|
49
|
+
/**
|
|
50
|
+
* The project that is asked for a fix where the run's program fails, in
|
|
51
|
+
* place of pi's agent: by its name among the user's projects, or by a
|
|
52
|
+
* path. Its program is run with a copy of the failing project as its
|
|
53
|
+
* workspace, is handed as arguments `program` (the program that failed),
|
|
54
|
+
* `error`, `journal` (the path of the failed run's record), `kept`
|
|
55
|
+
* (whether what it changes in the copy beside the program reaches the
|
|
56
|
+
* project) and `types` (the API as a program's author is shown it) — and
|
|
57
|
+
* answers with `{ program }`: the whole of the fixed program. (Or with
|
|
58
|
+
* `{ unfixable }`, saying why there is no fix: the run then fails with
|
|
59
|
+
* that, and nobody is asked again.) It is a run
|
|
60
|
+
* of the failing run's own: counted in its budget, stopped with it, and
|
|
61
|
+
* not itself fixed where it fails. Without it, the config's `repairWith`,
|
|
62
|
+
* if that has one.
|
|
63
|
+
*/
|
|
64
|
+
repairWith?: string;
|
|
49
65
|
/**
|
|
50
66
|
* Stops the run: its program is stopped, no repair is attempted, and the
|
|
51
67
|
* run ends as failed. A fix that was being tried leaves no trace.
|
|
@@ -76,6 +92,8 @@ export interface TramaConfig extends ModelConfig {
|
|
|
76
92
|
* is taken from the agent.
|
|
77
93
|
*/
|
|
78
94
|
agentChanges: "keep" | "discard";
|
|
95
|
+
/** The project asked for a fix, in place of pi's agent, where a run is not given one of its own (see RunOptions). */
|
|
96
|
+
repairWith?: string;
|
|
79
97
|
/**
|
|
80
98
|
* What a run may use of a model (see RunOptions) where it is not given a
|
|
81
99
|
* budget of its own — and what a `create` or an `update` may: writing the
|
package/dist/runner.d.ts
CHANGED
|
@@ -145,11 +145,11 @@ export type Making = (request: MakeRequest, asked: AskedToMake) => Promise<void>
|
|
|
145
145
|
interface AskedFor {
|
|
146
146
|
/** How many runs deep it is: one more than the run that asked. */
|
|
147
147
|
depth: number;
|
|
148
|
-
/** The call that asked for it: which run's, and which of that run's calls. */
|
|
148
|
+
/** The call that asked for it: which run's, and which of that run's calls — or none of them, where the run itself asked: for a fix. */
|
|
149
149
|
parent: {
|
|
150
150
|
run: string;
|
|
151
151
|
journal: string;
|
|
152
|
-
seq
|
|
152
|
+
seq?: number;
|
|
153
153
|
};
|
|
154
154
|
/** Called once this run has a journal of its own. */
|
|
155
155
|
started(run: string, journal: string): void;
|