Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/chat history2 #1009

Merged
merged 21 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bc7d95c
add migration query for chat history
chungyau97 Sep 14, 2023
7b54f17
modify addChatMessage
chungyau97 Sep 15, 2023
d6cf5fe
add chatId column
chungyau97 Sep 18, 2023
f717807
add retrieve internal chat message
chungyau97 Sep 19, 2023
922ba89
Merge branch 'main' into feature/ChatHistory2
chungyau97 Sep 19, 2023
51f9f0b
modify delete chat message method
chungyau97 Sep 19, 2023
3227321
Merge branch 'main' into feature/ChatHistory2
chungyau97 Sep 21, 2023
e22e797
Merge branch 'main' into feature/ChatHistory2
chungyau97 Oct 2, 2023
b3e66b0
Merge branch 'main' into feature/ChatHistory2
chungyau97 Oct 4, 2023
f55aef3
Merge branch 'main' into feature/ChatHistory2
chungyau97 Oct 7, 2023
e5720f0
Merge branch 'main' into feature/ChatHistory2
HenryHengZJ Oct 16, 2023
ea1f3f6
add fix for backed chat message
HenryHengZJ Oct 17, 2023
eef11ec
Merge branch 'main' into feature/ChatHistory2
HenryHengZJ Oct 18, 2023
4bc827c
update share chatbot config
HenryHengZJ Oct 18, 2023
43a117c
Merge branch 'main' into feature/ChatHistory2
chungyau97 Oct 21, 2023
59c3ea8
Merge branch 'feature/ChatHistory2' of https://github.com/FlowiseAI/F…
chungyau97 Oct 21, 2023
0217e5d
Merge branch 'main' into feature/ChatHistory2
chungyau97 Oct 22, 2023
ac88884
Merge branch 'main' into feature/ChatHistory2
HenryHengZJ Oct 24, 2023
dbde865
Merge branch 'main' into feature/ChatHistory2
HenryHengZJ Oct 25, 2023
6ab20cd
Merge branch 'main' into feature/ChatHistory2
HenryHengZJ Nov 2, 2023
4f6cab4
code cleanup
HenryHengZJ Nov 3, 2023
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
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"socket.io": "^4.6.1",
"sqlite3": "^5.1.6",
"typeorm": "^0.3.6",
"uuid": "^9.0.1",
"winston": "^3.9.0"
},
"devDependencies": {
Expand Down
11 changes: 10 additions & 1 deletion packages/server/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { ICommonObject, INode, INodeData as INodeDataFromComponent, INodeParams

export type MessageType = 'apiMessage' | 'userMessage'

export enum chatType {
INTERNAL = 'INTERNAL',
EXTERNAL = 'EXTERNAL'
}
/**
* Databases
*/
Expand All @@ -24,8 +28,12 @@ export interface IChatMessage {
role: MessageType
content: string
chatflowid: string
createdDate: Date
sourceDocuments?: string
chatType: string
chatId: string
memoryType?: string
sessionId?: string
createdDate: Date
}

export interface ITool {
Expand Down Expand Up @@ -146,6 +154,7 @@ export interface IncomingInput {
history: IMessage[]
overrideConfig?: ICommonObject
socketIOClientId?: string
chatId?: string
}

export interface IActiveChatflows {
Expand Down
12 changes: 12 additions & 0 deletions packages/server/src/database/entities/ChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ export class ChatMessage implements IChatMessage {
@Column({ nullable: true, type: 'text' })
sourceDocuments?: string

@Column()
chatType: string

@Column()
chatId: string

@Column({ nullable: true })
memoryType?: string

@Column({ nullable: true })
sessionId?: string

@CreateDateColumn()
createdDate: Date
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddApiConfig1694099200729 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`chat_flow\` ADD COLUMN \`apiConfig\` TEXT;`)
const columnExists = await queryRunner.hasColumn('chat_flow', 'apiConfig')
if (!columnExists) queryRunner.query(`ALTER TABLE \`chat_flow\` ADD COLUMN \`apiConfig\` TEXT;`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddAnalytic1694432361423 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`chat_flow\` ADD COLUMN \`analytic\` TEXT;`)
const columnExists = await queryRunner.hasColumn('chat_flow', 'analytic')
if (!columnExists) queryRunner.query(`ALTER TABLE \`chat_flow\` ADD COLUMN \`analytic\` TEXT;`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddChatHistory1694658767766 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const chatTypeColumnExists = await queryRunner.hasColumn('chat_message', 'chatType')
if (!chatTypeColumnExists)
await queryRunner.query(`ALTER TABLE \`chat_message\` ADD COLUMN \`chatType\` VARCHAR(255) NOT NULL DEFAULT 'INTERNAL';`)

const chatIdColumnExists = await queryRunner.hasColumn('chat_message', 'chatId')
if (!chatIdColumnExists) await queryRunner.query(`ALTER TABLE \`chat_message\` ADD COLUMN \`chatId\` VARCHAR(255);`)
const results: { id: string; chatflowid: string }[] = await queryRunner.query(`WITH RankedMessages AS (
SELECT
\`chatflowid\`,
\`id\`,
\`createdDate\`,
ROW_NUMBER() OVER (PARTITION BY \`chatflowid\` ORDER BY \`createdDate\`) AS row_num
FROM \`chat_message\`
)
SELECT \`chatflowid\`, \`id\`
FROM RankedMessages
WHERE row_num = 1;`)
for (const chatMessage of results) {
await queryRunner.query(
`UPDATE \`chat_message\` SET \`chatId\` = '${chatMessage.id}' WHERE \`chatflowid\` = '${chatMessage.chatflowid}'`
)
}
await queryRunner.query(`ALTER TABLE \`chat_message\` MODIFY \`chatId\` VARCHAR(255) NOT NULL;`)

const memoryTypeColumnExists = await queryRunner.hasColumn('chat_message', 'memoryType')
if (!memoryTypeColumnExists) await queryRunner.query(`ALTER TABLE \`chat_message\` ADD COLUMN \`memoryType\` VARCHAR(255);`)

const sessionIdColumnExists = await queryRunner.hasColumn('chat_message', 'sessionId')
if (!sessionIdColumnExists) await queryRunner.query(`ALTER TABLE \`chat_message\` ADD COLUMN \`sessionId\` VARCHAR(255);`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`chat_message\` DROP COLUMN \`chatType\`, DROP COLUMN \`chatId\`, DROP COLUMN \`memoryType\`, DROP COLUMN \`sessionId\`;`
)
}
}
4 changes: 3 additions & 1 deletion packages/server/src/database/migrations/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ModifyCredential1693999261583 } from './1693999261583-ModifyCredential'
import { ModifyTool1694001465232 } from './1694001465232-ModifyTool'
import { AddApiConfig1694099200729 } from './1694099200729-AddApiConfig'
import { AddAnalytic1694432361423 } from './1694432361423-AddAnalytic'
import { AddChatHistory1694658767766 } from './1694658767766-AddChatHistory'

export const mysqlMigrations = [
Init1693840429259,
Expand All @@ -13,5 +14,6 @@ export const mysqlMigrations = [
ModifyCredential1693999261583,
ModifyTool1694001465232,
AddApiConfig1694099200729,
AddAnalytic1694432361423
AddAnalytic1694432361423,
AddChatHistory1694658767766
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddApiConfig1694099183389 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "chat_flow" ADD COLUMN "apiConfig" TEXT;`)
await queryRunner.query(`ALTER TABLE "chat_flow" ADD COLUMN IF NOT EXISTS "apiConfig" TEXT;`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddAnalytic1694432361423 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "chat_flow" ADD COLUMN "analytic" TEXT;`)
await queryRunner.query(`ALTER TABLE "chat_flow" ADD COLUMN IF NOT EXISTS "analytic" TEXT;`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddChatHistory1694658756136 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "chat_message" ADD COLUMN IF NOT EXISTS "chatType" VARCHAR NOT NULL DEFAULT 'INTERNAL', ADD COLUMN IF NOT EXISTS "chatId" VARCHAR, ADD COLUMN IF NOT EXISTS "memoryType" VARCHAR, ADD COLUMN IF NOT EXISTS "sessionId" VARCHAR;`
)
const results: { id: string; chatflowid: string }[] = await queryRunner.query(`WITH RankedMessages AS (
SELECT
"chatflowid",
"id",
"createdDate",
ROW_NUMBER() OVER (PARTITION BY "chatflowid" ORDER BY "createdDate") AS row_num
FROM "chat_message"
)
SELECT "chatflowid", "id"
FROM RankedMessages
WHERE row_num = 1;`)
for (const chatMessage of results) {
await queryRunner.query(
`UPDATE "chat_message" SET "chatId" = '${chatMessage.id}' WHERE "chatflowid" = '${chatMessage.chatflowid}'`
)
}
await queryRunner.query(`ALTER TABLE "chat_message" ALTER COLUMN "chatId" SET NOT NULL;`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "chat_message" DROP COLUMN "chatType", DROP COLUMN "chatId", DROP COLUMN "memoryType", DROP COLUMN "sessionId";`
)
}
}
4 changes: 3 additions & 1 deletion packages/server/src/database/migrations/postgres/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ModifyCredential1693997070000 } from './1693997070000-ModifyCredential'
import { ModifyTool1693997339912 } from './1693997339912-ModifyTool'
import { AddApiConfig1694099183389 } from './1694099183389-AddApiConfig'
import { AddAnalytic1694432361423 } from './1694432361423-AddAnalytic'
import { AddChatHistory1694658756136 } from './1694658756136-AddChatHistory'

export const postgresMigrations = [
Init1693891895163,
Expand All @@ -13,5 +14,6 @@ export const postgresMigrations = [
ModifyCredential1693997070000,
ModifyTool1693997339912,
AddApiConfig1694099183389,
AddAnalytic1694432361423
AddAnalytic1694432361423,
AddChatHistory1694658756136
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddChatHistory1694657778173 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "chat_message" ADD COLUMN "chatId" VARCHAR;`)
const results: { id: string; chatflowid: string }[] = await queryRunner.query(`WITH RankedMessages AS (
SELECT
"chatflowid",
"id",
"createdDate",
ROW_NUMBER() OVER (PARTITION BY "chatflowid" ORDER BY "createdDate") AS row_num
FROM "chat_message"
)
SELECT "chatflowid", "id"
FROM RankedMessages
WHERE row_num = 1;`)
for (const chatMessage of results) {
await queryRunner.query(
`UPDATE "chat_message" SET "chatId" = '${chatMessage.id}' WHERE "chatflowid" = '${chatMessage.chatflowid}'`
)
}
await queryRunner.query(
`CREATE TABLE "temp_chat_message" ("id" varchar PRIMARY KEY NOT NULL, "role" varchar NOT NULL, "chatflowid" varchar NOT NULL, "content" text NOT NULL, "sourceDocuments" text, "createdDate" datetime NOT NULL DEFAULT (datetime('now')), "chatType" VARCHAR NOT NULL DEFAULT 'INTERNAL', "chatId" VARCHAR NOT NULL, "memoryType" VARCHAR, "sessionId" VARCHAR);`
)
await queryRunner.query(
`INSERT INTO "temp_chat_message" ("id", "role", "chatflowid", "content", "sourceDocuments", "createdDate", "chatId") SELECT "id", "role", "chatflowid", "content", "sourceDocuments", "createdDate", "chatId" FROM "chat_message";`
)
await queryRunner.query(`DROP TABLE "chat_message";`)
await queryRunner.query(`ALTER TABLE "temp_chat_message" RENAME TO "chat_message";`)
await queryRunner.query(`CREATE INDEX "IDX_e574527322272fd838f4f0f3d3" ON "chat_message" ("chatflowid") ;`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE IF EXISTS "temp_chat_message";`)
await queryRunner.query(`ALTER TABLE "chat_message" DROP COLUMN "chatType";`)
await queryRunner.query(`ALTER TABLE "chat_message" DROP COLUMN "chatId";`)
await queryRunner.query(`ALTER TABLE "chat_message" DROP COLUMN "memoryType";`)
await queryRunner.query(`ALTER TABLE "chat_message" DROP COLUMN "sessionId";`)
}
}
4 changes: 3 additions & 1 deletion packages/server/src/database/migrations/sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ModifyCredential1693923551694 } from './1693923551694-ModifyCredential'
import { ModifyTool1693924207475 } from './1693924207475-ModifyTool'
import { AddApiConfig1694090982460 } from './1694090982460-AddApiConfig'
import { AddAnalytic1694432361423 } from './1694432361423-AddAnalytic'
import { AddChatHistory1694657778173 } from './1694657778173-AddChatHistory'

export const sqliteMigrations = [
Init1693835579790,
Expand All @@ -13,5 +14,6 @@ export const sqliteMigrations = [
ModifyCredential1693923551694,
ModifyTool1693924207475,
AddApiConfig1694090982460,
AddAnalytic1694432361423
AddAnalytic1694432361423,
AddChatHistory1694657778173
]
Loading