feat:rename subtree from frontend-vue to frontend
This commit is contained in:
103
frontend/src/utils/collaboration_Brief_FrontEnd.ts
Normal file
103
frontend/src/utils/collaboration_Brief_FrontEnd.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import type { IRawStepTask, IRichText } from '@/stores'
|
||||
|
||||
function nameJoin(names: string[]): string {
|
||||
// join names with comma, and 'and' for the last one
|
||||
const tmp = [...names]
|
||||
const last = tmp.pop()!
|
||||
let t = tmp.join(', ')
|
||||
if (t.length > 0) {
|
||||
t = `${t} 和 ${last}`
|
||||
} else {
|
||||
t = last
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
export function changeBriefs(task?: IRawStepTask[]): IRawStepTask[] {
|
||||
if (!task) {
|
||||
return []
|
||||
}
|
||||
return task.map((item) => {
|
||||
const record = {
|
||||
...item,
|
||||
Collaboration_Brief_FrontEnd: changeBrief(item),
|
||||
}
|
||||
return record
|
||||
})
|
||||
}
|
||||
|
||||
function changeBrief(task: IRawStepTask): IRichText {
|
||||
// 如果不存在AgentSelection直接返回
|
||||
const agents = task.AgentSelection ?? []
|
||||
if (agents.length === 0) {
|
||||
return task.Collaboration_Brief_FrontEnd
|
||||
}
|
||||
const data: IRichText['data'] = {};
|
||||
let indexOffset = 0;
|
||||
|
||||
// 根据InputObject_List修改
|
||||
const inputs = task.InputObject_List ?? []
|
||||
const inputPlaceHolders = inputs.map((text, index) => {
|
||||
data[(index + indexOffset).toString()] = {
|
||||
text,
|
||||
style: { background: '#ACDBA0' },
|
||||
};
|
||||
return `!<${index + indexOffset}>!`;
|
||||
});
|
||||
const inputSentence = nameJoin(inputPlaceHolders);
|
||||
indexOffset += inputs.length;
|
||||
|
||||
// 根据AgentSelection修改
|
||||
const namePlaceholders = agents.map((text, index) => {
|
||||
data[(index + indexOffset).toString()] = {
|
||||
text,
|
||||
style: { background: '#E5E5E5', boxShadow: '1px 1px 4px 1px #0003' },
|
||||
};
|
||||
return `!<${index + indexOffset}>!`;
|
||||
});
|
||||
const nameSentence = nameJoin(namePlaceholders);
|
||||
indexOffset += agents.length;
|
||||
|
||||
|
||||
let actionSentence = task.TaskContent ?? '';
|
||||
|
||||
// delete the last '.' of actionSentence
|
||||
if (actionSentence[actionSentence.length - 1] === '.') {
|
||||
actionSentence = actionSentence.slice(0, -1);
|
||||
}
|
||||
const actionIndex = indexOffset++;
|
||||
|
||||
data[actionIndex.toString()] = {
|
||||
text: actionSentence,
|
||||
style: { background: '#DDD', border: '1.5px solid #ddd' },
|
||||
};
|
||||
|
||||
let outputSentence = '';
|
||||
const output = task.OutputObject ?? '';
|
||||
if (output) {
|
||||
data[indexOffset.toString()] = {
|
||||
text: output,
|
||||
style: { background: '#FFCA8C' },
|
||||
};
|
||||
outputSentence = `得到 !<${indexOffset}>!`;
|
||||
}
|
||||
|
||||
// Join them togeter
|
||||
let content = inputSentence;
|
||||
if (content) {
|
||||
content = `基于${content}, ${nameSentence} 执行任务 !<${actionIndex}>!`;
|
||||
} else {
|
||||
content = `${nameSentence} 执行任务 !<${actionIndex}>!`;
|
||||
}
|
||||
if (outputSentence) {
|
||||
content = `${content}, ${outputSentence}.`;
|
||||
} else {
|
||||
content = `${content}.`;
|
||||
}
|
||||
content = content.trim();
|
||||
|
||||
return {
|
||||
template: content,
|
||||
data,
|
||||
};
|
||||
}
|
||||
1
frontend/src/utils/index.ts
Normal file
1
frontend/src/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
4
frontend/src/utils/readJson.ts
Normal file
4
frontend/src/utils/readJson.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export async function readConfig<T>(fileName = 'config.json'): Promise<T> {
|
||||
const url = `${location.protocol}//${location.host}${location.pathname}${fileName}`
|
||||
return await fetch(url).then<T>((res) => res.json())
|
||||
}
|
||||
135
frontend/src/utils/request.ts
Normal file
135
frontend/src/utils/request.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
||||
import axios from 'axios'
|
||||
import qs from 'qs'
|
||||
import type { Ref } from 'vue'
|
||||
import { ElNotification } from 'element-plus'
|
||||
import { ref } from 'vue'
|
||||
|
||||
// 创建 axios 实例
|
||||
let service: AxiosInstance
|
||||
|
||||
export interface AxiosResponseData {
|
||||
code: number
|
||||
content: string
|
||||
}
|
||||
|
||||
export function initService() {
|
||||
service = axios.create({
|
||||
baseURL: '/api',
|
||||
timeout: 50000,
|
||||
headers: { 'Content-Type': 'application/json;charset=utf-8' },
|
||||
paramsSerializer: (params) => {
|
||||
return qs.stringify(params)
|
||||
},
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(
|
||||
(config: InternalAxiosRequestConfig) => {
|
||||
return config
|
||||
},
|
||||
(error: Error) => {
|
||||
return Promise.reject(error)
|
||||
},
|
||||
)
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
(response: AxiosResponse<AxiosResponseData, unknown>) => {
|
||||
// 判断响应状态码是否为2xx
|
||||
if (response.status < 200 || response.status >= 300) {
|
||||
throw new Error(response.data?.content)
|
||||
}
|
||||
|
||||
// 检查配置的响应类型是否为二进制类型('blob' 或 'arraybuffer'), 如果是,直接返回响应对象
|
||||
if (
|
||||
response.config.responseType === 'blob' ||
|
||||
response.config.responseType === 'arraybuffer'
|
||||
) {
|
||||
return response as unknown as AxiosResponse
|
||||
}
|
||||
|
||||
return response.data as unknown as AxiosResponse
|
||||
},
|
||||
(error: AxiosError<AxiosResponseData | string>) => {
|
||||
let message: string = ''
|
||||
if (error.response && error.response.status === 500) {
|
||||
message = '系统错误'
|
||||
}
|
||||
|
||||
// if (error.config?.url === '/irs/data-do' && error.config?.method === 'get') {
|
||||
// return Promise.reject(new Error(message))
|
||||
// }
|
||||
|
||||
return Promise.reject(new Error(message))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
export interface UseAxiosOption {
|
||||
// 错误时不弹窗
|
||||
hideErrorTip?: boolean
|
||||
}
|
||||
|
||||
// 导出 axios 实例
|
||||
export async function request<T = unknown, R = AxiosResponse<T>, D = unknown>(
|
||||
config: AxiosRequestConfig<D>,
|
||||
{ hideErrorTip = false } = {} as UseAxiosOption,
|
||||
): Promise<R> {
|
||||
try {
|
||||
return await service<T, R, D>(config)
|
||||
} catch (error) {
|
||||
if (!hideErrorTip) {
|
||||
ElNotification({
|
||||
showClose: true,
|
||||
message: (error as Error)?.message,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
}
|
||||
|
||||
export default request
|
||||
|
||||
export interface UseRequestOption<R> extends UseAxiosOption {
|
||||
defaultData?: R
|
||||
}
|
||||
|
||||
export interface UseRequestResult<R> {
|
||||
data: Ref<R>
|
||||
error: Ref<Error | undefined>
|
||||
loading: Ref<boolean>
|
||||
refresh: () => Promise<void>
|
||||
}
|
||||
|
||||
export function useRequest<T = unknown, R = AxiosResponse<T>, D = unknown>(
|
||||
config: AxiosRequestConfig<D>,
|
||||
{ defaultData = {} as R, ...rest } = {} as UseRequestOption<R>,
|
||||
) {
|
||||
const data = ref<R>(defaultData) as Ref<R>
|
||||
const loading = ref<boolean>(false)
|
||||
const error = ref<Error>()
|
||||
|
||||
const fetchResource = async (conf = config): Promise<void> => {
|
||||
loading.value = true
|
||||
try {
|
||||
data.value = await request<T, R, D>(conf, rest)
|
||||
} catch (e) {
|
||||
error.value = e as Error
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const promise = new Promise((resolve) => {
|
||||
void fetchResource().finally(() => resolve({ data, error, loading, refresh: fetchResource }))
|
||||
}) as unknown as UseRequestResult<R> & Promise<UseRequestResult<R>>
|
||||
|
||||
promise.data = data
|
||||
promise.error = error
|
||||
promise.loading = loading
|
||||
promise.refresh = fetchResource
|
||||
|
||||
return promise
|
||||
}
|
||||
Reference in New Issue
Block a user