@@ -11,6 +11,7 @@ export interface SimpleAgentConfig {
11
11
markdown ?: boolean ;
12
12
stream ?: boolean ;
13
13
tools ?: any [ ] ;
14
+ toolFunctions ?: Record < string , Function > ;
14
15
}
15
16
16
17
export class Agent {
@@ -39,6 +40,18 @@ export class Agent {
39
40
// Configure logging
40
41
Logger . setVerbose ( this . verbose ) ;
41
42
Logger . setPretty ( this . pretty ) ;
43
+
44
+ // Register directly provided tool functions if any
45
+ if ( config . toolFunctions ) {
46
+ for ( const [ name , func ] of Object . entries ( config . toolFunctions ) ) {
47
+ this . registerToolFunction ( name , func ) ;
48
+
49
+ // Auto-generate tool definition if not already provided
50
+ if ( ! this . hasToolDefinition ( name ) ) {
51
+ this . addAutoGeneratedToolDefinition ( name , func ) ;
52
+ }
53
+ }
54
+ }
42
55
}
43
56
44
57
private createSystemPrompt ( ) : string {
@@ -58,6 +71,76 @@ export class Agent {
58
71
this . toolFunctions [ name ] = fn ;
59
72
Logger . debug ( `Registered tool function: ${ name } ` ) ;
60
73
}
74
+
75
+ /**
76
+ * Check if a tool definition exists for the given function name
77
+ * @param name Function name
78
+ * @returns True if a tool definition exists
79
+ */
80
+ private hasToolDefinition ( name : string ) : boolean {
81
+ if ( ! this . tools ) return false ;
82
+
83
+ return this . tools . some ( tool => {
84
+ if ( tool . type === 'function' && tool . function ) {
85
+ return tool . function . name === name ;
86
+ }
87
+ return false ;
88
+ } ) ;
89
+ }
90
+
91
+ /**
92
+ * Auto-generate a tool definition based on the function
93
+ * @param name Function name
94
+ * @param func Function implementation
95
+ */
96
+ private addAutoGeneratedToolDefinition ( name : string , func : Function ) : void {
97
+ if ( ! this . tools ) {
98
+ this . tools = [ ] ;
99
+ }
100
+
101
+ // Extract parameter names from function
102
+ const funcStr = func . toString ( ) ;
103
+ const paramMatch = funcStr . match ( / \( ( [ ^ ) ] * ) \) / ) ;
104
+ const params = paramMatch ? paramMatch [ 1 ] . split ( ',' ) . map ( p => p . trim ( ) ) . filter ( p => p ) : [ ] ;
105
+
106
+ // Create a basic tool definition
107
+ const toolDef = {
108
+ type : "function" ,
109
+ function : {
110
+ name,
111
+ description : `Auto-generated function for ${ name } ` ,
112
+ parameters : {
113
+ type : "object" ,
114
+ properties : { } ,
115
+ required : [ ] as string [ ]
116
+ }
117
+ }
118
+ } ;
119
+
120
+ // Add parameters to the definition
121
+ if ( params . length > 0 ) {
122
+ const properties : Record < string , any > = { } ;
123
+ const required : string [ ] = [ ] ;
124
+
125
+ params . forEach ( param => {
126
+ // Remove type annotations if present
127
+ const paramName = param . split ( ':' ) [ 0 ] . trim ( ) ;
128
+ if ( paramName ) {
129
+ properties [ paramName ] = {
130
+ type : "string" ,
131
+ description : `Parameter ${ paramName } for function ${ name } `
132
+ } ;
133
+ required . push ( paramName ) ;
134
+ }
135
+ } ) ;
136
+
137
+ toolDef . function . parameters . properties = properties ;
138
+ toolDef . function . parameters . required = required ;
139
+ }
140
+
141
+ this . tools . push ( toolDef ) ;
142
+ Logger . debug ( `Auto-generated tool definition for ${ name } ` ) ;
143
+ }
61
144
62
145
/**
63
146
* Process tool calls from the model
0 commit comments