feat(assistant): models that read images see the live page

The Studio draws the page on request and uploads it once, each provider gets
the image where it expects it, and a refusal falls back to the text
description. No image is stored.
This commit is contained in:
Maurice
2026-10-22 21:25:46 +00:00
parent 499458f8b2
commit 8b93898b73
38 changed files with 2714 additions and 123 deletions
@@ -3,7 +3,9 @@ import {
aiApiKeyConnectRequestSchema,
aiChatgptFinishRequestSchema,
aiInstanceConnectionRequestSchema,
aiModelInfoSchema,
aiOpenrouterStartRequestSchema,
aiPrivacyAckRequestSchema,
aiRetentionSchema,
aiStatusResponseSchema,
} from './ai-connections.schema';
@@ -145,3 +147,22 @@ describe('aiStatusResponseSchema', () => {
).toBe(true);
});
});
describe('the Studio page image', () => {
it('AIVIS-001: a model may say whether it reads images, and a list that cannot say leaves it out', () => {
expect(aiModelInfoSchema.parse({ id: 'm', label: 'm', supportsTools: true, supportsImages: false })).toEqual({
id: 'm',
label: 'm',
supportsTools: true,
supportsImages: false,
});
expect(aiModelInfoSchema.safeParse({ id: 'm', label: 'm', supportsTools: true }).success).toBe(true);
});
it('AIVIS-002: the acknowledgement of page images is its own scope; no other scope exists', () => {
const ack = { kind: 'anthropic', host: 'api.anthropic.com' };
expect(aiPrivacyAckRequestSchema.safeParse(ack).success).toBe(true);
expect(aiPrivacyAckRequestSchema.safeParse({ ...ack, scope: 'studio_images' }).success).toBe(true);
expect(aiPrivacyAckRequestSchema.safeParse({ ...ack, scope: 'everything' }).success).toBe(false);
});
});
+9 -1
View File
@@ -48,6 +48,8 @@ export const aiModelInfoSchema = z.object({
free: z.boolean().optional(),
// The one a new connection starts on: the newest stable model of a capable line. Listed first.
recommended: z.boolean().optional(),
// Whether the model reads images (the Studio assistant's page image); left out where the list cannot say.
supportsImages: z.boolean().optional(),
});
export type AiModelInfo = z.infer<typeof aiModelInfoSchema>;
@@ -88,6 +90,8 @@ export const aiStatusResponseSchema = z.object({
quota: z.object({ used: z.number().int(), limit: z.number().int() }).nullable(),
// Whether the user acknowledged that the effective kind@host receives their data.
privacyAcked: z.boolean(),
// Whether the user also let kind@host receive images of their Studio pages, with their own photos on them.
studioImagesAcked: z.boolean().optional(),
retention: aiRetentionSchema,
// OpenRouter's OAuth needs an https callback; without one the client offers the key field instead.
openrouterOAuth: z.boolean(),
@@ -142,11 +146,15 @@ export type AiAuthStartResponse = z.infer<typeof aiAuthStartResponseSchema>;
export const aiModelListResponseSchema = z.object({ models: z.array(aiModelInfoSchema) });
export type AiModelListResponse = z.infer<typeof aiModelListResponseSchema>;
/** POST /api/ai/privacy-ack: the user saw that kind@host receives their messages. */
/**
* POST /api/ai/privacy-ack: the user saw that kind@host receives their
* messages, or (`scope: 'studio_images'`) also images of their Studio pages.
*/
export const aiPrivacyAckRequestSchema = z
.object({
kind: aiConnectionKindSchema,
host: z.string().max(253),
scope: z.enum(['studio_images']).optional(),
})
.strict();
export type AiPrivacyAckRequest = z.infer<typeof aiPrivacyAckRequestSchema>;
@@ -0,0 +1,85 @@
import {
ASSISTANT_PAGE_IMAGE,
assistantPageRenderSchema,
assistantPageViewSchema,
assistantRenderUploadRequestSchema,
drawnPhotoIdsOf,
previewBookPageRequestSchema,
} from './assistant-vision.schema';
import { assistantMessagePartSchema, uiStreamChunkSchema } from './assistant.schema';
import { describe, expect, it } from 'vitest';
const RENDER = {
requestId: '0b6f7c1e-3a52-4d8e-9a7b-2f1e0c9d8a76',
threadId: 4,
spreadId: 'sp-1',
bookVersion: 7,
hiddenPhotoIds: [12],
};
const VIEW = { spreadId: 'sp-1', bookVersion: 7, width: 1280, height: 640, bytes: 182_000, description: 'Page 2' };
describe('the Studio page image', () => {
it('ASTVIS-001: a render request names the page, the version and the photos left out; ops are a proposal', () => {
expect(assistantPageRenderSchema.safeParse(RENDER).success).toBe(true);
const ops = [{ op: 'background', color: '#ffffff' }];
expect(assistantPageRenderSchema.safeParse({ ...RENDER, ops }).success).toBe(true);
expect(assistantPageRenderSchema.safeParse({ ...RENDER, ops: [] }).success).toBe(false);
expect(assistantPageRenderSchema.safeParse({ ...RENDER, requestId: 'r1' }).success).toBe(false);
expect(assistantPageRenderSchema.safeParse({ ...RENDER, spreadId: '../x' }).success).toBe(false);
});
it('ASTVIS-002: a page view keeps the size of the image and the description, never the image', () => {
expect(assistantPageViewSchema.parse({ ...VIEW, data: 'aGVsbG8=' })).toEqual(VIEW);
const textOnly = { ...VIEW, width: null, height: null, bytes: null };
expect(assistantPageViewSchema.safeParse(textOnly).success).toBe(true);
});
it('ASTVIS-003: the page view is a stored part; the render request and the page view are stream chunks', () => {
expect(assistantMessagePartSchema.safeParse({ type: 'data-trek-page-view', data: VIEW }).success).toBe(true);
const request = { type: 'data-trek-render-request', data: RENDER, transient: true };
expect(uiStreamChunkSchema.safeParse(request).success).toBe(true);
expect(uiStreamChunkSchema.safeParse({ type: 'data-trek-page-view', data: VIEW }).success).toBe(true);
});
it('ASTVIS-004: an upload reads its text fields as multipart sends them, and its photo ids as a list', () => {
const fields = { bookVersion: '7', spreadId: 'sp-1', drawnPhotoIds: '12,30' };
expect(assistantRenderUploadRequestSchema.parse(fields)).toEqual({ ...fields, bookVersion: 7 });
expect(drawnPhotoIdsOf('12,30')).toEqual([12, 30]);
expect(drawnPhotoIdsOf('')).toEqual([]);
expect(drawnPhotoIdsOf(undefined)).toEqual([]);
});
it('ASTVIS-006: a preview names a page and may carry the ops of a proposed edit, nothing else', () => {
expect(previewBookPageRequestSchema.safeParse({ spreadId: 'sp-1' }).success).toBe(true);
const ops = [{ op: 'background', color: '#ffffff' }];
expect(previewBookPageRequestSchema.safeParse({ spreadId: 'sp-1', ops }).success).toBe(true);
expect(previewBookPageRequestSchema.safeParse({ spreadId: 'sp-1', ops: [] }).success).toBe(false);
expect(previewBookPageRequestSchema.safeParse({ spreadId: 'sp-1', journeyId: 3 }).success).toBe(false);
});
it('ASTVIS-005: an upload refuses ids that are no list of numbers, and fields it does not know', () => {
const base = { bookVersion: '7', spreadId: 'sp-1' };
expect(assistantRenderUploadRequestSchema.safeParse({ ...base, drawnPhotoIds: '12,,3' }).success).toBe(false);
expect(assistantRenderUploadRequestSchema.safeParse({ ...base, drawnPhotoIds: '1;2' }).success).toBe(false);
expect(assistantRenderUploadRequestSchema.safeParse({ ...base, data: 'x' }).success).toBe(false);
expect(assistantRenderUploadRequestSchema.safeParse({ ...base, bookVersion: '-1' }).success).toBe(false);
});
it('ASTVIS-007: a page may name as many photos as its frames and map stops hold, up to the cap', () => {
const many = Array.from({ length: ASSISTANT_PAGE_IMAGE.maxPagePhotos }, (_, i) => i + 1);
expect(assistantPageRenderSchema.safeParse({ ...RENDER, hiddenPhotoIds: many }).success).toBe(true);
const tooMany = [...many, ASSISTANT_PAGE_IMAGE.maxPagePhotos + 1];
expect(assistantPageRenderSchema.safeParse({ ...RENDER, hiddenPhotoIds: tooMany }).success).toBe(false);
const fields = { bookVersion: '7', spreadId: 'sp-1', drawnPhotoIds: many.join(',') };
expect(assistantRenderUploadRequestSchema.safeParse(fields).success).toBe(true);
});
it('ASTVIS-008: a preview and a render request may name a design draft, by an id that is no path', () => {
expect(previewBookPageRequestSchema.safeParse({ spreadId: 'sp-1', designId: 'd-1a2b' }).success).toBe(true);
expect(previewBookPageRequestSchema.safeParse({ spreadId: 'sp-1', designId: '../d' }).success).toBe(false);
expect(assistantPageRenderSchema.parse({ ...RENDER, designId: 'd-1a2b' }).designId).toBe('d-1a2b');
expect(assistantPageRenderSchema.safeParse({ ...RENDER, designId: 'd/1' }).success).toBe(false);
});
});
@@ -0,0 +1,110 @@
import { bookSpreadIdSchema } from '../book/book-op-ids';
import { bookDesignIdSchema, editBookPageArgs } from '../book/book-ops.schema';
import { idSchema } from '../common/primitives.schema';
import { z } from 'zod';
/**
* The Studio assistant looking at a page (journey-studio.md section 3). The
* server cannot draw a page, so it asks the Studio tab that holds the turn's
* stream for one: a `data-trek-render-request` chunk names the page, the book
* version and the photos to leave out, the tab draws that one page and
* uploads it as an image, and the turn hands it to a model that reads images,
* beside the page's description. Nothing of the image is kept: the message
* stores what was looked at (`data-trek-page-view`), never the bytes.
*/
export const ASSISTANT_PAGE_IMAGE = {
/** The long edge a page is drawn at, in pixels, and the JPEG quality the Studio sends it with. */
longEdge: 1280,
jpegQuality: 0.82,
/** What the server takes at most: the bytes, the long edge and the pixels of one image. */
maxBytes: 1024 * 1024,
maxEdge: 2048,
maxPixels: 4_000_000,
/** Photos one page may name as hidden or drawn: its frames and the pictures its maps mark stops with. */
maxPagePhotos: 1000,
/** How long a turn waits for a page image before it goes on with the description alone. */
timeoutMs: 15_000,
/** Images one model request carries at most, one per tool answer; earlier ones go as their text. */
perRequest: 3,
/** Previews of a proposed change one step and one turn may ask for. */
previewsPerStep: 3,
previewsPerTurn: 8,
} as const;
/**
* A page the server asks the Studio to draw, streamed while the turn waits.
* `ops` are a proposal to draw on an unsaved copy (a preview); without them
* the page is drawn as the book stands at `bookVersion`. The photos in
* `hiddenPhotoIds` are drawn as empty frames, and a map stop showing one as
* a plain dot: only the asking user's own photos leave the browser.
*/
export const assistantPageRenderSchema = z.object({
requestId: z.uuid(),
threadId: idSchema,
spreadId: bookSpreadIdSchema,
bookVersion: z.number().int().nonnegative(),
ops: editBookPageArgs.ops.optional(),
/** A page of a design draft (design_book) rather than of the book: the Studio draws it from the draft. */
designId: bookDesignIdSchema.optional(),
hiddenPhotoIds: z.array(idSchema).max(ASSISTANT_PAGE_IMAGE.maxPagePhotos),
});
export type AssistantPageRender = z.infer<typeof assistantPageRenderSchema>;
/**
* What a turn looked at, as the message keeps it: the page, the version, the
* image's size when one reached the model (null when the model read the
* description alone), and the description itself. Never the image.
*/
export const assistantPageViewSchema = z.object({
spreadId: bookSpreadIdSchema,
bookVersion: z.number().int().nonnegative(),
width: z.number().int().positive().nullable(),
height: z.number().int().positive().nullable(),
bytes: z.number().int().positive().nullable(),
description: z.string().max(20_000),
});
export type AssistantPageView = z.infer<typeof assistantPageViewSchema>;
/**
* `preview_book_page`, a read of the Studio turn's own (no MCP tool): one page
* as it stands, or with `ops` as an `edit_book_page` with them would leave it,
* nothing saved. It answers the page's description and findings, and a model
* that reads images also gets the page as the Studio draws it.
*/
export const previewBookPageRequestSchema = z.strictObject({
spreadId: bookSpreadIdSchema,
ops: editBookPageArgs.ops.optional(),
/** Look at the page of a design draft instead of the book's (design_book's design_id). */
designId: bookDesignIdSchema.optional(),
});
export type PreviewBookPageRequest = z.infer<typeof previewBookPageRequestSchema>;
/**
* POST /api/journeys/:journeyId/assistant/threads/:threadId/renders/:requestId,
* multipart: the image in the field `image`, these as text fields beside it.
* `drawnPhotoIds` lists the photos the image shows, comma-separated; the
* server refuses the image when one of them is not the user's to show.
*/
export const assistantRenderUploadRequestSchema = z
.object({
bookVersion: z.coerce.number().int().nonnegative(),
spreadId: bookSpreadIdSchema,
drawnPhotoIds: z
.string()
.max(ASSISTANT_PAGE_IMAGE.maxPagePhotos * 13)
.regex(/^(?:\d{1,12}(?:,\d{1,12})*)?$/)
.optional(),
})
.strict();
export type AssistantRenderUploadRequest = z.infer<typeof assistantRenderUploadRequestSchema>;
/** The answer to an image the turn took; anything it did not take is refused with an error instead. */
export const assistantRenderUploadResponseSchema = z.object({ accepted: z.literal(true) });
export type AssistantRenderUploadResponse = z.infer<typeof assistantRenderUploadResponseSchema>;
/** The photo ids of an upload's `drawnPhotoIds` field. */
export function drawnPhotoIdsOf(field: string | undefined): number[] {
return field ? field.split(',').map(Number) : [];
}