247 lines
17 KiB
JavaScript
247 lines
17 KiB
JavaScript
// Test-only browser-accessible provider. Never used by the product or business API.
|
|
const calls = [];
|
|
const cadAgentCalls = [];
|
|
let next = 0;
|
|
const send = (res, value, status = 200) => { res.writeHead(status, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' }); res.end(JSON.stringify(value)); };
|
|
const textContent = (message) => typeof message?.content === 'string' ? message.content : (message?.content || []).filter((item) => item.type === 'text').map((item) => item.text).join('\n');
|
|
export async function assistantModel(req, res, url, bytes) {
|
|
if (url.pathname.startsWith('/cad/ai/')) {
|
|
if (req.headers.authorization !== 'Bearer cad-browser-test-token') {
|
|
send(res, { code: 'UNAUTHORIZED', message: 'Sign in required', data: null, requestId: 'cad-agent-test' }, 401);
|
|
return true;
|
|
}
|
|
if (url.pathname === '/cad/ai/requests') { send(res, cadAgentCalls); return true; }
|
|
if (url.pathname === '/cad/ai/capabilities' && req.method === 'GET') {
|
|
cadAgentCalls.push({ path: url.pathname, method: req.method, authorization: req.headers.authorization });
|
|
send(res, { code: 'OK', message: 'OK', requestId: 'cad-agent-capabilities', data: {
|
|
enabled: true, protocolVersion: 'cad-agent-v1', model: 'Mock CAD Agent',
|
|
tools: [
|
|
{ name: 'batch', description: 'Run CAD commands transactionally', parameters: { type: 'object', properties: {
|
|
commands: { type: 'array', items: { type: 'object' } }, rollback: { type: 'boolean' },
|
|
}, required: ['commands'], additionalProperties: false } },
|
|
{ name: 'execute', description: 'Run a CAD command', parameters: { type: 'object', properties: {
|
|
command: { type: 'string' }, params: { type: 'object' }, verbose: { type: 'boolean' }, expect: { type: 'object' },
|
|
}, required: ['command'], additionalProperties: false } },
|
|
{ name: 'inspect_design', description: 'Inspect the current design', parameters: { type: 'object', properties: {
|
|
measure: { type: 'boolean' }, sketches: { type: 'boolean' }, joints: { type: 'boolean' },
|
|
}, additionalProperties: false } },
|
|
{ name: 'measure', description: 'Measure a body or the design summary', parameters: { type: 'object', properties: {
|
|
body: { type: 'string' }, summary: { type: 'boolean' },
|
|
}, additionalProperties: false } },
|
|
{ name: 'screenshot', description: 'Capture the CAD model view', parameters: { type: 'object', properties: {
|
|
view: { type: 'string' }, width: { type: 'integer' }, height: { type: 'integer' }, source: { type: 'string' },
|
|
camera: { type: 'object' },
|
|
grid: { type: 'boolean' }, axes: { type: 'boolean' },
|
|
}, additionalProperties: false } },
|
|
],
|
|
limits: {
|
|
requestBytes: 4194304, imageBytes: 1048576, imageMaxEdge: 2048, imagesPerRequest: 2, textBytes: 262144,
|
|
toolResultTextBytes: 32768, historyItems: 256, maxModelCallsPerTurn: 12, maxChangesPerTurn: 24,
|
|
requestTimeoutSeconds: 60, requestsPerMinute: 30,
|
|
},
|
|
} });
|
|
return true;
|
|
}
|
|
if (url.pathname !== '/cad/ai/chat' || req.method !== 'POST') {
|
|
send(res, { code: 'NOT_FOUND', message: 'Not found', data: null, requestId: 'cad-agent-test' }, 404);
|
|
return true;
|
|
}
|
|
let body;
|
|
try { body = JSON.parse(bytes.toString()); } catch { send(res, { code: 'BAD_REQUEST', message: 'Invalid JSON', data: null }, 400); return true; }
|
|
const revision = body.context?.documentRevision;
|
|
if (typeof revision !== 'string' || revision.length < 1 || revision.length > 128) {
|
|
send(res, { code: 'BAD_REQUEST', message: 'Invalid documentRevision', data: null }, 400);
|
|
return true;
|
|
}
|
|
const record = { path: url.pathname, authorization: req.headers.authorization, receivedAt: Date.now(), body };
|
|
cadAgentCalls.push(record);
|
|
const history = body.history || [], last = history.at(-1);
|
|
const prompt = [...history].reverse().find((item) => item.type === 'message' && item.role === 'user');
|
|
const text = textContent(prompt);
|
|
const envelope = (data) => ({ code: 'OK', message: 'OK', data: { documentSessionCode: body.documentSessionCode, turnCode: body.turnCode, ...data }, requestId: 'cad-agent-request-' + cadAgentCalls.length });
|
|
const write = async (event, data) => {
|
|
const bytes = Buffer.from(`event:${event}\r\ndata:${JSON.stringify(envelope(data))}\r\n\r\n`);
|
|
for (let at = 0; at < bytes.length; at += 17) {
|
|
if (res.destroyed) return;
|
|
res.write(bytes.subarray(at, at + 17));
|
|
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
}
|
|
};
|
|
res.writeHead(200, { 'Content-Type': 'text/event-stream; charset=utf-8', 'Cache-Control': 'no-store', 'X-Accel-Buffering': 'no' });
|
|
if (text.includes('vm-test:tool-text-budget')) {
|
|
const step = history.filter((item) => item.type === 'toolResult').length;
|
|
if (step >= 2) {
|
|
await write('completed', { finishReason: 'stop', output: [{ type: 'message', role: 'assistant', content: [
|
|
{ type: 'text', text: 'Used a smaller read-only summary after the oversized result; no modifications were replayed.' },
|
|
] }], usage: null });
|
|
} else {
|
|
const tool = step === 0 ? { name: 'inspect_design', arguments: { measure: true } }
|
|
: { name: 'measure', arguments: { body: 'LongTurnBox_0', summary: true } };
|
|
const callCode = 'text_budget_' + cadAgentCalls.length;
|
|
await write('tool_call', { callCode, ...tool });
|
|
await write('completed', { finishReason: 'tool_calls', output: [{ type: 'toolCall', callCode, ...tool }], usage: null });
|
|
}
|
|
res.end(); return true;
|
|
}
|
|
if (text.includes('vm-test:budget-')) {
|
|
const step = history.filter((item) => item.type === 'toolResult').length;
|
|
if (step >= 4) {
|
|
await write('completed', { finishReason: 'stop', output: [{ type: 'message', role: 'assistant', content: [
|
|
{ type: 'text', text: 'Finished with the backend image budget and CAD inspection.' },
|
|
] }], usage: null });
|
|
} else {
|
|
// Deliberately request one image beyond the default quota. The frontend
|
|
// must stop it before changing camera/rendering, then still feed back the error.
|
|
const tool = step < 3 ? { name: 'screenshot', arguments: { view: ['iso', 'front', 'top'][step], width: 160, height: 100 } }
|
|
: { name: 'inspect_design', arguments: {} };
|
|
const callCode = 'budget_' + cadAgentCalls.length;
|
|
await write('tool_call', { callCode, ...tool });
|
|
await write('completed', { finishReason: 'tool_calls', output: [{ type: 'toolCall', callCode, ...tool }], usage: null });
|
|
}
|
|
res.end(); return true;
|
|
}
|
|
if (text.includes('vm-test:long-turn')) {
|
|
// Keep capabilities at 12 calls / 24 changes to catch frontend per-turn caps.
|
|
const step = history.filter((item) => item.type === 'toolResult').length;
|
|
if (step < 15) {
|
|
const tool = { name: 'batch', arguments: { commands: [0, 1].map((offset) => {
|
|
const index = step * 2 + offset;
|
|
return { command: 'solid.box', params: {
|
|
length: 2, width: 3, height: 4, corner: [index * 4, 0, 0], body_name: 'LongTurnBox_' + index,
|
|
} };
|
|
}) } };
|
|
const callCode = 'long_turn_' + cadAgentCalls.length;
|
|
await write('tool_call', { callCode, ...tool });
|
|
await write('completed', { finishReason: 'tool_calls', output: [{ type: 'toolCall', callCode, ...tool }], usage: null });
|
|
} else {
|
|
await write('completed', { finishReason: 'stop', output: [{ type: 'message', role: 'assistant', content: [
|
|
{ type: 'text', text: 'Completed 30 CAD changes over 16 model calls in one turn.' },
|
|
] }], usage: null });
|
|
}
|
|
res.end(); return true;
|
|
}
|
|
if (text.includes('vm-test:recover') || text.includes('vm-test:partial')) {
|
|
const results = history.filter((item) => item.type === 'toolResult');
|
|
const partial = text.includes('vm-test:partial');
|
|
const step = results.length;
|
|
let tool;
|
|
if (step === 0) {
|
|
// The gear prompt in the report used m for its module, but m is the reserved metre unit.
|
|
const bad = { command: 'set_parameter', params: { name: 'm', value: '3 mm' } };
|
|
tool = { name: 'batch', arguments: partial
|
|
? { rollback: false, commands: [{ command: 'solid.box', params: { length: 2, width: 3, height: 4, body_name: 'KeptStep' } }, bad] }
|
|
: { commands: Array.from({ length: 24 }, () => bad) } };
|
|
} else if (step === 1) {
|
|
tool = { name: 'inspect_design', arguments: { measure: true } };
|
|
} else if (step === 2) {
|
|
const parameter = partial ? 'partial_gear_module' : 'gear_module';
|
|
tool = { name: 'batch', arguments: { commands: [
|
|
{ command: 'set_parameter', params: { name: parameter, value: '3 mm' } },
|
|
{ command: 'solid.box', params: { length: parameter, width: 3, height: 4, body_name: partial ? 'PartialRecovery' : 'RecoveredBox' } },
|
|
] } };
|
|
} else {
|
|
const resultText = 'Analysed the failed parameter, inspected the design, and completed the corrected step.';
|
|
await write('completed', { finishReason: 'stop', output: [{ type: 'message', role: 'assistant', content: [{ type: 'text', text: resultText }] }], usage: null });
|
|
res.end(); return true;
|
|
}
|
|
const callCode = 'recovery_' + cadAgentCalls.length;
|
|
await write('tool_call', { callCode, ...tool });
|
|
await write('completed', { finishReason: 'tool_calls', output: [{ type: 'toolCall', callCode, ...tool }], usage: null });
|
|
res.end(); return true;
|
|
}
|
|
if (text.includes('vm-test:screenshot-recover')) {
|
|
const step = history.filter((item) => item.type === 'toolResult').length;
|
|
const tool = step === 0 ? { name: 'screenshot', arguments: { view: 'MissingAgentView', width: 320, height: 200 } }
|
|
: step === 1 ? { name: 'inspect_design', arguments: {} }
|
|
: { name: 'screenshot', arguments: { view: 'iso', width: 320, height: 200 } };
|
|
if (step >= 3) {
|
|
await write('completed', { finishReason: 'stop', output: [{ type: 'message', role: 'assistant', content: [
|
|
{ type: 'text', text: 'Inspected the design, corrected the view, and received the screenshot.' },
|
|
] }], usage: null });
|
|
} else {
|
|
const callCode = 'screenshot_recovery_' + cadAgentCalls.length;
|
|
await write('tool_call', { callCode, ...tool });
|
|
await write('completed', { finishReason: 'tool_calls', output: [{ type: 'toolCall', callCode, ...tool }], usage: null });
|
|
}
|
|
res.end(); return true;
|
|
}
|
|
if (last?.type === 'toolResult') {
|
|
await write('text_delta', { text: 'CAD tool result received.' });
|
|
await write('completed', { finishReason: 'stop', output: [{ type: 'message', role: 'assistant', content: [{ type: 'text', text: 'CAD tool result received.' }] }], usage: null });
|
|
res.end(); return true;
|
|
}
|
|
if (text.includes('vm-test:truncated')) {
|
|
await write('text_delta', { text: 'A tool is being considered.' });
|
|
await write('tool_call', { callCode: 'call-truncated', name: 'execute', arguments: { command: 'solid.box', params: { length: 2, width: 3, height: 4 } } });
|
|
res.end(); return true;
|
|
}
|
|
const callCode = 'call_' + cadAgentCalls.length;
|
|
const tool = text.includes('vm-test:screenshot-camera')
|
|
? { name: 'screenshot', arguments: { camera: { target: [1, 1.5, 2], yaw: 1, pitch: 0.2, distance: 12, fov: 0.5, roll: 0.3 }, width: 320, height: 200, source: 'model', grid: false, axes: false } }
|
|
: text.includes('vm-test:screenshot')
|
|
? { name: 'screenshot', arguments: { view: text.includes('vm-test:screenshot-named') ? 'AgentSavedView'
|
|
: text.includes('vm-test:screenshot-isometric') ? 'Isometric' : 'iso', width: 320, height: 200, source: 'model', grid: false, axes: false } }
|
|
: text.includes('vm-test:read')
|
|
? { name: 'inspect_design', arguments: { measure: true, sketches: false, joints: false } }
|
|
: { name: 'execute', arguments: { command: 'solid.box', params: { length: 2, width: 3, height: 4, body_name: 'PlatformAIBox' }, expect: { bodies: 1, volume_mm3: 24 } } };
|
|
const assistantText = 'I will inspect or update the current CAD design.';
|
|
await write('text_delta', { text: assistantText });
|
|
await write('tool_call', { callCode, ...tool });
|
|
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
if (res.destroyed) return true;
|
|
await write('completed', { finishReason: 'tool_calls', output: [
|
|
{ type: 'message', role: 'assistant', content: [{ type: 'text', text: assistantText }] },
|
|
{ type: 'toolCall', callCode, ...tool },
|
|
], usage: null });
|
|
res.end(); return true;
|
|
}
|
|
if (!url.pathname.startsWith('/__assistant/')) return false;
|
|
if (!url.pathname.includes('/no-cors/')) {
|
|
res.setHeader('Access-Control-Allow-Origin', req.headers.origin || '*');
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
res.setHeader('Access-Control-Allow-Headers', 'content-type, authorization, x-api-key, anthropic-version, anthropic-dangerous-direct-browser-access');
|
|
}
|
|
if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return true; }
|
|
if (url.pathname === '/__assistant/requests') { send(res, calls); return true; }
|
|
if (url.pathname === '/__assistant/reset') { calls.length = 0; send(res, {}); return true; }
|
|
let body;
|
|
try { body = bytes.length ? JSON.parse(bytes.toString()) : {}; } catch { send(res, { error: 'bad json' }, 400); return true; }
|
|
calls.push({ path: url.pathname, authorization: req.headers.authorization || '', key: req.headers['x-api-key'] || '', cookie: req.headers.cookie || '', body });
|
|
if (url.pathname.endsWith('/models')) {
|
|
if (url.pathname.includes('/models-fail/')) send(res, { error: 'listing unavailable' }, 500);
|
|
else send(res, { data: [{ id: 'mock-tools', name: 'Deterministic CAD test model' }, { id: 'chat-only' }] });
|
|
return true;
|
|
}
|
|
if (body.model === 'unauthorized') { send(res, { error: 'invalid test key' }, 401); return true; }
|
|
if (body.model === 'rate-limit') { send(res, { error: 'rate limited' }, 429); return true; }
|
|
if (body.model === 'bad-json') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end('{not json'); return true; }
|
|
if (body.model === 'redirect') { res.writeHead(307, { Location: '/__assistant/redirect-target' }); res.end(); return true; }
|
|
const protocol = url.pathname.endsWith('/messages') ? 'anthropic' : 'openai';
|
|
const messages = body.messages || [], last = messages.at(-1);
|
|
const prompt = [...messages].reverse().find((item) => item.role === 'user' && textContent(item).includes('vm-test:'));
|
|
const text = textContent(prompt);
|
|
const reply = (toolCalls, content = 'Test model response') => {
|
|
const id = text.includes('vm-test:repeat-id') ? 'reused-provider-call' : 'mock-call-' + ++next;
|
|
if (protocol === 'anthropic') send(res, { id, type: 'message', role: 'assistant', model: body.model, content: toolCalls.length ? toolCalls.map(([name, input], index) => ({ type: 'tool_use', id: id + '-' + index, name, input })) : [{ type: 'text', text: content }], stop_reason: toolCalls.length ? 'tool_use' : 'end_turn' });
|
|
else send(res, { choices: [{ message: { role: 'assistant', content: toolCalls.length ? null : content, ...(toolCalls.length ? { tool_calls: toolCalls.map(([name, args], index) => ({ id: id + '-' + index, type: 'function', function: { name, arguments: text.includes('vm-test:invalid-arguments') ? '{invalid-json' : JSON.stringify(args) } })) } : {}) }, finish_reason: toolCalls.length ? 'tool_calls' : 'stop' }] });
|
|
};
|
|
if (body.model === 'chat-only') { reply([], 'Chat-only test response'); return true; }
|
|
const probe = body.tools?.some((tool) => (tool.function?.name || tool.name) === 'cad_connection_test');
|
|
if (probe) { reply([['cad_connection_test', { ok: true }]]); return true; }
|
|
const toolResult = last?.role === 'tool' || last?.content?.some?.((item) => item.type === 'tool_result');
|
|
if (toolResult) { reply([], 'CAD tool result received. Test turn complete.'); return true; }
|
|
if (text.includes('vm-test:slow')) await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
if (text.includes('vm-test:timeout')) await new Promise((resolve) => setTimeout(resolve, 32000));
|
|
if (res.destroyed) return true;
|
|
if (text.includes('vm-test:read') || text.includes('vm-test:repeat-id')) reply([['inspect_design', { measure: true, sketches: true }]]);
|
|
else if (text.includes('vm-test:tool-error') || text.includes('vm-test:invalid-arguments')) reply([['execute', { command: 'assistant.nonexistent_command', params: {} }]]);
|
|
else if (text.includes('vm-test:unknown-tool')) reply([['not_a_cad_tool', {}]]);
|
|
else if (text.includes('vm-test:forbidden')) reply([['execute', { command: 'file.new', params: {} }]]);
|
|
else if (text.includes('vm-test:batch')) reply([['batch', { commands: [
|
|
{ command: 'solid.box', params: { length: 2, width: 3, height: 4, body_name: 'BatchA' } },
|
|
{ command: 'solid.box', params: { length: 3, width: 4, height: 5, body_name: 'BatchB' } },
|
|
] }]]);
|
|
else reply([['execute', { command: 'solid.box', params: { length: 2, width: 3, height: 4, body_name: 'AIBox' }, expect: { bodies: 1, volume_mm3: 24 } }]]);
|
|
return true;
|
|
}
|