okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-023-int into f-023-guifcb0c2d0e

main
1.8 KiB32 linesraw
1import { rawUrlOf } from './deliverables';
2
3/**
4 * Which picture addresses a model's Markdown may put in an <img> (tools and images review O3, 2026-10-22).
5 *
6 * An answer can carry `![chart](...)`. It is shown only when the picture is already on this computer or inside the message:
7 *   - a data: address of a PNG, JPEG, WebP or GIF (never SVG: an SVG can carry script and styles);
8 *   - the host's own /api/files/raw address (a file inside the chat's folder, served by the host and by nobody else);
9 *   - a path without a scheme, which is read as a file of the chat's folder through that same address.
10 * Nothing is fetched from the internet by default (an address in the answer would tell its owner that the answer was read, and
11 * which IP read it): an http(s) picture stays a link the person can open. file:, javascript:, blob: and every other scheme show as text.
12 */
13export type ImagePlan =
14  | { kind: 'img'; src: string }
15  | { kind: 'link'; href: string }
16  | { kind: 'text' };
17
18const DATA_IMAGE = /^data:image\/(?:png|jpe?g|webp|gif);base64,[A-Za-z0-9+/]+={0,2}$/i;
19const SCHEME = /^[a-z][a-z0-9+.-]*:/i;
20
21export function imagePlan(src: string, sessionId?: string | null): ImagePlan {
22  const value = src.trim();
23  if (!value) return { kind: 'text' };
24  if (DATA_IMAGE.test(value)) return { kind: 'img', src: value };
25  if (/^https?:\/\//i.test(value)) return { kind: 'link', href: value };
26  if (value.startsWith('/api/files/raw?')) return { kind: 'img', src: value };
27  // a drive letter (C:\x.png, C:/x.png) is a path, not a scheme
28  if (SCHEME.test(value) && !/^[a-z]:[\\/]/i.test(value)) return { kind: 'text' };
29  if (value.startsWith('//') || value.startsWith('\\\\')) return { kind: 'text' };      // another computer's share
30  if (!sessionId) return { kind: 'text' };
31  return { kind: 'img', src: rawUrlOf(sessionId, value) };
32}