更新 lib/index.js

This commit is contained in:
QQ 2026-11-11 10:51:15 +08:00
parent 484e2d892d
commit 00ddf1b966
1 changed files with 29 additions and 5 deletions

View File

@ -13,13 +13,16 @@ import { CloudClient } from "./api.js";
import { Reporter } from "./reporter.js"; import { Reporter } from "./reporter.js";
import { createRoutes } from "./routes.js"; import { createRoutes } from "./routes.js";
import { SettingsStore } from "./store.js"; import { SettingsStore } from "./store.js";
import { createMemoryTools } from "./tools.js"; import { SkillSync } from "./skill-sync.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-cloud-memory';
/** 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.2.0'; const VERSION = '0.3.0';
/** How often the skill directory is reconciled with the cloud center. */
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. */
@ -41,6 +44,15 @@ export function apply(ctx, config) {
summaryToken: () => store.summaryToken(), summaryToken: () => store.summaryToken(),
log: (message) => logger.warn(message), log: (message) => logger.warn(message),
}); });
const skillSync = new SkillSync({
client: () => client,
settings: () => ({
enabled: settings().enabled,
skillsSyncEnabled: settings().skillsSyncEnabled,
skillsDir: settings().skillsDir,
}),
log: (message) => logger.info(message),
});
// Announce at most once per activation so a missing token is obvious in logs. // Announce at most once per activation so a missing token is obvious in logs.
const effective = settings(); const effective = settings();
if (!effective.enabled) { if (!effective.enabled) {
@ -70,11 +82,12 @@ export function apply(ctx, config) {
const tools = toolsCtx.tools; const tools = toolsCtx.tools;
if (tools === undefined) if (tools === undefined)
return; return;
for (const tool of createMemoryTools({ const toolDeps = {
client: () => client, client: () => client,
memoryToken: () => store.memoryToken(), memoryToken: () => store.memoryToken(),
settings: () => ({ enabled: settings().enabled, defaultK: settings().defaultK }), settings: () => ({ enabled: settings().enabled, defaultK: settings().defaultK }),
})) { };
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-cloud-memory: tool ${tool.name}`);
} }
}); });
@ -83,7 +96,7 @@ export function apply(ctx, config) {
logger.warn('webServer service unavailable; the configuration page stays disabled'); logger.warn('webServer service unavailable; the configuration page stays disabled');
} }
else { else {
const routes = createRoutes({ version: VERSION, store, reporter, client: () => client }); const routes = createRoutes({ version: VERSION, store, reporter, client: () => client, skills: () => skillSync.stats() });
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-cloud-memory: route ${route.path}`);
} }
@ -99,4 +112,15 @@ export function apply(ctx, config) {
timer.unref?.(); timer.unref?.();
return () => clearInterval(timer); return () => clearInterval(timer);
}, 'dsh-cloud-memory: retry timer'); }, 'dsh-cloud-memory: retry timer');
ctx.effect(() => {
const run = () => {
if (store.memoryToken() !== '')
void skillSync.syncOnce().catch(() => { });
};
const first = setTimeout(run, 5_000);
first.unref?.();
const timer = setInterval(run, SKILL_SYNC_INTERVAL_MS);
timer.unref?.();
return () => { clearTimeout(first); clearInterval(timer); };
}, 'dsh-cloud-memory: skill sync timer');
} }