4
4
condition ,
5
5
log ,
6
6
step ,
7
- agentInfo
7
+ agentInfo ,
8
+ AgentError
8
9
} from "@restackio/ai/agent" ;
9
10
import * as functions from "../functions" ;
10
11
@@ -18,22 +19,23 @@ export type CallEvent = {
18
19
19
20
export const messagesEvent = defineEvent < functions . Message [ ] > ( "messages" ) ;
20
21
export const endEvent = defineEvent ( "end" ) ;
21
- export const callEvent = defineEvent < string > ( "call" ) ;
22
+ export const callEvent = defineEvent < { phoneNumber : string ; sipCallId : string } > ( "call" ) ;
22
23
23
24
type agentTwilioOutput = {
24
25
messages : functions . Message [ ] ;
25
26
} ;
26
27
28
+
27
29
export async function agentTwilio ( ) : Promise < agentTwilioOutput > {
28
30
let endReceived = false ;
29
31
let messages : functions . Message [ ] = [ {
30
32
role : "system" ,
31
33
content : "You are a sales assistant making outbound calls to potential customers. Your answers are used in a text to speech, be concise and natural."
32
34
} ] ;
33
35
let roomId : string ;
34
- let recordingApproved = true
35
36
36
37
onEvent ( messagesEvent , async ( { messages, stream = true } : { messages : functions . Message [ ] , stream ?: boolean } ) => {
38
+ try {
37
39
const result = await step < typeof functions > ( { } ) . llmChat ( {
38
40
messages,
39
41
} ) ;
@@ -42,20 +44,32 @@ export async function agentTwilio(): Promise<agentTwilioOutput> {
42
44
43
45
messages . push ( result ) ;
44
46
return messages ;
47
+ } catch ( error ) {
48
+ log . error ( "error" , { error } ) ;
49
+ throw AgentError . nonRetryable ( "Error messagesEvent:" , null , { error} ) ;
50
+ }
45
51
} ) ;
46
52
47
53
onEvent ( callEvent , async ( { phoneNumber } : CallEvent ) => {
48
- const agentName = agentInfo ( ) . workflowType
49
- const agentId = agentInfo ( ) . workflowId
50
- const runId = agentInfo ( ) . runId
51
-
52
- const trunk = await step < typeof functions > ( { } ) . livekitOutboundTrunk ( ) ;
53
-
54
- const sipTrunkId = trunk . sipTrunkId
55
-
56
- const call = await step < typeof functions > ( { } ) . livekitCall ( { sipTrunkId, phoneNumber, roomId, agentName, agentId, runId} ) ;
57
-
58
- return call . sipCallId
54
+ try {
55
+ const agentName = agentInfo ( ) . workflowType
56
+ const agentId = agentInfo ( ) . workflowId
57
+ const runId = agentInfo ( ) . runId
58
+
59
+ const trunk = await step < typeof functions > ( { } ) . livekitOutboundTrunk ( ) ;
60
+
61
+ const sipTrunkId = trunk . sipTrunkId
62
+
63
+ const call = await step < typeof functions > ( { } ) . livekitCall ( { sipTrunkId, phoneNumber, roomId, agentName, agentId, runId} ) ;
64
+
65
+ return {
66
+ phoneNumber,
67
+ sipCallId : call . sipCallId
68
+ }
69
+ } catch ( error ) {
70
+ log . error ( "error" , { error } ) ;
71
+ throw AgentError . nonRetryable ( "Error callEvent: " , null , { error} ) ;
72
+ }
59
73
} ) ;
60
74
61
75
onEvent ( endEvent , async ( ) => {
0 commit comments