Files
cobble/src/functions/backend/db/profile-images/grab-picture-source.ts
T
tben f2802c1bf3 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.
2026-10-23 05:24:55 +01:00

65 lines
1.9 KiB
TypeScript

import type { BUN_SQLITE_COBBLE_MEDIA } from "@/db/types/db";
import grabBotFile from "@/src/functions/backend/db/bot-files/grab-bot-file";
import type { StorageScope } from "@/src/utils/storage-access";
import AppError from "@/src/utils/app-error";
import grabOwnPictureMedia from "./grab-own-picture-media";
type Params = {
user_id: number | string;
/**
* The directories the run may read (`grabStorageScopes`), so a file id
* outside them is refused here rather than by the caller.
*/
scopes?: StorageScope[];
/**
* A file the run can see: its `[file:ID]` ref, which is what every file
* tool answers with.
*/
file_id?: number | null;
/**
* An upload of the user's own, by media id.
*/
media_id?: number | null;
};
/**
* The image a picture is copied from, checked to be one the user may use:
* a file in one of the run's directories (`grabBotFile` decides that, the
* same gate every other file tool uses) or their own upload
* (`grabOwnPictureMedia`).
*
* Exactly one of `file_id` and `media_id` is needed. Null when neither is
* given; an id that isn't reachable throws, so a tool never quietly sets
* someone else's file as a picture.
*/
export default async function grabPictureSource({
user_id,
scopes = [],
file_id,
media_id,
}: Params): Promise<BUN_SQLITE_COBBLE_MEDIA | null> {
if (file_id) {
const file = await grabBotFile({ scopes, file_id });
if (!file?.media?.id) {
throw new AppError(
`No file with that id is in your files. Use list_files to see what you have.`,
);
}
return file.media;
}
if (media_id) {
const media = await grabOwnPictureMedia({ media_id, user_id });
if (!media?.id) {
throw new AppError(`That picture isn't one of yours. Upload it again.`);
}
return media;
}
return null;
}