1
+ import datetime
1
2
import json
2
3
import os
3
4
import sys
10
11
from botbuilder .schema import Activity
11
12
from litellm import acompletion
12
13
from memory_module import (
14
+ AssistantMessageInput ,
13
15
InternalMessageInput ,
14
16
LLMConfig ,
15
17
Memory ,
16
- MemoryMiddleware ,
17
18
MemoryModule ,
18
19
MemoryModuleConfig ,
20
+ UserMessageInput ,
19
21
)
20
22
from pydantic import BaseModel , Field
21
23
from teams import Application , ApplicationOptions , TeamsAdapter
61
63
)
62
64
)
63
65
64
- bot_app .adapter .use (MemoryMiddleware (memory_module ))
65
-
66
66
67
67
class TaskConfig (BaseModel ):
68
68
task_name : str
@@ -281,6 +281,75 @@ def get_available_functions():
281
281
]
282
282
283
283
284
+ def build_deep_link (context : TurnContext , message_id : str ):
285
+ conversation_ref = TurnContext .get_conversation_reference (context .activity )
286
+ if conversation_ref .conversation and conversation_ref .conversation .is_group :
287
+ deeplink_conversation_id = conversation_ref .conversation .id
288
+ elif conversation_ref .user and conversation_ref .bot :
289
+ user_aad_object_id = conversation_ref .user .aad_object_id
290
+ bot_id = conversation_ref .bot .id .replace ("28:" , "" )
291
+ deeplink_conversation_id = f"19:{ user_aad_object_id } _{ bot_id } @unq.gbl.spaces"
292
+ else :
293
+ return None
294
+ return f"https://teams.microsoft.com/l/message/{ deeplink_conversation_id } /{ message_id } ?context=%7B%22contextType%22%3A%22chat%22%7D"
295
+
296
+
297
+ async def add_user_message (context : TurnContext ):
298
+ conversation_ref_dict = TurnContext .get_conversation_reference (context .activity )
299
+ content = context .activity .text
300
+ if not content :
301
+ print ("content is not text, so ignoring..." )
302
+ return False
303
+ if conversation_ref_dict is None :
304
+ print ("conversation_ref_dict is None" )
305
+ return False
306
+ if conversation_ref_dict .user is None :
307
+ print ("conversation_ref_dict.user is None" )
308
+ return False
309
+ if conversation_ref_dict .conversation is None :
310
+ print ("conversation_ref_dict.conversation is None" )
311
+ return False
312
+ user_aad_object_id = conversation_ref_dict .user .aad_object_id
313
+ message_id = context .activity .id
314
+ await memory_module .add_message (
315
+ UserMessageInput (
316
+ id = message_id ,
317
+ content = context .activity .text ,
318
+ author_id = user_aad_object_id ,
319
+ conversation_ref = conversation_ref_dict .conversation .id ,
320
+ created_at = context .activity .timestamp if context .activity .timestamp else datetime .datetime .now (),
321
+ deep_link = build_deep_link (context , context .activity .id ),
322
+ )
323
+ )
324
+ return True
325
+
326
+
327
+ async def add_agent_message (context : TurnContext , message_id : str , content : str ):
328
+ conversation_ref_dict = TurnContext .get_conversation_reference (context .activity )
329
+ if not content :
330
+ print ("content is not text, so ignoring..." )
331
+ return False
332
+ if conversation_ref_dict is None :
333
+ print ("conversation_ref_dict is None" )
334
+ return False
335
+ if conversation_ref_dict .bot is None :
336
+ print ("conversation_ref_dict.bot is None" )
337
+ return False
338
+ if conversation_ref_dict .conversation is None :
339
+ print ("conversation_ref_dict.conversation is None" )
340
+ return False
341
+ await memory_module .add_message (
342
+ AssistantMessageInput (
343
+ id = message_id ,
344
+ content = content ,
345
+ author_id = conversation_ref_dict .bot .id ,
346
+ conversation_ref = conversation_ref_dict .conversation .id ,
347
+ deep_link = build_deep_link (context , message_id ),
348
+ )
349
+ )
350
+ return True
351
+
352
+
284
353
async def add_internal_message (context : TurnContext , content : str ):
285
354
conversation_ref_dict = TurnContext .get_conversation_reference (context .activity )
286
355
if not content :
@@ -307,12 +376,15 @@ async def add_internal_message(context: TurnContext, content: str):
307
376
308
377
@bot_app .conversation_update ("membersAdded" )
309
378
async def on_members_added (context : TurnContext , state : TurnState ):
310
- await send_string_message (context , "Hello! I'm your IT Support Assistant. How can I assist you today?" )
379
+ result = await send_string_message (context , "Hello! I'm your IT Support Assistant. How can I assist you today?" )
380
+ if result :
381
+ await add_agent_message (context , result , "Hello! I'm your IT Support Assistant. How can I assist you today?" )
311
382
312
383
313
384
@bot_app .activity ("message" )
314
385
async def on_message (context : TurnContext , state : TurnState ):
315
386
conversation_ref_dict = TurnContext .get_conversation_reference (context .activity )
387
+ await add_user_message (context )
316
388
system_prompt = """
317
389
You are an IT Chat Bot that helps users troubleshoot tasks
318
390
@@ -377,7 +449,9 @@ async def on_message(context: TurnContext, state: TurnState):
377
449
message = response .choices [0 ].message
378
450
379
451
if message .tool_calls is None and message .content is not None :
380
- await send_string_message (context , message .content )
452
+ agent_message_id = await send_string_message (context , message .content )
453
+ if agent_message_id :
454
+ await add_agent_message (context , agent_message_id , message .content )
381
455
break
382
456
elif message .tool_calls is None and message .content is None :
383
457
print ("No tool calls and no content" )
0 commit comments