diff --git a/agents/discord-agent/README.md b/agents/discord-agent/README.md index 4c54d44..df19537 100644 --- a/agents/discord-agent/README.md +++ b/agents/discord-agent/README.md @@ -77,13 +77,17 @@ Create `.dev.vars` for local development: ```bash DISCORD_BOT_TOKEN=your_discord_bot_token OPENROUTER_API_KEY=your_openrouter_key +AI_GATEWAY=you_gateway_name # optional ``` +If `OPENROUTER_GATEWAY_NAME` is set, the agent routes OpenRouter requests through the Cloudflare AI Gateway. Leave it unset to call your provider directly. + For production, set secrets: ```bash npx wrangler secret put DISCORD_BOT_TOKEN npx wrangler secret put OPENROUTER_API_KEY +npx wrangler secret put OPENROUTER_GATEWAY_NAME # optional ``` ### 4. Install & Deploy diff --git a/agents/discord-agent/src/index.ts b/agents/discord-agent/src/index.ts index 95ebcc5..c575aed 100644 --- a/agents/discord-agent/src/index.ts +++ b/agents/discord-agent/src/index.ts @@ -41,6 +41,21 @@ export class MyAgent extends DiscordAgent { ); } + private async createOpenRouterClient() { + const gatewayName = this.env.AI_GATEWAY?.trim(); + const openrouterOptions: { apiKey: string; baseUrl?: string } = { + apiKey: this.env.OPENROUTER_API_KEY, + }; + + if (gatewayName) { + openrouterOptions.baseUrl = await this.env.AI + .gateway(gatewayName) + .getUrl("openrouter"); + } + + return createOpenRouter(openrouterOptions); + } + private addMessage(message: { id: string; role: "user" | "assistant" | "tool"; @@ -133,9 +148,7 @@ export class MyAgent extends DiscordAgent { .filter((msg) => msg !== undefined); // Call LLM to summarize the conversation - const openrouter = createOpenRouter({ - apiKey: this.env.OPENROUTER_API_KEY, - }); + const openrouter = await this.createOpenRouterClient(); const { text: summary } = await generateText({ model: openrouter(MODEL_SUMMARY), @@ -284,9 +297,7 @@ export class MyAgent extends DiscordAgent { const messages = await this.getMessages(); // Create OpenRouter client - const openrouter = createOpenRouter({ - apiKey: this.env.OPENROUTER_API_KEY, - }); + const openrouter = await this.createOpenRouterClient(); // Merge local tools with MCP tools const mcpTools = this.mcp.getAITools(); diff --git a/agents/discord-agent/worker-configuration.d.ts b/agents/discord-agent/worker-configuration.d.ts index 83f121e..1ab80b5 100644 --- a/agents/discord-agent/worker-configuration.d.ts +++ b/agents/discord-agent/worker-configuration.d.ts @@ -6,15 +6,9 @@ declare namespace Cloudflare { durableNamespaces: "MyAgent" | "DiscordGateway"; } interface Env { - OPENAI_API_KEY: string; OPENROUTER_API_KEY: string; - CF_API_TOKEN: string; - DISCORD_APPLICATION_ID: string; - DISCORD_CLIENT_ID: string; - DISCORD_CLIENT_SECRET: string; - DISCORD_PUBLIC_KEY: string; DISCORD_BOT_TOKEN: string; - TAVILY_API_KEY: string; + AI_GATEWAY: string; AGENT: DurableObjectNamespace; DISCORD_GATEWAY: DurableObjectNamespace< import("./src/index").DiscordGateway @@ -33,15 +27,9 @@ declare namespace NodeJS { extends StringifyValues< Pick< Cloudflare.Env, - | "OPENAI_API_KEY" | "OPENROUTER_API_KEY" - | "CF_API_TOKEN" - | "DISCORD_APPLICATION_ID" - | "DISCORD_CLIENT_ID" - | "DISCORD_CLIENT_SECRET" - | "DISCORD_PUBLIC_KEY" | "DISCORD_BOT_TOKEN" - | "TAVILY_API_KEY" + | "AI_GATEWAY" > > {} }