feat: bind collaboration roles and verify outputs with required Messages limits

This commit is contained in:
Agent Workbench Dev
2026-11-06 18:45:48 +08:00
parent e892b53c1e
commit fa74f5888d
23 changed files with 498 additions and 50 deletions
+7 -2
View File
@@ -1,4 +1,4 @@
import { isPassthrough } from './model-capacity.mjs';
import { isPassthrough, messagesOutputSetting } from './model-capacity.mjs';
const object = value => value && typeof value === 'object' && !Array.isArray(value);
const fail = message => { throw new Error('Messages 请求未发送:' + message); };
@@ -14,6 +14,7 @@ function contentBlocks(content) {
}
export function messagesBody(config, messages, tools, native) {
const protocolOutput = messagesOutputSetting(config).value;
if (!isPassthrough(config) && native.nativeParameters.thinking?.type === 'enabled' && native.nativeParameters.thinking.budget_tokens >= config.maxOutputTokens) fail('thinking.budget_tokens 必须小于本次 max_tokens 输出预算。');
const system = [], wire = [];
const append = (role, content) => {
@@ -50,7 +51,11 @@ export function messagesBody(config, messages, tools, native) {
});
return { model: config.model, ...(system.length ? { system } : {}), messages: wire,
...(definitions.length ? { tools: definitions, tool_choice: { type: 'auto' } } : {}),
...(!isPassthrough(config) ? { max_tokens: config.maxOutputTokens } : {}), stream: config.streaming === true, ...native.nativeParameters };
// Messages requires max_tokens, even in passthrough mode. Prefer the
// synchronized upstream maximum; native parameters below remain exact.
// A saved connection declaration is a fallback, never a verified maximum.
max_tokens: isPassthrough(config) ? protocolOutput : config.maxOutputTokens,
stream: config.streaming === true, ...native.nativeParameters };
}
export function messagesHistory(body) {
@@ -2,3 +2,4 @@ export const DEFAULT_MODEL_CAPACITY: Readonly<{ contextLimit: number; maxOutputT
export const CAPACITY_RANGES: Readonly<Record<'contextLimit' | 'maxOutputTokens', number[]>>;
export function capacitySettings(input?: { contextLimit?: number; maxOutputTokens?: number }, previous?: { contextLimit?: number; maxOutputTokens?: number }): { contextLimit: number; maxOutputTokens: number };
export function capacityMode(value?: string): 'model' | 'manual';
export function messagesOutputSetting(config: { model?: string; nativeParameters?: Record<string, unknown>; maxOutputTokens?: number; upstreamModel?: { id: string; maxOutputTokens?: number | null } }): { value: unknown; source: string };
+9
View File
@@ -16,6 +16,15 @@ export function capacitySettings(input = {}, previous = DEFAULT_MODEL_CAPACITY,
export const isPassthrough = config => config.capacityMode === 'passthrough';
export function messagesOutputSetting(config) {
const member = config.modelRouting?.members?.find(value => value.id === config.modelConnectionId) ?? config;
const native = config.nativeParameters ?? member.nativeParameters ?? {};
if (Object.hasOwn(native, 'max_tokens')) return { value: native.max_tokens, source: 'explicit_native' };
const upstream = member.upstreamModel;
if (upstream?.id === member.model && upstream.maxOutputTokens) return { value: upstream.maxOutputTokens, source: 'upstream_catalog' };
return { value: member.maxOutputTokens ?? config.maxOutputTokens ?? DEFAULT_MODEL_CAPACITY.maxOutputTokens, source: 'connection_declaration' };
}
export function capacityMode(value = 'manual') {
if (!['passthrough', 'model', 'manual'].includes(value)) throw Object.assign(new Error('请选择默认透传、按模型容量或手动限制。'), { status: 400 });
return value;
+12 -3
View File
@@ -44,7 +44,11 @@ export function collaborationInput(input, config) {
maxDurationMs: bounded(raw.maxDurationMs, config.maxRunDurationMs ?? 600000, 1000, Math.min(3600000, config.maxRunDurationMs ?? 600000), '总时限'),
};
const children = input.children.map((item, index) => {
if (!item || typeof item !== 'object' || Array.isArray(item) || Object.keys(item).some(key => !['label', 'goal', 'paths', 'budget'].includes(key))) throw workerConflict('子任务字段无效。');
if (!item || typeof item !== 'object' || Array.isArray(item) || Object.keys(item).some(key => !['label', 'goal', 'paths', 'budget', 'role', 'requirements', 'modelSelection'].includes(key))) throw workerConflict('子任务字段无效。');
const selection = item.modelSelection;
if (selection !== undefined && (!selection || Array.isArray(selection) || Object.keys(selection).some(key => !['type', 'id'].includes(key))
|| !['connection', 'pool'].includes(selection.type) || typeof selection.id !== 'string' || !selection.id)) throw workerConflict('请选择模型中心已保存的连接或模型池。');
if (item.requirements !== undefined && !Array.isArray(item.requirements)) throw workerConflict('子任务要求需要逐项列出。');
if (!Array.isArray(item.paths) || !item.paths.length || !unrestricted(config) && item.paths.length > 16) throw workerConflict('每个子任务需要1到16个修改范围。');
const paths = item.paths.map(workerPath).sort();
if (new Set(paths.map(workerPathKey)).size !== paths.length) throw workerConflict('子任务范围不能重复。');
@@ -52,6 +56,8 @@ export function collaborationInput(input, config) {
if (!part || Array.isArray(part) || Object.keys(part).some(key => !['maxTokens', 'maxSteps', 'maxToolCalls'].includes(key))) throw workerConflict('子预算字段无效。');
const allocate = key => engine ? budget[key] : index === 0 ? Math.floor(budget[key] / 2) : budget[key] - Math.floor(budget[key] / 2);
return { label: text(item.label, ceiling(config, 64), '子任务名称'), goal: text(item.goal, ceiling(config, 8000), '子目标'), paths,
role: text(item.role ?? '工程实现', ceiling(config, 128), '子任务职责'), requirements: (item.requirements ?? []).map(value => text(value, ceiling(config, 8000), '子任务要求')),
...(selection ? { modelSelection: structuredClone(selection) } : {}),
budget: Object.fromEntries(['maxTokens', 'maxSteps', 'maxToolCalls'].map(key => [key, unrestricted(config) ? 0 : bounded(part[key], allocate(key), key === 'maxTokens' ? 500 : 1, budget[key], key)])) };
});
for (const key of ['maxTokens', 'maxSteps', 'maxToolCalls']) if (children.reduce((sum, child) => sum + child.budget[key], 0) > budget[key]) throw workerConflict('子任务预算之和不能超过父预算:' + key);
@@ -69,7 +75,8 @@ export function workerSeal(worker) {
projectId: worker.projectId, sessionId: worker.sessionId, taskId: worker.taskId, workspaceId: worker.workspaceId,
label: worker.label, goal: worker.goal, paths: worker.paths, budget: worker.budget, model: worker.model, baseCommit: worker.baseCommit,
baselineArtifactId: worker.baselineArtifactId, baselineSha256: worker.baselineSha256, baselineRunId: worker.baselineRunId,
...(worker.engine ? { engine: worker.engine } : {}), ...(worker.evaluation ? { evaluation: worker.evaluation } : {}) };
...(worker.engine ? { engine: worker.engine } : {}), ...(worker.evaluation ? { evaluation: worker.evaluation } : {}),
...(worker.assignment ? { assignment: worker.assignment } : {}) };
}
export function workerForProject(store, projectId) {
return [...(store.core.worker?.values() ?? [])].find(value => value.projectId === projectId) ?? null;
@@ -84,11 +91,13 @@ export function workerForSession(store, session) {
|| definition.taskId !== worker.taskId || definition.projectId !== worker.projectId || definition.sessionId !== worker.sessionId
|| definition.workspaceId !== worker.workspaceId || JSON.stringify(definition.paths) !== JSON.stringify(worker.paths)
|| JSON.stringify(definition.budget) !== JSON.stringify(worker.budget) || definition.goal !== worker.goal
|| JSON.stringify(group.model) !== JSON.stringify(worker.model)
|| JSON.stringify(definition.assignment?.model ?? group.model) !== JSON.stringify(worker.model)
|| JSON.stringify(definition.assignment) !== JSON.stringify(worker.assignment)
|| JSON.stringify(definition.engine) !== JSON.stringify(worker.engine)
|| JSON.stringify(definition.evaluation) !== JSON.stringify(worker.evaluation)) throw workerConflict('子任务与父任务的封存分工不一致。');
const task = store.core.task.get(worker.taskId);
if (!task || task.goal !== worker.goal || task.workerId !== worker.id || task.budget.maxTokens !== worker.budget.maxTokens) throw workerConflict('子任务目标、身份或累计预算已变化。');
if (worker.assignment && JSON.stringify(task.acceptance) !== JSON.stringify(worker.assignment.requirements)) throw workerConflict('子任务原要求与封存分工不一致。');
const original = store.owned('artifact', worker.contractArtifactId, session.id);
if (original.kind !== 'worker-contract' || original.taskId !== worker.taskId || original.sha256 !== worker.contractSha256
|| store.artifactStore.read(original) !== JSON.stringify(workerSeal(worker))) throw workerConflict('受限子任务契约与原工件不一致。');