feat(departments): management routes under the settings-ui bundle
This commit is contained in:
parent
cde027d330
commit
a835074f5c
|
|
@ -1,4 +1,5 @@
|
||||||
import { createSettingsUiRoutes } from "./settings-ui/routes.js";
|
import { createSettingsUiRoutes } from "./settings-ui/routes.js";
|
||||||
|
import { serveDepartments } from "./settings-ui/departments-routes.js";
|
||||||
import { brainStatus, migrateLegacyFiles } from "./brain-migration.js";
|
import { brainStatus, migrateLegacyFiles } from "./brain-migration.js";
|
||||||
import { mcpCapability } from "./mcp.js";
|
import { mcpCapability } from "./mcp.js";
|
||||||
import { fail, json, readBody, sameOrigin } from "./http.js";
|
import { fail, json, readBody, sameOrigin } from "./http.js";
|
||||||
|
|
@ -19,6 +20,7 @@ export const ROUTES = {
|
||||||
clients: '/dsh-brain/clients',
|
clients: '/dsh-brain/clients',
|
||||||
migrate: '/dsh-brain/migrate',
|
migrate: '/dsh-brain/migrate',
|
||||||
localMemory: '/dsh-brain/local-memory',
|
localMemory: '/dsh-brain/local-memory',
|
||||||
|
departments: '/dsh-brain/departments',
|
||||||
};
|
};
|
||||||
/** Reported when the MCP sub-plugin entry is not applied; the client keeps the tab hidden. */
|
/** Reported when the MCP sub-plugin entry is not applied; the client keeps the tab hidden. */
|
||||||
const MCP_UNAVAILABLE = {
|
const MCP_UNAVAILABLE = {
|
||||||
|
|
@ -381,6 +383,11 @@ export function createCoreRoutes(deps) {
|
||||||
path: ROUTES.localMemory,
|
path: ROUTES.localMemory,
|
||||||
handler: (req, res) => serveLocalMemory(deps, req, res),
|
handler: (req, res) => serveLocalMemory(deps, req, res),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
kind: 'prefix',
|
||||||
|
path: ROUTES.departments,
|
||||||
|
handler: (req, res) => serveDepartments(deps, req, res),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
kind: 'exact',
|
kind: 'exact',
|
||||||
path: ROUTES.status,
|
path: ROUTES.status,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,164 @@
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,7 @@ import type { CloudClient } from './api.ts'
|
||||||
import type { SettingsStore, SettingsView, TokenView } from './store.ts'
|
import type { SettingsStore, SettingsView, TokenView } from './store.ts'
|
||||||
import type { Reporter } from './reporter.ts'
|
import type { Reporter } from './reporter.ts'
|
||||||
import { createSettingsUiRoutes } from './settings-ui/routes.ts'
|
import { createSettingsUiRoutes } from './settings-ui/routes.ts'
|
||||||
|
import { serveDepartments } from './settings-ui/departments-routes.ts'
|
||||||
import type { CloudMemoryWebRoute } from './context-types.ts'
|
import type { CloudMemoryWebRoute } from './context-types.ts'
|
||||||
import type { LocalMemoryManager } from './local-memory/manager.ts'
|
import type { LocalMemoryManager } from './local-memory/manager.ts'
|
||||||
import type { DepartmentManager } from './local-memory/departments/index.ts'
|
import type { DepartmentManager } from './local-memory/departments/index.ts'
|
||||||
|
|
@ -54,6 +55,7 @@ export const ROUTES = {
|
||||||
clients: '/dsh-brain/clients',
|
clients: '/dsh-brain/clients',
|
||||||
migrate: '/dsh-brain/migrate',
|
migrate: '/dsh-brain/migrate',
|
||||||
localMemory: '/dsh-brain/local-memory',
|
localMemory: '/dsh-brain/local-memory',
|
||||||
|
departments: '/dsh-brain/departments',
|
||||||
} as const
|
} as const
|
||||||
|
|
||||||
/** Everything the routes need from the plugin core. */
|
/** Everything the routes need from the plugin core. */
|
||||||
|
|
@ -406,6 +408,11 @@ export function createCoreRoutes(deps: RouteDeps): CloudMemoryWebRoute[] {
|
||||||
path: ROUTES.localMemory,
|
path: ROUTES.localMemory,
|
||||||
handler: (req, res) => serveLocalMemory(deps, req, res),
|
handler: (req, res) => serveLocalMemory(deps, req, res),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
kind: 'prefix',
|
||||||
|
path: ROUTES.departments,
|
||||||
|
handler: (req, res) => serveDepartments(deps, req, res),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
kind: 'exact',
|
kind: 'exact',
|
||||||
path: ROUTES.status,
|
path: ROUTES.status,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,129 @@
|
||||||
|
/**
|
||||||
|
* Department management routes (P1.5 D1).
|
||||||
|
*
|
||||||
|
* Served by the settings-ui bundle through the root route table. The route
|
||||||
|
* layer only adapts HTTP to DepartmentManager; the department library remains
|
||||||
|
* unchanged and owns all persistence semantics.
|
||||||
|
*/
|
||||||
|
import type { IncomingMessage, ServerResponse } from 'node:http'
|
||||||
|
import type { SettingsStore } from '../store.ts'
|
||||||
|
import type { DepartmentManager } from '../local-memory/departments/index.ts'
|
||||||
|
import type { DepartmentCreateInput, DepartmentUpdateInput } from '../local-memory/departments/types.ts'
|
||||||
|
import { fail, json, readBody, sameOrigin } from '../http.ts'
|
||||||
|
|
||||||
|
export interface DepartmentRouteDeps {
|
||||||
|
store: SettingsStore
|
||||||
|
departments?: DepartmentManager
|
||||||
|
}
|
||||||
|
|
||||||
|
function errorStatus(message: string): number | undefined {
|
||||||
|
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: ServerResponse, error: unknown): void {
|
||||||
|
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: IncomingMessage, res: ServerResponse): Promise<Record<string, unknown> | undefined> {
|
||||||
|
let value: unknown
|
||||||
|
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 as Record<string, unknown>
|
||||||
|
}
|
||||||
|
|
||||||
|
function stringField(value: unknown): string | undefined { return typeof value === 'string' ? value : undefined }
|
||||||
|
function revisionField(value: unknown): number | undefined { return typeof value === 'number' && Number.isFinite(value) ? Math.trunc(value) : undefined }
|
||||||
|
|
||||||
|
export async function serveDepartments(deps: DepartmentRouteDeps, req: IncomingMessage, res: ServerResponse): Promise<void> {
|
||||||
|
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: DepartmentCreateInput = {
|
||||||
|
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): item is string => typeof item === 'string') } : {}),
|
||||||
|
...(Array.isArray(body.allowedTools) ? { allowedTools: body.allowedTools.filter((item): item is string => 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: DepartmentUpdateInput = {
|
||||||
|
...(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): item is string => typeof item === 'string') } : {}),
|
||||||
|
...(Array.isArray(body.allowedTools) ? { allowedTools: body.allowedTools.filter((item): item is string => 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: { currentTask?: string; handoff?: string } = {
|
||||||
|
...(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) }
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue