Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
Add Nano Banana image generation with reference photos and inline previews (#7178)
Eric Hayes93e65a0ff75b
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 098897f641..eec4751075 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -458,6 +458,7 @@"crates/harness_id","crates/harness_token","crates/harnesses",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -608,6 +609,7 @@"crates/google_token","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -684,6 +686,7 @@"crates/soup","crates/sqs_client","crates/sqs_worker",+ "crates/static_file","crates/static_file_service_client","crates/sync_service","crates/sync_service_client",@@ -1127,6 +1130,7 @@"crates/google_token","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -1202,6 +1206,7 @@"crates/soup","crates/sqs_client","crates/sqs_worker",+ "crates/static_file","crates/static_file_service_client","crates/sync_service","crates/sync_service_client",@@ -1353,6 +1358,7 @@"crates/google_token","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -1428,6 +1434,7 @@"crates/soup","crates/sqs_client","crates/sqs_worker",+ "crates/static_file","crates/static_file_service_client","crates/sync_service","crates/sync_service_client",@@ -2139,6 +2146,7 @@"crates/google_token","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -2214,6 +2222,7 @@"crates/soup","crates/sqs_client","crates/sqs_worker",+ "crates/static_file","crates/static_file_service_client","crates/sync_service","crates/sync_service_client",@@ -2887,6 +2896,7 @@"crates/graphql_soup_filter_input","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -2970,6 +2980,7 @@"crates/soup_realtime","crates/sqs_client","crates/sqs_worker",+ "crates/static_file","crates/static_file_service_client","crates/sync_service","crates/sync_service_client",@@ -3640,6 +3651,7 @@"crates/google_token","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -3815,6 +3827,7 @@"crates/harness_id","crates/harness_token","crates/harnesses",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -6139,6 +6152,7 @@"crates/graphql_scheduled_action","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -6218,6 +6232,7 @@"crates/soup","crates/sqs_client","crates/sqs_worker",+ "crates/static_file","crates/static_file_service_client","crates/sync_service","crates/sync_service_client",@@ -6433,6 +6448,111 @@"crates/remote_env_var","crates/workspace-hack"],+ "image_generation": [+ "crates/activity",+ "crates/agent",+ "crates/agent_fold",+ "crates/agent_runtime_protocol",+ "crates/ai_toolset",+ "crates/ai_usage",+ "crates/attachment",+ "crates/authentication_service_client",+ "crates/bot_id",+ "crates/bot_token",+ "crates/broadcast",+ "crates/calendar_events",+ "crates/channel_sender",+ "crates/channels",+ "crates/comms_db_client",+ "crates/connection",+ "crates/connection_gateway_client",+ "crates/connection_gateway_models",+ "crates/contacts",+ "crates/cowlike",+ "crates/crm",+ "crates/database_env_vars",+ "crates/document_sub_type",+ "crates/documents",+ "crates/email",+ "crates/email_utils",+ "crates/embedding",+ "crates/entity_access",+ "crates/entity_access_db_utils",+ "crates/entity_access_management",+ "crates/entity_mutation",+ "crates/entity_registry/entity_registry",+ "crates/entity_registry/entity_registry_db_utils",+ "crates/entity_registry/shared_entity_registry",+ "crates/filter_ast",+ "crates/foreign_entity",+ "crates/frecency",+ "crates/genai_telemetry",+ "crates/google_token",+ "crates/harness_id",+ "crates/harness_token",+ "crates/image_generation",+ "crates/initiative",+ "crates/invite_email",+ "crates/item_filters",+ "crates/kafka_util",+ "crates/last_online_tracker",+ "crates/lexical_client",+ "crates/macro_auth",+ "crates/macro_authorization",+ "crates/macro_aws_config",+ "crates/macro_db_client",+ "crates/macro_entrypoint",+ "crates/macro_env",+ "crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics",+ "crates/macro_service_urls",+ "crates/macro_sync_service_jwt",+ "crates/macro_tower_layers",+ "crates/macro_user_id",+ "crates/macro_uuid",+ "crates/mention_utils",+ "crates/messages",+ "crates/model",+ "crates/model-entity",+ "crates/model-error-response",+ "crates/model_file_type",+ "crates/model_notifications",+ "crates/model_owner",+ "crates/model_user",+ "crates/models_ai_projection",+ "crates/models_bulk_upload",+ "crates/models_dcs",+ "crates/models_email",+ "crates/models_entity_access_management",+ "crates/models_opensearch",+ "crates/models_pagination",+ "crates/models_permissions",+ "crates/models_properties",+ "crates/models_sfs",+ "crates/models_team",+ "crates/non_empty",+ "crates/notification",+ "crates/notification_state",+ "crates/prompt",+ "crates/properties",+ "crates/rate_limit",+ "crates/remote_env_var",+ "crates/s3_key",+ "crates/secretsmanager_client",+ "crates/share_permission_db_utils",+ "crates/sqs_client",+ "crates/sqs_worker",+ "crates/static_file_service_client",+ "crates/sync_service",+ "crates/sync_service_client",+ "crates/system_properties",+ "crates/system_skills",+ "crates/task_dedup",+ "crates/unfurl",+ "crates/user_quota",+ "crates/workspace-hack"+ ],"image_optimizer": ["crates/macro_aws_config","crates/macro_entrypoint",@@ -7326,6 +7446,7 @@"crates/google_token","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -7403,6 +7524,7 @@"crates/soup","crates/sqs_client","crates/sqs_worker",+ "crates/static_file","crates/static_file_service_client","crates/sync_service","crates/sync_service_client",@@ -7481,6 +7603,7 @@"crates/google_token","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -7557,6 +7680,7 @@"crates/soup","crates/sqs_client","crates/sqs_worker",+ "crates/static_file","crates/static_file_service_client","crates/sync_service","crates/sync_service_client",@@ -9018,6 +9142,7 @@"crates/google_token","crates/harness_id","crates/harness_token",+ "crates/image_generation","crates/import","crates/initiative","crates/initiative_documents",@@ -9097,6 +9222,7 @@"crates/soup","crates/sqs_client","crates/sqs_worker",+ "crates/static_file","crates/static_file_service_client","crates/sync_service","crates/sync_service_client",diff --git a/Cargo.lock b/Cargo.lockindex b0c2234d4a..583eae8407 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -519,6 +519,7 @@ dependencies = ["tracing","tracing-opentelemetry","tracing-subscriber",+ "url","workspace-hack",]@@ -863,6 +864,7 @@ dependencies = ["foreign_entity","frecency","futures",+ "image_generation","import","initiative","initiative_documents",@@ -913,6 +915,7 @@ dependencies = ["soup","sqlx","sqs_client",+ "static_file","strum 0.27.2","sync_service_client","system_properties",@@ -9395,6 +9398,37 @@ dependencies = ["quick-error",]+[[package]]+name = "image_generation"+version = "0.1.0"+dependencies = [+ "ai_toolset",+ "anyhow",+ "async-trait",+ "attachment",+ "base64 0.22.1",+ "bot_id",+ "documents",+ "entity_access",+ "macro_user_id",+ "model",+ "model-entity",+ "model_owner",+ "models_permissions",+ "non_empty",+ "reqwest 0.13.4",+ "rootcause",+ "schemars 1.2.1",+ "serde",+ "serde_json",+ "thiserror 2.0.18",+ "tokio",+ "tracing",+ "uuid",+ "wiremock",+ "workspace-hack",+]+[[package]]name = "image_optimizer"version = "0.1.0"diff --git a/Cargo.toml b/Cargo.tomlindex 3b0806b949..86cca1c8ef 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -34,6 +34,7 @@ members = ["crates/cursor_cloud_agents","crates/claude_cloud_agents","crates/embedding",+ "crates/image_generation","crates/dictation","crates/email_api_client","crates/entity_mutation",diff --git a/apps/docs/AI/mcp/tools/generate-image.mdx b/apps/docs/AI/mcp/tools/generate-image.mdxnew file mode 100644index 0000000000..7b7d7df796--- /dev/null+++ b/apps/docs/AI/mcp/tools/generate-image.mdx@@ -0,0 +1,18 @@+---+title: GenerateImage+description: "Generate or edit an image with Google's Nano Banana image model and save the result as a new image document in Macro. Use for pictures, illustrations, diagram concepts, logo ideas, mockups, or edits based on reference photos. When the user supplies photos or asks to modify an existing image, pass them in referenceImages; describing a photo in the prompt alone does not send it to the image model. Describe the subject, style, composition, lighting, and any text to render; only the prompt is required. Refer to reference images by their order (image 1, image 2, image 3) when explaining how to use them. Returns the new document ID to cite inline. Generation takes several seconds."+---++# GenerateImage++Generate or edit an image with Google's Nano Banana image model and save the result as a new image document in Macro. Use for pictures, illustrations, diagram concepts, logo ideas, mockups, or edits based on reference photos. When the user supplies photos or asks to modify an existing image, pass them in referenceImages; describing a photo in the prompt alone does not send it to the image model. Describe the subject, style, composition, lighting, and any text to render; only the prompt is required. Refer to reference images by their order (image 1, image 2, image 3) when explaining how to use them. Returns the new document ID to cite inline. Generation takes several seconds.++## Parameters++| Parameter | Type | Required | Description |+| --- | --- | --- | --- |+| `prompt` | string | Yes | Detailed description of the image to generate: subject, style (photo, illustration, flat vector...), composition, colours, mood, and any text that must appear. |+| `fileName` | string | No | Optional short descriptive name for the saved image, for example `sunset-lighthouse`. The file extension is added from the generated format. No directory path. Omit to name the image after the prompt. |+| `aspectRatio` | `"square"` \| `"landscape"` \| `"portrait"` \| `"widescreen"` \| `"tall"` | No | Shape of the image. Omit for the model default (square). `widescreen` (16:9) suits banners and slides, `tall` (9:16) suits phone screens and stories. |+| `projectId` | string | No | Optional destination project (folder) ID. Requires edit access. Omit to save to the user's top-level files. |+| `referenceImages` | ImageReferenceInput[] | No | Up to three reference photos, in the order used by the prompt. Use type document with a Macro image document ID, or type staticFile with the UUID from /file/<id> in an uploaded attachment's source URL. Use the actual IDs supplied in the conversation or by tools; do not invent IDs. Omit for text-only generation. |diff --git a/apps/docs/AI/mcp/tools/index.mdx b/apps/docs/AI/mcp/tools/index.mdxindex aa9b9f2979..407dcbe033 100644--- a/apps/docs/AI/mcp/tools/index.mdx+++ b/apps/docs/AI/mcp/tools/index.mdx@@ -35,6 +35,7 @@ These pages are generated from Macro's Rust MCP tool registry.- [EditDocument](/AI/mcp/tools/edit-document)- [EditSpreadsheet](/AI/mcp/tools/edit-spreadsheet)- [EditTag](/AI/mcp/tools/edit-tag)+- [GenerateImage](/AI/mcp/tools/generate-image)- [GetBotWebhooks](/AI/mcp/tools/get-bot-webhooks)- [GetCompany](/AI/mcp/tools/get-company)- [GetEntityProperties](/AI/mcp/tools/get-entity-properties)diff --git a/apps/docs/config/tool-pages.json b/apps/docs/config/tool-pages.jsonindex e56fa07f55..b600539d13 100644--- a/apps/docs/config/tool-pages.json+++ b/apps/docs/config/tool-pages.json@@ -26,6 +26,7 @@"AI/mcp/tools/edit-document","AI/mcp/tools/edit-spreadsheet","AI/mcp/tools/edit-tag",+ "AI/mcp/tools/generate-image","AI/mcp/tools/get-bot-webhooks","AI/mcp/tools/get-company","AI/mcp/tools/get-entity-properties",diff --git a/apps/docs/docs.json b/apps/docs/docs.jsonindex ae5d64c2b9..a839ec06ed 100644--- a/apps/docs/docs.json+++ b/apps/docs/docs.json@@ -97,6 +97,7 @@"AI/mcp/tools/edit-document","AI/mcp/tools/edit-spreadsheet","AI/mcp/tools/edit-tag",+ "AI/mcp/tools/generate-image","AI/mcp/tools/get-bot-webhooks","AI/mcp/tools/get-company","AI/mcp/tools/get-entity-properties",diff --git a/apps/web/src/features/block-agent/component/parts/MacroToolCall.tsx b/apps/web/src/features/block-agent/component/parts/MacroToolCall.tsxindex 3584a0f79b..35d24b2fd5 100644--- a/apps/web/src/features/block-agent/component/parts/MacroToolCall.tsx+++ b/apps/web/src/features/block-agent/component/parts/MacroToolCall.tsx@@ -35,6 +35,7 @@ type ToolResponse = NamedTool<ToolName, 'response'>;export function MacroToolCall(props: {detail: MacroDetail;common: ToolCallCommon;+ grouped?: boolean;context?: ToolCallContext;}): JSX.Element {const response = createMemo(() =>@@ -122,7 +123,10 @@ export function MacroToolCall(props: {part_index={props.context?.partIndex ?? 0}isComplete={true}renderContext={{- renderContext: { isStreaming: false, grouped: true },+ renderContext: {+ isStreaming: false,+ grouped: props.grouped ?? true,+ },}}/></Suspense>diff --git a/apps/web/src/features/block-agent/component/parts/ToolCallPart.test.tsx b/apps/web/src/features/block-agent/component/parts/ToolCallPart.test.tsxindex 9296b0d046..43858ccc9d 100644--- a/apps/web/src/features/block-agent/component/parts/ToolCallPart.test.tsx+++ b/apps/web/src/features/block-agent/component/parts/ToolCallPart.test.tsx@@ -1224,3 +1224,45 @@ describe('ToolCallPart result summaries', () => {expect(rendered.getByTestId('trailing').textContent).toBe('');});});++describe('generated image dispatch', () => {+ it.each(['native', 'mcp'] as const)(+ 'uses the image result renderer for %s calls outside group chrome',+ (kind) => {+ const input = { prompt: 'A frog under a leaf' };+ const output = {+ documentId: '01a0eecf-1162-7bea-9ba9-925769372a8a',+ fileName: 'frog.png',+ mimeType: 'image/png',+ sizeBytes: 132421,+ };+ const part: ToolUsePart = {+ kind: 'tool_use',+ id: 'image-call',+ status: 'completed',+ name:+ kind === 'native'+ ? { kind, name: 'GenerateImage' }+ : { kind, server: 'macro', tool: 'GenerateImage' },+ detail:+ kind === 'native'+ ? { kind: 'macro', input, output, error: null }+ : {+ kind: 'other',+ acpKind: 'other',+ input,+ output: null,+ result: output,+ error: null,+ },+ };+ const view = render(() => <ToolCallPart part={part} />);+ const image = view.getByTestId('macro-tool');+ expect(image.textContent).toBe('GenerateImage');+ expect(image.dataset.grouped).toBe('false');+ expect(JSON.parse(image.dataset.response ?? '')).toEqual(output);+ expect(view.queryByTestId('dashboard-view')).toBeNull();+ expect(view.queryByTestId('tool-card')).toBeNull();+ }+ );+});diff --git a/apps/web/src/features/block-agent/component/parts/ToolCallPart.tsx b/apps/web/src/features/block-agent/component/parts/ToolCallPart.tsxindex 6b34968fdd..e465da0786 100644--- a/apps/web/src/features/block-agent/component/parts/ToolCallPart.tsx+++ b/apps/web/src/features/block-agent/component/parts/ToolCallPart.tsx@@ -60,7 +60,12 @@ export function ToolCallPart(props: {// its detail object; each child receives the current detail through an accessor.return (<Switch>- <Match when={rendersOwnView(props.part)}>+ <Match+ when={+ rendersOwnView(props.part) &&+ toolLabel(props.part.name) === 'DisplayResults'+ }+ ><DisplayResultsToolCallinput={'input' in props.part.detail ? props.part.detail.input : undefined@@ -117,6 +122,7 @@ export function ToolCallPart(props: {error: detail().error,}}common={common()}+ grouped={!rendersOwnView(props.part)}context={props.context}/></Show>@@ -127,6 +133,7 @@ export function ToolCallPart(props: {<MacroToolCalldetail={detail()}common={common()}+ grouped={!rendersOwnView(props.part)}context={props.context}/>)}diff --git a/apps/web/src/features/block-agent/state/tool-groups.test.ts b/apps/web/src/features/block-agent/state/tool-groups.test.tsindex 3dfa1a9924..c4ced37765 100644--- a/apps/web/src/features/block-agent/state/tool-groups.test.ts+++ b/apps/web/src/features/block-agent/state/tool-groups.test.ts@@ -378,3 +378,60 @@ describe('rendersOwnView', () => {).toBe(false);});});++describe('generated image grouping', () => {+ it.each(['native', 'mcp'] as const)(+ 'keeps %s image calls outside surrounding tool groups in every state',+ (kind) => {+ for (const status of [+ 'pending',+ 'running',+ 'completed',+ 'failed',+ ] as const) {+ const image = tool({+ name:+ kind === 'native'+ ? { kind, name: 'GenerateImage' }+ : { kind, server: 'macro', tool: 'GenerateImage' },+ status,+ detail:+ kind === 'native'+ ? { kind: 'macro', input: null, output: null, error: null }+ : {+ kind: 'other',+ acpKind: 'other',+ input: null,+ output: null,+ result: null,+ error: null,+ },+ });+ expect(rendersOwnView(image)).toBe(true);+ expect(segmentParts([tool(), image, tool()])).toEqual([+ { kind: 'tools', start: 0, end: 1 },+ { kind: 'part', start: 1, end: 2 },+ { kind: 'tools', start: 2, end: 3 },+ ]);+ }+ }+ );++ it('keeps an unrelated MCP tool with the same name in its group', () => {+ const image = tool({+ name: { kind: 'mcp', server: 'external', tool: 'GenerateImage' },+ detail: {+ kind: 'other',+ acpKind: 'other',+ input: null,+ output: null,+ result: null,+ error: null,+ },+ });+ expect(rendersOwnView(image)).toBe(false);+ expect(segmentParts([tool(), image, tool()])).toEqual([+ { kind: 'tools', start: 0, end: 3 },+ ]);+ });+});diff --git a/apps/web/src/features/block-agent/state/tool-groups.ts b/apps/web/src/features/block-agent/state/tool-groups.tsindex 7f2af3e014..d7a3361afd 100644--- a/apps/web/src/features/block-agent/state/tool-groups.ts+++ b/apps/web/src/features/block-agent/state/tool-groups.ts@@ -5,7 +5,7 @@* replace the first call's host in the middle of its animation.* Subagents and user tools stay outside groups so delegated work and user* interactions remain visible regardless of the surrounding tool traffic.- * DisplayResults is inline answer content and always breaks the run, including+ * DisplayResults and GenerateImage are answer content and always break the run, including* while its arguments are still arriving.** A thought at the tail of a run stays out of the group only when the run@@ -30,13 +30,17 @@ export type PartSegment = {/*** Macro tools whose call renders the answer itself rather than a card about* it: `DisplayResults` renders the dynamic-UI view the model composed, the- * same full-width dashboard the chat shows.+ * same full-width dashboard the chat shows. `GenerateImage` shows the generated+ * image document.** Folding one of these into a group would put the answer behind a closed* caret, indented inside a row of muted tool chips — so they break the run* and stand on their own, like a paragraph of the reply.*/-const SELF_RENDERING_TOOLS: ReadonlySet<string> = new Set(['DisplayResults']);+const SELF_RENDERING_TOOLS: ReadonlySet<string> = new Set([+ 'DisplayResults',+ 'GenerateImage',+]);/*** Whether a part is a tool call that renders its own view (seediff --git a/apps/web/src/lib/core/component/AI/component/message/AssistantMessageParts.test.tsx b/apps/web/src/lib/core/component/AI/component/message/AssistantMessageParts.test.tsxindex b504642a7c..6a9b07c89e 100644--- a/apps/web/src/lib/core/component/AI/component/message/AssistantMessageParts.test.tsx+++ b/apps/web/src/lib/core/component/AI/component/message/AssistantMessageParts.test.tsx@@ -567,3 +567,36 @@ describe('AssistantMessageParts streaming identity', () => {disposeStream();});});++describe('generated image presentation', () => {+ it('keeps generated images outside the activity group with adjacent tools', () => {+ const parts: AssistantMessagePart[] = [+ { type: 'toolCall', id: 'before', name: 'ReadContent', json: {} },+ {+ type: 'toolCall',+ id: 'image',+ name: 'GenerateImage',+ json: { prompt: 'A frog' },+ },+ { type: 'toolCall', id: 'after', name: 'ReadContent', json: {} },+ ];+ const view = render(() => (+ <AssistantMessageParts+ parts={parts}+ message={{+ attachments: [],+ content: parts,+ id: 'message-1',+ role: 'assistant',+ }}+ isStreaming={false}+ />+ ));+ const image = view+ .getAllByTestId('tool')+ .find((tool) => tool.dataset.name === 'GenerateImage');+ expect(image?.dataset.grouped).toBe('false');+ expect(image?.textContent).toBe('image');+ expect(view.queryByTestId('activity-toggle')).toBeNull();+ });+});diff --git a/apps/web/src/lib/core/component/AI/component/message/AssistantMessageParts.tsx b/apps/web/src/lib/core/component/AI/component/message/AssistantMessageParts.tsxindex 518767b553..262c479fdc 100644--- a/apps/web/src/lib/core/component/AI/component/message/AssistantMessageParts.tsx+++ b/apps/web/src/lib/core/component/AI/component/message/AssistantMessageParts.tsx@@ -71,6 +71,7 @@ type RenderItem =const STANDALONE_TOOLS: ReadonlySet<string> = new Set(['CreateCalendarEvent','DisplayResults',+ 'GenerateImage','ReadActivity','SendEmail',]);diff --git a/apps/web/src/lib/core/component/AI/component/tool/GenerateImage.test.tsx b/apps/web/src/lib/core/component/AI/component/tool/GenerateImage.test.tsxnew file mode 100644index 0000000000..709643abdb--- /dev/null+++ b/apps/web/src/lib/core/component/AI/component/tool/GenerateImage.test.tsx@@ -0,0 +1,162 @@+import { cleanup, fireEvent, render } from '@solidjs/testing-library';+import { createSignal } from 'solid-js';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { generateImageHandler } from './GenerateImage';+import { ToolErrorContext } from './ToolRenderer';++const preview = vi.hoisted(() => ({+ isSuccess: true,+ isError: false,+ url: 'https://storage.example/frog.png',+ dataReads: 0,+}));++vi.mock('@queries/storage/binary-document', () => ({+ useBinaryDocumentQuery: () => ({+ get isSuccess() {+ return preview.isSuccess;+ },+ get isError() {+ return preview.isError;+ },+ get data() {+ preview.dataReads += 1;+ if (!preview.isSuccess) throw new Error('Pending data read');+ return preview.url;+ },+ }),+}));++const splits = vi.hoisted(() => ({+ activate: vi.fn(),+ insertSplit: vi.fn(),+ replaceOrInsertSplit: vi.fn(),+}));++vi.mock('@components/app/split-layout/layout', () => ({+ useSplitLayout: () => splits,+}));++vi.mock('@core/mobile/isTouchDevice', () => ({+ isTouchDevice: () => false,+}));++const result = {+ documentId: 'image-id',+ fileName: 'frog.png',+ mimeType: 'image/png',+ sizeBytes: 132421,+ note: 'A frog under a leaf.',+};++function imageTool(+ response = true,+ error?: string | (() => string | undefined)+) {+ const failure = () => (typeof error === 'function' ? error() : error);+ return render(() => (+ <ToolErrorContext.Provider value={failure}>+ <generateImageHandler.render+ tool={{+ id: 'call-1',+ name: 'GenerateImage',+ data: { prompt: 'A frog under a leaf', fileName: 'frog' },+ }}+ response={+ response+ ? { id: 'call-1', name: 'GenerateImage', data: result }+ : undefined+ }+ chat_id="chat-1"+ message_id="message-1"+ part_index={0}+ isComplete={response || !!failure()}+ renderContext={{ isStreaming: !response, grouped: false }}+ />+ </ToolErrorContext.Provider>+ ));+}++afterEach(cleanup);+beforeEach(() => {+ preview.isSuccess = true;+ preview.isError = false;+ preview.dataReads = 0;+ splits.activate.mockClear();+ splits.insertSplit.mockReset().mockReturnValue({ activate: splits.activate });+ splits.replaceOrInsertSplit+ .mockReset()+ .mockReturnValue({ activate: splits.activate });+});++describe('generated image result', () => {+ it('shows the filename and image as one card that opens and activates a new app split', () => {+ const view = imageTool();+ const card = view.getByRole('button', {+ name: 'Open frog.png in a new split',+ });+ expect(card.getAttribute('type')).toBe('button');+ expect(card.textContent).toBe('frog.png');+ fireEvent.click(card);+ expect(splits.insertSplit).toHaveBeenCalledExactlyOnceWith({+ type: 'image',+ id: 'image-id',+ });+ expect(splits.activate).toHaveBeenCalledOnce();+ expect(splits.replaceOrInsertSplit).not.toHaveBeenCalled();+ expect(+ view.getByRole('img', { name: 'frog.png' }).getAttribute('src')+ ).toBe(preview.url);+ expect(view.getByText(result.note)).toBeTruthy();+ expect(view.queryByText('Generate image')).toBeNull();+ expect(view.queryByRole('link')).toBeNull();+ });++ it('keeps the file card while the upload is pending without reading query data', () => {+ preview.isSuccess = false;+ const view = imageTool();+ expect(view.getByText('Preparing preview')).toBeTruthy();+ expect(view.getByRole('button')).toBeTruthy();+ expect(preview.dataReads).toBe(0);+ });++ it('keeps the file card when the preview query fails', () => {+ preview.isSuccess = false;+ preview.isError = true;+ const view = imageTool();+ expect(view.getByText('Preview unavailable')).toBeTruthy();+ expect(view.getByRole('button')).toBeTruthy();+ expect(preview.dataReads).toBe(0);+ });++ it('replaces a failed image load with an inline status', () => {+ const view = imageTool();+ fireEvent.error(view.getByRole('img'));+ expect(view.getByText('Preview unavailable')).toBeTruthy();+ expect(view.queryByRole('img')).toBeNull();+ expect(view.getByRole('button')).toBeTruthy();+ });++ it('shows the requested filename while generation is pending', () => {+ const view = imageTool(false);+ expect(view.getByText('frog')).toBeTruthy();+ expect(view.getByText('Generating image')).toBeTruthy();+ expect(view.queryByRole('button')).toBeNull();+ });++ it('replaces a pending generation with its failure state', () => {+ const [error, setError] = createSignal<string>();+ const view = imageTool(false, error);+ expect(view.getByText('Generating image')).toBeTruthy();+ setError('failed');+ expect(view.getByText('Image generation failed')).toBeTruthy();+ expect(view.queryByText('Generating image')).toBeNull();+ });++ it('shows a failed generation without a document button', () => {+ const view = imageTool(false, 'failed');+ expect(view.getByText('Image generation failed')).toBeTruthy();+ expect(view.queryByRole('button')).toBeNull();+ expect(view.queryByText('Generating image')).toBeNull();+ });+});diff --git a/apps/web/src/lib/core/component/AI/component/tool/GenerateImage.tsx b/apps/web/src/lib/core/component/AI/component/tool/GenerateImage.tsxnew file mode 100644index 0000000000..bff71dcd59--- /dev/null+++ b/apps/web/src/lib/core/component/AI/component/tool/GenerateImage.tsx@@ -0,0 +1,126 @@+import { useSplitLayout } from '@components/app/split-layout/layout';+import { openInNewSplitForMention } from '@core/util/openInNewSplit';+import { useSplitNavigationHandler } from '@core/util/useSplitNavigationHandler';+import LoadingSpinner from '@phosphor/spinner.svg';+import Image from '@phosphor-icons/core/regular/image.svg';+import { useBinaryDocumentQuery } from '@queries/storage/binary-document';+import { createSignal, Show, Suspense } from 'solid-js';+import { createToolRenderer, useToolError } from './ToolRenderer';++function ImageHeader(props: { fileName: string }) {+ return (+ <div class="flex w-0 min-w-full items-center gap-2 rounded-t-xl border-b border-edge-muted bg-surface px-3 py-2 text-sm text-ink">+ <Image class="size-4 shrink-0 text-ink-muted" />+ <span class="min-w-0 truncate" title={props.fileName}>+ {props.fileName}+ </span>+ </div>+ );+}++function PreviewStatus(props: { failed?: boolean; generating?: boolean }) {+ return (+ <div+ class="flex h-64 items-center justify-center gap-2 bg-surface text-sm text-ink-muted"+ role="status"+ >+ <Show when={!props.failed}>+ <LoadingSpinner class="size-4 animate-spin" />+ </Show>+ <span>+ {props.failed+ ? 'Preview unavailable'+ : props.generating+ ? 'Generating image'+ : 'Preparing preview'}+ </span>+ </div>+ );+}++/** Storage finishes the upload asynchronously; its query waits for a readable URL. */+function GeneratedImage(props: { documentId: string; fileName: string }) {+ const query = useBinaryDocumentQuery(() => props.documentId);+ // A pending query must not suspend the surrounding transcript.+ const url = () => (query.isSuccess ? query.data : undefined);+ const [failedUrl, setFailedUrl] = createSignal<string>();+ const failed = () =>+ query.isError || (url() !== undefined && failedUrl() === url());+ const { insertSplit, replaceOrInsertSplit } = useSplitLayout();+ const open = useSplitNavigationHandler<HTMLButtonElement>((event) => {+ const split = { type: 'image' as const, id: props.documentId };+ const handle = openInNewSplitForMention(event.shiftKey, true)+ ? insertSplit(split)+ : replaceOrInsertSplit(split);+ handle?.activate();+ });++ return (+ <button+ type="button"+ aria-label={`Open ${props.fileName} in a new split`}+ class="block w-fit max-w-full overflow-hidden rounded-xl border border-edge-muted text-left hover:border-accent hover-transition-border focus-visible:outline-2 focus-visible:outline-accent"+ {...open}+ >+ <ImageHeader fileName={props.fileName} />+ <Suspense fallback={<PreviewStatus />}>+ <Show+ when={url() && !failed()}+ fallback={<PreviewStatus failed={failed()} />}+ >+ <img+ src={url()}+ alt={props.fileName}+ class="block h-auto max-h-96 w-auto max-w-full"+ onError={() => setFailedUrl(url())}+ />+ </Show>+ </Suspense>+ </button>+ );+}++export const generateImageHandler = createToolRenderer({+ name: 'GenerateImage',+ render: (ctx) => {+ const error = () => useToolError();+ return (+ <Show+ when={ctx.response?.data}+ fallback={+ <div class="w-full max-w-md overflow-hidden rounded-xl border border-edge-muted">+ <ImageHeader+ fileName={ctx.tool.data.fileName ?? 'Generated image'}+ />+ <Show+ when={!error()}+ fallback={+ <div class="px-3 py-4 text-sm text-ink-muted">+ Image generation failed+ </div>+ }+ >+ <PreviewStatus generating />+ </Show>+ </div>+ }+ >+ {(result) => (+ <div class="my-2 max-w-md space-y-2">+ <GeneratedImage+ documentId={result().documentId}+ fileName={result().fileName}+ />+ <Show when={result().note}>+ {(note) => (+ <div class="whitespace-pre-wrap text-sm text-ink-muted">+ {note()}+ </div>+ )}+ </Show>+ </div>+ )}+ </Show>+ );+ },+});diff --git a/apps/web/src/lib/core/component/AI/component/tool/handler.tsx b/apps/web/src/lib/core/component/AI/component/tool/handler.tsxindex 5e697c42fc..65475b6d31 100644--- a/apps/web/src/lib/core/component/AI/component/tool/handler.tsx+++ b/apps/web/src/lib/core/component/AI/component/tool/handler.tsx@@ -39,6 +39,7 @@ import {} from './DocumentComments';import { editDocumentHandler } from './EditDocument';import { editTagHandler } from './EditTag';+import { generateImageHandler } from './GenerateImage';import { getThreadHandler } from './GetThread';import {createImportEntityHandler,@@ -154,6 +155,7 @@ const toolHandlers: ToolHandlerMap<RenderContext> = {ContentSearch: contentSearchHandler,CreateDocument: createDocumentHandler,UploadFile: uploadFileHandler,+ GenerateImage: generateImageHandler,CreateProject: createProjectHandler,CreateReminder: createReminderHandler,CreateTag: createTagHandler,diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.tsindex c51b10204b..e3b68ca97c 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts@@ -2552,6 +2552,60 @@ export const EditTagResponse = z.object({summary: z.string(),});+export const GenerateImage = z.object({+ prompt: z.string(),+ fileName: z.union([z.string(), z.null()]).optional(),+ aspectRatio: z+ .union([+ z.enum(['square', 'landscape', 'portrait', 'widescreen', 'tall']),+ z.null(),+ ])+ .optional(),+ projectId: z.union([z.string().uuid(), z.null()]).optional(),+ referenceImages: z+ .union([+ z+ .array(+ z.any().superRefine((x, ctx) => {+ const schemas = [+ z.object({ id: z.string().uuid(), type: z.literal('document') }),+ z.object({+ id: z.string().uuid(),+ type: z.literal('staticFile'),+ }),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) =>+ result.error ? [...errors, result.error] : errors)(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ })+ )+ .max(3),+ z.null(),+ ])+ .optional(),+});++export const GenerateImageResponse = z.object({+ documentId: z.string(),+ fileName: z.string(),+ mimeType: z.string(),+ sizeBytes: z.number().int().gte(0),+ note: z.union([z.string(), z.null()]).optional(),+});+export const GetBotWebhooks = z.object({ botId: z.string().uuid() });export const GetBotWebhooksResponse = z.object({diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.tsindex 82e44c5e87..b04b46b144 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts@@ -94,6 +94,10 @@ type ToolParserMap = {response: types.SpreadsheetResponse;};EditTag: { call: types.EditTag; response: types.EditTagResponse };+ GenerateImage: {+ call: types.GenerateImage;+ response: types.GenerateImageResponse;+ };GetBotWebhooks: {call: types.GetBotWebhooks;response: types.GetBotWebhooksResponse;@@ -386,6 +390,10 @@ const toolParserMap = {response: schemas.SpreadsheetResponse,},EditTag: { call: schemas.EditTag, response: schemas.EditTagResponse },+ GenerateImage: {+ call: schemas.GenerateImage,+ response: schemas.GenerateImageResponse,+ },GetBotWebhooks: {call: schemas.GetBotWebhooks,response: schemas.GetBotWebhooksResponse,@@ -713,6 +721,10 @@ type ToolDataMap = {response: types.SpreadsheetResponse;};EditTag: { call: types.EditTag; response: types.EditTagResponse };+ GenerateImage: {+ call: types.GenerateImage;+ response: types.GenerateImageResponse;+ };GetBotWebhooks: {call: types.GetBotWebhooks;response: types.GetBotWebhooksResponse;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.tsindex 9e629871ae..0564c7210d 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts@@ -524,6 +524,30 @@ export type SpreadsheetOperation =columns: SpreadsheetColumnWidth[];type: 'resize_columns';};+export type AspectRatio =+ | 'square'+ | 'landscape'+ | 'portrait'+ | 'widescreen'+ | 'tall';+/**+ * A reference photo already stored in Macro.+ */+export type ImageReferenceInput =+ | {+ /**+ * The Macro document ID.+ */+ id: string;+ type: 'document';+ }+ | {+ /**+ * Uploaded file ID from the /file/<id> segment of its attachment URL.+ */+ id: string;+ type: 'staticFile';+ };/*** Entity types that can be returned by the list entities AI tool.*/@@ -3509,6 +3533,63 @@ export interface EditTagResponse {*/summary: string;}+/**+ * Generate or edit an image with Google's Nano Banana image model and save the result as a new image document in Macro. Use for pictures, illustrations, diagram concepts, logo ideas, mockups, or edits based on reference photos. When the user supplies photos or asks to modify an existing image, pass them in referenceImages; describing a photo in the prompt alone does not send it to the image model. Describe the subject, style, composition, lighting, and any text to render; only the prompt is required. Refer to reference images by their order (image 1, image 2, image 3) when explaining how to use them. Returns the new document ID to cite inline. Generation takes several seconds.+ */+export interface GenerateImage {+ /**+ * Detailed description of the image to generate: subject, style (photo, illustration, flat vector...), composition, colours, mood, and any text that must appear.+ */+ prompt: string;+ /**+ * Optional short descriptive name for the saved image, for example `sunset-lighthouse`. The file extension is added from the generated format. No directory path. Omit to name the image after the prompt.+ */+ fileName?: string | null;+ /**+ * Shape of the image. Omit for the model default (square). `widescreen` (16:9) suits banners and slides, `tall` (9:16) suits phone screens and stories.+ */+ aspectRatio?: AspectRatio | null;+ /**+ * Optional destination project (folder) ID. Requires edit access. Omit to save to the user's top-level files.+ */+ projectId?: string | null;+ /**+ * Up to three reference photos, in the order used by the prompt. Use type document with a Macro image document ID, or type staticFile with the UUID from /file/<id> in an uploaded attachment's source URL. Use the actual IDs supplied in the conversation or by tools; do not invent IDs. Omit for text-only generation.+ *+ * @maxItems 3+ */+ referenceImages?:+ | []+ | [ImageReferenceInput]+ | [ImageReferenceInput, ImageReferenceInput]+ | [ImageReferenceInput, ImageReferenceInput, ImageReferenceInput]+ | null;+}+/**+ * Where the generated image landed. Does not echo the image bytes.+ */+export interface GenerateImageResponse {+ /**+ * ID of the new image document.+ */+ documentId: string;+ /**+ * Saved filename, including its extension.+ */+ fileName: string;+ /**+ * IANA media type of the image, e.g. `image/png`.+ */+ mimeType: string;+ /**+ * Size of the image in bytes.+ */+ sizeBytes: number;+ /**+ * Commentary the model produced alongside the image, when any.+ */+ note?: string | null;+}/*** Get the channel-specific webhook URLs for a bot the current user can manage. A bot has one URL per channel it can access. POST message content to a returned webhookUrl and authenticate with a token minted from the chat card or bot settings after IssueBotCredential or CreateBot; send it in the returned credentialHeader and send credentialScope in credentialScopeHeader. If no URLs are returned, add the bot to a channel with ManageBotChannelAccess or recreate it with CreateBot and channelId.*/diff --git a/crates/agent_inmem/Cargo.toml b/crates/agent_inmem/Cargo.tomlindex 39ca2aca3e..ddd2f525f3 100644--- a/crates/agent_inmem/Cargo.toml+++ b/crates/agent_inmem/Cargo.toml@@ -46,6 +46,7 @@ thiserror = { workspace = true }tokio = { workspace = true, features = ["sync", "rt", "time", "macros"] }tokio-util = { workspace = true }tracing = { workspace = true }+url = { workspace = true }workspace-hack = { version = "0.1", path = "../workspace-hack" }[dev-dependencies]diff --git a/crates/agent_inmem/src/domain/session.rs b/crates/agent_inmem/src/domain/session.rsindex 6ecae7335f..32b90815c3 100644--- a/crates/agent_inmem/src/domain/session.rs+++ b/crates/agent_inmem/src/domain/session.rs@@ -150,6 +150,22 @@ fn attachment_content(attachment: &PromptAttachment) -> AttachmentContent<'statiattachment.name, attachment.uri))};+ let mut content = NonEmpty::one(part);+ if is_image && fetchable {+ // The model conversion emits content parts, not AttachmentContent's+ // name/reference fields. Keep the source visible even when a local+ // adapter replaces the image URL with inline bytes, so tools can reuse+ // an uploaded reference. This describes any image URL without claiming+ // that a third-party image is a Macro static file.+ content.push(AttachmentPart::Metadata {+ key: "image_file_name".to_owned(),+ value: attachment.name.clone(),+ });+ content.push(AttachmentPart::Metadata {+ key: "image_source_uri".to_owned(),+ value: attachment.uri.clone(),+ });+ }AttachmentContent {// The static file id is the URL's last path segment; a URL shaped// some other way is identified by the whole URL.@@ -163,7 +179,7 @@ fn attachment_content(attachment: &PromptAttachment) -> AttachmentContent<'stati.to_owned(),),name: Some(attachment.name.clone()),- content: NonEmpty::one(part),+ content,}}diff --git a/crates/agent_inmem/src/domain/session/test.rs b/crates/agent_inmem/src/domain/session/test.rsindex a7021390d5..ba39d5b6b6 100644--- a/crates/agent_inmem/src/domain/session/test.rs+++ b/crates/agent_inmem/src/domain/session/test.rs@@ -134,3 +134,36 @@ fn a_plain_http_image_is_named_rather_than_handed_over_as_an_image_url() {};assert!(text.contains("a.png") && text.contains("http://localhost:8100/file/1"));}++#[test]+fn image_names_and_source_uris_reach_the_model_after_history_replay() {+ use agent_client_protocol::RawJsonRpcMessage;+ use agent_runtime_protocol::domain::schema::v0::{AcpMessage, ToRuntimeMessage};+ use agent_session::domain::model::Message;++ for uri in [+ "https://local.example.test/static-file/file/00000000-0000-4000-8000-000000000001",+ "https://external.example/photo.png",+ ] {+ let prompt = PromptRequest::new(+ SessionId::new("replayed-session"),+ vec![link("reference-photo.png", uri, Some("image/png"))],+ );+ let raw: RawJsonRpcMessage = serde_json::from_value(serde_json::json!({+ "jsonrpc": "2.0",+ "id": 1,+ "method": "session/prompt",+ "params": prompt,+ }))+ .expect("logged request");+ let history = crate::domain::replay::replay_history([Message::ToRuntime(+ ToRuntimeMessage::Acp(AcpMessage(raw)),+ )]);+ let messages = messages_for_turn(&history, &UserPrompt::text("use that reference"));+ let serialized = serde_json::to_string(&agent::to_rig_messages(&messages))+ .expect("model messages serialize");+ assert!(serialized.contains("image_file_name: reference-photo.png"));+ assert!(serialized.contains(&format!("image_source_uri: {uri}")));+ assert!(!serialized.contains("static_file_id"));+ }+}diff --git a/crates/agent_inmem/src/outbound/local_attachments.rs b/crates/agent_inmem/src/outbound/local_attachments.rsnew file mode 100644index 0000000000..01d3adff71--- /dev/null+++ b/crates/agent_inmem/src/outbound/local_attachments.rs@@ -0,0 +1,219 @@+//! Materialize local static-file images before invoking a remote model.+//!+//! Browser-facing local URLs cannot be fetched by model providers. This+//! adapter resolves only the configured static-file route through its owning+//! attachment service; other URLs retain their ordinary provider behavior.++use std::sync::Arc;++use agent::{AgentError, StreamPart};+use attachment::image::ImageData;+use attachment::{AttachmentContent, AttachmentPart, AttachmentService, Attachments};+use futures::future::BoxFuture;+use macro_uuid::Uuid;+use model_entity::EntityType;+use model_owner::Owner;+use non_empty::NonEmpty;+use tokio::sync::mpsc;+use tracing::Instrument as _;+use url::Url;++use crate::domain::engine::{TurnEngine, TurnRequest};++#[cfg(test)]+mod test;++/// A local deployment's turn engine, with private static-file images inlined.+///+/// The composition root supplies the browser-facing base URL and an attachment+/// service that can read the files internally. Resolution covers the whole+/// conversation, including prompts replayed after a process restart.+pub struct LocalAttachmentTurnEngine<S> {+ inner: Arc<dyn TurnEngine>,+ resolver: Arc<LocalImageResolver<S>>,+}++impl<S> LocalAttachmentTurnEngine<S> {+ /// Wrap `inner`, resolving images under `{public_base_url}/file/{uuid}`.+ ///+ /// The base must be an HTTP(S) URL without credentials, query or fragment.+ /// Configure this wrapper only where that origin is private to the local+ /// stack. Production URLs should continue directly to the model provider.+ pub fn new(+ inner: Arc<dyn TurnEngine>,+ mut public_base_url: Url,+ service: S,+ ) -> anyhow::Result<Self> {+ anyhow::ensure!(+ matches!(public_base_url.scheme(), "http" | "https")+ && public_base_url.host_str().is_some()+ && public_base_url.username().is_empty()+ && public_base_url.password().is_none()+ && public_base_url.query().is_none()+ && public_base_url.fragment().is_none(),+ "local static-file base must be an HTTP(S) URL without credentials, query or fragment"+ );+ public_base_url.set_path(&format!(+ "{}/file/",+ public_base_url.path().trim_end_matches('/')+ ));+ Ok(Self {+ inner,+ resolver: Arc::new(LocalImageResolver {+ file_base_url: public_base_url,+ service,+ }),+ })+ }+}++impl<S: AttachmentService> TurnEngine for LocalAttachmentTurnEngine<S> {+ fn supported_models(&self) -> &[&str] {+ self.inner.supported_models()+ }++ fn run_turn(&self, mut request: TurnRequest) -> mpsc::Receiver<Result<StreamPart, AgentError>> {+ let (parts, receiver) = mpsc::channel(256);+ let resolver = Arc::clone(&self.resolver);+ let inner = Arc::clone(&self.inner);+ let cancel = request.cancel.clone();+ tokio::spawn(+ async move {+ let turn = async {+ // Before the engine starts, cancellation drops an in-flight+ // attachment read without spending a model request.+ let resolution = tokio::select! {+ biased;+ () = cancel.cancelled() => return,+ result = resolver.resolve_request(&mut request) => result,+ };+ if let Err(error) = resolution {+ let _ = parts.send(Err(AgentError::Other(error))).await;+ return;+ }+ // The running engine owns cooperative cancellation. Drain+ // its final tool results even after the token is cancelled.+ let mut stream = inner.run_turn(request);+ while let Some(part) = stream.recv().await {+ if parts.send(part).await.is_err() {+ break;+ }+ }+ };+ tokio::select! {+ biased;+ () = parts.closed() => cancel.cancel(),+ () = turn => {},+ }+ }+ .in_current_span(),+ );+ receiver+ }+}++struct LocalImageResolver<S> {+ file_base_url: Url,+ service: S,+}++impl<S> LocalImageResolver<S> {+ /// Accept only canonical file identifiers beneath the configured route.+ /// Comparing the original URL also rejects paths normalized by parsing,+ /// such as literal or encoded traversal and backslash separators.+ fn file_id(&self, uri: &str) -> Option<Uuid> {+ let url = Url::parse(uri).ok()?;+ if url.origin() != self.file_base_url.origin()+ || url.as_str() != uri+ || !url.username().is_empty()+ || url.password().is_some()+ || url.query().is_some()+ || url.fragment().is_some()+ {+ return None;+ }+ let id = url.path().strip_prefix(self.file_base_url.path())?;+ let parsed = Uuid::parse_str(id).ok()?;+ (parsed.to_string() == id).then_some(parsed)+ }+}++impl<S: AttachmentService> LocalImageResolver<S> {+ async fn resolve_request(&self, request: &mut TurnRequest) -> anyhow::Result<()> {+ for message in &mut request.messages {+ let Some(attachments) = message.attachments.take() else {+ continue;+ };+ let mut contents = attachments.into_parts().into_inner();+ for content in contents.iter_mut().flatten() {+ self.resolve_content(&request.owner, content).await?;+ }+ message.attachments = Some(Attachments::new(+ NonEmpty::new(contents).expect("resolving images preserves attachment count"),+ ));+ }+ Ok(())+ }++ fn resolve_content<'a>(+ &'a self,+ owner: &'a Owner,+ content: &'a mut AttachmentContent<'static>,+ ) -> BoxFuture<'a, anyhow::Result<()>> {+ Box::pin(async move {+ for index in 0..content.content.len() {+ match content.content.get_mut(index).expect("index is in bounds") {+ AttachmentPart::Image(image) => {+ if let ImageData::StaticUrl(uri) = image+ && let Some(file_id) = self.file_id(uri)+ {+ *image = self.resolve_image(owner, file_id).await?;+ }+ }+ AttachmentPart::Child(child) => {+ if let Ok(child) = child.as_mut() {+ self.resolve_content(owner, child).await?;+ }+ }+ _ => {}+ }+ }+ Ok(())+ })+ }++ async fn resolve_image(&self, owner: &Owner, file_id: Uuid) -> anyhow::Result<ImageData> {+ let user = owner.as_user().ok_or_else(|| {+ anyhow::anyhow!("local image attachments require a user session owner")+ })?;+ let entity = EntityType::StaticFile.with_entity_string(file_id.to_string());+ let entities = [&entity];+ let resolved = self+ .service+ .resolve_attachments(+ user.clone(),+ NonEmpty::new(entities.as_slice()).expect("one image reference"),+ )+ .await;+ let mut contents = resolved.into_parts().into_inner();+ anyhow::ensure!(+ contents.len() == 1,+ "local image resolution returned multiple files"+ );+ let content = contents+ .pop()+ .expect("one resolved file")+ .map_err(|error| {+ anyhow::anyhow!("could not read local image attachment: {}", error.error)+ })?;+ let mut parts = content.content.into_inner();+ anyhow::ensure!(+ parts.len() == 1,+ "local image resolution returned multiple parts"+ );+ match parts.pop().expect("one resolved part") {+ AttachmentPart::Image(image @ ImageData::Base64(_)) => Ok(image),+ _ => anyhow::bail!("local image resolution did not return inline image content"),+ }+ }+}diff --git a/crates/agent_inmem/src/outbound/local_attachments/test.rs b/crates/agent_inmem/src/outbound/local_attachments/test.rsnew file mode 100644index 0000000000..a541dc635f--- /dev/null+++ b/crates/agent_inmem/src/outbound/local_attachments/test.rs@@ -0,0 +1,377 @@+use std::sync::Mutex;+use std::sync::atomic::{AtomicBool, Ordering};+use std::time::Duration;++use agent::types::{ChatMessage, ChatMessageContent, Role};+use attachment::{AttachmentError, ResolutionError};+use macro_user_id::user_id::MacroUserIdStr;+use model_entity::Entity;+use tokio::sync::Notify;+use tokio_util::sync::CancellationToken;++use super::*;++const BASE: &str = "https://local.example.test:8443/static-file";+const FILE_ID: &str = "00000000-0000-4000-8000-000000000001";+const ONE_BY_ONE_PNG: &[u8] = &[+ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,+ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,+ 0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0xF8, 0xCF, 0xC0, 0x00,+ 0x00, 0x03, 0x01, 0x01, 0x00, 0xC9, 0xFE, 0x92, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E,+ 0x44, 0xAE, 0x42, 0x60, 0x82,+];++#[derive(Default)]+struct ServiceState {+ calls: Mutex<Vec<(String, String)>>,+ started: Notify,+ dropped: AtomicBool,+}++#[derive(Clone, Copy)]+enum Outcome {+ Image,+ Error,+ Url,+ Pending,+}++struct Service {+ state: Arc<ServiceState>,+ outcome: Outcome,+}++struct ReadGuard(Arc<ServiceState>);++impl Drop for ReadGuard {+ fn drop(&mut self) {+ self.0.dropped.store(true, Ordering::SeqCst);+ }+}++impl AttachmentService for Service {+ async fn resolve_attachments<'a>(+ &self,+ user_id: MacroUserIdStr<'_>,+ ids: NonEmpty<&[&'a Entity<'a>]>,+ ) -> Attachments<'a> {+ let entity = ids[0];+ self.state+ .calls+ .lock()+ .expect("calls lock")+ .push((user_id.to_string(), entity.entity_id.to_string()));+ let _guard = ReadGuard(Arc::clone(&self.state));+ self.state.started.notify_one();+ let image = match self.outcome {+ Outcome::Image => ImageData::try_from_bytes(ONE_BY_ONE_PNG.to_vec()).expect("PNG"),+ Outcome::Url => ImageData::StaticUrl(format!("{BASE}/file/{FILE_ID}")),+ Outcome::Error => {+ return Attachments::one(Err(ResolutionError::new(+ EntityType::StaticFile.with_entity_string(entity.entity_id.to_string()),+ AttachmentError::NoContent,+ )));+ }+ Outcome::Pending => return std::future::pending().await,+ };+ Attachments::one(Ok(AttachmentContent {+ reference: entity.clone(),+ name: None,+ content: NonEmpty::one(AttachmentPart::Image(image)),+ }))+ }+}++#[derive(Default)]+struct Engine {+ requests: Mutex<Vec<TurnRequest>>,+}++impl TurnEngine for Engine {+ fn supported_models(&self) -> &[&str] {+ &["test-model"]+ }++ fn run_turn(&self, request: TurnRequest) -> mpsc::Receiver<Result<StreamPart, AgentError>> {+ self.requests.lock().expect("requests lock").push(request);+ let (parts, receiver) = mpsc::channel(1);+ parts+ .try_send(Ok(StreamPart::Content("reply".to_owned())))+ .expect("empty channel");+ receiver+ }+}++fn engine(+ outcome: Outcome,+) -> (+ LocalAttachmentTurnEngine<Service>,+ Arc<Engine>,+ Arc<ServiceState>,+) {+ let inner = Arc::new(Engine::default());+ let state = Arc::new(ServiceState::default());+ let engine = LocalAttachmentTurnEngine::new(+ inner.clone(),+ Url::parse(BASE).expect("base URL"),+ Service {+ state: Arc::clone(&state),+ outcome,+ },+ )+ .expect("valid local engine");+ (engine, inner, state)+}++fn content(uri: &str) -> AttachmentContent<'static> {+ AttachmentContent {+ reference: EntityType::StaticFile.with_entity_string("original-reference".to_owned()),+ name: Some("original-name.png".to_owned()),+ content: NonEmpty::new(vec![+ AttachmentPart::Content("caption".to_owned()),+ AttachmentPart::Image(ImageData::StaticUrl(uri.to_owned())),+ ])+ .expect("two parts"),+ }+}++fn message(uri: &str) -> ChatMessage {+ ChatMessage {+ content: ChatMessageContent::Text("look at this".to_owned()),+ role: Role::User,+ attachments: Some(Attachments::one(Ok(content(uri)))),+ }+}++fn request(messages: Vec<ChatMessage>) -> TurnRequest {+ TurnRequest {+ owner: Owner::from_principal_str("macro|[email protected]").expect("user"),+ model: "test-model".to_owned(),+ reasoning_effort: agent::ReasoningEffort::default(),+ identity: None,+ instructions: None,+ messages,+ mcp_tools: None,+ cancel: CancellationToken::new(),+ user_input: None,+ reviewer: None,+ }+}++fn attachment(message: &ChatMessage) -> &AttachmentContent<'static> {+ message.attachments.as_ref().expect("attachments").parts()[0]+ .as_ref()+ .expect("resolved content")+}++#[tokio::test]+async fn historical_images_are_inlined_and_original_content_is_preserved() {+ let (engine, inner, state) = engine(Outcome::Image);+ let mut turn = request(vec![message(&format!("{BASE}/file/{FILE_ID}"))]);+ turn.messages.push(ChatMessage {+ content: ChatMessageContent::Text("try again".to_owned()),+ role: Role::User,+ attachments: None,+ });+ assert_eq!(engine.supported_models(), &["test-model"]);+ let mut stream = engine.run_turn(turn);+ assert!(matches!(stream.recv().await, Some(Ok(StreamPart::Content(text))) if text == "reply"));+ assert!(stream.recv().await.is_none());++ let requests = inner.requests.lock().expect("requests lock");+ let attachment = attachment(&requests[0].messages[0]);+ assert_eq!(attachment.reference.entity_id, "original-reference");+ assert_eq!(attachment.name.as_deref(), Some("original-name.png"));+ assert!(matches!(&attachment.content[0], AttachmentPart::Content(text) if text == "caption"));+ assert!(matches!(+ &attachment.content[1],+ AttachmentPart::Image(ImageData::Base64(_))+ ));+ assert_eq!(requests[0].messages[1].content.message_text(), "try again");+ assert_eq!(+ *state.calls.lock().expect("calls lock"),+ vec![("macro|[email protected]".to_owned(), FILE_ID.to_owned())]+ );+}++#[test]+fn only_the_exact_static_file_origin_and_canonical_id_are_matched() {+ let (engine, _, _) = engine(Outcome::Image);+ let resolver = &engine.resolver;+ assert_eq!(+ resolver.file_id(&format!("{BASE}/file/{FILE_ID}")),+ Some(Uuid::parse_str(FILE_ID).unwrap())+ );+ for uri in [+ format!("https://other.example.test:8443/static-file/file/{FILE_ID}"),+ format!("https://local.example.test:8444/static-file/file/{FILE_ID}"),+ format!("http://local.example.test:8443/static-file/file/{FILE_ID}"),+ format!("{BASE}-other/file/{FILE_ID}"),+ format!("{BASE}/api/file/{FILE_ID}"),+ format!("{BASE}/internal/file/{FILE_ID}"),+ format!("{BASE}/file/not-a-uuid"),+ format!("{BASE}/file/{FILE_ID}/extra"),+ format!("{BASE}/other/../file/{FILE_ID}"),+ format!("{BASE}/other/%2e%2e/file/{FILE_ID}"),+ format!("{BASE}/file/{FILE_ID}?redirect=http://localhost"),+ format!("{BASE}/file/{FILE_ID}#fragment"),+ format!("https://user:[email protected]:8443/static-file/file/{FILE_ID}"),+ ] {+ assert_eq!(resolver.file_id(&uri), None, "must not resolve {uri}");+ }+}++#[tokio::test]+async fn public_images_are_left_for_the_provider_without_internal_reads() {+ let (engine, _, state) = engine(Outcome::Image);+ let uri = "https://public.example/file/picture.png";+ let mut turn = request(vec![message(uri)]);+ engine+ .resolver+ .resolve_request(&mut turn)+ .await+ .expect("resolve");+ assert!(+ matches!(&attachment(&turn.messages[0]).content[1], AttachmentPart::Image(ImageData::StaticUrl(url)) if url == uri)+ );+ assert!(state.calls.lock().expect("calls lock").is_empty());+}++#[tokio::test]+async fn nested_images_are_resolved_without_replacing_the_parent() {+ let (engine, _, _) = engine(Outcome::Image);+ let child = content(&format!("{BASE}/file/{FILE_ID}"));+ let mut turn = request(vec![message("https://public.example/image.png")]);+ turn.messages[0].attachments = Some(Attachments::one(Ok(AttachmentContent {+ reference: EntityType::Document.with_entity_string("parent-document".to_owned()),+ name: Some("parent".to_owned()),+ content: NonEmpty::one(AttachmentPart::Child(Box::new(Ok(child)))),+ })));+ engine+ .resolver+ .resolve_request(&mut turn)+ .await+ .expect("resolve");+ let parent = attachment(&turn.messages[0]);+ assert_eq!(parent.reference.entity_id, "parent-document");+ let AttachmentPart::Child(child) = &parent.content[0] else {+ panic!("child preserved")+ };+ assert!(matches!(+ &child.as_ref().as_ref().expect("child").content[1],+ AttachmentPart::Image(ImageData::Base64(_))+ ));+}++#[tokio::test]+async fn resolution_failures_and_non_inline_results_never_reach_the_model() {+ for outcome in [Outcome::Error, Outcome::Url] {+ let (engine, inner, _) = engine(outcome);+ let mut stream = engine.run_turn(request(vec![message(&format!("{BASE}/file/{FILE_ID}"))]));+ assert!(matches!(stream.recv().await, Some(Err(_))));+ assert!(stream.recv().await.is_none());+ assert!(inner.requests.lock().expect("requests lock").is_empty());+ }+}++#[tokio::test]+async fn non_user_owners_do_not_resolve_files_under_an_invented_identity() {+ let (engine, inner, state) = engine(Outcome::Image);+ let mut turn = request(vec![message(&format!("{BASE}/file/{FILE_ID}"))]);+ turn.owner = Owner::Bot(bot_id::BotId::TEST_A);+ let mut stream = engine.run_turn(turn);+ assert!(matches!(stream.recv().await, Some(Err(_))));+ assert!(inner.requests.lock().expect("requests lock").is_empty());+ assert!(state.calls.lock().expect("calls lock").is_empty());+}++#[tokio::test]+async fn cancelling_or_dropping_the_receiver_stops_pending_resolution() {+ for drop_receiver in [false, true] {+ let (engine, inner, state) = engine(Outcome::Pending);+ let turn = request(vec![message(&format!("{BASE}/file/{FILE_ID}"))]);+ let cancel = turn.cancel.clone();+ let mut stream = engine.run_turn(turn);+ tokio::time::timeout(Duration::from_secs(2), state.started.notified())+ .await+ .expect("resolution starts");+ if drop_receiver {+ drop(stream);+ } else {+ cancel.cancel();+ assert!(+ tokio::time::timeout(Duration::from_secs(2), stream.recv())+ .await+ .expect("stream closes")+ .is_none()+ );+ }+ tokio::time::timeout(Duration::from_secs(2), async {+ while !state.dropped.load(Ordering::SeqCst) {+ tokio::task::yield_now().await;+ }+ })+ .await+ .expect("attachment read is dropped");+ assert!(cancel.is_cancelled());+ assert!(inner.requests.lock().expect("requests lock").is_empty());+ }+}++struct CooperativeEngine {+ started: Arc<Notify>,+}++impl TurnEngine for CooperativeEngine {+ fn supported_models(&self) -> &[&str] {+ &["test-model"]+ }++ fn run_turn(&self, request: TurnRequest) -> mpsc::Receiver<Result<StreamPart, AgentError>> {+ let (parts, receiver) = mpsc::channel(1);+ let started = Arc::clone(&self.started);+ tokio::spawn(async move {+ started.notify_one();+ request.cancel.cancelled().await;+ let _ = parts+ .send(Ok(StreamPart::ToolResponse(agent::ToolResponse::Json {+ id: "running-tool".to_owned(),+ name: "cooperative-tool".to_owned(),+ json: serde_json::json!({"status": "cancelled"}),+ })))+ .await;+ });+ receiver+ }+}++#[tokio::test]+async fn a_running_engine_drains_its_final_tool_response_after_cancellation() {+ let started = Arc::new(Notify::new());+ let engine = LocalAttachmentTurnEngine::new(+ Arc::new(CooperativeEngine {+ started: Arc::clone(&started),+ }),+ Url::parse(BASE).expect("base URL"),+ Service {+ state: Arc::new(ServiceState::default()),+ outcome: Outcome::Image,+ },+ )+ .expect("local engine");+ let turn = request(vec![message(&format!("{BASE}/file/{FILE_ID}"))]);+ let cancel = turn.cancel.clone();+ let mut stream = engine.run_turn(turn);+ tokio::time::timeout(Duration::from_secs(2), started.notified())+ .await+ .expect("inner engine starts");+ cancel.cancel();+ let final_part = tokio::time::timeout(Duration::from_secs(2), stream.recv())+ .await+ .expect("inner engine drains");+ assert!(matches!(final_part,+ Some(Ok(StreamPart::ToolResponse(agent::ToolResponse::Json { id, json, .. })))+ if id == "running-tool" && json == serde_json::json!({"status": "cancelled"})+ ));+ assert!(stream.recv().await.is_none());+}diff --git a/crates/agent_inmem/src/outbound/mod.rs b/crates/agent_inmem/src/outbound/mod.rsindex 3eaa8e7833..feaeefb7d9 100644--- a/crates/agent_inmem/src/outbound/mod.rs+++ b/crates/agent_inmem/src/outbound/mod.rs@@ -2,6 +2,7 @@pub mod acp_mcp;pub mod egress_mcp;+pub mod local_attachments;pub mod log_frames;pub mod manager;pub mod tool_catalog;diff --git a/crates/ai_tools/Cargo.toml b/crates/ai_tools/Cargo.tomlindex e95d5aec1d..42b9cebe03 100644--- a/crates/ai_tools/Cargo.toml+++ b/crates/ai_tools/Cargo.toml@@ -15,6 +15,7 @@ agent = { path = "../agent" }ai_usage = { path = "../ai_usage" }anthropic = { path = "../anthropic", features = ["toolset"] }attachment = { path = "../attachment" }+static_file = { path = "../static_file", features = ["attachment"] }non_empty = { path = "../non_empty" }ai_toolset = { path = "../ai_toolset" }anyhow.workspace = true@@ -37,7 +38,7 @@ contacts = { path = "../contacts", default-features = false, features = ["outbound",] }crm = { path = "../crm" }-documents = { path = "../documents", features = ["ai_tools", "purge"] }+documents = { path = "../documents", features = ["ai_tools", "attachment", "purge"] }email = { path = "../email", features = ["ai_schema", "ai_tools", "axum", "outbound"] }email_service_client = { path = "../email_service_client" }entity_access = { path = "../entity_access" }@@ -47,6 +48,7 @@ entity_registry_db_utils = { path = "../entity_registry/entity_registry_db_utilsforeign_entity = { path = "../foreign_entity" }frecency = { path = "../frecency", features = ["outbound", "postgres"] }futures.workspace = true+image_generation = { path = "../image_generation" }import = { path = "../import" }item_filters = { path = "../item_filters" }lazy_static.workspace = truediff --git a/crates/ai_tools/src/build_context.rs b/crates/ai_tools/src/build_context.rsindex 0bc6204894..821665cc01 100644--- a/crates/ai_tools/src/build_context.rs+++ b/crates/ai_tools/src/build_context.rs@@ -33,6 +33,8 @@ use foreign_entity::{};use frecency::domain::services::FrecencyQueryServiceImpl;use frecency::outbound::postgres::FrecencyPgStorage;+use image_generation::domain::ports::{ImageGenerator, UnconfiguredImageGenerator};+use image_generation::outbound::gemini::GeminiImageGenerator;use lexical_client::LexicalClient;use macro_env::Environment;use macro_env_var::{env_var, maybe_env_var};@@ -75,6 +77,28 @@ maybe_env_var! {}}+maybe_env_var! {+ struct GoogleGenerativeAiApiKey;+}++/// The text-to-image provider for the GenerateImage tool: Gemini's Nano+/// Banana model when `GOOGLE_GENERATIVE_AI_API_KEY` is set, otherwise the+/// unconfigured generator, whose calls fail with a clear message rather than+/// keeping the host from booting.+pub fn build_image_generator_from_env() -> Arc<dyn ImageGenerator> {+ match GoogleGenerativeAiApiKey::new()+ .as_ref()+ .and_then(|key| key.value())+ .filter(|key| !key.trim().is_empty())+ {+ Some(key) => Arc::new(GeminiImageGenerator::new(key.to_string())),+ None => {+ tracing::warn!("GOOGLE_GENERATIVE_AI_API_KEY is not set; GenerateImage is disabled");+ Arc::new(UnconfiguredImageGenerator)+ }+ }+}+/// Builds a [`ToolServiceContext`] by reading the required environment/// variables and wiring up all the shared services.///@@ -432,6 +456,10 @@ pub async fn build_tool_service_context_from_env(properties_service,entity_access_service.clone(),),+ image_generation_tool_context: crate::build_image_generation_tool_context(+ &document_tool_context,+ build_image_generator_from_env(),+ )?,document_tool_context,properties_tool_context,email_tool_context,diff --git a/crates/ai_tools/src/lib.rs b/crates/ai_tools/src/lib.rsindex ee2e181ac1..a68694aeda 100644--- a/crates/ai_tools/src/lib.rs+++ b/crates/ai_tools/src/lib.rs@@ -46,10 +46,11 @@ use teams::inbound::toolset::team_toolset;#[cfg(any(test, feature = "test-support"))]pub use build_context::build_anthropic_tool_context_test;-pub use build_context::{build_anthropic_tool_context, build_tool_service_context_from_env};+pub use build_context::{+ build_anthropic_tool_context, build_image_generator_from_env,+ build_tool_service_context_from_env,+};pub use search::search_toolset;-#[cfg(any(test, feature = "test-support"))]-pub use tool_context::no_op_schedule_context;pub use tool_context::{ChannelSideEffectClients, NoOpCallRtcClient, NoOpConnectionService, NoOpNotificationIngress,NoOpNotificationService, NoOpScheduleContext, NoOpSnsEndpointManager, NoOpTaskProperties,@@ -60,21 +61,24 @@ pub use tool_context::{ToolChatService, ToolChatToolContext, ToolCommsService, ToolCrmService, ToolCrmToolContext,ToolDocumentService, ToolDocumentToolContext, ToolEmailService, ToolEmailToolContext,ToolEntityAccessManagementService, ToolEntityAccessService, ToolEntityCreator,- ToolForeignEntityService, ToolFrecencyService, ToolImportService, ToolImportToolContext,- ToolInitiativeToolContext, ToolMcpSelector, ToolNotificationQueue, ToolNotificationService,- ToolNotificationToolContext, ToolPipedreamConnection, ToolProjectService,- ToolProjectToolContext, ToolPropertiesService, ToolPropertiesToolContext, ToolRemindersService,- ToolRemindersToolContext, ToolServiceContext, ToolSkillService, ToolSkillToolContext,- ToolSoupService, ToolSystemPropertiesService, ToolTeamService, ToolTeamToolContext,- ToolUserEmailService, build_activity_tool_context, build_bot_tool_context,- build_calendar_tool_context, build_channel_tool_context_with_dispatcher,- build_channel_tool_context_with_side_effects, build_channel_tool_context_without_side_effects,- build_crm_tool_context, build_initiative_tool_context, build_message_service_with_side_effects,- build_message_service_without_side_effects, build_project_tool_context,- build_properties_service, build_properties_service_with_broker, build_properties_tool_context,- build_reminders_tool_context, build_skill_tool_context, build_task_properties_adapter,- build_team_repository, build_team_tool_context,+ ToolForeignEntityService, ToolFrecencyService, ToolImageGenerationToolContext,+ ToolImportService, ToolImportToolContext, ToolInitiativeToolContext, ToolMcpSelector,+ ToolNotificationQueue, ToolNotificationService, ToolNotificationToolContext,+ ToolPipedreamConnection, ToolProjectService, ToolProjectToolContext, ToolPropertiesService,+ ToolPropertiesToolContext, ToolRemindersService, ToolRemindersToolContext, ToolServiceContext,+ ToolSkillService, ToolSkillToolContext, ToolSoupService, ToolSystemPropertiesService,+ ToolTeamService, ToolTeamToolContext, ToolUserEmailService, build_activity_tool_context,+ build_bot_tool_context, build_calendar_tool_context,+ build_channel_tool_context_with_dispatcher, build_channel_tool_context_with_side_effects,+ build_channel_tool_context_without_side_effects, build_crm_tool_context,+ build_image_generation_tool_context, build_initiative_tool_context,+ build_message_service_with_side_effects, build_message_service_without_side_effects,+ build_project_tool_context, build_properties_service, build_properties_service_with_broker,+ build_properties_tool_context, build_reminders_tool_context, build_skill_tool_context,+ build_task_properties_adapter, build_team_repository, build_team_tool_context,};+#[cfg(any(test, feature = "test-support"))]+pub use tool_context::{build_image_generation_tool_context_test, no_op_schedule_context};pub type AiToolSet = AsyncToolCollection<ToolServiceContext>;pub struct ToolSetWithPrompt {@@ -100,6 +104,9 @@ pub(crate) fn subagent_toolset() -> AiToolSet {.add_tool::<ListEntities, SoupToolContext<ToolSoupService, ToolEmailService>>().add_subtoolset::<ToolActivityToolContext>(activity_toolset()).add_subtoolset::<ToolDocumentToolContext>(document_toolset())+ .add_subtoolset::<ToolImageGenerationToolContext>(+ image_generation::inbound::toolset::image_generation_toolset(),+ ).add_subtoolset::<ToolProjectToolContext>(project_toolset()).add_subtoolset::<ToolInitiativeToolContext>(initiative_toolset()).add_subtoolset::<ToolPropertiesToolContext>(properties_toolset())diff --git a/crates/ai_tools/src/test.rs b/crates/ai_tools/src/test.rsindex 3542e7164c..faa58a5233 100644--- a/crates/ai_tools/src/test.rs+++ b/crates/ai_tools/src/test.rs@@ -28,7 +28,11 @@ fn every_host_toolset_passes_schema_validation() {AiHost::ChannelBot,AiHost::Mcp,] {- let _ = tools_for(host);+ let tools = tools_for(host);+ assert!(+ tools.toolset.tools.contains_key("GenerateImage"),+ "{host:?} must expose image generation"+ );}}diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rsindex ee2d7cb514..e2871538ae 100644--- a/crates/ai_tools/src/tool_context.rs+++ b/crates/ai_tools/src/tool_context.rs@@ -65,6 +65,11 @@ use teams::{inbound::toolset::TeamToolContext, outbound::team_repo::TeamRepositouse tokio_util::task::TaskTracker;mod activity_metadata;+mod images;+#[cfg(any(test, feature = "test-support"))]+pub use images::build_image_generation_tool_context_test;+pub use images::{ToolImageGenerationToolContext, build_image_generation_tool_context};+mod initiatives;pub use initiatives::{ToolInitiativeToolContext, build_initiative_tool_context};@@ -1490,6 +1495,7 @@ pub struct ToolServiceContext {pub email_service: Arc<ToolEmailService>,pub activity_tool_context: ToolActivityToolContext,pub document_tool_context: ToolDocumentToolContext,+ pub image_generation_tool_context: ToolImageGenerationToolContext,pub properties_tool_context: ToolPropertiesToolContext,pub email_tool_context: ToolEmailToolContext,pub call_tool_context: ToolCallToolContext,@@ -1538,6 +1544,7 @@ impl ToolServiceContext {/// call this once when they build the context for that agent's session.pub fn with_actor(mut self, actor: bot_id::BotId) -> Self {self.document_tool_context = self.document_tool_context.with_actor(actor);+ self.image_generation_tool_context = self.image_generation_tool_context.with_actor(actor);self.properties_tool_context = self.properties_tool_context.with_actor(actor);self.project_tool_context = self.project_tool_context.with_actor(actor);self.initiative_tool_context = self.initiative_tool_context.with_actor(actor);diff --git a/crates/ai_tools/src/tool_context/images.rs b/crates/ai_tools/src/tool_context/images.rsnew file mode 100644index 0000000000..2142e0c21a--- /dev/null+++ b/crates/ai_tools/src/tool_context/images.rs@@ -0,0 +1,94 @@+//! Composition of image generation with document uploads and reference-image reads.+use super::{ToolDocumentService, ToolDocumentToolContext, ToolEntityAccessService};+use std::sync::Arc;++macro_env_var::env_var! {+ /// Static file bucket, used only with local AWS.+ pub struct StaticStorageBucket;+}++/// Reference photos resolved by their owning domains.+pub type ToolImageReferenceReader =+ image_generation::outbound::references::AttachmentImageReferenceReader<+ documents::inbound::attachment::DocumentAttachmentService<+ ToolDocumentService,+ ToolEntityAccessService,+ >,+ static_file::inbound::attachment::StaticFileAttachmentService<+ static_file::outbound::CdnStaticFileRepo,+ >,+ >;++/// Image generation service composed with the document upload capability.+pub type ToolImageGenerationService = image_generation::domain::service::ImageGenerationServiceImpl<+ image_generation::outbound::documents::DocumentsImageStore<+ documents::inbound::toolset::DefaultDocumentToolCreator<ToolDocumentService>,+ >,+ ToolImageReferenceReader,+>;++/// Image-generation context shared by all AI hosts.+pub type ToolImageGenerationToolContext =+ image_generation::inbound::toolset::ImageGenerationToolContext<+ ToolImageGenerationService,+ ToolEntityAccessService,+ >;++/// Compose image generation with an already-wired document upload service.+pub fn build_image_generation_tool_context(+ documents: &ToolDocumentToolContext,+ generator: Arc<dyn image_generation::domain::ports::ImageGenerator>,+) -> anyhow::Result<ToolImageGenerationToolContext> {+ // Local service URLs address the API container, while public file bytes+ // live in the static-file bucket. Providers receive bytes from this reader.+ let cdn_base = if let Some(local_aws) = macro_aws_config::LocalAwsUrl::new() {+ format!(+ "{}/{}",+ local_aws.as_ref().trim_end_matches('/'),+ StaticStorageBucket::new()?.as_ref(),+ )+ } else {+ macro_service_urls::StaticFileServiceUrl::new()?.to_string()+ };+ Ok(build_with_cdn(documents, generator, cdn_base))+}++fn build_with_cdn(+ documents: &ToolDocumentToolContext,+ generator: Arc<dyn image_generation::domain::ports::ImageGenerator>,+ cdn_base: String,+) -> ToolImageGenerationToolContext {+ let references = image_generation::outbound::references::AttachmentImageReferenceReader::new(+ documents::inbound::attachment::DocumentAttachmentService::new(+ documents.service.clone(),+ documents.entity_access_service.clone(),+ documents.lexical_client.clone(),+ ),+ static_file::inbound::attachment::StaticFileAttachmentService::new(Arc::new(+ static_file::outbound::CdnStaticFileRepo::new(cdn_base),+ )),+ );+ image_generation::inbound::toolset::ImageGenerationToolContext::new(+ image_generation::domain::service::ImageGenerationServiceImpl::new(+ generator,+ image_generation::outbound::documents::DocumentsImageStore::new(+ documents.creator.clone(),+ ),+ )+ .with_reference_reader(references),+ documents.entity_access_service.clone(),+ documents.actor,+ )+}++/// Test context with an explicitly unconfigured image provider.+#[cfg(any(test, feature = "test-support"))]+pub fn build_image_generation_tool_context_test(+ documents: &ToolDocumentToolContext,+) -> ToolImageGenerationToolContext {+ build_with_cdn(+ documents,+ Arc::new(image_generation::domain::ports::UnconfiguredImageGenerator),+ "https://static.example.test".to_string(),+ )+}diff --git a/crates/documents/src/domain/create/upload.rs b/crates/documents/src/domain/create/upload.rsindex 1deb988af9..0921fbeb70 100644--- a/crates/documents/src/domain/create/upload.rs+++ b/crates/documents/src/domain/create/upload.rs@@ -199,3 +199,21 @@ fn receipt_is_for(_ => false,}}++impl<Svc, MarkdownInit, BytesUpload, MentionTracker>+ crate::domain::ports::create::DocumentUploadService+ for DocumentCreator<Svc, MarkdownInit, BytesUpload, MentionTracker>+where+ Svc: DocumentCreationService + Clone + 'static,+ BytesUpload: DocumentBytesUploadPort + Clone + 'static,+ MarkdownInit: Send + Sync,+ MentionTracker: Send + Sync,+{+ async fn upload_file(+ &self,+ principal: &CreationPrincipal,+ upload: NewFileUpload,+ ) -> Result<CreatedDocument, DocumentError> {+ DocumentCreator::upload_file(self, principal, upload).await+ }+}diff --git a/crates/documents/src/domain/ports/create.rs b/crates/documents/src/domain/ports/create.rsindex 9ddd655737..8d9b92b9f5 100644--- a/crates/documents/src/domain/ports/create.rs+++ b/crates/documents/src/domain/ports/create.rs@@ -105,3 +105,13 @@ where(**self).cleanup_created_document(document_id).await}}++/// Inbound service for saving file bytes as a document using the existing upload lifecycle.+pub trait DocumentUploadService: Send + Sync {+ /// Validate the destination and file, create the document, and upload its bytes.+ fn upload_file(+ &self,+ principal: &CreationPrincipal,+ upload: crate::domain::create::upload::NewFileUpload,+ ) -> impl Future<Output = Result<crate::domain::create::CreatedDocument, DocumentError>> + Send;+}diff --git a/crates/documents/src/outbound/document_bytes_upload.rs b/crates/documents/src/outbound/document_bytes_upload.rsindex 3d084b44a2..68aec0ec2f 100644--- a/crates/documents/src/outbound/document_bytes_upload.rs+++ b/crates/documents/src/outbound/document_bytes_upload.rs@@ -27,9 +27,15 @@ impl DocumentBytesUploadPort for ReqwestDocumentBytesUploader {&self,upload: DocumentBytesUpload,) -> Result<(), DocumentError> {+ // The document service mints the presigned URL for a browser on the+ // host (`localhost:4566`); this uploader runs inside the Docker+ // network and has to use LocalStack's own hostname to reach the same+ // object. No-op outside local AWS.+ let presigned_url =+ macro_aws_config::transform_aws_url_for_internal_fetch(&upload.presigned_url);let upload_response = self.http_client- .put(&upload.presigned_url)+ .put(&presigned_url).header("content-type", &upload.content_type).header("x-amz-checksum-sha256", &upload.base64_sha256).body(upload.bytes)diff --git a/crates/image_generation/Cargo.toml b/crates/image_generation/Cargo.tomlnew file mode 100644index 0000000000..5bba06f950--- /dev/null+++ b/crates/image_generation/Cargo.toml@@ -0,0 +1,34 @@+[package]+name = "image_generation"+version = "0.1.0"+edition = "2024"+publish = false++[dependencies]+ai_toolset = { path = "../ai_toolset" }+anyhow.workspace = true+async-trait.workspace = true+attachment = { path = "../attachment" }+base64.workspace = true+bot_id = { path = "../bot_id" }+documents = { path = "../documents", default-features = false, features = ["document_create"] }+entity_access = { path = "../entity_access" }+model = { path = "../model" }+model-entity = { path = "../model-entity" }+model_owner = { path = "../model_owner" }+non_empty = { path = "../non_empty" }+reqwest.workspace = true+rootcause.workspace = true+schemars.workspace = true+serde.workspace = true+serde_json.workspace = true+thiserror.workspace = true+tracing.workspace = true+uuid.workspace = true+workspace-hack = { version = "0.1", path = "../workspace-hack" }++[dev-dependencies]+models_permissions = { path = "../models_permissions" }+macro_user_id = { path = "../macro_user_id" }+tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }+wiremock.workspace = truediff --git a/crates/image_generation/README.md b/crates/image_generation/README.mdnew file mode 100644index 0000000000..38ea312669--- /dev/null+++ b/crates/image_generation/README.md@@ -0,0 +1,24 @@+# Image generation++`image_generation_toolset()` exposes `GenerateImage` to the shared AI toolset.+The tool mints an edit receipt for an optional destination project and invokes+`ImageGenerationService` with the requesting user and delegated bot identity.++The domain service validates the prompt, resolves up to three ordered reference+images through `ImageReferenceReader`, asks `ImageGenerator` for image bytes,+chooses the filename, and saves through `ImageDocumentStore`. Gemini implements+the provider port and receives references as inline image bytes.+`DocumentsImageStore` implements the save port by calling the `documents`+domain's `DocumentUploadService` inbound port.++`AttachmentImageReferenceReader` uses the document and static-file domains'+existing inbound `AttachmentService` implementations. Document references require+view access for the acting user. Static-file IDs retain their existing bearer+capability semantics and resolve only through configured storage; arbitrary URLs+are not accepted. Cross-domain adapters depend only on inbound ports.++Document creation, destination authorization, filename and size validation,+byte uploads, and failure cleanup remain in the existing document upload+lifecycle. The storage adapter uses no document repositories or outbound+adapters. Hosts compose the provider, reference reader, and storage services in+`ai_tools`.diff --git a/crates/image_generation/src/domain.rs b/crates/image_generation/src/domain.rsnew file mode 100644index 0000000000..9f6cd54803--- /dev/null+++ b/crates/image_generation/src/domain.rs@@ -0,0 +1,4 @@+//! Image generation use cases and their contracts.+pub mod models;+pub mod ports;+pub mod service;diff --git a/crates/image_generation/src/domain/models.rs b/crates/image_generation/src/domain/models.rsnew file mode 100644index 0000000000..f2174b40d3--- /dev/null+++ b/crates/image_generation/src/domain/models.rs@@ -0,0 +1,219 @@+//! Image requests, generated bytes, and provider errors.++use entity_access::domain::models::{EditAccessLevel, EntityAccessReceipt};+use model::document::FileType;++/// The shape of image the model is asked for, as the model vocabulary calls+/// it. The closed set keeps the agent from inventing ratios the provider+/// rejects.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum ImageAspectRatio {+ /// 1:1+ Square,+ /// 4:3+ Landscape,+ /// 3:4+ Portrait,+ /// 16:9+ Widescreen,+ /// 9:16+ Tall,+}++impl ImageAspectRatio {+ /// The `width:height` form image providers accept.+ pub fn as_ratio(self) -> &'static str {+ match self {+ Self::Square => "1:1",+ Self::Landscape => "4:3",+ Self::Portrait => "3:4",+ Self::Widescreen => "16:9",+ Self::Tall => "9:16",+ }+ }+}++/// What to generate.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct ImageGenerationRequest {+ /// Natural-language description of the image.+ pub prompt: String,+ /// Requested shape; the provider default when `None`.+ pub aspect_ratio: Option<ImageAspectRatio>,+ /// Reference photos in the order the prompt refers to them.+ pub reference_images: Vec<ReferenceImage>,+}++/// A saved image the caller wants to use as a reference.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum ImageReference {+ /// A Macro document, read with the caller's document permissions.+ Document(uuid::Uuid),+ /// An uploaded static file, identified by its public capability ID.+ StaticFile(uuid::Uuid),+}++/// Image bytes resolved internally for the provider, never returned in tool output.+#[derive(Clone, PartialEq, Eq)]+pub struct ReferenceImage {+ /// Encoded image bytes.+ pub bytes: Vec<u8>,+ /// Media type of the encoded bytes.+ pub mime_type: String,+}++impl std::fmt::Debug for ReferenceImage {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("ReferenceImage")+ .field("size_bytes", &self.bytes.len())+ .field("mime_type", &self.mime_type)+ .finish()+ }+}++/// Why a reference photo could not be loaded.+#[derive(Debug, thiserror::Error)]+pub enum ReadImageError {+ /// This host has no reference reader configured.+ #[error("reference images are not available on this host")]+ Unavailable,+ /// The caller cannot read the requested source.+ #[error("reference image is missing or you do not have access to it")]+ Unauthorized,+ /// The reference does not resolve to one image.+ #[error("reference must identify an image file")]+ NotImage,+ /// Storage or image decoding failed.+ #[error("could not read the reference image: {0}")]+ Internal(rootcause::Report),+}++/// A generated image, as the provider returned it.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct GeneratedImage {+ /// Encoded image bytes in the format `mime_type` names.+ pub bytes: Vec<u8>,+ /// IANA media type of `bytes`, e.g. `image/png`.+ pub mime_type: String,+ /// Any commentary the model produced alongside the image.+ pub note: Option<String>,+}++impl GeneratedImage {+ /// The Macro file type for `mime_type`; `None` for a media type Macro+ /// does not know as an image.+ pub fn file_type(&self) -> Option<FileType> {+ match self.mime_type.as_str() {+ "image/png" => Some(FileType::Png),+ "image/jpeg" | "image/jpg" => Some(FileType::Jpg),+ "image/webp" => Some(FileType::Webp),+ "image/gif" => Some(FileType::Gif),+ _ => None,+ }+ }+}++/// Why an image could not be generated.+#[derive(Debug, thiserror::Error)]+pub enum ImageGenerationError {+ /// No provider is configured in this host.+ #[error("image generation is not configured")]+ Unavailable,+ /// The provider declined to render this prompt (safety filters, or the+ /// model answered in text instead of an image). The message is written+ /// for the agent that asked.+ #[error("{0}")]+ Refused(String),+ /// The provider call failed: transport, authentication, quota, or an+ /// unparseable response.+ #[error("image provider request failed")]+ Provider(#[source] anyhow::Error),+}++/// Longest prompt forwarded to the provider, in bytes.+pub const MAX_PROMPT_BYTES: usize = 8 * 1024;++/// Product limit matching the image model's recommended reference count.+pub const MAX_REFERENCE_IMAGES: usize = 3;++/// Total encoded image bytes before Base64 expansion, below the provider's 20 MB request limit.+pub const MAX_REFERENCE_BYTES: usize = 12 * 1024 * 1024;++/// An image to generate and store as a document.+pub struct NewGeneratedImage {+ /// Natural-language description of the image.+ pub prompt: String,+ /// Requested shape; the provider default when `None`.+ pub aspect_ratio: Option<ImageAspectRatio>,+ /// Document name; the extension is added from the generated format. A+ /// name that already carries an image extension keeps it. `None` names+ /// the document after the opening words of the prompt.+ pub file_name: Option<String>,+ /// Edit capability for the destination project, minted for the creating+ /// principal; absent for top-level files.+ pub project: Option<EntityAccessReceipt<EditAccessLevel>>,+ /// Reference images, resolved under the creating principal before generation.+ pub reference_images: Vec<ImageReference>,+}++/// A generated image stored as a document.+#[derive(Debug)]+pub struct GeneratedImageDocument {+ /// The created document.+ pub document_id: uuid::Uuid,+ /// Final filename, including its extension.+ pub file_name: String,+ /// IANA media type of the stored bytes.+ pub mime_type: String,+ /// Number of stored bytes.+ pub size_bytes: usize,+ /// Any commentary the model produced alongside the image.+ pub note: Option<String>,+}++/// Why a generated image document was not created.+#[derive(Debug, thiserror::Error)]+pub enum GenerateImageError {+ /// The prompt is invalid.+ #[error("{0}")]+ BadRequest(String),+ /// A requested reference image could not be loaded.+ #[error(transparent)]+ Reference(#[from] ReadImageError),+ /// The image could not be generated.+ #[error(transparent)]+ Generation(#[from] ImageGenerationError),+ /// The image was generated but not stored.+ #[error(transparent)]+ Document(#[from] SaveImageError),+}++/// Generated bytes and metadata to persist as a Macro document.+pub struct NewImageDocument {+ /// Filename including the image extension.+ pub file_name: String,+ /// Encoded image bytes.+ pub bytes: Vec<u8>,+ /// Verified edit capability for the destination project.+ pub project: Option<EntityAccessReceipt<EditAccessLevel>>,+}++/// Failure returned by the document-saving capability.+#[derive(Debug, thiserror::Error)]+pub enum SaveImageError {+ /// Invalid filename or file bytes.+ #[error("{0}")]+ BadRequest(String),+ /// The document name exceeds its allowed length.+ #[error("fileName is too long (maximum {max} characters)")]+ NameTooLong {+ /// Maximum permitted name length.+ max: usize,+ },+ /// The supplied destination capability does not authorize this principal.+ #[error("unauthorized destination project")]+ Unauthorized,+ /// Document persistence failed.+ #[error("could not save the generated image: {0}")]+ Internal(rootcause::Report),+}diff --git a/crates/image_generation/src/domain/ports.rs b/crates/image_generation/src/domain/ports.rsnew file mode 100644index 0000000000..fb0cceb628--- /dev/null+++ b/crates/image_generation/src/domain/ports.rs@@ -0,0 +1,78 @@+//! Inbound image-generation service and outbound provider/storage capabilities.+use super::models::{+ GenerateImageError, GeneratedImage, GeneratedImageDocument, ImageGenerationError,+ ImageGenerationRequest, ImageReference, NewGeneratedImage, NewImageDocument, ReadImageError,+ ReferenceImage, SaveImageError,+};+use model_owner::CreationPrincipal;+use std::future::Future;+use uuid::Uuid;++/// Renders an image from a text prompt and optional reference photos.+#[async_trait::async_trait]+pub trait ImageGenerator: Send + Sync + 'static {+ /// Generate one image for `request`.+ async fn generate_image(+ &self,+ request: &ImageGenerationRequest,+ ) -> Result<GeneratedImage, ImageGenerationError>;+}++/// The generator for hosts with no image provider configured: every call+/// fails with [`ImageGenerationError::Unavailable`].+#[derive(Debug, Clone, Copy, Default)]+pub struct UnconfiguredImageGenerator;++#[async_trait::async_trait]+impl ImageGenerator for UnconfiguredImageGenerator {+ async fn generate_image(+ &self,+ _request: &ImageGenerationRequest,+ ) -> Result<GeneratedImage, ImageGenerationError> {+ Err(ImageGenerationError::Unavailable)+ }+}++/// Loads reference photos through their owning domains' inbound services.+pub trait ImageReferenceReader: Send + Sync {+ /// Resolve one image, checking document access for the creating principal.+ fn read_image(+ &self,+ principal: &CreationPrincipal,+ reference: &ImageReference,+ ) -> impl Future<Output = Result<ReferenceImage, ReadImageError>> + Send;+}++/// Reference reader for hosts that only support text-to-image generation.+#[derive(Debug, Clone, Copy, Default)]+pub struct UnconfiguredImageReferenceReader;++impl ImageReferenceReader for UnconfiguredImageReferenceReader {+ async fn read_image(+ &self,+ _principal: &CreationPrincipal,+ _reference: &ImageReference,+ ) -> Result<ReferenceImage, ReadImageError> {+ Err(ReadImageError::Unavailable)+ }+}++/// Saves generated image bytes through the owning document domain.+pub trait ImageDocumentStore: Send + Sync {+ /// Store a document using the caller's identity and verified destination.+ fn save_image(+ &self,+ principal: &CreationPrincipal,+ image: NewImageDocument,+ ) -> impl Future<Output = Result<Uuid, SaveImageError>> + Send;+}++/// Inbound port for generating an image and saving it as a Macro document.+pub trait ImageGenerationService: Send + Sync {+ /// Generate and persist an image for the creating principal.+ fn create_generated_image(+ &self,+ principal: &CreationPrincipal,+ image: NewGeneratedImage,+ ) -> impl Future<Output = Result<GeneratedImageDocument, GenerateImageError>> + Send;+}diff --git a/crates/image_generation/src/domain/service.rs b/crates/image_generation/src/domain/service.rsnew file mode 100644index 0000000000..22550f2f43--- /dev/null+++ b/crates/image_generation/src/domain/service.rs@@ -0,0 +1,179 @@+//! Prompt validation, naming, and generation-to-document orchestration.+use super::models::{+ GenerateImageError, GeneratedImageDocument, ImageGenerationError, ImageGenerationRequest,+ MAX_PROMPT_BYTES, MAX_REFERENCE_BYTES, MAX_REFERENCE_IMAGES, NewGeneratedImage,+ NewImageDocument,+};+use super::ports::{+ ImageDocumentStore, ImageGenerationService, ImageGenerator, ImageReferenceReader,+ UnconfiguredImageReferenceReader,+};+use model::document::{FileType, FileTypeExt};+use model_owner::CreationPrincipal;+use std::sync::Arc;++#[cfg(test)]+mod test;++/// Image generation use case composed from a model provider and document store.+pub struct ImageGenerationServiceImpl<Store, References = UnconfiguredImageReferenceReader> {+ generator: Arc<dyn ImageGenerator>,+ store: Store,+ references: References,+}++impl<Store> ImageGenerationServiceImpl<Store> {+ /// Compose the provider and document-saving capability.+ pub fn new(generator: Arc<dyn ImageGenerator>, store: Store) -> Self {+ Self {+ generator,+ store,+ references: UnconfiguredImageReferenceReader,+ }+ }+}++impl<Store, References> ImageGenerationServiceImpl<Store, References> {+ /// Enable reference photos using the owning domains' read capabilities.+ pub fn with_reference_reader<R>(self, references: R) -> ImageGenerationServiceImpl<Store, R> {+ ImageGenerationServiceImpl {+ generator: self.generator,+ store: self.store,+ references,+ }+ }+}++impl<Store: ImageDocumentStore, References: ImageReferenceReader> ImageGenerationService+ for ImageGenerationServiceImpl<Store, References>+{+ /// Generate an image and store it through the document-saving port.+ #[tracing::instrument(skip_all, err)]+ async fn create_generated_image(+ &self,+ principal: &CreationPrincipal,+ image: NewGeneratedImage,+ ) -> Result<GeneratedImageDocument, GenerateImageError> {+ let NewGeneratedImage {+ prompt,+ aspect_ratio,+ file_name,+ project,+ reference_images,+ } = image;+ let prompt = prompt.trim();+ if prompt.is_empty() {+ return Err(GenerateImageError::BadRequest(+ "prompt must not be empty".to_string(),+ ));+ }+ if prompt.len() > MAX_PROMPT_BYTES {+ return Err(GenerateImageError::BadRequest(format!(+ "prompt exceeds the {MAX_PROMPT_BYTES} byte limit"+ )));+ }+ if reference_images.len() > MAX_REFERENCE_IMAGES {+ return Err(GenerateImageError::BadRequest(format!(+ "referenceImages accepts at most {MAX_REFERENCE_IMAGES} images"+ )));+ }+ let file_name = match file_name.as_deref().map(str::trim) {+ Some(name) if !name.is_empty() => name.to_string(),+ _ => file_name_from_prompt(prompt),+ };++ let mut references = Vec::with_capacity(reference_images.len());+ let mut reference_bytes = 0;+ for reference in &reference_images {+ let image = self.references.read_image(principal, reference).await?;+ if image.bytes.is_empty()+ || !matches!(+ image.mime_type.as_str(),+ "image/png" | "image/jpeg" | "image/webp"+ )+ {+ return Err(GenerateImageError::BadRequest(+ "referenceImages must contain readable PNG, JPEG, or WebP images".to_string(),+ ));+ }+ if image.bytes.len() > MAX_REFERENCE_BYTES - reference_bytes {+ return Err(GenerateImageError::BadRequest(+ "reference images exceed the 12 MiB total limit; use smaller images"+ .to_string(),+ ));+ }+ reference_bytes += image.bytes.len();+ references.push(image);+ }++ let generated = self+ .generator+ .generate_image(&ImageGenerationRequest {+ prompt: prompt.to_string(),+ aspect_ratio,+ reference_images: references,+ })+ .await?;+ let file_type = generated.file_type().ok_or_else(|| {+ ImageGenerationError::Provider(anyhow::anyhow!(+ "provider returned an unsupported image type {}",+ generated.mime_type+ ))+ })?;+ let file_name = with_extension(&file_name, file_type);+ let size_bytes = generated.bytes.len();++ let document_id = self+ .store+ .save_image(+ principal,+ NewImageDocument {+ file_name: file_name.clone(),+ bytes: generated.bytes,+ project,+ },+ )+ .await?;++ Ok(GeneratedImageDocument {+ document_id,+ file_name,+ mime_type: generated.mime_type,+ size_bytes,+ note: generated.note,+ })+ }+}++/// Words of `prompt` used as the document name when the caller gave none.+const FILE_NAME_WORDS: usize = 6;++/// A document name from the opening words of `prompt`: letters, digits, and+/// plain hyphens only, so the name needs no further filename validation.+fn file_name_from_prompt(prompt: &str) -> String {+ let name = prompt+ .split(|c: char| !(c.is_alphanumeric() || c == '-'))+ .filter(|word| !word.is_empty())+ .take(FILE_NAME_WORDS)+ .collect::<Vec<_>>()+ .join(" ");+ if name.is_empty() {+ "Generated image".to_string()+ } else {+ name+ }+}++/// `name` with `file_type`'s extension, unless it already ends in an image+/// extension the storage pipeline recognises.+fn with_extension(name: &str, file_type: FileType) -> String {+ let lower = name.to_ascii_lowercase();+ let already_image = FileType::split_suffix_match(&lower)+ .and_then(|(_, extension)| extension.parse::<FileType>().ok())+ .is_some_and(|existing| existing.is_image());+ if already_image {+ name.to_string()+ } else {+ format!("{name}.{}", file_type.as_str())+ }+}diff --git a/crates/image_generation/src/domain/service/test.rs b/crates/image_generation/src/domain/service/test.rsnew file mode 100644index 0000000000..433914f01a--- /dev/null+++ b/crates/image_generation/src/domain/service/test.rs@@ -0,0 +1,482 @@+use super::*;+use crate::domain::models::{+ GeneratedImage, ImageAspectRatio, ImageReference, ReadImageError, ReferenceImage,+ SaveImageError,+};+use crate::domain::ports::UnconfiguredImageGenerator;+use macro_user_id::user_id::MacroUserIdStr;+use std::collections::VecDeque;+use std::sync::{Arc, Mutex};+use uuid::Uuid;++const DOCUMENT_ID: Uuid = Uuid::from_u128(0x123);+const PNG: &[u8] = b"\x89PNG\r\n\x1a\nfake";+const DOCUMENT_REFERENCE: ImageReference = ImageReference::Document(Uuid::from_u128(0x456));+const STATIC_REFERENCE: ImageReference = ImageReference::StaticFile(Uuid::from_u128(0x789));++fn principal() -> CreationPrincipal {+ CreationPrincipal::BotForUser {+ bot: bot_id::MACRO_AI_BOT_ID,+ user: MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap(),+ }+}++#[derive(Default)]+struct RecordingStore {+ saves: Mutex<Vec<(CreationPrincipal, NewImageDocument)>>,+ deny: bool,+}++impl ImageDocumentStore for Arc<RecordingStore> {+ async fn save_image(+ &self,+ principal: &CreationPrincipal,+ image: NewImageDocument,+ ) -> Result<Uuid, SaveImageError> {+ if self.deny {+ return Err(SaveImageError::Unauthorized);+ }+ self.saves.lock().unwrap().push((principal.clone(), image));+ Ok(DOCUMENT_ID)+ }+}++/// Returns a fixed image and records what it was asked for.+struct FakeGenerator {+ requests: Mutex<Vec<ImageGenerationRequest>>,+ result: Result<GeneratedImage, &'static str>,+}++impl FakeGenerator {+ fn returning(image: GeneratedImage) -> Self {+ Self {+ requests: Mutex::default(),+ result: Ok(image),+ }+ }++ fn refusing(reason: &'static str) -> Self {+ Self {+ requests: Mutex::default(),+ result: Err(reason),+ }+ }+}++#[async_trait::async_trait]+impl ImageGenerator for FakeGenerator {+ async fn generate_image(+ &self,+ request: &ImageGenerationRequest,+ ) -> Result<GeneratedImage, ImageGenerationError> {+ self.requests.lock().unwrap().push(request.clone());+ self.result+ .clone()+ .map_err(|reason| ImageGenerationError::Refused(reason.to_string()))+ }+}++fn png() -> GeneratedImage {+ GeneratedImage {+ bytes: PNG.to_vec(),+ mime_type: "image/png".to_string(),+ note: Some("A calm scene.".to_string()),+ }+}++fn request(prompt: &str, file_name: &str) -> NewGeneratedImage {+ NewGeneratedImage {+ prompt: prompt.to_string(),+ aspect_ratio: Some(ImageAspectRatio::Widescreen),+ file_name: Some(file_name.to_string()),+ project: None,+ reference_images: Vec::new(),+ }+}++#[tokio::test]+async fn generates_then_saves_with_the_delegated_identity() {+ let store = Arc::new(RecordingStore::default());+ let generator = Arc::new(FakeGenerator::returning(png()));+ let service = ImageGenerationServiceImpl::new(generator.clone(), store.clone());+ let created = service+ .create_generated_image(+ &principal(),+ request(" a lighthouse at dusk ", "Lighthouse"),+ )+ .await+ .unwrap();+ assert_eq!(created.document_id, DOCUMENT_ID);+ assert_eq!(created.file_name, "Lighthouse.png");+ assert_eq!(created.mime_type, "image/png");+ assert_eq!(created.size_bytes, PNG.len());+ assert_eq!(created.note.as_deref(), Some("A calm scene."));+ assert_eq!(+ *generator.requests.lock().unwrap(),+ [ImageGenerationRequest {+ prompt: "a lighthouse at dusk".to_string(),+ aspect_ratio: Some(ImageAspectRatio::Widescreen),+ reference_images: Vec::new(),+ }]+ );+ let saves = store.saves.lock().unwrap();+ assert_eq!(saves.len(), 1);+ assert_eq!(saves[0].0, principal());+ assert_eq!(saves[0].1.bytes, PNG);+ assert_eq!(saves[0].1.file_name, "Lighthouse.png");+ assert!(saves[0].1.project.is_none());+}++#[tokio::test]+async fn keeps_an_existing_image_extension_and_adds_the_generated_one_otherwise() {+ for (file_name, mime_type, expected) in [+ ("hero.png", "image/png", "hero.png"),+ ("hero.PNG", "image/jpeg", "hero.PNG"),+ ("hero", "image/jpeg", "hero.jpg"),+ ("hero", "image/webp", "hero.webp"),+ ("v1.2 draft", "image/png", "v1.2 draft.png"),+ ("notes.md", "image/png", "notes.md.png"),+ ] {+ let generator = Arc::new(FakeGenerator::returning(GeneratedImage {+ mime_type: mime_type.to_string(),+ ..png()+ }));+ let service =+ ImageGenerationServiceImpl::new(generator, Arc::new(RecordingStore::default()));+ let created = service+ .create_generated_image(&principal(), request("prompt", file_name))+ .await+ .unwrap();+ assert_eq!(created.file_name, expected, "{file_name} / {mime_type}");+ }+}++#[tokio::test]+async fn names_the_document_after_the_prompt_when_no_name_is_given() {+ for (prompt, file_name, expected) in [+ (+ "A red lighthouse on a rocky shore at dusk, flat vector",+ None,+ "A red lighthouse on a rocky.png",+ ),+ (+ "Draw: \"Q3 revenue\" chart (bar)!",+ Some(" "),+ "Draw Q3 revenue chart bar.png",+ ),+ ("café-style sign", None, "café-style sign.png"),+ ("!!! ???", None, "Generated image.png"),+ ] {+ let service = ImageGenerationServiceImpl::new(+ Arc::new(FakeGenerator::returning(png())),+ Arc::new(RecordingStore::default()),+ );+ let created = service+ .create_generated_image(+ &principal(),+ NewGeneratedImage {+ prompt: prompt.to_string(),+ aspect_ratio: None,+ file_name: file_name.map(str::to_string),+ project: None,+ reference_images: Vec::new(),+ },+ )+ .await+ .unwrap();+ assert_eq!(created.file_name, expected, "{prompt:?}");+ }+}++#[tokio::test]+async fn rejects_bad_input_before_calling_the_provider() {+ let generator = Arc::new(FakeGenerator::returning(png()));+ let store = Arc::new(RecordingStore::default());+ let service = ImageGenerationServiceImpl::new(generator.clone(), store.clone());+ for image in [+ request(" ", "name"),+ request(&"p".repeat(MAX_PROMPT_BYTES + 1), "name"),+ ] {+ assert!(matches!(+ service.create_generated_image(&principal(), image).await,+ Err(GenerateImageError::BadRequest(_))+ ));+ }+ assert!(generator.requests.lock().unwrap().is_empty());+ assert!(store.saves.lock().unwrap().is_empty());+}++#[tokio::test]+async fn provider_failures_create_no_document() {+ let store = Arc::new(RecordingStore::default());+ let service = ImageGenerationServiceImpl::new(+ Arc::new(FakeGenerator::refusing("blocked by safety filters")),+ store.clone(),+ );+ assert!(+ matches!(service.create_generated_image(&principal(), request("prompt", "name")).await,+ Err(GenerateImageError::Generation(ImageGenerationError::Refused(ref reason))) if reason == "blocked by safety filters")+ );+ let service =+ ImageGenerationServiceImpl::new(Arc::new(UnconfiguredImageGenerator), store.clone());+ assert!(matches!(+ service+ .create_generated_image(&principal(), request("prompt", "name"))+ .await,+ Err(GenerateImageError::Generation(+ ImageGenerationError::Unavailable+ ))+ ));+ let service = ImageGenerationServiceImpl::new(+ Arc::new(FakeGenerator::returning(GeneratedImage {+ mime_type: "application/pdf".to_string(),+ ..png()+ })),+ store.clone(),+ );+ assert!(matches!(+ service+ .create_generated_image(&principal(), request("prompt", "name"))+ .await,+ Err(GenerateImageError::Generation(+ ImageGenerationError::Provider(_)+ ))+ ));+ assert!(store.saves.lock().unwrap().is_empty());+}++#[tokio::test]+async fn propagates_document_authorization_failures() {+ let service = ImageGenerationServiceImpl::new(+ Arc::new(FakeGenerator::returning(png())),+ Arc::new(RecordingStore {+ deny: true,+ ..Default::default()+ }),+ );+ assert!(matches!(+ service+ .create_generated_image(&principal(), request("prompt", "name"))+ .await,+ Err(GenerateImageError::Document(SaveImageError::Unauthorized))+ ));+}++struct RecordingReader {+ calls: Mutex<Vec<(CreationPrincipal, ImageReference)>>,+ results: Mutex<VecDeque<Result<ReferenceImage, ReadImageError>>>,+}++impl RecordingReader {+ fn returning(results: Vec<Result<ReferenceImage, ReadImageError>>) -> Self {+ Self {+ calls: Mutex::default(),+ results: Mutex::new(results.into()),+ }+ }+}++impl ImageReferenceReader for Arc<RecordingReader> {+ async fn read_image(+ &self,+ principal: &CreationPrincipal,+ reference: &ImageReference,+ ) -> Result<ReferenceImage, ReadImageError> {+ self.calls+ .lock()+ .unwrap()+ .push((principal.clone(), *reference));+ self.results+ .lock()+ .unwrap()+ .pop_front()+ .expect("unexpected reference read")+ }+}++fn reference_image(bytes: Vec<u8>, mime_type: &str) -> ReferenceImage {+ ReferenceImage {+ bytes,+ mime_type: mime_type.to_string(),+ }+}++#[tokio::test]+async fn resolves_references_in_order_under_the_creating_principal_before_generation() {+ let images = vec![+ reference_image(PNG.to_vec(), "image/png"),+ reference_image(b"reference jpeg".to_vec(), "image/jpeg"),+ ];+ let reader = Arc::new(RecordingReader::returning(+ images.iter().cloned().map(Ok).collect(),+ ));+ let generator = Arc::new(FakeGenerator::returning(png()));+ let store = Arc::new(RecordingStore::default());+ let service = ImageGenerationServiceImpl::new(generator.clone(), store.clone())+ .with_reference_reader(reader.clone());+ let mut image = request(" edit image 1 using image 2 ", "Edited image");+ image.reference_images = vec![DOCUMENT_REFERENCE, STATIC_REFERENCE];++ let created = service+ .create_generated_image(&principal(), image)+ .await+ .unwrap();++ assert_eq!(created.document_id, DOCUMENT_ID);+ assert_eq!(+ *reader.calls.lock().unwrap(),+ [+ (principal(), DOCUMENT_REFERENCE),+ (principal(), STATIC_REFERENCE)+ ]+ );+ assert_eq!(+ *generator.requests.lock().unwrap(),+ [ImageGenerationRequest {+ prompt: "edit image 1 using image 2".to_string(),+ aspect_ratio: Some(ImageAspectRatio::Widescreen),+ reference_images: images,+ }]+ );+ let saves = store.saves.lock().unwrap();+ assert_eq!(saves.len(), 1);+ assert_eq!(saves[0].0, principal());+ assert_eq!(saves[0].1.bytes, PNG);+ assert_eq!(saves[0].1.file_name, "Edited image.png");+}++#[tokio::test]+async fn excessive_reference_count_is_rejected_before_any_read_or_generation() {+ let reader = Arc::new(RecordingReader::returning(Vec::new()));+ let generator = Arc::new(FakeGenerator::returning(png()));+ let store = Arc::new(RecordingStore::default());+ let service = ImageGenerationServiceImpl::new(generator.clone(), store.clone())+ .with_reference_reader(reader.clone());+ let mut image = request("edit these images", "Edited");+ image.reference_images = vec![DOCUMENT_REFERENCE; MAX_REFERENCE_IMAGES + 1];++ assert!(matches!(+ service.create_generated_image(&principal(), image).await,+ Err(GenerateImageError::BadRequest(_))+ ));+ assert!(reader.calls.lock().unwrap().is_empty());+ assert!(generator.requests.lock().unwrap().is_empty());+ assert!(store.saves.lock().unwrap().is_empty());+}++#[tokio::test]+async fn failed_reference_reads_never_generate_from_a_partial_set() {+ for error in [+ ReadImageError::Unauthorized,+ ReadImageError::NotImage,+ ReadImageError::Unavailable,+ ReadImageError::Internal(rootcause::report!("storage unavailable")),+ ] {+ let expected_error = error.to_string();+ let reader = Arc::new(RecordingReader::returning(vec![+ Ok(reference_image(PNG.to_vec(), "image/png")),+ Err(error),+ ]));+ let generator = Arc::new(FakeGenerator::returning(png()));+ let store = Arc::new(RecordingStore::default());+ let service = ImageGenerationServiceImpl::new(generator.clone(), store.clone())+ .with_reference_reader(reader.clone());+ let mut image = request("combine all three images", "Combined");+ image.reference_images = vec![DOCUMENT_REFERENCE, STATIC_REFERENCE, DOCUMENT_REFERENCE];++ assert!(matches!(+ service.create_generated_image(&principal(), image).await,+ Err(GenerateImageError::Reference(error)) if error.to_string() == expected_error+ ));+ assert_eq!(reader.calls.lock().unwrap().len(), 2);+ assert!(generator.requests.lock().unwrap().is_empty());+ assert!(store.saves.lock().unwrap().is_empty());+ }+}++#[tokio::test]+async fn invalid_reference_content_is_rejected_before_generation_or_storage() {+ for images in [+ vec![reference_image(PNG.to_vec(), "image/gif")],+ vec![reference_image(Vec::new(), "image/png")],+ vec![reference_image(+ vec![0; MAX_REFERENCE_BYTES + 1],+ "image/png",+ )],+ vec![+ reference_image(vec![0; MAX_REFERENCE_BYTES / 2], "image/png"),+ reference_image(vec![0; MAX_REFERENCE_BYTES / 2 + 1], "image/webp"),+ ],+ ] {+ let image_count = images.len();+ let reader = Arc::new(RecordingReader::returning(+ images.into_iter().map(Ok).collect(),+ ));+ let generator = Arc::new(FakeGenerator::returning(png()));+ let store = Arc::new(RecordingStore::default());+ let service = ImageGenerationServiceImpl::new(generator.clone(), store.clone())+ .with_reference_reader(reader.clone());+ let mut image = request("edit these images", "Edited");+ image.reference_images = vec![DOCUMENT_REFERENCE; image_count];++ assert!(matches!(+ service.create_generated_image(&principal(), image).await,+ Err(GenerateImageError::BadRequest(_))+ ));+ assert_eq!(reader.calls.lock().unwrap().len(), image_count);+ assert!(generator.requests.lock().unwrap().is_empty());+ assert!(store.saves.lock().unwrap().is_empty());+ }+}++#[tokio::test]+async fn accepts_three_supported_images_at_the_total_byte_limit() {+ let images = vec![+ reference_image(vec![0; MAX_REFERENCE_BYTES - 2], "image/png"),+ reference_image(vec![1], "image/jpeg"),+ reference_image(vec![2], "image/webp"),+ ];+ let reader = Arc::new(RecordingReader::returning(+ images.into_iter().map(Ok).collect(),+ ));+ let generator = Arc::new(FakeGenerator::returning(png()));+ let store = Arc::new(RecordingStore::default());+ let service = ImageGenerationServiceImpl::new(generator.clone(), store.clone())+ .with_reference_reader(reader);+ let mut image = request("combine all three images", "Combined");+ image.reference_images = vec![DOCUMENT_REFERENCE, STATIC_REFERENCE, DOCUMENT_REFERENCE];++ service+ .create_generated_image(&principal(), image)+ .await+ .unwrap();++ let requests = generator.requests.lock().unwrap();+ assert_eq!(requests.len(), 1);+ assert_eq!(requests[0].reference_images.len(), MAX_REFERENCE_IMAGES);+ assert_eq!(+ requests[0]+ .reference_images+ .iter()+ .map(|image| image.bytes.len())+ .sum::<usize>(),+ MAX_REFERENCE_BYTES+ );+ assert_eq!(store.saves.lock().unwrap().len(), 1);+}++#[tokio::test]+async fn missing_reference_reader_does_not_silently_fall_back_to_text_only() {+ let generator = Arc::new(FakeGenerator::returning(png()));+ let store = Arc::new(RecordingStore::default());+ let service = ImageGenerationServiceImpl::new(generator.clone(), store.clone());+ let mut image = request("edit this photo", "Edited");+ image.reference_images = vec![DOCUMENT_REFERENCE];++ assert!(matches!(+ service.create_generated_image(&principal(), image).await,+ Err(GenerateImageError::Reference(ReadImageError::Unavailable))+ ));+ assert!(generator.requests.lock().unwrap().is_empty());+ assert!(store.saves.lock().unwrap().is_empty());+}diff --git a/crates/image_generation/src/inbound.rs b/crates/image_generation/src/inbound.rsnew file mode 100644index 0000000000..7eb9d9629c--- /dev/null+++ b/crates/image_generation/src/inbound.rs@@ -0,0 +1,2 @@+//! Inbound adapters for image generation.+pub mod toolset;diff --git a/crates/image_generation/src/inbound/toolset.rs b/crates/image_generation/src/inbound/toolset.rsnew file mode 100644index 0000000000..5fa5233a8b--- /dev/null+++ b/crates/image_generation/src/inbound/toolset.rs@@ -0,0 +1,54 @@+//! AI toolset for generating and saving images.+mod generate_image;++use crate::domain::ports::ImageGenerationService;+use ai_toolset::AsyncToolCollection;+use bot_id::BotId;+use entity_access::domain::ports::EntityAccessService;+use generate_image::GenerateImage;+use std::sync::Arc;++/// Services and delegated bot identity used by the image-generation tool.+pub struct ImageGenerationToolContext<Svc, Access> {+ /// Image-generation use case.+ pub service: Arc<Svc>,+ /// Inbound access service for minting destination capabilities.+ pub entity_access_service: Arc<Access>,+ /// Bot creating images on behalf of the requesting user.+ pub actor: BotId,+}++impl<Svc, Access> Clone for ImageGenerationToolContext<Svc, Access> {+ fn clone(&self) -> Self {+ Self {+ service: self.service.clone(),+ entity_access_service: self.entity_access_service.clone(),+ actor: self.actor,+ }+ }+}++impl<Svc, Access> ImageGenerationToolContext<Svc, Access> {+ /// Compose the use case, access boundary, and delegated bot identity.+ pub fn new(service: Svc, entity_access_service: Arc<Access>, actor: BotId) -> Self {+ Self {+ service: Arc::new(service),+ entity_access_service,+ actor,+ }+ }++ /// Set the bot acting on behalf of the requesting user.+ pub fn with_actor(mut self, actor: BotId) -> Self {+ self.actor = actor;+ self+ }+}++/// Image-generation tools shared by chat, agent, and MCP hosts.+pub fn image_generation_toolset<+ Svc: ImageGenerationService + 'static,+ Access: EntityAccessService,+>() -> AsyncToolCollection<ImageGenerationToolContext<Svc, Access>> {+ AsyncToolCollection::new().add_tool::<GenerateImage, ImageGenerationToolContext<Svc, Access>>()+}diff --git a/crates/image_generation/src/inbound/toolset/generate_image.rs b/crates/image_generation/src/inbound/toolset/generate_image.rsnew file mode 100644index 0000000000..fbe8680e8d--- /dev/null+++ b/crates/image_generation/src/inbound/toolset/generate_image.rs@@ -0,0 +1,234 @@+//! GenerateImage renders a prompt with the configured image model and stores+//! the result as an image document.++use ai_toolset::{+ AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolCallError,+ ToolResult,+};+use async_trait::async_trait;+use entity_access::domain::{+ models::{BotAccessScope, EditAccessLevel, EntityType},+ ports::EntityAccessService,+};+use schemars::JsonSchema;+use serde::{Deserialize, Serialize};++use super::ImageGenerationToolContext;+use crate::domain::{+ models::{+ GenerateImageError, ImageAspectRatio, ImageGenerationError, ImageReference,+ NewGeneratedImage, ReadImageError, SaveImageError,+ },+ ports::ImageGenerationService,+};+use model_owner::CreationPrincipal;++#[cfg(test)]+mod test;++// Shape of the generated image: square 1:1, landscape 4:3, portrait 3:4,+// widescreen 16:9, tall 9:16. Doc comments on the variants would turn the+// schema enum into a named type, so the ratios stay a plain comment.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema)]+#[serde(rename_all = "snake_case")]+enum AspectRatio {+ Square,+ Landscape,+ Portrait,+ Widescreen,+ Tall,+}++impl From<AspectRatio> for ImageAspectRatio {+ fn from(value: AspectRatio) -> Self {+ match value {+ AspectRatio::Square => Self::Square,+ AspectRatio::Landscape => Self::Landscape,+ AspectRatio::Portrait => Self::Portrait,+ AspectRatio::Widescreen => Self::Widescreen,+ AspectRatio::Tall => Self::Tall,+ }+ }+}++/// A reference photo already stored in Macro.+#[derive(Debug, Clone, Copy, Deserialize, JsonSchema)]+#[serde(tag = "type", rename_all = "camelCase")]+enum ImageReferenceInput {+ /// An image document the user can view, including a previously generated image.+ Document {+ /// The Macro document ID.+ id: uuid::Uuid,+ },+ /// A photo uploaded with the conversation's attachment control.+ StaticFile {+ /// Uploaded file ID from the /file/<id> segment of its attachment URL.+ id: uuid::Uuid,+ },+}++impl From<ImageReferenceInput> for ImageReference {+ fn from(value: ImageReferenceInput) -> Self {+ match value {+ ImageReferenceInput::Document { id } => Self::Document(id),+ ImageReferenceInput::StaticFile { id } => Self::StaticFile(id),+ }+ }+}++#[derive(Deserialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+#[schemars(+ title = "GenerateImage",+ description = "Generate or edit an image with Google's Nano Banana image model and save the result as a new image document in Macro. Use for pictures, illustrations, diagram concepts, logo ideas, mockups, or edits based on reference photos. When the user supplies photos or asks to modify an existing image, pass them in referenceImages; describing a photo in the prompt alone does not send it to the image model. Describe the subject, style, composition, lighting, and any text to render; only the prompt is required. Refer to reference images by their order (image 1, image 2, image 3) when explaining how to use them. Returns the new document ID to cite inline. Generation takes several seconds."+)]+/// Generate an image and save it as a Macro document.+pub struct GenerateImage {+ #[schemars(+ description = "Detailed description of the image to generate: subject, style (photo, illustration, flat vector...), composition, colours, mood, and any text that must appear."+ )]+ prompt: String,+ #[serde(default)]+ #[schemars(+ description = "Optional short descriptive name for the saved image, for example `sunset-lighthouse`. The file extension is added from the generated format. No directory path. Omit to name the image after the prompt."+ )]+ file_name: Option<String>,+ #[serde(default)]+ #[schemars(+ description = "Shape of the image. Omit for the model default (square). `widescreen` (16:9) suits banners and slides, `tall` (9:16) suits phone screens and stories."+ )]+ aspect_ratio: Option<AspectRatio>,+ #[serde(default)]+ #[schemars(+ description = "Optional destination project (folder) ID. Requires edit access. Omit to save to the user's top-level files."+ )]+ project_id: Option<uuid::Uuid>,+ #[serde(default)]+ #[schemars(+ description = "Up to three reference photos, in the order used by the prompt. Use type document with a Macro image document ID, or type staticFile with the UUID from /file/<id> in an uploaded attachment's source URL. Use the actual IDs supplied in the conversation or by tools; do not invent IDs. Omit for text-only generation.",+ length(max = 3)+ )]+ reference_images: Option<Vec<ImageReferenceInput>>,+}++/// Where the generated image landed. Does not echo the image bytes.+#[derive(Serialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct GenerateImageResponse {+ /// ID of the new image document.+ pub document_id: String,+ /// Saved filename, including its extension.+ pub file_name: String,+ /// IANA media type of the image, e.g. `image/png`.+ pub mime_type: String,+ /// Size of the image in bytes.+ pub size_bytes: usize,+ /// Commentary the model produced alongside the image, when any.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub note: Option<String>,+}++impl ToolAnnotated for GenerateImage {+ const ANNOTATIONS: ToolAnnotations =+ ToolAnnotations::additive("Generate image").with_open_world();+}++fn generate_error(error: GenerateImageError) -> ToolCallError {+ let description = match &error {+ GenerateImageError::BadRequest(message) => message.clone(),+ GenerateImageError::Reference(ReadImageError::Internal(_)) => {+ "could not read a reference image; check its ID and access, then retry".to_string()+ }+ GenerateImageError::Reference(error) => error.to_string(),+ GenerateImageError::Generation(ImageGenerationError::Unavailable) => {+ "image generation is not available in this workspace".to_string()+ }+ GenerateImageError::Generation(ImageGenerationError::Refused(reason)) => {+ format!("the image model did not return an image: {reason}")+ }+ GenerateImageError::Generation(ImageGenerationError::Provider(_)) => {+ "the image model request failed; try again shortly".to_string()+ }+ GenerateImageError::Document(SaveImageError::BadRequest(message)) => message.clone(),+ GenerateImageError::Document(SaveImageError::NameTooLong { max }) => {+ format!("fileName is too long (maximum {max} characters)")+ }+ GenerateImageError::Document(SaveImageError::Unauthorized) => {+ "you need edit access to the destination project".to_string()+ }+ GenerateImageError::Document(_) => {+ "the image was generated but could not be saved to Macro".to_string()+ }+ };+ ToolCallError {+ description,+ internal_error: error.into(),+ }+}++#[async_trait]+impl<Svc, ESvc> AsyncTool<ImageGenerationToolContext<Svc, ESvc>> for GenerateImage+where+ Svc: ImageGenerationService + 'static,+ ESvc: EntityAccessService,+{+ type Output = GenerateImageResponse;++ async fn call(+ &self,+ service_context: ServiceContext<ImageGenerationToolContext<Svc, ESvc>>,+ request_context: RequestContext,+ ) -> ToolResult<Self::Output> {+ let project = match self.project_id {+ Some(project_id) => Some(+ service_context+ .entity_access_service+ .generate_bot_entity_access_receipt::<EditAccessLevel>(+ service_context.actor,+ BotAccessScope::user(request_context.user_id.clone()),+ &project_id.to_string(),+ EntityType::Project,+ )+ .await+ .map_err(|error| ToolCallError {+ description:+ "you need edit access to the destination project, or it does not exist"+ .to_string(),+ internal_error: error.into(),+ })?,+ ),+ None => None,+ };+ let created = service_context+ .service+ .create_generated_image(+ &CreationPrincipal::BotForUser {+ bot: service_context.actor,+ user: request_context.user_id,+ },+ NewGeneratedImage {+ prompt: self.prompt.clone(),+ aspect_ratio: self.aspect_ratio.map(Into::into),+ file_name: self.file_name.clone(),+ project,+ reference_images: self+ .reference_images+ .iter()+ .flatten()+ .copied()+ .map(Into::into)+ .collect(),+ },+ )+ .await+ .map_err(generate_error)?;++ Ok(GenerateImageResponse {+ document_id: created.document_id.to_string(),+ file_name: created.file_name,+ mime_type: created.mime_type,+ size_bytes: created.size_bytes,+ note: created.note,+ })+ }+}diff --git a/crates/image_generation/src/inbound/toolset/generate_image/test.rs b/crates/image_generation/src/inbound/toolset/generate_image/test.rsnew file mode 100644index 0000000000..8809cc88f1--- /dev/null+++ b/crates/image_generation/src/inbound/toolset/generate_image/test.rs@@ -0,0 +1,168 @@+use super::*;+use ai_toolset::schema::generate_validated_input_schema;++#[test]+fn schema_is_valid_and_optional_fields_default() {+ let schema = generate_validated_input_schema::<GenerateImage>().unwrap();+ assert_eq!(schema.name, "GenerateImage");+ let tool: GenerateImage = serde_json::from_value(serde_json::json!({+ "prompt": "a lighthouse at dusk"+ }))+ .unwrap();+ assert!(tool.file_name.is_none());+ assert!(tool.aspect_ratio.is_none());+ assert!(tool.project_id.is_none());+ assert!(tool.reference_images.is_none());++ let tool: GenerateImage = serde_json::from_value(serde_json::json!({+ "prompt": "a lighthouse at dusk", "fileName": "lighthouse", "aspectRatio": "widescreen"+ }))+ .unwrap();+ assert_eq!(tool.file_name.as_deref(), Some("lighthouse"));+ assert_eq!(+ tool.aspect_ratio.map(ImageAspectRatio::from),+ Some(ImageAspectRatio::Widescreen)+ );+}++#[test]+fn omitted_and_null_references_both_mean_text_only_generation() {+ for input in [+ serde_json::json!({ "prompt": "a lighthouse at dusk" }),+ serde_json::json!({ "prompt": "a lighthouse at dusk", "referenceImages": null }),+ ] {+ let tool: GenerateImage = serde_json::from_value(input).unwrap();+ assert!(tool.reference_images.is_none());+ }+}++#[test]+fn an_empty_reference_array_is_valid_and_contains_no_source_images() {+ let tool: GenerateImage = serde_json::from_value(serde_json::json!({+ "prompt": "a lighthouse at dusk", "referenceImages": []+ }))+ .unwrap();+ assert!(tool.reference_images.as_ref().is_some_and(Vec::is_empty));+ assert!(tool.reference_images.into_iter().flatten().next().is_none());+}++#[test]+fn reference_inputs_preserve_typed_identifiers_and_order() {+ let document_id = uuid::Uuid::from_u128(0x123);+ let static_file_id = uuid::Uuid::from_u128(0x456);+ let tool: GenerateImage = serde_json::from_value(serde_json::json!({+ "prompt": "edit the first photo using the second",+ "referenceImages": [+ { "type": "document", "id": document_id },+ { "type": "staticFile", "id": static_file_id },+ ]+ }))+ .unwrap();++ assert_eq!(+ tool.reference_images+ .into_iter()+ .flatten()+ .map(ImageReference::from)+ .collect::<Vec<_>>(),+ [+ ImageReference::Document(document_id),+ ImageReference::StaticFile(static_file_id)+ ]+ );+}++#[test]+fn reference_inputs_reject_unknown_sources_and_invalid_identifiers() {+ for reference in [+ serde_json::json!({ "type": "url", "id": uuid::Uuid::from_u128(0x123) }),+ serde_json::json!({ "type": "document", "id": "not-a-uuid" }),+ serde_json::json!({ "type": "staticFile", "id": "https://host/file/image" }),+ serde_json::json!({ "type": "document" }),+ ] {+ assert!(+ serde_json::from_value::<GenerateImage>(serde_json::json!({+ "prompt": "edit this photo", "referenceImages": [reference]+ }))+ .is_err()+ );+ }+}++#[test]+fn every_aspect_ratio_maps_to_a_provider_ratio() {+ for (name, ratio) in [+ ("square", "1:1"),+ ("landscape", "4:3"),+ ("portrait", "3:4"),+ ("widescreen", "16:9"),+ ("tall", "9:16"),+ ] {+ let aspect: AspectRatio = serde_json::from_value(serde_json::json!(name)).unwrap();+ assert_eq!(ImageAspectRatio::from(aspect).as_ratio(), ratio);+ }+}++#[test]+fn errors_describe_what_the_agent_can_do_about_them() {+ let description = |error: GenerateImageError| generate_error(error).description;+ assert_eq!(+ description(ImageGenerationError::Unavailable.into()),+ "image generation is not available in this workspace"+ );+ assert_eq!(+ description(ImageGenerationError::Refused("only text".to_string()).into()),+ "the image model did not return an image: only text"+ );+ assert_eq!(+ description(ImageGenerationError::Provider(anyhow::anyhow!("HTTP 429")).into()),+ "the image model request failed; try again shortly"+ );+ assert_eq!(+ description(GenerateImageError::BadRequest(+ "prompt must not be empty".to_string()+ )),+ "prompt must not be empty"+ );+ assert_eq!(+ description(SaveImageError::Unauthorized.into()),+ "you need edit access to the destination project"+ );+ assert_eq!(+ description(SaveImageError::Internal(rootcause::report!("save failed")).into()),+ "the image was generated but could not be saved to Macro"+ );+ for error in [+ ReadImageError::Unavailable,+ ReadImageError::Unauthorized,+ ReadImageError::NotImage,+ ] {+ let expected = error.to_string();+ assert_eq!(description(error.into()), expected);+ }+ assert_eq!(+ description(ReadImageError::Internal(rootcause::report!("private storage detail")).into()),+ "could not read a reference image; check its ID and access, then retry"+ );+}++#[test]+fn response_omits_an_absent_note() {+ let json = serde_json::to_value(GenerateImageResponse {+ document_id: "doc".to_string(),+ file_name: "lighthouse.png".to_string(),+ mime_type: "image/png".to_string(),+ size_bytes: 3,+ note: None,+ })+ .unwrap();+ assert_eq!(+ json,+ serde_json::json!({+ "documentId": "doc",+ "fileName": "lighthouse.png",+ "mimeType": "image/png",+ "sizeBytes": 3+ })+ );+}diff --git a/crates/image_generation/src/lib.rs b/crates/image_generation/src/lib.rsnew file mode 100644index 0000000000..ead4f4ae9d--- /dev/null+++ b/crates/image_generation/src/lib.rs@@ -0,0 +1,6 @@+//! Image generation and storage as Macro documents, exposed through an AI toolset.+#![deny(missing_docs)]++pub mod domain;+pub mod inbound;+pub mod outbound;diff --git a/crates/image_generation/src/outbound.rs b/crates/image_generation/src/outbound.rsnew file mode 100644index 0000000000..53d4512a64--- /dev/null+++ b/crates/image_generation/src/outbound.rs@@ -0,0 +1,4 @@+//! Provider and document storage adapters.+pub mod documents;+pub mod gemini;+pub mod references;diff --git a/crates/image_generation/src/outbound/documents.rs b/crates/image_generation/src/outbound/documents.rsnew file mode 100644index 0000000000..5267eba549--- /dev/null+++ b/crates/image_generation/src/outbound/documents.rs@@ -0,0 +1,56 @@+//! Saves images through the documents domain's inbound upload port.+use crate::domain::{+ models::{NewImageDocument, SaveImageError},+ ports::ImageDocumentStore,+};+use documents::domain::{+ create::upload::NewFileUpload, models::DocumentError, ports::create::DocumentUploadService,+};+use model_owner::CreationPrincipal;+use uuid::Uuid;++/// Adapter to the existing document upload lifecycle.+pub struct DocumentsImageStore<Svc> {+ service: Svc,+}++impl<Svc> DocumentsImageStore<Svc> {+ /// Use an already-composed document upload service.+ pub fn new(service: Svc) -> Self {+ Self { service }+ }+}++impl<Svc: DocumentUploadService> ImageDocumentStore for DocumentsImageStore<Svc> {+ #[tracing::instrument(skip_all, err)]+ async fn save_image(+ &self,+ principal: &CreationPrincipal,+ image: NewImageDocument,+ ) -> Result<Uuid, SaveImageError> {+ let created = self+ .service+ .upload_file(+ principal,+ NewFileUpload {+ file_name: image.file_name,+ bytes: image.bytes,+ project: image.project,+ },+ )+ .await+ .map_err(|error| match error {+ DocumentError::BadRequest(message) => SaveImageError::BadRequest(message),+ DocumentError::NameTooLong { max } => SaveImageError::NameTooLong { max },+ DocumentError::Unauthorized => SaveImageError::Unauthorized,+ error => SaveImageError::Internal(rootcause::report!(error).into()),+ })?;+ created+ .document_id()+ .parse::<Uuid>()+ .map_err(|error| SaveImageError::Internal(rootcause::report!(error).into()))+ }+}++#[cfg(test)]+mod test;diff --git a/crates/image_generation/src/outbound/documents/test.rs b/crates/image_generation/src/outbound/documents/test.rsnew file mode 100644index 0000000000..5b4a8768af--- /dev/null+++ b/crates/image_generation/src/outbound/documents/test.rs@@ -0,0 +1,186 @@+use super::*;+use documents::domain::{+ content::DocumentContent,+ create::DocumentCreator,+ models::{CreateTaskRequest, NewDocument},+ ports::create::{DocumentBytesUpload, DocumentBytesUploadPort, DocumentCreationService},+ response::{CreateDocumentResponseData, DocumentResponse, DocumentResponseMetadataWithContent},+};+use macro_user_id::user_id::MacroUserIdStr;+use model::document::{FileType, response::DocumentResponseMetadata};+use std::sync::{Arc, Mutex};+const DOCUMENT_ID: &str = "00000000-0000-0000-0000-000000000123";+const PNG: &[u8] = b"\x89PNG\r\n\x1a\nfake";++fn principal() -> CreationPrincipal {+ CreationPrincipal::BotForUser {+ bot: bot_id::MACRO_AI_BOT_ID,+ user: MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap(),+ }+}+#[derive(Default)]+struct RecordingService {+ creates: Mutex<Vec<NewDocument>>,+}++impl DocumentCreationService for RecordingService {+ async fn create_document(+ &self,+ principal: &CreationPrincipal,+ document: NewDocument,+ _job_id: Option<String>,+ ) -> Result<CreateDocumentResponseData, DocumentError> {+ let file_type = document.file_type.map(|kind| kind.to_string());+ let document_name = document.document_name.clone();+ self.creates.lock().unwrap().push(document);+ Ok(CreateDocumentResponseData {+ document_response: DocumentResponse {+ document_metadata: DocumentResponseMetadataWithContent::new(+ DocumentResponseMetadata {+ document_id: DOCUMENT_ID.to_string(),+ document_version_id: 1,+ owner: principal.owner(),+ document_name,+ file_type: file_type.clone(),+ sha: None,+ branched_from_id: None,+ branched_from_version_id: None,+ document_family_id: None,+ document_bom: None,+ modification_data: None,+ created_at: None,+ updated_at: None,+ sub_type: None,+ },+ DocumentContent::pending(),+ ),+ presigned_url: Some("https://storage.example/upload".to_string()),+ },+ content_type: "image/png".to_string(),+ file_type,+ })+ }++ async fn handle_task_properties(+ &self,+ _: &CreationPrincipal,+ _: &str,+ _: &CreateTaskRequest,+ ) -> Result<(), DocumentError> {+ panic!("generated images are not tasks")+ }++ async fn mark_document_uploaded(&self, _: &str) -> Result<(), DocumentError> {+ panic!("the storage event pipeline finalizes uploads")+ }++ async fn set_document_content(&self, _: &str, _: DocumentContent) -> Result<(), DocumentError> {+ panic!("the storage event pipeline finalizes uploads")+ }++ async fn cleanup_created_document(&self, _: &str) {}+}++#[derive(Clone, Default)]+struct RecordingUploader {+ uploads: Arc<Mutex<Vec<DocumentBytesUpload>>>,+}++impl DocumentBytesUploadPort for RecordingUploader {+ async fn upload_document_bytes(+ &self,+ upload: DocumentBytesUpload,+ ) -> Result<(), DocumentError> {+ self.uploads.lock().unwrap().push(upload);+ Ok(())+ }+}++#[tokio::test]+async fn saves_through_the_existing_document_upload_lifecycle() {+ let service = Arc::new(RecordingService::default());+ let uploader = RecordingUploader::default();+ let store = DocumentsImageStore::new(DocumentCreator::new(+ service.clone(),+ (),+ uploader.clone(),+ (),+ ));+ let id = store+ .save_image(+ &principal(),+ NewImageDocument {+ file_name: "Lighthouse.png".to_string(),+ bytes: PNG.to_vec(),+ project: None,+ },+ )+ .await+ .unwrap();+ assert_eq!(id.to_string(), DOCUMENT_ID);+ let creates = service.creates.lock().unwrap();+ assert_eq!(creates.len(), 1);+ assert_eq!(creates[0].document_name, "Lighthouse");+ assert_eq!(creates[0].file_type, Some(FileType::Png));+ let uploads = uploader.uploads.lock().unwrap();+ assert_eq!(uploads.len(), 1);+ assert_eq!(uploads[0].bytes, PNG);+ assert_eq!(uploads[0].content_type, "image/png");+}++#[tokio::test]+async fn preserves_destination_capabilities_and_document_authorization() {+ use entity_access::domain::models::{+ BotReceiptScope, EditAccessLevel, Entity, EntityAccessReceipt, EntityPermission, EntityType,+ };+ use models_permissions::share_permission::access_level::AccessLevel;++ let project_id = Uuid::from_u128(0x456);+ for (user, entity_type, allowed) in [+ ("macro|[email protected]", EntityType::Project, true),+ ("macro|[email protected]", EntityType::Project, false),+ ("macro|[email protected]", EntityType::Document, false),+ ] {+ let receipt = EntityAccessReceipt::<EditAccessLevel>::try_new_bot(+ bot_id::MACRO_AI_BOT_ID.into(),+ BotReceiptScope::User {+ acting_user: MacroUserIdStr::try_from(user.to_string()).unwrap(),+ },+ Entity {+ entity_id: project_id.to_string(),+ entity_type,+ },+ EntityPermission::AccessLevel {+ access_level: AccessLevel::Edit,+ },+ )+ .unwrap();+ let service = Arc::new(RecordingService::default());+ let uploader = RecordingUploader::default();+ let store = DocumentsImageStore::new(DocumentCreator::new(+ service.clone(),+ (),+ uploader.clone(),+ (),+ ));+ let result = store+ .save_image(+ &principal(),+ NewImageDocument {+ file_name: "Lighthouse.png".to_string(),+ bytes: PNG.to_vec(),+ project: Some(receipt),+ },+ )+ .await;+ let creates = service.creates.lock().unwrap();+ if allowed {+ assert_eq!(result.unwrap().to_string(), DOCUMENT_ID);+ assert_eq!(creates[0].project_id, Some(project_id));+ } else {+ assert!(matches!(result, Err(SaveImageError::Unauthorized)));+ assert!(creates.is_empty());+ assert!(uploader.uploads.lock().unwrap().is_empty());+ }+ }+}diff --git a/crates/image_generation/src/outbound/gemini.rs b/crates/image_generation/src/outbound/gemini.rsnew file mode 100644index 0000000000..d21ef20834--- /dev/null+++ b/crates/image_generation/src/outbound/gemini.rs@@ -0,0 +1,233 @@+//! Outbound adapter generating images with Google's Gemini image models+//! ("Nano Banana") over the GenerateContent REST API.++use std::time::Duration;++use anyhow::Context as _;+use base64::Engine as _;+use serde::Deserialize;++use crate::domain::models::{GeneratedImage, ImageGenerationError, ImageGenerationRequest};+use crate::domain::ports::ImageGenerator;++#[cfg(test)]+mod test;++/// Gemini's native image model, marketed as Nano Banana.+pub const NANO_BANANA_MODEL: &str = "gemini-2.5-flash-image";++const DEFAULT_BASE_URL: &str = "https://generativelanguage.googleapis.com/v1beta";++/// Image generation runs for tens of seconds; well past a chat completion.+const REQUEST_TIMEOUT: Duration = Duration::from_secs(120);++/// Reqwest-backed [`ImageGenerator`] over the Gemini API.+#[derive(Clone)]+pub struct GeminiImageGenerator {+ client: reqwest::Client,+ base_url: String,+ api_key: String,+ model: String,+}++impl GeminiImageGenerator {+ /// A generator calling [`NANO_BANANA_MODEL`] with `api_key`.+ pub fn new(api_key: String) -> Self {+ Self {+ client: reqwest::Client::new(),+ base_url: DEFAULT_BASE_URL.to_string(),+ api_key,+ model: NANO_BANANA_MODEL.to_string(),+ }+ }++ /// Call a different Gemini image model.+ pub fn with_model(mut self, model: impl Into<String>) -> Self {+ self.model = model.into();+ self+ }++ /// Call a different API root (tests, proxies).+ pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {+ self.base_url = base_url.into().trim_end_matches('/').to_string();+ self+ }++ fn request_body(&self, request: &ImageGenerationRequest) -> serde_json::Value {+ let mut parts = vec![serde_json::json!({ "text": request.prompt })];+ parts.extend(request.reference_images.iter().map(|image| {+ serde_json::json!({+ "inlineData": {+ "mimeType": image.mime_type,+ "data": base64::engine::general_purpose::STANDARD.encode(&image.bytes),+ }+ })+ }));+ let mut generation_config = serde_json::json!({ "responseModalities": ["IMAGE"] });+ if let Some(aspect_ratio) = request.aspect_ratio {+ generation_config["imageConfig"] =+ serde_json::json!({ "aspectRatio": aspect_ratio.as_ratio() });+ }+ serde_json::json!({+ "contents": [{ "role": "user", "parts": parts }],+ "generationConfig": generation_config,+ })+ }+}++#[async_trait::async_trait]+impl ImageGenerator for GeminiImageGenerator {+ #[tracing::instrument(skip_all, fields(model = %self.model), err)]+ async fn generate_image(+ &self,+ request: &ImageGenerationRequest,+ ) -> Result<GeneratedImage, ImageGenerationError> {+ let response = self+ .client+ .post(format!(+ "{}/models/{}:generateContent",+ self.base_url, self.model+ ))+ .header("x-goog-api-key", &self.api_key)+ .timeout(REQUEST_TIMEOUT)+ .json(&self.request_body(request))+ .send()+ .await+ .context("Gemini request failed")+ .map_err(ImageGenerationError::Provider)?;++ let status = response.status();+ let body = response+ .text()+ .await+ .context("failed to read Gemini response")+ .map_err(ImageGenerationError::Provider)?;+ if !status.is_success() {+ let message = serde_json::from_str::<ErrorEnvelope>(&body)+ .ok()+ .map(|envelope| envelope.error.message)+ .unwrap_or(body);+ return Err(ImageGenerationError::Provider(anyhow::anyhow!(+ "Gemini returned HTTP {status}: {message}"+ )));+ }++ let parsed: GenerateContentResponse = serde_json::from_str(&body)+ .context("unexpected Gemini response shape")+ .map_err(ImageGenerationError::Provider)?;+ parsed.into_image()+ }+}++#[derive(Deserialize)]+struct ErrorEnvelope {+ error: ErrorBody,+}++#[derive(Deserialize)]+struct ErrorBody {+ #[serde(default)]+ message: String,+}++#[derive(Deserialize)]+#[serde(rename_all = "camelCase")]+struct GenerateContentResponse {+ #[serde(default)]+ candidates: Vec<Candidate>,+ prompt_feedback: Option<PromptFeedback>,+}++#[derive(Deserialize)]+#[serde(rename_all = "camelCase")]+struct PromptFeedback {+ block_reason: Option<String>,+}++#[derive(Deserialize)]+#[serde(rename_all = "camelCase")]+struct Candidate {+ content: Option<Content>,+ finish_reason: Option<String>,+}++#[derive(Deserialize)]+struct Content {+ #[serde(default)]+ parts: Vec<Part>,+}++#[derive(Deserialize)]+#[serde(rename_all = "camelCase")]+struct Part {+ text: Option<String>,+ inline_data: Option<InlineData>,+}++#[derive(Deserialize)]+#[serde(rename_all = "camelCase")]+struct InlineData {+ mime_type: String,+ data: String,+}++impl GenerateContentResponse {+ /// The first image in the response, with any accompanying text as its+ /// note. A response without an image is a refusal: the prompt was+ /// blocked, or the model answered in words.+ fn into_image(self) -> Result<GeneratedImage, ImageGenerationError> {+ if let Some(reason) = self+ .prompt_feedback+ .and_then(|feedback| feedback.block_reason)+ {+ return Err(ImageGenerationError::Refused(format!(+ "the prompt was blocked ({reason}); rephrase it and try again"+ )));+ }++ let mut image = None;+ let mut text = Vec::new();+ let mut finish_reason = None;+ for candidate in self.candidates {+ finish_reason = finish_reason.or(candidate.finish_reason);+ for part in candidate+ .content+ .into_iter()+ .flat_map(|content| content.parts)+ {+ match part {+ Part {+ inline_data: Some(data),+ ..+ } if image.is_none() => image = Some(data),+ Part {+ text: Some(text_part),+ ..+ } if !text_part.trim().is_empty() => text.push(text_part.trim().to_string()),+ _ => {}+ }+ }+ }+ let note = (!text.is_empty()).then(|| text.join("\n"));++ let Some(data) = image else {+ let reason = match (note, finish_reason) {+ (Some(text), _) => format!("the model returned no image, only text: {text}"),+ (None, Some(reason)) => {+ format!("the model returned no image (finish reason {reason})")+ }+ (None, None) => "the model returned no image".to_string(),+ };+ return Err(ImageGenerationError::Refused(reason));+ };+ let bytes = base64::engine::general_purpose::STANDARD+ .decode(data.data.as_bytes())+ .context("Gemini image data is not valid base64")+ .map_err(ImageGenerationError::Provider)?;+ Ok(GeneratedImage {+ bytes,+ mime_type: data.mime_type,+ note,+ })+ }+}diff --git a/crates/image_generation/src/outbound/gemini/test.rs b/crates/image_generation/src/outbound/gemini/test.rsnew file mode 100644index 0000000000..421644efd5--- /dev/null+++ b/crates/image_generation/src/outbound/gemini/test.rs@@ -0,0 +1,247 @@+use super::*;+use crate::domain::models::{ImageAspectRatio, ReferenceImage};+use wiremock::matchers::{body_json, header, method, path};+use wiremock::{Mock, MockServer, ResponseTemplate};++const PNG: &[u8] = b"\x89PNG\r\n\x1a\nfake";++fn generator(server: &MockServer) -> GeminiImageGenerator {+ GeminiImageGenerator::new("test-key".to_string()).with_base_url(server.uri())+}++fn request(aspect_ratio: Option<ImageAspectRatio>) -> ImageGenerationRequest {+ ImageGenerationRequest {+ prompt: "a lighthouse at dusk".to_string(),+ aspect_ratio,+ reference_images: Vec::new(),+ }+}++fn image_response(parts: serde_json::Value) -> serde_json::Value {+ serde_json::json!({+ "candidates": [{ "content": { "role": "model", "parts": parts }, "finishReason": "STOP" }],+ "usageMetadata": { "promptTokenCount": 8, "totalTokenCount": 1300 }+ })+}++#[tokio::test]+async fn posts_the_prompt_and_decodes_the_first_inline_image() {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path(format!("/models/{NANO_BANANA_MODEL}:generateContent")))+ .and(header("x-goog-api-key", "test-key"))+ .and(body_json(serde_json::json!({+ "contents": [{ "role": "user", "parts": [{ "text": "a lighthouse at dusk" }] }],+ "generationConfig": {+ "responseModalities": ["IMAGE"],+ "imageConfig": { "aspectRatio": "16:9" }+ }+ })))+ .respond_with(ResponseTemplate::new(200).set_body_json(image_response(+ serde_json::json!([+ { "text": " Here is your lighthouse. " },+ { "inlineData": { "mimeType": "image/png", "data": base64::engine::general_purpose::STANDARD.encode(PNG) } },+ { "inlineData": { "mimeType": "image/jpeg", "data": "AAAA" } }+ ]),+ )))+ .expect(1)+ .mount(&server)+ .await;++ let image = generator(&server)+ .generate_image(&request(Some(ImageAspectRatio::Widescreen)))+ .await+ .unwrap();++ assert_eq!(image.bytes, PNG);+ assert_eq!(image.mime_type, "image/png");+ assert_eq!(image.note.as_deref(), Some("Here is your lighthouse."));+}++#[tokio::test]+async fn posts_ordered_reference_images_with_their_mime_types_and_exact_bytes() {+ let server = MockServer::start().await;+ let jpeg = b"\xff\xd8\xff\xe0reference";+ let prompt = "Draw the subject from image 1 in the style of image 2";+ Mock::given(method("POST"))+ .and(path(format!("/models/{NANO_BANANA_MODEL}:generateContent")))+ .and(header("x-goog-api-key", "test-key"))+ .and(body_json(serde_json::json!({+ "contents": [{+ "role": "user",+ "parts": [+ { "text": prompt },+ {+ "inlineData": {+ "mimeType": "image/png",+ "data": base64::engine::general_purpose::STANDARD.encode(PNG),+ }+ },+ {+ "inlineData": {+ "mimeType": "image/jpeg",+ "data": base64::engine::general_purpose::STANDARD.encode(jpeg),+ }+ },+ ]+ }],+ "generationConfig": {+ "responseModalities": ["IMAGE"],+ "imageConfig": { "aspectRatio": "3:4" },+ }+ })))+ .respond_with(+ ResponseTemplate::new(200).set_body_json(image_response(serde_json::json!([+ { "text": "Here is the edited image." },+ {+ "inlineData": {+ "mimeType": "image/png",+ "data": base64::engine::general_purpose::STANDARD.encode(PNG),+ }+ },+ ]))),+ )+ .expect(1)+ .mount(&server)+ .await;++ let image = generator(&server)+ .generate_image(&ImageGenerationRequest {+ prompt: prompt.to_string(),+ aspect_ratio: Some(ImageAspectRatio::Portrait),+ reference_images: vec![+ ReferenceImage {+ bytes: PNG.to_vec(),+ mime_type: "image/png".to_string(),+ },+ ReferenceImage {+ bytes: jpeg.to_vec(),+ mime_type: "image/jpeg".to_string(),+ },+ ],+ })+ .await+ .unwrap();++ assert_eq!(image.bytes, PNG);+ assert_eq!(image.mime_type, "image/png");+ assert_eq!(image.note.as_deref(), Some("Here is the edited image."));+}++#[tokio::test]+async fn omits_image_config_without_an_aspect_ratio() {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .respond_with(ResponseTemplate::new(200).set_body_json(image_response(+ serde_json::json!([{ "inlineData": { "mimeType": "image/png", "data": "AAAA" } }]),+ )))+ .mount(&server)+ .await;++ generator(&server)+ .generate_image(&request(None))+ .await+ .unwrap();++ let requests = server.received_requests().await.unwrap();+ let body: serde_json::Value = serde_json::from_slice(&requests[0].body).unwrap();+ assert_eq!(+ body["generationConfig"],+ serde_json::json!({ "responseModalities": ["IMAGE"] })+ );+}++#[tokio::test]+async fn text_only_and_blocked_responses_are_refusals() {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .respond_with(ResponseTemplate::new(200).set_body_json(image_response(+ serde_json::json!([{ "text": "I can't draw that." }]),+ )))+ .up_to_n_times(1)+ .mount(&server)+ .await;+ let error = generator(&server)+ .generate_image(&request(None))+ .await+ .unwrap_err();+ assert!(+ matches!(&error, ImageGenerationError::Refused(reason) if reason.contains("I can't draw that.")),+ "{error:?}"+ );++ server.reset().await;+ Mock::given(method("POST"))+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({+ "promptFeedback": { "blockReason": "PROHIBITED_CONTENT" }+ })))+ .mount(&server)+ .await;+ let error = generator(&server)+ .generate_image(&request(None))+ .await+ .unwrap_err();+ assert!(+ matches!(&error, ImageGenerationError::Refused(reason) if reason.contains("PROHIBITED_CONTENT")),+ "{error:?}"+ );++ server.reset().await;+ Mock::given(method("POST"))+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({+ "candidates": [{ "finishReason": "IMAGE_SAFETY" }]+ })))+ .mount(&server)+ .await;+ let error = generator(&server)+ .generate_image(&request(None))+ .await+ .unwrap_err();+ assert!(+ matches!(&error, ImageGenerationError::Refused(reason) if reason.contains("IMAGE_SAFETY")),+ "{error:?}"+ );+}++#[tokio::test]+async fn http_errors_surface_the_provider_message() {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .respond_with(ResponseTemplate::new(429).set_body_json(serde_json::json!({+ "error": { "code": 429, "message": "Resource has been exhausted", "status": "RESOURCE_EXHAUSTED" }+ })))+ .mount(&server)+ .await;++ let error = generator(&server)+ .generate_image(&request(None))+ .await+ .unwrap_err();+ match error {+ ImageGenerationError::Provider(error) => {+ let message = format!("{error:#}");+ assert!(message.contains("429"), "{message}");+ assert!(message.contains("Resource has been exhausted"), "{message}");+ }+ other => panic!("expected a provider error, got {other:?}"),+ }+}++#[tokio::test]+async fn invalid_base64_is_a_provider_error() {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .respond_with(ResponseTemplate::new(200).set_body_json(image_response(+ serde_json::json!([{ "inlineData": { "mimeType": "image/png", "data": "not base64!" } }]),+ )))+ .mount(&server)+ .await;++ assert!(matches!(+ generator(&server)+ .generate_image(&request(None))+ .await+ .unwrap_err(),+ ImageGenerationError::Provider(_)+ ));+}diff --git a/crates/image_generation/src/outbound/references.rs b/crates/image_generation/src/outbound/references.rsnew file mode 100644index 0000000000..d2807d4b40--- /dev/null+++ b/crates/image_generation/src/outbound/references.rs@@ -0,0 +1,102 @@+//! Reads image references through the owning domains' attachment services.++use attachment::image::ImageData;+use attachment::{AttachmentError, AttachmentPart, AttachmentService};+use base64::Engine as _;+use base64::engine::general_purpose::STANDARD;+use model_entity::EntityType;+use model_owner::CreationPrincipal;+use non_empty::NonEmpty;++use crate::domain::models::{ImageReference, ReadImageError, ReferenceImage};+use crate::domain::ports::ImageReferenceReader;++#[cfg(test)]+mod test;++/// Resolves document and static-file identifiers through inbound services.+///+/// The document service verifies caller access. Static-file identifiers carry+/// the existing CDN capability semantics; their service reads only its+/// configured storage. Arbitrary URLs never enter this adapter.+pub struct AttachmentImageReferenceReader<D, S> {+ documents: D,+ static_files: S,+}++impl<D, S> AttachmentImageReferenceReader<D, S> {+ /// Use already-composed document and static-file attachment services.+ pub fn new(documents: D, static_files: S) -> Self {+ Self {+ documents,+ static_files,+ }+ }+}++impl<D: AttachmentService, S: AttachmentService> ImageReferenceReader+ for AttachmentImageReferenceReader<D, S>+{+ #[tracing::instrument(skip_all, err)]+ async fn read_image(+ &self,+ principal: &CreationPrincipal,+ reference: &ImageReference,+ ) -> Result<ReferenceImage, ReadImageError> {+ let user = principal.user().ok_or(ReadImageError::Unauthorized)?;+ let entity = match reference {+ ImageReference::Document(id) => EntityType::Document.with_entity_string(id.to_string()),+ ImageReference::StaticFile(id) => {+ EntityType::StaticFile.with_entity_string(id.to_string())+ }+ };+ let entities = [&entity];+ let entities = NonEmpty::new(entities.as_slice()).expect("one reference");+ let resolved = match reference {+ ImageReference::Document(_) => {+ self.documents+ .resolve_attachments(user.clone(), entities)+ .await+ }+ ImageReference::StaticFile(_) => {+ self.static_files+ .resolve_attachments(user.clone(), entities)+ .await+ }+ };+ let mut attachments = resolved.into_parts().into_inner();+ if attachments.len() != 1 {+ return Err(ReadImageError::NotImage);+ }+ let content = attachments+ .pop()+ .expect("one resolved attachment")+ .map_err(|error| attachment_error(error.error))?;+ let mut parts = content.content.into_inner();+ if parts.len() != 1 {+ return Err(ReadImageError::NotImage);+ }+ let AttachmentPart::Image(ImageData::Base64(image)) =+ parts.pop().expect("one attachment part")+ else {+ return Err(ReadImageError::NotImage);+ };+ let bytes = STANDARD+ .decode(image.base64_data())+ .map_err(|error| ReadImageError::Internal(rootcause::report!(error).into()))?;+ Ok(ReferenceImage {+ bytes,+ mime_type: "image/webp".to_owned(),+ })+ }+}++fn attachment_error(error: AttachmentError) -> ReadImageError {+ match error {+ AttachmentError::PermissionDenied(_) => ReadImageError::Unauthorized,+ AttachmentError::UnknownFileType+ | AttachmentError::UnsupportedFileType(_)+ | AttachmentError::NoContent => ReadImageError::NotImage,+ error => ReadImageError::Internal(rootcause::report!(error).into()),+ }+}diff --git a/crates/image_generation/src/outbound/references/test.rs b/crates/image_generation/src/outbound/references/test.rsnew file mode 100644index 0000000000..6199562f55--- /dev/null+++ b/crates/image_generation/src/outbound/references/test.rs@@ -0,0 +1,213 @@+use std::sync::{Arc, Mutex};++use attachment::{AttachmentContent, Attachments, ResolutionError};+use macro_user_id::user_id::MacroUserIdStr;+use model_entity::Entity;+use uuid::Uuid;++use super::*;++const ONE_BY_ONE_PNG: &[u8] = &[+ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,+ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,+ 0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0xF8, 0xCF, 0xC0, 0x00,+ 0x00, 0x03, 0x01, 0x01, 0x00, 0xC9, 0xFE, 0x92, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E,+ 0x44, 0xAE, 0x42, 0x60, 0x82,+];++#[derive(Clone, Copy)]+enum Outcome {+ Image,+ Denied,+ Unsupported,+ Failed,+ Text,+ Url,+ Mixed,+ Multiple,+}++type Calls = Arc<Mutex<Vec<(String, Entity<'static>)>>>;++struct Service {+ outcome: Outcome,+ calls: Calls,+}++impl AttachmentService for Service {+ async fn resolve_attachments<'a>(+ &self,+ user_id: MacroUserIdStr<'_>,+ ids: NonEmpty<&[&'a Entity<'a>]>,+ ) -> Attachments<'a> {+ let entity = ids[0];+ self.calls.lock().unwrap().push((+ user_id.to_string(),+ entity+ .entity_type+ .with_entity_string(entity.entity_id.to_string()),+ ));+ let error = match self.outcome {+ Outcome::Denied => Some(AttachmentError::PermissionDenied(Box::new(+ std::io::Error::new(std::io::ErrorKind::PermissionDenied, "not shared"),+ ))),+ Outcome::Unsupported => Some(AttachmentError::UnsupportedFileType("pdf".to_owned())),+ Outcome::Failed => Some(AttachmentError::Internal(anyhow::anyhow!("storage failed"))),+ _ => None,+ };+ if let Some(error) = error {+ return Attachments::one(Err(ResolutionError::new(+ entity+ .entity_type+ .with_entity_string(entity.entity_id.to_string()),+ error,+ )));+ }+ let image = || {+ AttachmentPart::Image(+ ImageData::try_from_bytes(ONE_BY_ONE_PNG.to_vec()).expect("valid PNG"),+ )+ };+ let parts = match self.outcome {+ Outcome::Text => vec![AttachmentPart::Content("not an image".to_owned())],+ Outcome::Url => vec![AttachmentPart::Image(ImageData::StaticUrl(+ "https://unresolved.example/photo.png".to_owned(),+ ))],+ Outcome::Mixed => vec![+ image(),+ AttachmentPart::Content("additional content".to_owned()),+ ],+ _ => vec![image()],+ };+ let content = AttachmentContent {+ reference: entity.clone(),+ name: None,+ content: NonEmpty::new(parts).expect("content"),+ };+ let resolved = Attachments::one(Ok(content));+ if matches!(self.outcome, Outcome::Multiple) {+ resolved.append(Ok(AttachmentContent {+ reference: entity.clone(),+ name: None,+ content: NonEmpty::one(image()),+ }))+ } else {+ resolved+ }+ }+}++fn principal() -> CreationPrincipal {+ CreationPrincipal::BotForUser {+ bot: bot_id::MACRO_AI_BOT_ID,+ user: MacroUserIdStr::try_from("macro|[email protected]".to_owned()).unwrap(),+ }+}++fn service(outcome: Outcome) -> (Service, Calls) {+ let calls = Arc::new(Mutex::new(Vec::new()));+ (+ Service {+ outcome,+ calls: Arc::clone(&calls),+ },+ calls,+ )+}++#[tokio::test]+async fn routes_references_to_the_owning_service_with_the_acting_user() {+ let (documents, document_calls) = service(Outcome::Image);+ let (static_files, static_calls) = service(Outcome::Image);+ let reader = AttachmentImageReferenceReader::new(documents, static_files);+ let document_id = Uuid::from_u128(1);+ let static_id = Uuid::from_u128(2);+ for reference in [+ ImageReference::Document(document_id),+ ImageReference::StaticFile(static_id),+ ] {+ let image = reader+ .read_image(&principal(), &reference)+ .await+ .expect("reference image");+ assert_eq!(image.mime_type, "image/webp");+ assert!(image.bytes.starts_with(b"RIFF"));+ assert_eq!(&image.bytes[8..12], b"WEBP");+ }+ assert_eq!(+ *document_calls.lock().unwrap(),+ vec![(+ "macro|[email protected]".to_owned(),+ EntityType::Document.with_entity_string(document_id.to_string()),+ )]+ );+ assert_eq!(+ *static_calls.lock().unwrap(),+ vec![(+ "macro|[email protected]".to_owned(),+ EntityType::StaticFile.with_entity_string(static_id.to_string()),+ )]+ );+}++#[tokio::test]+async fn denied_missing_or_failed_references_surface_errors() {+ for outcome in [Outcome::Denied, Outcome::Unsupported, Outcome::Failed] {+ let (documents, _) = service(outcome);+ let (static_files, _) = service(Outcome::Image);+ let reader = AttachmentImageReferenceReader::new(documents, static_files);+ let error = reader+ .read_image(&principal(), &ImageReference::Document(Uuid::from_u128(1)))+ .await+ .expect_err("reference rejected");+ match outcome {+ Outcome::Denied => assert!(matches!(error, ReadImageError::Unauthorized)),+ Outcome::Unsupported => assert!(matches!(error, ReadImageError::NotImage)),+ Outcome::Failed => assert!(matches!(error, ReadImageError::Internal(_))),+ _ => unreachable!(),+ }+ }+}++#[tokio::test]+async fn urls_text_and_ambiguous_results_are_never_forwarded_as_references() {+ for outcome in [+ Outcome::Url,+ Outcome::Text,+ Outcome::Mixed,+ Outcome::Multiple,+ ] {+ let (documents, _) = service(Outcome::Image);+ let (static_files, _) = service(outcome);+ let reader = AttachmentImageReferenceReader::new(documents, static_files);+ assert!(matches!(+ reader+ .read_image(+ &principal(),+ &ImageReference::StaticFile(Uuid::from_u128(1))+ )+ .await,+ Err(ReadImageError::NotImage)+ ));+ }+}++#[tokio::test]+async fn a_principal_without_an_acting_user_cannot_read_user_references() {+ let (documents, document_calls) = service(Outcome::Image);+ let (static_files, static_calls) = service(Outcome::Image);+ let reader = AttachmentImageReferenceReader::new(documents, static_files);+ let principal = CreationPrincipal::TeamBot {+ bot: bot_id::NonSystemBotId::new(bot_id::BotId::new_from_uuid(Uuid::from_u128(1)))+ .expect("non-system bot"),+ team: Uuid::from_u128(2),+ };+ assert!(matches!(+ reader+ .read_image(&principal, &ImageReference::Document(Uuid::from_u128(3)))+ .await,+ Err(ReadImageError::Unauthorized)+ ));+ assert!(document_calls.lock().unwrap().is_empty());+ assert!(static_calls.lock().unwrap().is_empty());+}diff --git a/crates/macro_aws_config/src/lib.rs b/crates/macro_aws_config/src/lib.rsindex 081a7e04ce..76889f523b 100644--- a/crates/macro_aws_config/src/lib.rs+++ b/crates/macro_aws_config/src/lib.rs@@ -11,7 +11,7 @@ maybe_env_var! {}maybe_env_var! {- /// Browser-facing LocalStack origin for a local stack with mapped ports.+ /// Browser-facing LocalStack endpoint, including any reverse-proxy path prefix.#[derive(Clone)]pub struct LocalAwsPublicUrl;}@@ -94,7 +94,7 @@ fn transform_local_url(url: &str, public_url: Option<&str>) -> String {/// For example, presigned urls for localstack come out as `http://{BUCKET_NAME}.localstack:{PORT}`/// but we need them to be formulated as `http://localhost:{PORT}/bucket-name`./// `LOCAL_AWS_PUBLIC_URL`, when set by the local stack, supplies the browser-facing-/// origin so named instances use their published port instead of Docker's port.+/// endpoint so named instances can use their HTTPS storage proxy.pub fn transform_aws_url(url: &str) -> String {if is_local_aws() {let public_url = LocalAwsPublicUrl::new();@@ -105,13 +105,22 @@ pub fn transform_aws_url(url: &str) -> String {/// internal method to transform a browser-facing local url into one reachable/// from inside the docker network-fn transform_internal_url(url: &str, local_aws_url: &str) -> String {+fn transform_internal_url(url: &str, local_aws_url: &str, public_url: Option<&str>) -> String {// NOTE: it is ok to use expect as this is only run locallylet parsed = url::Url::parse(url).expect("valid url");let host = parsed.host_str().unwrap();let path = parsed.path();let query = parsed.query().map(|q| format!("?{q}")).unwrap_or_default();+ // The HTTPS app proxy exposes storage below a path prefix. Match only+ // that configured endpoint and remove its prefix before calling S3.+ if let Some(public) = public_url.and_then(|url| url::Url::parse(url).ok())+ && parsed.origin() == public.origin()+ && let Some(key) = path.strip_prefix(&format!("{}/", public.path().trim_end_matches('/')))+ {+ return format!("{}/{key}{query}", local_aws_url.trim_end_matches('/'));+ }+// Browser-facing local URLs use `localhost`, which inside a container// resolves to the container itself. Use the SDK endpoint, including its// internal port, so service-to-service fetches reach LocalStack. Leave any other@@ -126,14 +135,17 @@ fn transform_internal_url(url: &str, local_aws_url: &str) -> String {/// Transforms a browser-facing local URL into one reachable from inside the/// app's own containers when fetching an object server-side.///-/// The inverse of [`transform_aws_url`]: presigned and distribution URLs are-/// minted with the `localhost` host so the browser on the host machine can-/// reach LocalStack, but a service fetching the same object from inside the-/// Docker network must use the `localstack` service hostname instead. No-op-/// outside local AWS.+/// The inverse of [`transform_aws_url`]: remove the configured public endpoint's+/// origin and proxy prefix so container clients reach LocalStack directly.+/// Legacy localhost URLs are also supported. No-op outside local AWS.pub fn transform_aws_url_for_internal_fetch(url: &str) -> String {if let Some(local_aws_url) = LocalAwsUrl::new() {- return transform_internal_url(url, local_aws_url.as_ref());+ let public_url = LocalAwsPublicUrl::new();+ return transform_internal_url(+ url,+ local_aws_url.as_ref(),+ public_url.as_ref().map(|url| url.as_ref()),+ );}url.to_string()}diff --git a/crates/macro_aws_config/src/test.rs b/crates/macro_aws_config/src/test.rsindex b935ab79ba..10647a713c 100644--- a/crates/macro_aws_config/src/test.rs+++ b/crates/macro_aws_config/src/test.rs@@ -115,7 +115,7 @@ fn test_internal_fetch_rewrites_localhost_to_localstack() {let input = "http://localhost:4566/doc-storage/macro%7Cteo%40macro.com/doc/1";let expected = "http://localstack:4566/doc-storage/macro%7Cteo%40macro.com/doc/1";- let result = transform_internal_url(input, "http://localstack:4566");+ let result = transform_internal_url(input, "http://localstack:4566", None);assert_eq!(result, expected);}@@ -124,7 +124,7 @@ fn test_internal_fetch_preserves_query_params() {let input = "http://localhost:4566/doc-storage/key?versionId=123";let expected = "http://localstack:4566/doc-storage/key?versionId=123";- let result = transform_internal_url(input, "http://localstack:4566");+ let result = transform_internal_url(input, "http://localstack:4566", None);assert_eq!(result, expected);}@@ -133,7 +133,7 @@ fn test_internal_fetch_localstack_is_idempotent() {let input = "http://localstack:4566/doc-storage/key";let expected = "http://localstack:4566/doc-storage/key";- let result = transform_internal_url(input, "http://localstack:4566");+ let result = transform_internal_url(input, "http://localstack:4566", None);assert_eq!(result, expected);}@@ -142,7 +142,7 @@ fn test_internal_fetch_leaves_remote_url_untouched() {let input = "https://d123.cloudfront.net/doc-storage/key?Signature=abc";let expected = "https://d123.cloudfront.net/doc-storage/key?Signature=abc";- let result = transform_internal_url(input, "http://localstack:4566");+ let result = transform_internal_url(input, "http://localstack:4566", None);assert_eq!(result, expected);}@@ -158,7 +158,7 @@ fn named_instance_uses_public_port_and_preserves_signature_and_encoded_key() {"http://localhost:29806/static-file-storage/file/pasted%20image.png?X-Amz-Signature=test&x-id=PutObject");assert_eq!(- transform_internal_url(&public, "http://localstack:4566"),+ transform_internal_url(&public, "http://localstack:4566", None),"http://localstack:4566/static-file-storage/file/pasted%20image.png?X-Amz-Signature=test&x-id=PutObject");}@@ -168,7 +168,37 @@ fn named_instance_uses_public_port_and_preserves_signature_and_encoded_key() {fn host_process_keeps_its_configured_localstack_endpoint_for_internal_fetch() {let input = "http://localhost:29806/doc-storage/key";assert_eq!(- transform_internal_url(input, "http://localhost:29806"),+ transform_internal_url(input, "http://localhost:29806", None),input);}++#[test]+fn https_storage_proxy_round_trips_encoded_keys_and_presigned_queries() {+ let endpoint = "https://dev-machine:21909/local-storage";+ let internal = "http://localstack:4566/doc-storage/macro%7Cuser%40example.com/image/10?X-Amz-Signature=test&x-id=PutObject";+ let public = transform_local_url(internal, Some(endpoint));+ assert_eq!(+ public,+ "https://dev-machine:21909/local-storage/doc-storage/macro%7Cuser%40example.com/image/10?X-Amz-Signature=test&x-id=PutObject"+ );+ assert_eq!(+ transform_internal_url(&public, "http://localstack:4566", Some(endpoint)),+ internal+ );+}++#[test]+fn storage_proxy_rewrite_is_scoped_to_its_origin_and_path() {+ let endpoint = "https://dev-machine:21909/local-storage";+ for input in [+ "https://other-machine:21909/local-storage/doc-storage/key",+ "https://dev-machine:21909/local-storage-other/key",+ "https://dev-machine:21909/dss/documents/key",+ ] {+ assert_eq!(+ transform_internal_url(input, "http://localstack:4566", Some(endpoint)),+ input+ );+ }+}diff --git a/crates/prompt/src/tool_usage.rs b/crates/prompt/src/tool_usage.rsindex 68b327aa0e..649d6bf5d0 100644--- a/crates/prompt/src/tool_usage.rs+++ b/crates/prompt/src/tool_usage.rs@@ -32,6 +32,8 @@ when the user explicitely asks you to _execute_ code.(which creates a file for the code execution environment) for the `CreateDocument` tool which creates a document in theusers workspace. If the user asks you to create a document, write a code file, or create any file you should use the `CreateDocument` tool.+- To make a picture (illustration, photo-style image, logo concept, mockup, diagram concept), use `GenerateImage`. It renders the prompt with an image model and saves the result as an image document; cite the returned document with a mention tag so the user sees it inline. `CreateDocument` cannot produce image bytes.+- `CreateDocument` content (for Markdown documents) is rendered with the same Markdown parser as your chat responses, channel messages, and email bodies, and citation syntax (`[[uuid]]`, `[[md;...]]`) works identically inside created documents. For linking to other Macro items from within that content, see the "Linking Macro items inside document content" rules. Non-Markdown documents (PDF, CSV, images, etc.) take raw content instead — no Markdown syntax or mention tags.- Create native Macro workbooks with `CreateDocument` using `fileExtension: "spreadsheet"`, empty `fileContent`, and `isTask: false`; then read and populate them with spreadsheet tools. Creation and editing work server-side even when nobody has the workbook open. Never use CSV/plaintext replacement to edit a native workbook.diff --git a/docker/docker-compose.yml b/docker/docker-compose.ymlindex d7ed7dc35e..84af9008b3 100644--- a/docker/docker-compose.yml+++ b/docker/docker-compose.yml@@ -549,6 +549,8 @@ services:- "8787:8787"volumes:- ./services/sync-service/src:/app/src+ # Durable Objects, KV, R2 and D1 must survive container replacement.+ - sync_state:/app/.wrangler/stateenvironment:- INTERNAL_API_SECRET_KEY=${INTERNAL_API_SECRET_KEY}- DOCUMENT_PERMISSIONS_SECRET=${DOCUMENT_PERMISSION_JWT}@@ -805,6 +807,7 @@ services:- otel-collectorvolumes:+ sync_state:# Backing store for the `lgtm` collector (profile-gated, global project).lgtm_data:diff --git a/docs/AGENT_GUIDE/ai-chat.md b/docs/AGENT_GUIDE/ai-chat.mdindex c052f043ee..90bf0b21c5 100644--- a/docs/AGENT_GUIDE/ai-chat.md+++ b/docs/AGENT_GUIDE/ai-chat.md@@ -33,6 +33,34 @@ bytes reached storage; previews, DOCX conversion, Markdown initialization, andindexing may finish asynchronously. Invalid contents, oversized files, and folderpermission failures should display a failed tool call without a successful result.+## Generating images with AI++`GenerateImage` takes a text prompt, renders it with Google's Nano Banana image+model, and saves the result as an image document. Optional arguments: a short file+name (otherwise the opening words of the prompt name the document), an aspect ratio+(`square`, `landscape`, `portrait`, `widescreen`, `tall`), and a project ID (edit+access required). The file extension comes from the generated format, so+`fileName: "lighthouse"` yields `lighthouse.png`.++For edits or variations, attach photos with the existing paperclip or use Macro+image documents the user can view. Pass up to three references in `referenceImages`,+for example `[{"type":"staticFile","id":"<UUID>"},{"type":"document","id":"<UUID>"}]`.+For an uploaded photo, use the UUID from `/file/<id>` in its attachment URL;+for a document, use its document ID. Describe the edit in `prompt`, referring to+image 1, image 2, and image 3 in array order. A description alone does not send+the photo to the image model. The result is saved as a new document.++The result appears as a standalone image card outside grouped tool calls. A+rounded header shows the filename above the image preview; clicking the card+opens the image document in a new app split. The card fits the image's scaled+width without side padding, preserves its aspect ratio, and truncates long+filenames in the header. The preview loads once the upload+is ready, with a placeholder while it is being prepared. Any commentary the+model added appears below the image.+Refused prompts, provider failures, and hosts without a Google Generative AI key+display a failed tool call; the error tells the agent whether to rephrase, retry,+or stop.+## Where chats liveThe Agents conversation list shows row skeletons after a short delay on first@@ -562,6 +590,11 @@ agent or opening repository settings; sending clears the attachment previews.Expanded queued promptslist their attached file names under the text; editing a queued prompt keeps them.+On a local stack, the in-memory agent reads uploaded image attachments from local+storage and sends their bytes to the model. The provider does not need access to+the stack's private hostname. This also applies to images in earlier turns when+resuming a conversation after a service restart.+Cursor walkthrough files the run re-hosts appear in the transcript after theanswer: screenshots as images, recordings as video players, and `.txt` / `.log`files as an inline `txt` code block (not a download link). Larger or non-UTF-8diff --git a/docs/AGENT_GUIDE/documents.md b/docs/AGENT_GUIDE/documents.mdindex 75a7252cb0..e80090d90c 100644--- a/docs/AGENT_GUIDE/documents.md+++ b/docs/AGENT_GUIDE/documents.md@@ -4,6 +4,13 @@ The document header has separate Share, Copy Share Link, and Side Panel buttons.They are borderless with a soft rounded background on hover. Share opens thesharing dialog; copying a link is a separate action.+On a local HTTPS stack, document and image downloads use `/local-storage/`+on the app's HTTPS origin. A request to HTTP localhost indicates a stale+storage URL or stack configuration; hard-refresh after updating the stack.+Markdown also needs a successful `/sync/document/.../connect` WebSocket upgrade.+A 403 there indicates the sync origin check, which the local proxy handles for+HTTPS machine hostnames; verify the proxy configuration before retrying.+## SpreadsheetsSpreadsheets are an internal pilot controlled by the `enable-spreadsheets` PostHogdiff --git a/docs/RUNNING_LOCALLY.md b/docs/RUNNING_LOCALLY.mdindex 354d450337..99d1b0b988 100644--- a/docs/RUNNING_LOCALLY.md+++ b/docs/RUNNING_LOCALLY.md@@ -131,15 +131,23 @@ This command:When startup finishes, the command prints the frontend URL and the important service URLs.The reverse proxy is HTTPS at `https://localhost:8090` (or the instance's proxy port) using a generated machine certificate signed by the checked-in development CA. Trust `infra/local/certs/ca.pem` once so the browser accepts it; see that directory's README. Local Caddy also reflects any `Origin` (wildcard CORS) so `https://` and `*.localhost` frontends can call the proxy.+For document sync, local Caddy normalizes the upstream `Origin` to the worker's+accepted localhost origin so HTTPS machine hostnames can open WebSockets.+Sync's local Durable Object, KV, R2 and D1 state lives in the Compose `sync_state`+volume so replacing its container preserves document content. When upgrading an+existing stack without that mount, stop sync and copy `/app/.wrangler/state` into+the volume before replacing the container. A restart alone does not rebuild its+compiled worker; rebuild the sync image after backend protocol changes.Open the frontend URL in your browser.The local environment supplies both `LOCAL_AWS_URL` (the container endpoint)-and `LOCAL_AWS_PUBLIC_URL` (the instance's published LocalStack port). SFS and-other presigned uploads use the public endpoint in browser-facing URLs. If an-upload attempts `localhost:4566` on a named instance, rebuild the service and-reload its generated environment; named instances publish storage on their own-port.+and `LOCAL_AWS_PUBLIC_URL` (the app's HTTPS origin with `/local-storage`).+Document downloads and presigned uploads use this storage proxy, so remote+browsers do not need a separate localhost storage connection. Container clients+translate that public endpoint back to LocalStack internally. If a download or+upload still attempts HTTP localhost, rebuild the services, reload the generated+environment and proxy, and hard-refresh the browser to discard cached URLs.If LocalStack loses its temporary resources after a restart, restore missingbuckets, queues, tables, and keys without resetting Postgres or volumes:diff --git a/services/agent_harness_service/Cargo.toml b/services/agent_harness_service/Cargo.tomlindex 96dc6a6679..7cbb5b3003 100644--- a/services/agent_harness_service/Cargo.toml+++ b/services/agent_harness_service/Cargo.toml@@ -63,7 +63,7 @@ lexical_client = { path = "../../crates/lexical_client" }lexical_mention_extractor = { path = "../../crates/lexical_mention_extractor" }macro_service_urls = { path = "../../crates/macro_service_urls" }messages = { path = "../../crates/messages", features = ["broker", "delivery"] }-static_file = { path = "../../crates/static_file" }+static_file = { path = "../../crates/static_file", features = ["attachment"] }static_file_service_client = { path = "../../crates/static_file_service_client" }macro_tower_layers = { path = "../../crates/macro_tower_layers" }notification = { path = "../../crates/notification" }diff --git a/services/agent_harness_service/src/config.rs b/services/agent_harness_service/src/config.rsindex 4c2e34630a..3729ee2a5e 100644--- a/services/agent_harness_service/src/config.rs+++ b/services/agent_harness_service/src/config.rs@@ -14,6 +14,10 @@ use macro_uuid::Uuid;use secretsmanager_client::LocalOrRemoteSecret;macro_env_var::env_vars!(+ /// Browser-facing static file base, read only when using local AWS.+ pub struct StaticFileServiceUrl;+ /// Local static file bucket, read only when using local AWS.+ pub struct StaticStorageBucket;/// Comma-separated Kafka bootstrap servers.#[derive(Clone)]pub struct KafkaBrokers;diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rsindex 48793cb9bb..4acb90f28a 100644--- a/services/agent_harness_service/src/main.rs+++ b/services/agent_harness_service/src/main.rs@@ -74,6 +74,7 @@ use agent_harness::outbound::runtime_registry::{HarnessKeyedConnections, Runtimeuse agent_inmem::domain::engine::TurnEngine;use agent_inmem::outbound::acp_mcp::AcpMcpConnector;use agent_inmem::outbound::egress_mcp::EgressMcpClient;+use agent_inmem::outbound::local_attachments::LocalAttachmentTurnEngine;use agent_inmem::outbound::log_frames::LogFrameSource;use agent_inmem::outbound::manager::InMemAgentManager;use agent_inmem::outbound::tool_catalog::McpToolCatalog;@@ -453,6 +454,29 @@ async fn run() -> anyhow::Result<()> {.context("failed to build the in-memory agent tool context")?;let inmem_model_engine: Arc<dyn TurnEngine> =Arc::new(RigTurnEngine::new(pool.clone(), tool_context));+ // A model provider cannot fetch images from a private local-stack hostname.+ // Resolve its static-file links through the existing attachment service,+ // including links replayed from earlier turns, before calling the model.+ let inmem_model_engine: Arc<dyn TurnEngine> = if let (Environment::Local, Some(local_aws)) =+ (&config.environment, macro_aws_config::LocalAwsUrl::new())+ {+ let public_base = url::Url::parse(config::StaticFileServiceUrl::new()?.as_ref())?;+ let cdn_base = format!(+ "{}/{}",+ local_aws.as_ref().trim_end_matches('/'),+ config::StaticStorageBucket::new()?.as_ref(),+ );+ let attachments = static_file::inbound::attachment::StaticFileAttachmentService::new(+ Arc::new(static_file::outbound::CdnStaticFileRepo::new(cdn_base)),+ );+ Arc::new(LocalAttachmentTurnEngine::new(+ inmem_model_engine,+ public_base,+ attachments,+ )?)+ } else {+ inmem_model_engine+ };// Cold attaches (fresh spawns and post-restart resumes) rebuild// their model context from the same log every frame lands in.let frames = Arc::new(LogFrameSource::new(session_repo.clone()));diff --git a/services/document_cognition_service/src/api/context/test.rs b/services/document_cognition_service/src/api/context/test.rsindex bf7ffad5fd..e8c0f062eb 100644--- a/services/document_cognition_service/src/api/context/test.rs+++ b/services/document_cognition_service/src/api/context/test.rs@@ -385,6 +385,9 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Arproperties_service.clone(),entity_access_service.clone(),),+ image_generation_tool_context: ai_tools::build_image_generation_tool_context_test(+ &document_tool_context,+ ),document_tool_context: document_tool_context.clone(),properties_tool_context: properties_tool_context.clone(),email_tool_context: email_tool_context.clone(),diff --git a/services/document_cognition_service/src/main.rs b/services/document_cognition_service/src/main.rsindex cb14747198..eb7aae8d61 100644--- a/services/document_cognition_service/src/main.rs+++ b/services/document_cognition_service/src/main.rs@@ -657,6 +657,10 @@ async fn main() -> anyhow::Result<()> {properties_service,entity_access_service.clone(),),+ image_generation_tool_context: ai_tools::build_image_generation_tool_context(+ &document_tool_context,+ ai_tools::build_image_generator_from_env(),+ )?,document_tool_context: document_tool_context.clone(),properties_tool_context: properties_tool_context.clone(),email_tool_context: email_tool_context.clone(),diff --git a/services/mcp_service/src/context.rs b/services/mcp_service/src/context.rsindex 1501eaeb28..1f11c82a07 100644--- a/services/mcp_service/src/context.rs+++ b/services/mcp_service/src/context.rs@@ -406,6 +406,10 @@ async fn build_tool_context(args: ToolContextBuildArgs<'_>) -> anyhow::Result<Toproperties_service,entity_access_service.clone(),),+ image_generation_tool_context: ai_tools::build_image_generation_tool_context(+ &document_tool_context,+ ai_tools::build_image_generator_from_env(),+ )?,document_tool_context,properties_tool_context,email_tool_context,diff --git a/tooling/xtask/crates/xtask_local/src/local/local_env.rs b/tooling/xtask/crates/xtask_local/src/local/local_env.rsindex 36f49e3e25..601a1a33eb 100644--- a/tooling/xtask/crates/xtask_local/src/local/local_env.rs+++ b/tooling/xtask/crates/xtask_local/src/local/local_env.rs@@ -97,6 +97,7 @@ impl LocalEnv {} else {frontend::https_origin(instance)?};+ let infra = InfraEnv::local(&frontend_origin);Ok(LocalEnv {// Both local flavors run against local infra (`local` env defaults).environment: mode.environment_var(),@@ -118,7 +119,7 @@ impl LocalEnv {preview_ssh_proxy_host: tunnels.preview_ssh.map(str::to_owned),preview_ssh_port: instance.port(Port::PreviewSsh),preview_https_port: instance.port(Port::PreviewHttps),- infra: InfraEnv::local(instance),+ infra,storage: StorageEnv::local(),queues: QueueEnv::local(),mail: MailEnv::local(),@@ -219,13 +220,13 @@ struct InfraEnv {}impl InfraEnv {- fn local(instance: &Instance) -> Self {+ fn local(frontend_origin: &str) -> Self {InfraEnv {database_url: "postgres://user:password@postgres:5432/macrodb".into(),redis_uri: "redis://redis:6379".into(),opensearch_url: "http://search:9200".into(),local_aws_url: "http://localstack:4566".into(),- local_aws_public_url: format!("http://localhost:{}", instance.port(Port::LocalStack)),+ local_aws_public_url: format!("{frontend_origin}/local-storage"),// The broker's in-network listener (see docker/docker-compose-databases.yml);// host processes use localhost:9092 instead.kafka_brokers: "kafka:29092".into(),@@ -247,6 +248,16 @@ impl InfraEnv {"LOCAL_AWS_PUBLIC_URL".into(),self.local_aws_public_url.clone(),);+ // Document GET URLs skip CloudFront signing locally. Keep their base+ // on this instance's published S3 endpoint, above the Doppler layer.+ env.insert(+ "DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_DISTRIBUTION_URL".into(),+ format!(+ "{}/{}",+ self.local_aws_public_url,+ resources::DOC_STORAGE_BUCKET+ ),+ );env.insert("KAFKA_BROKERS".into(), self.kafka_brokers.clone());// In-network services resolve the gateway through the OVERRIDE_ var;// without it the resolver's Environment::Local default@@ -658,13 +669,8 @@ impl BootStubEnv {env.insert("PIPEDREAM_WEBHOOK_SECRET".into(), "local".into());env.insert("OPENSEARCH_USERNAME".into(), "macrouser".into());env.insert("OPENSEARCH_PASSWORD".into(), "local".into());- // document_storage_service's presigned-URL config. Locally the- // `is_local_aws()` branch skips CloudFront signing entirely, so only a- // well-formed base URL is needed.- env.insert(- "DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_DISTRIBUTION_URL".into(),- "http://localhost:8100".into(),- );+ // Local document URLs use the instance endpoint supplied by InfraEnv;+ // signing is skipped by the `is_local_aws()` branch.env.insert("DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_SIGNER_PUBLIC_KEY_ID".into(),"local-cloudfront-signer".into(),diff --git a/tooling/xtask/crates/xtask_local/src/local/local_env/test.rs b/tooling/xtask/crates/xtask_local/src/local/local_env/test.rsindex 9a70eec331..58cb881087 100644--- a/tooling/xtask/crates/xtask_local/src/local/local_env/test.rs+++ b/tooling/xtask/crates/xtask_local/src/local/local_env/test.rs@@ -510,7 +510,11 @@ fn named_instance_separates_browser_and_container_aws_endpoints() {assert_eq!(env["LOCAL_AWS_URL"], "http://localstack:4566");assert_eq!(env["LOCAL_AWS_PUBLIC_URL"],- format!("http://localhost:{}", instance.port(Port::LocalStack))+ format!("{}/local-storage", env["FRONTEND_ORIGIN"])+ );+ assert_eq!(+ env["DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_DISTRIBUTION_URL"],+ format!("{}/local-storage/doc-storage", env["FRONTEND_ORIGIN"]));}diff --git a/tooling/xtask/crates/xtask_local/src/local/proxy.rs b/tooling/xtask/crates/xtask_local/src/local/proxy.rsindex 749bc2ac22..ee7a79d792 100644--- a/tooling/xtask/crates/xtask_local/src/local/proxy.rs+++ b/tooling/xtask/crates/xtask_local/src/local/proxy.rs@@ -59,6 +59,7 @@ pub fn frontend_path_prefixes() -> Vec<&'static str> {"/lexical","/ai-editing","/static-file",+ "/local-storage",]).collect()}@@ -111,8 +112,17 @@ fn caddyfile(mode: Mode, static_frontend: bool) -> String {// every service allowlist. `run_dev` still fans out to the shared-dev// gateway, so it keeps service CORS as-is.let cors_block = if mode == Mode::Local { LOCAL_CORS } else { "" };+ // Sync actively rejects unknown origins, including HTTPS machine names,+ // before upgrading a socket. Local CORS is owned by this proxy; normalize+ // only the local worker's upstream origin to its existing dev allowlist.+ let sync_origin = if mode == Mode::Local {+ " header_up Origin http://localhost:3000\n"+ } else {+ ""+ };+ let special_routes = SPECIAL_ROUTES.replace("SYNC_ORIGIN_HEADER", sync_origin);format!(- "{CADDY_HEAD}{cors_block}{routes}{SPECIAL_ROUTES}{mailpit_block}{static_block}{frontend_block}{CADDY_TAIL}{preview_block}",+ "{CADDY_HEAD}{cors_block}{routes}{special_routes}{mailpit_block}{static_block}{frontend_block}{CADDY_TAIL}{preview_block}",routes = service_routes(mode))}@@ -245,7 +255,8 @@ const SPECIAL_ROUTES: &str = r#" @websocket path /websocket /websocket/*@sync path /sync /sync/*handle @sync {uri strip_prefix /sync- reverse_proxy sync-service:8787+ reverse_proxy sync-service:8787 {+SYNC_ORIGIN_HEADER }}# Analytics/telemetry proxy worker (PostHog and OTLP traces/logs).# No prefix strip: the worker itself routes on the /i/{ph,dd,otlp} prefix,@@ -276,7 +287,10 @@ const MAILPIT_ROUTE: &str = r#" # Mailpit serves itself under /mailpit (MP_WE/// Local: /api and /internal go to the service, everything else to the S3 bucket/// via LocalStack (mirrors infra/local/nginx/static-file-cdn.conf).-const STATIC_FILE_LOCAL: &str = r#" handle_path /static-file/* {+const STATIC_FILE_LOCAL: &str = r#" handle_path /local-storage/* {+ reverse_proxy localstack:4566+ }+ handle_path /static-file/* {# Keep service dispatch before the S3 rewrite inside this exclusive handle.route {@svc path /api/* /internal/*diff --git a/tooling/xtask/crates/xtask_local/src/local/proxy/test.rs b/tooling/xtask/crates/xtask_local/src/local/proxy/test.rsindex a146875716..ffd99d81c0 100644--- a/tooling/xtask/crates/xtask_local/src/local/proxy/test.rs+++ b/tooling/xtask/crates/xtask_local/src/local/proxy/test.rs@@ -16,6 +16,7 @@ fn vite_routes_cover_every_backend_prefix_and_no_frontend_routes() {"/lexical","/ai-editing","/static-file",+ "/local-storage",] {assert!(prefixes.contains(&prefix));}@@ -117,6 +118,8 @@ fn document_content_services_are_available_through_the_proxy() {assert!(caddy.contains("uri strip_prefix /sync"));assert!(caddy.contains("reverse_proxy sync-service:8787"));+ assert!(caddy.contains("header_up Origin http://localhost:3000"));+ assert!(!caddyfile(Mode::Dev, false).contains("header_up Origin"));assert!(caddy.contains("handle_path /lexical/*"));assert!(caddy.contains("reverse_proxy lexical-service:8096"));assert!(caddy.contains("handle_path /ai-editing/*"));@@ -134,6 +137,8 @@ fn static_file_block_is_mode_specific() {assert!(!local.contains("route /static-file/*"));assert!(caddyfile(Mode::Dev, false).contains("handle_path /static-file/*"));assert!(!caddyfile(Mode::Dev, false).contains("/static-file-storage"));+ assert!(local.contains("handle_path /local-storage/*"));+ assert!(!caddyfile(Mode::Dev, false).contains("handle_path /local-storage/*"));}/// Drift gate across the Rust↔TypeScript seam: every proxied service's prefix