pi-mono/packages/ai/test/compat-schema.test.ts
Mario Zechner 6536f80129 feat(ai,agent,coding-agent): validate tool arguments with pi-schema, drop TypeBox
pi-schema: opt-in coercion, compile(schema, { coerce: true }) with coerce() and
parse(), the superset of TypeBox's Value.Convert rules and pi-ai's old coercion;
unresolvedRefs option; url and json-pointer-uri-fragment formats.
pi-ai: Tool takes any JSON Schema; validation through pi-schema; Type, Static
and TSchema are no longer exported (breaking). pi-agent-core and pi-durable:
tools on pi-schema, no TypeBox dependency. Coding agent: extensions keep Type,
Static, TSchema and StringEnum through an extension view of pi-ai; built-in
tools stay on TypeBox.
2026-11-01 10:08:42 +00:00

49 lines
2.2 KiB
TypeScript

import type { Static } from "@earendil-works/pi-schema";
import { describe, expect, expectTypeOf, it } from "vitest";
import {
AnthropicMessagesCompatSchema,
BedrockCompatSchema,
OpenAICompletionsCompatSchema,
OpenAIResponsesCompatSchema,
ProviderCompatSchema,
} from "../src/providers/compat-schema.ts";
type ProviderCompat = Static<typeof ProviderCompatSchema>;
/** The `default` annotation of each property of an object schema. */
function defaults(schema: { readonly [keyword: string]: unknown }): Record<string, unknown> {
const properties = schema.properties as Record<string, { default?: unknown }>;
return Object.fromEntries(
Object.entries(properties)
.filter(([, property]) => property.default !== undefined)
.map(([name, property]) => [name, property.default]),
);
}
describe("compatibility schemas", () => {
it("preserves property types in the provider superset", () => {
expectTypeOf<ProviderCompat["supportsStore"]>().toEqualTypeOf<boolean | undefined>();
expectTypeOf<ProviderCompat["sessionAffinityFormat"]>().toEqualTypeOf<
"openai" | "openai-nosession" | "openrouter" | undefined
>();
});
it("preserves API-specific defaults", () => {
const completionsDefaults = defaults(OpenAICompletionsCompatSchema);
expect(completionsDefaults).not.toHaveProperty("thinkingFormat");
expect(completionsDefaults).not.toHaveProperty("supportsLongCacheRetention");
expect(defaults(OpenAIResponsesCompatSchema)).toMatchObject({ supportsDeveloperRole: true });
const anthropicDefaults = defaults(AnthropicMessagesCompatSchema);
expect(anthropicDefaults).not.toHaveProperty("sendSessionAffinityHeaders");
expect(anthropicDefaults).toMatchObject({ supportsLongCacheRetention: true });
expect(defaults(BedrockCompatSchema)).toMatchObject({ supportsStrictMode: false });
});
it("does not assign API-specific defaults to the provider superset", () => {
const providerDefaults = defaults(ProviderCompatSchema);
expect(providerDefaults).not.toHaveProperty("supportsDeveloperRole");
expect(providerDefaults).not.toHaveProperty("sendSessionAffinityHeaders");
expect(providerDefaults).not.toHaveProperty("supportsLongCacheRetention");
expect(providerDefaults).not.toHaveProperty("supportsStrictMode");
});
});