fix: preserve upstream model errors and native request parameters
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Activity, LoaderCircle, RotateCw } from 'lucide-react';
|
||||
import type { RuntimeDiagnostics } from './types';
|
||||
import { Status } from './components';
|
||||
import { ModelErrorDetails } from './ModelErrorDetails';
|
||||
|
||||
export function DiagnosticsPanel({ value, busy, onRefresh }: { value: RuntimeDiagnostics | null; busy: boolean; onRefresh: () => void }) {
|
||||
if (!value) return <div className="diagnostic-loading"><LoaderCircle size={20} className="spin" />{busy ? '正在读取本机执行记录…' : '诊断记录暂时不可用,请刷新重试。'}<button type="button" className="button secondary" disabled={busy} onClick={onRefresh}>刷新</button></div>;
|
||||
@@ -16,7 +17,7 @@ export function DiagnosticsPanel({ value, busy, onRefresh }: { value: RuntimeDia
|
||||
<dt>当前项目上次核对</dt><dd>{value.projectCache.latest ? new Date(value.projectCache.latest.checkedAt).toLocaleString('zh-CN') : '本次进程尚未检索'}</dd></dl>
|
||||
{value.projectCache.latest && <p className="diagnostic-note">{value.projectCache.latest.reusedFiles} 份解析材料复用,{value.projectCache.latest.parsedFiles} 份重新解析;来源{value.projectCache.latest.sourceVerified ? '已核对' : '范围不完整'}。</p>}
|
||||
<p className="diagnostic-note">在工程「索引 → 解析缓存」管理开关与清除。此处仅显示派生缓存状态,不触发工程检索或模型请求。</p>{value.projectCache.error && <p className="diagnostic-note">{value.projectCache.error}</p>}</section>}
|
||||
{failure && <section className="diagnostic-failure"><div className="diagnostic-section-heading"><h3>最近的错误记录</h3><span>{failure.source === 'model_connection' ? '模型服务请求' : '工作台任务'}</span></div><strong>{failure.title}</strong><p>{failure.message}</p>{failure.detail && <p>{failure.detail}</p>}{failure.occurredAt && <small>{new Date(failure.occurredAt).toLocaleString('zh-CN')}</small>}{failure.suggestion && <p className="diagnostic-note">{failure.suggestion}</p>}</section>}
|
||||
{failure && <section className="diagnostic-failure"><div className="diagnostic-section-heading"><h3>最近的错误记录</h3><span>{failure.source === 'model_connection' ? '模型服务请求' : '工作台任务'}</span></div><strong>{failure.title}</strong><p>{failure.message}</p><ModelErrorDetails failure={failure} />{failure.occurredAt && <small>{new Date(failure.occurredAt).toLocaleString('zh-CN')}</small>}{failure.suggestion && <p className="diagnostic-note">{failure.suggestion}</p>}</section>}
|
||||
{!!value.session?.changes.length && <section><h3>文件改动的实际状态</h3><div className="diagnostic-changes">{value.session.changes.map(change => <div key={change.id}><code>{change.path}</code><Status value={change.status} /></div>)}</div></section>}
|
||||
{!!value.session?.recentTools.length && <section><h3>近期工具结果</h3><div className="diagnostic-tools">{value.session.recentTools.slice(-8).map((tool, index) => <details key={index}><summary><code>{tool.tool}</code><Status value={tool.phase} />{tool.exitCode !== null && <span>退出码 {tool.exitCode}</span>}</summary><pre>{tool.outputExcerpt || '没有文本输出。'}</pre></details>)}</div></section>}
|
||||
</div>;
|
||||
|
||||
@@ -20,3 +20,5 @@
|
||||
.mc-stream-receipt .button { margin-top: 8px; }
|
||||
.mc-editor > p { color: #475b73; font-size: 13px; line-height: 1.8; margin: 12px 0; }
|
||||
.mc-form-grid label > small { color: #475b73; font-size: 12px; line-height: 1.7; font-weight: 400; }
|
||||
|
||||
.mc-native>label{display:flex;flex-direction:column;gap:8px;font-size:12px;color:#40576e;margin:14px 0}.mc-native select,.mc-native textarea{width:100%;min-width:0;padding:10px;border:1px solid #a9bdcf;border-radius:5px;background:white;font-size:12px;line-height:1.75}.mc-native textarea{resize:vertical;font-family:Consolas,"Microsoft YaHei",monospace}.mc-native select:focus-visible,.mc-native textarea:focus-visible{outline:2px solid #2560a3;outline-offset:2px}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { Activity, Check, CircleHelp, KeyRound, Layers3, Plus, Power, RefreshCw, Route, Server, Settings2 } from 'lucide-react';
|
||||
import { api, downloadArtifact } from './api';
|
||||
import type { Config, ModelSelection } from './types';
|
||||
import type { Config, ModelSelection, RuntimeFailure } from './types';
|
||||
import './ModelCenter.css';
|
||||
import { ModelWaitSettings, type WaitSettings } from './ModelWaitSettings';
|
||||
import { NativeParameters } from './NativeParameters';
|
||||
import { ModelErrorDetails } from './ModelErrorDetails';
|
||||
|
||||
type Connection = { id: string; name: string; brand: string; protocol: string; baseUrl: string; model: string; enabled: boolean; revision: number; hasKey: boolean;
|
||||
requiresKey: boolean; streaming?: boolean; capabilityDeclarations?: Record<string, string>; capabilities?: Record<string, string>; capabilityEvidence?: { checkedAt: string }; contextLimit: number; maxOutputTokens: number; concurrency: number; status: string; activeRequests: number; savedCredential?: { status: string }; health: { state: string; reason?: string; cooldownUntil?: number } };
|
||||
requiresKey: boolean; nativeParameters?: Record<string, unknown>; outputTokenField?: string; streaming?: boolean; capabilityDeclarations?: Record<string, string>; capabilities?: Record<string, string>; capabilityEvidence?: { checkedAt: string }; contextLimit: number; maxOutputTokens: number; concurrency: number; status: string; activeRequests: number; savedCredential?: { status: string }; health: { state: string; reason?: string; cooldownUntil?: number } };
|
||||
type Pool = { id?: string; name: string; members: { connectionId: string; weight: number }[]; strategy: string; sticky: boolean; maxAttempts: number; revision?: number; queueTimeoutMs: number };
|
||||
type Attempt = { id: string; connectionId: string; poolId: string | null; model: string; protocol?: string; streaming?: boolean; rejections?: { connectionName: string; capability: string; reason: string }[]; status: string; startedAt: string; ordinal: number; usageStatus: string; reason?: string; taskId?: string; sessionId?: string; runId: string; requestArtifactId?: string; responseArtifactId?: string; responseSha256?: string; responseRedacted?: boolean; stream?: { bytes: number; capturedBytes?: number; frames: number; sha256: string; terminal: boolean }; usage?: { prompt_tokens?: number; completion_tokens?: number } };
|
||||
type Attempt = { diagnostic?: RuntimeFailure; upstreamResponse?: { requestId?: string | null }; id: string; connectionId: string; poolId: string | null; model: string; protocol?: string; streaming?: boolean; rejections?: { connectionName: string; capability: string; reason: string }[]; status: string; startedAt: string; ordinal: number; usageStatus: string; reason?: string; taskId?: string; sessionId?: string; runId: string; requestArtifactId?: string; responseArtifactId?: string; responseSha256?: string; responseRedacted?: boolean; stream?: { bytes: number; capturedBytes?: number; frames: number; sha256: string; terminal: boolean }; usage?: { prompt_tokens?: number; completion_tokens?: number } };
|
||||
type ImportRecord = { id: string; source: string; path: string; enabled: boolean; lastCheckedAt?: string; lastSyncedAt?: string; lastError?: string; lastResult?: { imported?: number; conflicts?: string[] } };
|
||||
type Center = { revision: number; selected: { type: string; id: string }; waitSettings: WaitSettings; connections: Connection[]; pools: Pool[]; imports: ImportRecord[]; attempts: Attempt[]; waiting: number };
|
||||
type Operations = { checkedAt: string; version: string; scope: string; activeTasks: number; queue: number; checks: { id: string; title: string; status: string; detail: string }[];
|
||||
@@ -26,10 +28,12 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
const [tab, setTab] = useState('connections');
|
||||
const [draft, setDraft] = useState<typeof initial & { id?: string; revision?: number }>(initial);
|
||||
const [editing, setEditing] = useState(false), [key, setKey] = useState('');
|
||||
const [nativeTarget, setNativeTarget] = useState<Connection | null>(null);
|
||||
const [persistKey, setPersistKey] = useState(false), [persistImports, setPersistImports] = useState(false);
|
||||
const [credentialView, setCredentialView] = useState<CredentialView | null>(null), [credentialKey, setCredentialKey] = useState(''), [credentialTarget, setCredentialTarget] = useState('');
|
||||
const [pool, setPool] = useState<Pool>(emptyPool), [poolEditing, setPoolEditing] = useState(false);
|
||||
const [working, setWorking] = useState(false), [notice, setNotice] = useState('');
|
||||
const [noticeFailure, setNoticeFailure] = useState<RuntimeFailure | null>(null);
|
||||
const [probeScopes, setProbeScopes] = useState<Record<string, string>>({});
|
||||
const [operations, setOperations] = useState<Operations | null>(null), [autoInspect, setAutoInspect] = useState(false);
|
||||
const [importPath, setImportPath] = useState(''), [importSource, setImportSource] = useState('ccswitch');
|
||||
@@ -39,9 +43,10 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
const tabsRef = useRef<HTMLElement>(null);
|
||||
async function refresh() { const marker = ++generation.current; const result = await api<Center>('/model-center'); if (alive.current && marker === generation.current) setView(result); }
|
||||
async function act(action: () => Promise<void>) {
|
||||
setWorking(true); setNotice('');
|
||||
setWorking(true); setNotice(''); setNoticeFailure(null);
|
||||
try { await action(); if (alive.current) { await refresh(); if (credentialView) await refreshCredentials(); } }
|
||||
catch (cause) { if (alive.current) setNotice(cause instanceof Error ? cause.message : '操作未完成,请刷新核对。'); }
|
||||
catch (cause) { if (alive.current) { setNotice(cause instanceof Error ? cause.message : '操作未完成,请刷新核对。');
|
||||
if (cause && typeof cause === 'object' && 'diagnostic' in cause && cause.diagnostic) setNoticeFailure(cause.diagnostic as RuntimeFailure); } }
|
||||
finally { if (alive.current) setWorking(false); }
|
||||
}
|
||||
async function inspect(offset?: number) { const value = await api<Operations>('/operations' + (offset === undefined ? '' : '?artifactOffset=' + offset)); if (alive.current) setOperations(value); }
|
||||
@@ -65,7 +70,7 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
<nav ref={tabsRef} className="mc-tabs" aria-label="模型中心分类">{[['connections', '模型连接', Server], ['pools', '模型池', Layers3], ['waits', '等待时限', Settings2], ['attempts', '请求账本', Route], ['imports', '配置导入', Settings2], ['credentials', '凭据保存', KeyRound], ['operations', '运行巡检', Activity]].map(([id, title, Icon]) => {
|
||||
const Symbol = Icon as typeof Server; return <button key={id as string} type="button" aria-current={tab === id ? 'page' : undefined} onClick={() => { setTab(id as string); setNotice(''); }}><Symbol size={16} />{title as string}</button>;
|
||||
})}<button className="mc-refresh" type="button" disabled={working} onClick={() => void act(refresh)} aria-label="刷新模型中心"><RefreshCw size={16} /></button></nav>
|
||||
{notice && <div className="mc-notice" role="status">{notice}</div>}
|
||||
{notice && <div className="mc-notice" role="status">{notice}{noticeFailure && <ModelErrorDetails failure={noticeFailure} />}</div>}
|
||||
{busy && <p className="mc-notice">有任务正在执行;连接可停用,其他配置请在任务结束后修改。</p>}
|
||||
{!view ? <p className="mc-empty">正在读取实际连接状态…</p> : <div className="mc-content">
|
||||
{tab === 'waits' && <ModelWaitSettings key={JSON.stringify(view.waitSettings)} value={view.waitSettings} disabled={locked} onSave={value => void act(async () => {
|
||||
@@ -83,9 +88,9 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
<button className="button secondary" disabled={locked} onClick={() => { if (value.id === 'legacy-default') { onLegacy(); return; } setDraft({ ...initial, ...value, streaming: value.streaming === true, capabilityDeclarations: { ...initial.capabilityDeclarations, ...value.capabilityDeclarations } }); setKey(''); setPersistKey(false); setEditing(true); }}>编辑</button>
|
||||
{value.id !== 'legacy-default' && <button className="mc-icon-action" aria-label={(value.enabled ? '停用' : '启用') + value.name} disabled={working} onClick={() => void act(async () => { await api('/model-center/toggle', { id: value.id, enabled: !value.enabled }); })}><Power size={15} />{value.enabled ? '停用' : '启用'}</button>}
|
||||
<label className="mc-probe-scope">检测范围<select aria-label={value.name + '检测范围'} value={probeScopes[value.id] ?? 'text'} disabled={locked} onChange={event => setProbeScopes({ ...probeScopes, [value.id]: event.target.value })}><option value="text">文本响应</option><option value="tools">工具调用</option><option value="streaming">流式文本</option><option value="vision">合成图像识别</option></select></label>
|
||||
<button className="mc-probe" disabled={locked || !value.enabled} onClick={() => void act(async () => { const result = await api<{ scope: string }>('/model-center/probe', { id: value.id, scope: probeScopes[value.id] ?? 'text' }); if (alive.current) setNotice(capabilityLabels[result.scope] + '检测通过;只验证所选能力,原请求与用量已记入账本。'); })}>验证响应(调用模型)</button></footer>
|
||||
<button className="button secondary" disabled={locked} onClick={() => { setNativeTarget(value); setEditing(false); }}>原生参数</button><button className="mc-probe" disabled={locked || !value.enabled} onClick={() => void act(async () => { const result = await api<{ scope: string }>('/model-center/probe', { id: value.id, scope: probeScopes[value.id] ?? 'text' }); if (alive.current) setNotice(capabilityLabels[result.scope] + '检测通过;只验证所选能力,原请求与用量已记入账本。'); })}>验证响应(调用模型)</button></footer>
|
||||
</article>)}</div>
|
||||
{editing && <form className="mc-editor" aria-label="编辑模型连接" onSubmit={event => { event.preventDefault(); void act(async () => { await api('/model-center/connections', { ...draft, ...(key ? { apiKey: key } : {}), ...(persistKey ? { persistKey: true } : {}) }); if (alive.current) { setKey(''); setEditing(false); setNotice(persistKey ? '连接与本机加密凭据已一起保存。' : '连接已保存。'); } }); }}>
|
||||
{nativeTarget && <NativeParameters key={nativeTarget.id + ":" + nativeTarget.revision} connection={nativeTarget} busy={locked} onCancel={() => setNativeTarget(null)} onSave={async input => { await act(async () => { const result = await api<{ config: Config }>("/model-center/native-parameters", input); if (alive.current) { onConfig(result.config); setNativeTarget(null); setNotice("原生参数已保存;新任务按所选协议发送,旧请求保持原记录。"); } }); }} />}{editing && <form className="mc-editor" aria-label="编辑模型连接" onSubmit={event => { event.preventDefault(); void act(async () => { await api('/model-center/connections', { ...draft, ...(key ? { apiKey: key } : {}), ...(persistKey ? { persistKey: true } : {}) }); if (alive.current) { setKey(''); setEditing(false); setNotice(persistKey ? '连接与本机加密凭据已一起保存。' : '连接已保存。'); } }); }}>
|
||||
<h3>{draft.id ? '编辑模型连接' : '添加模型连接'}</h3><div className="mc-form-grid">
|
||||
<label>连接名称<input required value={draft.name} onChange={event => setDraft({ ...draft, name: event.target.value })} /></label>
|
||||
<label>服务品牌<select value={draft.brand} onChange={event => setDraft({ ...draft, brand: event.target.value })}>{['自定义', 'OpenAI', 'Claude 兼容代理', 'DeepSeek', 'Ollama', 'vLLM', '兼容服务'].map(brand => <option key={brand}>{brand}</option>)}</select></label>
|
||||
@@ -141,7 +146,7 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
|
||||
<fieldset><legend>模型池成员与权重</legend>{view.connections.map(connection => { const member = pool.members.find(value => value.connectionId === connection.id); return <label key={connection.id} className="mc-member-choice"><input type="checkbox" checked={!!member} onChange={event => setPool({ ...pool, members: event.target.checked ? [...pool.members, { connectionId: connection.id, weight: 1 }] : pool.members.filter(value => value.connectionId !== connection.id) })} /><span>{connection.name}<small>{connection.model || '尚未选择模型'} · {states[connection.status]}</small></span><input type="number" aria-label={connection.name + '权重'} disabled={!member} min="1" max="10" value={member?.weight ?? 1} onChange={event => setPool({ ...pool, members: pool.members.map(value => value.connectionId === connection.id ? { ...value, weight: Number(event.target.value) } : value) })} /></label>; })}</fieldset>
|
||||
<div className="mc-actions"><button className="button secondary" type="button" onClick={() => setPoolEditing(false)}>取消</button><button className="button primary" disabled={locked || !pool.members.length}>保存模型池</button></div></form>}
|
||||
</>}
|
||||
{tab === 'attempts' && <><div className="mc-section-title"><div><h3>请求账本</h3><p>最近100次物理请求;最多保存1000次。未知用量保持未知。</p></div><span>{view.attempts.length} 条记录</span></div>{!view.attempts.length && <p className="mc-empty">当前没有模型中心请求。选择连接或模型池执行任务后,实际尝试会保存在这里。</p>}<div className="mc-attempts">{view.attempts.map(value => <article key={value.id}><div><span className={'mc-state mc-state-' + value.status}>{states[value.status]}</span><strong>{value.model}</strong><time>{new Date(value.startedAt).toLocaleString()}</time></div><p>{view.connections.find(connection => connection.id === value.connectionId)?.name || '未派发'} · {value.protocol === 'responses' ? 'Responses' : value.protocol ? 'Chat Completions' : '能力筛选'}{value.streaming ? ' · 流式' : ''} · {value.ordinal ? '尝试 ' + value.ordinal : '未发送'} · {value.usageStatus === 'reported' ? `${value.usage?.prompt_tokens ?? '?'} 输入 / ${value.usage?.completion_tokens ?? '?'} 输出 tokens` : value.usageStatus === 'not_dispatched' ? '未产生请求用量' : '用量未知'}</p><code>Run {value.runId}</code>{value.stream && <div className="mc-stream-receipt"><p>{value.stream.terminal ? "响应完整终结" : "响应未完整,片段保留待核对"} · 已接收 {value.stream.bytes.toLocaleString()} bytes · {value.stream.frames} 个事件{value.stream.capturedBytes !== undefined && value.stream.capturedBytes < value.stream.bytes ? " · 仅保存上限内片段" : ""}</p><code>原流 SHA256 {value.stream.sha256}</code>{value.responseSha256 && <code>工件 SHA256 {value.responseSha256}{value.responseRedacted ? "(已脱敏)" : ""}</code>}{value.sessionId && value.responseArtifactId && <button className="button secondary" disabled={working} onClick={() => void act(async () => { await downloadArtifact(value.sessionId!, value.responseArtifactId!, "model-stream-" + value.id + ".sse"); })}>下载已保存的响应</button>}</div>}{value.reason && <p className="mc-card-warning">{value.reason}</p>}{!!value.rejections?.length && <p className="mc-route-rejections">{value.rejections.map(item => item.connectionName + ':' + (capabilityLabels[item.capability] ?? item.capability) + ' · ' + item.reason).join(';')}</p>}</article>)}</div></>}
|
||||
{tab === 'attempts' && <><div className="mc-section-title"><div><h3>请求账本</h3><p>最近100次物理请求;最多保存1000次。未知用量保持未知。</p></div><span>{view.attempts.length} 条记录</span></div>{!view.attempts.length && <p className="mc-empty">当前没有模型中心请求。选择连接或模型池执行任务后,实际尝试会保存在这里。</p>}<div className="mc-attempts">{view.attempts.map(value => <article key={value.id}><div><span className={'mc-state mc-state-' + value.status}>{states[value.status]}</span><strong>{value.model}</strong><time>{new Date(value.startedAt).toLocaleString()}</time></div><p>{view.connections.find(connection => connection.id === value.connectionId)?.name || '未派发'} · {value.protocol === 'responses' ? 'Responses' : value.protocol ? 'Chat Completions' : '能力筛选'}{value.streaming ? ' · 流式' : ''} · {value.ordinal ? '尝试 ' + value.ordinal : '未发送'} · {value.usageStatus === 'reported' ? `${value.usage?.prompt_tokens ?? '?'} 输入 / ${value.usage?.completion_tokens ?? '?'} 输出 tokens` : value.usageStatus === 'not_dispatched' ? '未产生请求用量' : '用量未知'}</p><code>Run {value.runId}</code>{value.stream && <div className="mc-stream-receipt"><p>{value.stream.terminal ? "响应完整终结" : "响应未完整,片段保留待核对"} · 已接收 {value.stream.bytes.toLocaleString()} bytes · {value.stream.frames} 个事件{value.stream.capturedBytes !== undefined && value.stream.capturedBytes < value.stream.bytes ? " · 仅保存上限内片段" : ""}</p><code>原流 SHA256 {value.stream.sha256}</code>{value.responseSha256 && <code>工件 SHA256 {value.responseSha256}{value.responseRedacted ? "(已脱敏)" : ""}</code>}{value.sessionId && value.responseArtifactId && <button className="button secondary" disabled={working} onClick={() => void act(async () => { await downloadArtifact(value.sessionId!, value.responseArtifactId!, "model-stream-" + value.id + ".sse"); })}>下载已保存的响应</button>}</div>}{value.reason && <p className="mc-card-warning">{value.reason}</p>}{value.diagnostic && <ModelErrorDetails failure={value.diagnostic} />}{!value.diagnostic && value.upstreamResponse?.requestId && <p className="mc-help">上游请求 ID:{value.upstreamResponse.requestId}</p>}{value.requestArtifactId && value.sessionId && <button className="button secondary" disabled={working} onClick={() => void act(async () => { await downloadArtifact(value.sessionId!, value.requestArtifactId!, "model-request-" + value.id + ".json"); })}>下载实际请求</button>}{!!value.rejections?.length && <p className="mc-route-rejections">{value.rejections.map(item => item.connectionName + ':' + (capabilityLabels[item.capability] ?? item.capability) + ' · ' + item.reason).join(';')}</p>}</article>)}</div></>}
|
||||
{tab === 'imports' && <><div className="mc-section-title"><div><h3>已有配置导入</h3><p>先预览来源和模型,再导入。原客户端配置保持原样。</p></div><button className="button secondary" disabled={working} onClick={() => void act(async () => { const value = await api<{ sources: typeof discovery }>('/model-center/imports'); if (alive.current) setDiscovery(value.sources); })}>识别本机来源</button></div>
|
||||
{view.imports.length > 0 && <><h4>已登记来源</h4>{view.imports.map(value => <article className="mc-import-source" key={value.id}><strong>{value.source}</strong><code>{value.path}</code><span>{value.enabled ? '每分钟检查' : '自动同步已关闭'}</span>{value.lastCheckedAt && <p>最近检查 {new Date(value.lastCheckedAt).toLocaleString()}</p>}{value.lastError && <p className="mc-card-warning">{value.lastError}</p>}{!!value.lastResult?.conflicts?.length && <p className="mc-card-warning">有 {value.lastResult.conflicts.length} 项与手动修改冲突,已保留工作台中的修改。</p>}<button className="button secondary" disabled={locked} onClick={() => void act(async () => { await api('/model-center/import-toggle', { id: value.id, enabled: !value.enabled }); })}>{value.enabled ? '关闭自动同步' : '启用自动同步'}</button></article>)}<button className="button secondary" disabled={locked || !view.imports.some(value => value.enabled)} onClick={() => void act(async () => { const result = await api<{ results: { imported?: string[]; conflicts?: string[]; error?: string }[] }>('/model-center/import-sync', {}); const imported = result.results.reduce((sum, value) => sum + (value.imported?.length ?? 0), 0); const conflicts = result.results.reduce((sum, value) => sum + (value.conflicts?.length ?? 0), 0); if (alive.current) setNotice(`已检查启用来源:更新 ${imported} 项,冲突 ${conflicts} 项。`); })}>立即同步已启用来源</button></>}
|
||||
{discovery.map(value => <article className="mc-import-source" key={value.source}><strong>{value.source}</strong><code>{value.path || '当前版本尚无可发现的配置路径'}</code><span>{value.available ? value.supported ? '可预览' : '当前不支持' : '未发现'}</span>{value.detail && <p>{value.detail}</p>}<button className="button secondary" disabled={!value.available || !value.supported || locked} onClick={() => { setImportSource(value.source); setImportPath(value.path); }}>选择来源</button></article>)}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
.model-error-details{font-size:12px;line-height:1.7;margin-top:10px;min-width:0;color:#344e40}
|
||||
.model-error-details summary{cursor:pointer;font-weight:600;color:#365d48;padding:6px 0}
|
||||
.model-error-details summary:focus-visible,.model-error-details pre:focus-visible{outline:2px solid #4c795e;outline-offset:3px}
|
||||
.model-error-details dl{display:grid;grid-template-columns:minmax(84px,128px) minmax(0,1fr);gap:6px 14px;margin:12px 0}
|
||||
.model-error-details dt{color:#56695b}.model-error-details dd{margin:0;min-width:0;overflow-wrap:anywhere}
|
||||
.model-error-details pre{max-height:280px;max-width:100%;overflow:auto;white-space:pre-wrap;overflow-wrap:anywhere;padding:12px;background:#f2f4f1;border:1px solid #d5ded5;border-radius:5px;font-size:12px;line-height:1.7;color:#2c4134}
|
||||
.model-error-caption,.model-error-hash{font-size:11px;overflow-wrap:anywhere;color:#526556}.model-error-hash code{font-size:10px}
|
||||
.model-error-details .button{margin:6px 0;max-width:100%;min-height:38px}
|
||||
@media(max-width:560px){.model-error-details dl{grid-template-columns:1fr;gap:2px}.model-error-details dd{margin-bottom:7px}.model-error-details pre{padding:9px}}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { useState } from 'react';
|
||||
import type { RuntimeFailure } from './types';
|
||||
import { downloadArtifact } from './api';
|
||||
import './ModelErrorDetails.css';
|
||||
|
||||
const decisions: Record<string, string> = { retry_after_wait: '按等待时间重试', stream_not_replayed: '流式响应已开始,保留原片段并停止自动重发',
|
||||
not_retryable: '该错误不在同一线路自动重试', attempt_limit: '达到本次尝试上限', wait_budget: '剩余等待预算不足,保持上游建议时间并结束本次请求' };
|
||||
|
||||
export function ModelErrorDetails({ failure }: { failure: RuntimeFailure }) {
|
||||
const upstream = failure.upstream;
|
||||
const requestId = upstream?.requestId ?? failure.upstreamRequestId;
|
||||
const [notice, setNotice] = useState(''), [busy, setBusy] = useState(false);
|
||||
const origin = failure.origin === 'upstream' ? '上游模型服务' : failure.origin === 'local' || failure.source === 'workbench' ? '工作台本地' : '旧记录未保存具体层级';
|
||||
return <details className="model-error-details"><summary>{upstream ? '查看上游原文与请求信息' : '查看错误详情'}</summary>
|
||||
<dl><dt>错误来源</dt><dd>{origin}</dd>
|
||||
{failure.model && <><dt>实际模型</dt><dd>{failure.model}</dd></>}
|
||||
{failure.endpoint && <><dt>实际接口</dt><dd>{failure.endpoint}{failure.route}</dd></>}
|
||||
{requestId && <><dt>上游请求 ID</dt><dd><code>{requestId}</code></dd></>}
|
||||
{upstream?.responseId && <><dt>上游响应 ID</dt><dd><code>{upstream.responseId}</code></dd></>}
|
||||
{failure.wireAttemptId && <><dt>本地物理尝试 ID</dt><dd><code>{failure.wireAttemptId}</code></dd></>}
|
||||
{upstream?.code && <><dt>上游 code</dt><dd><code>{upstream.code}</code></dd></>}
|
||||
{upstream?.type && <><dt>上游 type</dt><dd><code>{upstream.type}</code></dd></>}
|
||||
{upstream?.param && <><dt>上游参数位置</dt><dd>{upstream.param}</dd></>}
|
||||
{upstream?.eventType && <><dt>上游事件</dt><dd>{upstream.eventType}</dd></>}
|
||||
{upstream?.reason && <><dt>上游结束原因</dt><dd>{upstream.reason}</dd></>}
|
||||
{upstream?.retryAfter !== null && upstream?.retryAfter !== undefined && <><dt>原 Retry-After</dt><dd><code>{upstream.retryAfter}</code>{upstream.retryAfterMs !== null && upstream.retryAfterMs !== undefined ? ' · ' + upstream.retryAfterMs / 1000 + ' 秒' : ' · 未能解析,已停止自动重试'}</dd></>}
|
||||
{failure.limitReason === 'unspecified' && <><dt>限额类型</dt><dd>上游未提供明确原因</dd></>}
|
||||
{failure.retryDecision && <><dt>重试处理</dt><dd>{decisions[failure.retryDecision] ?? failure.retryDecision}</dd></>}
|
||||
</dl>
|
||||
{!upstream && failure.detail && <p>{failure.detail}</p>}
|
||||
{upstream?.rawPreview !== undefined && <><p className="model-error-caption">上游原文{upstream.scope === 'sse-event' ? ' · 失败事件' : ' · 响应正文'}{upstream.capture?.redacted ? ' · 已脱敏' : ''}{upstream.previewTruncated ? ' · 此处显示前 8000 字符' : ''}</p>
|
||||
<pre tabIndex={0} aria-label="上游错误原文">{upstream.rawPreview || '(空响应)'}</pre>
|
||||
{upstream.capture && <p className="model-error-caption">已接收 {upstream.capture.receivedBytes.toLocaleString()} 字节,保留前 {upstream.capture.capturedBytes.toLocaleString()} 字节。{upstream.capture.complete ? '该范围完整接收。' : upstream.capture.truncated ? '已达到保存上限,仅保留前缀;完整长度未知。' : '接收中断,原文可能不完整。'}{upstream.artifactScope === 'sse-stream' ? '下载文件包含已保存的整段事件流。' : ''}</p>}
|
||||
</>}
|
||||
{!!upstream?.shortenedFields?.length && <p className="model-error-caption">部分诊断字段已缩短,详见保存的原文。</p>}
|
||||
{upstream?.capture?.retention === 'preview-only' && <p className="model-error-caption">此请求未绑定工程任务,仅保留以上诊断预览;完整原文未归档为任务工件。</p>}
|
||||
{upstream?.artifactSha256 && <p className="model-error-hash">工件 SHA-256 <code>{upstream.artifactSha256}</code></p>}
|
||||
{upstream?.artifactId && upstream.sessionId && <button className="button secondary" type="button" disabled={busy} onClick={async () => {
|
||||
setBusy(true); setNotice('');
|
||||
try { await downloadArtifact(upstream.sessionId!, upstream.artifactId!, 'model-error-' + (failure.wireAttemptId ?? upstream.artifactId) + (upstream.artifactScope === 'sse-stream' ? '.sse' : '.txt')); setNotice('已下载保存的脱敏响应。'); }
|
||||
catch (cause) { setNotice(cause instanceof Error ? cause.message : '下载失败,请重试。'); }
|
||||
finally { setBusy(false); }
|
||||
}}>下载已保存的原文</button>}
|
||||
{notice && <p role="status">{notice}</p>}
|
||||
</details>;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { nativeParameterSettings } from '../shared/contracts/native-parameters.mjs';
|
||||
|
||||
export type NativeConnection = { id: string; revision: number; name: string; protocol: string; nativeParameters?: Record<string, unknown>; outputTokenField?: string };
|
||||
export function NativeParameters({ connection, busy, onCancel, onSave }: { connection: NativeConnection; busy: boolean; onCancel: () => void;
|
||||
onSave: (input: { id: string; revision: number; nativeParameters: Record<string, unknown>; outputTokenField: string }) => Promise<void> }) {
|
||||
const [text, setText] = useState(JSON.stringify(connection.nativeParameters ?? {}, null, 2));
|
||||
const [field, setField] = useState(connection.outputTokenField ?? (connection.protocol === 'responses' ? 'max_output_tokens' : 'max_tokens'));
|
||||
const [error, setError] = useState('');
|
||||
const form = useRef<HTMLFormElement>(null);
|
||||
useEffect(() => { form.current?.scrollIntoView({ block: 'nearest' }); }, []);
|
||||
return <form ref={form} className="mc-editor mc-native" aria-label="原生请求参数" onSubmit={async event => {
|
||||
event.preventDefault(); setError('');
|
||||
try { const settings = nativeParameterSettings(connection.protocol, JSON.parse(text), field); await onSave({ id: connection.id, revision: connection.revision, ...settings }); }
|
||||
catch (cause) { setError(cause instanceof Error ? cause.message : '参数格式无效。'); }
|
||||
}}>
|
||||
<h3>{connection.name} · 原生请求参数</h3>
|
||||
<p className="mc-help">参数按所选协议原字段发送,并封存在实际请求中。具体模型是否接受这些选项,以服务响应为准;未填写的选项采用上游默认值。</p>
|
||||
<label>输出上限字段<select aria-label="输出上限字段" value={field} disabled={busy} onChange={event => setField(event.target.value)}>{(connection.protocol === 'responses' ? ['max_output_tokens'] : ['max_tokens', 'max_completion_tokens']).map(value => <option key={value}>{value}</option>)}</select></label>
|
||||
<p className="mc-help">数值沿用当前任务和连接的输出预算,不允许原生参数覆盖预算、消息、工具或接口。</p>
|
||||
<label>原生参数 JSON<textarea aria-label="原生参数 JSON" rows={7} maxLength={8192} spellCheck={false} value={text} disabled={busy} onChange={event => setText(event.target.value)} /></label>
|
||||
<p className="mc-help">{connection.protocol === 'responses' ? '已接入 temperature、top_p、parallel_tool_calls、reasoning.effort、text.verbosity。' : '已接入 temperature、top_p、seed、parallel_tool_calls、reasoning_effort、verbosity。'}其他字段保存时会给出具体提示。语音、视频和服务端内置工具尚未接入。</p>
|
||||
{error && <p className="mc-card-warning" role="alert">{error}</p>}
|
||||
<div className="mc-actions"><button className="button secondary" type="button" disabled={busy} onClick={onCancel}>取消</button><button className="button primary" disabled={busy} type="submit">保存原生参数</button></div>
|
||||
</form>;
|
||||
}
|
||||
+1
-1
@@ -7,7 +7,7 @@ export async function api<T>(path: string, input?: unknown,method?:string): Prom
|
||||
...(input === undefined ? {} : { body: JSON.stringify(input) }),
|
||||
});
|
||||
const body = await response.json();
|
||||
if (!response.ok) throw new Error(body.error || '请求失败,请重试。');
|
||||
if (!response.ok) throw Object.assign(new Error(body.error || '请求失败,请重试。'), { status: response.status, diagnostic: body.diagnostic ?? null });
|
||||
return body as T;
|
||||
}
|
||||
export async function exportSession(id: string) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { ReactNode, ButtonHTMLAttributes } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { AlertTriangle, Check, ChevronRight, Circle, Clock3, FileCode2, LoaderCircle, RotateCw, ShieldQuestion, SquareTerminal, X, XCircle } from 'lucide-react';
|
||||
import type { Change, ToolEvent, RuntimeFailure } from './types';
|
||||
import { ModelErrorDetails } from './ModelErrorDetails';
|
||||
|
||||
const statusNames: Record<string, string> = {
|
||||
idle: '就绪', running: '执行中', awaiting_approval: '等待确认', needs_review: '待审查', completed: '已完成',
|
||||
@@ -109,7 +110,7 @@ export function FailureCard({ failure, onDiagnose, onRetry, busy = false }: { fa
|
||||
<p>{failure.message}</p>
|
||||
{failure.suggestion && <p className="failure-suggestion">{failure.suggestion}</p>}
|
||||
<div className="failure-meta"><span>{failure.stage === 'model_request' ? '发生在请求模型响应时' : '发生在工作台任务执行时'}</span>{failure.attempts && <span>{failure.attempts} 次请求</span>}{failure.httpStatus && <code>HTTP {failure.httpStatus}</code>}{failure.code && <code>{failure.code}</code>}</div>
|
||||
{failure.detail && failure.detail !== failure.message && <details><summary>查看错误详情</summary><p>{failure.detail}</p></details>}
|
||||
<ModelErrorDetails failure={failure} />
|
||||
<div className="failure-actions"><button type="button" className="button secondary" onClick={onDiagnose}>查看运行诊断</button>{onRetry && <button type="button" className="button primary" onClick={onRetry} disabled={busy}><RotateCw size={14} />{failure.source === 'model_connection' ? '重试模型请求' : '继续任务'}</button>}</div>
|
||||
</div>;
|
||||
}
|
||||
|
||||
@@ -53,6 +53,12 @@ export interface RuntimeFailure {
|
||||
source: string; stage: string; kind: string; title: string; message: string; messageId?: string;
|
||||
detail?: string; suggestion?: string; attempts?: number; httpStatus?: number; code?: string | null;
|
||||
occurredAt?: string; retryable?: boolean; historical?: boolean; endpoint?: string; model?: string;
|
||||
origin?: 'upstream' | 'local'; route?: string; type?: string | null; wireAttemptId?: string; upstreamRequestId?: string | null; retryAfterMs?: number; retryDecision?: string; limitReason?: string | null;
|
||||
upstream?: { schema: number; scope: string; httpStatus?: number; code?: string | null; type?: string | null; message?: string | null; param?: string | null;
|
||||
requestId?: string | null; responseId?: string | null; eventType?: string | null; reason?: string | null; headers?: Record<string, string>; retryAfter?: string | null;
|
||||
retryAfterMs?: number | null; retryAfterInvalid?: boolean; rawPreview?: string; previewTruncated?: boolean; shortenedFields?: string[];
|
||||
artifactId?: string; artifactSha256?: string; artifactScope?: string; sessionId?: string;
|
||||
capture?: { scope: string; receivedBytes: number; capturedBytes: number; storedBytes: number; retention?: string; complete: boolean; truncated: boolean; interrupted: boolean; redacted: boolean; sha256: string; capturedRange: number[] } };
|
||||
}
|
||||
export interface ExecutionEnvironment { platform: string; node: string; shell: string; encoding: string; shellSyntax: string }
|
||||
export interface RuntimeDiagnostics {
|
||||
|
||||
@@ -6,6 +6,8 @@ import { visionProbe } from './vision-probe.mjs';
|
||||
import { modelProtocol, capabilityDeclarations, capabilityView, capabilityRejections } from '../../shared/contracts/model-capabilities.mjs';
|
||||
import { actualRequest, requestVariants } from '../../shared/contracts/model-request.mjs';
|
||||
import { modelWaitSettings } from './request-deadline.mjs';
|
||||
import { sanitizeModelText } from './upstream-error.mjs';
|
||||
import { nativeParameterSettings } from '../../shared/contracts/native-parameters.mjs';
|
||||
|
||||
const legacyId = 'legacy-default';
|
||||
const stamp = () => new Date().toISOString();
|
||||
@@ -75,7 +77,8 @@ export class ModelHub {
|
||||
connections() {
|
||||
return [{ id: legacyId, name: '原模型连接', brand: '兼容服务', protocol: 'chat-completions', baseUrl: this.store.config.baseUrl,
|
||||
model: this.store.config.model, enabled: true, requiresKey: false, revision: this.store.config.configRevision ?? 1,
|
||||
contextLimit: this.store.config.contextLimit, maxOutputTokens: this.store.config.maxOutputTokens, concurrency: 1, source: 'legacy' }, ...this.state.connections];
|
||||
contextLimit: this.store.config.contextLimit, maxOutputTokens: this.store.config.maxOutputTokens, concurrency: 1, source: 'legacy',
|
||||
...(this.store.config.nativeParameters ? { nativeParameters: this.store.config.nativeParameters, outputTokenField: this.store.config.outputTokenField } : {}) }, ...this.state.connections];
|
||||
}
|
||||
credential(connection) { return connection.id === legacyId ? { key: this.provider.apiKey, revision: this.provider.legacyCredentialRevision ?? 1 } : this.provider.credentials.get(connection.id); }
|
||||
vault() { return this.credentialVault ??= new CredentialVault(this.store, this.provider); }
|
||||
@@ -166,6 +169,8 @@ export class ModelHub {
|
||||
if (this.state.connections.length >= 100 && !previous) throw error('最多保存100个模型连接。');
|
||||
if (previous && input.revision !== previous.revision) throw error('模型连接已修改,请刷新后重试。', 409);
|
||||
const protocol = modelProtocol(input.protocol ?? previous?.protocol ?? 'chat-completions');
|
||||
const native = Object.hasOwn(input, 'nativeParameters') || Object.hasOwn(input, 'outputTokenField') || previous?.nativeParameters
|
||||
? nativeParameterSettings(protocol, input.nativeParameters ?? previous?.nativeParameters ?? {}, input.outputTokenField ?? previous?.outputTokenField) : null;
|
||||
const declarations = capabilityDeclarations(input.capabilityDeclarations, previous?.capabilityDeclarations);
|
||||
if (input.streaming !== undefined && typeof input.streaming !== 'boolean') throw error('流式选项必须为开启或关闭。');
|
||||
const streaming = input.streaming ?? previous?.streaming ?? false;
|
||||
@@ -173,7 +178,7 @@ export class ModelHub {
|
||||
const name = label(input.name), model = label(input.model, 200);
|
||||
if (!name || !model) throw error('请填写连接名称和实际模型名称。');
|
||||
const connection = { id: previous?.id ?? randomUUID(), name, brand: label(input.brand || '自定义', 40), protocol, model,
|
||||
capabilitySchema: 1, capabilityDeclarations: declarations, streaming,
|
||||
capabilitySchema: 1, capabilityDeclarations: declarations, streaming, ...(native ?? {}),
|
||||
baseUrl: modelEndpoint(input.baseUrl), enabled: input.enabled !== false, requiresKey: input.requiresKey !== false,
|
||||
contextLimit: bounded(input.contextLimit, 16000, 1000, 200000), maxOutputTokens: bounded(input.maxOutputTokens, 2048, 256, 16000),
|
||||
concurrency: bounded(input.concurrency, 1, 1, 8), revision: (previous?.revision ?? 0) + 1,
|
||||
@@ -191,6 +196,14 @@ export class ModelHub {
|
||||
}
|
||||
return this.status(connection);
|
||||
}
|
||||
setNativeParameters(input) {
|
||||
const connection = this.connection(input.id);
|
||||
if (connection.revision !== input.revision) throw error('连接已变化,请刷新后核对原生参数。', 409);
|
||||
const settings = nativeParameterSettings(connection.protocol, input.nativeParameters, input.outputTokenField);
|
||||
if (connection.id === legacyId) this.save(settings);
|
||||
else this.upsert({ ...connection, ...settings });
|
||||
return { center: this.view(), config: { ...this.store.config, hasKey: this.provider.hasKey() } };
|
||||
}
|
||||
toggle(id, enabled) {
|
||||
if (id === legacyId) throw error('原连接不能停用,请选择离线演示或其他连接。');
|
||||
const value = this.connection(id); value.enabled = enabled === true; value.revision++; value.updatedAt = stamp(); this.save(); return this.status(value);
|
||||
@@ -230,6 +243,7 @@ export class ModelHub {
|
||||
credentialRevision: this.credential(this.connection(value.connectionId))?.revision ?? 0 }));
|
||||
if (!members.some(value => this.status(value).status === 'configured' || this.status(value).status === 'cooling')) throw error('没有可用模型连接,请核对启用状态、临时凭据和故障。');
|
||||
return { ...config, baseUrl: members[0].baseUrl, model: members[0].model, protocol: modelProtocol(members[0].protocol), streaming: members[0].streaming === true,
|
||||
nativeParameters: members[0].nativeParameters ?? {}, outputTokenField: members[0].outputTokenField,
|
||||
contextLimit: Math.min(config.contextLimit, ...members.map(value => value.contextLimit)), maxOutputTokens: Math.min(config.maxOutputTokens, ...members.map(value => value.maxOutputTokens)),
|
||||
modelRouting: { type: selection.type, id: selection.id, revision: pool?.revision ?? members[0].revision,
|
||||
strategy: pool?.strategy ?? 'round-robin', sticky: pool?.sticky ?? true, maxAttempts: pool?.maxAttempts ?? 1, queueTimeoutMs: pool?.queueTimeoutMs ?? 15000, members } };
|
||||
@@ -310,6 +324,8 @@ export class ModelHub {
|
||||
}
|
||||
this.attempts.push(attempt); this.saveAttempts();
|
||||
const progress = event => {
|
||||
if (event.wireAttemptId) attempt.wireAttemptId = event.wireAttemptId;
|
||||
if (event.upstreamResponse) attempt.upstreamResponse = event.upstreamResponse;
|
||||
if (event.consumption) {
|
||||
attempt.usage = event.consumption.usage; attempt.usageStatus = event.consumption.usageStatus;
|
||||
onProgress?.({ consumption: { ...event.consumption, wireAttemptId: event.consumption.attemptId, attemptId: attempt.id,
|
||||
@@ -317,16 +333,21 @@ export class ModelHub {
|
||||
this.saveAttempts();
|
||||
return;
|
||||
}
|
||||
if (event.streamCapture !== undefined) {
|
||||
attempt.stream = event.stream;
|
||||
if (event.streamCapture !== undefined || event.responseCapture !== undefined) {
|
||||
if (event.stream) attempt.stream = event.stream;
|
||||
if (plan?.sessionId) {
|
||||
const session = this.store.session(plan.sessionId), run = this.store.owned('run', plan.runId, session.id);
|
||||
const raw = event.streamCapture, decoded = raw.toString('utf8'), redacted = this.provider.redact(decoded);
|
||||
const raw = event.streamCapture ?? event.responseCapture, decoded = raw.toString('utf8'), redacted = sanitizeModelText(decoded, value => this.provider.redact(value));
|
||||
const captured = redacted === decoded ? raw : Buffer.from(redacted, 'utf8');
|
||||
const artifact = this.store.artifactStore.write(session, run, captured, { kind: 'model-stream-response', mediaType: 'text/event-stream' });
|
||||
const artifact = this.store.artifactStore.write(session, run, captured, { kind: event.streamCapture !== undefined ? 'model-stream-response' : 'model-http-response',
|
||||
mediaType: event.streamCapture !== undefined ? 'text/event-stream' : 'text/plain' });
|
||||
this.store.core.artifact.set(artifact.id, artifact); attempt.responseArtifactId = artifact.id;
|
||||
attempt.responseSha256 = artifact.sha256; attempt.responseRedacted = redacted !== decoded; this.store.save(session);
|
||||
attempt.responseSha256 = artifact.sha256; attempt.responseRedacted = redacted !== decoded || event.upstream?.capture?.redacted === true; this.store.save(session);
|
||||
if (event.upstream) Object.assign(event.upstream, { artifactId: artifact.id, artifactSha256: artifact.sha256,
|
||||
artifactScope: event.streamCapture !== undefined ? 'sse-stream' : 'http-body', sessionId: session.id });
|
||||
if (event.upstream?.capture) Object.assign(event.upstream.capture, { retention: 'artifact', storedBytes: artifact.bytes });
|
||||
}
|
||||
if (event.upstream) attempt.upstream = event.upstream;
|
||||
return;
|
||||
}
|
||||
if (event.streaming) attempt.partial = { sequence: event.streamSequence, bytes: event.streamBytes, textCharacters: event.textCharacters, textPreview: this.provider.redact(event.textPreview ?? '').slice(0, 8000), toolFragments: event.toolFragments };
|
||||
@@ -340,6 +361,7 @@ export class ModelHub {
|
||||
return result;
|
||||
} catch (cause) {
|
||||
const diagnostic = cause.diagnostic;
|
||||
if (diagnostic) attempt.diagnostic = diagnostic;
|
||||
attempt.status = signal?.aborted ? 'interrupted' : diagnostic?.outcome === 'unknown' ? 'unknown' : 'failed';
|
||||
attempt.reason = this.provider.redact(cause.message).slice(0, 600); attempt.httpStatus = diagnostic?.status ?? diagnostic?.httpStatus ?? null;
|
||||
const health = this.health.get(this.identity(member)) ?? {};
|
||||
@@ -348,14 +370,14 @@ export class ModelHub {
|
||||
// the attempt, release its lease, and leave the connection eligible.
|
||||
this.health.set(this.identity(member), { ...health, state: 'interrupted', lastInterruptedAt: stamp() });
|
||||
} else {
|
||||
const blocked = [401, 403].includes(attempt.httpStatus);
|
||||
const blocked = [401, 403].includes(attempt.httpStatus) || diagnostic?.kind === 'quota';
|
||||
const failedAt = Date.now();
|
||||
this.health.set(this.identity(member), { state: blocked ? 'needs_attention' : 'failed', blocked, lastFailureAt: new Date(failedAt).toISOString(),
|
||||
failures: (health.failures ?? 0) + 1, cooldownUntil: failedAt + (diagnostic?.retryAfterMs ?? Math.min(60000, 1000 * 2 ** Math.min(health.failures ?? 0, 5))), reason: attempt.reason });
|
||||
}
|
||||
lastError = cause; excluded.add(member.id); this.sticky.delete(runId);
|
||||
signal?.throwIfAborted();
|
||||
if (!diagnostic?.retryable || count === routing.maxAttempts) throw cause;
|
||||
if ((!diagnostic?.retryable && !diagnostic?.failoverAllowed) || count === routing.maxAttempts) throw cause;
|
||||
onProgress?.({ phase: 'retrying', attempt: count, maxAttempts: routing.maxAttempts, connectionId: member.id, modelAttemptId: attempt.id,
|
||||
diagnostic, waitMs: 0, switching: true });
|
||||
} finally { attempt.finishedAt = stamp(); lease.release(); this.saveAttempts(); }
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { protocolFailure, normalizeProtocolResponse, validateModelResponse, reportedUsage } from './protocols.mjs';
|
||||
import { httpFailure } from '../../server/diagnostics.mjs';
|
||||
import { upstreamCapture } from './upstream-error.mjs';
|
||||
|
||||
// Only a fully terminated and validated stream produces a dispatchable message.
|
||||
// Progress is cumulative, bounded and explicitly not a tool invocation.
|
||||
export async function readModelStream(response, config, signal, onProgress, tools, onBytes) {
|
||||
export async function readModelStream(response, config, signal, onProgress, tools, onBytes, redact = String) {
|
||||
if (!response.headers.get('content-type')?.toLowerCase().includes('text/event-stream') || !response.body) throw protocolFailure('服务未返回所选协议的事件流,请核对流式配置。');
|
||||
const reader = response.body.getReader(), decoder = new TextDecoder('utf-8', { fatal: true }), hash = createHash('sha256');
|
||||
const chunks = [];
|
||||
let buffer = '', data = [], bytes = 0, frames = 0, sequence = -1, text = '', done = false, terminal = null, usage = null, finish = null;
|
||||
let lastProgress = 0, responseId = null, validated = false, progressTimer = null, progressFailure = null;
|
||||
let upstreamFailure = null;
|
||||
const calls = new Map(), responseCalls = new Map();
|
||||
const progress = force => {
|
||||
if (!force && Date.now() - lastProgress < 100) {
|
||||
@@ -30,7 +33,18 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
if (raw === '[DONE]') { if (config.protocol === 'responses') throw protocolFailure('Responses缺少正式终结事件。'); done = true; return; }
|
||||
let event;
|
||||
try { event = JSON.parse(raw); } catch { throw protocolFailure('模型事件流包含无效JSON。'); }
|
||||
if (event.error || event.type === 'error') throw protocolFailure('模型流返回错误,已收到片段保留且不会自动重发。', 'stream_interrupted');
|
||||
if (event.usage) usage = event.usage;
|
||||
const failUpstream = () => {
|
||||
usage = event.response?.usage ?? usage;
|
||||
const { details } = upstreamCapture({ raw: Buffer.from(raw, 'utf8'), event, scope: 'sse-event', status: response.status, headers: response.headers }, redact);
|
||||
details.responseId ??= responseId;
|
||||
upstreamFailure = httpFailure(response.status, (details.message ?? details.reason ?? '上游事件 ' + event.type).slice(0, 600), { upstream: details });
|
||||
if (event.type === 'response.incomplete') {
|
||||
upstreamFailure.diagnostic.kind = 'incomplete'; upstreamFailure.diagnostic.title = '上游模型响应未完整结束';
|
||||
}
|
||||
throw upstreamFailure;
|
||||
};
|
||||
if (event.error || event.type === 'error') failUpstream();
|
||||
if (config.protocol === 'responses') {
|
||||
if (event.sequence_number !== undefined) {
|
||||
if (!Number.isSafeInteger(event.sequence_number) || event.sequence_number <= sequence) throw protocolFailure('Responses事件序列重复或逆序。');
|
||||
@@ -54,8 +68,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
if (!event.response || responseId && event.response.id !== responseId) throw protocolFailure('Responses终结身份与创建身份不一致。');
|
||||
terminal = event.response;
|
||||
} else if (['response.failed', 'response.incomplete', 'response.cancelled'].includes(event.type)) {
|
||||
usage = event.response?.usage ?? usage;
|
||||
throw protocolFailure('Responses流未完整结束,工具未执行。', 'stream_interrupted');
|
||||
failUpstream();
|
||||
}
|
||||
} else {
|
||||
if (!Array.isArray(event.choices)) throw protocolFailure('Chat流缺少choices数组。');
|
||||
@@ -135,7 +148,8 @@ export async function readModelStream(response, config, signal, onProgress, tool
|
||||
} finally {
|
||||
if (progressTimer) clearTimeout(progressTimer);
|
||||
try { await reader.cancel(); } catch {} reader.releaseLock();
|
||||
onProgress?.({ streamCapture: Buffer.concat(chunks), reportedUsage: reportedUsage(config, { usage: terminal?.usage ?? usage }), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
|
||||
sha256: hash.digest('hex'), terminal: validated, terminalSeen: !!terminal || done, protocol: config.protocol ?? 'chat-completions' } });
|
||||
onProgress?.({ streamCapture: Buffer.concat(chunks), ...(upstreamFailure ? { upstream: upstreamFailure.diagnostic.upstream } : {}),
|
||||
reportedUsage: reportedUsage(config, { usage: terminal?.usage ?? usage }), stream: { bytes, capturedBytes: chunks.reduce((sum, item) => sum + item.length, 0), frames,
|
||||
sha256: hash.digest('hex'), terminal: validated, terminalSeen: !!terminal || done || !!upstreamFailure, protocol: config.protocol ?? 'chat-completions' } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ModelRequestError } from '../../server/diagnostics.mjs';
|
||||
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', stage: 'model_request',
|
||||
export const protocolFailure = (message, kind = 'protocol') => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request',
|
||||
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString() });
|
||||
export const protocolRoute = config => config.protocol === 'responses' ? '/responses' : '/chat/completions';
|
||||
|
||||
@@ -55,9 +55,9 @@ export function normalizeProtocolResponse(config, value) {
|
||||
for (const content of item.content) {
|
||||
if (content.type === 'output_text' && typeof content.text === 'string') texts.push(content.text);
|
||||
else if (content.type === 'refusal' && typeof content.refusal === 'string') texts.push(content.refusal);
|
||||
else throw protocolFailure('Responses包含当前尚未接入的输出内容类型。');
|
||||
else throw protocolFailure('Responses包含当前尚未接入的输出内容类型:' + String(content?.type ?? '未标注').slice(0, 100) + '。');
|
||||
}
|
||||
} else if (item.type !== 'reasoning') throw protocolFailure('Responses包含当前尚未接入的服务端工具或输出类型。');
|
||||
} else if (item.type !== 'reasoning') throw protocolFailure('Responses包含当前尚未接入的服务端工具或输出类型:' + String(item.type ?? '未标注').slice(0, 100) + '。');
|
||||
}
|
||||
const raw = value.usage, usage = raw ? { prompt_tokens: raw.input_tokens, completion_tokens: raw.output_tokens,
|
||||
...(raw.total_tokens !== undefined ? { total_tokens: raw.total_tokens } : {}), raw, protocol: 'responses' } : null;
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
export const ERROR_CAPTURE_LIMIT = 64 * 1024;
|
||||
const PREVIEW_LIMIT = 8000;
|
||||
const allowedHeaders = ['content-type', 'retry-after', 'date', 'x-request-id', 'request-id', 'requestid', 'x-amzn-requestid',
|
||||
'anthropic-request-id', 'x-ratelimit-limit-requests', 'x-ratelimit-remaining-requests', 'x-ratelimit-reset-requests',
|
||||
'x-ratelimit-limit-tokens', 'x-ratelimit-remaining-tokens', 'x-ratelimit-reset-tokens'];
|
||||
|
||||
export function sanitizeModelText(value, redact = String) {
|
||||
return redact(String(value))
|
||||
.replace(/("(?:api[_-]?key|authorization|access[_-]?token|refresh[_-]?token|password|secret|cookie|set-cookie)"\s*:\s*")((?:\\.|[^"\\])*)(")/gi, '$1[已隐藏]$3')
|
||||
.replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, 'Bearer [已隐藏]');
|
||||
}
|
||||
|
||||
export function retryAfterMilliseconds(value, now = Date.now()) {
|
||||
if (!value) return null;
|
||||
const milliseconds = /^\d+$/.test(value) ? Number(value) * 1000 : Date.parse(value) - now;
|
||||
return Number.isSafeInteger(milliseconds) ? Math.max(0, milliseconds) : null;
|
||||
}
|
||||
|
||||
// Captures are bounded independently of the generated-response limit. An
|
||||
// interrupted or truncated prefix never claims to be the complete response.
|
||||
export async function readErrorResponse(response, signal) {
|
||||
if (!response.body) return { raw: Buffer.alloc(0), receivedBytes: 0, truncated: false };
|
||||
const reader = response.body.getReader(), parts = [];
|
||||
let receivedBytes = 0, capturedBytes = 0, truncated = false, interrupted = false;
|
||||
try {
|
||||
while (true) {
|
||||
signal?.throwIfAborted();
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
receivedBytes += value.length;
|
||||
const keep = Math.min(value.length, ERROR_CAPTURE_LIMIT - capturedBytes);
|
||||
if (keep) { parts.push(Buffer.from(value.subarray(0, keep))); capturedBytes += keep; }
|
||||
if (receivedBytes > ERROR_CAPTURE_LIMIT) { truncated = true; break; }
|
||||
}
|
||||
} catch (cause) {
|
||||
interrupted = true;
|
||||
} finally {
|
||||
try { await reader.cancel(); } catch {}
|
||||
reader.releaseLock();
|
||||
}
|
||||
return { raw: Buffer.concat(parts), receivedBytes, truncated, ...(interrupted ? { interrupted: true } : {}) };
|
||||
}
|
||||
|
||||
export function upstreamCapture({ raw, receivedBytes = raw.length, truncated = false, interrupted = false, status,
|
||||
headers, event, scope = 'http-body' }, redact = String) {
|
||||
const bounded = raw.subarray(0, ERROR_CAPTURE_LIMIT), decoded = bounded.toString('utf8');
|
||||
const text = sanitizeModelText(decoded, redact), changed = text !== decoded;
|
||||
const capture = changed ? Buffer.from(text, 'utf8') : Buffer.from(bounded);
|
||||
let parsed = event;
|
||||
if (!parsed) { try { parsed = JSON.parse(decoded); } catch {} }
|
||||
const original = parsed?.response?.error ?? parsed?.error;
|
||||
const fields = original && typeof original === 'object' ? original : parsed;
|
||||
const shortenedFields = [];
|
||||
const field = (name, value, limit = 512) => {
|
||||
if (typeof value !== 'string' && typeof value !== 'number') return null;
|
||||
const clean = sanitizeModelText(value, redact);
|
||||
if (clean.length > limit) shortenedFields.push(name);
|
||||
return clean.slice(0, limit);
|
||||
};
|
||||
const diagnosticHeaders = {};
|
||||
for (const name of allowedHeaders) {
|
||||
const value = headers?.get(name);
|
||||
if (value !== null && value !== undefined) diagnosticHeaders[name] = field('header:' + name, value, 1024);
|
||||
}
|
||||
const retryAfter = diagnosticHeaders['retry-after'] ?? null;
|
||||
const retryAfterMs = retryAfterMilliseconds(retryAfter);
|
||||
const details = {
|
||||
schema: 1, scope, httpStatus: status,
|
||||
code: field('code', fields?.code, 160), type: field('type', fields?.type ?? (parsed?.type === 'error' ? null : parsed?.type), 160),
|
||||
message: field('message', fields?.message ?? (typeof original === 'string' ? original : null), 4096),
|
||||
param: field('param', fields?.param), reason: field('reason', parsed?.response?.incomplete_details?.reason ?? parsed?.incomplete_details?.reason),
|
||||
requestId: diagnosticHeaders['x-request-id'] ?? diagnosticHeaders['request-id'] ?? diagnosticHeaders.requestid
|
||||
?? diagnosticHeaders['anthropic-request-id'] ?? diagnosticHeaders['x-amzn-requestid']
|
||||
?? field('requestId', parsed?.request_id ?? fields?.request_id ?? parsed?.requestId),
|
||||
responseId: field('responseId', parsed?.response?.id ?? (parsed?.object === 'response' ? parsed.id : null)),
|
||||
eventType: scope === 'sse-event' ? field('eventType', parsed?.type) : null,
|
||||
headers: diagnosticHeaders, retryAfter, retryAfterMs,
|
||||
...(retryAfter && retryAfterMs === null ? { retryAfterInvalid: true } : {}),
|
||||
rawPreview: text.slice(0, PREVIEW_LIMIT), previewTruncated: text.length > PREVIEW_LIMIT,
|
||||
capture: { scope, receivedBytes, capturedBytes: bounded.length, storedBytes: Buffer.byteLength(text.slice(0, PREVIEW_LIMIT), 'utf8'), retention: 'preview-only',
|
||||
complete: !truncated && !interrupted && raw.length <= ERROR_CAPTURE_LIMIT,
|
||||
truncated: truncated || raw.length > ERROR_CAPTURE_LIMIT, interrupted, capturedRange: [0, bounded.length],
|
||||
redacted: changed, sha256: createHash('sha256').update(capture).digest('hex') },
|
||||
...(shortenedFields.length ? { shortenedFields } : {}),
|
||||
};
|
||||
return { capture, details };
|
||||
}
|
||||
@@ -12,6 +12,7 @@ export const DEFAULT_CONFIG = {
|
||||
export function cleanConfig(value = {}) {
|
||||
const result = { ...DEFAULT_CONFIG };
|
||||
for (const key of Object.keys(result)) if (Object.hasOwn(value, key)) result[key] = value[key];
|
||||
for (const key of ['nativeParameters', 'outputTokenField']) if (Object.hasOwn(value, key)) result[key] = structuredClone(value[key]);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -17,6 +17,7 @@ import { hostAuthority } from '../core/desktop/host-authority.mjs';
|
||||
import { companionGateway } from './companion-gateway.mjs';
|
||||
import { inspectOperations } from './operations.mjs';
|
||||
import { validateModelWaitSettings } from '../core/models/request-deadline.mjs';
|
||||
import { nativeParameterSettings } from '../shared/contracts/native-parameters.mjs';
|
||||
|
||||
export function createSessionPublisher(clients) {
|
||||
return session => {
|
||||
@@ -173,6 +174,7 @@ return async (request, response) => {
|
||||
return json(response, { center: hub.view(), config: { ...store.config, hasKey: provider.hasKey() } });
|
||||
}
|
||||
if (route === '/api/model-center/credentials') return json(response, hub.manageCredential(input));
|
||||
if (route === '/api/model-center/native-parameters') return json(response, hub.setNativeParameters(input));
|
||||
if (route === '/api/model-center/connections') return json(response, hub.upsert(input), 201);
|
||||
if (route === '/api/model-center/pools') return json(response, hub.pool(input), 201);
|
||||
if (route === '/api/model-center/select') return json(response, { center: hub.select(input), config: { ...store.config, hasKey: provider.hasKey() } });
|
||||
@@ -731,7 +733,8 @@ return async (request, response) => {
|
||||
const config = { ...store.config, baseUrl: String(input.baseUrl || store.config.baseUrl), model: String(input.model || store.config.model), mode: 'compatible' };
|
||||
provider.validate(config);
|
||||
const startedAt = Date.now();
|
||||
const result = await provider.request(config, '/chat/completions', { model: config.model, stream: false, max_tokens: 64,
|
||||
const native = nativeParameterSettings('chat-completions', config.nativeParameters ?? {}, config.outputTokenField);
|
||||
const result = await provider.request(config, '/chat/completions', { model: config.model, stream: false, [native.outputTokenField]: 64, ...native.nativeParameters,
|
||||
messages: [{ role: 'user', content: 'Connection check. Reply with OK.' }] }, undefined,
|
||||
Object.hasOwn(input, 'apiKey') ? String(input.apiKey).trim() : provider.apiKey);
|
||||
if (!result.choices?.[0]?.message) throw new Error('模型服务响应中缺少 choices[0].message。');
|
||||
|
||||
+21
-10
@@ -1,6 +1,6 @@
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
|
||||
export const APP_VERSION = '1.20.2';
|
||||
export const APP_VERSION = '1.20.3';
|
||||
|
||||
export function executionEnvironment() {
|
||||
const windows = process.platform === 'win32';
|
||||
@@ -22,7 +22,8 @@ export class ModelRequestError extends Error {
|
||||
}
|
||||
|
||||
export function connectionFailure(error, { endpoint, model, route, attempt, redact = String, timedOut = false, timeout = null }) {
|
||||
if (error instanceof ModelRequestError) return { endpoint, model, route, ...error.diagnostic, attempts: attempt };
|
||||
if (error instanceof ModelRequestError) return { ...error.diagnostic, endpoint: error.diagnostic.endpoint ?? endpoint,
|
||||
model: error.diagnostic.model ?? model, route: error.diagnostic.route ?? route, attempts: attempt };
|
||||
const causes = [error, error?.cause, ...(error?.cause?.errors ?? [])];
|
||||
const code = causes.map(item => item?.code).find(value => typeof value === 'string' && /^[A-Z0-9_]{2,60}$/.test(value)) ?? null;
|
||||
const kind = timedOut || causes.some(item => item?.name === 'TimeoutError') ? 'timeout'
|
||||
@@ -31,7 +32,7 @@ export function connectionFailure(error, { endpoint, model, route, attempt, reda
|
||||
const title = kind === 'timeout' ? '模型服务响应超时' : kind === 'dns' ? '模型服务地址解析失败'
|
||||
: kind === 'certificate' ? '模型服务证书校验失败' : '模型服务连接失败';
|
||||
return {
|
||||
id: randomUUID(), source: 'model_connection', stage: 'model_request', kind, code,
|
||||
id: randomUUID(), source: 'model_connection', origin: 'local', stage: 'model_request', kind, code,
|
||||
endpoint, model, route, attempts: attempt, occurredAt: new Date().toISOString(),
|
||||
retryable: kind !== 'certificate', title,
|
||||
message: title + (code ? '(' + code + ')' : '') + '。' + (timeout ? timeout.kind === 'idle'
|
||||
@@ -45,17 +46,27 @@ export function connectionFailure(error, { endpoint, model, route, attempt, reda
|
||||
};
|
||||
}
|
||||
|
||||
export function httpFailure(status, detail, { endpoint, model, route, attempt }) {
|
||||
const kind = status === 401 || status === 403 ? 'authentication' : status === 429 ? 'rate_limit' : 'service';
|
||||
export function httpFailure(status, detail, { endpoint, model, route, attempt, upstream, attemptId } = {}) {
|
||||
const codes = [upstream?.code, upstream?.type].filter(value => typeof value === 'string').map(value => value.toLowerCase());
|
||||
const quota = codes.some(value => ['insufficient_quota', 'billing_hard_limit_reached', 'billing_limit_reached', 'usage_limit_reached',
|
||||
'credit_balance_too_low', 'insufficient_balance', 'exceeded_quota'].includes(value));
|
||||
const limited = status === 429 || codes.some(value => ['rate_limit_exceeded', 'rate_limit_error', 'requests_limit_exceeded', 'tokens_limit_exceeded'].includes(value));
|
||||
const kind = quota ? 'quota' : status === 401 || status === 403 ? 'authentication' : limited ? 'rate_limit' : 'service';
|
||||
const title = kind === 'authentication' ? '模型服务拒绝了身份验证'
|
||||
: kind === 'rate_limit' ? '模型服务请求达到限额' : '模型服务暂时无法处理请求';
|
||||
: kind === 'quota' ? '模型服务额度或消费限制' : kind === 'rate_limit' ? '模型服务请求达到限额' : '模型服务暂时无法处理请求';
|
||||
return new ModelRequestError({
|
||||
id: randomUUID(), source: 'model_connection', stage: 'model_request', kind, httpStatus: status,
|
||||
id: randomUUID(), source: 'model_connection', origin: 'upstream', stage: 'model_request', kind, httpStatus: status,
|
||||
endpoint, model, route, attempts: attempt, occurredAt: new Date().toISOString(),
|
||||
retryable: [408, 429, 500, 502, 503, 504].includes(status), title,
|
||||
message: title + '(HTTP ' + status + ')。' + (detail ? ' ' + detail : ''), detail,
|
||||
retryable: !quota && !upstream?.retryAfterInvalid && (limited || [408, 500, 502, 503, 504].includes(status)),
|
||||
...(quota ? { failoverAllowed: true } : {}), title,
|
||||
...(upstream ? { upstream, upstreamRequestId: upstream.requestId, code: upstream.code, type: upstream.type,
|
||||
...(upstream.retryAfterMs !== null ? { retryAfterMs: upstream.retryAfterMs } : {}),
|
||||
limitReason: quota ? 'quota' : limited ? codes.some(value => /rate_limit|requests_limit|tokens_limit/.test(value)) ? 'rate' : 'unspecified' : null } : {}),
|
||||
...(attemptId ? { wireAttemptId: attemptId } : {}),
|
||||
message: title + (status >= 400 ? '(HTTP ' + status + ')' : '(上游返回错误内容)') + '。' + (detail ? ' ' + detail : ''), detail,
|
||||
suggestion: kind === 'authentication' ? '在模型连接中核对 API Key、模型权限和接口地址。'
|
||||
: kind === 'rate_limit' ? '等待服务限额恢复后重试;同时检查账户额度。' : '稍后重试,或检查模型服务状态。',
|
||||
: quota ? '核对上游账户余额、消费上限或用量限制。该线路不会自动重复请求;模型池可按规则选择其他可用成员。'
|
||||
: kind === 'rate_limit' ? '依据上游错误码核对限制原因;有 Retry-After 时按原等待时间处理,原因未给出时保持未确定。' : '稍后重试,或检查模型服务状态。',
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+54
-22
@@ -7,6 +7,7 @@ import { ModelHub } from '../core/models/model-hub.mjs';
|
||||
import { normalizeProtocolResponse, protocolRoute, validateModelResponse, reportedUsage } from '../core/models/protocols.mjs';
|
||||
import { readModelStream } from '../core/models/model-stream.mjs';
|
||||
import { modelWaitSettings, requestDeadline } from '../core/models/request-deadline.mjs';
|
||||
import { readErrorResponse, upstreamCapture } from '../core/models/upstream-error.mjs';
|
||||
|
||||
export const engineToolDefinitions = [
|
||||
{ type: 'function', function: { name: 'inspect_engine_checks', description: '读取当前Task已经保存的外部引擎连接核验:真实安装版本、启动完成信号、SDK会话、进程关闭、原报告工件和能力限制。不会启动引擎或发送prompt;完整原记录通过已有read_artifact读取。', parameters: { type: 'object', properties: {}, additionalProperties: false } } },
|
||||
@@ -121,7 +122,10 @@ export class Provider {
|
||||
prepareConfig(config, store) { return this.modelHub(store).prepare(config); }
|
||||
redact(value, key = this.apiKey) {
|
||||
let text = String(value);
|
||||
for (const secret of [key, this.apiKey, ...[...(this.credentials?.values() ?? [])].map(value => value.key)].filter(Boolean).sort((a, b) => b.length - a.length)) text = text.replaceAll(secret, '[已隐藏]');
|
||||
for (const secret of [key, this.apiKey, ...[...(this.credentials?.values() ?? [])].map(value => value.key)].filter(Boolean).sort((a, b) => b.length - a.length)) {
|
||||
const encoded = JSON.stringify(secret).slice(1, -1);
|
||||
text = text.replaceAll(encoded, '[已隐藏]').replaceAll(secret, '[已隐藏]');
|
||||
}
|
||||
return text;
|
||||
}
|
||||
validate(config) {
|
||||
@@ -148,14 +152,31 @@ export class Provider {
|
||||
attempts = attempt;
|
||||
const timeout = requestDeadline({ signal, timeoutMs: Math.max(1, Math.min(policy.timeoutMs, deadline - Date.now())), idleTimeoutMs: policy.idleTimeoutMs });
|
||||
const requestSignal = timeout.signal;
|
||||
const context = { endpoint: base, model: config.model, route, attempt };
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route });
|
||||
let readingStream = false, streamTerminalSeen = false, dispatched = false, usage = null, outcome = 'unknown';
|
||||
const receipt = { attemptId: randomUUID(), endpoint: base, model: config.model, protocol: config.protocol ?? 'chat-completions', startedAt: new Date().toISOString() };
|
||||
const context = { endpoint: base, model: config.model, route, attempt, attemptId: receipt.attemptId };
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route, wireAttemptId: receipt.attemptId });
|
||||
let responseDetails = null;
|
||||
const captureResponse = (response, input) => {
|
||||
const captured = upstreamCapture({ ...input, status: response.status, headers: response.headers }, value => this.redact(value, key));
|
||||
responseDetails = captured.details;
|
||||
onProgress?.({ responseCapture: captured.capture, upstream: responseDetails, attempt, maxAttempts: policy.attempts, wireAttemptId: receipt.attemptId });
|
||||
return responseDetails;
|
||||
};
|
||||
try {
|
||||
dispatched = true;
|
||||
const response = await (this.fetcher ?? fetch)(base + route, { method: body ? 'POST' : 'GET', headers,
|
||||
body: body ? JSON.stringify(body) : undefined, signal: requestSignal });
|
||||
const metadata = upstreamCapture({ raw: Buffer.alloc(0), status: response.status, headers: response.headers }, value => this.redact(value, key)).details;
|
||||
receipt.upstreamRequestId = metadata.requestId; receipt.httpStatus = response.status;
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, upstreamResponse: { httpStatus: response.status, requestId: metadata.requestId, headers: metadata.headers } });
|
||||
if (!response.ok) {
|
||||
const received = await readErrorResponse(response, requestSignal);
|
||||
if (!received.truncated && !received.interrupted) { try { usage = reportedUsage(config, JSON.parse(received.raw.toString('utf8'))); } catch {} }
|
||||
const upstream = captureResponse(response, received);
|
||||
requestSignal.throwIfAborted();
|
||||
throw httpFailure(response.status, (upstream.message ?? upstream.rawPreview).slice(0, 600), { ...context, upstream });
|
||||
}
|
||||
if (response.ok && body?.stream === true) {
|
||||
readingStream = true;
|
||||
timeout.beginStream();
|
||||
@@ -163,31 +184,37 @@ export class Provider {
|
||||
if (progress.reportedUsage) usage = progress.reportedUsage;
|
||||
if (progress.stream?.terminalSeen) streamTerminalSeen = true;
|
||||
onProgress?.({ ...progress, attempt, maxAttempts: policy.attempts });
|
||||
}, body.tools, length => timeout.receive(length));
|
||||
}, body.tools, length => timeout.receive(length), value => this.redact(value, key));
|
||||
const value = options.transformResponse ? options.transformResponse(streamed.value) : streamed.value;
|
||||
outcome = 'completed';
|
||||
this.lastRequest = { source: 'model_connection', endpoint: base, route, model: config.model, status: 'completed', attempts: attempt, durationMs: Date.now() - startedAt, stream: streamed.evidence };
|
||||
onProgress?.({ phase: 'completed', attempt, maxAttempts: policy.attempts, durationMs: Date.now() - startedAt, stream: streamed.evidence });
|
||||
return streamed.value;
|
||||
return value;
|
||||
}
|
||||
const text = await boundedResponse(response);
|
||||
try { usage = reportedUsage(config, JSON.parse(text)); } catch {}
|
||||
if (!response.ok) {
|
||||
let message = text;
|
||||
try { message = JSON.parse(text).error?.message ?? text; } catch {}
|
||||
const failure = httpFailure(response.status, this.redact(message, key).slice(0, 600), context);
|
||||
const retryAfter = response.headers.get('retry-after');
|
||||
if (retryAfter) {
|
||||
const milliseconds = /^\d+$/.test(retryAfter) ? Number(retryAfter) * 1000 : Date.parse(retryAfter) - Date.now();
|
||||
if (Number.isFinite(milliseconds)) failure.diagnostic.retryAfterMs = Math.max(0, Math.min(milliseconds, 90000));
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
let result;
|
||||
try { result = JSON.parse(text); } catch {
|
||||
const upstream = captureResponse(response, { raw: Buffer.from(text, 'utf8') });
|
||||
throw new ModelRequestError({ ...context, id: randomUUID(), source: 'model_connection', stage: 'model_request',
|
||||
origin: 'local', upstream,
|
||||
kind: 'protocol', title: '模型接口响应格式不兼容', message: '模型服务没有返回有效 JSON,请核对兼容接口地址。',
|
||||
attempts: attempt, retryable: false, occurredAt: new Date().toISOString(), suggestion: '填写服务实际提供的 Chat Completions 接口根地址。' });
|
||||
}
|
||||
if (result.error || config.protocol === 'responses' && ['failed', 'incomplete', 'cancelled'].includes(result.status)) {
|
||||
const upstream = captureResponse(response, { raw: Buffer.from(text, 'utf8') });
|
||||
const failure = httpFailure(response.status, (upstream.message ?? upstream.reason ?? upstream.rawPreview).slice(0, 600), { ...context, upstream });
|
||||
if (result.status === 'incomplete') { failure.diagnostic.kind = 'incomplete'; failure.diagnostic.title = '上游模型响应未完整结束'; }
|
||||
throw failure;
|
||||
}
|
||||
if (options.transformResponse) {
|
||||
try { result = options.transformResponse(result); }
|
||||
catch (cause) {
|
||||
const upstream = captureResponse(response, { raw: Buffer.from(text, 'utf8') });
|
||||
if (cause.diagnostic) cause.diagnostic.upstream = upstream;
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
this.lastRequest = { source: 'model_connection', endpoint: base, route, model: config.model, status: 'completed', attempts: attempt, durationMs: Date.now() - startedAt };
|
||||
outcome = 'completed';
|
||||
onProgress?.({ phase: 'completed', attempt, maxAttempts: policy.attempts, durationMs: Date.now() - startedAt });
|
||||
@@ -196,11 +223,18 @@ export class Provider {
|
||||
outcome = signal?.aborted ? 'interrupted' : readingStream && !streamTerminalSeen ? 'unknown' : 'failed';
|
||||
signal?.throwIfAborted();
|
||||
let diagnostic = connectionFailure(error, { ...context, timedOut: !!timeout.timeout, timeout: timeout.timeout, redact: value => this.redact(value, key) });
|
||||
if (readingStream) diagnostic = { ...diagnostic, retryable: false, outcome: streamTerminalSeen ? 'failed' : 'unknown', kind: timeout.timeout ? 'timeout' : streamTerminalSeen || diagnostic.kind === 'protocol' ? 'protocol' : 'stream_interrupted',
|
||||
message: diagnostic.message + (streamTerminalSeen ? ' 服务流已终结,响应校验未通过;原流保留,不自动重发。' : ' 已接收的流片段保留,请求不会自动重发。') };
|
||||
diagnostic = { ...diagnostic, origin: diagnostic.origin ?? 'local', wireAttemptId: receipt.attemptId, upstreamRequestId: diagnostic.upstreamRequestId ?? receipt.upstreamRequestId,
|
||||
...(responseDetails && !diagnostic.upstream ? { upstream: responseDetails } : {}) };
|
||||
if (readingStream) diagnostic = { ...diagnostic, retryable: false, failoverAllowed: false, outcome: streamTerminalSeen ? 'failed' : 'unknown',
|
||||
kind: diagnostic.origin === 'upstream' ? diagnostic.kind : timeout.timeout ? 'timeout' : streamTerminalSeen || diagnostic.kind === 'protocol' ? 'protocol' : 'stream_interrupted',
|
||||
...(diagnostic.origin === 'upstream' ? { suggestion: '可查看原失败事件、上游请求 ID 和已保存的响应。核对服务状态后手动续接;该流不会自动重发。' } : {}),
|
||||
message: diagnostic.message + (diagnostic.origin === 'upstream' ? ' 已保存上游失败事件与原流,工具未执行,不会自动重发。'
|
||||
: streamTerminalSeen ? ' 服务流已终结,响应校验未通过;原流保留,不自动重发。' : ' 已接收的流片段保留,请求不会自动重发。') };
|
||||
const remaining = deadline - Date.now();
|
||||
const waitMs = diagnostic.retryAfterMs ?? policy.delays[Math.min(attempt - 1, policy.delays.length - 1)] ?? 500;
|
||||
const retry = diagnostic.retryable && attempt < policy.attempts && remaining > waitMs;
|
||||
diagnostic.retryDecision = retry ? 'retry_after_wait' : readingStream ? 'stream_not_replayed' : !diagnostic.retryable ? 'not_retryable' : attempt >= policy.attempts ? 'attempt_limit' : 'wait_budget';
|
||||
diagnostic.remainingWaitBudgetMs = Math.max(0, remaining);
|
||||
this.lastRequest = { ...diagnostic, status: retry ? 'retrying' : 'failed', durationMs: Date.now() - startedAt };
|
||||
onProgress?.({ phase: retry ? 'retrying' : 'failed', attempt, maxAttempts: policy.attempts,
|
||||
diagnostic, waitMs: retry ? waitMs : 0, durationMs: Date.now() - startedAt });
|
||||
@@ -232,10 +266,8 @@ export class Provider {
|
||||
if (config.mode === 'demo') return this.demo(messages, project);
|
||||
this.validate(config);
|
||||
const execute = async (selected, key, input, progress) => {
|
||||
const value = normalizeProtocolResponse(selected, await this.request(selected, protocolRoute(selected), input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy }));
|
||||
if (!value.choices?.[0]?.message || value.choices[0].finish_reason === 'length') throw new ModelRequestError({ kind: 'protocol', source: 'model_connection',
|
||||
stage: 'model_request', title: '模型响应不完整', message: '模型响应缺少完整message或达到输出上限,未执行工具。', retryable: false, occurredAt: new Date().toISOString() });
|
||||
return validateModelResponse(value, tools);
|
||||
return this.request(selected, protocolRoute(selected), input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy,
|
||||
transformResponse: raw => validateModelResponse(normalizeProtocolResponse(selected, raw), tools) });
|
||||
};
|
||||
const result = config.modelRouting && this.hub ? await this.hub.complete(config, body, signal, onProgress, requestPlan, execute)
|
||||
: await execute(config, this.apiKey, body, onProgress);
|
||||
|
||||
+13
-1
@@ -1,6 +1,7 @@
|
||||
import { spawn, execFile } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { sanitizeModelText } from '../core/models/upstream-error.mjs';
|
||||
import { StringDecoder } from 'node:string_decoder';
|
||||
import { setImmediate as yieldExecution } from 'node:timers/promises';
|
||||
import { safePath, listFiles, readLines, searchFiles, proposeFile, proposePatch } from './files.mjs';
|
||||
@@ -317,6 +318,17 @@ export class Runtime {
|
||||
if (run.worker) { planned.record.workerUsageAccounted = true; this.store.save(session); }
|
||||
return;
|
||||
}
|
||||
if (progress.responseCapture !== undefined || progress.streamCapture !== undefined) {
|
||||
const raw = progress.responseCapture ?? progress.streamCapture;
|
||||
const decoded = raw.toString('utf8'), redacted = sanitizeModelText(decoded, value => this.provider.redact(value));
|
||||
const captured = redacted === decoded ? raw : Buffer.from(redacted, 'utf8');
|
||||
const artifact = this.store.artifactStore.write(session, run.record, captured, { kind: progress.streamCapture !== undefined ? 'model-stream-response' : 'model-http-response', mediaType: 'text/plain' });
|
||||
this.store.core.artifact.set(artifact.id, artifact);
|
||||
if (progress.upstream) Object.assign(progress.upstream, { artifactId: artifact.id, artifactSha256: artifact.sha256,
|
||||
artifactScope: progress.streamCapture !== undefined ? 'sse-stream' : 'http-body', sessionId: session.id });
|
||||
if (progress.upstream?.capture) Object.assign(progress.upstream.capture, { retention: 'artifact', storedBytes: artifact.bytes });
|
||||
this.store.save(session); return;
|
||||
}
|
||||
if (!requestEvent) requestEvent = this.store.event(session, { kind: externalEngine ? 'engine' : 'model', runId: run.id,
|
||||
source: externalEngine ? 'controlled_engine' : 'model_connection', label: externalEngine ? '受控引擎执行' : '请求模型服务', phase: 'running', startedAt: now(), contextPlanId: planned.record.id });
|
||||
if (progress.engineRunId) requestEvent.engineRunId = progress.engineRunId;
|
||||
@@ -472,7 +484,7 @@ export class Runtime {
|
||||
this.store.message(session, 'system', budget?.message ?? '任务已停止,执行记录已保存。');
|
||||
} else {
|
||||
session.status = 'failed';
|
||||
const diagnostic = error.diagnostic ?? { source: 'workbench', stage: 'agent_loop', kind: 'runtime',
|
||||
const diagnostic = error.diagnostic ?? { source: 'workbench', origin: 'local', stage: 'agent_loop', kind: 'runtime',
|
||||
title: '工作台任务需要处理', message: this.provider.redact(error.message ?? String(error)), occurredAt: now() };
|
||||
session.lastFailure = { ...diagnostic, runId: run.id };
|
||||
const message = this.store.message(session, 'system', diagnostic.message);
|
||||
|
||||
@@ -17,6 +17,7 @@ export function modelSnapshot(config) {
|
||||
configRevision: config.configRevision ?? 1,
|
||||
...modelWaitSettings(config),
|
||||
...(config.protocol ? { protocol: config.protocol, streaming: config.streaming === true } : {}),
|
||||
...(config.nativeParameters ? { nativeParameters: structuredClone(config.nativeParameters), outputTokenField: config.outputTokenField } : {}),
|
||||
...(config.modelRouting ? { routing: structuredClone(config.modelRouting) } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { modelProtocol } from './model-capabilities.mjs';
|
||||
import { nativeParameterSettings } from './native-parameters.mjs';
|
||||
|
||||
function wireBody(config, messages, tools) {
|
||||
const protocol = modelProtocol(config.protocol), stream = config.streaming === true;
|
||||
const native = nativeParameterSettings(protocol, config.nativeParameters ?? {}, config.outputTokenField);
|
||||
if(config.inputImages?.length) messages=[...messages,{role:'user',content:protocol==='responses'
|
||||
? [{type:'input_text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'input_image',image_url:image.dataUrl,detail:'high'}))]
|
||||
: [{type:'text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'image_url',image_url:{url:image.dataUrl,detail:'high'}}))]}];
|
||||
const cleanMessages = messages.map(({ providerState, ...message }) => message);
|
||||
if (protocol === 'chat-completions') return { model: config.model, messages: cleanMessages, tools, tool_choice: 'auto', max_tokens: config.maxOutputTokens, stream,
|
||||
if (protocol === 'chat-completions') return { model: config.model, messages: cleanMessages, tools, tool_choice: 'auto', [native.outputTokenField]: config.maxOutputTokens, stream, ...native.nativeParameters,
|
||||
...(stream ? { stream_options: { include_usage: true } } : {}) };
|
||||
const input = messages.flatMap(message => {
|
||||
if (message.role === 'tool') return [{ type: 'function_call_output', call_id: message.tool_call_id, output: message.content }];
|
||||
@@ -19,14 +21,15 @@ function wireBody(config, messages, tools) {
|
||||
return [{ role: message.role, content: message.content }];
|
||||
});
|
||||
return { model: config.model, input, tools: (tools ?? []).map(tool => ({ type: 'function', ...tool.function, strict: false })),
|
||||
tool_choice: 'auto', max_output_tokens: config.maxOutputTokens, stream, store: false, include: ['reasoning.encrypted_content'] };
|
||||
tool_choice: 'auto', max_output_tokens: config.maxOutputTokens, stream, store: false, include: ['reasoning.encrypted_content'], ...native.nativeParameters };
|
||||
}
|
||||
|
||||
export function completionBody(config, messages, tools) {
|
||||
const members = config.modelRouting?.members;
|
||||
if (members?.some(member => member.protocol === 'responses' || member.streaming === true)) {
|
||||
if (members?.some(member => member.protocol === 'responses' || member.streaming === true || member.nativeParameters || member.outputTokenField)) {
|
||||
return { schema: 1, kind: 'model-route-plan', messages, variants: members.map(member => ({ connectionId: member.id,
|
||||
protocol: modelProtocol(member.protocol), body: wireBody({ ...config, ...member, maxOutputTokens: config.maxOutputTokens }, messages, tools) })) };
|
||||
protocol: modelProtocol(member.protocol), body: wireBody({ ...config, ...member, nativeParameters: member.nativeParameters ?? {}, outputTokenField: member.outputTokenField,
|
||||
maxOutputTokens: config.maxOutputTokens }, messages, tools) })) };
|
||||
}
|
||||
return wireBody(config, messages, tools);
|
||||
}
|
||||
@@ -42,7 +45,7 @@ export function actualRequest(body, member) {
|
||||
return structuredClone(variant.body);
|
||||
}
|
||||
|
||||
export const outputReservation = body => body.max_output_tokens ?? body.max_tokens;
|
||||
export const outputReservation = body => body.max_output_tokens ?? body.max_completion_tokens ?? body.max_tokens;
|
||||
|
||||
export function requestHistory(body) {
|
||||
if (body.messages) return body.messages;
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export function nativeParameterSettings(protocol?: string, parameters?: Record<string, unknown>, outputTokenField?: string): {
|
||||
nativeParameters: Record<string, unknown>;
|
||||
outputTokenField: string;
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
const fail = message => { throw Object.assign(new Error(message), { status: 400 }); };
|
||||
const object = value => value && typeof value === 'object' && !Array.isArray(value);
|
||||
const efforts = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh'];
|
||||
const verbosity = ['low', 'medium', 'high'];
|
||||
|
||||
export function nativeParameterSettings(protocol = 'chat-completions', parameters = {}, outputTokenField) {
|
||||
if (!object(parameters) || JSON.stringify(parameters).length > 8192) fail('原生参数必须是8KB以内的JSON对象。');
|
||||
const allowed = protocol === 'responses' ? ['temperature', 'top_p', 'parallel_tool_calls', 'reasoning', 'text']
|
||||
: ['temperature', 'top_p', 'parallel_tool_calls', 'reasoning_effort', 'verbosity', 'seed'];
|
||||
for (const [key, value] of Object.entries(parameters)) {
|
||||
if (!allowed.includes(key)) fail('所选协议尚未接入原生参数:' + key + '。参数未保存或发送。');
|
||||
if (key === 'temperature' && (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > 2)) fail('temperature应为0–2之间的数字。');
|
||||
if (key === 'top_p' && (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > 1)) fail('top_p应为0–1之间的数字。');
|
||||
if (key === 'seed' && !Number.isSafeInteger(value)) fail('seed应为安全整数。');
|
||||
if (key === 'parallel_tool_calls' && typeof value !== 'boolean') fail('parallel_tool_calls应为布尔值。');
|
||||
if (key === 'reasoning_effort' && !efforts.includes(value)) fail('reasoning_effort应为none/minimal/low/medium/high/xhigh。');
|
||||
if (key === 'verbosity' && !verbosity.includes(value)) fail('verbosity应为low/medium/high。');
|
||||
if (key === 'reasoning' && (!object(value) || Object.keys(value).length !== 1 || !efforts.includes(value.effort))) fail('Responses reasoning当前接入格式为{"effort":"low|medium|high等"}。');
|
||||
if (key === 'text' && (!object(value) || Object.keys(value).length !== 1 || !verbosity.includes(value.verbosity))) fail('Responses text当前接入格式为{"verbosity":"low|medium|high"}。');
|
||||
}
|
||||
const field = outputTokenField ?? (protocol === 'responses' ? 'max_output_tokens' : 'max_tokens');
|
||||
if (!(protocol === 'responses' ? ['max_output_tokens'] : ['max_tokens', 'max_completion_tokens']).includes(field)) fail('输出上限字段与所选协议不兼容。');
|
||||
return { nativeParameters: structuredClone(parameters), outputTokenField: field };
|
||||
}
|
||||
Reference in New Issue
Block a user