@@ -153,19 +153,43 @@ func (b *SlackBot) handleEventAPI(ctx context.Context, evt socketmode.Event) {
153153}
154154
155155func (b * SlackBot ) handleMessage (ctx context.Context , event * slackevents.MessageEvent ) {
156- log .Printf ("Message from %s in channel %s: %s" , event .User , event .Channel , event .Text )
156+ // Only process direct messages (not in a channel)
157+ if ! b .isDirectMessage (event .Channel ) {
158+ // In channels, we only respond to mentions (handled by handleMention)
159+ return
160+ }
161+
162+ log .Printf ("DM from %s: %s" , event .User , event .Text )
163+
164+ // For DMs, use the message timestamp as thread (maintains conversation flow)
165+ threadTS := event .ThreadTimeStamp
166+ if threadTS == "" {
167+ threadTS = event .TimeStamp
168+ }
157169
158170 // Process message through Flyt workflow
159- b .processWithFlyt (ctx , event .Text , event .Channel , event .ThreadTimeStamp )
171+ b .processWithFlyt (ctx , event .Text , event .Channel , threadTS , event .User )
160172}
161173
162174func (b * SlackBot ) handleMention (ctx context.Context , event * slackevents.AppMentionEvent ) {
163175 log .Printf ("Mention from %s in channel %s: %s" , event .User , event .Channel , event .Text )
164176
177+ // For mentions in channels, always reply in thread
178+ threadTS := event .ThreadTimeStamp
179+ if threadTS == "" {
180+ // Start a new thread with the mention message as root
181+ threadTS = event .TimeStamp
182+ }
183+
165184 // Process mention through Flyt workflow
166- b .processWithFlyt (ctx , event .Text , event .Channel , event .ThreadTimeStamp )
185+ b .processWithFlyt (ctx , event .Text , event .Channel , threadTS , event .User )
167186}
168187
188+ func (b * SlackBot ) isDirectMessage (channel string ) bool {
189+ // Direct message channels start with 'D'
190+ // Group DMs start with 'G'
191+ return len (channel ) > 0 && (channel [0 ] == 'D' || channel [0 ] == 'G' )
192+ }
169193func (b * SlackBot ) getLLMService (channel , threadTS string ) * LLMService {
170194 // Create a key for this conversation context
171195 key := channel
@@ -188,15 +212,20 @@ func (b *SlackBot) getLLMService(channel, threadTS string) *LLMService {
188212 return service
189213}
190214
191- func (b * SlackBot ) processWithFlyt (ctx context.Context , message , channel , threadTS string ) {
215+ func (b * SlackBot ) processWithFlyt (ctx context.Context , message , channel , threadTS , userID string ) {
192216 // Get or create LLM service for this thread
193217 llmService := b .getLLMService (channel , threadTS )
194218
219+ // Fetch conversation history for context
220+ history := b .fetchConversationHistory (channel , threadTS )
221+
195222 // Create shared store
196223 shared := flyt .NewSharedStore ()
197224 shared .Set ("message" , message )
198225 shared .Set ("channel" , channel )
199226 shared .Set ("thread_ts" , threadTS )
227+ shared .Set ("user_id" , userID )
228+ shared .Set ("history" , history )
200229
201230 // Create workflow with injected LLM service
202231 flow := b .createWorkflow (llmService )
@@ -216,6 +245,40 @@ func (b *SlackBot) processWithFlyt(ctx context.Context, message, channel, thread
216245 }
217246}
218247
248+ func (b * SlackBot ) fetchConversationHistory (channel , threadTS string ) []map [string ]string {
249+ var history []map [string ]string
250+
251+ if threadTS != "" {
252+ // Fetch thread messages
253+ messages , err := b .slack .GetThreadMessages (channel , threadTS )
254+ if err != nil {
255+ log .Printf ("Failed to fetch thread history: %v" , err )
256+ return history
257+ }
258+
259+ // Convert to simplified format, excluding bot's own messages
260+ botID := b .slack .GetBotUserID ()
261+ for _ , msg := range messages {
262+ // Skip bot's own messages and empty messages
263+ if msg .User == botID || msg .BotID != "" || msg .Text == "" {
264+ continue
265+ }
266+
267+ history = append (history , map [string ]string {
268+ "user" : msg .User ,
269+ "text" : msg .Text ,
270+ "timestamp" : msg .Timestamp ,
271+ })
272+ }
273+ }
274+
275+ // Limit history to last 10 messages for context
276+ if len (history ) > 10 {
277+ history = history [len (history )- 10 :]
278+ }
279+
280+ return history
281+ }
219282func (b * SlackBot ) createWorkflow (llmService * LLMService ) * flyt.Flow {
220283 // Create nodes with injected dependencies
221284 parseNode := & ParseMessageNode {
0 commit comments