diff --git a/packages/kiro/cache-points.test.mjs b/packages/kiro/cache-points.test.mjs new file mode 100644 index 0000000..316cfae --- /dev/null +++ b/packages/kiro/cache-points.test.mjs @@ -0,0 +1,87 @@ +// The caller's prompt-cache marks reach Kiro (#110: "kiro cli cache 率为 0"). +// Kiro's generateAssistantResponse takes Bedrock's cachePoint +// ({"type":"default"}) on a userInputMessage, on an assistantResponseMessage +// and as an entry of the tools (the CodeWhisperer streaming model's +// UserInputMessage.cachePoint, AssistantResponseMessage.cachePoint and +// Tool::CachePoint; Kiro CLI 2.27's model list tells +// supportsPromptCaching and maximumCacheCheckpointsPerRequest). The plugin +// dropped every cache_control, so Kiro was never asked to cache. +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const { buildKiro } = _internal +const sent = (r) => JSON.parse(buildKiro(r, "claude-sonnet-4.5", "", 0)).conversationState +const EPH = { type: "ephemeral" } +const POINT = { type: "default" } + +// where Claude Code 2.1.296 put its marks, from a captured request (texts +// cut short): the last two system blocks, and the last message, the +// system-role message holding a hook's context after a tool result +const claudeCode = { + model: "claude-sonnet-4.5", + system: [ + { type: "text", text: "x-anthropic-billing-header: cc_version=2" }, + { type: "text", text: "You are a Claude agent, built on Anthropic's Claude Agent SDK.", cache_control: EPH }, + { type: "text", text: "You are an agent working with the user …", cache_control: EPH }, + ], + messages: [ + { role: "user", content: [{ type: "text", text: "\nAttribution …" }, { type: "text", text: "run echo hi" }] }, + { role: "system", content: "# Environment\nYou have been invoked in the following environment: …" }, + { role: "assistant", content: [{ type: "tool_use", id: "toolu_01", name: "Bash", input: { command: "sleep 6; echo hi", description: "say hi" } }] }, + { role: "user", content: [{ tool_use_id: "toolu_01", type: "tool_result", is_error: false, content: "hi" }] }, + { role: "system", content: [{ type: "text", text: "PreToolUse:Bash hook additional context: …", cache_control: EPH }] }, + ], + tools: [{ name: "Bash", description: "Run a command", input_schema: { type: "object", properties: { command: { type: "string" } } } }], +} + +test("Claude Code's marks go on the messages they end up in", () => { + const s = sent(claudeCode) + // the instructions are at the head of the first message, so its mark + // covers them; the hook's context joins the tool result's message + expect(s.history[0].userInputMessage.cachePoint).toEqual(POINT) + expect(s.history[1].assistantResponseMessage.cachePoint).toBeUndefined() + expect(s.currentMessage.userInputMessage.cachePoint).toEqual(POINT) + expect(s.currentMessage.userInputMessage.userInputMessageContext.tools).toEqual([ + { toolSpecification: { name: "Bash", description: "Run a command", inputSchema: { json: claudeCode.tools[0].input_schema } } }, + ]) +}) + +test("a marked tool is followed by a cachePoint, as magpie marks the last tool of a translated request", () => { + const r = { + system: "You are Codex.", + messages: [ + { role: "user", content: [{ type: "text", text: "hi", cache_control: EPH }] }, + { role: "assistant", content: [{ type: "text", text: "hello", cache_control: EPH }] }, + { role: "user", content: "and now?" }, + ], + tools: [ + { name: "a", input_schema: { type: "object", properties: {} } }, + { name: "b", input_schema: { type: "object", properties: {} }, cache_control: EPH }, + ], + } + const s = sent(r) + expect(s.currentMessage.userInputMessage.userInputMessageContext.tools.map((t) => Object.keys(t)[0])).toEqual(["toolSpecification", "toolSpecification", "cachePoint"]) + expect(s.history[0].userInputMessage.cachePoint).toEqual(POINT) + expect(s.history[1].assistantResponseMessage.cachePoint).toEqual(POINT) + expect(s.currentMessage.userInputMessage.cachePoint).toBeUndefined() +}) + +test("nothing marked, nothing sent", () => { + const s = sent({ messages: [{ role: "user", content: "hi" }, { role: "assistant", content: "yo" }, { role: "user", content: "again" }] }) + expect(JSON.stringify(s)).not.toContain("cachePoint") +}) + +test("at most 4, the last ones kept", () => { + const msgs = [] + for (let i = 0; i < 6; i++) { + msgs.push({ role: "user", content: [{ type: "text", text: "q" + i, cache_control: EPH }] }) + msgs.push({ role: "assistant", content: "a" + i }) + } + msgs.push({ role: "user", content: [{ type: "text", text: "last", cache_control: EPH }] }) + const s = sent({ messages: msgs, tools: [{ name: "t", input_schema: { type: "object", properties: {} }, cache_control: EPH }] }) + const body = JSON.stringify(s) + expect(body.match(/cachePoint/g).length).toBe(4) + expect(s.currentMessage.userInputMessage.cachePoint).toEqual(POINT) + expect(s.currentMessage.userInputMessage.userInputMessageContext.tools.some((t) => t.cachePoint)).toBe(false) + expect(s.history.filter((e) => e.userInputMessage?.cachePoint).map((e) => e.userInputMessage.content)).toEqual(["q3", "q4", "q5"]) +}) diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index ff74659..ce373b0 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -646,6 +646,12 @@ function thinking(req, model) { // buildKiro is the body of a generateAssistantResponse call. function buildKiro(req, model, profile, budget) { const entries = [] + // the messages the caller marked for its prompt cache (cache_control), + // which go to Kiro as a cachePoint on the message they end up in (#110) + const marked = new WeakSet() + const mark = (msg, parts) => { + if (parts.some((p) => p?.cache_control)) marked.add(msg) + } const user = () => ({ content: "", modelId: model, origin: "KIRO_CLI" }) for (const m of req.messages ?? []) { const parts = typeof m.content === "string" ? [{ type: "text", text: m.content }] : m.content ?? [] @@ -666,6 +672,7 @@ function buildKiro(req, model, profile, budget) { prev.content = joinNonEmpty(prev.content, a.content) prev.toolUses.push(...a.toolUses) } else entries.push({ assistantResponseMessage: a }) + mark(prev ?? a, parts) continue } const u = user() @@ -693,6 +700,7 @@ function buildKiro(req, model, profile, budget) { prev.images.push(...u.images) if (results.length) (prev.userInputMessageContext ??= { toolResults: [] }).toolResults.push(...results) } else entries.push({ userInputMessage: u }) + mark(prev ?? u, parts) } // the conversation opens with something the user said: tool results with // no call before them go, and a reply the caller began with @@ -748,6 +756,7 @@ function buildKiro(req, model, profile, budget) { let system = typeof req.system === "string" ? req.system : textOf(req.system) 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) // the caller's tools, and any the conversation used that it no longer // offers — Kiro rejects a history naming a tool it wasn't given @@ -760,6 +769,7 @@ function buildKiro(req, model, profile, budget) { const name = toolName(t.name) offered.add(name) tools.push({ toolSpecification: { name, description: t.description || t.name, inputSchema: { json: t.input_schema ?? EMPTY } } }) + if (t.cache_control) tools.push({ cachePoint: { type: "default" } }) } for (const e of entries) for (const c of e.assistantResponseMessage?.toolUses ?? []) @@ -768,6 +778,19 @@ function buildKiro(req, model, profile, budget) { tools.push({ toolSpecification: { name: c.name, description: "Tool", inputSchema: { json: EMPTY } } }) } if (tools.length) (current.userInputMessageContext ??= {}).tools = tools + // Kiro's cachePoint is Bedrock's: it caches the prompt up to the point, + // and a request may hold 4 at most, as Anthropic's cache_control; past + // that the last ones are kept, the most of the prompt they cover + const points = [] + for (const t of tools) if (t.cachePoint) points.push(() => tools.splice(tools.indexOf(t), 1)) + for (const e of entries) { + const msg = e.userInputMessage ?? e.assistantResponseMessage + if (marked.has(msg)) { + msg.cachePoint = { type: "default" } + points.push(() => delete msg.cachePoint) + } + } + for (const drop of points.slice(0, Math.max(0, points.length - 4))) drop() const state = { chatTriggerType: "MANUAL", agentTaskType: "vibe", conversationId: randomUUID(), currentMessage: { userInputMessage: current } } if (entries.length > 1) state.history = entries.slice(0, -1) diff --git a/packages/kiro/package.json b/packages/kiro/package.json index efd3878..c111693 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.9", + "version": "0.1.10", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./index.mjs",