refactor: dedupe reader helpers

This commit is contained in:
Peter Steinberger
2026-04-07 04:31:30 +01:00
parent d9fbfa268f
commit 2f115bc645
10 changed files with 63 additions and 60 deletions

View File

@@ -1,10 +1,13 @@
import { readStringValue } from "./string-coerce.js";
export function extractFirstTextBlock(message: unknown): string | undefined {
if (!message || typeof message !== "object") {
return undefined;
}
const content = (message as { content?: unknown }).content;
if (typeof content === "string") {
return content;
const inline = readStringValue(content);
if (inline !== undefined) {
return inline;
}
if (!Array.isArray(content) || content.length === 0) {
return undefined;
@@ -13,8 +16,7 @@ export function extractFirstTextBlock(message: unknown): string | undefined {
if (!first || typeof first !== "object") {
return undefined;
}
const text = (first as { text?: unknown }).text;
return typeof text === "string" ? text : undefined;
return readStringValue((first as { text?: unknown }).text);
}
export type AssistantPhase = "commentary" | "final_answer";

View File

@@ -15,6 +15,18 @@ export function normalizeTrimmedStringList(value: unknown): string[] {
);
}
export function normalizeOptionalTrimmedStringList(value: unknown): string[] | undefined {
const normalized = normalizeTrimmedStringList(value);
return normalized.length > 0 ? normalized : undefined;
}
export function normalizeArrayBackedTrimmedStringList(value: unknown): string[] | undefined {
if (!Array.isArray(value)) {
return undefined;
}
return normalizeTrimmedStringList(value);
}
export function normalizeSingleOrTrimmedStringList(value: unknown): string[] {
if (Array.isArray(value)) {
return normalizeTrimmedStringList(value);