From d32e71e8c973e541a9dc862984af4b8ba7b61c9b Mon Sep 17 00:00:00 2001 From: Aj Wazzan Date: Tue, 3 Jun 2025 21:01:55 -0700 Subject: [PATCH] Simple AI Router --- apps/server/src/routes/ai.ts | 56 ++++++++++++++++++++- apps/server/src/services/mcp-service/mcp.ts | 2 +- apps/server/wrangler.jsonc | 1 + 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/apps/server/src/routes/ai.ts b/apps/server/src/routes/ai.ts index 66e80ae597..2f1d5051de 100644 --- a/apps/server/src/routes/ai.ts +++ b/apps/server/src/routes/ai.ts @@ -1,6 +1,9 @@ +import { getDriverFromConnectionId, ZeroMCP } from '../services/mcp-service/mcp'; import { CallService } from '../services/call-service/call-service'; -import { ZeroMCP } from '../services/mcp-service/mcp'; +import { composeEmail } from '../trpc/routes/ai/compose'; import { env } from 'cloudflare:workers'; +import { tools } from './agent/tools'; +import { Tools } from '../types'; import twilio from 'twilio'; import { Hono } from 'hono'; @@ -8,6 +11,57 @@ export const aiRouter = new Hono(); aiRouter.get('/', (c) => c.text('Twilio + ElevenLabs + AI Phone System Ready')); +aiRouter.post('/do/:action', async (c) => { + const action = c.req.param('action') as Tools; + const body = await c.req.json(); + console.log('[DEBUG] action', action, body); + const connectionId = c.req.header('X-Connection-Id'); + if (!connectionId) { + return new Response('Unauthorized', { status: 401 }); + } + try { + const driver = await getDriverFromConnectionId(connectionId); + switch (action) { + case Tools.ListThreads: + const threads = await Promise.all( + (await driver.list({ folder: 'inbox', maxResults: 5 })).threads.map((thread) => + driver.get(thread.id).then((thread) => ({ + id: thread.latest?.id, + subject: thread.latest?.subject, + sender: thread.latest?.sender, + date: thread.latest?.receivedOn, + })), + ), + ); + return c.json({ success: true, result: threads }); + case Tools.ComposeEmail: + const newBody = await composeEmail({ + prompt: body.prompt, + emailSubject: body.emailSubject, + username: 'Nizar Abi Zaher', + connectionId, + }); + return c.json({ success: true, result: newBody }); + case Tools.SendEmail: + const result = await driver.create({ + to: body.to.map((to: any) => ({ + name: to.name ?? to.email, + email: to.email ?? 'founders@0.email', + })), + subject: body.subject, + message: body.message, + attachments: [], + headers: {}, + }); + return c.json({ success: true, result }); + default: + return c.json({ success: false, error: 'Not implemented' }, 400); + } + } catch (error: any) { + return c.json({ success: false, error: error.message }, 400); + } +}); + aiRouter.mount( '/mcp', async (request, env, ctx) => { diff --git a/apps/server/src/services/mcp-service/mcp.ts b/apps/server/src/services/mcp-service/mcp.ts index 1b91141681..1ecd2bfb1b 100644 --- a/apps/server/src/services/mcp-service/mcp.ts +++ b/apps/server/src/services/mcp-service/mcp.ts @@ -9,7 +9,7 @@ import { createDb } from '../../db'; import { generateText } from 'ai'; import { z } from 'zod'; -const getDriverFromConnectionId = async (connectionId: string) => { +export const getDriverFromConnectionId = async (connectionId: string) => { const db = createDb(env.HYPERDRIVE.connectionString); const activeConnection = await db.query.connection.findFirst({ where: (connection, ops) => ops.eq(connection.id, connectionId), diff --git a/apps/server/wrangler.jsonc b/apps/server/wrangler.jsonc index a48f88d018..adfb02780a 100644 --- a/apps/server/wrangler.jsonc +++ b/apps/server/wrangler.jsonc @@ -67,6 +67,7 @@ "VITE_PUBLIC_BACKEND_URL": "http://localhost:8787", "VITE_PUBLIC_APP_URL": "http://localhost:3000", "JWT_SECRET": "secret", + "ELEVENLABS_API_KEY": "1234567890", }, "kv_namespaces": [ {