Add computer switching, crew reporting, job steps and markdown chat
- Bots have a current computer (bots.computer_id), switched from the chat header beside the model button or by clicking a computer the bot names ([computer:ID]); tasks run on it by default - search_computers runs one read-only command on all of a bot's computers; chat replies know their computers (compose-computers-note) - The coordinator can use every computer and grants or revokes crew access with assign_computer - Delegated jobs record requested_by_bot_id; the worker's result or failure is reported back in the coordinator's conversation - bots_queue_updates records each model turn of a job, pushed live and shown in the working-on panel - Live relay: every process pushes new messages, job steps and bot changes from the database to its own sockets, so work done elsewhere shows live - Bot messages render as markdown (react-markdown + remark-gfm, no raw HTML) with twui components; rail previews are plain text - Fix header button overflow in the model and computer selects - Bump @moduletrace/twui to 0.1.2 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import composeComputersNote from "./compose-computers-note";
|
||||
|
||||
describe("composeComputersNote", () => {
|
||||
test("lists each computer by ref, the current one first", () => {
|
||||
const note = composeComputersNote({
|
||||
computers: [
|
||||
{ id: 4, name: "Scraper", kind: "sandbox", description: "Runs the crawlers" },
|
||||
{ id: 1, name: "Main computer", kind: "main" },
|
||||
],
|
||||
});
|
||||
|
||||
expect(note).toContain(
|
||||
"- [computer:4] Scraper (you're on it): Runs the crawlers\n- [computer:1] Main computer (main)",
|
||||
);
|
||||
expect(note).toContain("search_computers");
|
||||
expect(note).toContain("start_task");
|
||||
});
|
||||
|
||||
test("tells the coordinator it can use and assign every computer", () => {
|
||||
const computers = [{ id: 1, name: "Main computer", kind: "main" as const }];
|
||||
|
||||
expect(composeComputersNote({ computers, is_coordinator: true })).toContain("assign_computer");
|
||||
expect(composeComputersNote({ computers })).not.toContain("assign_computer");
|
||||
});
|
||||
|
||||
test("says so when the bot has no computer", () => {
|
||||
expect(composeComputersNote({ computers: [] })).toContain("You have no computer");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import type { BUN_SQLITE_COBBLE_COMPUTERS } from "@/db/types/db";
|
||||
import computerRef from "@/src/utils/computer-ref";
|
||||
|
||||
type Params = {
|
||||
/**
|
||||
* The bot's computers, the current one first (`grabBotComputers`).
|
||||
*/
|
||||
computers: BUN_SQLITE_COBBLE_COMPUTERS[];
|
||||
/**
|
||||
* The coordinator can use every computer and give the crew access.
|
||||
*/
|
||||
is_coordinator?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Added to a bot's chat prompt so it knows which computers it has, which
|
||||
* one it's on, and how it reaches them (markdown). Without it a bot asked
|
||||
* "can you see the computer?" says it can't.
|
||||
*/
|
||||
export default function composeComputersNote({ computers, is_coordinator }: Params) {
|
||||
if (!computers.length) {
|
||||
return `# Your computers\n\nYou have no computer, so you can't run commands, open files or browse. If the user asks for that, say so.`;
|
||||
}
|
||||
|
||||
const listed = computers
|
||||
.map(
|
||||
(computer, index) =>
|
||||
`- ${computerRef({ id: computer.id || 0 })} ${computer.name || "Computer"}${
|
||||
[index == 0 ? "you're on it" : "", computer.kind == "main" ? "main" : ""]
|
||||
.filter(Boolean)
|
||||
.map((tag) => ` (${tag})`)
|
||||
.join("")
|
||||
}${computer.description ? `: ${computer.description}` : ""}`,
|
||||
)
|
||||
.join("\n");
|
||||
|
||||
return `# Your computers
|
||||
|
||||
${listed}
|
||||
|
||||
Each is a Linux sandbox of your own (a shell, files, code runners and a browser), set up for the crew. It isn't the user's desktop: you can't see their screen or their own files unless they're put on it. The user picks which one you're on; your tasks run there.${
|
||||
is_coordinator
|
||||
? ` As the coordinator you can use every computer the user has, and you decide which ones each crew member can use with assign_computer.`
|
||||
: ""
|
||||
}
|
||||
|
||||
- To find which computer holds something (a file, a running app, a logged-in session), use search_computers: it runs one read-only command on all of them at once.
|
||||
- For anything else on a computer, start_task: the task runs on it with its tools and you post what you found here. Don't say you can't see a computer; search it or start a task.
|
||||
- When you name a computer, write its ref exactly, e.g. ${computerRef({ id: computers[0]!.id || 0 })}. The user sees it as a button that switches you to that computer, so when they're looking for one, say which and that they can click it.`;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import type { HarnessCandidate } from "@/src/harness/types";
|
||||
import describeJobStep from "./describe-job-step";
|
||||
|
||||
const candidate = { model: { slug: "deepseek/deepseek-chat:free" } } as HarnessCandidate;
|
||||
|
||||
describe("describeJobStep", () => {
|
||||
test("names each tool once, never its arguments", () => {
|
||||
expect(
|
||||
describeJobStep({
|
||||
index: 2,
|
||||
text: "Let me look.",
|
||||
candidate,
|
||||
tool_calls: [
|
||||
{ id: "a", name: "sandbox_execute_bash", arguments: '{"command":"cat secret"}' },
|
||||
{ id: "b", name: "sandbox_execute_bash", arguments: "{}" },
|
||||
{ id: "c", name: "update_memory", arguments: "{}" },
|
||||
],
|
||||
}),
|
||||
).toEqual({
|
||||
kind: "tool",
|
||||
text: "Using sandbox_execute_bash, update_memory",
|
||||
metadata: {
|
||||
step: 2,
|
||||
model: "deepseek/deepseek-chat:free",
|
||||
tools: ["sandbox_execute_bash", "update_memory"],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test("keeps the first line of a text-only turn, cut to one line", () => {
|
||||
const step = describeJobStep({
|
||||
index: 0,
|
||||
text: `\n ${"a".repeat(250)}\nSecond line`,
|
||||
candidate,
|
||||
tool_calls: [],
|
||||
});
|
||||
|
||||
expect(step?.kind).toBe("note");
|
||||
expect(step?.text).toHaveLength(200);
|
||||
expect(step?.text.endsWith("…")).toBe(true);
|
||||
});
|
||||
|
||||
test("skips an empty turn", () => {
|
||||
expect(describeJobStep({ index: 0, text: " ", candidate, tool_calls: [] })).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { BotJobUpdateKind } from "@/src/dict/bot-queue-dict";
|
||||
import type { TextLoopStep } from "@/src/harness/text/run-text-loop";
|
||||
|
||||
/**
|
||||
* Longest step line kept: the panel shows one line per step.
|
||||
*/
|
||||
const MaxStepChars = 200;
|
||||
|
||||
/**
|
||||
* One model turn as a `bots_queue_updates` row: the tools it called, or
|
||||
* the first line of what it wrote. Null for an empty turn. Never the tool
|
||||
* arguments, which can hold anything the bot typed.
|
||||
*/
|
||||
export default function describeJobStep({
|
||||
text,
|
||||
tool_calls,
|
||||
index,
|
||||
candidate,
|
||||
}: TextLoopStep): {
|
||||
kind: BotJobUpdateKind;
|
||||
text: string;
|
||||
metadata: Record<string, unknown>;
|
||||
} | null {
|
||||
const metadata = { step: index, model: candidate.model.slug };
|
||||
|
||||
if (tool_calls.length) {
|
||||
const tools = [...new Set(tool_calls.map((call) => call.name))];
|
||||
|
||||
return {
|
||||
kind: "tool",
|
||||
text: `Using ${tools.join(", ")}`,
|
||||
metadata: { ...metadata, tools },
|
||||
};
|
||||
}
|
||||
|
||||
const line = text?.trim().split("\n").find((l) => l.trim())?.trim();
|
||||
|
||||
if (!line) return null;
|
||||
|
||||
return {
|
||||
kind: "note",
|
||||
text: line.length > MaxStepChars ? `${line.slice(0, MaxStepChars - 1)}…` : line,
|
||||
metadata,
|
||||
};
|
||||
}
|
||||
@@ -4,15 +4,19 @@ import setBotFace from "@/src/functions/backend/db/bots/set-bot-face";
|
||||
import completeBotJob from "@/src/functions/backend/db/bots-queue/complete-bot-job";
|
||||
import failBotJob from "@/src/functions/backend/db/bots-queue/fail-bot-job";
|
||||
import updateBotJob from "@/src/functions/backend/db/bots-queue/update-bot-job";
|
||||
import insertBotJobUpdate from "@/src/functions/backend/db/bots-queue/insert-bot-job-update";
|
||||
import { logError } from "@/src/functions/backend/logger";
|
||||
import createKeyLock from "@/src/utils/create-key-lock";
|
||||
import HarnessError from "@/src/harness/harness-error";
|
||||
import deferBotJob from "@/src/functions/backend/db/bots-queue/defer-bot-job";
|
||||
import { BotWaitingStatusLine } from "@/src/dict/bot-queue-dict";
|
||||
import BotJobFatalError from "./bot-job-fatal-error";
|
||||
import describeJobStep from "./describe-job-step";
|
||||
import pushToBotOwner from "./push-to-bot-owner";
|
||||
import pushBotNotice from "./push-bot-notice";
|
||||
import runBotJob from "./run-bot-job";
|
||||
import parseJobJson from "./parse-job-json";
|
||||
import type { BotJobUpdateData } from "@/src/types";
|
||||
import type { BotJobContext, BotJobRunner } from "./types";
|
||||
|
||||
/**
|
||||
@@ -66,6 +70,28 @@ export default async function processBotJob({
|
||||
return held;
|
||||
};
|
||||
|
||||
const report: BotJobContext["report"] = async (step) => {
|
||||
const described = describeJobStep(step);
|
||||
|
||||
if (!described || controller.signal.aborted) return;
|
||||
|
||||
try {
|
||||
if (!(await update({ progress: described.text }))) return;
|
||||
|
||||
const row = await insertBotJobUpdate({ job_id, bot_id, ...described });
|
||||
|
||||
await pushToBotOwner({
|
||||
bot_id,
|
||||
data: {
|
||||
event: "server:bot-job-update",
|
||||
data: { ...row, task_id: job.task_id || null } satisfies BotJobUpdateData,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
await logError({ title: `Bot job #${job_id} step not saved`, error }).catch(() => {});
|
||||
}
|
||||
};
|
||||
|
||||
const heartbeat = setInterval(() => {
|
||||
update({}).catch(() => {});
|
||||
}, HEARTBEAT_MS);
|
||||
@@ -91,6 +117,7 @@ export default async function processBotJob({
|
||||
checkpoint: parseJobJson({ text: job.checkpoint }),
|
||||
signal: controller.signal,
|
||||
update,
|
||||
report,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -8,14 +8,20 @@ import pushToBotOwner from "./push-to-bot-owner";
|
||||
type Params = {
|
||||
bot_id: number | string;
|
||||
message?: BUN_SQLITE_COBBLE_BOT_MESSAGES | null;
|
||||
bot?: Pick<BUN_SQLITE_COBBLE_BOTS, "id" | "face" | "status_line"> | null;
|
||||
bot?: Pick<BUN_SQLITE_COBBLE_BOTS, "id" | "face" | "status_line" | "computer_id"> | null;
|
||||
/**
|
||||
* A job's result reported to the bot that handed it over
|
||||
* (`relayJobToRequester`): a message in that bot's conversation.
|
||||
*/
|
||||
relay?: { bot_id: number; message: BUN_SQLITE_COBBLE_BOT_MESSAGES } | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Pushes a new bot message and/or face change to the owner's open tabs.
|
||||
* A face change carries the bot's active task, for the working-on panel.
|
||||
* A relayed report goes to the conversation it was posted in.
|
||||
*/
|
||||
export default async function pushBotNotice({ bot_id, message, bot }: Params) {
|
||||
export default async function pushBotNotice({ bot_id, message, bot, relay }: Params) {
|
||||
if (message) {
|
||||
await pushToBotOwner({
|
||||
bot_id,
|
||||
@@ -23,6 +29,13 @@ export default async function pushBotNotice({ bot_id, message, bot }: Params) {
|
||||
});
|
||||
}
|
||||
|
||||
if (relay) {
|
||||
await pushToBotOwner({
|
||||
bot_id: relay.bot_id,
|
||||
data: { event: "server:bot-message", data: relay.message },
|
||||
});
|
||||
}
|
||||
|
||||
if (bot) {
|
||||
await pushToBotOwner({
|
||||
bot_id,
|
||||
|
||||
@@ -3,9 +3,11 @@ import { AppData } from "@/src/data/app-data";
|
||||
import { ChatReplyNote, CoordinatorReplyNote } from "@/src/data/harness-prompts";
|
||||
import grabBotMessages from "@/src/functions/backend/db/bots/grab-bot-messages";
|
||||
import grabBotPrompt from "@/src/functions/backend/db/bots/grab-bot-prompt";
|
||||
import grabBotComputers from "@/src/functions/backend/db/computers/grab-bot-computers";
|
||||
import runTextLoop from "@/src/harness/text/run-text-loop";
|
||||
import grabBotChatTools from "@/src/harness/tools/bot/grab-bot-chat-tools";
|
||||
import botMessagesToHarness from "./bot-messages-to-harness";
|
||||
import composeComputersNote from "./compose-computers-note";
|
||||
import loadAttachmentParts from "./load-attachment-parts";
|
||||
import type { BotJobContext, BotJobOutcome } from "./types";
|
||||
|
||||
@@ -16,7 +18,7 @@ type Params = BotJobContext & {
|
||||
/**
|
||||
* Answers the user in chat: the bot reads the recent conversation (and
|
||||
* the files of the latest messages, `loadAttachmentParts`) and
|
||||
* replies, with its chat tools (memory, instructions, start_task, and for
|
||||
* replies, knowing its computers (`composeComputersNote`), with its chat tools (memory, instructions, start_task, and for
|
||||
* the coordinator delegate_task). Finishes silently when the last message
|
||||
* is already the bot's, i.e. an earlier reply answered everything.
|
||||
*/
|
||||
@@ -24,6 +26,7 @@ export default async function runBotReply({
|
||||
bot,
|
||||
job,
|
||||
signal,
|
||||
report,
|
||||
}: Params): Promise<BotJobOutcome> {
|
||||
const history = await grabBotMessages({
|
||||
bot_id: bot.id,
|
||||
@@ -35,11 +38,13 @@ export default async function runBotReply({
|
||||
}
|
||||
|
||||
const prompt = await grabBotPrompt({ bot_id: bot.id, user_id: bot.user_id });
|
||||
const computers = await grabBotComputers({ bot_id: bot.id, user_id: bot.user_id });
|
||||
|
||||
const result = await runTextLoop({
|
||||
system: [
|
||||
prompt,
|
||||
ChatReplyNote,
|
||||
composeComputersNote({ computers, is_coordinator: bot.kind == "coordinator" }),
|
||||
bot.kind == "coordinator" ? CoordinatorReplyNote : "",
|
||||
`Now: ${new Date().toUTCString()}.`,
|
||||
]
|
||||
@@ -52,11 +57,13 @@ export default async function runBotReply({
|
||||
tools: await grabBotChatTools({
|
||||
bot,
|
||||
user_id: bot.user_id,
|
||||
computers,
|
||||
message_id: job.message_id || undefined,
|
||||
}),
|
||||
max_steps: AppData.BotReplyMaxToolSteps,
|
||||
bot_id: bot.id,
|
||||
signal,
|
||||
onStep: report,
|
||||
});
|
||||
|
||||
return { result: result.text };
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { TaskRunNote } from "@/src/data/harness-prompts";
|
||||
import grabBotComputers from "@/src/functions/backend/db/computers/grab-bot-computers";
|
||||
import grabBotPrompt from "@/src/functions/backend/db/bots/grab-bot-prompt";
|
||||
import runTextLoop from "@/src/harness/text/run-text-loop";
|
||||
import openComputerTools from "@/src/harness/tools/computer/open-computer-tools";
|
||||
import grabBotTools from "@/src/harness/tools/bot/grab-bot-tools";
|
||||
import searchComputersTool from "@/src/harness/tools/computer/search-computers-tool";
|
||||
import computerRef from "@/src/utils/computer-ref";
|
||||
import composeJobBrief from "./compose-job-brief";
|
||||
import type { BotJobContext, BotJobOutcome } from "./types";
|
||||
|
||||
@@ -12,16 +15,16 @@ type Params = BotJobContext & {
|
||||
|
||||
/**
|
||||
* Works through a task: the bot follows its brief with its full prompt,
|
||||
* its own tools and, when the job has a computer, that computer's tools
|
||||
* (over MCP), until it answers. That answer is the report for the user.
|
||||
* Progress is saved after every model turn.
|
||||
* its own tools, search_computers across all its computers and, when the
|
||||
* job has a computer, that computer's tools (over MCP), until it answers. That answer is the report for the user.
|
||||
* Every model turn is reported as a step (`report`).
|
||||
*/
|
||||
export default async function runBotTask({
|
||||
bot,
|
||||
job,
|
||||
params,
|
||||
signal,
|
||||
update,
|
||||
report,
|
||||
}: Params): Promise<BotJobOutcome> {
|
||||
const computer = job.computer_id
|
||||
? await openComputerTools({ computer_id: job.computer_id, bot_id: bot.id, signal })
|
||||
@@ -29,6 +32,7 @@ export default async function runBotTask({
|
||||
|
||||
try {
|
||||
const prompt = await grabBotPrompt({ bot_id: bot.id, user_id: bot.user_id });
|
||||
const computers = await grabBotComputers({ bot_id: bot.id, user_id: bot.user_id });
|
||||
|
||||
const result = await runTextLoop({
|
||||
system: [
|
||||
@@ -37,6 +41,9 @@ export default async function runBotTask({
|
||||
computer
|
||||
? `You're working on the computer "${computer.name}" (a Linux sandbox with a shell, files, code runners and a browser). Use its tools; don't ask the user to run anything.`
|
||||
: `You have no computer for this task.`,
|
||||
computers.length > 1
|
||||
? `search_computers runs a read-only command on every computer you have. When you name one in the report, write its ref, e.g. ${computerRef({ id: computers[0]!.id || 0 })}: the user can click it to switch you to that computer.`
|
||||
: "",
|
||||
computer?.envs.length
|
||||
? `Environment variables set on this computer (use them by name, e.g. $KEY; never print their values):\n${computer.envs
|
||||
.map((env) => `- ${env.key}${env.description ? `: ${env.description}` : ""}`)
|
||||
@@ -57,17 +64,13 @@ export default async function runBotTask({
|
||||
],
|
||||
tools: [
|
||||
...grabBotTools({ user_id: bot.user_id, bot_id: bot.id }),
|
||||
...(computers.length ? [searchComputersTool({ bot_id: bot.id, computers })] : []),
|
||||
...(computer?.tools || []),
|
||||
],
|
||||
bot_id: bot.id,
|
||||
signal,
|
||||
onStep: async ({ text, tool_calls }) => {
|
||||
await update({
|
||||
progress: tool_calls.length
|
||||
? `Using ${tool_calls.map((call) => call.name).join(", ")}`
|
||||
: text?.slice(0, 500) || undefined,
|
||||
});
|
||||
},
|
||||
onStep: (step) =>
|
||||
report({ ...step, text: step.text && computer ? computer.redact(step.text) : step.text }),
|
||||
});
|
||||
|
||||
return { result: computer ? computer.redact(result.text) : result.text };
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { BUN_SQLITE_COBBLE_BOTS_QUEUE } from "@/db/types/db";
|
||||
import type { TextLoopStep } from "@/src/harness/text/run-text-loop";
|
||||
|
||||
export type BotJobContext = {
|
||||
job: BUN_SQLITE_COBBLE_BOTS_QUEUE;
|
||||
@@ -21,6 +22,12 @@ export type BotJobContext = {
|
||||
progress?: string;
|
||||
checkpoint?: Record<string, unknown>;
|
||||
}) => Promise<boolean>;
|
||||
/**
|
||||
* Records a model turn as a step (`bots_queue_updates`), saves it as
|
||||
* the job's progress and pushes it live. Pass it as the text loop's
|
||||
* `onStep`. Best effort: a failed write never stops the job.
|
||||
*/
|
||||
report: (step: TextLoopStep) => Promise<void>;
|
||||
};
|
||||
|
||||
export type BotJobOutcome = {
|
||||
|
||||
Reference in New Issue
Block a user