@soffits/dsh-cron-jobs 0.7.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -7
- package/bin/dsh-cron-event.js +101 -0
- package/client.js +32 -5
- package/index.js +28 -1
- package/package.json +7 -3
- package/src/config.js +18 -1
- package/src/controller.js +341 -22
- package/src/deliver.js +15 -3
- package/src/events.js +302 -0
- package/src/history.js +28 -8
- package/src/job.js +216 -81
- package/src/prompt.js +55 -9
- package/src/session.js +2 -2
- package/src/spool.js +176 -0
- package/src/store.js +17 -1
- package/src/watchers.js +374 -0
- package/tools.js +59 -4
package/tools.js
CHANGED
|
@@ -15,7 +15,7 @@ export const inject = ['tools']
|
|
|
15
15
|
|
|
16
16
|
export const Config = z.object({
|
|
17
17
|
prefix: z.string().default('cron_job')
|
|
18
|
-
.description('Tool name prefix: <prefix>_create, _edit, _list and
|
|
18
|
+
.description('Tool name prefix: <prefix>_create, _edit, _list, _view and _watch. "schedule" gives the names of DSH\'s built-in Schedule tools, for use in its place.'),
|
|
19
19
|
})
|
|
20
20
|
|
|
21
21
|
/** Runs shown by the view tool, and characters of each output tail. */
|
|
@@ -46,6 +46,12 @@ export function toolSchemas(prefix) {
|
|
|
46
46
|
answerCheck: { type: 'string', description: 'Shell command run in the session\'s cwd on the agent\'s final answer (on stdin) before its turn closes. Empty stdout passes the answer; any stdout is sent back into the turn as a request to rewrite it, once per run. Skipped for [SILENT].' },
|
|
47
47
|
cwd: { type: 'string', description: 'Working directory of the command and of new sessions; defaults to this session\'s.' },
|
|
48
48
|
deliver: { type: 'string', description: 'Where each run\'s result is sent: "<service>:<address>", a DSH service that delivers messages and an address it understands (such as telegramTopics:notify), or "none". The result is the agent\'s final answer (exactly [SILENT] sends nothing; MEDIA:<path> lines become attachments), or for a job without a prompt the command\'s stdout and failures. Defaults to the plugin\'s default target, or none when the prompt goes to an existing session.' },
|
|
49
|
+
on: {
|
|
50
|
+
type: 'array',
|
|
51
|
+
items: { type: 'string' },
|
|
52
|
+
description: `Event channels that start a run, with * for any characters (mail.sakina, ci.*, dsh.started). Events come from \`dsh-cron-event <channel>\` (any script or hook), from watchers (${prefix}_watch), and dsh.started once after each DSH start. Events within about 30 s run the job once; the command gets them as JSON on stdin and the prompt gets them appended as untrusted data. A job with on may leave out the schedule; keep a slower one as a safety net when events can be missed.`,
|
|
53
|
+
},
|
|
54
|
+
once: { type: 'boolean', description: 'End the job after its first run that was not started by hand (an event or a scheduled run). With on: dsh.started, it waits for the next DSH start.' },
|
|
49
55
|
}
|
|
50
56
|
const model = {
|
|
51
57
|
type: 'object',
|
|
@@ -66,6 +72,14 @@ export function toolSchemas(prefix) {
|
|
|
66
72
|
timeZone: { type: 'string', description: 'IANA zone such as America/Toronto. Required with cron and with an at without offset; rrule takes its zone from DTSTART.' },
|
|
67
73
|
model,
|
|
68
74
|
}, ['title']),
|
|
75
|
+
watch: object({
|
|
76
|
+
action: { type: 'string', enum: ['create', 'delete', 'list'], description: 'create starts a new watcher; delete stops and removes one; list shows every watcher with its state.' },
|
|
77
|
+
id: { type: 'string', description: 'Watcher id: letters, digits, _ . - (create, delete).' },
|
|
78
|
+
channel: { type: 'string', description: 'Channel its output lines are emitted on, such as mail.sakina (create).' },
|
|
79
|
+
command: { type: 'string', description: 'Shell command run with bash that keeps running and prints one line per change (create). A JSON object line is the event\'s data (its "id" field dedupes), any other line its text; {"wakeAgent": false} lines are ignored. It is restarted with backoff when it exits; down for 15 minutes sends one alert. It gets DSH_CRON_STATE_DIR, a directory for its cursor, so it can catch up after a restart.' },
|
|
80
|
+
cwd: { type: 'string', description: 'Working directory; defaults to this session\'s (create).' },
|
|
81
|
+
once: { type: 'boolean', description: 'Run the command one time and remove the watcher when it exits, for a one-off wait such as `gh run watch 123 --exit-status >/dev/null; echo "{\\"exit\\": $?}"` (create).' },
|
|
82
|
+
}, ['action']),
|
|
69
83
|
edit: object({
|
|
70
84
|
id: { type: 'string', description: 'Job id.' },
|
|
71
85
|
action: {
|
|
@@ -77,13 +91,17 @@ export function toolSchemas(prefix) {
|
|
|
77
91
|
prompt: { ...fields.prompt, description: 'New prompt; an empty string removes it.' },
|
|
78
92
|
command: { ...fields.command, description: 'New command; an empty string removes it.' },
|
|
79
93
|
answerCheck: { ...fields.answerCheck, description: 'New answer check command; an empty string removes it.' },
|
|
80
|
-
...schedule,
|
|
94
|
+
cron: { ...schedule.cron, description: `${schedule.cron.description} An empty cron, rrule or at removes the schedule of a job with on.` },
|
|
95
|
+
rrule: schedule.rrule,
|
|
96
|
+
at: schedule.at,
|
|
81
97
|
timeZone: { type: 'string', description: `Zone for a new cron or at, as in ${prefix}_create; given alone, it moves a cron schedule to this zone.` },
|
|
82
98
|
target: { ...fields.target, description: `New target, as in ${prefix}_create.` },
|
|
83
99
|
sessionId: fields.sessionId,
|
|
84
100
|
cwd: { type: 'string', description: 'New working directory of the command and of new sessions.' },
|
|
85
101
|
deliver: { type: 'string', description: `New delivery target, as in ${prefix}_create; an empty string returns to the default.` },
|
|
86
102
|
model: { ...model, description: `New model, as in ${prefix}_create. An empty object ({}) returns to the preset's or host default.` },
|
|
103
|
+
on: { ...fields.on, description: `New event channels, as in ${prefix}_create; an empty list removes them.` },
|
|
104
|
+
once: fields.once,
|
|
87
105
|
}, ['id', 'action']),
|
|
88
106
|
list: object({}, []),
|
|
89
107
|
view: object({ id: { type: 'string', description: 'Job id.' } }, ['id']),
|
|
@@ -122,13 +140,15 @@ export function createTools({ prefix = 'cron_job', getJobs, ctx }) {
|
|
|
122
140
|
|
|
123
141
|
return [
|
|
124
142
|
tool('create',
|
|
125
|
-
'Create a scheduled job. Each run executes command and/or delivers prompt to the target session as a scheduled message from the user. Give
|
|
143
|
+
'Create a scheduled job. Each run executes command and/or delivers prompt to the target session as a scheduled message from the user. Give one of cron, rrule or at, and/or on (event channels) to run when an event arrives instead of polling. Returns the job with its id and next run. A run missed while DSH was down happens once at startup.',
|
|
126
144
|
async (args, jobs, agent) => modelJob(await jobs.create({
|
|
127
145
|
title: args.title,
|
|
128
146
|
prompt: args.prompt,
|
|
129
147
|
command: args.command,
|
|
130
148
|
answerCheck: args.answerCheck,
|
|
131
149
|
schedule: { cron: args.cron, rrule: args.rrule, at: args.at, timeZone: args.timeZone },
|
|
150
|
+
...(args.on !== undefined ? { on: args.on } : {}),
|
|
151
|
+
...(args.once !== undefined ? { once: args.once } : {}),
|
|
132
152
|
cwd: args.cwd || callerCwd(agent),
|
|
133
153
|
preset: callerPreset(agent),
|
|
134
154
|
...(toModel(args.model) ? { model: toModel(args.model) } : {}),
|
|
@@ -141,7 +161,7 @@ export function createTools({ prefix = 'cron_job', getJobs, ctx }) {
|
|
|
141
161
|
`Update, pause, resume or delete one job created with ${prefix}_create, by id. Jobs from the plugin config are read-only.`,
|
|
142
162
|
async (args, jobs, agent) => {
|
|
143
163
|
const { id, action } = args
|
|
144
|
-
const changes = ['title', 'prompt', 'command', 'answerCheck', 'cron', 'rrule', 'at', 'timeZone', 'target', 'sessionId', 'cwd', 'deliver', 'model'].filter(key => args[key] !== undefined)
|
|
164
|
+
const changes = ['title', 'prompt', 'command', 'answerCheck', 'cron', 'rrule', 'at', 'timeZone', 'target', 'sessionId', 'cwd', 'deliver', 'model', 'on', 'once'].filter(key => args[key] !== undefined)
|
|
145
165
|
if (action !== 'update' && changes.length > 0) return failure('invalid_arguments', `${action} takes only id and action.`)
|
|
146
166
|
if (action === 'delete') return await jobs.remove(id)
|
|
147
167
|
if (action === 'pause' || action === 'resume') {
|
|
@@ -159,6 +179,8 @@ export function createTools({ prefix = 'cron_job', getJobs, ctx }) {
|
|
|
159
179
|
command: args.command,
|
|
160
180
|
answerCheck: args.answerCheck,
|
|
161
181
|
cwd: args.cwd,
|
|
182
|
+
on: args.on,
|
|
183
|
+
once: args.once,
|
|
162
184
|
...(args.model !== undefined ? { model: toModel(args.model) ?? null } : {}),
|
|
163
185
|
...(scheduleGiven ? { schedule: { cron: args.cron, rrule: args.rrule, at: args.at, timeZone: args.timeZone } } : {}),
|
|
164
186
|
...(targetGiven ? { target: await resolveTarget(ctx, agent, args.target ?? 'new', args.sessionId) } : {}),
|
|
@@ -178,6 +200,25 @@ export function createTools({ prefix = 'cron_job', getJobs, ctx }) {
|
|
|
178
200
|
return { ...modelJob(job), recentRuns: (jobs.runs(id) ?? []).slice(0, VIEW_RUNS).map(modelRun) }
|
|
179
201
|
},
|
|
180
202
|
'View scheduled job', 'read'),
|
|
203
|
+
tool('watch',
|
|
204
|
+
`Manage watchers: long-running commands whose output lines are events on a channel, which start the jobs listening on it (on in ${prefix}_create). Use them to react to a stream (a mailbox push connection, a notification feed, a log) instead of polling, or with once for a one-off wait that survives a DSH restart.`,
|
|
205
|
+
async (args, jobs, agent) => {
|
|
206
|
+
const { action } = args
|
|
207
|
+
const given = ['id', 'channel', 'command', 'cwd', 'once'].filter(key => args[key] !== undefined)
|
|
208
|
+
if (action === 'list') {
|
|
209
|
+
if (given.length > 0) return failure('invalid_arguments', 'list takes only action.')
|
|
210
|
+
return { watchers: jobs.listWatchers().map(modelWatcher) }
|
|
211
|
+
}
|
|
212
|
+
if (action === 'delete') {
|
|
213
|
+
if (given.some(key => key !== 'id') || !args.id) return failure('invalid_arguments', 'delete takes action and id.')
|
|
214
|
+
return await jobs.removeWatcher(args.id)
|
|
215
|
+
}
|
|
216
|
+
return modelWatcher(await jobs.createWatcher({
|
|
217
|
+
id: args.id, channel: args.channel, command: args.command,
|
|
218
|
+
cwd: args.cwd || callerCwd(agent), once: args.once === true, createdBy: agent?.session?.id,
|
|
219
|
+
}))
|
|
220
|
+
},
|
|
221
|
+
'Manage watchers', 'other'),
|
|
181
222
|
]
|
|
182
223
|
}
|
|
183
224
|
|
|
@@ -266,6 +307,9 @@ export function modelJob(job, { brief = false } = {}) {
|
|
|
266
307
|
...(job.error ? { error: job.error } : {}),
|
|
267
308
|
schedule: job.schedule?.text ?? null,
|
|
268
309
|
...(job.nextRunAt ? { nextRunAt: job.nextRunAt, nextRunAtLocal: `${formatLocalTime(new Date(job.nextRunAt), zone)}${zone ? ` ${zone}` : ''}` } : { nextRunAt: null }),
|
|
310
|
+
...(job.on ? { on: job.on } : {}),
|
|
311
|
+
...(job.once ? { once: true } : {}),
|
|
312
|
+
...(job.pendingEvents ? { pendingEvents: job.pendingEvents } : {}),
|
|
269
313
|
...(job.target ? { target: job.target.sessionId ? `${job.target.kind} ${job.target.sessionId}` : job.target.kind } : {}),
|
|
270
314
|
...(job.agent?.model ? { model: job.agent.model } : {}),
|
|
271
315
|
deliver: job.deliver?.via ? (job.deliver.to ? `${job.deliver.via}:${job.deliver.to}` : job.deliver.via) : 'none',
|
|
@@ -278,11 +322,17 @@ export function modelJob(job, { brief = false } = {}) {
|
|
|
278
322
|
}
|
|
279
323
|
}
|
|
280
324
|
|
|
325
|
+
/** A watcher as the model sees it. */
|
|
326
|
+
function modelWatcher(view) {
|
|
327
|
+
return Object.fromEntries(Object.entries(view).filter(([, value]) => value !== null && value !== undefined))
|
|
328
|
+
}
|
|
329
|
+
|
|
281
330
|
function modelRun(run) {
|
|
282
331
|
const tail = text => (text && text.length > VIEW_TAIL_CHARS ? `…${text.slice(-VIEW_TAIL_CHARS)}` : text)
|
|
283
332
|
return Object.fromEntries(Object.entries({
|
|
284
333
|
startedAt: run.startedAt,
|
|
285
334
|
trigger: run.trigger,
|
|
335
|
+
events: run.events ? `${run.events.count} on ${run.events.channels.join(', ')}` : undefined,
|
|
286
336
|
status: run.status,
|
|
287
337
|
outcome: run.outcome,
|
|
288
338
|
sessionId: run.sessionId,
|
|
@@ -302,6 +352,11 @@ export function checkArgs(schema, args) {
|
|
|
302
352
|
const property = schema.properties[key]
|
|
303
353
|
if (property === undefined) return `unknown argument ${key}`
|
|
304
354
|
if (value === undefined) continue
|
|
355
|
+
if (property.type === 'array') {
|
|
356
|
+
if (!Array.isArray(value)) return `${key} must be an array`
|
|
357
|
+
if (value.some(item => typeof item !== property.items.type)) return `${key} must hold only ${property.items.type}s`
|
|
358
|
+
continue
|
|
359
|
+
}
|
|
305
360
|
if (property.type === 'object') {
|
|
306
361
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) return `${key} must be an object`
|
|
307
362
|
const inner = checkArgs(property, value)
|