handles google streaming json response format variation
This commit is contained in:
parent
68d829bceb
commit
59bda40bbc
|
@ -84,26 +84,28 @@ export class SSEStreamAdapter extends Transform {
|
|||
}
|
||||
|
||||
/** Processes an incoming array element from the Google AI JSON stream. */
|
||||
protected processGoogleObject(value: any): string | null {
|
||||
protected processGoogleObject(data: any): string | null {
|
||||
// Sometimes data has fields key and value, sometimes it's just the
|
||||
// candidates array.
|
||||
const candidates = data.value?.candidates ?? data.candidates ?? [{}];
|
||||
try {
|
||||
const candidates = value.candidates ?? [{}];
|
||||
const hasParts = candidates[0].content?.parts?.length > 0;
|
||||
if (hasParts) {
|
||||
return `data: ${JSON.stringify(value)}`;
|
||||
return `data: ${JSON.stringify(data)}`;
|
||||
} else {
|
||||
this.log.error({ event: value }, "Received bad Google AI event");
|
||||
this.log.error({ event: data }, "Received bad Google AI event");
|
||||
return `data: ${makeCompletionSSE({
|
||||
format: "google-ai",
|
||||
title: "Proxy stream error",
|
||||
message:
|
||||
"The proxy received malformed or unexpected data from Google AI while streaming.",
|
||||
obj: value,
|
||||
obj: data,
|
||||
reqId: "proxy-sse-adapter-message",
|
||||
model: "",
|
||||
})}`;
|
||||
}
|
||||
} catch (error) {
|
||||
error.lastEvent = value;
|
||||
error.lastEvent = data;
|
||||
this.emit("error", error);
|
||||
}
|
||||
return null;
|
||||
|
|
Loading…
Reference in New Issue