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,13 +1,14 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import { AppData } from "@/src/data/app-data";
import grabUserBot from "@/src/functions/backend/db/bots/grab-user-bot";
import deleteMedia from "@/src/functions/backend/db/media/delete-media";
import uploadAndRecordMedia from "@/src/functions/backend/db/users/media/upload-and-record-media";
import grabUserWorkspace from "@/src/functions/backend/db/workspaces/grab-user-workspace";
import type { ProfileImageTarget } from "@/src/schemas/profile-image-schema";
import AppError from "@/src/utils/app-error";
import bufferFromBase64 from "@/src/utils/buffer-from-base-64";
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
import { PictureParadigms } from "./copy-media-as-picture";
import removeReplacedPicture from "./remove-replaced-picture";
type Params = {
user_id: number | string;
@@ -19,14 +20,19 @@ type Params = {
data_url: string | null;
};
const Targets = {
bot: { table: "bots", paradigm: "bot-image" },
workspace: { table: "workspaces", paradigm: "workspace-image" },
} as const;
const Tables: Record<ProfileImageTarget, string> = {
bot: "bots",
workspace: "workspaces",
};
/**
* Sets (or removes) the picture of one of the user's bots or workspaces:
* a private image, the old one deleted. Returns the new `image_media_id`.
* a private image, the old picture deleted. Returns the new
* `image_media_id`.
*
* The picture picked here is copied into its own row
* (`PictureParadigms`), and only a picture is deleted when it is replaced
* (`removeReplacedPicture`), never a row a chat message still attaches.
*/
export default async function setProfileImage({ user_id, target, id, data_url }: Params) {
await assertCrewManager({ user_id });
@@ -40,7 +46,7 @@ export default async function setProfileImage({ user_id, target, id, data_url }:
throw new AppError(target == "bot" ? `Bot not found.` : `Workspace not found.`);
}
const { table, paradigm } = Targets[target];
const table = Tables[target];
let image_media_id: number | null = null;
if (data_url) {
@@ -59,8 +65,12 @@ export default async function setProfileImage({ user_id, target, id, data_url }:
const res = await uploadAndRecordMedia({
data: buffer,
media_type: "image",
/**
* The image's own format: a transparent PNG stays a PNG
* (`imageOutputFormat`).
*/
media_mime_type: ext,
media_paradigms: [paradigm],
media_paradigms: [PictureParadigms[target]],
media: { title: `${record.name || target} picture` },
user: { id: Number(user_id) },
});
@@ -77,9 +87,12 @@ export default async function setProfileImage({ user_id, target, id, data_url }:
values: [image_media_id, Date.now(), record.id],
});
if (record.image_media_id) {
await deleteMedia({ id: record.image_media_id, user_id: Number(user_id) }).catch(() => {});
}
await removeReplacedPicture({
previous: record.image_media_id,
next: image_media_id,
user_id,
target,
});
return { image_media_id };
}