@trama-dev/runtime 0.6.1 → 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 +100 -0
- package/dist/agent-backend.js +4 -0
- package/dist/agent-host.d.ts +46 -7
- package/dist/agent-host.js +59 -19
- package/dist/agent-hosts.d.ts +8 -0
- package/dist/agent-hosts.js +123 -56
- package/dist/agent.d.ts +18 -5
- package/dist/agent.js +23 -64
- package/dist/bridge-client.d.ts +6 -2
- package/dist/bridge-client.js +7 -1
- package/dist/budget.d.ts +6 -0
- package/dist/budget.js +1 -0
- package/dist/client.d.ts +3 -2
- package/dist/client.js +146 -2
- package/dist/context.js +3 -2
- package/dist/history.d.ts +9 -0
- package/dist/history.js +98 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +73 -41
- package/dist/ipc-server.d.ts +2 -3
- package/dist/ipc-server.js +99 -38
- package/dist/journal-view.d.ts +8 -0
- package/dist/journal-view.js +63 -11
- package/dist/journal.d.ts +10 -4
- package/dist/journal.js +73 -7
- package/dist/merge-deeds.d.ts +22 -0
- package/dist/merge-deeds.js +201 -0
- package/dist/merge-doing.d.ts +9 -0
- package/dist/merge-doing.js +410 -0
- package/dist/merge-settle.d.ts +14 -0
- package/dist/merge-settle.js +166 -0
- package/dist/merge-shared.d.ts +64 -0
- package/dist/merge-shared.js +159 -0
- package/dist/merge.d.ts +150 -0
- package/dist/merge.js +209 -0
- package/dist/model-relay.d.ts +19 -0
- package/dist/model-relay.js +91 -9
- package/dist/model-request.d.ts +9 -0
- package/dist/model-request.js +185 -0
- package/dist/own-tools.d.ts +60 -0
- package/dist/own-tools.js +209 -0
- package/dist/path-guard.js +8 -6
- package/dist/pi-adapter.d.ts +32 -9
- package/dist/pi-adapter.js +198 -51
- package/dist/project.d.ts +39 -0
- package/dist/project.js +270 -0
- package/dist/replace-file.d.ts +13 -2
- package/dist/replace-file.js +23 -6
- package/dist/runner-backend.d.ts +43 -0
- package/dist/runner-backend.js +149 -0
- package/dist/runner-types.d.ts +26 -0
- package/dist/runner.d.ts +10 -125
- package/dist/runner.js +148 -461
- package/dist/schema.d.ts +45 -0
- package/dist/schema.js +113 -0
- package/dist/stopping.js +2 -3
- package/dist/tools.d.ts +8 -1
- package/dist/tools.js +36 -10
- package/dist/tree.d.ts +33 -149
- package/dist/tree.js +43 -1077
- package/dist/types.d.ts +182 -2
- package/dist/workspace.d.ts +28 -1
- package/dist/workspace.js +88 -13
- package/package.json +2 -2
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// A program's own tools, on the runner's side of the bridge.
|
|
2
|
+
//
|
|
3
|
+
// A program hands an agent call tools that are functions of its own. The
|
|
4
|
+
// functions stay where they are, in the program's process: what crosses the
|
|
5
|
+
// bridge with the call is what each is called and called with, and the
|
|
6
|
+
// runner makes of that a tool whose every use asks the program. The bridge
|
|
7
|
+
// only carries calls from a program to its runner, so the program asks for
|
|
8
|
+
// them: beside the agent call it makes another, which the runner holds until
|
|
9
|
+
// a tool of the call's is used, and it says what the tool answered by a third.
|
|
10
|
+
// Nothing listens in the program, and what a tool was asked and answered is
|
|
11
|
+
// seen where everything a program is told is seen.
|
|
12
|
+
import { IMAGE_KINDS, NOT_WAITED_FOR } from "./agent-backend.js";
|
|
13
|
+
import { argumentsOf } from "./schema.js";
|
|
14
|
+
import { onAbort } from "./abort.js";
|
|
15
|
+
import { messageOf } from "./errors.js";
|
|
16
|
+
import { describeJson, isJsonObject } from "./serializable.js";
|
|
17
|
+
/** What a tool's name may be: what every provider takes for one. */
|
|
18
|
+
const NAME = /^[A-Za-z0-9_-]{1,64}$/;
|
|
19
|
+
const DECLARED = ["name", "description", "parameters", "parallel", "ends"];
|
|
20
|
+
const IMAGES = new Set(IMAGE_KINDS);
|
|
21
|
+
/**
|
|
22
|
+
* The images a tool answered with, as its program's client sent them: each
|
|
23
|
+
* its kind and its bytes in base64, and nothing else. (pi hands a model
|
|
24
|
+
* whatever it is given by that name; what is no image is refused here.)
|
|
25
|
+
*/
|
|
26
|
+
export function imagesOf(given) {
|
|
27
|
+
if (given == null)
|
|
28
|
+
return undefined;
|
|
29
|
+
const isImage = (image) => isJsonObject(image) && Object.keys(image).length === 2
|
|
30
|
+
&& typeof image.mimeType === "string" && IMAGES.has(image.mimeType)
|
|
31
|
+
&& typeof image.data === "string" && image.data !== "" && /^[A-Za-z0-9+/]*={0,2}$/.test(image.data);
|
|
32
|
+
if (!Array.isArray(given) || !given.every(isImage)) {
|
|
33
|
+
throw new Error(`Invalid field: images (expected a list of images, each its mimeType — ${[...IMAGES].join(", ")} — and its data, in base64)`);
|
|
34
|
+
}
|
|
35
|
+
return given.length > 0 ? given.map(({ mimeType, data }) => ({ mimeType, data })) : undefined;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A call's `tools` as its program wrote them: the agent's own by name, and
|
|
39
|
+
* the program's, each read for what it declares. What is wrong with one is
|
|
40
|
+
* thrown, in words for whoever wrote it.
|
|
41
|
+
*/
|
|
42
|
+
export function toolsOf(tools) {
|
|
43
|
+
const names = [], declared = [];
|
|
44
|
+
// A tool of the program's is nobody else's by its name: not another of the program's, and not one of the
|
|
45
|
+
// agent's that the call names. (A name of the agent's said twice is the agent's tool twice named, which is
|
|
46
|
+
// the backend's to read — as a list of names and nothing else is.)
|
|
47
|
+
const own = new Set();
|
|
48
|
+
const take = (name) => {
|
|
49
|
+
if (own.has(name) || names.includes(name))
|
|
50
|
+
throw new Error(`Two tools are named ${JSON.stringify(name)}: a tool is called by its name`);
|
|
51
|
+
own.add(name);
|
|
52
|
+
};
|
|
53
|
+
for (const tool of tools) {
|
|
54
|
+
if (typeof tool === "string") {
|
|
55
|
+
if (own.has(tool))
|
|
56
|
+
throw new Error(`Two tools are named ${JSON.stringify(tool)}: a tool is called by its name`);
|
|
57
|
+
names.push(tool);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (!isJsonObject(tool))
|
|
61
|
+
throw new Error(`A tool is a name, or an object with a name, a description and a run: not ${describeJson(tool)}`);
|
|
62
|
+
const { name, description, parameters, parallel, ends } = tool;
|
|
63
|
+
if (typeof name !== "string" || !NAME.test(name)) {
|
|
64
|
+
throw new Error(`A tool's name is letters, digits, "_" and "-", 64 of them at most: not ${typeof name === "string" ? JSON.stringify(name) : describeJson(name)}`);
|
|
65
|
+
}
|
|
66
|
+
const unknown = Object.keys(tool).find(field => !DECLARED.includes(field));
|
|
67
|
+
if (unknown !== undefined)
|
|
68
|
+
throw new Error(`The tool ${JSON.stringify(name)} has a field a tool does not take: ${unknown} (a tool takes: ${DECLARED.join(", ")}, run)`);
|
|
69
|
+
if (typeof description !== "string" || description.trim() === "") {
|
|
70
|
+
throw new Error(`The tool ${JSON.stringify(name)} has no description: it is all a model is told of what the tool is for`);
|
|
71
|
+
}
|
|
72
|
+
for (const [option, value] of Object.entries({ parallel, ends })) {
|
|
73
|
+
if (value != null && typeof value !== "boolean")
|
|
74
|
+
throw new Error(`The tool ${JSON.stringify(name)}: ${option} is true or false, not ${describeJson(value)}`);
|
|
75
|
+
}
|
|
76
|
+
let called;
|
|
77
|
+
try {
|
|
78
|
+
called = argumentsOf(parameters ?? {});
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
throw new Error(`The tool ${JSON.stringify(name)}: ${messageOf(err)}`);
|
|
82
|
+
}
|
|
83
|
+
take(name);
|
|
84
|
+
declared.push({ name, description, parameters: called.schema, ...(parallel === true && { parallel }), ...(ends === true && { ends }), held: called.held });
|
|
85
|
+
}
|
|
86
|
+
return { names, declared };
|
|
87
|
+
}
|
|
88
|
+
/** How many calls that are over are remembered as that. */
|
|
89
|
+
const MAX_CLOSED = 1024;
|
|
90
|
+
/** The tools of one program's agent calls: how each is asked, and how the program comes to hear of it. */
|
|
91
|
+
export function createToolAsks() {
|
|
92
|
+
const ways = new Map();
|
|
93
|
+
/**
|
|
94
|
+
* The calls that are over, by their ids — the latest of them: whoever asks what is next of one is told
|
|
95
|
+
* that nothing is, and is not taken for one who asks ahead of a call to come.
|
|
96
|
+
*/
|
|
97
|
+
const closed = new Set();
|
|
98
|
+
/** Whoever asked of a call that is not here yet: it and the asking were sent together, and either may be first. */
|
|
99
|
+
const early = new Map();
|
|
100
|
+
const tell = (way, event) => {
|
|
101
|
+
const waiting = way.waiting.shift();
|
|
102
|
+
if (waiting)
|
|
103
|
+
waiting(event);
|
|
104
|
+
else
|
|
105
|
+
way.events.push(event);
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
open(of, declared, record) {
|
|
109
|
+
if (of === "")
|
|
110
|
+
throw new Error("Invalid field: toolsId (expected a non-empty string)");
|
|
111
|
+
if (ways.has(of) || closed.has(of))
|
|
112
|
+
throw new Error(`Duplicate toolsId: ${of}`);
|
|
113
|
+
const way = { events: [], waiting: [], pending: new Map(), uses: 0, closed: false };
|
|
114
|
+
ways.set(of, way);
|
|
115
|
+
for (const wake of early.get(of) ?? [])
|
|
116
|
+
wake();
|
|
117
|
+
early.delete(of);
|
|
118
|
+
const tools = declared.map(({ held, ...tool }) => ({
|
|
119
|
+
...tool,
|
|
120
|
+
run: (given, id, signal) => new Promise((resolve) => {
|
|
121
|
+
if (way.closed || signal?.aborted)
|
|
122
|
+
return resolve(NOT_WAITED_FOR);
|
|
123
|
+
// A backend holds a model's call to the tool's parameters before it asks; what asks here may be
|
|
124
|
+
// somebody else's word of what a backend said (a process that serves the call, with other code in it).
|
|
125
|
+
// The program is asked with what its tool declared, or not at all.
|
|
126
|
+
let args;
|
|
127
|
+
try {
|
|
128
|
+
args = held(given);
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
return resolve({ text: `The tool ${JSON.stringify(tool.name)} was called with what its parameters do not allow: ${messageOf(err).replace(/^Schema validation failed: /, "")}`, failed: true });
|
|
132
|
+
}
|
|
133
|
+
const key = String(++way.uses);
|
|
134
|
+
const answered = record(id, tool.name, args);
|
|
135
|
+
let off = () => { };
|
|
136
|
+
// (An answer the program gave is written down. One that was not waited for is no answer: such a use has none on record.)
|
|
137
|
+
way.pending.set(key, (answer) => {
|
|
138
|
+
way.pending.delete(key);
|
|
139
|
+
off();
|
|
140
|
+
if (answer)
|
|
141
|
+
answered(answer);
|
|
142
|
+
resolve(answer ?? NOT_WAITED_FOR);
|
|
143
|
+
});
|
|
144
|
+
off = onAbort(signal, () => {
|
|
145
|
+
const waiting = way.pending.get(key);
|
|
146
|
+
if (!waiting)
|
|
147
|
+
return;
|
|
148
|
+
waiting(null);
|
|
149
|
+
tell(way, { stop: key });
|
|
150
|
+
});
|
|
151
|
+
tell(way, { key, name: tool.name, args });
|
|
152
|
+
}),
|
|
153
|
+
}));
|
|
154
|
+
return {
|
|
155
|
+
tools,
|
|
156
|
+
close() {
|
|
157
|
+
// (A use that is still waiting for the program is waited for by nobody: its call is over. It has
|
|
158
|
+
// the answer of one that was not waited for, and none on record.)
|
|
159
|
+
way.closed = true;
|
|
160
|
+
ways.delete(of);
|
|
161
|
+
if (closed.size >= MAX_CLOSED)
|
|
162
|
+
closed.delete(closed.values().next().value);
|
|
163
|
+
closed.add(of);
|
|
164
|
+
for (const settle of [...way.pending.values()])
|
|
165
|
+
settle(null);
|
|
166
|
+
for (const waiting of way.waiting.splice(0))
|
|
167
|
+
waiting(null);
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
},
|
|
171
|
+
async next(of, gone) {
|
|
172
|
+
if (closed.has(of))
|
|
173
|
+
return null;
|
|
174
|
+
if (!ways.has(of)) {
|
|
175
|
+
const opened = await new Promise((resolve) => {
|
|
176
|
+
const wake = () => resolve(true);
|
|
177
|
+
const waiting = early.get(of) ?? new Set();
|
|
178
|
+
early.set(of, waiting.add(wake));
|
|
179
|
+
void gone.then(() => {
|
|
180
|
+
if (waiting.delete(wake) && waiting.size === 0 && early.get(of) === waiting)
|
|
181
|
+
early.delete(of);
|
|
182
|
+
resolve(false);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
if (!opened)
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
const way = ways.get(of);
|
|
189
|
+
if (!way)
|
|
190
|
+
return null;
|
|
191
|
+
const told = way.events.shift();
|
|
192
|
+
if (told)
|
|
193
|
+
return told;
|
|
194
|
+
return new Promise((resolve) => {
|
|
195
|
+
way.waiting.push(resolve);
|
|
196
|
+
void gone.then(() => {
|
|
197
|
+
const at = way.waiting.indexOf(resolve);
|
|
198
|
+
if (at === -1)
|
|
199
|
+
return;
|
|
200
|
+
way.waiting.splice(at, 1);
|
|
201
|
+
resolve(null);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
answered(of, key, answer) {
|
|
206
|
+
ways.get(of)?.pending.get(key)?.(answer);
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
package/dist/path-guard.js
CHANGED
|
@@ -19,6 +19,9 @@ import { dirname, isAbsolute, join, parse, resolve, sep } from "path";
|
|
|
19
19
|
*/
|
|
20
20
|
const MAX_LINKS = 32;
|
|
21
21
|
const tooManyLinks = (path) => new Error(`Too many levels of symbolic links: ${path}`);
|
|
22
|
+
/** Nothing lies beyond what is not a directory: the system refuses to go on from one, in these words. */
|
|
23
|
+
const notADirectory = (at, path) => Object.assign(new Error(`ENOTDIR: not a directory, ${at} (on the way to ${path})`), { code: "ENOTDIR" });
|
|
24
|
+
const saysNothing = (link) => new Error(`Path cannot be followed: ${link} (it links to nothing, or to what is not readable as text)`);
|
|
22
25
|
/**
|
|
23
26
|
* A step that is not a name: what the walk has come to must be a directory.
|
|
24
27
|
* A path says so by ending in a slash. (It is not the same as a last step of
|
|
@@ -122,7 +125,7 @@ function follow(path, from, watch = {}) {
|
|
|
122
125
|
// Nothing lies beyond a file — not a name, and not "." or ".." either.
|
|
123
126
|
// The system refuses to go on, and so does this.
|
|
124
127
|
if (!stat.isDirectory() && steps.length > 0) {
|
|
125
|
-
throw
|
|
128
|
+
throw notADirectory(next, path);
|
|
126
129
|
}
|
|
127
130
|
at = next;
|
|
128
131
|
continue;
|
|
@@ -134,16 +137,15 @@ function follow(path, from, watch = {}) {
|
|
|
134
137
|
({ at, missing } = instead);
|
|
135
138
|
// As where a file is come to by a step: nothing lies beyond one.
|
|
136
139
|
if (steps.length > 0 && !missing && !lstatSync(at).isDirectory()) {
|
|
137
|
-
throw
|
|
140
|
+
throw notADirectory(at, path);
|
|
138
141
|
}
|
|
139
142
|
continue;
|
|
140
143
|
}
|
|
141
144
|
if (++links > MAX_LINKS)
|
|
142
145
|
throw tooManyLinks(path);
|
|
143
146
|
const target = linkText(next);
|
|
144
|
-
if (target === undefined)
|
|
145
|
-
throw
|
|
146
|
-
}
|
|
147
|
+
if (target === undefined)
|
|
148
|
+
throw saysNothing(next);
|
|
147
149
|
if (isAbsolute(target))
|
|
148
150
|
at = parse(target).root;
|
|
149
151
|
steps = [...stepsOf(target), ...steps];
|
|
@@ -233,7 +235,7 @@ export function linksOf(root) {
|
|
|
233
235
|
try {
|
|
234
236
|
const text = linkText(link);
|
|
235
237
|
if (text === undefined)
|
|
236
|
-
throw
|
|
238
|
+
throw saysNothing(link);
|
|
237
239
|
const { place } = lead(dirname(link), text);
|
|
238
240
|
named.set(link, place);
|
|
239
241
|
return place;
|
package/dist/pi-adapter.d.ts
CHANGED
|
@@ -1,26 +1,49 @@
|
|
|
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>;
|
|
5
|
+
/** What an extension may have a hand in, of a request to a provider: a session where one does asks its model itself. */
|
|
6
|
+
export declare const HANDS_IN: string[];
|
|
5
7
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* what an agent finds of pi, which key it has for a provider, and what the
|
|
12
|
-
* commands it runs are given, are a process's — and this process is the
|
|
13
|
-
* runner's, whose own are not a run's to change.
|
|
8
|
+
* What pi's agent has every request of its own go with, to be told of what
|
|
9
|
+
* a provider is sent and answers: pi's own, which do nothing where no
|
|
10
|
+
* extension has a hand in (HANDS_IN). A request that goes with all of them,
|
|
11
|
+
* by the way the agent asks, is one the agent makes for itself — or one made
|
|
12
|
+
* with all the agent's was made with, which is the same to a provider.
|
|
14
13
|
*/
|
|
14
|
+
export declare const HOOKS: string[];
|
|
15
|
+
/**
|
|
16
|
+
* What else goes with a request the agent makes for itself, and is nothing
|
|
17
|
+
* a provider is handed: what the agent's loop calls as a turn goes, which pi
|
|
18
|
+
* passes along with what a provider is to have. (The functions of
|
|
19
|
+
* pi-agent-core's `createLoopConfig` that pi sets, less the hooks above. The
|
|
20
|
+
* rest of what a loop is set up by is data, and goes as any data.) Named,
|
|
21
|
+
* because it cannot be told otherwise: a function of the loop's is a
|
|
22
|
+
* function like one an asker might mean a provider to have. A pi whose loop
|
|
23
|
+
* is set up by a function more has its agent's requests asked where the call
|
|
24
|
+
* is served, until it is named here.
|
|
25
|
+
*/
|
|
26
|
+
export declare const LOOPS: string[];
|
|
15
27
|
export declare class PiAdapter implements AgentBackend {
|
|
16
28
|
private config;
|
|
17
29
|
/** Whether every call is served in a process of its own: where those processes are given variables this one has not. */
|
|
18
30
|
private apart;
|
|
19
31
|
/** The processes that serve this adapter's calls. */
|
|
20
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?;
|
|
21
35
|
constructor(config: ModelConfig, env?: Record<string, string>);
|
|
22
36
|
/** Have a call served: here, or by a host. */
|
|
23
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>>;
|
|
24
47
|
/** End the processes that served calls: what this adapter was for is over. (It can be asked again: it starts them anew.) */
|
|
25
48
|
close(): Promise<void>;
|
|
26
49
|
private createSession;
|