File size: 2,995 Bytes
e81f4e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | import type { GoogleResponsePart } from "../../completion/google-turn";
import { upstreamErrorCode, upstreamErrorMessage } from "../../shared/errors";
import type { SSEWrite } from "../core/sse";
import { streamWarningObject } from "../core/stream-errors";
import { isPromiseLike } from "../stream/promise";
export function googleErrorResponseBody(
message: unknown,
code: unknown = undefined,
) {
const error: Record<string, unknown> = { message };
if (code) error.code = code;
return { error };
}
export async function writeGoogleStreamError(
write: SSEWrite,
model: unknown,
e: unknown,
): Promise<void> {
const result = write(
`data: ${JSON.stringify({
error: {
message: upstreamErrorMessage(e),
code: upstreamErrorCode(e) || "upstream_error",
},
modelVersion: model,
})}\n\n`,
);
if (isPromiseLike(result)) await result;
}
export async function writeGoogleCandidate(
write: SSEWrite,
model: unknown,
parts: GoogleResponsePart[] | null,
finishReason: string | null,
): Promise<void> {
const candidate: Record<string, unknown> = { index: 0 };
if (Array.isArray(parts) && parts.length)
candidate.content = { parts, role: "model" };
if (finishReason) candidate.finishReason = finishReason;
const result = write(
`data: ${JSON.stringify({ candidates: [candidate], modelVersion: model })}\n\n`,
);
if (isPromiseLike(result)) await result;
}
export async function writeGoogleDone(
write: SSEWrite,
model: unknown,
usageMetadata: unknown,
): Promise<void> {
const result = write(
`data: ${JSON.stringify({
candidates: [{ finishReason: "STOP", index: 0 }],
usageMetadata,
modelVersion: model,
})}\n\n`,
);
if (isPromiseLike(result)) await result;
}
export function googleGenerateContentResponse(params: {
model: string;
responseParts: GoogleResponsePart[];
promptTokens: number;
candidateTokens: number;
upstreamEmpty: boolean;
warning?: unknown;
}) {
const responseObj: Record<string, unknown> = {
candidates: [
{
content: { parts: params.responseParts, role: "model" },
finishReason: "STOP",
index: 0,
},
],
usageMetadata: {
promptTokenCount: params.promptTokens,
candidatesTokenCount: params.candidateTokens,
totalTokenCount: params.promptTokens + params.candidateTokens,
},
modelVersion: params.model,
};
if (params.upstreamEmpty)
responseObj.promptFeedback = {
blockReason: "OTHER",
warning: params.warning,
};
return responseObj;
}
export function googleStreamDonePayload(
model: string,
promptTokens: number,
candidateTokens: number,
streamErr: unknown = null,
) {
const donePayload: Record<string, unknown> = {
candidates: [{ finishReason: "STOP", index: 0 }],
usageMetadata: {
promptTokenCount: promptTokens,
candidatesTokenCount: candidateTokens,
totalTokenCount: promptTokens + candidateTokens,
},
modelVersion: model,
};
if (streamErr)
donePayload.promptFeedback = { warning: streamWarningObject(streamErr) };
return donePayload;
}
|