Skip to content

Commit 815407d

Browse files
russellwheatleymikehardy
authored andcommitted
chat-session-helpers.ts
1 parent 9a6b795 commit 815407d

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

packages/ai/lib/methods/chat-session-helpers.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

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';
2020

2121
// https://ai.google.dev/api/rest/v1beta/Content#part
2222

@@ -48,32 +48,29 @@ export function validateChatHistory(history: Content[]): void {
4848
for (const currContent of history) {
4949
const { role, parts } = currContent;
5050
if (!prevContent && role !== 'user') {
51-
throw new VertexAIError(
52-
VertexAIErrorCode.INVALID_CONTENT,
51+
throw new AIError(
52+
AIErrorCode.INVALID_CONTENT,
5353
`First Content should be with role 'user', got ${role}`,
5454
);
5555
}
5656
if (!POSSIBLE_ROLES.includes(role)) {
57-
throw new VertexAIError(
58-
VertexAIErrorCode.INVALID_CONTENT,
57+
throw new AIError(
58+
AIErrorCode.INVALID_CONTENT,
5959
`Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(
6060
POSSIBLE_ROLES,
6161
)}`,
6262
);
6363
}
6464

6565
if (!Array.isArray(parts)) {
66-
throw new VertexAIError(
67-
VertexAIErrorCode.INVALID_CONTENT,
66+
throw new AIError(
67+
AIErrorCode.INVALID_CONTENT,
6868
`Content should have 'parts' but property with an array of Parts`,
6969
);
7070
}
7171

7272
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`);
7774
}
7875

7976
const countFields: Record<keyof Part, number> = {
@@ -93,8 +90,8 @@ export function validateChatHistory(history: Content[]): void {
9390
const validParts = VALID_PARTS_PER_ROLE[role];
9491
for (const key of VALID_PART_FIELDS) {
9592
if (!validParts.includes(key) && countFields[key] > 0) {
96-
throw new VertexAIError(
97-
VertexAIErrorCode.INVALID_CONTENT,
93+
throw new AIError(
94+
AIErrorCode.INVALID_CONTENT,
9895
`Content with role '${role}' can't contain '${key}' part`,
9996
);
10097
}
@@ -103,9 +100,9 @@ export function validateChatHistory(history: Content[]): void {
103100
if (prevContent) {
104101
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
105102
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 '${
109106
prevContent.role
110107
}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`,
111108
);

0 commit comments

Comments
 (0)