Skip to content

Commit

Permalink
Loosen Role type (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 authored Apr 3, 2024
1 parent 3f95168 commit e636823
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-yaks-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": minor
---

Loosen role field typing on Content.
2 changes: 1 addition & 1 deletion docs/reference/generative-ai.content.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export interface Content
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [parts](./generative-ai.content.parts.md) | | [Part](./generative-ai.part.md)<!-- -->\[\] | |
| [role](./generative-ai.content.role.md) | | [Role](./generative-ai.role.md) | |
| [role](./generative-ai.content.role.md) | | string | |

2 changes: 1 addition & 1 deletion docs/reference/generative-ai.content.role.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
**Signature:**

```typescript
role: Role;
role: string;
```
1 change: 0 additions & 1 deletion docs/reference/generative-ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@
| Type Alias | Description |
| --- | --- |
| [Part](./generative-ai.part.md) | Content part - includes text or image part types. |
| [Role](./generative-ai.role.md) | Role is the producer of the content. |
| [Tool](./generative-ai.tool.md) | Defines a tool that model can call to access external knowledge. |

15 changes: 0 additions & 15 deletions docs/reference/generative-ai.role.md

This file was deleted.

8 changes: 5 additions & 3 deletions packages/main/src/methods/chat-session-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
* limitations under the License.
*/

import { Content, POSSIBLE_ROLES, Part, Role } from "../../types";
import { Content, POSSIBLE_ROLES, Part } from "../../types";
import { GoogleGenerativeAIError } from "../errors";

type Role = (typeof POSSIBLE_ROLES)[number];

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

const VALID_PART_FIELDS: Array<keyof Part> = [
Expand All @@ -42,7 +44,7 @@ const VALID_PREVIOUS_CONTENT_ROLES: { [key in Role]: Role[] } = {
export function validateChatHistory(history: Content[]): void {
let prevContent: Content;
for (const currContent of history) {
const { role, parts } = currContent;
const { role, parts } = currContent as { role: Role; parts: Part[] };
if (!prevContent && role !== "user") {
throw new GoogleGenerativeAIError(
`First content should be with role 'user', got ${role}`,
Expand Down Expand Up @@ -93,7 +95,7 @@ export function validateChatHistory(history: Content[]): void {

if (prevContent) {
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
if (!validPreviousContentRoles.includes(prevContent.role)) {
if (!validPreviousContentRoles.includes(prevContent.role as Role)) {
throw new GoogleGenerativeAIError(
`Content with role '${role}' can't follow '${
prevContent.role
Expand Down
4 changes: 1 addition & 3 deletions packages/main/types/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
* limitations under the License.
*/

import { Role } from "./enums";

/**
* Content type for both prompts and response candidates.
* @public
*/
export interface Content {
role: Role;
role: string;
parts: Part[];
}

Expand Down
6 changes: 0 additions & 6 deletions packages/main/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
* limitations under the License.
*/

/**
* Role is the producer of the content.
* @public
*/
export type Role = (typeof POSSIBLE_ROLES)[number];

/**
* Possible roles.
* @public
Expand Down

0 comments on commit e636823

Please sign in to comment.