universal-chat.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import type { IOnCompleted, IOnData, IOnError, IOnThought } from './base'
  2. import {
  3. del, get, patch, post, ssePost,
  4. } from './base'
  5. import type { Feedbacktype } from '@/app/components/app/chat/type'
  6. const baseUrl = 'universal-chat'
  7. function getUrl(url: string) {
  8. return `${baseUrl}/${url.startsWith('/') ? url.slice(1) : url}`
  9. }
  10. export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, onThought, getAbortController }: {
  11. onData: IOnData
  12. onCompleted: IOnCompleted
  13. onError: IOnError
  14. onThought: IOnThought
  15. getAbortController?: (abortController: AbortController) => void
  16. }) => {
  17. return ssePost(getUrl('messages'), {
  18. body: {
  19. ...body,
  20. response_mode: 'streaming',
  21. },
  22. }, { onData, onCompleted, onThought, onError, getAbortController })
  23. }
  24. export const stopChatMessageResponding = async (taskId: string) => {
  25. return post(getUrl(`messages/${taskId}/stop`))
  26. }
  27. export const fetchConversations = async (last_id?: string, pinned?: boolean, limit?: number) => {
  28. return get(getUrl('conversations'), { params: { ...{ limit: limit || 20 }, ...(last_id ? { last_id } : {}), ...(pinned !== undefined ? { pinned } : {}) } })
  29. }
  30. export const pinConversation = async (id: string) => {
  31. return patch(getUrl(`conversations/${id}/pin`))
  32. }
  33. export const unpinConversation = async (id: string) => {
  34. return patch(getUrl(`conversations/${id}/unpin`))
  35. }
  36. export const delConversation = async (id: string) => {
  37. return del(getUrl(`conversations/${id}`))
  38. }
  39. export const renameConversation = async (id: string, name: string) => {
  40. return post(getUrl(`conversations/${id}/name`), { body: { name } })
  41. }
  42. export const fetchChatList = async (conversationId: string) => {
  43. return get(getUrl('messages'), { params: { conversation_id: conversationId, limit: 20, last_id: '' } })
  44. }
  45. // init value. wait for server update
  46. export const fetchAppParams = async () => {
  47. return get(getUrl('parameters'))
  48. }
  49. export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }) => {
  50. return post(getUrl(url), { body })
  51. }
  52. export const fetchMoreLikeThis = async (messageId: string) => {
  53. return get(getUrl(`/messages/${messageId}/more-like-this`), {
  54. params: {
  55. response_mode: 'blocking',
  56. },
  57. })
  58. }
  59. export const fetchSuggestedQuestions = (messageId: string) => {
  60. return get(getUrl(`/messages/${messageId}/suggested-questions`))
  61. }
  62. export const audioToText = (url: string, body: FormData) => {
  63. return post(url, { body }, { bodyStringify: false, deleteContentType: true }) as Promise<{ text: string }>
  64. }