|
1 | | -import { describe, it } from 'vitest'; |
2 | | -import type { DynamicToolUIPart, ToolUIPart } from './ui-messages'; |
| 1 | +import { describe, expectTypeOf, it } from 'vitest'; |
| 2 | +import { |
| 3 | + isToolOutputErrorUIPart, |
| 4 | + type DynamicToolUIPart, |
| 5 | + type ToolOutputErrorUIPart, |
| 6 | + type ToolUIPart, |
| 7 | + type UIMessagePart, |
| 8 | +} from './ui-messages'; |
3 | 9 |
|
4 | 10 | type TestTools = { |
5 | 11 | weather: { |
@@ -86,3 +92,50 @@ describe('UIMessagePart', () => { |
86 | 92 | type _Responded = AssertAssignable<ToolUIPart<TestTools>, RespondedPart>; |
87 | 93 | }); |
88 | 94 | }); |
| 95 | + |
| 96 | +describe('ToolOutputErrorUIPart', () => { |
| 97 | + it('represents static and dynamic tool output errors', () => { |
| 98 | + type StaticPart = { |
| 99 | + type: 'tool-weather'; |
| 100 | + state: 'output-error'; |
| 101 | + toolCallId: 'call-1'; |
| 102 | + input: { city: 'Tokyo' }; |
| 103 | + errorText: 'Weather service unavailable'; |
| 104 | + }; |
| 105 | + type _Static = AssertAssignable< |
| 106 | + ToolOutputErrorUIPart<TestTools>, |
| 107 | + StaticPart |
| 108 | + >; |
| 109 | + |
| 110 | + type DynamicPart = { |
| 111 | + type: 'dynamic-tool'; |
| 112 | + toolName: 'weather'; |
| 113 | + state: 'output-error'; |
| 114 | + toolCallId: 'call-2'; |
| 115 | + input: { city: 'Tokyo' }; |
| 116 | + errorText: 'Weather service unavailable'; |
| 117 | + }; |
| 118 | + type _Dynamic = AssertAssignable< |
| 119 | + ToolOutputErrorUIPart<TestTools>, |
| 120 | + DynamicPart |
| 121 | + >; |
| 122 | + }); |
| 123 | + |
| 124 | + it('narrows tool output errors while preserving static tool input types', () => { |
| 125 | + const part = null as unknown as UIMessagePart< |
| 126 | + Record<string, never>, |
| 127 | + TestTools |
| 128 | + >; |
| 129 | + |
| 130 | + if (isToolOutputErrorUIPart(part)) { |
| 131 | + expectTypeOf(part).toEqualTypeOf<ToolOutputErrorUIPart<TestTools>>(); |
| 132 | + expectTypeOf(part.errorText).toEqualTypeOf<string>(); |
| 133 | + |
| 134 | + if (part.type === 'tool-weather') { |
| 135 | + expectTypeOf(part.input).toEqualTypeOf< |
| 136 | + TestTools['weather']['input'] | undefined |
| 137 | + >(); |
| 138 | + } |
| 139 | + } |
| 140 | + }); |
| 141 | +}); |
0 commit comments