更新 src/index.ts

This commit is contained in:
QQ 2026-11-11 10:51:42 +08:00
parent 13344e2bbf
commit ba74b161f7
1 changed files with 30 additions and 5 deletions

View File

@ -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')
}