@@ -28,80 +28,8 @@ public RouterInstance(IServiceProvider services,
2828
2929 public IRouterInstance Load ( )
3030 {
31- var db = _services . GetRequiredService < IBotSharpRepository > ( ) ;
32-
33- _router = new Agent ( )
34- {
35- Id = _settings . RouterId ,
36- Name = _settings . RouterName ,
37- Description = _settings . Description
38- } ;
39- var agents = db . Agents . Where ( x => ! x . Disabled && x . AllowRouting ) . ToArray ( ) ;
40-
41- // Assemble prompt
42- var prompt = @$ "You're { _settings . RouterName } ({ _settings . Description } ). Follow these steps to handle user's request:
43- 1. Read the CONVERSATION context.
44- 2. Select a appropriate function from FUNCTIONS.
45- 3. Determine which agent is suitable according to conversation context.
46- 4. Re-think about selected function is from FUNCTIONS to handle the request.
47- 5. Make sure agent is not in args." ;
48-
49- // Append function
50- prompt += "\r \n " ;
51- prompt += "\r \n FUNCTIONS" ;
52- GetHandlers ( ) . Select ( ( handler , i ) =>
53- {
54- prompt += "\r \n " ;
55- prompt += $ "\r \n { i + 1 } . { handler . Name } ";
56- prompt += $ "\r \n { handler . Description } ";
57-
58- // Append parameters
59- if ( handler . Parameters . Any ( ) )
60- {
61- prompt += "\r \n Parameters:" ;
62- handler . Parameters . Select ( ( p , i ) =>
63- {
64- prompt += $ "\r \n - { p . Name } : { p . Description } ";
65- return p ;
66- } ) . ToList ( ) ;
67- }
68-
69- return handler ;
70- } ) . ToList ( ) ;
71-
72- prompt += "\r \n " ;
73- prompt += "\r \n AGENTS" ;
74- agents . Select ( x => new RoutingItem
75- {
76- AgentId = x . Id ,
77- Description = x . Description ,
78- Name = x . Name ,
79- RequiredFields = x . RoutingRules . Where ( x => x . Required )
80- . Select ( x => new NameDesc ( x . Field , x . Description ) )
81- . ToList ( )
82- } ) . Select ( ( agent , i ) =>
83- {
84- prompt += "\r \n " ;
85- prompt += $ "\r \n { i + 1 } . { agent . Name } ";
86- prompt += $ "\r \n { agent . Description } ";
87-
88- // Append parameters
89- if ( agent . RequiredFields . Any ( ) )
90- {
91- prompt += $ "\r \n Required:";
92- agent . RequiredFields . Select ( ( field , i ) =>
93- {
94- prompt += $ "\r \n - { field . Name } : { field . Description } ";
95- return field ;
96- } ) . ToList ( ) ;
97- }
98- return agent ;
99- } ) . ToList ( ) ;
100-
101- prompt += "\r \n " ;
102- prompt += "\r \n CONVERSATION" ;
103- _router . Instruction = prompt ;
104-
31+ var agentService = _services . GetRequiredService < IAgentService > ( ) ;
32+ _router = agentService . LoadAgent ( _settings . RouterId ) . Result ;
10533 return this ;
10634 }
10735
@@ -119,7 +47,7 @@ public List<RoutingHandlerDef> GetHandlers()
11947 return _services . GetServices < IRoutingHandler > ( )
12048 . Where ( x => x . IsReasoning == _settings . EnableReasoning )
12149 . Where ( x => ! string . IsNullOrEmpty ( x . Description ) )
122- . Select ( x => new RoutingHandlerDef
50+ . Select ( ( x , i ) => new RoutingHandlerDef
12351 {
12452 Name = x . Name ,
12553 Description = x . Description ,
@@ -157,6 +85,25 @@ protected RoutingRule[] GetRoutingRecords()
15785 return records ;
15886 }
15987
88+ #if ! DEBUG
89+ [ MemoryCache ( 10 * 60 ) ]
90+ #endif
91+ public RoutingItem [ ] GetRoutingItems ( )
92+ {
93+ var db = _services . GetRequiredService < IBotSharpRepository > ( ) ;
94+
95+ var agents = db . Agents . Where ( x => ! x . Disabled && x . AllowRouting ) . ToArray ( ) ;
96+ return agents . Select ( x => new RoutingItem
97+ {
98+ AgentId = x . Id ,
99+ Description = x . Description ,
100+ Name = x . Name ,
101+ RequiredFields = x . RoutingRules . Where ( x => x . Required )
102+ . Select ( x => new NameDesc ( x . Field , x . Description ) )
103+ . ToList ( )
104+ } ) . ToArray ( ) ;
105+ }
106+
160107 public RoutingRule [ ] GetRulesByName ( string name )
161108 {
162109 return GetRoutingRecords ( )
0 commit comments