Skip to content

Commit 377086a

Browse files
authored
add frontend schema test for tools (#7252)
## 📝 Summary <!-- Provide a concise summary of what this pull request is addressing. If this PR fixes any issues, list them here by number (e.g., Fixes #123). --> ## 🔍 Description of Changes <!-- Detail the specific changes made in this pull request. Explain the problem addressed and how it was resolved. If applicable, provide before and after comparisons, screenshots, or any relevant details to help reviewers understand the changes easily. --> ## 📋 Checklist - [x] I have read the [contributor guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md). - [ ] For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on [Discord](https://marimo.io/discord?ref=pr), or the community [discussions](https://github.com/marimo-team/marimo/discussions) (Please provide a link if applicable). - [x] I have added tests for the changes made. - [x] I have run the code and verified that it works as expected.
1 parent 74ee2aa commit 377086a

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

‎frontend/src/core/ai/tools/__tests__/registry.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
/* Copyright 2024 Marimo. All rights reserved. */
22

33
import { describe, expect, it } from "vitest";
4-
import { FrontendToolRegistry } from "../registry";
4+
import { COPILOT_MODES } from "@/core/config/config-schema";
5+
import { FRONTEND_TOOL_REGISTRY, FrontendToolRegistry } from "../registry";
56
import { TestFrontendTool } from "../sample-tool";
67

8+
// Tools that have these keys in their parameters are likely not supported by Google
9+
// So we should be careful to add these types of schemas
10+
const INVALID_KEYS_IN_SCHEMA_PARAMS = new Set(["anyOf", "oneOf"]);
11+
712
describe("FrontendToolRegistry", () => {
813
it("registers tools via constructor and supports has()", () => {
914
const registry = new FrontendToolRegistry([new TestFrontendTool()]);
@@ -87,4 +92,33 @@ describe("FrontendToolRegistry", () => {
8792
const schemas3 = registry.getToolSchemas("agent");
8893
expect(schemas3.length).toBe(0);
8994
});
95+
96+
it("tool schemas should not contain invalid keys", () => {
97+
const registry = FRONTEND_TOOL_REGISTRY;
98+
99+
function hasInvalidKeys(obj: unknown): boolean {
100+
if (obj && typeof obj === "object") {
101+
for (const [key, value] of Object.entries(obj)) {
102+
if (INVALID_KEYS_IN_SCHEMA_PARAMS.has(key)) {
103+
return true;
104+
}
105+
if (
106+
typeof value === "object" &&
107+
value !== null &&
108+
hasInvalidKeys(value)
109+
) {
110+
return true;
111+
}
112+
}
113+
}
114+
return false;
115+
}
116+
117+
for (const mode of COPILOT_MODES) {
118+
const schemas = registry.getToolSchemas(mode);
119+
for (const schema of schemas) {
120+
expect(hasInvalidKeys(schema.parameters)).toBe(false);
121+
}
122+
}
123+
});
90124
});

‎frontend/src/core/config/config-schema.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { z } from "zod";
33
import { invariant } from "@/utils/invariant";
44
import { Logger } from "@/utils/Logger";
5-
import type { MarimoConfig } from "../network/types";
5+
import type { MarimoConfig, schemas } from "../network/types";
66

77
// This has to be defined in the same file as the zod schema to satisfy zod
88
export const PackageManagerNames = [
@@ -44,8 +44,8 @@ export const DEFAULT_AI_MODEL = "openai/gpt-4o";
4444
*/
4545
const AUTO_DOWNLOAD_FORMATS = ["html", "markdown", "ipynb"] as const;
4646

47-
const COPILOT_MODES = ["manual", "ask", "agent"] as const;
48-
export type CopilotMode = (typeof COPILOT_MODES)[number];
47+
export type CopilotMode = NonNullable<schemas["AiConfig"]["mode"]>;
48+
export const COPILOT_MODES: CopilotMode[] = ["manual", "ask", "agent"];
4949

5050
const AiConfigSchema = z
5151
.object({

0 commit comments

Comments
 (0)