55using BotSharp . Abstraction . Agents . Models ;
66using MongoDB . Driver ;
77using BotSharp . Abstraction . Routing . Models ;
8+ using BotSharp . Abstraction . Repositories . Filters ;
9+ using BotSharp . Abstraction . Utilities ;
10+ using BotSharp . Abstraction . Conversations . Models ;
11+
812namespace BotSharp . Core . Repository ;
913
1014public class FileRepository : IBotSharpRepository
@@ -500,33 +504,32 @@ public List<string> GetAgentResponses(string agentId, string prefix, string inte
500504 return null ;
501505 }
502506
503- public List < Agent > GetAgents ( string ? name = null , bool ? disabled = null , bool ? allowRouting = null ,
504- bool ? isPublic = null , List < string > ? agentIds = null )
507+ public List < Agent > GetAgents ( AgentFilter filter )
505508 {
506509 var query = Agents ;
507- if ( ! string . IsNullOrEmpty ( name ) )
510+ if ( ! string . IsNullOrEmpty ( filter . AgentName ) )
508511 {
509- query = query . Where ( x => x . Name . ToLower ( ) == name . ToLower ( ) ) ;
512+ query = query . Where ( x => x . Name . ToLower ( ) == filter . AgentName . ToLower ( ) ) ;
510513 }
511514
512- if ( disabled . HasValue )
515+ if ( filter . Disabled . HasValue )
513516 {
514- query = query . Where ( x => x . Disabled == disabled ) ;
517+ query = query . Where ( x => x . Disabled == filter . Disabled ) ;
515518 }
516519
517- if ( allowRouting . HasValue )
520+ if ( filter . AllowRouting . HasValue )
518521 {
519- query = query . Where ( x => x . AllowRouting == allowRouting ) ;
522+ query = query . Where ( x => x . AllowRouting == filter . AllowRouting ) ;
520523 }
521524
522- if ( isPublic . HasValue )
525+ if ( filter . IsPublic . HasValue )
523526 {
524- query = query . Where ( x => x . IsPublic == isPublic ) ;
527+ query = query . Where ( x => x . IsPublic == filter . IsPublic ) ;
525528 }
526529
527- if ( agentIds != null )
530+ if ( filter . AgentIds != null )
528531 {
529- query = query . Where ( x => agentIds . Contains ( x . Id ) ) ;
532+ query = query . Where ( x => filter . AgentIds . Contains ( x . Id ) ) ;
530533 }
531534
532535 return query . ToList ( ) ;
@@ -539,7 +542,12 @@ join u in Users on ua.UserId equals u.Id
539542 where ua . UserId == userId || u . ExternalId == userId
540543 select ua . AgentId ) . ToList ( ) ;
541544
542- var agents = GetAgents ( isPublic : true , agentIds : agentIds ) ;
545+ var filter = new AgentFilter
546+ {
547+ IsPublic = true ,
548+ AgentIds = agentIds
549+ } ;
550+ var agents = GetAgents ( filter ) ;
543551 return agents ;
544552 }
545553
@@ -552,7 +560,7 @@ public string GetAgentTemplate(string agentId, string templateName)
552560 foreach ( var file in Directory . GetFiles ( dir ) )
553561 {
554562 var fileName = file . Split ( Path . DirectorySeparatorChar ) . Last ( ) ;
555- var splits = fileName . ToLower ( ) . Split ( '.' ) ;
563+ var splits = ParseFileNameByPath ( fileName . ToLower ( ) ) ;
556564 var name = splits [ 0 ] ;
557565 var extension = splits [ 1 ] ;
558566 if ( name . IsEqualTo ( templateName ) && extension . IsEqualTo ( _agentSettings . TemplateFormat ) )
@@ -610,10 +618,10 @@ public bool DeleteConversation(string conversationId)
610618 {
611619 if ( string . IsNullOrEmpty ( conversationId ) ) return false ;
612620
613- var dir = Path . Combine ( _dbSettings . FileRepository , _conversationSettings . DataDir , conversationId ) ;
614- if ( ! Directory . Exists ( dir ) ) return false ;
621+ var convDir = FindConversationDirectory ( conversationId ) ;
622+ if ( string . IsNullOrEmpty ( convDir ) ) return false ;
615623
616- Directory . Delete ( dir , true ) ;
624+ Directory . Delete ( convDir , true ) ;
617625 return true ;
618626 }
619627
@@ -734,7 +742,7 @@ public Conversation GetConversation(string conversationId)
734742 return record ;
735743 }
736744
737- public List < Conversation > GetConversations ( string ? agentId = null , string ? status = null , string ? channel = null , string ? userId = null )
745+ public List < Conversation > GetConversations ( ConversationFilter filter )
738746 {
739747 var records = new List < Conversation > ( ) ;
740748 var dir = Path . Combine ( _dbSettings . FileRepository , _conversationSettings . DataDir ) ;
@@ -749,10 +757,10 @@ public List<Conversation> GetConversations(string? agentId = null, string? statu
749757 if ( record == null ) continue ;
750758
751759 var matched = true ;
752- if ( ! string . IsNullOrEmpty ( agentId ) ) matched = matched && record . AgentId == agentId ;
753- if ( ! string . IsNullOrEmpty ( status ) ) matched = matched && record . Status == status ;
754- if ( ! string . IsNullOrEmpty ( channel ) ) matched = matched && record . Channel == channel ;
755- if ( ! string . IsNullOrEmpty ( userId ) ) matched = matched && record . UserId == userId ;
760+ if ( ! string . IsNullOrEmpty ( filter . AgentId ) ) matched = matched && record . AgentId == filter . AgentId ;
761+ if ( ! string . IsNullOrEmpty ( filter . Status ) ) matched = matched && record . Status == filter . Status ;
762+ if ( ! string . IsNullOrEmpty ( filter . Channel ) ) matched = matched && record . Channel == filter . Channel ;
763+ if ( ! string . IsNullOrEmpty ( filter . UserId ) ) matched = matched && record . UserId == filter . UserId ;
756764
757765 if ( ! matched ) continue ;
758766 records . Add ( record ) ;
@@ -835,6 +843,25 @@ public void CreateUser(User user)
835843 }
836844 #endregion
837845
846+ #region LLM Completion Log
847+ public void SaveLlmCompletionLog ( LlmCompletionLog log )
848+ {
849+ var convDir = FindConversationDirectory ( log . ConversationId ) ;
850+ if ( ! Directory . Exists ( convDir ) ) return ;
851+
852+ var logDir = Path . Combine ( convDir , "llm_prompt_log" ) ;
853+ if ( ! Directory . Exists ( logDir ) )
854+ {
855+ Directory . CreateDirectory ( logDir ) ;
856+ }
857+
858+ var index = GetLlmCompletionLogIndex ( logDir , log . MessageId ) ;
859+ var file = Path . Combine ( logDir , $ "{ log . MessageId } .{ index } .log") ;
860+ File . WriteAllText ( file , JsonSerializer . Serialize ( log , _options ) ) ;
861+ }
862+ #endregion
863+
864+
838865 #region Private methods
839866 private string GetAgentDataDir ( string agentId )
840867 {
@@ -927,22 +954,10 @@ private List<AgentResponse> FetchResponses(string fileDir)
927954
928955 private string ? FindConversationDirectory ( string conversationId )
929956 {
930- var dir = Path . Combine ( _dbSettings . FileRepository , _conversationSettings . DataDir ) ;
931-
932- foreach ( var d in Directory . GetDirectories ( dir ) )
933- {
934- var path = Path . Combine ( d , "conversation.json" ) ;
935- if ( ! File . Exists ( path ) ) continue ;
936-
937- var json = File . ReadAllText ( path ) ;
938- var conv = JsonSerializer . Deserialize < Conversation > ( json , _options ) ;
939- if ( conv != null && conv . Id == conversationId )
940- {
941- return d ;
942- }
943- }
957+ var dir = Path . Combine ( _dbSettings . FileRepository , _conversationSettings . DataDir , conversationId ) ;
958+ if ( ! Directory . Exists ( dir ) ) return null ;
944959
945- return null ;
960+ return dir ;
946961 }
947962
948963 private List < DialogElement > CollectDialogElements ( string dialogDir )
@@ -993,5 +1008,30 @@ private List<StateKeyValue> CollectConversationStates(string stateDir)
9931008 }
9941009 return states ;
9951010 }
1011+
1012+ private int GetLlmCompletionLogIndex ( string logDir , string id )
1013+ {
1014+ var files = Directory . GetFiles ( logDir ) ;
1015+ if ( files . IsNullOrEmpty ( ) )
1016+ return 0 ;
1017+
1018+ var logIndexes = files . Where ( file =>
1019+ {
1020+ var fileName = ParseFileNameByPath ( file ) ;
1021+ return fileName [ 0 ] . IsEqualTo ( id ) ;
1022+ } ) . Select ( file =>
1023+ {
1024+ var fileName = ParseFileNameByPath ( file ) ;
1025+ return int . Parse ( fileName [ 1 ] ) ;
1026+ } ) . ToList ( ) ;
1027+
1028+ return logIndexes . IsNullOrEmpty ( ) ? 0 : logIndexes . Max ( ) + 1 ;
1029+ }
1030+
1031+ private string [ ] ParseFileNameByPath ( string path , string separator = "." )
1032+ {
1033+ var name = path . Split ( Path . DirectorySeparatorChar ) . Last ( ) ;
1034+ return name . Split ( separator ) ;
1035+ }
9961036 #endregion
9971037}
0 commit comments