feat: annotation management frontend (#1764)
This commit is contained in:
65
web/service/annotation.ts
Normal file
65
web/service/annotation.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { Fetcher } from 'swr'
|
||||
import { del, get, post } from './base'
|
||||
import type { AnnotationEnableStatus, AnnotationItemBasic, EmbeddingModelConfig } from '@/app/components/app/annotation/type'
|
||||
import { ANNOTATION_DEFAULT } from '@/config'
|
||||
|
||||
export const fetchAnnotationConfig = (appId: string) => {
|
||||
return get(`apps/${appId}/annotation-setting`)
|
||||
}
|
||||
export const updateAnnotationStatus = (appId: string, action: AnnotationEnableStatus, embeddingModel?: EmbeddingModelConfig, score?: number) => {
|
||||
let body: any = {
|
||||
score_threshold: score || ANNOTATION_DEFAULT.score_threshold,
|
||||
}
|
||||
if (embeddingModel) {
|
||||
body = {
|
||||
...body,
|
||||
...embeddingModel,
|
||||
}
|
||||
}
|
||||
|
||||
return post(`apps/${appId}/annotation-reply/${action}`, {
|
||||
body,
|
||||
})
|
||||
}
|
||||
|
||||
export const updateAnnotationScore = (appId: string, settingId: string, score: number) => {
|
||||
return post(`apps/${appId}/annotation-settings/${settingId}`, {
|
||||
body: { score_threshold: score },
|
||||
})
|
||||
}
|
||||
|
||||
export const queryAnnotationJobStatus = (appId: string, action: AnnotationEnableStatus, jobId: string) => {
|
||||
return get(`apps/${appId}/annotation-reply/${action}/status/${jobId}`)
|
||||
}
|
||||
|
||||
export const fetchAnnotationList = (appId: string, params: Record<string, any>) => {
|
||||
return get(`apps/${appId}/annotations`, { params })
|
||||
}
|
||||
|
||||
export const fetchExportAnnotationList = (appId: string) => {
|
||||
return get(`apps/${appId}/annotations/export`)
|
||||
}
|
||||
|
||||
export const addAnnotation = (appId: string, body: AnnotationItemBasic) => {
|
||||
return post(`apps/${appId}/annotations`, { body })
|
||||
}
|
||||
|
||||
export const annotationBatchImport: Fetcher<{ job_id: string; job_status: string }, { url: string; body: FormData }> = ({ url, body }) => {
|
||||
return post<{ job_id: string; job_status: string }>(url, { body }, { bodyStringify: false, deleteContentType: true })
|
||||
}
|
||||
|
||||
export const checkAnnotationBatchImportProgress: Fetcher<{ job_id: string; job_status: string }, { jobID: string; appId: string }> = ({ jobID, appId }) => {
|
||||
return get<{ job_id: string; job_status: string }>(`/apps/${appId}/annotations/batch-import-status/${jobID}`)
|
||||
}
|
||||
|
||||
export const editAnnotation = (appId: string, annotationId: string, body: AnnotationItemBasic) => {
|
||||
return post(`apps/${appId}/annotations/${annotationId}`, { body })
|
||||
}
|
||||
|
||||
export const delAnnotation = (appId: string, annotationId: string) => {
|
||||
return del(`apps/${appId}/annotations/${annotationId}`)
|
||||
}
|
||||
|
||||
export const fetchHitHistoryList = (appId: string, annotationId: string, params: Record<string, any>) => {
|
||||
return get(`apps/${appId}/annotations/${annotationId}/hit-histories`, { params })
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { API_PREFIX, IS_CE_EDITION, PUBLIC_API_PREFIX } from '@/config'
|
||||
import Toast from '@/app/components/base/toast'
|
||||
import type { MessageEnd, MessageReplace, ThoughtItem } from '@/app/components/app/chat/type'
|
||||
import type { AnnotationReply, MessageEnd, MessageReplace, ThoughtItem } from '@/app/components/app/chat/type'
|
||||
|
||||
const TIME_OUT = 100000
|
||||
|
||||
@@ -34,6 +34,7 @@ export type IOnData = (message: string, isFirstMessage: boolean, moreInfo: IOnDa
|
||||
export type IOnThought = (though: ThoughtItem) => void
|
||||
export type IOnMessageEnd = (messageEnd: MessageEnd) => void
|
||||
export type IOnMessageReplace = (messageReplace: MessageReplace) => void
|
||||
export type IOnAnnotationReply = (messageReplace: AnnotationReply) => void
|
||||
export type IOnCompleted = (hasError?: boolean) => void
|
||||
export type IOnError = (msg: string, code?: string) => void
|
||||
|
||||
@@ -46,6 +47,7 @@ type IOtherOptions = {
|
||||
onThought?: IOnThought
|
||||
onMessageEnd?: IOnMessageEnd
|
||||
onMessageReplace?: IOnMessageReplace
|
||||
onAnnotationReply?: IOnAnnotationReply
|
||||
onError?: IOnError
|
||||
onCompleted?: IOnCompleted // for stream
|
||||
getAbortController?: (abortController: AbortController) => void
|
||||
@@ -79,7 +81,7 @@ export function format(text: string) {
|
||||
return res.replaceAll('\n', '<br/>').replaceAll('```', '')
|
||||
}
|
||||
|
||||
const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought, onMessageEnd?: IOnMessageEnd, onMessageReplace?: IOnMessageReplace) => {
|
||||
const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought, onMessageEnd?: IOnMessageEnd, onMessageReplace?: IOnMessageReplace, onAnnotationReply?: IOnAnnotationReply) => {
|
||||
if (!response.ok)
|
||||
throw new Error('Network response was not ok')
|
||||
|
||||
@@ -140,6 +142,9 @@ const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnComp
|
||||
else if (bufferObj.event === 'message_replace') {
|
||||
onMessageReplace?.(bufferObj as MessageReplace)
|
||||
}
|
||||
else if (bufferObj.event === 'annotation') {
|
||||
onAnnotationReply?.(bufferObj as AnnotationReply)
|
||||
}
|
||||
}
|
||||
})
|
||||
buffer = lines[lines.length - 1]
|
||||
@@ -345,7 +350,7 @@ export const upload = (options: any, isPublicAPI?: boolean): Promise<any> => {
|
||||
})
|
||||
}
|
||||
|
||||
export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAPI = false, onData, onCompleted, onThought, onMessageEnd, onMessageReplace, onError, getAbortController }: IOtherOptions) => {
|
||||
export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAPI = false, onData, onCompleted, onThought, onMessageEnd, onMessageReplace, onAnnotationReply, onError, getAbortController }: IOtherOptions) => {
|
||||
const abortController = new AbortController()
|
||||
|
||||
const options = Object.assign({}, baseOptions, {
|
||||
@@ -384,7 +389,7 @@ export const ssePost = (url: string, fetchOptions: FetchOptionType, { isPublicAP
|
||||
return
|
||||
}
|
||||
onData?.(str, isFirstMessage, moreInfo)
|
||||
}, onCompleted, onThought, onMessageEnd, onMessageReplace)
|
||||
}, onCompleted, onThought, onMessageEnd, onMessageReplace, onAnnotationReply)
|
||||
}).catch((e) => {
|
||||
if (e.toString() !== 'AbortError: The user aborted a request.')
|
||||
Toast.notify({ type: 'error', message: e })
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
|
||||
import type { IOnAnnotationReply, IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
|
||||
import { get, post, ssePost } from './base'
|
||||
import type { ChatPromptConfig, CompletionPromptConfig } from '@/models/debug'
|
||||
import type { ModelModeType } from '@/types/app'
|
||||
@@ -9,11 +9,12 @@ export type AutomaticRes = {
|
||||
opening_statement: string
|
||||
}
|
||||
|
||||
export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace }: {
|
||||
export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace, onAnnotationReply }: {
|
||||
onData: IOnData
|
||||
onCompleted: IOnCompleted
|
||||
onMessageEnd: IOnMessageEnd
|
||||
onMessageReplace: IOnMessageReplace
|
||||
onAnnotationReply: IOnAnnotationReply
|
||||
onError: IOnError
|
||||
getAbortController?: (abortController: AbortController) => void
|
||||
}) => {
|
||||
@@ -22,7 +23,7 @@ export const sendChatMessage = async (appId: string, body: Record<string, any>,
|
||||
...body,
|
||||
response_mode: 'streaming',
|
||||
},
|
||||
}, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace })
|
||||
}, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace, onAnnotationReply })
|
||||
}
|
||||
|
||||
export const stopChatMessageResponding = async (appId: string, taskId: string) => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
|
||||
import type { IOnAnnotationReply, IOnCompleted, IOnData, IOnError, IOnMessageEnd, IOnMessageReplace } from './base'
|
||||
import {
|
||||
del as consoleDel, get as consoleGet, patch as consolePatch, post as consolePost,
|
||||
delPublic as del, getPublic as get, patchPublic as patch, postPublic as post, ssePost,
|
||||
@@ -22,12 +22,13 @@ function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) {
|
||||
return isInstalledApp ? `installed-apps/${installedAppId}/${url.startsWith('/') ? url.slice(1) : url}` : url
|
||||
}
|
||||
|
||||
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace }: {
|
||||
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd, onMessageReplace, onAnnotationReply }: {
|
||||
onData: IOnData
|
||||
onCompleted: IOnCompleted
|
||||
onError: IOnError
|
||||
onMessageEnd?: IOnMessageEnd
|
||||
onMessageReplace?: IOnMessageReplace
|
||||
onAnnotationReply: IOnAnnotationReply
|
||||
getAbortController?: (abortController: AbortController) => void
|
||||
}, isInstalledApp: boolean, installedAppId = '') => {
|
||||
return ssePost(getUrl('chat-messages', isInstalledApp, installedAppId), {
|
||||
@@ -35,7 +36,7 @@ export const sendChatMessage = async (body: Record<string, any>, { onData, onCom
|
||||
...body,
|
||||
response_mode: 'streaming',
|
||||
},
|
||||
}, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd, onMessageReplace })
|
||||
}, { onData, onCompleted, isPublicAPI: !isInstalledApp, onError, getAbortController, onMessageEnd, onMessageReplace, onAnnotationReply })
|
||||
}
|
||||
|
||||
export const stopChatMessageResponding = async (appId: string, taskId: string, isInstalledApp: boolean, installedAppId = '') => {
|
||||
|
||||
Reference in New Issue
Block a user