@operad-io/harness-process 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +11 -2
- package/access-user.schema.json +52 -0
- package/package.json +22 -4
- package/src/acp.ts +370 -0
- package/src/always-allow.ts +204 -0
- package/src/ask-call.ts +252 -0
- package/src/ask-note.ts +47 -0
- package/src/event-note.ts +23 -0
- package/src/fold-lines.ts +238 -0
- package/src/grants.ts +53 -0
- package/src/images.ts +43 -0
- package/src/inbox.ts +45 -0
- package/src/index.ts +23 -0
- package/src/jsonl-log.ts +71 -0
- package/src/limit.ts +89 -0
- package/src/lines.ts +138 -0
- package/src/operad-tools.ts +57 -0
- package/src/process.ts +568 -0
- package/src/step.ts +293 -0
- package/src/usage.ts +62 -0
package/src/lines.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lines the child programs of the claude and pi harnesses, and radian's
|
|
3
|
+
* journal once `radianLogLine` has turned it into the same shapes, write to a
|
|
4
|
+
* step's log. A harness keeps in its own protocol only the lines that are its
|
|
5
|
+
* own (a session, a message's usage, a result).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** The longest tool output one `tool_output` line carries, in bytes of UTF-8. */
|
|
9
|
+
export const TOOL_OUTPUT_CAP_BYTES = 64 * 1024;
|
|
10
|
+
|
|
11
|
+
export type SharedLine =
|
|
12
|
+
| {
|
|
13
|
+
readonly type: "tool_execution_start";
|
|
14
|
+
readonly toolCallId: string;
|
|
15
|
+
readonly toolName: string;
|
|
16
|
+
/** When the call started, in epoch milliseconds. */
|
|
17
|
+
readonly at: number;
|
|
18
|
+
/** What ran: the command (500 characters), the path, or the main argument; absent when there is none. */
|
|
19
|
+
readonly input?: string;
|
|
20
|
+
}
|
|
21
|
+
| {
|
|
22
|
+
readonly type: "tool_execution_end";
|
|
23
|
+
readonly toolCallId: string;
|
|
24
|
+
readonly toolName: string;
|
|
25
|
+
readonly isError: boolean;
|
|
26
|
+
/** How long the call took. */
|
|
27
|
+
readonly durationMs: number;
|
|
28
|
+
/** The first line of the error, when the call failed. */
|
|
29
|
+
readonly error?: string;
|
|
30
|
+
}
|
|
31
|
+
| { readonly type: "blocked"; readonly toolName: string; readonly reason: string }
|
|
32
|
+
/**
|
|
33
|
+
* The program asks to use up a one-time permission before it lets a call
|
|
34
|
+
* through. The host writes the fact and answers on stdin with a
|
|
35
|
+
* `GrantAck` line; only `used: true` lets the call run.
|
|
36
|
+
*/
|
|
37
|
+
| { readonly type: "grant_use"; readonly id: number; readonly waitId: string }
|
|
38
|
+
/**
|
|
39
|
+
* The program asks a person whether one call may run (the access profile's
|
|
40
|
+
* `askCommands`) and holds the call until the host answers with an `AskAck`
|
|
41
|
+
* line. `session` is what a late answer continues. `ask_resolved` follows
|
|
42
|
+
* the answer, so the host knows the question is closed.
|
|
43
|
+
*/
|
|
44
|
+
| {
|
|
45
|
+
readonly type: "ask_call";
|
|
46
|
+
readonly id: number;
|
|
47
|
+
readonly kind: "command" | "path";
|
|
48
|
+
readonly value: string;
|
|
49
|
+
readonly tool?: string;
|
|
50
|
+
readonly shown: string;
|
|
51
|
+
readonly session: string | null;
|
|
52
|
+
}
|
|
53
|
+
| { readonly type: "ask_resolved"; readonly id: number }
|
|
54
|
+
/** New assistant text only, not the text so far. */
|
|
55
|
+
| { readonly type: "text_delta"; readonly text: string }
|
|
56
|
+
/** New thinking only, not the thinking so far. */
|
|
57
|
+
| { readonly type: "thinking_delta"; readonly text: string }
|
|
58
|
+
/** The full text of a finished assistant message. */
|
|
59
|
+
| { readonly type: "text_end"; readonly text: string }
|
|
60
|
+
/** The full text of a finished thinking block. */
|
|
61
|
+
| { readonly type: "thinking_end"; readonly text: string }
|
|
62
|
+
/**
|
|
63
|
+
* What a tool call printed. A line carries at most `TOOL_OUTPUT_CAP_BYTES`;
|
|
64
|
+
* a longer output is written as consecutive lines with the same call id.
|
|
65
|
+
* `truncated` is only for a line a caller had to cut.
|
|
66
|
+
*/
|
|
67
|
+
| {
|
|
68
|
+
readonly type: "tool_output";
|
|
69
|
+
readonly toolCallId: string;
|
|
70
|
+
readonly text: string;
|
|
71
|
+
readonly truncated?: true;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** Cuts `text` to the output cap without splitting a character; says whether it cut. */
|
|
75
|
+
export function capToolOutput(text: string): { readonly text: string; readonly truncated: boolean } {
|
|
76
|
+
const bytes = new TextEncoder().encode(text);
|
|
77
|
+
if (bytes.length <= TOOL_OUTPUT_CAP_BYTES) return { text, truncated: false };
|
|
78
|
+
// Back off to the start of a character: a continuation byte is 10xxxxxx.
|
|
79
|
+
let end = TOOL_OUTPUT_CAP_BYTES;
|
|
80
|
+
while (end > 0 && ((bytes[end] ?? 0) & 0xc0) === 0x80) end -= 1;
|
|
81
|
+
const cut = new TextDecoder().decode(bytes.subarray(0, end));
|
|
82
|
+
return { text: cut, truncated: true };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The `tool_output` payloads for what a tool printed: the text masked once,
|
|
87
|
+
* then cut into pieces no longer than the shared cap. Joining the pieces gives
|
|
88
|
+
* the whole output back; a short output is one piece.
|
|
89
|
+
*/
|
|
90
|
+
export function toolOutputLines(
|
|
91
|
+
text: string,
|
|
92
|
+
secrets: readonly string[],
|
|
93
|
+
): readonly { readonly text: string }[] {
|
|
94
|
+
const masked = maskSecrets(text, secrets);
|
|
95
|
+
const bytes = new TextEncoder().encode(masked);
|
|
96
|
+
if (bytes.length <= TOOL_OUTPUT_CAP_BYTES) return [{ text: masked }];
|
|
97
|
+
const lines: { text: string }[] = [];
|
|
98
|
+
const decoder = new TextDecoder();
|
|
99
|
+
let start = 0;
|
|
100
|
+
while (start < bytes.length) {
|
|
101
|
+
let end = Math.min(start + TOOL_OUTPUT_CAP_BYTES, bytes.length);
|
|
102
|
+
// Back off to the start of a character: a continuation byte is 10xxxxxx.
|
|
103
|
+
while (end > start && ((bytes[end] ?? 0) & 0xc0) === 0x80) end -= 1;
|
|
104
|
+
lines.push({ text: decoder.decode(bytes.subarray(start, end)) });
|
|
105
|
+
start = end;
|
|
106
|
+
}
|
|
107
|
+
return lines;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Replaces every secret value in a log line, also as it is written inside JSON text, with `***`. */
|
|
111
|
+
export function maskSecrets(line: string, secrets: readonly string[]): string {
|
|
112
|
+
let masked = line;
|
|
113
|
+
for (const secret of [...secrets].sort((x, y) => y.length - x.length)) {
|
|
114
|
+
if (secret.length === 0) continue;
|
|
115
|
+
masked = masked.split(secret).join("***");
|
|
116
|
+
const escaped = JSON.stringify(secret).slice(1, -1);
|
|
117
|
+
if (escaped !== secret) masked = masked.split(escaped).join("***");
|
|
118
|
+
}
|
|
119
|
+
return masked;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Hands a line that was written to the log to the caller's receiver, with the
|
|
124
|
+
* secret values masked. A receiver that throws does not stop the step: the log
|
|
125
|
+
* has the line.
|
|
126
|
+
*/
|
|
127
|
+
export function deliverLine(
|
|
128
|
+
receiver: ((line: string) => void) | undefined,
|
|
129
|
+
line: string,
|
|
130
|
+
secrets: readonly string[],
|
|
131
|
+
): void {
|
|
132
|
+
if (receiver === undefined) return;
|
|
133
|
+
try {
|
|
134
|
+
receiver(maskSecrets(line, secrets));
|
|
135
|
+
} catch {
|
|
136
|
+
// the log is the record; the caller's trouble stays the caller's
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Operad's own tools for a harness: the `operadTools` param of an `agent` step
|
|
3
|
+
* names the home whose tools the agent may call. A harness that starts MCP
|
|
4
|
+
* servers starts `operad`'s as a program (`serverCommand`); one that takes
|
|
5
|
+
* tools in its own process opens them (`@operad-io/mcp`, `openMcpTools`). The
|
|
6
|
+
* agent never runs the `operad` command.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { isRecord } from "@operad-io/engine";
|
|
11
|
+
|
|
12
|
+
/** The name the tools go by in a harness: the server's name, and the prefix claude gives its tools. */
|
|
13
|
+
export const OPERAD_SERVER = "operad";
|
|
14
|
+
|
|
15
|
+
/** The prefix of an MCP tool's name in claude. */
|
|
16
|
+
export const OPERAD_TOOL_PREFIX = `mcp__${OPERAD_SERVER}__`;
|
|
17
|
+
|
|
18
|
+
/** What the `operadTools` param holds. */
|
|
19
|
+
export interface OperadTools {
|
|
20
|
+
/** The operad home the tools read and write. */
|
|
21
|
+
readonly home: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The tools a step's params ask for, or undefined when they ask for none. */
|
|
25
|
+
export function operadToolsOf(value: unknown): OperadTools | undefined {
|
|
26
|
+
if (!isRecord(value)) return undefined;
|
|
27
|
+
const home = value.home;
|
|
28
|
+
return typeof home === "string" && home.length > 0 ? { home } : undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The program that serves the tools on stdio, and its arguments. */
|
|
32
|
+
export function serverCommand(tools: OperadTools): { readonly command: string; readonly args: readonly string[] } {
|
|
33
|
+
const script = fileURLToPath(import.meta.resolve("@operad-io/mcp/stdio"));
|
|
34
|
+
return { command: process.execPath, args: [script, "--home", tools.home] };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A server a bot's settings list, started as this command; its name is the one its tools go by. */
|
|
38
|
+
export interface MountedServer {
|
|
39
|
+
readonly command: string;
|
|
40
|
+
readonly args: readonly string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The `mcpServers` param of an `agent` step: other MCP servers to start for
|
|
45
|
+
* the agent, by name. The name of operad's own server is not available.
|
|
46
|
+
*/
|
|
47
|
+
export function mountedServersOf(value: unknown): Readonly<Record<string, MountedServer>> | undefined {
|
|
48
|
+
if (!isRecord(value)) return undefined;
|
|
49
|
+
const found: Record<string, MountedServer> = {};
|
|
50
|
+
for (const [name, entry] of Object.entries(value)) {
|
|
51
|
+
if (name === OPERAD_SERVER || name.length === 0 || name.includes("__")) continue;
|
|
52
|
+
if (!isRecord(entry) || typeof entry.command !== "string" || entry.command.length === 0) continue;
|
|
53
|
+
const args = Array.isArray(entry.args) ? entry.args.filter((item): item is string => typeof item === "string") : [];
|
|
54
|
+
found[name] = { command: entry.command, args };
|
|
55
|
+
}
|
|
56
|
+
return Object.keys(found).length === 0 ? undefined : found;
|
|
57
|
+
}
|