1
+ import { HandOffBuilder } from '@botonic/core'
1
2
import { ActionRequest , Text } from '@botonic/react'
2
3
import axios , { AxiosResponse } from 'axios'
3
4
@@ -110,6 +111,35 @@ const actionResults = (parameters: any) => {
110
111
status : 'on time' ,
111
112
...parameters ,
112
113
} ,
114
+ 'human-agent-handoff' : {
115
+ queueId : 'b5062566-b5f6-4e68-be44-d95697dbac11' ,
116
+ ...parameters ,
117
+ } ,
118
+ 'available-flights' : {
119
+ flights : [
120
+ {
121
+ number : 'XDC1234' ,
122
+ departureTime : new Date ( ) . toISOString ( ) ,
123
+ arrivalTime : new Date (
124
+ new Date ( ) . getTime ( ) + 2 * 60 * 60 * 1000
125
+ ) . toISOString ( ) ,
126
+ departureLocation : parameters . departure_location ,
127
+ arrivalLocation : 'Madrid' ,
128
+ status : 'on time' ,
129
+ } ,
130
+ {
131
+ number : 'XDC1235' ,
132
+ departureTime : new Date ( ) . toISOString ( ) ,
133
+ arrivalTime : new Date (
134
+ new Date ( ) . getTime ( ) + 3 * 60 * 60 * 1000
135
+ ) . toISOString ( ) ,
136
+ departureLocation : parameters . departureLocation ,
137
+ arrivalLocation : 'London' ,
138
+ status : 'delayed' ,
139
+ } ,
140
+ ] ,
141
+ ...parameters ,
142
+ } ,
113
143
}
114
144
}
115
145
@@ -148,13 +178,32 @@ class AiAgentAPI {
148
178
// TODO: Resolve how to executed the actions in the bot
149
179
// const result = action.name(action.parameters)
150
180
const result = actionResults ( action . parameters ) [ action . name ]
181
+ if ( action . name === 'human-agent-handoff' ) {
182
+ await this . handoff ( request , result . queueId )
183
+ newActionCallsOutputs . push ( {
184
+ name : action . name ,
185
+ args : action . parameters ,
186
+ call_id : action . call_id ,
187
+ output : 'Case created' ,
188
+ } )
189
+ } else if ( action . name === 'exit' ) {
190
+ console . log ( 'EXIT!!!!!!!!' )
191
+ request . session . aiAgent ! . active = false
192
+ newActionCallsOutputs . push ( {
193
+ name : action . name ,
194
+ args : action . parameters ,
195
+ call_id : action . call_id ,
196
+ output : 'ya no hablas con ai agent' ,
197
+ } )
198
+ } else {
199
+ newActionCallsOutputs . push ( {
200
+ name : action . name ,
201
+ args : action . parameters ,
202
+ call_id : action . call_id ,
203
+ output : JSON . stringify ( result ) ,
204
+ } )
205
+ }
151
206
console . log ( 'Response with actions:' , response )
152
- newActionCallsOutputs . push ( {
153
- name : action . name ,
154
- args : action . parameters ,
155
- call_id : action . call_id ,
156
- output : JSON . stringify ( result ) ,
157
- } )
158
207
}
159
208
160
209
return await this . runLoop (
@@ -214,8 +263,8 @@ class AiAgentAPI {
214
263
toolMessages . push ( toolMessage )
215
264
toolCalls . push ( {
216
265
id : action_call_output . call_id ,
217
- name : 'action' ,
218
- args : { } ,
266
+ name : action_call_output . name , // 'action',
267
+ args : action_call_output . args , // {},
219
268
} )
220
269
}
221
270
messages . push ( { role : 'ai' , tool_calls : toolCalls } )
@@ -239,12 +288,18 @@ class AiAgentAPI {
239
288
240
289
if ( response . data . actions ) {
241
290
console . log ( 'Actions from api agent ai' , response . data . actions )
242
- request . session . aiAgent ! . active = false
291
+ // request.session.aiAgent!.active = false
243
292
return { actions : response . data . actions }
244
293
}
245
294
} catch ( e ) {
246
295
console . error ( e )
247
296
}
248
297
return { answer : 'API call failed' }
249
298
}
299
+
300
+ async handoff ( request : ActionRequest , queueId : string ) : Promise < void > {
301
+ const handoffBuilder = new HandOffBuilder ( request . session )
302
+ handoffBuilder . withQueue ( queueId )
303
+ handoffBuilder . handOff ( )
304
+ }
250
305
}
0 commit comments