// A value a schema admits, for a pack that must answer structured output without running a model (a generative pack's // labeled stub: the Claude API's JSON-schema output format, Gemini's responseSchema). The schema is JSON Schema, or the // OpenAPI subset of it some vendors take (Gemini's upper-case type names, OBJECT and STRING, read as JSON Schema's). The // value is the first the schema allows: an enum's first member, a const, the first of anyOf/oneOf, every property of an // object, one item of an array, a number's minimum (else 0), false, and for a string a labeled text (a date-time, an // email or a URI in its format). Importing it does nothing. type Row = Record; export function sampleFromSchema(schema: unknown, name = 'value'): unknown { const s = (schema && typeof schema === 'object' ? schema : {}) as Row; if (Array.isArray(s.enum) && s.enum.length) return s.enum[0]; if (s.const !== undefined) return s.const; for (const k of ['anyOf', 'oneOf', 'allOf']) if (Array.isArray(s[k]) && (s[k] as unknown[]).length) return sampleFromSchema((s[k] as unknown[])[0], name); const raw = Array.isArray(s.type) ? (s.type as string[]).find((t) => String(t).toLowerCase() !== 'null') : s.type; const type = typeof raw === 'string' ? raw.toLowerCase() : s.properties ? 'object' : s.items ? 'array' : undefined; switch (type) { case 'object': return Object.fromEntries(Object.entries((s.properties as Row | undefined) ?? {}).map(([k, v]) => [k, sampleFromSchema(v, k)])); case 'array': return [sampleFromSchema(s.items, name)]; case 'integer': case 'number': return typeof s.minimum === 'number' ? s.minimum : 0; case 'boolean': return false; case 'null': return null; default: return s.format === 'date-time' ? '2026-01-01T00:00:00Z' : s.format === 'email' ? 'twin@example.com' : s.format === 'uri' ? 'https://example.com/' : `twin-stub ${name}`; } }