165 lines
7.3 KiB
JavaScript
165 lines
7.3 KiB
JavaScript
import { fail, json, readBody, sameOrigin } from "../http.js";
|
|
function errorStatus(message) {
|
|
if (message === 'department_not_found')
|
|
return 404;
|
|
if (message === 'revision_conflict')
|
|
return 409;
|
|
if (message === 'department_disabled')
|
|
return 409;
|
|
if (message === 'department_required_field' || message === 'department_invalid_host' || message === 'secret_not_allowed')
|
|
return 400;
|
|
return undefined;
|
|
}
|
|
function failDepartment(res, error) {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
const status = errorStatus(message);
|
|
if (status === undefined) {
|
|
fail(res, 500, 'internal-error');
|
|
return;
|
|
}
|
|
fail(res, status, message);
|
|
}
|
|
async function bodyObject(req, res) {
|
|
let value;
|
|
try {
|
|
value = JSON.parse(await readBody(req));
|
|
}
|
|
catch {
|
|
fail(res, 400, 'invalid-json');
|
|
return undefined;
|
|
}
|
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
fail(res, 400, 'invalid-body');
|
|
return undefined;
|
|
}
|
|
return value;
|
|
}
|
|
function stringField(value) { return typeof value === 'string' ? value : undefined; }
|
|
function revisionField(value) { return typeof value === 'number' && Number.isFinite(value) ? Math.trunc(value) : undefined; }
|
|
export async function serveDepartments(deps, req, res) {
|
|
const manager = deps.departments;
|
|
if (manager === undefined) {
|
|
fail(res, 503, 'departments-unavailable');
|
|
return;
|
|
}
|
|
if (deps.store.settings().localMemoryEnabled === false) {
|
|
fail(res, 503, 'local-memory-disabled');
|
|
return;
|
|
}
|
|
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
const marker = '/departments';
|
|
const at = url.pathname.indexOf(marker);
|
|
const suffix = at < 0 ? '' : url.pathname.slice(at + marker.length);
|
|
try {
|
|
if (suffix === '' || suffix === '/' || suffix === '/list') {
|
|
if (req.method !== 'GET') {
|
|
fail(res, 405, 'method-not-allowed');
|
|
return;
|
|
}
|
|
const scope = url.searchParams.get('scope') === 'global' ? 'global' : url.searchParams.get('scope') === 'project' ? 'project' : undefined;
|
|
json(res, 200, { ok: true, departments: await manager.list(scope) });
|
|
return;
|
|
}
|
|
if (suffix === '/create') {
|
|
if (req.method !== 'POST') {
|
|
fail(res, 405, 'method-not-allowed');
|
|
return;
|
|
}
|
|
if (!sameOrigin(req)) {
|
|
fail(res, 403, 'origin-rejected');
|
|
return;
|
|
}
|
|
const body = await bodyObject(req, res);
|
|
if (body === undefined)
|
|
return;
|
|
const name = stringField(body.name)?.trim() ?? '';
|
|
if (name === '') {
|
|
fail(res, 400, 'name-required');
|
|
return;
|
|
}
|
|
const input = {
|
|
name,
|
|
...(stringField(body.role) === undefined ? {} : { role: stringField(body.role) }),
|
|
...(body.scope === 'global' || body.scope === 'project' ? { scope: body.scope } : {}),
|
|
...(Array.isArray(body.skills) ? { skills: body.skills.filter((item) => typeof item === 'string') } : {}),
|
|
...(Array.isArray(body.allowedTools) ? { allowedTools: body.allowedTools.filter((item) => typeof item === 'string') } : {}),
|
|
...(typeof body.readGlobal === 'boolean' ? { readGlobal: body.readGlobal } : {}),
|
|
...(typeof body.readProject === 'boolean' ? { readProject: body.readProject } : {}),
|
|
};
|
|
json(res, 200, { ok: true, ...(await manager.create(input)) });
|
|
return;
|
|
}
|
|
if (suffix === '/update' || suffix === '/disable' || suffix === '/checkpoint' || suffix === '/resume') {
|
|
if (req.method !== 'POST') {
|
|
fail(res, 405, 'method-not-allowed');
|
|
return;
|
|
}
|
|
if (!sameOrigin(req)) {
|
|
fail(res, 403, 'origin-rejected');
|
|
return;
|
|
}
|
|
const body = await bodyObject(req, res);
|
|
if (body === undefined)
|
|
return;
|
|
const id = stringField(body.id)?.trim() ?? '';
|
|
if (id === '') {
|
|
fail(res, 400, 'id-required');
|
|
return;
|
|
}
|
|
const expectedRevision = revisionField(body.expectedRevision);
|
|
if (suffix === '/update') {
|
|
const patch = {
|
|
...(stringField(body.name) === undefined ? {} : { name: stringField(body.name) }),
|
|
...(stringField(body.role) === undefined ? {} : { role: stringField(body.role) }),
|
|
...(Array.isArray(body.skills) ? { skills: body.skills.filter((item) => typeof item === 'string') } : {}),
|
|
...(Array.isArray(body.allowedTools) ? { allowedTools: body.allowedTools.filter((item) => typeof item === 'string') } : {}),
|
|
...(typeof body.readGlobal === 'boolean' ? { readGlobal: body.readGlobal } : {}),
|
|
...(typeof body.readProject === 'boolean' ? { readProject: body.readProject } : {}),
|
|
};
|
|
json(res, 200, { ok: true, department: await manager.update(id, patch, expectedRevision) });
|
|
return;
|
|
}
|
|
if (suffix === '/disable') {
|
|
json(res, 200, { ok: true, department: await manager.disable(id, expectedRevision) });
|
|
return;
|
|
}
|
|
if (suffix === '/checkpoint') {
|
|
const input = {
|
|
...(stringField(body.currentTask) === undefined ? {} : { currentTask: stringField(body.currentTask) }),
|
|
...(stringField(body.handoff) === undefined ? {} : { handoff: stringField(body.handoff) }),
|
|
};
|
|
json(res, 200, { ok: true, department: await manager.checkpoint(id, input, expectedRevision) });
|
|
return;
|
|
}
|
|
const host = stringField(body.host)?.trim() || 'dsh';
|
|
const hostSessionId = stringField(body.hostSessionId)?.trim();
|
|
const sessionRef = hostSessionId !== undefined && hostSessionId !== ''
|
|
? await manager.attachSession(id, host, hostSessionId)
|
|
: await manager.resumeSession(id, host);
|
|
json(res, 200, { ok: true, sessionRef });
|
|
return;
|
|
}
|
|
if (suffix === '/recall') {
|
|
if (req.method !== 'GET') {
|
|
fail(res, 405, 'method-not-allowed');
|
|
return;
|
|
}
|
|
const id = (url.searchParams.get('id') ?? '').trim();
|
|
if (id === '') {
|
|
fail(res, 400, 'id-required');
|
|
return;
|
|
}
|
|
const query = (url.searchParams.get('query') ?? '').trim();
|
|
const requested = Number.parseInt(url.searchParams.get('k') ?? '5', 10);
|
|
const k = Number.isFinite(requested) ? Math.max(1, Math.min(5, requested)) : 5;
|
|
const workspace = url.searchParams.get('workspace')?.trim() || undefined;
|
|
json(res, 200, { ok: true, hits: await manager.recall(id, query, k, workspace) });
|
|
return;
|
|
}
|
|
fail(res, 404, 'unknown-department-route');
|
|
}
|
|
catch (error) {
|
|
failDepartment(res, error);
|
|
}
|
|
}
|