Skip to content
Français

defineJsonResponse

import { defineJsonResponse } from "@elie-laloum/outpost";

Declare a response read from the last complete <tag>…</tag> pair, parsed as JSON, optionally inside a Markdown code fence, then checked by schema. A missing tag, invalid JSON, schema issues or a thrown parse error fail with ResponseError; an invalid tag or repairs fails with code configuration.

Complete example and detailed rules.

  • optionsRequired
    JsonResponseOptions<T>
    Tag name, schema or parsing function, and the number of repair turns allowed after an invalid answer.
  • options.tagRequired
    string
    Tag name without angle brackets: a letter followed by letters, digits, _ or -. Another form fails with code configuration.
  • options.schemaRequired
    StandardValidator<T> | ((input: unknown) => T | Promise<T>)
    Standard Schema validator, such as Zod or Valibot, or a function that receives the parsed JSON as unknown and returns the typed value. Reported issues or a thrown error reject the answer.
  • options.repairsOptional
    number | undefined
    Correction turns allowed after an invalid answer, default 0; must be a nonnegative integer. Above 0, the agent must be able to resume its conversation.

ResponseSpec<T>

export declare function defineJsonResponse<T>(
  options: JsonResponseOptions<T>,
): ResponseSpec<T>;