@@ -4,16 +4,14 @@ import {
44 shouldDebounceTextInbound ,
55} from "openclaw/plugin-sdk/channel-inbound" ;
66import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime" ;
7+ import type { ChannelReplayClaimHandle } from "openclaw/plugin-sdk/persistent-dedupe" ;
78import { danger } from "openclaw/plugin-sdk/runtime-env" ;
89import { resolveOpenProviderRuntimeGroupPolicy } from "openclaw/plugin-sdk/runtime-group-policy" ;
910import type { Client } from "../internal/discord.js" ;
1011import {
1112 buildDiscordInboundReplayKey ,
12- claimDiscordInboundReplay ,
13- commitDiscordInboundReplay ,
1413 createDiscordInboundReplayGuard ,
1514 DiscordRetryableInboundError ,
16- releaseDiscordInboundReplay ,
1715} from "./inbound-dedupe.js" ;
1816import { buildDiscordInboundJob } from "./inbound-job.js" ;
1917import type { DiscordMessageEvent , DiscordMessageHandler } from "./listeners.js" ;
@@ -76,15 +74,14 @@ export function createDiscordMessageHandler(
7674 runtime : params . runtime ,
7775 setStatus : params . setStatus ,
7876 abortSignal : params . abortSignal ,
79- replayGuard,
8077 testing : params . testing ,
8178 } ) ;
8279
8380 const { debouncer } = createChannelInboundDebouncer < {
8481 data : DiscordMessageEvent ;
8582 client : Client ;
8683 abortSignal ?: AbortSignal ;
87- replayKey ?: string ;
84+ replayClaim ?: ChannelReplayClaimHandle ;
8885 } > ( {
8986 cfg : params . cfg ,
9087 channel : "discord" ,
@@ -122,14 +119,14 @@ export function createDiscordMessageHandler(
122119 if ( ! last ) {
123120 return ;
124121 }
125- const replayKeys = entries . map ( ( entry ) => entry . replayKey ) . filter ( isNonEmptyString ) ;
122+ const replayClaims = entries
123+ . map ( ( entry ) => entry . replayClaim )
124+ . filter ( ( claim ) : claim is ChannelReplayClaimHandle => claim !== undefined ) ;
126125 const abortSignal = last . abortSignal ;
127126 if ( abortSignal ?. aborted ) {
128- releaseDiscordInboundReplay ( {
129- replayKeys,
130- error : abortSignal . reason ,
131- replayGuard,
132- } ) ;
127+ for ( const claim of replayClaims ) {
128+ claim . release ( { error : abortSignal . reason } ) ;
129+ }
133130 return ;
134131 }
135132 try {
@@ -146,11 +143,11 @@ export function createDiscordMessageHandler(
146143 client : last . client ,
147144 } ) ;
148145 if ( ! ctx ) {
149- await commitDiscordInboundReplay ( { replayKeys , replayGuard } ) ;
146+ await Promise . all ( replayClaims . map ( ( claim ) => claim . commit ( ) ) ) ;
150147 return ;
151148 }
152149 applyImplicitReplyBatchGate ( ctx , params . replyToMode , false ) ;
153- messageRunQueue . enqueue ( buildDiscordInboundJob ( ctx , { replayKeys } ) ) ;
150+ messageRunQueue . enqueue ( buildDiscordInboundJob ( ctx , { replayClaims } ) ) ;
154151 return ;
155152 }
156153 const combinedBaseText = entries
@@ -195,7 +192,7 @@ export function createDiscordMessageHandler(
195192 client : last . client ,
196193 } ) ;
197194 if ( ! ctx ) {
198- await commitDiscordInboundReplay ( { replayKeys , replayGuard } ) ;
195+ await Promise . all ( replayClaims . map ( ( claim ) => claim . commit ( ) ) ) ;
199196 return ;
200197 }
201198 applyImplicitReplyBatchGate ( ctx , params . replyToMode , true ) ;
@@ -212,12 +209,14 @@ export function createDiscordMessageHandler(
212209 ctxBatch . MessageSidLast = ids [ ids . length - 1 ] ;
213210 }
214211 }
215- messageRunQueue . enqueue ( buildDiscordInboundJob ( ctx , { replayKeys } ) ) ;
212+ messageRunQueue . enqueue ( buildDiscordInboundJob ( ctx , { replayClaims } ) ) ;
216213 } catch ( error ) {
217214 if ( error instanceof DiscordRetryableInboundError ) {
218- releaseDiscordInboundReplay ( { replayKeys, error, replayGuard } ) ;
215+ for ( const claim of replayClaims ) {
216+ claim . release ( { error } ) ;
217+ }
219218 } else {
220- await commitDiscordInboundReplay ( { replayKeys , replayGuard } ) ;
219+ await Promise . all ( replayClaims . map ( ( claim ) => claim . commit ( ) ) ) ;
221220 }
222221 throw error ;
223222 }
@@ -245,20 +244,16 @@ export function createDiscordMessageHandler(
245244 accountId : params . accountId ,
246245 data,
247246 } ) ;
248- if (
249- ! ( await claimDiscordInboundReplay ( {
250- replayKey,
251- replayGuard,
252- } ) )
253- ) {
247+ const replayClaim = await replayGuard . claim ( replayKey ) ;
248+ if ( replayClaim . kind !== "claimed" && replayClaim . kind !== "invalid" ) {
254249 return ;
255250 }
256251
257252 await debouncer . enqueue ( {
258253 data,
259254 client,
260255 abortSignal : options ?. abortSignal ,
261- replayKey : replayKey ?? undefined ,
256+ ... ( replayClaim . kind === "claimed" ? { replayClaim : replayClaim . handle } : { } ) ,
262257 } ) ;
263258 } catch ( err ) {
264259 params . runtime . error ( danger ( `handler failed: ${ String ( err ) } ` ) ) ;
0 commit comments