/** * Schema + answer types. Field names, limits, and descriptions are the LLM * contract — kept verbatim from rpiv-ask-user-question. */ import { type Static, Type } from "typebox"; export const MAX_QUESTIONS = 4; export const MIN_OPTIONS = 2; export const MAX_OPTIONS = 4; export const MAX_HEADER_LENGTH = 16; export const MAX_LABEL_LENGTH = 60; /** Labels the model may not author — the runtime appends its own sentinels. */ export const RESERVED_LABELS = ["Other", "Type something.", "Next"] as const; export const OptionSchema = Type.Object({ label: Type.String({ minLength: 1, maxLength: MAX_LABEL_LENGTH, description: `MAX ${MAX_LABEL_LENGTH} CHARACTERS — hard limit, requests over the limit are rejected. The display text for this option that the user will see and select. Should be concise (1-5 words) and clearly describe the choice.`, }), description: Type.String({ description: "Explanation of what this option means or what will happen if chosen. Useful for providing context about trade-offs or implications.", }), preview: Type.Optional( Type.String({ description: "Optional preview content rendered when this option is focused. Use for mockups, code snippets, or visual comparisons that help users compare options. See the tool description for the expected content format.", }), ), }); export const QuestionSchema = Type.Object({ question: Type.String({ minLength: 1, description: 'The complete question to ask the user. Should be clear, specific, and end with a question mark. Example: "Which library should we use for date formatting?" If multiSelect is true, phrase it accordingly, e.g. "Which features do you want to enable?"', }), header: Type.String({ maxLength: MAX_HEADER_LENGTH, description: `MAX ${MAX_HEADER_LENGTH} CHARACTERS — hard limit, requests over the limit are rejected. Very short chip/tag shown next to the question. Examples: "Auth method", "Library", "Approach".`, }), options: Type.Array(OptionSchema, { minItems: MIN_OPTIONS, maxItems: MAX_OPTIONS, description: "The available choices for this question. Must have 2-4 options. Each option should be a distinct, mutually exclusive choice (unless multiSelect is enabled). The 'Type something.' row is appended automatically — do NOT author it.", }), multiSelect: Type.Optional( Type.Boolean({ default: false, description: "Set to true to allow the user to select multiple options instead of just one. Use when choices are not mutually exclusive.", }), ), }); export const QuestionsSchema = Type.Array(QuestionSchema, { minItems: 1, maxItems: MAX_QUESTIONS, description: "Questions to ask the user (1-4 questions)", }); export const QuestionParamsSchema = Type.Object({ questions: QuestionsSchema, }); export type QuestionData = Static; export type QuestionParams = Static; export interface QuestionAnswer { questionIndex: number; question: string; kind: "option" | "custom" | "multi"; answer: string | null; selected?: string[]; notes?: string; /** Markdown from the chosen option's preview field (single-select only). */ preview?: string; } export type QuestionnaireError = | "no_ui" | "no_questions" | "empty_options" | "too_many_questions" | "duplicate_question" | "duplicate_option_label" | "invalid_label" | "reserved_label" | "preview_on_multiselect"; export interface QuestionnaireResult { answers: QuestionAnswer[]; cancelled: boolean; error?: QuestionnaireError; }