| 5 | * including handling of custom choice nodes (attrs.options/selectedOption). |
| 6 | */ |
| 7 | export function inputFormatter(content: JSONContent): string { |
| 8 | let result = ''; |
| 9 | |
| 10 | function walk(node: JSONContent) { |
| 11 | const anyNode = node as any; |
| 12 | |
| 13 | // Extract text nodes |
| 14 | if (typeof anyNode.text === 'string') { |
| 15 | result += anyNode.text; |
| 16 | } |
| 17 | |
| 18 | // Handle custom 'choice' nodes |
| 19 | if (anyNode.type === 'choice') { |
| 20 | const attrs = anyNode.attrs || {}; |
| 21 | const options = Array.isArray(attrs.options) ? attrs.options : []; |
| 22 | const selected = attrs.selectedOption || options[0] || ''; |
| 23 | if (typeof selected === 'string') { |
| 24 | result += selected; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // Recurse into children |
| 29 | if (Array.isArray(anyNode.content)) { |
| 30 | anyNode.content.forEach((child: JSONContent) => walk(child)); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | walk(content); |
| 35 | return result.trim(); |
| 36 | } |