@@ -24,10 +24,7 @@ import {
2424} from "./message-utils.js" ;
2525import { buildDirectLabel , buildGuildLabel , resolveReplyContext } from "./reply-context.js" ;
2626import { deliverDiscordReply } from "./reply-delivery.js" ;
27- import {
28- resolveDiscordAutoThreadReplyPlan ,
29- resolveDiscordThreadStarter ,
30- } from "./threading.js" ;
27+ import { resolveDiscordAutoThreadReplyPlan , resolveDiscordThreadStarter } from "./threading.js" ;
3128import { sendTyping } from "./typing.js" ;
3229
3330export async function processDiscordMessage ( ctx : DiscordMessagePreflightContext ) {
@@ -191,72 +188,72 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
191188 peer : { kind : "channel" , id : threadParentId } ,
192189 } ) ;
193190 }
194- }
195- const mediaPayload = buildDiscordMediaPayload ( mediaList ) ;
196- const threadKeys = resolveThreadSessionKeys ( {
197- baseSessionKey,
198- threadId : threadChannel ? message . channelId : undefined ,
199- parentSessionKey,
200- useSuffix : false ,
201- } ) ;
202- const replyPlan = await resolveDiscordAutoThreadReplyPlan ( {
203- client,
204- message,
205- isGuildMessage,
206- channelConfig,
207- threadChannel,
208- baseText : baseText ?? "" ,
209- combinedBody,
210- replyToMode,
211- agentId : route . agentId ,
212- channel : route . channel ,
213- } ) ;
214- const deliverTarget = replyPlan . deliverTarget ;
215- const replyTarget = replyPlan . replyTarget ;
216- const replyReference = replyPlan . replyReference ;
217- const autoThreadContext = replyPlan . autoThreadContext ;
191+ }
192+ const mediaPayload = buildDiscordMediaPayload ( mediaList ) ;
193+ const threadKeys = resolveThreadSessionKeys ( {
194+ baseSessionKey,
195+ threadId : threadChannel ? message . channelId : undefined ,
196+ parentSessionKey,
197+ useSuffix : false ,
198+ } ) ;
199+ const replyPlan = await resolveDiscordAutoThreadReplyPlan ( {
200+ client,
201+ message,
202+ isGuildMessage,
203+ channelConfig,
204+ threadChannel,
205+ baseText : baseText ?? "" ,
206+ combinedBody,
207+ replyToMode,
208+ agentId : route . agentId ,
209+ channel : route . channel ,
210+ } ) ;
211+ const deliverTarget = replyPlan . deliverTarget ;
212+ const replyTarget = replyPlan . replyTarget ;
213+ const replyReference = replyPlan . replyReference ;
214+ const autoThreadContext = replyPlan . autoThreadContext ;
218215
219- const effectiveFrom = isDirectMessage
220- ? `discord:${ author . id } `
221- : ( autoThreadContext ?. From ?? `group:${ message . channelId } ` ) ;
222- const effectiveTo = autoThreadContext ?. To ?? replyTarget ;
223- if ( ! effectiveTo ) {
224- runtime . error ?.( danger ( "discord: missing reply target" ) ) ;
225- return ;
226- }
216+ const effectiveFrom = isDirectMessage
217+ ? `discord:${ author . id } `
218+ : ( autoThreadContext ?. From ?? `group:${ message . channelId } ` ) ;
219+ const effectiveTo = autoThreadContext ?. To ?? replyTarget ;
220+ if ( ! effectiveTo ) {
221+ runtime . error ?.( danger ( "discord: missing reply target" ) ) ;
222+ return ;
223+ }
227224
228- const ctxPayload = {
229- Body : combinedBody ,
230- RawBody : baseText ,
231- CommandBody : baseText ,
232- From : effectiveFrom ,
233- To : effectiveTo ,
234- SessionKey : autoThreadContext ?. SessionKey ?? threadKeys . sessionKey ,
235- AccountId : route . accountId ,
236- ChatType : isDirectMessage ? "direct" : "group" ,
237- SenderName : data . member ?. nickname ?? author . globalName ?? author . username ,
238- SenderId : author . id ,
239- SenderUsername : author . username ,
240- SenderTag : formatDiscordUserTag ( author ) ,
241- GroupSubject : groupSubject ,
242- GroupRoom : groupRoom ,
243- GroupSystemPrompt : isGuildMessage ? groupSystemPrompt : undefined ,
244- GroupSpace : isGuildMessage ? ( guildInfo ?. id ?? guildSlug ) || undefined : undefined ,
245- Provider : "discord" as const ,
246- Surface : "discord" as const ,
247- WasMentioned : effectiveWasMentioned ,
248- MessageSid : message . id ,
249- ParentSessionKey : autoThreadContext ?. ParentSessionKey ?? threadKeys . parentSessionKey ,
250- ThreadStarterBody : threadStarterBody ,
251- ThreadLabel : threadLabel ,
252- Timestamp : resolveTimestampMs ( message . timestamp ) ,
253- ...mediaPayload ,
254- CommandAuthorized : commandAuthorized ,
255- CommandSource : "text" as const ,
256- // Originating channel for reply routing.
257- OriginatingChannel : "discord" as const ,
258- OriginatingTo : autoThreadContext ?. OriginatingTo ?? replyTarget ,
259- } ;
225+ const ctxPayload = {
226+ Body : combinedBody ,
227+ RawBody : baseText ,
228+ CommandBody : baseText ,
229+ From : effectiveFrom ,
230+ To : effectiveTo ,
231+ SessionKey : autoThreadContext ?. SessionKey ?? threadKeys . sessionKey ,
232+ AccountId : route . accountId ,
233+ ChatType : isDirectMessage ? "direct" : "group" ,
234+ SenderName : data . member ?. nickname ?? author . globalName ?? author . username ,
235+ SenderId : author . id ,
236+ SenderUsername : author . username ,
237+ SenderTag : formatDiscordUserTag ( author ) ,
238+ GroupSubject : groupSubject ,
239+ GroupRoom : groupRoom ,
240+ GroupSystemPrompt : isGuildMessage ? groupSystemPrompt : undefined ,
241+ GroupSpace : isGuildMessage ? ( guildInfo ?. id ?? guildSlug ) || undefined : undefined ,
242+ Provider : "discord" as const ,
243+ Surface : "discord" as const ,
244+ WasMentioned : effectiveWasMentioned ,
245+ MessageSid : message . id ,
246+ ParentSessionKey : autoThreadContext ?. ParentSessionKey ?? threadKeys . parentSessionKey ,
247+ ThreadStarterBody : threadStarterBody ,
248+ ThreadLabel : threadLabel ,
249+ Timestamp : resolveTimestampMs ( message . timestamp ) ,
250+ ...mediaPayload ,
251+ CommandAuthorized : commandAuthorized ,
252+ CommandSource : "text" as const ,
253+ // Originating channel for reply routing.
254+ OriginatingChannel : "discord" as const ,
255+ OriginatingTo : autoThreadContext ?. OriginatingTo ?? replyTarget ,
256+ } ;
260257
261258 if ( isDirectMessage ) {
262259 const sessionCfg = cfg . session ;
@@ -272,20 +269,20 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
272269 } ) ;
273270 }
274271
275- if ( shouldLogVerbose ( ) ) {
276- const preview = truncateUtf16Safe ( combinedBody , 200 ) . replace ( / \n / g, "\\n" ) ;
277- logVerbose (
278- `discord inbound: channel=${ message . channelId } deliver=${ deliverTarget } from=${ ctxPayload . From } preview="${ preview } "` ,
279- ) ;
280- }
272+ if ( shouldLogVerbose ( ) ) {
273+ const preview = truncateUtf16Safe ( combinedBody , 200 ) . replace ( / \n / g, "\\n" ) ;
274+ logVerbose (
275+ `discord inbound: channel=${ message . channelId } deliver=${ deliverTarget } from=${ ctxPayload . From } preview="${ preview } "` ,
276+ ) ;
277+ }
281278
282- let didSendReply = false ;
283- const typingChannelId = deliverTarget . startsWith ( "channel:" )
284- ? deliverTarget . slice ( "channel:" . length )
285- : message . channelId ;
286- const { dispatcher, replyOptions, markDispatchIdle } = createReplyDispatcherWithTyping ( {
287- responsePrefix : resolveEffectiveMessagesConfig ( cfg , route . agentId ) . responsePrefix ,
288- humanDelay : resolveHumanDelayConfig ( cfg , route . agentId ) ,
279+ let didSendReply = false ;
280+ const typingChannelId = deliverTarget . startsWith ( "channel:" )
281+ ? deliverTarget . slice ( "channel:" . length )
282+ : message . channelId ;
283+ const { dispatcher, replyOptions, markDispatchIdle } = createReplyDispatcherWithTyping ( {
284+ responsePrefix : resolveEffectiveMessagesConfig ( cfg , route . agentId ) . responsePrefix ,
285+ humanDelay : resolveHumanDelayConfig ( cfg , route . agentId ) ,
289286 deliver : async ( payload : ReplyPayload ) => {
290287 const replyToId = replyReference . use ( ) ;
291288 await deliverDiscordReply ( {
@@ -302,11 +299,11 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
302299 didSendReply = true ;
303300 replyReference . markSent ( ) ;
304301 } ,
305- onError : ( err , info ) => {
306- runtime . error ?.( danger ( `discord ${ info . kind } reply failed: ${ String ( err ) } ` ) ) ;
307- } ,
308- onReplyStart : ( ) => sendTyping ( { client, channelId : typingChannelId } ) ,
309- } ) ;
302+ onError : ( err , info ) => {
303+ runtime . error ?.( danger ( `discord ${ info . kind } reply failed: ${ String ( err ) } ` ) ) ;
304+ } ,
305+ onReplyStart : ( ) => sendTyping ( { client, channelId : typingChannelId } ) ,
306+ } ) ;
310307
311308 const { queuedFinal, counts } = await dispatchReplyFromConfig ( {
312309 ctx : ctxPayload ,
0 commit comments