@runappsai/agent 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +162 -2
- package/dist/agent-core.umd.js +266 -0
- package/dist/agent.d.ts +222 -0
- package/dist/agent.js +1331 -0
- package/dist/agent.umd.js +732 -0
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +4 -0
- package/dist/chat-theme.d.ts +24 -0
- package/dist/chat-theme.js +101 -0
- package/dist/chat.d.ts +271 -0
- package/dist/chat.js +4099 -0
- package/dist/compaction.d.ts +48 -0
- package/dist/compaction.js +208 -0
- package/dist/completion.d.ts +29 -0
- package/dist/completion.js +88 -0
- package/dist/context.d.ts +44 -0
- package/dist/context.js +177 -0
- package/dist/debug-panel.d.ts +40 -0
- package/dist/debug-panel.js +262 -0
- package/dist/dialogs.d.ts +7 -0
- package/dist/dialogs.js +231 -0
- package/dist/directory-fs.d.ts +5 -0
- package/dist/directory-fs.js +118 -0
- package/dist/dom.d.ts +83 -0
- package/dist/dom.js +163 -0
- package/dist/edit.d.ts +22 -0
- package/dist/edit.js +133 -0
- package/dist/file-guard.d.ts +46 -0
- package/dist/file-guard.js +153 -0
- package/dist/file-tools.d.ts +16 -0
- package/dist/file-tools.js +339 -0
- package/dist/files-panel.d.ts +60 -0
- package/dist/files-panel.js +613 -0
- package/dist/index.cjs +4848 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +44 -0
- package/dist/json-schema.d.ts +3 -0
- package/dist/json-schema.js +141 -0
- package/dist/llm.d.ts +95 -0
- package/dist/llm.js +5 -0
- package/dist/markdown.d.ts +4 -0
- package/dist/markdown.js +265 -0
- package/dist/mcp.d.ts +82 -0
- package/dist/mcp.js +325 -0
- package/dist/node.cjs +402 -0
- package/dist/node.d.ts +78 -0
- package/dist/node.js +432 -0
- package/dist/pdf.d.ts +52 -0
- package/dist/pdf.js +77 -0
- package/dist/provider.d.ts +135 -0
- package/dist/provider.js +181 -0
- package/dist/sandbox.d.ts +33 -0
- package/dist/sandbox.js +219 -0
- package/dist/select.d.ts +30 -0
- package/dist/select.js +185 -0
- package/dist/skill-zips.d.ts +28 -0
- package/dist/skill-zips.js +123 -0
- package/dist/skills.d.ts +20 -0
- package/dist/skills.js +111 -0
- package/dist/templates.d.ts +24 -0
- package/dist/templates.js +136 -0
- package/dist/tool.d.ts +28 -0
- package/dist/tool.js +102 -0
- package/dist/types.d.ts +571 -0
- package/dist/types.js +7 -0
- package/dist/ui.cjs +6247 -0
- package/dist/ui.d.ts +6 -0
- package/dist/ui.js +11 -0
- package/dist/vlist.d.ts +80 -0
- package/dist/vlist.js +277 -0
- package/dist/workspace.d.ts +343 -0
- package/dist/workspace.js +1203 -0
- package/dist/zip.d.ts +26 -0
- package/dist/zip.js +204 -0
- package/docs/agent.md +228 -0
- package/docs/chat-ui.md +307 -0
- package/docs/node.md +135 -0
- package/docs/providers.md +176 -0
- package/docs/reference.md +143 -0
- package/docs/skills.md +131 -0
- package/docs/tools.md +254 -0
- package/docs/workspace.md +198 -0
- package/package.json +59 -4
package/dist/tool.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// defineTool: the one way to declare a tool for the agent; plus the
|
|
2
|
+
// helpers the loop uses to turn a tool into the chat schema and a tool's
|
|
3
|
+
// return value into what the model reads.
|
|
4
|
+
const NAME_RE = /^[A-Za-z0-9_-]{1,64}$/;
|
|
5
|
+
export const DEFAULT_MAX_RESULT_CHARS = 32_000;
|
|
6
|
+
/**
|
|
7
|
+
* Declares a tool. Checks the name and fills in an empty-object schema
|
|
8
|
+
* when the tool takes no arguments. The JSON Schema in `parameters`
|
|
9
|
+
* should close objects with `additionalProperties: false` and list
|
|
10
|
+
* `required` fields: the model's arguments are checked against it before
|
|
11
|
+
* `execute` runs, and a mismatch is reported back to the model.
|
|
12
|
+
*/
|
|
13
|
+
export function defineTool(def) {
|
|
14
|
+
if (!NAME_RE.test(def.name)) {
|
|
15
|
+
throw new Error(`tool name "${def.name}" must match ${NAME_RE}`);
|
|
16
|
+
}
|
|
17
|
+
if (!def.description || !def.description.trim()) {
|
|
18
|
+
throw new Error(`tool "${def.name}" needs a description`);
|
|
19
|
+
}
|
|
20
|
+
const parameters = def.parameters ?? { type: "object", properties: {}, additionalProperties: false };
|
|
21
|
+
return { ...def, parameters };
|
|
22
|
+
}
|
|
23
|
+
export function toChatTool(tool) {
|
|
24
|
+
return {
|
|
25
|
+
type: "function",
|
|
26
|
+
function: {
|
|
27
|
+
name: tool.name,
|
|
28
|
+
description: tool.description,
|
|
29
|
+
parameters: tool.parameters,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Turns whatever `execute` returned into the tool message. */
|
|
34
|
+
export function normalizeResult(value, maxChars = DEFAULT_MAX_RESULT_CHARS) {
|
|
35
|
+
let content;
|
|
36
|
+
let details;
|
|
37
|
+
if (value === undefined || value === null) {
|
|
38
|
+
content = "OK";
|
|
39
|
+
}
|
|
40
|
+
else if (typeof value === "string") {
|
|
41
|
+
content = value.length ? value : "OK";
|
|
42
|
+
}
|
|
43
|
+
else if (Array.isArray(value) && value.every(isContentPart)) {
|
|
44
|
+
content = value;
|
|
45
|
+
}
|
|
46
|
+
else if (isWrapped(value)) {
|
|
47
|
+
content = value.content;
|
|
48
|
+
details = value.details;
|
|
49
|
+
if (typeof content === "string" && !content.length)
|
|
50
|
+
content = "OK";
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
content = JSON.stringify(value);
|
|
54
|
+
}
|
|
55
|
+
let truncated = false;
|
|
56
|
+
if (typeof content === "string") {
|
|
57
|
+
const t = truncate(content, maxChars);
|
|
58
|
+
truncated = t.truncated;
|
|
59
|
+
content = t.text;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
content = content.map((p) => {
|
|
63
|
+
if (p.type !== "text")
|
|
64
|
+
return p;
|
|
65
|
+
const t = truncate(p.text, maxChars);
|
|
66
|
+
if (t.truncated)
|
|
67
|
+
truncated = true;
|
|
68
|
+
return { type: "text", text: t.text };
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return { content, text: typeof content === "string" ? content : textOf(content), details, truncated };
|
|
72
|
+
}
|
|
73
|
+
function isContentPart(p) {
|
|
74
|
+
return !!p && typeof p === "object" && typeof p.type === "string" &&
|
|
75
|
+
["text", "image_url", "video_url", "audio_url"].includes(p.type);
|
|
76
|
+
}
|
|
77
|
+
function isWrapped(v) {
|
|
78
|
+
if (!v || typeof v !== "object" || Array.isArray(v))
|
|
79
|
+
return false;
|
|
80
|
+
const c = v.content;
|
|
81
|
+
const keys = Object.keys(v);
|
|
82
|
+
return (typeof c === "string" || (Array.isArray(c) && c.every(isContentPart))) &&
|
|
83
|
+
keys.every((k) => k === "content" || k === "details");
|
|
84
|
+
}
|
|
85
|
+
export function truncate(text, maxChars) {
|
|
86
|
+
if (text.length <= maxChars)
|
|
87
|
+
return { text, truncated: false };
|
|
88
|
+
const kept = text.slice(0, Math.max(0, maxChars - 120));
|
|
89
|
+
const more = text.length - kept.length;
|
|
90
|
+
return {
|
|
91
|
+
text: `${kept}\n\n[... ${more} more characters not shown. Ask for a narrower range or a smaller result.]`,
|
|
92
|
+
truncated: true,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** The text of a content-parts array (images noted, not included). */
|
|
96
|
+
export function textOf(content) {
|
|
97
|
+
if (!content)
|
|
98
|
+
return "";
|
|
99
|
+
if (typeof content === "string")
|
|
100
|
+
return content;
|
|
101
|
+
return content.map((p) => (p.type === "text" ? p.text : `[${p.type}]`)).join("\n");
|
|
102
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
import type { ChatCompletionParams, ChatMessage, ContentPart } from "./llm.js";
|
|
2
|
+
import type { Usage } from "./llm.js";
|
|
3
|
+
import type { Agent } from "./agent.js";
|
|
4
|
+
import type { CompactionSettings, CutPlan } from "./compaction.js";
|
|
5
|
+
import type { Workspace } from "./workspace.js";
|
|
6
|
+
import type { TextEdit } from "./edit.js";
|
|
7
|
+
/** A JSON Schema object (draft 7 / 2020-12 subset — see json-schema.ts). */
|
|
8
|
+
export type JSONSchema = Record<string, unknown>;
|
|
9
|
+
/** What a tool's `execute` receives besides its arguments. */
|
|
10
|
+
export interface ToolContext {
|
|
11
|
+
/** Aborted when the user stops the run; long tools should honour it. */
|
|
12
|
+
signal: AbortSignal;
|
|
13
|
+
/** The agent's dialogs (confirm / select / input / notify). */
|
|
14
|
+
ui: AgentUI;
|
|
15
|
+
/** The agent's workspace, when it has one. */
|
|
16
|
+
workspace: Workspace | null;
|
|
17
|
+
agent: Agent;
|
|
18
|
+
/** The model's id for this call (`tool_call_id`). */
|
|
19
|
+
callId: string;
|
|
20
|
+
/** Report progress; the app sees it as a `tool_update` event. */
|
|
21
|
+
update(content: string): void;
|
|
22
|
+
/**
|
|
23
|
+
* Say whether this call brought anything new, for the guard against
|
|
24
|
+
* repeated calls (by default a result counts as new unless the run has
|
|
25
|
+
* already had the same one). `read_file` says false for lines already read.
|
|
26
|
+
*/
|
|
27
|
+
progress(made: boolean): void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* What `execute` may return. A string goes to the model as is; content
|
|
31
|
+
* parts let a tool answer with an image; any other value is serialised as
|
|
32
|
+
* JSON. `{ content, details }` keeps `details` for the app (events) and
|
|
33
|
+
* sends only `content` to the model.
|
|
34
|
+
*/
|
|
35
|
+
export type ToolResultValue = string | ContentPart[] | {
|
|
36
|
+
content: string | ContentPart[];
|
|
37
|
+
details?: unknown;
|
|
38
|
+
} | number | boolean | object | null | undefined | void;
|
|
39
|
+
export interface ToolDefinition<A = Record<string, unknown>> {
|
|
40
|
+
/** `^[A-Za-z0-9_-]{1,64}$`, what the model calls. */
|
|
41
|
+
name: string;
|
|
42
|
+
/** What it does and when to use it — the model reads this. */
|
|
43
|
+
description: string;
|
|
44
|
+
/** JSON Schema of the arguments object. Default: no arguments. */
|
|
45
|
+
parameters?: JSONSchema;
|
|
46
|
+
/**
|
|
47
|
+
* Extra guidance placed in the system prompt (one or more sentences
|
|
48
|
+
* that name the tool), for rules too long for `description`.
|
|
49
|
+
*/
|
|
50
|
+
guidelines?: string | string[];
|
|
51
|
+
/**
|
|
52
|
+
* Asked before running under permissions `"ask"`: return the question
|
|
53
|
+
* to put to the user (a short sentence naming what will happen), or
|
|
54
|
+
* `false` to run without asking this time. Without `confirm`, a tool
|
|
55
|
+
* asks when it is `destructive`.
|
|
56
|
+
*/
|
|
57
|
+
confirm?: (args: A) => ToolConfirm | Promise<ToolConfirm>;
|
|
58
|
+
/**
|
|
59
|
+
* Checks a call before the user is asked or anything runs (after the
|
|
60
|
+
* arguments matched the schema): return or throw the reason to refuse it,
|
|
61
|
+
* which the model gets as the call's error. Use it for what the schema
|
|
62
|
+
* cannot say (the text to replace is not there, the target changed), so
|
|
63
|
+
* the user is never asked to approve a call that would fail.
|
|
64
|
+
*/
|
|
65
|
+
validate?: (args: A) => string | void | Promise<string | void>;
|
|
66
|
+
/** Changes or removes something the user may not want gone. */
|
|
67
|
+
destructive?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* How the chat UI (`renderChat`) shows a call of this tool; the model
|
|
70
|
+
* never sees it. Every function is the app's own: an exception in one
|
|
71
|
+
* falls back to the default display.
|
|
72
|
+
*/
|
|
73
|
+
display?: ToolDisplay<A>;
|
|
74
|
+
/**
|
|
75
|
+
* What the model is told when the run is stopped while this call runs
|
|
76
|
+
* (default: that it was stopped). The call's own work may go on; this is
|
|
77
|
+
* where a tool says how far it got, so a later turn does not redo it.
|
|
78
|
+
*/
|
|
79
|
+
whenStopped?: (args: A) => ToolResultValue;
|
|
80
|
+
/**
|
|
81
|
+
* Only looks (reads, lists, searches): running it changes nothing. A
|
|
82
|
+
* successful call of a tool that is not read-only counts as progress for
|
|
83
|
+
* the guard against repeated calls; a read-only one only when its result is new.
|
|
84
|
+
*/
|
|
85
|
+
readOnly?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Ends the run: when a reply has text and calls only tools like this one,
|
|
88
|
+
* the agent runs them and stops instead of asking the model to go on — no
|
|
89
|
+
* further call for a tool whose result the model does not need (the chat's
|
|
90
|
+
* next-message suggestion). Called alongside other tools, or in a reply
|
|
91
|
+
* with no text, the run goes on as usual.
|
|
92
|
+
*/
|
|
93
|
+
endsRun?: boolean;
|
|
94
|
+
/** Longest result sent to the model, in characters (default 32 000). */
|
|
95
|
+
maxResultChars?: number;
|
|
96
|
+
/**
|
|
97
|
+
* Only useful to a model that sees pictures (a screenshot tool, say): it
|
|
98
|
+
* is offered only while the model in use is known to take images
|
|
99
|
+
* (`agent.accepts("image")`).
|
|
100
|
+
*/
|
|
101
|
+
vision?: boolean;
|
|
102
|
+
execute(args: A, ctx: ToolContext): Promise<ToolResultValue> | ToolResultValue;
|
|
103
|
+
}
|
|
104
|
+
/** A confirmation question: a title, or a title and a message (the details: arguments, what will happen); `false` runs without asking. */
|
|
105
|
+
/**
|
|
106
|
+
* What `confirm` asks: the question, or `{ title, message }` with details
|
|
107
|
+
* under it; `always` adds a third answer ("Always allow") that allows this
|
|
108
|
+
* call and runs `onChoose` (where the tool stops asking, e.g. turns on its
|
|
109
|
+
* app's auto-apply setting).
|
|
110
|
+
*/
|
|
111
|
+
export type ToolConfirm = string | false | {
|
|
112
|
+
title: string;
|
|
113
|
+
message?: string;
|
|
114
|
+
always?: {
|
|
115
|
+
label: string;
|
|
116
|
+
onChoose: () => void;
|
|
117
|
+
};
|
|
118
|
+
/** The confirm button's text ("Apply"); the UI's own word when absent. */
|
|
119
|
+
ok?: string;
|
|
120
|
+
/**
|
|
121
|
+
* Content of the tool's own in the question (a diff the user picks
|
|
122
|
+
* changes in): the chat's card shows it under the message. Whatever
|
|
123
|
+
* the user sets in it, the tool reads when it runs. UIs without a
|
|
124
|
+
* document (a terminal) leave it out.
|
|
125
|
+
*/
|
|
126
|
+
body?: () => HTMLElement;
|
|
127
|
+
};
|
|
128
|
+
/** A tool as the agent holds it (`defineTool` normalises a definition). */
|
|
129
|
+
export interface Tool<A = any> extends ToolDefinition<A> {
|
|
130
|
+
parameters: JSONSchema;
|
|
131
|
+
}
|
|
132
|
+
export type ToolStatus = "ok" | "error" | "declined" | "blocked" | "skipped";
|
|
133
|
+
/** A tool's look in the chat UI (see `ToolDefinition.display`). */
|
|
134
|
+
export interface ToolDisplay<A = any> {
|
|
135
|
+
/** No card in the conversation or its export: a call the user need not see. */
|
|
136
|
+
hidden?: boolean;
|
|
137
|
+
/** The call in words, e.g. "Run 3 nodes": the card's title and the folded line of a reply's calls. Default: the tool's name and its arguments. */
|
|
138
|
+
label?: (args: A) => string;
|
|
139
|
+
/** The card's icon (SVG markup, about 14px). */
|
|
140
|
+
icon?: string;
|
|
141
|
+
/**
|
|
142
|
+
* Content of the app's own on the card, under its title and visible while
|
|
143
|
+
* it is folded (a card's arguments and raw result stay behind it): called
|
|
144
|
+
* when the call starts (`status: "running"`) and again with its result.
|
|
145
|
+
* `details` is what `execute` returned as details (live calls only).
|
|
146
|
+
*/
|
|
147
|
+
render?: (call: {
|
|
148
|
+
args: A;
|
|
149
|
+
status: ToolStatus | "running";
|
|
150
|
+
result?: string;
|
|
151
|
+
details?: unknown;
|
|
152
|
+
}) => HTMLElement | null | undefined;
|
|
153
|
+
/** A short outcome beside the status mark when the call ends ("$0.12", "3 nodes"). */
|
|
154
|
+
outcome?: (call: {
|
|
155
|
+
args: A;
|
|
156
|
+
status: ToolStatus;
|
|
157
|
+
result: string;
|
|
158
|
+
details?: unknown;
|
|
159
|
+
}) => string | undefined;
|
|
160
|
+
}
|
|
161
|
+
export interface SelectOption {
|
|
162
|
+
value: string;
|
|
163
|
+
label?: string;
|
|
164
|
+
description?: string;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The dialogs the harness and its built-in tools use. `domUI()` is the
|
|
168
|
+
* default (plain `<dialog>`s); an app with its own design passes an
|
|
169
|
+
* object implementing this interface.
|
|
170
|
+
*/
|
|
171
|
+
/** The agent's dialogs. A question given `signal` closes (as if dismissed) when the run is stopped. */
|
|
172
|
+
export interface AgentUI {
|
|
173
|
+
/**
|
|
174
|
+
* Yes/no question; resolves false when dismissed. With `always`, a UI may
|
|
175
|
+
* offer a third answer under its label ("Always allow"): it calls
|
|
176
|
+
* `onChoose` and resolves true. A UI that does not just answers yes or no.
|
|
177
|
+
*/
|
|
178
|
+
confirm(title: string, message?: string, opts?: {
|
|
179
|
+
ok?: string;
|
|
180
|
+
cancel?: string;
|
|
181
|
+
danger?: boolean;
|
|
182
|
+
signal?: AbortSignal;
|
|
183
|
+
always?: {
|
|
184
|
+
label: string;
|
|
185
|
+
onChoose: () => void;
|
|
186
|
+
};
|
|
187
|
+
body?: () => HTMLElement;
|
|
188
|
+
}): Promise<boolean>;
|
|
189
|
+
/**
|
|
190
|
+
* Pick one (or several with `multi`) of `options`; `allowOther` adds a
|
|
191
|
+
* free-text choice. Resolves the chosen values, or null when dismissed.
|
|
192
|
+
*/
|
|
193
|
+
select(title: string, options: SelectOption[], opts?: {
|
|
194
|
+
message?: string;
|
|
195
|
+
multi?: boolean;
|
|
196
|
+
allowOther?: boolean;
|
|
197
|
+
signal?: AbortSignal;
|
|
198
|
+
}): Promise<string[] | null>;
|
|
199
|
+
/** Free text; null when dismissed. */
|
|
200
|
+
input(title: string, opts?: {
|
|
201
|
+
message?: string;
|
|
202
|
+
placeholder?: string;
|
|
203
|
+
multiline?: boolean;
|
|
204
|
+
initial?: string;
|
|
205
|
+
signal?: AbortSignal;
|
|
206
|
+
}): Promise<string | null>;
|
|
207
|
+
notify(message: string, level?: "info" | "warning" | "error"): void;
|
|
208
|
+
}
|
|
209
|
+
export interface FileStat {
|
|
210
|
+
/** Absolute POSIX path inside the workspace. */
|
|
211
|
+
path: string;
|
|
212
|
+
size: number;
|
|
213
|
+
contentType?: string;
|
|
214
|
+
/** Milliseconds since the epoch, when known. */
|
|
215
|
+
mtime?: number;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* A file tree an app mounts into the agent's workspace (its document as
|
|
219
|
+
* files, a cloud folder, …). Paths given to it are relative to the mount
|
|
220
|
+
* point, absolute POSIX style (`/notes/today.md`). Read-only mounts leave
|
|
221
|
+
* `write`/`delete` out.
|
|
222
|
+
*/
|
|
223
|
+
export interface AgentFS {
|
|
224
|
+
/** Every file under `prefix` (recursive). */
|
|
225
|
+
list(prefix: string): Promise<FileStat[]>;
|
|
226
|
+
/** Text as a string, anything else as bytes; throws when missing. */
|
|
227
|
+
read(path: string): Promise<string | Uint8Array>;
|
|
228
|
+
write?(path: string, content: string | Uint8Array, contentType?: string): Promise<void>;
|
|
229
|
+
delete?(path: string): Promise<void>;
|
|
230
|
+
stat?(path: string): Promise<FileStat | null>;
|
|
231
|
+
/**
|
|
232
|
+
* Applies the agent's `edit_file` replacements itself (optional): each
|
|
233
|
+
* `oldText` is to be found once in the file and replaced by its `newText`
|
|
234
|
+
* — all of them against the original, none overlapping. Implement it to
|
|
235
|
+
* change your data in place (one node of a document, a range of a text
|
|
236
|
+
* buffer) instead of receiving the whole file. Throw an Error with a
|
|
237
|
+
* message for the model when an edit does not apply. Without it, the
|
|
238
|
+
* workspace reads the file, applies the edits (`applyEdits`) and writes
|
|
239
|
+
* the whole result back.
|
|
240
|
+
*/
|
|
241
|
+
edit?(path: string, edits: TextEdit[]): Promise<void>;
|
|
242
|
+
/** Shown to the model in the workspace description. */
|
|
243
|
+
description?: string;
|
|
244
|
+
}
|
|
245
|
+
/** A skill folder found in the workspace (`<skillsDir>/<name>/SKILL.md`, default `/.agent/skills`). */
|
|
246
|
+
export interface Skill {
|
|
247
|
+
name: string;
|
|
248
|
+
description: string;
|
|
249
|
+
/** Path of SKILL.md in the workspace. */
|
|
250
|
+
path: string;
|
|
251
|
+
/** The skill's folder. */
|
|
252
|
+
dir: string;
|
|
253
|
+
/** Only loaded when the app or user asks for it, never picked by the model. */
|
|
254
|
+
disableModelInvocation?: boolean;
|
|
255
|
+
/** It comes with the app (the workspace's `skills` option): the user cannot remove it. */
|
|
256
|
+
builtin?: boolean;
|
|
257
|
+
/** Where the user's skill is kept: its zip (`/skills/pack.zip`) or folder (`/skills/name`) in `userSkillsDir`. */
|
|
258
|
+
from?: string;
|
|
259
|
+
/** Its zip is loaded the first time it is used (a LazySkill of the app's). */
|
|
260
|
+
lazy?: boolean;
|
|
261
|
+
license?: string;
|
|
262
|
+
compatibility?: string;
|
|
263
|
+
allowedTools?: string[];
|
|
264
|
+
metadata?: Record<string, string>;
|
|
265
|
+
}
|
|
266
|
+
export type RunEndReason = "done" | "aborted" | "max_turns" | "error";
|
|
267
|
+
export interface AgentUsage {
|
|
268
|
+
/** Sum of every model call's `total_cost` (the gateway's figure, before platform markup and creator fee). */
|
|
269
|
+
total_cost: number;
|
|
270
|
+
prompt_tokens: number;
|
|
271
|
+
completion_tokens: number;
|
|
272
|
+
/** Model calls made (turns), including retries that returned usage. */
|
|
273
|
+
calls: number;
|
|
274
|
+
}
|
|
275
|
+
export interface RunResult {
|
|
276
|
+
runId: string;
|
|
277
|
+
reason: RunEndReason;
|
|
278
|
+
error?: Error;
|
|
279
|
+
/** The last assistant text of the run. */
|
|
280
|
+
text: string;
|
|
281
|
+
usage: AgentUsage;
|
|
282
|
+
}
|
|
283
|
+
export interface AgentEventMap {
|
|
284
|
+
/** `message` is null for `continue()`, which adds no user message. */
|
|
285
|
+
run_start: {
|
|
286
|
+
runId: string;
|
|
287
|
+
message: ChatMessage | null;
|
|
288
|
+
};
|
|
289
|
+
turn_start: {
|
|
290
|
+
runId: string;
|
|
291
|
+
turn: number;
|
|
292
|
+
};
|
|
293
|
+
/** Streamed assistant text: the new piece and the text so far. */
|
|
294
|
+
text_delta: {
|
|
295
|
+
runId: string;
|
|
296
|
+
delta: string;
|
|
297
|
+
text: string;
|
|
298
|
+
};
|
|
299
|
+
reasoning_delta: {
|
|
300
|
+
runId: string;
|
|
301
|
+
delta: string;
|
|
302
|
+
text: string;
|
|
303
|
+
};
|
|
304
|
+
/** A model call is retried after it had already streamed text: discard what was shown. */
|
|
305
|
+
message_reset: {
|
|
306
|
+
runId: string;
|
|
307
|
+
reason: string;
|
|
308
|
+
};
|
|
309
|
+
message_end: {
|
|
310
|
+
runId: string;
|
|
311
|
+
message: ChatMessage;
|
|
312
|
+
truncated: boolean;
|
|
313
|
+
};
|
|
314
|
+
/**
|
|
315
|
+
* A tool call's arguments as they stream in, before the call is complete
|
|
316
|
+
* (`arguments` is the raw JSON so far; it may stop anywhere). Nothing runs
|
|
317
|
+
* until the reply has ended; after a `message_reset` the calls start over.
|
|
318
|
+
*/
|
|
319
|
+
tool_call_delta: {
|
|
320
|
+
runId: string;
|
|
321
|
+
index: number;
|
|
322
|
+
id: string;
|
|
323
|
+
name: string;
|
|
324
|
+
delta: string;
|
|
325
|
+
arguments: string;
|
|
326
|
+
};
|
|
327
|
+
tool_start: {
|
|
328
|
+
runId: string;
|
|
329
|
+
id: string;
|
|
330
|
+
name: string;
|
|
331
|
+
args: unknown;
|
|
332
|
+
};
|
|
333
|
+
tool_update: {
|
|
334
|
+
runId: string;
|
|
335
|
+
id: string;
|
|
336
|
+
name: string;
|
|
337
|
+
content: string;
|
|
338
|
+
};
|
|
339
|
+
tool_end: {
|
|
340
|
+
runId: string;
|
|
341
|
+
id: string;
|
|
342
|
+
name: string;
|
|
343
|
+
status: ToolStatus;
|
|
344
|
+
/** What the model was told (as text: pictures are named). */
|
|
345
|
+
result: string;
|
|
346
|
+
/** The result as the model got it, pictures included (when it had any). */
|
|
347
|
+
content?: string | ContentPart[];
|
|
348
|
+
details?: unknown;
|
|
349
|
+
durationMs: number;
|
|
350
|
+
};
|
|
351
|
+
turn_end: {
|
|
352
|
+
runId: string;
|
|
353
|
+
turn: number;
|
|
354
|
+
usage?: Usage;
|
|
355
|
+
};
|
|
356
|
+
/** The transcript was shortened: by a model-written summary (`summary: true`) or by eliding/dropping. */
|
|
357
|
+
/** A summary of older turns is being written (by the loop, or `compact()`; no runId then). A `compaction` event follows when it replaced them. */
|
|
358
|
+
compaction_start: {
|
|
359
|
+
runId?: string;
|
|
360
|
+
};
|
|
361
|
+
compaction: {
|
|
362
|
+
runId?: string;
|
|
363
|
+
summary: boolean;
|
|
364
|
+
tokensBefore: number;
|
|
365
|
+
tokensAfter: number;
|
|
366
|
+
droppedTurns: number;
|
|
367
|
+
elidedResults: number;
|
|
368
|
+
};
|
|
369
|
+
/** The session was written to the workspace. */
|
|
370
|
+
session_saved: {
|
|
371
|
+
id: string;
|
|
372
|
+
};
|
|
373
|
+
/**
|
|
374
|
+
* A model call is about to be made: the request as sent (debugging views,
|
|
375
|
+
* counting calls). `purpose: "compaction"` for a call that writes a
|
|
376
|
+
* summary of older turns (a long history may take several).
|
|
377
|
+
*/
|
|
378
|
+
request: {
|
|
379
|
+
runId: string;
|
|
380
|
+
turn: number;
|
|
381
|
+
params: ChatCompletionParams;
|
|
382
|
+
purpose?: "compaction";
|
|
383
|
+
};
|
|
384
|
+
/** A model call came back (or failed): timing, finish reason, usage, the assembled message. */
|
|
385
|
+
response: {
|
|
386
|
+
runId: string;
|
|
387
|
+
turn: number;
|
|
388
|
+
durationMs: number;
|
|
389
|
+
finishReason?: string;
|
|
390
|
+
usage?: Usage;
|
|
391
|
+
message?: ChatMessage;
|
|
392
|
+
error?: string;
|
|
393
|
+
purpose?: "compaction";
|
|
394
|
+
};
|
|
395
|
+
/** Queued steering entered the history (at a turn, or before a new prompt): draw these messages now. */
|
|
396
|
+
steered: {
|
|
397
|
+
runId: string;
|
|
398
|
+
messages: ChatMessage[];
|
|
399
|
+
};
|
|
400
|
+
/** `rewind(index)` dropped the messages from `index` on. */
|
|
401
|
+
rewind: {
|
|
402
|
+
index: number;
|
|
403
|
+
};
|
|
404
|
+
queued: {
|
|
405
|
+
kind: "steer" | "followUp";
|
|
406
|
+
message: ChatMessage;
|
|
407
|
+
};
|
|
408
|
+
run_end: RunResult;
|
|
409
|
+
}
|
|
410
|
+
export type AgentEventName = keyof AgentEventMap;
|
|
411
|
+
export type Permissions = "ask" | "auto" | ((call: {
|
|
412
|
+
name: string;
|
|
413
|
+
args: unknown;
|
|
414
|
+
tool: Tool;
|
|
415
|
+
}) => boolean | Promise<boolean>);
|
|
416
|
+
export interface ToolCallHookEvent {
|
|
417
|
+
id: string;
|
|
418
|
+
name: string;
|
|
419
|
+
args: unknown;
|
|
420
|
+
tool: Tool;
|
|
421
|
+
}
|
|
422
|
+
export interface SessionData {
|
|
423
|
+
v: 1 | 2;
|
|
424
|
+
/** The agent's id (v2). */
|
|
425
|
+
id?: string;
|
|
426
|
+
/** The workspace (directory) id it was saved from; set by the store. */
|
|
427
|
+
space?: string;
|
|
428
|
+
/** The first user message, shortened (v2). */
|
|
429
|
+
title?: string;
|
|
430
|
+
model?: string;
|
|
431
|
+
messages: ChatMessage[];
|
|
432
|
+
usage: AgentUsage;
|
|
433
|
+
droppedTurns: number;
|
|
434
|
+
createdAt?: number;
|
|
435
|
+
savedAt: number;
|
|
436
|
+
/** How the last run ended when it did not finish (stopped, turn limit, error); gone once another run starts. */
|
|
437
|
+
ended?: RunEnd;
|
|
438
|
+
}
|
|
439
|
+
/** A run that did not finish: why, and when. */
|
|
440
|
+
export interface RunEnd {
|
|
441
|
+
reason: Exclude<RunEndReason, "done">;
|
|
442
|
+
error?: string;
|
|
443
|
+
at: number;
|
|
444
|
+
}
|
|
445
|
+
/** What a workspace lists for each saved session. */
|
|
446
|
+
export interface SessionSummary {
|
|
447
|
+
id: string;
|
|
448
|
+
/** The workspace (directory) id the session was saved from. */
|
|
449
|
+
workspace?: string;
|
|
450
|
+
title: string;
|
|
451
|
+
model?: string;
|
|
452
|
+
messages: number;
|
|
453
|
+
createdAt: number;
|
|
454
|
+
savedAt: number;
|
|
455
|
+
}
|
|
456
|
+
export interface AgentOptions {
|
|
457
|
+
/** Model name from the catalogue; the platform default when omitted. */
|
|
458
|
+
model?: string;
|
|
459
|
+
/** The app's own system instructions (a function is re-read every turn). */
|
|
460
|
+
instructions?: string | (() => string);
|
|
461
|
+
/** A short name for the app, used in the system prompt. */
|
|
462
|
+
appName?: string;
|
|
463
|
+
/** Private prompt reference(s) (RunApps: `{id, vars}`), forwarded on every request. */
|
|
464
|
+
prompt?: unknown;
|
|
465
|
+
tools?: Tool[];
|
|
466
|
+
/** Adds the file tools and the skills list; `client.workspace()` makes one. */
|
|
467
|
+
workspace?: Workspace | null;
|
|
468
|
+
/**
|
|
469
|
+
* Which of the workspace's file tools to offer (default all:
|
|
470
|
+
* list_files, read_file, write_file, edit_file, delete_file,
|
|
471
|
+
* move_file, copy_file, search_files). `["read_file", "list_files"]`
|
|
472
|
+
* gives a read-only agent.
|
|
473
|
+
*/
|
|
474
|
+
fileTools?: boolean | string[];
|
|
475
|
+
/**
|
|
476
|
+
* The file tools refuse to overwrite or edit an existing file the model
|
|
477
|
+
* has not read in this conversation, or that changed since it read it
|
|
478
|
+
* (the user edited it meanwhile), and ask it to read the file again —
|
|
479
|
+
* so nobody's changes are overwritten from a stale copy. Default true.
|
|
480
|
+
*/
|
|
481
|
+
readBeforeWrite?: boolean;
|
|
482
|
+
/** Dialogs; `domUI()` when omitted. */
|
|
483
|
+
ui?: AgentUI;
|
|
484
|
+
/**
|
|
485
|
+
* `"ask"` (default): destructive tools and tools with `confirm` ask the
|
|
486
|
+
* user first. `"auto"`: never ask. A function decides per call.
|
|
487
|
+
*/
|
|
488
|
+
permissions?: Permissions;
|
|
489
|
+
/** Model calls per run (default 100 — a safety net; pi, opencode and Claude Code have none, the loop guard and the user's stop button are the real limits). */
|
|
490
|
+
maxTurns?: number;
|
|
491
|
+
/** `max_tokens` of each model call; `"auto"` (default) uses the catalogue's maximum output for the model (8192 when unknown). */
|
|
492
|
+
maxTokens?: number | "auto";
|
|
493
|
+
/** Context budget in tokens; `"auto"` (default) reads the catalogue. */
|
|
494
|
+
/** The context budget: `"auto"` is the model's window; a number caps it (never above the window the catalogue gives). */
|
|
495
|
+
maxInputTokens?: number | "auto";
|
|
496
|
+
/** Run a turn's tool calls at once instead of one after another. */
|
|
497
|
+
parallelTools?: boolean;
|
|
498
|
+
/** Keep this many recent turns intact when the context is trimmed (default 4). */
|
|
499
|
+
keepTurns?: number;
|
|
500
|
+
/**
|
|
501
|
+
* Whether the model sees pictures, when you know better than the
|
|
502
|
+
* catalogue (default: its `input_modalities`; unknown when it has none).
|
|
503
|
+
* `true` offers `vision` tools and lets read_file show pictures; `false`
|
|
504
|
+
* never sends a picture — attached or returned by a tool — to the model.
|
|
505
|
+
*/
|
|
506
|
+
vision?: boolean;
|
|
507
|
+
/**
|
|
508
|
+
* The memory notes file in the workspace (default `/memory/notes.md`):
|
|
509
|
+
* shown to the model at the start of every conversation, and it is told
|
|
510
|
+
* to append to it. `false` for none — when the workspace is the user's own
|
|
511
|
+
* project, say, and you keep memory elsewhere.
|
|
512
|
+
*/
|
|
513
|
+
memoryFile?: string | false;
|
|
514
|
+
/** The project instructions file added to the system prompt (default `/AGENTS.md`); `false` for none. */
|
|
515
|
+
instructionsFile?: string | false;
|
|
516
|
+
/**
|
|
517
|
+
* The guard against tool calls that go nowhere: after `warn` rounds in a
|
|
518
|
+
* row that brought nothing new (the same results, lines already read,
|
|
519
|
+
* the same errors) the model is told to use what it has; after `stop` the
|
|
520
|
+
* run ends with an error. A round in which a tool that changes things
|
|
521
|
+
* succeeded is progress. Default `{ warn: 3, stop: 6 }`; `false` turns it off.
|
|
522
|
+
*/
|
|
523
|
+
loopGuard?: false | {
|
|
524
|
+
warn?: number;
|
|
525
|
+
stop?: number;
|
|
526
|
+
};
|
|
527
|
+
temperature?: number;
|
|
528
|
+
top_p?: number;
|
|
529
|
+
/** Any other chat parameter, sent as is (`reasoning_effort`, …). */
|
|
530
|
+
extra?: Record<string, unknown>;
|
|
531
|
+
/** Continue a saved session. */
|
|
532
|
+
restore?: SessionData;
|
|
533
|
+
/** Runs before every tool call; return `{ block: true }` to refuse it. */
|
|
534
|
+
onToolCall?: (ev: ToolCallHookEvent) => void | {
|
|
535
|
+
block: boolean;
|
|
536
|
+
reason?: string;
|
|
537
|
+
} | Promise<void | {
|
|
538
|
+
block: boolean;
|
|
539
|
+
reason?: string;
|
|
540
|
+
}>;
|
|
541
|
+
/** Offer the gateway's server tools when the agent has no tools of its own. */
|
|
542
|
+
serverTools?: Array<"web_search" | "web_fetch" | "twitter_search">;
|
|
543
|
+
/**
|
|
544
|
+
* The `run_js` sandbox tool (default on): the model computes and runs
|
|
545
|
+
* scripts in a Worker with no DOM and no network. `false` removes it;
|
|
546
|
+
* an object configures it (timeout, network).
|
|
547
|
+
*/
|
|
548
|
+
sandbox?: boolean | {
|
|
549
|
+
timeoutMs?: number;
|
|
550
|
+
network?: boolean;
|
|
551
|
+
maxLogChars?: number;
|
|
552
|
+
};
|
|
553
|
+
/** The `ask_user` tool (default on). */
|
|
554
|
+
askUser?: boolean;
|
|
555
|
+
/**
|
|
556
|
+
* Session id (default: a random id). With a workspace, an agent whose
|
|
557
|
+
* id has a saved session continues it, and every turn is saved under
|
|
558
|
+
* it (`persist: false` turns that off).
|
|
559
|
+
*/
|
|
560
|
+
id?: string;
|
|
561
|
+
/** Save the session in the workspace after every turn (default true when there is a workspace). */
|
|
562
|
+
persist?: boolean;
|
|
563
|
+
/**
|
|
564
|
+
* When the transcript nears the model's window, older turns are
|
|
565
|
+
* summarised by the model (pi-style checkpoint) and replaced by the
|
|
566
|
+
* summary; the newest turns stay verbatim. `false` falls back to
|
|
567
|
+
* eliding old tool results and dropping old turns; an object tunes
|
|
568
|
+
* the thresholds; a function writes the summary itself.
|
|
569
|
+
*/
|
|
570
|
+
compaction?: false | Partial<CompactionSettings> | ((plan: CutPlan, agent: Agent) => Promise<string>);
|
|
571
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Shared types of the agent harness: tools, events, the UI and file
|
|
2
|
+
// interfaces an app implements, and the options of `Agent`.
|
|
3
|
+
//
|
|
4
|
+
// The harness runs the model ↔ tool loop (`agent.ts`) over the chat
|
|
5
|
+
// endpoint; everything an app owns — its dialogs, its document as
|
|
6
|
+
// files, its skills zip, its tools — comes in through these interfaces.
|
|
7
|
+
export {};
|