@@ -9,10 +9,8 @@ import type {
99} from "openclaw/plugin-sdk/config-contracts" ;
1010import type { SessionScope } from "openclaw/plugin-sdk/config-contracts" ;
1111import type { DmPolicy , GroupPolicy } from "openclaw/plugin-sdk/config-contracts" ;
12- import { resolveRuntimeConversationBindingRoute } from "openclaw/plugin-sdk/conversation-runtime" ;
1312import { createDedupeCache } from "openclaw/plugin-sdk/dedupe-runtime" ;
1413import type { HistoryEntry } from "openclaw/plugin-sdk/reply-history" ;
15- import { resolveAgentRoute } from "openclaw/plugin-sdk/routing" ;
1614import { resolveThreadSessionKeys } from "openclaw/plugin-sdk/routing" ;
1715import { logVerbose } from "openclaw/plugin-sdk/runtime-env" ;
1816import { getChildLogger } from "openclaw/plugin-sdk/runtime-env" ;
@@ -37,6 +35,8 @@ import {
3735 type SlackSuggestedPromptsInput ,
3836 updateSlackSuggestedPrompts ,
3937} from "./suggested-prompts.js" ;
38+ import { resolveSlackSystemEventRouteSessionKey } from "./system-event-session.js" ;
39+ import { qualifySlackRoutePeerId } from "./workspace-routing.js" ;
4040
4141export { normalizeSlackChannelType , resolveSlackChatType } from "./channel-type.js" ;
4242export { DEFAULT_SLACK_SUGGESTED_PROMPTS } from "./suggested-prompts.js" ;
@@ -162,6 +162,7 @@ export type SlackMonitorContext = {
162162 channelType ?: string | null ;
163163 senderId ?: string | null ;
164164 threadTs ?: string | null ;
165+ eventScope ?: SlackEventScope ;
165166 } ) => string ;
166167 isChannelAllowed : ( params : {
167168 channelId ?: string ;
@@ -381,13 +382,14 @@ export function createSlackMonitorContext(params: {
381382 channelType ?: string | null ;
382383 senderId ?: string | null ;
383384 threadTs ?: string | null ;
385+ eventScope ?: SlackEventScope ;
384386 } ) => {
385387 const channelId = normalizeOptionalString ( p . channelId ) ?? "" ;
386388 const senderId = normalizeOptionalString ( p . senderId ) ?? "" ;
387389 // System events can omit channel_type too; prefer a type already seen on events
388390 // for this channel over C-prefix inference so they key the same session (#102676).
389391 const channelType = normalizeSlackChannelType (
390- p . channelType ?? recallSlackChannelType ( channelId ) ,
392+ p . channelType ?? recallSlackChannelType ( channelId , p . eventScope ) ,
391393 channelId ,
392394 ) ;
393395 const isDirectMessage = channelType === "im" ;
@@ -401,60 +403,31 @@ export function createSlackMonitorContext(params: {
401403 ? `slack:group:${ channelId } `
402404 : `slack:channel:${ channelId } ` ;
403405 const chatType = isDirectMessage ? "direct" : isGroup ? "group" : "channel" ;
404- // Resolve through shared channel/account bindings so system events route to
405- // the same agent session as regular inbound messages.
406- try {
407- const peerKind = isDirectMessage ? "direct" : isGroup ? "group" : "channel" ;
408- const peerId = isDirectMessage ? senderId : channelId ;
409- if ( peerId ) {
410- const route = resolveAgentRoute ( {
411- cfg : params . cfg ,
412- channel : "slack" ,
413- accountId : params . accountId ,
414- teamId : params . teamId ,
415- peer : { kind : peerKind , id : peerId } ,
416- } ) ;
417- const threadTs = normalizeOptionalString ( p . threadTs ) ;
418- const baseConversationId = isDirectMessage ? `user:${ senderId } ` : channelId ;
419- const threadBindingRoute = threadTs
420- ? resolveRuntimeConversationBindingRoute ( {
421- route,
422- conversation : {
423- channel : "slack" ,
424- accountId : params . accountId ,
425- conversationId : threadTs ,
426- parentConversationId : baseConversationId ,
427- } ,
428- } )
429- : null ;
430- const runtimeRoute =
431- threadBindingRoute ?. boundSessionKey || threadBindingRoute ?. bindingRecord
432- ? threadBindingRoute
433- : resolveRuntimeConversationBindingRoute ( {
434- route,
435- conversation : {
436- channel : "slack" ,
437- accountId : params . accountId ,
438- conversationId : baseConversationId ,
439- } ,
440- } ) ;
441- if ( runtimeRoute . boundSessionKey ) {
442- return runtimeRoute . route . sessionKey ;
443- }
444- return resolveThreadSessionKeys ( {
445- baseSessionKey : runtimeRoute . route . sessionKey ,
446- threadId : threadTs ,
447- parentSessionKey :
448- threadTs && params . threadInheritParent ? runtimeRoute . route . sessionKey : undefined ,
449- } ) . sessionKey ;
450- }
451- } catch {
452- // Fall through to legacy key derivation.
406+ const routedSessionKey = resolveSlackSystemEventRouteSessionKey ( {
407+ cfg : params . cfg ,
408+ accountId : params . accountId ,
409+ teamId : params . teamId ,
410+ threadInheritParent : params . threadInheritParent ,
411+ channelId,
412+ channelType,
413+ senderId,
414+ threadTs : p . threadTs ,
415+ eventScope : p . eventScope ,
416+ } ) ;
417+ if ( routedSessionKey ) {
418+ return routedSessionKey ;
453419 }
454420
421+ const fallbackFrom = p . eventScope
422+ ? `slack:${ qualifySlackRoutePeerId ( {
423+ id : isDirectMessage ? senderId : channelId ,
424+ kind : isDirectMessage ? "user" : "channel" ,
425+ eventScope : p . eventScope ,
426+ } ) } `
427+ : from ;
455428 const legacySessionKey = resolveSessionKey (
456429 params . sessionScope ,
457- { From : from , ChatType : chatType , Provider : "slack" } ,
430+ { From : fallbackFrom , ChatType : chatType , Provider : "slack" } ,
458431 params . mainKey ,
459432 resolveDefaultAgentId ( params . cfg ) ,
460433 ) ;
0 commit comments