diff --git a/components/ChatView/MessageTextarea.tsx b/components/ChatView/MessageTextarea.tsx index 0e22b86..2e082cc 100644 --- a/components/ChatView/MessageTextarea.tsx +++ b/components/ChatView/MessageTextarea.tsx @@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react"; import { toast } from "react-hot-toast"; import TextareaAutosize from "react-textarea-autosize"; import { localUser, useChatStore, useMessageStore } from "../../store"; +import { CreatorRole } from "../../types"; import { generateUUID } from "../../utils"; import Icon from "../Icon"; @@ -45,6 +46,7 @@ const MessageTextarea = (props: Props) => { id: generateUUID(), chatId: chatStore.currentChat.id, creatorId: localUser.id, + creatorRole: CreatorRole.User, createdAt: Date.now(), content: value, }); diff --git a/components/ChatView/index.tsx b/components/ChatView/index.tsx index 72aafd3..cd9d0d7 100644 --- a/components/ChatView/index.tsx +++ b/components/ChatView/index.tsx @@ -1,7 +1,7 @@ import axios from "axios"; import { useEffect, useRef, useState } from "react"; import { defaultChat, getAssistantById, getPromptOfAssistant, localUser, useChatStore, useMessageStore } from "../../store"; -import { Chat, Message, UserRole } from "../../types"; +import { Chat, CreatorRole, Message } from "../../types"; import { generateUUID } from "../../utils"; import Icon from "../Icon"; import Header from "./Header"; @@ -50,11 +50,11 @@ const ChatView = () => { const { data } = await axios.post("/api/chat", { messages: [ { - role: "system", + role: CreatorRole.System, content: prompt, }, ...messageList.map((message) => ({ - role: message.creatorId === localUser.id ? UserRole.User : UserRole.Assistant, + role: message.creatorId === localUser.id ? CreatorRole.User : CreatorRole.Assistant, content: message.content, })), ], @@ -63,6 +63,7 @@ const ChatView = () => { id: generateUUID(), chatId: currentChat.id, creatorId: currentChat.assistantId, + creatorRole: CreatorRole.Assistant, createdAt: Date.now(), content: data, }); diff --git a/store/assistant.ts b/store/assistant.ts index 9191ece..8fc5827 100644 --- a/store/assistant.ts +++ b/store/assistant.ts @@ -1,5 +1,5 @@ import { first } from "lodash-es"; -import { Id, User, UserRole } from "../types"; +import { Id, User } from "../types"; // Assistant is a special user. export const assistantList: User[] = [ @@ -8,7 +8,6 @@ export const assistantList: User[] = [ name: "SQL Chat", description: "🤖️ I'm an expert in SQL. I can answer your questions about databases and SQL.", avatar: "", - role: UserRole.Assistant, }, ]; diff --git a/store/chat.ts b/store/chat.ts index 0875bd3..c43beff 100644 --- a/store/chat.ts +++ b/store/chat.ts @@ -1,10 +1,12 @@ import { create } from "zustand"; import { persist } from "zustand/middleware"; -import { Chat, User } from "../types"; +import { Chat, UNKNOWN_ID, User } from "../types"; import { generateUUID } from "../utils"; export const defaultChat: Chat = { id: generateUUID(), + connectionId: UNKNOWN_ID, + databaseName: "", assistantId: "sql-assistant", }; @@ -20,10 +22,12 @@ export const useChatStore = create()( (set) => ({ chatList: [defaultChat], currentChat: defaultChat, - createChat: (user: User) => { + createChat: (assistant: User) => { const chat: Chat = { id: generateUUID(), - assistantId: user.id, + connectionId: UNKNOWN_ID, + databaseName: "", + assistantId: assistant.id, }; set((state) => ({ chatList: [...state.chatList, chat], diff --git a/store/user.ts b/store/user.ts index d8aa722..fb08010 100644 --- a/store/user.ts +++ b/store/user.ts @@ -1,9 +1,8 @@ -import { User, UserRole } from "../types"; +import { User } from "../types"; export const localUser: User = { id: "local-user", name: "Local user", description: "", avatar: "", - role: UserRole.User, }; diff --git a/types/chat.ts b/types/chat.ts index 05d8581..1822194 100644 --- a/types/chat.ts +++ b/types/chat.ts @@ -2,5 +2,7 @@ import { Id } from "./common"; export interface Chat { id: string; + connectionId: Id; + databaseName: string; assistantId: Id; } diff --git a/types/common.ts b/types/common.ts index e5bada3..2049883 100644 --- a/types/common.ts +++ b/types/common.ts @@ -1,2 +1,4 @@ export type Id = string; export type Timestamp = number; + +export const UNKNOWN_ID = "unknown"; diff --git a/types/connection.ts b/types/connection.ts new file mode 100644 index 0000000..e5c4480 --- /dev/null +++ b/types/connection.ts @@ -0,0 +1,18 @@ +import { Id } from "./common"; + +enum Engine { + MySQL = "MYSQL", + PostgreSQL = "POSTGRESQL", +} + +export interface Connection { + id: Id; + title: string; + engineType: Engine; + host: string; + port: string; + username: string; + password: string; + // database is only required for PostgreSQL. + database?: string; +} diff --git a/types/database.ts b/types/database.ts new file mode 100644 index 0000000..277b2a6 --- /dev/null +++ b/types/database.ts @@ -0,0 +1,14 @@ +import { Id } from "./common"; + +export interface Database { + connectionId: Id; + name: string; + tableList: Table[]; +} + +interface Table { + name: string; + // structure is a string of the table structure. + // It's mainly used for providing a chat context for the assistant. + structure: string; +} diff --git a/types/message.ts b/types/message.ts index 1cfe635..3860f59 100644 --- a/types/message.ts +++ b/types/message.ts @@ -1,9 +1,16 @@ import { Id, Timestamp } from "./"; +export enum CreatorRole { + System = "system", + User = "user", + Assistant = "assistant", +} + export interface Message { id: Id; chatId: string; creatorId: Id; + creatorRole: CreatorRole; createdAt: Timestamp; content: string; } diff --git a/types/user.ts b/types/user.ts index 85b3a35..224dd1e 100644 --- a/types/user.ts +++ b/types/user.ts @@ -1,13 +1,6 @@ -export enum UserRole { - System = "system", - User = "user", - Assistant = "assistant", -} - export interface User { id: string; name: string; description: string; avatar: string; - role: UserRole; }