11import { AbstractAgent } from "@ag-ui/client" ;
22import { FrontendTool , CopilotKitCore } from "@copilotkitnext/core" ;
3- import {
4- Injectable ,
5- Injector ,
6- Signal ,
7- WritableSignal ,
8- runInInjectionContext ,
9- signal ,
10- inject ,
11- } from "@angular/core" ;
12- import {
13- FrontendToolConfig ,
14- HumanInTheLoopConfig ,
15- RenderToolCallConfig ,
16- } from "./tools" ;
3+ import { Injectable , Injector , Signal , WritableSignal , runInInjectionContext , signal , inject } from "@angular/core" ;
4+ import { FrontendToolConfig , HumanInTheLoopConfig , RenderToolCallConfig } from "./tools" ;
175import { injectCopilotKitConfig } from "./config" ;
186import { HumanInTheLoop } from "./human-in-the-loop" ;
197
@@ -22,9 +10,7 @@ export class CopilotKit {
2210 readonly #config = injectCopilotKitConfig ( ) ;
2311 readonly #hitl = inject ( HumanInTheLoop ) ;
2412 readonly #rootInjector = inject ( Injector ) ;
25- readonly #agents = signal < Record < string , AbstractAgent > > (
26- this . #config. agents ?? { }
27- ) ;
13+ readonly #agents = signal < Record < string , AbstractAgent > > ( this . #config. agents ?? { } ) ;
2814 readonly agents = this . #agents. asReadonly ( ) ;
2915
3016 readonly core = new CopilotKitCore ( {
@@ -35,18 +21,12 @@ export class CopilotKit {
3521 tools : this . #config. tools ,
3622 } ) ;
3723
38- readonly #toolCallRenderConfigs: WritableSignal < RenderToolCallConfig [ ] > =
39- signal ( [ ] ) ;
40- readonly #clientToolCallRenderConfigs: WritableSignal < FrontendToolConfig [ ] > =
41- signal ( [ ] ) ;
42- readonly #humanInTheLoopToolRenderConfigs: WritableSignal <
43- HumanInTheLoopConfig [ ]
44- > = signal ( [ ] ) ;
45-
46- readonly toolCallRenderConfigs : Signal < RenderToolCallConfig [ ] > =
47- this . #toolCallRenderConfigs. asReadonly ( ) ;
48- readonly clientToolCallRenderConfigs : Signal < FrontendToolConfig [ ] > =
49- this . #clientToolCallRenderConfigs. asReadonly ( ) ;
24+ readonly #toolCallRenderConfigs: WritableSignal < RenderToolCallConfig [ ] > = signal ( [ ] ) ;
25+ readonly #clientToolCallRenderConfigs: WritableSignal < FrontendToolConfig [ ] > = signal ( [ ] ) ;
26+ readonly #humanInTheLoopToolRenderConfigs: WritableSignal < HumanInTheLoopConfig [ ] > = signal ( [ ] ) ;
27+
28+ readonly toolCallRenderConfigs : Signal < RenderToolCallConfig [ ] > = this . #toolCallRenderConfigs. asReadonly ( ) ;
29+ readonly clientToolCallRenderConfigs : Signal < FrontendToolConfig [ ] > = this . #clientToolCallRenderConfigs. asReadonly ( ) ;
5030 readonly humanInTheLoopToolRenderConfigs : Signal < HumanInTheLoopConfig [ ] > =
5131 this . #humanInTheLoopToolRenderConfigs. asReadonly ( ) ;
5232
@@ -84,38 +64,36 @@ export class CopilotKit {
8464 #bindClientTool(
8565 clientToolWithInjector : FrontendToolConfig & {
8666 injector : Injector ;
87- }
67+ } ,
8868 ) : FrontendTool {
89- const { injector, handler, ... frontendCandidate } = clientToolWithInjector ;
69+ const { injector, handler, args , description , name , agentId } = clientToolWithInjector ;
9070
9171 return {
92- ...frontendCandidate ,
72+ description,
73+ name,
74+ agentId,
9375 handler : ( args ) => runInInjectionContext ( injector , ( ) => handler ( args ) ) ,
76+ parameters : args ,
9477 } ;
9578 }
9679
9780 addFrontendTool (
9881 clientToolWithInjector : FrontendToolConfig & {
9982 injector : Injector ;
100- }
83+ } ,
10184 ) : void {
10285 const tool = this . #bindClientTool( clientToolWithInjector ) ;
10386
10487 this . core . addTool ( tool ) ;
10588
106- this . #clientToolCallRenderConfigs. update ( ( current ) => [
107- ...current ,
108- clientToolWithInjector ,
109- ] ) ;
89+ this . #clientToolCallRenderConfigs. update ( ( current ) => [ ...current , clientToolWithInjector ] ) ;
11090 }
11191
11292 addRenderToolCall ( renderConfig : RenderToolCallConfig ) : void {
11393 this . #toolCallRenderConfigs. update ( ( current ) => [ ...current , renderConfig ] ) ;
11494 }
11595
116- #bindHumanInTheLoopTool(
117- humanInTheLoopTool : HumanInTheLoopConfig
118- ) : FrontendTool {
96+ #bindHumanInTheLoopTool( humanInTheLoopTool : HumanInTheLoopConfig ) : FrontendTool {
11997 return {
12098 ...humanInTheLoopTool ,
12199 handler : ( args , toolCall ) => {
@@ -125,20 +103,14 @@ export class CopilotKit {
125103 }
126104
127105 addHumanInTheLoop ( humanInTheLoopTool : HumanInTheLoopConfig ) : void {
128- this . #humanInTheLoopToolRenderConfigs. update ( ( current ) => [
129- ...current ,
130- humanInTheLoopTool ,
131- ] ) ;
106+ this . #humanInTheLoopToolRenderConfigs. update ( ( current ) => [ ...current , humanInTheLoopTool ] ) ;
132107
133108 const tool = this . #bindHumanInTheLoopTool( humanInTheLoopTool ) ;
134109
135110 this . core . addTool ( tool ) ;
136111 }
137112
138- #isSameAgentId< T extends { agentId ?: string } > (
139- target : T ,
140- agentId ?: string
141- ) : boolean {
113+ #isSameAgentId< T extends { agentId ?: string } > ( target : T , agentId ?: string ) : boolean {
142114 if ( agentId ) {
143115 return target . agentId === agentId ;
144116 }
@@ -149,25 +121,13 @@ export class CopilotKit {
149121 removeTool ( toolName : string , agentId ?: string ) : void {
150122 this . core . removeTool ( toolName ) ;
151123 this . #clientToolCallRenderConfigs. update ( ( current ) =>
152- current . filter (
153- ( renderConfig ) =>
154- renderConfig . name !== toolName &&
155- this . #isSameAgentId( renderConfig , agentId )
156- )
124+ current . filter ( ( renderConfig ) => renderConfig . name !== toolName && this . #isSameAgentId( renderConfig , agentId ) ) ,
157125 ) ;
158126 this . #humanInTheLoopToolRenderConfigs. update ( ( current ) =>
159- current . filter (
160- ( renderConfig ) =>
161- renderConfig . name !== toolName &&
162- this . #isSameAgentId( renderConfig , agentId )
163- )
127+ current . filter ( ( renderConfig ) => renderConfig . name !== toolName && this . #isSameAgentId( renderConfig , agentId ) ) ,
164128 ) ;
165129 this . #toolCallRenderConfigs. update ( ( current ) =>
166- current . filter (
167- ( renderConfig ) =>
168- renderConfig . name !== toolName &&
169- this . #isSameAgentId( renderConfig , agentId )
170- )
130+ current . filter ( ( renderConfig ) => renderConfig . name !== toolName && this . #isSameAgentId( renderConfig , agentId ) ) ,
171131 ) ;
172132 }
173133
0 commit comments