Files
plugins/packages/kiro/structured-output.test.mjs
yetone 060bda8f65 kiro 0.1.12: a request's structured output (output_config.format) is told in the system prompt, as the built-in told it
Kiro's generateAssistantResponse has no field for the answer's format, so
magpie's built-in Kiro (internal/gateway/kiro.go, req.inSystem()) told
the model the schema at the end of the system prompt: "Respond with a
single JSON value and nothing else, matching this JSON schema:\n<schema>."
The plugin dropped output_config.format, and magpie's generic plugin path
falls back to inSystem only when an upstream answers 400 naming the
format, which the plugin never did, so a moved user asking for JSON got
prose. buildKiro now says the schema the same way, after the caller's
system prompt and before the thinking budget prefix. The AI SDK's
output_format is read too. A JSON object with no schema needs nothing
here: magpie's Anthropic body already tells it in the system prompt.

Run: HOME=$(mktemp -d) bun test in packages/kiro: 46 pass, 0 fail.
structured-output.test.mjs: 3 of its 4 tests red with the index.mjs
change taken out (the fourth checks a request with no format is
unchanged). bun scripts/check.mjs kiro: ok.
Not run: a real Kiro account, nor magpie end to end.
2026-10-16 17:32:22 +08:00

40 lines
2.4 KiB
JavaScript

// A request's structured output reaches Kiro in words. Kiro's
// generateAssistantResponse has no field for the answer's format, so
// magpie's built-in told the model the schema in the system prompt
// (internal/gateway/kiro.go: req.inSystem(), format.go's instruction). The
// plugin dropped output_config.format, so a caller asking for JSON got
// prose back.
import { expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const { buildKiro } = _internal
const first = (r, budget = 0) => {
const s = JSON.parse(buildKiro(r, "claude-sonnet-4.5", "", budget)).conversationState
return (s.history?.[0] ?? s.currentMessage).userInputMessage.content
}
const schema = { type: "object", properties: { answer: { type: "string" } }, required: ["answer"], additionalProperties: false }
const told = 'Respond with a single JSON value and nothing else, matching this JSON schema:\n{"type":"object","properties":{"answer":{"type":"string"}},"required":["answer"],"additionalProperties":false}.'
test("output_config.format's schema is told at the end of the system prompt, as the built-in told it", () => {
const req = { system: "Be brief.", messages: [{ role: "user", content: "hi" }], output_config: { format: { type: "json_schema", schema } } }
expect(first(req)).toBe("Be brief.\n\n" + told + "\n\nhi")
})
test("with no system prompt the schema is the whole of it, after a thinking budget", () => {
const req = { messages: [{ role: "user", content: "hi" }], output_config: { effort: "high", format: { type: "json_schema", schema } } }
expect(first(req)).toBe(told + "\n\nhi")
expect(first(req, 30000)).toBe("<thinking_mode>enabled</thinking_mode><max_thinking_length>30000</max_thinking_length>\n" + told + "\n\nhi")
})
test("the AI SDK's output_format is told too", () => {
const req = { system: [{ type: "text", text: "Be brief." }], messages: [{ role: "user", content: "hi" }], output_format: { type: "json_schema", schema } }
expect(first(req)).toBe("Be brief.\n\n" + told + "\n\nhi")
})
test("a request with no format, or one with no schema, is sent as before", () => {
for (const extra of [{}, { output_config: { effort: "low" } }, { output_config: { format: { type: "json_schema" } } }, { output_config: { format: { type: "text" } } }]) {
const req = { system: "Be brief.", messages: [{ role: "user", content: "hi" }], ...extra }
expect(first(req)).toBe("Be brief.\n\nhi")
}
})