diff --git a/src/cron/isolated-agent/delivery-dispatch.ts b/src/cron/isolated-agent/delivery-dispatch.ts index 96922e7000a..d09dd317bd2 100644 --- a/src/cron/isolated-agent/delivery-dispatch.ts +++ b/src/cron/isolated-agent/delivery-dispatch.ts @@ -1,20 +1,14 @@ import { countActiveDescendantRuns } from "../../agents/subagent-registry-read.js"; import { SILENT_REPLY_TOKEN } from "../../auto-reply/tokens.js"; import type { ReplyPayload } from "../../auto-reply/types.js"; -import { createOutboundSendDeps, type CliDeps } from "../../cli/outbound-send-deps.js"; +import type { CliDeps } from "../../cli/outbound-send-deps.js"; import type { OpenClawConfig } from "../../config/config.js"; import { resolveAgentMainSessionKey, resolveMainSessionKey, } from "../../config/sessions/main-session.js"; import { sleepWithAbort } from "../../infra/backoff.js"; -import { - deliverOutboundPayloads, - type OutboundDeliveryResult, -} from "../../infra/outbound/deliver.js"; -import { resolveAgentOutboundIdentity } from "../../infra/outbound/identity.js"; -import { buildOutboundSessionContext } from "../../infra/outbound/session-context.js"; -import { enqueueSystemEvent } from "../../infra/system-events.js"; +import type { OutboundDeliveryResult } from "../../infra/outbound/deliver.js"; import { logWarn, logError } from "../../logger.js"; import type { CronJob, CronRunTelemetry } from "../types.js"; import type { DeliveryTargetResolution } from "./delivery-target.js"; @@ -136,6 +130,9 @@ type CompletedDirectCronDelivery = { }; let gatewayCallRuntimePromise: Promise | undefined; +let deliveryOutboundRuntimePromise: + | Promise + | undefined; let subagentFollowupRuntimePromise: | Promise | undefined; @@ -147,6 +144,13 @@ async function loadGatewayCallRuntime(): Promise { + deliveryOutboundRuntimePromise ??= import("./delivery-outbound.runtime.js"); + return await deliveryOutboundRuntimePromise; +} + async function loadSubagentFollowupRuntime(): Promise< typeof import("./subagent-followup.runtime.js") > { @@ -254,20 +258,21 @@ function resolveCronAwarenessMainSessionKey(params: { : resolveAgentMainSessionKey({ cfg: params.cfg, agentId: params.agentId }); } -function queueCronAwarenessSystemEvent(params: { +async function queueCronAwarenessSystemEvent(params: { cfg: OpenClawConfig; jobId: string; agentId: string; deliveryIdempotencyKey: string; outputText?: string; synthesizedText?: string; -}): void { +}): Promise { const text = params.outputText?.trim() || params.synthesizedText?.trim() || undefined; if (!text) { return; } try { + const { enqueueSystemEvent } = await loadDeliveryOutboundRuntime(); enqueueSystemEvent(text, { sessionKey: resolveCronAwarenessMainSessionKey({ cfg: params.cfg, @@ -379,6 +384,12 @@ export async function dispatchCronDelivery( delivery: SuccessfulDeliveryTarget, options?: { retryTransient?: boolean }, ): Promise => { + const { + buildOutboundSessionContext, + createOutboundSendDeps, + deliverOutboundPayloads, + resolveAgentOutboundIdentity, + } = await loadDeliveryOutboundRuntime(); const identity = resolveAgentOutboundIdentity(params.cfgWithAgentDefaults, params.agentId); const deliveryIdempotencyKey = buildDirectCronDeliveryIdempotencyKey({ runSessionId: params.runSessionId, @@ -490,7 +501,7 @@ export async function dispatchCronDelivery( // successful subset, but caching it here would permanently drop the // failed payloads by converting the replay into delivered=true. if (delivered && shouldQueueCronAwareness(params.job, params.deliveryBestEffort)) { - queueCronAwarenessSystemEvent({ + await queueCronAwarenessSystemEvent({ cfg: params.cfgWithAgentDefaults, jobId: params.job.id, agentId: params.agentId, diff --git a/src/cron/isolated-agent/delivery-outbound.runtime.ts b/src/cron/isolated-agent/delivery-outbound.runtime.ts new file mode 100644 index 00000000000..fe8244b13aa --- /dev/null +++ b/src/cron/isolated-agent/delivery-outbound.runtime.ts @@ -0,0 +1,8 @@ +export { createOutboundSendDeps } from "../../cli/outbound-send-deps.js"; +export { + deliverOutboundPayloads, + type OutboundDeliveryResult, +} from "../../infra/outbound/deliver.js"; +export { resolveAgentOutboundIdentity } from "../../infra/outbound/identity.js"; +export { buildOutboundSessionContext } from "../../infra/outbound/session-context.js"; +export { enqueueSystemEvent } from "../../infra/system-events.js";