fix: image response handler
This commit is contained in:
@@ -2,8 +2,7 @@ import useOverlayScroller from '@/hooks/use-overlay-scroller';
|
|||||||
import { extractErrorMessage, promptList } from '@/pages/playground/config';
|
import { extractErrorMessage, promptList } from '@/pages/playground/config';
|
||||||
import {
|
import {
|
||||||
fetchChunkedData,
|
fetchChunkedData,
|
||||||
fetchChunkedDataPostFormData,
|
fetchChunkedDataPostFormData
|
||||||
readLargeStreamData as readStreamData
|
|
||||||
} from '@/utils/fetch-chunk-data';
|
} from '@/utils/fetch-chunk-data';
|
||||||
import { useIntl } from '@umijs/max';
|
import { useIntl } from '@umijs/max';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
@@ -151,57 +150,104 @@ export default function useTextImage(props: any) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log('result:', result);
|
console.log('result:', result);
|
||||||
const { reader, decoder } = result;
|
if (requestIdRef.current !== currentRequestId) {
|
||||||
streamReaderRef.current = reader;
|
// If the request ID has changed, ignore this chunk
|
||||||
await readStreamData(reader, decoder, (chunk: any, done?: boolean) => {
|
// return;
|
||||||
console.log('chunk done:', chunk, done);
|
}
|
||||||
if (requestIdRef.current !== currentRequestId) {
|
// if (result?.error) {
|
||||||
// If the request ID has changed, ignore this chunk
|
// setTokenResult({
|
||||||
return;
|
// error: true,
|
||||||
}
|
// errorMessage: extractErrorMessage(result)
|
||||||
if (chunk?.error) {
|
// });
|
||||||
setTokenResult({
|
// return;
|
||||||
error: true,
|
// }
|
||||||
errorMessage: extractErrorMessage(chunk)
|
result?.data?.forEach((item: any, index: number) => {
|
||||||
});
|
const imgItem = newImageList[index];
|
||||||
return;
|
|
||||||
}
|
|
||||||
chunk?.data?.forEach((item: any) => {
|
|
||||||
const imgItem = newImageList[item.index];
|
|
||||||
if (item.b64_json && _.get(parameters, chunkFields)) {
|
|
||||||
imgItem.dataUrl += removeBase64Suffix(item.b64_json, ODD_STRING);
|
|
||||||
} else if (item.b64_json) {
|
|
||||||
imgItem.dataUrl = `data:image/png;base64,${removeBase64Suffix(item.b64_json, ODD_STRING)}`;
|
|
||||||
}
|
|
||||||
const progress = item.progress;
|
|
||||||
|
|
||||||
newImageList[item.index] = {
|
if (item.b64_json) {
|
||||||
dataUrl: imgItem.dataUrl,
|
imgItem.dataUrl = `data:image/png;base64,${item.b64_json}`;
|
||||||
height: imgSize[1],
|
|
||||||
width: imgSize[0],
|
|
||||||
maxHeight: `${imgSize[1]}px`,
|
|
||||||
maxWidth: `${imgSize[0]}px`,
|
|
||||||
uid: imgItem.uid,
|
|
||||||
span: imgItem.span,
|
|
||||||
loading: _.get(parameters, chunkFields) ? progress < 100 : false,
|
|
||||||
preview: API === CREAT_IMAGE_API ? progress >= 100 : false,
|
|
||||||
progress: progress
|
|
||||||
};
|
|
||||||
});
|
|
||||||
if (
|
|
||||||
done &&
|
|
||||||
!chunk?.error &&
|
|
||||||
newImageList.some((item) => item.progress < 100)
|
|
||||||
) {
|
|
||||||
setTokenResult({
|
|
||||||
error: true,
|
|
||||||
errorMessage: intl.formatMessage({
|
|
||||||
id: 'playground.image.generate.error'
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
setImageList([...newImageList]);
|
|
||||||
|
const progress = 100;
|
||||||
|
|
||||||
|
newImageList[index] = {
|
||||||
|
dataUrl: imgItem.dataUrl,
|
||||||
|
height: imgSize[1],
|
||||||
|
width: imgSize[0],
|
||||||
|
maxHeight: `${imgSize[1]}px`,
|
||||||
|
maxWidth: `${imgSize[0]}px`,
|
||||||
|
uid: imgItem.uid,
|
||||||
|
span: imgItem.span,
|
||||||
|
loading: _.get(parameters, chunkFields) ? progress < 100 : false,
|
||||||
|
preview: API === CREAT_IMAGE_API ? progress >= 100 : false,
|
||||||
|
progress: progress
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
console.log('newImageList:', newImageList);
|
||||||
|
// if (
|
||||||
|
// done &&
|
||||||
|
// !chunk?.error &&
|
||||||
|
// newImageList.some((item) => item.progress < 100)
|
||||||
|
// ) {
|
||||||
|
// setTokenResult({
|
||||||
|
// error: true,
|
||||||
|
// errorMessage: intl.formatMessage({
|
||||||
|
// id: 'playground.image.generate.error'
|
||||||
|
// })
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
setImageList([...newImageList]);
|
||||||
|
// const { reader, decoder } = result;
|
||||||
|
// streamReaderRef.current = reader;
|
||||||
|
// await readStreamData(reader, decoder, (chunk: any, done?: boolean) => {
|
||||||
|
// console.log('chunk done:', chunk, done);
|
||||||
|
// if (requestIdRef.current !== currentRequestId) {
|
||||||
|
// // If the request ID has changed, ignore this chunk
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// if (chunk?.error) {
|
||||||
|
// setTokenResult({
|
||||||
|
// error: true,
|
||||||
|
// errorMessage: extractErrorMessage(chunk)
|
||||||
|
// });
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// chunk?.data?.forEach((item: any) => {
|
||||||
|
// const imgItem = newImageList[item.index];
|
||||||
|
// if (item.b64_json && _.get(parameters, chunkFields)) {
|
||||||
|
// imgItem.dataUrl += removeBase64Suffix(item.b64_json, ODD_STRING);
|
||||||
|
// } else if (item.b64_json) {
|
||||||
|
// imgItem.dataUrl = `data:image/png;base64,${removeBase64Suffix(item.b64_json, ODD_STRING)}`;
|
||||||
|
// }
|
||||||
|
// const progress = item.progress;
|
||||||
|
|
||||||
|
// newImageList[item.index] = {
|
||||||
|
// dataUrl: imgItem.dataUrl,
|
||||||
|
// height: imgSize[1],
|
||||||
|
// width: imgSize[0],
|
||||||
|
// maxHeight: `${imgSize[1]}px`,
|
||||||
|
// maxWidth: `${imgSize[0]}px`,
|
||||||
|
// uid: imgItem.uid,
|
||||||
|
// span: imgItem.span,
|
||||||
|
// loading: _.get(parameters, chunkFields) ? progress < 100 : false,
|
||||||
|
// preview: API === CREAT_IMAGE_API ? progress >= 100 : false,
|
||||||
|
// progress: progress
|
||||||
|
// };
|
||||||
|
// });
|
||||||
|
// if (
|
||||||
|
// done &&
|
||||||
|
// !chunk?.error &&
|
||||||
|
// newImageList.some((item) => item.progress < 100)
|
||||||
|
// ) {
|
||||||
|
// setTokenResult({
|
||||||
|
// error: true,
|
||||||
|
// errorMessage: intl.formatMessage({
|
||||||
|
// id: 'playground.image.generate.error'
|
||||||
|
// })
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// setImageList([...newImageList]);
|
||||||
|
// });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('error:', error);
|
console.log('error:', error);
|
||||||
updateRequestId();
|
updateRequestId();
|
||||||
|
|||||||
@@ -78,14 +78,7 @@ export const fetchChunkedData = async (params: {
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return await errorHandler(response);
|
return await errorHandler(response);
|
||||||
}
|
}
|
||||||
const reader = response?.body?.getReader();
|
return response.json();
|
||||||
const decoder = new TextDecoder('utf-8', {
|
|
||||||
fatal: true
|
|
||||||
});
|
|
||||||
return {
|
|
||||||
reader,
|
|
||||||
decoder
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const createFormData = (data: any): FormData => {
|
const createFormData = (data: any): FormData => {
|
||||||
|
|||||||
Reference in New Issue
Block a user