File size: 3,124 Bytes
b273c57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import express from "express";
import path from "path";
import { createServer as createViteServer } from "vite";
import { GoogleGenAI } from "@google/genai";

async function startServer() {
  const app = express();
  app.use(express.json());

  const PORT = 3000;

  // Initialize Gemini
  const apiKey = process.env.GEMINI_API_KEY;
  if (!apiKey) {
    console.warn("GEMINI_API_KEY environment variable is not set. Go to Settings > Secrets to add it.");
  }
  
  const ai = new GoogleGenAI({
    apiKey: apiKey || "",
    httpOptions: {
      headers: {
        'User-Agent': 'aistudio-build',
      }
    }
  });

  // API Endpoints
  app.post("/api/generate", async (req, res) => {
    try {
      const { model, stream, contents, config } = req.body;
      
      // Determine the best model to use
      let targetModel = "gemini-3.5-flash"; // Default
      if (model && typeof model === 'string') {
          if (model.includes('pro')) {
              targetModel = 'gemini-3.1-pro-preview';
          } else if (model.includes('flash')) {
              targetModel = 'gemini-3.5-flash';
          }
      }

      if (stream) {
        res.setHeader('Content-Type', 'text/event-stream');
        res.setHeader('Cache-Control', 'no-cache');
        res.setHeader('Connection', 'keep-alive');

        try {
            const responseStream = await ai.models.generateContentStream({
              model: targetModel,
              contents: contents,
              config: config
            });

            for await (const chunk of responseStream) {
              res.write(`data: ${JSON.stringify(chunk)}\n\n`);
            }
            res.end();
        } catch (streamError: any) {
            console.error("Gemini Streaming Error:", streamError);
            res.write(`data: ${JSON.stringify({ error: { message: streamError.message } })}\n\n`);
            res.end();
        }
      } else {
        const response = await ai.models.generateContent({
          model: targetModel,
          contents: contents,
          config: config
        });
        
        // Support both direct SDK response structure and extended text for convenience
        res.json({
          text: response.text,
          candidates: response.candidates,
          usageMetadata: response.usageMetadata
        });
      }
    } catch (error: any) {
      console.error("Gemini API Error:", error);
      res.status(500).json({ error: { message: error.message } });
    }
  });

  // Health check
  app.get("/api/health", (req, res) => {
    res.json({ status: "ok" });
  });

  // Vite middleware for development
  if (process.env.NODE_ENV !== "production") {
    const vite = await createViteServer({
      server: { middlewareMode: true },
      appType: "spa",
    });
    app.use(vite.middlewares);
  } else {
    const distPath = path.join(process.cwd(), 'dist');
    app.use(express.static(distPath));
    app.get('*', (req, res) => {
      res.sendFile(path.join(distPath, 'index.html'));
    });
  }

  app.listen(PORT, "0.0.0.0", () => {
    console.log(`Server running on port ${PORT}`);
  });
}

startServer();