π File detail
utils/semanticBoolean.ts
π§© .tsπ 30 linesπΎ 1,167 bytesπ text
β Back to All Filesπ― Use case
This file lives under βutils/β, which covers cross-cutting helpers (shell, tempfiles, settings, messages, process input, β¦). On the API surface it exposes semanticBoolean β mainly functions, hooks, or classes. Dependencies touch schema validation.
Generated from folder role, exports, dependency roots, and inline comments β not hand-reviewed for every path.
π§ Inline summary
import { z } from 'zod/v4' /** * Boolean that also accepts the string literals "true"/"false". *
π€ Exports (heuristic)
semanticBoolean
π External import roots
Package roots from from "β¦" (relative paths omitted).
zod
π₯οΈ Source preview
import { z } from 'zod/v4'
/**
* Boolean that also accepts the string literals "true"/"false".
*
* Tool inputs arrive as model-generated JSON. The model occasionally quotes
* booleans β `"replace_all":"false"` instead of `"replace_all":false` β and
* z.boolean() rejects that with a type error. z.coerce.boolean() is the wrong
* fix: it uses JS truthiness, so "false" β true.
*
* z.preprocess emits {"type":"boolean"} to the API schema, so the model is
* still told this is a boolean β the string tolerance is invisible client-side
* coercion, not an advertised input shape.
*
* .optional()/.default() go INSIDE (on the inner schema), not chained after:
* chaining them onto ZodPipe widens z.output<> to unknown in Zod v4.
*
* semanticBoolean() β boolean
* semanticBoolean(z.boolean().optional()) β boolean | undefined
* semanticBoolean(z.boolean().default(false)) β boolean
*/
export function semanticBoolean<T extends z.ZodType>(
inner: T = z.boolean() as unknown as T,
) {
return z.preprocess(
(v: unknown) => (v === 'true' ? true : v === 'false' ? false : v),
inner,
)
}