11import { Type } from "@sinclair/typebox" ;
2+ import type { CronDelivery , CronMessageChannel } from "../../cron/types.js" ;
23import { loadConfig } from "../../config/config.js" ;
34import { normalizeCronJobCreate , normalizeCronJobPatch } from "../../cron/normalize.js" ;
4- import type { CronDelivery , CronMessageChannel } from "../../cron/types.js" ;
55import { normalizeHttpWebhookUrl } from "../../cron/webhook-url.js" ;
66import { parseAgentSessionKey } from "../../sessions/session-key-utils.js" ;
77import { extractTextFromChatContent } from "../../shared/chat-content.js" ;
@@ -50,6 +50,12 @@ type CronToolOptions = {
5050 agentSessionKey ?: string ;
5151} ;
5252
53+ type GatewayToolCaller = typeof callGatewayTool ;
54+
55+ type CronToolDeps = {
56+ callGatewayTool ?: GatewayToolCaller ;
57+ } ;
58+
5359type ChatMessage = {
5460 role ?: unknown ;
5561 content ?: unknown ;
@@ -84,6 +90,7 @@ async function buildReminderContextLines(params: {
8490 agentSessionKey ?: string ;
8591 gatewayOpts : GatewayCallOptions ;
8692 contextMessages : number ;
93+ callGatewayTool : GatewayToolCaller ;
8794} ) {
8895 const maxMessages = Math . min (
8996 REMINDER_CONTEXT_MESSAGES_MAX ,
@@ -100,7 +107,7 @@ async function buildReminderContextLines(params: {
100107 const { mainKey, alias } = resolveMainSessionAlias ( cfg ) ;
101108 const resolvedKey = resolveInternalSessionKey ( { key : sessionKey , alias, mainKey } ) ;
102109 try {
103- const res = await callGatewayTool < { messages : Array < unknown > } > (
110+ const res = await params . callGatewayTool < { messages : Array < unknown > } > (
104111 "chat.history" ,
105112 params . gatewayOpts ,
106113 {
@@ -197,7 +204,8 @@ function inferDeliveryFromSessionKey(agentSessionKey?: string): CronDelivery | n
197204 return delivery ;
198205}
199206
200- export function createCronTool ( opts ?: CronToolOptions ) : AnyAgentTool {
207+ export function createCronTool ( opts ?: CronToolOptions , deps ?: CronToolDeps ) : AnyAgentTool {
208+ const callGateway = deps ?. callGatewayTool ?? callGatewayTool ;
201209 return {
202210 label : "Cron" ,
203211 name : "cron" ,
@@ -272,10 +280,10 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
272280
273281 switch ( action ) {
274282 case "status" :
275- return jsonResult ( await callGatewayTool ( "cron.status" , gatewayOpts , { } ) ) ;
283+ return jsonResult ( await callGateway ( "cron.status" , gatewayOpts , { } ) ) ;
276284 case "list" :
277285 return jsonResult (
278- await callGatewayTool ( "cron.list" , gatewayOpts , {
286+ await callGateway ( "cron.list" , gatewayOpts , {
279287 includeDisabled : Boolean ( params . includeDisabled ) ,
280288 } ) ,
281289 ) ;
@@ -412,14 +420,15 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
412420 agentSessionKey : opts ?. agentSessionKey ,
413421 gatewayOpts,
414422 contextMessages,
423+ callGatewayTool : callGateway ,
415424 } ) ;
416425 if ( contextLines . length > 0 ) {
417426 const baseText = stripExistingContext ( payload . text ) ;
418427 payload . text = `${ baseText } ${ REMINDER_CONTEXT_MARKER } ${ contextLines . join ( "\n" ) } ` ;
419428 }
420429 }
421430 }
422- return jsonResult ( await callGatewayTool ( "cron.add" , gatewayOpts , job ) ) ;
431+ return jsonResult ( await callGateway ( "cron.add" , gatewayOpts , job ) ) ;
423432 }
424433 case "update" : {
425434 const id = readStringParam ( params , "jobId" ) ?? readStringParam ( params , "id" ) ;
@@ -431,7 +440,7 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
431440 }
432441 const patch = normalizeCronJobPatch ( params . patch ) ?? params . patch ;
433442 return jsonResult (
434- await callGatewayTool ( "cron.update" , gatewayOpts , {
443+ await callGateway ( "cron.update" , gatewayOpts , {
435444 id,
436445 patch,
437446 } ) ,
@@ -442,7 +451,7 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
442451 if ( ! id ) {
443452 throw new Error ( "jobId required (id accepted for backward compatibility)" ) ;
444453 }
445- return jsonResult ( await callGatewayTool ( "cron.remove" , gatewayOpts , { id } ) ) ;
454+ return jsonResult ( await callGateway ( "cron.remove" , gatewayOpts , { id } ) ) ;
446455 }
447456 case "run" : {
448457 const id = readStringParam ( params , "jobId" ) ?? readStringParam ( params , "id" ) ;
@@ -451,14 +460,14 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
451460 }
452461 const runMode =
453462 params . runMode === "due" || params . runMode === "force" ? params . runMode : "force" ;
454- return jsonResult ( await callGatewayTool ( "cron.run" , gatewayOpts , { id, mode : runMode } ) ) ;
463+ return jsonResult ( await callGateway ( "cron.run" , gatewayOpts , { id, mode : runMode } ) ) ;
455464 }
456465 case "runs" : {
457466 const id = readStringParam ( params , "jobId" ) ?? readStringParam ( params , "id" ) ;
458467 if ( ! id ) {
459468 throw new Error ( "jobId required (id accepted for backward compatibility)" ) ;
460469 }
461- return jsonResult ( await callGatewayTool ( "cron.runs" , gatewayOpts , { id } ) ) ;
470+ return jsonResult ( await callGateway ( "cron.runs" , gatewayOpts , { id } ) ) ;
462471 }
463472 case "wake" : {
464473 const text = readStringParam ( params , "text" , { required : true } ) ;
@@ -467,7 +476,7 @@ Use jobId as the canonical identifier; id is accepted for compatibility. Use con
467476 ? params . mode
468477 : "next-heartbeat" ;
469478 return jsonResult (
470- await callGatewayTool ( "wake" , gatewayOpts , { mode, text } , { expectFinal : false } ) ,
479+ await callGateway ( "wake" , gatewayOpts , { mode, text } , { expectFinal : false } ) ,
471480 ) ;
472481 }
473482 default :
0 commit comments