15
15
* limitations under the License.
16
16
*/
17
17
18
- import { Content , POSSIBLE_ROLES , Part , Role , VertexAIErrorCode } from '../types' ;
19
- import { VertexAIError } from '../errors' ;
18
+ import { Content , POSSIBLE_ROLES , Part , Role , AIErrorCode } from '../types' ;
19
+ import { AIError } from '../errors' ;
20
20
21
21
// https://ai.google.dev/api/rest/v1beta/Content#part
22
22
@@ -48,32 +48,29 @@ export function validateChatHistory(history: Content[]): void {
48
48
for ( const currContent of history ) {
49
49
const { role, parts } = currContent ;
50
50
if ( ! prevContent && role !== 'user' ) {
51
- throw new VertexAIError (
52
- VertexAIErrorCode . INVALID_CONTENT ,
51
+ throw new AIError (
52
+ AIErrorCode . INVALID_CONTENT ,
53
53
`First Content should be with role 'user', got ${ role } ` ,
54
54
) ;
55
55
}
56
56
if ( ! POSSIBLE_ROLES . includes ( role ) ) {
57
- throw new VertexAIError (
58
- VertexAIErrorCode . INVALID_CONTENT ,
57
+ throw new AIError (
58
+ AIErrorCode . INVALID_CONTENT ,
59
59
`Each item should include role field. Got ${ role } but valid roles are: ${ JSON . stringify (
60
60
POSSIBLE_ROLES ,
61
61
) } `,
62
62
) ;
63
63
}
64
64
65
65
if ( ! Array . isArray ( parts ) ) {
66
- throw new VertexAIError (
67
- VertexAIErrorCode . INVALID_CONTENT ,
66
+ throw new AIError (
67
+ AIErrorCode . INVALID_CONTENT ,
68
68
`Content should have 'parts' but property with an array of Parts` ,
69
69
) ;
70
70
}
71
71
72
72
if ( parts . length === 0 ) {
73
- throw new VertexAIError (
74
- VertexAIErrorCode . INVALID_CONTENT ,
75
- `Each Content should have at least one part` ,
76
- ) ;
73
+ throw new AIError ( AIErrorCode . INVALID_CONTENT , `Each Content should have at least one part` ) ;
77
74
}
78
75
79
76
const countFields : Record < keyof Part , number > = {
@@ -93,8 +90,8 @@ export function validateChatHistory(history: Content[]): void {
93
90
const validParts = VALID_PARTS_PER_ROLE [ role ] ;
94
91
for ( const key of VALID_PART_FIELDS ) {
95
92
if ( ! validParts . includes ( key ) && countFields [ key ] > 0 ) {
96
- throw new VertexAIError (
97
- VertexAIErrorCode . INVALID_CONTENT ,
93
+ throw new AIError (
94
+ AIErrorCode . INVALID_CONTENT ,
98
95
`Content with role '${ role } ' can't contain '${ key } ' part` ,
99
96
) ;
100
97
}
@@ -103,9 +100,9 @@ export function validateChatHistory(history: Content[]): void {
103
100
if ( prevContent ) {
104
101
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES [ role ] ;
105
102
if ( ! validPreviousContentRoles . includes ( prevContent . role ) ) {
106
- throw new VertexAIError (
107
- VertexAIErrorCode . INVALID_CONTENT ,
108
- `Content with role '${ role } can't follow '${
103
+ throw new AIError (
104
+ AIErrorCode . INVALID_CONTENT ,
105
+ `Content with role '${ role } ' can't follow '${
109
106
prevContent . role
110
107
} '. Valid previous roles: ${ JSON . stringify ( VALID_PREVIOUS_CONTENT_ROLES ) } `,
111
108
) ;
0 commit comments