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:
@@ -1,4 +1,5 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BotJobKind } from "@/src/dict/bot-queue-dict";
|
||||
import type {
|
||||
BUN_SQLITE_COBBLE_BOTS_QUEUE,
|
||||
BunSQLiteTables,
|
||||
@@ -11,21 +12,43 @@ type Params = {
|
||||
*/
|
||||
worker: string;
|
||||
limit?: number;
|
||||
/**
|
||||
* Only jobs of this kind, so replies and tasks get their own slots.
|
||||
*/
|
||||
kind: BotJobKind;
|
||||
};
|
||||
|
||||
/**
|
||||
* Claims the next pending jobs for a queue handler. Jobs whose handler
|
||||
* stopped sending updates are released back to the queue first, so a lost
|
||||
* job is picked up again (resume from its `checkpoint`).
|
||||
* Claims the next pending jobs of one kind for a queue handler. Jobs
|
||||
* whose handler stopped sending updates are released back to the queue
|
||||
* first, so a lost job is picked up again (resume from its `checkpoint`).
|
||||
*
|
||||
* bun-sqlite's `queue.claim` takes no filter, so the claim is its same
|
||||
* single statement (atomic: two workers never claim one job) plus `kind`.
|
||||
*/
|
||||
export default async function claimBotJobs({ worker, limit = 1 }: Params) {
|
||||
const res = await BunSQLite.queue.claim<
|
||||
export default async function claimBotJobs({ worker, limit = 1, kind }: Params) {
|
||||
const released = await BunSQLite.queue.release_stale<
|
||||
BUN_SQLITE_COBBLE_BOTS_QUEUE,
|
||||
(typeof BunSQLiteTables)[number]
|
||||
>({
|
||||
table: "bots_queue",
|
||||
worker,
|
||||
limit,
|
||||
>({ table: "bots_queue" });
|
||||
|
||||
if (!released.success) {
|
||||
throw new Error(`Couldn't release stale jobs: ${released.msg || released.error}`);
|
||||
}
|
||||
|
||||
if (limit < 1) return [];
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS_QUEUE>({
|
||||
sql: `UPDATE bots_queue SET status = 'processing', attempts = attempts + 1,
|
||||
locked_at = ?, locked_by = ?, updated_at = ?
|
||||
WHERE id IN (
|
||||
SELECT id FROM bots_queue
|
||||
WHERE status = 'pending' AND available_at <= ? AND kind = ?
|
||||
ORDER BY id LIMIT ?
|
||||
) RETURNING *`,
|
||||
values: [now, worker, now, now, kind, Math.floor(limit)],
|
||||
});
|
||||
|
||||
if (!res.success) {
|
||||
|
||||
@@ -17,12 +17,17 @@ type Params = {
|
||||
* Flag the message as needing the user.
|
||||
*/
|
||||
needs_you?: boolean;
|
||||
/**
|
||||
* Finish without posting a message.
|
||||
*/
|
||||
silent?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Marks a job done, closes its task and sends the completion message back
|
||||
* to the user in the bot's conversation. The bot shows done (or needs-you)
|
||||
* unless it has other jobs open. Throws if this handler no longer holds
|
||||
* to the user in the bot's conversation. Unless it has other jobs open,
|
||||
* the bot shows needs-you if asked, else done after a task and idle after
|
||||
* a reply. Throws if this handler no longer holds
|
||||
* the job.
|
||||
*/
|
||||
export default async function completeBotJob({
|
||||
@@ -30,6 +35,7 @@ export default async function completeBotJob({
|
||||
worker,
|
||||
result,
|
||||
needs_you,
|
||||
silent,
|
||||
}: Params) {
|
||||
const job = await grabLockedBotJob({ job_id, worker });
|
||||
|
||||
@@ -60,11 +66,13 @@ export default async function completeBotJob({
|
||||
});
|
||||
}
|
||||
|
||||
const message = await insertBotMessage({
|
||||
bot_id: job.bot_id,
|
||||
text: result,
|
||||
needs_you,
|
||||
});
|
||||
const message = silent
|
||||
? null
|
||||
: await insertBotMessage({
|
||||
bot_id: job.bot_id,
|
||||
text: result,
|
||||
needs_you,
|
||||
});
|
||||
|
||||
const bot = needs_you
|
||||
? await setBotFace({
|
||||
@@ -74,7 +82,10 @@ export default async function completeBotJob({
|
||||
})
|
||||
: (await countOpenBotJobs({ bot_id: job.bot_id }))
|
||||
? null
|
||||
: await setBotFace({ bot_id: job.bot_id, face: "done" });
|
||||
: await setBotFace({
|
||||
bot_id: job.bot_id,
|
||||
face: job.kind == "reply" ? "idle" : "done",
|
||||
});
|
||||
|
||||
return { message, bot };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
|
||||
type Params = {
|
||||
job_id: number | string;
|
||||
worker: string;
|
||||
/**
|
||||
* Unix ms before which the job can't be claimed again.
|
||||
*/
|
||||
until: number;
|
||||
/**
|
||||
* Give back the attempt this claim used: nothing was tried.
|
||||
*/
|
||||
refund?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Holds a job back without failing it: a pending job whose `available_at`
|
||||
* is pushed to `until` (capped at `AppData.HarnessMaxCooldownMs` from
|
||||
* now), or, for a job still held by `worker`, released back to pending
|
||||
* that way. False when the job is neither.
|
||||
*/
|
||||
export default async function deferBotJob({ job_id, worker, until, refund }: Params) {
|
||||
const now = Date.now();
|
||||
const at = Math.min(Math.max(until, now), now + AppData.HarnessMaxCooldownMs);
|
||||
|
||||
const res = await BunSQLite.sql({
|
||||
sql: `UPDATE bots_queue SET status = 'pending', locked_at = NULL, locked_by = NULL,
|
||||
attempts = CASE WHEN ? AND status = 'processing' THEN MAX(attempts - 1, 0) ELSE attempts END,
|
||||
available_at = MAX(COALESCE(available_at, 0), ?), updated_at = ?
|
||||
WHERE id = ? AND (status = 'pending' OR (status = 'processing' AND locked_by = ?))
|
||||
RETURNING id`,
|
||||
values: [refund ? 1 : 0, at, now, job_id, worker],
|
||||
});
|
||||
|
||||
return Boolean(res.payload?.length);
|
||||
}
|
||||
@@ -4,11 +4,17 @@ import type {
|
||||
BUN_SQLITE_COBBLE_BOTS_QUEUE,
|
||||
BunSQLiteTables,
|
||||
} from "@/db/types/db";
|
||||
import type { BotJobKind } from "@/src/dict/bot-queue-dict";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import grabUserBot from "../bots/grab-user-bot";
|
||||
import grabBotComputers from "../computers/grab-bot-computers";
|
||||
|
||||
type Params = {
|
||||
/**
|
||||
* `task` (the default) shows in the working-on panel; a `reply` answers
|
||||
* the user in chat and has no task.
|
||||
*/
|
||||
kind?: BotJobKind;
|
||||
user_id: number | string;
|
||||
bot_id: number | string;
|
||||
title: string;
|
||||
@@ -24,7 +30,7 @@ type Params = {
|
||||
message_id?: number | string;
|
||||
/**
|
||||
* The task for the working-on panel. One is created from `title` when
|
||||
* not given.
|
||||
* not given (replies have none).
|
||||
*/
|
||||
task_id?: number | string;
|
||||
/**
|
||||
@@ -44,6 +50,7 @@ type Params = {
|
||||
* every message needs a job; quick answers are replied to directly.
|
||||
*/
|
||||
export default async function enqueueBotJob({
|
||||
kind = "task",
|
||||
user_id,
|
||||
bot_id,
|
||||
title,
|
||||
@@ -76,7 +83,10 @@ export default async function enqueueBotJob({
|
||||
}
|
||||
}
|
||||
|
||||
const final_task_id = task_id || (await createJobTask({ bot_id: bot.id, title }));
|
||||
const final_task_id =
|
||||
kind == "reply"
|
||||
? undefined
|
||||
: task_id || (await createJobTask({ bot_id: bot.id, title }));
|
||||
|
||||
const res = await BunSQLite.queue.add<
|
||||
BUN_SQLITE_COBBLE_BOTS_QUEUE,
|
||||
@@ -85,12 +95,13 @@ export default async function enqueueBotJob({
|
||||
table: "bots_queue",
|
||||
data: [
|
||||
{
|
||||
kind,
|
||||
bot_id: bot.id,
|
||||
title,
|
||||
instructions,
|
||||
params: params ? JSON.stringify(params) : undefined,
|
||||
message_id: message_id ? Number(message_id) : undefined,
|
||||
task_id: Number(final_task_id),
|
||||
task_id: final_task_id ? Number(final_task_id) : undefined,
|
||||
computer_id: computer_id ? Number(computer_id) : undefined,
|
||||
},
|
||||
],
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_COBBLE_BOTS_QUEUE } from "@/db/types/db";
|
||||
import enqueueBotJob from "./enqueue-bot-job";
|
||||
import { BotReplyJobTitle } from "@/src/dict/bot-queue-dict";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
bot_id: number | string;
|
||||
/**
|
||||
* The user message to answer.
|
||||
*/
|
||||
message_id: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Queues the bot's chat reply to the user's latest message(s). A reply
|
||||
* reads the whole recent conversation when it runs, so a burst of
|
||||
* messages shares one reply: while one is still pending, it is reused.
|
||||
*/
|
||||
export default async function enqueueBotReply({
|
||||
user_id,
|
||||
bot_id,
|
||||
message_id,
|
||||
}: Params) {
|
||||
const pending = (
|
||||
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS_QUEUE>({
|
||||
sql: `SELECT id FROM bots_queue
|
||||
WHERE bot_id = ? AND kind = 'reply' AND status = 'pending'
|
||||
ORDER BY id DESC LIMIT 1`,
|
||||
values: [bot_id],
|
||||
})
|
||||
).payload?.[0];
|
||||
|
||||
if (pending?.id) {
|
||||
await BunSQLite.sql({
|
||||
sql: `UPDATE bots_queue SET message_id = ? WHERE id = ?`,
|
||||
values: [message_id, pending.id],
|
||||
});
|
||||
|
||||
return pending.id;
|
||||
}
|
||||
|
||||
return await enqueueBotJob({
|
||||
kind: "reply",
|
||||
user_id,
|
||||
bot_id,
|
||||
title: BotReplyJobTitle,
|
||||
instructions: "Reply to the user's latest messages.",
|
||||
message_id,
|
||||
});
|
||||
}
|
||||
@@ -30,7 +30,12 @@ export default async function notifyBotJobFailed({
|
||||
bot_id: job.bot_id,
|
||||
text:
|
||||
failure_message ||
|
||||
[`I couldn't finish "${job.title || "the job"}".`, job.last_error]
|
||||
[
|
||||
job.kind == "reply"
|
||||
? `I couldn't reply.`
|
||||
: `I couldn't finish "${job.title || "the job"}".`,
|
||||
job.last_error,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" "),
|
||||
needs_you: true,
|
||||
|
||||
Reference in New Issue
Block a user