Skip to content

Commit 9e40cbe

Browse files
authored
Allow destructuring output and errorText on ToolUIPart (#7140)
## Summary This way you can accomplish the following use case, rendering a component for a tool type safely: ```tsx export function EditorToolPreview({ input, state, toolCallId, output, }: Extract<MyToolPart, { type: 'tool-str_replace_editor' }>) { return <pre>{JSON.stringify(output, null, 2)}</pre>; } function ChatMessage({ message }: { message: MyUIMessage }) { return ( <ChatAssistantMessage className=""> {message.parts.map((part: any, index: number) => { if (part.type === 'text') { return <Markdown key={index} markdown={part.text} />; } if (part.type === 'tool-str_replace_editor') { return <EditorToolPreview key={index} {...part} />; } return null; })} </ChatAssistantMessage> ); } ``` Without this PR you can't destructure output directly in the component props because you would need to discriminate on `state` first: ```tsx export function EditorToolPreview( props: Extract<MyToolPart, { type: 'tool-str_replace_editor' }>, ) { if (props.state === 'output-available') { return <pre>{JSON.stringify(props.output, null, 2)}</pre>; } return null; } ``` This means changing a lot of code to migrate to v5 to use props instead of destructuring
1 parent 7dd709b commit 9e40cbe

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

‎.changeset/cuddly-phones-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
Allow destructuring output and errorText on `ToolUIPart` type

‎packages/ai/src/ui/ui-messages.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ export type DataUIPart<DATA_TYPES extends UIDataTypes> = ValueOf<{
169169
data: DATA_TYPES[NAME];
170170
};
171171
}>;
172-
173172
export type ToolUIPart<TOOLS extends UITools = UITools> = ValueOf<{
174173
[NAME in keyof TOOLS & string]: {
175174
type: `tool-${NAME}`;
@@ -179,21 +178,27 @@ export type ToolUIPart<TOOLS extends UITools = UITools> = ValueOf<{
179178
state: 'input-streaming';
180179
input: DeepPartial<TOOLS[NAME]['input']>;
181180
providerExecuted?: boolean;
181+
output?: never;
182+
errorText?: never;
182183
}
183184
| {
184185
state: 'input-available';
185186
input: TOOLS[NAME]['input'];
186187
providerExecuted?: boolean;
188+
output?: never;
189+
errorText?: never;
187190
}
188191
| {
189192
state: 'output-available';
190193
input: TOOLS[NAME]['input'];
191194
output: TOOLS[NAME]['output'];
195+
errorText?: never;
192196
providerExecuted?: boolean;
193197
}
194198
| {
195199
state: 'output-error';
196200
input: TOOLS[NAME]['input'];
201+
output?: never;
197202
errorText: string;
198203
providerExecuted?: boolean;
199204
}

0 commit comments

Comments
 (0)