Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-tool-approval-signature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---\
'ai': patch\
---\
\
fix(ai): preserve tool approval signature when transitioning to approval-responded
50 changes: 50 additions & 0 deletions packages/ai/src/ui/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,56 @@ describe('Chat', () => {
});

describe('addToolApprovalResponse', () => {
describe('approved with signature', () => {
let chat: TestChat;

beforeEach(async () => {
chat = new TestChat({
id: '123',
generateId: mockId({ prefix: 'newid' }),
transport: new DefaultChatTransport({
api: 'http://localhost:3000/api/chat',
}),
messages: [
{
id: 'id-0',
role: 'user',
parts: [{ text: 'What is the weather in Tokyo?', type: 'text' }],
},
{
id: 'id-1',
role: 'assistant',
parts: [
{ type: 'step-start' },
{
type: 'tool-weather',
toolCallId: 'call-1',
state: 'approval-requested',
input: { city: 'Tokyo' },
approval: { id: 'approval-1', signature: 'test-signature' },
},
],
},
],
});

await chat.addToolApprovalResponse({
id: 'approval-1',
approved: true,
});
});

it('should preserve the signature in the approval response', () => {
expect(chat.messages[1].parts[1]).toMatchObject({
approval: {
id: 'approval-1',
approved: true,
signature: 'test-signature',
},
});
});
});

describe('approved', () => {
let chat: TestChat;

Expand Down
9 changes: 8 additions & 1 deletion packages/ai/src/ui/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,14 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
? {
...part,
state: 'approval-responded',
approval: { id, approved, reason },
approval: {
id,
approved,
reason,
...(part.approval.signature != null
? { signature: part.approval.signature }
: {}),
},
}
: part;

Expand Down
69 changes: 69 additions & 0 deletions packages/ai/src/ui/process-ui-message-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7757,6 +7757,75 @@ describe('processUIMessageStream', () => {
});
});

describe('automatic tool approval signature preservation', () => {
beforeEach(async () => {
const stream = createUIMessageStream([
{ type: 'start' },
{ type: 'start-step' },
{
input: {
value: 'value',
},
toolCallId: 'call-1',
toolName: 'tool1',
type: 'tool-input-available',
},
{
approvalId: 'id-1',
isAutomatic: true,
signature: 'test-signature',
toolCallId: 'call-1',
type: 'tool-approval-request',
},
{
approvalId: 'id-1',
approved: true,
type: 'tool-approval-response',
},
{ type: 'finish-step' },
{ type: 'finish' },
]);

state = createStreamingUIMessageState({
messageId: 'msg-123',
lastMessage: undefined,
});

await consumeStream({
stream: processUIMessageStream({
stream,
runUpdateMessageJob,
onError: error => {
throw error;
},
}),
});
});

it('should keep signature through approval response', () => {
expect(writeCalls.map(call => call.message.parts[1])).toMatchObject([
{}, // tool-input-available
{
approval: {
id: 'id-1',
isAutomatic: true,
signature: 'test-signature',
},
state: 'approval-requested',
},
{
approval: {
id: 'id-1',
approved: true,
isAutomatic: true,
signature: 'test-signature',
},
state: 'approval-responded',
},
]);
});
});

describe('automatic tool approval denial (static tool)', () => {
beforeEach(async () => {
const stream = createUIMessageStream([
Expand Down
3 changes: 3 additions & 0 deletions packages/ai/src/ui/process-ui-message-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,9 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({
approved: chunk.approved,
...(chunk.reason != null ? { reason: chunk.reason } : {}),
...(approval.isAutomatic === true ? { isAutomatic: true } : {}),
...(approval.signature != null
? { signature: approval.signature }
: {}),
};
if (chunk.providerExecuted != null) {
toolInvocation.providerExecuted = chunk.providerExecuted;
Expand Down