初始化
This commit is contained in:
138
easyflow-ui-admin/app/src/api/ai/bot.ts
Normal file
138
easyflow-ui-admin/app/src/api/ai/bot.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import type {
|
||||
AiLlm,
|
||||
BotInfo,
|
||||
ChatMessage,
|
||||
RequestResult,
|
||||
Session,
|
||||
} from '@easyflow/types';
|
||||
|
||||
import { api } from '#/api/request.js';
|
||||
|
||||
/** 获取bot详情 */
|
||||
export const getBotDetails = (id: string) => {
|
||||
return api.get<RequestResult<BotInfo>>('/api/v1/bot/getDetail', {
|
||||
params: { id },
|
||||
});
|
||||
};
|
||||
|
||||
export interface GetSessionListParams {
|
||||
botId: string;
|
||||
tempUserId: string;
|
||||
}
|
||||
/** 获取bot对话列表 */
|
||||
export const getSessionList = (params: GetSessionListParams) => {
|
||||
return api.get<RequestResult<{ cons: Session[] }>>(
|
||||
'/api/v1/conversation/externalList',
|
||||
{ params },
|
||||
);
|
||||
};
|
||||
|
||||
export interface SaveBotParams {
|
||||
icon: string;
|
||||
title: string;
|
||||
alias: string;
|
||||
description: string;
|
||||
categoryId: any;
|
||||
status: number;
|
||||
}
|
||||
/** 创建Bot */
|
||||
export const saveBot = (params: SaveBotParams) => {
|
||||
return api.post<RequestResult>('/api/v1/bot/save', { ...params });
|
||||
};
|
||||
|
||||
export interface UpdateBotParams extends SaveBotParams {
|
||||
id: string;
|
||||
}
|
||||
/** 修改Bot */
|
||||
export const updateBotApi = (params: UpdateBotParams) => {
|
||||
return api.post<RequestResult>('/api/v1/bot/update', { ...params });
|
||||
};
|
||||
|
||||
/** 删除Bot */
|
||||
export const removeBotFromId = (id: string) => {
|
||||
return api.post<RequestResult>('/api/v1/bot/remove', { id });
|
||||
};
|
||||
|
||||
export interface GetMessageListParams {
|
||||
conversationId: string;
|
||||
botId: string;
|
||||
tempUserId: string;
|
||||
}
|
||||
/** 获取单个对话的信息列表 */
|
||||
export const getMessageList = (params: GetMessageListParams) => {
|
||||
return api.get<RequestResult<ChatMessage[]>>(
|
||||
'/api/v1/botMessage/messageList',
|
||||
{
|
||||
params,
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
/** 更新Bot的LLM配置 */
|
||||
export interface UpdateLlmOptionsParams {
|
||||
id: string;
|
||||
llmOptions: {
|
||||
[key: string]: any;
|
||||
};
|
||||
}
|
||||
export interface UpdateBotOptionsParams {
|
||||
id: string;
|
||||
options: {
|
||||
[key: string]: any;
|
||||
};
|
||||
}
|
||||
|
||||
export const updateLlmOptions = (params: UpdateLlmOptionsParams) => {
|
||||
return api.post<RequestResult>('/api/v1/bot/updateLlmOptions', {
|
||||
...params,
|
||||
});
|
||||
};
|
||||
|
||||
export const updateBotOptions = (params: UpdateBotOptionsParams) => {
|
||||
return api.post<RequestResult>('/api/v1/bot/updateOptions', {
|
||||
...params,
|
||||
});
|
||||
};
|
||||
|
||||
/** 更新Bot的LLM配置 */
|
||||
export interface GetAiLlmListParams {
|
||||
[key: string]: any;
|
||||
}
|
||||
export const getAiLlmList = (params: GetAiLlmListParams) => {
|
||||
return api.get<RequestResult<AiLlm[]>>('/api/v1/model/list', {
|
||||
params,
|
||||
});
|
||||
};
|
||||
|
||||
/** 更新modelId */
|
||||
export interface UpdateLlmIdParams {
|
||||
id: string;
|
||||
modelId: string;
|
||||
}
|
||||
export const updateLlmId = (params: UpdateLlmIdParams) => {
|
||||
return api.post<RequestResult>('/api/v1/bot/updateLlmId', {
|
||||
...params,
|
||||
});
|
||||
};
|
||||
|
||||
export const doPostBotPluginTools = (botId: string) => {
|
||||
return api.post<RequestResult<any[]>>('/api/v1/pluginItem/tool/list', {
|
||||
id: botId,
|
||||
});
|
||||
};
|
||||
|
||||
export const getPerQuestions = (presetQuestions: any[]) => {
|
||||
if (!presetQuestions) {
|
||||
return [];
|
||||
}
|
||||
return presetQuestions
|
||||
.filter((item: any) => {
|
||||
return (
|
||||
typeof item.description === 'string' && item.description.trim() !== ''
|
||||
);
|
||||
})
|
||||
.map((item: any) => ({
|
||||
key: item.key,
|
||||
description: item.description,
|
||||
}));
|
||||
};
|
||||
2
easyflow-ui-admin/app/src/api/ai/index.ts
Normal file
2
easyflow-ui-admin/app/src/api/ai/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './bot';
|
||||
export * from './llm';
|
||||
0
easyflow-ui-admin/app/src/api/ai/knowledge.ts
Normal file
0
easyflow-ui-admin/app/src/api/ai/knowledge.ts
Normal file
44
easyflow-ui-admin/app/src/api/ai/llm.ts
Normal file
44
easyflow-ui-admin/app/src/api/ai/llm.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { api } from '#/api/request.js';
|
||||
|
||||
// 获取LLM供应商
|
||||
export async function getLlmProviderList() {
|
||||
return api.get('/api/v1/modelProvider/list');
|
||||
}
|
||||
|
||||
// 保存LLM
|
||||
export async function saveLlm(data: string) {
|
||||
return api.post('/api/v1/model/save', data);
|
||||
}
|
||||
|
||||
// 删除LLM
|
||||
export async function deleteLlm(data: any) {
|
||||
return api.post(`/api/v1/model/remove`, data);
|
||||
}
|
||||
|
||||
// 修改LLM
|
||||
export async function updateLlm(data: any) {
|
||||
return api.post(`/api/v1/model/update`, data);
|
||||
}
|
||||
|
||||
// 一键添加LLM
|
||||
export async function quickAddLlm(data: any) {
|
||||
return api.post(`/api/v1/model/quickAdd`, data);
|
||||
}
|
||||
|
||||
export interface llmType {
|
||||
id: string;
|
||||
title: string;
|
||||
modelProvider: {
|
||||
icon: string;
|
||||
providerName: string;
|
||||
providerType: string;
|
||||
};
|
||||
withUsed: boolean;
|
||||
llmModel: string;
|
||||
icon: string;
|
||||
description: string;
|
||||
modelType: string;
|
||||
groupName: string;
|
||||
added: boolean;
|
||||
aiLlmProvider: any;
|
||||
}
|
||||
27
easyflow-ui-admin/app/src/api/common/file.ts
Normal file
27
easyflow-ui-admin/app/src/api/common/file.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* 格式化文件大小(字节转 B/KB/MB/GB/TB)
|
||||
* @param bytes - 文件大小(单位:字节 Byte)
|
||||
* @param decimalPlaces - 保留小数位数(默认 2 位)
|
||||
* @returns 格式化后的大小字符串(如:1.23 MB、456 B、7.8 GB)
|
||||
*/
|
||||
export function formatFileSize(
|
||||
bytes: number,
|
||||
decimalPlaces: number = 2,
|
||||
): string {
|
||||
// 处理特殊情况:bytes 为 0 或非数字
|
||||
if (Number.isNaN(bytes) || bytes < 0) return '0 B';
|
||||
if (bytes === 0) return '0 B';
|
||||
|
||||
// 单位数组(从 Byte 到 TB)
|
||||
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
// 计算合适的单位索引(1 KB = 1024 B,每次除以 1024 切换单位)
|
||||
const unitIndex = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||
// 计算对应单位的大小(保留指定小数位)
|
||||
const formattedSize = (bytes / 1024 ** unitIndex).toFixed(decimalPlaces);
|
||||
|
||||
// 移除末尾多余的 .00(如 2.00 MB → 2 MB,1.50 KB → 1.5 KB)
|
||||
const sizeWithoutTrailingZeros = Number.parseFloat(formattedSize).toString();
|
||||
|
||||
// 返回格式化结果(单位与大小拼接)
|
||||
return `${sizeWithoutTrailingZeros} ${units[unitIndex]}`;
|
||||
}
|
||||
9
easyflow-ui-admin/app/src/api/common/hasPermission.ts
Normal file
9
easyflow-ui-admin/app/src/api/common/hasPermission.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { useAccessStore } from '@easyflow/stores';
|
||||
|
||||
export function hasPermission(codes: string[]) {
|
||||
const accessStore = useAccessStore();
|
||||
const userCodesSet = new Set(accessStore.accessCodes);
|
||||
|
||||
const intersection = codes.filter((item) => userCodesSet.has(item));
|
||||
return intersection.length > 0;
|
||||
}
|
||||
52
easyflow-ui-admin/app/src/api/core/auth.ts
Normal file
52
easyflow-ui-admin/app/src/api/core/auth.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { baseRequestClient, requestClient } from '#/api/request';
|
||||
|
||||
export namespace AuthApi {
|
||||
/** 登录接口参数 */
|
||||
export interface LoginParams {
|
||||
password?: string;
|
||||
username?: string;
|
||||
}
|
||||
|
||||
/** 登录接口返回值 */
|
||||
export interface LoginResult {
|
||||
accessToken: string;
|
||||
token: string;
|
||||
}
|
||||
|
||||
export interface RefreshTokenResult {
|
||||
data: string;
|
||||
status: number;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
export async function loginApi(data: AuthApi.LoginParams) {
|
||||
return requestClient.post<AuthApi.LoginResult>('/api/v1/auth/login', data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新accessToken
|
||||
*/
|
||||
export async function refreshTokenApi() {
|
||||
return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
|
||||
withCredentials: true,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
export async function logoutApi() {
|
||||
return requestClient.post('/api/v1/auth/logout', {
|
||||
withCredentials: true,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户权限码
|
||||
*/
|
||||
export async function getAccessCodesApi() {
|
||||
return requestClient.get<string[]>('/api/v1/auth/getPermissions');
|
||||
}
|
||||
3
easyflow-ui-admin/app/src/api/core/index.ts
Normal file
3
easyflow-ui-admin/app/src/api/core/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './auth';
|
||||
export * from './menu';
|
||||
export * from './user';
|
||||
12
easyflow-ui-admin/app/src/api/core/menu.ts
Normal file
12
easyflow-ui-admin/app/src/api/core/menu.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { RouteRecordStringComponent } from '@easyflow/types';
|
||||
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
/**
|
||||
* 获取用户所有菜单
|
||||
*/
|
||||
export async function getAllMenusApi() {
|
||||
return requestClient.get<RouteRecordStringComponent[]>(
|
||||
'/api/v1/sysMenu/treeV2',
|
||||
);
|
||||
}
|
||||
10
easyflow-ui-admin/app/src/api/core/user.ts
Normal file
10
easyflow-ui-admin/app/src/api/core/user.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { UserInfo } from '@easyflow/types';
|
||||
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
export async function getUserInfoApi() {
|
||||
return requestClient.get<UserInfo>('/api/v1/sysAccount/myProfile');
|
||||
}
|
||||
2
easyflow-ui-admin/app/src/api/index.ts
Normal file
2
easyflow-ui-admin/app/src/api/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './ai';
|
||||
export * from './core';
|
||||
221
easyflow-ui-admin/app/src/api/request.ts
Normal file
221
easyflow-ui-admin/app/src/api/request.ts
Normal file
@@ -0,0 +1,221 @@
|
||||
import type { ServerSentEventMessage } from 'fetch-event-stream';
|
||||
|
||||
/**
|
||||
* 该文件可自行根据业务逻辑进行调整
|
||||
*/
|
||||
import type { RequestClientOptions } from '@easyflow/request';
|
||||
|
||||
import { useAppConfig } from '@easyflow/hooks';
|
||||
import { preferences } from '@easyflow/preferences';
|
||||
import {
|
||||
authenticateResponseInterceptor,
|
||||
defaultResponseInterceptor,
|
||||
errorMessageResponseInterceptor,
|
||||
RequestClient,
|
||||
} from '@easyflow/request';
|
||||
import { useAccessStore } from '@easyflow/stores';
|
||||
|
||||
import { ElMessage } from 'element-plus';
|
||||
import { events } from 'fetch-event-stream';
|
||||
|
||||
import { useAuthStore } from '#/store';
|
||||
|
||||
import { refreshTokenApi } from './core';
|
||||
|
||||
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
|
||||
|
||||
function createRequestClient(baseURL: string, options?: RequestClientOptions) {
|
||||
const client = new RequestClient({
|
||||
...options,
|
||||
baseURL,
|
||||
});
|
||||
|
||||
/**
|
||||
* 重新认证逻辑
|
||||
*/
|
||||
async function doReAuthenticate() {
|
||||
console.warn('Access token or refresh token is invalid or expired. ');
|
||||
const accessStore = useAccessStore();
|
||||
const authStore = useAuthStore();
|
||||
accessStore.setAccessToken(null);
|
||||
if (
|
||||
preferences.app.loginExpiredMode === 'modal' &&
|
||||
accessStore.isAccessChecked
|
||||
) {
|
||||
accessStore.setLoginExpired(true);
|
||||
} else {
|
||||
await authStore.logout();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新token逻辑
|
||||
*/
|
||||
async function doRefreshToken() {
|
||||
const accessStore = useAccessStore();
|
||||
const resp = await refreshTokenApi();
|
||||
const newToken = resp.data;
|
||||
accessStore.setAccessToken(newToken);
|
||||
return newToken;
|
||||
}
|
||||
|
||||
function formatToken(token: null | string) {
|
||||
return token ? `${token}` : null;
|
||||
}
|
||||
|
||||
// 请求头处理
|
||||
client.addRequestInterceptor({
|
||||
fulfilled: async (config) => {
|
||||
const accessStore = useAccessStore();
|
||||
|
||||
config.headers['easyflow-token'] = formatToken(accessStore.accessToken);
|
||||
config.headers['Accept-Language'] = preferences.app.locale;
|
||||
return config;
|
||||
},
|
||||
});
|
||||
|
||||
// 处理返回的响应数据格式
|
||||
client.addResponseInterceptor(
|
||||
defaultResponseInterceptor({
|
||||
codeField: 'errorCode',
|
||||
dataField: 'data',
|
||||
showErrorMessage: (message) => {
|
||||
ElMessage.error(message);
|
||||
},
|
||||
successCode: 0,
|
||||
}),
|
||||
);
|
||||
|
||||
// token过期的处理
|
||||
client.addResponseInterceptor(
|
||||
authenticateResponseInterceptor({
|
||||
client,
|
||||
doReAuthenticate,
|
||||
doRefreshToken,
|
||||
enableRefreshToken: preferences.app.enableRefreshToken,
|
||||
formatToken,
|
||||
}),
|
||||
);
|
||||
|
||||
// 通用的错误处理,如果没有进入上面的错误处理逻辑,就会进入这里
|
||||
client.addResponseInterceptor(
|
||||
errorMessageResponseInterceptor((msg: string, error) => {
|
||||
// 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg
|
||||
// 当前mock接口返回的错误字段是 error 或者 message
|
||||
const responseData = error?.response?.data ?? {};
|
||||
const errorMessage = responseData?.error ?? responseData?.message ?? '';
|
||||
// 如果没有错误信息,则会根据状态码进行提示
|
||||
ElMessage.error(errorMessage || msg);
|
||||
}),
|
||||
);
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
export const requestClient = createRequestClient(apiURL, {
|
||||
responseReturn: 'data',
|
||||
});
|
||||
|
||||
export const api = createRequestClient(apiURL, {
|
||||
responseReturn: 'body',
|
||||
});
|
||||
|
||||
export const baseRequestClient = new RequestClient({ baseURL: apiURL });
|
||||
|
||||
export interface SseOptions {
|
||||
onMessage?: (message: ServerSentEventMessage) => void;
|
||||
onError?: (err: any) => void;
|
||||
onFinished?: () => void;
|
||||
}
|
||||
export class SseClient {
|
||||
private controller: AbortController | null = null;
|
||||
private currentRequestId = 0;
|
||||
|
||||
abort(): void {
|
||||
if (this.controller) {
|
||||
this.controller.abort();
|
||||
this.controller = null;
|
||||
}
|
||||
}
|
||||
|
||||
isActive(): boolean {
|
||||
return this.controller !== null;
|
||||
}
|
||||
|
||||
async post(url: string, data?: any, options?: SseOptions): Promise<void> {
|
||||
// 生成唯一的请求ID
|
||||
const requestId = ++this.currentRequestId;
|
||||
const currentRequestId = requestId;
|
||||
|
||||
// 如果已有请求,先取消
|
||||
this.abort();
|
||||
|
||||
// 创建新的控制器
|
||||
const controller = new AbortController();
|
||||
this.controller = controller;
|
||||
|
||||
// 保存信号的引用到局部变量
|
||||
const signal = controller.signal;
|
||||
|
||||
try {
|
||||
const res = await fetch(apiURL + url, {
|
||||
method: 'POST',
|
||||
signal, // 使用局部变量 signal
|
||||
headers: this.getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const error = new Error(`HTTP ${res.status}: ${res.statusText}`);
|
||||
options?.onError?.(error);
|
||||
return;
|
||||
}
|
||||
|
||||
// 在开始事件流之前检查是否还是同一个请求
|
||||
if (this.currentRequestId !== currentRequestId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const msgEvents = events(res, signal);
|
||||
|
||||
try {
|
||||
for await (const event of msgEvents) {
|
||||
// 每次迭代都检查是否还是同一个请求
|
||||
if (this.currentRequestId !== currentRequestId) {
|
||||
break;
|
||||
}
|
||||
options?.onMessage?.(event);
|
||||
}
|
||||
} catch (innerError) {
|
||||
options?.onError?.(innerError);
|
||||
}
|
||||
|
||||
// 只有在还是同一个请求的情况下才调用 onFinished
|
||||
if (this.currentRequestId === currentRequestId) {
|
||||
options?.onFinished?.();
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.currentRequestId !== currentRequestId) {
|
||||
return;
|
||||
}
|
||||
console.error('SSE错误:', error);
|
||||
options?.onError?.(error);
|
||||
} finally {
|
||||
// 只有当还是当前请求时才清除 controller
|
||||
if (this.currentRequestId === currentRequestId) {
|
||||
this.controller = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private getHeaders() {
|
||||
const accessStore = useAccessStore();
|
||||
return {
|
||||
Accept: 'text/event-stream',
|
||||
'Content-Type': 'application/json',
|
||||
'easyflow-token': accessStore.accessToken || '',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const sseClient = new SseClient();
|
||||
Reference in New Issue
Block a user