@trama-dev/runtime 0.5.0 → 0.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/agent-backend.d.ts +127 -1
- package/dist/agent-backend.js +41 -2
- package/dist/agent-host.d.ts +50 -0
- package/dist/agent-host.js +97 -0
- package/dist/agent-hosts.d.ts +14 -0
- package/dist/agent-hosts.js +321 -0
- package/dist/agent.d.ts +10 -2
- package/dist/agent.js +85 -36
- package/dist/bridge-client.d.ts +6 -0
- package/dist/bridge-client.js +31 -8
- package/dist/budget.d.ts +52 -0
- package/dist/budget.js +118 -0
- package/dist/child-process.d.ts +18 -0
- package/dist/child-process.js +29 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.js +4 -4
- package/dist/index.d.ts +3 -1
- package/dist/index.js +191 -56
- package/dist/ipc-server.d.ts +11 -2
- package/dist/ipc-server.js +104 -41
- package/dist/journal-view.d.ts +2 -0
- package/dist/journal-view.js +41 -4
- package/dist/journal.d.ts +12 -4
- package/dist/journal.js +7 -2
- package/dist/model-relay.d.ts +130 -0
- package/dist/model-relay.js +339 -0
- package/dist/pi-adapter.d.ts +34 -9
- package/dist/pi-adapter.js +857 -47
- package/dist/runner-types.d.ts +35 -0
- package/dist/runner.d.ts +190 -4
- package/dist/runner.js +652 -189
- package/dist/stopping.d.ts +33 -2
- package/dist/stopping.js +119 -11
- package/dist/tools.d.ts +2 -1
- package/dist/tools.js +52 -13
- package/dist/tree.d.ts +9 -2
- package/dist/tree.js +25 -3
- package/dist/types.d.ts +100 -12
- package/package.json +1 -1
package/dist/agent-backend.d.ts
CHANGED
|
@@ -1,14 +1,140 @@
|
|
|
1
|
+
import type { ModelRelay } from "./model-relay.js";
|
|
2
|
+
/**
|
|
3
|
+
* What a call used, as far as its backend knows: the tokens the model read and
|
|
4
|
+
* wrote, the ones it read from a cache and wrote to one, and what the backend's
|
|
5
|
+
* price list makes of that in US dollars.
|
|
6
|
+
*/
|
|
7
|
+
export interface AgentUsage {
|
|
8
|
+
/** The model that served the call, as the backend names it. */
|
|
9
|
+
model?: string;
|
|
10
|
+
input: number;
|
|
11
|
+
output: number;
|
|
12
|
+
cacheRead: number;
|
|
13
|
+
cacheWrite: number;
|
|
14
|
+
cost: number;
|
|
15
|
+
}
|
|
16
|
+
/** What replies used, added up as they came — by whoever saw them come. */
|
|
17
|
+
export interface Tally {
|
|
18
|
+
input: number;
|
|
19
|
+
output: number;
|
|
20
|
+
cacheRead: number;
|
|
21
|
+
cacheWrite: number;
|
|
22
|
+
cost: number;
|
|
23
|
+
}
|
|
24
|
+
export declare const nothingYet: () => Tally;
|
|
25
|
+
/** Add to a tally what one reply used, as a model's reply says it. */
|
|
26
|
+
export declare function addReply(tally: Tally, used: {
|
|
27
|
+
input: number;
|
|
28
|
+
output: number;
|
|
29
|
+
cacheRead: number;
|
|
30
|
+
cacheWrite: number;
|
|
31
|
+
cost: {
|
|
32
|
+
total: number;
|
|
33
|
+
};
|
|
34
|
+
}): void;
|
|
35
|
+
/**
|
|
36
|
+
* A count, and what was seen besides: of each figure, the greater. Two
|
|
37
|
+
* counts of one call, made at two places, are each a count of what passed
|
|
38
|
+
* there — and what passed at both is most of it. (Where each saw something
|
|
39
|
+
* the other did not, this is still short of the whole: it is the least the
|
|
40
|
+
* call used, which is what a count that may be short is to be.) Nothing,
|
|
41
|
+
* where no tokens were counted at either.
|
|
42
|
+
*/
|
|
43
|
+
export declare function atLeast(usage: AgentUsage | undefined, seen?: Tally): AgentUsage | undefined;
|
|
44
|
+
/** Of a report, what a report is declared to hold — whatever else a backend put beside it. */
|
|
45
|
+
export declare const declaredUsage: ({ model, input, output, cacheRead, cacheWrite, cost }: AgentUsage) => AgentUsage;
|
|
1
46
|
export interface AgentCall {
|
|
2
47
|
/** The directory the agent works in: what its tools read is here, and what they write lands here. */
|
|
3
48
|
cwd: string;
|
|
4
49
|
/** Instructions for this call, on top of the agent's own. */
|
|
5
50
|
system?: string;
|
|
51
|
+
/** The model that is to serve this call, as the backend names its models — in place of the one it would use. */
|
|
52
|
+
model?: string;
|
|
53
|
+
/** How much the model is to think, as the backend names its levels. */
|
|
54
|
+
thinking?: string;
|
|
55
|
+
/** The agent's tools for this call, by name: these and no others. Without it, the agent's own choice. */
|
|
56
|
+
tools?: string[];
|
|
57
|
+
/**
|
|
58
|
+
* The skills the agent has for this call, of those the backend finds for
|
|
59
|
+
* the user and where it works: all of them, or the ones named. Without it,
|
|
60
|
+
* or with `false` or none named: none.
|
|
61
|
+
*/
|
|
62
|
+
skills?: boolean | string[];
|
|
63
|
+
/** The same of prompt templates: with one, a prompt that begins with its name is the template, filled in. */
|
|
64
|
+
templates?: boolean | string[];
|
|
65
|
+
/**
|
|
66
|
+
* Whether the agent has the user's own extensions of it — or, with
|
|
67
|
+
* "project", those of where it works as well: somebody's code, which the
|
|
68
|
+
* backend runs.
|
|
69
|
+
*/
|
|
70
|
+
extensions?: boolean | "project";
|
|
71
|
+
/**
|
|
72
|
+
* The prompt is text and nothing else: it goes to the model as written,
|
|
73
|
+
* and is not read for what it begins with — a skill called up by name, a
|
|
74
|
+
* command of an extension — though the call has skills or extensions.
|
|
75
|
+
*/
|
|
76
|
+
asText?: boolean;
|
|
6
77
|
/** Cancels the call. A cancelled call rejects; it never resolves with a partial answer. */
|
|
7
78
|
signal?: AbortSignal;
|
|
79
|
+
/**
|
|
80
|
+
* Told what the call has used so far, each time its model is about to be
|
|
81
|
+
* asked again — for the next step of the agent's work, to have a long
|
|
82
|
+
* conversation summed up, or for anything else that is asked of a model on
|
|
83
|
+
* the call's account: whoever hears it may cancel the call there, before
|
|
84
|
+
* more is used. A backend that asks its model once, or cannot say, never
|
|
85
|
+
* calls it. (It does not throw.) Whoever hears it may take a moment over
|
|
86
|
+
* it — it is in another process, say — and return a promise: a backend
|
|
87
|
+
* that can wait for that before it asks its model does.
|
|
88
|
+
*
|
|
89
|
+
* `answered`: the call is over by now — it has its answer, or has
|
|
90
|
+
* failed — and what would be asked is asked beside that: by an
|
|
91
|
+
* extension, as it is told that the session is over. Such a call is not
|
|
92
|
+
* to be cancelled for what it has used: what it ended with stands.
|
|
93
|
+
* Whoever hears says `false` instead (or resolves to it), and the model
|
|
94
|
+
* is then not asked.
|
|
95
|
+
*/
|
|
96
|
+
sofar?: (usage: AgentUsage, answered?: boolean) => unknown;
|
|
97
|
+
/**
|
|
98
|
+
* Told what the call has used as soon as a reply of its model's has come.
|
|
99
|
+
* Nothing waits for it, and nothing is cancelled by it: it is for whoever
|
|
100
|
+
* counts — what the call has used is known to them from then, and not
|
|
101
|
+
* only when the call next asks or is over — and for whoever may lose the
|
|
102
|
+
* call before it says what it used in the end: the runner of a process
|
|
103
|
+
* that serves it, which may have to end that process.
|
|
104
|
+
*/
|
|
105
|
+
had?: (usage: AgentUsage) => void;
|
|
106
|
+
/**
|
|
107
|
+
* Somebody to ask the model by, for a call that is served where code runs
|
|
108
|
+
* that is neither trama's nor the backend's — an extension's, in a process
|
|
109
|
+
* of its own: what a reply used is then read where no such code is. A
|
|
110
|
+
* backend asks by it where it can, and by itself where it cannot.
|
|
111
|
+
*/
|
|
112
|
+
relay?: ModelRelay;
|
|
113
|
+
/**
|
|
114
|
+
* Told what the call used, at most once and before the call settles —
|
|
115
|
+
* however it ended: a call that failed or was cancelled half-way has used
|
|
116
|
+
* what it used. (When all is over that could use anything on the call's
|
|
117
|
+
* account: what an extension asks a model as it is told that the session
|
|
118
|
+
* ends is the call's too.) A backend that cannot know, or a call that used nothing,
|
|
119
|
+
* says nothing. (It does not throw: what it is told is only written down.)
|
|
120
|
+
*/
|
|
121
|
+
used?: (usage: AgentUsage) => void;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A reply that ended at the model's output limit, and not where the model
|
|
125
|
+
* would have ended it. It is not the answer — the call fails with this —
|
|
126
|
+
* and it carries what there was of the reply, for a caller that has a use
|
|
127
|
+
* for that. (The agent goes no further than the reply that was cut off: where
|
|
128
|
+
* that reply was leading up to something still to be done, it was not done.)
|
|
129
|
+
*/
|
|
130
|
+
export declare class CutOff extends Error {
|
|
131
|
+
readonly text: string;
|
|
132
|
+
constructor(text: string);
|
|
8
133
|
}
|
|
9
134
|
export interface AgentBackend {
|
|
10
135
|
/**
|
|
11
|
-
* Run one prompt to completion and return the agent's final text.
|
|
136
|
+
* Run one prompt to completion and return the agent's final text. A final
|
|
137
|
+
* reply that was cut off at the output limit is a rejection, with `CutOff`.
|
|
12
138
|
*
|
|
13
139
|
* Each call stands alone — nothing carries over from one to the next, and
|
|
14
140
|
* everything a call depends on is in its arguments. A backend that cannot
|
package/dist/agent-backend.js
CHANGED
|
@@ -1,9 +1,48 @@
|
|
|
1
1
|
// The one thing the runtime needs from whatever does the LLM work: hand it a
|
|
2
|
-
// prompt, get its final text back.
|
|
2
|
+
// prompt, get its final text back — and hear what that took.
|
|
3
3
|
//
|
|
4
4
|
// Everything trama asks of an LLM goes through this — a program's agent calls,
|
|
5
5
|
// generating a program, repairing one. To a backend they are all the same
|
|
6
6
|
// request; what to ask for, and what to make of the reply, is decided by the
|
|
7
7
|
// caller. Nothing outside the backend's own file knows which agent, harness or
|
|
8
8
|
// model is behind it.
|
|
9
|
-
export {};
|
|
9
|
+
export const nothingYet = () => ({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0 });
|
|
10
|
+
/** Add to a tally what one reply used, as a model's reply says it. */
|
|
11
|
+
export function addReply(tally, used) {
|
|
12
|
+
tally.input += used.input;
|
|
13
|
+
tally.output += used.output;
|
|
14
|
+
tally.cacheRead += used.cacheRead;
|
|
15
|
+
tally.cacheWrite += used.cacheWrite;
|
|
16
|
+
tally.cost += used.cost.total;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A count, and what was seen besides: of each figure, the greater. Two
|
|
20
|
+
* counts of one call, made at two places, are each a count of what passed
|
|
21
|
+
* there — and what passed at both is most of it. (Where each saw something
|
|
22
|
+
* the other did not, this is still short of the whole: it is the least the
|
|
23
|
+
* call used, which is what a count that may be short is to be.) Nothing,
|
|
24
|
+
* where no tokens were counted at either.
|
|
25
|
+
*/
|
|
26
|
+
export function atLeast(usage, seen) {
|
|
27
|
+
const most = (figure) => Math.max(usage?.[figure] ?? 0, seen?.[figure] ?? 0);
|
|
28
|
+
const input = most("input"), output = most("output"), cacheRead = most("cacheRead"), cacheWrite = most("cacheWrite");
|
|
29
|
+
if (input + output + cacheRead + cacheWrite === 0)
|
|
30
|
+
return undefined;
|
|
31
|
+
return { ...(usage?.model !== undefined && { model: usage.model }), input, output, cacheRead, cacheWrite, cost: most("cost") };
|
|
32
|
+
}
|
|
33
|
+
/** Of a report, what a report is declared to hold — whatever else a backend put beside it. */
|
|
34
|
+
export const declaredUsage = ({ model, input, output, cacheRead, cacheWrite, cost }) => ({ model, input, output, cacheRead, cacheWrite, cost });
|
|
35
|
+
/**
|
|
36
|
+
* A reply that ended at the model's output limit, and not where the model
|
|
37
|
+
* would have ended it. It is not the answer — the call fails with this —
|
|
38
|
+
* and it carries what there was of the reply, for a caller that has a use
|
|
39
|
+
* for that. (The agent goes no further than the reply that was cut off: where
|
|
40
|
+
* that reply was leading up to something still to be done, it was not done.)
|
|
41
|
+
*/
|
|
42
|
+
export class CutOff extends Error {
|
|
43
|
+
text;
|
|
44
|
+
constructor(text) {
|
|
45
|
+
super("The model's answer was cut off at its output limit.");
|
|
46
|
+
this.text = text;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type AgentCall, type AgentUsage } from "./agent-backend.js";
|
|
2
|
+
import type { ModelConfig } from "./runner-types.js";
|
|
3
|
+
import { type RelayAnswered, type RelayAsked } from "./model-relay.js";
|
|
4
|
+
/** What a call is made of that can be sent: all of it but where to say what it used, and what cancels it. */
|
|
5
|
+
export type SentCall = Omit<AgentCall, "signal" | "used" | "sofar" | "had" | "relay">;
|
|
6
|
+
/**
|
|
7
|
+
* What a runner says to a host: a call to serve; that the call it is serving
|
|
8
|
+
* may go on, having said what it has used so far; that it is cancelled; and
|
|
9
|
+
* what a model answers that the call had asked by its runner (see
|
|
10
|
+
* model-relay.ts). And to a host that serves no call, but asks models for
|
|
11
|
+
* the calls of others — the runner's own, where the run was given variables,
|
|
12
|
+
* which a process is to be started with to ask by them: what to ask.
|
|
13
|
+
*/
|
|
14
|
+
export type ToHost = {
|
|
15
|
+
ask: {
|
|
16
|
+
config: ModelConfig;
|
|
17
|
+
prompt: string;
|
|
18
|
+
call: SentCall;
|
|
19
|
+
};
|
|
20
|
+
} | {
|
|
21
|
+
go: boolean;
|
|
22
|
+
} | {
|
|
23
|
+
abort: true;
|
|
24
|
+
} | RelayAnswered | RelayAsked;
|
|
25
|
+
/**
|
|
26
|
+
* What a host says to its runner: that it is there; what the call has used
|
|
27
|
+
* so far, where its model is about to be asked again (and the host waits to
|
|
28
|
+
* hear that it may be); what it has used, as each reply of its model's has
|
|
29
|
+
* come (which nothing waits for: it is what is known of the call if this
|
|
30
|
+
* process has to be ended); what it used in the end; how the call ended —
|
|
31
|
+
* its answer, an answer cut off at the output limit (with what there was of
|
|
32
|
+
* it), or its failure; and what the call would have a model asked (or, of a
|
|
33
|
+
* host that asks for others: what the model answered).
|
|
34
|
+
*/
|
|
35
|
+
export type FromHost = {
|
|
36
|
+
ready: true;
|
|
37
|
+
} | {
|
|
38
|
+
sofar: AgentUsage;
|
|
39
|
+
answered?: true;
|
|
40
|
+
} | {
|
|
41
|
+
had: AgentUsage;
|
|
42
|
+
} | {
|
|
43
|
+
used: AgentUsage;
|
|
44
|
+
} | {
|
|
45
|
+
answer: string;
|
|
46
|
+
} | {
|
|
47
|
+
cutOff: string;
|
|
48
|
+
} | {
|
|
49
|
+
error: string;
|
|
50
|
+
} | RelayAsked | RelayAnswered;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// A pi in a process of its own: what serves an agent call that has extensions.
|
|
2
|
+
//
|
|
3
|
+
// An extension is somebody's code, and pi runs it where pi runs. In the
|
|
4
|
+
// runner that is where every run of a tree is carried, counted and stopped
|
|
5
|
+
// from — so there a call with extensions is not served: it is handed to a
|
|
6
|
+
// process like this one (see agent-hosts.ts), which can be put an end to,
|
|
7
|
+
// and whose end is one call's failure and nothing else's.
|
|
8
|
+
//
|
|
9
|
+
// One call at a time, asked for and answered over the channel to the runner
|
|
10
|
+
// that started this process. The serving is the adapter's, as it is in a
|
|
11
|
+
// runner: nothing here knows what pi is. And the call's model is asked over
|
|
12
|
+
// the same channel, where it can be (see model-relay.ts): by the runner,
|
|
13
|
+
// where no extension is.
|
|
14
|
+
import { PiAdapter } from "./pi-adapter.js";
|
|
15
|
+
import { CutOff } from "./agent-backend.js";
|
|
16
|
+
import { messageOf } from "./errors.js";
|
|
17
|
+
import { createModelRelay, relayClient, relayServer, withRepliesWhole } from "./model-relay.js";
|
|
18
|
+
import { groupsUnder, signalGroup } from "./stopping.js";
|
|
19
|
+
const say = (message) => {
|
|
20
|
+
// (A runner that is gone is told nothing: this process is on its way out with it.)
|
|
21
|
+
if (process.connected)
|
|
22
|
+
process.send?.(message);
|
|
23
|
+
};
|
|
24
|
+
/** Cancels the call being served. */
|
|
25
|
+
let cancel;
|
|
26
|
+
/**
|
|
27
|
+
* Lets the call being served go on to ask its model again. It has said what
|
|
28
|
+
* it used so far, and whoever counts that is in the runner: the model is not
|
|
29
|
+
* asked before the runner has said whether the call is to go on.
|
|
30
|
+
*/
|
|
31
|
+
let goOn;
|
|
32
|
+
const answered = (go) => {
|
|
33
|
+
goOn?.(go);
|
|
34
|
+
goOn = undefined;
|
|
35
|
+
};
|
|
36
|
+
/** The end that asks, of what the call being served has its model asked by: this process's runner. */
|
|
37
|
+
const asks = relayClient(say);
|
|
38
|
+
/** The end that answers, where this process is one that asks models for the calls of others: with its own pi, and the keys it was started with. */
|
|
39
|
+
let answers;
|
|
40
|
+
/** Whether this process has been told to end. What it was serving is then stopped, and how that ended is not said: its runner hears that the process did. */
|
|
41
|
+
let leaving = false;
|
|
42
|
+
process.on("message", (message) => {
|
|
43
|
+
if ("go" in message)
|
|
44
|
+
return answered(message.go);
|
|
45
|
+
if ("abort" in message) {
|
|
46
|
+
cancel?.abort();
|
|
47
|
+
return answered(false);
|
|
48
|
+
}
|
|
49
|
+
if ("relayed" in message)
|
|
50
|
+
return asks.heard(message.relayed);
|
|
51
|
+
if ("relay" in message || "relayAbort" in message)
|
|
52
|
+
return (answers ??= relayServer(createModelRelay(), say)).heard(message);
|
|
53
|
+
// (A process that has been told to end serves nothing more: whoever asked hears that it ended.)
|
|
54
|
+
if (leaving)
|
|
55
|
+
return;
|
|
56
|
+
const { config, prompt, call } = message.ask;
|
|
57
|
+
const serving = cancel = new AbortController();
|
|
58
|
+
new PiAdapter(config).serve(prompt, {
|
|
59
|
+
...call,
|
|
60
|
+
signal: serving.signal,
|
|
61
|
+
used: (usage) => say({ used: usage }),
|
|
62
|
+
had: (usage) => say({ had: usage }),
|
|
63
|
+
relay: withRepliesWhole(asks.relay),
|
|
64
|
+
sofar: (usage, has) => new Promise((resolve) => {
|
|
65
|
+
goOn = resolve;
|
|
66
|
+
say({ sofar: usage, ...(has && { answered: true }) });
|
|
67
|
+
}),
|
|
68
|
+
}).then((answer) => { if (!leaving)
|
|
69
|
+
say({ answer }); }, (err) => { if (!leaving)
|
|
70
|
+
say(err instanceof CutOff ? { cutOff: err.text } : { error: messageOf(err) }); });
|
|
71
|
+
});
|
|
72
|
+
// The runner is gone, or has no more use for this process — it says so, or its watchdog does, by asking
|
|
73
|
+
// this process to end: there is nothing left to serve. What this process has under it in groups of their
|
|
74
|
+
// own — an MCP server, what an agent was running — goes first: nobody else knows of it, once this process
|
|
75
|
+
// is gone. (This process's own group is its runner's to end, or the watchdog's; and a process that cannot
|
|
76
|
+
// hear either is ended without being asked again, when the time anything asked to stop is given is up.)
|
|
77
|
+
// And the call it is serving is stopped first, where it is: the system takes a moment to say what is
|
|
78
|
+
// under this process, and in that moment the call is not to go on — to ask its model again, or to answer.
|
|
79
|
+
const leave = (asked) => {
|
|
80
|
+
if (leaving)
|
|
81
|
+
return;
|
|
82
|
+
leaving = true;
|
|
83
|
+
cancel?.abort();
|
|
84
|
+
answered(false);
|
|
85
|
+
void groupsUnder(process.pid).then((groups) => {
|
|
86
|
+
for (const group of groups)
|
|
87
|
+
signalGroup(group, "SIGKILL");
|
|
88
|
+
// (Asked to end, it ends as what was asked to: whoever waits for it is told how it went.)
|
|
89
|
+
if (!asked)
|
|
90
|
+
process.exit(0);
|
|
91
|
+
process.removeAllListeners(asked);
|
|
92
|
+
process.kill(process.pid, asked);
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
process.on("disconnect", () => leave());
|
|
96
|
+
process.on("SIGTERM", leave);
|
|
97
|
+
say({ ready: true });
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type AgentCall } from "./agent-backend.js";
|
|
2
|
+
import type { ModelConfig } from "./runner-types.js";
|
|
3
|
+
export interface AgentHosts {
|
|
4
|
+
/** Have a host serve one call, as the adapter would (see AgentBackend.ask). */
|
|
5
|
+
ask(config: ModelConfig, prompt: string, call: AgentCall): Promise<string>;
|
|
6
|
+
/** End every host: what they were for is over. Resolves when none is left. (A call made after this starts one anew.) */
|
|
7
|
+
close(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The hosts of one runner's calls. `env`: variables the hosts are started
|
|
11
|
+
* with, over this process's own — a run's, whose agents are then that run's
|
|
12
|
+
* in what they find of pi, of a provider's key, and in the commands they run.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createAgentHosts(env?: Record<string, string>): AgentHosts;
|