Fix smooth stream for MakerSuite

This commit is contained in:
Cohee
2024-04-02 16:13:01 +03:00
parent 7389286862
commit ca047034b7

View File

@ -122,15 +122,24 @@ async function* parseStreamData(json) {
// MakerSuite // MakerSuite
else if (Array.isArray(json.candidates)) { else if (Array.isArray(json.candidates)) {
for (let i = 0; i < json.candidates.length; i++) { for (let i = 0; i < json.candidates.length; i++) {
if (typeof json.candidates[i].content === 'string' && json.candidates[i].content.length > 0) { const isNotPrimary = json.candidates?.[0]?.index > 0;
for (let j = 0; j < json.candidates[i].content.length; j++) { if (isNotPrimary || json.candidates.length === 0) {
const str = json.candidates[i].content[j]; return null;
const candidatesClone = structuredClone(json.candidates[i]); }
candidatesClone[i].content = str; if (typeof json.candidates[0].content === 'object' && Array.isArray(json.candidates[i].content.parts)) {
yield { for (let j = 0; j < json.candidates[i].content.parts.length; j++) {
data: { ...json, candidates: candidatesClone }, if (typeof json.candidates[i].content.parts[j].text === 'string') {
chunk: str, for (let k = 0; k < json.candidates[i].content.parts[j].text.length; k++) {
}; const str = json.candidates[i].content.parts[j].text[k];
const candidateClone = structuredClone(json.candidates[0]);
candidateClone.content.parts[j].text = str;
const candidates = [candidateClone];
yield {
data: { ...json, candidates },
chunk: str,
};
}
}
} }
} }
} }