diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index a973c40..733e206 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -1648,10 +1648,13 @@ async function* events(body, model, budget) { if (budget) yield* think.feed(text) else if (text) yield { kind: "text", text } } else if (kind === "reasoningContentEvent") { + // one event may carry the reasoning and its signature together (auto, + // called as the IDE): the signature goes on the block it ends if (m.text) { said += m.text.length yield { kind: "think", text: m.text } - } else if (m.signature) yield { kind: "sig", text: m.signature } + } + if (m.signature) yield { kind: "sig", text: m.signature } } else if (kind === "toolUseEvent") { if (m.toolUseId && m.toolUseId !== tool) { yield* think.end() // the text before a call is all said diff --git a/packages/kiro/package.json b/packages/kiro/package.json index 74c5486..326c6b2 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.30", + "version": "0.1.31", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./opencode.mjs", diff --git a/packages/kiro/reasoning-signature.test.mjs b/packages/kiro/reasoning-signature.test.mjs new file mode 100644 index 0000000..38a2e56 --- /dev/null +++ b/packages/kiro/reasoning-signature.test.mjs @@ -0,0 +1,67 @@ +// Called as the Kiro IDE (AI_EDITOR), Kiro's auto ends a reply with one +// reasoningContentEvent carrying text and its signature together. These are +// the payloads of a real reply (2026-11-07, auto, "Reply with the single word +// OK."); the signature was dropped and the block sent with an empty one. +import { test, expect } from "bun:test" +import { _internal } from "./index.mjs" + +const { events, reply } = _internal + +function frame(headers, payload) { + const enc = new TextEncoder() + const hs = [] + for (const [k, v] of Object.entries(headers)) { + const name = enc.encode(k) + const val = enc.encode(v) + const h = new Uint8Array(1 + name.length + 1 + 2 + val.length) + h[0] = name.length + h.set(name, 1) + h[1 + name.length] = 7 + new DataView(h.buffer).setUint16(2 + name.length, val.length) + h.set(val, 4 + name.length) + hs.push(h) + } + const hlen = hs.reduce((n, h) => n + h.length, 0) + const body = enc.encode(JSON.stringify(payload)) + const total = 12 + hlen + body.length + 4 + const out = new Uint8Array(total) + const v = new DataView(out.buffer) + v.setUint32(0, total) + v.setUint32(4, hlen) + let i = 12 + for (const h of hs) { + out.set(h, i) + i += h.length + } + out.set(body, i) + return out +} + +const stream = (...fs) => + new ReadableStream({ + start(c) { + for (const f of fs) c.enqueue(f) + c.close() + }, + }) + +const ev = (type, payload) => frame({ ":message-type": "event", ":event-type": type }, payload) +const SIG = ".KTR~~eyJlbmNyeXB0aW9uUmVnaW9uIjpudWxsLCJtaW50ZWRBdE1zIjoxNzk0MDU1MjcyODg0LCJtb2RlbEhhc2giOiJ5RUxWOXhiY2Z3dlUiLCJzbG90cyI6W10sInYiOjF9" +const body = () => stream( + ev("assistantResponseEvent", { content: "OK", modelId: "auto" }), + ev("reasoningContentEvent", { signature: SIG, text: "..." }), + ev("metadataEvent", { stopReason: "END_TURN" }), +) + +test("reasoning sent with its signature in one event keeps the signature", async () => { + const res = await reply(events(body(), "auto", 0), "auto", false) + const j = await res.json() + expect(j.content).toEqual([{ type: "text", text: "OK" }, { type: "thinking", thinking: "...", signature: SIG }]) +}) + +test("streamed, the signature follows the reasoning in its block", async () => { + const res = await reply(events(body(), "auto", 0), "auto", true) + const out = await res.text() + expect(out).toContain(JSON.stringify({ type: "thinking_delta", thinking: "..." })) + expect(out).toContain(JSON.stringify({ type: "signature_delta", signature: SIG })) +})