@karmaniverous/jeeves-runner 0.1.2 → 0.2.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/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
- import { DatabaseSync } from 'node:sqlite';
3
2
  import { Logger } from 'pino';
3
+ import { DatabaseSync } from 'node:sqlite';
4
4
 
5
5
  /**
6
6
  * Runner configuration schema and types.
@@ -16,6 +16,7 @@ declare const runnerConfigSchema: z.ZodObject<{
16
16
  runRetentionDays: z.ZodDefault<z.ZodNumber>;
17
17
  cursorCleanupIntervalMs: z.ZodDefault<z.ZodNumber>;
18
18
  shutdownGraceMs: z.ZodDefault<z.ZodNumber>;
19
+ reconcileIntervalMs: z.ZodDefault<z.ZodNumber>;
19
20
  notifications: z.ZodDefault<z.ZodObject<{
20
21
  slackTokenPath: z.ZodOptional<z.ZodString>;
21
22
  defaultOnFailure: z.ZodDefault<z.ZodNullable<z.ZodString>>;
@@ -32,6 +33,10 @@ declare const runnerConfigSchema: z.ZodObject<{
32
33
  }>>;
33
34
  file: z.ZodOptional<z.ZodString>;
34
35
  }, z.core.$strip>>;
36
+ gateway: z.ZodDefault<z.ZodObject<{
37
+ url: z.ZodDefault<z.ZodString>;
38
+ tokenPath: z.ZodOptional<z.ZodString>;
39
+ }, z.core.$strip>>;
35
40
  }, z.core.$strip>;
36
41
  /** Inferred runner configuration type. */
37
42
  type RunnerConfig = z.infer<typeof runnerConfigSchema>;
@@ -42,6 +47,7 @@ type RunnerConfig = z.infer<typeof runnerConfigSchema>;
42
47
  * @module
43
48
  */
44
49
 
50
+ /** Job definition schema for scheduled tasks. */
45
51
  declare const jobSchema: z.ZodObject<{
46
52
  id: z.ZodString;
47
53
  name: z.ZodString;
@@ -62,33 +68,60 @@ declare const jobSchema: z.ZodObject<{
62
68
  onFailure: z.ZodDefault<z.ZodNullable<z.ZodString>>;
63
69
  onSuccess: z.ZodDefault<z.ZodNullable<z.ZodString>>;
64
70
  }, z.core.$strip>;
71
+ /** Inferred Job type from schema. */
65
72
  type Job = z.infer<typeof jobSchema>;
66
73
 
74
+ /**
75
+ * Queue definition schema and types.
76
+ *
77
+ * @module
78
+ */
79
+
80
+ /** Queue definition schema for managing queue behavior and retention. */
81
+ declare const queueSchema: z.ZodObject<{
82
+ id: z.ZodString;
83
+ name: z.ZodString;
84
+ description: z.ZodOptional<z.ZodNullable<z.ZodString>>;
85
+ dedupExpr: z.ZodOptional<z.ZodNullable<z.ZodString>>;
86
+ dedupScope: z.ZodDefault<z.ZodEnum<{
87
+ pending: "pending";
88
+ all: "all";
89
+ }>>;
90
+ maxAttempts: z.ZodDefault<z.ZodNumber>;
91
+ retentionDays: z.ZodDefault<z.ZodNumber>;
92
+ createdAt: z.ZodString;
93
+ }, z.core.$strip>;
94
+ /** Inferred Queue type from schema. */
95
+ type Queue = z.infer<typeof queueSchema>;
96
+
67
97
  /**
68
98
  * Run record schema and types.
69
99
  *
70
100
  * @module
71
101
  */
72
102
 
103
+ /** Run status enumeration schema (pending, running, ok, error, timeout, skipped). */
73
104
  declare const runStatusSchema: z.ZodEnum<{
74
- error: "error";
75
105
  pending: "pending";
106
+ error: "error";
76
107
  running: "running";
77
108
  ok: "ok";
78
109
  timeout: "timeout";
79
110
  skipped: "skipped";
80
111
  }>;
112
+ /** Run trigger type enumeration schema (schedule, manual, retry). */
81
113
  declare const runTriggerSchema: z.ZodEnum<{
82
114
  schedule: "schedule";
83
115
  manual: "manual";
84
116
  retry: "retry";
85
117
  }>;
118
+ /** Run record schema representing a job execution instance. */
86
119
  declare const runSchema: z.ZodObject<{
87
120
  id: z.ZodNumber;
88
121
  jobId: z.ZodString;
89
122
  status: z.ZodEnum<{
90
- error: "error";
91
123
  pending: "pending";
124
+ error: "error";
92
125
  running: "running";
93
126
  ok: "ok";
94
127
  timeout: "timeout";
@@ -109,44 +142,101 @@ declare const runSchema: z.ZodObject<{
109
142
  retry: "retry";
110
143
  }>>;
111
144
  }, z.core.$strip>;
145
+ /** Inferred Run type representing a job execution record. */
112
146
  type Run = z.infer<typeof runSchema>;
147
+ /** Inferred RunStatus type from schema. */
113
148
  type RunStatus = z.infer<typeof runStatusSchema>;
149
+ /** Inferred RunTrigger type from schema. */
114
150
  type RunTrigger = z.infer<typeof runTriggerSchema>;
115
151
 
116
152
  /**
117
153
  * Main runner orchestrator. Wires up database, scheduler, API server, and handles graceful shutdown on SIGTERM/SIGINT.
118
154
  */
119
155
 
120
- /** Runner interface. */
156
+ /** Runner interface for managing the runner lifecycle. */
121
157
  interface Runner {
158
+ /** Initialize and start all runner components (database, scheduler, API server). */
122
159
  start(): Promise<void>;
160
+ /** Gracefully stop all runner components and clean up resources. */
123
161
  stop(): Promise<void>;
124
162
  }
163
+ /** Optional dependencies for test injection. */
164
+ interface RunnerDeps {
165
+ /** Optional custom logger instance. */
166
+ logger?: Logger;
167
+ }
125
168
  /**
126
169
  * Create the runner. Initializes database, scheduler, API server, and sets up graceful shutdown.
127
170
  */
128
- declare function createRunner(config: RunnerConfig): Runner;
171
+ declare function createRunner(config: RunnerConfig, deps?: RunnerDeps): Runner;
172
+
173
+ /**
174
+ * Queue operations module for runner client. Provides enqueue, dequeue, done, and fail operations.
175
+ */
176
+
177
+ /** Queue item returned from dequeue operation. */
178
+ interface QueueItem {
179
+ /** Queue item unique identifier. */
180
+ id: number;
181
+ /** Queue item payload (deserialized from JSON). */
182
+ payload: unknown;
183
+ }
129
184
 
130
185
  /**
131
186
  * Job client library for runner jobs. Provides cursor (state) and queue operations. Opens its own DB connection via JR_DB_PATH env var.
132
187
  */
188
+
133
189
  /** Client interface for job scripts to interact with runner state and queues. */
134
190
  interface RunnerClient {
191
+ /** Retrieve a cursor value by namespace and key. Returns null if not found or expired. */
135
192
  getCursor(namespace: string, key: string): string | null;
193
+ /** Set or update a cursor value with optional TTL (e.g., '30d', '24h', '60m'). */
136
194
  setCursor(namespace: string, key: string, value: string, options?: {
137
195
  ttl?: string;
138
196
  }): void;
197
+ /** Delete a cursor by namespace and key. */
139
198
  deleteCursor(namespace: string, key: string): void;
199
+ /** Retrieve a state value by namespace and key (alias for getCursor). Returns null if not found or expired. */
200
+ getState(namespace: string, key: string): string | null;
201
+ /** Set or update a state value with optional TTL (alias for setCursor). */
202
+ setState(namespace: string, key: string, value: string, options?: {
203
+ ttl?: string;
204
+ }): void;
205
+ /** Delete a state value by namespace and key (alias for deleteCursor). */
206
+ deleteState(namespace: string, key: string): void;
207
+ /** Check if a state item exists in a collection. */
208
+ hasItem(namespace: string, key: string, itemKey: string): boolean;
209
+ /** Retrieve a state item value from a collection. Returns null if not found. */
210
+ getItem(namespace: string, key: string, itemKey: string): string | null;
211
+ /** Set or update a state item in a collection. Value is optional (for existence-only tracking). Auto-creates parent state row if needed. */
212
+ setItem(namespace: string, key: string, itemKey: string, value?: string): void;
213
+ /** Delete a state item from a collection. */
214
+ deleteItem(namespace: string, key: string, itemKey: string): void;
215
+ /** Count state items in a collection. */
216
+ countItems(namespace: string, key: string): number;
217
+ /** Delete oldest items keeping only keepCount newest (by updated_at). Returns number deleted. */
218
+ pruneItems(namespace: string, key: string, keepCount: number): number;
219
+ /** List item_key values ordered by updated_at. Default order is desc. */
220
+ listItemKeys(namespace: string, key: string, options?: {
221
+ limit?: number;
222
+ order?: 'asc' | 'desc';
223
+ }): string[];
224
+ /** Add an item to a queue with optional priority and max attempts. Returns the queue item ID, or -1 if skipped due to deduplication. */
140
225
  enqueue(queue: string, payload: unknown, options?: {
141
226
  priority?: number;
142
227
  maxAttempts?: number;
143
228
  }): number;
144
- dequeue(queue: string, count?: number): Array<{
145
- id: number;
146
- payload: unknown;
147
- }>;
229
+ /**
230
+ * Claim and retrieve items from a queue for processing. Returns array of queue items with id and payload.
231
+ * @param queue - Queue name
232
+ * @param count - Number of items to dequeue (default 1)
233
+ */
234
+ dequeue(queue: string, count?: number): QueueItem[];
235
+ /** Mark a queue item as successfully completed. */
148
236
  done(queueItemId: number): void;
237
+ /** Mark a queue item as failed with optional error message. Retries if under max_attempts, else dead-letters. */
149
238
  fail(queueItemId: number, error?: string): void;
239
+ /** Close the database connection. */
150
240
  close(): void;
151
241
  }
152
242
  /**
@@ -159,38 +249,121 @@ declare function createClient(dbPath?: string): RunnerClient;
159
249
  */
160
250
  /** Result of a job execution. */
161
251
  interface ExecutionResult {
252
+ /** Execution outcome: 'ok', 'error', or 'timeout'. */
162
253
  status: 'ok' | 'error' | 'timeout';
254
+ /** Process exit code (null if timeout or spawn error). */
163
255
  exitCode: number | null;
256
+ /** Total execution duration in milliseconds. */
164
257
  durationMs: number;
258
+ /** Token count parsed from JR_RESULT output (for LLM jobs). */
165
259
  tokens: number | null;
260
+ /** Additional result metadata parsed from JR_RESULT output. */
166
261
  resultMeta: string | null;
262
+ /** Last N lines of stdout. */
167
263
  stdoutTail: string;
264
+ /** Last N lines of stderr. */
168
265
  stderrTail: string;
266
+ /** Error message if execution failed. */
169
267
  error: string | null;
170
268
  }
269
+ /** Command resolution result. */
270
+ interface ResolvedCommand {
271
+ /** Command to execute. */
272
+ command: string;
273
+ /** Arguments to pass to the command. */
274
+ args: string[];
275
+ }
171
276
  /** Options for executing a job script. */
172
277
  interface ExecutionOptions {
278
+ /** Path to the script file to execute. */
173
279
  script: string;
280
+ /** Path to the SQLite database for job state access. */
174
281
  dbPath: string;
282
+ /** Job identifier (passed as JR_JOB_ID env var). */
175
283
  jobId: string;
284
+ /** Run identifier (passed as JR_RUN_ID env var). */
176
285
  runId: number;
286
+ /** Optional execution timeout in milliseconds. */
177
287
  timeoutMs?: number;
288
+ /** Optional custom command resolver (for extensibility). */
289
+ commandResolver?: (script: string) => ResolvedCommand;
178
290
  }
179
291
  /**
180
292
  * Execute a job script as a child process. Captures output, parses metadata, enforces timeout.
181
293
  */
182
294
  declare function executeJob(options: ExecutionOptions): Promise<ExecutionResult>;
183
295
 
296
+ /**
297
+ * OpenClaw Gateway HTTP client for spawning and monitoring sessions.
298
+ */
299
+ /** Options for creating a Gateway client. */
300
+ interface GatewayClientOptions {
301
+ /** Gateway base URL (e.g., http://127.0.0.1:18789). */
302
+ url: string;
303
+ /** Bearer token for authentication. */
304
+ token: string;
305
+ /** Request timeout in milliseconds. */
306
+ timeoutMs?: number;
307
+ }
308
+ /** Options for spawning a session. */
309
+ interface SpawnSessionOptions {
310
+ /** Optional session label. */
311
+ label?: string;
312
+ /** Thinking level: low, medium, or high. */
313
+ thinking?: 'low' | 'medium' | 'high';
314
+ /** Run timeout in seconds. */
315
+ runTimeoutSeconds?: number;
316
+ }
317
+ /** Result of spawning a session. */
318
+ interface SpawnSessionResult {
319
+ /** Session key for tracking. */
320
+ sessionKey: string;
321
+ /** Run identifier. */
322
+ runId: string;
323
+ }
324
+ /** Session history message. */
325
+ interface SessionMessage {
326
+ /** Message role (user, assistant, etc.). */
327
+ role: string;
328
+ /** Stop reason (if present, indicates completion). */
329
+ stopReason?: string;
330
+ }
331
+ /** Session information from sessions_list. */
332
+ interface SessionInfo {
333
+ /** Total tokens used. */
334
+ totalTokens: number;
335
+ /** Model name. */
336
+ model: string;
337
+ /** Transcript file path (if available). */
338
+ transcriptPath?: string;
339
+ }
340
+ /** Gateway client for invoking tools via /tools/invoke. */
341
+ interface GatewayClient {
342
+ /** Spawn a new session with the given task. */
343
+ spawnSession(task: string, options?: SpawnSessionOptions): Promise<SpawnSessionResult>;
344
+ /** Get session history messages. */
345
+ getSessionHistory(sessionKey: string, limit?: number): Promise<SessionMessage[]>;
346
+ /** Get session info (tokens, model, etc.). Returns null if not found. */
347
+ getSessionInfo(sessionKey: string): Promise<SessionInfo | null>;
348
+ /** Check if session is complete. */
349
+ isSessionComplete(sessionKey: string): Promise<boolean>;
350
+ }
351
+ /** Create a Gateway client. */
352
+ declare function createGatewayClient(options: GatewayClientOptions): GatewayClient;
353
+
184
354
  /**
185
355
  * Slack notification module. Sends job completion/failure messages via Slack Web API (chat.postMessage). Falls back gracefully if no token.
186
356
  */
187
357
  /** Notification configuration. */
188
358
  interface NotifyConfig {
359
+ /** Slack bot token for posting messages (null if not configured). */
189
360
  slackToken: string | null;
190
361
  }
191
362
  /** Notifier interface for job completion events. */
192
363
  interface Notifier {
364
+ /** Send a success notification to a Slack channel. */
193
365
  notifySuccess(jobName: string, durationMs: number, channel: string): Promise<void>;
366
+ /** Send a failure notification to a Slack channel. */
194
367
  notifyFailure(jobName: string, durationMs: number, error: string | null, channel: string): Promise<void>;
195
368
  }
196
369
  /**
@@ -204,24 +377,61 @@ declare function createNotifier(config: NotifyConfig): Notifier;
204
377
 
205
378
  /** Scheduler dependencies. */
206
379
  interface SchedulerDeps {
380
+ /** SQLite database connection. */
207
381
  db: DatabaseSync;
382
+ /** Job executor function. */
208
383
  executor: typeof executeJob;
384
+ /** Notification service for job completion events. */
209
385
  notifier: Notifier;
386
+ /** Runner configuration. */
210
387
  config: RunnerConfig;
388
+ /** Logger instance. */
211
389
  logger: Logger;
390
+ /** Optional Gateway client for session-type jobs. */
391
+ gatewayClient?: GatewayClient;
212
392
  }
213
- /** Scheduler interface. */
393
+ /** Scheduler interface for managing job schedules and execution. */
214
394
  interface Scheduler {
395
+ /** Initialize and start the scheduler, loading all enabled jobs. */
215
396
  start(): void;
216
- stop(): void;
397
+ /** Stop the scheduler and gracefully wait for running jobs to complete. */
398
+ stop(): Promise<void>;
399
+ /** Manually trigger a job by ID, bypassing the schedule. */
217
400
  triggerJob(jobId: string): Promise<ExecutionResult>;
401
+ /** Force an immediate reconciliation of job schedules with the database. */
402
+ reconcileNow(): void;
403
+ /** Get list of currently running job IDs. */
218
404
  getRunningJobs(): string[];
405
+ /** Get list of job IDs that failed to register cron schedules. */
406
+ getFailedRegistrations(): string[];
219
407
  }
220
408
  /**
221
409
  * Create the job scheduler. Manages cron schedules, job execution, overlap policies, and notifications.
222
410
  */
223
411
  declare function createScheduler(deps: SchedulerDeps): Scheduler;
224
412
 
413
+ /**
414
+ * Session executor for job type='session'. Spawns OpenClaw Gateway sessions and polls for completion.
415
+ */
416
+
417
+ /** Options for executing a session job. */
418
+ interface SessionExecutionOptions {
419
+ /** Script field from job: can be raw prompt, path to .md/.txt, or legacy .js dispatcher. */
420
+ script: string;
421
+ /** Job identifier (used as session label). */
422
+ jobId: string;
423
+ /** Optional execution timeout in milliseconds. */
424
+ timeoutMs?: number;
425
+ /** Gateway client instance. */
426
+ gatewayClient: GatewayClient;
427
+ /** Initial poll interval in milliseconds (default 5000). Exposed for testing. */
428
+ pollIntervalMs?: number;
429
+ }
430
+ /**
431
+ * Execute a session job: spawn a Gateway session, poll for completion, fetch token usage.
432
+ */
433
+ declare function executeSession(options: SessionExecutionOptions): Promise<ExecutionResult>;
434
+
225
435
  /**
226
436
  * SQLite connection manager. Creates DB file with parent directories, enables WAL mode for concurrency.
227
437
  */
@@ -242,12 +452,16 @@ declare function closeConnection(db: DatabaseSync): void;
242
452
 
243
453
  /** Configuration for maintenance tasks. */
244
454
  interface MaintenanceConfig {
455
+ /** Number of days to retain completed run records before pruning. */
245
456
  runRetentionDays: number;
457
+ /** Interval in milliseconds between maintenance task runs. */
246
458
  cursorCleanupIntervalMs: number;
247
459
  }
248
460
  /** Maintenance controller with start/stop lifecycle. */
249
461
  interface Maintenance {
462
+ /** Start maintenance tasks (runs immediately, then on interval). */
250
463
  start(): void;
464
+ /** Stop maintenance task interval. */
251
465
  stop(): void;
252
466
  /** Run all maintenance tasks immediately (useful for testing and startup). */
253
467
  runNow(): void;
@@ -266,5 +480,5 @@ declare function createMaintenance(db: DatabaseSync, config: MaintenanceConfig,
266
480
  */
267
481
  declare function runMigrations(db: DatabaseSync): void;
268
482
 
269
- export { closeConnection, createClient, createConnection, createMaintenance, createNotifier, createRunner, createScheduler, executeJob, jobSchema, runMigrations, runSchema, runStatusSchema, runTriggerSchema, runnerConfigSchema };
270
- export type { ExecutionOptions, ExecutionResult, Job, Maintenance, MaintenanceConfig, Notifier, NotifyConfig, Run, RunStatus, RunTrigger, Runner, RunnerClient, RunnerConfig, Scheduler, SchedulerDeps };
483
+ export { closeConnection, createClient, createConnection, createGatewayClient, createMaintenance, createNotifier, createRunner, createScheduler, executeJob, executeSession, jobSchema, queueSchema, runMigrations, runSchema, runStatusSchema, runTriggerSchema, runnerConfigSchema };
484
+ export type { ExecutionOptions, ExecutionResult, GatewayClient, GatewayClientOptions, Job, Maintenance, MaintenanceConfig, Notifier, NotifyConfig, Queue, QueueItem, Run, RunStatus, RunTrigger, Runner, RunnerClient, RunnerConfig, Scheduler, SchedulerDeps, SessionExecutionOptions, SessionInfo, SessionMessage, SpawnSessionOptions, SpawnSessionResult };