feat(runtime): default to passthrough and persist execution permissions
This commit is contained in:
+25
-15
@@ -1,9 +1,11 @@
|
||||
import { unrestricted, ceiling } from '../shared/contracts/execution-policy.mjs';
|
||||
import { validateRuntimeBudgets } from '../core/models/runtime-settings.mjs';
|
||||
import { capacitySettings, capacityMode } from '../shared/contracts/model-capacity.mjs';
|
||||
import fs from 'node:fs';
|
||||
import { createHash } from 'node:crypto';
|
||||
import path from 'node:path';
|
||||
import { listFiles, readText, proposeFile, proposePatch, applyChange } from './files.mjs';
|
||||
import { listFiles, readText, proposeFile, proposePatch, applyChange, rollbackChange } from './files.mjs';
|
||||
import { ensureGitBaseline } from '../core/workspaces/git-baseline.mjs';
|
||||
import { APP_VERSION, executionEnvironment, sessionSnapshot } from './diagnostics.mjs';
|
||||
import { inspectProject } from '../core/project-inspector.mjs';
|
||||
import { assertProposalUnsealed } from '../shared/contracts/change-sets.mjs';
|
||||
@@ -60,7 +62,7 @@ function json(response, value, status = 200) {
|
||||
'X-Content-Type-Options': 'nosniff' });
|
||||
response.end(JSON.stringify(value));
|
||||
}
|
||||
async function body(request, maximum=2*1024*1024) {
|
||||
async function body(request, maximum=Infinity) {
|
||||
if (!String(request.headers['content-type'] ?? '').startsWith('application/json')) throw Object.assign(new Error('请求需要 JSON。'), { status: 415 });
|
||||
const chunks = []; let length = 0;
|
||||
for await (const chunk of request) {
|
||||
@@ -125,7 +127,7 @@ return async (request, response) => {
|
||||
const input = await body(request); const result = host().unregisterWindow(input); runtime.browsing?.disconnect(input.windowId); return json(response, result);
|
||||
}
|
||||
if (method === 'POST' && /^\/api\/browsing-desktop\/(poll|complete|validate)$/.test(route)) {
|
||||
const input = await body(request, 12 * 1024 * 1024), owner = input.owner, registered = host().windows?.get(owner?.windowId);
|
||||
const input = await body(request, ceiling(store, 12 * 1024 * 1024)), owner = input.owner, registered = host().windows?.get(owner?.windowId);
|
||||
if (!registered || ['windowId','pid','createdAt','instanceId','runtimeIdentity'].some(key => registered[key] !== owner[key])) throw Object.assign(new Error('原桌面进程登记已变化。'), { status: 403 });
|
||||
runtime.workbenchOrigin = 'http://127.0.0.1:' + actualPort; const manager = runtime.browsingManager(); manager.workbenchOrigin = runtime.workbenchOrigin;
|
||||
if (route.endsWith('/poll')) return json(response, manager.poll(owner.windowId, input.states));
|
||||
@@ -204,6 +206,11 @@ return async (request, response) => {
|
||||
const input = await body(request);
|
||||
if (route === '/api/model-center/catalog/cancel') return json(response, hub.modelCatalog().cancel(input.id));
|
||||
if (route === '/api/model-center/toggle') return json(response, hub.toggle(input.id, input.enabled));
|
||||
if (route === '/api/model-center/permissions') {
|
||||
if (Object.keys(input).length !== 1 || !Object.hasOwn(input, 'approvalMode')) throw new Error('权限设置只接受操作批准方式。');
|
||||
store.saveConfig(validateRuntimeBudgets(input)); runtime.permissionsChanged();
|
||||
return json(response, { config: { ...store.config, hasKey: provider.hasKey() }, modelSelection: hub.selectionView() });
|
||||
}
|
||||
if (runtime.hasActivity() || hub.networkChecks?.active.size) throw Object.assign(new Error('请等待正在运行的任务或连接诊断结束,再修改或测试模型配置。'), { status: 409 });
|
||||
if (route === '/api/model-center/runtime-budgets') {
|
||||
store.saveConfig(validateRuntimeBudgets(input));
|
||||
@@ -366,7 +373,7 @@ return async (request, response) => {
|
||||
if (action === 'input-attachments') {
|
||||
if (method === 'GET') return json(response,runtime.inputAttachments().list(session));
|
||||
if (workerForProject(store,session.projectId)) throw workerConflict('截图附件请在父任务中添加。');
|
||||
if (method === 'POST') return json(response,runtime.inputAttachments().upload(session,await body(request,3*1024*1024)),201);
|
||||
if (method === 'POST') return json(response,runtime.inputAttachments().upload(session,await body(request,ceiling(store,3*1024*1024))),201);
|
||||
}
|
||||
const inputImage = action.match(/^input-attachments\/([a-f0-9-]+)(?:\/(image|original))?$/);
|
||||
if (inputImage) {
|
||||
@@ -832,13 +839,14 @@ return async (request, response) => {
|
||||
runtime.decide(id, approval[1], input.allow === true);
|
||||
return json(response, store.publicSession(session));
|
||||
}
|
||||
const change = action.match(/^changes\/([a-f0-9-]+)\/(apply|reject)$/);
|
||||
const change = action.match(/^changes\/([a-f0-9-]+)\/(apply|reject|rollback)$/);
|
||||
if (method === 'POST' && change) {
|
||||
await body(request);
|
||||
if (runtime.projectHasActivity(session.projectId)) throw Object.assign(new Error('项目正在执行任务或检查,请等待结束或停止后再审查文件改动。'), { status: 409 });
|
||||
assertWorkspacesResolved(store, session.projectId, { includePrepared: true });
|
||||
await verifyWorkspaceProject(store, store.project(session.projectId));
|
||||
if (change[2] === 'apply') applyChange(store, session, change[1]);
|
||||
if (change[2] === 'rollback') rollbackChange(store, session, change[1]);
|
||||
else if (change[2] === 'apply') { if (unrestricted(store)) await ensureGitBaseline(store, session); applyChange(store, session, change[1]); }
|
||||
else {
|
||||
const item = session.changes.find(candidate => candidate.id === change[1]);
|
||||
if (!item || !['pending', 'conflict'].includes(item.status)) throw new Error('改动无法撤回。');
|
||||
@@ -854,8 +862,9 @@ return async (request, response) => {
|
||||
if (runtime.workers?.projectActive(session.projectId) || runtime.checks?.projectActive(session.projectId) || runtime.deliveries?.projectActive(session.projectId) || runtime.changeSets?.projectActive(session.projectId) || runtime.workspaces?.projectActive(session.projectId)) throw Object.assign(new Error('协作、工程检查、Git交付、文件批次或工作区正在执行,请等待结束后再编辑文件。'), { status: 409 });
|
||||
assertWorkspacesResolved(store, session.projectId, { includePrepared: true });
|
||||
await verifyWorkspaceProject(store, store.project(session.projectId));
|
||||
if (action === 'patch-proposals') proposePatch(store, session, input);
|
||||
else proposeFile(store, session, input);
|
||||
if (unrestricted(store)) await ensureGitBaseline(store, session);
|
||||
const result = action === 'patch-proposals' ? proposePatch(store, session, input) : proposeFile(store, session, input);
|
||||
if (unrestricted(store) && result.change_id) applyChange(store, session, result.change_id);
|
||||
return json(response, store.publicSession(session), 201);
|
||||
}
|
||||
}
|
||||
@@ -865,12 +874,13 @@ return async (request, response) => {
|
||||
if (!['demo', 'compatible'].includes(input.mode)) throw new Error('请选择有效运行模式。');
|
||||
const config = {
|
||||
mode: input.mode, baseUrl: String(input.baseUrl || '').trim(), model: String(input.model || '').trim(),
|
||||
...capacitySettings(input, store.config), capacityMode: capacityMode(input.capacityMode),
|
||||
maxSteps: Math.trunc(Math.max(1, Math.min(30, Number(input.maxSteps) || 12))),
|
||||
...capacitySettings(input, store.config), capacityMode: capacityMode(input.capacityMode ?? store.config.capacityMode), historyPolicy: (input.capacityMode ?? store.config.capacityMode) === 'passthrough' ? 'preserve' : input.historyPolicy ?? store.config.historyPolicy,
|
||||
executionMode: input.executionMode ?? store.config.executionMode,
|
||||
maxSteps: validateRuntimeBudgets({ maxSteps: input.maxSteps ?? store.config.maxSteps }).maxSteps,
|
||||
...validateModelWaitSettings(input, store.config),
|
||||
maxRunDurationMs: Math.trunc(Math.max(1000, Math.min(3600000, Number(input.maxRunDurationMs) || store.config.maxRunDurationMs))),
|
||||
maxRunTokens: Math.trunc(Math.max(1000, Math.min(2000000, Number(input.maxRunTokens) || store.config.maxRunTokens))),
|
||||
maxTaskTokens: Math.trunc(Math.max(1000, Math.min(10000000, Number(input.maxTaskTokens) || store.config.maxTaskTokens))),
|
||||
maxRunDurationMs: validateRuntimeBudgets({ maxRunDurationMs: input.maxRunDurationMs ?? store.config.maxRunDurationMs }).maxRunDurationMs,
|
||||
maxRunTokens: validateRuntimeBudgets({ maxRunTokens: input.maxRunTokens ?? store.config.maxRunTokens }).maxRunTokens,
|
||||
maxTaskTokens: validateRuntimeBudgets({ maxTaskTokens: input.maxTaskTokens ?? store.config.maxTaskTokens }).maxTaskTokens,
|
||||
};
|
||||
provider.validate(config);
|
||||
if (Object.hasOwn(input, 'apiKey') && (typeof input.apiKey !== 'string' || input.apiKey.length > 16000)) throw new Error('模型凭据格式无效。');
|
||||
@@ -891,8 +901,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 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,
|
||||
const native = nativeParameterSettings('chat-completions', config.nativeParameters ?? {}, config.outputTokenField, config.parameterProfile, config);
|
||||
const result = await provider.request(config, '/chat/completions', { model: config.model, stream: false, ...(config.capacityMode === 'passthrough' ? {} : { [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。');
|
||||
|
||||
Reference in New Issue
Block a user