Migrate monorepo to Zod 4
This commit is contained in:
@@ -63,7 +63,8 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@agentclientprotocol/sdk": "^0.17.1",
|
||||
"@anthropic-ai/claude-agent-sdk": "^0.2.133",
|
||||
"@anthropic-ai/claude-agent-sdk": "^0.3.181",
|
||||
"@anthropic-ai/sdk": "^0.104.2",
|
||||
"@getpaseo/client": "0.1.97-beta.3",
|
||||
"@getpaseo/highlight": "0.1.97-beta.3",
|
||||
"@getpaseo/protocol": "0.1.97-beta.3",
|
||||
@@ -81,7 +82,7 @@
|
||||
"lru-cache": "^11.5.1",
|
||||
"mnemonic-id": "^3.2.7",
|
||||
"node-pty": "1.2.0-beta.11",
|
||||
"openai": "^4.20.0",
|
||||
"openai": "^6.44.0",
|
||||
"p-limit": "^7.3.0",
|
||||
"p-memoize": "^8.0.0",
|
||||
"pino": "^10.2.0",
|
||||
@@ -94,8 +95,7 @@
|
||||
"uuid": "^9.0.1",
|
||||
"which": "^5.0.0",
|
||||
"ws": "^8.14.2",
|
||||
"zod": "^3.23.8",
|
||||
"zod-to-json-schema": "^3.25.1"
|
||||
"zod": "^4.4.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@playwright/test": "^1.56.1",
|
||||
|
||||
@@ -2,7 +2,7 @@ import { fileURLToPath } from "node:url";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
|
||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
||||
import { z } from "zod";
|
||||
import { PersistedConfigSchema } from "../src/server/persisted-config.js";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
@@ -13,9 +13,12 @@ function main() {
|
||||
const outPath = path.join(repoRoot, "packages/website/public/schemas/paseo.config.v1.json");
|
||||
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
||||
|
||||
const schema = zodToJsonSchema(PersistedConfigSchema, {
|
||||
name: "PaseoConfigV1",
|
||||
const schema = z.toJSONSchema(PersistedConfigSchema, {
|
||||
target: "draft-07",
|
||||
unrepresentable: "any",
|
||||
io: "input",
|
||||
});
|
||||
schema.title = "PaseoConfigV1";
|
||||
|
||||
fs.writeFileSync(outPath, JSON.stringify(schema, null, 2) + "\n", "utf8");
|
||||
process.stdout.write(`Wrote ${outPath}\n`);
|
||||
|
||||
@@ -370,7 +370,7 @@ interface SubscriptionRecord {
|
||||
}
|
||||
|
||||
const BUSY_STATUSES: Set<AgentLifecycleStatus> = new Set(["initializing", "running"]);
|
||||
const AgentIdSchema = z.string().uuid();
|
||||
const AgentIdSchema = z.guid();
|
||||
|
||||
function isAgentBusy(status: AgentLifecycleStatus): boolean {
|
||||
return BUSY_STATUSES.has(status);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { z } from "zod";
|
||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
||||
import Ajv, { type ErrorObject, type Options as AjvOptions } from "ajv";
|
||||
import type { AgentProvider, AgentSessionConfig } from "./agent-sdk-types.js";
|
||||
import type { AgentManager } from "./agent-manager.js";
|
||||
@@ -106,16 +105,19 @@ interface SchemaValidator<T> {
|
||||
validate: (value: unknown) => { ok: true; value: T } | { ok: false; errors: string[] };
|
||||
}
|
||||
|
||||
function isZodSchema(value: unknown): value is z.ZodTypeAny {
|
||||
return typeof (value as z.ZodTypeAny | undefined)?.safeParse === "function";
|
||||
function isZodSchema(value: unknown): value is z.ZodType {
|
||||
return typeof (value as z.ZodType | undefined)?.safeParse === "function";
|
||||
}
|
||||
|
||||
function buildZodValidator<T>(schema: z.ZodTypeAny, schemaName: string): SchemaValidator<T> {
|
||||
const zodToJsonSchemaAny = zodToJsonSchema as unknown as (
|
||||
input: z.ZodTypeAny,
|
||||
name?: string,
|
||||
) => JsonSchema;
|
||||
const jsonSchema = zodToJsonSchemaAny(schema, schemaName);
|
||||
function buildZodValidator<T>(schema: z.ZodType, schemaName: string): SchemaValidator<T> {
|
||||
const jsonSchema = z.toJSONSchema(schema, {
|
||||
target: "draft-07",
|
||||
unrepresentable: "any",
|
||||
io: "input",
|
||||
}) as JsonSchema;
|
||||
if (typeof jsonSchema.title !== "string") {
|
||||
jsonSchema.title = schemaName;
|
||||
}
|
||||
return {
|
||||
jsonSchema,
|
||||
validate: (value) => {
|
||||
|
||||
@@ -14,10 +14,10 @@ const SERIALIZABLE_CONFIG_SCHEMA = z
|
||||
modeId: z.string().nullable().optional(),
|
||||
model: z.string().nullable().optional(),
|
||||
thinkingOptionId: z.string().nullable().optional(),
|
||||
featureValues: z.record(z.unknown()).nullable().optional(),
|
||||
extra: z.record(z.any()).nullable().optional(),
|
||||
featureValues: z.record(z.string(), z.unknown()).nullable().optional(),
|
||||
extra: z.record(z.string(), z.any()).nullable().optional(),
|
||||
systemPrompt: z.string().nullable().optional(),
|
||||
mcpServers: z.record(z.any()).nullable().optional(),
|
||||
mcpServers: z.record(z.string(), z.any()).nullable().optional(),
|
||||
})
|
||||
.nullable()
|
||||
.optional();
|
||||
@@ -27,7 +27,7 @@ const PERSISTENCE_HANDLE_SCHEMA = z
|
||||
provider: z.string(),
|
||||
sessionId: z.string(),
|
||||
nativeHandle: z.any().optional(),
|
||||
metadata: z.record(z.any()).optional(),
|
||||
metadata: z.record(z.string(), z.any()).optional(),
|
||||
})
|
||||
.nullable()
|
||||
.optional();
|
||||
@@ -42,7 +42,7 @@ const STORED_AGENT_SCHEMA = z.object({
|
||||
lastActivityAt: z.string().optional(),
|
||||
lastUserMessageAt: z.string().nullable().optional(),
|
||||
title: z.string().nullable().optional(),
|
||||
labels: z.record(z.string()).default({}),
|
||||
labels: z.record(z.string(), z.string()).default({}),
|
||||
lastStatus: AgentStatusSchema.default("closed"),
|
||||
lastModeId: z.string().nullable().optional(),
|
||||
config: SERIALIZABLE_CONFIG_SCHEMA,
|
||||
@@ -53,7 +53,7 @@ const STORED_AGENT_SCHEMA = z.object({
|
||||
model: z.string().nullable().optional(),
|
||||
thinkingOptionId: z.string().nullable().optional(),
|
||||
modeId: z.string().nullable().optional(),
|
||||
extra: z.record(z.unknown()).optional(),
|
||||
extra: z.record(z.string(), z.unknown()).optional(),
|
||||
})
|
||||
.optional(),
|
||||
features: z.array(AgentFeatureSchema).optional(),
|
||||
|
||||
@@ -9,6 +9,8 @@ import { z } from "zod";
|
||||
|
||||
import { AGENT_WAIT_TIMEOUT_MS } from "./mcp-shared.js";
|
||||
import { createTestPaseoDaemon, type TestPaseoDaemon } from "../test-utils/paseo-daemon.js";
|
||||
import { createTestAgentClients } from "../test-utils/fake-agent-client.js";
|
||||
import type { AgentClient, AgentProvider, AgentSessionConfig } from "./agent-sdk-types.js";
|
||||
import { PARENT_AGENT_ID_LABEL } from "@getpaseo/protocol/agent-labels";
|
||||
|
||||
interface StructuredContent {
|
||||
@@ -31,7 +33,7 @@ function str(val: unknown): string {
|
||||
}
|
||||
|
||||
function recordArr(val: unknown): StructuredContent[] {
|
||||
return z.array(z.record(z.unknown())).parse(val);
|
||||
return z.array(z.record(z.string(), z.unknown())).parse(val);
|
||||
}
|
||||
|
||||
function expectAgentFeatureValue(snapshot: StructuredContent, featureId: string, value: unknown) {
|
||||
@@ -142,6 +144,56 @@ let agentScopedClient: McpClient;
|
||||
let parentAgentId: string;
|
||||
let parentAgentCwd: string;
|
||||
let worktreeRepoCwd: string;
|
||||
let launchConfigsByProvider: Record<AgentProvider, AgentSessionConfig[]>;
|
||||
|
||||
function createRecordingAgentClients(): Record<AgentProvider, AgentClient> {
|
||||
const baseClients = createTestAgentClients();
|
||||
launchConfigsByProvider = {};
|
||||
const wrappedClients: Record<AgentProvider, AgentClient> = {};
|
||||
|
||||
for (const [provider, client] of Object.entries(baseClients)) {
|
||||
const launchConfigs: AgentSessionConfig[] = [];
|
||||
launchConfigsByProvider[provider] = launchConfigs;
|
||||
const wrappedClient: AgentClient = {
|
||||
provider: client.provider,
|
||||
capabilities: client.capabilities,
|
||||
createSession: async (config, launchContext, options) => {
|
||||
launchConfigs.push(config);
|
||||
return await client.createSession(config, launchContext, options);
|
||||
},
|
||||
resumeSession: async (handle, overrides, launchContext) =>
|
||||
await client.resumeSession(handle, overrides, launchContext),
|
||||
listModels: async (options) => await client.listModels(options),
|
||||
isAvailable: async () => await client.isAvailable(),
|
||||
};
|
||||
if (client.listModes) {
|
||||
wrappedClient.listModes = async (options) => await client.listModes!(options);
|
||||
}
|
||||
if (client.resolveCreateConfig) {
|
||||
wrappedClient.resolveCreateConfig = (input) => client.resolveCreateConfig!(input);
|
||||
}
|
||||
if (client.isCreateConfigUnattended) {
|
||||
wrappedClient.isCreateConfigUnattended = (input) => client.isCreateConfigUnattended!(input);
|
||||
}
|
||||
if (client.listCommands) {
|
||||
wrappedClient.listCommands = async (config) => await client.listCommands!(config);
|
||||
}
|
||||
if (client.listFeatures) {
|
||||
wrappedClient.listFeatures = async (config) => await client.listFeatures!(config);
|
||||
}
|
||||
if (client.listImportableSessions) {
|
||||
wrappedClient.listImportableSessions = async (options) =>
|
||||
await client.listImportableSessions!(options);
|
||||
}
|
||||
if (client.importSession) {
|
||||
wrappedClient.importSession = async (input, context) =>
|
||||
await client.importSession!(input, context);
|
||||
}
|
||||
wrappedClients[provider] = wrappedClient;
|
||||
}
|
||||
|
||||
return wrappedClients;
|
||||
}
|
||||
|
||||
async function makeCwd(prefix: string): Promise<string> {
|
||||
return await mkdtemp(path.join(tempRoot, `${prefix}-`));
|
||||
@@ -232,7 +284,7 @@ beforeAll(async () => {
|
||||
parentAgentCwd = await makeCwd("parent-agent-cwd");
|
||||
worktreeRepoCwd = await makeCwd("worktree-repo");
|
||||
|
||||
daemonHandle = await createTestPaseoDaemon();
|
||||
daemonHandle = await createTestPaseoDaemon({ agentClients: createRecordingAgentClients() });
|
||||
topLevelClient = await createMcpClient(`http://127.0.0.1:${daemonHandle.port}/mcp/agents`);
|
||||
|
||||
const parentPayload = await callToolStructured(topLevelClient, "create_agent", {
|
||||
@@ -302,10 +354,11 @@ describe("Suite A: Core Fixes", () => {
|
||||
try {
|
||||
const listenTarget = daemonHandle.daemon.getListenTarget();
|
||||
expect(listenTarget?.type).toBe("tcp");
|
||||
const cwd = await makeCwd("manager-direct-agent-cwd");
|
||||
|
||||
const snapshot = await daemonHandle.daemon.agentManager.createAgent({
|
||||
provider: "claude",
|
||||
cwd: await makeCwd("manager-direct-agent-cwd"),
|
||||
cwd,
|
||||
title: "Manager direct parity agent",
|
||||
modeId: "bypassPermissions",
|
||||
});
|
||||
@@ -317,20 +370,19 @@ describe("Suite A: Core Fixes", () => {
|
||||
agentId,
|
||||
});
|
||||
|
||||
expect(snapshot.config.mcpServers).toMatchObject({
|
||||
const launchConfig = launchConfigsByProvider.claude
|
||||
?.toReversed()
|
||||
.find((config) => config.cwd === cwd);
|
||||
expect(launchConfig?.mcpServers).toMatchObject({
|
||||
paseo: {
|
||||
type: "http",
|
||||
url: expectedUrl,
|
||||
},
|
||||
});
|
||||
expect(snapshot.config.mcpServers?.paseo).toBeUndefined();
|
||||
|
||||
const liveAgent = daemonHandle.daemon.agentManager.getAgent(agentId);
|
||||
expect(liveAgent?.config.mcpServers).toMatchObject({
|
||||
paseo: {
|
||||
type: "http",
|
||||
url: expectedUrl,
|
||||
},
|
||||
});
|
||||
expect(liveAgent?.config.mcpServers?.paseo).toBeUndefined();
|
||||
} finally {
|
||||
await archiveAgentIfPresent(agentId);
|
||||
}
|
||||
@@ -355,7 +407,7 @@ describe("Suite A: Core Fixes", () => {
|
||||
expect(internalSnapshot?.config.featureValues).toEqual({ test_feature: true });
|
||||
|
||||
const status = await callToolStructured(topLevelClient, "get_agent_status", { agentId });
|
||||
const snapshot = z.record(z.unknown()).parse(status.snapshot);
|
||||
const snapshot = z.record(z.string(), z.unknown()).parse(status.snapshot);
|
||||
expectAgentFeatureValue(snapshot, "test_feature", true);
|
||||
} finally {
|
||||
await archiveAgentIfPresent(agentId);
|
||||
@@ -373,7 +425,7 @@ describe("Suite A: Core Fixes", () => {
|
||||
expect(internalSnapshot?.config.featureValues).toEqual({ test_feature: true });
|
||||
|
||||
const status = await callToolStructured(topLevelClient, "get_agent_status", { agentId });
|
||||
const snapshot = z.record(z.unknown()).parse(status.snapshot);
|
||||
const snapshot = z.record(z.string(), z.unknown()).parse(status.snapshot);
|
||||
expectAgentFeatureValue(snapshot, "test_feature", true);
|
||||
} finally {
|
||||
await archiveAgentIfPresent(agentId);
|
||||
@@ -393,7 +445,7 @@ describe("Suite A: Core Fixes", () => {
|
||||
expect(internalSnapshot?.config.featureValues).toEqual({ test_feature: true });
|
||||
|
||||
const status = await callToolStructured(topLevelClient, "get_agent_status", { agentId });
|
||||
const snapshot = z.record(z.unknown()).parse(status.snapshot);
|
||||
const snapshot = z.record(z.string(), z.unknown()).parse(status.snapshot);
|
||||
expectAgentFeatureValue(snapshot, "test_feature", true);
|
||||
} finally {
|
||||
await archiveAgentIfPresent(agentId);
|
||||
|
||||
@@ -6,7 +6,6 @@ import { join, resolve as resolvePath } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import Ajv from "ajv";
|
||||
import { z } from "zod";
|
||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
||||
|
||||
import { createTestLogger } from "../../test-utils/test-logger.js";
|
||||
import { createAgentMcpServer } from "./mcp-server.js";
|
||||
@@ -114,7 +113,11 @@ async function invokeToolWithParsedInput(
|
||||
|
||||
function expectOutputSchemaAccepts(tool: RegisteredMcpTool, data: unknown): void {
|
||||
expect(tool.outputSchema).toBeDefined();
|
||||
const jsonSchema = zodToJsonSchema(tool.outputSchema as z.ZodTypeAny);
|
||||
const jsonSchema = z.toJSONSchema(tool.outputSchema as z.ZodType, {
|
||||
target: "draft-07",
|
||||
unrepresentable: "any",
|
||||
io: "input",
|
||||
});
|
||||
const ajv = new Ajv({ allErrors: true, strict: false });
|
||||
const validate = ajv.compile(jsonSchema);
|
||||
expect(validate(data), JSON.stringify(validate.errors, null, 2)).toBe(true);
|
||||
@@ -123,7 +126,7 @@ function expectOutputSchemaAccepts(tool: RegisteredMcpTool, data: unknown): void
|
||||
function agentsOf(response: {
|
||||
structuredContent: LooseStructuredContent;
|
||||
}): Array<Record<string, unknown>> {
|
||||
return z.array(z.record(z.unknown())).parse(response.structuredContent.agents);
|
||||
return z.array(z.record(z.string(), z.unknown())).parse(response.structuredContent.agents);
|
||||
}
|
||||
|
||||
type AgentManagerSpies = ReturnType<typeof buildAgentManagerSpies>;
|
||||
@@ -3363,7 +3366,7 @@ describe("agent snapshot MCP serialization", () => {
|
||||
const tool = registeredTool(server, "list_agents");
|
||||
const response = await tool.handler({});
|
||||
const structured = z
|
||||
.object({ agents: z.array(z.record(z.unknown())) })
|
||||
.object({ agents: z.array(z.record(z.string(), z.unknown())) })
|
||||
.parse(response.structuredContent);
|
||||
|
||||
expect(structured).toEqual({
|
||||
@@ -3485,7 +3488,7 @@ describe("agent snapshot MCP serialization", () => {
|
||||
});
|
||||
const tool = registeredTool(server, "get_agent_status");
|
||||
const response = await tool.handler({ agentId: "full-detail-agent" });
|
||||
const snapshot = z.record(z.unknown()).parse(response.structuredContent.snapshot);
|
||||
const snapshot = z.record(z.string(), z.unknown()).parse(response.structuredContent.snapshot);
|
||||
|
||||
const parsed = AgentSnapshotPayloadSchema.safeParse(snapshot);
|
||||
if (!parsed.success) {
|
||||
|
||||
@@ -169,9 +169,11 @@ function formatStructuredContentForModel(structuredContent: unknown): string {
|
||||
return summary.length > 0 ? `${summary.join("\n")}\n\n${json}` : json;
|
||||
}
|
||||
|
||||
function isZodSchema(value: unknown): value is z.ZodTypeAny {
|
||||
function isZodSchema(value: unknown): value is z.ZodType {
|
||||
return (
|
||||
typeof value === "object" && value !== null && "_def" in value && "safeParseAsync" in value
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
typeof (value as { safeParseAsync?: unknown }).safeParseAsync === "function"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -714,7 +716,7 @@ export async function createAgentMcpServer(options: AgentMcpServerOptions): Prom
|
||||
modeId: z.string().optional().describe("Session mode to configure before the first run."),
|
||||
thinkingOptionId: z.string().optional().describe("Thinking option ID."),
|
||||
features: z
|
||||
.record(z.unknown())
|
||||
.record(z.string(), z.unknown())
|
||||
.optional()
|
||||
.describe("Provider-specific feature values, for example { fast_mode: true } for Codex."),
|
||||
})
|
||||
@@ -729,7 +731,7 @@ export async function createAgentMcpServer(options: AgentMcpServerOptions): Prom
|
||||
.optional()
|
||||
.describe("Thinking option ID. Pass null to clear."),
|
||||
features: z
|
||||
.record(z.unknown())
|
||||
.record(z.string(), z.unknown())
|
||||
.optional()
|
||||
.describe("Provider-specific feature values, for example { fast_mode: true } for Codex."),
|
||||
})
|
||||
@@ -739,7 +741,10 @@ export async function createAgentMcpServer(options: AgentMcpServerOptions): Prom
|
||||
modeId: z.string().optional().describe("Draft session mode ID."),
|
||||
model: z.string().optional().describe("Draft model ID."),
|
||||
thinkingOptionId: z.string().optional().describe("Draft thinking option ID."),
|
||||
features: z.record(z.unknown()).optional().describe("Draft provider feature values."),
|
||||
features: z
|
||||
.record(z.string(), z.unknown())
|
||||
.optional()
|
||||
.describe("Draft provider feature values."),
|
||||
})
|
||||
.strict();
|
||||
const agentToAgentInputSchema = {
|
||||
|
||||
@@ -66,8 +66,8 @@ const cases: ParityCase[] = [
|
||||
},
|
||||
{
|
||||
label: "non-existent path (canonicalize fallback)",
|
||||
// realpath will throw; canonicalize falls back to NFC of input. SDK does
|
||||
// the same, so paths should still match.
|
||||
// realpath will throw; canonicalize falls back to the SDK's
|
||||
// platform-specific normalization of the input.
|
||||
build: async () => join(workspaceRoot, "this-path-was-never-created-" + randomUUID()),
|
||||
},
|
||||
];
|
||||
|
||||
@@ -6,8 +6,8 @@ import { join } from "node:path";
|
||||
// Verbatim port of the Claude Agent SDK's project-directory encoding so
|
||||
// paseo computes the same `~/.claude/projects/<dir>` path the SDK does.
|
||||
// The SDK ships only as a precompiled bundle; grep the JS source at
|
||||
// node_modules/@anthropic-ai/claude-agent-sdk/sdk.mjs for `function f1`,
|
||||
// `function i4`, `function tB`, `S0=200`.
|
||||
// node_modules/@anthropic-ai/claude-agent-sdk/sdk.mjs for `function Ar`,
|
||||
// `function So`, `async function wn`, `function Dy`, `Ni=200`.
|
||||
|
||||
const PROJECT_DIR_LENGTH_CAP = 200;
|
||||
|
||||
@@ -32,20 +32,24 @@ export function claudeProjectDirSync(cwd: string, options?: ClaudeProjectDirOpti
|
||||
|
||||
async function canonicalize(input: string): Promise<string> {
|
||||
try {
|
||||
return (await realpath(input)).normalize("NFC");
|
||||
return normalizeProjectPath(await realpath(input));
|
||||
} catch {
|
||||
return input.normalize("NFC");
|
||||
return normalizeProjectPath(input);
|
||||
}
|
||||
}
|
||||
|
||||
function canonicalizeSync(input: string): string {
|
||||
try {
|
||||
return realpathSync.native(input).normalize("NFC");
|
||||
return normalizeProjectPath(realpathSync.native(input));
|
||||
} catch {
|
||||
return input.normalize("NFC");
|
||||
return normalizeProjectPath(input);
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeProjectPath(input: string): string {
|
||||
return process.platform === "darwin" ? input.normalize("NFC") : input;
|
||||
}
|
||||
|
||||
function encode(input: string): string {
|
||||
const replaced = input.replace(/[^a-zA-Z0-9]/g, "-");
|
||||
if (replaced.length <= PROJECT_DIR_LENGTH_CAP) {
|
||||
|
||||
@@ -11,6 +11,61 @@ import {
|
||||
|
||||
export const CommandValueSchema = z.union([z.string(), z.array(z.string())]);
|
||||
|
||||
type ToolDetailSchema = z.ZodType;
|
||||
// Zod 4 loses the generic output type when these branch helpers constrain
|
||||
// schemas through z.ZodType, so the paired shape/value interfaces preserve the
|
||||
// transform callback types without changing runtime parsing.
|
||||
interface ToolDetailNameShape<
|
||||
Name extends string,
|
||||
InputSchema extends ToolDetailSchema,
|
||||
OutputSchema extends ToolDetailSchema,
|
||||
> {
|
||||
name: z.ZodLiteral<Name>;
|
||||
input: z.ZodNullable<InputSchema>;
|
||||
output: z.ZodNullable<OutputSchema>;
|
||||
}
|
||||
interface ToolDetailToolNameShape<
|
||||
Name extends string,
|
||||
InputSchema extends ToolDetailSchema,
|
||||
OutputSchema extends ToolDetailSchema,
|
||||
> {
|
||||
toolName: z.ZodLiteral<Name>;
|
||||
input: z.ZodNullable<InputSchema>;
|
||||
output: z.ZodNullable<OutputSchema>;
|
||||
}
|
||||
interface ToolDetailNameWithCwdShape<
|
||||
Name extends string,
|
||||
InputSchema extends ToolDetailSchema,
|
||||
OutputSchema extends ToolDetailSchema,
|
||||
> extends ToolDetailNameShape<Name, InputSchema, OutputSchema> {
|
||||
cwd: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
||||
}
|
||||
interface ToolDetailNameValue<
|
||||
Name extends string,
|
||||
InputSchema extends ToolDetailSchema,
|
||||
OutputSchema extends ToolDetailSchema,
|
||||
> {
|
||||
name: Name;
|
||||
input: z.output<InputSchema> | null;
|
||||
output: z.output<OutputSchema> | null;
|
||||
}
|
||||
interface ToolDetailToolNameValue<
|
||||
Name extends string,
|
||||
InputSchema extends ToolDetailSchema,
|
||||
OutputSchema extends ToolDetailSchema,
|
||||
> {
|
||||
toolName: Name;
|
||||
input: z.output<InputSchema> | null;
|
||||
output: z.output<OutputSchema> | null;
|
||||
}
|
||||
interface ToolDetailNameWithCwdValue<
|
||||
Name extends string,
|
||||
InputSchema extends ToolDetailSchema,
|
||||
OutputSchema extends ToolDetailSchema,
|
||||
> extends ToolDetailNameValue<Name, InputSchema, OutputSchema> {
|
||||
cwd?: string | null;
|
||||
}
|
||||
|
||||
export const ToolShellInputSchema = z
|
||||
.union([
|
||||
z
|
||||
@@ -424,11 +479,13 @@ interface ToolReadOutputValue {
|
||||
content?: string;
|
||||
}
|
||||
|
||||
export const ToolReadOutputSchema: z.ZodType<ToolReadOutputValue, z.ZodTypeDef, unknown> =
|
||||
export const ToolReadOutputSchema: z.ZodType<ToolReadOutputValue, unknown> =
|
||||
ToolReadOutputContentSchema;
|
||||
|
||||
export const ToolReadOutputWithPathSchema: z.ZodType<ToolReadOutputValue, z.ZodTypeDef, unknown> =
|
||||
z.union([ToolReadOutputContentSchema, ToolReadOutputPathSchema]);
|
||||
export const ToolReadOutputWithPathSchema: z.ZodType<ToolReadOutputValue, unknown> = z.union([
|
||||
ToolReadOutputContentSchema,
|
||||
ToolReadOutputPathSchema,
|
||||
]);
|
||||
|
||||
export const ToolWriteContentSchema = z
|
||||
.object({
|
||||
@@ -918,8 +975,8 @@ export function toFetchToolDetail(
|
||||
|
||||
export function toolDetailBranchByName<
|
||||
Name extends string,
|
||||
InputSchema extends z.ZodTypeAny,
|
||||
OutputSchema extends z.ZodTypeAny,
|
||||
InputSchema extends ToolDetailSchema,
|
||||
OutputSchema extends ToolDetailSchema,
|
||||
>(
|
||||
name: Name,
|
||||
inputSchema: InputSchema,
|
||||
@@ -929,20 +986,23 @@ export function toolDetailBranchByName<
|
||||
output: z.infer<OutputSchema> | null,
|
||||
) => ToolCallDetail | undefined,
|
||||
) {
|
||||
const schema = z.object({
|
||||
const shape: ToolDetailNameShape<Name, InputSchema, OutputSchema> = {
|
||||
name: z.literal(name),
|
||||
input: inputSchema.nullable(),
|
||||
output: outputSchema.nullable(),
|
||||
});
|
||||
input: z.nullable(inputSchema),
|
||||
output: z.nullable(outputSchema),
|
||||
};
|
||||
const schema = z.object(shape);
|
||||
return schema.transform((value: z.infer<typeof schema>) => {
|
||||
return mapper(value.input, value.output);
|
||||
// Zod v4 drops generic unknown-valued shape fields from object output inference here.
|
||||
const parsedValue = value as ToolDetailNameValue<Name, InputSchema, OutputSchema>;
|
||||
return mapper(parsedValue.input, parsedValue.output);
|
||||
});
|
||||
}
|
||||
|
||||
export function toolDetailBranchByToolName<
|
||||
Name extends string,
|
||||
InputSchema extends z.ZodTypeAny,
|
||||
OutputSchema extends z.ZodTypeAny,
|
||||
InputSchema extends ToolDetailSchema,
|
||||
OutputSchema extends ToolDetailSchema,
|
||||
>(
|
||||
toolName: Name,
|
||||
inputSchema: InputSchema,
|
||||
@@ -952,20 +1012,22 @@ export function toolDetailBranchByToolName<
|
||||
output: z.infer<OutputSchema> | null,
|
||||
) => ToolCallDetail | undefined,
|
||||
) {
|
||||
const schema = z.object({
|
||||
const shape: ToolDetailToolNameShape<Name, InputSchema, OutputSchema> = {
|
||||
toolName: z.literal(toolName),
|
||||
input: inputSchema.nullable(),
|
||||
output: outputSchema.nullable(),
|
||||
});
|
||||
input: z.nullable(inputSchema),
|
||||
output: z.nullable(outputSchema),
|
||||
};
|
||||
const schema = z.object(shape);
|
||||
return schema.transform((value: z.infer<typeof schema>) => {
|
||||
return mapper(value.input, value.output);
|
||||
const parsedValue = value as ToolDetailToolNameValue<Name, InputSchema, OutputSchema>;
|
||||
return mapper(parsedValue.input, parsedValue.output);
|
||||
});
|
||||
}
|
||||
|
||||
export function toolDetailBranchByNameWithCwd<
|
||||
Name extends string,
|
||||
InputSchema extends z.ZodTypeAny,
|
||||
OutputSchema extends z.ZodTypeAny,
|
||||
InputSchema extends ToolDetailSchema,
|
||||
OutputSchema extends ToolDetailSchema,
|
||||
>(
|
||||
name: Name,
|
||||
inputSchema: InputSchema,
|
||||
@@ -976,13 +1038,15 @@ export function toolDetailBranchByNameWithCwd<
|
||||
cwd: string | null,
|
||||
) => ToolCallDetail | undefined,
|
||||
) {
|
||||
const schema = z.object({
|
||||
const shape: ToolDetailNameWithCwdShape<Name, InputSchema, OutputSchema> = {
|
||||
name: z.literal(name),
|
||||
input: inputSchema.nullable(),
|
||||
output: outputSchema.nullable(),
|
||||
input: z.nullable(inputSchema),
|
||||
output: z.nullable(outputSchema),
|
||||
cwd: z.string().optional().nullable(),
|
||||
});
|
||||
};
|
||||
const schema = z.object(shape);
|
||||
return schema.transform((value: z.infer<typeof schema>) => {
|
||||
return mapper(value.input, value.output, value.cwd ?? null);
|
||||
const parsedValue = value as ToolDetailNameWithCwdValue<Name, InputSchema, OutputSchema>;
|
||||
return mapper(parsedValue.input, parsedValue.output, parsedValue.cwd ?? null);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -249,7 +249,7 @@ export const PersistedConfigSchema = z
|
||||
// localhost service proxying remains always enabled.
|
||||
enabled: z.boolean().optional(),
|
||||
listen: z.string().optional(),
|
||||
publicBaseUrl: z.string().url().optional(),
|
||||
publicBaseUrl: z.url().optional(),
|
||||
})
|
||||
.strict()
|
||||
.optional(),
|
||||
|
||||
@@ -544,7 +544,7 @@ const MIN_STREAMING_SEGMENT_DURATION_MS = 1000;
|
||||
const MIN_STREAMING_SEGMENT_BYTES = Math.round(
|
||||
PCM_BYTES_PER_MS * MIN_STREAMING_SEGMENT_DURATION_MS,
|
||||
);
|
||||
const AgentIdSchema = z.string().uuid();
|
||||
const AgentIdSchema = z.guid();
|
||||
interface VoiceModeBaseConfig {
|
||||
systemPrompt?: string;
|
||||
}
|
||||
|
||||
@@ -45,9 +45,13 @@ export interface LocalSpeechSttLanguageConfig {
|
||||
const NumberLikeSchema = z.union([z.number(), z.string().trim().min(1)]);
|
||||
const LanguageSchema = z.string().trim().min(1).default(DEFAULT_STT_LANGUAGE);
|
||||
|
||||
const OptionalFiniteNumberSchema = NumberLikeSchema.pipe(z.coerce.number().finite()).optional();
|
||||
const OptionalFiniteNumberSchema = NumberLikeSchema.pipe(
|
||||
z.coerce.number<string | number>().finite(),
|
||||
).optional();
|
||||
|
||||
const OptionalIntegerSchema = NumberLikeSchema.pipe(z.coerce.number().int()).optional();
|
||||
const OptionalIntegerSchema = NumberLikeSchema.pipe(
|
||||
z.coerce.number<string | number>().int(),
|
||||
).optional();
|
||||
|
||||
const LocalSpeechResolutionSchema = z.object({
|
||||
includeProviderConfig: z.boolean(),
|
||||
|
||||
@@ -85,9 +85,7 @@ function resolveDefaultModelId(role: DefaultModelRole): SherpaOnnxModelId {
|
||||
export const DEFAULT_LOCAL_STT_MODEL = resolveDefaultModelId("stt");
|
||||
export const DEFAULT_LOCAL_TTS_MODEL = resolveDefaultModelId("tts");
|
||||
|
||||
function createModelIdSchema<T extends string>(
|
||||
modelIds: readonly T[],
|
||||
): z.ZodType<T, z.ZodTypeDef, string> {
|
||||
function createModelIdSchema<T extends string>(modelIds: readonly T[]): z.ZodType<T, string> {
|
||||
const validIds = new Set<string>(modelIds);
|
||||
return z
|
||||
.string()
|
||||
|
||||
@@ -21,7 +21,9 @@ const OpenAiTtsModelSchema = z.enum(["tts-1", "tts-1-hd"]);
|
||||
|
||||
const NumberLikeSchema = z.union([z.number(), z.string().trim().min(1)]);
|
||||
|
||||
const OptionalFiniteNumberSchema = NumberLikeSchema.pipe(z.coerce.number().finite()).optional();
|
||||
const OptionalFiniteNumberSchema = NumberLikeSchema.pipe(
|
||||
z.coerce.number<string | number>().finite(),
|
||||
).optional();
|
||||
|
||||
const OptionalTrimmedStringSchema = z
|
||||
.string()
|
||||
|
||||
@@ -78,7 +78,7 @@ type RegistryRecord = PersistedProjectRecord | PersistedWorkspaceRecord;
|
||||
class FileBackedRegistry<TRecord extends RegistryRecord> {
|
||||
private readonly filePath: string;
|
||||
private readonly logger: Logger;
|
||||
private readonly schema: z.ZodType<TRecord, z.ZodTypeDef, unknown>;
|
||||
private readonly schema: z.ZodType<TRecord, unknown>;
|
||||
private readonly getId: (record: TRecord) => string;
|
||||
private loaded = false;
|
||||
private readonly cache = new Map<string, TRecord>();
|
||||
@@ -87,7 +87,7 @@ class FileBackedRegistry<TRecord extends RegistryRecord> {
|
||||
constructor(options: {
|
||||
filePath: string;
|
||||
logger: Logger;
|
||||
schema: z.ZodType<TRecord, z.ZodTypeDef, unknown>;
|
||||
schema: z.ZodType<TRecord, unknown>;
|
||||
getId: (record: TRecord) => string;
|
||||
component: string;
|
||||
}) {
|
||||
|
||||
Reference in New Issue
Block a user