mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 19:51:20 -05:00
40c6dade8e
* test(mcp): consolidate reduced output schema proof at the SDK boundary * test(mcp): verify both required fields on the reduced output root
105 lines
3.4 KiB
TypeScript
105 lines
3.4 KiB
TypeScript
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { compactAdvertisedOutputSchema } from "./compactOutputSchema.js";
|
|
|
|
const serializedBytes = (value: unknown): number =>
|
|
Buffer.byteLength(JSON.stringify(value), "utf8");
|
|
|
|
const annotatedSchema = {
|
|
type: "object",
|
|
description: "Root result guidance stays.",
|
|
properties: {
|
|
value: {
|
|
type: "string",
|
|
description: "Field guidance leaves.",
|
|
examples: ["seed"],
|
|
default: "seed",
|
|
title: "Value",
|
|
},
|
|
nested: {
|
|
type: "object",
|
|
description: "Nested guidance leaves.",
|
|
properties: {
|
|
count: { type: "integer", description: "Leaves.", minimum: 1 },
|
|
},
|
|
required: ["count"],
|
|
additionalProperties: false,
|
|
},
|
|
},
|
|
required: ["value", "nested"],
|
|
additionalProperties: false,
|
|
} as const;
|
|
|
|
const referencedSchema = {
|
|
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
$id: "urn:rea:tool-output:sha256:fixture",
|
|
type: "object",
|
|
properties: {
|
|
entries: { type: "array", items: { $ref: "#/$defs/entry" } },
|
|
},
|
|
required: ["entries"],
|
|
$defs: {
|
|
entry: {
|
|
type: "object",
|
|
properties: { name: { type: "string" } },
|
|
required: ["name"],
|
|
},
|
|
},
|
|
} as const;
|
|
|
|
describe("compact advertised output schema", () => {
|
|
it("drops nested annotation prose while keeping the root guidance and validation", () => {
|
|
const { schema, presentation } = compactAdvertisedOutputSchema(
|
|
structuredClone(annotatedSchema),
|
|
4 * 1024,
|
|
);
|
|
expect(presentation).toBe("inline");
|
|
expect(schema.description).toBe("Root result guidance stays.");
|
|
const value = (schema.properties as Record<string, unknown>)
|
|
.value as Record<string, unknown>;
|
|
expect(value).toEqual({ type: "string" });
|
|
const nested = (schema.properties as Record<string, unknown>)
|
|
.nested as Record<string, unknown>;
|
|
expect(nested.description).toBeUndefined();
|
|
expect(nested.required).toEqual(["count"]);
|
|
expect(schema.required).toEqual(["value", "nested"]);
|
|
expect(schema.additionalProperties).toBe(false);
|
|
});
|
|
|
|
it("inlines local definitions and drops the content-bound identity", () => {
|
|
const { schema, presentation } = compactAdvertisedOutputSchema(
|
|
structuredClone(referencedSchema),
|
|
4 * 1024,
|
|
);
|
|
expect(presentation).toBe("inline");
|
|
expect(schema.$defs).toBeUndefined();
|
|
expect(schema.$id).toBeUndefined();
|
|
expect(schema.$schema).toBe("https://json-schema.org/draft/2020-12/schema");
|
|
const entries = (schema.properties as Record<string, unknown>)
|
|
.entries as Record<string, unknown>;
|
|
expect(entries.items).toMatchObject({ type: "object" });
|
|
});
|
|
|
|
it("keeps a non-object root accepting every JSON value when it cannot fit", () => {
|
|
const arraySchema = {
|
|
type: "array",
|
|
items: Object.fromEntries(
|
|
Array.from({ length: 200 }, (_, index) => [
|
|
`field_${index}`,
|
|
{ type: "string", description: `Field ${index}` },
|
|
]),
|
|
),
|
|
};
|
|
const { schema, presentation } = compactAdvertisedOutputSchema(
|
|
arraySchema,
|
|
128,
|
|
);
|
|
expect(presentation).toBe("reduced");
|
|
expect(serializedBytes(schema)).toBeLessThanOrEqual(128);
|
|
const validate = new Ajv2020({ strict: false }).compile(schema);
|
|
expect(validate(["anything"])).toBe(true);
|
|
expect(validate({ also: "anything" })).toBe(true);
|
|
});
|
|
});
|