Skip to content

Instantly share code, notes, and snippets.

@vladinator1000
Last active February 22, 2026 18:57
Show Gist options
  • Select an option

  • Save vladinator1000/08319aec76dfcdc1c501bf926e7c2376 to your computer and use it in GitHub Desktop.

Select an option

Save vladinator1000/08319aec76dfcdc1c501bf926e7c2376 to your computer and use it in GitHub Desktop.
We have agents at home
import { serve } from "bun";
import index from "./index.html";
import { OpenRouter } from "@openrouter/sdk";
import { parseCode } from "./lib/parse-code";
import { publicFunctions } from "./lib/public-interface-impl";
const client = new OpenRouter({
apiKey: process.env.OPENROUTER_API_KEY,
});
let publicInterfaceDefFile = Bun.file("src/lib/public-interface.ts");
let publicInterfaceContent = await publicInterfaceDefFile.text();
let systemPrompt = `
You can create and execute js programs wrapped in <code></code> tags, you have this API to work with: ${publicInterfaceContent}
The stuff in this interface is directly available in publicFunctions.call scope for you to call as long as you've wrapped it in <code></code> tags
`;
const server = serve({
routes: {
// Serve index.html for all unmatched routes.
"/*": index,
"/api/message": {
async GET(req) {
const stream = await client.chat.send({
chatGenerationParams: {
model: "minimax/minimax-m2.5",
messages: [
{
role: "system",
content: systemPrompt,
},
{
role: "user",
content: "print invoices of more than £70",
},
],
stream: true,
},
});
let finalContent = "";
let reasoningTrace = "";
for await (const chunk of stream) {
const reasoning = chunk.choices[0]?.delta?.reasoning;
if (reasoning) {
reasoningTrace += reasoning;
}
const content = chunk.choices[0]?.delta?.content;
if (content) {
finalContent += content;
}
}
let code = parseCode(finalContent);
if (code) {
const fnNames = Object.keys(publicFunctions);
const fn = new Function(...fnNames, code);
let result = fn(...Object.values(publicFunctions));
}
return Response.json({
reasoningTrace,
content: finalContent,
code,
method: "POST",
});
},
},
},
development: process.env.NODE_ENV !== "production" && {
// Enable browser hot reloading in development
hmr: true,
// Echo console logs from the browser to the server
console: true,
},
});
import { type LmFunctions } from "./public-interface";
export let publicFunctions: LmFunctions = {
printReport: () => {
console.log("Report OK.");
},
printHello: () => {
console.log("Hello, world!");
},
printInvoice(filter) {
for (const invoice of invoices) {
if (filter) {
if (filter.amountGreater && invoice.amount > filter.amountGreater) {
console.log(invoice);
} else if (
filter.amountLesser &&
invoice.amount < filter.amountLesser
) {
console.log(invoice);
}
} else {
console.log(invoice);
}
}
},
};
let invoices = [
{
id: 1,
amount: 10,
client: "Acme Corp",
},
{
id: 2,
amount: 25,
client: "Beta Industries",
},
{
id: 3,
amount: 50,
client: "Gamma LLC",
},
{
id: 4,
amount: 75,
client: "Delta Systems",
},
{
id: 5,
amount: 100,
client: "Epsilon Technologies",
},
];
export interface LmFunctions {
printReport(): void;
printHello(): void;
printInvoice(filter?: { amountGreater?: number; amountLesser?: number }): any;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment