@agentorg/cli 0.0.6 → 0.0.8
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/README.md +10 -7
- package/dist/node_modules/@agentorg/computer/LICENSE +201 -0
- package/dist/node_modules/@agentorg/computer/NOTICE +2 -0
- package/dist/node_modules/@agentorg/computer/dist/THIRD_PARTY_LICENSES.txt +1321 -0
- package/dist/node_modules/@agentorg/computer/dist/sdk.js +24909 -0
- package/dist/node_modules/@agentorg/computer/dist/types/generated/schema.d.ts +860 -0
- package/dist/node_modules/@agentorg/computer/dist/types/sdk.d.ts +242 -0
- package/dist/node_modules/@agentorg/computer/package.json +12 -0
- package/dist/node_modules/@agentorg/computer-connector/dist/src/index.d.ts +1 -0
- package/dist/node_modules/@agentorg/computer-connector/dist/src/index.js +1 -0
- package/dist/node_modules/@agentorg/computer-connector/dist/src/provider.d.ts +24 -0
- package/dist/node_modules/@agentorg/computer-connector/dist/src/provider.js +482 -0
- package/dist/node_modules/@agentorg/computer-connector/dist/src/unit.d.ts +19 -0
- package/dist/node_modules/@agentorg/computer-connector/dist/src/unit.js +149 -0
- package/dist/node_modules/@agentorg/computer-connector/package.json +33 -0
- package/dist/node_modules/@agentorg/computer-connector/skills/browser/SKILL.md +31 -0
- package/dist/node_modules/@agentorg/computer-connector/skills/computer/SKILL.md +30 -0
- package/dist/node_modules/@agentorg/computer-connector/skills/files/SKILL.md +28 -0
- package/dist/node_modules/@agentorg/computer-connector/skills/terminal/SKILL.md +39 -0
- package/dist/node_modules/@agentorg/computer-connector/skills/web-research/SKILL.md +29 -0
- package/dist/node_modules/@agentorg/hub/dist/src/router.d.ts +4 -2
- package/dist/node_modules/@agentorg/hub/dist/src/router.js +3 -1
- package/dist/node_modules/@agentorg/hub/dist/src/validate.js +16 -2
- package/dist/node_modules/@agentorg/interfaces/dist/src/agent-provider.d.ts +34 -47
- package/dist/node_modules/@agentorg/interfaces/dist/src/agent-provider.js +25 -34
- package/dist/node_modules/@agentorg/interfaces/dist/src/chat.d.ts +90 -50
- package/dist/node_modules/@agentorg/interfaces/dist/src/chat.js +69 -34
- package/dist/node_modules/@agentorg/interfaces/dist/src/connection.d.ts +36 -0
- package/dist/node_modules/@agentorg/interfaces/dist/src/connection.js +31 -0
- package/dist/node_modules/@agentorg/interfaces/dist/src/folders.d.ts +19 -0
- package/dist/node_modules/@agentorg/interfaces/dist/src/folders.js +25 -0
- package/dist/node_modules/@agentorg/interfaces/dist/src/index.d.ts +6 -4
- package/dist/node_modules/@agentorg/interfaces/dist/src/index.js +6 -4
- package/dist/node_modules/@agentorg/interfaces/dist/src/llm-session.d.ts +2 -0
- package/dist/node_modules/@agentorg/interfaces/dist/src/llm-session.js +2 -1
- package/dist/node_modules/@agentorg/interfaces/dist/src/manager.d.ts +381 -27
- package/dist/node_modules/@agentorg/interfaces/dist/src/manager.js +179 -15
- package/dist/node_modules/@agentorg/interfaces/dist/src/units.d.ts +460 -0
- package/dist/node_modules/@agentorg/interfaces/dist/src/units.js +204 -0
- package/dist/node_modules/@agentorg/looper/dist/src/index.d.ts +5 -0
- package/dist/node_modules/@agentorg/looper/dist/src/index.js +5 -0
- package/dist/node_modules/@agentorg/looper/dist/src/loop.d.ts +89 -0
- package/dist/node_modules/@agentorg/looper/dist/src/loop.js +366 -0
- package/dist/node_modules/@agentorg/looper/dist/src/looper.d.ts +81 -0
- package/dist/node_modules/@agentorg/looper/dist/src/looper.js +574 -0
- package/dist/node_modules/@agentorg/looper/dist/src/main.d.ts +34 -0
- package/dist/node_modules/@agentorg/looper/dist/src/main.js +116 -0
- package/dist/node_modules/@agentorg/looper/dist/src/record.d.ts +181 -0
- package/dist/node_modules/@agentorg/looper/dist/src/record.js +433 -0
- package/dist/node_modules/@agentorg/looper/dist/src/tools.d.ts +11 -0
- package/dist/node_modules/@agentorg/looper/dist/src/tools.js +42 -0
- package/dist/node_modules/@agentorg/looper/dist/src/types.d.ts +147 -0
- package/dist/node_modules/@agentorg/looper/dist/src/types.js +32 -0
- package/dist/node_modules/@agentorg/looper/package.json +24 -0
- package/dist/node_modules/@agentorg/manager/dist/src/activity.d.ts +18 -0
- package/dist/node_modules/@agentorg/manager/dist/src/activity.js +65 -0
- package/dist/node_modules/@agentorg/manager/dist/src/control.d.ts +134 -0
- package/dist/node_modules/@agentorg/manager/dist/src/control.js +1115 -0
- package/dist/node_modules/@agentorg/manager/dist/src/form.d.ts +40 -0
- package/dist/node_modules/@agentorg/manager/dist/src/form.js +191 -0
- package/dist/node_modules/@agentorg/manager/dist/src/manager.d.ts +4 -0
- package/dist/node_modules/@agentorg/manager/dist/src/manager.js +341 -189
- package/dist/node_modules/@agentorg/manager/dist/src/skills.d.ts +2 -0
- package/dist/node_modules/@agentorg/manager/dist/src/skills.js +7 -0
- package/dist/node_modules/@agentorg/manager/dist/src/stack.d.ts +454 -0
- package/dist/node_modules/@agentorg/manager/dist/src/stack.js +917 -0
- package/dist/node_modules/@agentorg/mcp/dist/src/connection.d.ts +2 -1
- package/dist/node_modules/@agentorg/mcp/dist/src/connection.js +8 -2
- package/dist/node_modules/@agentorg/mcp/dist/src/index.d.ts +3 -0
- package/dist/node_modules/@agentorg/mcp/dist/src/index.js +3 -0
- package/dist/node_modules/@agentorg/mcp/dist/src/kinds.d.ts +108 -0
- package/dist/node_modules/@agentorg/mcp/dist/src/kinds.js +95 -0
- package/dist/node_modules/@agentorg/mcp/dist/src/provider.d.ts +2 -2
- package/dist/node_modules/@agentorg/mcp/dist/src/provider.js +48 -18
- package/dist/node_modules/@agentorg/mcp/dist/src/public.d.ts +6 -0
- package/dist/node_modules/@agentorg/mcp/dist/src/public.js +72 -0
- package/dist/node_modules/@agentorg/sdk/dist/src/filled.d.ts +21 -0
- package/dist/node_modules/@agentorg/sdk/dist/src/filled.js +24 -0
- package/dist/node_modules/@agentorg/sdk/dist/src/index.d.ts +1 -0
- package/dist/node_modules/@agentorg/sdk/dist/src/index.js +1 -0
- package/dist/node_modules/@agentorg/shell/dist/app.css +1306 -181
- package/dist/node_modules/@agentorg/shell/dist/app.js +207 -166
- package/dist/node_modules/@agentorg/shell/package.json +6 -1
- package/dist/node_modules/assistant/dist/src/chats.d.ts +94 -0
- package/dist/node_modules/assistant/dist/src/chats.js +355 -0
- package/dist/node_modules/assistant/dist/src/engine.d.ts +26 -7
- package/dist/node_modules/assistant/dist/src/engine.js +107 -35
- package/dist/node_modules/assistant/dist/src/local-store.js +1 -1
- package/dist/node_modules/assistant/dist/src/provider.d.ts +1 -1
- package/dist/node_modules/assistant/dist/src/provider.js +47 -19
- package/dist/node_modules/assistant/dist/src/tools.d.ts +11 -28
- package/dist/node_modules/assistant/dist/src/tools.js +19 -40
- package/dist/node_modules/assistant/package.json +1 -0
- package/dist/src/api.js +4 -0
- package/dist/src/folders.d.ts +2 -0
- package/dist/src/folders.js +45 -0
- package/dist/src/gateway.d.ts +7 -1
- package/dist/src/gateway.js +81 -12
- package/dist/src/live.d.ts +17 -0
- package/dist/src/live.js +109 -0
- package/dist/src/programs.d.ts +7 -0
- package/dist/src/programs.js +29 -2
- package/dist/src/signin.d.ts +12 -3
- package/dist/src/signin.js +30 -6
- package/package.json +2 -1
- package/dist/node_modules/@agentorg/interfaces/dist/src/mcp-provider.d.ts +0 -88
- package/dist/node_modules/@agentorg/interfaces/dist/src/mcp-provider.js +0 -78
- package/dist/node_modules/assistant/dist/src/agent.d.ts +0 -62
- package/dist/node_modules/assistant/dist/src/agent.js +0 -226
- package/dist/node_modules/assistant/dist/src/history.d.ts +0 -97
- package/dist/node_modules/assistant/dist/src/history.js +0 -235
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { defineInterface, method } from "@agentorg/sdk";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
import { Id } from "./hub.js";
|
|
4
|
-
/** A way a provider makes connections, such as `url` or `command`. */
|
|
5
|
-
export const ConnectionKind = z
|
|
6
|
-
.string()
|
|
7
|
-
.regex(/^[a-z][a-z0-9-]*$/)
|
|
8
|
-
.max(64);
|
|
9
|
-
/** `connecting` before its first success, `connected`, or `down` and being tried again. */
|
|
10
|
-
export const ConnectionState = z.enum(["connecting", "connected", "down"]);
|
|
11
|
-
/** A connection's tools, for people to read; a provider cuts a long description to fit. */
|
|
12
|
-
export const ConnectionTool = z.object({
|
|
13
|
-
name: z.string().min(1).max(128),
|
|
14
|
-
description: z.string().max(512).optional(),
|
|
15
|
-
});
|
|
16
|
-
/** How a connection stands now: `detail` says why it is down, and never holds a secret. */
|
|
17
|
-
export const ConnectionStatus = z.object({
|
|
18
|
-
state: ConnectionState,
|
|
19
|
-
detail: z.string().max(2000).optional(),
|
|
20
|
-
tools: z.array(ConnectionTool),
|
|
21
|
-
});
|
|
22
|
-
/** The parameters of a connection, fitting its kind's `params` schema. */
|
|
23
|
-
export const ConnectionParams = z.record(z.string(), z.json());
|
|
24
|
-
/**
|
|
25
|
-
* What a server needs and shouldn't be seen, by name, such as a bearer token. Write-only: the values
|
|
26
|
-
* cross once, in `create`, and go only to the provider's own private file.
|
|
27
|
-
*/
|
|
28
|
-
export const ConnectionSecrets = z.record(z
|
|
29
|
-
.string()
|
|
30
|
-
.regex(/^[A-Za-z_][A-Za-z0-9_]*$/)
|
|
31
|
-
.max(256), z.string().min(1).max(16_384));
|
|
32
|
-
/** What a connection attaches to or runs, for people to read: never a secret. */
|
|
33
|
-
export const ConnectionTarget = z.string().min(1).max(4096);
|
|
34
|
-
/**
|
|
35
|
-
* `mcp.provider@1`: a program that makes connections to MCP servers. Each connection joins the
|
|
36
|
-
* Hub as its own Actor, whose id is `connection`, exporting `tools@1` as `tools`. MCP itself stays
|
|
37
|
-
* behind this interface.
|
|
38
|
-
*
|
|
39
|
-
* A secret a server needs is given once, in `create`'s `secrets`, and stays with the provider: its
|
|
40
|
-
* value is never in `params`, `target`, `detail`, or anything else read back. `status` and `remove` are refused `unknown` for a connection the
|
|
41
|
-
* provider doesn't hold.
|
|
42
|
-
*/
|
|
43
|
-
export const mcpProvider = defineInterface({
|
|
44
|
-
name: "mcp.provider",
|
|
45
|
-
major: 1,
|
|
46
|
-
kind: "rpc",
|
|
47
|
-
methods: {
|
|
48
|
-
/** The kinds it offers; `params` and `secrets` are the JSON Schemas of what each one takes. */
|
|
49
|
-
kinds: method({
|
|
50
|
-
input: z.object({}),
|
|
51
|
-
output: z.object({
|
|
52
|
-
kinds: z.array(z.object({
|
|
53
|
-
kind: ConnectionKind,
|
|
54
|
-
title: z.string().min(1).max(128),
|
|
55
|
-
params: z.record(z.string(), z.json()),
|
|
56
|
-
secrets: z.record(z.string(), z.json()),
|
|
57
|
-
})),
|
|
58
|
-
}),
|
|
59
|
-
}),
|
|
60
|
-
/**
|
|
61
|
-
* Make a connection and its Actor, named `name`. It returns once the Actor has joined the Hub,
|
|
62
|
-
* whether or not the server has answered yet; `status` says how it stands. Refused `invalid`
|
|
63
|
-
* when `kind` isn't offered or `params` or `secrets` don't fit it.
|
|
64
|
-
*/
|
|
65
|
-
create: method({
|
|
66
|
-
input: z.object({
|
|
67
|
-
kind: ConnectionKind,
|
|
68
|
-
params: ConnectionParams,
|
|
69
|
-
secrets: ConnectionSecrets.optional(),
|
|
70
|
-
name: z.string().min(1).max(128),
|
|
71
|
-
}),
|
|
72
|
-
output: z.object({ connection: Id, target: ConnectionTarget }),
|
|
73
|
-
}),
|
|
74
|
-
status: method({ input: z.object({ connection: Id }), output: ConnectionStatus }),
|
|
75
|
-
/** End the connection and close its Actor, which removes what was bound to it. */
|
|
76
|
-
remove: method({ input: z.object({ connection: Id }), output: z.object({}) }),
|
|
77
|
-
},
|
|
78
|
-
});
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The agent loop: a turn sends the conversation and the tools to the model, runs each function the
|
|
3
|
-
* model calls through its slot, and sends the results back, until the model answers without
|
|
4
|
-
* calling anything, or the turn has made its 20 calls.
|
|
5
|
-
*/
|
|
6
|
-
import type { ChatMessage, llmSession } from "@agentorg/interfaces";
|
|
7
|
-
import { type Log, type Slot } from "@agentorg/sdk";
|
|
8
|
-
import type { z } from "zod";
|
|
9
|
-
import { type History } from "./history.js";
|
|
10
|
-
import { type Tools } from "./tools.js";
|
|
11
|
-
export declare const MAX_CALLS = 20;
|
|
12
|
-
type Status = {
|
|
13
|
-
state: "idle" | "thinking" | "tool";
|
|
14
|
-
} | {
|
|
15
|
-
state: "error";
|
|
16
|
-
detail: {
|
|
17
|
-
code: string;
|
|
18
|
-
message: string;
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
export interface Chat {
|
|
22
|
-
message(message: z.input<typeof ChatMessage>): void;
|
|
23
|
-
delta(id: string, text: string): void;
|
|
24
|
-
status(status: Status): void;
|
|
25
|
-
}
|
|
26
|
-
export declare function createAgent(options: {
|
|
27
|
-
/** Read for each model request: what it's told and what it can call follow what it's bound to. */
|
|
28
|
-
request(): Promise<{
|
|
29
|
-
instructions: string;
|
|
30
|
-
tools: Tools;
|
|
31
|
-
}>;
|
|
32
|
-
model: Slot<typeof llmSession>;
|
|
33
|
-
/** The model chosen for this agent, read for each request; none means the provider's default. */
|
|
34
|
-
chosenModel(): Promise<string | undefined>;
|
|
35
|
-
history: History;
|
|
36
|
-
chat: Chat;
|
|
37
|
-
log: Log;
|
|
38
|
-
}): {
|
|
39
|
-
/** Start a turn with the person's message; its progress arrives as events. */
|
|
40
|
-
send(text: string): Promise<{
|
|
41
|
-
messageId: string;
|
|
42
|
-
}>;
|
|
43
|
-
/**
|
|
44
|
-
* Run the last turn again, without recording the person's message again. What followed it goes
|
|
45
|
-
* from the record, so the new reply takes its place; what its tools did stays done.
|
|
46
|
-
*/
|
|
47
|
-
retry(): Promise<{
|
|
48
|
-
messageId: string;
|
|
49
|
-
}>;
|
|
50
|
-
/** The status now. */
|
|
51
|
-
state: () => Status;
|
|
52
|
-
/**
|
|
53
|
-
* Stop the turn now: its model request is cancelled, no further call starts, and a new turn
|
|
54
|
-
* may begin at once. A call already running finishes, and its result is kept.
|
|
55
|
-
*/
|
|
56
|
-
cancel(): Promise<void>;
|
|
57
|
-
/** Settles when the latest turn has written all it will. */
|
|
58
|
-
settled: () => Promise<void>;
|
|
59
|
-
/** Streamed text of the model request running now, for the reply it becomes. */
|
|
60
|
-
delta(requestId: string, text: string): void;
|
|
61
|
-
};
|
|
62
|
-
export {};
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { Refused } from "@agentorg/sdk";
|
|
2
|
-
import { argumentsOf, head, messageOf } from "./history.js";
|
|
3
|
-
import { capped, codeOf, runTool } from "./tools.js";
|
|
4
|
-
export const MAX_CALLS = 20;
|
|
5
|
-
export function createAgent(options) {
|
|
6
|
-
const { model, history, chat, log } = options;
|
|
7
|
-
let turn;
|
|
8
|
-
/** A cancelled turn whose running call hasn't settled: the next turn waits for its records. */
|
|
9
|
-
let draining;
|
|
10
|
-
/** Settles when the latest turn's loop has ended. */
|
|
11
|
-
let ending;
|
|
12
|
-
/** The latest status, for a view that asks. */
|
|
13
|
-
let latest = { state: "idle" };
|
|
14
|
-
const say = (next) => {
|
|
15
|
-
latest = next;
|
|
16
|
-
chat.status(next);
|
|
17
|
-
};
|
|
18
|
-
/** A turn's status, while it is still the turn: a cancelled one has already said idle. */
|
|
19
|
-
const status = (current, next) => {
|
|
20
|
-
if (turn === current)
|
|
21
|
-
say(next);
|
|
22
|
-
};
|
|
23
|
-
/** Run a turn on the record as it stands; its progress arrives as events. */
|
|
24
|
-
const begin = (current) => {
|
|
25
|
-
current.ended = run(current)
|
|
26
|
-
.then(() => status(current, { state: "idle" }), (error) => {
|
|
27
|
-
if (current.cancelled)
|
|
28
|
-
return;
|
|
29
|
-
log.error("A turn failed.", { error: String(error) });
|
|
30
|
-
status(current, { state: "error", detail: codeOf(error) });
|
|
31
|
-
})
|
|
32
|
-
.finally(() => {
|
|
33
|
-
if (turn === current)
|
|
34
|
-
turn = undefined;
|
|
35
|
-
})
|
|
36
|
-
.catch((error) => log.error("A turn's status failed.", { error: String(error) }));
|
|
37
|
-
ending = current.ended;
|
|
38
|
-
};
|
|
39
|
-
const show = (entry) => chat.message(messageOf(entry));
|
|
40
|
-
/** A call starting: shown as running, under the id its record will have when it ends. */
|
|
41
|
-
const running = (call, id) => chat.message({
|
|
42
|
-
id,
|
|
43
|
-
role: "tool",
|
|
44
|
-
text: `${call.name}: running`,
|
|
45
|
-
at: new Date().toISOString(),
|
|
46
|
-
tool: { name: head(call.name, 128), state: "running", input: argumentsOf(call.arguments) },
|
|
47
|
-
});
|
|
48
|
-
/**
|
|
49
|
-
* Record a function call with its output, keeping every call paired, as the API needs. Its end
|
|
50
|
-
* is shown even if the record can't be written, so no call is left shown as running.
|
|
51
|
-
*/
|
|
52
|
-
const answered = async (call, result, id) => {
|
|
53
|
-
const entry = {
|
|
54
|
-
role: "tool",
|
|
55
|
-
text: `${call.name}: ${result.ok ? "done" : result.code}`,
|
|
56
|
-
state: result.ok ? "done" : "refused",
|
|
57
|
-
items: [
|
|
58
|
-
call,
|
|
59
|
-
{
|
|
60
|
-
type: "function_call_output",
|
|
61
|
-
callId: call.callId,
|
|
62
|
-
output: capped(result.output),
|
|
63
|
-
},
|
|
64
|
-
],
|
|
65
|
-
};
|
|
66
|
-
try {
|
|
67
|
-
show(await history.append(entry, id));
|
|
68
|
-
}
|
|
69
|
-
catch (error) {
|
|
70
|
-
chat.message(messageOf({
|
|
71
|
-
...entry,
|
|
72
|
-
id,
|
|
73
|
-
at: new Date().toISOString(),
|
|
74
|
-
items: [{ ...call, arguments: argumentsOf(call.arguments) }, ...entry.items.slice(1)],
|
|
75
|
-
}));
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
/**
|
|
80
|
-
* Note calls that won't run, in one message however many there are. They never ran, so the
|
|
81
|
-
* record leaves them out: what the model is sent next holds only calls with their outputs.
|
|
82
|
-
*/
|
|
83
|
-
const skipped = async (calls, code) => {
|
|
84
|
-
if (calls.length === 0)
|
|
85
|
-
return;
|
|
86
|
-
const text = calls.length === 1 ? `${calls[0]?.name}: ${code}` : `${calls.length} calls: ${code}`;
|
|
87
|
-
show(await history.append({ role: "tool", text, items: [] }));
|
|
88
|
-
};
|
|
89
|
-
async function run(current) {
|
|
90
|
-
let calls = 0;
|
|
91
|
-
for (;;) {
|
|
92
|
-
status(current, { state: "thinking" });
|
|
93
|
-
const { instructions, tools } = await options.request();
|
|
94
|
-
const input = await history.input();
|
|
95
|
-
const chosen = await options.chosenModel();
|
|
96
|
-
if (current.cancelled)
|
|
97
|
-
return;
|
|
98
|
-
const requestId = crypto.randomUUID();
|
|
99
|
-
const replyId = crypto.randomUUID();
|
|
100
|
-
current.requestId = requestId;
|
|
101
|
-
current.replyId = replyId;
|
|
102
|
-
let output;
|
|
103
|
-
try {
|
|
104
|
-
({ output } = await model.call.respond({
|
|
105
|
-
requestId,
|
|
106
|
-
...(chosen ? { model: chosen } : {}),
|
|
107
|
-
instructions,
|
|
108
|
-
input,
|
|
109
|
-
tools: [...tools.values()].map((found) => found.tool),
|
|
110
|
-
}));
|
|
111
|
-
}
|
|
112
|
-
finally {
|
|
113
|
-
current.requestId = undefined;
|
|
114
|
-
}
|
|
115
|
-
if (current.cancelled)
|
|
116
|
-
return;
|
|
117
|
-
const texts = output.flatMap((item) => (item.type === "message" ? [item.content] : []));
|
|
118
|
-
const called = output.filter((item) => item.type === "function_call");
|
|
119
|
-
if (texts.length > 0 || called.length === 0)
|
|
120
|
-
show(await history.append({
|
|
121
|
-
role: "assistant",
|
|
122
|
-
items: texts.map((content) => ({ type: "message", role: "assistant", content })),
|
|
123
|
-
}, replyId));
|
|
124
|
-
if (called.length === 0)
|
|
125
|
-
return;
|
|
126
|
-
for (const [i, call] of called.entries()) {
|
|
127
|
-
if (current.cancelled || calls >= MAX_CALLS) {
|
|
128
|
-
const rest = called.slice(i);
|
|
129
|
-
await skipped(rest, current.cancelled ? "cancelled" : "limit");
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
calls++;
|
|
133
|
-
status(current, { state: "tool" });
|
|
134
|
-
// A call that has started runs to its end, and its real result is kept: it may have
|
|
135
|
-
// taken effect.
|
|
136
|
-
const id = crypto.randomUUID();
|
|
137
|
-
running(call, id);
|
|
138
|
-
const result = await runTool(tools, call.name, call.arguments);
|
|
139
|
-
await answered(call, result, id);
|
|
140
|
-
}
|
|
141
|
-
if (current.cancelled)
|
|
142
|
-
return;
|
|
143
|
-
if (calls >= MAX_CALLS)
|
|
144
|
-
throw new Refused("limit", `This turn stopped after ${MAX_CALLS} tool calls.`);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return {
|
|
148
|
-
/** Start a turn with the person's message; its progress arrives as events. */
|
|
149
|
-
async send(text) {
|
|
150
|
-
if (turn)
|
|
151
|
-
throw new Refused("busy", "A turn is running. Wait for it, or cancel it.");
|
|
152
|
-
const current = { cancelled: false };
|
|
153
|
-
turn = current;
|
|
154
|
-
try {
|
|
155
|
-
// A cancelled turn's last call lands in the record before this message does.
|
|
156
|
-
await draining;
|
|
157
|
-
draining = undefined;
|
|
158
|
-
const entry = await history.append({
|
|
159
|
-
role: "user",
|
|
160
|
-
items: [{ type: "message", role: "user", content: text }],
|
|
161
|
-
});
|
|
162
|
-
show(entry);
|
|
163
|
-
begin(current);
|
|
164
|
-
return { messageId: entry.id };
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
if (turn === current)
|
|
168
|
-
turn = undefined;
|
|
169
|
-
throw error;
|
|
170
|
-
}
|
|
171
|
-
},
|
|
172
|
-
/**
|
|
173
|
-
* Run the last turn again, without recording the person's message again. What followed it goes
|
|
174
|
-
* from the record, so the new reply takes its place; what its tools did stays done.
|
|
175
|
-
*/
|
|
176
|
-
async retry() {
|
|
177
|
-
if (turn)
|
|
178
|
-
throw new Refused("busy", "A turn is running. Wait for it, or cancel it.");
|
|
179
|
-
const current = { cancelled: false };
|
|
180
|
-
turn = current;
|
|
181
|
-
try {
|
|
182
|
-
await draining;
|
|
183
|
-
draining = undefined;
|
|
184
|
-
const last = await history.lastUser();
|
|
185
|
-
if (!last)
|
|
186
|
-
throw new Refused("nothing_to_retry", "No message has been sent yet.");
|
|
187
|
-
await history.dropAfter(last.id);
|
|
188
|
-
begin(current);
|
|
189
|
-
return { messageId: last.id };
|
|
190
|
-
}
|
|
191
|
-
catch (error) {
|
|
192
|
-
if (turn === current)
|
|
193
|
-
turn = undefined;
|
|
194
|
-
throw error;
|
|
195
|
-
}
|
|
196
|
-
},
|
|
197
|
-
/** The status now. */
|
|
198
|
-
state: () => latest,
|
|
199
|
-
/**
|
|
200
|
-
* Stop the turn now: its model request is cancelled, no further call starts, and a new turn
|
|
201
|
-
* may begin at once. A call already running finishes, and its result is kept.
|
|
202
|
-
*/
|
|
203
|
-
async cancel() {
|
|
204
|
-
const current = turn;
|
|
205
|
-
if (!current)
|
|
206
|
-
return;
|
|
207
|
-
current.cancelled = true;
|
|
208
|
-
turn = undefined;
|
|
209
|
-
// In a model call, nothing more is recorded; in a tool call, its result still is.
|
|
210
|
-
if (!current.requestId)
|
|
211
|
-
draining = current.ended;
|
|
212
|
-
say({ state: "idle" });
|
|
213
|
-
if (current.requestId && model.bound)
|
|
214
|
-
await model.call.cancel({ requestId: current.requestId }).catch(() => { });
|
|
215
|
-
},
|
|
216
|
-
/** Settles when the latest turn has written all it will. */
|
|
217
|
-
settled: async () => {
|
|
218
|
-
await ending;
|
|
219
|
-
},
|
|
220
|
-
/** Streamed text of the model request running now, for the reply it becomes. */
|
|
221
|
-
delta(requestId, text) {
|
|
222
|
-
if (turn?.requestId === requestId && turn.replyId)
|
|
223
|
-
chat.delta(turn.replyId, text);
|
|
224
|
-
},
|
|
225
|
-
};
|
|
226
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The assistant's record of its chat, kept in its `store` slot: one key per message, holding the
|
|
3
|
-
* Responses items it stands for, and for a tool message the line the person sees. It is read once,
|
|
4
|
-
* then kept in memory, and the oldest messages are deleted past a retention cap.
|
|
5
|
-
*/
|
|
6
|
-
import { type ChatMessage, Item, type kv } from "@agentorg/interfaces";
|
|
7
|
-
import type { Log, Slot } from "@agentorg/sdk";
|
|
8
|
-
import { z } from "zod";
|
|
9
|
-
export type ResponseItem = z.output<typeof Item>;
|
|
10
|
-
declare const Entry: z.ZodObject<{
|
|
11
|
-
id: z.ZodString;
|
|
12
|
-
role: z.ZodEnum<{
|
|
13
|
-
assistant: "assistant";
|
|
14
|
-
tool: "tool";
|
|
15
|
-
user: "user";
|
|
16
|
-
}>;
|
|
17
|
-
text: z.ZodOptional<z.ZodString>;
|
|
18
|
-
at: z.ZodString;
|
|
19
|
-
items: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
20
|
-
type: z.ZodLiteral<"message">;
|
|
21
|
-
role: z.ZodEnum<{
|
|
22
|
-
assistant: "assistant";
|
|
23
|
-
user: "user";
|
|
24
|
-
}>;
|
|
25
|
-
content: z.ZodString;
|
|
26
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
27
|
-
type: z.ZodLiteral<"function_call">;
|
|
28
|
-
callId: z.ZodString;
|
|
29
|
-
name: z.ZodString;
|
|
30
|
-
arguments: z.ZodString;
|
|
31
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
32
|
-
type: z.ZodLiteral<"function_call_output">;
|
|
33
|
-
callId: z.ZodString;
|
|
34
|
-
output: z.ZodString;
|
|
35
|
-
}, z.core.$strip>], "type">>;
|
|
36
|
-
state: z.ZodOptional<z.ZodEnum<{
|
|
37
|
-
done: "done";
|
|
38
|
-
refused: "refused";
|
|
39
|
-
}>>;
|
|
40
|
-
}, z.core.$strip>;
|
|
41
|
-
export type Entry = z.output<typeof Entry>;
|
|
42
|
-
/** What the model is sent of the past: the newest exchanges up to this many bytes of JSON. */
|
|
43
|
-
export declare const INPUT_BUDGET = 400000;
|
|
44
|
-
/** At most this many messages are kept, in at most this many bytes, oldest deleted first. */
|
|
45
|
-
export declare const KEEP = 2000;
|
|
46
|
-
export declare const KEEP_BYTES: number;
|
|
47
|
-
/** The first `length` UTF-16 units of `text`, never ending inside a surrogate pair. */
|
|
48
|
-
export declare function head(text: string, length: number): string;
|
|
49
|
-
/** `text`, cut so its JSON fits `max` bytes, saying so when it is cut. */
|
|
50
|
-
export declare function fitted(text: string, max: number): string;
|
|
51
|
-
/** What the person sees of a message. */
|
|
52
|
-
export declare const textOf: (entry: Entry) => string;
|
|
53
|
-
/** A call's arguments as they are kept and shown: past their cap, a note in their place. */
|
|
54
|
-
export declare const argumentsOf: (args: string) => string;
|
|
55
|
-
/**
|
|
56
|
-
* A message as the chat shows it. A tool message whose call ran carries `tool`: the call's name,
|
|
57
|
-
* how it ended, its arguments and its output.
|
|
58
|
-
*/
|
|
59
|
-
export declare function messageOf(entry: Entry): z.input<typeof ChatMessage>;
|
|
60
|
-
export declare function createHistory(store: Slot<typeof kv>["call"], log: Log, limits?: {
|
|
61
|
-
keep: number;
|
|
62
|
-
keepBytes: number;
|
|
63
|
-
}): {
|
|
64
|
-
/**
|
|
65
|
-
* Keep a message, under `id` or its own number, and delete the oldest past the cap. A tool
|
|
66
|
-
* message the store refuses is still kept in memory, since its call has run.
|
|
67
|
-
*/
|
|
68
|
-
append(entry: Omit<Entry, "id" | "at">, id?: string): Promise<Entry>;
|
|
69
|
-
/** Forget everything after the message `id`: a retry takes the place of what followed it. */
|
|
70
|
-
dropAfter(id: string): Promise<void>;
|
|
71
|
-
/** The person's last message, if any. */
|
|
72
|
-
lastUser(): Promise<Entry | undefined>;
|
|
73
|
-
/** Messages before `before`, oldest first, as many as fit `limit` and one frame. */
|
|
74
|
-
page(before: string | undefined, limit: number): Promise<{
|
|
75
|
-
messages: {
|
|
76
|
-
id: string;
|
|
77
|
-
role: "assistant" | "tool" | "user";
|
|
78
|
-
text: string;
|
|
79
|
-
at: string;
|
|
80
|
-
tool?: {
|
|
81
|
-
name: string;
|
|
82
|
-
state: "done" | "refused" | "running";
|
|
83
|
-
input?: string | undefined;
|
|
84
|
-
output?: string | undefined;
|
|
85
|
-
} | undefined;
|
|
86
|
-
}[];
|
|
87
|
-
more: boolean;
|
|
88
|
-
}>;
|
|
89
|
-
/**
|
|
90
|
-
* The conversation to send: the newest whole exchanges (each from a user message on) that fit
|
|
91
|
-
* the budget, and always the latest one. A latest one that alone is over the budget has its
|
|
92
|
-
* oldest function arguments and outputs left out, each call still paired with an output.
|
|
93
|
-
*/
|
|
94
|
-
input(): Promise<ResponseItem[]>;
|
|
95
|
-
};
|
|
96
|
-
export type History = ReturnType<typeof createHistory>;
|
|
97
|
-
export {};
|