fix(ai): replace generic record checks

This commit is contained in:
Armin Ronacher
2026-07-19 22:21:31 +02:00
parent f1c587dde3
commit 956074697f
3 changed files with 41 additions and 30 deletions
+3 -6
View File
@@ -103,14 +103,11 @@ export class PiMessagesResponseError extends Error {
}
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function parsePiMessagesErrorBody(body: string): PiMessagesErrorBody | undefined {
try {
const parsed = JSON.parse(body) as unknown;
return isRecord(parsed) && isRecord(parsed.error) ? (parsed as PiMessagesErrorBody) : undefined;
const parsed = JSON.parse(body) as PiMessagesErrorBody | null;
const error = parsed?.error;
return parsed && typeof error === "object" && error !== null && !Array.isArray(error) ? parsed : undefined;
} catch {
return undefined;
}
+16 -15
View File
@@ -23,28 +23,29 @@ export type RadiusOAuthCredential = OAuthCredential & {
gatewayConfig?: RadiusGatewayConfig;
};
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isRadiusGatewayModel(value: unknown): value is RadiusGatewayModel {
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
const model = value as Partial<RadiusGatewayModel>;
return (
isRecord(value) &&
typeof value.id === "string" &&
typeof value.name === "string" &&
typeof value.reasoning === "boolean" &&
Array.isArray(value.input) &&
isRecord(value.cost) &&
typeof value.contextWindow === "number" &&
typeof value.maxTokens === "number"
typeof model.id === "string" &&
typeof model.name === "string" &&
typeof model.reasoning === "boolean" &&
Array.isArray(model.input) &&
typeof model.cost === "object" &&
model.cost !== null &&
!Array.isArray(model.cost) &&
typeof model.contextWindow === "number" &&
typeof model.maxTokens === "number"
);
}
function sanitizeRadiusGatewayConfig(config: unknown): RadiusGatewayConfig | undefined {
if (!isRecord(config) || typeof config.baseUrl !== "string" || !Array.isArray(config.models)) return undefined;
if (typeof config !== "object" || config === null || Array.isArray(config)) return undefined;
const { baseUrl, models } = config as Partial<RadiusGatewayConfig>;
if (typeof baseUrl !== "string" || !Array.isArray(models)) return undefined;
return {
baseUrl: config.baseUrl,
models: config.models.filter(isRadiusGatewayModel).map((model) => ({ ...model })),
baseUrl,
models: models.filter(isRadiusGatewayModel).map((model) => ({ ...model })),
};
}