feat: MetonaAI Desktop 初始项目
- Electron + React + TypeScript 架构 - 三栏布局: Sidebar | ChatPanel | DetailPanel - 9 个内置工具 (文件系统/网络/记忆/命令) - SQLite 持久化 (better-sqlite3) - MUI 暗色/亮色主题系统 - Agent Loop ReAct 状态机引擎 - DeepSeek / Agnes AI / Ollama Provider 适配器 - MCP 协议集成 - 系统托盘 + 全局快捷键 - Tailwind CSS v4 + Tailwind Merge - 修复: Sidebar 缺失 TextField 导入导致黑屏
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Agnes AI Provider Adapter
|
||||
*
|
||||
* OpenAI 兼容 API,免费使用。
|
||||
* 支持 Tool Calling、Thinking 模式、512K 上下文、多模态(图片 base64)。
|
||||
*
|
||||
* 差异于 DeepSeek:
|
||||
* - Thinking 模式使用 chat_template_kwargs(非 thinking 字段)
|
||||
* - 图片使用 base64 格式(与 Ollama 一致,通过 images 字段传递)
|
||||
* - 512K 上下文,65.5K 最大输出
|
||||
*
|
||||
* @see apis/agnes-ai-api-docs-20260625.html
|
||||
*/
|
||||
|
||||
import { DeepSeekAdapter } from './deepseek.adapter';
|
||||
import type { MetonaRequest } from '../types';
|
||||
|
||||
export class AgnesAdapter extends DeepSeekAdapter {
|
||||
override readonly provider: string = 'agnes';
|
||||
readonly supportedModels = ['agnes-2.0-flash'];
|
||||
readonly supportsToolCalling = true;
|
||||
readonly supportsThinking = true;
|
||||
|
||||
/**
|
||||
* 覆盖原生请求构建
|
||||
*
|
||||
* 差异:
|
||||
* 1. Thinking 模式使用 chat_template_kwargs
|
||||
* 2. 图片使用 OpenAI 多模态 content 数组格式
|
||||
*/
|
||||
protected override toNativeRequest(request: MetonaRequest): Record<string, unknown> {
|
||||
const base = super.toNativeRequest(request) as Record<string, unknown>;
|
||||
|
||||
// Agnes Thinking 模式:使用 chat_template_kwargs
|
||||
if (request.params?.thinkingEnabled) {
|
||||
delete base.thinking;
|
||||
delete base.reasoning_effort;
|
||||
base.chat_template_kwargs = { enable_thinking: true };
|
||||
}
|
||||
|
||||
// Agnes 默认 max_tokens 更大
|
||||
if (!request.params.maxTokens) {
|
||||
base.max_tokens = 65536;
|
||||
}
|
||||
|
||||
// 图片处理:将 images 转为 OpenAI 多模态 content 数组格式
|
||||
const messages = base.messages as Array<Record<string, unknown>>;
|
||||
for (let i = 0; i < messages.length; i++) {
|
||||
const msg = messages[i];
|
||||
// 找到对应的原始消息(跳过 system 消息)
|
||||
const origMsg = request.messages.filter((m) => m.role !== 'system')[i];
|
||||
if (!origMsg?.images || origMsg.images.length === 0) continue;
|
||||
|
||||
// 将 content 转为数组格式:[{type: "text", text: ...}, {type: "image_url", ...}]
|
||||
const contentParts: Array<Record<string, unknown>> = [];
|
||||
if (msg.content && typeof msg.content === 'string') {
|
||||
contentParts.push({ type: 'text', text: msg.content });
|
||||
}
|
||||
for (const img of origMsg.images) {
|
||||
contentParts.push({
|
||||
type: 'image_url',
|
||||
image_url: { url: img.url, detail: img.detail ?? 'auto' },
|
||||
});
|
||||
}
|
||||
msg.content = contentParts;
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Provider Adapter — 基类
|
||||
*
|
||||
* 所有 Provider 适配器共享的基类逻辑:
|
||||
* - 请求超时处理
|
||||
* - 错误映射到 MetonaError
|
||||
* - 流式事件标准化
|
||||
*/
|
||||
|
||||
import type {
|
||||
IMetonaProviderAdapter,
|
||||
AdapterConfig,
|
||||
MetonaRequest,
|
||||
MetonaResponse,
|
||||
MetonaStreamEvent,
|
||||
MetonaError,
|
||||
} from '../types';
|
||||
import { MetonaErrorCode } from '../types';
|
||||
|
||||
export abstract class BaseAdapter implements IMetonaProviderAdapter {
|
||||
abstract readonly provider: string;
|
||||
abstract readonly supportedModels: string[];
|
||||
abstract readonly supportsToolCalling: boolean;
|
||||
abstract readonly supportsThinking: boolean;
|
||||
|
||||
constructor(protected config: AdapterConfig) {}
|
||||
|
||||
abstract chat(request: MetonaRequest): Promise<MetonaResponse>;
|
||||
abstract chatStream(request: MetonaRequest): AsyncIterable<MetonaStreamEvent>;
|
||||
|
||||
async healthCheck(): Promise<boolean> {
|
||||
try {
|
||||
await this.listModels?.();
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async listModels(): Promise<string[]> {
|
||||
return this.supportedModels;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将原生错误映射为 MetonaError
|
||||
*/
|
||||
protected mapError(error: unknown): MetonaError {
|
||||
if (error instanceof Error) {
|
||||
const msg = error.message.toLowerCase();
|
||||
|
||||
if (msg.includes('timeout') || msg.includes('ETIMEDOUT')) {
|
||||
return {
|
||||
code: MetonaErrorCode.NETWORK_TIMEOUT,
|
||||
message: error.message,
|
||||
provider: this.provider,
|
||||
retryable: true,
|
||||
retryAfterMs: 3000,
|
||||
};
|
||||
}
|
||||
|
||||
if (msg.includes('401') || msg.includes('unauthorized')) {
|
||||
return {
|
||||
code: MetonaErrorCode.AUTH_INVALID,
|
||||
message: 'API key 无效或已过期',
|
||||
provider: this.provider,
|
||||
retryable: false,
|
||||
};
|
||||
}
|
||||
|
||||
if (msg.includes('429') || msg.includes('rate limit')) {
|
||||
return {
|
||||
code: MetonaErrorCode.RATE_LIMITED,
|
||||
message: '请求过于频繁,请稍后重试',
|
||||
provider: this.provider,
|
||||
retryable: true,
|
||||
retryAfterMs: 5000,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
code: MetonaErrorCode.UNKNOWN,
|
||||
message: error instanceof Error ? error.message : 'Unknown error',
|
||||
provider: this.provider,
|
||||
retryable: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
/**
|
||||
* DeepSeek Provider Adapter
|
||||
*
|
||||
* 基于 OpenAI 兼容 API 的 DeepSeek 适配器。
|
||||
* 支持 Tool Calling、Thinking 模式、流式输出、JSON 结构化输出。
|
||||
*
|
||||
* 完整实现 DeepSeek API 文档中所有参数:
|
||||
* - model, messages, thinking, reasoning_effort
|
||||
* - stream, stream_options
|
||||
* - tools, tool_choice
|
||||
* - response_format (JSON)
|
||||
* - stop, max_tokens, temperature, top_p
|
||||
* - logprobs, top_logprobs
|
||||
* - prompt_cache_hit/miss_tokens, reasoning_tokens
|
||||
* - GET /models, GET /user/balance
|
||||
*
|
||||
* @see apis/deepseek-api-docs-20260518.html
|
||||
*/
|
||||
|
||||
import { BaseAdapter } from './base-adapter';
|
||||
import { nanoid } from 'nanoid';
|
||||
import type { MetonaRequest, MetonaResponse, MetonaStreamEvent } from '../types';
|
||||
import { MetonaFinishReason, MetonaStreamEventType, MetonaErrorCode } from '../types';
|
||||
|
||||
export class DeepSeekAdapter extends BaseAdapter {
|
||||
override readonly provider: string = 'deepseek';
|
||||
readonly supportedModels = ['deepseek-v4-pro', 'deepseek-v4-flash'];
|
||||
readonly supportsToolCalling = true;
|
||||
readonly supportsThinking = true;
|
||||
|
||||
async chat(request: MetonaRequest): Promise<MetonaResponse> {
|
||||
const nativeRequest = this.toNativeRequest(request);
|
||||
|
||||
const response = await fetch(`${this.config.baseURL}/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
...this.config.headers,
|
||||
},
|
||||
body: JSON.stringify(nativeRequest),
|
||||
signal: AbortSignal.timeout(this.config.timeoutMs ?? 120_000),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.text().catch(() => '');
|
||||
throw new Error(`DeepSeek API error: ${response.status} ${response.statusText} - ${errorBody}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return this.toMetonaResponse(data, request.meta.requestId);
|
||||
}
|
||||
|
||||
async *chatStream(request: MetonaRequest): AsyncIterable<MetonaStreamEvent> {
|
||||
const nativeRequest = { ...this.toNativeRequest(request), stream: true, stream_options: { include_usage: true } };
|
||||
|
||||
const response = await fetch(`${this.config.baseURL}/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
...this.config.headers,
|
||||
},
|
||||
body: JSON.stringify(nativeRequest),
|
||||
});
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
throw new Error(`DeepSeek stream error: ${response.status}`);
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let seq = 0;
|
||||
let buffer = '';
|
||||
|
||||
// 工具调用缓冲区:index → { name, argsBuffer }
|
||||
const toolCallsBuffer = new Map<number, { name: string; argsBuffer: string }>();
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split('\n');
|
||||
buffer = lines.pop() ?? '';
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || !trimmed.startsWith('data: ')) continue;
|
||||
const data = trimmed.slice(6);
|
||||
if (data === '[DONE]') {
|
||||
// 流结束前,将缓冲区中的工具调用转为 TOOL_CALL_COMPLETE
|
||||
for (const [index, buf] of toolCallsBuffer) {
|
||||
try {
|
||||
yield {
|
||||
type: MetonaStreamEventType.TOOL_CALL_COMPLETE,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
toolCall: {
|
||||
id: `tc_${nanoid(8)}`,
|
||||
name: buf.name,
|
||||
args: buf.argsBuffer ? JSON.parse(buf.argsBuffer) : {},
|
||||
iteration: request.meta.iteration,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
// JSON 解析失败,跳过
|
||||
}
|
||||
}
|
||||
toolCallsBuffer.clear();
|
||||
|
||||
yield {
|
||||
type: MetonaStreamEventType.DONE,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const chunk = JSON.parse(data);
|
||||
const delta = chunk.choices?.[0]?.delta;
|
||||
|
||||
// 文本内容增量
|
||||
if (delta?.content) {
|
||||
yield {
|
||||
type: MetonaStreamEventType.TEXT_DELTA,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
delta: delta.content,
|
||||
};
|
||||
}
|
||||
|
||||
// 推理内容增量(Thinking 模式)
|
||||
if (delta?.reasoning_content) {
|
||||
yield {
|
||||
type: MetonaStreamEventType.REASONING_DELTA,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
delta: delta.reasoning_content,
|
||||
};
|
||||
}
|
||||
|
||||
// 工具调用增量 — 缓冲拼接
|
||||
if (delta?.tool_calls) {
|
||||
for (const tc of delta.tool_calls) {
|
||||
const idx = tc.index ?? 0;
|
||||
if (!toolCallsBuffer.has(idx)) {
|
||||
toolCallsBuffer.set(idx, { name: tc.function?.name ?? '', argsBuffer: '' });
|
||||
}
|
||||
const buf = toolCallsBuffer.get(idx)!;
|
||||
if (tc.function?.name) buf.name = tc.function.name;
|
||||
if (tc.function?.arguments) buf.argsBuffer += tc.function.arguments;
|
||||
|
||||
yield {
|
||||
type: MetonaStreamEventType.TOOL_CALL_DELTA,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
toolCallDelta: {
|
||||
index: idx,
|
||||
name: tc.function?.name,
|
||||
argsDelta: tc.function?.arguments,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 流结束时的 usage 信息
|
||||
if (chunk.usage) {
|
||||
yield {
|
||||
type: MetonaStreamEventType.USAGE,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
usage: {
|
||||
inputTokens: chunk.usage.prompt_tokens ?? 0,
|
||||
outputTokens: chunk.usage.completion_tokens ?? 0,
|
||||
totalTokens: chunk.usage.total_tokens ?? 0,
|
||||
reasoningTokens: chunk.usage.completion_tokens_details?.reasoning_tokens,
|
||||
cacheHitTokens: chunk.usage.prompt_cache_hit_tokens,
|
||||
cacheMissTokens: chunk.usage.prompt_cache_miss_tokens,
|
||||
},
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// 跳过解析失败的行
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出可用模型
|
||||
*/
|
||||
async listModels(): Promise<string[]> {
|
||||
try {
|
||||
const response = await fetch(`${this.config.baseURL}/models`, {
|
||||
headers: { Authorization: `Bearer ${this.config.apiKey}` },
|
||||
signal: AbortSignal.timeout(10_000),
|
||||
});
|
||||
if (!response.ok) return this.supportedModels;
|
||||
const data = await response.json() as { data?: Array<{ id: string }> };
|
||||
return data.data?.map((m) => m.id) ?? this.supportedModels;
|
||||
} catch {
|
||||
return this.supportedModels;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询账户余额
|
||||
*/
|
||||
async getBalance(): Promise<{ currency: string; totalBalance: string; grantedBalance: string; toppedUpBalance: string } | null> {
|
||||
try {
|
||||
const response = await fetch(`${this.config.baseURL}/user/balance`, {
|
||||
headers: { Authorization: `Bearer ${this.config.apiKey}` },
|
||||
signal: AbortSignal.timeout(10_000),
|
||||
});
|
||||
if (!response.ok) return null;
|
||||
const data = await response.json() as { currency?: string; total_balance?: string; granted_balance?: string; topped_up_balance?: string };
|
||||
return {
|
||||
currency: data.currency ?? 'CNY',
|
||||
totalBalance: data.total_balance ?? '0',
|
||||
grantedBalance: data.granted_balance ?? '0',
|
||||
toppedUpBalance: data.topped_up_balance ?? '0',
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 私有转换方法 ==========
|
||||
|
||||
/**
|
||||
* 将 MetonaRequest 转换为 OpenAI 兼容的原生请求格式
|
||||
*/
|
||||
protected toNativeRequest(request: MetonaRequest): Record<string, unknown> {
|
||||
const messages = [
|
||||
{
|
||||
role: 'system',
|
||||
content: [
|
||||
request.systemPrompt.roleDefinition,
|
||||
request.systemPrompt.outputConstraints,
|
||||
request.systemPrompt.safetyGuidelines,
|
||||
request.systemPrompt.dynamicReminders,
|
||||
].filter(Boolean).join('\n\n'),
|
||||
},
|
||||
...request.messages.filter((m) => m.role !== 'system').map((m) => {
|
||||
const msg: Record<string, unknown> = { role: m.role, content: m.content };
|
||||
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) },
|
||||
}));
|
||||
// 工具调用轮次的 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;
|
||||
}),
|
||||
];
|
||||
|
||||
const body: Record<string, unknown> = {
|
||||
model: this.config.defaultModel,
|
||||
messages,
|
||||
temperature: request.params.temperature ?? 0,
|
||||
max_tokens: request.params.maxTokens,
|
||||
stream: request.params.stream ?? false,
|
||||
};
|
||||
|
||||
// Tool Calling
|
||||
if (request.tools?.length) {
|
||||
body.tools = request.tools.map((t) => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: t.name,
|
||||
description: t.description,
|
||||
parameters: t.parameters,
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
// Thinking 模式
|
||||
if (request.params.thinkingEnabled) {
|
||||
body.thinking = { type: 'enabled' };
|
||||
// reasoning_effort 映射
|
||||
const effortMap: Record<string, string> = { low: 'high', medium: 'high', high: 'high', max: 'max' };
|
||||
body.reasoning_effort = effortMap[request.params.thinkingEffort ?? 'high'] ?? 'high';
|
||||
}
|
||||
|
||||
// 停止序列
|
||||
if (request.params.stopSequences?.length) {
|
||||
body.stop = request.params.stopSequences;
|
||||
}
|
||||
|
||||
// 安全约束
|
||||
if (request.constraints?.allowedTools?.length) {
|
||||
body.tool_choice = 'auto';
|
||||
}
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
private toMetonaResponse(data: Record<string, unknown>, requestId: string): MetonaResponse {
|
||||
const choice = (data.choices as Array<Record<string, unknown>>)?.[0];
|
||||
const message = choice?.message as Record<string, unknown> | undefined;
|
||||
const usage = data.usage as Record<string, unknown> | undefined;
|
||||
const toolCalls = message?.tool_calls as Array<Record<string, unknown>> | undefined;
|
||||
|
||||
return {
|
||||
meta: {
|
||||
requestId,
|
||||
provider: this.provider,
|
||||
model: (data.model as string) ?? this.config.defaultModel,
|
||||
latencyMs: 0,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
content: (message?.content as string) ?? '',
|
||||
reasoningContent: message?.reasoning_content as string | undefined,
|
||||
toolCalls: toolCalls?.map((tc) => {
|
||||
const fn = tc.function as Record<string, unknown>;
|
||||
return {
|
||||
id: tc.id as string,
|
||||
name: fn.name as string,
|
||||
args: JSON.parse(fn.arguments as string),
|
||||
iteration: 0,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
}),
|
||||
usage: {
|
||||
inputTokens: (usage?.prompt_tokens as number) ?? 0,
|
||||
outputTokens: (usage?.completion_tokens as number) ?? 0,
|
||||
totalTokens: (usage?.total_tokens as number) ?? 0,
|
||||
reasoningTokens: (usage?.completion_tokens_details as Record<string, unknown>)?.reasoning_tokens as number | undefined,
|
||||
cacheHitTokens: usage?.prompt_cache_hit_tokens as number | undefined,
|
||||
cacheMissTokens: usage?.prompt_cache_miss_tokens as number | undefined,
|
||||
},
|
||||
finishReason: this.mapFinishReason(choice?.finish_reason as string),
|
||||
};
|
||||
}
|
||||
|
||||
private mapFinishReason(reason: string): MetonaFinishReason {
|
||||
switch (reason) {
|
||||
case 'stop': return MetonaFinishReason.STOP;
|
||||
case 'length': return MetonaFinishReason.LENGTH;
|
||||
case 'tool_calls': return MetonaFinishReason.TOOL_CALLS;
|
||||
case 'content_filter': return MetonaFinishReason.CONTENT_FILTER;
|
||||
default: return MetonaFinishReason.STOP;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export { BaseAdapter } from './base-adapter';
|
||||
export { DeepSeekAdapter } from './deepseek.adapter';
|
||||
export { AgnesAdapter } from './agnes-ai.adapter';
|
||||
export { OllamaAdapter } from './ollama.adapter';
|
||||
@@ -0,0 +1,454 @@
|
||||
/**
|
||||
* Ollama Provider Adapter
|
||||
*
|
||||
* 本地推理引擎,支持 Tool Calling、Thinking 模式、NDJSON 流式。
|
||||
* 无需 API Key,连接本地 http://localhost:11434。
|
||||
*
|
||||
* 完整实现 Ollama API 文档中所有端点和参数:
|
||||
* - POST /api/chat (对话)
|
||||
* - POST /api/generate (补全)
|
||||
* - POST /api/embed (嵌入)
|
||||
* - GET /api/tags (列出模型)
|
||||
* - POST /api/show (模型详情)
|
||||
* - POST /api/pull (下载模型)
|
||||
* - GET /api/ps (运行中模型)
|
||||
* - GET /api/version (版本)
|
||||
* - think 参数(Thinking 模式)
|
||||
* - options 参数(temperature/top_k/top_p/stop/num_ctx/num_predict)
|
||||
* - format 参数(structured output)
|
||||
* - images 参数(多模态)
|
||||
* - tool_calls 流式处理
|
||||
*
|
||||
* @see apis/ollama-api-docs-20260518.html
|
||||
*/
|
||||
|
||||
import { BaseAdapter } from './base-adapter';
|
||||
import type { MetonaRequest, MetonaResponse, MetonaStreamEvent } from '../types';
|
||||
import { MetonaFinishReason, MetonaStreamEventType } from '../types';
|
||||
|
||||
export class OllamaAdapter extends BaseAdapter {
|
||||
override readonly provider: string = 'ollama';
|
||||
readonly supportedModels = ['qwen3:latest', 'gemma3:latest', 'deepseek-r1:latest'];
|
||||
readonly supportsToolCalling = true;
|
||||
readonly supportsThinking = true;
|
||||
|
||||
private baseURL: string;
|
||||
|
||||
constructor(config: ConstructorParameters<typeof BaseAdapter>[0]) {
|
||||
super(config);
|
||||
this.baseURL = config.baseURL || 'http://localhost:11434';
|
||||
}
|
||||
|
||||
// ===== POST /api/chat =====
|
||||
|
||||
async chat(request: MetonaRequest): Promise<MetonaResponse> {
|
||||
const nativeRequest = this.toNativeRequest(request);
|
||||
|
||||
const response = await fetch(`${this.baseURL}/api/chat`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ...nativeRequest, stream: false }),
|
||||
signal: AbortSignal.timeout(this.config.timeoutMs ?? 300_000),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Ollama API error: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return this.toMetonaResponse(data, request.meta.requestId);
|
||||
}
|
||||
|
||||
async *chatStream(request: MetonaRequest): AsyncIterable<MetonaStreamEvent> {
|
||||
const nativeRequest = this.toNativeRequest(request);
|
||||
|
||||
const response = await fetch(`${this.baseURL}/api/chat`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ...nativeRequest, stream: true }),
|
||||
});
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
throw new Error(`Ollama stream error: ${response.status}`);
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let seq = 0;
|
||||
let buffer = '';
|
||||
|
||||
// 工具调用缓冲区
|
||||
const toolCallsBuffer = new Map<number, { name: string; argsBuffer: string }>();
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split('\n');
|
||||
buffer = lines.pop() ?? '';
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) continue;
|
||||
|
||||
try {
|
||||
const chunk = JSON.parse(trimmed);
|
||||
|
||||
// 思考内容
|
||||
if (chunk.message?.thinking) {
|
||||
yield {
|
||||
type: MetonaStreamEventType.REASONING_DELTA,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
delta: chunk.message.thinking,
|
||||
};
|
||||
}
|
||||
|
||||
// 文本内容
|
||||
if (chunk.message?.content) {
|
||||
yield {
|
||||
type: MetonaStreamEventType.TEXT_DELTA,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
delta: chunk.message.content,
|
||||
};
|
||||
}
|
||||
|
||||
// 工具调用(Ollama 在最后一个 chunk 中整块返回)
|
||||
if (chunk.message?.tool_calls) {
|
||||
for (const tc of chunk.message.tool_calls) {
|
||||
yield {
|
||||
type: MetonaStreamEventType.TOOL_CALL_COMPLETE,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
toolCall: {
|
||||
id: `tc_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
||||
name: tc.function?.name ?? '',
|
||||
args: tc.function?.arguments ?? {},
|
||||
iteration: request.meta.iteration,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 流结束
|
||||
if (chunk.done) {
|
||||
// 发送 usage 信息
|
||||
yield {
|
||||
type: MetonaStreamEventType.USAGE,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
usage: {
|
||||
inputTokens: chunk.prompt_eval_count ?? 0,
|
||||
outputTokens: chunk.eval_count ?? 0,
|
||||
totalTokens: (chunk.prompt_eval_count ?? 0) + (chunk.eval_count ?? 0),
|
||||
},
|
||||
};
|
||||
|
||||
yield {
|
||||
type: MetonaStreamEventType.DONE,
|
||||
requestId: request.meta.requestId,
|
||||
sessionId: request.meta.sessionId,
|
||||
iteration: request.meta.iteration,
|
||||
seq: seq++,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// 跳过解析失败的行
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== POST /api/generate =====
|
||||
|
||||
async generate(params: {
|
||||
model: string;
|
||||
prompt: string;
|
||||
suffix?: string;
|
||||
system?: string;
|
||||
stream?: boolean;
|
||||
think?: boolean | string;
|
||||
format?: string | object;
|
||||
images?: string[];
|
||||
options?: Record<string, unknown>;
|
||||
}): Promise<{ response: string; thinking?: string; done: boolean; totalDuration: number; evalCount: number }> {
|
||||
const response = await fetch(`${this.baseURL}/api/generate`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ ...params, stream: false }),
|
||||
signal: AbortSignal.timeout(300_000),
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(`Ollama generate error: ${response.status}`);
|
||||
const data = await response.json() as {
|
||||
response?: string; thinking?: string; done?: boolean;
|
||||
total_duration?: number; eval_count?: number;
|
||||
};
|
||||
|
||||
return {
|
||||
response: data.response ?? '',
|
||||
thinking: data.thinking,
|
||||
done: data.done ?? true,
|
||||
totalDuration: data.total_duration ?? 0,
|
||||
evalCount: data.eval_count ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
// ===== POST /api/embed =====
|
||||
|
||||
async embed(params: {
|
||||
model: string;
|
||||
input: string | string[];
|
||||
dimensions?: number;
|
||||
}): Promise<{ embeddings: number[][]; totalDuration: number }> {
|
||||
const response = await fetch(`${this.baseURL}/api/embed`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(params),
|
||||
signal: AbortSignal.timeout(60_000),
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(`Ollama embed error: ${response.status}`);
|
||||
const data = await response.json() as { embeddings?: number[][]; total_duration?: number };
|
||||
|
||||
return {
|
||||
embeddings: data.embeddings ?? [],
|
||||
totalDuration: data.total_duration ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
// ===== GET /api/tags =====
|
||||
|
||||
async listModels(): Promise<string[]> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseURL}/api/tags`, {
|
||||
signal: AbortSignal.timeout(10_000),
|
||||
});
|
||||
if (!response.ok) return this.supportedModels;
|
||||
const data = await response.json() as { models?: Array<{ name: string }> };
|
||||
return data.models?.map((m) => m.name) ?? this.supportedModels;
|
||||
} catch {
|
||||
return this.supportedModels;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== POST /api/show =====
|
||||
|
||||
async showModel(model: string): Promise<{ parameters: string; template: string; capabilities: string[] } | null> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseURL}/api/show`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ model }),
|
||||
signal: AbortSignal.timeout(10_000),
|
||||
});
|
||||
if (!response.ok) return null;
|
||||
const data = await response.json() as { parameters?: string; template?: string; capabilities?: string[] };
|
||||
return {
|
||||
parameters: data.parameters ?? '',
|
||||
template: data.template ?? '',
|
||||
capabilities: data.capabilities ?? [],
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== POST /api/pull =====
|
||||
|
||||
async pullModel(model: string, onProgress?: (progress: { status: string; completed?: number; total?: number }) => void): Promise<void> {
|
||||
const response = await fetch(`${this.baseURL}/api/pull`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ model, stream: true }),
|
||||
});
|
||||
|
||||
if (!response.ok || !response.body) throw new Error(`Ollama pull error: ${response.status}`);
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split('\n');
|
||||
buffer = lines.pop() ?? '';
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) continue;
|
||||
try {
|
||||
const chunk = JSON.parse(line);
|
||||
onProgress?.({ status: chunk.status, completed: chunk.completed, total: chunk.total });
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== GET /api/ps =====
|
||||
|
||||
async listRunning(): Promise<Array<{ name: string; size: number; sizeVram: number; contextLength: number }>> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseURL}/api/ps`, {
|
||||
signal: AbortSignal.timeout(10_000),
|
||||
});
|
||||
if (!response.ok) return [];
|
||||
const data = await response.json() as { models?: Array<{ name: string; size?: number; size_vram?: number; context_length?: number }> };
|
||||
return (data.models ?? []).map((m) => ({
|
||||
name: m.name ?? '',
|
||||
size: m.size ?? 0,
|
||||
sizeVram: m.size_vram ?? 0,
|
||||
contextLength: m.context_length ?? 0,
|
||||
}));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// ===== GET /api/version =====
|
||||
|
||||
async getVersion(): Promise<string> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseURL}/api/version`, {
|
||||
signal: AbortSignal.timeout(5_000),
|
||||
});
|
||||
if (!response.ok) return 'unknown';
|
||||
const data = await response.json() as { version?: string };
|
||||
return data.version ?? 'unknown';
|
||||
} catch {
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 私有转换方法 ==========
|
||||
|
||||
private toNativeRequest(request: MetonaRequest): Record<string, unknown> {
|
||||
const messages = [
|
||||
{
|
||||
role: 'system',
|
||||
content: [
|
||||
request.systemPrompt.roleDefinition,
|
||||
request.systemPrompt.outputConstraints,
|
||||
request.systemPrompt.safetyGuidelines,
|
||||
].filter(Boolean).join('\n\n'),
|
||||
},
|
||||
...request.messages.filter((m) => m.role !== 'system').map((m) => {
|
||||
const msg: Record<string, unknown> = { role: m.role, content: m.content };
|
||||
// Ollama 图片使用 images 字段(纯 base64 数组,不含 data: 前缀)
|
||||
if (m.images?.length) {
|
||||
msg.images = m.images.map((img) => {
|
||||
const url = img.url;
|
||||
// data:image/png;base64,iVBOR... → iVBOR...
|
||||
if (url.startsWith('data:')) {
|
||||
const base64Part = url.split(',')[1];
|
||||
return base64Part ?? url;
|
||||
}
|
||||
return url;
|
||||
});
|
||||
}
|
||||
// 工具结果
|
||||
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);
|
||||
}
|
||||
// assistant 工具调用
|
||||
if (m.role === 'assistant' && m.toolCalls?.length) {
|
||||
msg.tool_calls = m.toolCalls.map((tc) => ({
|
||||
function: { name: tc.name, arguments: tc.args },
|
||||
}));
|
||||
}
|
||||
return msg;
|
||||
}),
|
||||
];
|
||||
|
||||
const body: Record<string, unknown> = {
|
||||
model: this.config.defaultModel,
|
||||
messages,
|
||||
options: {
|
||||
temperature: request.params.temperature ?? 0,
|
||||
num_predict: request.params.maxTokens,
|
||||
top_p: request.params.topP,
|
||||
stop: request.params.stopSequences,
|
||||
},
|
||||
};
|
||||
|
||||
// Tool Calling
|
||||
if (request.tools?.length) {
|
||||
body.tools = request.tools.map((t) => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: t.name,
|
||||
description: t.description,
|
||||
parameters: t.parameters,
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
// Thinking 模式
|
||||
if (request.params.thinkingEnabled) {
|
||||
const effortMap: Record<string, string | boolean> = { low: 'low', medium: 'medium', high: 'high', max: true };
|
||||
body.think = effortMap[request.params.thinkingEffort ?? 'high'] ?? true;
|
||||
}
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
private toMetonaResponse(data: Record<string, unknown>, requestId: string): MetonaResponse {
|
||||
const message = data.message as Record<string, unknown> | undefined;
|
||||
const toolCalls = message?.tool_calls as Array<Record<string, unknown>> | undefined;
|
||||
return {
|
||||
meta: {
|
||||
requestId,
|
||||
provider: this.provider,
|
||||
model: (data.model as string) ?? this.config.defaultModel,
|
||||
latencyMs: 0,
|
||||
timestamp: Date.now(),
|
||||
perfStats: {
|
||||
loadDurationMs: data.load_duration ? (data.load_duration as number) / 1e6 : undefined,
|
||||
promptEvalDurationMs: data.prompt_eval_duration ? (data.prompt_eval_duration as number) / 1e6 : undefined,
|
||||
evalDurationMs: data.eval_duration ? (data.eval_duration as number) / 1e6 : undefined,
|
||||
tokensPerSecond: data.eval_count && data.eval_duration
|
||||
? ((data.eval_count as number) / ((data.eval_duration as number) / 1e9))
|
||||
: undefined,
|
||||
},
|
||||
},
|
||||
content: (message?.content as string) ?? '',
|
||||
reasoningContent: message?.thinking as string | undefined,
|
||||
toolCalls: toolCalls?.map((tc, i) => {
|
||||
const fn = tc.function as Record<string, unknown>;
|
||||
return {
|
||||
id: `tc_${Date.now()}_${i}`,
|
||||
name: (fn?.name as string) ?? '',
|
||||
args: (fn?.arguments as Record<string, unknown>) ?? {},
|
||||
iteration: 0,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
}),
|
||||
usage: {
|
||||
inputTokens: (data.prompt_eval_count as number) ?? 0,
|
||||
outputTokens: (data.eval_count as number) ?? 0,
|
||||
totalTokens: ((data.prompt_eval_count as number) ?? 0) + ((data.eval_count as number) ?? 0),
|
||||
},
|
||||
finishReason: data.done ? MetonaFinishReason.STOP : MetonaFinishReason.LENGTH,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user