All right, let's try it out. We're going to send an email to bob@example.com saying hello. It comes back actually with a bit of clarifying information.
What would you like the subject of the email to be?
Subject is hello.
It's still really clarifying with me. That's kind of funny. Yes, that sounds right. And now down here, I have a bit of UI that I can use to review the email. We've got hello in the subject here and the body is hello.
Apologies for the terrible orange on blue AI. I did get AI to code this for me. Let's try a cancel here and see what happens. It says:
I'm sorry, I wasn't able to send the email.
So let's do a try again here and see what happens. All right it tries calling the tool again. Let's try sending it this time and now it says:
Email sent to bob@example.com.
In my terminal we can see that it has:
Sending email to bob@example.com
Our solution worked really nicely. This needsApproval: true boolean is kind of clever actually.
const tools = {sendEmail: tool({description: 'Send an email to a recipient',inputSchema: z.object({to: z.string().describe('The email address of the recipient'),subject: z.string().describe('The subject of the email'),body: z.string
We can pass a function here instead to just say okay passing in the body, subject, and to for instance.
We might only need approval if it's an unknown recipient or something, or our boss or something. And then we can return, right, either false or true, depending on whether it needs approval or not.
There's lots of potential for playing with stuff in the front end too. We can go into our components.tsx here inside our message. approval-requested is a nice state, but we've also got access to part.state === 'approval-responded'.
That means that we can render the buttons and then render a "this is the option that you chose" UI, which we're not currently doing.
Overall, this gives us lots of stuff to play with when we want to build these human in the loop UIs, and you pretty much always need human in the loop whenever you're doing tool calling, at least whenever you're plugging the agent into dangerous actions.
So I hope your mind is already racing on the kind of things that you could build with this and the kind of UIs this will allow you to put together.
Nice work and I will see you in the next one.