Resolve a question's translated text + its options. The `*_code` / * `text_params` keys are i18n inputs only — they're not preserved on the * normalized output. `responseType` defaults to `single_choice` when * options exist, else `free_text` (mirrors the backend default).
(raw: any)
| 34 | * normalized output. `responseType` defaults to `single_choice` when |
| 35 | * options exist, else `free_text` (mirrors the backend default). */ |
| 36 | function normalizeQuestion(raw: any): ClarificationQuestion | null { |
| 37 | if (!raw || typeof raw !== 'object') return null; |
| 38 | |
| 39 | const text = translateBackend( |
| 40 | String(raw.text || ''), |
| 41 | typeof raw.text_code === 'string' ? raw.text_code : undefined, |
| 42 | raw.text_params && typeof raw.text_params === 'object' ? raw.text_params : undefined, |
| 43 | ).trim(); |
| 44 | if (!text) return null; |
| 45 | |
| 46 | const rawOptions = Array.isArray(raw.options) ? raw.options : []; |
| 47 | const options = rawOptions |
| 48 | .map((option: any) => normalizeOption(option)) |
| 49 | .filter((option: ClarificationOption | null): option is ClarificationOption => option !== null); |
| 50 | |
| 51 | const responseType = raw.responseType === 'free_text' || raw.responseType === 'single_choice' |
| 52 | ? raw.responseType |
| 53 | : (options.length > 0 ? 'single_choice' : 'free_text'); |
| 54 | |
| 55 | return { |
| 56 | text, |
| 57 | responseType, |
| 58 | ...(options.length > 0 ? { options } : {}), |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | export function normalizeClarifyEvent(result: any): NormalizedClarification { |
| 63 | const rawQuestions = Array.isArray(result?.questions) ? result.questions : []; |
no test coverage detected