-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
148 lines (132 loc) · 4.34 KB
/
Copy pathapp.js
File metadata and controls
148 lines (132 loc) · 4.34 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
import express from 'express';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {
saveChunk,
fetchRecent,
searchChunks,
searchByTime,
feedback,
promoteDemote,
forget,
audit,
dashboardOverview,
setChunkStatus,
pipelineStatus,
} from './service.js';
import { createAuth } from './auth.js';
import { buildSetupStatus } from './system-status.js';
import { getDashboardContract } from './dashboard-contract.js';
export function createApp() {
const app = express();
const { requireRole } = createAuth();
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const publicDir = path.join(__dirname, 'public');
const dashboardHtml = path.join(publicDir, 'dashboard.html');
app.use(express.json({ limit: '1mb' }));
app.get(['/dashboard', '/dashboard/'], (_req, res) => {
res.sendFile(dashboardHtml);
});
app.use('/dashboard', express.static(publicDir, { index: false, redirect: false }));
app.get('/healthz', (_req, res) => {
res.json({ ok: true });
});
app.get('/v1/system/setup-status', (_req, res) => {
res.json({ ok: true, data: buildSetupStatus(process.env) });
});
app.post('/v1/memory/save', async (req, res) => {
try {
const data = await saveChunk(req.body || {});
res.json({ ok: true, data });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.post('/v1/memory/fetch_recent', async (req, res) => {
try {
const chunks = await fetchRecent(req.body || {});
res.json({ ok: true, data: { chunks, total: chunks.length } });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.post('/v1/memory/search', async (req, res) => {
try {
const chunks = await searchChunks({
...req.body,
query_text: req.body?.query,
});
res.json({ ok: true, data: { chunks, total: chunks.length } });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.post('/v1/memory/search_by_time', async (req, res) => {
try {
const items = await searchByTime(req.body || {});
res.json({ ok: true, data: { items, total: items.length } });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.post('/v1/memory/feedback', async (req, res) => {
try {
const data = await feedback(req.body || {});
res.json({ ok: true, data });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.post('/v1/memory/promote_demote', async (req, res) => {
try {
const data = await promoteDemote(req.body || {});
res.json({ ok: true, data });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.post('/v1/memory/forget', async (req, res) => {
try {
const data = await forget(req.body || {});
res.json({ ok: true, data });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.post('/v1/memory/audit', async (req, res) => {
try {
const data = await audit(req.body || {});
res.json({ ok: true, data });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.get('/v1/dashboard/contract', requireRole('viewer'), (_req, res) => {
res.json({ ok: true, data: getDashboardContract() });
});
app.post('/v1/dashboard/overview', requireRole('viewer'), async (req, res) => {
try {
const data = await dashboardOverview(req.body || {});
res.json({ ok: true, data });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.post('/v1/dashboard/set_status', requireRole('operator'), async (req, res) => {
try {
const data = await setChunkStatus(req.body || {});
res.json({ ok: true, data });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
app.get('/v1/dashboard/pipeline_status', requireRole('viewer'), async (_req, res) => {
try {
const data = await pipelineStatus();
res.json({ ok: true, data });
} catch (e) {
res.status(500).json({ ok: false, error: String(e?.message || e) });
}
});
return app;
}