Files
vibemake/scripts/cad-agent-proxy.mjs
T
2026-11-24 20:09:21 +08:00

35 lines
2.0 KiB
JavaScript

// The agent runtime has a separate upstream. Never let v2 requests fall through to Java.
export function isCadAgentPath(value) {
const path = String(value).split('?')[0];
return path === '/api/cad-agent/v2' || path.startsWith('/api/cad-agent/v2/');
}
export function cadAgentTarget(value) {
if (!value) return null;
let target;
try { target = new URL(value); } catch { throw new Error('CAD_AGENT_TARGET must be an origin URL'); }
const local = ['localhost', '127.0.0.1', '[::1]'].includes(target.hostname);
if ((target.protocol !== 'https:' && !(target.protocol === 'http:' && local))
|| target.username || target.password || target.search || target.hash || target.pathname !== '/') {
throw new Error('CAD_AGENT_TARGET must use HTTPS or loopback HTTP, with no path or credentials');
}
return target;
}
export function cadAgentBuildConfiguration(environment = process.env) {
const runtime = environment.VIBEMAKE_CAD_AGENT_RUNTIME || 'java-v1';
const apiBase = environment.VIBEMAKE_CAD_AGENT_API_BASE || '/api/cad-agent/v2';
if (!['java-v1', 'deepagents'].includes(runtime)) throw new Error('VIBEMAKE_CAD_AGENT_RUNTIME must be java-v1 or deepagents');
let url;
try { url = new URL(apiBase, 'https://cad-build.invalid'); } catch { throw new Error('Invalid VIBEMAKE_CAD_AGENT_API_BASE'); }
const local = ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname);
if (apiBase.startsWith('//') || (url.protocol !== 'https:' && !(url.protocol === 'http:' && local))
|| url.username || url.password || url.search || url.hash || !url.pathname.endsWith('/api/cad-agent/v2')) {
throw new Error('Invalid VIBEMAKE_CAD_AGENT_API_BASE');
}
const escaped = apiBase.replace(/[&<>"']/g, value => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[value]);
return { runtime, apiBase, html: runtime === 'deepagents'
? `<meta name="cad-agent-runtime" content="deepagents"><meta name="cad-agent-api-base" content="${escaped}">`
: '' };
}