核心引擎修复: - CE-1: 上下文压缩摘要 role 从 system 改为 user,避免被 adapter 过滤 - CE-2: 工具失败时优先使用 error 字段(engine/openai-format/ollama 三处) - P0-1: DeadLoopError 终止时正确传 error 参数,前端可见 ERROR 事件 - MT-1: 新增 waitForAbort 方法,abortSession 等待 run 结束再返回 - MT-2: TERMINATED 状态到达时标记步骤完成,避免 Trace Viewer 转圈 - MT-3: 压缩边界检测孤立 tool 消息,避免 API 400 错误 - isRetryableError 与 catch 分支统一 toLowerCase IPC 与主进程修复: - P0-2: createAdapter 配置缺失返回 null,FALLBACK_ADAPTER 兜底 - P0-3: reloadAdapter 失败返回 success:false 通知前端 - P1-5: 校验失败发 ERROR+DONE 流事件,防止 isStreaming 卡死 - P1-6: configLoaded 标志,配置加载前禁用发送按钮 - P1-7: MCP initialize 移到 agentLoop 后,完成后同步工具 - P2-11: beforeLoad 在 loadURL 前注册 IPC handler - P2-12: provider 切换竞态保护 前端状态同步修复: - clearSessions 后同步清空前端会话与消息状态 - clearMemories 通过 memoryVersion 触发 MemoryViewer 重新加载 - ContextMenu 4 个 session 操作补全 IPC 调用与 try/catch - useConfig 配置保存失败回滚 UI 并提示 - handleToggle 工具切换失败回滚单个工具状态 错误处理全量补全: - 所有 await window.metona 调用补全 try/catch 与 toast 反馈 - MCP addServer/toggleServer/removeServer 检查返回值 - showItemInFolder 检查返回值(handleOpen/handleOpenInFolder) - sse-stream/ollama NDJSON 解析失败改为 log.warn - adapter throwHttpError 带 status 属性供 isRetryableError 判断
562 lines
20 KiB
TypeScript
562 lines
20 KiB
TypeScript
/**
|
||
* 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 log from 'electron-log';
|
||
import { nanoid } from 'nanoid';
|
||
import type { MetonaRequest, MetonaResponse, MetonaStreamEvent } from '../types';
|
||
import { MetonaFinishReason, MetonaStreamEventType } from '../types';
|
||
import type { MetonaModelInfo } from '../types/metona-adapter';
|
||
|
||
export class OllamaAdapter extends BaseAdapter {
|
||
// H-2 修复: provider → providerId(规范要求)
|
||
override readonly providerId: string = 'ollama';
|
||
readonly supportedModels = ['qwen3:latest', 'gemma3:latest', 'deepseek-r1:latest'];
|
||
readonly supportsToolCalling = true;
|
||
readonly supportsThinking = true;
|
||
|
||
// H-2 修复: Ollama 本地模型默认上下文窗口(可由 options.num_ctx 覆盖)
|
||
private static readonly DEFAULT_CONTEXT_WINDOW = 4096;
|
||
|
||
private baseURL: string;
|
||
|
||
constructor(config: ConstructorParameters<typeof BaseAdapter>[0]) {
|
||
super(config);
|
||
this.baseURL = config.baseURL || 'http://localhost:11434';
|
||
}
|
||
|
||
// ===== POST /api/chat =====
|
||
|
||
// H-2 修复: chat → send(规范要求)
|
||
async send(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 }),
|
||
// C-2 修复: 使用合并后的 signal(外部 abort + timeout)
|
||
signal: this.getFetchSignal(this.config.timeoutMs ?? 300_000),
|
||
});
|
||
|
||
if (!response.ok) {
|
||
await this.throwHttpError(response, 'Ollama API error');
|
||
}
|
||
|
||
const data = await response.json() as Record<string, unknown>;
|
||
return this.toMetonaResponse(data, request.meta.requestId, request.meta.iteration);
|
||
}
|
||
|
||
// H-2 修复: chatStream → sendStream(规范要求)
|
||
async *sendStream(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 }),
|
||
// C-2 修复: 使用合并后的 signal(外部 abort + timeout)
|
||
signal: this.getFetchSignal(this.config.timeoutMs ?? 300_000),
|
||
});
|
||
|
||
if (!response.ok || !response.body) {
|
||
await this.throwHttpError(response, 'Ollama stream error');
|
||
}
|
||
|
||
// 非空断言:上方 if 已确保 response.body 不为 null
|
||
const reader = response.body!.getReader();
|
||
const decoder = new TextDecoder();
|
||
let seq = 0;
|
||
let buffer = '';
|
||
let streamEndedNormally = false;
|
||
|
||
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) {
|
||
const args = tc.function?.arguments;
|
||
const parsedArgs = typeof args === 'string' ? JSON.parse(args) : (args ?? {});
|
||
yield {
|
||
type: MetonaStreamEventType.TOOL_CALL_COMPLETE,
|
||
requestId: request.meta.requestId,
|
||
sessionId: request.meta.sessionId,
|
||
iteration: request.meta.iteration,
|
||
seq: seq++,
|
||
timestamp: Date.now(),
|
||
toolCall: {
|
||
// L-9 修复: 统一使用 nanoid 生成工具调用 ID(与 sse-stream.ts 一致)
|
||
id: `tc_${nanoid(8)}`,
|
||
name: tc.function?.name ?? '',
|
||
args: parsedArgs,
|
||
iteration: request.meta.iteration,
|
||
timestamp: Date.now(),
|
||
},
|
||
};
|
||
}
|
||
}
|
||
|
||
// 流结束
|
||
if (chunk.done) {
|
||
streamEndedNormally = true;
|
||
// 发送 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 (parseErr) {
|
||
// P2-8 修复: 与 sse-stream.ts 一致,记录解析失败行便于诊断
|
||
log.warn(`[Ollama] Failed to parse NDJSON line: ${(parseErr as Error).message}`, trimmed.slice(0, 200));
|
||
}
|
||
}
|
||
}
|
||
|
||
// 流未正常结束(连接断开等),补发 DONE 事件防止 Agent Loop 挂起
|
||
if (!streamEndedNormally) {
|
||
yield {
|
||
type: MetonaStreamEventType.DONE,
|
||
requestId: request.meta.requestId,
|
||
sessionId: request.meta.sessionId,
|
||
iteration: request.meta.iteration,
|
||
seq: seq++,
|
||
timestamp: Date.now(),
|
||
};
|
||
}
|
||
}
|
||
|
||
// ===== 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 =====
|
||
|
||
/**
|
||
* H-2 修复: 返回 MetonaModelInfo[](规范要求)
|
||
*
|
||
* Ollama /api/tags 返回模型列表含详细信息(name, size, details),
|
||
* 转换为 MetonaModelInfo 并补充默认元数据。
|
||
*/
|
||
async listModels(): Promise<MetonaModelInfo[]> {
|
||
try {
|
||
const response = await fetch(`${this.baseURL}/api/tags`, {
|
||
signal: AbortSignal.timeout(10_000),
|
||
});
|
||
if (response.ok) {
|
||
const data = await response.json() as {
|
||
models?: Array<{
|
||
name: string;
|
||
size?: number;
|
||
details?: { parameter_size?: string; quantization_level?: string; family?: string };
|
||
}>;
|
||
};
|
||
if (data.models?.length) {
|
||
return data.models.map((m) => ({
|
||
id: m.name,
|
||
name: m.name,
|
||
// Ollama 模型上下文窗口由 options.num_ctx 决定,此处给保守值
|
||
contextWindow: OllamaAdapter.DEFAULT_CONTEXT_WINDOW,
|
||
supportsToolCalling: true, // Ollama 多数模型支持,具体能力需通过 /api/show 查询
|
||
supportsThinking: true,
|
||
description: m.details
|
||
? `${m.details.family ?? 'unknown'} / ${m.details.parameter_size ?? '?'} / ${m.details.quantization_level ?? '?'}`
|
||
: undefined,
|
||
}));
|
||
}
|
||
}
|
||
} catch {
|
||
// API 不可用时降级
|
||
}
|
||
// 回退到 supportedModels
|
||
return this.supportedModels.map((id) => ({ id }));
|
||
}
|
||
|
||
/**
|
||
* H-2 修复: 获取上下文窗口大小(规范要求)
|
||
*
|
||
* Ollama 上下文窗口由 options.num_ctx 决定(默认 4096),
|
||
* Engine 应通过 MetonaRequest.params.contextLength 显式设置。
|
||
* 此处返回默认值,供 Engine 在未指定时参考。
|
||
*/
|
||
override getContextWindow(): number {
|
||
return OllamaAdapter.DEFAULT_CONTEXT_WINDOW;
|
||
}
|
||
|
||
// ===== 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 }),
|
||
signal: AbortSignal.timeout(600_000), // 模型下载可能较慢,10 分钟超时
|
||
});
|
||
|
||
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 {
|
||
// L-3 修复: 添加日志便于诊断非标准行(如进度通知、空行等)
|
||
log.debug('[Ollama] skipped non-JSON line during pull:', line.slice(0, 100));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// ===== 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,
|
||
request.systemPrompt.dynamicReminders,
|
||
].filter(Boolean).join('\n\n'),
|
||
},
|
||
...request.messages.filter((m) => m.role !== 'system').map((m) => {
|
||
// C-6 修复: Ollama API 不支持 null content,assistant 仅有 tool_calls 时转为空字符串
|
||
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;
|
||
// CE-2 修复: 工具失败时 result 为 null,优先用 error 字段作为 content
|
||
msg.content = m.toolResult.error
|
||
? m.toolResult.error
|
||
: (typeof m.toolResult.result === 'string'
|
||
? m.toolResult.result
|
||
: JSON.stringify(m.toolResult.result));
|
||
}
|
||
// assistant 工具调用(Ollama REST API 要求 arguments 为 JSON 字符串)
|
||
if (m.role === 'assistant' && m.toolCalls?.length) {
|
||
msg.tool_calls = m.toolCalls.map((tc) => ({
|
||
function: { name: tc.name, arguments: JSON.stringify(tc.args) },
|
||
}));
|
||
}
|
||
// 推理内容回传(保持多轮推理链完整)
|
||
if (m.role === 'assistant' && m.reasoningContent) {
|
||
(msg as Record<string, unknown>).reasoning_content = m.reasoningContent;
|
||
}
|
||
return msg;
|
||
}),
|
||
];
|
||
|
||
const body: Record<string, unknown> = {
|
||
model: this.config.defaultModel,
|
||
messages,
|
||
options: {
|
||
temperature: request.params.temperature,
|
||
num_predict: request.params.maxTokens,
|
||
...(request.params.topP != null && { top_p: request.params.topP }),
|
||
...(request.params.stopSequences?.length && { stop: request.params.stopSequences }),
|
||
...(request.params.contextLength != null && { num_ctx: request.params.contextLength }),
|
||
},
|
||
};
|
||
|
||
// 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, iteration: number = 0): 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.providerId,
|
||
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>;
|
||
const rawArgs = fn?.arguments;
|
||
let args: Record<string, unknown> = {};
|
||
try {
|
||
args = typeof rawArgs === 'string' ? JSON.parse(rawArgs) : (rawArgs as Record<string, unknown>) ?? {};
|
||
} catch {
|
||
args = {};
|
||
}
|
||
return {
|
||
// L-9 修复(审计补充): 非流式路径统一使用 nanoid,与流式路径(sendStream)保持一致
|
||
id: `tc_${nanoid(8)}`,
|
||
name: (fn?.name as string) ?? '',
|
||
args,
|
||
iteration,
|
||
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: mapOllamaDoneReason(data.done_reason as string | undefined, !!message?.tool_calls),
|
||
};
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 映射 Ollama done_reason → MetonaFinishReason
|
||
*
|
||
* @see apis/ollama-api-docs-20260518.html — /api/chat 响应字段
|
||
*/
|
||
function mapOllamaDoneReason(
|
||
reason: string | undefined,
|
||
hasToolCalls: boolean,
|
||
): MetonaFinishReason {
|
||
if (hasToolCalls) return MetonaFinishReason.TOOL_CALLS;
|
||
switch (reason) {
|
||
case 'stop': return MetonaFinishReason.STOP;
|
||
case 'length': return MetonaFinishReason.LENGTH;
|
||
case 'load': return MetonaFinishReason.STOP; // 冷启动加载完成,非错误
|
||
case 'unload': return MetonaFinishReason.STOP;
|
||
default: return MetonaFinishReason.STOP;
|
||
}
|
||
}
|