refactor: dedupe preview streaming helpers

This commit is contained in:
Peter Steinberger
2026-04-06 18:12:48 +01:00
parent 8ae6cf32bb
commit 673878188d
3 changed files with 47 additions and 93 deletions

View File

@@ -21,6 +21,7 @@ export type {
type StreamingCompatEntry = {
streaming?: unknown;
streamMode?: unknown;
chunkMode?: unknown;
blockStreaming?: unknown;
draftChunk?: unknown;
@@ -42,6 +43,27 @@ function asBoolean(value: unknown): boolean | undefined {
return typeof value === "boolean" ? value : undefined;
}
function normalizeStreamingMode(value: unknown): string | null {
if (typeof value !== "string") {
return null;
}
const normalized = value.trim().toLowerCase();
return normalized || null;
}
function parsePreviewStreamingMode(value: unknown): "off" | "partial" | "block" | null {
const normalized = normalizeStreamingMode(value);
if (
normalized === "off" ||
normalized === "partial" ||
normalized === "block" ||
normalized === "progress"
) {
return normalized === "progress" ? "partial" : normalized;
}
return null;
}
function asBlockStreamingCoalesceConfig(value: unknown): BlockStreamingCoalesceConfig | undefined {
return asObjectRecord(value) as BlockStreamingCoalesceConfig | undefined;
}
@@ -99,3 +121,24 @@ export function resolveChannelStreamingNativeTransport(
const config = getChannelStreamingConfigObject(entry);
return asBoolean(config?.nativeTransport) ?? asBoolean(entry?.nativeStreaming);
}
export function resolveChannelPreviewStreamMode(
entry: StreamingCompatEntry | null | undefined,
defaultMode: "off" | "partial",
): "off" | "partial" | "block" {
const parsedStreaming = parsePreviewStreamingMode(
getChannelStreamingConfigObject(entry)?.mode ?? entry?.streaming,
);
if (parsedStreaming) {
return parsedStreaming;
}
const legacy = parsePreviewStreamingMode(entry?.streamMode);
if (legacy) {
return legacy;
}
if (typeof entry?.streaming === "boolean") {
return entry.streaming ? "partial" : "off";
}
return defaultMode;
}