add support for StandardJSONSchemaV1#2901
Conversation
|
@phryneas is attempting to deploy a commit to the CopilotKit Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughAdds first-class support for a StandardSchemaWithJSON schema alongside Zod for tool/renderer parameter definitions, including type changes, a runtime type-guard, conditional schema generation, helper to produce runtime Schema, dependency updates, and aligned documentation edits. (47 words) Changes
Sequence Diagram(s)sequenceDiagram
participant Dev as Developer code (ToolDefinition)
participant Agent as Agent.convertToolDefinitionsToVercelAITools
participant SchemaHelper as standardSchema / zodToJsonSchema
participant Provider as ProviderUtils (Schema/jsonSchema)
participant Vercel as Vercel AI ToolSet
Dev->>Agent: supply ToolDefinition (parameters)
Agent->>SchemaHelper: isStandardSchemaWithJSON? (runtime type-guard)
alt standard schema
SchemaHelper->>Provider: parameters["~standard"].jsonSchema.input({target:"draft-07"})
Provider-->>Agent: JSON schema (from standard)
else zod schema
SchemaHelper->>Provider: zodToJsonSchema(parameters)
Provider-->>Agent: JSON schema (from zod)
end
Agent->>Vercel: build ToolSet with resolved JSON schema
Vercel-->>Agent: ToolSet ready
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20–25 minutes Suggested labels
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (4)
src/v2.x/apps/docs/reference/use-frontend-tool.mdx (1)
81-101: Update documentation to reflect StandardSchemaWithJSON support.The type annotation changed to
StandardSchemaWithJSON<T>, but the description on line 83 still states "A Zod schema defining the tool's input parameters." This is inconsistent and misleading since StandardSchemaWithJSON is designed to support other validation libraries like ArkType and Valibot (per PR objectives).🔎 Suggested documentation update
Update the description to clarify that StandardSchemaWithJSON accepts schemas from multiple validation libraries:
### parameters `StandardSchemaWithJSON<T>` **(optional)** -A Zod schema defining the tool's input parameters. Provides type safety and automatic validation. +A JSON-standard schema (e.g., from Zod, ArkType, or Valibot) defining the tool's input parameters. Provides type safety and automatic validation.Consider adding an example demonstrating usage with a non-Zod schema library to illustrate the broader compatibility.
src/v2.x/apps/docs/reference/use-human-in-the-loop.mdx (1)
90-106: Update documentation to reflect StandardSchemaWithJSON support.The type annotation changed to
StandardSchemaWithJSON<T>, but the description on line 92 still states "A Zod schema defining the parameters the agent will provide when requesting human input." This is inconsistent with the type change and doesn't reflect the PR's goal of supporting multiple validation libraries (Zod, ArkType, Valibot).🔎 Suggested documentation update
Update the description to clarify that StandardSchemaWithJSON accepts schemas from multiple validation libraries:
### parameters `StandardSchemaWithJSON<T>` **(optional)** -A Zod schema defining the parameters the agent will provide when requesting human input. +A JSON-standard schema (e.g., from Zod, ArkType, or Valibot) defining the parameters the agent will provide when requesting human input.src/v2.x/docs/ANGULAR_DESIGN.md (1)
346-354: Design documentation inconsistent with implementation.The design document shows
args: StandardSchemaWithJSON<Args>on line 350, but the actual implementation insrc/v2.x/packages/angular/src/lib/tools.tsuses a union type:args: z.ZodType<Args> | StandardSchemaWithJSON<Args>(line 61).This design document should reflect the actual implementation to maintain accuracy and help future contributors understand the correct interface.
🔎 Suggested fix
interface ToolCallRendererConfig< Args extends Record<string, unknown> = Record<string, unknown>, > { name: string; - args: StandardSchemaWithJSON<Args>; + args: z.ZodType<Args> | StandardSchemaWithJSON<Args>; component: Type<ClientToolRenderer<Args>>; agentId?: string; }src/v2.x/packages/agent/src/index.ts (1)
229-241: Fix return type mismatch in defineTool.The
defineToolfunction returnsToolDefinition<TParameters>, butToolDefinition<Args>expectsArgsto be the argument type (the validated data type), not the schema type. This should beToolDefinition<z.infer<TParameters>>to match the updated generic signature.🔎 Proposed fix
export function defineTool<TParameters extends z.ZodTypeAny>(config: { name: string; description: string; parameters: TParameters; execute: (args: z.infer<TParameters>) => Promise<unknown>; -}): ToolDefinition<TParameters> { +}): ToolDefinition<z.infer<TParameters>> { return { name: config.name, description: config.description, parameters: config.parameters, execute: config.execute, }; }
🧹 Nitpick comments (2)
src/v2.x/packages/agent/src/index.ts (2)
48-48: Remove unused import.The
StandardTypedV1import doesn't appear to be used anywhere in this file.🔎 Suggested cleanup
-import { StandardSchemaWithJSON, isStandardSchemaWithJSON } from "@copilotkitnext/core"; -import { StandardTypedV1 } from "@standard-schema/spec"; +import { StandardSchemaWithJSON, isStandardSchemaWithJSON } from "@copilotkitnext/core";
750-753: Consider simplifying the type assertion.The pattern
satisfies ToolSet as ToolSetis redundant. IfmcpToolssatisfiesToolSet, the additionalas ToolSetcast is unnecessary. You can use eithersatisfiesfor type checking orasfor casting, but both together is redundant.🔎 Suggested simplification
streamTextParams.tools = { ...streamTextParams.tools, - ...(mcpTools satisfies ToolSet as ToolSet), + ...mcpTools, };Or if you want to keep the type check:
streamTextParams.tools = { ...streamTextParams.tools, - ...(mcpTools satisfies ToolSet as ToolSet), + ...(mcpTools satisfies ToolSet), };
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
src/v2.x/pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (12)
src/v2.x/apps/docs/reference/frontend-tool.mdx(2 hunks)src/v2.x/apps/docs/reference/use-frontend-tool.mdx(1 hunks)src/v2.x/apps/docs/reference/use-human-in-the-loop.mdx(1 hunks)src/v2.x/docs/ANGULAR_DESIGN.md(1 hunks)src/v2.x/packages/agent/package.json(1 hunks)src/v2.x/packages/agent/src/index.ts(7 hunks)src/v2.x/packages/angular/src/lib/tools.ts(3 hunks)src/v2.x/packages/core/package.json(1 hunks)src/v2.x/packages/core/src/core/run-handler.ts(2 hunks)src/v2.x/packages/core/src/index.ts(1 hunks)src/v2.x/packages/core/src/types.ts(2 hunks)src/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
src/v2.x/**/package.json
📄 CodeRabbit inference engine (src/v2.x/.cursor/rules/10-repo-conventions.always.mdc)
src/v2.x/**/package.json: Keep scripts standardized:build,dev,lint,check-types,test, andtest:watch.
All packages publish within the workspace usingpnpmworkspaces.
Files:
src/v2.x/packages/core/package.jsonsrc/v2.x/packages/agent/package.json
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/frontend-development.mdc)
**/*.{ts,tsx}: Import and useCopilotProviderfrom '@copilotkit/react-core' as the main provider component at the root of your React application
UseuseCopilotChathook from '@copilotkit/react-core' for implementing chat functionality
UseuseCopilotActionhook from '@copilotkit/react-core' to define actions that agents can execute
UseuseCopilotReadablehook from '@copilotkit/react-core' to expose application state to agents
UseCopilotChatcomponent from '@copilotkit/react-ui' for implementing pre-built chat interface UI
UseCopilotPopupcomponent from '@copilotkit/react-ui' for implementing popup chat interface
UseCopilotSidebarcomponent from '@copilotkit/react-ui' for implementing sidebar chat interface
UseCopilotTextareacomponent from '@copilotkit/react-textarea' for implementing AI-enhanced textarea with auto-completion and suggestionsAlways use the provided hooks and functions to modify suggestions state - never modify suggestions state directly
Files:
src/v2.x/packages/angular/src/lib/tools.tssrc/v2.x/packages/core/src/types.tssrc/v2.x/packages/core/src/index.tssrc/v2.x/packages/core/src/core/run-handler.tssrc/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.tssrc/v2.x/packages/agent/src/index.ts
**/*.ts
📄 CodeRabbit inference engine (.cursor/rules/frontend-development.mdc)
Use TypeScript for component development to ensure better type safety
Files:
src/v2.x/packages/angular/src/lib/tools.tssrc/v2.x/packages/core/src/types.tssrc/v2.x/packages/core/src/index.tssrc/v2.x/packages/core/src/core/run-handler.tssrc/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.tssrc/v2.x/packages/agent/src/index.ts
**/*.{tsx,ts,jsx,js}
📄 CodeRabbit inference engine (.cursor/rules/quick-reference.mdc)
For building a simple Copilot, start with copilot-chat-with-your-data example, use react-core package for basic integration, and add react-ui package for pre-built components
Files:
src/v2.x/packages/angular/src/lib/tools.tssrc/v2.x/packages/core/src/types.tssrc/v2.x/packages/core/src/index.tssrc/v2.x/packages/core/src/core/run-handler.tssrc/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.tssrc/v2.x/packages/agent/src/index.ts
src/v2.x/**/*.{ts,tsx}
📄 CodeRabbit inference engine (src/v2.x/.cursor/rules/20-typescript.auto.mdc)
src/v2.x/**/*.{ts,tsx}: Prefertypealiases overinterfacefor data structures in TypeScript
Avoidanyin TypeScript; use explicit generics orunknownwhere necessary
Files should be named inkebab-case.tsorkebab-case.tsxwith a single export per file
Files:
src/v2.x/packages/angular/src/lib/tools.tssrc/v2.x/packages/core/src/types.tssrc/v2.x/packages/core/src/index.tssrc/v2.x/packages/core/src/core/run-handler.tssrc/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.tssrc/v2.x/packages/agent/src/index.ts
🧠 Learnings (13)
📚 Learning: 2025-12-18T18:11:31.601Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: src/v2.x/.cursor/rules/10-repo-conventions.always.mdc:0-0
Timestamp: 2025-12-18T18:11:31.601Z
Learning: Applies to src/v2.x/**/package.json : Keep scripts standardized: `build`, `dev`, `lint`, `check-types`, `test`, and `test:watch`.
Applied to files:
src/v2.x/packages/core/package.jsonsrc/v2.x/packages/core/src/index.tssrc/v2.x/packages/core/src/core/run-handler.tssrc/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.ts
📚 Learning: 2025-12-18T18:10:03.431Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/agent-development.mdc:0-0
Timestamp: 2025-12-18T18:10:03.431Z
Learning: Applies to **/agent-js/{package.json,tsconfig.json} : JavaScript agent setup should install `copilotkit/sdk-js` package and configure TypeScript with proper types
Applied to files:
src/v2.x/packages/core/package.jsonsrc/v2.x/packages/core/src/index.tssrc/v2.x/packages/core/src/core/run-handler.tssrc/v2.x/packages/agent/package.jsonsrc/v2.x/packages/agent/src/index.ts
📚 Learning: 2025-12-18T18:11:01.653Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/quick-reference.mdc:0-0
Timestamp: 2025-12-18T18:11:01.653Z
Learning: Applies to {src/v1.x/package.json,src/v1.x/scripts/**/*.{js,ts},src/v1.x/utilities/**/*.{js,ts}} : For setting up development environment, review src/v1.x/package.json for workspace setup, use scripts in src/v1.x/scripts, and check utilities for shared configurations
Applied to files:
src/v2.x/packages/core/package.jsonsrc/v2.x/packages/core/src/index.tssrc/v2.x/packages/core/src/core/run-handler.ts
📚 Learning: 2025-12-18T18:09:47.478Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: src/v2.x/CLAUDE.md:0-0
Timestamp: 2025-12-18T18:09:47.478Z
Learning: Use `pnpm` for package management (never use `npm`). Add workspace dependencies with `pnpm add -w <pkg>`. Keep scripts standardized across packages: `build`, `dev`, `lint`, `check-types`, `test`, `test:watch`.
Applied to files:
src/v2.x/packages/core/package.json
📚 Learning: 2025-12-18T18:11:01.653Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/quick-reference.mdc:0-0
Timestamp: 2025-12-18T18:11:01.653Z
Learning: Applies to **/*.{tsx,ts,jsx,js} : For building a simple Copilot, start with copilot-chat-with-your-data example, use react-core package for basic integration, and add react-ui package for pre-built components
Applied to files:
src/v2.x/packages/core/package.jsonsrc/v2.x/packages/core/src/core/run-handler.tssrc/v2.x/packages/agent/package.json
📚 Learning: 2025-12-18T18:10:15.706Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/copilotkit-architecture.mdc:0-0
Timestamp: 2025-12-18T18:10:15.706Z
Learning: Examples should use local packages via workspace references as defined in package.json workspace configuration
Applied to files:
src/v2.x/packages/core/package.json
📚 Learning: 2025-12-18T18:11:58.438Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: src/v2.x/packages/runtime/.cursor/rules/runtime.always.mdc:0-0
Timestamp: 2025-12-18T18:11:58.438Z
Learning: Applies to src/v2.x/packages/runtime/src/index.ts : Exported APIs live under `src/index.ts`
Applied to files:
src/v2.x/packages/core/src/index.ts
📚 Learning: 2025-12-18T18:11:58.438Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: src/v2.x/packages/runtime/.cursor/rules/runtime.always.mdc:0-0
Timestamp: 2025-12-18T18:11:58.438Z
Learning: Applies to src/v2.x/packages/runtime/src/**/*.{ts,tsx} : Node-targeted code compiled from `src/` to `dist/` using TypeScript
Applied to files:
src/v2.x/packages/core/src/index.ts
📚 Learning: 2025-12-18T18:11:36.534Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: src/v2.x/.cursor/rules/20-typescript.auto.mdc:0-0
Timestamp: 2025-12-18T18:11:36.534Z
Learning: Applies to src/v2.x/**/*.{ts,tsx} : Files should be named in `kebab-case.ts` or `kebab-case.tsx` with a single export per file
Applied to files:
src/v2.x/packages/core/src/index.ts
📚 Learning: 2025-12-18T18:11:01.653Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/quick-reference.mdc:0-0
Timestamp: 2025-12-18T18:11:01.653Z
Learning: Applies to src/v1.x/packages/sdk-js/**/*.{ts,tsx,js,jsx} : For developing JavaScript agents, use sdk-js package and reference coagents-starter/agent-js for examples
Applied to files:
src/v2.x/packages/core/src/index.tssrc/v2.x/packages/core/src/core/run-handler.tssrc/v2.x/packages/agent/package.jsonsrc/v2.x/packages/agent/src/index.ts
📚 Learning: 2025-12-18T18:10:03.431Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/agent-development.mdc:0-0
Timestamp: 2025-12-18T18:10:03.431Z
Learning: Applies to **/agent-js/**/*.{js,ts} : JavaScript agents should be located in folders named `agent-js/` and use the sdk-js package
Applied to files:
src/v2.x/packages/core/src/index.ts
📚 Learning: 2025-12-18T18:10:47.795Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/frontend-development.mdc:0-0
Timestamp: 2025-12-18T18:10:47.795Z
Learning: Applies to **/*.{ts,tsx} : Use `useCopilotReadable` hook from 'copilotkit/react-core' to expose application state to agents
Applied to files:
src/v2.x/packages/core/src/index.tssrc/v2.x/packages/core/src/core/run-handler.ts
📚 Learning: 2025-12-18T18:10:47.795Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/frontend-development.mdc:0-0
Timestamp: 2025-12-18T18:10:47.795Z
Learning: Applies to **/*.{ts,tsx} : Use `useCopilotAction` hook from 'copilotkit/react-core' to define actions that agents can execute
Applied to files:
src/v2.x/packages/core/src/core/run-handler.ts
🧬 Code graph analysis (4)
src/v2.x/packages/angular/src/lib/tools.ts (1)
src/v2.x/packages/core/src/types.ts (1)
StandardSchemaWithJSON(12-14)
src/v2.x/packages/core/src/core/run-handler.ts (1)
src/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.ts (1)
isStandardSchemaWithJSON(3-17)
src/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.ts (1)
src/v2.x/packages/core/src/types.ts (1)
StandardSchemaWithJSON(12-14)
src/v2.x/packages/agent/src/index.ts (3)
src/v2.x/packages/core/src/types.ts (1)
StandardSchemaWithJSON(12-14)src/v2.x/packages/core/src/core/run-handler.ts (1)
tools(36-38)src/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.ts (1)
isStandardSchemaWithJSON(3-17)
🔇 Additional comments (12)
src/v2.x/packages/core/src/index.ts (1)
5-5: LGTM!The re-export of the
isStandardSchemaWithJSONutility follows the established pattern and correctly exposes the new functionality to package consumers.src/v2.x/packages/angular/src/lib/tools.ts (2)
5-5: LGTM!The import of
StandardSchemaWithJSONfrom@copilotkitnext/coreis correct and follows the package structure.
61-81: Type broadening is consistent and maintains backward compatibility.The changes correctly widen the accepted types for schema parameters across all three configuration interfaces:
RenderToolCallConfig.args(line 61)FrontendToolConfig.parameters(line 69)HumanInTheLoopConfig.parameters(line 78)By using union types (
z.ZodType<Args> | StandardSchemaWithJSON<Args>), the changes maintain full backward compatibility while enabling support for additional validation libraries.src/v2.x/packages/agent/package.json (1)
42-48: Dependency versions contain inaccuracies.@ai-sdk/provider-utils latest version is 3.0.17, not 3.0.19. @standard-schema/spec latest version is 1.0.0, not 1.1.0. @ai-sdk/mcp 0.0.12 exists, and ai 5.0.115 exists. Correct the version specifications to match available releases on npm.
Likely an incorrect or invalid review comment.
src/v2.x/packages/core/package.json (1)
41-41: Change @standard-schema/spec version from 1.1.0 to 1.0.0.The latest version of @standard-schema/spec is 1.0.0. Version 1.1.0 does not exist and will cause dependency resolution failures. Update the dependency to
"@standard-schema/spec": "^1.0.0"to ensure correct package resolution.⛔ Skipped due to learnings
Learnt from: CR Repo: CopilotKit/CopilotKit PR: 0 File: src/v2.x/.cursor/rules/10-repo-conventions.always.mdc:0-0 Timestamp: 2025-12-18T18:11:31.601Z Learning: Applies to src/v2.x/**/package.json : Keep scripts standardized: `build`, `dev`, `lint`, `check-types`, `test`, and `test:watch`.src/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.ts (1)
3-17: LGTM! Thorough type guard implementation.The runtime checks correctly validate the shape of
StandardSchemaWithJSONby verifying all required nested properties and their types. This will properly discriminate between standard schemas and Zod types at runtime.src/v2.x/packages/core/src/types.ts (2)
3-14: Well-structured type definitions for standard schema support.The
CombinedPropsinterface cleanly combines bothStandardSchemaV1andStandardJSONSchemaV1properties, andStandardSchemaWithJSONfollows the standard-schema convention with the"~standard"property marker. This provides a solid foundation for interoperability with multiple validation libraries.
30-30: LGTM! Union type enables multi-library support.The updated
parameterssignature allows bothStandardSchemaWithJSON<T>andz.ZodType<T>, providing flexibility for users to choose their preferred validation library while maintaining backward compatibility with existing Zod-based code.src/v2.x/packages/agent/src/index.ts (4)
214-219: LGTM! Cleaner generic design for ToolDefinition.The updated generic signature
ToolDefinition<Args>is more intuitive than the previousToolDefinition<TParameters extends z.ZodTypeAny>, and the union type forparametersenables multi-library support while maintaining type safety.
411-435: Good temporary implementation with proper error handling.The
standardSchemahelper correctly convertsStandardSchemaWithJSONto the Vercel AI SDK'sSchemaformat by extracting the JSON schema and implementing validation. The async validation properly handles the result format and wraps errors inTypeValidationError.Good to see the comment noting this will be replaced by
asSchemafrom@ai-sdk/provider-utilsonce it's available.
440-455: LGTM! Runtime schema type detection and conversion.The updated function correctly handles both
StandardSchemaWithJSONand Zod schemas using runtime type checking. TheisStandardSchemaWithJSONguard properly routes to the appropriate schema conversion path.
524-524: Correct generic parameter for heterogeneous tools array.Using
ToolDefinition<any>[]is appropriate here since the tools array can contain tools with different argument types. This aligns with the updated generic signature.
| const parameters = tool.parameters; | ||
| const rawSchema = isStandardSchemaWithJSON(parameters) | ||
| ? parameters["~standard"].jsonSchema.input({ | ||
| target: "draft-07", | ||
| }) | ||
| : zodToJsonSchema(parameters as z.ZodType, { | ||
| $refStrategy: "none", | ||
| }); |
There was a problem hiding this comment.
JSON Schema Draft-07 target causes compatibility issues with downstream consumers
The schema conversion logic correctly handles both StandardSchemaWithJSON and Zod schema types, but hardcoding target: "draft-07" creates compatibility problems:
- Anthropic's modern MCP clients require strict compliance with JSON Schema draft-2020-12
- Anthropic production API rejects schemas that aren't Draft 2020-12
The implementation should either:
- Use JSON Schema draft-2020-12 as the target instead of draft-07
- Make the schema version configurable to support multiple downstream consumers
- Document the limitation and add support for modern schema versions
🤖 Prompt for AI Agents
In src/v2.x/packages/core/src/core/run-handler.ts around lines 563 to 570, the
JSON Schema generation is hardcoded to target "draft-07", which breaks
compatibility with downstream consumers that require draft-2020-12; update the
code to target "draft-2020-12" instead, or better: introduce a configurable
schemaVersion option (defaulting to "draft-2020-12") that is passed into the
StandardSchemaWithJSON.jsonSchema.input call and used as the target for Zod ->
JSON Schema conversion so callers can override when needed; ensure the new
option is plumbed from the handler config, validate acceptable values, and keep
draft-2020-12 as the default to preserve Anthropic compatibility.
There was a problem hiding this comment.
That's what Vercels ai package uses: https://github.com/vercel/ai/blob/f76e1d7e3649f63cf8acee692fe7af2994f0ee73/packages/provider-utils/src/schema.ts#L151-L153
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/v2.x/packages/agent/src/index.ts (1)
213-240: UpdatedefineToolto support StandardSchemaWithJSON.The
ToolDefinitioninterface (lines 213-218) now acceptsStandardSchemaWithJSON<Args> | z.ZodType<Args>, but thedefineToolhelper function (lines 228-240) still only accepts Zod schemas. This inconsistency means developers cannot usedefineToolto create tools with StandardSchemaWithJSON and must manually constructToolDefinitionobjects instead.🔎 Proposed fix to support both schema types
Update the function to accept either schema type:
/** * Define a tool for use with BuiltInAgent * @param name - The name of the tool * @param description - Description of what the tool does - * @param parameters - Zod schema for the tool's input parameters + * @param parameters - Schema for the tool's input parameters (Zod or StandardSchemaWithJSON) * @param execute - Function to execute the tool server-side * @returns Tool definition */ -export function defineTool<TParameters extends z.ZodTypeAny>(config: { +export function defineTool<Args>(config: { name: string; description: string; - parameters: TParameters; - execute: (args: z.infer<TParameters>) => Promise<unknown>; -}): ToolDefinition<TParameters> { + parameters: StandardSchemaWithJSON<Args> | z.ZodType<Args>; + execute: (args: Args) => Promise<unknown>; +}): ToolDefinition<Args> { return { name: config.name, description: config.description, parameters: config.parameters, execute: config.execute, }; }
🧹 Nitpick comments (2)
src/v2.x/packages/agent/src/index.ts (2)
439-454: Consider usingunknowninstead ofanyfor better type safety.The parameter type
ToolDefinition<any>[]and the cast(tool.parameters as any as z.ZodTypeAny)violate the coding guideline to avoidany. While the implementation works correctly at runtime, usingunknownwould provide better type safety.🔎 Proposed refactor for better type safety
-export function convertToolDefinitionsToVercelAITools(tools: ToolDefinition<any>[]): ToolSet { +export function convertToolDefinitionsToVercelAITools(tools: ToolDefinition<unknown>[]): ToolSet { // eslint-disable-next-line @typescript-eslint/no-explicit-any const result: Record<string, any> = {}; for (const tool of tools) { result[tool.name] = createVercelAISDKTool({ description: tool.description, inputSchema: isStandardSchemaWithJSON(tool.parameters) ? standardSchema(tool.parameters) - : (tool.parameters as any as z.ZodTypeAny), + : (tool.parameters as z.ZodTypeAny), execute: tool.execute, }); } return result; }Note: If the single cast still raises type errors, you may need to adjust the ToolDefinition generic constraint, but
unknownis preferable toany.As per coding guidelines, prefer explicit generics or
unknownoverany.
784-784: Minor: String literal style consistency.Changed from single quotes to double quotes. While this has no functional impact, you may want to establish a consistent quote style across the file for maintainability.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/v2.x/packages/agent/src/index.ts
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/frontend-development.mdc)
**/*.{ts,tsx}: Import and useCopilotProviderfrom '@copilotkit/react-core' as the main provider component at the root of your React application
UseuseCopilotChathook from '@copilotkit/react-core' for implementing chat functionality
UseuseCopilotActionhook from '@copilotkit/react-core' to define actions that agents can execute
UseuseCopilotReadablehook from '@copilotkit/react-core' to expose application state to agents
UseCopilotChatcomponent from '@copilotkit/react-ui' for implementing pre-built chat interface UI
UseCopilotPopupcomponent from '@copilotkit/react-ui' for implementing popup chat interface
UseCopilotSidebarcomponent from '@copilotkit/react-ui' for implementing sidebar chat interface
UseCopilotTextareacomponent from '@copilotkit/react-textarea' for implementing AI-enhanced textarea with auto-completion and suggestionsAlways use the provided hooks and functions to modify suggestions state - never modify suggestions state directly
Files:
src/v2.x/packages/agent/src/index.ts
**/*.ts
📄 CodeRabbit inference engine (.cursor/rules/frontend-development.mdc)
Use TypeScript for component development to ensure better type safety
Files:
src/v2.x/packages/agent/src/index.ts
**/*.{tsx,ts,jsx,js}
📄 CodeRabbit inference engine (.cursor/rules/quick-reference.mdc)
For building a simple Copilot, start with copilot-chat-with-your-data example, use react-core package for basic integration, and add react-ui package for pre-built components
Files:
src/v2.x/packages/agent/src/index.ts
src/v2.x/**/*.{ts,tsx}
📄 CodeRabbit inference engine (src/v2.x/.cursor/rules/20-typescript.auto.mdc)
src/v2.x/**/*.{ts,tsx}: Prefertypealiases overinterfacefor data structures in TypeScript
Avoidanyin TypeScript; use explicit generics orunknownwhere necessary
Files should be named inkebab-case.tsorkebab-case.tsxwith a single export per file
Files:
src/v2.x/packages/agent/src/index.ts
🧠 Learnings (3)
📚 Learning: 2025-12-18T18:10:03.431Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/agent-development.mdc:0-0
Timestamp: 2025-12-18T18:10:03.431Z
Learning: Applies to **/agent-js/{package.json,tsconfig.json} : JavaScript agent setup should install `copilotkit/sdk-js` package and configure TypeScript with proper types
Applied to files:
src/v2.x/packages/agent/src/index.ts
📚 Learning: 2025-12-18T18:11:01.653Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/quick-reference.mdc:0-0
Timestamp: 2025-12-18T18:11:01.653Z
Learning: Applies to src/v1.x/packages/sdk-js/**/*.{ts,tsx,js,jsx} : For developing JavaScript agents, use sdk-js package and reference coagents-starter/agent-js for examples
Applied to files:
src/v2.x/packages/agent/src/index.ts
📚 Learning: 2025-12-18T18:11:58.438Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: src/v2.x/packages/runtime/.cursor/rules/runtime.always.mdc:0-0
Timestamp: 2025-12-18T18:11:58.438Z
Learning: Applies to src/v2.x/packages/runtime/src/**/*.{ts,tsx} : Node-targeted code compiled from `src/` to `dist/` using TypeScript
Applied to files:
src/v2.x/packages/agent/src/index.ts
🧬 Code graph analysis (1)
src/v2.x/packages/agent/src/index.ts (2)
src/v2.x/packages/core/src/types.ts (1)
StandardSchemaWithJSON(12-14)src/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.ts (1)
isStandardSchemaWithJSON(3-17)
🔇 Additional comments (5)
src/v2.x/packages/agent/src/index.ts (5)
32-47: LGTM! New imports support StandardSchemaWithJSON.The new imports correctly support the StandardJSONSchemaV1 feature, enabling runtime detection and validation of standard schemas alongside Zod.
523-523: Type consistency withconvertToolDefinitionsToVercelAITools.This type change to
ToolDefinition<any>[]is consistent with the function signature at line 439. However, both should preferably useunknowninstead ofany(see previous comment on lines 439-454).
749-752: LGTM! Formatting improvement.The multi-line spread improves readability without changing functionality.
410-434: ThestandardSchemafunction is not a temporary implementation awaiting upgrade to@ai-sdk/provider-utils@4.0.0-beta.53. The project currently depends on@ai-sdk/provider-utils@^3.0.19(stable), with no evidence of planned upgrades to beta versions. The function serves as a workaround to supportStandardSchemaWithJSONwith the current stable version and should remain in place.Likely an incorrect or invalid review comment.
47-47: Add integration tests for StandardSchemaWithJSON with ArkType and Valibot.The
StandardSchemaWithJSONinterface correctly requires both validation (StandardSchemaV1.Props) and JSON schema conversion (StandardJSONSchemaV1.Props), andisStandardSchemaWithJSONproperly checks for the~standardproperty with bothvalidate()andjsonSchemamethods. However, there is no test coverage verifying end-to-end integration with ArkType and Valibot schemas, which are listed as supported in the PR objectives. Add tests to confirm that schemas from these libraries, which support Standard Schema, are correctly identified and processed through the conversion pipeline.
⏭️ Changeset Not RequiredLatest commit: d284392 No changes in this PR affected the Changeset is not required for this PR. |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/v2.x/packages/agent/src/index.ts (2)
410-434: Document the temporary nature and removal timeline for this polyfill.This is a well-implemented polyfill for
asSchemafunctionality. However, consider adding:
- A TODO comment indicating when this can be removed (e.g., "TODO: Remove once @ai-sdk/provider-utils is upgraded to >=4.0.0-beta.53")
- A clearer indication that this is temporary scaffolding
This will help future maintainers know when to clean up the polyfill.
🔎 Suggested documentation enhancement
/* - * supported in "@ai-sdk/provider-utils@"4.0.0-beta.53" by `asSchema`, copied here for now + * TODO: Remove this polyfill once @ai-sdk/provider-utils is upgraded to >=4.0.0-beta.53 + * This functionality is available as `asSchema` in the newer version. + * Temporary polyfill copied from the expected API. */ function standardSchema<OBJECT>(standardSchema: StandardSchemaWithJSON<OBJECT>): Schema<OBJECT> {
446-448: Consider refining the type cast to avoid double casting.The double cast
as any as z.ZodTypeAnyon line 448 bypasses TypeScript's type checking. While this may be necessary for the current API constraints, consider if there's a cleaner approach:
- Define a helper type that represents the union of possible input schemas
- Use a type assertion function instead of casting
- Add a runtime check to ensure the schema is actually a Zod schema
🔎 Alternative approach using type predicate
function isZodType<T>(schema: StandardSchemaWithJSON<T> | z.ZodType<T>): schema is z.ZodType<T> { return !isStandardSchemaWithJSON(schema); } // Then in the function: inputSchema: isStandardSchemaWithJSON(tool.parameters) ? standardSchema(tool.parameters) : tool.parameters as z.ZodTypeAny, // Single cast with type guard above
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/v2.x/packages/agent/src/index.ts
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/frontend-development.mdc)
**/*.{ts,tsx}: Import and useCopilotProviderfrom '@copilotkit/react-core' as the main provider component at the root of your React application
UseuseCopilotChathook from '@copilotkit/react-core' for implementing chat functionality
UseuseCopilotActionhook from '@copilotkit/react-core' to define actions that agents can execute
UseuseCopilotReadablehook from '@copilotkit/react-core' to expose application state to agents
UseCopilotChatcomponent from '@copilotkit/react-ui' for implementing pre-built chat interface UI
UseCopilotPopupcomponent from '@copilotkit/react-ui' for implementing popup chat interface
UseCopilotSidebarcomponent from '@copilotkit/react-ui' for implementing sidebar chat interface
UseCopilotTextareacomponent from '@copilotkit/react-textarea' for implementing AI-enhanced textarea with auto-completion and suggestionsAlways use the provided hooks and functions to modify suggestions state - never modify suggestions state directly
Files:
src/v2.x/packages/agent/src/index.ts
**/*.ts
📄 CodeRabbit inference engine (.cursor/rules/frontend-development.mdc)
Use TypeScript for component development to ensure better type safety
Files:
src/v2.x/packages/agent/src/index.ts
**/*.{tsx,ts,jsx,js}
📄 CodeRabbit inference engine (.cursor/rules/quick-reference.mdc)
For building a simple Copilot, start with copilot-chat-with-your-data example, use react-core package for basic integration, and add react-ui package for pre-built components
Files:
src/v2.x/packages/agent/src/index.ts
src/v2.x/**/*.{ts,tsx}
📄 CodeRabbit inference engine (src/v2.x/.cursor/rules/20-typescript.auto.mdc)
src/v2.x/**/*.{ts,tsx}: Prefertypealiases overinterfacefor data structures in TypeScript
Avoidanyin TypeScript; use explicit generics orunknownwhere necessary
Files should be named inkebab-case.tsorkebab-case.tsxwith a single export per file
Files:
src/v2.x/packages/agent/src/index.ts
🧠 Learnings (6)
📚 Learning: 2025-12-18T18:10:03.431Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/agent-development.mdc:0-0
Timestamp: 2025-12-18T18:10:03.431Z
Learning: Applies to **/agent-js/{package.json,tsconfig.json} : JavaScript agent setup should install `copilotkit/sdk-js` package and configure TypeScript with proper types
Applied to files:
src/v2.x/packages/agent/src/index.ts
📚 Learning: 2025-12-18T18:11:01.653Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/quick-reference.mdc:0-0
Timestamp: 2025-12-18T18:11:01.653Z
Learning: Applies to src/v1.x/packages/sdk-js/**/*.{ts,tsx,js,jsx} : For developing JavaScript agents, use sdk-js package and reference coagents-starter/agent-js for examples
Applied to files:
src/v2.x/packages/agent/src/index.ts
📚 Learning: 2025-12-18T18:11:01.653Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: .cursor/rules/quick-reference.mdc:0-0
Timestamp: 2025-12-18T18:11:01.653Z
Learning: Applies to {src/v1.x/package.json,src/v1.x/scripts/**/*.{js,ts},src/v1.x/utilities/**/*.{js,ts}} : For setting up development environment, review src/v1.x/package.json for workspace setup, use scripts in src/v1.x/scripts, and check utilities for shared configurations
Applied to files:
src/v2.x/packages/agent/src/index.ts
📚 Learning: 2025-12-18T18:11:31.601Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: src/v2.x/.cursor/rules/10-repo-conventions.always.mdc:0-0
Timestamp: 2025-12-18T18:11:31.601Z
Learning: Applies to src/v2.x/**/package.json : Keep scripts standardized: `build`, `dev`, `lint`, `check-types`, `test`, and `test:watch`.
Applied to files:
src/v2.x/packages/agent/src/index.ts
📚 Learning: 2025-12-18T18:11:58.438Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: src/v2.x/packages/runtime/.cursor/rules/runtime.always.mdc:0-0
Timestamp: 2025-12-18T18:11:58.438Z
Learning: Applies to src/v2.x/packages/runtime/src/index.ts : Exported APIs live under `src/index.ts`
Applied to files:
src/v2.x/packages/agent/src/index.ts
📚 Learning: 2025-12-18T18:11:58.438Z
Learnt from: CR
Repo: CopilotKit/CopilotKit PR: 0
File: src/v2.x/packages/runtime/.cursor/rules/runtime.always.mdc:0-0
Timestamp: 2025-12-18T18:11:58.438Z
Learning: Applies to src/v2.x/packages/runtime/src/**/*.{ts,tsx} : Node-targeted code compiled from `src/` to `dist/` using TypeScript
Applied to files:
src/v2.x/packages/agent/src/index.ts
🧬 Code graph analysis (1)
src/v2.x/packages/agent/src/index.ts (3)
src/v2.x/packages/core/src/types.ts (1)
StandardSchemaWithJSON(12-14)src/v2.x/packages/core/src/core/run-handler.ts (1)
tools(36-38)src/v2.x/packages/core/src/utils/isStandardSchemaWithJSON.ts (1)
isStandardSchemaWithJSON(3-17)
🔇 Additional comments (6)
src/v2.x/packages/agent/src/index.ts (6)
213-218: LGTM! Generic type simplified for dual schema support.The interface correctly supports both
StandardSchemaWithJSON<Args>andz.ZodType<Args>for parameters. The generic type simplification fromTParameters extends z.ZodTypeAnytoArgsis appropriate for enabling interoperability with multiple validation libraries while maintaining type safety through the union type.
228-240: LGTM! Proper use ofNoInferfor type safety.The function signature correctly uses
NoInfer<Args>in the execute parameter to prevent reverse inference, ensuring thatArgsis always derived from theparametersschema. The updated JSDoc clearly documents support for both schema types.
523-523: Type relaxation is necessary for dual schema support.The change from a Zod-constrained generic to
ToolDefinition<any>[]is a necessary trade-off to support bothStandardSchemaWithJSONandz.ZodTypeschemas. Type safety is still maintained at the individualToolDefinitionlevel through the union type in the interface.
749-752: Good formatting improvement for readability.The multi-line object spread makes the MCP tools merge more readable and easier to maintain.
784-784: String literal consistency improvement.Changing from single quotes to double quotes improves consistency with other case statements in the switch block.
32-47: Verify current versions of @ai-sdk/provider-utils and ai packages to assess polyfill necessity.The current stable version of @ai-sdk/provider-utils is 3.0.17. The code includes a polyfill with a comment suggesting it's "supported in '@ai-sdk/provider-utils@4.0.0-beta.53' by
asSchema", but this cannot be verified through available documentation. TypeValidationError from the ai package is valid, and Schema/jsonSchema imports are correctly used. Confirm the actual project versions in package.json and whether the beta version and asSchema export actually exist before deciding if the polyfill can be removed.
What does this PR do?
Adds support for Standard JSON Schema, which enables a lot of other validation libraries like ArkType, Valibot etc.
Note that the separate handling of zod is only in place to support old versions of zod - modern versions (>=4.2) support Standard JSON Schema.
I'm not sure how to best test this since I don't know what your policies on new dependencies are.
Related PRs and Issues
Checklist
Summary by CodeRabbit
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.