Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.
Rust ai-agentsai-gatewayclaude-codecodexcoding-agentcost-optimizationdeepseekllm-gatewayllm-routerlocal-firstmodel-routingrusttask-routingtauri
export class CdpClient {
#nextId = 1;
#pending = new Map();
#socket;
constructor(url) {
this.#socket = new WebSocket(url);
this.#socket.addEventListener('message', event => {
const message = JSON.parse(String(event.data));
if (!message.id) return;
const pending = this.#pending.get(message.id);
if (!pending) return;
this.#pending.delete(message.id);
if (message.error) pending.reject(new Error(message.error.message));
else pending.resolve(message.result ?? {});
});
}
async ready() {
if (this.#socket.readyState === WebSocket.OPEN) return;
await new Promise((resolve, reject) => {
this.#socket.addEventListener('open', resolve, { once: true });
this.#socket.addEventListener('error', reject, { once: true });
});
}
send(method, params = {}) {
const id = this.#nextId++;
return new Promise((resolve, reject) => {
this.#pending.set(id, { resolve, reject });
this.#socket.send(JSON.stringify({ id, method, params }));
});
}
close() {
this.#socket.close();
}
}
export async function connectPage(port) {
const targets = await fetch(`http://127.0.0.1:${port}/json/list`).then(response => response.json());
const target = targets.find(item => item.type === 'page' && /^https?:/.test(item.url))
?? targets.find(item => item.type === 'page');
if (!target) throw new Error(`No inspectable page target on CDP port ${port}`);
const client = new CdpClient(target.webSocketDebuggerUrl);
await client.ready();
return client;
}
export async function evaluate(client, expression) {
const result = await client.send('Runtime.evaluate', {
expression,
awaitPromise: true,
returnByValue: true,
});
if (result.exceptionDetails) throw new Error(result.exceptionDetails.text);
return result.result?.value;
}
export async function navigate(client, url, { width = 1200, height = 781 } = {}) {
await client.send('Page.enable');
await client.send('Runtime.enable');
await client.send('Emulation.setDeviceMetricsOverride', {
width,
height,
deviceScaleFactor: 1,
mobile: false,
});
await client.send('Page.navigate', { url });
await waitFor(client, `document.readyState === 'complete'`);
await evaluate(client, 'document.fonts.ready');
}
export async function waitFor(client, expression, { timeout = 5000, interval = 25 } = {}) {
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
if (await evaluate(client, expression)) return;
await new Promise(resolve => setTimeout(resolve, interval));
}
throw new Error(`Timed out waiting for: ${expression}`);
}