File size: 1,585 Bytes
5dfbe50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Hono } from 'hono';
import { streamSSE } from 'hono/streaming';

const chatApp = new Hono();

// Visual RAG Chat SSE endpoint
chatApp.get('/', async (c) => {
  const queryId = c.req.query('queryId');
  const query = c.req.query('query');
  const docIds = c.req.query('docIds');

  if (!queryId || !query || !docIds) {
    return c.json({ error: 'Missing required parameters: queryId, query, docIds' }, 400);
  }

  return streamSSE(c, async (stream) => {
    try {
      // Mock response for now - in production this would use an LLM
      const messages = [
        `I'll analyze the search results for your query: "${query}"`,
        "Based on the documents provided, here are the key findings:",
        "1. LINQTO filed for Chapter 11 bankruptcy protection",
        "2. The filing includes detailed financial statements and creditor information",
        "3. Various claims and assets are documented in the court filings",
        "",
        "This is a demo response. In production, this would analyze the actual document contents using an LLM."
      ];
      
      for (const msg of messages) {
        await stream.writeSSE({ data: msg });
        await new Promise(resolve => setTimeout(resolve, 300)); // Simulate typing
      }
    } catch (error) {
      console.error('Chat streaming error:', error);
      await stream.writeSSE({
        event: 'error',
        data: JSON.stringify({ 
          error: 'Streaming failed', 
          message: error instanceof Error ? error.message : 'Unknown error' 
        }),
      });
    }
  });
});

export { chatApp };