@@ -21,9 +21,10 @@ import {
21
21
FunctionCall ,
22
22
GenerateContentCandidate ,
23
23
GenerateContentResponse ,
24
- VertexAIErrorCode ,
24
+ AIErrorCode ,
25
+ InlineDataPart ,
25
26
} from '../types' ;
26
- import { VertexAIError } from '../errors' ;
27
+ import { AIError } from '../errors' ;
27
28
import { logger } from '../logger' ;
28
29
29
30
/**
@@ -62,8 +63,8 @@ export function addHelpers(response: GenerateContentResponse): EnhancedGenerateC
62
63
) ;
63
64
}
64
65
if ( hadBadFinishReason ( response . candidates [ 0 ] ! ) ) {
65
- throw new VertexAIError (
66
- VertexAIErrorCode . RESPONSE_ERROR ,
66
+ throw new AIError (
67
+ AIErrorCode . RESPONSE_ERROR ,
67
68
`Response error: ${ formatBlockErrorMessage (
68
69
response ,
69
70
) } . Response body stored in error.response`,
@@ -74,8 +75,8 @@ export function addHelpers(response: GenerateContentResponse): EnhancedGenerateC
74
75
}
75
76
return getText ( response ) ;
76
77
} else if ( response . promptFeedback ) {
77
- throw new VertexAIError (
78
- VertexAIErrorCode . RESPONSE_ERROR ,
78
+ throw new AIError (
79
+ AIErrorCode . RESPONSE_ERROR ,
79
80
`Text not available. ${ formatBlockErrorMessage ( response ) } ` ,
80
81
{
81
82
response,
@@ -84,6 +85,40 @@ export function addHelpers(response: GenerateContentResponse): EnhancedGenerateC
84
85
}
85
86
return '' ;
86
87
} ;
88
+ ( response as EnhancedGenerateContentResponse ) . inlineDataParts = ( ) :
89
+ | InlineDataPart [ ]
90
+ | undefined => {
91
+ if ( response . candidates && response . candidates . length > 0 ) {
92
+ if ( response . candidates . length > 1 ) {
93
+ logger . warn (
94
+ `This response had ${ response . candidates . length } ` +
95
+ `candidates. Returning data from the first candidate only. ` +
96
+ `Access response.candidates directly to use the other candidates.` ,
97
+ ) ;
98
+ }
99
+ if ( hadBadFinishReason ( response . candidates [ 0 ] ! ) ) {
100
+ throw new AIError (
101
+ AIErrorCode . RESPONSE_ERROR ,
102
+ `Response error: ${ formatBlockErrorMessage (
103
+ response ,
104
+ ) } . Response body stored in error.response`,
105
+ {
106
+ response,
107
+ } ,
108
+ ) ;
109
+ }
110
+ return getInlineDataParts ( response ) ;
111
+ } else if ( response . promptFeedback ) {
112
+ throw new AIError (
113
+ AIErrorCode . RESPONSE_ERROR ,
114
+ `Data not available. ${ formatBlockErrorMessage ( response ) } ` ,
115
+ {
116
+ response,
117
+ } ,
118
+ ) ;
119
+ }
120
+ return undefined ;
121
+ } ;
87
122
( response as EnhancedGenerateContentResponse ) . functionCalls = ( ) => {
88
123
if ( response . candidates && response . candidates . length > 0 ) {
89
124
if ( response . candidates . length > 1 ) {
@@ -94,8 +129,8 @@ export function addHelpers(response: GenerateContentResponse): EnhancedGenerateC
94
129
) ;
95
130
}
96
131
if ( hadBadFinishReason ( response . candidates [ 0 ] ! ) ) {
97
- throw new VertexAIError (
98
- VertexAIErrorCode . RESPONSE_ERROR ,
132
+ throw new AIError (
133
+ AIErrorCode . RESPONSE_ERROR ,
99
134
`Response error: ${ formatBlockErrorMessage (
100
135
response ,
101
136
) } . Response body stored in error.response`,
@@ -106,8 +141,8 @@ export function addHelpers(response: GenerateContentResponse): EnhancedGenerateC
106
141
}
107
142
return getFunctionCalls ( response ) ;
108
143
} else if ( response . promptFeedback ) {
109
- throw new VertexAIError (
110
- VertexAIErrorCode . RESPONSE_ERROR ,
144
+ throw new AIError (
145
+ AIErrorCode . RESPONSE_ERROR ,
111
146
`Function call not available. ${ formatBlockErrorMessage ( response ) } ` ,
112
147
{
113
148
response,
@@ -125,7 +160,7 @@ export function addHelpers(response: GenerateContentResponse): EnhancedGenerateC
125
160
export function getText ( response : GenerateContentResponse ) : string {
126
161
const textStrings = [ ] ;
127
162
if ( response . candidates ?. [ 0 ] ?. content ?. parts ) {
128
- for ( const part of response . candidates ?. [ 0 ] . content ?. parts ) {
163
+ for ( const part of response . candidates ?. [ 0 ] ? .content ?. parts ) {
129
164
if ( part . text ) {
130
165
textStrings . push ( part . text ) ;
131
166
}
@@ -139,7 +174,7 @@ export function getText(response: GenerateContentResponse): string {
139
174
}
140
175
141
176
/**
142
- * Returns <code> {@link FunctionCall}</code> s associated with first candidate.
177
+ * Returns {@link FunctionCall}s associated with first candidate.
143
178
*/
144
179
export function getFunctionCalls ( response : GenerateContentResponse ) : FunctionCall [ ] | undefined {
145
180
const functionCalls : FunctionCall [ ] = [ ] ;
@@ -157,6 +192,31 @@ export function getFunctionCalls(response: GenerateContentResponse): FunctionCal
157
192
}
158
193
}
159
194
195
+ /**
196
+ * Returns {@link InlineDataPart}s in the first candidate if present.
197
+ *
198
+ * @internal
199
+ */
200
+ export function getInlineDataParts (
201
+ response : GenerateContentResponse ,
202
+ ) : InlineDataPart [ ] | undefined {
203
+ const data : InlineDataPart [ ] = [ ] ;
204
+
205
+ if ( response . candidates ?. [ 0 ] ?. content ?. parts ) {
206
+ for ( const part of response . candidates ?. [ 0 ] ?. content ?. parts ) {
207
+ if ( part . inlineData ) {
208
+ data . push ( part ) ;
209
+ }
210
+ }
211
+ }
212
+
213
+ if ( data . length > 0 ) {
214
+ return data ;
215
+ } else {
216
+ return undefined ;
217
+ }
218
+ }
219
+
160
220
const badFinishReasons = [ FinishReason . RECITATION , FinishReason . SAFETY ] ;
161
221
162
222
function hadBadFinishReason ( candidate : GenerateContentCandidate ) : boolean {
0 commit comments