chatbox ui

This commit is contained in:
archer
2023-07-11 23:22:01 +08:00
parent eb768d9c04
commit b2e2f60e0d
46 changed files with 1123 additions and 2817 deletions

View File

@@ -1,14 +1,13 @@
import { GET, POST, DELETE, PUT } from './request';
import type { AppSchema } from '@/types/mongoSchema';
import type { AppModuleItemType, AppUpdateParams } from '@/types/app';
import type { AppListItemType, AppUpdateParams } from '@/types/app';
import { RequestPaging } from '../types/index';
import type { AppListResponse } from './response/app';
import type { Props as CreateAppProps } from '@/pages/api/app/create';
/**
* 获取模型列表
*/
export const getMyModels = () => GET<AppListResponse>('/app/list');
export const getMyModels = () => GET<AppListItemType[]>('/app/myApps');
/**
* 创建一个模型
@@ -18,12 +17,12 @@ export const postCreateApp = (data: CreateAppProps) => POST<string>('/app/create
/**
* 根据 ID 删除模型
*/
export const delModelById = (id: string) => DELETE(`/app/del?modelId=${id}`);
export const delModelById = (id: string) => DELETE(`/app/del?appId=${id}`);
/**
* 根据 ID 获取模型
*/
export const getModelById = (id: string) => GET<AppSchema>(`/app/detail?modelId=${id}`);
export const getModelById = (id: string) => GET<AppSchema>(`/app/detail?appId=${id}`);
/**
* 根据 ID 更新模型
@@ -41,5 +40,5 @@ export const getShareModelList = (data: { searchText?: string } & RequestPaging)
/**
* 收藏/取消收藏模型
*/
export const triggerModelCollection = (modelId: string) =>
POST<number>(`/app/share/collection?modelId=${modelId}`);
export const triggerModelCollection = (appId: string) =>
POST<number>(`/app/share/collection?appId=${appId}`);

View File

@@ -1,5 +1,5 @@
import { GET, POST, DELETE, PUT } from './request';
import type { HistoryItemType } from '@/types/chat';
import type { ChatHistoryItemType } from '@/types/chat';
import type { InitChatResponse, InitShareChatResponse } from './response/chat';
import { RequestPaging } from '../types/index';
import type { ShareChatSchema } from '@/types/mongoSchema';
@@ -11,14 +11,14 @@ import type { Props as UpdateHistoryProps } from '@/pages/api/chat/history/updat
/**
* 获取初始化聊天内容
*/
export const getInitChatSiteInfo = (modelId: '' | string, chatId: '' | string) =>
GET<InitChatResponse>(`/chat/init?modelId=${modelId}&chatId=${chatId}`);
export const getInitChatSiteInfo = (data: { appId: string; historyId?: string }) =>
GET<InitChatResponse>(`/chat/init`, data);
/**
* 获取历史记录
*/
export const getChatHistory = (data: RequestPaging) =>
POST<HistoryItemType[]>('/chat/history/getHistory', data);
export const getChatHistory = (data: RequestPaging & { appId?: string }) =>
POST<ChatHistoryItemType[]>('/chat/history/getHistory', data);
/**
* 删除一条历史记录
@@ -44,8 +44,8 @@ export const updateHistoryQuote = (params: {
/**
* 删除一句对话
*/
export const delChatRecordByIndex = (chatId: string, contentId: string) =>
DELETE(`/chat/delChatRecordByContentId?chatId=${chatId}&contentId=${contentId}`);
export const delChatRecordByIndex = (data: { historyId: string; contentId: string }) =>
DELETE(`/chat/delChatRecordByContentId`, data);
/**
* 修改历史记录: 标题/置顶

View File

@@ -9,12 +9,12 @@ interface StreamFetchProps {
abortSignal: AbortController;
}
export const streamFetch = ({
url = '/api/openapi/v1/chat/completions2',
url = '/api/openapi/v1/chat/completions',
data,
onMessage,
abortSignal
}: StreamFetchProps) =>
new Promise<{ responseText: string; errMsg: string; newChatId: string | null }>(
new Promise<{ responseText: string; errMsg: string; newHistoryId: string | null }>(
async (resolve, reject) => {
try {
const response = await window.fetch(url, {
@@ -43,7 +43,7 @@ export const streamFetch = ({
// response data
let responseText = '';
let errMsg = '';
const newChatId = response.headers.get('newChatId');
const newHistoryId = response.headers.get('newHistoryId');
const read = async () => {
try {
@@ -53,7 +53,7 @@ export const streamFetch = ({
return resolve({
responseText,
errMsg,
newChatId
newHistoryId
});
} else {
return reject('响应过程出现异常~');
@@ -85,7 +85,7 @@ export const streamFetch = ({
return resolve({
responseText,
errMsg,
newChatId
newHistoryId
});
}
reject(getErrText(err, '请求异常'));

View File

@@ -92,8 +92,8 @@ function request(url: string, data: any, config: ConfigType, method: Method): an
baseURL: '/api',
url,
method,
data: method === 'GET' ? null : data,
params: method === 'GET' ? data : null, // get请求不携带dataparams放在url上
data: ['POST', 'PUT'].includes(method) ? data : null,
params: !['POST', 'PUT'].includes(method) ? data : null,
...config // 用户自定义配置,可以覆盖前面的配置
})
.then((res) => checkRes(res.data))
@@ -119,6 +119,6 @@ export function PUT<T>(url: string, data = {}, config: ConfigType = {}): Promise
return request(url, data, config, 'PUT');
}
export function DELETE<T>(url: string, config: ConfigType = {}): Promise<T> {
return request(url, {}, config, 'DELETE');
export function DELETE<T>(url: string, data = {}, config: ConfigType = {}): Promise<T> {
return request(url, data, config, 'DELETE');
}

View File

@@ -1,19 +1,20 @@
import type { ChatPopulate, AppSchema } from '@/types/mongoSchema';
import type { AppSchema } from '@/types/mongoSchema';
import type { ChatItemType } from '@/types/chat';
import { VariableItemType } from '@/types/app';
export interface InitChatResponse {
chatId: string;
modelId: string;
systemPrompt?: string;
limitPrompt?: string;
model: {
historyId: string;
appId: string;
app: {
variableModules?: VariableItemType[];
welcomeText?: string;
name: string;
avatar: string;
intro: string;
canUse: boolean;
};
chatModel: AppSchema['chat']['chatModel']; // 对话模型名
title: string;
variables: Record<string, any>;
history: ChatItemType[];
}