更新 lib/index.js

This commit is contained in:
QQ 2026-11-11 23:13:56 +08:00
parent bffa7e8330
commit f48cb70ea1
1 changed files with 14 additions and 13 deletions

View File

@ -4,32 +4,32 @@
* Responsibilities: * Responsibilities:
* 1. report one activity record per finished conversation round, * 1. report one activity record per finished conversation round,
* 2. expose the memory tools to the agent, * 2. expose the memory tools to the agent,
* 3. serve the configuration page under `/cloud-memory`. * 3. serve the configuration page under `/dsh-brain` (legacy `/cloud-memory` routes stay as aliases).
* *
* Everything registers through `ctx.effect(...)` so a plugin reload tears down * Everything registers through `ctx.effect(...)` so a plugin reload tears down
* its routes, timer, listeners, and tools without leftovers. * its routes, timer, listeners, and tools without leftovers.
*/ */
import { CloudClient } from "./api.js"; import { CloudClient } from "./api.js";
import { Reporter } from "./reporter.js"; import { Reporter } from "./reporter.js";
import { createRoutes } from "./routes.js"; import { createLegacyRoutes, createRoutes } from "./routes.js";
import { join } from 'node:path'; import { join } from 'node:path';
import { SettingsStore, dshHome } from "./store.js"; import { SettingsStore, dshHome } from "./store.js";
import { SkillSync } from "./skill-sync.js"; import { SkillSync, skillsStatePath } from "./skill-sync.js";
import { SkillAdmin, skillsHoldingDir } from "./skill-admin.js"; import { SkillAdmin, skillsHoldingDir } from "./skill-admin.js";
import { createMemoryTools, createSkillTools } from "./tools.js"; import { createMemoryTools, createSkillTools } from "./tools.js";
/** Package name; also the client bundle id. */ /** Package name; also the client bundle id. */
export const name = 'dsh-cloud-memory'; export const name = 'dsh-brain';
/** Services required before `apply` runs. */ /** Services required before `apply` runs. */
export const inject = ['webServer']; export const inject = ['webServer'];
/** Version reported by the status card. */ /** Version reported by the status card. */
const VERSION = '0.4.1'; const VERSION = '0.5.0';
/** How often the skill directory is reconciled with the cloud center. */ /** How often the skill directory is reconciled with the cloud center. */
const SKILL_SYNC_INTERVAL_MS = 15 * 60_000; const SKILL_SYNC_INTERVAL_MS = 15 * 60_000;
/** How often queued reports are retried. */ /** How often queued reports are retried. */
const RETRY_INTERVAL_MS = 30_000; const RETRY_INTERVAL_MS = 30_000;
/** Register the plugin with the host. */ /** Register the plugin with the host. */
export function apply(ctx, config) { export function apply(ctx, config) {
const logger = ctx.logger('cloud-memory'); const logger = ctx.logger('dsh-brain');
const store = new SettingsStore(config); const store = new SettingsStore(config);
const settings = () => store.settings(); const settings = () => store.settings();
// One client per instance; the settings closures keep it live across edits. // One client per instance; the settings closures keep it live across edits.
@ -59,7 +59,7 @@ export function apply(ctx, config) {
const skillAdmin = new SkillAdmin({ const skillAdmin = new SkillAdmin({
skillsRoot: () => skillSync.skillsRoot(), skillsRoot: () => skillSync.skillsRoot(),
holdingDir: () => skillsHoldingDir(), holdingDir: () => skillsHoldingDir(),
syncStatePath: () => join(dshHome(), 'cloud-memory-skills.json'), syncStatePath: () => skillsStatePath(),
syncRunning: () => skillSync.stats().running, syncRunning: () => skillSync.stats().running,
log: (message) => logger.info(message), log: (message) => logger.info(message),
}); });
@ -69,7 +69,7 @@ export function apply(ctx, config) {
logger.info('disabled by settings'); logger.info('disabled by settings');
} }
else if (store.activityToken() === '') { else if (store.activityToken() === '') {
logger.warn(`reporting enabled but no activity token; configure one at ${store.path} or open /cloud-memory`); logger.warn(`reporting enabled but no activity token; configure one at ${store.path} or open /dsh-brain`);
} }
else { else {
logger.info(`reporting to ${effective.baseUrl} as ${effective.clientId}`); logger.info(`reporting to ${effective.baseUrl} as ${effective.clientId}`);
@ -87,7 +87,7 @@ export function apply(ctx, config) {
catch (error) { catch (error) {
logger.warn(`turn event handling failed: ${error instanceof Error ? error.message : String(error)}`); logger.warn(`turn event handling failed: ${error instanceof Error ? error.message : String(error)}`);
} }
}), 'dsh-cloud-memory: turn reporting'); }), 'dsh-brain: turn reporting');
ctx.inject(['tools'], (toolsCtx) => { ctx.inject(['tools'], (toolsCtx) => {
const tools = toolsCtx.tools; const tools = toolsCtx.tools;
if (tools === undefined) if (tools === undefined)
@ -98,7 +98,7 @@ export function apply(ctx, config) {
settings: () => ({ enabled: settings().enabled, defaultK: settings().defaultK }), settings: () => ({ enabled: settings().enabled, defaultK: settings().defaultK }),
}; };
for (const tool of [...createMemoryTools(toolDeps), ...createSkillTools(toolDeps)]) { for (const tool of [...createMemoryTools(toolDeps), ...createSkillTools(toolDeps)]) {
toolsCtx.effect(() => tools.register(tool), `dsh-cloud-memory: tool ${tool.name}`); toolsCtx.effect(() => tools.register(tool), `dsh-brain: tool ${tool.name}`);
} }
}); });
const webServer = ctx.webServer; const webServer = ctx.webServer;
@ -107,8 +107,9 @@ export function apply(ctx, config) {
} }
else { else {
const routes = createRoutes({ version: VERSION, store, reporter, client: () => client, skills: () => skillSync.stats(), admin: skillAdmin }); const routes = createRoutes({ version: VERSION, store, reporter, client: () => client, skills: () => skillSync.stats(), admin: skillAdmin });
routes.push(...createLegacyRoutes({ version: VERSION, store, reporter, client: () => client, skills: () => skillSync.stats(), admin: skillAdmin }));
for (const route of routes) { for (const route of routes) {
ctx.effect(() => webServer.register(route), `dsh-cloud-memory: route ${route.path}`); ctx.effect(() => webServer.register(route), `dsh-brain: route ${route.path}`);
} }
} }
ctx.effect(() => { ctx.effect(() => {
@ -121,7 +122,7 @@ export function apply(ctx, config) {
}, RETRY_INTERVAL_MS); }, RETRY_INTERVAL_MS);
timer.unref?.(); timer.unref?.();
return () => clearInterval(timer); return () => clearInterval(timer);
}, 'dsh-cloud-memory: retry timer'); }, 'dsh-brain: retry timer');
ctx.effect(() => { ctx.effect(() => {
const run = () => { const run = () => {
if (store.memoryToken() !== '') if (store.memoryToken() !== '')
@ -132,5 +133,5 @@ export function apply(ctx, config) {
const timer = setInterval(run, SKILL_SYNC_INTERVAL_MS); const timer = setInterval(run, SKILL_SYNC_INTERVAL_MS);
timer.unref?.(); timer.unref?.();
return () => { clearTimeout(first); clearInterval(timer); }; return () => { clearTimeout(first); clearInterval(timer); };
}, 'dsh-cloud-memory: skill sync timer'); }, 'dsh-brain: skill sync timer');
} }