feat: implement connection and database types

This commit is contained in:
steven
2023-03-22 16:32:12 +08:00
parent 90a6fec65c
commit 5dc3c05a2a
11 changed files with 58 additions and 17 deletions

View File

@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react";
import { toast } from "react-hot-toast"; import { toast } from "react-hot-toast";
import TextareaAutosize from "react-textarea-autosize"; import TextareaAutosize from "react-textarea-autosize";
import { localUser, useChatStore, useMessageStore } from "../../store"; import { localUser, useChatStore, useMessageStore } from "../../store";
import { CreatorRole } from "../../types";
import { generateUUID } from "../../utils"; import { generateUUID } from "../../utils";
import Icon from "../Icon"; import Icon from "../Icon";
@ -45,6 +46,7 @@ const MessageTextarea = (props: Props) => {
id: generateUUID(), id: generateUUID(),
chatId: chatStore.currentChat.id, chatId: chatStore.currentChat.id,
creatorId: localUser.id, creatorId: localUser.id,
creatorRole: CreatorRole.User,
createdAt: Date.now(), createdAt: Date.now(),
content: value, content: value,
}); });

View File

@ -1,7 +1,7 @@
import axios from "axios"; import axios from "axios";
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { defaultChat, getAssistantById, getPromptOfAssistant, localUser, useChatStore, useMessageStore } from "../../store"; 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 { generateUUID } from "../../utils";
import Icon from "../Icon"; import Icon from "../Icon";
import Header from "./Header"; import Header from "./Header";
@ -50,11 +50,11 @@ const ChatView = () => {
const { data } = await axios.post<string>("/api/chat", { const { data } = await axios.post<string>("/api/chat", {
messages: [ messages: [
{ {
role: "system", role: CreatorRole.System,
content: prompt, content: prompt,
}, },
...messageList.map((message) => ({ ...messageList.map((message) => ({
role: message.creatorId === localUser.id ? UserRole.User : UserRole.Assistant, role: message.creatorId === localUser.id ? CreatorRole.User : CreatorRole.Assistant,
content: message.content, content: message.content,
})), })),
], ],
@ -63,6 +63,7 @@ const ChatView = () => {
id: generateUUID(), id: generateUUID(),
chatId: currentChat.id, chatId: currentChat.id,
creatorId: currentChat.assistantId, creatorId: currentChat.assistantId,
creatorRole: CreatorRole.Assistant,
createdAt: Date.now(), createdAt: Date.now(),
content: data, content: data,
}); });

View File

@ -1,5 +1,5 @@
import { first } from "lodash-es"; import { first } from "lodash-es";
import { Id, User, UserRole } from "../types"; import { Id, User } from "../types";
// Assistant is a special user. // Assistant is a special user.
export const assistantList: User[] = [ export const assistantList: User[] = [
@ -8,7 +8,6 @@ export const assistantList: User[] = [
name: "SQL Chat", name: "SQL Chat",
description: "🤖️ I'm an expert in SQL. I can answer your questions about databases and SQL.", description: "🤖️ I'm an expert in SQL. I can answer your questions about databases and SQL.",
avatar: "", avatar: "",
role: UserRole.Assistant,
}, },
]; ];

View File

@ -1,10 +1,12 @@
import { create } from "zustand"; import { create } from "zustand";
import { persist } from "zustand/middleware"; import { persist } from "zustand/middleware";
import { Chat, User } from "../types"; import { Chat, UNKNOWN_ID, User } from "../types";
import { generateUUID } from "../utils"; import { generateUUID } from "../utils";
export const defaultChat: Chat = { export const defaultChat: Chat = {
id: generateUUID(), id: generateUUID(),
connectionId: UNKNOWN_ID,
databaseName: "",
assistantId: "sql-assistant", assistantId: "sql-assistant",
}; };
@ -20,10 +22,12 @@ export const useChatStore = create<ChatState>()(
(set) => ({ (set) => ({
chatList: [defaultChat], chatList: [defaultChat],
currentChat: defaultChat, currentChat: defaultChat,
createChat: (user: User) => { createChat: (assistant: User) => {
const chat: Chat = { const chat: Chat = {
id: generateUUID(), id: generateUUID(),
assistantId: user.id, connectionId: UNKNOWN_ID,
databaseName: "",
assistantId: assistant.id,
}; };
set((state) => ({ set((state) => ({
chatList: [...state.chatList, chat], chatList: [...state.chatList, chat],

View File

@ -1,9 +1,8 @@
import { User, UserRole } from "../types"; import { User } from "../types";
export const localUser: User = { export const localUser: User = {
id: "local-user", id: "local-user",
name: "Local user", name: "Local user",
description: "", description: "",
avatar: "", avatar: "",
role: UserRole.User,
}; };

View File

@ -2,5 +2,7 @@ import { Id } from "./common";
export interface Chat { export interface Chat {
id: string; id: string;
connectionId: Id;
databaseName: string;
assistantId: Id; assistantId: Id;
} }

View File

@ -1,2 +1,4 @@
export type Id = string; export type Id = string;
export type Timestamp = number; export type Timestamp = number;
export const UNKNOWN_ID = "unknown";

18
types/connection.ts Normal file
View File

@ -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;
}

14
types/database.ts Normal file
View File

@ -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;
}

View File

@ -1,9 +1,16 @@
import { Id, Timestamp } from "./"; import { Id, Timestamp } from "./";
export enum CreatorRole {
System = "system",
User = "user",
Assistant = "assistant",
}
export interface Message { export interface Message {
id: Id; id: Id;
chatId: string; chatId: string;
creatorId: Id; creatorId: Id;
creatorRole: CreatorRole;
createdAt: Timestamp; createdAt: Timestamp;
content: string; content: string;
} }

View File

@ -1,13 +1,6 @@
export enum UserRole {
System = "system",
User = "user",
Assistant = "assistant",
}
export interface User { export interface User {
id: string; id: string;
name: string; name: string;
description: string; description: string;
avatar: string; avatar: string;
role: UserRole;
} }