refactor(settings-ui): extract the settings-ui bundle behind its own routes and page

This commit is contained in:
newbie 2026-11-23 16:19:29 +08:00
parent 9f5daaf266
commit dd5d58659e
9 changed files with 346 additions and 77 deletions

View File

@ -119,6 +119,11 @@ window.__ModuleLoader__.load({
return fallback === undefined ? statusText(result.status) : fallback
}
// ---- settings-ui bundle (configuration surface) ----
// pickSettings + the SettingsCard draft/save/login/test/flush logic and the
// 连接与上报 tab belong to the settings-ui bundle; the skills/mcp tabs
// belong to their own bundles. The card only talks to the public HTTP API —
// it never touches SettingsStore/CloudClient/Reporter/SkillSync directly.
function pickSettings(settings) {
var source = settings === undefined || settings === null ? {} : settings
return {
@ -130,6 +135,9 @@ window.__ModuleLoader__.load({
requestTimeoutMs: String(source.requestTimeoutMs === undefined ? 8000 : source.requestTimeoutMs),
defaultK: String(source.defaultK === undefined ? 10 : source.defaultK),
summaryEnabled: source.summaryEnabled === true,
activityEnabled: source.activityEnabled === true,
memoryEnabled: source.memoryEnabled === true,
skillsDir: String(source.skillsDir === undefined ? '' : source.skillsDir),
skillsSyncEnabled: source.skillsSyncEnabled === true,
}
}
@ -1704,6 +1712,10 @@ window.__ModuleLoader__.load({
requestTimeoutMs: Number(draft.requestTimeoutMs),
defaultK: Number(draft.defaultK),
summaryEnabled: draft.summaryEnabled,
activityEnabled: draft.activityEnabled,
memoryEnabled: draft.memoryEnabled,
skillsSyncEnabled: draft.skillsSyncEnabled,
skillsDir: String(draft.skillsDir).trim(),
}
if (tokenDraft.activity.trim() !== '') payload.activityToken = tokenDraft.activity.trim()
if (tokenDraft.memory.trim() !== '') payload.memoryToken = tokenDraft.memory.trim()
@ -2451,15 +2463,7 @@ window.__ModuleLoader__.load({
patchDraft({ requestTimeoutMs: value })
},
}),
h(Field, {
label: '单次搜索返回上限',
type: 'number',
value: draft.defaultK,
hint: '只限制一次搜索返回多少条,不限制可搜索的历史范围。默认 10。',
onChange: function (value) {
patchDraft({ defaultK: value })
},
}),
),
h(
'div',
@ -2487,6 +2491,19 @@ window.__ModuleLoader__.load({
}),
h('label', { htmlFor: 'dsh-brain-summary', style: styles.label }, '启用自动摘要(会把该轮对话文本上传为待确认提案;与上面的元数据上报是两件事)'),
),
h(
'div',
{ style: styles.checkboxRow },
h('input', {
type: 'checkbox',
checked: draft.activityEnabled,
id: 'dsh-brain-activity',
onChange: function (event) {
patchDraft({ activityEnabled: event.target.checked })
},
}),
h('label', { htmlFor: 'dsh-brain-activity', style: styles.label }, '启用活动上报(细粒度开关;未显式设置时跟随总开关)'),
),
)
: h(
'div',
@ -2495,6 +2512,72 @@ window.__ModuleLoader__.load({
),
),
h(
Section,
{
title: '记忆配置',
hint: '只影响记忆工具:开关未显式设置时跟随总开关,检索上限只限制单次返回条数。',
},
h('div', { style: styles.grid },
h(Field, {
label: '单次搜索返回上限(defaultK)',
type: 'number',
value: draft.defaultK,
hint: '只限制一次搜索返回多少条,不限制可搜索的历史范围。默认 10。',
onChange: function (value) {
patchDraft({ defaultK: value })
},
}),
),
h('div', { style: styles.checkboxRow },
h('input', {
type: 'checkbox',
checked: draft.memoryEnabled,
id: 'dsh-brain-memory-enabled',
onChange: function (event) {
patchDraft({ memoryEnabled: event.target.checked })
},
}),
h('label', { htmlFor: 'dsh-brain-memory-enabled', style: styles.label }, '启用记忆工具(memory_search / memory_read / memory_add)'),
),
),
features.skillsSync === false
? h('div', null)
: h(
'div',
null,
h(
Section,
{
title: 'Skill 同步',
hint: '同步由后台定时执行,这里只保存开关与目录;目录留空使用默认位置。',
},
h(Field, {
label: '技能目录(skillsDir,可选)',
value: draft.skillsDir,
placeholder: '默认 $DSH_HOME/skills',
hint: '保存后下一次同步生效,无需重启。',
onChange: function (value) {
patchDraft({ skillsDir: value })
},
}),
h('div', { style: styles.checkboxRow },
h('input', {
type: 'checkbox',
checked: draft.skillsSyncEnabled,
id: 'dsh-brain-skills-sync',
onChange: function (event) {
patchDraft({ skillsSyncEnabled: event.target.checked })
},
}),
h('label', { htmlFor: 'dsh-brain-skills-sync', style: styles.label }, '启用云端技能同步(下载云端技能库、上传本机新技能)'),
),
),
),
h(
Section,
{

View File

@ -1,4 +1,4 @@
import { renderConfigPage } from "./page.js";
import { createSettingsUiRoutes } from "./settings-ui/routes.js";
import { brainStatus, migrateLegacyFiles } from "./brain-migration.js";
import { mcpCapability } from "./mcp.js";
import { fail, json, readBody, sameOrigin } from "./http.js";
@ -71,6 +71,8 @@ function statusPayload(deps) {
defaultK: view.defaultK,
summaryEnabled: view.summaryEnabled,
summaryMaxChars: view.summaryMaxChars,
activityEnabled: view.activityEnabled,
memoryEnabled: view.memoryEnabled,
skillsSyncEnabled: view.skillsSyncEnabled,
skillsDir: view.skillsDir,
};
@ -85,23 +87,6 @@ function statusPayload(deps) {
...(deps.skills === undefined ? {} : { skills: deps.skills() }),
};
}
/** Serve the configuration page. */
function servePage(deps, req, res) {
if (req.method !== 'GET' && req.method !== 'HEAD') {
fail(res, 405, 'method-not-allowed', true);
return;
}
const html = renderConfigPage({ version: deps.version, settingsPath: deps.store.path });
res.writeHead(200, {
'content-type': 'text/html; charset=utf-8',
'cache-control': 'no-store',
'content-length': Buffer.byteLength(html),
});
if (req.method === 'HEAD')
res.end();
else
res.end(html);
}
/** `GET /dsh-brain/status`. */
function serveStatus(deps, req, res) {
if (req.method !== 'GET') {
@ -183,17 +168,13 @@ export function createLegacyRoutes(deps) {
* Build the exact-route table registered with the host web server.
*
* Composition (m00001 §4): the root owns `/status` and `/capabilities`; the
* cloud-sync bundle returns login/test/flush/sync; the skill bundle returns
* skills/market/clients. No path shadows another, so the grouped registration
* order is safe.
* settings-ui bundle returns the page; the cloud-sync bundle returns
* login/test/flush/sync; the skill bundle returns skills/market/clients. No
* path shadows another, so the grouped registration order is safe.
*/
export function createRoutes(deps) {
return [
{
kind: 'exact',
path: ROUTES.page,
handler: (req, res) => servePage(deps, req, res),
},
...createSettingsUiRoutes({ version: deps.version, store: deps.store }),
{
kind: 'exact',
path: ROUTES.status,

View File

@ -1,11 +1,16 @@
/**
* Standalone configuration page.
* Settings UI bundle — standalone configuration page.
*
* This profile runs with the native settings surface disabled
* (`ui-settings` is turned off in `cordis.patch.yml`), so the plugin ships its
* own HTML page at `/dsh-brain`. It is deliberately dependency-free: no
* bundler assets, no CDN, no fonts — one file the host web server can emit.
*
* The page is a pure view: every control reads and writes through the public
* HTTP API (`GET /status`, `PUT /config`, `POST login/test/flush`) and never
* touches SettingsStore, CloudClient, Reporter or SkillSync directly, so
* removing the settings-ui bundle only loses the page.
*
* The page renders tokens as masked values only; the plaintext never leaves
* `$DSH_HOME/dsh-brain.json`.
*/
@ -229,10 +234,6 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
<label for="requestTimeoutMs">请求超时(毫秒)</label>
<input id="requestTimeoutMs" type="number" min="1000" max="60000" step="500" />
</div>
<div>
<label for="defaultK">默认检索条数</label>
<input id="defaultK" type="number" min="1" max="100" />
</div>
</div>
<div class="toggles">
<label class="toggle" for="enabled">
@ -245,6 +246,11 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
<span class="track"></span>
<span><span class="ttitle">轮次结束后自动生成待确认摘要</span><span class="tdesc">只进入待确认队列,确认后才写云端</span></span>
</label>
<label class="toggle" for="activityEnabled">
<input id="activityEnabled" type="checkbox" />
<span class="track"></span>
<span><span class="ttitle">活动上报开关</span><span class="tdesc">只控制每轮活动上报;未显式设置时跟随总开关</span></span>
</label>
<div class="inline-field">
<label for="summaryMaxChars">单轮摘要字数上限</label>
<input id="summaryMaxChars" type="number" min="1000" max="16000" step="500" />
@ -252,6 +258,43 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
</div>
</section>
<section class="card" id="memoryCard">
<div class="card-head"><span class="chip">🧠</span><h2>记忆配置</h2><span class="desc">memory_* 工具</span></div>
<div class="grid">
<div>
<label for="defaultK">默认检索条数 defaultK</label>
<input id="defaultK" type="number" min="1" max="100" />
<div class="hint">memory_search 未显式指定 k 时的返回上限(1–100)。</div>
</div>
</div>
<div class="toggles">
<label class="toggle" for="memoryEnabled">
<input id="memoryEnabled" type="checkbox" />
<span class="track"></span>
<span><span class="ttitle">启用记忆工具</span><span class="tdesc">未显式设置时跟随总开关</span></span>
</label>
</div>
</section>
<section class="card" id="skillsCard">
<div class="card-head"><span class="chip">🔄</span><h2>Skill 同步</h2><span class="desc">后台定时双向同步</span></div>
<div class="grid">
<div>
<label for="skillsDir">技能目录(可选)</label>
<input id="skillsDir" type="text" spellcheck="false" placeholder="默认 $DSH_HOME/skills" />
<div class="hint">留空使用默认目录;保存后下一次同步生效,无需重启。</div>
</div>
</div>
<div class="toggles">
<label class="toggle" for="skillsSyncEnabled">
<input id="skillsSyncEnabled" type="checkbox" />
<span class="track"></span>
<span><span class="ttitle">启用云端技能同步</span><span class="tdesc">下载云端技能库、上传本机新技能</span></span>
</label>
</div>
<div class="tip">同步动作由后台定时执行(能力缺失时本卡片自动隐藏);这里只保存开关与目录。</div>
</section>
<section class="card">
<div class="card-head"><span class="chip">🔑</span><h2>账号登录</h2><span class="desc">用账号一次性换取令牌,密码不保存</span></div>
<div class="grid">
@ -366,8 +409,14 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
el('requestTimeoutMs').value = settings.requestTimeoutMs || '';
el('defaultK').value = settings.defaultK || '';
el('enabled').checked = settings.enabled === true;
el('activityEnabled').checked = settings.activityEnabled === true;
el('summaryEnabled').checked = settings.summaryEnabled === true;
el('summaryMaxChars').value = settings.summaryMaxChars || '';
el('memoryEnabled').checked = settings.memoryEnabled === true;
el('skillsSyncEnabled').checked = settings.skillsSyncEnabled === true;
el('skillsDir').value = settings.skillsDir || '';
var features = (view.capabilities && view.capabilities.features) || {};
el('skillsCard').style.display = features.skillsSync === false ? 'none' : '';
el('activityTokenHint').innerHTML = '用于 POST /activity/turn。当前 ' +
(tokens.activity && tokens.activity.configured
@ -441,8 +490,12 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
requestTimeoutMs: Number(el('requestTimeoutMs').value),
defaultK: Number(el('defaultK').value),
enabled: el('enabled').checked,
activityEnabled: el('activityEnabled').checked,
summaryEnabled: el('summaryEnabled').checked,
summaryMaxChars: Number(el('summaryMaxChars').value)
summaryMaxChars: Number(el('summaryMaxChars').value),
memoryEnabled: el('memoryEnabled').checked,
skillsSyncEnabled: el('skillsSyncEnabled').checked,
skillsDir: el('skillsDir').value.trim()
};
var activity = el('activityToken').value.trim();
var memory = el('memoryToken').value.trim();

31
lib/settings-ui/routes.js Normal file
View File

@ -0,0 +1,31 @@
import { fail } from "../http.js";
import { renderConfigPage } from "./page.js";
/** Must match `ROUTES.page` in `src/routes.ts`. */
const PAGE_PATH = '/dsh-brain';
/** Serve the configuration page. */
function servePage(deps, req, res) {
if (req.method !== 'GET' && req.method !== 'HEAD') {
fail(res, 405, 'method-not-allowed', true);
return;
}
const html = renderConfigPage({ version: deps.version, settingsPath: deps.store.path });
res.writeHead(200, {
'content-type': 'text/html; charset=utf-8',
'cache-control': 'no-store',
'content-length': Buffer.byteLength(html),
});
if (req.method === 'HEAD')
res.end();
else
res.end(html);
}
/** The exact route the root aggregator registers for the settings page. */
export function createSettingsUiRoutes(deps) {
return [
{
kind: 'exact',
path: PAGE_PATH,
handler: (req, res) => servePage(deps, req, res),
},
];
}

View File

@ -84,10 +84,10 @@ function parseState(raw) {
const state = { version: 1 };
if (typeof source.enabled === 'boolean')
state.enabled = source.enabled;
if (typeof source.activityEnabled === 'boolean')
state.activityEnabled = source.activityEnabled;
if (typeof source.memoryEnabled === 'boolean')
state.memoryEnabled = source.memoryEnabled;
if (typeof source.skillsSyncEnabled === 'boolean')
state.skillsSyncEnabled = source.skillsSyncEnabled;
if (typeof source.skillsDir === 'string')
state.skillsDir = source.skillsDir;
const baseUrl = normalizeBaseUrl(source.baseUrl);
if (baseUrl !== undefined)
state.baseUrl = baseUrl;
@ -188,6 +188,10 @@ export class SettingsStore {
resolved.summaryEnabled = this.state.summaryEnabled;
if (this.state.summaryMaxChars !== undefined)
resolved.summaryMaxChars = this.state.summaryMaxChars;
if (this.state.skillsSyncEnabled !== undefined)
resolved.skillsSyncEnabled = this.state.skillsSyncEnabled;
if (this.state.skillsDir !== undefined)
resolved.skillsDir = this.state.skillsDir;
// The split gates inherit `enabled` unless the config or the sidecar set
// them explicitly, so an old configuration keeps its exact behaviour.
if (this.state.activityEnabled === undefined && this.configExplicit.activityEnabled === undefined) {
@ -302,6 +306,19 @@ export class SettingsStore {
return { ok: false, error: 'summaryMaxChars must be a number' };
next.summaryMaxChars = summaryMaxChars;
}
if (patch.skillsSyncEnabled !== undefined) {
if (typeof patch.skillsSyncEnabled !== 'boolean')
return { ok: false, error: 'skillsSyncEnabled must be a boolean' };
next.skillsSyncEnabled = patch.skillsSyncEnabled;
}
if (patch.skillsDir !== undefined) {
if (typeof patch.skillsDir !== 'string')
return { ok: false, error: 'skillsDir must be a string' };
const skillsDir = patch.skillsDir.trim();
if (skillsDir.length > 512)
return { ok: false, error: 'skillsDir too long' };
next.skillsDir = skillsDir;
}
const summaryToken = cleanString(patch.summaryToken);
if (summaryToken !== undefined) {
if (!summaryToken.startsWith('cmt_'))

View File

@ -23,7 +23,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
import type { CloudClient } from './api.ts'
import type { SettingsStore, SettingsView, TokenView } from './store.ts'
import type { Reporter } from './reporter.ts'
import { renderConfigPage } from './page.ts'
import { createSettingsUiRoutes } from './settings-ui/routes.ts'
import type { CloudMemoryWebRoute } from './context-types.ts'
import type { SkillSync, SkillSyncStats } from './skill-sync.ts'
import type { SkillAdmin } from './skill-admin.ts'
@ -154,6 +154,8 @@ function statusPayload(deps: RouteDeps): Record<string, unknown> {
defaultK: view.defaultK,
summaryEnabled: view.summaryEnabled,
summaryMaxChars: view.summaryMaxChars,
activityEnabled: view.activityEnabled,
memoryEnabled: view.memoryEnabled,
skillsSyncEnabled: view.skillsSyncEnabled,
skillsDir: view.skillsDir,
}
@ -169,21 +171,6 @@ function statusPayload(deps: RouteDeps): Record<string, unknown> {
}
}
/** Serve the configuration page. */
function servePage(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): void {
if (req.method !== 'GET' && req.method !== 'HEAD') {
fail(res, 405, 'method-not-allowed', true)
return
}
const html = renderConfigPage({ version: deps.version, settingsPath: deps.store.path })
res.writeHead(200, {
'content-type': 'text/html; charset=utf-8',
'cache-control': 'no-store',
'content-length': Buffer.byteLength(html),
})
if (req.method === 'HEAD') res.end()
else res.end(html)
}
/** `GET /dsh-brain/status`. */
function serveStatus(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): void {
@ -262,17 +249,13 @@ export function createLegacyRoutes(deps: RouteDeps): CloudMemoryWebRoute[] {
* Build the exact-route table registered with the host web server.
*
* Composition (m00001 §4): the root owns `/status` and `/capabilities`; the
* cloud-sync bundle returns login/test/flush/sync; the skill bundle returns
* skills/market/clients. No path shadows another, so the grouped registration
* order is safe.
* settings-ui bundle returns the page; the cloud-sync bundle returns
* login/test/flush/sync; the skill bundle returns skills/market/clients. No
* path shadows another, so the grouped registration order is safe.
*/
export function createRoutes(deps: RouteDeps): CloudMemoryWebRoute[] {
return [
{
kind: 'exact',
path: ROUTES.page,
handler: (req, res) => servePage(deps, req, res),
},
...createSettingsUiRoutes({ version: deps.version, store: deps.store }),
{
kind: 'exact',
path: ROUTES.status,

View File

@ -1,11 +1,16 @@
/**
* Standalone configuration page.
* Settings UI bundle — standalone configuration page.
*
* This profile runs with the native settings surface disabled
* (`ui-settings` is turned off in `cordis.patch.yml`), so the plugin ships its
* own HTML page at `/dsh-brain`. It is deliberately dependency-free: no
* bundler assets, no CDN, no fonts — one file the host web server can emit.
*
* The page is a pure view: every control reads and writes through the public
* HTTP API (`GET /status`, `PUT /config`, `POST login/test/flush`) and never
* touches SettingsStore, CloudClient, Reporter or SkillSync directly, so
* removing the settings-ui bundle only loses the page.
*
* The page renders tokens as masked values only; the plaintext never leaves
* `$DSH_HOME/dsh-brain.json`.
*/
@ -231,10 +236,6 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
<label for="requestTimeoutMs">请求超时(毫秒)</label>
<input id="requestTimeoutMs" type="number" min="1000" max="60000" step="500" />
</div>
<div>
<label for="defaultK">默认检索条数</label>
<input id="defaultK" type="number" min="1" max="100" />
</div>
</div>
<div class="toggles">
<label class="toggle" for="enabled">
@ -247,6 +248,11 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
<span class="track"></span>
<span><span class="ttitle">轮次结束后自动生成待确认摘要</span><span class="tdesc">只进入待确认队列,确认后才写云端</span></span>
</label>
<label class="toggle" for="activityEnabled">
<input id="activityEnabled" type="checkbox" />
<span class="track"></span>
<span><span class="ttitle">活动上报开关</span><span class="tdesc">只控制每轮活动上报;未显式设置时跟随总开关</span></span>
</label>
<div class="inline-field">
<label for="summaryMaxChars">单轮摘要字数上限</label>
<input id="summaryMaxChars" type="number" min="1000" max="16000" step="500" />
@ -254,6 +260,43 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
</div>
</section>
<section class="card" id="memoryCard">
<div class="card-head"><span class="chip">🧠</span><h2>记忆配置</h2><span class="desc">memory_* 工具</span></div>
<div class="grid">
<div>
<label for="defaultK">默认检索条数 defaultK</label>
<input id="defaultK" type="number" min="1" max="100" />
<div class="hint">memory_search 未显式指定 k 时的返回上限(1–100)。</div>
</div>
</div>
<div class="toggles">
<label class="toggle" for="memoryEnabled">
<input id="memoryEnabled" type="checkbox" />
<span class="track"></span>
<span><span class="ttitle">启用记忆工具</span><span class="tdesc">未显式设置时跟随总开关</span></span>
</label>
</div>
</section>
<section class="card" id="skillsCard">
<div class="card-head"><span class="chip">🔄</span><h2>Skill 同步</h2><span class="desc">后台定时双向同步</span></div>
<div class="grid">
<div>
<label for="skillsDir">技能目录(可选)</label>
<input id="skillsDir" type="text" spellcheck="false" placeholder="默认 $DSH_HOME/skills" />
<div class="hint">留空使用默认目录;保存后下一次同步生效,无需重启。</div>
</div>
</div>
<div class="toggles">
<label class="toggle" for="skillsSyncEnabled">
<input id="skillsSyncEnabled" type="checkbox" />
<span class="track"></span>
<span><span class="ttitle">启用云端技能同步</span><span class="tdesc">下载云端技能库、上传本机新技能</span></span>
</label>
</div>
<div class="tip">同步动作由后台定时执行(能力缺失时本卡片自动隐藏);这里只保存开关与目录。</div>
</section>
<section class="card">
<div class="card-head"><span class="chip">🔑</span><h2>账号登录</h2><span class="desc">用账号一次性换取令牌,密码不保存</span></div>
<div class="grid">
@ -368,8 +411,14 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
el('requestTimeoutMs').value = settings.requestTimeoutMs || '';
el('defaultK').value = settings.defaultK || '';
el('enabled').checked = settings.enabled === true;
el('activityEnabled').checked = settings.activityEnabled === true;
el('summaryEnabled').checked = settings.summaryEnabled === true;
el('summaryMaxChars').value = settings.summaryMaxChars || '';
el('memoryEnabled').checked = settings.memoryEnabled === true;
el('skillsSyncEnabled').checked = settings.skillsSyncEnabled === true;
el('skillsDir').value = settings.skillsDir || '';
var features = (view.capabilities && view.capabilities.features) || {};
el('skillsCard').style.display = features.skillsSync === false ? 'none' : '';
el('activityTokenHint').innerHTML = '用于 POST /activity/turn。当前 ' +
(tokens.activity && tokens.activity.configured
@ -443,8 +492,12 @@ tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, transparent)
requestTimeoutMs: Number(el('requestTimeoutMs').value),
defaultK: Number(el('defaultK').value),
enabled: el('enabled').checked,
activityEnabled: el('activityEnabled').checked,
summaryEnabled: el('summaryEnabled').checked,
summaryMaxChars: Number(el('summaryMaxChars').value)
summaryMaxChars: Number(el('summaryMaxChars').value),
memoryEnabled: el('memoryEnabled').checked,
skillsSyncEnabled: el('skillsSyncEnabled').checked,
skillsDir: el('skillsDir').value.trim()
};
var activity = el('activityToken').value.trim();
var memory = el('memoryToken').value.trim();

54
src/settings-ui/routes.ts Normal file
View File

@ -0,0 +1,54 @@
/**
* Settings UI bundle — page route.
*
* The bundle owns the two configuration views: the standalone HTML page and
* the 连接与上报 tab in the client card. It owns no business logic: the page
* only calls the public HTTP API, so removing this bundle loses the
* configuration page while memory tools, activity reporting and skill sync
* keep working.
*
* The path is kept in sync with `ROUTES.page` in `src/routes.ts` (the root
* owns the route table); the legacy `/cloud-memory` alias is derived from the
* root route list, so it follows automatically.
*/
import type { IncomingMessage, ServerResponse } from 'node:http'
import type { SettingsStore } from '../store.ts'
import { fail } from '../http.ts'
import type { CloudMemoryWebRoute } from '../context-types.ts'
import { renderConfigPage } from './page.ts'
/** Must match `ROUTES.page` in `src/routes.ts`. */
const PAGE_PATH = '/dsh-brain'
/** Everything the page route needs from the plugin core. */
export interface SettingsUiRouteDeps {
version: string
store: SettingsStore
}
/** Serve the configuration page. */
function servePage(deps: SettingsUiRouteDeps, req: IncomingMessage, res: ServerResponse): void {
if (req.method !== 'GET' && req.method !== 'HEAD') {
fail(res, 405, 'method-not-allowed', true)
return
}
const html = renderConfigPage({ version: deps.version, settingsPath: deps.store.path })
res.writeHead(200, {
'content-type': 'text/html; charset=utf-8',
'cache-control': 'no-store',
'content-length': Buffer.byteLength(html),
})
if (req.method === 'HEAD') res.end()
else res.end(html)
}
/** The exact route the root aggregator registers for the settings page. */
export function createSettingsUiRoutes(deps: SettingsUiRouteDeps): CloudMemoryWebRoute[] {
return [
{
kind: 'exact',
path: PAGE_PATH,
handler: (req, res) => servePage(deps, req, res),
},
]
}

View File

@ -31,6 +31,8 @@ export interface PersistedState {
summaryEnabled?: boolean
summaryMaxChars?: number
summaryToken?: string
skillsSyncEnabled?: boolean
skillsDir?: string
}
/** Masked token view handed to the browser. */
@ -119,8 +121,8 @@ function parseState(raw: string): PersistedState {
const source = value as Record<string, unknown>
const state: PersistedState = { version: 1 }
if (typeof source.enabled === 'boolean') state.enabled = source.enabled
if (typeof source.activityEnabled === 'boolean') state.activityEnabled = source.activityEnabled
if (typeof source.memoryEnabled === 'boolean') state.memoryEnabled = source.memoryEnabled
if (typeof source.skillsSyncEnabled === 'boolean') state.skillsSyncEnabled = source.skillsSyncEnabled
if (typeof source.skillsDir === 'string') state.skillsDir = source.skillsDir
const baseUrl = normalizeBaseUrl(source.baseUrl)
if (baseUrl !== undefined) state.baseUrl = baseUrl
const clientId = cleanString(source.clientId)
@ -205,6 +207,8 @@ export class SettingsStore {
if (this.state.defaultK !== undefined) resolved.defaultK = this.state.defaultK
if (this.state.summaryEnabled !== undefined) resolved.summaryEnabled = this.state.summaryEnabled
if (this.state.summaryMaxChars !== undefined) resolved.summaryMaxChars = this.state.summaryMaxChars
if (this.state.skillsSyncEnabled !== undefined) resolved.skillsSyncEnabled = this.state.skillsSyncEnabled
if (this.state.skillsDir !== undefined) resolved.skillsDir = this.state.skillsDir
// The split gates inherit `enabled` unless the config or the sidecar set
// them explicitly, so an old configuration keeps its exact behaviour.
if (this.state.activityEnabled === undefined && this.configExplicit.activityEnabled === undefined) {
@ -313,6 +317,16 @@ export class SettingsStore {
if (summaryMaxChars === undefined) return { ok: false, error: 'summaryMaxChars must be a number' }
next.summaryMaxChars = summaryMaxChars
}
if (patch.skillsSyncEnabled !== undefined) {
if (typeof patch.skillsSyncEnabled !== 'boolean') return { ok: false, error: 'skillsSyncEnabled must be a boolean' }
next.skillsSyncEnabled = patch.skillsSyncEnabled
}
if (patch.skillsDir !== undefined) {
if (typeof patch.skillsDir !== 'string') return { ok: false, error: 'skillsDir must be a string' }
const skillsDir = patch.skillsDir.trim()
if (skillsDir.length > 512) return { ok: false, error: 'skillsDir too long' }
next.skillsDir = skillsDir
}
const summaryToken = cleanString(patch.summaryToken)
if (summaryToken !== undefined) {
if (!summaryToken.startsWith('cmt_')) return { ok: false, error: 'summary token must start with cmt_' }