feat(vision): bind task screenshots, route vision and resume budget pauses

This commit is contained in:
Agent Workbench Dev
2026-11-01 00:48:38 +08:00
parent 0d1ab743f1
commit 96a7ed19c7
79 changed files with 1721 additions and 36 deletions
+10 -6
View File
@@ -2,6 +2,7 @@ import { randomUUID, createHash } from 'node:crypto';
import { setTimeout as delay } from 'node:timers/promises';
import { ModelRequestError } from '../../server/diagnostics.mjs';
import { CredentialVault } from './credential-vault.mjs';
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';
@@ -118,7 +119,7 @@ export class ModelHub {
storage: { credentials: this.credentialVault ? 'memory-or-windows-user-dpapi' : 'memory', restart: '临时凭据在宿主退出后需要重新填写;已明确本机加密保存的连接按用户与原配置加载。', attemptsRetained: 1000 } }; }
connection(id) { const value = this.connections().find(item => item.id === id); if (!value) throw error('模型连接不存在。', 404); return value; }
async probe(id, scope = 'text') {
if (!['text', 'tools', 'streaming'].includes(scope)) throw error('请选择文本、工具调用或流式检测。');
if (!['text', 'tools', 'streaming', 'vision'].includes(scope)) throw error('请选择文本、工具调用、流式或图像检测。');
if (this.probing) throw error('已有模型能力检测正在执行,请等待结束。', 409);
this.probing = true;
try {
@@ -131,13 +132,16 @@ export class ModelHub {
const config = { ...this.store.config, mode: 'compatible', model: connection.model, baseUrl: connection.baseUrl,
protocol: modelProtocol(connection.protocol), maxOutputTokens: 256, capabilityProbe: true, modelRouting: { type: 'connection', id: connection.id, revision: connection.revision,
strategy: 'round-robin', sticky: false, maxAttempts: 1, queueTimeoutMs: 1000, members: [member] } };
const visual=scope==='vision'?visionProbe():null;
if(visual)config.inputImages=[visual.image];
let attemptId;
const result = await this.provider.complete(config, [{ role: 'user', content: scope === 'tools' ? 'Call workbench_capability_echo with nonce ' + nonce + '. Do not reply with plain text.' : 'Reply OK.' }], null, undefined,
const result = await this.provider.complete(config, [{ role: 'user', content: visual?.prompt ?? (scope === 'tools' ? 'Call workbench_capability_echo with nonce ' + nonce + '. Do not reply with plain text.' : 'Reply OK.') }], null, undefined,
event => { if (event.modelAttemptId) attemptId = event.modelAttemptId; }, null, tools, { attempts: 1 });
if (scope === 'tools') {
const calls = result.message.tool_calls;
if (calls?.length !== 1 || calls[0].function.name !== 'workbench_capability_echo' || JSON.parse(calls[0].function.arguments).nonce !== nonce) throw error('服务没有返回要求的原工具调用;未标为已验证。');
} else if (!result.message.content.trim() || result.message.tool_calls?.length) throw error('服务没有返回纯文本响应;未标为已验证。');
if(visual&&result.message.content.trim().toUpperCase().replace(/\s/g,'')!==visual.expected)throw error('服务未正确识别合成图像的四色顺序,原请求保留,未标为视觉已验证。');
if (this.connection(id).revision !== connection.revision || (this.credential(connection)?.revision ?? 0) !== member.credentialRevision) throw error('检测期间连接已变化,原结果保留但不更新能力结论。', 409);
this.editRegistry(() => {
this.state.capabilityResults ??= {};
@@ -217,9 +221,8 @@ export class ModelHub {
this.save(config ?? (input.type !== 'legacy' ? { mode: 'compatible' } : null));
return this.view();
}
prepare(config) {
if (config.mode === 'demo' || this.state.selected.type === 'legacy') return config;
const selection = this.state.selected;
prepare(config, selection = this.state.selected) {
if (config.mode === 'demo' || selection.type === 'legacy') return config;
const pool = selection.type === 'pool' ? this.state.pools.find(value => value.id === selection.id) : null;
if (selection.type === 'pool' && !pool) throw error('选中的模型池不存在。');
const members = (pool?.members ?? [{ connectionId: selection.id, weight: 1 }]).map(value => ({ ...this.connection(value.connectionId), capabilityEvidence: this.state.capabilityResults?.[value.connectionId], weight: value.weight,
@@ -272,7 +275,8 @@ export class ModelHub {
async completeOwned(config, body, signal, onProgress, plan, execute) {
const routing = config.modelRouting, excluded = new Set(), deadline = 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'] : [])];
const requirements = config.capabilityProbe ? [] : ['text', ...(requestVariants(body).some(value => value.tools?.length) ? ['tools'] : []),
...(requestVariants(body).some(value => (value.messages??value.input??[]).some(message=>Array.isArray(message.content)&&message.content.some(part=>['image_url','input_image'].includes(part.type))))?['vision']:[])];
const rejections = config.capabilityProbe ? [] : 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) {
+13
View File
@@ -0,0 +1,13 @@
import { randomInt, createHash, randomUUID } from 'node:crypto';
import { PNG } from 'pngjs';
// Synthetic pixels only. The expected order is never placed in the prompt.
export function visionProbe() {
const colors = [{name:'RED',rgb:[230,0,0]},{name:'GREEN',rgb:[0,180,0]},{name:'BLUE',rgb:[0,0,230]},{name:'YELLOW',rgb:[240,220,0]}];
for (let i=colors.length-1;i>0;i--) { const j=randomInt(i+1);[colors[i],colors[j]]=[colors[j],colors[i]]; }
const png=new PNG({width:128,height:128});
for(let y=0;y<128;y++)for(let x=0;x<128;x++)png.data.set([...colors[(y>=64?2:0)+(x>=64?1:0)].rgb,255],(y*128+x)*4);
const bytes=PNG.sync.write(png);
return {expected:colors.map(value=>value.name).join(','),prompt:'Read the four colored squares in this attached synthetic image in order: top-left, top-right, bottom-left, bottom-right. Reply only with four comma-separated English color names, using RED, GREEN, BLUE, YELLOW. Do not call tools.',
image:{id:randomUUID(),sha256:createHash('sha256').update(bytes).digest('hex'),width:128,height:128,dataUrl:'data:image/png;base64,'+bytes.toString('base64')}};
}