forked from deepseek-ai/deepseek-harness
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
77 lines (70 loc) · 2.51 KB
/
Copy pathindex.ts
File metadata and controls
77 lines (70 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Agent-scoped durable one-shot and fixed-rate reminders over the session event log.
* @module @deepseek-ai/dsh-schedule
*/
import type { Context } from '@deepseek-ai/cordis'
import type { Agent } from '@deepseek-ai/dsh-agent'
import type {} from '@deepseek-ai/dsh-session-persistence'
import { ScheduleRuntime } from './runtime.ts'
import { registerScheduleTools } from './tools.ts'
export type * from './types.ts'
export {
SCHEDULE_CHANGE_VERSION,
MIN_EVERY_INTERVAL_SECONDS,
ScheduleId,
ScheduleInputError,
ScheduleLogError,
allocateScheduleId,
createAfterScheduleRecord,
createAtScheduleRecord,
createEveryScheduleRecord,
decodeScheduleChange,
foldScheduleEvents,
renderReminderFraming,
renderEveryReminderBatchFraming,
resolveEveryOccurrence,
scheduleView,
} from './domain.ts'
export { registerScheduleTools } from './tools.ts'
/** Cordis function-plugin name. */
export const name = 'schedule'
/** Services required before future root agents can receive Schedule. */
export const inject = ['agents', 'sessions', 'tools', 'sessionPersistence']
type OwnerCleanup = () => void | Promise<void>
/** Install Schedule only for root agents published after this plugin loads. */
export function apply(ctx: Context): void {
const runtimes = new Map<Agent, OwnerCleanup>()
let stopping = false
ctx.effect(() => {
const stopCreated = ctx.on('agent/created', ({ agent }) => {
if (stopping || runtimes.has(agent) || !ctx.agents.roots().includes(agent)) return
const runtime = new ScheduleRuntime(ctx, agent)
const cleanup: OwnerCleanup = agent.ctx.effect(() => {
const disposeTools = registerScheduleTools(ctx, agent.ctx, agent, () => { runtime.requestDrive() })
const stopStatus = agent.ctx.on('agent/status', ({ status }) => {
if (status === 'idle' && agent.session.events.some(event => event.type === 'schedule/change')) {
runtime.requestDrive()
}
})
runtime.start()
return async () => {
stopStatus()
disposeTools()
try {
await runtime.dispose()
} finally {
if (runtimes.get(agent) === cleanup) runtimes.delete(agent)
}
}
}, 'schedule.runtime()')
runtimes.set(agent, cleanup)
})
return async () => {
stopping = true
stopCreated()
const cleanups = [...runtimes.values()]
runtimes.clear()
await Promise.allSettled(cleanups.map(cleanup => Promise.resolve(cleanup())))
}
}, 'schedule.lifecycle()')
}