feat(runtime): default to passthrough and persist execution permissions

This commit is contained in:
Agent Workbench Dev
2026-11-06 04:58:57 +08:00
parent 67c034f4b6
commit 2fa220735d
113 changed files with 1866 additions and 631 deletions
+7 -6
View File
@@ -1,3 +1,4 @@
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
@@ -22,11 +23,11 @@ export class ConfigImporter {
read(source, filename) {
if (!path.isAbsolute(filename)) throw new Error('请选择配置文件的绝对路径。');
const stat = fs.lstatSync(filename);
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 64 * 1024 * 1024) throw new Error('配置必须为64MB以内的普通文件。');
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > ceiling(this.hub.store, 64 * 1024 * 1024)) throw new Error('配置必须为64MB以内的普通文件。');
const rows = [], notices = [];
if (source === 'workbench-json') {
const value = JSON.parse(fs.readFileSync(filename, 'utf8').replace(/^\uFEFF/, ''));
if (value.schema !== 1 || !Array.isArray(value.connections) || value.connections.length > 100) throw new Error('连接JSON需要schema:1与connections数组(最多100项)。');
if (value.schema !== 1 || !Array.isArray(value.connections) || value.connections.length > ceiling(this.hub.store, 100)) throw new Error('连接JSON需要schema:1与connections数组(最多100项)。');
for (const [index, item] of value.connections.entries()) {
if (item.protocol && !MODEL_PROTOCOLS.includes(item.protocol)) { notices.push('第' + (index + 1) + '项协议暂不支持,已跳过。'); continue; }
rows.push({ sourceId: String(item.id ?? index), name: item.name, model: item.model, protocol: item.protocol ?? 'chat-completions',
@@ -44,8 +45,8 @@ export class ConfigImporter {
try {
const columns = db.prepare('PRAGMA table_info(providers)').all().map(value => value.name);
if (!['id', 'app_type', 'name', 'settings_config'].every(value => columns.includes(value))) throw new Error('CCSwitch providers表不符合已验证格式。');
const providers = db.prepare('SELECT id,app_type,name,settings_config FROM providers ORDER BY app_type,id LIMIT 101').all();
if (providers.length > 100) throw new Error('来源超过100个provider,请缩小导入范围。');
const providers = db.prepare('SELECT id,app_type,name,settings_config FROM providers ORDER BY app_type,id' + (unrestricted(this.hub.store) ? '' : ' LIMIT 101')).all();
if (providers.length > ceiling(this.hub.store, 100)) throw new Error('来源超过100个provider,请缩小导入范围。');
for (const provider of providers) {
let config;
try { config = JSON.parse(provider.settings_config); } catch { notices.push(provider.name + '配置不是有效JSON,已跳过。'); continue; }
@@ -66,7 +67,7 @@ export class ConfigImporter {
if (!String(row.model ?? '').trim() || !String(row.name ?? '').trim()) throw new Error('缺少名称或模型');
if (row.apiKey !== undefined && typeof row.apiKey !== 'string') throw new Error('凭据不是字符串');
const native = row.nativeParameters !== undefined || row.outputTokenField !== undefined || row.parameterProfile !== undefined
? nativeParameterSettings(row.protocol, row.nativeParameters ?? {}, row.outputTokenField, row.parameterProfile) : {};
? nativeParameterSettings(row.protocol, row.nativeParameters ?? {}, row.outputTokenField, row.parameterProfile, this.hub.store.config) : {};
entries.push({ ...row, ...native, ...messagesAuthSettings(row.protocol, row.messagesAuth), name: String(row.name).slice(0, 100), model: String(row.model).slice(0, 200), baseUrl: validateProtocolEndpoint(row.protocol, modelEndpoint(row.baseUrl)),
origin: digest({ source, path: path.resolve(filename), id: row.sourceId }), apiKey: row.apiKey?.trim() ?? '' });
} catch { notices.push(String(row.name ?? '某项配置').slice(0, 100) + '缺少有效的接口或模型,已跳过。'); }
@@ -76,7 +77,7 @@ export class ConfigImporter {
preview(source, filename) {
const contents = this.read(source, filename), id = randomUUID();
for (const [key, value] of this.previews) if (Date.now() - value.created > 300000) this.previews.delete(key);
if (this.previews.size >= 8) throw new Error('预览数量过多,请先完成已有导入。');
if (this.previews.size >= ceiling(this.hub.store, 8)) throw new Error('预览数量过多,请先完成已有导入。');
this.previews.set(id, { source, path: path.resolve(filename), created: Date.now(), ...contents });
return { id, entries: contents.entries.map(({ apiKey, sourceId, origin, ...value }, index) => ({ ...value, id: String(index), hasKey: !!apiKey })), notices: contents.notices, expiresInMs: 300000 };
}
+2 -2
View File
@@ -4,7 +4,7 @@ const object = value => value && typeof value === 'object' && !Array.isArray(val
const fail = message => { throw protocolFailure('Messages 流:' + message); };
// Each block and the whole message must close before any tool becomes dispatchable.
export function messagesStream() {
export function messagesStream(config = {}) {
let message = null, active = null, deltaSeen = false, stopped = false, text = '', toolCount = 0;
const blocks = [];
return {
@@ -23,7 +23,7 @@ export function messagesStream() {
if (!object(block) || !['text', 'tool_use', 'thinking', 'redacted_thinking'].includes(block.type)) fail('未接入的内容块类型。');
if (block.type === 'text' && typeof block.text !== 'string' || block.type === 'tool_use' && (!object(block.input) || typeof block.id !== 'string' || !block.id || typeof block.name !== 'string' || !block.name)
|| block.type === 'thinking' && (typeof block.thinking !== 'string' || block.signature !== undefined && typeof block.signature !== 'string') || block.type === 'redacted_thinking' && typeof block.data !== 'string') fail('初始内容块格式无效。');
if (block.type === 'tool_use' && (++toolCount > 12 || blocks.some(item => item.block.id === block.id))) fail('工具数量超限或身份重复。');
if (block.type === 'tool_use' && (++toolCount > 12 && config.executionMode !== 'unrestricted' || blocks.some(item => item.block.id === block.id))) fail('工具数量超限或身份重复。');
// Thinking signatures normally arrive in a later signature_delta.
// Final protocol normalization still requires a complete signature.
const initial = structuredClone(block);
+8 -7
View File
@@ -1,3 +1,4 @@
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
import { randomUUID, createHash } from 'node:crypto';
import { catalogPage, catalogRoute } from '../../shared/contracts/model-catalog.mjs';
import { messagesAuthSettings, validateProtocolEndpoint } from '../../shared/contracts/model-endpoint.mjs';
@@ -72,23 +73,23 @@ export class ModelCatalog {
do {
controller.signal.throwIfAborted();
this.current({ binding });
if (Date.now() >= deadline) throw fail('获取模型目录超过60秒,请稍后重试。');
if (!unrestricted(this.hub.store) && (Date.now() >= deadline)) throw fail('获取模型目录超过60秒,请稍后重试。');
const query = cursor ? '?after_id=' + encodeURIComponent(cursor) + '&limit=100' : connection.protocol === 'anthropic-messages' ? '?limit=100' : '';
const raw = await this.hub.provider.request({ ...connection, model: '', mode: 'demo' }, route + query, undefined, controller.signal, key, undefined,
{ attempts: 1, timeoutMs: Math.min(20000, deadline - Date.now()), totalTimeoutMs: Math.min(20000, deadline - Date.now()) });
{ attempts: 1, timeoutMs: unrestricted(this.hub.store) ? 0 : Math.min(20000, deadline - Date.now()), totalTimeoutMs: unrestricted(this.hub.store) ? 0 : Math.min(20000, deadline - Date.now()) });
const encoded = this.hub.provider.redact(JSON.stringify(raw), key); bytes += Buffer.byteLength(encoded);
if (bytes > 4 * 1024 * 1024) throw fail('模型目录超过4MB上限,请使用供应商筛选接口或手动添加。');
const page = catalogPage(JSON.parse(encoded)); pages++;
if (page.entries.length > 1000) throw fail('单页模型超过1000项,请核对上游分页。');
if (!unrestricted(this.hub.store) && (page.entries.length > 1000)) throw fail('单页模型超过1000项,请核对上游分页。');
for (const item of page.entries) {
if (entries.has(item.id) && JSON.stringify(entries.get(item.id)) !== JSON.stringify(item)) throw fail('上游目录包含相同型号的冲突定义,请核对后重试。');
if (entries.size === 1000 && !entries.has(item.id)) { complete = false; warning = '已达到1000项目录上限;当前只显示已获取部分,可使用手动兼容入口。'; break; }
if (!unrestricted(this.hub.store) && (entries.size === 1000 && !entries.has(item.id))) { complete = false; warning = '已达到1000项目录上限;当前只显示已获取部分,可使用手动兼容入口。'; break; }
entries.set(item.id, item);
}
if (!page.hasMore || !complete) break;
if (cursors.has(page.cursor) || !page.entries.length) throw fail('上游分页未向前推进,已停止获取。');
cursors.add(page.cursor); cursor = page.cursor;
if (pages === 20 || entries.size === 1000) { complete = false; warning = '目录仍有后续页,已达到本次获取上限;当前只显示部分模型。'; break; }
if (!unrestricted(this.hub.store) && (pages === 20 || entries.size === 1000)) { complete = false; warning = '目录仍有后续页,已达到本次获取上限;当前只显示部分模型。'; break; }
} while (true);
controller.signal.throwIfAborted(); this.current({ binding }); this.prune();
if (this.previews.size >= 8) this.previews.delete(this.previews.keys().next().value);
@@ -103,7 +104,7 @@ export class ModelCatalog {
this.prune(); const value = this.previews.get(input.previewId);
if (!value) throw fail('模型目录预览已取消、过期或宿主已重启,请重新获取。', 409);
this.current(value);
if (!Array.isArray(input.models) || !input.models.length || input.models.length > 100 || new Set(input.models).size !== input.models.length) throw fail('请选择1–100个不重复的上游模型。');
if (!Array.isArray(input.models) || !input.models.length || !unrestricted(this.hub.store) && input.models.length > 100 || new Set(input.models).size !== input.models.length) throw fail('请选择1–100个不重复的上游模型。');
const selected = input.models.map(id => value.preview.entries.find(model => model.id === id));
if (selected.some(model => !model)) throw fail('所选模型不属于本次上游目录。');
if (selected.some(model => model.lifecycle === 'retired')) throw fail('已退役型号不能新增使用,请选择其它模型。');
@@ -111,7 +112,7 @@ export class ModelCatalog {
const declarations = capabilityDeclarations(input.declarations, { text: 'unknown', tools: 'unknown', streaming: 'unknown', vision: 'unknown', audio: 'unknown' });
if (!['keep', 'manual', 'auto'].includes(input.routing ?? 'keep')) throw fail('请选择保留当前选择、手动指定或自动适配。');
if (input.routing === 'manual' && !input.models.includes(input.defaultModel)) throw fail('请在勾选的模型中指定默认型号。');
if (input.routing === 'auto' && input.models.length > 32) throw fail('单个自动模型池最多32个成员;可减少本次选择或分别建立模型池。');
if (!unrestricted(this.hub.store) && (input.routing === 'auto' && input.models.length > 32)) throw fail('单个自动模型池最多32个成员;可减少本次选择或分别建立模型池。');
if (input.persistKey === true && !value.explicitKey) throw fail('本机加密保存需要本次新填写的 Key;已有凭据沿用原保存状态。');
const hub = this.hub;
const result = hub.editRegistry(() => {
+18 -17
View File
@@ -1,3 +1,4 @@
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
import { randomUUID, createHash } from 'node:crypto';
import { setTimeout as delay } from 'node:timers/promises';
import { ModelRequestError } from '../../server/diagnostics.mjs';
@@ -84,7 +85,7 @@ export class ModelHub {
throw cause;
} finally { this.editingRegistry = false; }
}
saveAttempts() { this.attempts = this.attempts.slice(-1000); this.store.storage.transaction(() => this.store.storage.setMeta('modelAttempts', this.attempts)); }
saveAttempts() { if (!unrestricted(this.store)) this.attempts = this.attempts.slice(-1000); this.store.storage.transaction(() => this.store.storage.setMeta('modelAttempts', this.attempts)); }
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,
@@ -203,15 +204,15 @@ export class ModelHub {
if (!this.editingRegistry) return this.editRegistry(() => this.upsert(input));
if (input.id === legacyId) throw error('原连接请通过模型连接设置修改。');
const previous = input.id ? this.connection(input.id) : null;
if (this.state.connections.length >= 100 && !previous) throw error('最多保存100个模型连接。');
if (!unrestricted(this.store) && 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') || Object.hasOwn(input, 'parameterProfile') || previous?.nativeParameters || previous?.parameterProfile
? nativeParameterSettings(protocol, input.nativeParameters ?? previous?.nativeParameters ?? {}, input.outputTokenField ?? previous?.outputTokenField, input.parameterProfile ?? previous?.parameterProfile) : null;
? nativeParameterSettings(protocol, input.nativeParameters ?? previous?.nativeParameters ?? {}, input.outputTokenField ?? previous?.outputTokenField, input.parameterProfile ?? previous?.parameterProfile, this.store.config) : 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;
if (streaming && declarations.streaming !== 'supported') throw error('请先声明服务支持流式输出,再开启流式请求。');
if (!unrestricted(this.store) && streaming && declarations.streaming !== 'supported') throw error('请先声明服务支持流式输出,再开启流式请求。');
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,
@@ -219,7 +220,7 @@ export class ModelHub {
...messagesAuthSettings(protocol, input.messagesAuth ?? previous?.messagesAuth),
baseUrl: validateProtocolEndpoint(protocol, modelEndpoint(input.baseUrl)), enabled: input.enabled !== false, requiresKey: input.requiresKey !== false,
...capacitySettings(input, previous ?? undefined),
concurrency: bounded(input.concurrency, 1, 1, 8), revision: (previous?.revision ?? 0) + 1,
concurrency: unrestricted(this.store) ? (input.concurrency ?? 0) : bounded(input.concurrency, 1, 1, 8), revision: (previous?.revision ?? 0) + 1,
source: previous?.source ?? 'manual', ...(previous?.importOrigin ? { importOrigin: previous.importOrigin, importRevision: previous.importRevision } : {}),
createdAt: previous?.createdAt ?? stamp(), updatedAt: stamp() };
if (previous?.credentialSource && !Object.hasOwn(input, 'apiKey')) {
@@ -227,7 +228,7 @@ export class ModelHub {
connection.credentialSource = previous.credentialSource;
}
if (previous?.upstreamModel && previous.model === connection.model && credentialScope(previous) === credentialScope(connection)) connection.upstreamModel = previous.upstreamModel;
assertModelParameterSupport(connection, connection.nativeParameters ?? {});
if (!unrestricted(this.store)) assertModelParameterSupport(connection, connection.nativeParameters ?? {});
if (Object.hasOwn(input, 'apiKey')) {
if (typeof input.apiKey !== 'string' || input.apiKey.length > 16000) throw error('模型凭据格式无效。');
}
@@ -244,7 +245,7 @@ export class ModelHub {
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, input.parameterProfile ?? connection.parameterProfile);
const settings = nativeParameterSettings(connection.protocol, input.nativeParameters, input.outputTokenField, input.parameterProfile ?? connection.parameterProfile, this.store.config);
if (connection.id === legacyId) this.save(settings);
else this.upsert({ ...connection, ...settings });
return { center: this.view(), config: { ...this.store.config, hasKey: this.provider.hasKey() } };
@@ -257,8 +258,8 @@ export class ModelHub {
const previous = input.id ? this.state.pools.find(value => value.id === input.id) : null;
if (input.id && !previous) throw error('模型池不存在。', 404);
if (previous && input.revision !== previous.revision) throw error('模型池已修改,请刷新后重试。', 409);
if (!previous && this.state.pools.length >= 32) throw error('最多保存32个模型池。');
if (!label(input.name) || !Array.isArray(input.members) || !input.members.length || input.members.length > 32) throw error('模型池需要名称和1–32个成员。');
if (!unrestricted(this.store) && !previous && this.state.pools.length >= 32) throw error('最多保存32个模型池。');
if (!label(input.name) || !Array.isArray(input.members) || !input.members.length || !unrestricted(this.store) && input.members.length > 32) throw error('模型池需要名称和1–32个成员。');
const seen = new Set();
const members = input.members.map(value => {
this.connection(value.connectionId);
@@ -288,7 +289,7 @@ export class ModelHub {
const members = (pool?.members ?? [{ connectionId: selection.id, weight: 1 }]).map(value => ({ ...this.connection(value.connectionId), capabilityEvidence: this.state.capabilityResults?.[value.connectionId], weight: value.weight,
credentialRevision: this.credential(this.connection(value.connectionId))?.revision ?? 0,
...(this.connection(value.connectionId).credentialSource ? { credentialSourceRevision: this.credentialOwner(this.connection(value.connectionId))?.revision ?? null } : {}) }));
if (!members.some(value => this.status(value).status === 'configured' || this.status(value).status === 'cooling')) throw error('没有可用模型连接,请核对启用状态、临时凭据和故障。');
if (!members.some(value => unrestricted(this.store) && value.enabled || this.status(value).status === 'configured' || this.status(value).status === 'cooling')) throw error('没有可用模型连接,请核对启用状态、临时凭据和故障。');
return { ...config, ...messagesAuthSettings(members[0].protocol, members[0].messagesAuth), 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, parameterProfile: members[0].parameterProfile,
modelConnectionId: members[0].id, modelCredentialRevision: members[0].credentialRevision,
@@ -301,18 +302,18 @@ export class ModelHub {
if (!current?.enabled || current.revision !== member.revision || (this.credential(current)?.revision ?? 0) !== member.credentialRevision) return false;
if (current.credentialSource && (!this.credentialOwner(current) || member.credentialSourceRevision !== undefined && this.credentialOwner(current).revision !== member.credentialSourceRevision)) return false;
const health = this.health.get(this.identity(member));
return (!member.requiresKey || !!this.credential(member)?.key) && !health?.blocked && !(health?.cooldownUntil > Date.now());
return unrestricted(this.store) || (!member.requiresKey || !!this.credential(member)?.key) && !health?.blocked && !(health?.cooldownUntil > Date.now());
}
async acquire(routing, runId, excluded, signal, deadline, requirements = []) {
if (this.waiting >= 100) throw error('模型请求队列已满,请稍后继续任务。');
if (!unrestricted(this.store) && this.waiting >= 100) throw error('模型请求队列已满,请稍后继续任务。');
this.waiting++;
try {
while (true) {
signal?.throwIfAborted();
const eligible = routing.members.filter(value => capabilityRejections(value, requirements, value.credentialRevision).length === 0);
const eligible = routing.members.filter(value => unrestricted(this.store) || capabilityRejections(value, requirements, value.credentialRevision).length === 0);
if (!eligible.length) throw Object.assign(error('模型池没有满足本次任务能力的成员;请核对工具调用和流式能力声明。'), { rejections: routing.members.flatMap(value => capabilityRejections(value, requirements, value.credentialRevision)) });
const healthy = eligible.filter(value => this.available(value) && !excluded.has(value.id));
const ready = healthy.filter(value => (this.leases.get(value.id) ?? 0) < value.concurrency);
const ready = healthy.filter(value => unrestricted(this.store) || (value.concurrency === 0 || (this.leases.get(value.id) ?? 0) < value.concurrency));
if (ready.length) {
const remembered = routing.sticky && this.sticky.get(runId);
let selected = ready.find(value => value.id === remembered);
@@ -337,11 +338,11 @@ export class ModelHub {
finally { operation?.finish(); }
}
async completeOwned(config, body, signal, onProgress, plan, execute,beforeDispatch) {
const routing = config.modelRouting, excluded = new Set(), deadline = Date.now() + Math.min(routing.queueTimeoutMs, 60000);
const routing = config.modelRouting, excluded = new Set(), deadline = unrestricted(config) ? Infinity : Date.now() + Math.min(routing.queueTimeoutMs, 60000);
const requestId = plan?.id ?? randomUUID(), runId = plan?.runId ?? requestId;
const requirements = config.capabilityProbe ? [] : ['text', ...(requestVariants(body).some(value => value.tools?.length) ? ['tools'] : []),
...(requestImages(body).length ? ['vision'] : [])];
const rejections = config.capabilityProbe ? [] : routing.members.flatMap(value => capabilityRejections(value, [...requirements, ...(value.streaming ? ['streaming'] : [])], value.credentialRevision));
const rejections = config.capabilityProbe || unrestricted(config) ? [] : routing.members.flatMap(value => capabilityRejections(value, [...requirements, ...(value.streaming ? ['streaming'] : [])], value.credentialRevision));
const eligibleRouting = { ...routing, members: routing.members.filter(value => !rejections.some(item => item.connectionId === value.id)) };
if (!eligibleRouting.members.length) {
const attempt = { id: randomUUID(), requestId, taskId: plan?.taskId ?? null, runId, sessionId: plan?.sessionId ?? null, projectId: plan?.projectId ?? null,
@@ -438,7 +439,7 @@ export class ModelHub {
}
lastError = cause; excluded.add(member.id); this.sticky.delete(runId);
signal?.throwIfAborted();
if ((!diagnostic?.retryable && !diagnostic?.failoverAllowed) || count === routing.maxAttempts) throw cause;
if (config.capacityMode === 'passthrough' || (!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(); }
+6 -6
View File
@@ -15,7 +15,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
let upstreamFailure = null;
let reasoning = '', hasReasoning = false;
const calls = new Map(), responseCalls = new Map();
const messages = config.protocol === 'anthropic-messages' ? messagesStream() : null;
const messages = config.protocol === 'anthropic-messages' ? messagesStream(config) : null;
let eventName = '';
const progress = force => {
if (!force && Date.now() - lastProgress < 100) {
@@ -33,7 +33,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
const processData = raw => {
if (terminal || done || messages?.stopped) throw protocolFailure('模型流在终结后仍包含数据,未采用响应。');
frames++;
if (frames > 100000) throw protocolFailure('模型事件流超过事件数量上限。');
if (config.executionMode !== 'unrestricted' && frames > 100000) throw protocolFailure('模型事件流超过事件数量上限。');
if (raw === '[DONE]') { if (config.protocol !== 'chat-completions' && config.protocol) throw protocolFailure('所选协议缺少正式终结事件。'); done = true; return; }
let event;
try { event = JSON.parse(raw); } catch { throw protocolFailure('模型事件流包含无效JSON。'); }
@@ -43,7 +43,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
if (event.usage && !messages) 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);
const { details } = upstreamCapture({ raw: Buffer.from(raw, 'utf8'), event, scope: 'sse-event', ...(config.capacityMode === 'passthrough' ? { maximum: Infinity } : {}), 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') {
@@ -110,7 +110,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
if (choice.finish_reason) { if (finish && finish !== choice.finish_reason) throw protocolFailure('Chat流终结原因发生变化。'); finish = choice.finish_reason; }
}
}
if (calls.size > 12 || responseCalls.size > 12) throw protocolFailure('模型流工具数量超出上限。');
if (config.executionMode !== 'unrestricted' && (calls.size > 12 || responseCalls.size > 12)) throw protocolFailure('模型流工具数量超出上限。');
progress(false);
};
const line = value => {
@@ -127,7 +127,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
if (chunk.done) break;
onBytes?.(chunk.value.length);
bytes += chunk.value.length; hash.update(chunk.value);
if (bytes > 4 * 1024 * 1024) throw protocolFailure('模型事件流超过4MB读取上限。');
if (config.capacityMode !== 'passthrough' && bytes > 4 * 1024 * 1024) throw protocolFailure('模型事件流超过4MB读取上限。');
chunks.push(Buffer.from(chunk.value));
buffer += decoder.decode(chunk.value, { stream: true });
// CRLF, LF and CR line endings, including a CR split across chunks.
@@ -159,7 +159,7 @@ export async function readModelStream(response, config, signal, onProgress, tool
if (!done || !['stop', 'tool_calls'].includes(finish)) throw protocolFailure('Chat流未完整终结或达到输出上限,未执行工具。', 'stream_interrupted');
value = { choices: [{ message: { role: 'assistant', content: text, ...(hasReasoning ? { reasoning_content: reasoning } : {}), ...(calls.size ? { tool_calls: [...calls.entries()].sort((a, b) => a[0] - b[0]).map(([, item]) => item) } : {}) }, finish_reason: finish }], usage };
}
validateModelResponse(config.protocol !== 'chat-completions' && config.protocol ? normalizeProtocolResponse(config, value) : value, tools);
validateModelResponse(config.protocol !== 'chat-completions' && config.protocol ? normalizeProtocolResponse(config, value) : value, tools, config);
validated = true; progress(true);
return { value, evidence: { bytes, frames, sha256: hash.copy().digest('hex'), terminal: true, protocol: config.protocol ?? 'chat-completions' } };
} finally {
+3 -2
View File
@@ -1,3 +1,4 @@
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
import { ModelRequestError } from '../../server/diagnostics.mjs';
export const protocolFailure = (message, kind = 'protocol', extra = null) => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request',
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString(), ...(extra ? extra : {}) });
@@ -46,12 +47,12 @@ function acceptsEmptyObject(tools, name) {
['type', 'properties', 'required', 'additionalProperties', 'description', 'title', '$schema'].includes(key));
}
export function validateModelResponse(value, tools) {
export function validateModelResponse(value, tools, config = {}) {
const choice = value.choices?.[0], message = choice?.message;
if (choice?.finish_reason === 'length') throw outputTruncatedFailure();
if (!message || choice.finish_reason === 'content_filter') throw protocolFailure('模型响应缺少完整message或因内容过滤未完整结束,未执行工具。');
const calls = message.tool_calls;
if (calls !== undefined && (!Array.isArray(calls) || calls.length > 12)) throw protocolFailure('模型工具调用数量或格式无效。');
if (calls !== undefined && (!Array.isArray(calls) || !unrestricted(config) && calls.length > 12)) throw protocolFailure('模型工具调用数量或格式无效。');
const ids = new Set();
for (const call of calls ?? []) {
if (!call.id || typeof call.id !== 'string' || ids.has(call.id) || call.type !== 'function' || typeof call.function?.name !== 'string' || !call.function.name || typeof call.function.arguments !== 'string') throw protocolFailure('模型工具身份或参数无效。');
+4 -4
View File
@@ -2,7 +2,7 @@ export const DEFAULT_MODEL_TIMEOUT_MS = 300000;
export const DEFAULT_STREAM_IDLE_TIMEOUT_MS = 120000;
export function modelWaitSettings(config = {}) {
const bounded = (value, fallback, minimum, maximum) => Number.isSafeInteger(value) && value >= minimum && value <= maximum ? value : fallback;
const bounded = (value, fallback, minimum, maximum) => Number.isSafeInteger(value) && (value === 0 || value >= minimum && value <= maximum) ? value : fallback;
return { modelResponseTimeoutMs: bounded(config.modelResponseTimeoutMs, DEFAULT_MODEL_TIMEOUT_MS, 15000, 900000),
modelStreamIdleTimeoutMs: bounded(config.modelStreamIdleTimeoutMs, DEFAULT_STREAM_IDLE_TIMEOUT_MS, 5000, 300000) };
}
@@ -11,7 +11,7 @@ export function validateModelWaitSettings(input, previous = {}) {
const next = modelWaitSettings(previous);
for (const [key, minimum, maximum] of [['modelResponseTimeoutMs', 15000, 900000], ['modelStreamIdleTimeoutMs', 5000, 300000]]) {
if (!Object.hasOwn(input, key)) continue;
if (!Number.isSafeInteger(input[key]) || input[key] < minimum || input[key] > maximum) throw Object.assign(new Error('模型等待时限必须为范围内的整数毫秒。'), { status: 400 });
if (!Number.isSafeInteger(input[key]) || input[key] !== 0 && (input[key] < minimum || input[key] > maximum)) throw Object.assign(new Error('模型等待时限必须为范围内的整数毫秒。'), { status: 400 });
next[key] = input[key];
}
return next;
@@ -29,8 +29,8 @@ export function requestDeadline({ signal, timeoutMs, idleTimeoutMs }) {
controller.abort(new DOMException('Model response deadline exceeded', 'TimeoutError'));
};
let bytes = 0;
absoluteTimer = setTimeout(() => abort('total'), Math.max(1, timeoutMs));
absoluteTimer.unref?.();
if (timeoutMs > 0) absoluteTimer = setTimeout(() => abort('total'), Math.max(1, timeoutMs));
absoluteTimer?.unref?.();
const armIdle = () => {
clearTimeout(idleTimer);
if (idleTimeoutMs > 0 && !controller.signal.aborted && !signal?.aborted) {
+9 -4
View File
@@ -1,19 +1,24 @@
import { openDefaults, approvalModes } from '../../shared/contracts/execution-policy.mjs';
import { CAPACITY_RANGES, capacityMode } from '../../shared/contracts/model-capacity.mjs';
const ranges = {
...CAPACITY_RANGES, maxSteps: [1, 30],
maxRunDurationMs: [1000, 3600000], maxRunTokens: [1000, 2000000], maxTaskTokens: [1000, 10000000],
...CAPACITY_RANGES, maxSteps: [0, Number.MAX_SAFE_INTEGER],
maxRunDurationMs: [0, Number.MAX_SAFE_INTEGER], maxRunTokens: [0, Number.MAX_SAFE_INTEGER], maxTaskTokens: [0, Number.MAX_SAFE_INTEGER],
modelResponseTimeoutMs: [0, Number.MAX_SAFE_INTEGER], modelStreamIdleTimeoutMs: [0, Number.MAX_SAFE_INTEGER],
};
// Budget-only edits cannot change the selected route, endpoint or credential.
export function validateRuntimeBudgets(input) {
if (!input || typeof input !== 'object' || Array.isArray(input) || !Object.keys(input).length
|| Object.keys(input).some(key => !['capacityMode', 'historyPolicy'].includes(key) && !Object.hasOwn(ranges, key))) throw new Error('只接受运行预算字段;模型连接请在模型选择中切换。');
|| Object.keys(input).some(key => !['approvalMode', 'executionMode', 'capacityMode', 'historyPolicy'].includes(key) && !Object.hasOwn(ranges, key))) throw new Error('只接受运行设置字段;模型连接请在模型选择中切换。');
for (const [key, value] of Object.entries(input)) {
if (key === 'approvalMode') { if (!approvalModes.includes(value)) throw new Error('请选择请求批准、帮我批准或完全访问权限。'); continue; }
if (key === 'executionMode') { if (!['unrestricted', 'restricted'].includes(value)) throw new Error('执行方式无效。'); continue; }
if (key === 'capacityMode') { capacityMode(value); continue; }
if (key === 'historyPolicy') { if (!['preserve', 'compact'].includes(value)) throw new Error('请选择完整传递或允许整理历史。'); continue; }
const [min, max] = ranges[key];
if (!Number.isSafeInteger(value) || value < min || value > max) throw new Error(key + ' 需填写范围内的整数。');
}
return { ...input, ...(!Object.hasOwn(input, 'capacityMode') && ['contextLimit', 'maxOutputTokens'].some(key => Object.hasOwn(input, key)) ? { capacityMode: 'manual' } : {}) };
if (input.executionMode === 'unrestricted') return { ...input, ...openDefaults };
return { ...input, ...(input.capacityMode === 'passthrough' ? { historyPolicy: 'preserve' } : {}), ...(!Object.hasOwn(input, 'capacityMode') && ['contextLimit', 'maxOutputTokens'].some(key => Object.hasOwn(input, key)) ? { capacityMode: 'manual' } : {}) };
}
+2 -2
View File
@@ -1,7 +1,7 @@
import { Agent,EnvHttpProxyAgent,fetch } from 'undici';
export class ModelTransport {
constructor({environment=process.env,lookup,connectTimeoutMs=10000}={}){
constructor({environment=process.env,lookup,connectTimeoutMs=0}={}){
const setting=(lower,upper)=>environment[lower]??environment[upper]??'';
this.proxy={httpProxy:setting('http_proxy','HTTP_PROXY'),httpsProxy:setting('https_proxy','HTTPS_PROXY'),noProxy:setting('no_proxy','NO_PROXY')};
this.connect={autoSelectFamily:true,autoSelectFamilyAttemptTimeout:250,timeout:connectTimeoutMs,...(lookup?{lookup}:{})};
@@ -10,7 +10,7 @@ export class ModelTransport {
}
fetch(url,options){
this.dispatcher??=this.profile.httpProxyConfigured||this.profile.httpsProxyConfigured
?new EnvHttpProxyAgent({...this.proxy,connect:this.connect,proxyTls:this.connect,requestTls:this.connect}):new Agent({connect:this.connect});
?new EnvHttpProxyAgent({...this.proxy,connect:this.connect,proxyTls:this.connect,requestTls:this.connect,headersTimeout:0,bodyTimeout:0}):new Agent({connect:this.connect,headersTimeout:0,bodyTimeout:0});
return fetch(url,{...options,dispatcher:this.dispatcher});
}
async close(){const dispatcher=this.dispatcher;this.dispatcher=null;await dispatcher?.close();}
+8 -8
View File
@@ -20,7 +20,7 @@ export function retryAfterMilliseconds(value, now = Date.now()) {
// 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) {
export async function readErrorResponse(response, signal, maximum = ERROR_CAPTURE_LIMIT) {
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;
@@ -30,9 +30,9 @@ export async function readErrorResponse(response, signal) {
const { done, value } = await reader.read();
if (done) break;
receivedBytes += value.length;
const keep = Math.min(value.length, ERROR_CAPTURE_LIMIT - capturedBytes);
const keep = Math.min(value.length, maximum - capturedBytes);
if (keep) { parts.push(Buffer.from(value.subarray(0, keep))); capturedBytes += keep; }
if (receivedBytes > ERROR_CAPTURE_LIMIT) { truncated = true; break; }
if (receivedBytes > maximum) { truncated = true; break; }
}
} catch (cause) {
interrupted = true;
@@ -44,8 +44,8 @@ export async function readErrorResponse(response, signal) {
}
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');
headers, event, scope = 'http-body', maximum = ERROR_CAPTURE_LIMIT }, redact = String) {
const bounded = raw.subarray(0, Number.isFinite(maximum) ? maximum : raw.length), 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;
@@ -70,7 +70,7 @@ export function upstreamCapture({ raw, receivedBytes = raw.length, truncated = f
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),
param: field('param', fields?.param), reason: field('reason', parsed?.response?.incomplete_details?.reason ?? parsed?.incomplete_details?.reason ?? parsed?.choices?.[0]?.finish_reason ?? parsed?.stop_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),
@@ -80,8 +80,8 @@ export function upstreamCapture({ raw, receivedBytes = raw.length, truncated = f
...(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],
complete: !truncated && !interrupted && raw.length <= maximum,
truncated: truncated || raw.length > maximum, interrupted, capturedRange: [0, bounded.length],
redacted: changed, sha256: createHash('sha256').update(capture).digest('hex') },
...(shortenedFields.length ? { shortenedFields } : {}),
};