From adee678daa0995716bcc175452a04d15daea49be Mon Sep 17 00:00:00 2001 From: QQ Date: Thu, 12 Nov 2026 00:02:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20src/routes.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes.ts | 775 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 675 insertions(+), 100 deletions(-) diff --git a/src/routes.ts b/src/routes.ts index fd40822..c2cfe22 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -2,16 +2,22 @@ * HTTP surface served by the host web server. * * All routes live under `/dsh-brain`: - * - `GET /dsh-brain` configuration page (HTML) - * - `GET /dsh-brain/status` settings + reporter counters (JSON) - * - `PUT /dsh-brain/config` apply a settings patch (JSON) - * - `POST /dsh-brain/login` password sign-in, mints both tokens (JSON) - * - `POST /dsh-brain/test` validate both tokens (JSON) - * - `POST /dsh-brain/flush` retry the queued reports (JSON) + * - `GET /dsh-brain` configuration page (HTML) + * - `GET /dsh-brain/status` settings + reporter counters + capabilities + * - `GET /dsh-brain/capabilities` what this build can and cannot do + * - `PUT /dsh-brain/config` apply a settings patch + * - `POST /dsh-brain/login` password sign-in, mints the tokens + * - `POST /dsh-brain/test` read-only checks, per credential + * - `POST /dsh-brain/flush` retry the queued reports + * - `GET /dsh-brain/skills/api` skill inventory + pending plans + * - `POST /dsh-brain/skills/*` detail / toggle / install plan / commit / uninstall * * Every JSON route is same-origin only: the tokens are read from the local file * and never echoed back, but a cross-site `PUT` could still be used to redirect * reports to an attacker's base URL, so the origin check stays on. + * + * Skill writes follow a plan → commit contract: a destructive operation is + * never reachable in one request, and a client-supplied path is never trusted. */ import type { IncomingMessage, ServerResponse } from 'node:http' import type { CloudClient } from './api.ts' @@ -20,20 +26,33 @@ import type { SettingsView, SettingsStore, TokenView } from './store.ts' import type { Reporter } from './reporter.ts' import { renderConfigPage } from './page.ts' import type { CloudMemoryWebRoute } from './context-types.ts' -import type { SkillSyncStats } from './skill-sync.ts' +import type { SkillSync, SkillSyncStats } from './skill-sync.ts' import { SkillAdminError } from './skill-admin.ts' import type { SkillAdmin } from './skill-admin.ts' +import { MarketError } from './market.ts' +import type { MarketService } from './market.ts' +import type { SkillUpdater } from './skill-update.ts' +import type { ClientRegistry } from './clients.ts' +import { brainStatus, migrateLegacyFiles } from './brain-migration.ts' +import { SkillGroupError } from './skill-groups.ts' +import type { SkillGroupManager } from './skill-groups.ts' import { renderSkillsPage } from './skill-page.ts' +import { randomUUID } from 'node:crypto' /** Route constants — kept in one place so the client half can reuse them. */ export const ROUTES = { page: '/dsh-brain', status: '/dsh-brain/status', + capabilities: '/dsh-brain/capabilities', config: '/dsh-brain/config', login: '/dsh-brain/login', test: '/dsh-brain/test', flush: '/dsh-brain/flush', skills: '/dsh-brain/skills', + market: '/dsh-brain/market', + sync: '/dsh-brain/sync', + clients: '/dsh-brain/clients', + migrate: '/dsh-brain/migrate', } as const /** Largest request body we accept (a settings patch is tiny). */ @@ -47,8 +66,81 @@ export interface RouteDeps { client: () => CloudClient /** Latest skill-sync stats, when the engine is wired in. */ skills?: () => SkillSyncStats - /** Local skill management (list / toggle / trash / install), when wired in. */ + /** Local skill management (list / toggle / install / uninstall), when wired in. */ admin?: SkillAdmin + /** Multi-source skill tree (sources, categories, per-group switches). */ + groups?: SkillGroupManager + /** Skill market providers (search / detail / resolve artifact). */ + market?: MarketService + /** The sync engine itself, for conflict details and interactive resolution. */ + sync?: SkillSync + /** Update / rollback over a recorded git or market source. */ + updates?: SkillUpdater + /** Cross-software / workspace inventory (v1.1 §15). */ + clients?: ClientRegistry +} + +/** One feature flag the card reads before it renders a control. */ +export interface CapabilityReport { + version: string + features: { + connection: boolean + identitySwitch: boolean + skills: boolean + skillInstallPlans: boolean + skillUninstallPlan: boolean + skillsSync: boolean + skillsMarketplace: boolean + /** Market search/detail/install is wired to a real provider. */ + marketProviders?: number + /** Update/rollback is available for skills with a source record. */ + skillUpdate?: boolean + /** Interactive sync conflict resolution is available. */ + syncResolve?: boolean + /** Cross-software and workspace skill inventory is available. */ + multiClient?: boolean + /** dsh-brain data migration status (package rename stays a deploy step). */ + brainMigration?: 'ready' | 'pending-deploy' + mcpBridge: 'host-plugin' | 'unavailable' + } + notes: string[] +} + +/** What this build actually implements, stated per feature. */ +function capabilities(deps: RouteDeps): CapabilityReport { + const marketProviders = deps.market?.list().length ?? 0 + return { + version: deps.version, + features: { + connection: true, + identitySwitch: true, + skills: deps.admin !== undefined, + skillInstallPlans: deps.admin !== undefined, + skillUninstallPlan: deps.admin !== undefined, + skillsSync: deps.skills !== undefined, + skillsMarketplace: deps.market !== undefined, + marketProviders, + skillUpdate: deps.updates !== undefined, + syncResolve: deps.sync !== undefined, + multiClient: deps.clients !== undefined, + brainMigration: 'ready', + // The MCP bridge is provided by the host `dsh-mcp-connector` plugin over + // the same origin; the browser half probes it at runtime. + mcpBridge: 'host-plugin', + }, + notes: [ + ...(deps.market === undefined + ? ['技能市场未启用:没有配置市场来源。'] + : [`技能市场已接入 ${marketProviders} 个来源;安装走暂存计划并固定提交。`]), + ...(deps.updates === undefined + ? ['技能更新/回退未启用。'] + : ['更新/回退仅对带来源记录(.skill-source.json)的技能生效,更新前自动备份。']), + ...(deps.sync === undefined ? [] : ['同步冲突可在技能页签逐项“保留本地 / 采用云端 / 手动合并”。']), + ...(deps.clients === undefined ? [] : ['跨软件与工作区技能清单为只读(DSH 之外不写目录);dsh-brain 数据迁移已就绪,实际改名需部署重启。']), + 'MCP 管理由宿主 dsh-mcp-connector 插件提供,本卡通过其公开接口读写,不另建凭据库。', + 'MCP 工具可见性与会话级激活分属两层:本卡展示连接与工具缓存,会话级“选择精确工具后激活”由宿主 session-tools 负责,本卡不伪造激活。', + ], + } } /** Collect a request body as UTF-8 text. */ @@ -95,17 +187,75 @@ function fail(res: ServerResponse, status: number, message: string, asHtml = fal res.end(text) } +/** + * Read one JSON object body, or answer the request and return undefined. + * + * The method, origin and content-type gates live here so every write route + * cannot forget one of them. + */ +async function jsonBody(req: IncomingMessage, res: ServerResponse): Promise | undefined> { + if (req.method !== 'POST' && req.method !== 'PUT') { + fail(res, 405, 'method-not-allowed') + return undefined + } + if (!sameOrigin(req)) { + fail(res, 403, 'origin-rejected') + return undefined + } + const type = (req.headers['content-type'] ?? '').toLowerCase() + if (!type.startsWith('application/json')) { + fail(res, 415, 'json-required') + return undefined + } + let parsed: unknown + try { + parsed = JSON.parse(await readBody(req)) + } catch (error) { + fail(res, 400, error instanceof Error && error.message === 'request-too-large' ? 'request-too-large' : 'invalid-json') + return undefined + } + if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) { + fail(res, 400, 'invalid-body') + return undefined + } + return parsed as Record +} + +/** One credential check as the card renders it. */ +type CheckState = 'ok' | 'unconfigured' | 'invalid' | 'forbidden' | 'unreachable' | 'error' + +interface CheckItem { + id: string + label: string + state: CheckState + message: string +} + +/** Last test run, kept in memory so the card can show "最后检查". */ +let lastCheck: { at: number; checks: CheckItem[] } | null = null + +/** + * Uninstall confirmations. + * + * A preview issues a single-use token bound to one folder; the delete route + * refuses without it, so a stray click cannot remove a skill. + */ +const uninstallTokens = new Map() +const UNINSTALL_TOKEN_TTL_MS = 10 * 60_000 + /** The payload the page and the client card both consume. */ function statusPayload(deps: RouteDeps): Record { - const view = deps.store.describe() - const tokens: Record<'activity' | 'memory' | 'summary', TokenView> = { - activity: view.activityToken, - memory: view.memoryToken, - summary: view.summaryToken, + const view: SettingsView = deps.store.describe() + const tokens = { + activity: view.activityToken as TokenView, + memory: view.memoryToken as TokenView, + summary: view.summaryToken as TokenView, } const settings = { enabled: view.enabled, baseUrl: view.baseUrl, + baseUrlConfigured: view.baseUrlConfigured, + baseUrlOrigin: view.baseUrlOrigin, clientId: view.clientId, requestTimeoutMs: view.requestTimeoutMs, defaultK: view.defaultK, @@ -115,7 +265,13 @@ function statusPayload(deps: RouteDeps): Record { skillsDir: view.skillsDir, } return { - ok: true, settings, tokens, settingsPath: view.statePath, reporter: deps.reporter.snapshot(), + ok: true, + settings, + tokens, + settingsPath: view.statePath, + reporter: deps.reporter.snapshot(), + lastCheck, + capabilities: capabilities(deps), ...(deps.skills === undefined ? {} : { skills: deps.skills() }), } } @@ -145,6 +301,15 @@ function serveStatus(deps: RouteDeps, req: IncomingMessage, res: ServerResponse) json(res, 200, statusPayload(deps)) } +/** `GET /dsh-brain/capabilities`. */ +function serveCapabilities(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): void { + if (req.method !== 'GET') { + fail(res, 405, 'method-not-allowed') + return + } + json(res, 200, { ok: true, ...capabilities(deps) }) +} + /** `PUT /dsh-brain/config`. */ async function serveConfig(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { if (req.method !== 'PUT' && req.method !== 'POST') { @@ -176,10 +341,21 @@ async function serveConfig(deps: RouteDeps, req: IncomingMessage, res: ServerRes fail(res, 400, result.error ?? 'invalid-patch') return } - json(res, 200, statusPayload(deps)) + json(res, 200, { + ...statusPayload(deps), + ...(result.clearedTokens === undefined ? {} : { credentialsCleared: result.clearedTokens }), + }) } -/** `POST /dsh-brain/test` — validate both tokens without recording anything. */ +/** Map one failed credential probe onto a check state. */ +function checkState(error: { status: number; code: string }): CheckState { + if (error.status === 0) return 'unreachable' + if (error.status === 401) return 'invalid' + if (error.status === 403) return 'forbidden' + return 'error' +} + +/** `POST /dsh-brain/test` — read-only checks, one per credential. */ async function serveTest(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed') @@ -190,40 +366,68 @@ async function serveTest(deps: RouteDeps, req: IncomingMessage, res: ServerRespo return } const client = deps.client() + const checks: CheckItem[] = [] + const settings = deps.store.settings() + + const reach = await client.reach() + checks.push({ + id: 'network', + label: '服务地址', + state: reach.ok ? 'ok' : 'unreachable', + message: deps.store.describe().baseUrlConfigured === false ? '服务地址还是打包默认值,请先填写真实地址。' : reach.message, + }) + const activityToken = deps.store.activityToken() + if (activityToken === '') { + checks.push({ id: 'activity', label: '活动上报权限(activity:write)', state: 'unconfigured', message: '未配置令牌' }) + } else { + const probe = await client.probeActivity(activityToken) + checks.push({ + id: 'activity', + label: '活动上报权限(activity:write)', + state: probe.ok ? 'ok' : checkState(probe.error), + message: probe.ok ? '鉴权通过(只验证权限,不代表已写入一条上报)' : describeError(probe.error), + }) + } + const memoryToken = deps.store.memoryToken() - - const activity: { ok: boolean; message: string } = activityToken === '' - ? { ok: false, message: '还没填令牌:把 activity:write 密钥粘到下面「活动上报令牌」再点保存' } - : await (async () => { - const probe = await client.probeActivity(activityToken) - return probe.ok - ? { ok: true, message: '令牌有效(鉴权通过)' } - : { ok: false, message: describeError(probe.error) } - })() - - const memory: { ok: boolean; message: string } = memoryToken === '' - ? { ok: false, message: '还没填令牌:把 memory:write 密钥粘到下面「记忆读写令牌」再点保存' } - : await (async () => { - const probe = await client.projects() - return probe.ok - ? { ok: true, message: `令牌有效(${(probe.data.projects ?? []).length} 个项目)` } - : { ok: false, message: describeError(probe.error) } - })() + if (memoryToken === '') { + checks.push({ id: 'memory', label: '记忆读写权限(memory:write)', state: 'unconfigured', message: '未配置令牌' }) + } else { + const probe = await client.projects() + checks.push({ + id: 'memory', + label: '记忆读写权限(memory:write)', + state: probe.ok ? 'ok' : checkState(probe.error), + message: probe.ok ? `鉴权通过(${(probe.data.projects ?? []).length} 个项目)` : describeError(probe.error), + }) + } const summaryToken = deps.store.summaryToken() - const summary = !deps.store.settings().summaryEnabled - ? { ok: false, message: '自动摘要已关闭(可在下方勾选开启)' } - : summaryToken === '' - ? { ok: false, message: '未配置摘要令牌:留空则不做自动摘要' } - : await (async () => { - const probe = await client.probeSummary(summaryToken) - return probe.ok - ? { ok: true, message: '摘要令牌有效(只进待确认队列)' } - : { ok: false, message: describeError(probe.error) } - })() + if (!settings.summaryEnabled) { + checks.push({ id: 'summary', label: '摘要权限(summary:write)', state: 'unconfigured', message: '自动摘要已关闭' }) + } else if (summaryToken === '') { + checks.push({ id: 'summary', label: '摘要权限(summary:write)', state: 'unconfigured', message: '未配置摘要令牌(可选)' }) + } else { + const probe = await client.probeSummary(summaryToken) + checks.push({ + id: 'summary', + label: '摘要权限(summary:write)', + state: probe.ok ? 'ok' : checkState(probe.error), + message: probe.ok ? '鉴权通过(只进待确认队列)' : describeError(probe.error), + }) + } - json(res, 200, { ok: true, activity, memory, summary }) + lastCheck = { at: Date.now(), checks } + json(res, 200, { + ok: true, + checkedAt: lastCheck.at, + checks, + // Kept for the older card/page shape; both views read the same probes. + activity: { ok: checks[1]?.state === 'ok', message: checks[1]?.message ?? '' }, + memory: { ok: checks[2]?.state === 'ok', message: checks[2]?.message ?? '' }, + summary: { ok: checks[3]?.state === 'ok', message: checks[3]?.message ?? '' }, + }) } /** `POST /dsh-brain/flush`. */ @@ -240,21 +444,21 @@ async function serveFlush(deps: RouteDeps, req: IncomingMessage, res: ServerResp json(res, 200, { ok: true, ...result }) } -/** Labels attached to the two tokens this plugin asks the dashboard to mint. */ +/** Labels attached to the tokens this plugin asks the dashboard to mint. */ const TOKEN_LABELS = { activity: 'DSH desktop 活动上报', memory: 'DSH desktop 记忆读写', summary: 'DSH desktop 对话摘要', -} as const +} /** * `POST /dsh-brain/login` — sign in with the cloud account and exchange it - * for the two scoped tokens. + * for the scoped tokens. * * The password is used once, in memory, and never written anywhere: the flow is - * password → short-lived session cookie → two `cmt_…` tokens → cookie dropped. - * Only the tokens are persisted, so a later password change does not break the - * plugin and revoking a token does not reveal the password. + * password → short-lived session cookie → tokens → cookie dropped. Only the + * tokens are persisted, so a later password change does not break the plugin + * and revoking a token does not reveal the password. */ async function serveLogin(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { if (req.method !== 'POST') { @@ -297,7 +501,6 @@ async function serveLogin(deps: RouteDeps, req: IncomingMessage, res: ServerResp json(res, 400, { ok: false, error: '用户名或密码过长' }) return } - const client = deps.client() const session = await client.login(username, password) if (!session.ok) { @@ -305,7 +508,6 @@ async function serveLogin(deps: RouteDeps, req: IncomingMessage, res: ServerResp json(res, status, { ok: false, error: describeError(session.error) }) return } - const activity = await client.mintToken(session.data.sessionCookie, 'activity:write', TOKEN_LABELS.activity) const memory = await client.mintToken(session.data.sessionCookie, 'memory:write', TOKEN_LABELS.memory) const summary = await client.mintToken(session.data.sessionCookie, 'summary:write', TOKEN_LABELS.summary) @@ -315,8 +517,7 @@ async function serveLogin(deps: RouteDeps, req: IncomingMessage, res: ServerResp json(res, 400, { ok: false, error: `登录成功,但创建令牌失败:${reason}` }) return } - - const patch: Parameters[0] = {} + const patch: Record = {} if (activity.ok && typeof activity.data.token === 'string') patch.activityToken = activity.data.token if (memory.ok && typeof memory.data.token === 'string') patch.memoryToken = memory.data.token if (summary.ok && typeof summary.data.token === 'string') patch.summaryToken = summary.data.token @@ -325,7 +526,6 @@ async function serveLogin(deps: RouteDeps, req: IncomingMessage, res: ServerResp json(res, 500, { ok: false, error: applied.error }) return } - const failed = activity.ok ? memory : activity const partial = !activity.ok || !memory.ok json(res, 200, { @@ -345,35 +545,66 @@ async function serveLogin(deps: RouteDeps, req: IncomingMessage, res: ServerResp }) } - /** HTTP status for one skill-admin failure code. */ function adminStatus(code: string): number { switch (code) { case 'not_found': return 404 case 'invalid_name': - case 'invalid_url': return 400 + case 'invalid_url': + case 'invalid_plan': return 400 case 'collision': - case 'sync_running': return 409 + case 'sync_running': + case 'plan_expired': + case 'confirmation_required': + case 'no_source': + case 'up_to_date': + case 'stale_plan': + case 'unsupported': return 409 + case 'no_backup': return 404 case 'no_skills_found': return 422 case 'git_failed': return 502 default: return 500 } } +/** Issue the single-use token a delete needs. */ +function issueUninstallToken(folder: string): string { + const token = `uninstall_${randomUUID()}` + uninstallTokens.set(folder, { token, expiresAt: Date.now() + UNINSTALL_TOKEN_TTL_MS }) + return token +} + +/** Consume one uninstall token; false when it is missing, stale or foreign. */ +function useUninstallToken(folder: string, token: unknown): boolean { + const record = uninstallTokens.get(folder) + if (record === undefined) return false + if (typeof token !== 'string' || token !== record.token) return false + if (record.expiresAt < Date.now()) { + uninstallTokens.delete(folder) + return false + } + uninstallTokens.delete(folder) + return true +} + /** * The skill management surface, one prefix route: - * `GET /dsh-brain/skills` the management page - * `GET /dsh-brain/skills/api` inventory JSON (skills + trash) - * `POST /dsh-brain/skills/toggle` { folder, enabled } - * `POST /dsh-brain/skills/remove` { folder } - * `POST /dsh-brain/skills/install` { url } + * `GET /dsh-brain/skills` the management page + * `GET /dsh-brain/skills/api` inventory JSON (skills + plans) + * `POST /dsh-brain/skills/detail` { folder } — one skill with SKILL.md + * `POST /dsh-brain/skills/toggle` { folder, enabled } + * `POST /dsh-brain/skills/install/plan` { url } — stage the repository + * `POST /dsh-brain/skills/install/commit` { planId, folders } + * `POST /dsh-brain/skills/install/cancel` { planId } + * `POST /dsh-brain/skills/uninstall/plan` { folder } — impact preview + * `POST /dsh-brain/skills/remove` { folder, token, confirmed: true } */ async function serveSkills(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { - if (deps.admin === undefined) { + const admin = deps.admin + if (admin === undefined) { fail(res, 404, 'skills-admin-unavailable') return } - const admin = deps.admin const suffix = (req.url ?? '').split('?')[0]?.slice(ROUTES.skills.length) ?? '' if (suffix === '' || suffix === '/') { if (req.method !== 'GET' && req.method !== 'HEAD') { @@ -395,56 +626,375 @@ async function serveSkills(deps: RouteDeps, req: IncomingMessage, res: ServerRes fail(res, 405, 'method-not-allowed') return } - json(res, 200, { ok: true, ...(await admin.list()) }) + const inventory = await admin.list() + json(res, 200, { ok: true, ...inventory, ...(await admin.listPlans()) }) return } - const actions: Record) => Promise>> = { - '/toggle': async (body) => admin.setEnabled(body.folder, body.enabled === true), - '/remove': async (body) => admin.remove(body.folder), - '/install': async (body) => admin.installFromGit(body.url), + if (suffix === '/sources') { + if (req.method !== 'GET') { + fail(res, 405, 'method-not-allowed') + return + } + const groups = deps.groups + if (groups === undefined) { + fail(res, 404, 'skill-groups-unavailable') + return + } + json(res, 200, { ok: true, ...(await groups.overview()) }) + return } - const action = actions[suffix] + + type Handler = (body: Record) => Promise> + const actions: Record = { + '/detail': async (body) => ({ ...(await admin.detail(body.folder)) }), + '/toggle': async (body) => ({ ...(await admin.setEnabled(body.folder, body.enabled === true)) }), + // One-shot install kept for the standalone page; the card uses plan/commit. + '/install': async (body) => ({ ...(await admin.installFromGit(body.url)) }), + '/install/plan': async (body) => ({ ...(await admin.prepareInstall(body.url)) }), + '/install/commit': async (body) => ({ ...(await admin.commitInstall({ planId: body.planId, folders: body.folders })) }), + '/install/cancel': async (body) => ({ ...(await admin.cancelInstall({ planId: body.planId })) }), + '/uninstall/plan': async (body) => { + const plan = await admin.uninstallPlan(body.folder) + return { ...plan, token: issueUninstallToken(plan.folder) } + }, + '/remove': async (body) => { + if (body.confirmed !== true) { + throw new SkillAdminError('confirmation_required', '删除必须显式确认:先预览影响,再传 confirmed=true。') + } + const folder = typeof body.folder === 'string' ? body.folder : '' + if (!useUninstallToken(folder, body.token)) { + throw new SkillAdminError('confirmation_required', '卸载确认已过期或与该技能不匹配,请重新预览。') + } + return { ...(await admin.remove(folder)) } + }, + } + // Update/rollback operate on skills with a `.skill-source.json` record. They + // are optional: without the updater wired in the routes answer 404. + const updateActions: Record = { + '/update/check': async (body) => ({ ...(await requireUpdates(deps).checkUpdate(body.folder)) }), + '/update/apply': async (body) => ({ ...(await requireUpdates(deps).applyUpdate(body.folder, body.expectedCommit)) }), + '/rollback': async (body) => ({ ...(await requireUpdates(deps).rollback(body.folder)) }), + } + // Category and source switches live in the same prefix so the card has one + // base URL; they are grouped here because they never touch a skill folder + // directly (except the DSH park/unpark the group manager delegates). + const groupActions: Record = { + '/groups/create': async (body) => ({ ...(await requireGroups(deps).createGroup(body.sourceId, body.name)) }), + '/groups/rename': async (body) => ({ ...(await requireGroups(deps).renameGroup(body.sourceId, body.groupId, body.name)) }), + '/groups/delete': async (body) => ({ ...(await requireGroups(deps).deleteGroup(body.sourceId, body.groupId)) }), + '/groups/assign': async (body) => ({ ...(await requireGroups(deps).assign(body.sourceId, body.groupId, body.folders)) }), + '/groups/toggle': async (body) => ({ ...(await requireGroups(deps).toggleGroup(body.sourceId, body.groupId, body.enabled === true)) }), + '/source/toggle': async (body) => ({ ...(await requireGroups(deps).toggleSource(body.sourceId, body.enabled === true)) }), + '/source/reveal': async (body) => ({ ...(await requireGroups(deps).reveal(body.sourceId)) }), + } + const action = actions[suffix] ?? groupActions[suffix] ?? updateActions[suffix] if (action === undefined) { fail(res, 404, 'unknown-skill-route') return } - if (req.method !== 'POST') { - fail(res, 405, 'method-not-allowed') - return - } - if (!sameOrigin(req)) { - fail(res, 403, 'origin-rejected') - return - } - const type = (req.headers['content-type'] ?? '').toLowerCase() - if (!type.startsWith('application/json')) { - fail(res, 415, 'json-required') - return - } - let body: unknown + const body = await jsonBody(req, res) + if (body === undefined) return try { - body = JSON.parse(await readBody(req)) - } catch { - fail(res, 400, 'invalid-json') - return - } - if (body === null || typeof body !== 'object' || Array.isArray(body)) { - fail(res, 400, 'invalid-body') - return - } - try { - json(res, 200, { ok: true, ...(await action(body as Record)) }) + json(res, 200, { ok: true, ...(await action(body)) }) } catch (error) { if (error instanceof SkillAdminError) { json(res, adminStatus(error.code), { ok: false, code: error.code, error: error.message }) - } else { - json(res, 500, { ok: false, code: 'internal', error: error instanceof Error ? error.message : String(error) }) + return } + if (error instanceof SkillGroupError) { + json(res, groupStatus(error.code), { ok: false, code: error.code, error: error.message }) + return + } + json(res, 500, { ok: false, code: 'internal', error: error instanceof Error ? error.message : String(error) }) } } +/** The group manager is optional; the routes answer 404 when it is absent. */ +function requireGroups(deps: RouteDeps): SkillGroupManager { + if (deps.groups === undefined) throw new SkillGroupError('unavailable', '技能分组功能未启用。') + return deps.groups +} + +/** HTTP status for one group failure code. */ +function groupStatus(code: string): number { + switch (code) { + case 'not_found': + case 'unknown_source': return 404 + case 'invalid_name': + case 'duplicate': + case 'too_many': return 400 + case 'unavailable': return 503 + default: return 500 + } +} + +/** The updater is optional; its routes answer 404 when it is absent. */ +function requireUpdates(deps: RouteDeps): SkillUpdater { + if (deps.updates === undefined) throw new SkillAdminError('unsupported', '技能更新/回退未启用。') + return deps.updates +} + +/** HTTP status for one market failure code. */ +function marketStatus(code: string): number { + switch (code) { + case 'unknown_provider': + case 'invalid_manifest': return 400 + case 'not_found': return 404 + case 'unreachable': + case 'unavailable': return 502 + default: + return code.startsWith('http_') ? 502 : 500 + } +} + +/** + * Market surface, one prefix route: + * `GET /dsh-brain/market/providers` configured sources + * `GET /dsh-brain/market/search` ?provider=&q=&cursor= + * `POST /dsh-brain/market/detail` { provider, id } + * `POST /dsh-brain/market/plan` { provider, id } — pin commit + stage a plan + * `POST /dsh-brain/market/commit` { planId, folders } + * `POST /dsh-brain/market/cancel` { planId } + */ +async function serveMarket(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { + const market = deps.market + if (market === undefined) { + fail(res, 404, 'market-unavailable') + return + } + const url = new URL(req.url ?? '/', 'http://localhost') + const suffix = url.pathname.slice(ROUTES.market.length) + try { + if (suffix === '' || suffix === '/' || suffix === '/providers') { + if (req.method !== 'GET') { fail(res, 405, 'method-not-allowed'); return } + json(res, 200, { ok: true, providers: market.list() }) + return + } + if (suffix === '/search') { + if (req.method !== 'GET') { fail(res, 405, 'method-not-allowed'); return } + const providerId = url.searchParams.get('provider') ?? market.list()[0]?.id ?? '' + const query = url.searchParams.get('q') ?? '' + const cursorRaw = Number(url.searchParams.get('cursor') ?? '0') + const cursor = Number.isFinite(cursorRaw) && cursorRaw > 0 ? cursorRaw : 0 + const result = await market.search(providerId, query, cursor) + json(res, 200, { ok: true, provider: market.get(providerId).info(), ...result }) + return + } + type Handler = (body: Record) => Promise> + const admin = deps.admin + const requireAdmin = (): SkillAdmin => { + if (admin === undefined) throw new MarketError('unavailable', '技能管理未启用,无法安装。') + return admin + } + const actions: Record = { + '/detail': async (body) => ({ ...(await market.detail(String(body.provider ?? ''), String(body.id ?? ''))) }), + '/plan': async (body) => { + const providerId = String(body.provider ?? '') + const id = String(body.id ?? '') + const artifact = await market.resolve(providerId, id, typeof body.version === 'string' ? body.version : undefined) + const plan = await requireAdmin().prepareInstall(artifact.repoUrl, { + ref: artifact.ref, + source: { + provider: providerId, + entryId: id, + ...(artifact.version === undefined ? {} : { version: artifact.version }), + }, + }) + const wanted = new Set(artifact.folders) + return { + planId: plan.planId, + ...(plan.commit === undefined ? {} : { commit: plan.commit }), + artifact, + candidates: plan.candidates.filter((candidate) => wanted.has(candidate.folder)), + totalInRepo: plan.candidates.length, + } + }, + '/commit': async (body) => ({ ...(await requireAdmin().commitInstall({ planId: body.planId, folders: body.folders })) }), + '/cancel': async (body) => ({ ...(await requireAdmin().cancelInstall({ planId: body.planId })) }), + } + const action = actions[suffix] + if (action === undefined) { fail(res, 404, 'unknown-market-route'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + json(res, 200, { ok: true, ...(await action(body)) }) + } catch (error) { + if (error instanceof MarketError) { + json(res, marketStatus(error.code), { ok: false, code: error.code, error: error.message }) + return + } + if (error instanceof SkillAdminError) { + json(res, adminStatus(error.code), { ok: false, code: error.code, error: error.message }) + return + } + json(res, 500, { ok: false, code: 'internal', error: error instanceof Error ? error.message : String(error) }) + } +} + +/** + * Sync surface: + * `GET /dsh-brain/sync/status` latest stats + both sides of each conflict + * `POST /dsh-brain/sync/run` trigger one pass now + * `POST /dsh-brain/sync/resolve` { name, action: keep_local | take_cloud | manual } + */ +async function serveSync(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { + const sync = deps.sync + if (sync === undefined) { + fail(res, 404, 'sync-unavailable') + return + } + const url = new URL(req.url ?? '/', 'http://localhost') + const suffix = url.pathname.slice(ROUTES.sync.length) + try { + if (suffix === '' || suffix === '/' || suffix === '/status') { + if (req.method !== 'GET') { fail(res, 405, 'method-not-allowed'); return } + json(res, 200, { ok: true, stats: sync.stats(), conflicts: await sync.conflictDetails() }) + return + } + if (suffix === '/run') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const stats = await sync.syncOnce() + json(res, 200, { ok: true, stats, conflicts: await sync.conflictDetails() }) + return + } + if (suffix === '/resolve') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + const result = await sync.resolveConflict(body.name, body.action) + json(res, 200, { ok: true, ...result, conflicts: await sync.conflictDetails() }) + return + } + fail(res, 404, 'unknown-sync-route') + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + const known = ['invalid_name', 'invalid_action', 'no_local_copy', 'no_cloud_copy', 'stale_revision', 'sync_running', 'manifest_mismatch'] + const code = known.includes(message) ? message : 'internal' + const status = code === 'internal' ? 500 + : code === 'invalid_name' || code === 'invalid_action' ? 400 + : 409 + json(res, status, { ok: false, code, error: message }) + } +} + +/** Cross-software / workspace inventory routes. */ +async function serveClients(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { + const registry = deps.clients + if (registry === undefined) { + fail(res, 404, 'clients-unavailable') + return + } + const url = new URL(req.url ?? '/', 'http://localhost') + const suffix = url.pathname.slice(ROUTES.clients.length) + try { + if (suffix === '' || suffix === '/' || suffix === '/overview') { + if (req.method !== 'GET') { fail(res, 405, 'method-not-allowed'); return } + json(res, 200, { ok: true, ...(await registry.overview()) }) + return + } + if (suffix === '/inventory') { + if (req.method !== 'GET') { fail(res, 405, 'method-not-allowed'); return } + const result = await registry.inventory( + url.searchParams.get('software'), + url.searchParams.get('scope'), + url.searchParams.get('workspaceId') ?? undefined, + ) + json(res, 200, { ok: true, ...result }) + return + } + if (suffix === '/workspaces/add') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + const workspace = await registry.addWorkspace(body.software, body.path, body.name) + json(res, 200, { ok: true, workspace }) + return + } + if (suffix === '/workspaces/remove') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + json(res, 200, { ok: true, ...(await registry.removeWorkspace(body.software, body.id)) }) + return + } + if (suffix === '/workspace/inventory') { + if (req.method !== 'GET') { fail(res, 405, 'method-not-allowed'); return } + const { admin, workspace } = await registry.workspaceSkillAdmin(url.searchParams.get('software'), url.searchParams.get('workspaceId')) + json(res, 200, { ok: true, workspace, ...(await admin.list()) }) + return + } + if (suffix === '/workspace/detail') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + const { admin } = await registry.workspaceSkillAdmin(body.software, body.workspaceId) + json(res, 200, { ok: true, ...(await admin.detail(body.folder)) }) + return + } + if (suffix === '/workspace/toggle') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + const { admin } = await registry.workspaceSkillAdmin(body.software, body.workspaceId) + json(res, 200, { ok: true, ...(await admin.setEnabled(body.folder, body.enabled === true)) }) + return + } + if (suffix === '/workspace/remove') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + if (body.confirmed !== true) throw new SkillAdminError('confirmation_required', '工作区技能删除必须显式 confirmed:true。') + const { admin } = await registry.workspaceSkillAdmin(body.software, body.workspaceId) + json(res, 200, { ok: true, ...(await admin.remove(body.folder)) }) + return + } + if (suffix === '/workspace/install/plan') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + const { admin } = await registry.workspaceSkillAdmin(body.software, body.workspaceId) + json(res, 200, { ok: true, ...(await admin.prepareInstall(body.url)) }) + return + } + if (suffix === '/workspace/install/commit') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + const { admin } = await registry.workspaceSkillAdmin(body.software, body.workspaceId) + json(res, 200, { ok: true, ...(await admin.commitInstall({ planId: body.planId, folders: body.folders })) }) + return + } + fail(res, 404, 'unknown-clients-route') + } catch (error) { + if (error instanceof SkillAdminError) { + json(res, adminStatus(error.code), { ok: false, code: error.code, error: error.message }) + return + } + const message = error instanceof Error ? error.message : String(error) + const known = ['invalid_software', 'invalid_path', 'duplicate', 'not_found', 'unknown_workspace', 'unsupported_workspace_write'] + const code = known.includes(message) ? message : 'internal' + const status = code === 'internal' ? 500 + : code === 'not_found' || code === 'unknown_workspace' ? 404 + : code === 'duplicate' ? 409 + : code === 'unsupported_workspace_write' ? 501 : 400 + json(res, status, { ok: false, code, error: message }) + } +} + +/** dsh-brain data migration: GET status, POST run. */ +async function serveMigrate(_deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { + const url = new URL(req.url ?? '/', 'http://localhost') + const suffix = url.pathname.slice(ROUTES.migrate.length) + if (suffix === '' || suffix === '/' || suffix === '/brain') { + if (req.method === 'GET') { const status = await brainStatus(); json(res, 200, { ok: true, ...status }); return } + if (req.method === 'POST') { const result = await migrateLegacyFiles(); json(res, 200, { ok: true, ...result }); return } + fail(res, 405, 'method-not-allowed') + return + } + fail(res, 404, 'unknown-migrate-route') +} + /** Build the exact-route table registered with the host web server. */ -/** Pre-rename aliases (v0.5.0): the same handlers under the old `/cloud-memory` prefix. */ +/** Pre-rename aliases (0.8.0): the same handlers under the old `/cloud-memory` prefix. */ export function createLegacyRoutes(deps: RouteDeps): CloudMemoryWebRoute[] { return createRoutes(deps).map((route) => ({ ...route, path: route.path.replace('/dsh-brain', '/cloud-memory') })) } @@ -461,6 +1011,11 @@ export function createRoutes(deps: RouteDeps): CloudMemoryWebRoute[] { path: ROUTES.status, handler: (req, res) => serveStatus(deps, req, res), }, + { + kind: 'exact', + path: ROUTES.capabilities, + handler: (req, res) => serveCapabilities(deps, req, res), + }, { kind: 'exact', path: ROUTES.config, @@ -486,5 +1041,25 @@ export function createRoutes(deps: RouteDeps): CloudMemoryWebRoute[] { path: ROUTES.skills, handler: (req, res) => serveSkills(deps, req, res), }, + { + kind: 'prefix', + path: ROUTES.market, + handler: (req, res) => serveMarket(deps, req, res), + }, + { + kind: 'prefix', + path: ROUTES.sync, + handler: (req, res) => serveSync(deps, req, res), + }, + { + kind: 'prefix', + path: ROUTES.clients, + handler: (req, res) => serveClients(deps, req, res), + }, + { + kind: 'prefix', + path: ROUTES.migrate, + handler: (req, res) => serveMigrate(deps, req, res), + }, ] }