Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat(web): email notify + WS token routes

Gener561638df1e1c
diff --git a/src/web/src/app/api/email/notify/route.ts b/src/web/src/app/api/email/notify/route.ts
new file mode 100644
index 000000000..b474862fb
--- /dev/null
+++ b/src/web/src/app/api/email/notify/route.ts
@@ -0,0 +1,39 @@
+import { NextRequest } from "next/server"
+import { getCloudflareContext } from "@opennextjs/cloudflare"
+import { createDb, queries } from "@alook/shared"
+import { writeJSON, writeError } from "@/lib/middleware/helpers"
+import { TaskService } from "@/lib/services/task"
+
+export async function POST(req: NextRequest) {
+ const { env } = getCloudflareContext()
+ const db = createDb((env as Env).DB)
+
+ let body: { agentId: string; r2Key: string; from: string; subject: string; isWhitelisted: boolean; forwarded?: boolean }
+ try { body = await req.json() } catch { return writeError("invalid body", 400) }
+
+ await queries.email.createEmail(db, {
+ agentId: body.agentId,
+ fromEmail: body.from,
+ toEmail: "",
+ subject: body.subject,
+ r2Key: body.r2Key,
+ isWhitelisted: body.isWhitelisted,
+ forwarded: body.forwarded ?? false,
+ })
+
+ if (body.isWhitelisted) {
+ const agent = await queries.agent.getAgent(db, body.agentId)
+ if (agent && agent.runtimeId) {
+ const conv = await queries.conversation.createConversation(db, {
+ workspaceId: agent.workspaceId,
+ agentId: agent.id,
+ userId: agent.ownerId!,
+ title: `Email: ${body.subject}`.slice(0, 50),
+ })
+ const taskService = new TaskService(db)
+ await taskService.enqueueTask(agent.id, conv.id, agent.workspaceId, `New email from ${body.from}: ${body.subject}`)
+ }
+ }
+
+ return writeJSON({ ok: true })
+}
diff --git a/src/web/src/app/api/ws/token/route.ts b/src/web/src/app/api/ws/token/route.ts
new file mode 100644
index 000000000..28e81d25f
--- /dev/null
+++ b/src/web/src/app/api/ws/token/route.ts
@@ -0,0 +1,14 @@
+import { getCloudflareContext } from "@opennextjs/cloudflare"
+import { createAuth } from "@/lib/auth"
+
+export async function GET(request: Request) {
+ const { env } = getCloudflareContext()
+ const auth = createAuth(env as Env)
+ const session = await auth.api.getSession({ headers: request.headers })
+ if (!session) return new Response("Unauthorized", { status: 401 })
+
+ return Response.json({
+ userId: session.user.id,
+ token: session.session.token,
+ })
+}