From 5b13de377b7d94c6912d64122bd8701d412948b3 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 18 Jul 2024 08:48:56 -0300 Subject: [PATCH] feat: started openai integration, added models to the database This commit introduces a new openai integration in the project, along with necessary changes in the database schema. The package.json file has been updated to include the openai package, and the prisma/postgresql-schema.prisma file has been modified to include OpenaiCreds, OpenaiAssistant, OpenaiAssistantThread, and OpenaiChatCompletion models. These models will handle the integration with OpenAI's API for various tasks. Some of the key changes include: - Addition of OpenaiCreds model to manage API credentials. - Addition of OpenaiAssistant model to handle OpenAI assistant instances. - Addition of OpenaiAssistantThread model to manage OpenAI assistant threads. - Addition of OpenaiChatCompletion model to handle OpenAI chat completions. These changes will enable seamless integration with OpenAI's API and allow for better management of assistant instances, threads, and chat completions. --- package.json | 1 + prisma/postgresql-schema.prisma | 195 ++++++++++++++++++++++++-------- 2 files changed, 146 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index e6295277..2a74fa64 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "node-cache": "^5.1.2", "node-mime-types": "^1.1.0", "node-windows": "^1.0.0-beta.8", + "openai": "^4.52.7", "parse-bmfont-xml": "^1.1.4", "pg": "^8.11.3", "pino": "^8.11.0", diff --git a/prisma/postgresql-schema.prisma b/prisma/postgresql-schema.prisma index d90d0384..62aab81f 100644 --- a/prisma/postgresql-schema.prisma +++ b/prisma/postgresql-schema.prisma @@ -47,36 +47,41 @@ enum TriggerOperator { } model Instance { - id String @id @default(cuid()) - name String @unique @db.VarChar(255) - connectionStatus InstanceConnectionStatus @default(open) - ownerJid String? @db.VarChar(100) - profileName String? @db.VarChar(100) - profilePicUrl String? @db.VarChar(500) - integration String? @db.VarChar(100) - number String? @db.VarChar(100) - businessId String? @db.VarChar(100) - token String? @unique @db.VarChar(255) - clientName String? @db.VarChar(100) - createdAt DateTime? @default(now()) @db.Timestamp - updatedAt DateTime? @updatedAt @db.Timestamp - Chat Chat[] - Contact Contact[] - Message Message[] - Webhook Webhook? - Chatwoot Chatwoot? - Label Label[] - Proxy Proxy? - Setting Setting? - Rabbitmq Rabbitmq? - Sqs Sqs? - Websocket Websocket? - Typebot Typebot[] - Session Session? - MessageUpdate MessageUpdate[] - TypebotSession TypebotSession[] - TypebotSetting TypebotSetting? - Media Media[] + id String @id @default(cuid()) + name String @unique @db.VarChar(255) + connectionStatus InstanceConnectionStatus @default(open) + ownerJid String? @db.VarChar(100) + profileName String? @db.VarChar(100) + profilePicUrl String? @db.VarChar(500) + integration String? @db.VarChar(100) + number String? @db.VarChar(100) + businessId String? @db.VarChar(100) + token String? @unique @db.VarChar(255) + clientName String? @db.VarChar(100) + createdAt DateTime? @default(now()) @db.Timestamp + updatedAt DateTime? @updatedAt @db.Timestamp + Chat Chat[] + Contact Contact[] + Message Message[] + Webhook Webhook? + Chatwoot Chatwoot? + Label Label[] + Proxy Proxy? + Setting Setting? + Rabbitmq Rabbitmq? + Sqs Sqs? + Websocket Websocket? + Typebot Typebot[] + Session Session? + MessageUpdate MessageUpdate[] + TypebotSession TypebotSession[] + TypebotSetting TypebotSetting? + Media Media[] + OpenaiCreds OpenaiCreds? + OpenaiAssistant OpenaiAssistant[] + OpenaiAssistantThread OpenaiAssistantThread[] + OpenaiChatCompletion OpenaiChatCompletion[] + OpenaiChatCompletionSession OpenaiChatCompletionSession[] } model Session { @@ -109,26 +114,30 @@ model Contact { } model Message { - id String @id @default(cuid()) - key Json @db.JsonB - pushName String? @db.VarChar(100) - participant String? @db.VarChar(100) - messageType String @db.VarChar(100) - message Json @db.JsonB - contextInfo Json? @db.JsonB - source DeviceMessage - messageTimestamp Int @db.Integer - chatwootMessageId Int? @db.Integer - chatwootInboxId Int? @db.Integer - chatwootConversationId Int? @db.Integer - chatwootContactInboxSourceId String? @db.VarChar(100) - chatwootIsRead Boolean? @db.Boolean - Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) - instanceId String - typebotSessionId String? - MessageUpdate MessageUpdate[] - TypebotSession TypebotSession? @relation(fields: [typebotSessionId], references: [id]) - Media Media? + id String @id @default(cuid()) + key Json @db.JsonB + pushName String? @db.VarChar(100) + participant String? @db.VarChar(100) + messageType String @db.VarChar(100) + message Json @db.JsonB + contextInfo Json? @db.JsonB + source DeviceMessage + messageTimestamp Int @db.Integer + chatwootMessageId Int? @db.Integer + chatwootInboxId Int? @db.Integer + chatwootConversationId Int? @db.Integer + chatwootContactInboxSourceId String? @db.VarChar(100) + chatwootIsRead Boolean? @db.Boolean + Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) + instanceId String + typebotSessionId String? + MessageUpdate MessageUpdate[] + TypebotSession TypebotSession? @relation(fields: [typebotSessionId], references: [id]) + Media Media? + OpenaiAssistantThread OpenaiAssistantThread? @relation(fields: [openaiAssistantThreadId], references: [id]) + openaiAssistantThreadId String? + OpenaiChatCompletionSession OpenaiChatCompletionSession? @relation(fields: [openaiChatCompletionSessionId], references: [id]) + openaiChatCompletionSessionId String? } model MessageUpdate { @@ -325,3 +334,89 @@ model Media { Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) instanceId String } + +model OpenaiCreds { + id String @id @default(cuid()) + apiKey String @unique @db.VarChar(255) + createdAt DateTime? @default(now()) @db.Timestamp + updatedAt DateTime @updatedAt @db.Timestamp + Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) + instanceId String @unique +} + +model OpenaiAssistant { + id String @id @default(cuid()) + assistantId String @unique @db.VarChar(255) + expire Int? @default(0) @db.Integer + keywordFinish String? @db.VarChar(100) + delayMessage Int? @db.Integer + unknownMessage String? @db.VarChar(100) + listeningFromMe Boolean? @default(false) @db.Boolean + stopBotFromMe Boolean? @default(false) @db.Boolean + keepOpen Boolean? @default(false) @db.Boolean + debounceTime Int? @db.Integer + ignoreJids Json? + triggerType TriggerType? + triggerOperator TriggerOperator? + triggerValue String? + createdAt DateTime? @default(now()) @db.Timestamp + updatedAt DateTime @updatedAt @db.Timestamp + Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) + instanceId String + OpenaiAssistantThread OpenaiAssistantThread[] +} + +model OpenaiAssistantThread { + id String @id @default(cuid()) + threadId String @db.VarChar(255) + remoteJid String @db.VarChar(100) + status TypebotSessionStatus + awaitUser Boolean @default(false) @db.Boolean + createdAt DateTime? @default(now()) @db.Timestamp + updatedAt DateTime @updatedAt @db.Timestamp + OpenaiAssistant OpenaiAssistant @relation(fields: [openaiAssistantId], references: [id], onDelete: Cascade) + openaiAssistantId String + Message Message[] + Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) + instanceId String +} + +model OpenaiChatCompletion { + id String @id @default(cuid()) + model String @db.VarChar(100) + systemMessages Json? @db.JsonB + assistantMessages Json? @db.JsonB + userMessages Json? @db.JsonB + maxTokens Int? @db.Integer + expire Int? @default(0) @db.Integer + keywordFinish String? @db.VarChar(100) + delayMessage Int? @db.Integer + unknownMessage String? @db.VarChar(100) + listeningFromMe Boolean? @default(false) @db.Boolean + stopBotFromMe Boolean? @default(false) @db.Boolean + keepOpen Boolean? @default(false) @db.Boolean + debounceTime Int? @db.Integer + ignoreJids Json? + triggerType TriggerType? + triggerOperator TriggerOperator? + triggerValue String? + createdAt DateTime? @default(now()) @db.Timestamp + updatedAt DateTime @updatedAt @db.Timestamp + Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) + instanceId String + OpenaiChatCompletionSession OpenaiChatCompletionSession[] +} + +model OpenaiChatCompletionSession { + id String @id @default(cuid()) + remoteJid String @db.VarChar(100) + sessionId String @db.VarChar(100) + status TypebotSessionStatus + createdAt DateTime? @default(now()) @db.Timestamp + updatedAt DateTime @updatedAt @db.Timestamp + OpenaiChatCompletion OpenaiChatCompletion @relation(fields: [openaiChatCompletionId], references: [id], onDelete: Cascade) + openaiChatCompletionId String + Message Message[] + Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) + instanceId String +}