Skip to content

Commit 9c12f60

Browse files
authored
Merge pull request #121 from mattzcarey/fix/tool-confirmations
fix: tool confirmations
2 parents 23009a4 + e79bca7 commit 9c12f60

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/utils.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ import type {
44
UIMessage,
55
UIMessageStreamWriter,
66
ToolSet,
7-
CoreMessage
7+
ToolCallOptions
88
} from "ai";
99
import { convertToModelMessages, isToolUIPart } from "ai";
1010
import { APPROVAL } from "./shared";
1111

12-
interface ToolContext {
13-
messages: CoreMessage[];
14-
toolCallId: string;
15-
}
16-
1712
function isValidToolName<K extends PropertyKey, T extends object>(
1813
key: K,
1914
obj: T
@@ -35,7 +30,7 @@ export async function processToolCalls<Tools extends ToolSet>({
3530
executions: Record<
3631
string,
3732
// biome-ignore lint/suspicious/noExplicitAny: needs a better type
38-
(args: any, context: ToolContext) => Promise<unknown>
33+
(args: any, context: ToolCallOptions) => Promise<unknown>
3934
>;
4035
}): Promise<UIMessage[]> {
4136
// Process all messages, not just the last one
@@ -55,12 +50,12 @@ export async function processToolCalls<Tools extends ToolSet>({
5550
) as keyof typeof executions;
5651

5752
// Only process tools that require confirmation (are in executions object) and are in 'input-available' state
58-
if (!(toolName in executions) || part.state !== "input-available")
53+
if (!(toolName in executions) || part.state !== "output-available")
5954
return part;
6055

6156
let result: unknown;
6257

63-
if (part.input === APPROVAL.YES) {
58+
if (part.output === APPROVAL.YES) {
6459
// User approved the tool execution
6560
if (!isValidToolName(toolName, executions)) {
6661
return part;
@@ -75,7 +70,7 @@ export async function processToolCalls<Tools extends ToolSet>({
7570
} else {
7671
result = "Error: No execute function found on tool";
7772
}
78-
} else if (part.input === APPROVAL.NO) {
73+
} else if (part.output === APPROVAL.NO) {
7974
result = "Error: User denied access to tool execution";
8075
} else {
8176
// If no approval input yet, leave the part as-is for user interaction
@@ -84,17 +79,14 @@ export async function processToolCalls<Tools extends ToolSet>({
8479

8580
// Forward updated tool result to the client.
8681
dataStream.write({
87-
type: "data-tool-result",
88-
data: {
89-
toolCallId: part.toolCallId,
90-
result: result
91-
}
82+
type: "tool-output-available",
83+
toolCallId: part.toolCallId,
84+
output: result
9285
});
9386

9487
// Return updated tool part with the actual result.
9588
return {
9689
...part,
97-
state: "output-available" as const,
9890
output: result
9991
};
10092
})

0 commit comments

Comments
 (0)