|
| 1 | +import { db } from "@/lib/db"; |
| 2 | +import { mainConfig } from "@/lib/config"; |
| 3 | +import { eventsTable, type InsertEvent } from "@/lib/schema"; |
| 4 | +import { createLumaClient } from "@/lib/luma"; |
| 5 | +import { and, eq } from "drizzle-orm"; |
| 6 | +import { createGateway, generateText, tool } from "ai"; |
| 7 | +import { z } from "zod"; |
| 8 | + |
| 9 | +const DISCORD_AGENT_MODEL = "openai/gpt-5.3-medium"; |
| 10 | + |
| 11 | +const updateEventInputSchema = z.object({ |
| 12 | + slug: z.string().min(1), |
| 13 | + eventData: z |
| 14 | + .object({ |
| 15 | + name: z.string().optional(), |
| 16 | + slug: z.string().optional(), |
| 17 | + attendeeLimit: z.number().int().positive().optional(), |
| 18 | + tagline: z.string().optional(), |
| 19 | + startDate: z.string().optional(), |
| 20 | + endDate: z.string().optional(), |
| 21 | + lumaEventId: z.string().nullable().optional(), |
| 22 | + isDraft: z.boolean().optional(), |
| 23 | + isHackathon: z.boolean().optional(), |
| 24 | + highlightOnLandingPage: z.boolean().optional(), |
| 25 | + fullAddress: z.string().nullable().optional(), |
| 26 | + shortLocation: z.string().nullable().optional(), |
| 27 | + streetAddress: z.string().nullable().optional(), |
| 28 | + recordingUrl: z.string().nullable().optional(), |
| 29 | + }) |
| 30 | + .refine((data) => Object.keys(data).length > 0, { |
| 31 | + message: "eventData must include at least one field", |
| 32 | + }), |
| 33 | +}); |
| 34 | + |
| 35 | +function getGatewayModel() { |
| 36 | + const gatewayApiKey = mainConfig.ai.gatewayApiKey; |
| 37 | + const oidcToken = mainConfig.ai.vercelOidcToken; |
| 38 | + |
| 39 | + if (!gatewayApiKey && !oidcToken) { |
| 40 | + throw new Error( |
| 41 | + "AI gateway auth missing. Set AI_GATEWAY_API_KEY or VERCEL_OIDC_TOKEN.", |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + if (gatewayApiKey) { |
| 46 | + return createGateway({ apiKey: gatewayApiKey })(DISCORD_AGENT_MODEL); |
| 47 | + } |
| 48 | + |
| 49 | + return createGateway({ |
| 50 | + headers: { |
| 51 | + Authorization: `Bearer ${oidcToken}`, |
| 52 | + }, |
| 53 | + })(DISCORD_AGENT_MODEL); |
| 54 | +} |
| 55 | + |
| 56 | +function toDate(value: string): Date { |
| 57 | + const date = new Date(value); |
| 58 | + if (Number.isNaN(date.getTime())) { |
| 59 | + throw new Error(`Invalid date value: ${value}`); |
| 60 | + } |
| 61 | + return date; |
| 62 | +} |
| 63 | + |
| 64 | +function normalizeUpdateData( |
| 65 | + eventData: z.infer<typeof updateEventInputSchema>["eventData"], |
| 66 | +): Partial<InsertEvent> { |
| 67 | + const normalized: Partial<InsertEvent> = {}; |
| 68 | + |
| 69 | + if (typeof eventData.name === "string") normalized.name = eventData.name; |
| 70 | + if (typeof eventData.slug === "string") normalized.slug = eventData.slug; |
| 71 | + if (typeof eventData.attendeeLimit === "number") { |
| 72 | + normalized.attendeeLimit = eventData.attendeeLimit; |
| 73 | + } |
| 74 | + if (typeof eventData.tagline === "string") normalized.tagline = eventData.tagline; |
| 75 | + if (typeof eventData.startDate === "string") { |
| 76 | + normalized.startDate = toDate(eventData.startDate); |
| 77 | + } |
| 78 | + if (typeof eventData.endDate === "string") { |
| 79 | + normalized.endDate = toDate(eventData.endDate); |
| 80 | + } |
| 81 | + if ("lumaEventId" in eventData) normalized.lumaEventId = eventData.lumaEventId; |
| 82 | + if (typeof eventData.isDraft === "boolean") normalized.isDraft = eventData.isDraft; |
| 83 | + if (typeof eventData.isHackathon === "boolean") { |
| 84 | + normalized.isHackathon = eventData.isHackathon; |
| 85 | + } |
| 86 | + if (typeof eventData.highlightOnLandingPage === "boolean") { |
| 87 | + normalized.highlightOnLandingPage = eventData.highlightOnLandingPage; |
| 88 | + } |
| 89 | + if ("fullAddress" in eventData) normalized.fullAddress = eventData.fullAddress; |
| 90 | + if ("shortLocation" in eventData) normalized.shortLocation = eventData.shortLocation; |
| 91 | + if ("streetAddress" in eventData) normalized.streetAddress = eventData.streetAddress; |
| 92 | + if ("recordingUrl" in eventData) normalized.recordingUrl = eventData.recordingUrl; |
| 93 | + |
| 94 | + return normalized; |
| 95 | +} |
| 96 | + |
| 97 | +async function getEventBySlug(slug: string) { |
| 98 | + const [event] = await db |
| 99 | + .select() |
| 100 | + .from(eventsTable) |
| 101 | + .where(eq(eventsTable.slug, slug)) |
| 102 | + .limit(1); |
| 103 | + |
| 104 | + return event ?? null; |
| 105 | +} |
| 106 | + |
| 107 | +async function updateEventBySlug(input: z.infer<typeof updateEventInputSchema>) { |
| 108 | + const existingEvent = await getEventBySlug(input.slug); |
| 109 | + if (!existingEvent) { |
| 110 | + throw new Error(`Event not found for slug: ${input.slug}`); |
| 111 | + } |
| 112 | + |
| 113 | + const normalized = normalizeUpdateData(input.eventData); |
| 114 | + const [updated] = await db |
| 115 | + .update(eventsTable) |
| 116 | + .set(normalized) |
| 117 | + .where(and(eq(eventsTable.id, existingEvent.id))) |
| 118 | + .returning(); |
| 119 | + |
| 120 | + return updated ?? null; |
| 121 | +} |
| 122 | + |
| 123 | +function buildTools() { |
| 124 | + return { |
| 125 | + get_event_by_slug: tool({ |
| 126 | + description: "Fetch an AllThingsWeb event by slug.", |
| 127 | + inputSchema: z.object({ |
| 128 | + slug: z.string().min(1), |
| 129 | + }), |
| 130 | + execute: async ({ slug }) => getEventBySlug(slug), |
| 131 | + }), |
| 132 | + update_event: tool({ |
| 133 | + description: |
| 134 | + "Update an AllThingsWeb event by slug. Use this only when the user explicitly asks to change event data.", |
| 135 | + inputSchema: updateEventInputSchema, |
| 136 | + execute: async ({ slug, eventData }) => |
| 137 | + updateEventBySlug({ |
| 138 | + slug, |
| 139 | + eventData, |
| 140 | + }), |
| 141 | + }), |
| 142 | + get_luma_event: tool({ |
| 143 | + description: "Fetch a Luma event payload by Luma event ID (api_id).", |
| 144 | + inputSchema: z.object({ |
| 145 | + eventId: z.string().min(1), |
| 146 | + }), |
| 147 | + execute: async ({ eventId }) => createLumaClient().getEvent(eventId), |
| 148 | + }), |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +export async function runDiscordPromptAgent(input: { |
| 153 | + prompt: string; |
| 154 | +}): Promise<string> { |
| 155 | + const { text } = await generateText({ |
| 156 | + model: getGatewayModel(), |
| 157 | + tools: buildTools(), |
| 158 | + system: `You are the AllThingsWeb Discord ops assistant. |
| 159 | +You can inspect and update AllThingsWeb event data and fetch Luma event payloads. |
| 160 | +Use tools whenever the answer depends on current data. |
| 161 | +Before updating event data, confirm intent from the user message and summarize what changed. |
| 162 | +Be concise and action-oriented.`, |
| 163 | + prompt: input.prompt, |
| 164 | + }); |
| 165 | + |
| 166 | + return text.trim(); |
| 167 | +} |
0 commit comments