@ai-agent-forge/plugin-image-generation 0.88.10 → 0.88.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/MANUAL.md CHANGED
@@ -12,3 +12,4 @@
12
12
  2. 生成结果是文件产物(落盘路径在工具结果里),把路径告诉用户,不要把 base64 贴进对话。
13
13
  3. 未配置槽位时的错误消息含全部三种配置方式,照转给用户即可。
14
14
  4. **图片下载走标准代理环境变量**(`HTTPS_PROXY`/`HTTP_PROXY`,curl 同款约定)。供应商返回的图床 URL 若在宿主网络不可直连,启动 agent-forge 时带代理 env,否则 image_generate 会失败在下载阶段;此时可如实用 bash + 代理取 `b64_json` 变通,但要如实说明工具本体失败。
15
+ 5. `image_edit` 自动按模型 ID 选请求协议:`grok-imagine-image` 与 `grok-imagine-image-*` 使用 JSON data URL,其他模型使用 multipart; endpoint 和凭据仍来自宿主 provider 配置。
package/README.md CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  First-party image generation capability plugin for Agent Forge. Registers the
4
4
  `image_generate` and `image_edit` tools (full-control hosts) against the
5
- host-configured provider's OpenAI-compatible images API:
5
+ host-configured provider's image endpoint (OpenAI-compatible request shapes
6
+ by default, with a Grok Imagine edit adapter):
6
7
 
7
8
  - Credentials (`apiKey` + `baseUrl`) are resolved at runtime through the public
8
9
  `CredentialAPI` (`api.credentials.resolve({ providerId })`); the plugin keeps
@@ -31,6 +32,11 @@ host-configured provider's OpenAI-compatible images API:
31
32
  - Cancellation cascades through `api.sessionAbortSignal()` and the invocation
32
33
  signal; requests time out after 180s and error messages never echo the API
33
34
  key or request headers.
35
+ - Image edits use the provider's `/images/edits` endpoint. The plugin selects
36
+ the request protocol from the model ID: `grok-imagine-image` and
37
+ `grok-imagine-image-*` use the Grok JSON data-URL shape; other models use
38
+ OpenAI multipart. Provider credentials and endpoint configuration remain
39
+ host supplied.
34
40
 
35
41
  ## Configuring an image provider
36
42
 
@@ -45,7 +51,7 @@ host-configured provider's OpenAI-compatible images API:
45
51
  ```
46
52
 
47
53
  `models.json` registers the provider and its image model (the `output` field
48
- marks image-capable models; `cost` requires all four rate keys):
54
+ marks image-capable models):
49
55
 
50
56
  ```json
51
57
  {
@@ -59,7 +65,6 @@ marks image-capable models; `cost` requires all four rate keys):
59
65
  "reasoning": false,
60
66
  "input": ["text"],
61
67
  "output": ["image"],
62
- "cost": { "input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0 },
63
68
  "contextWindow": 8192,
64
69
  "maxTokens": 4096
65
70
  }
@@ -78,7 +83,7 @@ provider-registered keys are resolved too.
78
83
  With only the `models.json` registration (no `capabilities` settings), the
79
84
  plugin auto-uses the single registered image provider (inference layers 4/5).
80
85
  Registering an image model in `models.json` implies that provider really has
81
- an `/images/generations` endpoint; builtin chat catalog models never take part
86
+ an `/images/generations` endpoint; chat models never take part
82
87
  in the inference. The slot travels to the plugin through the host's
83
88
  assembly-time `configOverride` injection (`api.config.imageGeneration` +
84
89
  `api.config.imageModelCatalog` snapshot) — the plugin never reads host
package/agent-forge.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "packageFormatVersion": 1,
3
3
  "id": "agent-forge.plugin.image-generation",
4
- "pluginVersion": "0.88.10",
4
+ "pluginVersion": "0.88.12",
5
5
  "apiVersion": "1",
6
6
  "minHostVersion": "0.85.14",
7
7
  "capabilityRefs": [
@@ -1 +1 @@
1
- {"version":3,"file":"capability.d.ts","sourceRoot":"","sources":["../src/capability.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAiB,2BAA2B,EAAgB,MAAM,yBAAyB,CAAC;AA8dxG;;;;;GAKG;AACH,wBAAgB,wCAAwC,CAAC,OAAO,CAAC,EAAE;IAClE,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;CAClC,GAAG,2BAA2B,CA+K9B","sourcesContent":["import { mkdir, readFile, realpath, stat, writeFile } from \"node:fs/promises\";\nimport { join, sep } from \"node:path\";\nimport type { CapabilityAPI, CapabilityPluginSyncFactory, PluginConfig } from \"@agent-forge/plugin-sdk\";\nimport { Type } from \"typebox\";\nimport { wrapFetchWithEnvProxyV1 } from \"./env-proxy-fetch.ts\";\nimport { createImagesClientV1 } from \"./images-client.ts\";\nimport {\n\tboundedImageMessageV1,\n\tdetectImageFormatV1,\n\tIMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1,\n\tIMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1,\n\ttype ImageFormatV1,\n\ttype ImageGenResultV1,\n\ttype ImagesClientResponseV1,\n} from \"./types.ts\";\n\nconst IMAGE_GENERATE_PARAMETERS = Type.Object(\n\t{\n\t\tprompt: Type.String({ minLength: 1, maxLength: 4000 }),\n\t\tsize: Type.Optional(Type.String({ minLength: 1, maxLength: 32 })),\n\t\tquality: Type.Optional(Type.String({ minLength: 1, maxLength: 32 })),\n\t\tn: Type.Optional(Type.Integer({ minimum: 1, maximum: 4 })),\n\t\tprovider: Type.Optional(Type.String({ minLength: 1, maxLength: 64 })),\n\t\tmodel: Type.Optional(Type.String({ minLength: 1, maxLength: 128 })),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst IMAGE_EDIT_PARAMETERS = Type.Object(\n\t{\n\t\timagePath: Type.String({ minLength: 1, maxLength: 512, pattern: \"^[A-Za-z0-9_][A-Za-z0-9._/-]*$\" }),\n\t\tprompt: Type.String({ minLength: 1, maxLength: 4000 }),\n\t\tprovider: Type.Optional(Type.String({ minLength: 1, maxLength: 64 })),\n\t\tmodel: Type.Optional(Type.String({ minLength: 1, maxLength: 128 })),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst IMAGE_PATH_PATTERN_V1 = /^[A-Za-z0-9_][A-Za-z0-9._/-]*$/u;\n\nconst GENERATED_IMAGES_DIRECTORY = \"generated-images\";\n\ninterface ImageFailureV1 {\n\treadonly code: string;\n\treadonly message: string;\n}\n\nfunction objectInput(first: unknown, second: unknown): Record<string, unknown> {\n\t// Shape-based dual-convention read (aligned with subagent-delegate): the\n\t// runtime invoke face passes the input object first and the host\n\t// CapabilityToolContext second; the agent tool face passes the tool call\n\t// id (a string) first and the params object second.\n\tconst value = first !== null && typeof first === \"object\" && !Array.isArray(first) ? first : second;\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new Error(\"Image generation input must be an object\");\n\treturn value as Record<string, unknown>;\n}\n\nfunction signalFrom(value: unknown): AbortSignal | undefined {\n\treturn value instanceof AbortSignal ? value : undefined;\n}\n\nfunction combineSignals(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined {\n\tconst present = signals.filter((signal): signal is AbortSignal => signal !== undefined);\n\tif (present.length === 0) return undefined;\n\tif (present.length === 1) return present[0];\n\treturn AbortSignal.any(present);\n}\n\nfunction failed(model: string, code: string, message: string): ImageGenResultV1 {\n\treturn {\n\t\tversion: 1,\n\t\tstatus: \"failed\",\n\t\tfiles: [],\n\t\tmodel,\n\t\terror: { code, message: boundedImageMessageV1(message) },\n\t};\n}\n\nfunction cancelled(model: string): ImageGenResultV1 {\n\treturn {\n\t\tversion: 1,\n\t\tstatus: \"cancelled\",\n\t\tfiles: [],\n\t\tmodel,\n\t\terror: { code: \"cancelled\", message: \"Image generation was cancelled\" },\n\t};\n}\n\n/** `param` (non-empty string), a non-empty env value, or undefined. */\nfunction nonEmptyEnvValue(name: string): string | undefined {\n\tconst value = process.env[name];\n\treturn typeof value === \"string\" && value.trim() !== \"\" ? value : undefined;\n}\n\nfunction boundedString(value: unknown, maxLength: number): string | undefined {\n\treturn typeof value === \"string\" && value !== \"\" && value.length <= maxLength ? value : undefined;\n}\n\n/** settings 槽位形状(宿主经 configOverride 注入,设计 §3.1/§3.3)。 */\ninterface ImageGenerationSlotV1 {\n\treadonly provider: string;\n\treadonly model?: string;\n}\n\n/** 目录快照条目:宿主装配期从 models.json 登记项投影(设计 §3.2 推断供料)。 */\ninterface CatalogModelV1 {\n\treadonly id: string;\n\treadonly name?: string;\n}\n\ntype ImageModelCatalogV1 = Readonly<Record<string, ReadonlyArray<CatalogModelV1>>>;\n\nfunction readInjectedSlot(config: PluginConfig): { readonly slot?: ImageGenerationSlotV1; readonly error?: string } {\n\tconst raw = config.imageGeneration;\n\tif (raw === undefined) return {};\n\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw))\n\t\treturn { error: \"injected config imageGeneration must be an object\" };\n\tconst record = raw as Record<string, unknown>;\n\tif (typeof record.provider !== \"string\" || record.provider.trim() === \"\")\n\t\treturn {\n\t\t\terror: `injected config imageGeneration.provider must be a non-empty string (got ${typeof record.provider})`,\n\t\t};\n\tif (record.model !== undefined && (typeof record.model !== \"string\" || record.model.trim() === \"\"))\n\t\treturn { error: \"injected config imageGeneration.model must be a non-empty string when present\" };\n\treturn { slot: { provider: record.provider, ...(record.model === undefined ? {} : { model: record.model }) } };\n}\n\nfunction readInjectedCatalog(config: PluginConfig): { readonly catalog: ImageModelCatalogV1; readonly error?: string } {\n\tconst raw = config.imageModelCatalog;\n\tif (raw === undefined) return { catalog: {} };\n\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw))\n\t\treturn {\n\t\t\tcatalog: {},\n\t\t\terror: \"injected config imageModelCatalog must be an object mapping providers to image models\",\n\t\t};\n\tconst catalog: Record<string, Array<{ id: string; name?: string }>> = {};\n\tfor (const [providerId, entries] of Object.entries(raw as Record<string, unknown>)) {\n\t\tif (!Array.isArray(entries))\n\t\t\treturn { catalog: {}, error: `injected config imageModelCatalog[\"${providerId}\"] must be an array` };\n\t\tconst models: Array<{ id: string; name?: string }> = [];\n\t\tfor (const entry of entries) {\n\t\t\tif (typeof entry !== \"object\" || entry === null)\n\t\t\t\treturn { catalog: {}, error: `injected config imageModelCatalog[\"${providerId}\"] entries must be objects` };\n\t\t\tconst record = entry as Record<string, unknown>;\n\t\t\tif (typeof record.id !== \"string\" || record.id === \"\")\n\t\t\t\treturn {\n\t\t\t\t\tcatalog: {},\n\t\t\t\t\terror: `injected config imageModelCatalog[\"${providerId}\"] entries need a non-empty string id`,\n\t\t\t\t};\n\t\t\tmodels.push({ id: record.id, ...(typeof record.name === \"string\" ? { name: record.name } : {}) });\n\t\t}\n\t\tif (models.length > 0) catalog[providerId] = models;\n\t}\n\treturn { catalog };\n}\n\n/** 确定性排序键:name 优先(缺省回退 id),同键按 id;码点序,与宿主快照顺序无关。 */\nfunction compareCatalogModels(left: CatalogModelV1, right: CatalogModelV1): number {\n\tconst leftKey = left.name ?? left.id;\n\tconst rightKey = right.name ?? right.id;\n\tif (leftKey !== rightKey) return leftKey < rightKey ? -1 : 1;\n\treturn left.id < right.id ? -1 : left.id > right.id ? 1 : 0;\n}\n\ntype ImageTargetResolution =\n\t| { readonly ok: true; readonly provider: string; readonly model: string; readonly modelSourceNote?: string }\n\t| { readonly ok: false; readonly message: string };\n\nconst IMAGE_GENERATION_CONFIG_EXAMPLE =\n\t'Example settings.json: { \"capabilities\": { \"imageGeneration\": { \"provider\": \"myimg\", \"model\": \"gpt-image-1\" } } }';\n\n/**\n * 六层取值链(供应商能力槽位设计 §3.2):\n * 1. 工具参数 provider/model;\n * 2. settings 槽位(宿主注入的 config.imageGeneration,model 可省);\n * 3. env AGENT_FORGE_IMAGEGEN_PROVIDER/MODEL(存量兼容);\n * 4. 主供应商推断——会话当前 provider 在目录快照中登记过生图模型时取用;\n * 5. 唯一候选推断——全配置域恰好一个 provider 登记过生图模型(且非当前主)时取用;\n * 6. fail-closed——config_missing,多候选时列出候选清单。\n * model 在 1-3 层均未命中时按已解析 provider 的登记生图模型推断(唯一命中直接\n * 用;多命中确定性排序取第一个并注明来源)。\n */\nfunction resolveImageTarget(\n\tinput: Record<string, unknown>,\n\tslot: ImageGenerationSlotV1 | undefined,\n\tcatalog: ImageModelCatalogV1,\n\tsessionProvider: string | undefined,\n): ImageTargetResolution {\n\tconst paramProvider = boundedString(input.provider, 64);\n\tconst paramModel = boundedString(input.model, 128);\n\tconst envProvider = nonEmptyEnvValue(\"AGENT_FORGE_IMAGEGEN_PROVIDER\");\n\tconst envModel = nonEmptyEnvValue(\"AGENT_FORGE_IMAGEGEN_MODEL\");\n\n\tlet provider = paramProvider ?? slot?.provider ?? envProvider;\n\tif (provider === undefined) {\n\t\tif (sessionProvider !== undefined && (catalog[sessionProvider]?.length ?? 0) > 0) provider = sessionProvider;\n\t\telse {\n\t\t\tconst candidates = Object.keys(catalog)\n\t\t\t\t.filter((providerId) => catalog[providerId].length > 0)\n\t\t\t\t.sort();\n\t\t\tif (candidates.length === 1) provider = candidates[0];\n\t\t\telse if (candidates.length > 1)\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t`Multiple image-generation providers are registered in models.json (${candidates.join(\", \")}); image generation fails closed instead of guessing. ` +\n\t\t\t\t\t\t`Configure capabilities.imageGeneration in settings.json or pass the provider parameter. ${IMAGE_GENERATION_CONFIG_EXAMPLE}`,\n\t\t\t\t};\n\t\t\telse\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Image generation is not configured: pass provider and model via the tool parameters, set capabilities.imageGeneration in settings.json, \" +\n\t\t\t\t\t\t'set AGENT_FORGE_IMAGEGEN_PROVIDER and AGENT_FORGE_IMAGEGEN_MODEL, or register an image model (\"output\": [\"image\"]) for a provider in models.json. ' +\n\t\t\t\t\t\tIMAGE_GENERATION_CONFIG_EXAMPLE,\n\t\t\t\t};\n\t\t}\n\t}\n\n\tlet model = paramModel ?? slot?.model ?? envModel;\n\tlet modelSourceNote: string | undefined;\n\tif (model === undefined) {\n\t\tconst registered = catalog[provider] ?? [];\n\t\tif (registered.length === 1) model = registered[0].id;\n\t\telse if (registered.length > 1) {\n\t\t\tconst first = [...registered].sort(compareCatalogModels)[0];\n\t\t\tmodel = first.id;\n\t\t\tmodelSourceNote =\n\t\t\t\t`model ${first.id} was picked deterministically (first by name) from ${registered.length} image models registered for provider ${provider} in models.json; ` +\n\t\t\t\t\"pass an explicit model to choose another\";\n\t\t} else\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\tmessage:\n\t\t\t\t\t`No image model is configured for provider ${provider}: no model came from the tool parameters, settings, or env, ` +\n\t\t\t\t\t`and provider ${provider} has no models.json model with \"output\": [\"image\"]. Register one or pass an explicit model. ${IMAGE_GENERATION_CONFIG_EXAMPLE}`,\n\t\t\t};\n\t}\n\treturn { ok: true, provider, model, ...(modelSourceNote === undefined ? {} : { modelSourceNote }) };\n}\n\nfunction promptFrom(value: unknown): string | undefined {\n\treturn boundedString(value, 4000);\n}\n\nfunction countFrom(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isSafeInteger(value) && value >= 1 && value <= 4 ? value : undefined;\n}\n\n/** Workspace-relative identifier confined to the generated-images directory. */\nfunction validImagePath(imagePath: string): boolean {\n\tif (!IMAGE_PATH_PATTERN_V1.test(imagePath)) return false;\n\tif (!imagePath.startsWith(`${GENERATED_IMAGES_DIRECTORY}/`)) return false;\n\treturn !imagePath.split(\"/\").includes(\"..\");\n}\n\n/**\n * image_edit ships workspace bytes to an external image provider, so the\n * lexical validImagePath gate is not enough: a symlink inside\n * generated-images/ can point anywhere on disk. Same sandbox model as the\n * workspace-files-write host: resolve the workspace root and the candidate to\n * their real paths and require the candidate to stay inside the real root.\n * A missing file (or dangling link) fails realpath and maps to the regular\n * not-found outcome; a link resolving outside the workspace is a boundary\n * escape and is refused. Links that stay inside the workspace keep working —\n * their bytes never leave the sandbox boundary.\n */\nasync function sandboxedImagePath(workspace: string, imagePath: string): Promise<\"missing\" | \"escape\" | \"ok\"> {\n\tconst root = await realpath(workspace).catch(() => undefined);\n\tif (root === undefined) return \"missing\";\n\tconst real = await realpath(join(workspace, imagePath)).catch(() => undefined);\n\tif (real === undefined) return \"missing\";\n\tif (real !== root && !real.startsWith(root + sep)) return \"escape\";\n\treturn \"ok\";\n}\n\n/**\n * Local-time `yyyymmdd-HHmmss` stamp shared by every file of one invocation.\n * Local (not UTC): users find today's images by the date in the filename, and\n * on UTC+8 machines a UTC stamp labels evening images as \"tomorrow\". Sorting\n * stays monotonic wherever local time has no DST jumps (e.g. China).\n */\nfunction localStampV1(date: Date): string {\n\tconst pad = (value: number): string => String(value).padStart(2, \"0\");\n\treturn `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}-${pad(\n\t\tdate.getHours(),\n\t)}${pad(date.getMinutes())}${pad(date.getSeconds())}`;\n}\n\ninterface ImageToolSuccessV1 {\n\treadonly content: readonly { readonly type: \"text\"; readonly text: string }[];\n\treadonly details: Record<string, never>;\n}\n\ninterface ProducedImageV1 {\n\treadonly bytes: Uint8Array;\n\treadonly format: ImageFormatV1;\n}\n\nfunction successResult(\n\tresult: ImageGenResultV1,\n\timages: readonly ProducedImageV1[],\n\tmodelSourceNote?: string,\n): ImageToolSuccessV1 | ImageGenResultV1 {\n\tif (result.status !== \"succeeded\") return result;\n\t// Zero inlining (user decision, 2026-09-30): image payloads never ride\n\t// the conversation — they would cost context on every later turn. The\n\t// tool result carries conclusions only (paths/model/usage); the model\n\t// reads a file on demand when it actually needs to look, which the host\n\t// read tool serves as an auto-resized image attachment.\n\tvoid images;\n\treturn {\n\t\tcontent: [\n\t\t\t{\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: JSON.stringify(\n\t\t\t\t\t{\n\t\t\t\t\t\tfiles: result.files,\n\t\t\t\t\t\tmodel: result.model,\n\t\t\t\t\t\t...(modelSourceNote === undefined ? {} : { modelSource: modelSourceNote }),\n\t\t\t\t\t\t...(result.usage === undefined ? {} : { usage: result.usage }),\n\t\t\t\t\t\tnote: \"images are saved to disk only; read a file to view it\",\n\t\t\t\t\t},\n\t\t\t\t\tnull,\n\t\t\t\t\t1,\n\t\t\t\t),\n\t\t\t},\n\t\t],\n\t\tdetails: {},\n\t};\n}\n\n/** Write one image; the exclusive-create retry makes concurrent invocations (parallel tool groups) safe. */\nasync function writeImage(\n\tworkspace: string,\n\tstamp: string,\n\tstartSequence: number,\n\tproduced: ProducedImageV1,\n): Promise<{ readonly relativePath: string; readonly sequence: number }> {\n\tconst directory = join(workspace, GENERATED_IMAGES_DIRECTORY);\n\tawait mkdir(directory, { recursive: true });\n\t// flag \"wx\" fails atomically when the name is taken (same-second stamp or\n\t// a concurrent tool group) instead of racing existsSync-then-write, which\n\t// silently overwrote one image with another under parallel execution.\n\tlet sequence = startSequence;\n\tfor (;;) {\n\t\tconst name = `img-${stamp}-${sequence}.${produced.format.extension}`;\n\t\ttry {\n\t\t\tawait writeFile(join(directory, name), produced.bytes, { flag: \"wx\" });\n\t\t\treturn { relativePath: `${GENERATED_IMAGES_DIRECTORY}/${name}`, sequence };\n\t\t} catch (error) {\n\t\t\tif ((error as NodeJS.ErrnoException).code === \"EEXIST\") {\n\t\t\t\tsequence += 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n}\n\n/** Persist every produced image file for a settled response. */\nasync function persistImages(\n\tworkspace: string,\n\tproduced: readonly ProducedImageV1[],\n): Promise<{ readonly files: string[]; readonly images: ProducedImageV1[] }> {\n\tconst stamp = localStampV1(new Date());\n\tconst files: string[] = [];\n\tconst images: ProducedImageV1[] = [];\n\tlet sequence = 1;\n\tfor (const image of produced) {\n\t\tconst written = await writeImage(workspace, stamp, sequence, image);\n\t\tfiles.push(written.relativePath);\n\t\timages.push(image);\n\t\tsequence = written.sequence + 1;\n\t}\n\treturn { files, images };\n}\n\n/** Inline b64 items pass through; url items are downloaded with the same fetch injection. */\nasync function collectImages(\n\tresponse: ImagesClientResponseV1,\n\tfetchImpl: typeof fetch,\n\tsignal: AbortSignal | undefined,\n): Promise<ProducedImageV1[] | ImageFailureV1> {\n\tconst images: ProducedImageV1[] = [];\n\tfor (const item of response.data) {\n\t\tif (item.b64 !== undefined) {\n\t\t\tconst bytes = Buffer.from(item.b64, \"base64\");\n\t\t\timages.push({ bytes, format: detectImageFormatV1(bytes) });\n\t\t\tcontinue;\n\t\t}\n\t\tif (item.url === undefined) continue;\n\t\t// The download carries its own 120s budget: a slow proxy route must\n\t\t// not hang the tool on undici's default 300s body timeout.\n\t\tconst downloadSignal =\n\t\t\tsignal === undefined\n\t\t\t\t? AbortSignal.timeout(IMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1)\n\t\t\t\t: AbortSignal.any([signal, AbortSignal.timeout(IMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1)]);\n\t\tlet download: Response;\n\t\ttry {\n\t\t\tdownload = await fetchImpl(item.url, { signal: downloadSignal });\n\t\t} catch (error) {\n\t\t\t// Network-level download failures must name the host and the proxy\n\t\t\t// fix: a bare \"fetch failed\" filed under the *request* code sent the\n\t\t\t// seat diagnosis down the wrong path for an hour (the generation\n\t\t\t// itself was succeeding; the CDN was unreachable without a proxy).\n\t\t\tlet host = \"the provider cdn\";\n\t\t\ttry {\n\t\t\t\thost = new URL(item.url).host;\n\t\t\t} catch {}\n\t\t\treturn {\n\t\t\t\tcode: \"download_failed\",\n\t\t\t\tmessage: `image download from ${host} failed (${error instanceof Error ? error.message : \"network error\"}); if that host is only reachable through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README`,\n\t\t\t};\n\t\t}\n\t\tif (!download.ok)\n\t\t\treturn {\n\t\t\t\tcode: \"download_failed\",\n\t\t\t\tmessage: `image download from the provider url failed with status ${download.status} (if this machine reaches the internet through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README)`,\n\t\t\t};\n\t\tconst bytes = Buffer.from(await download.arrayBuffer());\n\t\tif (bytes.byteLength > IMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1)\n\t\t\treturn { code: \"response_too_large\", message: \"downloaded image exceeds the 20 MiB response limit\" };\n\t\timages.push({ bytes, format: detectImageFormatV1(bytes) });\n\t}\n\treturn images;\n}\n\nfunction usageOf(response: ImagesClientResponseV1): { readonly usage?: Record<string, unknown> } {\n\treturn response.usage === undefined ? {} : { usage: response.usage };\n}\n\n/** Resolve provider config/credentials/workspace; a miss returns the failed result directly. */\nasync function resolveInvocation(\n\tapi: CapabilityAPI,\n\tproviderId: string,\n\tfetchImpl: typeof fetch,\n\tmodel: string,\n): Promise<\n\t| { readonly status: \"ok\"; readonly client: ReturnType<typeof createImagesClientV1>; readonly workspace: string }\n\t| { readonly status: \"failed\"; readonly result: ImageGenResultV1 }\n> {\n\tconst workspace = api.host.workspaceDirectory;\n\tif (typeof workspace !== \"string\" || workspace === \"\")\n\t\treturn {\n\t\t\tstatus: \"failed\",\n\t\t\tresult: failed(\n\t\t\t\tmodel,\n\t\t\t\t\"workspace_unavailable\",\n\t\t\t\t\"Image generation requires the host workspaceDirectory service to write generated-images/\",\n\t\t\t),\n\t\t};\n\tconst resolution = api.credentials === undefined ? undefined : await api.credentials.resolve({ providerId });\n\tif (resolution === undefined) {\n\t\treturn { status: \"failed\", result: credentialsFailure(model, providerId) };\n\t}\n\tif (\n\t\ttypeof resolution.apiKey !== \"string\" ||\n\t\tresolution.apiKey === \"\" ||\n\t\ttypeof resolution.baseUrl !== \"string\" ||\n\t\tresolution.baseUrl === \"\"\n\t) {\n\t\treturn { status: \"failed\", result: credentialsFailure(model, providerId) };\n\t}\n\treturn {\n\t\tstatus: \"ok\",\n\t\tworkspace,\n\t\tclient: createImagesClientV1({ baseUrl: resolution.baseUrl, apiKey: resolution.apiKey, fetchImpl }),\n\t};\n}\n\nfunction credentialsFailure(model: string, providerId: string): ImageGenResultV1 {\n\treturn failed(\n\t\tmodel,\n\t\t\"credentials_unavailable\",\n\t\t`Image generation credentials are unavailable for provider ${providerId}: the host must resolve apiKey and baseUrl for this provider (auth.json; store the key via /login)`,\n\t);\n}\n\n/**\n * Registers the full-control `image_generate` and `image_edit` tools. The\n * factory takes no credentials: provider auth (`apiKey` + `baseUrl`) is\n * resolved at call time through the host's public `CredentialAPI`. The\n * optional `fetchImpl` is a test seam only; production hosts use global fetch.\n */\nexport function createImageGenerationCapabilityFactoryV1(options?: {\n\treadonly fetchImpl?: typeof fetch;\n}): CapabilityPluginSyncFactory {\n\t// Every outbound request (generation and url download alike) rides the\n\t// same proxy-aware fetch: HTTPS_PROXY/HTTP_PROXY/NO_PROXY are honored\n\t// per request without touching the host's global dispatcher. The default\n\t// base is the undici package's fetch — the global fetch bundles another\n\t// undici version and rejects this package's dispatchers.\n\tconst fetchImpl: typeof fetch = wrapFetchWithEnvProxyV1(options?.fetchImpl);\n\treturn (api) => {\n\t\tif (api.host.mode === \"readonly\") return;\n\t\t// 宿主装配期注入(设计 §3.3):settings 槽位 + 生图模型目录快照经\n\t\t// configOverride 合并进 api.config。注入形状非法不在装载期崩溃——每次\n\t\t// 调用按 config_missing 可见失败(失败可见,不静默降级)。\n\t\tconst slotInjection = readInjectedSlot(api.config);\n\t\tconst catalogInjection = readInjectedCatalog(api.config);\n\t\tconst sessionProvider = (): string | undefined => api.session?.getModel?.()?.provider;\n\t\tconst resolveTarget = (input: Record<string, unknown>): ImageTargetResolution => {\n\t\t\tif (slotInjection.error !== undefined)\n\t\t\t\treturn { ok: false, message: `Invalid injected image-generation config: ${slotInjection.error}` };\n\t\t\tif (catalogInjection.error !== undefined)\n\t\t\t\treturn { ok: false, message: `Invalid injected image-generation config: ${catalogInjection.error}` };\n\t\t\treturn resolveImageTarget(input, slotInjection.slot, catalogInjection.catalog, sessionProvider());\n\t\t};\n\t\tapi.registerTool({\n\t\t\tname: \"image_generate\",\n\t\t\tlabel: \"Image generate\",\n\t\t\tdescription:\n\t\t\t\t\"Generate images from a prompt via the host-configured provider's OpenAI-compatible images API; saves image files (png/jpg/webp/gif as produced) under generated-images/ and returns their workspace-relative paths — images are never inlined into the conversation, read a returned file to view it\",\n\t\t\tparameters: IMAGE_GENERATE_PARAMETERS,\n\t\t\texecute: async (first: unknown, second?: unknown, third?: unknown) => {\n\t\t\t\tlet input: Record<string, unknown>;\n\t\t\t\ttry {\n\t\t\t\t\tinput = objectInput(first, second);\n\t\t\t\t} catch {\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"Image generation input must be an object\");\n\t\t\t\t}\n\t\t\t\tconst prompt = promptFrom(input.prompt);\n\t\t\t\tif (prompt === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"prompt must be a non-empty string of at most 4000 characters\");\n\t\t\t\tconst size = boundedString(input.size, 32);\n\t\t\t\tconst quality = boundedString(input.quality, 32);\n\t\t\t\tif (input.n !== undefined && countFrom(input.n) === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"n must be an integer between 1 and 4\");\n\t\t\t\tconst n = countFrom(input.n) ?? 1;\n\t\t\t\tconst sessionSignal = api.sessionAbortSignal?.();\n\t\t\t\tconst invocationSignal = signalFrom(third);\n\t\t\t\tconst clientSignal = combineSignals([invocationSignal, sessionSignal]);\n\t\t\t\tif (clientSignal?.aborted) return cancelled(\"\");\n\t\t\t\tconst target = resolveTarget(input);\n\t\t\t\tif (!target.ok) return failed(\"\", \"config_missing\", boundedImageMessageV1(target.message));\n\t\t\t\tconst { provider, model, modelSourceNote } = target;\n\t\t\t\ttry {\n\t\t\t\t\tconst resolved = await resolveInvocation(api, provider, fetchImpl, model);\n\t\t\t\t\tif (resolved.status === \"failed\") return resolved.result;\n\t\t\t\t\tconst response = await resolved.client.generateImages(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\tprompt,\n\t\t\t\t\t\t\tn,\n\t\t\t\t\t\t\t...(size === undefined ? {} : { size }),\n\t\t\t\t\t\t\t...(quality === undefined ? {} : { quality }),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tclientSignal,\n\t\t\t\t\t);\n\t\t\t\t\tconst collected = await collectImages(response, fetchImpl, clientSignal);\n\t\t\t\t\tif (!Array.isArray(collected)) return failed(model, collected.code, collected.message);\n\t\t\t\t\tconst persisted = await persistImages(resolved.workspace, collected);\n\t\t\t\t\treturn successResult(\n\t\t\t\t\t\t{ version: 1, status: \"succeeded\", files: persisted.files, model, ...usageOf(response) },\n\t\t\t\t\t\tpersisted.images,\n\t\t\t\t\t\tmodelSourceNote,\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\treturn failureFrom(error, clientSignal, model, \"Image generation request failed\");\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tapi.registerTool({\n\t\t\tname: \"image_edit\",\n\t\t\tlabel: \"Image edit\",\n\t\t\tdescription:\n\t\t\t\t\"Edit a previously generated image (workspace-relative path under generated-images/) with a revision prompt; returns the new file path\",\n\t\t\tparameters: IMAGE_EDIT_PARAMETERS,\n\t\t\texecute: async (first: unknown, second?: unknown, third?: unknown) => {\n\t\t\t\tlet input: Record<string, unknown>;\n\t\t\t\ttry {\n\t\t\t\t\tinput = objectInput(first, second);\n\t\t\t\t} catch {\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"Image generation input must be an object\");\n\t\t\t\t}\n\t\t\t\tconst prompt = promptFrom(input.prompt);\n\t\t\t\tif (prompt === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"prompt must be a non-empty string of at most 4000 characters\");\n\t\t\t\tconst imagePath = typeof input.imagePath === \"string\" ? input.imagePath : undefined;\n\t\t\t\tif (imagePath === undefined || !validImagePath(imagePath))\n\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\"\",\n\t\t\t\t\t\t\"invalid_path\",\n\t\t\t\t\t\t\"imagePath must be a workspace-relative identifier inside the generated-images/ directory (no .. segments)\",\n\t\t\t\t\t);\n\t\t\t\tconst sessionSignal = api.sessionAbortSignal?.();\n\t\t\t\tconst invocationSignal = signalFrom(third);\n\t\t\t\tconst clientSignal = combineSignals([invocationSignal, sessionSignal]);\n\t\t\t\tif (clientSignal?.aborted) return cancelled(\"\");\n\t\t\t\tconst target = resolveTarget(input);\n\t\t\t\tif (!target.ok) return failed(\"\", \"config_missing\", boundedImageMessageV1(target.message));\n\t\t\t\tconst { provider, model, modelSourceNote } = target;\n\t\t\t\ttry {\n\t\t\t\t\tconst resolved = await resolveInvocation(api, provider, fetchImpl, model);\n\t\t\t\t\tif (resolved.status === \"failed\") return resolved.result;\n\t\t\t\t\t// Defense in depth (readonly never registers these tools): the\n\t\t\t\t\t// lexical validImagePath gate cannot see symlinks, so refuse any\n\t\t\t\t\t// candidate whose real path escapes the workspace before a byte\n\t\t\t\t\t// is read and shipped to the provider.\n\t\t\t\t\tconst sandbox = await sandboxedImagePath(resolved.workspace, imagePath);\n\t\t\t\t\tif (sandbox === \"missing\")\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tif (sandbox === \"escape\")\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"invalid_path\",\n\t\t\t\t\t\t\t`imagePath ${imagePath} resolves outside the workspace; only files under the workspace generated-images/ directory can be read`,\n\t\t\t\t\t\t);\n\t\t\t\t\t// Size-check before reading: users can drop their own files\n\t\t\t\t\t// into generated-images/, and the 20 MiB download cap must\n\t\t\t\t\t// not be bypassed by a renamed local file.\n\t\t\t\t\tconst sourceStats = await stat(join(resolved.workspace, imagePath)).catch(() => undefined);\n\t\t\t\t\tif (sourceStats === undefined || !sourceStats.isFile())\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tif (sourceStats.size > IMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1)\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"response_too_large\",\n\t\t\t\t\t\t\t`image ${imagePath} is ${sourceStats.size} bytes, above the 20 MiB limit`,\n\t\t\t\t\t\t);\n\t\t\t\t\tconst source = await readFile(join(resolved.workspace, imagePath)).catch(() => undefined);\n\t\t\t\t\tif (source === undefined)\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tconst response = await resolved.client.editImage(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\timage: source,\n\t\t\t\t\t\t\t// The edits endpoint accepts png/jpeg/webp inputs; the\n\t\t\t\t\t\t\t// multipart blob must carry the source's real format\n\t\t\t\t\t\t\t// (a prior url-form download may be JPEG).\n\t\t\t\t\t\t\tmimeType: detectImageFormatV1(source).mimeType,\n\t\t\t\t\t\t\tprompt,\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tclientSignal,\n\t\t\t\t\t);\n\t\t\t\t\tconst collected = await collectImages(response, fetchImpl, clientSignal);\n\t\t\t\t\tif (!Array.isArray(collected)) return failed(model, collected.code, collected.message);\n\t\t\t\t\tconst persisted = await persistImages(resolved.workspace, collected);\n\t\t\t\t\treturn successResult(\n\t\t\t\t\t\t{ version: 1, status: \"succeeded\", files: persisted.files, model, ...usageOf(response) },\n\t\t\t\t\t\tpersisted.images,\n\t\t\t\t\t\tmodelSourceNote,\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\treturn failureFrom(error, clientSignal, model, \"Image edit request failed\");\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t};\n}\n\nfunction failureFrom(\n\terror: unknown,\n\tsignal: AbortSignal | undefined,\n\tmodel: string,\n\tfallbackMessage: string,\n): ImageGenResultV1 {\n\tif (signal?.aborted) return cancelled(model);\n\tlet message = error instanceof Error ? error.message : fallbackMessage;\n\t// Append (never replace) a proxy hint for opaque network failures: the\n\t// single most common cause on machines that route out through a VPN.\n\tif (/fetch failed|terminated|ENOTFOUND|ECONNREFUSED|ETIMEDOUT|EAI_AGAIN|UND_ERR/u.test(message))\n\t\tmessage = `${message} (if this machine reaches the internet through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README)`;\n\treturn failed(model, \"image_request_failed\", boundedImageMessageV1(message));\n}\n"]}
1
+ {"version":3,"file":"capability.d.ts","sourceRoot":"","sources":["../src/capability.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAiB,2BAA2B,EAAgB,MAAM,yBAAyB,CAAC;AA+dxG;;;;;GAKG;AACH,wBAAgB,wCAAwC,CAAC,OAAO,CAAC,EAAE;IAClE,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;CAClC,GAAG,2BAA2B,CA+K9B","sourcesContent":["import { mkdir, readFile, realpath, stat, writeFile } from \"node:fs/promises\";\nimport { join, sep } from \"node:path\";\nimport type { CapabilityAPI, CapabilityPluginSyncFactory, PluginConfig } from \"@agent-forge/plugin-sdk\";\nimport { Type } from \"typebox\";\nimport { wrapFetchWithEnvProxyV1 } from \"./env-proxy-fetch.ts\";\nimport { createImagesClientV1 } from \"./images-client.ts\";\nimport {\n\tboundedImageMessageV1,\n\tdetectImageFormatV1,\n\tIMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1,\n\tIMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1,\n\ttype ImageFormatV1,\n\ttype ImageGenResultV1,\n\ttype ImagesClientResponseV1,\n} from \"./types.ts\";\n\nconst IMAGE_GENERATE_PARAMETERS = Type.Object(\n\t{\n\t\tprompt: Type.String({ minLength: 1, maxLength: 4000 }),\n\t\tsize: Type.Optional(Type.String({ minLength: 1, maxLength: 32 })),\n\t\tquality: Type.Optional(Type.String({ minLength: 1, maxLength: 32 })),\n\t\tn: Type.Optional(Type.Integer({ minimum: 1, maximum: 4 })),\n\t\tprovider: Type.Optional(Type.String({ minLength: 1, maxLength: 64 })),\n\t\tmodel: Type.Optional(Type.String({ minLength: 1, maxLength: 128 })),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst IMAGE_EDIT_PARAMETERS = Type.Object(\n\t{\n\t\timagePath: Type.String({ minLength: 1, maxLength: 512, pattern: \"^[A-Za-z0-9_][A-Za-z0-9._/-]*$\" }),\n\t\tprompt: Type.String({ minLength: 1, maxLength: 4000 }),\n\t\tprovider: Type.Optional(Type.String({ minLength: 1, maxLength: 64 })),\n\t\tmodel: Type.Optional(Type.String({ minLength: 1, maxLength: 128 })),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst IMAGE_PATH_PATTERN_V1 = /^[A-Za-z0-9_][A-Za-z0-9._/-]*$/u;\n\nconst GENERATED_IMAGES_DIRECTORY = \"generated-images\";\n\ninterface ImageFailureV1 {\n\treadonly code: string;\n\treadonly message: string;\n}\n\nfunction objectInput(first: unknown, second: unknown): Record<string, unknown> {\n\t// Shape-based dual-convention read (aligned with subagent-delegate): the\n\t// runtime invoke face passes the input object first and the host\n\t// CapabilityToolContext second; the agent tool face passes the tool call\n\t// id (a string) first and the params object second.\n\tconst value = first !== null && typeof first === \"object\" && !Array.isArray(first) ? first : second;\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new Error(\"Image generation input must be an object\");\n\treturn value as Record<string, unknown>;\n}\n\nfunction signalFrom(value: unknown): AbortSignal | undefined {\n\treturn value instanceof AbortSignal ? value : undefined;\n}\n\nfunction combineSignals(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined {\n\tconst present = signals.filter((signal): signal is AbortSignal => signal !== undefined);\n\tif (present.length === 0) return undefined;\n\tif (present.length === 1) return present[0];\n\treturn AbortSignal.any(present);\n}\n\nfunction failed(model: string, code: string, message: string): ImageGenResultV1 {\n\treturn {\n\t\tversion: 1,\n\t\tstatus: \"failed\",\n\t\tfiles: [],\n\t\tmodel,\n\t\terror: { code, message: boundedImageMessageV1(message) },\n\t};\n}\n\nfunction cancelled(model: string): ImageGenResultV1 {\n\treturn {\n\t\tversion: 1,\n\t\tstatus: \"cancelled\",\n\t\tfiles: [],\n\t\tmodel,\n\t\terror: { code: \"cancelled\", message: \"Image generation was cancelled\" },\n\t};\n}\n\n/** `param` (non-empty string), a non-empty env value, or undefined. */\nfunction nonEmptyEnvValue(name: string): string | undefined {\n\tconst value = process.env[name];\n\treturn typeof value === \"string\" && value.trim() !== \"\" ? value : undefined;\n}\n\nfunction boundedString(value: unknown, maxLength: number): string | undefined {\n\treturn typeof value === \"string\" && value !== \"\" && value.length <= maxLength ? value : undefined;\n}\n\n/** settings 槽位形状(宿主经 configOverride 注入,设计 §3.1/§3.3)。 */\ninterface ImageGenerationSlotV1 {\n\treadonly provider: string;\n\treadonly model?: string;\n}\n\n/** 目录快照条目:宿主装配期从 models.json 登记项投影(设计 §3.2 推断供料)。 */\ninterface CatalogModelV1 {\n\treadonly id: string;\n\treadonly name?: string;\n}\n\ntype ImageModelCatalogV1 = Readonly<Record<string, ReadonlyArray<CatalogModelV1>>>;\n\nfunction readInjectedSlot(config: PluginConfig): { readonly slot?: ImageGenerationSlotV1; readonly error?: string } {\n\tconst raw = config.imageGeneration;\n\tif (raw === undefined) return {};\n\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw))\n\t\treturn { error: \"injected config imageGeneration must be an object\" };\n\tconst record = raw as Record<string, unknown>;\n\tif (typeof record.provider !== \"string\" || record.provider.trim() === \"\")\n\t\treturn {\n\t\t\terror: `injected config imageGeneration.provider must be a non-empty string (got ${typeof record.provider})`,\n\t\t};\n\tif (record.model !== undefined && (typeof record.model !== \"string\" || record.model.trim() === \"\"))\n\t\treturn { error: \"injected config imageGeneration.model must be a non-empty string when present\" };\n\treturn { slot: { provider: record.provider, ...(record.model === undefined ? {} : { model: record.model }) } };\n}\n\nfunction readInjectedCatalog(config: PluginConfig): { readonly catalog: ImageModelCatalogV1; readonly error?: string } {\n\tconst raw = config.imageModelCatalog;\n\tif (raw === undefined) return { catalog: {} };\n\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw))\n\t\treturn {\n\t\t\tcatalog: {},\n\t\t\terror: \"injected config imageModelCatalog must be an object mapping providers to image models\",\n\t\t};\n\tconst catalog: Record<string, Array<{ id: string; name?: string }>> = {};\n\tfor (const [providerId, entries] of Object.entries(raw as Record<string, unknown>)) {\n\t\tif (!Array.isArray(entries))\n\t\t\treturn { catalog: {}, error: `injected config imageModelCatalog[\"${providerId}\"] must be an array` };\n\t\tconst models: Array<{ id: string; name?: string }> = [];\n\t\tfor (const entry of entries) {\n\t\t\tif (typeof entry !== \"object\" || entry === null)\n\t\t\t\treturn { catalog: {}, error: `injected config imageModelCatalog[\"${providerId}\"] entries must be objects` };\n\t\t\tconst record = entry as Record<string, unknown>;\n\t\t\tif (typeof record.id !== \"string\" || record.id === \"\")\n\t\t\t\treturn {\n\t\t\t\t\tcatalog: {},\n\t\t\t\t\terror: `injected config imageModelCatalog[\"${providerId}\"] entries need a non-empty string id`,\n\t\t\t\t};\n\t\t\tmodels.push({ id: record.id, ...(typeof record.name === \"string\" ? { name: record.name } : {}) });\n\t\t}\n\t\tif (models.length > 0) catalog[providerId] = models;\n\t}\n\treturn { catalog };\n}\n\n/** 确定性排序键:name 优先(缺省回退 id),同键按 id;码点序,与宿主快照顺序无关。 */\nfunction compareCatalogModels(left: CatalogModelV1, right: CatalogModelV1): number {\n\tconst leftKey = left.name ?? left.id;\n\tconst rightKey = right.name ?? right.id;\n\tif (leftKey !== rightKey) return leftKey < rightKey ? -1 : 1;\n\treturn left.id < right.id ? -1 : left.id > right.id ? 1 : 0;\n}\n\ntype ImageTargetResolution =\n\t| { readonly ok: true; readonly provider: string; readonly model: string; readonly modelSourceNote?: string }\n\t| { readonly ok: false; readonly message: string };\n\nconst IMAGE_GENERATION_CONFIG_EXAMPLE =\n\t'Example settings.json: { \"capabilities\": { \"imageGeneration\": { \"provider\": \"myimg\", \"model\": \"gpt-image-1\" } } }';\n\n/**\n * 六层取值链(供应商能力槽位设计 §3.2):\n * 1. 工具参数 provider/model;\n * 2. settings 槽位(宿主注入的 config.imageGeneration,model 可省);\n * 3. env AGENT_FORGE_IMAGEGEN_PROVIDER/MODEL(存量兼容);\n * 4. 主供应商推断——会话当前 provider 在目录快照中登记过生图模型时取用;\n * 5. 唯一候选推断——全配置域恰好一个 provider 登记过生图模型(且非当前主)时取用;\n * 6. fail-closed——config_missing,多候选时列出候选清单。\n * model 在 1-3 层均未命中时按已解析 provider 的登记生图模型推断(唯一命中直接\n * 用;多命中确定性排序取第一个并注明来源)。\n */\nfunction resolveImageTarget(\n\tinput: Record<string, unknown>,\n\tslot: ImageGenerationSlotV1 | undefined,\n\tcatalog: ImageModelCatalogV1,\n\tsessionProvider: string | undefined,\n): ImageTargetResolution {\n\tconst paramProvider = boundedString(input.provider, 64);\n\tconst paramModel = boundedString(input.model, 128);\n\tconst envProvider = nonEmptyEnvValue(\"AGENT_FORGE_IMAGEGEN_PROVIDER\");\n\tconst envModel = nonEmptyEnvValue(\"AGENT_FORGE_IMAGEGEN_MODEL\");\n\n\tlet provider = paramProvider ?? slot?.provider ?? envProvider;\n\tif (provider === undefined) {\n\t\tif (sessionProvider !== undefined && (catalog[sessionProvider]?.length ?? 0) > 0) provider = sessionProvider;\n\t\telse {\n\t\t\tconst candidates = Object.keys(catalog)\n\t\t\t\t.filter((providerId) => catalog[providerId].length > 0)\n\t\t\t\t.sort();\n\t\t\tif (candidates.length === 1) provider = candidates[0];\n\t\t\telse if (candidates.length > 1)\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t`Multiple image-generation providers are registered in models.json (${candidates.join(\", \")}); image generation fails closed instead of guessing. ` +\n\t\t\t\t\t\t`Configure capabilities.imageGeneration in settings.json or pass the provider parameter. ${IMAGE_GENERATION_CONFIG_EXAMPLE}`,\n\t\t\t\t};\n\t\t\telse\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Image generation is not configured: pass provider and model via the tool parameters, set capabilities.imageGeneration in settings.json, \" +\n\t\t\t\t\t\t'set AGENT_FORGE_IMAGEGEN_PROVIDER and AGENT_FORGE_IMAGEGEN_MODEL, or register an image model (\"output\": [\"image\"]) for a provider in models.json. ' +\n\t\t\t\t\t\tIMAGE_GENERATION_CONFIG_EXAMPLE,\n\t\t\t\t};\n\t\t}\n\t}\n\n\tlet model = paramModel ?? slot?.model ?? envModel;\n\tlet modelSourceNote: string | undefined;\n\tif (model === undefined) {\n\t\tconst registered = catalog[provider] ?? [];\n\t\tif (registered.length === 1) model = registered[0].id;\n\t\telse if (registered.length > 1) {\n\t\t\tconst first = [...registered].sort(compareCatalogModels)[0];\n\t\t\tmodel = first.id;\n\t\t\tmodelSourceNote =\n\t\t\t\t`model ${first.id} was picked deterministically (first by name) from ${registered.length} image models registered for provider ${provider} in models.json; ` +\n\t\t\t\t\"pass an explicit model to choose another\";\n\t\t} else\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\tmessage:\n\t\t\t\t\t`No image model is configured for provider ${provider}: no model came from the tool parameters, settings, or env, ` +\n\t\t\t\t\t`and provider ${provider} has no models.json model with \"output\": [\"image\"]. Register one or pass an explicit model. ${IMAGE_GENERATION_CONFIG_EXAMPLE}`,\n\t\t\t};\n\t}\n\treturn { ok: true, provider, model, ...(modelSourceNote === undefined ? {} : { modelSourceNote }) };\n}\n\nfunction promptFrom(value: unknown): string | undefined {\n\treturn boundedString(value, 4000);\n}\n\nfunction countFrom(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isSafeInteger(value) && value >= 1 && value <= 4 ? value : undefined;\n}\n\n/** Workspace-relative identifier confined to the generated-images directory. */\nfunction validImagePath(imagePath: string): boolean {\n\tif (!IMAGE_PATH_PATTERN_V1.test(imagePath)) return false;\n\tif (!imagePath.startsWith(`${GENERATED_IMAGES_DIRECTORY}/`)) return false;\n\treturn !imagePath.split(\"/\").includes(\"..\");\n}\n\n/**\n * image_edit ships workspace bytes to an external image provider, so the\n * lexical validImagePath gate is not enough: a symlink inside\n * generated-images/ can point anywhere on disk. Same sandbox model as the\n * workspace-files-write host: resolve the workspace root and the candidate to\n * their real paths and require the candidate to stay inside the real root.\n * A missing file (or dangling link) fails realpath and maps to the regular\n * not-found outcome; a link resolving outside the workspace is a boundary\n * escape and is refused. Links that stay inside the workspace keep working —\n * their bytes never leave the sandbox boundary.\n */\nasync function sandboxedImagePath(workspace: string, imagePath: string): Promise<\"missing\" | \"escape\" | \"ok\"> {\n\tconst root = await realpath(workspace).catch(() => undefined);\n\tif (root === undefined) return \"missing\";\n\tconst real = await realpath(join(workspace, imagePath)).catch(() => undefined);\n\tif (real === undefined) return \"missing\";\n\tif (real !== root && !real.startsWith(root + sep)) return \"escape\";\n\treturn \"ok\";\n}\n\n/**\n * Local-time `yyyymmdd-HHmmss` stamp shared by every file of one invocation.\n * Local (not UTC): users find today's images by the date in the filename, and\n * on UTC+8 machines a UTC stamp labels evening images as \"tomorrow\". Sorting\n * stays monotonic wherever local time has no DST jumps (e.g. China).\n */\nfunction localStampV1(date: Date): string {\n\tconst pad = (value: number): string => String(value).padStart(2, \"0\");\n\treturn `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}-${pad(\n\t\tdate.getHours(),\n\t)}${pad(date.getMinutes())}${pad(date.getSeconds())}`;\n}\n\ninterface ImageToolSuccessV1 {\n\treadonly content: readonly { readonly type: \"text\"; readonly text: string }[];\n\treadonly details: Record<string, never>;\n}\n\ninterface ProducedImageV1 {\n\treadonly bytes: Uint8Array;\n\treadonly format: ImageFormatV1;\n}\n\nfunction successResult(\n\tresult: ImageGenResultV1,\n\timages: readonly ProducedImageV1[],\n\tmodelSourceNote?: string,\n): ImageToolSuccessV1 | ImageGenResultV1 {\n\tif (result.status !== \"succeeded\") return result;\n\t// Zero inlining (user decision, 2026-09-30): image payloads never ride\n\t// the conversation — they would cost context on every later turn. The\n\t// tool result carries conclusions only (paths/model/usage); the model\n\t// reads a file on demand when it actually needs to look, which the host\n\t// read tool serves as an auto-resized image attachment.\n\tvoid images;\n\treturn {\n\t\tcontent: [\n\t\t\t{\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: JSON.stringify(\n\t\t\t\t\t{\n\t\t\t\t\t\tfiles: result.files,\n\t\t\t\t\t\tmodel: result.model,\n\t\t\t\t\t\t...(modelSourceNote === undefined ? {} : { modelSource: modelSourceNote }),\n\t\t\t\t\t\t...(result.usage === undefined ? {} : { usage: result.usage }),\n\t\t\t\t\t\tnote: \"images are saved to disk only; read a file to view it\",\n\t\t\t\t\t},\n\t\t\t\t\tnull,\n\t\t\t\t\t1,\n\t\t\t\t),\n\t\t\t},\n\t\t],\n\t\tdetails: {},\n\t};\n}\n\n/** Write one image; the exclusive-create retry makes concurrent invocations (parallel tool groups) safe. */\nasync function writeImage(\n\tworkspace: string,\n\tstamp: string,\n\tstartSequence: number,\n\tproduced: ProducedImageV1,\n): Promise<{ readonly relativePath: string; readonly sequence: number }> {\n\tconst directory = join(workspace, GENERATED_IMAGES_DIRECTORY);\n\tawait mkdir(directory, { recursive: true });\n\t// flag \"wx\" fails atomically when the name is taken (same-second stamp or\n\t// a concurrent tool group) instead of racing existsSync-then-write, which\n\t// silently overwrote one image with another under parallel execution.\n\tlet sequence = startSequence;\n\tfor (;;) {\n\t\tconst name = `img-${stamp}-${sequence}.${produced.format.extension}`;\n\t\ttry {\n\t\t\tawait writeFile(join(directory, name), produced.bytes, { flag: \"wx\" });\n\t\t\treturn { relativePath: `${GENERATED_IMAGES_DIRECTORY}/${name}`, sequence };\n\t\t} catch (error) {\n\t\t\tif ((error as NodeJS.ErrnoException).code === \"EEXIST\") {\n\t\t\t\tsequence += 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n}\n\n/** Persist every produced image file for a settled response. */\nasync function persistImages(\n\tworkspace: string,\n\tproduced: readonly ProducedImageV1[],\n): Promise<{ readonly files: string[]; readonly images: ProducedImageV1[] }> {\n\tconst stamp = localStampV1(new Date());\n\tconst files: string[] = [];\n\tconst images: ProducedImageV1[] = [];\n\tlet sequence = 1;\n\tfor (const image of produced) {\n\t\tconst written = await writeImage(workspace, stamp, sequence, image);\n\t\tfiles.push(written.relativePath);\n\t\timages.push(image);\n\t\tsequence = written.sequence + 1;\n\t}\n\treturn { files, images };\n}\n\n/** Inline b64 items pass through; url items are downloaded with the same fetch injection. */\nasync function collectImages(\n\tresponse: ImagesClientResponseV1,\n\tfetchImpl: typeof fetch,\n\tsignal: AbortSignal | undefined,\n): Promise<ProducedImageV1[] | ImageFailureV1> {\n\tconst images: ProducedImageV1[] = [];\n\tfor (const item of response.data) {\n\t\tif (item.b64 !== undefined) {\n\t\t\tconst bytes = Buffer.from(item.b64, \"base64\");\n\t\t\timages.push({ bytes, format: detectImageFormatV1(bytes) });\n\t\t\tcontinue;\n\t\t}\n\t\tif (item.url === undefined) continue;\n\t\t// The download carries its own 120s budget: a slow proxy route must\n\t\t// not hang the tool on undici's default 300s body timeout.\n\t\tconst downloadSignal =\n\t\t\tsignal === undefined\n\t\t\t\t? AbortSignal.timeout(IMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1)\n\t\t\t\t: AbortSignal.any([signal, AbortSignal.timeout(IMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1)]);\n\t\tlet download: Response;\n\t\ttry {\n\t\t\tdownload = await fetchImpl(item.url, { signal: downloadSignal });\n\t\t} catch (error) {\n\t\t\tif (signal?.aborted) throw error;\n\t\t\t// Network-level download failures must name the host and the proxy\n\t\t\t// fix: a bare \"fetch failed\" filed under the *request* code sent the\n\t\t\t// seat diagnosis down the wrong path for an hour (the generation\n\t\t\t// itself was succeeding; the CDN was unreachable without a proxy).\n\t\t\tlet host = \"the provider cdn\";\n\t\t\ttry {\n\t\t\t\thost = new URL(item.url).host;\n\t\t\t} catch {}\n\t\t\treturn {\n\t\t\t\tcode: \"download_failed\",\n\t\t\t\tmessage: `image download from ${host} failed (${error instanceof Error ? error.message : \"network error\"}); if that host is only reachable through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README`,\n\t\t\t};\n\t\t}\n\t\tif (!download.ok)\n\t\t\treturn {\n\t\t\t\tcode: \"download_failed\",\n\t\t\t\tmessage: `image download from the provider url failed with status ${download.status} (if this machine reaches the internet through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README)`,\n\t\t\t};\n\t\tconst bytes = Buffer.from(await download.arrayBuffer());\n\t\tif (bytes.byteLength > IMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1)\n\t\t\treturn { code: \"response_too_large\", message: \"downloaded image exceeds the 20 MiB response limit\" };\n\t\timages.push({ bytes, format: detectImageFormatV1(bytes) });\n\t}\n\treturn images;\n}\n\nfunction usageOf(response: ImagesClientResponseV1): { readonly usage?: Record<string, unknown> } {\n\treturn response.usage === undefined ? {} : { usage: response.usage };\n}\n\n/** Resolve provider config/credentials/workspace; a miss returns the failed result directly. */\nasync function resolveInvocation(\n\tapi: CapabilityAPI,\n\tproviderId: string,\n\tfetchImpl: typeof fetch,\n\tmodel: string,\n): Promise<\n\t| { readonly status: \"ok\"; readonly client: ReturnType<typeof createImagesClientV1>; readonly workspace: string }\n\t| { readonly status: \"failed\"; readonly result: ImageGenResultV1 }\n> {\n\tconst workspace = api.host.workspaceDirectory;\n\tif (typeof workspace !== \"string\" || workspace === \"\")\n\t\treturn {\n\t\t\tstatus: \"failed\",\n\t\t\tresult: failed(\n\t\t\t\tmodel,\n\t\t\t\t\"workspace_unavailable\",\n\t\t\t\t\"Image generation requires the host workspaceDirectory service to write generated-images/\",\n\t\t\t),\n\t\t};\n\tconst resolution = api.credentials === undefined ? undefined : await api.credentials.resolve({ providerId });\n\tif (resolution === undefined) {\n\t\treturn { status: \"failed\", result: credentialsFailure(model, providerId) };\n\t}\n\tif (\n\t\ttypeof resolution.apiKey !== \"string\" ||\n\t\tresolution.apiKey === \"\" ||\n\t\ttypeof resolution.baseUrl !== \"string\" ||\n\t\tresolution.baseUrl === \"\"\n\t) {\n\t\treturn { status: \"failed\", result: credentialsFailure(model, providerId) };\n\t}\n\treturn {\n\t\tstatus: \"ok\",\n\t\tworkspace,\n\t\tclient: createImagesClientV1({ baseUrl: resolution.baseUrl, apiKey: resolution.apiKey, fetchImpl }),\n\t};\n}\n\nfunction credentialsFailure(model: string, providerId: string): ImageGenResultV1 {\n\treturn failed(\n\t\tmodel,\n\t\t\"credentials_unavailable\",\n\t\t`Image generation credentials are unavailable for provider ${providerId}: the host must resolve apiKey and baseUrl for this provider (auth.json; store the key via /login)`,\n\t);\n}\n\n/**\n * Registers the full-control `image_generate` and `image_edit` tools. The\n * factory takes no credentials: provider auth (`apiKey` + `baseUrl`) is\n * resolved at call time through the host's public `CredentialAPI`. The\n * optional `fetchImpl` is a test seam only; production hosts use global fetch.\n */\nexport function createImageGenerationCapabilityFactoryV1(options?: {\n\treadonly fetchImpl?: typeof fetch;\n}): CapabilityPluginSyncFactory {\n\t// Every outbound request (generation and url download alike) rides the\n\t// same proxy-aware fetch: HTTPS_PROXY/HTTP_PROXY/NO_PROXY are honored\n\t// per request without touching the host's global dispatcher. The default\n\t// base is the undici package's fetch — the global fetch bundles another\n\t// undici version and rejects this package's dispatchers.\n\tconst fetchImpl: typeof fetch = wrapFetchWithEnvProxyV1(options?.fetchImpl);\n\treturn (api) => {\n\t\tif (api.host.mode === \"readonly\") return;\n\t\t// 宿主装配期注入(设计 §3.3):settings 槽位 + 生图模型目录快照经\n\t\t// configOverride 合并进 api.config。注入形状非法不在装载期崩溃——每次\n\t\t// 调用按 config_missing 可见失败(失败可见,不静默降级)。\n\t\tconst slotInjection = readInjectedSlot(api.config);\n\t\tconst catalogInjection = readInjectedCatalog(api.config);\n\t\tconst sessionProvider = (): string | undefined => api.session?.getModel?.()?.provider;\n\t\tconst resolveTarget = (input: Record<string, unknown>): ImageTargetResolution => {\n\t\t\tif (slotInjection.error !== undefined)\n\t\t\t\treturn { ok: false, message: `Invalid injected image-generation config: ${slotInjection.error}` };\n\t\t\tif (catalogInjection.error !== undefined)\n\t\t\t\treturn { ok: false, message: `Invalid injected image-generation config: ${catalogInjection.error}` };\n\t\t\treturn resolveImageTarget(input, slotInjection.slot, catalogInjection.catalog, sessionProvider());\n\t\t};\n\t\tapi.registerTool({\n\t\t\tname: \"image_generate\",\n\t\t\tlabel: \"Image generate\",\n\t\t\tdescription:\n\t\t\t\t\"Generate images from a prompt via the host-configured provider's OpenAI-compatible images API; saves image files (png/jpg/webp/gif as produced) under generated-images/ and returns their workspace-relative paths — images are never inlined into the conversation, read a returned file to view it\",\n\t\t\tparameters: IMAGE_GENERATE_PARAMETERS,\n\t\t\texecute: async (first: unknown, second?: unknown, third?: unknown) => {\n\t\t\t\tlet input: Record<string, unknown>;\n\t\t\t\ttry {\n\t\t\t\t\tinput = objectInput(first, second);\n\t\t\t\t} catch {\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"Image generation input must be an object\");\n\t\t\t\t}\n\t\t\t\tconst prompt = promptFrom(input.prompt);\n\t\t\t\tif (prompt === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"prompt must be a non-empty string of at most 4000 characters\");\n\t\t\t\tconst size = boundedString(input.size, 32);\n\t\t\t\tconst quality = boundedString(input.quality, 32);\n\t\t\t\tif (input.n !== undefined && countFrom(input.n) === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"n must be an integer between 1 and 4\");\n\t\t\t\tconst n = countFrom(input.n) ?? 1;\n\t\t\t\tconst sessionSignal = api.sessionAbortSignal?.();\n\t\t\t\tconst invocationSignal = signalFrom(third);\n\t\t\t\tconst clientSignal = combineSignals([invocationSignal, sessionSignal]);\n\t\t\t\tif (clientSignal?.aborted) return cancelled(\"\");\n\t\t\t\tconst target = resolveTarget(input);\n\t\t\t\tif (!target.ok) return failed(\"\", \"config_missing\", boundedImageMessageV1(target.message));\n\t\t\t\tconst { provider, model, modelSourceNote } = target;\n\t\t\t\ttry {\n\t\t\t\t\tconst resolved = await resolveInvocation(api, provider, fetchImpl, model);\n\t\t\t\t\tif (resolved.status === \"failed\") return resolved.result;\n\t\t\t\t\tconst response = await resolved.client.generateImages(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\tprompt,\n\t\t\t\t\t\t\tn,\n\t\t\t\t\t\t\t...(size === undefined ? {} : { size }),\n\t\t\t\t\t\t\t...(quality === undefined ? {} : { quality }),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tclientSignal,\n\t\t\t\t\t);\n\t\t\t\t\tconst collected = await collectImages(response, fetchImpl, clientSignal);\n\t\t\t\t\tif (!Array.isArray(collected)) return failed(model, collected.code, collected.message);\n\t\t\t\t\tconst persisted = await persistImages(resolved.workspace, collected);\n\t\t\t\t\treturn successResult(\n\t\t\t\t\t\t{ version: 1, status: \"succeeded\", files: persisted.files, model, ...usageOf(response) },\n\t\t\t\t\t\tpersisted.images,\n\t\t\t\t\t\tmodelSourceNote,\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\treturn failureFrom(error, clientSignal, model, \"Image generation request failed\");\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tapi.registerTool({\n\t\t\tname: \"image_edit\",\n\t\t\tlabel: \"Image edit\",\n\t\t\tdescription:\n\t\t\t\t\"Edit a previously generated image (workspace-relative path under generated-images/) with a revision prompt; returns the new file path\",\n\t\t\tparameters: IMAGE_EDIT_PARAMETERS,\n\t\t\texecute: async (first: unknown, second?: unknown, third?: unknown) => {\n\t\t\t\tlet input: Record<string, unknown>;\n\t\t\t\ttry {\n\t\t\t\t\tinput = objectInput(first, second);\n\t\t\t\t} catch {\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"Image generation input must be an object\");\n\t\t\t\t}\n\t\t\t\tconst prompt = promptFrom(input.prompt);\n\t\t\t\tif (prompt === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"prompt must be a non-empty string of at most 4000 characters\");\n\t\t\t\tconst imagePath = typeof input.imagePath === \"string\" ? input.imagePath : undefined;\n\t\t\t\tif (imagePath === undefined || !validImagePath(imagePath))\n\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\"\",\n\t\t\t\t\t\t\"invalid_path\",\n\t\t\t\t\t\t\"imagePath must be a workspace-relative identifier inside the generated-images/ directory (no .. segments)\",\n\t\t\t\t\t);\n\t\t\t\tconst sessionSignal = api.sessionAbortSignal?.();\n\t\t\t\tconst invocationSignal = signalFrom(third);\n\t\t\t\tconst clientSignal = combineSignals([invocationSignal, sessionSignal]);\n\t\t\t\tif (clientSignal?.aborted) return cancelled(\"\");\n\t\t\t\tconst target = resolveTarget(input);\n\t\t\t\tif (!target.ok) return failed(\"\", \"config_missing\", boundedImageMessageV1(target.message));\n\t\t\t\tconst { provider, model, modelSourceNote } = target;\n\t\t\t\ttry {\n\t\t\t\t\tconst resolved = await resolveInvocation(api, provider, fetchImpl, model);\n\t\t\t\t\tif (resolved.status === \"failed\") return resolved.result;\n\t\t\t\t\t// Defense in depth (readonly never registers these tools): the\n\t\t\t\t\t// lexical validImagePath gate cannot see symlinks, so refuse any\n\t\t\t\t\t// candidate whose real path escapes the workspace before a byte\n\t\t\t\t\t// is read and shipped to the provider.\n\t\t\t\t\tconst sandbox = await sandboxedImagePath(resolved.workspace, imagePath);\n\t\t\t\t\tif (sandbox === \"missing\")\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tif (sandbox === \"escape\")\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"invalid_path\",\n\t\t\t\t\t\t\t`imagePath ${imagePath} resolves outside the workspace; only files under the workspace generated-images/ directory can be read`,\n\t\t\t\t\t\t);\n\t\t\t\t\t// Size-check before reading: users can drop their own files\n\t\t\t\t\t// into generated-images/, and the 20 MiB download cap must\n\t\t\t\t\t// not be bypassed by a renamed local file.\n\t\t\t\t\tconst sourceStats = await stat(join(resolved.workspace, imagePath)).catch(() => undefined);\n\t\t\t\t\tif (sourceStats === undefined || !sourceStats.isFile())\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tif (sourceStats.size > IMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1)\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"response_too_large\",\n\t\t\t\t\t\t\t`image ${imagePath} is ${sourceStats.size} bytes, above the 20 MiB limit`,\n\t\t\t\t\t\t);\n\t\t\t\t\tconst source = await readFile(join(resolved.workspace, imagePath)).catch(() => undefined);\n\t\t\t\t\tif (source === undefined)\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tconst response = await resolved.client.editImage(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\timage: source,\n\t\t\t\t\t\t\t// The edits endpoint accepts png/jpeg/webp inputs; the\n\t\t\t\t\t\t\t// multipart blob must carry the source's real format\n\t\t\t\t\t\t\t// (a prior url-form download may be JPEG).\n\t\t\t\t\t\t\tmimeType: detectImageFormatV1(source).mimeType,\n\t\t\t\t\t\t\tprompt,\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tclientSignal,\n\t\t\t\t\t);\n\t\t\t\t\tconst collected = await collectImages(response, fetchImpl, clientSignal);\n\t\t\t\t\tif (!Array.isArray(collected)) return failed(model, collected.code, collected.message);\n\t\t\t\t\tconst persisted = await persistImages(resolved.workspace, collected);\n\t\t\t\t\treturn successResult(\n\t\t\t\t\t\t{ version: 1, status: \"succeeded\", files: persisted.files, model, ...usageOf(response) },\n\t\t\t\t\t\tpersisted.images,\n\t\t\t\t\t\tmodelSourceNote,\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\treturn failureFrom(error, clientSignal, model, \"Image edit request failed\");\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t};\n}\n\nfunction failureFrom(\n\terror: unknown,\n\tsignal: AbortSignal | undefined,\n\tmodel: string,\n\tfallbackMessage: string,\n): ImageGenResultV1 {\n\tif (signal?.aborted) return cancelled(model);\n\tlet message = error instanceof Error ? error.message : fallbackMessage;\n\t// Append (never replace) a proxy hint for opaque network failures: the\n\t// single most common cause on machines that route out through a VPN.\n\tif (/fetch failed|terminated|ENOTFOUND|ECONNREFUSED|ETIMEDOUT|EAI_AGAIN|UND_ERR/u.test(message))\n\t\tmessage = `${message} (if this machine reaches the internet through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README)`;\n\treturn failed(model, \"image_request_failed\", boundedImageMessageV1(message));\n}\n"]}
@@ -313,6 +313,8 @@ async function collectImages(response, fetchImpl, signal) {
313
313
  download = await fetchImpl(item.url, { signal: downloadSignal });
314
314
  }
315
315
  catch (error) {
316
+ if (signal?.aborted)
317
+ throw error;
316
318
  // Network-level download failures must name the host and the proxy
317
319
  // fix: a bare "fetch failed" filed under the *request* code sent the
318
320
  // seat diagnosis down the wrong path for an hour (the generation
@@ -1 +1 @@
1
- {"version":3,"file":"capability.js","sourceRoot":"","sources":["../src/capability.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC9E,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,EAAE,o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CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC;oBACvF,MAAM,SAAS,GAAG,MAAM,aAAa,CAAC,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;oBACrE,OAAO,aAAa,CACnB,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,EAAE,EACxF,SAAS,CAAC,MAAM,EAChB,eAAe,CACf,CAAC;gBACH,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBAChB,OAAO,WAAW,CAAC,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,2BAA2B,CAAC,CAAC;gBAC7E,CAAC;YAAA,CACD;SACD,CAAC,CAAC;IAAA,CACH,CAAC;AAAA,CACF;AAED,SAAS,WAAW,CACnB,KAAc,EACd,MAA+B,EAC/B,KAAa,EACb,eAAuB,EACJ;IACnB,IAAI,MAAM,EAAE,OAAO;QAAE,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;IAC7C,IAAI,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC;IACvE,uEAAuE;IACvE,qEAAqE;IACrE,IAAI,6EAA6E,CAAC,IAAI,CAAC,OAAO,CAAC;QAC9F,OAAO,GAAG,GAAG,OAAO,gIAA8H,CAAC;IACpJ,OAAO,MAAM,CAAC,KAAK,EAAE,sBAAsB,EAAE,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC;AAAA,CAC7E","sourcesContent":["import { mkdir, readFile, realpath, stat, writeFile } from \"node:fs/promises\";\nimport { join, sep } from \"node:path\";\nimport type { CapabilityAPI, CapabilityPluginSyncFactory, PluginConfig } from \"@agent-forge/plugin-sdk\";\nimport { Type } from \"typebox\";\nimport { wrapFetchWithEnvProxyV1 } from \"./env-proxy-fetch.ts\";\nimport { createImagesClientV1 } from \"./images-client.ts\";\nimport {\n\tboundedImageMessageV1,\n\tdetectImageFormatV1,\n\tIMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1,\n\tIMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1,\n\ttype ImageFormatV1,\n\ttype ImageGenResultV1,\n\ttype ImagesClientResponseV1,\n} from \"./types.ts\";\n\nconst IMAGE_GENERATE_PARAMETERS = Type.Object(\n\t{\n\t\tprompt: Type.String({ minLength: 1, maxLength: 4000 }),\n\t\tsize: Type.Optional(Type.String({ minLength: 1, maxLength: 32 })),\n\t\tquality: Type.Optional(Type.String({ minLength: 1, maxLength: 32 })),\n\t\tn: Type.Optional(Type.Integer({ minimum: 1, maximum: 4 })),\n\t\tprovider: Type.Optional(Type.String({ minLength: 1, maxLength: 64 })),\n\t\tmodel: Type.Optional(Type.String({ minLength: 1, maxLength: 128 })),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst IMAGE_EDIT_PARAMETERS = Type.Object(\n\t{\n\t\timagePath: Type.String({ minLength: 1, maxLength: 512, pattern: \"^[A-Za-z0-9_][A-Za-z0-9._/-]*$\" }),\n\t\tprompt: Type.String({ minLength: 1, maxLength: 4000 }),\n\t\tprovider: Type.Optional(Type.String({ minLength: 1, maxLength: 64 })),\n\t\tmodel: Type.Optional(Type.String({ minLength: 1, maxLength: 128 })),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst IMAGE_PATH_PATTERN_V1 = /^[A-Za-z0-9_][A-Za-z0-9._/-]*$/u;\n\nconst GENERATED_IMAGES_DIRECTORY = \"generated-images\";\n\ninterface ImageFailureV1 {\n\treadonly code: string;\n\treadonly message: string;\n}\n\nfunction objectInput(first: unknown, second: unknown): Record<string, unknown> {\n\t// Shape-based dual-convention read (aligned with subagent-delegate): the\n\t// runtime invoke face passes the input object first and the host\n\t// CapabilityToolContext second; the agent tool face passes the tool call\n\t// id (a string) first and the params object second.\n\tconst value = first !== null && typeof first === \"object\" && !Array.isArray(first) ? first : second;\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new Error(\"Image generation input must be an object\");\n\treturn value as Record<string, unknown>;\n}\n\nfunction signalFrom(value: unknown): AbortSignal | undefined {\n\treturn value instanceof AbortSignal ? value : undefined;\n}\n\nfunction combineSignals(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined {\n\tconst present = signals.filter((signal): signal is AbortSignal => signal !== undefined);\n\tif (present.length === 0) return undefined;\n\tif (present.length === 1) return present[0];\n\treturn AbortSignal.any(present);\n}\n\nfunction failed(model: string, code: string, message: string): ImageGenResultV1 {\n\treturn {\n\t\tversion: 1,\n\t\tstatus: \"failed\",\n\t\tfiles: [],\n\t\tmodel,\n\t\terror: { code, message: boundedImageMessageV1(message) },\n\t};\n}\n\nfunction cancelled(model: string): ImageGenResultV1 {\n\treturn {\n\t\tversion: 1,\n\t\tstatus: \"cancelled\",\n\t\tfiles: [],\n\t\tmodel,\n\t\terror: { code: \"cancelled\", message: \"Image generation was cancelled\" },\n\t};\n}\n\n/** `param` (non-empty string), a non-empty env value, or undefined. */\nfunction nonEmptyEnvValue(name: string): string | undefined {\n\tconst value = process.env[name];\n\treturn typeof value === \"string\" && value.trim() !== \"\" ? value : undefined;\n}\n\nfunction boundedString(value: unknown, maxLength: number): string | undefined {\n\treturn typeof value === \"string\" && value !== \"\" && value.length <= maxLength ? value : undefined;\n}\n\n/** settings 槽位形状(宿主经 configOverride 注入,设计 §3.1/§3.3)。 */\ninterface ImageGenerationSlotV1 {\n\treadonly provider: string;\n\treadonly model?: string;\n}\n\n/** 目录快照条目:宿主装配期从 models.json 登记项投影(设计 §3.2 推断供料)。 */\ninterface CatalogModelV1 {\n\treadonly id: string;\n\treadonly name?: string;\n}\n\ntype ImageModelCatalogV1 = Readonly<Record<string, ReadonlyArray<CatalogModelV1>>>;\n\nfunction readInjectedSlot(config: PluginConfig): { readonly slot?: ImageGenerationSlotV1; readonly error?: string } {\n\tconst raw = config.imageGeneration;\n\tif (raw === undefined) return {};\n\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw))\n\t\treturn { error: \"injected config imageGeneration must be an object\" };\n\tconst record = raw as Record<string, unknown>;\n\tif (typeof record.provider !== \"string\" || record.provider.trim() === \"\")\n\t\treturn {\n\t\t\terror: `injected config imageGeneration.provider must be a non-empty string (got ${typeof record.provider})`,\n\t\t};\n\tif (record.model !== undefined && (typeof record.model !== \"string\" || record.model.trim() === \"\"))\n\t\treturn { error: \"injected config imageGeneration.model must be a non-empty string when present\" };\n\treturn { slot: { provider: record.provider, ...(record.model === undefined ? {} : { model: record.model }) } };\n}\n\nfunction readInjectedCatalog(config: PluginConfig): { readonly catalog: ImageModelCatalogV1; readonly error?: string } {\n\tconst raw = config.imageModelCatalog;\n\tif (raw === undefined) return { catalog: {} };\n\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw))\n\t\treturn {\n\t\t\tcatalog: {},\n\t\t\terror: \"injected config imageModelCatalog must be an object mapping providers to image models\",\n\t\t};\n\tconst catalog: Record<string, Array<{ id: string; name?: string }>> = {};\n\tfor (const [providerId, entries] of Object.entries(raw as Record<string, unknown>)) {\n\t\tif (!Array.isArray(entries))\n\t\t\treturn { catalog: {}, error: `injected config imageModelCatalog[\"${providerId}\"] must be an array` };\n\t\tconst models: Array<{ id: string; name?: string }> = [];\n\t\tfor (const entry of entries) {\n\t\t\tif (typeof entry !== \"object\" || entry === null)\n\t\t\t\treturn { catalog: {}, error: `injected config imageModelCatalog[\"${providerId}\"] entries must be objects` };\n\t\t\tconst record = entry as Record<string, unknown>;\n\t\t\tif (typeof record.id !== \"string\" || record.id === \"\")\n\t\t\t\treturn {\n\t\t\t\t\tcatalog: {},\n\t\t\t\t\terror: `injected config imageModelCatalog[\"${providerId}\"] entries need a non-empty string id`,\n\t\t\t\t};\n\t\t\tmodels.push({ id: record.id, ...(typeof record.name === \"string\" ? { name: record.name } : {}) });\n\t\t}\n\t\tif (models.length > 0) catalog[providerId] = models;\n\t}\n\treturn { catalog };\n}\n\n/** 确定性排序键:name 优先(缺省回退 id),同键按 id;码点序,与宿主快照顺序无关。 */\nfunction compareCatalogModels(left: CatalogModelV1, right: CatalogModelV1): number {\n\tconst leftKey = left.name ?? left.id;\n\tconst rightKey = right.name ?? right.id;\n\tif (leftKey !== rightKey) return leftKey < rightKey ? -1 : 1;\n\treturn left.id < right.id ? -1 : left.id > right.id ? 1 : 0;\n}\n\ntype ImageTargetResolution =\n\t| { readonly ok: true; readonly provider: string; readonly model: string; readonly modelSourceNote?: string }\n\t| { readonly ok: false; readonly message: string };\n\nconst IMAGE_GENERATION_CONFIG_EXAMPLE =\n\t'Example settings.json: { \"capabilities\": { \"imageGeneration\": { \"provider\": \"myimg\", \"model\": \"gpt-image-1\" } } }';\n\n/**\n * 六层取值链(供应商能力槽位设计 §3.2):\n * 1. 工具参数 provider/model;\n * 2. settings 槽位(宿主注入的 config.imageGeneration,model 可省);\n * 3. env AGENT_FORGE_IMAGEGEN_PROVIDER/MODEL(存量兼容);\n * 4. 主供应商推断——会话当前 provider 在目录快照中登记过生图模型时取用;\n * 5. 唯一候选推断——全配置域恰好一个 provider 登记过生图模型(且非当前主)时取用;\n * 6. fail-closed——config_missing,多候选时列出候选清单。\n * model 在 1-3 层均未命中时按已解析 provider 的登记生图模型推断(唯一命中直接\n * 用;多命中确定性排序取第一个并注明来源)。\n */\nfunction resolveImageTarget(\n\tinput: Record<string, unknown>,\n\tslot: ImageGenerationSlotV1 | undefined,\n\tcatalog: ImageModelCatalogV1,\n\tsessionProvider: string | undefined,\n): ImageTargetResolution {\n\tconst paramProvider = boundedString(input.provider, 64);\n\tconst paramModel = boundedString(input.model, 128);\n\tconst envProvider = nonEmptyEnvValue(\"AGENT_FORGE_IMAGEGEN_PROVIDER\");\n\tconst envModel = nonEmptyEnvValue(\"AGENT_FORGE_IMAGEGEN_MODEL\");\n\n\tlet provider = paramProvider ?? slot?.provider ?? envProvider;\n\tif (provider === undefined) {\n\t\tif (sessionProvider !== undefined && (catalog[sessionProvider]?.length ?? 0) > 0) provider = sessionProvider;\n\t\telse {\n\t\t\tconst candidates = Object.keys(catalog)\n\t\t\t\t.filter((providerId) => catalog[providerId].length > 0)\n\t\t\t\t.sort();\n\t\t\tif (candidates.length === 1) provider = candidates[0];\n\t\t\telse if (candidates.length > 1)\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t`Multiple image-generation providers are registered in models.json (${candidates.join(\", \")}); image generation fails closed instead of guessing. ` +\n\t\t\t\t\t\t`Configure capabilities.imageGeneration in settings.json or pass the provider parameter. ${IMAGE_GENERATION_CONFIG_EXAMPLE}`,\n\t\t\t\t};\n\t\t\telse\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Image generation is not configured: pass provider and model via the tool parameters, set capabilities.imageGeneration in settings.json, \" +\n\t\t\t\t\t\t'set AGENT_FORGE_IMAGEGEN_PROVIDER and AGENT_FORGE_IMAGEGEN_MODEL, or register an image model (\"output\": [\"image\"]) for a provider in models.json. ' +\n\t\t\t\t\t\tIMAGE_GENERATION_CONFIG_EXAMPLE,\n\t\t\t\t};\n\t\t}\n\t}\n\n\tlet model = paramModel ?? slot?.model ?? envModel;\n\tlet modelSourceNote: string | undefined;\n\tif (model === undefined) {\n\t\tconst registered = catalog[provider] ?? [];\n\t\tif (registered.length === 1) model = registered[0].id;\n\t\telse if (registered.length > 1) {\n\t\t\tconst first = [...registered].sort(compareCatalogModels)[0];\n\t\t\tmodel = first.id;\n\t\t\tmodelSourceNote =\n\t\t\t\t`model ${first.id} was picked deterministically (first by name) from ${registered.length} image models registered for provider ${provider} in models.json; ` +\n\t\t\t\t\"pass an explicit model to choose another\";\n\t\t} else\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\tmessage:\n\t\t\t\t\t`No image model is configured for provider ${provider}: no model came from the tool parameters, settings, or env, ` +\n\t\t\t\t\t`and provider ${provider} has no models.json model with \"output\": [\"image\"]. Register one or pass an explicit model. ${IMAGE_GENERATION_CONFIG_EXAMPLE}`,\n\t\t\t};\n\t}\n\treturn { ok: true, provider, model, ...(modelSourceNote === undefined ? {} : { modelSourceNote }) };\n}\n\nfunction promptFrom(value: unknown): string | undefined {\n\treturn boundedString(value, 4000);\n}\n\nfunction countFrom(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isSafeInteger(value) && value >= 1 && value <= 4 ? value : undefined;\n}\n\n/** Workspace-relative identifier confined to the generated-images directory. */\nfunction validImagePath(imagePath: string): boolean {\n\tif (!IMAGE_PATH_PATTERN_V1.test(imagePath)) return false;\n\tif (!imagePath.startsWith(`${GENERATED_IMAGES_DIRECTORY}/`)) return false;\n\treturn !imagePath.split(\"/\").includes(\"..\");\n}\n\n/**\n * image_edit ships workspace bytes to an external image provider, so the\n * lexical validImagePath gate is not enough: a symlink inside\n * generated-images/ can point anywhere on disk. Same sandbox model as the\n * workspace-files-write host: resolve the workspace root and the candidate to\n * their real paths and require the candidate to stay inside the real root.\n * A missing file (or dangling link) fails realpath and maps to the regular\n * not-found outcome; a link resolving outside the workspace is a boundary\n * escape and is refused. Links that stay inside the workspace keep working —\n * their bytes never leave the sandbox boundary.\n */\nasync function sandboxedImagePath(workspace: string, imagePath: string): Promise<\"missing\" | \"escape\" | \"ok\"> {\n\tconst root = await realpath(workspace).catch(() => undefined);\n\tif (root === undefined) return \"missing\";\n\tconst real = await realpath(join(workspace, imagePath)).catch(() => undefined);\n\tif (real === undefined) return \"missing\";\n\tif (real !== root && !real.startsWith(root + sep)) return \"escape\";\n\treturn \"ok\";\n}\n\n/**\n * Local-time `yyyymmdd-HHmmss` stamp shared by every file of one invocation.\n * Local (not UTC): users find today's images by the date in the filename, and\n * on UTC+8 machines a UTC stamp labels evening images as \"tomorrow\". Sorting\n * stays monotonic wherever local time has no DST jumps (e.g. China).\n */\nfunction localStampV1(date: Date): string {\n\tconst pad = (value: number): string => String(value).padStart(2, \"0\");\n\treturn `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}-${pad(\n\t\tdate.getHours(),\n\t)}${pad(date.getMinutes())}${pad(date.getSeconds())}`;\n}\n\ninterface ImageToolSuccessV1 {\n\treadonly content: readonly { readonly type: \"text\"; readonly text: string }[];\n\treadonly details: Record<string, never>;\n}\n\ninterface ProducedImageV1 {\n\treadonly bytes: Uint8Array;\n\treadonly format: ImageFormatV1;\n}\n\nfunction successResult(\n\tresult: ImageGenResultV1,\n\timages: readonly ProducedImageV1[],\n\tmodelSourceNote?: string,\n): ImageToolSuccessV1 | ImageGenResultV1 {\n\tif (result.status !== \"succeeded\") return result;\n\t// Zero inlining (user decision, 2026-09-30): image payloads never ride\n\t// the conversation — they would cost context on every later turn. The\n\t// tool result carries conclusions only (paths/model/usage); the model\n\t// reads a file on demand when it actually needs to look, which the host\n\t// read tool serves as an auto-resized image attachment.\n\tvoid images;\n\treturn {\n\t\tcontent: [\n\t\t\t{\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: JSON.stringify(\n\t\t\t\t\t{\n\t\t\t\t\t\tfiles: result.files,\n\t\t\t\t\t\tmodel: result.model,\n\t\t\t\t\t\t...(modelSourceNote === undefined ? {} : { modelSource: modelSourceNote }),\n\t\t\t\t\t\t...(result.usage === undefined ? {} : { usage: result.usage }),\n\t\t\t\t\t\tnote: \"images are saved to disk only; read a file to view it\",\n\t\t\t\t\t},\n\t\t\t\t\tnull,\n\t\t\t\t\t1,\n\t\t\t\t),\n\t\t\t},\n\t\t],\n\t\tdetails: {},\n\t};\n}\n\n/** Write one image; the exclusive-create retry makes concurrent invocations (parallel tool groups) safe. */\nasync function writeImage(\n\tworkspace: string,\n\tstamp: string,\n\tstartSequence: number,\n\tproduced: ProducedImageV1,\n): Promise<{ readonly relativePath: string; readonly sequence: number }> {\n\tconst directory = join(workspace, GENERATED_IMAGES_DIRECTORY);\n\tawait mkdir(directory, { recursive: true });\n\t// flag \"wx\" fails atomically when the name is taken (same-second stamp or\n\t// a concurrent tool group) instead of racing existsSync-then-write, which\n\t// silently overwrote one image with another under parallel execution.\n\tlet sequence = startSequence;\n\tfor (;;) {\n\t\tconst name = `img-${stamp}-${sequence}.${produced.format.extension}`;\n\t\ttry {\n\t\t\tawait writeFile(join(directory, name), produced.bytes, { flag: \"wx\" });\n\t\t\treturn { relativePath: `${GENERATED_IMAGES_DIRECTORY}/${name}`, sequence };\n\t\t} catch (error) {\n\t\t\tif ((error as NodeJS.ErrnoException).code === \"EEXIST\") {\n\t\t\t\tsequence += 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n}\n\n/** Persist every produced image file for a settled response. */\nasync function persistImages(\n\tworkspace: string,\n\tproduced: readonly ProducedImageV1[],\n): Promise<{ readonly files: string[]; readonly images: ProducedImageV1[] }> {\n\tconst stamp = localStampV1(new Date());\n\tconst files: string[] = [];\n\tconst images: ProducedImageV1[] = [];\n\tlet sequence = 1;\n\tfor (const image of produced) {\n\t\tconst written = await writeImage(workspace, stamp, sequence, image);\n\t\tfiles.push(written.relativePath);\n\t\timages.push(image);\n\t\tsequence = written.sequence + 1;\n\t}\n\treturn { files, images };\n}\n\n/** Inline b64 items pass through; url items are downloaded with the same fetch injection. */\nasync function collectImages(\n\tresponse: ImagesClientResponseV1,\n\tfetchImpl: typeof fetch,\n\tsignal: AbortSignal | undefined,\n): Promise<ProducedImageV1[] | ImageFailureV1> {\n\tconst images: ProducedImageV1[] = [];\n\tfor (const item of response.data) {\n\t\tif (item.b64 !== undefined) {\n\t\t\tconst bytes = Buffer.from(item.b64, \"base64\");\n\t\t\timages.push({ bytes, format: detectImageFormatV1(bytes) });\n\t\t\tcontinue;\n\t\t}\n\t\tif (item.url === undefined) continue;\n\t\t// The download carries its own 120s budget: a slow proxy route must\n\t\t// not hang the tool on undici's default 300s body timeout.\n\t\tconst downloadSignal =\n\t\t\tsignal === undefined\n\t\t\t\t? AbortSignal.timeout(IMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1)\n\t\t\t\t: AbortSignal.any([signal, AbortSignal.timeout(IMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1)]);\n\t\tlet download: Response;\n\t\ttry {\n\t\t\tdownload = await fetchImpl(item.url, { signal: downloadSignal });\n\t\t} catch (error) {\n\t\t\t// Network-level download failures must name the host and the proxy\n\t\t\t// fix: a bare \"fetch failed\" filed under the *request* code sent the\n\t\t\t// seat diagnosis down the wrong path for an hour (the generation\n\t\t\t// itself was succeeding; the CDN was unreachable without a proxy).\n\t\t\tlet host = \"the provider cdn\";\n\t\t\ttry {\n\t\t\t\thost = new URL(item.url).host;\n\t\t\t} catch {}\n\t\t\treturn {\n\t\t\t\tcode: \"download_failed\",\n\t\t\t\tmessage: `image download from ${host} failed (${error instanceof Error ? error.message : \"network error\"}); if that host is only reachable through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README`,\n\t\t\t};\n\t\t}\n\t\tif (!download.ok)\n\t\t\treturn {\n\t\t\t\tcode: \"download_failed\",\n\t\t\t\tmessage: `image download from the provider url failed with status ${download.status} (if this machine reaches the internet through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README)`,\n\t\t\t};\n\t\tconst bytes = Buffer.from(await download.arrayBuffer());\n\t\tif (bytes.byteLength > IMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1)\n\t\t\treturn { code: \"response_too_large\", message: \"downloaded image exceeds the 20 MiB response limit\" };\n\t\timages.push({ bytes, format: detectImageFormatV1(bytes) });\n\t}\n\treturn images;\n}\n\nfunction usageOf(response: ImagesClientResponseV1): { readonly usage?: Record<string, unknown> } {\n\treturn response.usage === undefined ? {} : { usage: response.usage };\n}\n\n/** Resolve provider config/credentials/workspace; a miss returns the failed result directly. */\nasync function resolveInvocation(\n\tapi: CapabilityAPI,\n\tproviderId: string,\n\tfetchImpl: typeof fetch,\n\tmodel: string,\n): Promise<\n\t| { readonly status: \"ok\"; readonly client: ReturnType<typeof createImagesClientV1>; readonly workspace: string }\n\t| { readonly status: \"failed\"; readonly result: ImageGenResultV1 }\n> {\n\tconst workspace = api.host.workspaceDirectory;\n\tif (typeof workspace !== \"string\" || workspace === \"\")\n\t\treturn {\n\t\t\tstatus: \"failed\",\n\t\t\tresult: failed(\n\t\t\t\tmodel,\n\t\t\t\t\"workspace_unavailable\",\n\t\t\t\t\"Image generation requires the host workspaceDirectory service to write generated-images/\",\n\t\t\t),\n\t\t};\n\tconst resolution = api.credentials === undefined ? undefined : await api.credentials.resolve({ providerId });\n\tif (resolution === undefined) {\n\t\treturn { status: \"failed\", result: credentialsFailure(model, providerId) };\n\t}\n\tif (\n\t\ttypeof resolution.apiKey !== \"string\" ||\n\t\tresolution.apiKey === \"\" ||\n\t\ttypeof resolution.baseUrl !== \"string\" ||\n\t\tresolution.baseUrl === \"\"\n\t) {\n\t\treturn { status: \"failed\", result: credentialsFailure(model, providerId) };\n\t}\n\treturn {\n\t\tstatus: \"ok\",\n\t\tworkspace,\n\t\tclient: createImagesClientV1({ baseUrl: resolution.baseUrl, apiKey: resolution.apiKey, fetchImpl }),\n\t};\n}\n\nfunction credentialsFailure(model: string, providerId: string): ImageGenResultV1 {\n\treturn failed(\n\t\tmodel,\n\t\t\"credentials_unavailable\",\n\t\t`Image generation credentials are unavailable for provider ${providerId}: the host must resolve apiKey and baseUrl for this provider (auth.json; store the key via /login)`,\n\t);\n}\n\n/**\n * Registers the full-control `image_generate` and `image_edit` tools. The\n * factory takes no credentials: provider auth (`apiKey` + `baseUrl`) is\n * resolved at call time through the host's public `CredentialAPI`. The\n * optional `fetchImpl` is a test seam only; production hosts use global fetch.\n */\nexport function createImageGenerationCapabilityFactoryV1(options?: {\n\treadonly fetchImpl?: typeof fetch;\n}): CapabilityPluginSyncFactory {\n\t// Every outbound request (generation and url download alike) rides the\n\t// same proxy-aware fetch: HTTPS_PROXY/HTTP_PROXY/NO_PROXY are honored\n\t// per request without touching the host's global dispatcher. The default\n\t// base is the undici package's fetch — the global fetch bundles another\n\t// undici version and rejects this package's dispatchers.\n\tconst fetchImpl: typeof fetch = wrapFetchWithEnvProxyV1(options?.fetchImpl);\n\treturn (api) => {\n\t\tif (api.host.mode === \"readonly\") return;\n\t\t// 宿主装配期注入(设计 §3.3):settings 槽位 + 生图模型目录快照经\n\t\t// configOverride 合并进 api.config。注入形状非法不在装载期崩溃——每次\n\t\t// 调用按 config_missing 可见失败(失败可见,不静默降级)。\n\t\tconst slotInjection = readInjectedSlot(api.config);\n\t\tconst catalogInjection = readInjectedCatalog(api.config);\n\t\tconst sessionProvider = (): string | undefined => api.session?.getModel?.()?.provider;\n\t\tconst resolveTarget = (input: Record<string, unknown>): ImageTargetResolution => {\n\t\t\tif (slotInjection.error !== undefined)\n\t\t\t\treturn { ok: false, message: `Invalid injected image-generation config: ${slotInjection.error}` };\n\t\t\tif (catalogInjection.error !== undefined)\n\t\t\t\treturn { ok: false, message: `Invalid injected image-generation config: ${catalogInjection.error}` };\n\t\t\treturn resolveImageTarget(input, slotInjection.slot, catalogInjection.catalog, sessionProvider());\n\t\t};\n\t\tapi.registerTool({\n\t\t\tname: \"image_generate\",\n\t\t\tlabel: \"Image generate\",\n\t\t\tdescription:\n\t\t\t\t\"Generate images from a prompt via the host-configured provider's OpenAI-compatible images API; saves image files (png/jpg/webp/gif as produced) under generated-images/ and returns their workspace-relative paths — images are never inlined into the conversation, read a returned file to view it\",\n\t\t\tparameters: IMAGE_GENERATE_PARAMETERS,\n\t\t\texecute: async (first: unknown, second?: unknown, third?: unknown) => {\n\t\t\t\tlet input: Record<string, unknown>;\n\t\t\t\ttry {\n\t\t\t\t\tinput = objectInput(first, second);\n\t\t\t\t} catch {\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"Image generation input must be an object\");\n\t\t\t\t}\n\t\t\t\tconst prompt = promptFrom(input.prompt);\n\t\t\t\tif (prompt === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"prompt must be a non-empty string of at most 4000 characters\");\n\t\t\t\tconst size = boundedString(input.size, 32);\n\t\t\t\tconst quality = boundedString(input.quality, 32);\n\t\t\t\tif (input.n !== undefined && countFrom(input.n) === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"n must be an integer between 1 and 4\");\n\t\t\t\tconst n = countFrom(input.n) ?? 1;\n\t\t\t\tconst sessionSignal = api.sessionAbortSignal?.();\n\t\t\t\tconst invocationSignal = signalFrom(third);\n\t\t\t\tconst clientSignal = combineSignals([invocationSignal, sessionSignal]);\n\t\t\t\tif (clientSignal?.aborted) return cancelled(\"\");\n\t\t\t\tconst target = resolveTarget(input);\n\t\t\t\tif (!target.ok) return failed(\"\", \"config_missing\", boundedImageMessageV1(target.message));\n\t\t\t\tconst { provider, model, modelSourceNote } = target;\n\t\t\t\ttry {\n\t\t\t\t\tconst resolved = await resolveInvocation(api, provider, fetchImpl, model);\n\t\t\t\t\tif (resolved.status === \"failed\") return resolved.result;\n\t\t\t\t\tconst response = await resolved.client.generateImages(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\tprompt,\n\t\t\t\t\t\t\tn,\n\t\t\t\t\t\t\t...(size === undefined ? {} : { size }),\n\t\t\t\t\t\t\t...(quality === undefined ? {} : { quality }),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tclientSignal,\n\t\t\t\t\t);\n\t\t\t\t\tconst collected = await collectImages(response, fetchImpl, clientSignal);\n\t\t\t\t\tif (!Array.isArray(collected)) return failed(model, collected.code, collected.message);\n\t\t\t\t\tconst persisted = await persistImages(resolved.workspace, collected);\n\t\t\t\t\treturn successResult(\n\t\t\t\t\t\t{ version: 1, status: \"succeeded\", files: persisted.files, model, ...usageOf(response) },\n\t\t\t\t\t\tpersisted.images,\n\t\t\t\t\t\tmodelSourceNote,\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\treturn failureFrom(error, clientSignal, model, \"Image generation request failed\");\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tapi.registerTool({\n\t\t\tname: \"image_edit\",\n\t\t\tlabel: \"Image edit\",\n\t\t\tdescription:\n\t\t\t\t\"Edit a previously generated image (workspace-relative path under generated-images/) with a revision prompt; returns the new file path\",\n\t\t\tparameters: IMAGE_EDIT_PARAMETERS,\n\t\t\texecute: async (first: unknown, second?: unknown, third?: unknown) => {\n\t\t\t\tlet input: Record<string, unknown>;\n\t\t\t\ttry {\n\t\t\t\t\tinput = objectInput(first, second);\n\t\t\t\t} catch {\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"Image generation input must be an object\");\n\t\t\t\t}\n\t\t\t\tconst prompt = promptFrom(input.prompt);\n\t\t\t\tif (prompt === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"prompt must be a non-empty string of at most 4000 characters\");\n\t\t\t\tconst imagePath = typeof input.imagePath === \"string\" ? input.imagePath : undefined;\n\t\t\t\tif (imagePath === undefined || !validImagePath(imagePath))\n\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\"\",\n\t\t\t\t\t\t\"invalid_path\",\n\t\t\t\t\t\t\"imagePath must be a workspace-relative identifier inside the generated-images/ directory (no .. segments)\",\n\t\t\t\t\t);\n\t\t\t\tconst sessionSignal = api.sessionAbortSignal?.();\n\t\t\t\tconst invocationSignal = signalFrom(third);\n\t\t\t\tconst clientSignal = combineSignals([invocationSignal, sessionSignal]);\n\t\t\t\tif (clientSignal?.aborted) return cancelled(\"\");\n\t\t\t\tconst target = resolveTarget(input);\n\t\t\t\tif (!target.ok) return failed(\"\", \"config_missing\", boundedImageMessageV1(target.message));\n\t\t\t\tconst { provider, model, modelSourceNote } = target;\n\t\t\t\ttry {\n\t\t\t\t\tconst resolved = await resolveInvocation(api, provider, fetchImpl, model);\n\t\t\t\t\tif (resolved.status === \"failed\") return resolved.result;\n\t\t\t\t\t// Defense in depth (readonly never registers these tools): the\n\t\t\t\t\t// lexical validImagePath gate cannot see symlinks, so refuse any\n\t\t\t\t\t// candidate whose real path escapes the workspace before a byte\n\t\t\t\t\t// is read and shipped to the provider.\n\t\t\t\t\tconst sandbox = await sandboxedImagePath(resolved.workspace, imagePath);\n\t\t\t\t\tif (sandbox === \"missing\")\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tif (sandbox === \"escape\")\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"invalid_path\",\n\t\t\t\t\t\t\t`imagePath ${imagePath} resolves outside the workspace; only files under the workspace generated-images/ directory can be read`,\n\t\t\t\t\t\t);\n\t\t\t\t\t// Size-check before reading: users can drop their own files\n\t\t\t\t\t// into generated-images/, and the 20 MiB download cap must\n\t\t\t\t\t// not be bypassed by a renamed local file.\n\t\t\t\t\tconst sourceStats = await stat(join(resolved.workspace, imagePath)).catch(() => undefined);\n\t\t\t\t\tif (sourceStats === undefined || !sourceStats.isFile())\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tif (sourceStats.size > IMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1)\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"response_too_large\",\n\t\t\t\t\t\t\t`image ${imagePath} is ${sourceStats.size} bytes, above the 20 MiB limit`,\n\t\t\t\t\t\t);\n\t\t\t\t\tconst source = await readFile(join(resolved.workspace, imagePath)).catch(() => undefined);\n\t\t\t\t\tif (source === undefined)\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tconst response = await resolved.client.editImage(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\timage: source,\n\t\t\t\t\t\t\t// The edits endpoint accepts png/jpeg/webp inputs; the\n\t\t\t\t\t\t\t// multipart blob must carry the source's real format\n\t\t\t\t\t\t\t// (a prior url-form download may be JPEG).\n\t\t\t\t\t\t\tmimeType: detectImageFormatV1(source).mimeType,\n\t\t\t\t\t\t\tprompt,\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tclientSignal,\n\t\t\t\t\t);\n\t\t\t\t\tconst collected = await collectImages(response, fetchImpl, clientSignal);\n\t\t\t\t\tif (!Array.isArray(collected)) return failed(model, collected.code, collected.message);\n\t\t\t\t\tconst persisted = await persistImages(resolved.workspace, collected);\n\t\t\t\t\treturn successResult(\n\t\t\t\t\t\t{ version: 1, status: \"succeeded\", files: persisted.files, model, ...usageOf(response) },\n\t\t\t\t\t\tpersisted.images,\n\t\t\t\t\t\tmodelSourceNote,\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\treturn failureFrom(error, clientSignal, model, \"Image edit request failed\");\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t};\n}\n\nfunction failureFrom(\n\terror: unknown,\n\tsignal: AbortSignal | undefined,\n\tmodel: string,\n\tfallbackMessage: string,\n): ImageGenResultV1 {\n\tif (signal?.aborted) return cancelled(model);\n\tlet message = error instanceof Error ? error.message : fallbackMessage;\n\t// Append (never replace) a proxy hint for opaque network failures: the\n\t// single most common cause on machines that route out through a VPN.\n\tif (/fetch failed|terminated|ENOTFOUND|ECONNREFUSED|ETIMEDOUT|EAI_AGAIN|UND_ERR/u.test(message))\n\t\tmessage = `${message} (if this machine reaches the internet through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README)`;\n\treturn failed(model, \"image_request_failed\", bounded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AC,eAAe,CAAC;IACvE,uEAAuE;IACvE,qEAAqE;IACrE,IAAI,6EAA6E,CAAC,IAAI,CAAC,OAAO,CAAC;QAC9F,OAAO,GAAG,GAAG,OAAO,gIAA8H,CAAC;IACpJ,OAAO,MAAM,CAAC,KAAK,EAAE,sBAAsB,EAAE,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC;AAAA,CAC7E","sourcesContent":["import { mkdir, readFile, realpath, stat, writeFile } from \"node:fs/promises\";\nimport { join, sep } from \"node:path\";\nimport type { CapabilityAPI, CapabilityPluginSyncFactory, PluginConfig } from \"@agent-forge/plugin-sdk\";\nimport { Type } from \"typebox\";\nimport { wrapFetchWithEnvProxyV1 } from \"./env-proxy-fetch.ts\";\nimport { createImagesClientV1 } from \"./images-client.ts\";\nimport {\n\tboundedImageMessageV1,\n\tdetectImageFormatV1,\n\tIMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1,\n\tIMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1,\n\ttype ImageFormatV1,\n\ttype ImageGenResultV1,\n\ttype ImagesClientResponseV1,\n} from \"./types.ts\";\n\nconst IMAGE_GENERATE_PARAMETERS = Type.Object(\n\t{\n\t\tprompt: Type.String({ minLength: 1, maxLength: 4000 }),\n\t\tsize: Type.Optional(Type.String({ minLength: 1, maxLength: 32 })),\n\t\tquality: Type.Optional(Type.String({ minLength: 1, maxLength: 32 })),\n\t\tn: Type.Optional(Type.Integer({ minimum: 1, maximum: 4 })),\n\t\tprovider: Type.Optional(Type.String({ minLength: 1, maxLength: 64 })),\n\t\tmodel: Type.Optional(Type.String({ minLength: 1, maxLength: 128 })),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst IMAGE_EDIT_PARAMETERS = Type.Object(\n\t{\n\t\timagePath: Type.String({ minLength: 1, maxLength: 512, pattern: \"^[A-Za-z0-9_][A-Za-z0-9._/-]*$\" }),\n\t\tprompt: Type.String({ minLength: 1, maxLength: 4000 }),\n\t\tprovider: Type.Optional(Type.String({ minLength: 1, maxLength: 64 })),\n\t\tmodel: Type.Optional(Type.String({ minLength: 1, maxLength: 128 })),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst IMAGE_PATH_PATTERN_V1 = /^[A-Za-z0-9_][A-Za-z0-9._/-]*$/u;\n\nconst GENERATED_IMAGES_DIRECTORY = \"generated-images\";\n\ninterface ImageFailureV1 {\n\treadonly code: string;\n\treadonly message: string;\n}\n\nfunction objectInput(first: unknown, second: unknown): Record<string, unknown> {\n\t// Shape-based dual-convention read (aligned with subagent-delegate): the\n\t// runtime invoke face passes the input object first and the host\n\t// CapabilityToolContext second; the agent tool face passes the tool call\n\t// id (a string) first and the params object second.\n\tconst value = first !== null && typeof first === \"object\" && !Array.isArray(first) ? first : second;\n\tif (value === null || typeof value !== \"object\" || Array.isArray(value))\n\t\tthrow new Error(\"Image generation input must be an object\");\n\treturn value as Record<string, unknown>;\n}\n\nfunction signalFrom(value: unknown): AbortSignal | undefined {\n\treturn value instanceof AbortSignal ? value : undefined;\n}\n\nfunction combineSignals(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined {\n\tconst present = signals.filter((signal): signal is AbortSignal => signal !== undefined);\n\tif (present.length === 0) return undefined;\n\tif (present.length === 1) return present[0];\n\treturn AbortSignal.any(present);\n}\n\nfunction failed(model: string, code: string, message: string): ImageGenResultV1 {\n\treturn {\n\t\tversion: 1,\n\t\tstatus: \"failed\",\n\t\tfiles: [],\n\t\tmodel,\n\t\terror: { code, message: boundedImageMessageV1(message) },\n\t};\n}\n\nfunction cancelled(model: string): ImageGenResultV1 {\n\treturn {\n\t\tversion: 1,\n\t\tstatus: \"cancelled\",\n\t\tfiles: [],\n\t\tmodel,\n\t\terror: { code: \"cancelled\", message: \"Image generation was cancelled\" },\n\t};\n}\n\n/** `param` (non-empty string), a non-empty env value, or undefined. */\nfunction nonEmptyEnvValue(name: string): string | undefined {\n\tconst value = process.env[name];\n\treturn typeof value === \"string\" && value.trim() !== \"\" ? value : undefined;\n}\n\nfunction boundedString(value: unknown, maxLength: number): string | undefined {\n\treturn typeof value === \"string\" && value !== \"\" && value.length <= maxLength ? value : undefined;\n}\n\n/** settings 槽位形状(宿主经 configOverride 注入,设计 §3.1/§3.3)。 */\ninterface ImageGenerationSlotV1 {\n\treadonly provider: string;\n\treadonly model?: string;\n}\n\n/** 目录快照条目:宿主装配期从 models.json 登记项投影(设计 §3.2 推断供料)。 */\ninterface CatalogModelV1 {\n\treadonly id: string;\n\treadonly name?: string;\n}\n\ntype ImageModelCatalogV1 = Readonly<Record<string, ReadonlyArray<CatalogModelV1>>>;\n\nfunction readInjectedSlot(config: PluginConfig): { readonly slot?: ImageGenerationSlotV1; readonly error?: string } {\n\tconst raw = config.imageGeneration;\n\tif (raw === undefined) return {};\n\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw))\n\t\treturn { error: \"injected config imageGeneration must be an object\" };\n\tconst record = raw as Record<string, unknown>;\n\tif (typeof record.provider !== \"string\" || record.provider.trim() === \"\")\n\t\treturn {\n\t\t\terror: `injected config imageGeneration.provider must be a non-empty string (got ${typeof record.provider})`,\n\t\t};\n\tif (record.model !== undefined && (typeof record.model !== \"string\" || record.model.trim() === \"\"))\n\t\treturn { error: \"injected config imageGeneration.model must be a non-empty string when present\" };\n\treturn { slot: { provider: record.provider, ...(record.model === undefined ? {} : { model: record.model }) } };\n}\n\nfunction readInjectedCatalog(config: PluginConfig): { readonly catalog: ImageModelCatalogV1; readonly error?: string } {\n\tconst raw = config.imageModelCatalog;\n\tif (raw === undefined) return { catalog: {} };\n\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw))\n\t\treturn {\n\t\t\tcatalog: {},\n\t\t\terror: \"injected config imageModelCatalog must be an object mapping providers to image models\",\n\t\t};\n\tconst catalog: Record<string, Array<{ id: string; name?: string }>> = {};\n\tfor (const [providerId, entries] of Object.entries(raw as Record<string, unknown>)) {\n\t\tif (!Array.isArray(entries))\n\t\t\treturn { catalog: {}, error: `injected config imageModelCatalog[\"${providerId}\"] must be an array` };\n\t\tconst models: Array<{ id: string; name?: string }> = [];\n\t\tfor (const entry of entries) {\n\t\t\tif (typeof entry !== \"object\" || entry === null)\n\t\t\t\treturn { catalog: {}, error: `injected config imageModelCatalog[\"${providerId}\"] entries must be objects` };\n\t\t\tconst record = entry as Record<string, unknown>;\n\t\t\tif (typeof record.id !== \"string\" || record.id === \"\")\n\t\t\t\treturn {\n\t\t\t\t\tcatalog: {},\n\t\t\t\t\terror: `injected config imageModelCatalog[\"${providerId}\"] entries need a non-empty string id`,\n\t\t\t\t};\n\t\t\tmodels.push({ id: record.id, ...(typeof record.name === \"string\" ? { name: record.name } : {}) });\n\t\t}\n\t\tif (models.length > 0) catalog[providerId] = models;\n\t}\n\treturn { catalog };\n}\n\n/** 确定性排序键:name 优先(缺省回退 id),同键按 id;码点序,与宿主快照顺序无关。 */\nfunction compareCatalogModels(left: CatalogModelV1, right: CatalogModelV1): number {\n\tconst leftKey = left.name ?? left.id;\n\tconst rightKey = right.name ?? right.id;\n\tif (leftKey !== rightKey) return leftKey < rightKey ? -1 : 1;\n\treturn left.id < right.id ? -1 : left.id > right.id ? 1 : 0;\n}\n\ntype ImageTargetResolution =\n\t| { readonly ok: true; readonly provider: string; readonly model: string; readonly modelSourceNote?: string }\n\t| { readonly ok: false; readonly message: string };\n\nconst IMAGE_GENERATION_CONFIG_EXAMPLE =\n\t'Example settings.json: { \"capabilities\": { \"imageGeneration\": { \"provider\": \"myimg\", \"model\": \"gpt-image-1\" } } }';\n\n/**\n * 六层取值链(供应商能力槽位设计 §3.2):\n * 1. 工具参数 provider/model;\n * 2. settings 槽位(宿主注入的 config.imageGeneration,model 可省);\n * 3. env AGENT_FORGE_IMAGEGEN_PROVIDER/MODEL(存量兼容);\n * 4. 主供应商推断——会话当前 provider 在目录快照中登记过生图模型时取用;\n * 5. 唯一候选推断——全配置域恰好一个 provider 登记过生图模型(且非当前主)时取用;\n * 6. fail-closed——config_missing,多候选时列出候选清单。\n * model 在 1-3 层均未命中时按已解析 provider 的登记生图模型推断(唯一命中直接\n * 用;多命中确定性排序取第一个并注明来源)。\n */\nfunction resolveImageTarget(\n\tinput: Record<string, unknown>,\n\tslot: ImageGenerationSlotV1 | undefined,\n\tcatalog: ImageModelCatalogV1,\n\tsessionProvider: string | undefined,\n): ImageTargetResolution {\n\tconst paramProvider = boundedString(input.provider, 64);\n\tconst paramModel = boundedString(input.model, 128);\n\tconst envProvider = nonEmptyEnvValue(\"AGENT_FORGE_IMAGEGEN_PROVIDER\");\n\tconst envModel = nonEmptyEnvValue(\"AGENT_FORGE_IMAGEGEN_MODEL\");\n\n\tlet provider = paramProvider ?? slot?.provider ?? envProvider;\n\tif (provider === undefined) {\n\t\tif (sessionProvider !== undefined && (catalog[sessionProvider]?.length ?? 0) > 0) provider = sessionProvider;\n\t\telse {\n\t\t\tconst candidates = Object.keys(catalog)\n\t\t\t\t.filter((providerId) => catalog[providerId].length > 0)\n\t\t\t\t.sort();\n\t\t\tif (candidates.length === 1) provider = candidates[0];\n\t\t\telse if (candidates.length > 1)\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t`Multiple image-generation providers are registered in models.json (${candidates.join(\", \")}); image generation fails closed instead of guessing. ` +\n\t\t\t\t\t\t`Configure capabilities.imageGeneration in settings.json or pass the provider parameter. ${IMAGE_GENERATION_CONFIG_EXAMPLE}`,\n\t\t\t\t};\n\t\t\telse\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t\"Image generation is not configured: pass provider and model via the tool parameters, set capabilities.imageGeneration in settings.json, \" +\n\t\t\t\t\t\t'set AGENT_FORGE_IMAGEGEN_PROVIDER and AGENT_FORGE_IMAGEGEN_MODEL, or register an image model (\"output\": [\"image\"]) for a provider in models.json. ' +\n\t\t\t\t\t\tIMAGE_GENERATION_CONFIG_EXAMPLE,\n\t\t\t\t};\n\t\t}\n\t}\n\n\tlet model = paramModel ?? slot?.model ?? envModel;\n\tlet modelSourceNote: string | undefined;\n\tif (model === undefined) {\n\t\tconst registered = catalog[provider] ?? [];\n\t\tif (registered.length === 1) model = registered[0].id;\n\t\telse if (registered.length > 1) {\n\t\t\tconst first = [...registered].sort(compareCatalogModels)[0];\n\t\t\tmodel = first.id;\n\t\t\tmodelSourceNote =\n\t\t\t\t`model ${first.id} was picked deterministically (first by name) from ${registered.length} image models registered for provider ${provider} in models.json; ` +\n\t\t\t\t\"pass an explicit model to choose another\";\n\t\t} else\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\tmessage:\n\t\t\t\t\t`No image model is configured for provider ${provider}: no model came from the tool parameters, settings, or env, ` +\n\t\t\t\t\t`and provider ${provider} has no models.json model with \"output\": [\"image\"]. Register one or pass an explicit model. ${IMAGE_GENERATION_CONFIG_EXAMPLE}`,\n\t\t\t};\n\t}\n\treturn { ok: true, provider, model, ...(modelSourceNote === undefined ? {} : { modelSourceNote }) };\n}\n\nfunction promptFrom(value: unknown): string | undefined {\n\treturn boundedString(value, 4000);\n}\n\nfunction countFrom(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isSafeInteger(value) && value >= 1 && value <= 4 ? value : undefined;\n}\n\n/** Workspace-relative identifier confined to the generated-images directory. */\nfunction validImagePath(imagePath: string): boolean {\n\tif (!IMAGE_PATH_PATTERN_V1.test(imagePath)) return false;\n\tif (!imagePath.startsWith(`${GENERATED_IMAGES_DIRECTORY}/`)) return false;\n\treturn !imagePath.split(\"/\").includes(\"..\");\n}\n\n/**\n * image_edit ships workspace bytes to an external image provider, so the\n * lexical validImagePath gate is not enough: a symlink inside\n * generated-images/ can point anywhere on disk. Same sandbox model as the\n * workspace-files-write host: resolve the workspace root and the candidate to\n * their real paths and require the candidate to stay inside the real root.\n * A missing file (or dangling link) fails realpath and maps to the regular\n * not-found outcome; a link resolving outside the workspace is a boundary\n * escape and is refused. Links that stay inside the workspace keep working —\n * their bytes never leave the sandbox boundary.\n */\nasync function sandboxedImagePath(workspace: string, imagePath: string): Promise<\"missing\" | \"escape\" | \"ok\"> {\n\tconst root = await realpath(workspace).catch(() => undefined);\n\tif (root === undefined) return \"missing\";\n\tconst real = await realpath(join(workspace, imagePath)).catch(() => undefined);\n\tif (real === undefined) return \"missing\";\n\tif (real !== root && !real.startsWith(root + sep)) return \"escape\";\n\treturn \"ok\";\n}\n\n/**\n * Local-time `yyyymmdd-HHmmss` stamp shared by every file of one invocation.\n * Local (not UTC): users find today's images by the date in the filename, and\n * on UTC+8 machines a UTC stamp labels evening images as \"tomorrow\". Sorting\n * stays monotonic wherever local time has no DST jumps (e.g. China).\n */\nfunction localStampV1(date: Date): string {\n\tconst pad = (value: number): string => String(value).padStart(2, \"0\");\n\treturn `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}-${pad(\n\t\tdate.getHours(),\n\t)}${pad(date.getMinutes())}${pad(date.getSeconds())}`;\n}\n\ninterface ImageToolSuccessV1 {\n\treadonly content: readonly { readonly type: \"text\"; readonly text: string }[];\n\treadonly details: Record<string, never>;\n}\n\ninterface ProducedImageV1 {\n\treadonly bytes: Uint8Array;\n\treadonly format: ImageFormatV1;\n}\n\nfunction successResult(\n\tresult: ImageGenResultV1,\n\timages: readonly ProducedImageV1[],\n\tmodelSourceNote?: string,\n): ImageToolSuccessV1 | ImageGenResultV1 {\n\tif (result.status !== \"succeeded\") return result;\n\t// Zero inlining (user decision, 2026-09-30): image payloads never ride\n\t// the conversation — they would cost context on every later turn. The\n\t// tool result carries conclusions only (paths/model/usage); the model\n\t// reads a file on demand when it actually needs to look, which the host\n\t// read tool serves as an auto-resized image attachment.\n\tvoid images;\n\treturn {\n\t\tcontent: [\n\t\t\t{\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: JSON.stringify(\n\t\t\t\t\t{\n\t\t\t\t\t\tfiles: result.files,\n\t\t\t\t\t\tmodel: result.model,\n\t\t\t\t\t\t...(modelSourceNote === undefined ? {} : { modelSource: modelSourceNote }),\n\t\t\t\t\t\t...(result.usage === undefined ? {} : { usage: result.usage }),\n\t\t\t\t\t\tnote: \"images are saved to disk only; read a file to view it\",\n\t\t\t\t\t},\n\t\t\t\t\tnull,\n\t\t\t\t\t1,\n\t\t\t\t),\n\t\t\t},\n\t\t],\n\t\tdetails: {},\n\t};\n}\n\n/** Write one image; the exclusive-create retry makes concurrent invocations (parallel tool groups) safe. */\nasync function writeImage(\n\tworkspace: string,\n\tstamp: string,\n\tstartSequence: number,\n\tproduced: ProducedImageV1,\n): Promise<{ readonly relativePath: string; readonly sequence: number }> {\n\tconst directory = join(workspace, GENERATED_IMAGES_DIRECTORY);\n\tawait mkdir(directory, { recursive: true });\n\t// flag \"wx\" fails atomically when the name is taken (same-second stamp or\n\t// a concurrent tool group) instead of racing existsSync-then-write, which\n\t// silently overwrote one image with another under parallel execution.\n\tlet sequence = startSequence;\n\tfor (;;) {\n\t\tconst name = `img-${stamp}-${sequence}.${produced.format.extension}`;\n\t\ttry {\n\t\t\tawait writeFile(join(directory, name), produced.bytes, { flag: \"wx\" });\n\t\t\treturn { relativePath: `${GENERATED_IMAGES_DIRECTORY}/${name}`, sequence };\n\t\t} catch (error) {\n\t\t\tif ((error as NodeJS.ErrnoException).code === \"EEXIST\") {\n\t\t\t\tsequence += 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n}\n\n/** Persist every produced image file for a settled response. */\nasync function persistImages(\n\tworkspace: string,\n\tproduced: readonly ProducedImageV1[],\n): Promise<{ readonly files: string[]; readonly images: ProducedImageV1[] }> {\n\tconst stamp = localStampV1(new Date());\n\tconst files: string[] = [];\n\tconst images: ProducedImageV1[] = [];\n\tlet sequence = 1;\n\tfor (const image of produced) {\n\t\tconst written = await writeImage(workspace, stamp, sequence, image);\n\t\tfiles.push(written.relativePath);\n\t\timages.push(image);\n\t\tsequence = written.sequence + 1;\n\t}\n\treturn { files, images };\n}\n\n/** Inline b64 items pass through; url items are downloaded with the same fetch injection. */\nasync function collectImages(\n\tresponse: ImagesClientResponseV1,\n\tfetchImpl: typeof fetch,\n\tsignal: AbortSignal | undefined,\n): Promise<ProducedImageV1[] | ImageFailureV1> {\n\tconst images: ProducedImageV1[] = [];\n\tfor (const item of response.data) {\n\t\tif (item.b64 !== undefined) {\n\t\t\tconst bytes = Buffer.from(item.b64, \"base64\");\n\t\t\timages.push({ bytes, format: detectImageFormatV1(bytes) });\n\t\t\tcontinue;\n\t\t}\n\t\tif (item.url === undefined) continue;\n\t\t// The download carries its own 120s budget: a slow proxy route must\n\t\t// not hang the tool on undici's default 300s body timeout.\n\t\tconst downloadSignal =\n\t\t\tsignal === undefined\n\t\t\t\t? AbortSignal.timeout(IMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1)\n\t\t\t\t: AbortSignal.any([signal, AbortSignal.timeout(IMAGE_GENERATION_DOWNLOAD_TIMEOUT_MS_V1)]);\n\t\tlet download: Response;\n\t\ttry {\n\t\t\tdownload = await fetchImpl(item.url, { signal: downloadSignal });\n\t\t} catch (error) {\n\t\t\tif (signal?.aborted) throw error;\n\t\t\t// Network-level download failures must name the host and the proxy\n\t\t\t// fix: a bare \"fetch failed\" filed under the *request* code sent the\n\t\t\t// seat diagnosis down the wrong path for an hour (the generation\n\t\t\t// itself was succeeding; the CDN was unreachable without a proxy).\n\t\t\tlet host = \"the provider cdn\";\n\t\t\ttry {\n\t\t\t\thost = new URL(item.url).host;\n\t\t\t} catch {}\n\t\t\treturn {\n\t\t\t\tcode: \"download_failed\",\n\t\t\t\tmessage: `image download from ${host} failed (${error instanceof Error ? error.message : \"network error\"}); if that host is only reachable through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README`,\n\t\t\t};\n\t\t}\n\t\tif (!download.ok)\n\t\t\treturn {\n\t\t\t\tcode: \"download_failed\",\n\t\t\t\tmessage: `image download from the provider url failed with status ${download.status} (if this machine reaches the internet through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README)`,\n\t\t\t};\n\t\tconst bytes = Buffer.from(await download.arrayBuffer());\n\t\tif (bytes.byteLength > IMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1)\n\t\t\treturn { code: \"response_too_large\", message: \"downloaded image exceeds the 20 MiB response limit\" };\n\t\timages.push({ bytes, format: detectImageFormatV1(bytes) });\n\t}\n\treturn images;\n}\n\nfunction usageOf(response: ImagesClientResponseV1): { readonly usage?: Record<string, unknown> } {\n\treturn response.usage === undefined ? {} : { usage: response.usage };\n}\n\n/** Resolve provider config/credentials/workspace; a miss returns the failed result directly. */\nasync function resolveInvocation(\n\tapi: CapabilityAPI,\n\tproviderId: string,\n\tfetchImpl: typeof fetch,\n\tmodel: string,\n): Promise<\n\t| { readonly status: \"ok\"; readonly client: ReturnType<typeof createImagesClientV1>; readonly workspace: string }\n\t| { readonly status: \"failed\"; readonly result: ImageGenResultV1 }\n> {\n\tconst workspace = api.host.workspaceDirectory;\n\tif (typeof workspace !== \"string\" || workspace === \"\")\n\t\treturn {\n\t\t\tstatus: \"failed\",\n\t\t\tresult: failed(\n\t\t\t\tmodel,\n\t\t\t\t\"workspace_unavailable\",\n\t\t\t\t\"Image generation requires the host workspaceDirectory service to write generated-images/\",\n\t\t\t),\n\t\t};\n\tconst resolution = api.credentials === undefined ? undefined : await api.credentials.resolve({ providerId });\n\tif (resolution === undefined) {\n\t\treturn { status: \"failed\", result: credentialsFailure(model, providerId) };\n\t}\n\tif (\n\t\ttypeof resolution.apiKey !== \"string\" ||\n\t\tresolution.apiKey === \"\" ||\n\t\ttypeof resolution.baseUrl !== \"string\" ||\n\t\tresolution.baseUrl === \"\"\n\t) {\n\t\treturn { status: \"failed\", result: credentialsFailure(model, providerId) };\n\t}\n\treturn {\n\t\tstatus: \"ok\",\n\t\tworkspace,\n\t\tclient: createImagesClientV1({ baseUrl: resolution.baseUrl, apiKey: resolution.apiKey, fetchImpl }),\n\t};\n}\n\nfunction credentialsFailure(model: string, providerId: string): ImageGenResultV1 {\n\treturn failed(\n\t\tmodel,\n\t\t\"credentials_unavailable\",\n\t\t`Image generation credentials are unavailable for provider ${providerId}: the host must resolve apiKey and baseUrl for this provider (auth.json; store the key via /login)`,\n\t);\n}\n\n/**\n * Registers the full-control `image_generate` and `image_edit` tools. The\n * factory takes no credentials: provider auth (`apiKey` + `baseUrl`) is\n * resolved at call time through the host's public `CredentialAPI`. The\n * optional `fetchImpl` is a test seam only; production hosts use global fetch.\n */\nexport function createImageGenerationCapabilityFactoryV1(options?: {\n\treadonly fetchImpl?: typeof fetch;\n}): CapabilityPluginSyncFactory {\n\t// Every outbound request (generation and url download alike) rides the\n\t// same proxy-aware fetch: HTTPS_PROXY/HTTP_PROXY/NO_PROXY are honored\n\t// per request without touching the host's global dispatcher. The default\n\t// base is the undici package's fetch — the global fetch bundles another\n\t// undici version and rejects this package's dispatchers.\n\tconst fetchImpl: typeof fetch = wrapFetchWithEnvProxyV1(options?.fetchImpl);\n\treturn (api) => {\n\t\tif (api.host.mode === \"readonly\") return;\n\t\t// 宿主装配期注入(设计 §3.3):settings 槽位 + 生图模型目录快照经\n\t\t// configOverride 合并进 api.config。注入形状非法不在装载期崩溃——每次\n\t\t// 调用按 config_missing 可见失败(失败可见,不静默降级)。\n\t\tconst slotInjection = readInjectedSlot(api.config);\n\t\tconst catalogInjection = readInjectedCatalog(api.config);\n\t\tconst sessionProvider = (): string | undefined => api.session?.getModel?.()?.provider;\n\t\tconst resolveTarget = (input: Record<string, unknown>): ImageTargetResolution => {\n\t\t\tif (slotInjection.error !== undefined)\n\t\t\t\treturn { ok: false, message: `Invalid injected image-generation config: ${slotInjection.error}` };\n\t\t\tif (catalogInjection.error !== undefined)\n\t\t\t\treturn { ok: false, message: `Invalid injected image-generation config: ${catalogInjection.error}` };\n\t\t\treturn resolveImageTarget(input, slotInjection.slot, catalogInjection.catalog, sessionProvider());\n\t\t};\n\t\tapi.registerTool({\n\t\t\tname: \"image_generate\",\n\t\t\tlabel: \"Image generate\",\n\t\t\tdescription:\n\t\t\t\t\"Generate images from a prompt via the host-configured provider's OpenAI-compatible images API; saves image files (png/jpg/webp/gif as produced) under generated-images/ and returns their workspace-relative paths — images are never inlined into the conversation, read a returned file to view it\",\n\t\t\tparameters: IMAGE_GENERATE_PARAMETERS,\n\t\t\texecute: async (first: unknown, second?: unknown, third?: unknown) => {\n\t\t\t\tlet input: Record<string, unknown>;\n\t\t\t\ttry {\n\t\t\t\t\tinput = objectInput(first, second);\n\t\t\t\t} catch {\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"Image generation input must be an object\");\n\t\t\t\t}\n\t\t\t\tconst prompt = promptFrom(input.prompt);\n\t\t\t\tif (prompt === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"prompt must be a non-empty string of at most 4000 characters\");\n\t\t\t\tconst size = boundedString(input.size, 32);\n\t\t\t\tconst quality = boundedString(input.quality, 32);\n\t\t\t\tif (input.n !== undefined && countFrom(input.n) === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"n must be an integer between 1 and 4\");\n\t\t\t\tconst n = countFrom(input.n) ?? 1;\n\t\t\t\tconst sessionSignal = api.sessionAbortSignal?.();\n\t\t\t\tconst invocationSignal = signalFrom(third);\n\t\t\t\tconst clientSignal = combineSignals([invocationSignal, sessionSignal]);\n\t\t\t\tif (clientSignal?.aborted) return cancelled(\"\");\n\t\t\t\tconst target = resolveTarget(input);\n\t\t\t\tif (!target.ok) return failed(\"\", \"config_missing\", boundedImageMessageV1(target.message));\n\t\t\t\tconst { provider, model, modelSourceNote } = target;\n\t\t\t\ttry {\n\t\t\t\t\tconst resolved = await resolveInvocation(api, provider, fetchImpl, model);\n\t\t\t\t\tif (resolved.status === \"failed\") return resolved.result;\n\t\t\t\t\tconst response = await resolved.client.generateImages(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\tprompt,\n\t\t\t\t\t\t\tn,\n\t\t\t\t\t\t\t...(size === undefined ? {} : { size }),\n\t\t\t\t\t\t\t...(quality === undefined ? {} : { quality }),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tclientSignal,\n\t\t\t\t\t);\n\t\t\t\t\tconst collected = await collectImages(response, fetchImpl, clientSignal);\n\t\t\t\t\tif (!Array.isArray(collected)) return failed(model, collected.code, collected.message);\n\t\t\t\t\tconst persisted = await persistImages(resolved.workspace, collected);\n\t\t\t\t\treturn successResult(\n\t\t\t\t\t\t{ version: 1, status: \"succeeded\", files: persisted.files, model, ...usageOf(response) },\n\t\t\t\t\t\tpersisted.images,\n\t\t\t\t\t\tmodelSourceNote,\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\treturn failureFrom(error, clientSignal, model, \"Image generation request failed\");\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tapi.registerTool({\n\t\t\tname: \"image_edit\",\n\t\t\tlabel: \"Image edit\",\n\t\t\tdescription:\n\t\t\t\t\"Edit a previously generated image (workspace-relative path under generated-images/) with a revision prompt; returns the new file path\",\n\t\t\tparameters: IMAGE_EDIT_PARAMETERS,\n\t\t\texecute: async (first: unknown, second?: unknown, third?: unknown) => {\n\t\t\t\tlet input: Record<string, unknown>;\n\t\t\t\ttry {\n\t\t\t\t\tinput = objectInput(first, second);\n\t\t\t\t} catch {\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"Image generation input must be an object\");\n\t\t\t\t}\n\t\t\t\tconst prompt = promptFrom(input.prompt);\n\t\t\t\tif (prompt === undefined)\n\t\t\t\t\treturn failed(\"\", \"invalid_input\", \"prompt must be a non-empty string of at most 4000 characters\");\n\t\t\t\tconst imagePath = typeof input.imagePath === \"string\" ? input.imagePath : undefined;\n\t\t\t\tif (imagePath === undefined || !validImagePath(imagePath))\n\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\"\",\n\t\t\t\t\t\t\"invalid_path\",\n\t\t\t\t\t\t\"imagePath must be a workspace-relative identifier inside the generated-images/ directory (no .. segments)\",\n\t\t\t\t\t);\n\t\t\t\tconst sessionSignal = api.sessionAbortSignal?.();\n\t\t\t\tconst invocationSignal = signalFrom(third);\n\t\t\t\tconst clientSignal = combineSignals([invocationSignal, sessionSignal]);\n\t\t\t\tif (clientSignal?.aborted) return cancelled(\"\");\n\t\t\t\tconst target = resolveTarget(input);\n\t\t\t\tif (!target.ok) return failed(\"\", \"config_missing\", boundedImageMessageV1(target.message));\n\t\t\t\tconst { provider, model, modelSourceNote } = target;\n\t\t\t\ttry {\n\t\t\t\t\tconst resolved = await resolveInvocation(api, provider, fetchImpl, model);\n\t\t\t\t\tif (resolved.status === \"failed\") return resolved.result;\n\t\t\t\t\t// Defense in depth (readonly never registers these tools): the\n\t\t\t\t\t// lexical validImagePath gate cannot see symlinks, so refuse any\n\t\t\t\t\t// candidate whose real path escapes the workspace before a byte\n\t\t\t\t\t// is read and shipped to the provider.\n\t\t\t\t\tconst sandbox = await sandboxedImagePath(resolved.workspace, imagePath);\n\t\t\t\t\tif (sandbox === \"missing\")\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tif (sandbox === \"escape\")\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"invalid_path\",\n\t\t\t\t\t\t\t`imagePath ${imagePath} resolves outside the workspace; only files under the workspace generated-images/ directory can be read`,\n\t\t\t\t\t\t);\n\t\t\t\t\t// Size-check before reading: users can drop their own files\n\t\t\t\t\t// into generated-images/, and the 20 MiB download cap must\n\t\t\t\t\t// not be bypassed by a renamed local file.\n\t\t\t\t\tconst sourceStats = await stat(join(resolved.workspace, imagePath)).catch(() => undefined);\n\t\t\t\t\tif (sourceStats === undefined || !sourceStats.isFile())\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tif (sourceStats.size > IMAGE_GENERATION_MAX_DOWNLOAD_BYTES_V1)\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"response_too_large\",\n\t\t\t\t\t\t\t`image ${imagePath} is ${sourceStats.size} bytes, above the 20 MiB limit`,\n\t\t\t\t\t\t);\n\t\t\t\t\tconst source = await readFile(join(resolved.workspace, imagePath)).catch(() => undefined);\n\t\t\t\t\tif (source === undefined)\n\t\t\t\t\t\treturn failed(\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\"image_not_found\",\n\t\t\t\t\t\t\t`image ${imagePath} was not found under the workspace generated-images/ directory`,\n\t\t\t\t\t\t);\n\t\t\t\t\tconst response = await resolved.client.editImage(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\timage: source,\n\t\t\t\t\t\t\t// The edits endpoint accepts png/jpeg/webp inputs; the\n\t\t\t\t\t\t\t// multipart blob must carry the source's real format\n\t\t\t\t\t\t\t// (a prior url-form download may be JPEG).\n\t\t\t\t\t\t\tmimeType: detectImageFormatV1(source).mimeType,\n\t\t\t\t\t\t\tprompt,\n\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tclientSignal,\n\t\t\t\t\t);\n\t\t\t\t\tconst collected = await collectImages(response, fetchImpl, clientSignal);\n\t\t\t\t\tif (!Array.isArray(collected)) return failed(model, collected.code, collected.message);\n\t\t\t\t\tconst persisted = await persistImages(resolved.workspace, collected);\n\t\t\t\t\treturn successResult(\n\t\t\t\t\t\t{ version: 1, status: \"succeeded\", files: persisted.files, model, ...usageOf(response) },\n\t\t\t\t\t\tpersisted.images,\n\t\t\t\t\t\tmodelSourceNote,\n\t\t\t\t\t);\n\t\t\t\t} catch (error) {\n\t\t\t\t\treturn failureFrom(error, clientSignal, model, \"Image edit request failed\");\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t};\n}\n\nfunction failureFrom(\n\terror: unknown,\n\tsignal: AbortSignal | undefined,\n\tmodel: string,\n\tfallbackMessage: string,\n): ImageGenResultV1 {\n\tif (signal?.aborted) return cancelled(model);\n\tlet message = error instanceof Error ? error.message : fallbackMessage;\n\t// Append (never replace) a proxy hint for opaque network failures: the\n\t// single most common cause on machines that route out through a VPN.\n\tif (/fetch failed|terminated|ENOTFOUND|ECONNREFUSED|ETIMEDOUT|EAI_AGAIN|UND_ERR/u.test(message))\n\t\tmessage = `${message} (if this machine reaches the internet through a proxy, set HTTPS_PROXY/HTTP_PROXY — see the image-generation plugin README)`;\n\treturn failed(model, \"image_request_failed\", boundedImageMessageV1(message));\n}\n"]}
@@ -5,10 +5,8 @@ export interface ImagesClientOptionsV1 {
5
5
  readonly fetchImpl?: typeof fetch;
6
6
  }
7
7
  /**
8
- * Minimal OpenAI-compatible images API client (globals only: fetch, FormData,
9
- * Blob, AbortSignal.timeout/any on Node 22). Error messages carry the status
10
- * and the first 300 characters of the response body only — never the API key
11
- * or request headers.
8
+ * Minimal images API client. Error messages carry the status and the first
9
+ * 300 characters of the response body only — never the API key or headers.
12
10
  */
13
11
  export declare function createImagesClientV1(options: ImagesClientOptionsV1): ImagesClientV1;
14
12
  //# sourceMappingURL=images-client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"images-client.d.ts","sourceRoot":"","sources":["../src/images-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA0B,cAAc,EAAyB,MAAM,YAAY,CAAC;AAKhG,MAAM,WAAW,qBAAqB;IACrC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;CAClC;AAyCD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAiEnF","sourcesContent":["import type { ImagesClientResponseV1, ImagesClientV1, ImagesGeneratedItemV1 } from \"./types.ts\";\n\n/** Per-request budget: image edits with large inputs legitimately take minutes. */\nconst IMAGES_REQUEST_TIMEOUT_MS_V1 = 180_000;\n\nexport interface ImagesClientOptionsV1 {\n\treadonly baseUrl: string;\n\treadonly apiKey: string;\n\treadonly fetchImpl?: typeof fetch;\n}\n\n/** Combine the caller's signal with the fixed request timeout. */\nfunction requestSignal(signal: AbortSignal | undefined): AbortSignal {\n\tconst timeout = AbortSignal.timeout(IMAGES_REQUEST_TIMEOUT_MS_V1);\n\treturn signal === undefined ? timeout : AbortSignal.any([signal, timeout]);\n}\n\nfunction trimTrailingSlash(baseUrl: string): string {\n\treturn baseUrl.replace(/\\/+$/u, \"\");\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n\treturn value !== null && typeof value === \"object\" && !Array.isArray(value);\n}\n\n/**\n * Map the OpenAI images response onto the client's normalized shape:\n * `b64_json`/`b64` stays inline, `url` is left for the capability layer to\n * download; unknown item shapes are dropped rather than guessed at.\n */\nfunction parseResponse(payload: unknown): ImagesClientResponseV1 {\n\tif (!isPlainObject(payload) || !Array.isArray(payload.data))\n\t\tthrow new Error(\"images api response has no data array\");\n\tconst data: ImagesGeneratedItemV1[] = [];\n\tfor (const entry of payload.data) {\n\t\tif (!isPlainObject(entry)) continue;\n\t\tif (typeof entry.b64_json === \"string\") data.push({ b64: entry.b64_json });\n\t\telse if (typeof entry.b64 === \"string\") data.push({ b64: entry.b64 });\n\t\telse if (typeof entry.url === \"string\")\n\t\t\tdata.push({\n\t\t\t\turl: entry.url,\n\t\t\t\t...(typeof entry.mime_type === \"string\" ? { mimeType: entry.mime_type } : {}),\n\t\t\t});\n\t}\n\treturn {\n\t\tdata,\n\t\t...(isPlainObject(payload.usage) ? { usage: payload.usage as Record<string, unknown> } : {}),\n\t};\n}\n\n/**\n * Minimal OpenAI-compatible images API client (globals only: fetch, FormData,\n * Blob, AbortSignal.timeout/any on Node 22). Error messages carry the status\n * and the first 300 characters of the response body only — never the API key\n * or request headers.\n */\nexport function createImagesClientV1(options: ImagesClientOptionsV1): ImagesClientV1 {\n\tconst fetchImpl: typeof fetch = options.fetchImpl ?? fetch;\n\tconst base = trimTrailingSlash(options.baseUrl);\n\n\tasync function post(path: string, init: RequestInit, signal: AbortSignal | undefined): Promise<unknown> {\n\t\tconst response = await fetchImpl(`${base}${path}`, { ...init, signal: requestSignal(signal) });\n\t\tif (!response.ok) {\n\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\tthrow new Error(`images api ${path} failed with status ${response.status}: ${body.slice(0, 300)}`);\n\t\t}\n\t\treturn response.json();\n\t}\n\n\treturn {\n\t\tgenerateImages: async (request, signal) =>\n\t\t\tparseResponse(\n\t\t\t\tawait post(\n\t\t\t\t\t\"/images/generations\",\n\t\t\t\t\t{\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: { \"content-type\": \"application/json\", authorization: `Bearer ${options.apiKey}` },\n\t\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\t\tmodel: request.model,\n\t\t\t\t\t\t\tprompt: request.prompt,\n\t\t\t\t\t\t\tn: request.n,\n\t\t\t\t\t\t\t...(request.size === undefined ? {} : { size: request.size }),\n\t\t\t\t\t\t\t...(request.quality === undefined ? {} : { quality: request.quality }),\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t),\n\t\teditImage: async (request, signal) => {\n\t\t\t// Native FormData + Blob: fetch sets the multipart Content-Type with\n\t\t\t// its own boundary, so it must not be set manually here. The part\n\t\t\t// filename follows the declared mime so the payload is not named\n\t\t\t// .png while carrying JPEG bytes (providers sniff either way).\n\t\t\tconst extension =\n\t\t\t\trequest.mimeType === \"image/jpeg\"\n\t\t\t\t\t? \"jpg\"\n\t\t\t\t\t: request.mimeType === \"image/webp\"\n\t\t\t\t\t\t? \"webp\"\n\t\t\t\t\t\t: request.mimeType === \"image/gif\"\n\t\t\t\t\t\t\t? \"gif\"\n\t\t\t\t\t\t\t: \"png\";\n\t\t\tconst form = new FormData();\n\t\t\tform.set(\"model\", request.model);\n\t\t\tform.set(\"prompt\", request.prompt);\n\t\t\tif (request.size !== undefined) form.set(\"size\", request.size);\n\t\t\tif (request.quality !== undefined) form.set(\"quality\", request.quality);\n\t\t\tform.set(\"image\", new Blob([request.image], { type: request.mimeType }), `input.${extension}`);\n\t\t\treturn parseResponse(\n\t\t\t\tawait post(\n\t\t\t\t\t\"/images/edits\",\n\t\t\t\t\t{\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t// No content-type header: the boundary is owned by fetch.\n\t\t\t\t\t\theaders: { authorization: `Bearer ${options.apiKey}` },\n\t\t\t\t\t\tbody: form,\n\t\t\t\t\t},\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t);\n\t\t},\n\t};\n}\n"]}
1
+ {"version":3,"file":"images-client.d.ts","sourceRoot":"","sources":["../src/images-client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAA0B,cAAc,EAAyB,MAAM,YAAY,CAAC;AAKhG,MAAM,WAAW,qBAAqB;IACrC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;CAClC;AAyCD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAwFnF","sourcesContent":["import { FormData as UndiciFormData, Response as UndiciResponse } from \"undici\";\nimport type { ImagesClientResponseV1, ImagesClientV1, ImagesGeneratedItemV1 } from \"./types.ts\";\n\n/** Per-request budget: image edits with large inputs legitimately take minutes. */\nconst IMAGES_REQUEST_TIMEOUT_MS_V1 = 180_000;\n\nexport interface ImagesClientOptionsV1 {\n\treadonly baseUrl: string;\n\treadonly apiKey: string;\n\treadonly fetchImpl?: typeof fetch;\n}\n\n/** Combine the caller's signal with the fixed request timeout. */\nfunction requestSignal(signal: AbortSignal | undefined): AbortSignal {\n\tconst timeout = AbortSignal.timeout(IMAGES_REQUEST_TIMEOUT_MS_V1);\n\treturn signal === undefined ? timeout : AbortSignal.any([signal, timeout]);\n}\n\nfunction trimTrailingSlash(baseUrl: string): string {\n\treturn baseUrl.replace(/\\/+$/u, \"\");\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n\treturn value !== null && typeof value === \"object\" && !Array.isArray(value);\n}\n\n/**\n * Map the OpenAI images response onto the client's normalized shape:\n * `b64_json`/`b64` stays inline, `url` is left for the capability layer to\n * download; unknown item shapes are dropped rather than guessed at.\n */\nfunction parseResponse(payload: unknown): ImagesClientResponseV1 {\n\tif (!isPlainObject(payload) || !Array.isArray(payload.data))\n\t\tthrow new Error(\"images api response has no data array\");\n\tconst data: ImagesGeneratedItemV1[] = [];\n\tfor (const entry of payload.data) {\n\t\tif (!isPlainObject(entry)) continue;\n\t\tif (typeof entry.b64_json === \"string\") data.push({ b64: entry.b64_json });\n\t\telse if (typeof entry.b64 === \"string\") data.push({ b64: entry.b64 });\n\t\telse if (typeof entry.url === \"string\")\n\t\t\tdata.push({\n\t\t\t\turl: entry.url,\n\t\t\t\t...(typeof entry.mime_type === \"string\" ? { mimeType: entry.mime_type } : {}),\n\t\t\t});\n\t}\n\treturn {\n\t\tdata,\n\t\t...(isPlainObject(payload.usage) ? { usage: payload.usage as Record<string, unknown> } : {}),\n\t};\n}\n\n/**\n * Minimal images API client. Error messages carry the status and the first\n * 300 characters of the response body only — never the API key or headers.\n */\nexport function createImagesClientV1(options: ImagesClientOptionsV1): ImagesClientV1 {\n\tconst fetchImpl: typeof fetch = options.fetchImpl ?? fetch;\n\tconst base = trimTrailingSlash(options.baseUrl);\n\n\tasync function post(path: string, init: RequestInit, signal: AbortSignal | undefined): Promise<unknown> {\n\t\tconst response = await fetchImpl(`${base}${path}`, { ...init, signal: requestSignal(signal) });\n\t\tif (!response.ok) {\n\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\tthrow new Error(`images api ${path} failed with status ${response.status}: ${body.slice(0, 300)}`);\n\t\t}\n\t\treturn response.json();\n\t}\n\n\treturn {\n\t\tgenerateImages: async (request, signal) =>\n\t\t\tparseResponse(\n\t\t\t\tawait post(\n\t\t\t\t\t\"/images/generations\",\n\t\t\t\t\t{\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: { \"content-type\": \"application/json\", authorization: `Bearer ${options.apiKey}` },\n\t\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\t\tmodel: request.model,\n\t\t\t\t\t\t\tprompt: request.prompt,\n\t\t\t\t\t\t\tn: request.n,\n\t\t\t\t\t\t\t...(request.size === undefined ? {} : { size: request.size }),\n\t\t\t\t\t\t\t...(request.quality === undefined ? {} : { quality: request.quality }),\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t),\n\t\teditImage: async (request, signal) => {\n\t\t\tconst headers = { authorization: `Bearer ${options.apiKey}` };\n\t\t\tif (request.model === \"grok-imagine-image\" || request.model.startsWith(\"grok-imagine-image-\")) {\n\t\t\t\treturn parseResponse(\n\t\t\t\t\tawait post(\n\t\t\t\t\t\t\"/images/edits\",\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\theaders: { ...headers, \"content-type\": \"application/json\" },\n\t\t\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\t\t\tmodel: request.model,\n\t\t\t\t\t\t\t\tprompt: request.prompt,\n\t\t\t\t\t\t\t\timage: {\n\t\t\t\t\t\t\t\t\turl: `data:${request.mimeType};base64,${Buffer.from(request.image).toString(\"base64\")}`,\n\t\t\t\t\t\t\t\t\ttype: \"image_url\",\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t...(request.size === undefined ? {} : { size: request.size }),\n\t\t\t\t\t\t\t\t...(request.quality === undefined ? {} : { quality: request.quality }),\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tsignal,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t}\n\t\t\t// The package's undici FormData and Response share fetch's branding.\n\t\t\t// Encode to a Blob so both the host's native fetch and undici fetch\n\t\t\t// accept the body; carry its generated boundary in Content-Type.\n\t\t\tconst extension =\n\t\t\t\trequest.mimeType === \"image/jpeg\"\n\t\t\t\t\t? \"jpg\"\n\t\t\t\t\t: request.mimeType === \"image/webp\"\n\t\t\t\t\t\t? \"webp\"\n\t\t\t\t\t\t: request.mimeType === \"image/gif\"\n\t\t\t\t\t\t\t? \"gif\"\n\t\t\t\t\t\t\t: \"png\";\n\t\t\tconst form = new UndiciFormData();\n\t\t\tform.set(\"model\", request.model);\n\t\t\tform.set(\"prompt\", request.prompt);\n\t\t\tif (request.size !== undefined) form.set(\"size\", request.size);\n\t\t\tif (request.quality !== undefined) form.set(\"quality\", request.quality);\n\t\t\tform.set(\"image\", new Blob([request.image], { type: request.mimeType }), `input.${extension}`);\n\t\t\tconst body = await new UndiciResponse(form).blob();\n\t\t\treturn parseResponse(\n\t\t\t\tawait post(\n\t\t\t\t\t\"/images/edits\",\n\t\t\t\t\t{\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t// The encoded Blob owns the boundary carried by this header.\n\t\t\t\t\t\theaders: { ...headers, \"content-type\": body.type },\n\t\t\t\t\t\tbody,\n\t\t\t\t\t},\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t);\n\t\t},\n\t};\n}\n"]}
@@ -1,3 +1,4 @@
1
+ import { FormData as UndiciFormData, Response as UndiciResponse } from "undici";
1
2
  /** Per-request budget: image edits with large inputs legitimately take minutes. */
2
3
  const IMAGES_REQUEST_TIMEOUT_MS_V1 = 180_000;
3
4
  /** Combine the caller's signal with the fixed request timeout. */
@@ -39,10 +40,8 @@ function parseResponse(payload) {
39
40
  };
40
41
  }
41
42
  /**
42
- * Minimal OpenAI-compatible images API client (globals only: fetch, FormData,
43
- * Blob, AbortSignal.timeout/any on Node 22). Error messages carry the status
44
- * and the first 300 characters of the response body only — never the API key
45
- * or request headers.
43
+ * Minimal images API client. Error messages carry the status and the first
44
+ * 300 characters of the response body only — never the API key or headers.
46
45
  */
47
46
  export function createImagesClientV1(options) {
48
47
  const fetchImpl = options.fetchImpl ?? fetch;
@@ -68,10 +67,26 @@ export function createImagesClientV1(options) {
68
67
  }),
69
68
  }, signal)),
70
69
  editImage: async (request, signal) => {
71
- // Native FormData + Blob: fetch sets the multipart Content-Type with
72
- // its own boundary, so it must not be set manually here. The part
73
- // filename follows the declared mime so the payload is not named
74
- // .png while carrying JPEG bytes (providers sniff either way).
70
+ const headers = { authorization: `Bearer ${options.apiKey}` };
71
+ if (request.model === "grok-imagine-image" || request.model.startsWith("grok-imagine-image-")) {
72
+ return parseResponse(await post("/images/edits", {
73
+ method: "POST",
74
+ headers: { ...headers, "content-type": "application/json" },
75
+ body: JSON.stringify({
76
+ model: request.model,
77
+ prompt: request.prompt,
78
+ image: {
79
+ url: `data:${request.mimeType};base64,${Buffer.from(request.image).toString("base64")}`,
80
+ type: "image_url",
81
+ },
82
+ ...(request.size === undefined ? {} : { size: request.size }),
83
+ ...(request.quality === undefined ? {} : { quality: request.quality }),
84
+ }),
85
+ }, signal));
86
+ }
87
+ // The package's undici FormData and Response share fetch's branding.
88
+ // Encode to a Blob so both the host's native fetch and undici fetch
89
+ // accept the body; carry its generated boundary in Content-Type.
75
90
  const extension = request.mimeType === "image/jpeg"
76
91
  ? "jpg"
77
92
  : request.mimeType === "image/webp"
@@ -79,7 +94,7 @@ export function createImagesClientV1(options) {
79
94
  : request.mimeType === "image/gif"
80
95
  ? "gif"
81
96
  : "png";
82
- const form = new FormData();
97
+ const form = new UndiciFormData();
83
98
  form.set("model", request.model);
84
99
  form.set("prompt", request.prompt);
85
100
  if (request.size !== undefined)
@@ -87,11 +102,12 @@ export function createImagesClientV1(options) {
87
102
  if (request.quality !== undefined)
88
103
  form.set("quality", request.quality);
89
104
  form.set("image", new Blob([request.image], { type: request.mimeType }), `input.${extension}`);
105
+ const body = await new UndiciResponse(form).blob();
90
106
  return parseResponse(await post("/images/edits", {
91
107
  method: "POST",
92
- // No content-type header: the boundary is owned by fetch.
93
- headers: { authorization: `Bearer ${options.apiKey}` },
94
- body: form,
108
+ // The encoded Blob owns the boundary carried by this header.
109
+ headers: { ...headers, "content-type": body.type },
110
+ body,
95
111
  }, signal));
96
112
  },
97
113
  };
@@ -1 +1 @@
1
- {"version":3,"file":"images-client.js","sourceRoot":"","sources":["../src/images-client.ts"],"names":[],"mappings":"AAEA,mFAAmF;AACnF,MAAM,4BAA4B,GAAG,OAAO,CAAC;AAQ7C,kEAAkE;AAClE,SAAS,aAAa,CAAC,MAA+B,EAAe;IACpE,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,4BAA4B,CAAC,CAAC;IAClE,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAAA,CAC3E;AAED,SAAS,iBAAiB,CAAC,OAAe,EAAU;IACnD,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AAAA,CACpC;AAED,SAAS,aAAa,CAAC,KAAc,EAAoC;IACxE,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAAA,CAC5E;AAED;;;;GAIG;AACH,SAAS,aAAa,CAAC,OAAgB,EAA0B;IAChE,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC;QAC1D,MAAM,I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esContent":["import type { ImagesClientResponseV1, ImagesClientV1, ImagesGeneratedItemV1 } from \"./types.ts\";\n\n/** Per-request budget: image edits with large inputs legitimately take minutes. */\nconst IMAGES_REQUEST_TIMEOUT_MS_V1 = 180_000;\n\nexport interface ImagesClientOptionsV1 {\n\treadonly baseUrl: string;\n\treadonly apiKey: string;\n\treadonly fetchImpl?: typeof fetch;\n}\n\n/** Combine the caller's signal with the fixed request timeout. */\nfunction requestSignal(signal: AbortSignal | undefined): AbortSignal {\n\tconst timeout = AbortSignal.timeout(IMAGES_REQUEST_TIMEOUT_MS_V1);\n\treturn signal === undefined ? timeout : AbortSignal.any([signal, timeout]);\n}\n\nfunction trimTrailingSlash(baseUrl: string): string {\n\treturn baseUrl.replace(/\\/+$/u, \"\");\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n\treturn value !== null && typeof value === \"object\" && !Array.isArray(value);\n}\n\n/**\n * Map the OpenAI images response onto the client's normalized shape:\n * `b64_json`/`b64` stays inline, `url` is left for the capability layer to\n * download; unknown item shapes are dropped rather than guessed at.\n */\nfunction parseResponse(payload: unknown): ImagesClientResponseV1 {\n\tif (!isPlainObject(payload) || !Array.isArray(payload.data))\n\t\tthrow new Error(\"images api response has no data array\");\n\tconst data: ImagesGeneratedItemV1[] = [];\n\tfor (const entry of payload.data) {\n\t\tif (!isPlainObject(entry)) continue;\n\t\tif (typeof entry.b64_json === \"string\") data.push({ b64: entry.b64_json });\n\t\telse if (typeof entry.b64 === \"string\") data.push({ b64: entry.b64 });\n\t\telse if (typeof entry.url === \"string\")\n\t\t\tdata.push({\n\t\t\t\turl: entry.url,\n\t\t\t\t...(typeof entry.mime_type === \"string\" ? { mimeType: entry.mime_type } : {}),\n\t\t\t});\n\t}\n\treturn {\n\t\tdata,\n\t\t...(isPlainObject(payload.usage) ? { usage: payload.usage as Record<string, unknown> } : {}),\n\t};\n}\n\n/**\n * Minimal OpenAI-compatible images API client (globals only: fetch, FormData,\n * Blob, AbortSignal.timeout/any on Node 22). Error messages carry the status\n * and the first 300 characters of the response body only — never the API key\n * or request headers.\n */\nexport function createImagesClientV1(options: ImagesClientOptionsV1): ImagesClientV1 {\n\tconst fetchImpl: typeof fetch = options.fetchImpl ?? fetch;\n\tconst base = trimTrailingSlash(options.baseUrl);\n\n\tasync function post(path: string, init: RequestInit, signal: AbortSignal | undefined): Promise<unknown> {\n\t\tconst response = await fetchImpl(`${base}${path}`, { ...init, signal: requestSignal(signal) });\n\t\tif (!response.ok) {\n\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\tthrow new Error(`images api ${path} failed with status ${response.status}: ${body.slice(0, 300)}`);\n\t\t}\n\t\treturn response.json();\n\t}\n\n\treturn {\n\t\tgenerateImages: async (request, signal) =>\n\t\t\tparseResponse(\n\t\t\t\tawait post(\n\t\t\t\t\t\"/images/generations\",\n\t\t\t\t\t{\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: { \"content-type\": \"application/json\", authorization: `Bearer ${options.apiKey}` },\n\t\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\t\tmodel: request.model,\n\t\t\t\t\t\t\tprompt: request.prompt,\n\t\t\t\t\t\t\tn: request.n,\n\t\t\t\t\t\t\t...(request.size === undefined ? {} : { size: request.size }),\n\t\t\t\t\t\t\t...(request.quality === undefined ? {} : { quality: request.quality }),\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t),\n\t\teditImage: async (request, signal) => {\n\t\t\t// Native FormData + Blob: fetch sets the multipart Content-Type with\n\t\t\t// its own boundary, so it must not be set manually here. The part\n\t\t\t// filename follows the declared mime so the payload is not named\n\t\t\t// .png while carrying JPEG bytes (providers sniff either way).\n\t\t\tconst extension =\n\t\t\t\trequest.mimeType === \"image/jpeg\"\n\t\t\t\t\t? \"jpg\"\n\t\t\t\t\t: request.mimeType === \"image/webp\"\n\t\t\t\t\t\t? \"webp\"\n\t\t\t\t\t\t: request.mimeType === \"image/gif\"\n\t\t\t\t\t\t\t? \"gif\"\n\t\t\t\t\t\t\t: \"png\";\n\t\t\tconst form = new FormData();\n\t\t\tform.set(\"model\", request.model);\n\t\t\tform.set(\"prompt\", request.prompt);\n\t\t\tif (request.size !== undefined) form.set(\"size\", request.size);\n\t\t\tif (request.quality !== undefined) form.set(\"quality\", request.quality);\n\t\t\tform.set(\"image\", new Blob([request.image], { type: request.mimeType }), `input.${extension}`);\n\t\t\treturn parseResponse(\n\t\t\t\tawait post(\n\t\t\t\t\t\"/images/edits\",\n\t\t\t\t\t{\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t// No content-type header: the boundary is owned by fetch.\n\t\t\t\t\t\theaders: { authorization: `Bearer ${options.apiKey}` },\n\t\t\t\t\t\tbody: form,\n\t\t\t\t\t},\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t);\n\t\t},\n\t};\n}\n"]}
1
+ {"version":3,"file":"images-client.js","sourceRoot":"","sources":["../src/images-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,QAAQ,CAAC;AAGhF,mFAAmF;AACnF,MAAM,4BAA4B,GAAG,OAAO,CAAC;AAQ7C,kEAAkE;AAClE,SAAS,aAAa,CAAC,MAA+B,EAAe;IACpE,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,4BAA4B,CAAC,CAAC;IAClE,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAAA,CAC3E;AAED,SAAS,iBAAiB,CAAC,OAAe,EAAU;IACnD,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AAAA,CACpC;AAED,SAAS,aAAa,CAAC,KAAc,EAAoC;IACxE,OAAO,KAAK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,IAAI,EAAE;gBAClD,IAAI;aACJ,EACD,MAAM,CACN,CACD,CAAC;QAAA,CACF;KACD,CAAC;AAAA,CACF","sourcesContent":["import { FormData as UndiciFormData, Response as UndiciResponse } from \"undici\";\nimport type { ImagesClientResponseV1, ImagesClientV1, ImagesGeneratedItemV1 } from \"./types.ts\";\n\n/** Per-request budget: image edits with large inputs legitimately take minutes. */\nconst IMAGES_REQUEST_TIMEOUT_MS_V1 = 180_000;\n\nexport interface ImagesClientOptionsV1 {\n\treadonly baseUrl: string;\n\treadonly apiKey: string;\n\treadonly fetchImpl?: typeof fetch;\n}\n\n/** Combine the caller's signal with the fixed request timeout. */\nfunction requestSignal(signal: AbortSignal | undefined): AbortSignal {\n\tconst timeout = AbortSignal.timeout(IMAGES_REQUEST_TIMEOUT_MS_V1);\n\treturn signal === undefined ? timeout : AbortSignal.any([signal, timeout]);\n}\n\nfunction trimTrailingSlash(baseUrl: string): string {\n\treturn baseUrl.replace(/\\/+$/u, \"\");\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n\treturn value !== null && typeof value === \"object\" && !Array.isArray(value);\n}\n\n/**\n * Map the OpenAI images response onto the client's normalized shape:\n * `b64_json`/`b64` stays inline, `url` is left for the capability layer to\n * download; unknown item shapes are dropped rather than guessed at.\n */\nfunction parseResponse(payload: unknown): ImagesClientResponseV1 {\n\tif (!isPlainObject(payload) || !Array.isArray(payload.data))\n\t\tthrow new Error(\"images api response has no data array\");\n\tconst data: ImagesGeneratedItemV1[] = [];\n\tfor (const entry of payload.data) {\n\t\tif (!isPlainObject(entry)) continue;\n\t\tif (typeof entry.b64_json === \"string\") data.push({ b64: entry.b64_json });\n\t\telse if (typeof entry.b64 === \"string\") data.push({ b64: entry.b64 });\n\t\telse if (typeof entry.url === \"string\")\n\t\t\tdata.push({\n\t\t\t\turl: entry.url,\n\t\t\t\t...(typeof entry.mime_type === \"string\" ? { mimeType: entry.mime_type } : {}),\n\t\t\t});\n\t}\n\treturn {\n\t\tdata,\n\t\t...(isPlainObject(payload.usage) ? { usage: payload.usage as Record<string, unknown> } : {}),\n\t};\n}\n\n/**\n * Minimal images API client. Error messages carry the status and the first\n * 300 characters of the response body only — never the API key or headers.\n */\nexport function createImagesClientV1(options: ImagesClientOptionsV1): ImagesClientV1 {\n\tconst fetchImpl: typeof fetch = options.fetchImpl ?? fetch;\n\tconst base = trimTrailingSlash(options.baseUrl);\n\n\tasync function post(path: string, init: RequestInit, signal: AbortSignal | undefined): Promise<unknown> {\n\t\tconst response = await fetchImpl(`${base}${path}`, { ...init, signal: requestSignal(signal) });\n\t\tif (!response.ok) {\n\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\tthrow new Error(`images api ${path} failed with status ${response.status}: ${body.slice(0, 300)}`);\n\t\t}\n\t\treturn response.json();\n\t}\n\n\treturn {\n\t\tgenerateImages: async (request, signal) =>\n\t\t\tparseResponse(\n\t\t\t\tawait post(\n\t\t\t\t\t\"/images/generations\",\n\t\t\t\t\t{\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: { \"content-type\": \"application/json\", authorization: `Bearer ${options.apiKey}` },\n\t\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\t\tmodel: request.model,\n\t\t\t\t\t\t\tprompt: request.prompt,\n\t\t\t\t\t\t\tn: request.n,\n\t\t\t\t\t\t\t...(request.size === undefined ? {} : { size: request.size }),\n\t\t\t\t\t\t\t...(request.quality === undefined ? {} : { quality: request.quality }),\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t),\n\t\teditImage: async (request, signal) => {\n\t\t\tconst headers = { authorization: `Bearer ${options.apiKey}` };\n\t\t\tif (request.model === \"grok-imagine-image\" || request.model.startsWith(\"grok-imagine-image-\")) {\n\t\t\t\treturn parseResponse(\n\t\t\t\t\tawait post(\n\t\t\t\t\t\t\"/images/edits\",\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\theaders: { ...headers, \"content-type\": \"application/json\" },\n\t\t\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\t\t\tmodel: request.model,\n\t\t\t\t\t\t\t\tprompt: request.prompt,\n\t\t\t\t\t\t\t\timage: {\n\t\t\t\t\t\t\t\t\turl: `data:${request.mimeType};base64,${Buffer.from(request.image).toString(\"base64\")}`,\n\t\t\t\t\t\t\t\t\ttype: \"image_url\",\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t...(request.size === undefined ? {} : { size: request.size }),\n\t\t\t\t\t\t\t\t...(request.quality === undefined ? {} : { quality: request.quality }),\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tsignal,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t}\n\t\t\t// The package's undici FormData and Response share fetch's branding.\n\t\t\t// Encode to a Blob so both the host's native fetch and undici fetch\n\t\t\t// accept the body; carry its generated boundary in Content-Type.\n\t\t\tconst extension =\n\t\t\t\trequest.mimeType === \"image/jpeg\"\n\t\t\t\t\t? \"jpg\"\n\t\t\t\t\t: request.mimeType === \"image/webp\"\n\t\t\t\t\t\t? \"webp\"\n\t\t\t\t\t\t: request.mimeType === \"image/gif\"\n\t\t\t\t\t\t\t? \"gif\"\n\t\t\t\t\t\t\t: \"png\";\n\t\t\tconst form = new UndiciFormData();\n\t\t\tform.set(\"model\", request.model);\n\t\t\tform.set(\"prompt\", request.prompt);\n\t\t\tif (request.size !== undefined) form.set(\"size\", request.size);\n\t\t\tif (request.quality !== undefined) form.set(\"quality\", request.quality);\n\t\t\tform.set(\"image\", new Blob([request.image], { type: request.mimeType }), `input.${extension}`);\n\t\t\tconst body = await new UndiciResponse(form).blob();\n\t\t\treturn parseResponse(\n\t\t\t\tawait post(\n\t\t\t\t\t\"/images/edits\",\n\t\t\t\t\t{\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t// The encoded Blob owns the boundary carried by this header.\n\t\t\t\t\t\theaders: { ...headers, \"content-type\": body.type },\n\t\t\t\t\t\tbody,\n\t\t\t\t\t},\n\t\t\t\t\tsignal,\n\t\t\t\t),\n\t\t\t);\n\t\t},\n\t};\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-agent-forge/plugin-image-generation",
3
- "version": "0.88.10",
3
+ "version": "0.88.12",
4
4
  "description": "Image generation capability plugin for Agent Forge (image_generate / image_edit tools)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,7 +22,8 @@
22
22
  "clean": "shx rm -rf dist",
23
23
  "build": "tsgo -p tsconfig.build.json",
24
24
  "test": "vitest --run",
25
- "prepublishOnly": "npm run clean && npm run build"
25
+ "prepublishOnly": "npm run clean && npm run build",
26
+ "test:coverage": "vitest --run --coverage"
26
27
  },
27
28
  "keywords": [
28
29
  "agent-forge",
@@ -46,11 +47,11 @@
46
47
  },
47
48
  "dependencies": {
48
49
  "typebox": "1.3.27",
49
- "undici": "8.9.0",
50
- "@ai-agent-forge/plugin-sdk": "^0.88.10"
50
+ "undici": "8.10.2",
51
+ "@ai-agent-forge/plugin-sdk": "^0.88.12"
51
52
  },
52
53
  "devDependencies": {
53
- "shx": "0.4.0",
54
+ "shx": "0.3.4",
54
55
  "vitest": "4.1.11"
55
56
  }
56
57
  }
package/plugin.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "agent-forge.plugin.image-generation",
3
- "version": "0.88.10",
3
+ "version": "0.88.12",
4
4
  "apiVersion": "1-draft",
5
5
  "entry": "./dist/entry.js",
6
6
  "hostVersion": ">=0.84.4",