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.
32 lines
859 B
TypeScript
32 lines
859 B
TypeScript
type Params = {
|
|
/**
|
|
* A file ref as a bot writes it (`[file:12]`), an id as text (`"12"`),
|
|
* or a number.
|
|
*/
|
|
ref?: string | number | null;
|
|
};
|
|
|
|
/**
|
|
* The file id a `[file:ID]` ref names, so a tool can take the ref a bot
|
|
* copied out of `list_files` instead of asking it for a bare number.
|
|
* Null when it names nothing usable.
|
|
*/
|
|
export default function parseFileRef({ ref }: Params): number | null {
|
|
if (typeof ref == "number") return Number.isInteger(ref) && ref > 0 ? ref : null;
|
|
|
|
if (typeof ref != "string") return null;
|
|
|
|
const text = ref.trim();
|
|
|
|
if (!text) return null;
|
|
|
|
const marked = text.match(/^\[file:\s*(\d+)\s*\]$/i)?.[1];
|
|
const bare = marked || (/^\d+$/.test(text) ? text : "");
|
|
|
|
if (!bare) return null;
|
|
|
|
const id = Number(bare);
|
|
|
|
return Number.isInteger(id) && id > 0 ? id : null;
|
|
}
|