diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index e888215..8f6663c 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -1322,6 +1322,18 @@ const inID = (id) => { return raw && outID(raw) === id ? raw : id } +// formatText is the structured output a chat's response_format asks for, +// said in words as magpie's built-in says it (format.go's instruction): +// json_schema's schema, or any JSON object for json_object or a schema of +// no shape; "" for text or none. +function formatText(f) { + if (f?.type === "json_object") return "Respond with a single JSON object and nothing else." + if (f?.type !== "json_schema") return "" + const schema = f.json_schema ? f.json_schema.schema : f.schema + if (schema === undefined || schema === null) return "Respond with a single JSON object and nothing else." + return "Respond with a single JSON value and nothing else, matching this JSON schema:\n" + JSON.stringify(schema) + "." +} + // build is the GetChatMessage request for a chat completion, to the model // uid. function build(chat, uid, key) { @@ -1378,6 +1390,10 @@ function build(chat, uid, key) { msgs.push({ role: USER, text, images }) } answer() + // Devin 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(chat.response_format) + if (format) system.push(format) const last = msgs[msgs.length - 1] if (!last || (last.role === ASSISTANT && !last.calls.length)) msgs.push({ role: USER, text: "Please proceed with the task." }) diff --git a/packages/devin/package.json b/packages/devin/package.json index 94768e0..ec213ae 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.18", + "version": "0.1.19", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/devin/structured-output.test.mjs b/packages/devin/structured-output.test.mjs new file mode 100644 index 0000000..bdf2046 --- /dev/null +++ b/packages/devin/structured-output.test.mjs @@ -0,0 +1,59 @@ +// A chat's structured output reaches Devin in words. Devin's +// GetChatMessage has no field for the answer's format, so magpie's built-in +// told the model in the instructions (internal/gateway/devin.go: +// req.inSystem(), format.go's instruction). The plugin dropped +// response_format, so a caller asking for JSON got prose back. +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const dec = new TextDecoder() + +// sent is each message's role and text as build() gives them to Devin. +const sent = (chat) => + _internal + .fields(_internal.build(chat, "swe-2-high", "k")) + .filter((f) => f.num === 3 && f.wire === 2) + .map((f) => { + const m = { role: 0, text: "" } + for (const g of _internal.fields(f.data)) { + if (g.num === 2) m.role = g.n + else if (g.num === 3) m.text = dec.decode(g.data) + } + return m + }) + +const schema = { type: "object", properties: { answer: { type: "string" } }, required: ["answer"] } +const told = 'Respond with a single JSON value and nothing else, matching this JSON schema:\n{"type":"object","properties":{"answer":{"type":"string"}},"required":["answer"]}.' +const object = "Respond with a single JSON object and nothing else." +const chat = (response_format) => ({ messages: [{ role: "system", content: "Be brief." }, { role: "user", content: "hi" }], response_format }) + +test("response_format's json_schema is told at the end of the instructions, as the built-in told it", () => { + expect(sent(chat({ type: "json_schema", json_schema: { name: "r", schema, strict: true } }))[0].text).toBe("Be brief.\n\n" + told + "\n\nhi") +}) + +test("json_object, or a json_schema with no schema, asks for any JSON object", () => { + expect(sent(chat({ type: "json_object" }))[0].text).toBe("Be brief.\n\n" + object + "\n\nhi") + expect(sent(chat({ type: "json_schema", json_schema: { name: "r" } }))[0].text).toBe("Be brief.\n\n" + object + "\n\nhi") +}) + +test("with no system message the format is the whole of the instructions", () => { + expect(sent({ messages: [{ role: "user", content: "hi" }], response_format: { type: "json_object" } })[0].text).toBe(object + "\n\nhi") +}) + +test("text, or no response_format, is sent as before", () => { + expect(sent(chat({ type: "text" }))[0].text).toBe("Be brief.\n\nhi") + expect(sent(chat(undefined))[0].text).toBe("Be brief.\n\nhi") +}) + +// magpie says a failed tool's result as "Error: …", as the built-in did +// (devin.go: IsError); it goes to Devin as it came +test("a failed tool's result goes to Devin as magpie worded it", () => { + const got = sent({ + messages: [ + { role: "user", content: "run it" }, + { role: "assistant", content: "", tool_calls: [{ id: "call_a", type: "function", function: { name: "bash", arguments: "{}" } }] }, + { role: "tool", tool_call_id: "call_a", content: "Error: exit status 1" }, + ], + }) + expect(got.find((m) => m.role === 4).text).toBe("Error: exit status 1") +})