import request from 'umi-request'; import { EventStreamContentType, FetchEventSourceInit, fetchEventSource } from '@microsoft/fetch-event-source'; let GANT_AI_URL = '/assistant'; let URL_SUFFIX = '.api'; if (process.env.NODE_ENV === 'development') { GANT_AI_URL = 'http://localhost:3000'; URL_SUFFIX = ''; fetch(GANT_AI_URL).catch(() => { GANT_AI_URL = '/assistant'; URL_SUFFIX = '.api'; }); } export async function create(data: any) { return request.post(GANT_AI_URL + '/gantai/create' + URL_SUFFIX, { data: data, }); } export async function stopConversation(data: any) { return request.post(GANT_AI_URL + '/gantai/stop_conversation' + URL_SUFFIX, { data: data, }); } export async function uploadFile(data: any) { return request.post(GANT_AI_URL + '/gantai/filesToMessage' + URL_SUFFIX, { data, requestType: 'form', }); } export async function uploadImageService(data: any) { return request.post(GANT_AI_URL + '/gantai/files' + URL_SUFFIX, { data, requestType: 'form', }); } export async function delFile(id: string) { return request.post(GANT_AI_URL + `/gantai/delfile/${id}` + URL_SUFFIX); } export async function message(data: any) { return request.post('/gantai/message' + URL_SUFFIX, { data: data, }); } export async function textToSpeech(params: any) { return request.post(`/gantai/textToSpeech` + URL_SUFFIX, { data: params, }); } export async function speechToTextService(formData: FormData) { const data = await fetch(GANT_AI_URL + '/gantai/speechToText' + URL_SUFFIX, { method: 'POST', body: formData, }) .then((response) => response.json()) .catch((error) => console.error('Error:', error)); return data; } export async function fetchMessages(thread_id: string) { return request.post(`/gantai/messages/${thread_id}` + URL_SUFFIX); } class RetriableError extends Error {} class FatalError extends Error {} export const fetchAi = async (url: string, params: FetchEventSourceInit) => { const ctrl = new AbortController(); const eventSource = await fetchEventSource(`${GANT_AI_URL}${url}${URL_SUFFIX}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(params.body), signal: ctrl.signal, openWhenHidden: true, async onopen(response) { params.onopen?.(response); if (response.ok && response.headers.get('content-type') === EventStreamContentType) { return; // everything's good } else if (response.status >= 400 && response.status < 500 && response.status !== 429) { // client-side errors are usually non-retriable: throw new FatalError(); } else { throw new RetriableError(); } }, onmessage: params.onmessage, onclose() { console.log('onclose'); params.onclose?.(); // setWaiting(false); ctrl.abort(); //关闭连接 // if the server closes the connection unexpectedly, retry: throw new RetriableError(); }, onerror(err) { console.log('err', err); params?.onerror?.(err); if (err instanceof FatalError) { // setWaiting(false); throw err; // rethrow to stop the operation } else { // do nothing to automatically retry. You can also // return a specific retry interval here. } }, }); return eventSource; };