Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(js/ai): Adds "restart" for tool interrupts with flexible resuming. #1693

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

mbleigh
Copy link
Collaborator

@mbleigh mbleigh commented Jan 29, 2025

Implements "restarts" on top of interrupts. Usage:

const sendText = ai.defineTool({
  name: 'sendText',
  description: 'send a text message',
  inputSchema: z.object({to: z.array(z.string()), message: z.string()}),
}, async (input, {context, interrupt, resumed}) => {
  // if not confirmed
  if (!resumed) interrupt({message: "Does this text look good to send?");
  sendTextMessage(context.auth.uid, input);
});

let response = await ai.generate({
  prompt: "Send a text to mom telling her I'll be late.",
  tools: [sendText],
});

if (interrupts.length) {
  const revisedMessage = await confirmOrReviseMessage(interrupts['0]);
  response = await ai.generate({
    resume: {restart: sendText.restart(interrupts[0], true, {replaceInput: revisedMessage});
  });
}

Checklist (if applicable):

@mbleigh mbleigh requested a review from pavelgj January 29, 2025 20:19
@mbleigh mbleigh marked this pull request as ready for review January 30, 2025 05:43
@mbleigh
Copy link
Collaborator Author

mbleigh commented Jan 30, 2025

This is ready for review, the build errors are unrelated to my code.

*/
restart(
interrupt: ToolRequestPart,
resumedMetadata?: any,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restart and resume are used interchangeably... should probably be consistent to avoid confusion. Maybe call it restartMetadata? and restarted?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the tool API (input, {interrupt, resumed}) because (input, {interrupt, restarted}) doesn't really feel as nice...but maybe it's fine?

Another option could be going back to the original confirm and make the argument confirmed. (Then we could have resume + complete + confirm?)

I think restart may still be the most accurate tho...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants