|
8 | 8 | buildPersistedAssistantMessage, |
9 | 9 | buildPersistedUserMessage, |
10 | 10 | normalizeMessage, |
| 11 | + type PersistedMessage, |
| 12 | + stripToolResultOutput, |
11 | 13 | } from './persisted-message' |
12 | 14 |
|
13 | 15 | describe('persisted-message', () => { |
@@ -234,3 +236,137 @@ describe('persisted-message', () => { |
234 | 236 | expect(msg.contexts).toBeUndefined() |
235 | 237 | }) |
236 | 238 | }) |
| 239 | + |
| 240 | +describe('stripToolResultOutput', () => { |
| 241 | + it('drops result.output but keeps success and error', () => { |
| 242 | + const message: PersistedMessage = { |
| 243 | + id: 'msg-1', |
| 244 | + role: 'assistant', |
| 245 | + content: '', |
| 246 | + timestamp: '2026-01-01T00:00:00.000Z', |
| 247 | + contentBlocks: [ |
| 248 | + { |
| 249 | + type: 'tool', |
| 250 | + phase: 'call', |
| 251 | + toolCall: { |
| 252 | + id: 'tool-1', |
| 253 | + name: 'get_workflow_logs', |
| 254 | + state: 'error', |
| 255 | + params: { workflowId: 'wf-1' }, |
| 256 | + display: { title: 'Reading logs' }, |
| 257 | + result: { success: false, output: { huge: 'x'.repeat(1000) }, error: 'boom' }, |
| 258 | + }, |
| 259 | + }, |
| 260 | + ], |
| 261 | + } |
| 262 | + |
| 263 | + const stripped = stripToolResultOutput(message) |
| 264 | + |
| 265 | + expect(stripped.contentBlocks?.[0].toolCall).toEqual({ |
| 266 | + id: 'tool-1', |
| 267 | + name: 'get_workflow_logs', |
| 268 | + state: 'error', |
| 269 | + params: { workflowId: 'wf-1' }, |
| 270 | + display: { title: 'Reading logs' }, |
| 271 | + result: { success: false, error: 'boom' }, |
| 272 | + }) |
| 273 | + expect(message.contentBlocks?.[0].toolCall?.result).toHaveProperty('output') |
| 274 | + }) |
| 275 | + |
| 276 | + it('omits error when the original result had none', () => { |
| 277 | + const message: PersistedMessage = { |
| 278 | + id: 'msg-1', |
| 279 | + role: 'assistant', |
| 280 | + content: '', |
| 281 | + timestamp: '2026-01-01T00:00:00.000Z', |
| 282 | + contentBlocks: [ |
| 283 | + { |
| 284 | + type: 'tool', |
| 285 | + phase: 'call', |
| 286 | + toolCall: { |
| 287 | + id: 't', |
| 288 | + name: 'read', |
| 289 | + state: 'success', |
| 290 | + result: { success: true, output: [1, 2, 3] }, |
| 291 | + }, |
| 292 | + }, |
| 293 | + ], |
| 294 | + } |
| 295 | + |
| 296 | + expect(stripToolResultOutput(message).contentBlocks?.[0].toolCall?.result).toEqual({ |
| 297 | + success: true, |
| 298 | + }) |
| 299 | + }) |
| 300 | + |
| 301 | + it('returns the same reference when there is nothing to strip', () => { |
| 302 | + const noBlocks: PersistedMessage = { |
| 303 | + id: 'u', |
| 304 | + role: 'user', |
| 305 | + content: 'hi', |
| 306 | + timestamp: '2026-01-01T00:00:00.000Z', |
| 307 | + } |
| 308 | + expect(stripToolResultOutput(noBlocks)).toBe(noBlocks) |
| 309 | + |
| 310 | + const noOutput: PersistedMessage = { |
| 311 | + id: 'msg', |
| 312 | + role: 'assistant', |
| 313 | + content: 'done', |
| 314 | + timestamp: '2026-01-01T00:00:00.000Z', |
| 315 | + contentBlocks: [ |
| 316 | + { type: 'text', channel: 'assistant', content: 'done' }, |
| 317 | + { type: 'tool', phase: 'call', toolCall: { id: 't', name: 'read', state: 'pending' } }, |
| 318 | + { |
| 319 | + type: 'tool', |
| 320 | + phase: 'call', |
| 321 | + toolCall: { |
| 322 | + id: 't2', |
| 323 | + name: 'read', |
| 324 | + state: 'error', |
| 325 | + result: { success: false, error: 'x' }, |
| 326 | + }, |
| 327 | + }, |
| 328 | + ], |
| 329 | + } |
| 330 | + expect(stripToolResultOutput(noOutput)).toBe(noOutput) |
| 331 | + }) |
| 332 | + |
| 333 | + it('strips every tool block while leaving text/thinking blocks intact', () => { |
| 334 | + const message: PersistedMessage = { |
| 335 | + id: 'msg', |
| 336 | + role: 'assistant', |
| 337 | + content: '', |
| 338 | + timestamp: '2026-01-01T00:00:00.000Z', |
| 339 | + contentBlocks: [ |
| 340 | + { type: 'text', channel: 'thinking', content: 'hmm' }, |
| 341 | + { |
| 342 | + type: 'tool', |
| 343 | + phase: 'call', |
| 344 | + toolCall: { |
| 345 | + id: 'a', |
| 346 | + name: 'run_workflow', |
| 347 | + state: 'success', |
| 348 | + result: { success: true, output: { big: 1 } }, |
| 349 | + }, |
| 350 | + }, |
| 351 | + { type: 'text', channel: 'assistant', content: 'answer' }, |
| 352 | + { |
| 353 | + type: 'tool', |
| 354 | + phase: 'call', |
| 355 | + toolCall: { |
| 356 | + id: 'b', |
| 357 | + name: 'read', |
| 358 | + state: 'success', |
| 359 | + result: { success: true, output: 'file contents' }, |
| 360 | + }, |
| 361 | + }, |
| 362 | + ], |
| 363 | + } |
| 364 | + |
| 365 | + const blocks = stripToolResultOutput(message).contentBlocks ?? [] |
| 366 | + expect(blocks[0]).toEqual({ type: 'text', channel: 'thinking', content: 'hmm' }) |
| 367 | + expect(blocks[1].toolCall?.result).toEqual({ success: true }) |
| 368 | + expect(blocks[2]).toEqual({ type: 'text', channel: 'assistant', content: 'answer' }) |
| 369 | + expect(blocks[3].toolCall?.result).toEqual({ success: true }) |
| 370 | + expect(JSON.stringify(blocks)).not.toContain('file contents') |
| 371 | + }) |
| 372 | +}) |
0 commit comments