Skip to content

Commit ecb0152

Browse files
aronlgrammel
andcommitted
fix(ai): consider output-error in tool call helper
This commit updates the `lastAssistantMessageIsCompleteWithToolCalls` helper function to also consider tool parts with a state of `output-error` a completed tool call. Co-Authored-By: Lars Grammel <[email protected]>
1 parent 3a10095 commit ecb0152

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

packages/ai/src/ui/last-assistant-message-is-complete-with-tool-calls.test.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { lastAssistantMessageIsCompleteWithToolCalls } from './last-assistant-me
22
import { describe, it, expect } from 'vitest';
33

44
describe('lastAssistantMessageIsCompleteWithToolCalls', () => {
5-
it('should return false if the last step of a multi-step sequency only has text', () => {
5+
it('should return false if the last step of a multi-step sequence only has text', () => {
66
expect(
77
lastAssistantMessageIsCompleteWithToolCalls({
88
messages: [
@@ -61,6 +61,36 @@ describe('lastAssistantMessageIsCompleteWithToolCalls', () => {
6161
).toBe(true);
6262
});
6363

64+
it('should return true when the tool has a output-error state', () => {
65+
expect(
66+
lastAssistantMessageIsCompleteWithToolCalls({
67+
messages: [
68+
{
69+
id: '1',
70+
role: 'assistant',
71+
parts: [
72+
{ type: 'step-start' },
73+
{
74+
type: 'tool-getWeatherInformation',
75+
toolCallId: 'call_6iy0GxZ9R4VDI5MKohXxV48y',
76+
state: 'output-error',
77+
input: {
78+
city: 'New York',
79+
},
80+
errorText: 'Unable to get weather information',
81+
},
82+
{
83+
type: 'text',
84+
text: 'The current weather in New York is windy.',
85+
state: 'done',
86+
},
87+
],
88+
},
89+
],
90+
}),
91+
).toBe(true);
92+
});
93+
6494
it('should return true when dynamic tool call is complete', () => {
6595
expect(
6696
lastAssistantMessageIsCompleteWithToolCalls({
@@ -137,7 +167,7 @@ describe('lastAssistantMessageIsCompleteWithToolCalls', () => {
137167
).toBe(false);
138168
});
139169

140-
it('should return false when dynamic tool call has an error', () => {
170+
it('should return true when dynamic tool call has an error', () => {
141171
expect(
142172
lastAssistantMessageIsCompleteWithToolCalls({
143173
messages: [
@@ -160,7 +190,7 @@ describe('lastAssistantMessageIsCompleteWithToolCalls', () => {
160190
},
161191
],
162192
}),
163-
).toBe(false);
193+
).toBe(true);
164194
});
165195

166196
it('should return true when mixing regular and dynamic tool calls and all are complete', () => {

packages/ai/src/ui/last-assistant-message-is-complete-with-tool-calls.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isToolOrDynamicToolUIPart, UIMessage } from './ui-messages';
1+
import { isToolOrDynamicToolUIPart, type UIMessage } from './ui-messages';
22

33
/**
44
Check if the message is an assistant message with completed tool calls.
@@ -30,6 +30,9 @@ export function lastAssistantMessageIsCompleteWithToolCalls({
3030

3131
return (
3232
lastStepToolInvocations.length > 0 &&
33-
lastStepToolInvocations.every(part => part.state === 'output-available')
33+
lastStepToolInvocations.every(
34+
part =>
35+
part.state === 'output-available' || part.state === 'output-error',
36+
)
3437
);
3538
}

0 commit comments

Comments
 (0)