|
| 1 | +import { describe, it, expect } from '@jest/globals'; |
| 2 | +import { PreparedSubagentError } from '@/tools/preparedSubagents'; |
| 3 | +import { StreamLimitExceededError } from '@/llm/streamLimits'; |
| 4 | +import { |
| 5 | + STOPPED_RUN_TOOL_ERROR, |
| 6 | + formatToolErrorContent, |
| 7 | +} from '@/tools/toolErrorContent'; |
| 8 | + |
| 9 | +const maxChars = 1000; |
| 10 | + |
| 11 | +describe('formatToolErrorContent', () => { |
| 12 | + it('instructs the model to stop for LibreChat host abort results on a stopped run', () => { |
| 13 | + const controller = new AbortController(); |
| 14 | + controller.abort(); |
| 15 | + |
| 16 | + expect( |
| 17 | + formatToolErrorContent( |
| 18 | + 'MCP error -32001: AbortError: This operation was aborted', |
| 19 | + maxChars, |
| 20 | + controller.signal |
| 21 | + ) |
| 22 | + ).toBe(STOPPED_RUN_TOOL_ERROR); |
| 23 | + expect( |
| 24 | + formatToolErrorContent( |
| 25 | + 'This operation was aborted', |
| 26 | + maxChars, |
| 27 | + controller.signal |
| 28 | + ) |
| 29 | + ).toBe(STOPPED_RUN_TOOL_ERROR); |
| 30 | + }); |
| 31 | + |
| 32 | + it('preserves ordinary errors, even when they race with a stop', () => { |
| 33 | + const controller = new AbortController(); |
| 34 | + controller.abort(); |
| 35 | + expect( |
| 36 | + formatToolErrorContent('Permission denied', maxChars, controller.signal) |
| 37 | + ).toBe('Error: Permission denied\n Please fix your mistakes.'); |
| 38 | + expect( |
| 39 | + formatToolErrorContent('AbortError: remote timeout', maxChars) |
| 40 | + ).toBe('Error: AbortError: remote timeout\n Please fix your mistakes.'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('recognizes direct-tool abort errors and the owning signal reason', () => { |
| 44 | + const controller = new AbortController(); |
| 45 | + const error = new Error('User cancelled the run'); |
| 46 | + controller.abort(error); |
| 47 | + |
| 48 | + expect( |
| 49 | + formatToolErrorContent(error.message, maxChars, controller.signal, error) |
| 50 | + ).toBe(STOPPED_RUN_TOOL_ERROR); |
| 51 | + |
| 52 | + const other = new Error('Tool cancelled'); |
| 53 | + other.name = 'AbortError'; |
| 54 | + expect( |
| 55 | + formatToolErrorContent(other.message, maxChars, controller.signal, other) |
| 56 | + ).toBe(STOPPED_RUN_TOOL_ERROR); |
| 57 | + }); |
| 58 | + |
| 59 | + it('does not attribute circuit-breaker safety aborts to a user stop', () => { |
| 60 | + const breaker = new AbortController(); |
| 61 | + breaker.abort( |
| 62 | + new StreamLimitExceededError({ |
| 63 | + kind: 'tool_call_args', |
| 64 | + limit: 10, |
| 65 | + observed: 11, |
| 66 | + toolName: 'db_query', |
| 67 | + }) |
| 68 | + ); |
| 69 | + expect( |
| 70 | + formatToolErrorContent('AbortError', maxChars, breaker.signal) |
| 71 | + ).toBe('Error: AbortError\n Please fix your mistakes.'); |
| 72 | + |
| 73 | + const preparation = new AbortController(); |
| 74 | + preparation.abort(new PreparedSubagentError('child run failed')); |
| 75 | + expect( |
| 76 | + formatToolErrorContent('AbortError', maxChars, preparation.signal) |
| 77 | + ).toBe('Error: AbortError\n Please fix your mistakes.'); |
| 78 | + }); |
| 79 | +}); |
0 commit comments