问题:shared/openai-format.ts 对所有 OpenAI 兼容 Adapter 统一处理 多模态图片 → content 数组。但 DeepSeek 不支持多模态,不应发送图片。 修复: - shared/openai-format.ts: 移除图片处理逻辑(由各 Provider 自行决定) - AgnesAdapter.toNativeRequest: 添加多模态图片转换(OpenAI content 数组) - DeepSeekAdapter: 不处理图片(DeepSeek 不支持多模态) - OllamaAdapter: 已有独立的图片处理(images: [base64] 数组) 各 Provider 多模态支持: DeepSeek ❌ 不支持 → 不发送 images 字段 Agnes AI ✅ 支持 → OpenAI content 数组 [{type:image_url}] Ollama ✅ 支持 → images: [base64]
87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
/**
|
||
* OpenAI 兼容 API 格式构建工具
|
||
*
|
||
* 将 MetonaRequest 转换为 OpenAI /chat/completions 兼容的原生请求格式。
|
||
* DeepSeek 和 Agnes AI 共享此工具,各自 Adapter 只需处理 Provider 特有的差异参数。
|
||
*
|
||
* @see electron/harness/types/metona-request.ts — MetonaRequest 定义
|
||
* @see apis/deepseek-api-docs-20260518.html
|
||
* @see apis/agnes-ai-api-docs-20260625.html
|
||
*/
|
||
|
||
import type { MetonaRequest, MetonaToolDef } from '../../types';
|
||
|
||
/**
|
||
* 构建 OpenAI 兼容的 messages 数组
|
||
*
|
||
* 处理:
|
||
* - System Prompt 拼接(静态区 + 动态区 + 安全准则)
|
||
* - 工具调用历史保留(reasoning_content + tool_calls)
|
||
* - 工具结果注入(tool_call_id + content)
|
||
*
|
||
* 注意:图片(多模态)处理不属于此共享函数。
|
||
* 各 Provider 对多模态的支持不同(DeepSeek 不支持,Agnes/Ollama 支持但格式各异),
|
||
* 应在各自 Adapter 的 toNativeRequest 中处理。
|
||
*/
|
||
export function buildOpenAICompatibleMessages(
|
||
request: MetonaRequest,
|
||
): Array<Record<string, unknown>> {
|
||
const systemContent = [
|
||
request.systemPrompt.roleDefinition,
|
||
request.systemPrompt.outputConstraints,
|
||
request.systemPrompt.safetyGuidelines,
|
||
request.systemPrompt.dynamicReminders,
|
||
]
|
||
.filter(Boolean)
|
||
.join('\n\n');
|
||
|
||
const nonSystemMessages = request.messages
|
||
.filter((m) => m.role !== 'system')
|
||
.map((m) => {
|
||
const msg: Record<string, unknown> = { role: m.role, content: m.content };
|
||
|
||
// === Assistant 工具调用历史 ===
|
||
if (m.role === 'assistant' && m.toolCalls?.length) {
|
||
msg.tool_calls = m.toolCalls.map((tc) => ({
|
||
id: tc.id,
|
||
type: 'function',
|
||
function: { name: tc.name, arguments: JSON.stringify(tc.args) },
|
||
}));
|
||
// 推理内容必须带回上下文(否则 LLM 丢失思考链)
|
||
if (m.reasoningContent) {
|
||
msg.reasoning_content = m.reasoningContent;
|
||
}
|
||
}
|
||
|
||
// === 工具执行结果 ===
|
||
if (m.role === 'tool' && m.toolResult) {
|
||
msg.tool_call_id = m.toolResult.toolCallId;
|
||
msg.content =
|
||
typeof m.toolResult.result === 'string'
|
||
? m.toolResult.result
|
||
: JSON.stringify(m.toolResult.result);
|
||
}
|
||
|
||
return msg;
|
||
});
|
||
|
||
return [{ role: 'system', content: systemContent }, ...nonSystemMessages];
|
||
}
|
||
|
||
/**
|
||
* 构建 OpenAI 兼容的 tools 数组
|
||
*/
|
||
export function buildOpenAICompatibleTools(
|
||
tools?: MetonaToolDef[],
|
||
): Array<Record<string, unknown>> | undefined {
|
||
if (!tools?.length) return undefined;
|
||
return tools.map((t) => ({
|
||
type: 'function',
|
||
function: {
|
||
name: t.name,
|
||
description: t.description,
|
||
parameters: t.parameters,
|
||
},
|
||
}));
|
||
}
|