-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
418 lines (344 loc) · 13.1 KB
/
app.py
File metadata and controls
418 lines (344 loc) · 13.1 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File: app.py
# Path: ~/knowledgeagent/app.py
from flask import Flask, render_template, jsonify, request, send_from_directory
from pathlib import Path
import os
import json
import sqlite3
from datetime import datetime
import markdown
import re
app = Flask(__name__)
@app.route('/midnight')
def midnight():
return render_template('midnight.html')
# Configuration
KNOWLEDGE_BASE_DIR = Path('knowledge_base')
DATABASE_PATH = Path('research_tasks.db')
# Ensure directories exist
KNOWLEDGE_BASE_DIR.mkdir(exist_ok=True)
def init_db():
"""Initialize the SQLite database"""
conn = sqlite3.connect(DATABASE_PATH)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
topic TEXT NOT NULL,
context TEXT,
source_type TEXT DEFAULT 'manual',
source_url TEXT,
status TEXT DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMP,
error TEXT
)
''')
conn.commit()
conn.close()
def get_db():
"""Get database connection"""
conn = sqlite3.connect(DATABASE_PATH)
conn.row_factory = sqlite3.Row
return conn
def scan_documents():
"""Scan knowledge base directory for documents"""
documents = []
if not KNOWLEDGE_BASE_DIR.exists():
return documents
for file_path in KNOWLEDGE_BASE_DIR.rglob('*.md'):
stat = file_path.stat()
rel_path = file_path.relative_to(KNOWLEDGE_BASE_DIR)
documents.append({
'name': file_path.name,
'relative_path': str(rel_path),
'size': stat.st_size,
'modified': stat.st_mtime
})
# Sort by modified time, newest first
documents.sort(key=lambda x: x['modified'], reverse=True)
return documents
def categorize_documents(documents):
"""Categorize documents based on their path or filename"""
categories = {
'midnight': {'count': 0, 'size': 0},
'cardano': {'count': 0, 'size': 0},
'healthcare': {'count': 0, 'size': 0},
'zkproofs': {'count': 0, 'size': 0},
'smart_contracts': {'count': 0, 'size': 0},
'architecture': {'count': 0, 'size': 0},
'competitors': {'count': 0, 'size': 0},
'research': {'count': 0, 'size': 0}
}
for doc in documents:
path = doc['relative_path'].lower()
name = doc['name'].lower()
size = doc['size']
categorized = False
# Check filename FIRST for specific keywords (higher priority)
if 'smart_contract' in name or 'smartcontract' in name or 'plutus' in name:
categories['smart_contracts']['count'] += 1
categories['smart_contracts']['size'] += size
categorized = True
elif 'zkproof' in name or 'zk_proof' in name or 'zero_knowledge' in name or 'zero-knowledge' in name:
categories['zkproofs']['count'] += 1
categories['zkproofs']['size'] += size
categorized = True
elif 'healthcare' in name or 'health' in name:
categories['healthcare']['count'] += 1
categories['healthcare']['size'] += size
categorized = True
elif 'architecture' in name:
categories['architecture']['count'] += 1
categories['architecture']['size'] += size
categorized = True
elif 'competitor' in name:
categories['competitors']['count'] += 1
categories['competitors']['size'] += size
categorized = True
# If not categorized by filename, check path
if not categorized:
if path.startswith('midnight'):
categories['midnight']['count'] += 1
categories['midnight']['size'] += size
elif path.startswith('cardano'):
categories['cardano']['count'] += 1
categories['cardano']['size'] += size
elif path.startswith('healthcare'):
categories['healthcare']['count'] += 1
categories['healthcare']['size'] += size
elif path.startswith('zkproofs') or path.startswith('zk'):
categories['zkproofs']['count'] += 1
categories['zkproofs']['size'] += size
elif path.startswith('smart_contracts') or path.startswith('smartcontracts'):
categories['smart_contracts']['count'] += 1
categories['smart_contracts']['size'] += size
elif path.startswith('architecture'):
categories['architecture']['count'] += 1
categories['architecture']['size'] += size
elif path.startswith('competitors'):
categories['competitors']['count'] += 1
categories['competitors']['size'] += size
elif path.startswith('research'):
categories['research']['count'] += 1
categories['research']['size'] += size
elif 'midnight' in name:
categories['midnight']['count'] += 1
categories['midnight']['size'] += size
elif 'cardano' in name:
categories['cardano']['count'] += 1
categories['cardano']['size'] += size
# In app.py - already works!
elif path.startswith('cardano'):
categories['cardano']['count'] += 1
categories['cardano']['size'] += size
elif 'cardano' in name: # This catches "Cardano_Node" files
categories['cardano']['count'] += 1
categories['cardano']['size'] += size
else:
# Default to research if no specific category matches
categories['research']['count'] += 1
categories['research']['size'] += size
return categories
# Routes
@app.route('/')
def index():
"""Render the dashboard"""
return render_template('dashboard.html')
@app.route('/api/stats')
def get_stats():
"""Get dashboard statistics"""
documents = scan_documents()
categories = categorize_documents(documents)
total_size = sum(doc['size'] for doc in documents)
total_size_kb = round(total_size / 1024, 1)
return jsonify({
'total_documents': len(documents),
'total_size_kb': total_size_kb,
'categories': categories
})
@app.route('/api/recent')
def get_recent():
"""Get recent documents"""
limit = request.args.get('limit', 50, type=int)
category = request.args.get('category', '')
documents = scan_documents()
# Filter by category if specified
if category:
filtered = []
for doc in documents:
path = doc['relative_path'].lower()
name = doc['name'].lower()
# Match category - filename takes priority
matches = False
if category == 'smart_contracts':
matches = 'plutus' in name or 'smart_contract' in name or path.startswith('smart_contracts')
elif category == 'zkproofs':
matches = 'zkproof' in name or 'zk' in name or path.startswith('zkproofs')
else:
matches = path.startswith(category.lower()) or category.lower() in name
if matches:
filtered.append(doc)
documents = filtered
# Limit results
documents = documents[:limit]
return jsonify({
'documents': documents,
'total': len(documents)
})
@app.route('/api/document/<path:doc_path>')
def get_document(doc_path):
"""Get document content"""
file_path = KNOWLEDGE_BASE_DIR / doc_path
if not file_path.exists():
return jsonify({'error': 'Document not found'}), 404
try:
content = file_path.read_text(encoding='utf-8')
html = markdown.markdown(content, extensions=['fenced_code', 'tables'])
return jsonify({
'content': content,
'html': html
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/document/<path:doc_path>', methods=['DELETE'])
def delete_document(doc_path):
"""Delete a document"""
file_path = KNOWLEDGE_BASE_DIR / doc_path
if not file_path.exists():
return jsonify({'error': 'Document not found'}), 404
try:
file_path.unlink()
return jsonify({'message': 'Document deleted successfully'})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/documents/bulk-delete', methods=['POST'])
def bulk_delete():
"""Delete multiple documents"""
data = request.get_json()
paths = data.get('paths', [])
deleted = []
errors = []
for path in paths:
file_path = KNOWLEDGE_BASE_DIR / path
try:
if file_path.exists():
file_path.unlink()
deleted.append(path)
else:
errors.append(f"{path}: Not found")
except Exception as e:
errors.append(f"{path}: {str(e)}")
return jsonify({
'message': f'Deleted {len(deleted)} document(s)',
'deleted': deleted,
'errors': errors
})
@app.route('/api/tasks')
def get_tasks():
"""Get all research tasks"""
conn = get_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM tasks ORDER BY created_at DESC')
rows = cursor.fetchall()
tasks = []
for row in rows:
tasks.append({
'id': row['id'],
'topic': row['topic'],
'context': row['context'],
'source_type': row['source_type'],
'source_url': row['source_url'],
'status': row['status'],
'created_at': row['created_at'],
'completed_at': row['completed_at'],
'error': row['error']
})
conn.close()
return jsonify({'tasks': tasks})
@app.route('/api/tasks', methods=['POST'])
def create_task():
"""Create a new research task"""
data = request.get_json()
topic = data.get('topic')
if not topic:
return jsonify({'error': 'Topic is required'}), 400
context = data.get('context', '')
source_type = data.get('source_type', 'manual')
source_url = data.get('source_url', '')
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO tasks (topic, context, source_type, source_url, status)
VALUES (?, ?, ?, ?, 'pending')
''', (topic, context, source_type, source_url))
task_id = cursor.lastrowid
conn.commit()
conn.close()
return jsonify({
'message': 'Task created successfully',
'task_id': task_id
}), 201
@app.route('/api/tasks/<int:task_id>/approve', methods=['POST'])
def approve_task(task_id):
"""Approve a task and queue it for processing"""
conn = get_db()
cursor = conn.cursor()
cursor.execute('UPDATE tasks SET status = ? WHERE id = ?', ('approved', task_id))
conn.commit()
conn.close()
return jsonify({'message': 'Task approved'})
@app.route('/api/tasks/<int:task_id>/deny', methods=['POST'])
def deny_task(task_id):
"""Deny a task"""
data = request.get_json()
reason = data.get('reason', 'Denied by user')
conn = get_db()
cursor = conn.cursor()
cursor.execute('UPDATE tasks SET status = ?, error = ? WHERE id = ?',
('denied', reason, task_id))
conn.commit()
conn.close()
return jsonify({'message': 'Task denied'})
@app.route('/api/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
"""Delete a task"""
conn = get_db()
cursor = conn.cursor()
cursor.execute('DELETE FROM tasks WHERE id = ?', (task_id,))
conn.commit()
conn.close()
return jsonify({'message': 'Task deleted'})
@app.route('/api/search')
def search():
"""Search documents"""
query = request.args.get('q', '').lower()
if not query:
return jsonify({'results': [], 'total': 0})
results = []
documents = scan_documents()
for doc in documents:
file_path = KNOWLEDGE_BASE_DIR / doc['relative_path']
try:
content = file_path.read_text(encoding='utf-8')
if query in content.lower():
# Find context around the match
lines = content.split('\n')
matched_lines = [line for line in lines if query in line.lower()]
preview = '\n'.join(matched_lines[:3])
results.append({
'relative_path': doc['relative_path'],
'name': doc['name'],
'preview': preview[:500]
})
except Exception as e:
print(f"Error searching {doc['relative_path']}: {e}")
return jsonify({
'results': results,
'total': len(results)
})
if __name__ == '__main__':
init_db()
print(f"Knowledge base directory: {KNOWLEDGE_BASE_DIR.absolute()}")
print(f"Database: {DATABASE_PATH.absolute()}")
app.run(debug=True, port=5000)