When updating code, optimize for local clarity and correctness, not “cleanup” by indirection.
Apply these rules: 1) Prefer locality over forced DRY
2) Improve readability with explicit intent
translatedBody = body), rather than relying on implicit state.3) Type-safety as a style baseline
typeof/guards already narrow.any for new helpers; use focused interfaces and Partial<...> for recovery/state update parameters.4) Preserve formatting in content transforms
Example (readability + type narrowing):
// Prefer guard-driven simplification over redundant casts
if (aliases && parsed.model) {
const directTarget = aliases[parsed.model];
if (typeof directTarget === "string" && directTarget.includes("/")) {
const slashIdx = directTarget.indexOf("/');
if (slashIdx !== -1) {
const providerPart = directTarget.slice(0, slashIdx);
const modelPart = directTarget.slice(slashIdx + 1);
// ...
}
}
}
Example (format-preserving cleaning):
function cleanResponse(text: string, strip = true): string {
let t = text;
// Remove only known unwanted tags/markers
t = t.replace(/<[?]xml[^?]*[?]>/g, "");
// Intentionally DO NOT collapse spaces/newlines, to preserve markdown/code formatting
return strip ? t.trim() : t;
}
Enter the URL of a public GitHub repository