Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions agents/discord-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions agents/discord-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 2 additions & 14 deletions agents/discord-agent/worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<import("./src/index").MyAgent>;
DISCORD_GATEWAY: DurableObjectNamespace<
import("./src/index").DiscordGateway
Expand All @@ -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"
>
> {}
}