* Extract a clean title from potentially malformed input: a plain string, * a JSON object string ('{"title":"…"}'), or an object with a title field. * Ported from the extension's taskMetadata sanitizeTitle.
(raw: unknown)
| 166 | * Ported from the extension's taskMetadata sanitizeTitle. |
| 167 | */ |
| 168 | function sanitizeTitle(raw: unknown): string | undefined { |
| 169 | if (raw == null) return undefined; |
| 170 | if (typeof raw === "string") { |
| 171 | const trimmed = raw.trim(); |
| 172 | if (!trimmed) return undefined; |
| 173 | if (trimmed.startsWith("{") || trimmed.startsWith("[")) { |
| 174 | try { |
| 175 | const extracted = sanitizeTitle(JSON.parse(trimmed)); |
| 176 | if (extracted) return extracted; |
| 177 | } catch { |
| 178 | // not JSON, use as plain string |
| 179 | } |
| 180 | } |
| 181 | return trimmed; |
| 182 | } |
| 183 | if (typeof raw === "object") { |
| 184 | const maybe = (raw as Record<string, unknown>)["title"]; |
| 185 | if (maybe != null) return sanitizeTitle(maybe); |
| 186 | } |
| 187 | return undefined; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Fetch the backend-generated task title (GET /axoncode/meta/<taskId>). |