From a0e254a681b7a799fdb4b7bb69668ce7168990e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=A9=98?= Date: Sat, 6 Jun 2026 01:12:13 +0000 Subject: [PATCH] fix: render const values as literals in output format instruction (#129) buildOutputFormatInstruction now renders const fields with their actual value (e.g. $status: greeted) instead of the type placeholder (). Also adds early return in resolvePropertySchema for const properties. Fixes #129 --- .changeset/const-prompt-literal.md | 9 ++++++ .../build-output-format-instruction.test.ts | 30 +++++++++++++++++++ .../src/build-output-format-instruction.ts | 9 ++++++ 3 files changed, 48 insertions(+) create mode 100644 .changeset/const-prompt-literal.md diff --git a/.changeset/const-prompt-literal.md b/.changeset/const-prompt-literal.md new file mode 100644 index 0000000..465cdc1 --- /dev/null +++ b/.changeset/const-prompt-literal.md @@ -0,0 +1,9 @@ +--- +"@united-workforce/util-agent": patch +--- + +fix: render const values as literals in output format instruction (#129) + +Previously `buildOutputFormatInstruction` rendered `const: greeted` as +`$status: `, causing agents to output `$status: const` instead of +the actual value. Now const fields render as `$status: greeted # required | fixed value`. diff --git a/packages/util-agent/__tests__/build-output-format-instruction.test.ts b/packages/util-agent/__tests__/build-output-format-instruction.test.ts index a975743..1e8509c 100644 --- a/packages/util-agent/__tests__/build-output-format-instruction.test.ts +++ b/packages/util-agent/__tests__/build-output-format-instruction.test.ts @@ -225,4 +225,34 @@ describe("buildOutputFormatInstruction", () => { const result = buildOutputFormatInstruction({}); expect(result).toContain("Focus exclusively on YOUR role"); }); + + test("renders const value as literal in flat schema example", () => { + const schema = { + type: "object", + properties: { + $status: { type: "string", const: "greeted" }, + message: { type: "string" }, + }, + required: ["$status", "message"], + }; + const result = buildOutputFormatInstruction(schema); + expect(result).toContain("$status: greeted"); + expect(result).toContain("fixed value"); + expect(result).not.toContain("$status: "); + }); + + test("renders const value for non-string types", () => { + const schema = { + type: "object", + properties: { + count: { type: "number", const: 42 }, + done: { type: "boolean", const: true }, + }, + required: ["count", "done"], + }; + const result = buildOutputFormatInstruction(schema); + expect(result).toContain("count: 42"); + expect(result).toContain("done: true"); + expect(result).toContain("fixed value"); + }); }); diff --git a/packages/util-agent/src/build-output-format-instruction.ts b/packages/util-agent/src/build-output-format-instruction.ts index 2df7e73..b83465b 100644 --- a/packages/util-agent/src/build-output-format-instruction.ts +++ b/packages/util-agent/src/build-output-format-instruction.ts @@ -74,6 +74,10 @@ function collectObjectSchemas(schema: JSONSchema): JSONSchema[] { } function resolvePropertySchema(prop: JSONSchema): JSONSchema { + if (prop.const !== undefined) { + return prop; + } + if (Array.isArray(prop.enum) && prop.enum.length > 0) { return prop; } @@ -113,6 +117,11 @@ function buildPropertyExampleLine(prop: SchemaProperty): string { commentParts.push("required"); } + if (resolved.const !== undefined) { + commentParts.push("fixed value"); + return `${prop.name}: ${formatYamlScalar(resolved.const)}${buildPropertyComment(commentParts)}`; + } + if (Array.isArray(resolved.enum) && resolved.enum.length > 0) { const enumValues = resolved.enum.map((v) => String(v)); commentParts.push(...enumValues); -- 2.43.0