aethex-forge/api/ai/title.ts
sirpiglr 834c4bd56e Add AI chat assistant and backend API for AI interactions
Introduces new API endpoints for AI chat and title generation, integrates an AI chat component into the layout, and updates client-side services to communicate with the new backend AI endpoints.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 9203795e-937a-4306-b81d-b4d5c78c240e
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 64961019-b4a5-48d8-97fc-c4980d29f3c4
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7c94b7a0-29c7-4f2e-94ef-44b2153872b7/9203795e-937a-4306-b81d-b4d5c78c240e/fhRML7y
Replit-Helium-Checkpoint-Created: true
2025-12-06 03:58:12 +00:00

39 lines
1.3 KiB
TypeScript

import type { Request, Response } from "express";
import { GoogleGenAI } from "@google/genai";
const GEMINI_API_KEY = process.env.AI_INTEGRATIONS_GEMINI_API_KEY || process.env.GEMINI_API_KEY || "";
const ai = GEMINI_API_KEY ? new GoogleGenAI({ apiKey: GEMINI_API_KEY }) : null;
export default async function handler(req: Request, res: Response) {
if (req.method !== "POST") {
return res.status(405).json({ error: "Method not allowed" });
}
if (!ai) {
return res.status(503).json({
error: "AI service not configured",
message: "Please ensure the Gemini API key is set up."
});
}
try {
const { message } = req.body as { message: string };
if (!message) {
return res.status(400).json({ error: "Message is required" });
}
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: `Generate a short, concise, and descriptive title (max 5 words) for a chat conversation that starts with this message: "${message}". Do not use quotes.`,
});
const title = response.text?.trim() || message.slice(0, 30);
return res.json({ title });
} catch (error) {
console.error("[AI] Title generation error:", error);
const fallbackTitle = (req.body?.message || "").slice(0, 30) + "...";
return res.json({ title: fallbackTitle });
}
}