You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
|
|
import { streamText } from "ai";
|
|
import { Hono } from "hono";
|
|
import { stream } from "hono/streaming";
|
|
import { personalAssistantAgent } from "./agents/assistant.js";
|
|
import { chefAgent } from "./agents/chef.js";
|
|
import { Agent } from "./types.js";
|
|
|
|
// This declaration is primarily for providing type hints in your code
|
|
// and it doesn't directly define the *values* of the environment variables.
|
|
|
|
interface Env {
|
|
OPENROUTER_API_KEY: string;
|
|
// Add other environment variables here
|
|
}
|
|
|
|
declare global {
|
|
const env: Env;
|
|
}
|
|
|
|
const app = new Hono();
|
|
|
|
const openrouter = createOpenRouter({
|
|
apiKey: import.meta.env.VITE_OPENROUTER_API_KEY || env.OPENROUTER_API_KEY,
|
|
});
|
|
|
|
const agentsByName: Record<string, Agent> = {
|
|
assistant: personalAssistantAgent,
|
|
chef: chefAgent,
|
|
};
|
|
|
|
app.post("/api/chat/:agent_name", async (c) => {
|
|
const input = await c.req.json();
|
|
const agentName = c.req.param("agent_name");
|
|
const agent = agentsByName[agentName];
|
|
if (!agent) {
|
|
return c.json({ error: `No such agent: ${agentName}` });
|
|
}
|
|
console.log(input);
|
|
const result = streamText({
|
|
model: openrouter(agent.modelName),
|
|
messages: [
|
|
{ role: "system", content: agent.systemMessage },
|
|
...input.messages,
|
|
],
|
|
tools: agent.tools,
|
|
});
|
|
|
|
// Mark the response as a v1 data stream:
|
|
c.header("X-Vercel-AI-Data-Stream", "v1");
|
|
c.header("Content-Type", "text/plain; charset=utf-8");
|
|
|
|
return stream(c, (stream) => stream.pipe(result.toDataStream()));
|
|
});
|
|
|
|
export default app;
|