**崩溃/挂死修复 (5):** - 统一 TrayManager.isQuitting 变量,修复 Cmd+Q 无法退出 - useAgentStream 闭包过期快照 → 每次 getState() - Agnes chatStream 添加 AbortSignal.timeout - SSE JSON.parse 添加 try-catch 保护 - Orchestrator setTools 污染 → save/restore 模式 **功能修复 (14):** - 上下文压缩实现 (每5轮 COMPRESSING 状态) - 修复 requestId 硬编码空串 - ConfigService.set() 保留已有 category - MemoryManager 新增 working 类型搜索 - PromptInjectionDefender 补全 sanitize() - Ollama: 补全 dynamicReminders + reasoningContent - openai-format: 所有 assistant 消息保留 reasoningContent - SSE: finish_reason 时提前 flush tool_calls - DeepSeek thinking effort 映射注释 - Ollama done_reason load→stop - RateLimitHook >= 边界修复 - WorkspaceService isValid 首次启动修复 - sessions:archive IPC handler - 托盘/窗口图标路径生产环境修复 **系统提示词优化:** - SOUL.md 存在时不显示兜底身份,原文放最前 - 兜底身份改为中文 (MetonaAI 自身描述) - 用户文本在前,附件内容在后 **文件上传:** - 非图片文件不再 base64 编码,保留 JSON 结构 - 用户文本优先于文件内容 **UI 修复:** - 首页 Logo 路径修复 (public/ + 相对路径) - TokenUsage contextWindow 动态计算 (Provider 感知) - 切换 Provider 同步 contextWindow - 托盘图标始终显示 Logo (状态由右键菜单展示)
90 lines
2.8 KiB
TypeScript
90 lines
2.8 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') {
|
||
// 工具调用历史
|
||
if (m.toolCalls?.length) {
|
||
msg.tool_calls = m.toolCalls.map((tc) => ({
|
||
id: tc.id,
|
||
type: 'function',
|
||
function: { name: tc.name, arguments: JSON.stringify(tc.args) },
|
||
}));
|
||
}
|
||
// 推理内容(无论是否有工具调用,都保留 reasoning_content)
|
||
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,
|
||
},
|
||
}));
|
||
}
|