Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions src/models/chat_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ export class ChatSession {
async sendMessage(
request: string | Array<string | Part>
): Promise<GenerateContentResult> {
const newContent: Content[] =
formulateNewContentFromSendMessageRequest(request);
const newContent: Content[] = formulateNewContentFromSendMessageRequest(
request,
this.historyInternal
);
const generateContentRequest: GenerateContentRequest = {
contents: this.historyInternal.concat(newContent),
safetySettings: this.safetySettings,
Expand Down Expand Up @@ -211,8 +213,10 @@ export class ChatSession {
async sendMessageStream(
request: string | Array<string | Part>
): Promise<StreamGenerateContentResult> {
const newContent: Content[] =
formulateNewContentFromSendMessageRequest(request);
const newContent: Content[] = formulateNewContentFromSendMessageRequest(
request,
this.historyInternal
);
const generateContentrequest: GenerateContentRequest = {
contents: this.historyInternal.concat(newContent),
safetySettings: this.safetySettings,
Expand Down Expand Up @@ -337,8 +341,10 @@ export class ChatSessionPreview {
async sendMessage(
request: string | Array<string | Part>
): Promise<GenerateContentResult> {
const newContent: Content[] =
formulateNewContentFromSendMessageRequest(request);
const newContent: Content[] = formulateNewContentFromSendMessageRequest(
request,
this.historyInternal
);
const generateContentRequest: GenerateContentRequest = {
contents: this.historyInternal.concat(newContent),
safetySettings: this.safetySettings,
Expand Down Expand Up @@ -424,8 +430,10 @@ export class ChatSessionPreview {
async sendMessageStream(
request: string | Array<string | Part>
): Promise<StreamGenerateContentResult> {
const newContent: Content[] =
formulateNewContentFromSendMessageRequest(request);
const newContent: Content[] = formulateNewContentFromSendMessageRequest(
request,
this.historyInternal
);
const generateContentRequest: GenerateContentRequest = {
contents: this.historyInternal.concat(newContent),
safetySettings: this.safetySettings,
Expand Down Expand Up @@ -464,7 +472,8 @@ export class ChatSessionPreview {
}

function formulateNewContentFromSendMessageRequest(
request: string | Array<string | Part>
request: string | Array<string | Part>,
historyInternal: Content[]
): Content[] {
let newParts: Part[] = [];

Expand All @@ -480,7 +489,10 @@ function formulateNewContentFromSendMessageRequest(
}
}

return assignRoleToPartsAndValidateSendMessageRequest(newParts);
return assignRoleToPartsAndValidateSendMessageRequest(
newParts,
historyInternal
);
}

/**
Expand All @@ -492,10 +504,15 @@ function formulateNewContentFromSendMessageRequest(
* @returns Array of content items
*/
function assignRoleToPartsAndValidateSendMessageRequest(
parts: Array<Part>
parts: Array<Part>,
historyInternal: Content[]
): Content[] {
const userContent: Content = {role: constants.USER_ROLE, parts: []};
const functionContent: Content = {role: constants.USER_ROLE, parts: []};
let id: string = crypto.randomUUID();
while (historyInternal.some(content => content.id === id)) {
id = crypto.randomUUID();
}
const userContent: Content = {id, role: constants.USER_ROLE, parts: []};
const functionContent: Content = {id, role: constants.USER_ROLE, parts: []};
let hasUserContent = false;
let hasFunctionContent = false;
for (const part of parts) {
Expand Down
2 changes: 2 additions & 0 deletions src/types/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ export declare interface Content {
parts: Part[];
/** The producer of the content. */
role: string;
/** The unique id of the content */
id?: string;
}

/**
Expand Down