(content: string)
| 154 | * Returns null if the content is not suitable for text formatting. |
| 155 | */ |
| 156 | export function formatToolResultAsText(content: string): string | null { |
| 157 | let parsed: unknown |
| 158 | try { |
| 159 | parsed = JSON.parse(content) |
| 160 | } catch { |
| 161 | return null |
| 162 | } |
| 163 | |
| 164 | if (!parsed || typeof parsed !== 'object') return null |
| 165 | const obj = parsed as Record<string, unknown> |
| 166 | |
| 167 | // If there's a messages array with senderName/content/timestamp, format it |
| 168 | if (Array.isArray(obj.messages) && obj.messages.length > 0) { |
| 169 | const msgs = obj.messages as FormattableMessage[] |
| 170 | if (msgs[0] && 'senderName' in msgs[0] && 'timestamp' in msgs[0]) { |
| 171 | return formatObjectWithMessages(obj, msgs) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // If there are ranking/keywords arrays, format as numbered list |
| 176 | if (Array.isArray(obj.ranking) || Array.isArray(obj.keywords)) { |
| 177 | return formatArrayResult(obj) |
| 178 | } |
| 179 | |
| 180 | // If there are sessions, format them compactly |
| 181 | if (Array.isArray(obj.sessions)) { |
| 182 | return formatSessionsList(obj) |
| 183 | } |
| 184 | |
| 185 | return null |
| 186 | } |
| 187 | |
| 188 | function formatObjectWithMessages(obj: Record<string, unknown>, msgs: FormattableMessage[]): string { |
| 189 | const lines: string[] = [] |
no test coverage detected