@@ -8,7 +8,10 @@ import {
88import { resolveStableChannelMessageIngress } from "openclaw/plugin-sdk/channel-ingress-runtime" ;
99import { createChannelPairingController } from "openclaw/plugin-sdk/channel-pairing" ;
1010import type { MarkdownTableMode , OpenClawConfig } from "openclaw/plugin-sdk/config-contracts" ;
11- import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime" ;
11+ import {
12+ createLazyRuntimeModule ,
13+ createLazyRuntimeNamedExport ,
14+ } from "openclaw/plugin-sdk/lazy-runtime" ;
1215import {
1316 deliverTextOrMediaReply ,
1417 resolveSendableOutboundReplyParts ,
@@ -48,6 +51,7 @@ import {
4851} from "./outbound-media.js" ;
4952import { resolveZaloProxyFetch } from "./proxy.js" ;
5053import { getZaloRuntime } from "./runtime.js" ;
54+ import type { ZaloWebhookIngressLifecycle } from "./webhook-spool.js" ;
5155
5256/** Default idle timeout for Zalo inbound photo downloads (30 seconds). */
5357const ZALO_MEDIA_READ_IDLE_TIMEOUT_MS = 30_000 ;
@@ -88,6 +92,7 @@ type ZaloProcessingContext = {
8892 webhookPath ?: string ;
8993 statusSink ?: ZaloStatusSink ;
9094 fetcher ?: ZaloFetch ;
95+ turnAdoptionLifecycle ?: ZaloWebhookIngressLifecycle ;
9196} ;
9297
9398type ZaloPollingLoopParams = ZaloProcessingContext & {
@@ -106,7 +111,18 @@ function resolveZaloTimestampMs(date: number | undefined): number | undefined {
106111 return date >= UNIX_MILLISECONDS_THRESHOLD ? date : date * 1000 ;
107112}
108113
109- const loadZaloWebhookModule = createLazyRuntimeModule ( ( ) => import ( "./monitor.webhook.js" ) ) ;
114+ const loadZaloWebhookRuntime = createLazyRuntimeNamedExport (
115+ ( ) => import ( "./monitor.webhook.js" ) ,
116+ "zaloWebhookRuntime" ,
117+ ) ;
118+ const loadZaloWebhookIngressRuntime = createLazyRuntimeNamedExport (
119+ ( ) => import ( "./webhook-spool.js" ) ,
120+ "zaloWebhookIngressRuntime" ,
121+ ) ;
122+ const loadZaloWebhookModule = createLazyRuntimeModule ( async ( ) => ( {
123+ ...( await loadZaloWebhookRuntime ( ) ) ,
124+ ...( await loadZaloWebhookIngressRuntime ( ) ) ,
125+ } ) ) ;
110126
111127function releaseSharedHostedMediaRouteRef ( routePath : string ) : void {
112128 const current = hostedMediaRouteRefs . get ( routePath ) ;
@@ -210,22 +226,7 @@ async function handleZaloWebhookRequest(
210226) : Promise < boolean > {
211227 const { handleZaloWebhookRequest : handleZaloWebhookRequestInternal } =
212228 await loadZaloWebhookModule ( ) ;
213- return await handleZaloWebhookRequestInternal ( req , res , async ( { update, target } ) => {
214- await processUpdate ( {
215- update,
216- token : target . token ,
217- account : target . account ,
218- config : target . config ,
219- runtime : target . runtime ,
220- core : target . core as ZaloCoreRuntime ,
221- mediaMaxMb : target . mediaMaxMb ,
222- canHostMedia : target . canHostMedia ,
223- webhookUrl : target . webhookUrl ,
224- webhookPath : target . webhookPath ,
225- statusSink : target . statusSink ,
226- fetcher : target . fetcher ,
227- } ) ;
228- } ) ;
229+ return await handleZaloWebhookRequestInternal ( req , res ) ;
229230}
230231
231232function startPollingLoop ( params : ZaloPollingLoopParams ) {
@@ -313,6 +314,7 @@ async function processUpdate(params: ZaloUpdateProcessingParams): Promise<void>
313314 webhookPath : params . webhookPath ,
314315 statusSink,
315316 fetcher,
317+ turnAdoptionLifecycle : params . turnAdoptionLifecycle ,
316318 } ;
317319 if ( ! message ) {
318320 return undefined ;
@@ -725,6 +727,9 @@ async function processMessageWithPipeline(params: ZaloMessagePipelineParams): Pr
725727 runtime . error ?.( `zalo: failed updating session meta: ${ String ( err ) } ` ) ;
726728 } ,
727729 } ,
730+ ...( params . turnAdoptionLifecycle
731+ ? { turnAdoptionLifecycle : params . turnAdoptionLifecycle }
732+ : { } ) ,
728733 } ) ;
729734}
730735
@@ -840,6 +845,7 @@ export async function monitorZaloProvider(options: ZaloMonitorOptions): Promise<
840845
841846 let stopped = false ;
842847 const stopHandlers : Array < ( ) => void > = [ ] ;
848+ const asyncStopHandlers : Array < ( ) => Promise < void > > = [ ] ;
843849 let cleanupWebhook : ( ( ) => Promise < void > ) | undefined ;
844850
845851 const stop = ( ) => {
@@ -874,7 +880,7 @@ export async function monitorZaloProvider(options: ZaloMonitorOptions): Promise<
874880 }
875881
876882 if ( useWebhook ) {
877- const { registerZaloWebhookTarget } = await loadZaloWebhookModule ( ) ;
883+ const { createZaloWebhookIngress , registerZaloWebhookTarget } = await loadZaloWebhookModule ( ) ;
878884 if ( ! effectiveWebhookUrl || ! webhookSecret ) {
879885 throw new Error ( "Zalo webhookUrl and webhookSecret are required for webhook mode" ) ;
880886 }
@@ -893,43 +899,38 @@ export async function monitorZaloProvider(options: ZaloMonitorOptions): Promise<
893899 runtime . log ?.(
894900 `[${ account . accountId } ] Zalo configuring webhook path=${ path } target=${ describeWebhookTarget ( effectiveWebhookUrl ) } ` ,
895901 ) ;
896- await setWebhook ( token , { url : effectiveWebhookUrl , secret_token : webhookSecret } , fetcher ) ;
897- let webhookCleanupPromise : Promise < void > | undefined ;
898- cleanupWebhook = async ( ) => {
899- if ( ! webhookCleanupPromise ) {
900- webhookCleanupPromise = ( async ( ) => {
901- runtime . log ?.( `[${ account . accountId } ] Zalo stopping; deleting webhook` ) ;
902- try {
903- await deleteWebhook ( token , fetcher , WEBHOOK_CLEANUP_TIMEOUT_MS ) ;
904- runtime . log ?.( `[${ account . accountId } ] Zalo webhook deleted` ) ;
905- } catch ( err ) {
906- const detail =
907- err instanceof Error && err . name === "AbortError"
908- ? `timed out after ${ String ( WEBHOOK_CLEANUP_TIMEOUT_MS ) } ms`
909- : formatZaloError ( err ) ;
910- runtime . error ?.( `[${ account . accountId } ] Zalo webhook delete failed: ${ detail } ` ) ;
911- }
912- } ) ( ) ;
913- }
914- await webhookCleanupPromise ;
915- } ;
916- runtime . log ?.( `[${ account . accountId } ] Zalo webhook registered path=${ path } ` ) ;
917-
902+ const ingress = createZaloWebhookIngress ( {
903+ accountId : account . accountId ,
904+ runtime,
905+ deliver : async ( update , turnAdoptionLifecycle ) => {
906+ statusSink ?.( { lastInboundAt : Date . now ( ) } ) ;
907+ await processUpdate ( {
908+ update,
909+ token,
910+ account,
911+ config,
912+ runtime,
913+ core,
914+ mediaMaxMb : effectiveMediaMaxMb ,
915+ canHostMedia,
916+ webhookUrl : effectiveWebhookUrl ,
917+ webhookPath : path ,
918+ statusSink,
919+ fetcher,
920+ turnAdoptionLifecycle,
921+ } ) ;
922+ } ,
923+ } ) ;
924+ ingress . start ( ) ;
925+ asyncStopHandlers . push ( ingress . stop ) ;
918926 const unregister = registerZaloWebhookTarget (
919927 {
920- token,
921928 account,
922929 config,
923930 runtime,
924- core,
925931 path,
926- webhookUrl : effectiveWebhookUrl ,
927- webhookPath : path ,
928932 secret : webhookSecret ,
929- statusSink : ( patch ) => statusSink ?.( patch ) ,
930- mediaMaxMb : effectiveMediaMaxMb ,
931- canHostMedia,
932- fetcher,
933+ acceptWebhook : ingress . accept ,
933934 } ,
934935 {
935936 route : {
@@ -951,6 +952,31 @@ export async function monitorZaloProvider(options: ZaloMonitorOptions): Promise<
951952 } ,
952953 ) ;
953954 stopHandlers . push ( unregister ) ;
955+ await setWebhook (
956+ token ,
957+ { url : effectiveWebhookUrl , secret_token : webhookSecret } , // pragma: allowlist secret
958+ fetcher ,
959+ ) ;
960+ let webhookCleanupPromise : Promise < void > | undefined ;
961+ cleanupWebhook = async ( ) => {
962+ if ( ! webhookCleanupPromise ) {
963+ webhookCleanupPromise = ( async ( ) => {
964+ runtime . log ?.( `[${ account . accountId } ] Zalo stopping; deleting webhook` ) ;
965+ try {
966+ await deleteWebhook ( token , fetcher , WEBHOOK_CLEANUP_TIMEOUT_MS ) ;
967+ runtime . log ?.( `[${ account . accountId } ] Zalo webhook deleted` ) ;
968+ } catch ( err ) {
969+ const detail =
970+ err instanceof Error && err . name === "AbortError"
971+ ? `timed out after ${ String ( WEBHOOK_CLEANUP_TIMEOUT_MS ) } ms`
972+ : formatZaloError ( err ) ;
973+ runtime . error ?.( `[${ account . accountId } ] Zalo webhook delete failed: ${ detail } ` ) ;
974+ }
975+ } ) ( ) ;
976+ }
977+ await webhookCleanupPromise ;
978+ } ;
979+ runtime . log ?.( `[${ account . accountId } ] Zalo webhook registered path=${ path } ` ) ;
954980 await waitForAbortSignal ( abortSignal ) ;
955981 return ;
956982 }
@@ -1012,6 +1038,9 @@ export async function monitorZaloProvider(options: ZaloMonitorOptions): Promise<
10121038 abortSignal . removeEventListener ( "abort" , stopOnAbort ) ;
10131039 await cleanupWebhook ?.( ) ;
10141040 stop ( ) ;
1041+ for ( const stopAsync of asyncStopHandlers ) {
1042+ await stopAsync ( ) ;
1043+ }
10151044 runtime . log ?.( `[${ account . accountId } ] Zalo provider stopped mode=${ mode } ` ) ;
10161045 }
10171046}
0 commit comments