6
6
*/
7
7
8
8
import { randomUUID } from 'node:crypto' ;
9
+ import { SfError } from '@salesforce/core' ;
9
10
import { Connection } from '@salesforce/core' ;
10
11
import { MaybeMock } from './maybe-mock' ;
11
12
@@ -31,8 +32,20 @@ export type AgentPreviewMessageLinks = {
31
32
end : Href | null ;
32
33
} ;
33
34
35
+ export type MessageType =
36
+ | 'Inform'
37
+ | 'TextChunk'
38
+ | 'ProgressIndicator'
39
+ | 'Inquire'
40
+ | 'Confirm'
41
+ | 'Failure'
42
+ | 'Escalate'
43
+ | 'SessionEnded'
44
+ | 'EndOfTurn'
45
+ | 'Error' ;
46
+
34
47
export type AgentPreviewMessage = {
35
- type : string ;
48
+ type : MessageType ;
36
49
id : string ;
37
50
feedbackId : string ;
38
51
planId : string ;
@@ -98,7 +111,12 @@ export class AgentPreview {
98
111
bypassUser : true ,
99
112
} ;
100
113
101
- return this . maybeMock . request < AgentPreviewStartResponse > ( 'POST' , url , body ) ;
114
+ try {
115
+ return await this . maybeMock . request < AgentPreviewStartResponse > ( 'POST' , url , body ) ;
116
+ } catch ( err ) {
117
+ const error = err as Error ;
118
+ throw new SfError ( error . message , 'AgentPreviewStartError' , undefined , error ) ;
119
+ }
102
120
}
103
121
104
122
public async send ( sessionId : string , message : string ) : Promise < AgentPreviewSendResponse > {
@@ -115,23 +133,38 @@ export class AgentPreview {
115
133
variables : [ ] ,
116
134
} ;
117
135
118
- return this . maybeMock . request < AgentPreviewSendResponse > ( 'POST' , url , body ) ;
136
+ try {
137
+ return await this . maybeMock . request < AgentPreviewSendResponse > ( 'POST' , url , body ) ;
138
+ } catch ( err ) {
139
+ const error = err as Error ;
140
+ throw new SfError ( error . message , 'AgentPreviewSendError' , undefined , error ) ;
141
+ }
119
142
}
120
143
121
144
public async end ( sessionId : string , reason : EndReason ) : Promise < AgentPreviewEndResponse > {
122
145
const url = `${ this . apiBase } /sessions/${ sessionId } ` ;
123
146
124
- // https://developer.salesforce.com/docs/einstein/genai/guide/agent-api-examples.html#end-session
125
- return this . maybeMock . request < AgentPreviewEndResponse > ( 'DELETE' , url , undefined , {
126
- 'x-session-end-reason' : reason ,
127
- } ) ;
147
+ try {
148
+ // https://developer.salesforce.com/docs/einstein/genai/guide/agent-api-examples.html#end-session
149
+ return await this . maybeMock . request < AgentPreviewEndResponse > ( 'DELETE' , url , undefined , {
150
+ 'x-session-end-reason' : reason ,
151
+ } ) ;
152
+ } catch ( err ) {
153
+ const error = err as Error ;
154
+ throw new SfError ( error . message , 'AgentPreviewEndError' , undefined , error ) ;
155
+ }
128
156
}
129
157
130
158
// Get the status of the Agent API (UP | DOWN)
131
159
public async status ( ) : Promise < ApiStatus > {
132
160
const base = 'https://test.api.salesforce.com' ;
133
161
const url = `${ base } /einstein/ai-agent/v1/status` ;
134
162
135
- return this . maybeMock . request < ApiStatus > ( 'GET' , url ) ;
163
+ try {
164
+ return await this . maybeMock . request < ApiStatus > ( 'GET' , url ) ;
165
+ } catch ( err ) {
166
+ const error = err as Error ;
167
+ throw new SfError ( error . message , 'AgentPreviewStatusError' , undefined , error ) ;
168
+ }
136
169
}
137
170
}
0 commit comments