Update createTool to support AI SDK v5 & 6 features like tool approval#6
Conversation
…and context handling - Introduced new types for tool execution and approval functions. - Added support for input examples, strict mode, and tool approval logic. - Updated the function signature to accommodate new properties and ensure compatibility with existing functionality.
commit: |
There was a problem hiding this comment.
Pull request overview
This PR extends the createTool API to support AI SDK v5 & 6 features, including tool approval workflows, custom output conversion, and improved type safety. The changes maintain backwards compatibility while adding significant new functionality.
Changes:
- Added support for tool approval workflows via
needsApprovalproperty (boolean or function) - Added optional
toModelOutputconversion function for custom result formatting - Introduced new properties:
title,strict,inputExamples, andproviderOptions - Refactored type definitions to support both legacy (
args/handler) and new (inputSchema/execute) API with proper type safety - Added utility types (
NeverOptional,ToolInputProperties,ToolOutputPropertiesCtx) for robust type handling
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…se types, update existing type definitions, and deprecate certain fields in favor of new alternatives.
…validators, updating serialization logic and deprecating the 'args' field in favor of 'input'.
|
@copilot I need you to go through all, non generated, references to |
- Updated component schema to use optional mediaType and mimeType - Updated files.ts addFile mutation to accept both mediaType and mimeType - Updated client files.ts to use mediaType when calling component - Updated getFile to support both mediaType and mimeType from database - Updated mapping.ts to return mediaType instead of mimeType - Maintained backwards compatibility throughout Co-authored-by: zboyles <2215540+zboyles@users.noreply.github.com>
Co-authored-by: zboyles <2215540+zboyles@users.noreply.github.com>
- Updated all file part definitions in generated component.ts - Changed mimeType from required to optional - Added mediaType as optional field - Resolved all TypeScript type errors related to mimeType/mediaType Co-authored-by: zboyles <2215540+zboyles@users.noreply.github.com>
Co-authored-by: zboyles <2215540+zboyles@users.noreply.github.com>
…t types Co-authored-by: zboyles <2215540+zboyles@users.noreply.github.com>
Co-authored-by: zboyles <2215540+zboyles@users.noreply.github.com>
…in generated types. Ran `convex codegen --component-dir ./src/component`.
…itions for consistency
Co-authored-by: zboyles <2215540+zboyles@users.noreply.github.com>
…esult output types Co-authored-by: zboyles <2215540+zboyles@users.noreply.github.com>
…plan Fix AI SDK v6 type errors: tool-call input/args union type and tool-result output types
[WIP] Update createTool to support AI SDK v5 and v6 features
|
@copilot Create a plan with one or more approaches to add an ability for additional data to be streamed alongside a streaming response as per Streaming Custom Data. The approaches should focus on best practice and ease of use. Maybe it could be a callback that's triggered on every step starting or perhaps there's a way it can run in parallel. Don't limit your investigation to my random thoughts. |
- Implemented handling of `tool-approval-request` and `tool-approval-response` in `deltas.ts` to update tool state accordingly. - Enhanced tests in `deltas.test.ts` and `toUIMessages.test.ts` to verify the approval workflow, including state transitions for approval requests and responses. - Updated `UIMessages.ts` to manage tool approval states and responses, ensuring correct state updates for tool calls based on approval outcomes.
…t` and `tool-approval-response`.
…tToolApprovals`. Without the fix, the tool call was being filtered out because it had no tool-result, even though it had an approval response - which caused the SDK to fail at line 91 in `ai/src/generate-text/collect-tool-approvals.ts`.
…rove deprecation handling.
…result errors since stored messages have the complete data.
… tool approval, as stored messages contain complete context.
This pull request introduces a major refactor and extension of the
createToolAPI insrc/client/createTool.ts, making tool definitions more flexible and feature-rich. The changes focus on improving type safety, enabling more advanced tool behaviors (such as approval workflows and custom output conversion), and supporting additional metadata for tools.Tool API Extensions and Refactor
Tool Definition Flexibility and Type Safety
inputSchema/args,execute/handler), improving backwards compatibility and type safety. Added utility types (NeverOptional,ToolInputProperties,ToolOutputPropertiesCtx) for more robust type handling.Advanced Tool Behaviors
needsApprovalproperty, which can be a boolean or a function that determines if a tool call requires approval before execution. [1] [2]Output Handling Improvements
toModelOutputconversion function to allow custom mapping of tool results to model output, enhancing integration with language models and streamlining result formatting. [1] [2]Internal API Changes
createToolto handle the new options and behaviors, including proper error handling when required properties are missing and correct binding of context for event handlers and output conversion. [1] [2]Type Import Updates
ToolResultOutput,ModelMessage, etc.) are available in the module.These changes make it easier to define complex tools for AI agents, support advanced workflows, and ensure better type safety and extensibility.