( text: string, key: string, )
| 80 | * Useful for fields that are appended (customTitle, tag, etc.). |
| 81 | */ |
| 82 | export function extractLastJsonStringField( |
| 83 | text: string, |
| 84 | key: string, |
| 85 | ): string | undefined { |
| 86 | const patterns = [`"${key}":"`, `"${key}": "`] |
| 87 | let lastValue: string | undefined |
| 88 | for (const pattern of patterns) { |
| 89 | let searchFrom = 0 |
| 90 | while (true) { |
| 91 | const idx = text.indexOf(pattern, searchFrom) |
| 92 | if (idx < 0) break |
| 93 | |
| 94 | const valueStart = idx + pattern.length |
| 95 | let i = valueStart |
| 96 | while (i < text.length) { |
| 97 | if (text[i] === '\\') { |
| 98 | i += 2 |
| 99 | continue |
| 100 | } |
| 101 | if (text[i] === '"') { |
| 102 | lastValue = unescapeJsonString(text.slice(valueStart, i)) |
| 103 | break |
| 104 | } |
| 105 | i++ |
| 106 | } |
| 107 | searchFrom = i + 1 |
| 108 | } |
| 109 | } |
| 110 | return lastValue |
| 111 | } |
| 112 | |
| 113 | // --------------------------------------------------------------------------- |
| 114 | // First prompt extraction from head chunk |
no test coverage detected