UnisKB/ui/src/api/chat/chat.ts

376 lines
9.1 KiB
TypeScript
Raw Normal View History

import {Result} from '@/request/Result'
2025-06-16 04:08:09 +00:00
import {
get,
post,
postStream,
del,
put,
request,
download,
exportFile,
} from '@/request/chat/index'
import {type ChatProfile} from '@/api/type/chat'
import {type Ref} from 'vue'
import type {ResetPasswordRequest} from '@/api/type/user.ts'
2025-06-16 04:08:09 +00:00
2025-06-16 11:00:46 +00:00
import useStore from '@/stores'
import type {LoginRequest} from '@/api/type/user'
2025-06-17 08:18:41 +00:00
const prefix: any = {_value: '/workspace/'}
2025-06-16 11:00:46 +00:00
Object.defineProperty(prefix, 'value', {
get: function () {
const {user} = useStore()
2025-06-16 11:00:46 +00:00
return this._value + user.getWorkspaceId() + '/application'
},
})
2025-06-16 04:08:09 +00:00
/**
* id
* @param application_id id
* @param loading
* @returns
*/
2025-06-17 03:58:35 +00:00
const open: (loading?: Ref<boolean>) => Promise<Result<string>> = (loading) => {
return get('/open', {}, loading)
2025-06-16 04:08:09 +00:00
}
/**
*
* @param
* chat_id: string
* data
*/
const chat: (chat_id: string, data: any) => Promise<any> = (chat_id, data) => {
2025-07-01 07:43:00 +00:00
const prefix = (window.MaxKB?.prefix ? window.MaxKB?.prefix : '/chat') + '/api'
return postStream(`${prefix}/chat_message/${chat_id}`, data)
2025-06-16 04:08:09 +00:00
}
2025-06-25 11:20:30 +00:00
/**
*
*/
2025-06-17 03:58:35 +00:00
const chatProfile: (assessToken: string, loading?: Ref<boolean>) => Promise<Result<ChatProfile>> = (
2025-06-16 04:08:09 +00:00
assessToken,
loading,
) => {
return get('/profile', {access_token: assessToken}, loading)
2025-06-16 04:08:09 +00:00
}
2025-06-17 03:58:35 +00:00
/**
*
* @param assessToken
* @param loading
* @returns
*/
const anonymousAuthentication: (
assessToken: string,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (assessToken, loading) => {
return post('/auth/anonymous', {access_token: assessToken}, {}, loading)
2025-06-17 03:58:35 +00:00
}
2025-06-19 12:02:52 +00:00
/**
*
* @param assessToken
* @param password
* @param loading
* @returns
*/
const passwordAuthentication: (
assessToken: string,
password: string,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (assessToken, password, loading) => {
return post('auth/password', {access_token: assessToken, password: password}, {}, loading)
2025-06-19 12:02:52 +00:00
}
2025-06-17 03:58:35 +00:00
/**
*
* @param loading
* @returns
*/
const applicationProfile: (loading?: Ref<boolean>) => Promise<Result<any>> = (loading) => {
return get('/application/profile', {}, loading)
2025-06-16 04:08:09 +00:00
}
2025-06-17 08:18:41 +00:00
/**
*
* @param request
* @param loading
* @returns
*/
2025-06-17 12:26:20 +00:00
const login: (
2025-06-17 08:18:41 +00:00
accessToken: string,
2025-06-17 12:26:20 +00:00
request: LoginRequest,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (accessToken: string, request, loading) => {
2025-06-17 08:18:41 +00:00
return post('/auth/login/' + accessToken, request, undefined, loading)
}
2025-06-17 12:26:20 +00:00
const ldapLogin: (
2025-06-17 08:18:41 +00:00
accessToken: string,
2025-06-17 12:26:20 +00:00
request: LoginRequest,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (accessToken: string, request, loading) => {
2025-06-17 08:18:41 +00:00
return post('/auth/ldap/login/' + accessToken, request, undefined, loading)
}
/**
*
* @param username
2025-06-17 08:18:41 +00:00
* @param loading
*/
const getCaptcha: (username?: string, accessToken?: string, loading?: Ref<boolean>) => Promise<Result<any>> = (username, accessToken, loading) => {
return get('/captcha', {username: username, accessToken: accessToken}, loading)
2025-06-17 08:18:41 +00:00
}
/**
*
*/
const getQrType: (loading?: Ref<boolean>) => Promise<Result<any>> = (loading) => {
return get('auth/qr_type', undefined, loading)
2025-06-17 08:18:41 +00:00
}
const getQrSource: (loading?: Ref<boolean>) => Promise<Result<any>> = (loading) => {
return get('auth/qr_type/source', undefined, loading)
2025-06-17 08:18:41 +00:00
}
const getDingCallback: (code: string, accessToken: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
2025-06-17 08:18:41 +00:00
code,
accessToken,
2025-06-17 12:26:20 +00:00
loading,
2025-06-17 08:18:41 +00:00
) => {
return get('auth/dingtalk', {code, accessToken: accessToken}, loading)
2025-06-17 08:18:41 +00:00
}
const getDingOauth2Callback: (code: string, accessToken: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
2025-06-17 08:18:41 +00:00
code,
accessToken,
2025-06-17 12:26:20 +00:00
loading,
2025-06-17 08:18:41 +00:00
) => {
return get('auth/dingtalk/oauth2', {code, accessToken: accessToken}, loading)
2025-06-17 08:18:41 +00:00
}
const getWecomCallback: (code: string, accessToken: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
2025-06-17 08:18:41 +00:00
code,
accessToken,
2025-06-17 12:26:20 +00:00
loading,
2025-06-17 08:18:41 +00:00
) => {
return get('auth/wecom', {code, accessToken: accessToken}, loading)
2025-06-17 08:18:41 +00:00
}
const getLarkCallback: (code: string, accessToken: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
2025-06-17 08:18:41 +00:00
code,
accessToken,
2025-06-17 12:26:20 +00:00
loading,
2025-06-17 08:18:41 +00:00
) => {
return get('auth/lark/oauth2', {code, accessToken: accessToken}, loading)
2025-06-17 08:18:41 +00:00
}
2025-06-17 08:31:07 +00:00
/**
*
*/
2025-06-17 12:26:20 +00:00
const getAuthSetting: (auth_type: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
auth_type,
loading,
) => {
2025-06-17 08:31:07 +00:00
return get(`/chat_user/${auth_type}/detail`, undefined, loading)
}
2025-06-23 12:19:32 +00:00
/**
*
* @param chat_id id
* @param chat_record_id id
* @param vote_status
* @param loading
* @returns
*/
const vote: (
chat_id: string,
chat_record_id: string,
vote_status: string,
loading?: Ref<boolean>,
) => Promise<Result<boolean>> = (chat_id, chat_record_id, vote_status, loading) => {
return put(
`/vote/chat/${chat_id}/chat_record/${chat_record_id}`,
{
vote_status,
},
undefined,
loading,
)
}
2025-06-23 13:28:10 +00:00
const pageChat: (
current_page: number,
page_size: number,
loading?: Ref<boolean>,
) => Promise<Result<boolean>> = (current_page, page_size, loading) => {
return get(`/historical_conversation/${current_page}/${page_size}`, undefined, loading)
}
2025-06-24 09:44:59 +00:00
const pageChatRecord: (
chat_id: string,
current_page: number,
page_size: number,
loading?: Ref<boolean>,
) => Promise<Result<boolean>> = (chat_id, current_page, page_size, loading) => {
return get(
`/historical_conversation_record/${chat_id}/${current_page}/${page_size}`,
undefined,
loading,
)
}
2025-06-25 08:49:17 +00:00
/**
*
*/
const logout: (loading?: Ref<boolean>) => Promise<Result<boolean>> = (loading) => {
return post('/auth/logout', undefined, undefined, loading)
}
/**
*
*/
const resetCurrentPassword: (
request: ResetPasswordRequest,
2025-06-27 10:54:05 +00:00
loading?: Ref<boolean>,
2025-06-25 08:49:17 +00:00
) => Promise<Result<boolean>> = (request, loading) => {
return post('/chat_user/current/reset_password', request, undefined, loading)
}
2025-06-25 11:20:30 +00:00
/**
*
*/
const getChatUserProfile: (loading?: Ref<boolean>) => Promise<Result<any>> = (loading) => {
return get('/chat_user/profile', {}, loading)
}
2025-06-27 10:54:05 +00:00
/**
*
* @param chat_id id
* @param chat_record_id id
* @param loading
* @returns
*/
const getChatRecord: (
chat_id: string,
chat_record_id: string,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (chat_id, chat_record_id, loading) => {
return get(`historical_conversation/${chat_id}/record/${chat_record_id}`, {}, loading)
}
const postChatRecordFeedback: (
chat_id: string,
chat_record_id: string,
data: any,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (chat_id, chat_record_id, data, loading) => {
return post(`historical_conversation/${chat_id}/record/${chat_record_id}/feedback`, data, undefined, loading)
}
/**
*
*/
const textToSpeech: (data: any, loading?: Ref<boolean>) => Promise<Result<any>> = (
data,
loading,
) => {
return download(`text_to_speech`, 'post', data, undefined, loading)
}
2025-06-27 10:54:05 +00:00
/**
*
*/
const speechToText: (data: any, loading?: Ref<boolean>) => Promise<Result<any>> = (
data,
loading,
) => {
return post(`speech_to_text`, data, undefined, loading)
}
2025-07-03 09:38:14 +00:00
/**
2025-07-04 11:52:45 +00:00
*
2025-07-03 09:38:14 +00:00
* @param chat_id ID
2025-07-04 11:52:45 +00:00
* @param loading
* @returns
2025-07-03 09:38:14 +00:00
*/
2025-07-04 11:52:45 +00:00
const deleteChat: (chat_id: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
2025-07-03 09:38:14 +00:00
chat_id,
loading,
) => {
return del(`historical_conversation/${chat_id}`, undefined, undefined, loading)
2025-07-15 06:59:40 +00:00
}
/**
*
* @param loading
* @returns
2025-07-15 06:59:40 +00:00
*/
const clearChat: (loading?: Ref<boolean>) => Promise<Result<any>> = (
loading
) => {
return del(`historical_conversation/clear`, undefined, undefined, loading)
2025-07-03 09:38:14 +00:00
}
/**
2025-07-04 11:52:45 +00:00
*
2025-07-03 09:38:14 +00:00
* @param chat_id id
* @param data
2025-07-04 11:52:45 +00:00
* @param loading
* @returns
2025-07-03 09:38:14 +00:00
*/
2025-07-04 11:52:45 +00:00
const modifyChat: (chat_id: string, data: any, loading?: Ref<boolean>) => Promise<Result<any>> = (
chat_id,
data,
loading,
2025-07-03 09:38:14 +00:00
) => {
return put(`historical_conversation/${chat_id}`, data, undefined, loading)
}
2025-07-04 11:52:45 +00:00
/**
*
* @param file
* @param sourceId id
* @param resourceType
* @returns
*/
2025-07-10 09:09:19 +00:00
const postUploadFile: (
2025-07-04 11:52:45 +00:00
file: any,
sourceId: string,
resourceType:
| 'KNOWLEDGE'
| 'APPLICATION'
| 'TOOL'
| 'DOCUMENT'
| 'CHAT'
| 'TEMPORARY_30_MINUTE'
| 'TEMPORARY_120_MINUTE'
| 'TEMPORARY_1_DAY',
2025-07-10 09:09:19 +00:00
loading?: Ref<boolean>,
) => Promise<Result<any>> = (file, sourceId, sourceType, loading) => {
2025-07-04 11:52:45 +00:00
const fd = new FormData()
fd.append('file', file)
fd.append('source_id', sourceId)
fd.append('source_type', sourceType)
2025-07-10 09:09:19 +00:00
return post(`/oss/file`, fd, undefined, loading)
2025-07-04 11:52:45 +00:00
}
2025-06-16 04:08:09 +00:00
export default {
open,
chat,
chatProfile,
2025-06-17 03:58:35 +00:00
anonymousAuthentication,
applicationProfile,
2025-06-17 08:18:41 +00:00
login,
getCaptcha,
getDingCallback,
getQrType,
getWecomCallback,
getDingOauth2Callback,
getLarkCallback,
getQrSource,
2025-06-17 08:31:07 +00:00
ldapLogin,
2025-06-17 12:26:20 +00:00
getAuthSetting,
2025-06-19 12:02:52 +00:00
passwordAuthentication,
2025-06-23 12:19:32 +00:00
vote,
2025-06-23 13:28:10 +00:00
pageChat,
2025-06-24 09:44:59 +00:00
pageChatRecord,
2025-06-25 08:49:17 +00:00
logout,
2025-06-25 11:20:30 +00:00
resetCurrentPassword,
2025-06-27 10:54:05 +00:00
getChatUserProfile,
getChatRecord,
postChatRecordFeedback,
textToSpeech,
speechToText,
2025-07-03 09:38:14 +00:00
deleteChat,
2025-07-15 06:59:40 +00:00
clearChat,
2025-07-04 11:52:45 +00:00
modifyChat,
2025-07-10 09:09:19 +00:00
postUploadFile,
2025-06-16 04:08:09 +00:00
}