[P0/严重] Ollama Adapter 将公网 URL 图片当作 base64 字符串直接传递 #2

Closed
opened 2026-07-21 21:55:34 +08:00 by thzxx · 1 comment
Owner

问题类型

缺陷 / 严重 / LLM 适配器

文件位置

electron/harness/adapters/ollama.adapter.ts

问题描述

当用户上传图片或 LLM 调用涉及图片消息时,MetonaMessage.images 中可能存储的是 http(s) URL。
当前 Ollama Adapter 的多模态处理直接将 URL 当作 base64 字符串塞入 images: [url] 字段传给 Ollama。
Ollama 无法识别该格式,会抛出 base64 解码错误或返回空响应。

影响

  • Ollama 用户上传任何图片都会导致请求失败
  • 多模态能力完全不可用

建议修复

参考 Agnes AI Adapter 实现:

  1. 检测 image 是否为 http(s) URL
  2. 若是 URL,先用 fetch 下载为 Buffer,再转 base64 dataURL
  3. 若已是 dataURL 或 base64,直接使用
async function resolveImageToBase64(image: string): Promise<string> {
  if (image.startsWith('http')) {
    const res = await fetch(image);
    const buf = Buffer.from(await res.arrayBuffer());
    return `data:${res.headers.get('content-type') ?? 'image/png'};base64,${buf.toString('base64')}`;
  }
  return image;
}
## 问题类型 缺陷 / 严重 / LLM 适配器 ## 文件位置 `electron/harness/adapters/ollama.adapter.ts` ## 问题描述 当用户上传图片或 LLM 调用涉及图片消息时,MetonaMessage.images 中可能存储的是 http(s) URL。 当前 Ollama Adapter 的多模态处理直接将 URL 当作 base64 字符串塞入 `images: [url]` 字段传给 Ollama。 Ollama 无法识别该格式,会抛出 base64 解码错误或返回空响应。 ## 影响 - Ollama 用户上传任何图片都会导致请求失败 - 多模态能力完全不可用 ## 建议修复 参考 Agnes AI Adapter 实现: 1. 检测 image 是否为 http(s) URL 2. 若是 URL,先用 fetch 下载为 Buffer,再转 base64 dataURL 3. 若已是 dataURL 或 base64,直接使用 ```ts async function resolveImageToBase64(image: string): Promise<string> { if (image.startsWith('http')) { const res = await fetch(image); const buf = Buffer.from(await res.arrayBuffer()); return `data:${res.headers.get('content-type') ?? 'image/png'};base64,${buf.toString('base64')}`; } return image; } ```
thzxx added the ????LLM??? labels 2026-07-21 21:55:34 +08:00
Author
Owner

修复说明

文件: electron/harness/adapters/ollama.adapter.ts

问题: Ollama Adapter 的 toNativeRequest 将 http(s) URL 图片直接当作 base64 字符串传给 Ollama API,导致 base64 解码错误,多模态功能不可用。

修复方案:

  1. toNativeRequest 改为 async,支持异步图片下载
  2. 新增 resolveImageToBase64(url) 方法:检测 http(s) URL → fetch 下载 → 转纯 base64(不含 data: 前缀)
  3. images 处理从 .map() 改为 for 循环(map 回调无法 await)
  4. 下载失败时返回空字符串,不阻断整个请求

验证: tsc --noEmit 类型检查通过。

## 修复说明 **文件**: `electron/harness/adapters/ollama.adapter.ts` **问题**: Ollama Adapter 的 `toNativeRequest` 将 http(s) URL 图片直接当作 base64 字符串传给 Ollama API,导致 base64 解码错误,多模态功能不可用。 **修复方案**: 1. `toNativeRequest` 改为 `async`,支持异步图片下载 2. 新增 `resolveImageToBase64(url)` 方法:检测 http(s) URL → fetch 下载 → 转纯 base64(不含 `data:` 前缀) 3. images 处理从 `.map()` 改为 `for` 循环(map 回调无法 await) 4. 下载失败时返回空字符串,不阻断整个请求 **验证**: `tsc --noEmit` 类型检查通过。
thzxx closed this issue 2026-07-22 09:14:46 +08:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: MetonaTeam/metona-ai-desktop#2