Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 170 lines 6.4 KB
Raw
1 /**
2 * The only way a journey container reaches a model during a live run.
3 *
4 * The agent inside the container gets a one-off token, never the API key: its
5 * shell commands inherit its environment, so a key there could end up in a
6 * transcript. This proxy checks the token, checks the model, swaps the token
7 * for the key, and streams the answer back unchanged.
8 *
9 * It listens on every interface, because that is how a container reaches the
10 * host, so the token is what keeps others on the network from using the key.
11 */
12
13 import { randomBytes, timingSafeEqual } from 'node:crypto';
14 import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
15 import { z } from 'zod';
16
17 /** The models a live run may call. Anything else is refused before it costs anything. */
18 export const ALLOWED_MODELS = ['claude-haiku-5-5', 'claude-opus-5-5'] as const;
19 export type AllowedModel = (typeof ALLOWED_MODELS)[number];
20
21 const isAllowedModel = (model: string | undefined): model is AllowedModel =>
22 ALLOWED_MODELS.some(allowed => allowed === model);
23
24 /** Request headers that describe the hop, or carry a credential, and are never forwarded. */
25 const DROPPED_REQUEST_HEADERS = new Set([
26 'host',
27 'connection',
28 'content-length',
29 'x-api-key',
30 'authorization',
31 'accept-encoding',
32 ]);
33
34 /**
35 * The requests an agent needs, by the path it sends, and the fixed upstream URL each goes to.
36 * Anything else (batches, files, admin) is refused, because a request there may name no model,
37 * or one the proxy cannot see. Nothing the agent sends becomes part of an upstream URL.
38 */
39 const FORWARDED: ReadonlyMap<string, string> = new Map(
40 ['/v1/messages', '/v1/messages/count_tokens'].flatMap(path =>
41 ['', '?beta=true'].map(query => [`${path}${query}`, `https://api.anthropic.com${path}${query}`])
42 )
43 );
44
45 const ModelField = z.looseObject({ model: z.string().optional() });
46
47 const parseJson = (body: Buffer): unknown => {
48 try {
49 return JSON.parse(body.toString('utf8'));
50 } catch {
51 return undefined;
52 }
53 };
54
55 export interface ModelProxy {
56 /** Base URL for `ANTHROPIC_BASE_URL` inside a container. */
57 readonly dockerUrl: string;
58 /** What the agent sends as its API key. Valid only for this proxy's lifetime. */
59 readonly token: string;
60 /** Requests forwarded so far, per model. */
61 readonly forwarded: () => Readonly<Record<string, number>>;
62 /** Requests refused so far, with the reason. */
63 readonly refused: () => ReadonlyArray<string>;
64 close(): Promise<void>;
65 }
66
67 const readBody = async (request: IncomingMessage): Promise<Buffer> => {
68 const chunks: Buffer[] = [];
69 for await (const chunk of request) chunks.push(Buffer.from(chunk));
70 return Buffer.concat(chunks);
71 };
72
73 const refuse = (response: ServerResponse, status: number, message: string) => {
74 response.writeHead(status, { 'content-type': 'application/json' });
75 response.end(JSON.stringify({ type: 'error', error: { type: 'proxy_refused', message } }));
76 };
77
78 export const startModelProxy = async (options: { apiKey: string }): Promise<ModelProxy> => {
79 const token = `sk-journey-${randomBytes(24).toString('hex')}`;
80 const expected = Buffer.from(token);
81 const forwarded: Record<string, number> = {};
82 const refused: string[] = [];
83
84 const tokenMatches = (request: IncomingMessage): boolean => {
85 const header = request.headers['x-api-key'] ?? request.headers.authorization ?? '';
86 const sent = Buffer.from(String(header).replace(/^Bearer\s+/i, ''));
87 return sent.length === expected.length && timingSafeEqual(sent, expected);
88 };
89
90 const server = createServer((request, response) => {
91 void (async () => {
92 const path = request.url ?? '/';
93 if (!tokenMatches(request)) {
94 refused.push(`bad token for ${path}`);
95 return refuse(response, 401, 'unknown token');
96 }
97 const target = request.method === 'POST' ? FORWARDED.get(path) : undefined;
98 if (target === undefined) {
99 refused.push(`${request.method} ${path}`);
100 return refuse(
101 response,
102 404,
103 `only POST ${[...FORWARDED.keys()].join(', ')} is forwarded, not ${request.method} ${path}`
104 );
105 }
106 // Every forwarded request names its model at the top level, so no other model is reachable.
107 const body = await readBody(request);
108 const parsed = ModelField.safeParse(parseJson(body));
109 const model = parsed.success ? parsed.data.model : undefined;
110 if (!isAllowedModel(model)) {
111 refused.push(`model ${model ?? '(none)'}`);
112 return refuse(response, 400, `model ${model ?? '(none)'} is not allowed in journey runs`);
113 }
114 forwarded[model] = (forwarded[model] ?? 0) + 1;
115
116 const headers = new Headers();
117 for (const [name, value] of Object.entries(request.headers)) {
118 if (value === undefined || DROPPED_REQUEST_HEADERS.has(name)) continue;
119 headers.set(name, Array.isArray(value) ? value.join(', ') : value);
120 }
121 headers.set('x-api-key', options.apiKey);
122
123 const upstream = await fetch(target, {
124 method: request.method,
125 headers,
126 body: body.length > 0 ? new Uint8Array(body) : undefined,
127 });
128 const responseHeaders: Record<string, string> = {};
129 upstream.headers.forEach((value, name) => {
130 // fetch already decoded the body, so its length and encoding no longer hold.
131 if (name !== 'content-encoding' && name !== 'content-length') responseHeaders[name] = value;
132 });
133 response.writeHead(upstream.status, responseHeaders);
134 if (upstream.body) {
135 for await (const chunk of upstream.body) response.write(chunk);
136 }
137 response.end();
138 })().catch(error => {
139 if (!response.headersSent) {
140 refuse(response, 502, error instanceof Error ? error.message : String(error));
141 } else {
142 response.destroy();
143 }
144 });
145 });
146
147 const port = await new Promise<number>((resolve, reject) => {
148 server.once('error', reject);
149 server.listen({ host: '0.0.0.0', port: 0 }, () => {
150 const address = server.address();
151 if (address === null || typeof address === 'string') {
152 reject(new Error('the model proxy has no port'));
153 return;
154 }
155 resolve(address.port);
156 });
157 });
158
159 return {
160 dockerUrl: `http://host.docker.internal:${port}`,
161 token,
162 forwarded: () => ({ ...forwarded }),
163 refused: () => [...refused],
164 close: () =>
165 new Promise<void>(resolve => {
166 server.closeAllConnections();
167 server.close(() => resolve());
168 }),
169 };
170 };