Skip to content

Commit

Permalink
refactor: move fetching thread messages logic to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr1sh1 committed Mar 9, 2024
1 parent 4e5ac7c commit e5f2aff
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 46 deletions.
28 changes: 28 additions & 0 deletions functions/helpers/assistant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import OpenAI from "openai"
import { MessageContentImageFile, MessageContentText, MessageListParams, ThreadMessage } from "openai/resources/beta/threads"
import { Message } from "../types/interfaces";

type Conversation = Message[];

export async function fetchThreadConversation(openai: OpenAI, threadId: string, config: MessageListParams) {
const pages = await openai.beta.threads.messages.list(threadId, config)
let messages: ThreadMessage[] = []

for await (const page of pages.iterPages()) {
messages = messages.concat(page.getPaginatedItems())
}

function isMessageContentText(message: MessageContentText | MessageContentImageFile): message is MessageContentText {
return message.type === "text"
}

const formattedMessages: Conversation = messages.map(message => {
const textMessage: MessageContentText = message.content.filter(isMessageContentText)[0]
return {
role: message.role,
content: textMessage.text.value
}
})

return formattedMessages
}
22 changes: 4 additions & 18 deletions functions/respond.protected.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ServerlessFunctionSignature } from '@twilio-labs/serverless-runtime-types/types';
import { RespondServerlessEventObject, TwilioEnvironmentVariables } from './types/interfaces';
import { sleep } from "openai/core";
import { MessageContentImageFile, MessageContentText, Run, ThreadMessage } from "openai/resources/beta/threads";
import { Run, ThreadMessage } from "openai/resources/beta/threads";
import { ClientManager } from "./helpers/clients";
import { fetchThreadConversation } from './helpers/assistant';

export const handler: ServerlessFunctionSignature<TwilioEnvironmentVariables, RespondServerlessEventObject> = async function (
context,
Expand Down Expand Up @@ -85,22 +86,7 @@ export const handler: ServerlessFunctionSignature<TwilioEnvironmentVariables, Re
}

async function retrieveMessagesFromThread(message: ThreadMessage) {
const pages = await openai.beta.threads.messages.list(callThread, { after: message.id, order: "asc" });
let messages: ThreadMessage[] = []

for await (const page of pages.iterPages()) {
messages = messages.concat(page.getPaginatedItems())
}

function isMessageContentText(message: MessageContentText | MessageContentImageFile): message is MessageContentText {
return message.type === "text"
}

const formattedMessages = messages.map(message => {
const textMessage: MessageContentText = message.content.filter(isMessageContentText)[0]
return textMessage.text.value;
})

return formattedMessages.join(" ");
const conversation = await fetchThreadConversation(openai, callThread, { after: message.id, order: "asc" })
return conversation.map(message => message.content).join(" ")
}
}
33 changes: 5 additions & 28 deletions functions/statusCallback.protected.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ServerlessFunctionSignature } from '@twilio-labs/serverless-runtime-types/types';
import { SQLParam, Message, StatusCallbackServerlessEventObject, SyncDocumentData, TwilioEnvironmentVariables } from './types/interfaces';
import { SQLParam, StatusCallbackServerlessEventObject, SyncDocumentData, TwilioEnvironmentVariables } from './types/interfaces';
import { connect, config, Request, TYPES } from 'mssql';
import { PutObjectCommand } from '@aws-sdk/client-s3';
import { MessageContentImageFile, MessageContentText, ThreadMessage } from 'openai/resources/beta/threads/messages/messages';
import { ClientManager } from './helpers/clients';
import { fetchThreadConversation } from './helpers/assistant';

type Conversation = Message[];
type SQLParams = SQLParam[]

const constructRequest = (request: Request, params: SQLParams) => {
Expand Down Expand Up @@ -105,7 +104,9 @@ export const handler: ServerlessFunctionSignature<TwilioEnvironmentVariables, St
return callback(null)

async function uploadConversationToS3(threadId: string) {
const conversation = await fetchThreadConversation(threadId)
const openai = ClientManager.getOpenAIClient(context)
const conversation = await fetchThreadConversation(openai, threadId, { order: "asc" })

const s3Client = ClientManager.getS3Client(context)

await s3Client.send(
Expand All @@ -116,28 +117,4 @@ export const handler: ServerlessFunctionSignature<TwilioEnvironmentVariables, St
})
);
}

async function fetchThreadConversation(threadId: string) {
const openai = ClientManager.getOpenAIClient(context)
let messages: ThreadMessage[] = []

const pages = await openai.beta.threads.messages.list(threadId, { order: "asc" })
for await (const page of pages.iterPages()) {
messages = messages.concat(page.getPaginatedItems())
}

const formattedMessages: Conversation = messages.map(message => {
const textMessage: MessageContentText = message.content.filter(isMessageContentText)[0]
return {
role: message.role,
content: textMessage.text.value
}
})

function isMessageContentText(message: MessageContentText | MessageContentImageFile): message is MessageContentText {
return message.type === "text"
}

return formattedMessages
}
}

0 comments on commit e5f2aff

Please sign in to comment.