11// Slack plugin module implements channel type behavior.
2+ import { pruneMapToMaxSize } from "openclaw/plugin-sdk/collection-runtime" ;
23import {
34 normalizeLowercaseStringOrEmpty ,
45 normalizeOptionalString ,
@@ -13,8 +14,27 @@ export type SlackConversationInfo = {
1314 user ?: string ;
1415} ;
1516
17+ const SLACK_CONVERSATION_INFO_CACHE_MAX_ENTRIES = 1024 ;
1618const SLACK_CONVERSATION_INFO_CACHE = new Map < string , SlackConversationInfo > ( ) ;
1719
20+ function getCachedSlackConversationInfo ( cacheKey : string ) : SlackConversationInfo | undefined {
21+ const cached = SLACK_CONVERSATION_INFO_CACHE . get ( cacheKey ) ;
22+ if ( cached ) {
23+ SLACK_CONVERSATION_INFO_CACHE . delete ( cacheKey ) ;
24+ SLACK_CONVERSATION_INFO_CACHE . set ( cacheKey , cached ) ;
25+ }
26+ return cached ;
27+ }
28+
29+ function setCachedSlackConversationInfo (
30+ cacheKey : string ,
31+ conversationInfo : SlackConversationInfo ,
32+ ) : void {
33+ SLACK_CONVERSATION_INFO_CACHE . delete ( cacheKey ) ;
34+ SLACK_CONVERSATION_INFO_CACHE . set ( cacheKey , conversationInfo ) ;
35+ pruneMapToMaxSize ( SLACK_CONVERSATION_INFO_CACHE , SLACK_CONVERSATION_INFO_CACHE_MAX_ENTRIES ) ;
36+ }
37+
1838export async function resolveSlackConversationInfo ( params : {
1939 cfg : OpenClawConfig ;
2040 accountId ?: string | null ;
@@ -26,7 +46,7 @@ export async function resolveSlackConversationInfo(params: {
2646 }
2747 const account = resolveSlackAccount ( { cfg : params . cfg , accountId : params . accountId } ) ;
2848 const cacheKey = `${ account . accountId } :${ channelId } ` ;
29- const cached = SLACK_CONVERSATION_INFO_CACHE . get ( cacheKey ) ;
49+ const cached = getCachedSlackConversationInfo ( cacheKey ) ;
3050 if ( cached ) {
3151 return cached ;
3252 }
@@ -42,7 +62,7 @@ export async function resolveSlackConversationInfo(params: {
4262 groupChannels . includes ( `mpim:${ channelIdLower } ` ) )
4363 ) {
4464 const result = { type : "group" } as const ;
45- SLACK_CONVERSATION_INFO_CACHE . set ( cacheKey , result ) ;
65+ setCachedSlackConversationInfo ( cacheKey , result ) ;
4666 return result ;
4767 }
4868
@@ -59,7 +79,7 @@ export async function resolveSlackConversationInfo(params: {
5979 } )
6080 ) {
6181 const result = { type : "channel" } as const ;
62- SLACK_CONVERSATION_INFO_CACHE . set ( cacheKey , result ) ;
82+ setCachedSlackConversationInfo ( cacheKey , result ) ;
6383 return result ;
6484 }
6585
@@ -70,7 +90,7 @@ export async function resolveSlackConversationInfo(params: {
7090 if ( ! token ) {
7191 const result = { type : isNativeImChannel ? "dm" : "unknown" } as const ;
7292 if ( ! isNativeImChannel ) {
73- SLACK_CONVERSATION_INFO_CACHE . set ( cacheKey , result ) ;
93+ setCachedSlackConversationInfo ( cacheKey , result ) ;
7494 }
7595 return result ;
7696 }
@@ -89,20 +109,20 @@ export async function resolveSlackConversationInfo(params: {
89109 : undefined ;
90110 const result : SlackConversationInfo = user ? { type : "dm" , user } : { type : "dm" } ;
91111 if ( user ) {
92- SLACK_CONVERSATION_INFO_CACHE . set ( cacheKey , result ) ;
112+ setCachedSlackConversationInfo ( cacheKey , result ) ;
93113 }
94114 return result ;
95115 }
96116 const info = await client . conversations . info ( { channel : channelId } ) ;
97117 const channel = info . channel as { is_im ?: boolean ; is_mpim ?: boolean } | undefined ;
98118 const type = channel ?. is_im ? "dm" : channel ?. is_mpim ? "group" : "channel" ;
99119 const result = { type } as const ;
100- SLACK_CONVERSATION_INFO_CACHE . set ( cacheKey , result ) ;
120+ setCachedSlackConversationInfo ( cacheKey , result ) ;
101121 return result ;
102122 } catch {
103123 const result = { type : isNativeImChannel ? "dm" : "unknown" } as const ;
104124 if ( ! isNativeImChannel ) {
105- SLACK_CONVERSATION_INFO_CACHE . set ( cacheKey , result ) ;
125+ setCachedSlackConversationInfo ( cacheKey , result ) ;
106126 }
107127 return result ;
108128 }
0 commit comments