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

Support abortion of tool execution #3272

Closed
wong2 opened this issue Oct 16, 2024 · 7 comments
Closed

Support abortion of tool execution #3272

wong2 opened this issue Oct 16, 2024 · 7 comments
Labels
ai/core enhancement New feature or request

Comments

@wong2
Copy link
Contributor

wong2 commented Oct 16, 2024

Feature Description

Let's say I have a web search tool, it sends out an HTTP request to do a web search when it's executed.

const tools = {
  web_search: tool({
    .......
  }),
}

streamText({
  model: openai('gpt-4o'),
  prompt: '...',
  tools,
  abortSignal: req.signal,
})

Now I really wish I had a way to stop search requests in web_search when an abort happens, but it seems like there's no way to do it right now.

@lgrammel lgrammel added enhancement New feature or request ai/core labels Oct 16, 2024
@lgrammel
Copy link
Collaborator

Would you want to use the same abort signal that we use for the main request? we could add it as an optional parameter to the tools, then you could use it in the tool implementation. alternatively you can use the abort signal from the closure when you create the tool

@wong2
Copy link
Contributor Author

wong2 commented Oct 16, 2024

I think using the same abort signal I passed to streamText is enough.

@wong2
Copy link
Contributor Author

wong2 commented Oct 16, 2024

Yeah, something like this would be cool :

execute: async ({ query }, { abortSignal }) => {
   ...  
}

@lgrammel
Copy link
Collaborator

#3277

@wong2
Copy link
Contributor Author

wong2 commented Oct 17, 2024

Thanks, good job as always!

@wong2
Copy link
Contributor Author

wong2 commented Oct 17, 2024

I've gave it a try, and get the following type error:

execute: async ({ query }, { abortSignal }) => {
  const searchResults = await searchWeb(query, numResults, abortSignal)
  return { searchResults }    
}
    Type '({ query }: { query: string; }, { abortSignal }: { abortSignal?: AbortSignal | undefined; }) => Promise<SpecificErrorInstance<[], { query: string; }, CustomClass, AggregateErrors, unknown> | { ...; }>' is not assignable to type '((args: { query: string; }, options: { abortSignal?: AbortSignal | undefined; }) => PromiseLike<SpecificErrorInstance<[], { query: string; }, CustomClass, AggregateErrors, unknown> | { ...; }>) & ((args: { ...; }) => PromiseLike<...>)'.
      Type '({ query }: { query: string; }, { abortSignal }: { abortSignal?: AbortSignal | undefined; }) => Promise<SpecificErrorInstance<[], { query: string; }, CustomClass, AggregateErrors, unknown> | { ...; }>' is not assignable to type '(args: { query: string; }) => PromiseLike<SpecificErrorInstance<[], { query: string; }, CustomClass, AggregateErrors, unknown> | { searchResults: WebSearchResult[]; }>'.
        Target signature provides too few arguments. Expected 2 or more, but got 1.
  Overload 2 of 2, '(tool: CoreTool<ZodObject<{ query: ZodString; }, "strip", ZodTypeAny, { query: string; }, { query: string; }>, SpecificErrorInstance<[], { query: string; }, CustomClass, AggregateErrors, unknown> | { ...; }> & { ...; }): CoreTool<...> & { ...; }', gave the following error.
    Type '({ query }: { query: string; }, { abortSignal }: { abortSignal?: AbortSignal | undefined; }) => Promise<SpecificErrorInstance<[], { query: string; }, CustomClass, AggregateErrors, unknown> | { ...; }>' is not assignable to type 'undefined'.ts(2769)

@lgrammel
Copy link
Collaborator

#3285

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/core enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants