Give workspace computers to attached bots in their own chat

A computer linked to a workspace now shows in the composer (and is usable
by tasks and replies) when chatting with any bot attached to that
workspace, not only in the workspace conversation. Members see those
computers through their assigned workers too, and assign_computer still
describes direct grants only.
This commit is contained in:
2026-10-19 20:03:38 +01:00
parent f57b464e80
commit b0c9c84b2b
6 changed files with 52 additions and 17 deletions
@@ -8,20 +8,28 @@ type Params = {
* workspace's, the default first, and nothing else.
*/
workspace_id?: number | string | null;
/**
* In its own chat, whether the computers of the workspaces it's
* attached to count too (they do by default). The `assign_computer`
* tool's view of direct access turns them off.
*/
include_workspace_computers?: boolean;
};
/**
* Every computer a bot can use where it's working. In a workspace, the
* workspace's computers (none linked: none at all), its default first. In
* its own chat, the ones it was given directly (`bot_computers`); the
* coordinator can use all of them. There the one it works on now
* (`bots.computer_id`) comes first, else the main one, so `computers[0]`
* is where its tasks run by default. The same for every user it works
* for: callers check the user reaches the bot.
* its own chat, the ones it was given directly (`bot_computers`) and those
* of the workspaces it's attached to, since a workspace's computer is
* every bit its own; the coordinator can use all of them. There the one it
* works on now (`bots.computer_id`) comes first, else the main one, so
* `computers[0]` is where its tasks run by default. The same for every
* user it works for: callers check the user reaches the bot.
*/
export default async function grabBotComputers({
bot_id,
workspace_id,
include_workspace_computers = true,
}: Params): Promise<BUN_SQLITE_COBBLE_COMPUTERS[]> {
if (workspace_id) {
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_COMPUTERS>({
@@ -39,15 +47,26 @@ export default async function grabBotComputers({
return res.payload || [];
}
const workspace_computers_sql = include_workspace_computers
? `OR c.id IN (
SELECT wc.computer_id FROM workspace_computers wc
JOIN workspaces w ON w.id = wc.workspace_id AND w.archived = 0
JOIN workspace_bots wb ON wb.workspace_id = w.id AND wb.bot_id = ?
)`
: "";
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_COMPUTERS>({
sql: `SELECT c.* FROM computers c
WHERE (
(SELECT kind FROM bots WHERE id = ?) = 'coordinator'
OR c.id IN (SELECT computer_id FROM bot_computers WHERE bot_id = ?)
${workspace_computers_sql}
)
ORDER BY c.id = (SELECT computer_id FROM bots WHERE id = ?) DESC,
c.kind = 'main' DESC, c.id ASC`,
values: [bot_id, bot_id, bot_id],
values: include_workspace_computers
? [bot_id, bot_id, bot_id, bot_id]
: [bot_id, bot_id, bot_id],
});
return res.payload || [];