pr_01m47d15m3e54sn21z27rpy5n9/services/runner/src/model-env.ts
| 1 | import type { AgentModel } from "@g1t/contracts"; |
| 2 | |
| 3 | /** A model as configured: what people see, and what is sent to the provider. */ |
| 4 | export type ConfiguredModel = AgentModel & { model: string }; |
| 5 | |
| 6 | /** The settings that decide where model requests go. */ |
| 7 | export type ModelRouting = { |
| 8 | ANTHROPIC_API_KEY?: string; |
| 9 | /** A Cloudflare AI Gateway id; empty sends requests to the provider directly. */ |
| 10 | AI_GATEWAY_ID: string; |
| 11 | CLOUDFLARE_ACCOUNT_ID: string; |
| 12 | /** Needed only if the gateway requires authentication. */ |
| 13 | AI_GATEWAY_TOKEN?: string; |
| 14 | }; |
| 15 | |
| 16 | /** Where the sandbox sends model requests, and what it sends with them. */ |
| 17 | export function modelEnv(env: ModelRouting, model: ConfiguredModel): Record<string, string> { |
| 18 | const vars: Record<string, string> = { |
| 19 | ANTHROPIC_API_KEY: env.ANTHROPIC_API_KEY!, |
| 20 | ANTHROPIC_MODEL: model.model, |
| 21 | // Recorded at the top of the session, so anyone can see what ran. |
| 22 | AGENT_MODEL_NAME: `${model.modelName} (${model.label})`, |
| 23 | }; |
| 24 | if (env.AI_GATEWAY_ID) { |
| 25 | vars.ANTHROPIC_BASE_URL = `https://gateway.ai.cloudflare.com/v1/${env.CLOUDFLARE_ACCOUNT_ID}/${env.AI_GATEWAY_ID}/anthropic`; |
| 26 | if (env.AI_GATEWAY_TOKEN) { |
| 27 | vars.AI_GATEWAY_TOKEN = env.AI_GATEWAY_TOKEN; |
| 28 | vars.ANTHROPIC_CUSTOM_HEADERS = `cf-aig-authorization: Bearer ${env.AI_GATEWAY_TOKEN}`; |
| 29 | } |
| 30 | } |
| 31 | return vars; |
| 32 | } |