diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index 0df5d09..9279728 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -645,6 +645,17 @@ function thinking(req, model) { return 20000 } +// formatText is the structured output a request asks for, said in words: +// Anthropic's output_config.format (or the AI SDK's output_format) with a +// JSON schema, "" when it asks for none. A JSON object with no schema +// reaches the plugin already told in the system prompt, as Messages has no +// field for it. +function formatText(req) { + const f = req.output_config?.format ?? req.output_format + if (f?.type !== "json_schema" || !f.schema || typeof f.schema !== "object") return "" + return "Respond with a single JSON value and nothing else, matching this JSON schema:\n" + JSON.stringify(f.schema) + "." +} + // buildKiro is the body of a generateAssistantResponse call. function buildKiro(req, model, profile, budget) { const entries = [] @@ -756,6 +767,10 @@ function buildKiro(req, model, profile, budget) { for (const e of entries) if (e.assistantResponseMessage && !e.assistantResponseMessage.toolUses.length) delete e.assistantResponseMessage.toolUses // the instructions go at the head of the first message let system = typeof req.system === "string" ? req.system : textOf(req.system) + // Kiro has no field for the answer's format: it is told in words, at the + // end of the instructions, as magpie's built-in tells it (inSystem) + const format = formatText(req) + if (format) system = system ? system + "\n\n" + format : format if (budget) system = `enabled${budget}` + (system ? "\n" + system : "") if (system) entries[0].userInputMessage.content = system + "\n\n" + entries[0].userInputMessage.content if (Array.isArray(req.system)) mark(entries[0].userInputMessage, req.system) diff --git a/packages/kiro/package.json b/packages/kiro/package.json index e257ff8..fa3478f 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.11", + "version": "0.1.12", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/kiro/structured-output.test.mjs b/packages/kiro/structured-output.test.mjs new file mode 100644 index 0000000..04e8f05 --- /dev/null +++ b/packages/kiro/structured-output.test.mjs @@ -0,0 +1,39 @@ +// 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("enabled30000\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") + } +})