Let bots set a picture from a file they can see

A coordinator asked to set an uploaded logo as a workspace picture could
not: the picture tools take a media id, and nothing ever gave the bot one.
Attachments reached the model as "[Attached image \"logo.png\"]", every
file tool answers with a [file:ID] ref, and an SVG is deliberately stored
as a document rather than an image.

- copyMediaAsPicture copies an approved image into a new picture row
  (bot-image/workspace-image). The source keeps its row and bytes, so a
  message that attached it still loads and changing a picture can never
  take a file away from a conversation.
- grabPictureSource resolves a picture by [file:ID] (the same scope gate
  every file tool uses) or by the user's own upload; parseFileRef reads
  the [file:12] form.
- The new set_picture tool ships on every run: a bot sets its own face,
  the coordinator can also dress a worker. update_workspace gained
  picture_file_id, and setBotPicture/setProfileImage share the copy and
  the delete.
- removeReplacedPicture deletes a replaced picture only when it really is
  one, never a chat upload.
- uploadAndRecordMedia writes images in their own format: PNG stays PNG
  with its transparency, WebP stays WebP, GIF becomes PNG, everything
  else JPEG, and the row records the real mime type (it used to say jpeg
  for every image).
- Pictures change live: BotUpdatedData carries image_media_id and a new
  server:workspace-updated pushes workspace pictures, relayed between
  processes.
- Attachments are labelled with their media id, and BotFilesNote tells a
  bot that a [file:ID] can become a picture.
- grabRunScopes is now the single source of a run's reachable folders,
  shared by the file tools, imports and the picture tools.
This commit is contained in:
2026-10-23 05:24:55 +01:00
parent d3357cb7df
commit f2802c1bf3
42 changed files with 1391 additions and 113 deletions
@@ -1,5 +1,6 @@
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
import grabUserWorkspaces from "@/src/functions/backend/db/workspaces/grab-user-workspaces";
import grabRunScopes from "@/src/functions/backend/db/bot-files/grab-run-scopes";
import type { StorageScope } from "@/src/utils/storage-access";
import type { HarnessTool } from "../../types";
import generateImageTool from "./generate-image-tool";
import grabStorageTools from "./grab-storage-tools";
@@ -11,6 +12,11 @@ type Params = {
user_id: number | string;
workspace_id?: number | string | null;
session_id?: number | string;
/**
* The files the run reaches, when the caller already read them
* (`grabRunScopes`); read here otherwise.
*/
scopes?: StorageScope[];
/**
* Called for a file the bot wrote with "attach" set, so the reply
* carries it.
@@ -30,19 +36,15 @@ export default async function grabBotFileTools({
user_id,
workspace_id,
session_id,
scopes,
onAttach,
}: Params): Promise<HarnessTool[]> {
const all_workspace_ids =
bot.kind == "coordinator"
? (await grabUserWorkspaces({ user_id })).filter((w) => !w.archived).map((w) => Number(w.id))
: [];
const context = storageToolContext({
bot,
user_id,
workspace_id,
session_id,
all_workspace_ids,
scopes: scopes || (await grabRunScopes({ bot, user_id, workspace_id })),
onAttach,
});
@@ -1,5 +1,5 @@
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
import grabStorageScopes, { type StorageScope } from "@/src/utils/storage-access";
import type { StorageScope } from "@/src/utils/storage-access";
export type StorageToolContext = {
/**
@@ -32,34 +32,29 @@ type Params = {
workspace_id?: number | string | null;
session_id?: number | string;
/**
* Every workspace, for the coordinator (it reaches them all).
* The directories the run may touch (`grabRunScopes`): the bot's own
* first, then the workspace's or, for the coordinator, every one's.
*/
all_workspace_ids?: number[];
scopes: StorageScope[];
onAttach?: StorageToolContext["onAttach"];
};
/**
* The directories one run may touch, from the bot it's for. The
* coordinator reaches every bot's and workspace's files; a worker its own,
* plus its workspace's shared one while it works there.
* The context every storage tool works from: which bot it is, whose files it
* reaches (`grabRunScopes`, which owns that decision) and where it works.
*/
export default function storageToolContext({
bot,
user_id,
workspace_id,
session_id,
all_workspace_ids,
scopes,
onAttach,
}: Params): StorageToolContext {
return {
user_id,
bot_id: bot.id || 0,
scopes: grabStorageScopes({
bot_id: bot.id || 0,
bot_kind: bot.kind,
workspace_id,
all_workspace_ids,
}),
scopes,
session_id,
workspace_id,
onAttach,