Add the harness, envs, per-bot models and chat attachments
- Harness: providers, model catalog synced hourly, automatic free picks, cooldowns, tool loop with context trimming, MCP computer tools - Bots queue runs replies and tasks through the harness; jobs wait (attempt refunded) while every model is resting - Envs: Settings > Environment, scoped general/workspace/computer, written to computers and redacted from tool output - Models: Settings > Models, per-harness provider select defaulting to OpenRouter, and a models modal on each bot's chat (the coordinator's edits the defaults) - Chat: thinking row while a reply runs, file attachments (upload, paste, drop) that replies read as images, audio and document text - Fix media uploads failing after a user's first file, and private files being served from the public media path Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { z } from "zod";
|
||||
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
|
||||
import pushBotNotice from "@/src/bots-queue/push-bot-notice";
|
||||
import enqueueBotJob from "@/src/functions/backend/db/bots-queue/enqueue-bot-job";
|
||||
import insertBotMessage from "@/src/functions/backend/db/bots/insert-bot-message";
|
||||
import grabBotComputers from "@/src/functions/backend/db/computers/grab-bot-computers";
|
||||
import type { HarnessTool } from "../../types";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
/**
|
||||
* The coordinator's name, shown in the worker's conversation.
|
||||
*/
|
||||
coordinator_name: string;
|
||||
/**
|
||||
* The user's worker bots.
|
||||
*/
|
||||
workers: BUN_SQLITE_COBBLE_BOTS[];
|
||||
};
|
||||
|
||||
type Args = {
|
||||
bot_id: number;
|
||||
title: string;
|
||||
instructions: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Lets the coordinator hand a task to a worker bot. The worker runs it on
|
||||
* its own main computer (if it has one) and posts the result in its own
|
||||
* conversation, where a note says who asked.
|
||||
*/
|
||||
export default function delegateTaskTool({
|
||||
user_id,
|
||||
coordinator_name,
|
||||
workers,
|
||||
}: Params): HarnessTool<Args> {
|
||||
const crew = workers
|
||||
.map((bot) => `${bot.id}: ${bot.name}${bot.role ? ` (${bot.role})` : ""}`)
|
||||
.join("; ");
|
||||
|
||||
return {
|
||||
name: "delegate_task",
|
||||
description: `Hand a task to a crew member. They run it in the background and post the result in their own conversation. Write the instructions as a full brief: what to do, where, and how they'll know it's done. Crew: ${crew}.`,
|
||||
schema: z.object({
|
||||
bot_id: z.number().int(),
|
||||
title: z.string().min(1).max(80),
|
||||
instructions: z.string().min(1).max(8000),
|
||||
}),
|
||||
run: async ({ args }) => {
|
||||
const worker = workers.find((bot) => bot.id == args.bot_id);
|
||||
|
||||
if (!worker?.id) {
|
||||
throw new Error(`No crew member has id ${args.bot_id}. Crew: ${crew}.`);
|
||||
}
|
||||
|
||||
const computers = await grabBotComputers({ bot_id: worker.id, user_id });
|
||||
|
||||
const job_id = await enqueueBotJob({
|
||||
user_id,
|
||||
bot_id: worker.id,
|
||||
title: args.title,
|
||||
instructions: args.instructions,
|
||||
computer_id: computers[0]?.id || undefined,
|
||||
});
|
||||
|
||||
await pushBotNotice({
|
||||
bot_id: worker.id,
|
||||
message: await insertBotMessage({
|
||||
bot_id: worker.id,
|
||||
text: `${coordinator_name} asked me to: **${args.title}**`,
|
||||
}),
|
||||
});
|
||||
|
||||
return `Task #${job_id} is with ${worker.name}. Their result will be in their conversation.`;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
|
||||
import grabBotComputers from "@/src/functions/backend/db/computers/grab-bot-computers";
|
||||
import type { HarnessTool } from "../../types";
|
||||
import delegateTaskTool from "./delegate-task-tool";
|
||||
import grabBotTools from "./grab-bot-tools";
|
||||
import startTaskTool from "./start-task-tool";
|
||||
|
||||
type Params = {
|
||||
bot: BUN_SQLITE_COBBLE_BOTS;
|
||||
user_id: number | string;
|
||||
/**
|
||||
* The user message being answered.
|
||||
*/
|
||||
message_id?: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
* The tools a bot replies in chat with: its own (memory, instructions),
|
||||
* start_task for longer work, and for the coordinator delegate_task to
|
||||
* the crew. Computers are reached through tasks, never mid-reply.
|
||||
*/
|
||||
export default async function grabBotChatTools({
|
||||
bot,
|
||||
user_id,
|
||||
message_id,
|
||||
}: Params): Promise<HarnessTool[]> {
|
||||
const bot_id = bot.id || 0;
|
||||
const tools = [
|
||||
...grabBotTools({ user_id, bot_id }),
|
||||
startTaskTool({
|
||||
user_id,
|
||||
bot_id,
|
||||
computers: await grabBotComputers({ bot_id, user_id }),
|
||||
message_id,
|
||||
}),
|
||||
];
|
||||
|
||||
if (bot.kind != "coordinator") return tools;
|
||||
|
||||
const workers =
|
||||
(
|
||||
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS>({
|
||||
sql: `SELECT * FROM bots WHERE user_id = ? AND kind = 'worker' ORDER BY id ASC`,
|
||||
values: [user_id],
|
||||
})
|
||||
).payload || [];
|
||||
|
||||
if (workers.length) {
|
||||
tools.push(
|
||||
delegateTaskTool({
|
||||
user_id,
|
||||
coordinator_name: bot.name || "The coordinator",
|
||||
workers,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return tools;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { HarnessTool } from "../../types";
|
||||
import updateInstructionsTool from "./update-instructions-tool";
|
||||
import updateMemoryTool from "./update-memory-tool";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
bot_id: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
* The tools a bot works with, bound to that bot. Computer, delegation and
|
||||
* media tools join this list as they are built.
|
||||
*/
|
||||
export default function grabBotTools({ user_id, bot_id }: Params): HarnessTool[] {
|
||||
return [
|
||||
updateMemoryTool({ user_id, bot_id }),
|
||||
updateInstructionsTool({ user_id, bot_id }),
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { z } from "zod";
|
||||
import type { BUN_SQLITE_COBBLE_COMPUTERS } from "@/db/types/db";
|
||||
import enqueueBotJob from "@/src/functions/backend/db/bots-queue/enqueue-bot-job";
|
||||
import type { HarnessTool } from "../../types";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
bot_id: number | string;
|
||||
/**
|
||||
* The bot's computers, its main one first (`grabBotComputers`).
|
||||
*/
|
||||
computers: BUN_SQLITE_COBBLE_COMPUTERS[];
|
||||
/**
|
||||
* The user message that asked for the work.
|
||||
*/
|
||||
message_id?: number | string;
|
||||
};
|
||||
|
||||
type Args = {
|
||||
title: string;
|
||||
instructions: string;
|
||||
computer_id?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Lets a bot queue long-running work for itself. The task runs in the
|
||||
* background with the computer's tools and posts its result to the chat.
|
||||
*/
|
||||
export default function startTaskTool({
|
||||
user_id,
|
||||
bot_id,
|
||||
computers,
|
||||
message_id,
|
||||
}: Params): HarnessTool<Args> {
|
||||
const listed = computers
|
||||
.map((computer) => `${computer.id} (${computer.name || computer.kind})`)
|
||||
.join(", ");
|
||||
|
||||
return {
|
||||
name: "start_task",
|
||||
description: `Queue work that takes a while or needs a computer. It runs in the background and its result is posted in this chat. Write the instructions as a full brief for yourself: what to do, where, and how you'll know it's done. ${
|
||||
computers.length
|
||||
? `Computers you can use: ${listed}. Defaults to ${computers[0]!.id}.`
|
||||
: `You have no computer, so the task can't run commands or browse.`
|
||||
}`,
|
||||
schema: z.object({
|
||||
title: z.string().min(1).max(80).describe("A short label, e.g. \"Check the backup logs\""),
|
||||
instructions: z.string().min(1).max(8000),
|
||||
computer_id: z.number().int().optional(),
|
||||
}),
|
||||
run: async ({ args }) => {
|
||||
const computer_id = args.computer_id ?? computers[0]?.id ?? undefined;
|
||||
|
||||
const job_id = await enqueueBotJob({
|
||||
user_id,
|
||||
bot_id,
|
||||
title: args.title,
|
||||
instructions: args.instructions,
|
||||
message_id,
|
||||
computer_id: computer_id || undefined,
|
||||
});
|
||||
|
||||
return `Task #${job_id} queued. Tell the user you're on it; the result will be posted here when it's done.`;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { z } from "zod";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import updateBotSystemPrompt from "@/src/functions/backend/db/bots/update-bot-system-prompt";
|
||||
import type { HarnessTool } from "../../types";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
bot_id: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Lets a bot rewrite its own core instructions as it adapts to the user.
|
||||
*/
|
||||
export default function updateInstructionsTool({
|
||||
user_id,
|
||||
bot_id,
|
||||
}: Params): HarnessTool<{ instructions: string }> {
|
||||
return {
|
||||
name: "update_instructions",
|
||||
description: `Replace your core instructions with this full text (markdown): the start of your prompt, before any workspace or memory. Change them only to work better for this user, such as a new standing rule they gave you. Keep everything that still applies.`,
|
||||
schema: z.object({
|
||||
instructions: z.string().trim().min(1).max(AppData.PromptMaxLength),
|
||||
}),
|
||||
run: async ({ args }) => {
|
||||
await updateBotSystemPrompt({
|
||||
user_id,
|
||||
bot_id,
|
||||
system_prompt: args.instructions,
|
||||
});
|
||||
return `Instructions saved. They apply from your next run.`;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { z } from "zod";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import updateBotMemory from "@/src/functions/backend/db/bots/update-bot-memory";
|
||||
import type { HarnessTool } from "../../types";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
bot_id: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Lets a bot rewrite its own memory (shown to it under `# Memory`).
|
||||
*/
|
||||
export default function updateMemoryTool({
|
||||
user_id,
|
||||
bot_id,
|
||||
}: Params): HarnessTool<{ memory: string }> {
|
||||
return {
|
||||
name: "update_memory",
|
||||
description: `Replace your memory with this full text (markdown). It's in your prompt under "# Memory" on every run. Keep what you must always remember: the user's preferences, facts about their work, decisions made. Anything you leave out is forgotten, so include the parts that still matter.`,
|
||||
schema: z.object({
|
||||
memory: z.string().max(AppData.PromptMaxLength),
|
||||
}),
|
||||
run: async ({ args }) => {
|
||||
await updateBotMemory({ user_id, bot_id, memory: args.memory });
|
||||
return `Memory saved.`;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
type Params = {
|
||||
envs: { key: string; value: string }[];
|
||||
};
|
||||
|
||||
/**
|
||||
* A computer's env files: `shell` for bash (`export KEY='value'`, single
|
||||
* quotes escaped) and `json` for code runners.
|
||||
*/
|
||||
export default function composeComputerEnvFiles({ envs }: Params) {
|
||||
const shell = envs
|
||||
.map((env) => `export ${env.key}='${env.value.replaceAll("'", `'\\''`)}'`)
|
||||
.join("\n");
|
||||
|
||||
return {
|
||||
shell: shell ? `${shell}\n` : "",
|
||||
json: JSON.stringify(Object.fromEntries(envs.map((env) => [env.key, env.value]))),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import { ComputerMcpExcludedTools } from "@/src/dict/computer-dict";
|
||||
import grabComputerMcpUrl from "@/src/functions/backend/db/computers/grab-computer-mcp-url";
|
||||
import grabComputerEnvs from "@/src/functions/backend/db/envs/grab-computer-envs";
|
||||
import redactSecrets from "@/src/utils/redact-secrets";
|
||||
import connectMcpServer from "../../mcp/connect-mcp-server";
|
||||
import grabMcpTools from "../../mcp/grab-mcp-tools";
|
||||
import readImage from "../../media/read-image";
|
||||
import type { HarnessTool } from "../../types";
|
||||
import withComputerEnv from "./with-computer-env";
|
||||
import writeComputerEnv from "./write-computer-env";
|
||||
|
||||
type Params = {
|
||||
computer_id: number | string;
|
||||
/**
|
||||
* Screenshots are read with this bot's media reading model, and only
|
||||
* workspaces it shares with the computer add envs.
|
||||
*/
|
||||
bot_id?: number | string;
|
||||
signal?: AbortSignal;
|
||||
};
|
||||
|
||||
export type OpenComputer = {
|
||||
name: string;
|
||||
tools: HarnessTool[];
|
||||
/**
|
||||
* The envs set on the computer, for the prompt: keys and descriptions,
|
||||
* never values.
|
||||
*/
|
||||
envs: { key: string; description: string }[];
|
||||
/**
|
||||
* Blanks the computer's secret values out of text, e.g. the report.
|
||||
*/
|
||||
redact: (text: string) => string;
|
||||
close: () => Promise<void>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Connects to a computer (its AIO sandbox, over MCP) and returns its tools:
|
||||
* shell, files, code and browser. Its envs (`grabComputerEnvs`) are written
|
||||
* to the computer and loaded by the shell and code tools; secret values are
|
||||
* redacted from everything the tools return. Screenshots come back as
|
||||
* readings from the media reading harness. Call `close` when the run ends.
|
||||
* Throws when the computer can't be reached, which the queue retries.
|
||||
*/
|
||||
export default async function openComputerTools({
|
||||
computer_id,
|
||||
bot_id,
|
||||
signal,
|
||||
}: Params): Promise<OpenComputer> {
|
||||
const target = await grabComputerMcpUrl({ computer_id });
|
||||
|
||||
if (!target) {
|
||||
throw new Error(`Computer #${computer_id} has no container to connect to.`);
|
||||
}
|
||||
|
||||
const envs = await grabComputerEnvs({ computer_id, bot_id });
|
||||
const paths = await writeComputerEnv({ base_url: target.base_url, envs, signal });
|
||||
const secrets = envs.filter((env) => env.secret);
|
||||
const redact = (text: string) => redactSecrets({ text, secrets });
|
||||
|
||||
const client = await connectMcpServer({ url: target.url, signal });
|
||||
|
||||
try {
|
||||
const tools = await grabMcpTools({
|
||||
client,
|
||||
exclude: ComputerMcpExcludedTools,
|
||||
redact,
|
||||
describeImage: ({ data, mime_type }) =>
|
||||
readImage({
|
||||
url: `data:${mime_type};base64,${data}`,
|
||||
focus: "This is a screenshot of a computer screen. Describe the page layout, text, buttons and form fields, and any errors or popups.",
|
||||
signal,
|
||||
bot_id,
|
||||
}),
|
||||
});
|
||||
|
||||
return {
|
||||
name: target.name,
|
||||
tools: tools.map((tool) => withComputerEnv({ tool, ...paths })),
|
||||
envs: envs.map((env) => ({ key: env.key, description: env.description })),
|
||||
redact,
|
||||
close: () => client.close().catch(() => {}),
|
||||
};
|
||||
} catch (error) {
|
||||
await client.close().catch(() => {});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import type { HarnessTool } from "../../types";
|
||||
import composeComputerEnvFiles from "./compose-computer-env-files";
|
||||
import withComputerEnv from "./with-computer-env";
|
||||
|
||||
const echoTool = (name: string): HarnessTool => ({
|
||||
name,
|
||||
description: "",
|
||||
parameters: {},
|
||||
run: async ({ args }) => JSON.stringify(args),
|
||||
});
|
||||
|
||||
const paths = { shell_path: "/home/gem/.cobble/env", json_path: "/home/gem/.cobble/env.json" };
|
||||
const signal = new AbortController().signal;
|
||||
|
||||
describe("withComputerEnv", () => {
|
||||
test("sources the env file before bash commands", async () => {
|
||||
const tool = withComputerEnv({ tool: echoTool("sandbox_execute_bash"), ...paths });
|
||||
const sent = JSON.parse(await tool.run({ args: { cmd: "echo $TOKEN" }, signal }));
|
||||
|
||||
expect(sent.cmd).toBe("[ -f /home/gem/.cobble/env ] && . /home/gem/.cobble/env; echo $TOKEN");
|
||||
});
|
||||
|
||||
test("loads the json file before python code", async () => {
|
||||
const tool = withComputerEnv({ tool: echoTool("sandbox_execute_code"), ...paths });
|
||||
const sent = JSON.parse(await tool.run({ args: { code: "print(1)", language: "python" }, signal }));
|
||||
|
||||
expect(sent.code).toStartWith("import json as _cobble_json");
|
||||
expect(sent.code).toEndWith("\nprint(1)");
|
||||
});
|
||||
|
||||
test("leaves other tools alone", () => {
|
||||
const tool = echoTool("browser_navigate");
|
||||
expect(withComputerEnv({ tool, ...paths })).toBe(tool);
|
||||
});
|
||||
});
|
||||
|
||||
describe("composeComputerEnvFiles", () => {
|
||||
test("escapes single quotes for the shell", () => {
|
||||
expect(
|
||||
composeComputerEnvFiles({ envs: [{ key: "A", value: "it's" }] }).shell,
|
||||
).toBe(`export A='it'\\''s'\n`);
|
||||
});
|
||||
|
||||
test("is empty without envs", () => {
|
||||
expect(composeComputerEnvFiles({ envs: [] })).toEqual({ shell: "", json: "{}" });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
import type { HarnessTool } from "../../types";
|
||||
|
||||
type Params = {
|
||||
tool: HarnessTool;
|
||||
/**
|
||||
* Absolute paths of the env files on the computer.
|
||||
*/
|
||||
shell_path: string;
|
||||
json_path: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Makes a computer tool run with the computer's envs. The sandbox's shells
|
||||
* don't read a profile, so bash commands source the env file first and
|
||||
* Python or JavaScript code loads the JSON one. The model never sees the
|
||||
* addition; other tools pass through.
|
||||
*/
|
||||
export default function withComputerEnv({
|
||||
tool,
|
||||
shell_path,
|
||||
json_path,
|
||||
}: Params): HarnessTool {
|
||||
const shell_prefix = `[ -f ${shell_path} ] && . ${shell_path}; `;
|
||||
|
||||
if (tool.name == "sandbox_execute_bash") {
|
||||
return {
|
||||
...tool,
|
||||
run: ({ args, signal }) =>
|
||||
tool.run({
|
||||
args: typeof args?.cmd == "string" ? { ...args, cmd: `${shell_prefix}${args.cmd}` } : args,
|
||||
signal,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
if (tool.name == "sandbox_execute_code") {
|
||||
return {
|
||||
...tool,
|
||||
run: ({ args, signal }) =>
|
||||
tool.run({
|
||||
args:
|
||||
typeof args?.code == "string"
|
||||
? { ...args, code: `${codePrefix({ language: args.language, json_path })}${args.code}` }
|
||||
: args,
|
||||
signal,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
return tool;
|
||||
}
|
||||
|
||||
function codePrefix({ language, json_path }: { language?: unknown; json_path: string }) {
|
||||
const path = JSON.stringify(json_path);
|
||||
const lang = typeof language == "string" ? language.toLowerCase() : "python";
|
||||
|
||||
if (lang.startsWith("py")) {
|
||||
return `import json as _cobble_json, os as _cobble_os; _cobble_os.environ.update(_cobble_json.load(open(${path})) if _cobble_os.path.exists(${path}) else {})\n`;
|
||||
}
|
||||
|
||||
if (lang.startsWith("js") || lang.startsWith("javascript") || lang.startsWith("node")) {
|
||||
return `try { Object.assign(process.env, JSON.parse(require("fs").readFileSync(${path}, "utf8"))); } catch {}\n`;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import composeComputerEnvFiles from "./compose-computer-env-files";
|
||||
|
||||
type Params = {
|
||||
/**
|
||||
* The sandbox's REST address (no `/mcp`).
|
||||
*/
|
||||
base_url: string;
|
||||
envs: { key: string; value: string }[];
|
||||
signal?: AbortSignal;
|
||||
};
|
||||
|
||||
/**
|
||||
* Writes the computer's env files (`~/.cobble/env` and `env.json`, owner
|
||||
* only), replacing the last run's, so removed envs disappear too. Returns
|
||||
* their paths for `withComputerEnv`.
|
||||
*/
|
||||
export default async function writeComputerEnv({ base_url, envs, signal }: Params) {
|
||||
const call = async (path: string, init?: RequestInit) => {
|
||||
const res = await fetch(`${base_url}${path}`, {
|
||||
...init,
|
||||
headers: { "content-type": "application/json" },
|
||||
signal: AbortSignal.any([
|
||||
...(signal ? [signal] : []),
|
||||
AbortSignal.timeout(AppData.McpConnectTimeoutMs),
|
||||
]),
|
||||
});
|
||||
const body: any = await res.json().catch(() => ({}));
|
||||
|
||||
if (!res.ok || body?.success === false) {
|
||||
throw new Error(`The computer refused ${path}: ${body?.message || res.status}`);
|
||||
}
|
||||
|
||||
return body;
|
||||
};
|
||||
|
||||
const context = await call(`/v1/sandbox`);
|
||||
const home = String(context.home_dir || "/root").replace(/\/+$/, "");
|
||||
const dir = `${home}/.cobble`;
|
||||
const shell_path = `${dir}/env`;
|
||||
const json_path = `${dir}/env.json`;
|
||||
const files = composeComputerEnvFiles({ envs });
|
||||
|
||||
await call(`/v1/shell/exec`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
command: `mkdir -p ${dir} && chmod 700 ${dir} && : > ${shell_path} && : > ${json_path} && chmod 600 ${shell_path} ${json_path}`,
|
||||
}),
|
||||
});
|
||||
|
||||
for (const [file, content] of [
|
||||
[shell_path, files.shell],
|
||||
[json_path, files.json],
|
||||
] as const) {
|
||||
await call(`/v1/file/write`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ file, content }),
|
||||
});
|
||||
}
|
||||
|
||||
return { shell_path, json_path };
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { z } from "zod";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import HarnessError from "../harness-error";
|
||||
import type { HarnessTool } from "../types";
|
||||
import runToolCall from "./run-tool-call";
|
||||
import toolDefinition from "./tool-definition";
|
||||
|
||||
const echo: HarnessTool<{ text: string }> = {
|
||||
name: "echo",
|
||||
description: "Says it back.",
|
||||
schema: z.object({ text: z.string().min(1) }),
|
||||
run: async ({ args }) => args.text,
|
||||
};
|
||||
|
||||
const failing: HarnessTool = {
|
||||
name: "fail",
|
||||
description: "Always fails.",
|
||||
schema: z.object({}),
|
||||
run: async () => {
|
||||
throw new Error("disk full");
|
||||
},
|
||||
};
|
||||
|
||||
const signal = new AbortController().signal;
|
||||
const call = (name: string, args: string) =>
|
||||
runToolCall({
|
||||
call: { id: "1", name, arguments: args },
|
||||
tools: [echo, failing],
|
||||
signal,
|
||||
});
|
||||
|
||||
describe("runToolCall", () => {
|
||||
test("runs the tool with validated arguments", async () => {
|
||||
expect(await call("echo", `{"text":"hi"}`)).toBe("hi");
|
||||
});
|
||||
|
||||
test("reports problems to the model instead of throwing", async () => {
|
||||
expect(await call("nope", "{}")).toContain(`no tool called "nope"`);
|
||||
expect(await call("echo", "{bad")).toContain("weren't valid JSON");
|
||||
expect(await call("echo", `{"text":""}`)).toContain(
|
||||
"Wrong arguments for echo",
|
||||
);
|
||||
expect(await call("fail", "")).toBe("Error: disk full");
|
||||
});
|
||||
|
||||
test("cuts long results", async () => {
|
||||
const long = "x".repeat(AppData.HarnessToolResultMaxChars + 5);
|
||||
const result = await call("echo", JSON.stringify({ text: long }));
|
||||
|
||||
expect(result).toEndWith("[Cut: 5 more characters.]");
|
||||
});
|
||||
|
||||
test("throws when cancelled", async () => {
|
||||
const controller = new AbortController();
|
||||
const slow: HarnessTool = {
|
||||
...failing,
|
||||
run: async () => {
|
||||
controller.abort();
|
||||
throw new Error("stopped");
|
||||
},
|
||||
};
|
||||
|
||||
const error = await runToolCall({
|
||||
call: { id: "1", name: "fail", arguments: "{}" },
|
||||
tools: [slow],
|
||||
signal: controller.signal,
|
||||
}).catch((e) => e);
|
||||
|
||||
expect(error).toBeInstanceOf(HarnessError);
|
||||
expect(error.code).toBe("aborted");
|
||||
});
|
||||
});
|
||||
|
||||
describe("toolDefinition", () => {
|
||||
test("describes the arguments as JSON schema", () => {
|
||||
expect(toolDefinition({ tool: echo })).toEqual({
|
||||
name: "echo",
|
||||
description: "Says it back.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: { text: { type: "string", minLength: 1 } },
|
||||
required: ["text"],
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,61 @@
|
||||
import { z } from "zod";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import HarnessError from "../harness-error";
|
||||
import type { HarnessTool, HarnessToolCall } from "../types";
|
||||
|
||||
type Params = {
|
||||
call: HarnessToolCall;
|
||||
tools: HarnessTool[];
|
||||
signal: AbortSignal;
|
||||
};
|
||||
|
||||
/**
|
||||
* Runs one tool call and returns the text the model gets back. Unknown
|
||||
* tools, bad arguments and tool failures come back as `Error: ...` so the
|
||||
* model can correct itself; only cancellation is thrown.
|
||||
*/
|
||||
export default async function runToolCall({ call, tools, signal }: Params) {
|
||||
const tool = tools.find((t) => t.name == call.name);
|
||||
|
||||
if (!tool) {
|
||||
return `Error: There is no tool called "${call.name}". Available: ${tools.map((t) => t.name).join(", ") || "none"}.`;
|
||||
}
|
||||
|
||||
let raw: unknown;
|
||||
|
||||
try {
|
||||
raw = call.arguments.trim() ? JSON.parse(call.arguments) : {};
|
||||
} catch {
|
||||
return `Error: The arguments for ${call.name} weren't valid JSON. Send a JSON object.`;
|
||||
}
|
||||
|
||||
if (!raw || typeof raw != "object" || Array.isArray(raw)) {
|
||||
return `Error: The arguments for ${call.name} must be a JSON object.`;
|
||||
}
|
||||
|
||||
const parsed = tool.schema?.safeParse(raw);
|
||||
|
||||
if (parsed && !parsed.success) {
|
||||
return `Error: Wrong arguments for ${call.name}.\n${z.prettifyError(parsed.error)}`;
|
||||
}
|
||||
|
||||
try {
|
||||
return truncate({
|
||||
text: await tool.run({ args: parsed ? parsed.data : raw, signal }),
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
if (signal.aborted) {
|
||||
throw new HarnessError({ message: `Cancelled.`, code: "aborted" });
|
||||
}
|
||||
|
||||
return `Error: ${error instanceof Error ? error.message : String(error)}`;
|
||||
}
|
||||
}
|
||||
|
||||
function truncate({ text }: { text: string }) {
|
||||
const max = AppData.HarnessToolResultMaxChars;
|
||||
|
||||
if (text.length <= max) return text;
|
||||
|
||||
return `${text.slice(0, max)}\n[Cut: ${text.length - max} more characters.]`;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { z } from "zod";
|
||||
import type { HarnessTool, HarnessToolDefinition } from "../types";
|
||||
|
||||
type Params = {
|
||||
tool: HarnessTool;
|
||||
};
|
||||
|
||||
/**
|
||||
* What the model sees of a tool: its name, description and arguments as
|
||||
* JSON schema (draft 7, the one providers accept most widely).
|
||||
*/
|
||||
export default function toolDefinition({ tool }: Params): HarnessToolDefinition {
|
||||
const { $schema, ...parameters } = (
|
||||
tool.schema
|
||||
? z.toJSONSchema(tool.schema, { target: "draft-7", io: "input" })
|
||||
: tool.parameters || { type: "object", properties: {} }
|
||||
) as Record<string, unknown>;
|
||||
|
||||
return {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user