File size: 5,365 Bytes
c3aa9b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env node

import express from 'express';
import { AIService } from './services/ai-service.js';
import { DocumentationService } from './services/documentation-service.js';
import { VectorService } from './services/vector-service.js';
import { ChangeDetectionService } from './services/change-detection-service.js';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';

dotenv.config();

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

const app = express();
const PORT = process.env.PORT || 3000;

// Initialize services
const aiService = new AIService();
const docService = new DocumentationService();
const vectorService = new VectorService();
const changeDetectionService = new ChangeDetectionService();

// Middleware
app.use(express.json());
// Serve static files from the public directory (one level up from src)
app.use(express.static(path.join(__dirname, '..', 'public')));

// API Routes

// Health check
app.get('/api/health', async (req, res) => {
  const aiAvailable = await aiService.checkAvailability();
  res.json({
    status: 'ok',
    aiService: aiAvailable ? 'available' : 'unavailable',
    provider: process.env.AI_PROVIDER || 'ollama',
  });
});

// List documentation sources
app.get('/api/sources', async (req, res) => {
  try {
    const sources = await docService.listSources();
    res.json({ sources });
  } catch (error) {
    res.status(500).json({ error: String(error) });
  }
});

// Search documentation
app.post('/api/search', async (req, res) => {
  try {
    const { query, source = 'all', limit = 5 } = req.body;
    
    if (!query) {
      return res.status(400).json({ error: 'Query is required' });
    }

    const results = await vectorService.search(query, source, limit);
    res.json({ results });
  } catch (error) {
    res.status(500).json({ error: String(error) });
  }
});

// Ask a question
app.post('/api/ask', async (req, res) => {
  try {
    const { question, context } = req.body;
    
    if (!question) {
      return res.status(400).json({ error: 'Question is required' });
    }

    // Search for relevant documentation
    const searchResults = await vectorService.search(question, 'all', 5);
    
    // Generate answer using AI
    const answer = await aiService.answerQuestion(question, searchResults, context);
    
    res.json({ answer, sources: searchResults });
  } catch (error) {
    res.status(500).json({ error: String(error) });
  }
});

// Get documentation content
app.post('/api/fetch', async (req, res) => {
  try {
    const { url } = req.body;
    
    if (!url) {
      return res.status(400).json({ error: 'URL is required' });
    }

    const { markdown, title } = await docService.fetchDocumentation(url);
    res.json({ content: markdown, title });
  } catch (error) {
    res.status(500).json({ error: String(error) });
  }
});

// Summarize documentation
app.post('/api/summarize', async (req, res) => {
  try {
    const { url, format = 'brief' } = req.body;
    
    if (!url) {
      return res.status(400).json({ error: 'URL is required' });
    }

    const { markdown } = await docService.fetchDocumentation(url);
    const summary = await aiService.summarize(markdown, format);
    
    res.json({ summary });
  } catch (error) {
    res.status(500).json({ error: String(error) });
  }
});

// Index documentation
app.post('/api/index', async (req, res) => {
  try {
    const { source, forceRefresh = false } = req.body;
    
    if (!source) {
      return res.status(400).json({ error: 'Source is required' });
    }

    // Return immediately and process in background
    res.json({ message: 'Indexing started', source });
    
    // Process in background
    const status = await docService.indexDocumentation(source, forceRefresh);
    console.log('Indexing complete:', status);
  } catch (error) {
    console.error('Indexing error:', error);
  }
});

// Check for changes
app.post('/api/check-changes', async (req, res) => {
  try {
    const { source, limit, olderThanDays } = req.body;
    
    await changeDetectionService.initialize();
    
    if (source) {
      const result = await changeDetectionService.checkSourceForChanges(source, {
        limit,
        olderThanDays,
        parallel: 3,
      });
      res.json({ result });
    } else {
      return res.status(400).json({ error: 'Source is required' });
    }
  } catch (error) {
    res.status(500).json({ error: String(error) });
  }
});

// Get change detection statistics
app.get('/api/change-stats', async (req, res) => {
  try {
    await changeDetectionService.initialize();
    const stats = await changeDetectionService.getStatistics();
    res.json({ stats });
  } catch (error) {
    res.status(500).json({ error: String(error) });
  }
});

// Serve the main HTML page
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
});

// Start server
app.listen(PORT, '0.0.0.0', () => {
  console.log(`\n🚀 SUSE Docs Navigator Web UI running at http://localhost:${PORT}`);
  console.log(`\nConfiguration:`);
  console.log(`  AI Provider: ${process.env.AI_PROVIDER || 'ollama'}`);
  console.log(`  Model: ${process.env.OLLAMA_MODEL || 'llama3.2:latest'}`);
  console.log(`\nOpen http://localhost:${PORT} in your browser to get started!\n`);
});