110 lines
4.2 KiB
JavaScript
110 lines
4.2 KiB
JavaScript
import { fail, json } from "../http.js";
|
|
function urlOf(req) {
|
|
return new URL(req.url ?? '/', 'http://localhost');
|
|
}
|
|
function method(req, res, allowed) {
|
|
if (req.method === allowed || (allowed === 'GET' && req.method === 'HEAD'))
|
|
return true;
|
|
fail(res, 405, 'method-not-allowed');
|
|
return false;
|
|
}
|
|
function numberParam(url, name, fallback, min, max) {
|
|
const parsed = Number.parseInt(url.searchParams.get(name) ?? '', 10);
|
|
if (!Number.isFinite(parsed))
|
|
return fallback;
|
|
return Math.min(max, Math.max(min, parsed));
|
|
}
|
|
function serveStatus(deps, req, res) {
|
|
if (!method(req, res, 'GET'))
|
|
return;
|
|
const settings = deps.settings();
|
|
void deps.store.status(settings.enabled, settings.retentionDays).then((status) => {
|
|
json(res, 200, { ok: true, ...status });
|
|
}).catch((error) => {
|
|
fail(res, 500, error instanceof Error ? error.message : 'raw-status-failed');
|
|
});
|
|
}
|
|
function serveSessions(deps, req, res) {
|
|
if (!method(req, res, 'GET'))
|
|
return;
|
|
const settings = deps.settings();
|
|
if (!settings.enabled) {
|
|
fail(res, 503, 'raw-disabled');
|
|
return;
|
|
}
|
|
const url = urlOf(req);
|
|
const limit = numberParam(url, 'limit', 30, 1, 100);
|
|
void deps.store.listSessions(limit).then((sessions) => {
|
|
json(res, 200, { ok: true, sessions });
|
|
}).catch((error) => {
|
|
fail(res, 500, error instanceof Error ? error.message : 'raw-sessions-failed');
|
|
});
|
|
}
|
|
function serveSearch(deps, req, res) {
|
|
if (!method(req, res, 'GET'))
|
|
return;
|
|
const settings = deps.settings();
|
|
if (!settings.enabled) {
|
|
fail(res, 503, 'raw-disabled');
|
|
return;
|
|
}
|
|
const url = urlOf(req);
|
|
const query = (url.searchParams.get('q') ?? '').trim();
|
|
if (query === '') {
|
|
fail(res, 400, 'query-required');
|
|
return;
|
|
}
|
|
const roleValue = url.searchParams.get('role');
|
|
const role = roleValue === 'user' || roleValue === 'assistant' ? roleValue : undefined;
|
|
const k = numberParam(url, 'k', settings.maxSearchResults, 1, 50);
|
|
const sessionId = (url.searchParams.get('session_id') ?? '').trim() || undefined;
|
|
void deps.store.search(query, { k, ...(role === undefined ? {} : { role }), ...(sessionId === undefined ? {} : { sessionId }) }).then((result) => {
|
|
json(res, 200, {
|
|
ok: true,
|
|
query,
|
|
total: result.total,
|
|
truncated: result.truncated,
|
|
sessions_scanned: result.sessionsScanned,
|
|
matches: result.matches,
|
|
});
|
|
}).catch((error) => {
|
|
fail(res, 500, error instanceof Error ? error.message : 'raw-search-failed');
|
|
});
|
|
}
|
|
function serveSession(deps, req, res) {
|
|
if (!method(req, res, 'GET'))
|
|
return;
|
|
const settings = deps.settings();
|
|
if (!settings.enabled) {
|
|
fail(res, 503, 'raw-disabled');
|
|
return;
|
|
}
|
|
const url = urlOf(req);
|
|
const sessionId = (url.searchParams.get('session_id') ?? '').trim();
|
|
if (sessionId === '') {
|
|
fail(res, 400, 'session-id-required');
|
|
return;
|
|
}
|
|
const seqValue = url.searchParams.get('seq');
|
|
const seq = seqValue === null ? undefined : Number.parseInt(seqValue, 10);
|
|
const offset = numberParam(url, 'offset', 0, 0, 1000000);
|
|
const limit = numberParam(url, 'limit', settings.maxReadRecords, 1, 500);
|
|
void deps.store.readSession(sessionId, {
|
|
...(seq === undefined || !Number.isFinite(seq) ? {} : { seq }),
|
|
...(seq === undefined ? { offset } : {}),
|
|
limit,
|
|
}).then((result) => {
|
|
json(res, 200, { ok: true, session_id: result.sessionId, total: result.total, items: result.items });
|
|
}).catch((error) => {
|
|
fail(res, 500, error instanceof Error ? error.message : 'raw-session-failed');
|
|
});
|
|
}
|
|
export function createRawMemoryRoutes(deps) {
|
|
return [
|
|
{ kind: 'exact', path: '/dsh-brain/raw/status', handler: (req, res) => serveStatus(deps, req, res) },
|
|
{ kind: 'exact', path: '/dsh-brain/raw/sessions', handler: (req, res) => serveSessions(deps, req, res) },
|
|
{ kind: 'exact', path: '/dsh-brain/raw/search', handler: (req, res) => serveSearch(deps, req, res) },
|
|
{ kind: 'exact', path: '/dsh-brain/raw/session', handler: (req, res) => serveSession(deps, req, res) },
|
|
];
|
|
}
|