File size: 4,185 Bytes
ddac6a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2316bca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddac6a2
 
fe9f896
 
 
 
 
 
ddac6a2
 
5def997
 
 
 
 
 
 
 
fe9f896
 
 
ddac6a2
 
 
 
 
 
 
fe9f896
 
ddac6a2
 
 
fe9f896
 
 
ddac6a2
 
fe9f896
 
 
 
ddac6a2
 
 
fe9f896
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddac6a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe9f896
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
import express from 'express';
import cors from 'cors';
import fetch from 'node-fetch';
import path from 'path'
import { fileURLToPath } from 'url'

const app = express();
const port = 7860;

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

app.use(cors());
app.use(express.json());

app.post('/api/summarize', async (req, res) => {
  const { content, apiEndpoint, apiKey, model } = req.body;
  
  console.log('Received summarize request with:', {
    apiEndpoint,
    model,
    contentLength: content.length
  });

  try {
    let apiUrl;
    if (apiEndpoint.endsWith('#')) {
        apiUrl = apiEndpoint.slice(0, -1);
    } else if (apiEndpoint.endsWith('/')) {
        apiUrl = `${apiEndpoint}chat/completions`;
    } else {
        apiUrl = `${apiEndpoint}/v1/chat/completions`;
    }
    console.log('Calling API endpoint:', apiUrl);
    
    const response = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`
      },
      body: JSON.stringify({
        model: model,
        messages: [{
          role: 'user',
          content: `Summarize this conversation in 3-5 words: ${content}`
        }],
        temperature: 0.2,
        max_tokens: 20
      })
    });

    if (!response.ok) {
      const errorData = await response.text();
      console.error('API error:', {
        status: response.status,
        error: errorData
      });
      throw new Error(`API error: ${response.status} - ${errorData}`);
    }

    const data = await response.json();
    const summary = data.choices[0].message.content.trim();
    res.json({ summary });
  } catch (error) {
    console.error('Error:', error);
    res.status(500).json({ error: error.message });
  }
});

app.post('/api/chat', async (req, res) => {
  const { messages, apiEndpoint, apiKey, model } = req.body;
  
  console.log('Received chat request with:', {
    apiEndpoint,
    model,
    messageCount: messages.length
  });

  try {
    let apiUrl;
    if (apiEndpoint.endsWith('#')) {
        apiUrl = apiEndpoint.slice(0, -1);
    } else if (apiEndpoint.endsWith('/')) {
        apiUrl = `${apiEndpoint}chat/completions`;
    } else {
        apiUrl = `${apiEndpoint}/v1/chat/completions`;
    }
    console.log('Calling API endpoint:', apiUrl);
    
    const response = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`
      },
      body: JSON.stringify({
        model: model,
        messages: messages,
        stream: true
      })
    });

    console.log('API response status:', response.status);
    console.log('API response headers:', Object.fromEntries(response.headers.entries()));

    if (!response.ok) {
      const errorData = await response.text();
      console.error('API error:', {
        status: response.status,
        error: errorData
      });
      throw new Error(`API error: ${response.status} - ${errorData}`);
    }

    // Set headers for streaming
    res.setHeader('Content-Type', 'text/event-stream');
    res.setHeader('Cache-Control', 'no-cache');
    res.setHeader('Connection', 'keep-alive');

    // Set SSE headers
    res.writeHead(200, {
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache',
      'Connection': 'keep-alive'
    });

    // Pipe the API response to the client
    response.body.pipe(res).on('error', (err) => {
      console.error('Stream error:', err);
      res.status(500).end();
    }).on('end', () => {
      console.log('Stream completed successfully');
    });
  } catch (error) {
    console.error('Error:', error);
    res.status(500).json({ error: error.message });
  }
});

if (process.env.NODE_ENV === 'production') {
  // Serve static files from the dist directory
  app.use(express.static(path.join(__dirname, '../dist')))

  // Handle all other routes by serving the index.html
  app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, '../dist/index.html'))
  })
}

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});