Skip to content

jsonSchemaToActionParameters implementation - #1859

Closed
kosukeoya wants to merge 2 commits into
CopilotKit:mainfrom
kosukeoya:feat/1854-jsonSchemaToActionParameters
Closed

kosukeoya wants to merge 2 commits into
CopilotKit:mainfrom
kosukeoya:feat/1854-jsonSchemaToActionParameters

Conversation

@kosukeoya

@kosukeoya kosukeoya commented May 22, 2025 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Convert the jsonSchema so that it can be passed to parameters of useCopilotAction.

The usage is as follows

const stepSchema = z.object({
  description: z.string().describe("The text of the step in imperative form"),
  status: z.enum(["enabled", "disabled", "executing"]).describe("The status of the step, always 'enabled'"),
});

const paramsSchema = z.object({
  steps: z.array(stepSchema)
    .length(10)
    .describe("An array of 10 step objects, each containing text and status"),
});

useCopilotAction({
  name: "generateTaskSteps",
  description:
    "Make up 10 steps (only a couple of words per step) that are required for a task. The step should be in imperative form (i.e. Dig hole, Open door, ...)",
  parameters: jsonSchemaToActionParameters(zodToJsonSchema(paramsSchema))
  renderAndWaitForResponse: ({ args, respond, status }) => {
    return <StepsFeedback args={args} respond={respond} status={status} />;
  },
});

Related PRs and Issues

Checklist

  • I have read the Contribution Guide
  • If the PR changes or adds functionality, I have updated the relevant documentation

Summary by CodeRabbit

  • New Features

    • Added the ability to convert JSON Schema objects into arrays of parameters, supporting various data types including nested objects, arrays, enums, and booleans.
  • Tests

    • Introduced comprehensive tests to ensure accurate conversion between JSON Schema and parameter arrays, including round-trip validation and error handling for unsupported cases.

@vercel

vercel Bot commented May 22, 2025

Copy link
Copy Markdown
Contributor

@kosukeoya is attempting to deploy a commit to the CopilotKit Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented May 22, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

A new function, jsonSchemaToActionParameters, was added to convert JSON Schema objects into arrays of Parameter objects, along with a recursive helper for property conversion. Comprehensive tests were introduced to verify this logic, covering various schema scenarios and ensuring round-trip consistency between JSON Schema and the internal representation.

Changes

File(s) Change Summary
CopilotKit/packages/shared/src/utils/json-schema.ts Added jsonSchemaToActionParameters function and a recursive helper to convert JSON Schema to Parameter[]. No changes to existing exports.
CopilotKit/packages/shared/src/utils/json-schema.test.ts Added extensive tests for jsonSchemaToActionParameters and actionParametersToJsonSchema, covering multiple schema scenarios and round-trip validation.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant jsonSchemaToActionParameters
    participant convertJsonSchemaToParameter

    Caller->>jsonSchemaToActionParameters: Provide JSONSchema
    jsonSchemaToActionParameters->>convertJsonSchemaToParameter: For each property in schema
    convertJsonSchemaToParameter-->>jsonSchemaToActionParameters: Return Parameter object(s)
    jsonSchemaToActionParameters-->>Caller: Return array of Parameter objects
Loading

Poem

In the warren, tests abound,
New schemas hop and leap around.
From JSON roots to Parameter shoots,
Each field and type, the code computes.
Round-trips checked, no bugs in sight—
The rabbits code through day and night!
🐇✨

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 30th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
CopilotKit/packages/shared/src/utils/json-schema.ts (1)

79-158: Consider improving error handling for edge cases.

The helper function is well-structured with clear type handling, but there are a few areas that could be improved:

  1. Line 145's error message could be more descriptive (e.g., "Nested arrays (array of arrays) are not supported in Parameter conversion").
  2. There's no explicit handling for cases where schema.items might be undefined in the array case.
 case "array":
+      if (!schema.items) {
+        throw new Error("Array schema must have items property defined");
+      }
       if (schema.items.type === "object" && "properties" in schema.items) {
       } else if (schema.items.type === "array") {
-        throw new Error("Nested arrays are not supported");
+        throw new Error("Nested arrays (array of arrays) are not supported in Parameter conversion");
       } else {
CopilotKit/packages/shared/src/utils/json-schema.test.ts (1)

177-381: Comprehensive test coverage!

The test suite provides excellent coverage for the new functionality, including:

  • Simple schema conversion
  • Enum handling
  • Nested objects
  • Arrays (primitives and objects)
  • Boolean types
  • Empty schemas
  • Error cases for nested arrays
  • Round-trip conversion

Consider adding a few additional tests:

  1. Test for arrays of numbers and booleans, not just strings
  2. Test for the exact error message in the nested arrays test
  3. Test handling of edge cases like undefined schema properties
it("should convert arrays of numbers and booleans", () => {
  const jsonSchema: JSONSchema = {
    type: "object",
    properties: {
      numbers: { type: "array", items: { type: "number" } },
      flags: { type: "array", items: { type: "boolean" } },
    },
  };

  const result = jsonSchemaToActionParameters(jsonSchema);
  expect(result[0].type).toEqual("number[]");
  expect(result[1].type).toEqual("boolean[]");
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f1f5145 and 3e39964.

📒 Files selected for processing (2)
  • CopilotKit/packages/shared/src/utils/json-schema.test.ts (2 hunks)
  • CopilotKit/packages/shared/src/utils/json-schema.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
CopilotKit/packages/shared/src/utils/json-schema.test.ts (1)
CopilotKit/packages/shared/src/utils/json-schema.ts (3)
  • JSONSchema (33-38)
  • jsonSchemaToActionParameters (63-77)
  • actionParametersToJsonSchema (40-60)
🔇 Additional comments (2)
CopilotKit/packages/shared/src/utils/json-schema.ts (1)

62-77: The implementation looks good!

The function correctly handles the conversion from JSONSchema to Parameter array, with proper handling of required fields.

CopilotKit/packages/shared/src/utils/json-schema.test.ts (1)

2-9: Import organization looks good.

The imports are properly organized and include all necessary dependencies for the tests.

@tylerslaton

Copy link
Copy Markdown
Contributor

Hey @kosukeoya, this is awesome! Really love the contribution here. We're going to give this a review internally. In the meantime I'm going to kick off CI, for now ignore everything except. Its worth noting that our CI will be overhauled completely very soon for external contributors.

  • CI/Test
  • Quality/ESLint
  • Quality/Prettier

@changesets-bot-copilotkit

Copy link
Copy Markdown

⏭️ Changeset Not Required

Latest commit: d277582

No changes in this PR affected the @copilitkit/* packages. Merging this PR will not cause a version bump for any packages.

Changeset is not required for this PR.

@tylerslaton

Copy link
Copy Markdown
Contributor

This has been merged as a part of #1868. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants