From e9df9cac06b491d02976431657259105be9b7d16 Mon Sep 17 00:00:00 2001 From: admin <572701190@qq.com> Date: Sat, 9 May 2026 18:47:19 +0800 Subject: [PATCH] finish api and docs validation fixes --- ...修改-Agent.md => API图片绘制及修改-Agent.md | 16 ++++++++++-- README.md | 2 +- server/index.ts | 26 ++++++++++++++++++- 迁移部署说明.md | 6 ++--- 4 files changed, 43 insertions(+), 7 deletions(-) rename API图片修改-Agent.md => API图片绘制及修改-Agent.md (91%) diff --git a/API图片修改-Agent.md b/API图片绘制及修改-Agent.md similarity index 91% rename from API图片修改-Agent.md rename to API图片绘制及修改-Agent.md index c77ff68..b9d47e2 100644 --- a/API图片修改-Agent.md +++ b/API图片绘制及修改-Agent.md @@ -1,6 +1,6 @@ -# API 图片修改 Agent 调用说明 +# API 图片绘制及修改 Agent 调用说明 -本服务提供一个本地 HTTP API,用于让其他 Agent、脚本或自动化系统上传已有图片/文档,并按文字指令生成或修改图片。 +本服务提供一个本地 HTTP API,用于让其他 Agent、脚本或自动化系统按文字指令绘制图片,或上传已有图片/文档并进一步修改、分析。 默认地址: @@ -120,6 +120,18 @@ curl -X POST http://localhost:3002/api/edit-image \ 调用方可以直接使用 `images[0].dataUrl` 预览图片,或把 `images[0].data` 解码保存为 PNG 文件。 +## 纯文字绘制图片 + +不上传图片时,直接用 `prompt` 生成新图片: + +```bash +curl -X POST http://localhost:3002/api/generate \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer YOUR_LONG_RANDOM_API_TOKEN" \ + -H "x-gemini-api-key: YOUR_GEMINI_API_KEY" \ + -d "{\"prompt\":\"画一个蓝天白云下的极简风景插画\",\"imageSize\":\"1K\",\"aspectRatio\":\"1:1\"}" +``` + ## JSON/Base64 方式修改图片 如果 Agent 已经拿到了图片 base64,可以不用 multipart: diff --git a/README.md b/README.md index 9a6198d..d835a14 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ curl -X POST http://localhost:3002/api/analyze-document \ API auth is required by default. For local-only development, you can set `API_AUTH_DISABLED=true`, but do not use that on a LAN or server. -For Agent-facing image editing instructions, see `API图片修改-Agent.md`. +For Agent-facing image generation and editing instructions, see `API图片绘制及修改-Agent.md`. ## Docker Compose Deploy diff --git a/server/index.ts b/server/index.ts index f9f2d94..4f69687 100644 --- a/server/index.ts +++ b/server/index.ts @@ -198,7 +198,7 @@ function uploadedFileToPart(file: Express.Multer.File): GeminiPart { return { inlineData: { data: file.buffer.toString('base64'), - mimeType: file.mimetype || 'application/octet-stream', + mimeType: inferMimeType(file), }, }; } @@ -219,6 +219,30 @@ function normalizeTextInput(value: unknown) { return value == null ? '' : String(value); } +function inferMimeType(file: Express.Multer.File) { + if (file.mimetype && file.mimetype !== 'application/octet-stream') { + return file.mimetype; + } + + const ext = path.extname(file.originalname || '').toLowerCase(); + const mimeByExt: Record = { + '.png': 'image/png', + '.jpg': 'image/jpeg', + '.jpeg': 'image/jpeg', + '.webp': 'image/webp', + '.gif': 'image/gif', + '.pdf': 'application/pdf', + '.txt': 'text/plain', + '.md': 'text/plain', + '.csv': 'text/csv', + '.json': 'application/json', + '.html': 'text/html', + '.htm': 'text/html', + }; + + return mimeByExt[ext] || 'application/octet-stream'; +} + async function buildParts(body: GenerateRequest, files: Express.Multer.File[] = []) { const prompt = normalizeTextInput(body.prompt || body.instruction).trim(); if (!prompt) { diff --git a/迁移部署说明.md b/迁移部署说明.md index 7af30c0..be6e3ef 100644 --- a/迁移部署说明.md +++ b/迁移部署说明.md @@ -83,13 +83,13 @@ docker compose -f docker_compose.yaml up -d --build --force-recreate ```yaml ports: - "4001:3000" - - "4002:3002" + - "4000:3002" ``` 修改后访问地址变为: - 前端页面:`http://目标机器IP:4001` -- API:`http://目标机器IP:4002` +- API:`http://目标机器IP:4000` 如果修改了前端宿主机端口,也建议同步修改 `.env.local`: @@ -184,7 +184,7 @@ APP_URL="http://192.168.31.5:3000" ```yaml ports: - "4001:3000" - - "4002:3002" + - "4000:3002" ``` 容器内端口右侧保持不变。