Spaces:
Running
Running
File size: 20,446 Bytes
27127dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
import type { Express, Request, Response, NextFunction } from "express";
import { createServer, type Server } from "http";
import { storage } from "./storage";
import { setupAuth } from "./auth";
import { generateChatResponse } from "./openai";
import { canUseOpenAI, canUseQwen } from "./fallbackChat";
import { getPersonalityConfig } from "./personalities";
import { generateImage, imageGenerationSchema, isFluxAvailable } from "./flux";
import { generateVideo, videoGenerationSchema, isVideoGenerationAvailable } from "./video";
import OpenAI from "openai";
import { nanoid } from "nanoid";
import {
messageSchema,
conversationSchema,
insertMessageSchema,
insertConversationSchema,
messageRoleSchema,
personalityTypeSchema
} from "@shared/schema";
import { z } from "zod";
// Track the current model in use
let currentModelStatus = {
model: 'openai',
isOpenAIAvailable: true,
isQwenAvailable: true,
lastChecked: new Date()
};
// Function to check and update model availability status
async function updateModelStatus() {
try {
const isOpenAIAvailable = await canUseOpenAI();
const isQwenAvailable = await canUseQwen();
// Determine current model based on availability
let model = 'unavailable';
if (isOpenAIAvailable) {
model = 'openai';
} else if (isQwenAvailable) {
model = 'qwen';
}
currentModelStatus = {
model,
isOpenAIAvailable,
isQwenAvailable,
lastChecked: new Date()
};
console.log(`Updated model status: ${model} (OpenAI: ${isOpenAIAvailable}, Qwen: ${isQwenAvailable})`);
return currentModelStatus;
} catch (error) {
console.error("Error updating model status:", error);
return currentModelStatus;
}
}
// Initialize model status
updateModelStatus();
export async function registerRoutes(app: Express): Promise<Server> {
// Set up authentication
setupAuth(app);
// Get all conversations (filtered by user if authenticated)
app.get("/api/conversations", async (req: Request, res: Response) => {
try {
let conversations;
// If user is authenticated, only get their conversations
if (req.isAuthenticated() && req.user) {
const userId = req.user.id;
conversations = await storage.getUserConversations(userId);
} else {
// For unauthenticated users, get only conversations without a userId
conversations = await storage.getConversations();
// Filter out conversations that belong to users
conversations = conversations.filter(conv => !conv.userId);
}
res.json(conversations);
} catch (error) {
console.error("Error fetching conversations:", error);
res.status(500).json({ message: "Failed to fetch conversations." });
}
});
// Create a new conversation
app.post("/api/conversations", async (req: Request, res: Response) => {
try {
const conversationId = nanoid();
// Generate title based on user's message
let title = req.body.title;
if (!title || title === "New Conversation") {
try {
const openaiClient = new OpenAI();
const response = await openaiClient.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: "You are a title generator. Create a concise, descriptive title (2-4 words) that summarizes the following message. Respond with just the title."
},
{
role: "user",
content: req.body.firstMessage || "New chat conversation"
}
],
max_tokens: 15,
temperature: 0.6
});
title = response.choices[0].message.content?.trim() || "New Conversation";
} catch (err) {
console.error("Error generating AI title:", err);
title = "New Conversation";
}
}
// Include user ID if authenticated
const conversationData: any = {
id: conversationId,
title: title,
personality: req.body.personality || "general"
};
// Associate conversation with user if authenticated
if (req.isAuthenticated() && req.user) {
conversationData.userId = req.user.id;
}
const result = insertConversationSchema.safeParse(conversationData);
if (!result.success) {
return res.status(400).json({ message: "Invalid conversation data." });
}
const conversation = await storage.createConversation(result.data);
res.status(201).json(conversation);
} catch (error) {
console.error("Error creating conversation:", error);
res.status(500).json({ message: "Failed to create conversation." });
}
});
// Generate AI title for conversation
app.post("/api/conversations/:id/generate-title", async (req: Request, res: Response) => {
try {
const { id } = req.params;
// Get the conversation messages
const messages = await storage.getMessages(id);
if (messages.length < 2) {
return res.status(400).json({ message: "Need at least one exchange to generate a title" });
}
// Extract the first few messages (user and assistant) to use as context
const contextMessages = messages.slice(0, Math.min(4, messages.length))
.map(msg => `${msg.role}: ${msg.content}`).join("\n");
// Generate the title using AI
let title;
try {
// First try using OpenAI
const openaiClient = new OpenAI();
const response = await openaiClient.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: "You are a helpful assistant that generates short, descriptive titles (max 6 words) for conversations based on their content. Respond with just the title."
},
{
role: "user",
content: `Generate a short, descriptive title (maximum 6 words) for this conversation:\n${contextMessages}`
}
],
max_tokens: 20,
temperature: 0.7
});
title = response.choices[0].message.content?.trim();
// Use fallback if title is undefined or empty
if (!title) {
title = `Chat ${new Date().toLocaleDateString()}`;
}
} catch (err) {
// Fallback to a generic title
console.error("Error generating AI title:", err);
title = `Chat ${new Date().toLocaleDateString()}`;
}
// Update the conversation with the new title
const updatedConversation = await storage.updateConversationTitle(id, title as string);
if (!updatedConversation) {
return res.status(404).json({ message: "Conversation not found" });
}
res.json(updatedConversation);
} catch (error) {
console.error("Error generating title:", error);
res.status(500).json({ message: "Failed to generate title." });
}
});
// Get messages for a conversation
app.get("/api/conversations/:id/messages", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const conversation = await storage.getConversation(id);
if (!conversation) {
return res.status(404).json({ message: "Conversation not found." });
}
// Check ownership if the conversation belongs to a user
if (conversation.userId && req.isAuthenticated() && req.user) {
// User must be the owner of the conversation
if (conversation.userId !== req.user.id) {
return res.status(403).json({ message: "You don't have permission to access this conversation." });
}
}
const messages = await storage.getMessages(id);
// If user is authenticated, include their system context
if (req.isAuthenticated() && req.user) {
const userContext = {
role: "system",
content: req.user.systemContext || `Chat with ${req.user.username}`,
conversationId: id,
createdAt: new Date()
};
messages.unshift(userContext);
}
res.json(messages);
} catch (error) {
console.error("Error fetching messages:", error);
res.status(500).json({ message: "Failed to fetch messages." });
}
});
// Send a message and get AI response
app.post("/api/chat", async (req: Request, res: Response) => {
try {
// Update model status before processing
await updateModelStatus();
// Check if any AI model is available
if (currentModelStatus.model === 'unavailable') {
return res.status(503).json({
message: "All AI models are currently unavailable. Please check your API keys."
});
}
// Validate incoming data
const result = conversationSchema.safeParse(req.body);
if (!result.success) {
return res.status(400).json({ message: "Invalid chat data format." });
}
const { messages } = result.data;
const conversationId = req.body.conversationId || "default";
// Ensure the conversation exists
const conversation = await storage.getConversation(conversationId);
if (!conversation && conversationId !== "default") {
return res.status(404).json({ message: "Conversation not found." });
}
// If conversation belongs to a user, check permissions
if (conversation && conversation.userId) {
// If user is not authenticated or not the owner
if (!req.isAuthenticated() || !req.user || conversation.userId !== req.user.id) {
return res.status(403).json({ message: "You don't have permission to access this conversation." });
}
}
// Store user message
const userMessage = messages[messages.length - 1];
if (userMessage.role !== "user") {
return res.status(400).json({ message: "Last message must be from the user." });
}
await storage.createMessage({
content: userMessage.content,
role: userMessage.role,
conversationId
});
// Get user system context if available
let userSystemContext: string | undefined = undefined;
if (req.isAuthenticated() && req.user && req.user.systemContext) {
// If we have a logged-in user, include their system context
userSystemContext = req.user.systemContext;
console.log("Including user system context in conversation:",
userSystemContext ? "Yes" : "None available");
}
// Generate AI response with user's system context if available
const aiResponse = await generateChatResponse(messages, userSystemContext);
// Store AI response
const savedMessage = await storage.createMessage({
content: aiResponse,
role: "assistant",
conversationId
});
// Return the AI response with model info
res.json({
message: savedMessage,
conversationId,
modelInfo: {
model: currentModelStatus.model,
isFallback: currentModelStatus.model !== 'openai'
}
});
} catch (error: any) {
console.error("Chat API error:", error);
res.status(500).json({
message: error.message || "Failed to process chat message."
});
}
});
// Get current model status
app.get("/api/model-status", async (_req: Request, res: Response) => {
try {
// If it's been more than 5 minutes since last check, update status
const fiveMinutesAgo = new Date(Date.now() - 5 * 60 * 1000);
if (currentModelStatus.lastChecked < fiveMinutesAgo) {
await updateModelStatus();
}
return res.json(currentModelStatus);
} catch (error) {
console.error("Error getting model status:", error);
return res.status(500).json({ message: "Failed to get model status" });
}
});
// Delete a conversation
app.delete("/api/conversations/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
// Don't allow deleting the default conversation
if (id === "default") {
return res.status(400).json({ message: "Cannot delete the default conversation" });
}
// Check if conversation exists
const conversation = await storage.getConversation(id);
if (!conversation) {
return res.status(404).json({ message: "Conversation not found" });
}
// Check ownership if the conversation belongs to a user
if (conversation.userId && req.isAuthenticated() && req.user) {
// User must be the owner of the conversation
if (conversation.userId !== req.user.id) {
return res.status(403).json({ message: "You don't have permission to delete this conversation." });
}
}
// Delete the conversation
const success = await storage.deleteConversation(id);
if (success) {
res.status(200).json({ message: "Conversation deleted successfully" });
} else {
res.status(500).json({ message: "Failed to delete conversation" });
}
} catch (error) {
console.error("Error deleting conversation:", error);
res.status(500).json({ message: "Server error deleting conversation" });
}
});
// Update conversation title
app.patch("/api/conversations/:id/title", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const { title } = req.body;
// Validate title
if (!title || typeof title !== 'string' || title.trim().length === 0) {
return res.status(400).json({ message: "Valid title is required" });
}
// Get the conversation
const conversation = await storage.getConversation(id);
if (!conversation) {
return res.status(404).json({ message: "Conversation not found" });
}
// Check ownership if the conversation belongs to a user
if (conversation.userId && req.isAuthenticated() && req.user) {
// User must be the owner of the conversation
if (conversation.userId !== req.user.id) {
return res.status(403).json({ message: "You don't have permission to update this conversation." });
}
}
// Update the conversation
const updatedConversation = await storage.createConversation({
...conversation,
title: title.trim()
});
res.json(updatedConversation);
} catch (error) {
console.error("Error updating conversation title:", error);
res.status(500).json({ message: "Failed to update conversation title" });
}
});
// Update conversation personality
app.patch("/api/conversations/:id/personality", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const { personality } = req.body;
// Validate personality
const result = personalityTypeSchema.safeParse(personality);
if (!result.success) {
return res.status(400).json({
message: "Invalid personality type",
validOptions: personalityTypeSchema.options
});
}
// Get the conversation
const conversation = await storage.getConversation(id);
if (!conversation) {
return res.status(404).json({ message: "Conversation not found" });
}
// Check ownership if the conversation belongs to a user
if (conversation.userId && req.isAuthenticated() && req.user) {
// User must be the owner of the conversation
if (conversation.userId !== req.user.id) {
return res.status(403).json({ message: "You don't have permission to update this conversation." });
}
}
// Update the conversation personality
const updatedConversation = await storage.updateConversationPersonality(id, result.data);
// Return the updated conversation with personality details
const personalityConfig = getPersonalityConfig(result.data);
res.json({
...updatedConversation,
personalityConfig: {
name: personalityConfig.name,
description: personalityConfig.description,
emoji: personalityConfig.emoji
}
});
} catch (error) {
console.error("Error updating conversation personality:", error);
res.status(500).json({ message: "Failed to update conversation personality" });
}
});
// Get available personalities
app.get("/api/personalities", async (_req: Request, res: Response) => {
try {
// Get all personality types from the schema
const personalityTypes = personalityTypeSchema.options;
// Map to include details for each personality
const personalities = personalityTypes.map(type => {
const config = getPersonalityConfig(type);
return {
id: type,
name: config.name,
description: config.description,
emoji: config.emoji
};
});
res.json(personalities);
} catch (error) {
console.error("Error fetching personalities:", error);
res.status(500).json({ message: "Failed to fetch personalities" });
}
});
// Generate image with FLUX.1-dev
app.post("/api/generate-image", async (req: Request, res: Response) => {
try {
// Validate the request body using the schema
const result = imageGenerationSchema.safeParse(req.body);
if (!result.success) {
return res.status(400).json({
message: "Invalid image generation parameters",
errors: result.error.format()
});
}
// Generate the image
const imageUrl = await generateImage(result.data);
// Return the image URL
return res.json({
success: true,
imageUrl,
params: result.data
});
} catch (error: any) {
console.error("Error generating image:", error);
return res.status(500).json({
success: false,
message: error.message || "Failed to generate image"
});
}
});
// Check FLUX availability
app.get("/api/flux-status", async (_req: Request, res: Response) => {
try {
const isAvailable = await isFluxAvailable();
return res.json({
isAvailable,
model: "FLUX.1-dev"
});
} catch (error) {
console.error("Error checking FLUX availability:", error);
return res.status(500).json({
isAvailable: false,
message: "Error checking FLUX availability"
});
}
});
// Generate video using Replicate
app.post("/api/generate-video", async (req: Request, res: Response) => {
try {
// Validate the request body using the schema
const result = videoGenerationSchema.safeParse(req.body);
if (!result.success) {
return res.status(400).json({
message: "Invalid video generation parameters",
errors: result.error.format()
});
}
// Generate the video
const videoUrl = await generateVideo(result.data);
// Return the video URL
return res.json({
success: true,
videoUrl,
params: result.data
});
} catch (error: any) {
console.error("Error generating video:", error);
return res.status(500).json({
success: false,
message: error.message || "Failed to generate video"
});
}
});
// Check video generation availability
app.get("/api/video-status", async (_req: Request, res: Response) => {
try {
const isAvailable = await isVideoGenerationAvailable();
return res.json({
isAvailable,
model: "Wan-AI/Wan2.1-T2V-14B"
});
} catch (error) {
console.error("Error checking video generation availability:", error);
return res.status(500).json({
isAvailable: false,
message: "Error checking video generation availability"
});
}
});
// Health check endpoint
app.get("/api/health", (_req: Request, res: Response) => {
return res.json({ status: "ok" });
});
const httpServer = createServer(app);
return httpServer;
}
|