|
@@ -71,22 +71,20 @@ export const speechToText = async (agentId: string, tempFilePath: string) => {
|
|
|
}
|
|
|
|
|
|
|
|
|
-export type TTextToSpeechParams = {
|
|
|
- params: {
|
|
|
- agentId: string,
|
|
|
- loginId: string,
|
|
|
- msgUk: string,
|
|
|
- text: string
|
|
|
- };
|
|
|
+// 流式请求
|
|
|
+export type TRequestStream<T> = {
|
|
|
+ url: string,
|
|
|
+ params: T;
|
|
|
onStart: () => void;
|
|
|
onReceived: (m: ICompleteCallback) => void;
|
|
|
- onAudioParsed: (m: {audio: string}) => void;
|
|
|
+ onAudioParsed: (m: ICompleteCallback) => void;
|
|
|
onFinished: (m: ICompleteCallback) => void;
|
|
|
onComplete?: () => void; // 无论失败或成功都会执行
|
|
|
onError: () => void;
|
|
|
};
|
|
|
|
|
|
-export const requestTextToSpeech = ({
|
|
|
+export const requestStream = <T>({
|
|
|
+ url,
|
|
|
params,
|
|
|
onStart,
|
|
|
onReceived,
|
|
@@ -94,24 +92,29 @@ export const requestTextToSpeech = ({
|
|
|
onFinished,
|
|
|
onComplete,
|
|
|
onError,
|
|
|
-}: TTextToSpeechParams) => {
|
|
|
+}: TRequestStream<T>) => {
|
|
|
onStart();
|
|
|
|
|
|
- let reqTask: Taro.RequestTask<any>|undefined = undefined;
|
|
|
+ let reqTask: Taro.RequestTask<any>|undefined|null = undefined;
|
|
|
const jsonParser = new JsonChunkParser();
|
|
|
jsonParser.onParseComplete((m) => {
|
|
|
onFinished(m);
|
|
|
});
|
|
|
|
|
|
const onChunkReceived = (chunk: any) => {
|
|
|
+ // console.log('chunkReceived: ', chunk);
|
|
|
const uint8Array = new Uint8Array(chunk.data);
|
|
|
+ // console.log('uint8Array: ', uint8Array);
|
|
|
var string = new TextDecoder("utf-8").decode(uint8Array);
|
|
|
- jsonParser.parseChunk(string, onReceived, onAudioParsed);
|
|
|
+ // console.log('chunked:', string);
|
|
|
+ jsonParser.parseChunk(string, (m) => {
|
|
|
+ // console.log('parseChunk', m);
|
|
|
+ onReceived(m);
|
|
|
+ }, onAudioParsed);
|
|
|
};
|
|
|
const header = getSimpleHeader()
|
|
|
|
|
|
try {
|
|
|
- const url = `${bluebookAiAgent}api/v1/chat/text/speech`;
|
|
|
reqTask = Taro.request({
|
|
|
url: url,
|
|
|
data: params,
|
|
@@ -122,12 +125,19 @@ export const requestTextToSpeech = ({
|
|
|
},
|
|
|
responseType: "arraybuffer",
|
|
|
success: function (res) {
|
|
|
- // console.log("text to speech 服务端响应 >>", res);
|
|
|
- // onComplete?.()
|
|
|
+ console.log("服务端响应 >>", res);
|
|
|
},
|
|
|
complete: function(res) {
|
|
|
- console.log("text to speech 服务端响应 complete", res);
|
|
|
+ console.log(reqTask)
|
|
|
+ console.log("reqTask >>", reqTask, res);
|
|
|
+ if(reqTask) {
|
|
|
onComplete?.()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: function (){
|
|
|
+ if(reqTask) {
|
|
|
+ onComplete?.()
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
|
|
@@ -140,6 +150,9 @@ export const requestTextToSpeech = ({
|
|
|
|
|
|
const stopChunk = () => {
|
|
|
reqTask?.offChunkReceived(onChunkReceived);
|
|
|
+ reqTask?.abort();
|
|
|
+ reqTask = null
|
|
|
+ console.log('stop reqTask: v1/chat/completions')
|
|
|
};
|
|
|
|
|
|
return {reqTask: reqTask, stopChunk};
|
|
@@ -147,8 +160,53 @@ export const requestTextToSpeech = ({
|
|
|
|
|
|
|
|
|
|
|
|
-// 文本聊天,流式返回消息
|
|
|
+// 文本转语音
|
|
|
+export type TTextToSpeechParams = {
|
|
|
+ params: {
|
|
|
+ agentId: string,
|
|
|
+ loginId: string,
|
|
|
+ msgUk: string,
|
|
|
+ text: string
|
|
|
+ };
|
|
|
+ onStart: () => void;
|
|
|
+ onReceived: (m: ICompleteCallback) => void;
|
|
|
+ onAudioParsed: (m: ICompleteCallback) => void;
|
|
|
+ onFinished: (m: ICompleteCallback) => void;
|
|
|
+ onComplete?: () => void; // 无论失败或成功都会执行
|
|
|
+ onError: () => void;
|
|
|
+};
|
|
|
+
|
|
|
+export const requestTextToSpeech = ({
|
|
|
+ params,
|
|
|
+ onStart,
|
|
|
+ onReceived,
|
|
|
+ onAudioParsed,
|
|
|
+ onFinished,
|
|
|
+ onComplete,
|
|
|
+ onError,
|
|
|
+}: TTextToSpeechParams) => {
|
|
|
+
|
|
|
+ const url = `${bluebookAiAgent}api/v1/chat/text/speech`;
|
|
|
+ return requestStream<{
|
|
|
+ agentId: string,
|
|
|
+ loginId: string,
|
|
|
+ msgUk: string,
|
|
|
+ text: string
|
|
|
+ }>({
|
|
|
+ url,
|
|
|
+ params,
|
|
|
+ onStart,
|
|
|
+ onReceived,
|
|
|
+ onAudioParsed,
|
|
|
+ onFinished,
|
|
|
+ onComplete,
|
|
|
+ onError,
|
|
|
+ })
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
|
|
|
+// 聊天,流式返回消息
|
|
|
export type TTextChatParams = {
|
|
|
params: TRequestBody;
|
|
|
onStart: () => void;
|
|
@@ -168,63 +226,15 @@ export const requestTextToChat = ({
|
|
|
onComplete,
|
|
|
onError,
|
|
|
}: TTextChatParams) => {
|
|
|
- onStart();
|
|
|
-
|
|
|
- let reqTask: Taro.RequestTask<any>|undefined = undefined;
|
|
|
- const jsonParser = new JsonChunkParser();
|
|
|
- jsonParser.onParseComplete((m) => {
|
|
|
- onFinished(m);
|
|
|
- });
|
|
|
-
|
|
|
- const onChunkReceived = (chunk: any) => {
|
|
|
- // console.log('chunkReceived: ', chunk);
|
|
|
- const uint8Array = new Uint8Array(chunk.data);
|
|
|
- // console.log('uint8Array: ', uint8Array);
|
|
|
- var string = new TextDecoder("utf-8").decode(uint8Array);
|
|
|
- // console.log('chunked:', string);
|
|
|
- jsonParser.parseChunk(string, (m) => {
|
|
|
- // console.log('parseChunk', m);
|
|
|
- onReceived(m);
|
|
|
- }, onAudioParsed);
|
|
|
- };
|
|
|
- const header = getSimpleHeader()
|
|
|
-
|
|
|
- try {
|
|
|
- const url = `${bluebookAiAgent}api/v1/chat/completions`;
|
|
|
- reqTask = Taro.request({
|
|
|
- url: url,
|
|
|
- data: params,
|
|
|
- enableChunked: true,
|
|
|
- method: "POST",
|
|
|
- header: {
|
|
|
- ...header
|
|
|
- },
|
|
|
- responseType: "arraybuffer",
|
|
|
- success: function (res) {
|
|
|
- console.log("服务端响应 >>", res);
|
|
|
- },
|
|
|
- complete: function(res) {
|
|
|
- console.log(reqTask)
|
|
|
- console.log("reqTask >>", reqTask, res);
|
|
|
- if(reqTask) {
|
|
|
- onComplete?.()
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- // reqTask.
|
|
|
- reqTask.onChunkReceived(onChunkReceived);
|
|
|
- } catch (e) {
|
|
|
- onComplete?.()
|
|
|
- onError();
|
|
|
- }
|
|
|
-
|
|
|
- const stopChunk = () => {
|
|
|
- reqTask?.offChunkReceived(onChunkReceived);
|
|
|
- reqTask?.abort();
|
|
|
- reqTask = null
|
|
|
- console.log('stop reqTask: v1/chat/completions')
|
|
|
- };
|
|
|
-
|
|
|
- return {reqTask: reqTask, stopChunk};
|
|
|
+ const url = `${bluebookAiAgent}api/v1/chat/completions`;
|
|
|
+ return requestStream<TRequestBody>({
|
|
|
+ url,
|
|
|
+ params,
|
|
|
+ onStart,
|
|
|
+ onReceived,
|
|
|
+ onAudioParsed,
|
|
|
+ onFinished,
|
|
|
+ onComplete,
|
|
|
+ onError,
|
|
|
+ })
|
|
|
};
|