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,19 +122,28 @@ 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)) {
for (let j = 0; j < json.candidates[i].content.parts.length; j++) {
if (typeof json.candidates[i].content.parts[j].text === 'string') {
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 { yield {
data: { ...json, candidates: candidatesClone }, data: { ...json, candidates },
chunk: str, chunk: str,
}; };
} }
} }
} }
} }
}
}
// NovelAI / KoboldCpp Classic // NovelAI / KoboldCpp Classic
else if (typeof json.token === 'string' && json.token.length > 0) { else if (typeof json.token === 'string' && json.token.length > 0) {
for (let i = 0; i < json.token.length; i++) { for (let i = 0; i < json.token.length; i++) {