(
responseFormatValue: any,
blockId: string,
options?: { allowReferences?: boolean }
)
| 56 | * Handles both string and object formats |
| 57 | */ |
| 58 | export function parseResponseFormatSafely( |
| 59 | responseFormatValue: any, |
| 60 | blockId: string, |
| 61 | options?: { allowReferences?: boolean } |
| 62 | ): any { |
| 63 | if (!responseFormatValue) { |
| 64 | return null |
| 65 | } |
| 66 | |
| 67 | const allowReferences = options?.allowReferences ?? false |
| 68 | |
| 69 | try { |
| 70 | if (typeof responseFormatValue === 'string') { |
| 71 | const trimmedValue = responseFormatValue.trim() |
| 72 | if (trimmedValue === '') return null |
| 73 | if (allowReferences && trimmedValue.startsWith('<') && trimmedValue.includes('>')) { |
| 74 | return trimmedValue |
| 75 | } |
| 76 | return JSON.parse(trimmedValue) |
| 77 | } |
| 78 | return responseFormatValue |
| 79 | } catch (error) { |
| 80 | logger.warn(`Failed to parse response format for block ${blockId}:`, error) |
| 81 | return null |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Extract field values from a parsed JSON object based on selected output paths |
no test coverage detected