@@ -32,7 +32,22 @@ import {
3232import { listConfiguredAnnounceChannelIdsForConfig } from "../../plugins/channel-plugin-ids.js" ;
3333import { isSubagentSessionKey } from "../../routing/session-key.js" ;
3434import { normalizeMessageChannel } from "../../utils/message-channel.js" ;
35- import type { GatewayRequestHandlers } from "./types.js" ;
35+ import type { GatewayRequestHandlers , RespondFn } from "./types.js" ;
36+
37+ type CronJobIdParams = { id ?: string ; jobId ?: string } ;
38+
39+ type CronRunsRequestParams = CronJobIdParams & {
40+ scope ?: "job" | "all" ;
41+ runId ?: string ;
42+ limit ?: number ;
43+ offset ?: number ;
44+ statuses ?: Array < "ok" | "error" | "skipped" > ;
45+ status ?: "all" | "ok" | "error" | "skipped" ;
46+ deliveryStatuses ?: Array < "delivered" | "not-delivered" | "unknown" | "not-requested" > ;
47+ deliveryStatus ?: "delivered" | "not-delivered" | "unknown" | "not-requested" ;
48+ query ?: string ;
49+ sortDir ?: "asc" | "desc" ;
50+ } ;
3651
3752function listConfiguredAnnounceChannelIds ( cfg : OpenClawConfig ) : string [ ] {
3853 return listConfiguredAnnounceChannelIdsForConfig ( {
@@ -167,6 +182,36 @@ function assertValidCronUpdateDelivery(params: {
167182 } ) ;
168183}
169184
185+ function resolveCronJobId ( params : CronJobIdParams ) : string | undefined {
186+ return params . id ?? params . jobId ;
187+ }
188+
189+ function respondInvalidCronParams ( respond : RespondFn , method : string , reason : string ) : void {
190+ respond (
191+ false ,
192+ undefined ,
193+ errorShape ( ErrorCodes . INVALID_REQUEST , `invalid ${ method } params: ${ reason } ` ) ,
194+ ) ;
195+ }
196+
197+ function respondMissingCronJobId ( respond : RespondFn , method : string ) : void {
198+ respondInvalidCronParams ( respond , method , "missing id" ) ;
199+ }
200+
201+ function cronRunLogPageFilters ( params : CronRunsRequestParams ) {
202+ return {
203+ limit : params . limit ,
204+ offset : params . offset ,
205+ statuses : params . statuses ,
206+ status : params . status ,
207+ runId : params . runId ,
208+ deliveryStatuses : params . deliveryStatuses ,
209+ deliveryStatus : params . deliveryStatus ,
210+ query : params . query ,
211+ sortDir : params . sortDir ,
212+ } ;
213+ }
214+
170215export const cronHandlers : GatewayRequestHandlers = {
171216 wake : ( { params, respond, context } ) => {
172217 if ( ! validateWakeParams ( params ) ) {
@@ -263,24 +308,16 @@ export const cronHandlers: GatewayRequestHandlers = {
263308 } ,
264309 "cron.get" : async ( { params, respond, context } ) => {
265310 if ( ! validateCronGetParams ( params ) ) {
266- respond (
267- false ,
268- undefined ,
269- errorShape (
270- ErrorCodes . INVALID_REQUEST ,
271- `invalid cron.get params: ${ formatValidationErrors ( validateCronGetParams . errors ) } ` ,
272- ) ,
311+ respondInvalidCronParams (
312+ respond ,
313+ "cron.get" ,
314+ formatValidationErrors ( validateCronGetParams . errors ) ,
273315 ) ;
274316 return ;
275317 }
276- const p = params as { id ?: string ; jobId ?: string } ;
277- const jobId = p . id ?? p . jobId ;
318+ const jobId = resolveCronJobId ( params as CronJobIdParams ) ;
278319 if ( ! jobId ) {
279- respond (
280- false ,
281- undefined ,
282- errorShape ( ErrorCodes . INVALID_REQUEST , "invalid cron.get params: missing id" ) ,
283- ) ;
320+ respondMissingCronJobId ( respond , "cron.get" ) ;
284321 return ;
285322 }
286323 const job = await context . cron . readJob ( jobId ) ;
@@ -468,24 +505,16 @@ export const cronHandlers: GatewayRequestHandlers = {
468505 } ,
469506 "cron.remove" : async ( { params, respond, context } ) => {
470507 if ( ! validateCronRemoveParams ( params ) ) {
471- respond (
472- false ,
473- undefined ,
474- errorShape (
475- ErrorCodes . INVALID_REQUEST ,
476- `invalid cron.remove params: ${ formatValidationErrors ( validateCronRemoveParams . errors ) } ` ,
477- ) ,
508+ respondInvalidCronParams (
509+ respond ,
510+ "cron.remove" ,
511+ formatValidationErrors ( validateCronRemoveParams . errors ) ,
478512 ) ;
479513 return ;
480514 }
481- const p = params as { id ?: string ; jobId ?: string } ;
482- const jobId = p . id ?? p . jobId ;
515+ const jobId = resolveCronJobId ( params as CronJobIdParams ) ;
483516 if ( ! jobId ) {
484- respond (
485- false ,
486- undefined ,
487- errorShape ( ErrorCodes . INVALID_REQUEST , "invalid cron.remove params: missing id" ) ,
488- ) ;
517+ respondMissingCronJobId ( respond , "cron.remove" ) ;
489518 return ;
490519 }
491520 const result = await context . cron . remove ( jobId ) ;
@@ -502,24 +531,17 @@ export const cronHandlers: GatewayRequestHandlers = {
502531 } ,
503532 "cron.run" : async ( { params, respond, context } ) => {
504533 if ( ! validateCronRunParams ( params ) ) {
505- respond (
506- false ,
507- undefined ,
508- errorShape (
509- ErrorCodes . INVALID_REQUEST ,
510- `invalid cron.run params: ${ formatValidationErrors ( validateCronRunParams . errors ) } ` ,
511- ) ,
534+ respondInvalidCronParams (
535+ respond ,
536+ "cron.run" ,
537+ formatValidationErrors ( validateCronRunParams . errors ) ,
512538 ) ;
513539 return ;
514540 }
515- const p = params as { id ?: string ; jobId ?: string ; mode ?: "due" | "force" } ;
516- const jobId = p . id ?? p . jobId ;
541+ const p = params as CronJobIdParams & { mode ?: "due" | "force" } ;
542+ const jobId = resolveCronJobId ( p ) ;
517543 if ( ! jobId ) {
518- respond (
519- false ,
520- undefined ,
521- errorShape ( ErrorCodes . INVALID_REQUEST , "invalid cron.run params: missing id" ) ,
522- ) ;
544+ respondMissingCronJobId ( respond , "cron.run" ) ;
523545 return ;
524546 }
525547 let result : Awaited < ReturnType < typeof context . cron . enqueueRun > > ;
@@ -536,39 +558,19 @@ export const cronHandlers: GatewayRequestHandlers = {
536558 } ,
537559 "cron.runs" : async ( { params, respond, context } ) => {
538560 if ( ! validateCronRunsParams ( params ) ) {
539- respond (
540- false ,
541- undefined ,
542- errorShape (
543- ErrorCodes . INVALID_REQUEST ,
544- `invalid cron.runs params: ${ formatValidationErrors ( validateCronRunsParams . errors ) } ` ,
545- ) ,
561+ respondInvalidCronParams (
562+ respond ,
563+ "cron.runs" ,
564+ formatValidationErrors ( validateCronRunsParams . errors ) ,
546565 ) ;
547566 return ;
548567 }
549- const p = params as {
550- scope ?: "job" | "all" ;
551- id ?: string ;
552- jobId ?: string ;
553- runId ?: string ;
554- limit ?: number ;
555- offset ?: number ;
556- statuses ?: Array < "ok" | "error" | "skipped" > ;
557- status ?: "all" | "ok" | "error" | "skipped" ;
558- deliveryStatuses ?: Array < "delivered" | "not-delivered" | "unknown" | "not-requested" > ;
559- deliveryStatus ?: "delivered" | "not-delivered" | "unknown" | "not-requested" ;
560- query ?: string ;
561- sortDir ?: "asc" | "desc" ;
562- } ;
568+ const p = params as CronRunsRequestParams ;
563569 const explicitScope = p . scope ;
564- const jobId = p . id ?? p . jobId ;
570+ const jobId = resolveCronJobId ( p ) ;
565571 const scope : "job" | "all" = explicitScope ?? ( jobId ? "job" : "all" ) ;
566572 if ( scope === "job" && ! jobId ) {
567- respond (
568- false ,
569- undefined ,
570- errorShape ( ErrorCodes . INVALID_REQUEST , "invalid cron.runs params: missing id" ) ,
571- ) ;
573+ respondMissingCronJobId ( respond , "cron.runs" ) ;
572574 return ;
573575 }
574576 if ( scope === "all" ) {
@@ -580,15 +582,7 @@ export const cronHandlers: GatewayRequestHandlers = {
580582 ) ;
581583 const page = await readCronRunLogEntriesPageAll ( {
582584 storePath : context . cronStorePath ,
583- limit : p . limit ,
584- offset : p . offset ,
585- statuses : p . statuses ,
586- status : p . status ,
587- runId : p . runId ,
588- deliveryStatuses : p . deliveryStatuses ,
589- deliveryStatus : p . deliveryStatus ,
590- query : p . query ,
591- sortDir : p . sortDir ,
585+ ...cronRunLogPageFilters ( p ) ,
592586 jobNameById,
593587 } ) ;
594588 respond ( true , page , undefined ) ;
@@ -598,15 +592,7 @@ export const cronHandlers: GatewayRequestHandlers = {
598592 const page = await readCronRunLogEntriesPage ( {
599593 storePath : context . cronStorePath ,
600594 jobId : jobId as string ,
601- limit : p . limit ,
602- offset : p . offset ,
603- statuses : p . statuses ,
604- status : p . status ,
605- runId : p . runId ,
606- deliveryStatuses : p . deliveryStatuses ,
607- deliveryStatus : p . deliveryStatus ,
608- query : p . query ,
609- sortDir : p . sortDir ,
595+ ...cronRunLogPageFilters ( p ) ,
610596 } ) ;
611597 respond ( true , page , undefined ) ;
612598 } catch ( err ) {
0 commit comments