File size: 1,904 Bytes
164270c
 
 
 
0714813
164270c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0714813
 
164270c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const WebSocket = require('ws');
const config = require('../config/config');

class PythonService {
  async getStreamingResponse(text, history, onChunk, searchContext = "") {
    return new Promise((resolve, reject) => {
      const ws = new WebSocket(config.localInferenceUrl);
      let fullResponse = "";

      ws.on('open', () => {
        // Transform Gemini-style history to Python backend style
        // Gemini: { role, parts: [{ text }] }
        // Python: { role, content }
        const formattedHistory = history.map(h => ({
          role: h.role === 'model' ? 'assistant' : 'user',
          content: h.parts[0].text
        }));

        ws.send(JSON.stringify({
          text: text,
          history: formattedHistory,
          searchContext: searchContext
        }));
      });

      ws.on('message', (data) => {
        const response = JSON.parse(data);
        
        // Handle standard chunks
        if (response.chunk) {
          fullResponse += response.chunk;
          onChunk({ type: 'chunk', content: response.chunk });
        }
        
        // Handle agentic thoughts
        if (response.thought) {
          onChunk({ type: 'thought', content: response.thought });
        }

        // Handle tool logs (e.g., "Running Python code...")
        if (response.tool) {
          onChunk({ type: 'tool', content: response.tool });
        }

        // Handle tool outputs
        if (response.output) {
          onChunk({ type: 'output', content: response.output });
        }

        if (response.done) {
          ws.close();
          resolve(fullResponse);
        }
      });

      ws.on('error', (error) => {
        console.error('Python Inference Error:', error);
        reject(error);
      });

      ws.on('close', () => {
        console.log('Python Inference connection closed');
      });
    });
  }
}

module.exports = new PythonService();