更新 src/index.ts
This commit is contained in:
parent
13344e2bbf
commit
ba74b161f7
35
src/index.ts
35
src/index.ts
|
|
@ -14,7 +14,8 @@ import type { CloudMemoryContext, SessionEventLike, SessionLike } from './contex
|
||||||
import { Reporter } from './reporter.ts'
|
import { Reporter } from './reporter.ts'
|
||||||
import { createRoutes } from './routes.ts'
|
import { createRoutes } from './routes.ts'
|
||||||
import { SettingsStore } from './store.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. */
|
/** Package name; also the client bundle id. */
|
||||||
export const name = 'dsh-cloud-memory'
|
export const name = 'dsh-cloud-memory'
|
||||||
|
|
@ -23,7 +24,10 @@ export const name = 'dsh-cloud-memory'
|
||||||
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
|
||||||
|
|
@ -50,6 +54,15 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void {
|
||||||
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) {
|
||||||
|
|
@ -84,11 +97,12 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void {
|
||||||
ctx.inject(['tools'], (toolsCtx) => {
|
ctx.inject(['tools'], (toolsCtx) => {
|
||||||
const tools = (toolsCtx as CloudMemoryContext).tools
|
const tools = (toolsCtx as CloudMemoryContext).tools
|
||||||
if (tools === undefined) return
|
if (tools === undefined) 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}`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -97,7 +111,7 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void {
|
||||||
if (webServer === undefined) {
|
if (webServer === undefined) {
|
||||||
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}`)
|
||||||
}
|
}
|
||||||
|
|
@ -114,4 +128,15 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void {
|
||||||
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')
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue