From ba74b161f7f5545409e40617a06727398ffad6c9 Mon Sep 17 00:00:00 2001 From: QQ Date: Wed, 11 Nov 2026 10:51:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20src/index.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 36aaca5..47d5da1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,8 @@ import type { CloudMemoryContext, SessionEventLike, SessionLike } from './contex import { Reporter } from './reporter.ts' import { createRoutes } from './routes.ts' import { SettingsStore } from './store.ts' -import { createMemoryTools } from './tools.ts' +import { SkillSync } from './skill-sync.ts' +import { createMemoryTools, createSkillTools } from './tools.ts' /** Package name; also the client bundle id. */ export const name = 'dsh-cloud-memory' @@ -23,7 +24,10 @@ export const name = 'dsh-cloud-memory' export const inject = ['webServer'] /** 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. */ const RETRY_INTERVAL_MS = 30_000 @@ -50,6 +54,15 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void { 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. const effective = settings() if (!effective.enabled) { @@ -84,11 +97,12 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void { ctx.inject(['tools'], (toolsCtx) => { const tools = (toolsCtx as CloudMemoryContext).tools if (tools === undefined) return - for (const tool of createMemoryTools({ + const toolDeps = { client: () => client, memoryToken: () => store.memoryToken(), 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}`) } }) @@ -97,7 +111,7 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void { if (webServer === undefined) { logger.warn('webServer service unavailable; the configuration page stays disabled') } 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) { ctx.effect(() => webServer.register(route), `dsh-cloud-memory: route ${route.path}`) } @@ -114,4 +128,15 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void { timer.unref?.() return () => clearInterval(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') }