mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-07-28 17:53:21 +08:00
chore: update request error message
This commit is contained in:
@ -93,15 +93,19 @@ const ChatView = () => {
|
|||||||
let prompt = "";
|
let prompt = "";
|
||||||
let tokens = 0;
|
let tokens = 0;
|
||||||
if (connectionStore.currentConnectionCtx?.database) {
|
if (connectionStore.currentConnectionCtx?.database) {
|
||||||
const tables = await connectionStore.getOrFetchDatabaseSchema(connectionStore.currentConnectionCtx?.database);
|
|
||||||
const promptGenerator = getPromptGeneratorOfAssistant(getAssistantById(currentChat.assistantId)!);
|
|
||||||
let schema = "";
|
let schema = "";
|
||||||
for (const table of tables) {
|
try {
|
||||||
if (tokens < MAX_TOKENS / 2) {
|
const tables = await connectionStore.getOrFetchDatabaseSchema(connectionStore.currentConnectionCtx?.database);
|
||||||
tokens += countTextTokens(schema + table.structure);
|
for (const table of tables) {
|
||||||
schema += table.structure;
|
if (tokens < MAX_TOKENS / 2) {
|
||||||
|
tokens += countTextTokens(schema + table.structure);
|
||||||
|
schema += table.structure;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
toast.error(error.message);
|
||||||
}
|
}
|
||||||
|
const promptGenerator = getPromptGeneratorOfAssistant(getAssistantById(currentChat.assistantId)!);
|
||||||
prompt = promptGenerator(schema);
|
prompt = promptGenerator(schema);
|
||||||
}
|
}
|
||||||
let formatedMessageList = [];
|
let formatedMessageList = [];
|
||||||
|
@ -2,8 +2,8 @@ import { cloneDeep, head } from "lodash-es";
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { createPortal } from "react-dom";
|
import { createPortal } from "react-dom";
|
||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import { testConnection, useConnectionStore } from "@/store";
|
import { useConnectionStore } from "@/store";
|
||||||
import { Connection, Engine } from "@/types";
|
import { Connection, Engine, ResponseObject } from "@/types";
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
import DataStorageBanner from "./DataStorageBanner";
|
import DataStorageBanner from "./DataStorageBanner";
|
||||||
import ActionConfirmModal from "./ActionConfirmModal";
|
import ActionConfirmModal from "./ActionConfirmModal";
|
||||||
@ -32,6 +32,7 @@ const CreateConnectionModal = (props: Props) => {
|
|||||||
const [isRequesting, setIsRequesting] = useState(false);
|
const [isRequesting, setIsRequesting] = useState(false);
|
||||||
const showDatabaseField = connection.engineType === Engine.PostgreSQL;
|
const showDatabaseField = connection.engineType === Engine.PostgreSQL;
|
||||||
const isEditing = editConnection !== undefined;
|
const isEditing = editConnection !== undefined;
|
||||||
|
const allowSave = connection.host !== "" && connection.username !== "";
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (show) {
|
if (show) {
|
||||||
@ -60,11 +61,25 @@ const CreateConnectionModal = (props: Props) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await testConnection(tempConnection);
|
const response = await fetch("/api/connection/test", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
connection: tempConnection,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const result = (await response.json()) as ResponseObject<boolean>;
|
||||||
|
if (result.message) {
|
||||||
|
toast.error(result.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
toast.error("Failed to test connection");
|
||||||
|
} finally {
|
||||||
setIsRequesting(false);
|
setIsRequesting(false);
|
||||||
toast.error("Failed to test connection, please check your connection configuration");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -187,7 +202,7 @@ const CreateConnectionModal = (props: Props) => {
|
|||||||
<button className="btn btn-outline" onClick={close}>
|
<button className="btn btn-outline" onClick={close}>
|
||||||
Close
|
Close
|
||||||
</button>
|
</button>
|
||||||
<button className="btn" disabled={isRequesting} onClick={handleCreateConnection}>
|
<button className="btn" disabled={isRequesting || !allowSave} onClick={handleCreateConnection}>
|
||||||
{isRequesting && <Icon.BiLoaderAlt className="w-4 h-auto animate-spin mr-1" />}
|
{isRequesting && <Icon.BiLoaderAlt className="w-4 h-auto animate-spin mr-1" />}
|
||||||
Save
|
Save
|
||||||
</button>
|
</button>
|
||||||
|
@ -5,6 +5,7 @@ import DataTable from "react-data-table-component";
|
|||||||
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 { useQueryStore } from "@/store";
|
import { useQueryStore } from "@/store";
|
||||||
|
import { ResponseObject } from "@/types";
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
import EngineIcon from "./EngineIcon";
|
import EngineIcon from "./EngineIcon";
|
||||||
|
|
||||||
@ -63,12 +64,17 @@ const QueryDrawer = () => {
|
|||||||
statement,
|
statement,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
const result = await response.json();
|
const result = (await response.json()) as ResponseObject<RawQueryResult[]>;
|
||||||
setIsLoading(false);
|
if (result.message) {
|
||||||
setRawResults(result);
|
toast.error(result.message);
|
||||||
|
} else {
|
||||||
|
setRawResults(result.data);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
toast.error("Failed to execute statement");
|
toast.error("Failed to execute statement");
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -23,10 +23,13 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
structure,
|
structure,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
res.status(200).json(tableStructures);
|
res.status(200).json({
|
||||||
} catch (error) {
|
data: tableStructures,
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
error: error,
|
message: error.message || "Failed to get database schema.",
|
||||||
|
code: error.code || "UNKNOWN",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -21,9 +21,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
try {
|
try {
|
||||||
const connector = newConnector(connection);
|
const connector = newConnector(connection);
|
||||||
const result = await connector.execute(db, statement);
|
const result = await connector.execute(db, statement);
|
||||||
res.status(200).json(result);
|
res.status(200).json({
|
||||||
} catch (error) {
|
data: result,
|
||||||
res.status(400).json([]);
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
res.status(400).json({
|
||||||
|
message: error.message || "Failed to execute statement.",
|
||||||
|
code: error.code || "UNKNOWN",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -15,8 +15,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
const connector = newConnector(connection);
|
const connector = newConnector(connection);
|
||||||
await connector.testConnection();
|
await connector.testConnection();
|
||||||
res.status(200).json({});
|
res.status(200).json({});
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
res.status(400).json({});
|
res.status(400).json({
|
||||||
|
message: error.message || "Failed to test connection.",
|
||||||
|
code: error.code || "UNKNOWN",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ import axios from "axios";
|
|||||||
import { uniqBy } from "lodash-es";
|
import { uniqBy } from "lodash-es";
|
||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import { persist } from "zustand/middleware";
|
import { persist } from "zustand/middleware";
|
||||||
import { Connection, Database, Engine, Table } from "@/types";
|
import { Connection, Database, Engine, ResponseObject, Table } from "@/types";
|
||||||
import { generateUUID } from "@/utils";
|
import { generateUUID } from "@/utils";
|
||||||
|
|
||||||
interface ConnectionContext {
|
interface ConnectionContext {
|
||||||
@ -92,11 +92,14 @@ export const useConnectionStore = create<ConnectionState>()(
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data } = await axios.post<Table[]>("/api/connection/db_schema", {
|
const { data: result } = await axios.post<ResponseObject<Table[]>>("/api/connection/db_schema", {
|
||||||
connection,
|
connection,
|
||||||
db: database.name,
|
db: database.name,
|
||||||
});
|
});
|
||||||
return data;
|
if (result.message) {
|
||||||
|
throw result.message;
|
||||||
|
}
|
||||||
|
return result.data;
|
||||||
},
|
},
|
||||||
getConnectionById: (connectionId: string) => {
|
getConnectionById: (connectionId: string) => {
|
||||||
return get().connectionList.find((connection) => connection.id === connectionId);
|
return get().connectionList.find((connection) => connection.id === connectionId);
|
||||||
@ -119,10 +122,3 @@ export const useConnectionStore = create<ConnectionState>()(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
export const testConnection = async (connection: Connection) => {
|
|
||||||
const { data: result } = await axios.post<boolean>("/api/connection/test", {
|
|
||||||
connection,
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
5
types/api.ts
Normal file
5
types/api.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export type ResponseObject<T> = {
|
||||||
|
data: T;
|
||||||
|
message?: string;
|
||||||
|
code?: string;
|
||||||
|
};
|
@ -4,3 +4,4 @@ export * from "./connection";
|
|||||||
export * from "./database";
|
export * from "./database";
|
||||||
export * from "./chat";
|
export * from "./chat";
|
||||||
export * from "./message";
|
export * from "./message";
|
||||||
|
export * from "./api";
|
||||||
|
Reference in New Issue
Block a user