diff --git a/packages/kiro/README.md b/packages/kiro/README.md new file mode 100644 index 0000000..f0b641e --- /dev/null +++ b/packages/kiro/README.md @@ -0,0 +1,92 @@ +# @magpie-community/opencode-kiro-auth + +Signs in to a [Kiro](https://kiro.dev) subscription (Free, Pro, Pro+, +Power) and makes its requests, in OpenCode and in magpie. Provider id: +`kiro`. + +## Signing in + +- **Kiro (Google, GitHub, AWS Builder ID, IAM Identity Center)**: the Kiro + IDE's own sign-in. + - Kiro's page (`app.kiro.dev/signin`) sends the browser back to one of + the ports the IDE listens on (3128, 4649, 6588, 8008, 9091, 49153, + 50153–53153) on `127.0.0.1`. While the IDE is signing in, those ports + may be busy. + - Google and GitHub come back with a code. Kiro's auth service trades it + for tokens. + - Builder ID and Identity Center come back with an AWS sign-in URL and + region. The plugin registers a client with AWS's OIDC service, sends + the browser to AWS, and trades the code AWS returns. + - The account is named by the email Kiro has for it (`Get-Usage-Limits`), + and its plan is read from the same call. + - A company's own identity provider (`external_idp`) is not supported + here. Sign in with `kiro-cli login` and use the next method instead. +- **Kiro CLI's or Kiro IDE's sign-in**: uses the account kiro-cli is signed + in to, or else the Kiro IDE's. Nothing is copied. It is read each time + from where they keep it: + - kiro-cli: its SQLite database's `auth_kv` table. + - macOS: `~/Library/Application Support/kiro-cli/data.sqlite3` + - Linux: `~/.local/share/kiro-cli/data.sqlite3` + - Windows: `%APPDATA%\kiro-cli\data.sqlite3` + - The IDE: `~/.aws/sso/cache/kiro-auth-token.json`. + + kiro-cli refreshes its own token first (`kiro-cli debug refresh-auth-token`). + A token the plugin refreshes itself is written back, so kiro-cli and the + IDE go on with it. +- **Kiro API key (`ksk_…`)**: sent with `tokentype: API_KEY`. Its profile is + found with `GetProfile`. + +A browser sign-in is kept where OpenCode keeps sign-ins (`auth.json`; in +magpie, `plugin-auth.json`) as an `oauth` entry. The entry holds the +tokens, the profile ARN, the region and, for AWS, the registered client. +Tokens are refreshed 2 minutes before they expire, and also when Kiro +turns one down (a 403, after which the request is tried once more): + +- Google/GitHub: through `prod..auth.desktop.kiro.dev/refreshToken`. +- Builder ID/Identity Center: through AWS's `oidc..amazonaws.com/token`. + +## Requests + +Kiro has no API OpenCode speaks. The models are declared on Anthropic's +Messages (`@ai-sdk/anthropic`), and the plugin's `fetch` handles each +request: + +- It sends the request to `runtime..kiro.dev/generateAssistantResponse`, + the API kiro-cli uses, with kiro-cli's headers. The region is the one the + profile ARN names. +- It turns the request into a Kiro conversation: + - The system prompt goes at the head of the first message. + - Tool calls and results are paired: an unanswered call gets an error + result, and a result longer than 250,000 characters is cut. + - Tool ids Kiro wouldn't take are rewritten. + - Only the latest images are sent. + - Tools the history used but the request doesn't offer are declared. +- It turns the AWS event stream Kiro answers with back into Messages' + events (streamed) or one message. The reply includes text, thinking, + tool calls and usage. When Kiro reports only how full the context is, + input tokens are estimated from that. +- Thinking (Claude models and Auto) is asked for the way kiro-cli asks, in + the prompt, with a budget: low 10k, medium 20k, high 30k, max 50k. The + `` block of the reply comes back as thinking. +- Kiro's failures keep their meaning: + - input too long → 400 + - no capacity → 503 + - usage limit → 429 + - throttling → "rate limited" + +## Models + +The `config` hook declares `auto` (Kiro picks the model). Once signed in, +the `provider.models` hook replaces it with the account's own list from +`List-Available-Models`, with the default first. Each model carries its +context window, output limit and whether it takes images. Claude models +and Auto have the thinking variants. + +## Not included + +- Usage and quota (credits, the monthly window). +- Switching between several accounts. OpenCode keeps one sign-in per + provider. +- Signing in with a company's own identity provider in the browser (use + kiro-cli's sign-in). +- magpie's web-search stand-in for Kiro. diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs new file mode 100644 index 0000000..4268013 --- /dev/null +++ b/packages/kiro/index.mjs @@ -0,0 +1,1292 @@ +// Kiro's subscriptions (kiro.dev) as an OpenCode provider plugin. +// +// An account is signed in the way the Kiro IDE signs in: Kiro's sign-in +// page (app.kiro.dev) offers Google, GitHub, AWS Builder ID and IAM +// Identity Center, and sends the browser back to one of the ports the IDE +// listens on. Google and GitHub come back with a code Kiro's auth service +// trades for tokens; Builder ID and Identity Center come back with where to +// sign in at AWS, which is then done with a client registered for it. +// kiro-cli's or the IDE's own sign-in can be used instead: it is read where +// they keep it each time, refreshed by kiro-cli first, and a token +// refreshed here is written back, so they go on with it. A Kiro API key +// (ksk_…) signs in too. +// +// Kiro has no API OpenCode speaks: the models are declared on Anthropic's +// Messages, and the fetch here sends each request whole to Kiro's own API +// (runtime..kiro.dev/generateAssistantResponse, the one kiro-cli +// talks to) as a conversation, and turns the AWS event stream it answers +// with back into Messages' events. +import { createServer } from "node:http" +import { createHash, randomBytes, randomUUID } from "node:crypto" +import { execFile } from "node:child_process" +import { existsSync, statSync } from "node:fs" +import { readFile, rename, writeFile } from "node:fs/promises" +import { homedir, hostname } from "node:os" +import { delimiter, join } from "node:path" + +const ID = "kiro" +const MESSAGES = "@ai-sdk/anthropic" +const BASE = "https://runtime.us-east-1.kiro.dev" // what OpenCode is given; the fetch sends each request to the account's region + +const PORTAL = "https://app.kiro.dev" +const AUTH_SERVICE = "https://prod.us-east-1.auth.desktop.kiro.dev" +const oidc = (region) => `https://oidc.${region}.amazonaws.com` +const runtime = (region) => `https://runtime.${region}.kiro.dev` +const management = (region) => `https://management.${region}.kiro.dev` +const refreshURL = (region) => `https://prod.${region}.auth.desktop.kiro.dev/refreshToken` +// the ports Kiro's sign-in page sends the browser back to, the IDE's +const CALLBACK_PORTS = [3128, 4649, 6588, 8008, 9091, 49153, 50153, 51153, 52153, 53153] +// what the IDE asks AWS for +const SCOPES = ["codewhisperer:completions", "codewhisperer:analysis", "codewhisperer:conversations", + "codewhisperer:transformations", "codewhisperer:taskassist"] +// how the IDE names itself to Kiro's auth service, and when refreshing +const IDE_UA = "KiroIDE-1.1.70-" + createHash("sha256").update("magpie:" + hostname()).digest("hex") +const DESKTOP_UA = "Kiro-Desktop/0.2.13 (darwin; arm64)" +const SIGN_IN_TIMEOUT = 10 * 60 * 1000 + +const env = (k) => process.env[k] ?? "" +const home = () => env("HOME") || homedir() + +// ---- where Kiro's own apps keep their sign-in ----------------------------------- + +function cliDB() { + if (process.platform === "win32") return join(env("APPDATA") || join(home(), "AppData", "Roaming"), "kiro-cli", "data.sqlite3") + if (process.platform === "darwin") return join(home(), "Library", "Application Support", "kiro-cli", "data.sqlite3") + return join(home(), ".local", "share", "kiro-cli", "data.sqlite3") +} +const ideDir = () => join(home(), ".aws", "sso", "cache") + +// kiroExecutable finds kiro-cli, which refreshes its own sign-in. +function kiroExecutable() { + for (const d of env("PATH").split(delimiter)) { + const p = join(d, process.platform === "win32" ? "kiro-cli.exe" : "kiro-cli") + if (d && isFile(p)) return p + } + for (const p of [join(home(), ".local", "bin", "kiro-cli"), "/Applications/Kiro CLI.app/Contents/MacOS/kiro-cli", + "/usr/local/bin/kiro-cli", "/opt/homebrew/bin/kiro-cli"]) + if (isFile(p)) return p + return "" +} +function isFile(p) { + try { + return statSync(p).isFile() + } catch { + return false + } +} + +// openDB opens kiro-cli's SQLite database, with Bun's driver or Node's. +async function openDB(path, readonly) { + try { + const { Database } = await import("bun:sqlite") + return new Database(path, readonly ? { readonly: true } : { readwrite: true }) + } catch (e) { + if (typeof Bun !== "undefined") throw e + } + const { DatabaseSync } = await import("node:sqlite") + return new DatabaseSync(path, { readOnly: readonly }) +} + +const time = (s) => { + const t = Date.parse(s ?? "") + return Number.isFinite(t) ? t : 0 +} + +// readCLI reads kiro-cli's sign-in: with Google or GitHub, with AWS (Builder +// ID, IAM Identity Center), or with a company's own identity provider. +async function readCLI() { + const path = cliDB() + if (!existsSync(path)) return null + let db + try { + db = await openDB(path, true) + const get = (key) => { + const row = db.prepare("SELECT value FROM auth_kv WHERE key = ?").get(key) + if (!row?.value) return null + try { + return JSON.parse(row.value) + } catch { + return null + } + } + for (const kind of ["social", "odic", "external-idp"]) { + const key = `kirocli:${kind}:token` + const m = get(key) + if (!m?.access_token) continue + const c = { access: m.access_token, refresh: m.refresh_token ?? "", expires: time(m.expires_at), region: m.region || "us-east-1", + profile: m.profile_arn ?? "", dbKey: key } + if (kind === "social") c.method = "social" + else if (kind === "odic") { + c.method = "idc" + const reg = get("kirocli:odic:device-registration") + if (reg) [c.clientId, c.clientSecret] = [reg.client_id ?? "", reg.client_secret ?? ""] + } else { + c.method = "external-idp" + c.clientId = m.client_id ?? "" + c.tokenURL = m.token_endpoint || (m.issuer_url ? m.issuer_url.replace(/\/$/, "") + "/v1/token" : "") + } + return c + } + } catch { + } finally { + db?.close() + } + return null +} + +// readIDE reads the Kiro IDE's sign-in. +async function readIDE() { + const path = join(ideDir(), "kiro-auth-token.json") + let t + try { + t = JSON.parse(await readFile(path, "utf8")) + } catch { + return null + } + if (!t?.accessToken) return null + const c = { access: t.accessToken, refresh: t.refreshToken ?? "", expires: time(t.expiresAt), region: t.region || "us-east-1", + profile: t.profileArn ?? "", method: "idc", idePath: path } + if (t.clientIdHash) { + try { + const reg = JSON.parse(await readFile(join(ideDir(), t.clientIdHash + ".json"), "utf8")) + ;[c.clientId, c.clientSecret] = [reg.clientId ?? "", reg.clientSecret ?? ""] + } catch {} + } + if (String(t.authMethod).toLowerCase() === "social" || !c.clientId) c.method = "social" + return c +} + +// saveBack writes a refreshed token back where it was read from, so +// kiro-cli or the IDE goes on with it (a refresh may replace the refresh +// token). +async function saveBack(c) { + const expires = new Date(c.expires).toISOString() + if (c.dbKey) { + let db + try { + db = await openDB(cliDB(), false) + db.exec("PRAGMA busy_timeout = 3000") + const row = db.prepare("SELECT value FROM auth_kv WHERE key = ?").get(c.dbKey) + if (!row?.value) return + const m = JSON.parse(row.value) + Object.assign(m, { access_token: c.access, refresh_token: c.refresh, expires_at: expires }) + if (c.profile && c.method === "social") m.profile_arn = c.profile + db.prepare("UPDATE auth_kv SET value = ? WHERE key = ?").run(JSON.stringify(m), c.dbKey) + } catch { + } finally { + db?.close() + } + } else if (c.idePath) { + try { + const m = JSON.parse(await readFile(c.idePath, "utf8")) + Object.assign(m, { accessToken: c.access, refreshToken: c.refresh, expiresAt: expires }) + await writeFile(c.idePath + ".magpie-tmp", JSON.stringify(m, null, 2), { mode: 0o600 }) + await rename(c.idePath + ".magpie-tmp", c.idePath) + } catch {} + } +} + +// ---- tokens -------------------------------------------------------------------- + +const EXPIRED = "Kiro's sign-in has expired; sign in again" +const fresh = (c) => c.method === "apikey" || !c.expires || c.expires - Date.now() > 2 * 60 * 1000 + +async function post(url, contentType, body, headers = {}) { + let res + try { + res = await fetch(url, { method: "POST", headers: { "Content-Type": contentType, Accept: "application/json", ...headers }, body, + signal: AbortSignal.timeout(30_000) }) + } catch (e) { + throw new Error("refreshing Kiro's sign-in: " + e.message) + } + const text = await res.text() + if (!res.ok) { + if ([400, 401, 403].includes(res.status)) throw new Error(EXPIRED) + throw new Error(`refreshing Kiro's sign-in: ${res.status}`) + } + return JSON.parse(text) +} + +// refresh refreshes a sign-in's token the way its owner would: kiro-cli's +// by kiro-cli first. +async function refresh(c) { + if (c.dbKey) { + const bin = kiroExecutable() + if (bin) { + await new Promise((r) => execFile(bin, ["debug", "refresh-auth-token"], { timeout: 20_000 }, () => r())) + const n = await readCLI() + if (n && n.dbKey === c.dbKey && n.access !== c.access && fresh(n)) return n + } + } + if (!c.refresh) throw new Error(EXPIRED) + let out + switch (c.method) { + case "social": { + const r = await post(refreshURL(c.region), "application/json", JSON.stringify({ refreshToken: c.refresh }), { "User-Agent": DESKTOP_UA }) + out = { access: r.accessToken, refresh: r.refreshToken, expiresIn: r.expiresIn } + if (r.profileArn) c.profile = r.profileArn + break + } + case "idc": { + const r = await post(oidc(c.region) + "/token", "application/json", + JSON.stringify({ clientId: c.clientId, clientSecret: c.clientSecret, refreshToken: c.refresh, grantType: "refresh_token" })) + out = { access: r.accessToken, refresh: r.refreshToken, expiresIn: r.expiresIn } + break + } + case "external-idp": { + if (!c.tokenURL) throw new Error(EXPIRED + " with `kiro-cli login`") + const form = new URLSearchParams({ grant_type: "refresh_token", client_id: c.clientId ?? "", refresh_token: c.refresh }) + const r = await post(c.tokenURL, "application/x-www-form-urlencoded", form.toString()) + out = { access: r.access_token, refresh: r.refresh_token, expiresIn: r.expires_in } + break + } + default: + throw new Error(EXPIRED) + } + if (!out.access) throw new Error(EXPIRED) + const n = { ...c, access: out.access, expires: Date.now() + (out.expiresIn > 0 ? out.expiresIn : 3600) * 1000 } + if (out.refresh) n.refresh = out.refresh + await saveBack(n) + return n +} + +// headers say who is calling: the token, and what kind it is. +function authHeaders(a) { + const h = { Authorization: `Bearer ${a.token}` } + if (a.tokenType) h.tokentype = a.tokenType + return h +} +const tokenType = (c) => (c.method === "apikey" ? "API_KEY" : c.method === "external-idp" ? "EXTERNAL_IDP" : "") + +// region is where the account's API is: the region its profile is in, else +// the nearer of Kiro's two to where it signed in. +function regionOf(profile, signedIn) { + const parts = String(profile ?? "").split(":") + if (parts.length > 4 && parts[3]) return parts[3] + return String(signedIn ?? "").startsWith("eu-") ? "eu-central-1" : "us-east-1" +} + +class KiroError extends Error { + constructor(status, body) { + let msg = "" + try { + msg = JSON.parse(body)?.message ?? "" + } catch {} + super(msg ? `Kiro: ${msg} (${status})` : `Kiro: HTTP ${status}`) + this.status = status + } +} + +// managementCall calls Kiro's management API: GET with a query, or POST +// with a JSON body. +async function managementCall(region, a, method, query, body) { + let url = `${management(region)}/${method}` + if (!body && query) url += "?" + new URLSearchParams(query) + const res = await fetch(url, { + method: body ? "POST" : "GET", + headers: { ...authHeaders(a), Accept: "application/json", ...(body ? { "Content-Type": "application/json" } : {}) }, + body: body ? JSON.stringify(body) : undefined, + signal: AbortSignal.timeout(20_000), + }) + const text = await res.text() + if (!res.ok) throw new KiroError(res.status, text) + return JSON.parse(text) +} + +// profileOf finds the profile a sign-in that doesn't name one uses: an API +// key's own, or the first Kiro lists in either of its regions. +async function profileOf(c) { + if (c.method === "apikey") { + const res = await fetch(management("us-east-1") + "/", { + method: "POST", + headers: { ...authHeaders({ token: c.access, tokenType: "API_KEY" }), "Content-Type": "application/x-amz-json-1.0", + "X-Amz-Target": "AmazonCodeWhispererService.GetProfile" }, + body: "{}", + signal: AbortSignal.timeout(20_000), + }) + if (!res.ok) throw new Error(`Kiro didn't take the API key: ${res.status}`) + const arn = (await res.json().catch(() => null))?.profile?.arn + if (!arn) throw new Error("Kiro didn't say which profile the API key is for") + return arn + } + let last + for (const region of ["us-east-1", "eu-central-1"]) { + try { + const out = await managementCall(region, { token: c.access, tokenType: tokenType(c) }, "List-Available-Profiles", null, {}) + const arn = (out?.profiles ?? []).find((p) => p?.arn)?.arn + if (arn) return arn + } catch (e) { + last = e + } + } + throw last ?? new Error("Kiro has no profile for this sign-in") +} + +// usageLimits is Get-Usage-Limits: the plan, the account's email, credits. +function usageLimits(a, email) { + return managementCall(a.region, a, "Get-Usage-Limits", + { origin: "KIRO_CLI", profileArn: a.profile, resourceType: "CREDIT", isEmailRequired: String(email) }) +} + +// planName is the subscription's name as Kiro gives it ("KIRO FREE"), said +// "Kiro Free". +const planName = (title) => + String(title ?? "").toLowerCase().split(/\s+/).filter(Boolean).map((w) => (w === "kiro" ? "Kiro" : w[0].toUpperCase() + w.slice(1))).join(" ") + +// ---- the account, as the plugin holds it ------------------------------------------- + +// credOf is the sign-in a stored auth names: a key, one signed in here, or +// kiro-cli's or the IDE's, read where they keep it now. +async function credOf(auth) { + if (auth?.type === "api" && auth.key) return { access: auth.key, method: "apikey", region: "us-east-1" } + if (auth?.type !== "oauth") return null + if (auth.source === "kiro-cli") return readCLI() + if (auth.source === "kiro-ide") return readIDE() + if (auth.source === "kiro") return (await readCLI()) ?? (await readIDE()) + if (!auth.access) return null + return { access: auth.access, refresh: auth.refresh ?? "", expires: auth.expires ?? 0, method: auth.method || "social", + region: auth.region || "us-east-1", profile: auth.profileArn ?? "", clientId: auth.clientId ?? "", clientSecret: auth.clientSecret ?? "", + own: true } +} + +// Account holds the credentials in use, refreshing them when near their +// end or when Kiro turned one down; one refresh at a time, as a refresh +// may replace the refresh token. +function account(client) { + let held = null // { id, c } + let lock = Promise.resolve() + const locked = (fn) => { + const run = lock.then(fn, fn) + lock = run.catch(() => {}) + return run + } + const save = async (auth, c) => { + if (!c.own || !client?.auth?.set) return + const { type: _t, ...rest } = auth + await client.auth.set({ path: { id: ID }, body: { ...rest, type: "oauth", access: c.access, refresh: c.refresh, expires: c.expires, + profileArn: c.profile, region: c.region } }).catch(() => {}) + } + return (auth, stale = false) => + locked(async () => { + const id = auth?.type === "api" ? "key:" + auth.key : "oauth:" + (auth?.source ?? "own") + // what its owner holds now: it may have refreshed it, signed out, or + // signed in to another account since + const read = await credOf(auth) + if (!read) { + held = null + throw new Error(auth?.source ? "Kiro isn't signed in; sign in with `kiro-cli login` or the Kiro IDE" : "Kiro isn't signed in; sign in again") + } + let c = held?.id === id ? held.c : null + if (!c || read.access !== c.access || !fresh(c) || stale) { + if (!read.profile && c && read.access === c.access) read.profile = c.profile + if (stale && c && read.access !== c.access && fresh(read)) stale = false // the owner refreshed it already + c = read + if (!fresh(c) || stale) { + if (c.method === "apikey") throw new Error("Kiro turned down the API key") + c = await refresh(c) + await save(auth, c) + } + } + if (!c.profile) { + c.profile = await profileOf(c) + if (!auth?.profileArn) await save(auth, c) + } + held = { id, c } + return { token: c.access, tokenType: tokenType(c), profile: c.profile, region: regionOf(c.profile, c.region) } + }) +} + +// ---- models ---------------------------------------------------------------------- + +const windows = new Map() // a model's context, as Kiro last listed it + +const thinks = (id) => /claude/i.test(id) || id === "auto" +const BUDGETS = { low: 10000, medium: 20000, high: 30000, max: 50000 } + +function configModel(m) { + return { + name: m.name, + limit: { context: m.context ?? 0, output: m.output ?? 0 }, + ...(m.images ? { attachment: true, modalities: { input: ["text", "image"], output: ["text"] } } : {}), + ...(thinks(m.id) + ? { reasoning: true, variants: Object.fromEntries(Object.entries(BUDGETS).map(([k, b]) => [k, { thinking: { type: "enabled", budgetTokens: b } }])) } + : {}), + tool_call: true, + } +} + +function runtimeModel(m) { + const input = { text: true, image: !!m.images, audio: false, video: false, pdf: false } + return { + id: m.id, + providerID: ID, + name: m.name ?? m.id, + api: { id: m.id, url: BASE, npm: MESSAGES }, + status: "active", + headers: {}, + options: {}, + cost: { input: 0, output: 0, cache: { read: 0, write: 0 } }, + limit: { context: m.context ?? 0, output: m.output ?? 0 }, + capabilities: { + temperature: true, + reasoning: thinks(m.id), + attachment: !!m.images, + toolcall: true, + input, + output: { text: true, image: false, audio: false, video: false, pdf: false }, + interleaved: false, + }, + release_date: "", + variants: thinks(m.id) ? Object.fromEntries(Object.entries(BUDGETS).map(([k, b]) => [k, { thinking: { type: "enabled", budgetTokens: b } }])) : {}, + } +} + +// Kiro's own list is asked for once signed in; until then, Auto, which +// picks a model for each request. +const MODELS = [{ id: "auto", name: "Auto" }] + +// listModels is List-Available-Models, the default first. +async function listModels(a) { + const out = await managementCall(a.region, a, "List-Available-Models", { origin: "KIRO_CLI", profileArn: a.profile }) + const list = [] + for (const m of out?.models ?? []) { + if (!m?.modelId) continue + let name = m.modelName + if (!name || name === m.modelId) name = m.modelId === "auto" ? "Auto" : m.modelId + const model = { id: m.modelId, name, context: m.tokenLimits?.maxInputTokens ?? 0, output: m.tokenLimits?.maxOutputTokens ?? 0, + images: (m.supportedInputTypes ?? []).some((t) => String(t).toUpperCase() === "IMAGE") } + if (m.modelId === out?.defaultModel?.modelId) list.unshift(model) + else list.push(model) + } + if (!list.length) throw new Error("Kiro listed no models") + for (const m of list) windows.set(m.id, m.context) + return list +} + +// ---- the request: Messages → a Kiro conversation --------------------------------------- + +const PROCEED = "Please proceed with the task." // Kiro takes no empty message +const NO_RESULT = "Tool use was interrupted and did not produce a result." +const RESULT_LIMIT = 250000 // a tool's output is cut at this, as Kiro's own agent cuts it +const TOOL_ID = /^[a-zA-Z0-9_.:-]{1,64}$/ + +// toolID is a tool call's id as Kiro takes one: another vendor's becomes +// one of Kiro's own, the same each time. +const toolID = (id) => (TOOL_ID.test(id ?? "") ? id : "t_" + createHash("sha256").update(String(id ?? "")).digest("base64url").slice(0, 32)) + +function imageFormat(mediaType) { + const t = String(mediaType ?? "").toLowerCase() + const f = t.replace(/^image\//, "") + if (f === "jpg") return "jpeg" + if (!f || f === t) return "png" + return f +} + +const textOf = (c) => + typeof c === "string" ? c : Array.isArray(c) ? c.filter((p) => p?.type === "text").map((p) => p.text ?? "").join("\n\n") : "" +const joinNonEmpty = (a, b) => (a && b ? a + "\n\n" + b : a + b) + +function documentText(p) { + const title = p.title ? `${p.title}\n` : "" + if (p.source?.type === "text") return title + (p.source.data ?? "") + if (p.source?.type === "content") return title + textOf(p.source.content) + return `${title}[a ${p.source?.media_type ?? "document"} attachment]` +} + +// thinking is whether the model is asked to think aloud, and for how +// long: when the caller asked for it, of a Claude model (or Auto). +function thinking(req, model) { + const effort = req.output_config?.effort ?? "" + const on = req.thinking?.type === "enabled" || req.thinking?.type === "adaptive" || (effort && effort !== "low") + if (!on || !thinks(String(model).toLowerCase())) return 0 + const budget = { low: 10000, high: 30000, xhigh: 50000, max: 50000 }[effort] + if (budget) return budget + if (!effort && req.thinking?.budget_tokens > 0) return req.thinking.budget_tokens + return 20000 +} + +// buildKiro is the body of a generateAssistantResponse call. +function buildKiro(req, model, profile, budget) { + const entries = [] + 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 ?? [] + const texts = [] + if (m.role === "assistant") { + const a = { content: "", toolUses: [] } + for (const p of parts) { + if (p?.type === "text" && p.text) texts.push(p.text) + else if (p?.type === "tool_use") { + const input = p.input && typeof p.input === "object" && !Array.isArray(p.input) ? p.input : {} + a.toolUses.push({ name: p.name, toolUseId: toolID(p.id), input }) + } + } + a.content = texts.join("\n\n") + if (!a.content && !a.toolUses.length) continue // a turn that only thought + const prev = entries.at(-1)?.assistantResponseMessage + if (prev) { + prev.content = joinNonEmpty(prev.content, a.content) + prev.toolUses.push(...a.toolUses) + } else entries.push({ assistantResponseMessage: a }) + continue + } + const u = user() + u.images = [] + const results = [] + for (const p of parts) { + if (p?.type === "text") { + if (p.text) texts.push(p.text) + } else if (p?.type === "document") texts.push(documentText(p)) + else if (p?.type === "image") { + if (p.source?.type === "base64" && p.source.data) u.images.push({ format: imageFormat(p.source.media_type), source: { bytes: p.source.data } }) + } else if (p?.type === "tool_result") { + let out = textOf(p.content) + if (out.length > RESULT_LIMIT) out = out.slice(0, RESULT_LIMIT) + "\n… (cut)" + results.push({ toolUseId: toolID(p.tool_use_id), status: p.is_error ? "error" : "success", content: [{ text: out || "(no output)" }] }) + } + } + u.content = texts.join("\n\n") + if (results.length) u.userInputMessageContext = { toolResults: results } + const prev = entries.at(-1)?.userInputMessage + if (prev) { + prev.content = joinNonEmpty(prev.content, u.content) + prev.images.push(...u.images) + if (results.length) (prev.userInputMessageContext ??= { toolResults: [] }).toolResults.push(...results) + } else entries.push({ userInputMessage: u }) + } + // the conversation opens with something the user said: tool results with + // no call before them go, and a reply the caller began with + while (entries.length) { + const u = entries[0].userInputMessage + if (u) { + if (u.userInputMessageContext && !u.content && !u.images.length) { + entries.shift() + continue + } + delete u.userInputMessageContext + break + } + entries.shift() + } + // and ends with it, the message being answered + if (!entries.at(-1)?.userInputMessage) entries.push({ userInputMessage: { ...user(), images: [] } }) + // each call is answered in the message after it, and only calls are + entries.forEach((e, i) => { + const u = e.userInputMessage + if (!u) return + const calls = entries[i - 1]?.assistantResponseMessage?.toolUses ?? [] + const results = u.userInputMessageContext?.toolResults ?? [] + const answered = new Set() + const kept = [] + for (const r of results) + if (!answered.has(r.toolUseId) && calls.some((c) => c.toolUseId === r.toolUseId)) { + answered.add(r.toolUseId) + kept.push(r) + } + for (const c of calls) + if (!answered.has(c.toolUseId)) { + answered.add(c.toolUseId) + kept.push({ toolUseId: c.toolUseId, status: "error", content: [{ text: NO_RESULT }] }) + } + delete u.userInputMessageContext + if (kept.length) u.userInputMessageContext = { toolResults: kept } + if (!u.content && !u.userInputMessageContext) u.content = PROCEED + }) + // only the latest images are sent again, as Kiro's own agent does + let latest = -1 + for (let i = entries.length - 1; i >= 0; i--) + if (entries[i].userInputMessage?.images.length) { + latest = i + break + } + entries.forEach((e, i) => { + const u = e.userInputMessage + if (u && (i !== latest || !u.images.length)) delete u.images + }) + 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) + if (budget) system = `enabled${budget}` + (system ? "\n" + system : "") + if (system) entries[0].userInputMessage.content = system + "\n\n" + entries[0].userInputMessage.content + + // 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 + const current = entries.at(-1).userInputMessage + const tools = [] + const offered = new Set() + const EMPTY = { type: "object", properties: {} } + for (const t of req.tools ?? []) { + if (!t?.name || (t.type && t.type !== "custom" && !t.input_schema)) continue // server tools Kiro hasn't + offered.add(t.name) + tools.push({ toolSpecification: { name: t.name, description: t.description || t.name, inputSchema: { json: t.input_schema ?? EMPTY } } }) + } + for (const e of entries) + for (const c of e.assistantResponseMessage?.toolUses ?? []) + if (!offered.has(c.name)) { + offered.add(c.name) + tools.push({ toolSpecification: { name: c.name, description: "Tool", inputSchema: { json: EMPTY } } }) + } + if (tools.length) (current.userInputMessageContext ??= {}).tools = tools + + const state = { chatTriggerType: "MANUAL", agentTaskType: "vibe", conversationId: randomUUID(), currentMessage: { userInputMessage: current } } + if (entries.length > 1) state.history = entries.slice(0, -1) + const out = { conversationState: state, agentMode: "vibe" } + if (profile) out.profileArn = profile + return JSON.stringify(out) +} + +function sendKiro(a, body, signal) { + const ua = `aws-sdk-rust/1.0.0 ua/2.1 os/other lang/rust api/codewhispererstreaming#1.28.3 m/E app/AmazonQ-For-CLI md/appVersion-1.28.3-${randomUUID().replaceAll("-", "")}` + return fetch(runtime(a.region) + "/generateAssistantResponse", { + method: "POST", + headers: { + ...authHeaders(a), + "Content-Type": "application/json", + Accept: "application/vnd.amazon.eventstream", + "x-amzn-codewhisperer-optout": "true", + "amz-sdk-invocation-id": randomUUID(), + "amz-sdk-request": "attempt=1; max=1", + "x-amzn-kiro-agent-mode": "vibe", + "x-amz-user-agent": ua, + "User-Agent": ua, + }, + body, + signal, + }) +} + +// failure is the status and message for an error Kiro answered with: +// {"message": "...", "reason": "..."}. +function failure(status, body) { + let msg = String(body ?? "").trim() + try { + const e = JSON.parse(body) + if (e?.message) msg = e.message + (e.reason ? ` (${e.reason})` : "") + } catch {} + msg ||= `HTTP ${status}` + if (msg.includes("CONTENT_LENGTH_EXCEEDS_THRESHOLD") || msg.toLowerCase().includes("input is too long")) + return { status: 400, message: "input is too long for the model's context: " + msg } + if (msg.includes("INSUFFICIENT_MODEL_CAPACITY")) return { status: 503, message: msg } + if (msg.includes("MONTHLY_REQUEST_COUNT") || msg.includes("USAGE_LIMIT")) return { status: 429, message: "usage limit reached: " + msg } + return { status, message: msg } +} + +const ERROR_TYPES = { 400: "invalid_request_error", 401: "authentication_error", 402: "billing_error", 403: "permission_error", + 404: "not_found_error", 413: "request_too_large", 429: "rate_limit_error", 503: "overloaded_error", 529: "overloaded_error" } +const errorType = (status) => ERROR_TYPES[status] ?? "api_error" +const errorResponse = (status, message) => + new Response(JSON.stringify({ type: "error", error: { type: errorType(status), message: "Kiro: " + message } }), { + status, + headers: { "Content-Type": "application/json" }, + }) + +// ---- the reply: an AWS event stream → Messages --------------------------------------- + +// frames reads an AWS event stream: each message its length, its headers' +// length and a checksum, the headers, the payload, a checksum of the whole. +async function* frames(body) { + let buf = new Uint8Array(0) + const reader = body.getReader() + try { + for (;;) { + while (buf.length >= 12) { + const v = new DataView(buf.buffer, buf.byteOffset, buf.byteLength) + const total = v.getUint32(0) + const hlen = v.getUint32(4) + if (total < 16 || total > 16 << 20 || hlen > total - 16) throw new Error("a malformed event stream") + if (buf.length < total) break + const rest = buf.subarray(12, total) + yield { headers: frameHeaders(rest.subarray(0, hlen)), payload: rest.subarray(hlen, rest.length - 4) } + buf = buf.slice(total) + } + const { value, done } = await reader.read() + if (done) { + if (buf.length) throw new Error("unexpected EOF") + return + } + const n = new Uint8Array(buf.length + value.length) + n.set(buf) + n.set(value, buf.length) + buf = n + } + } finally { + reader.releaseLock?.() + } +} + +function frameHeaders(h) { + const out = {} + const dec = new TextDecoder() + const v = new DataView(h.buffer, h.byteOffset, h.byteLength) + let i = 0 + while (i < h.length) { + const n = h[i] + if (h.length < i + 2 + n) break + const name = dec.decode(h.subarray(i + 1, i + 1 + n)) + const type = h[i + 1 + n] + i += 2 + n + const size = { 0: 0, 1: 0, 2: 1, 3: 2, 4: 4, 5: 8, 8: 8, 9: 16 }[type] + if (type === 6 || type === 7) { + if (h.length < i + 2) break + const l = v.getUint16(i) + if (h.length < i + 2 + l) break + if (type === 7) out[name] = dec.decode(h.subarray(i + 2, i + 2 + l)) + i += 2 + l + continue + } + if (size === undefined || h.length < i + size) break + i += size + } + return out +} + +// exception is what an error in the stream says. +function exception(kind, payload) { + let e = {} + try { + e = JSON.parse(new TextDecoder().decode(payload)) + } catch {} + let msg = e.message || e.Message || new TextDecoder().decode(payload).trim() + if (e.reason) msg += ` (${e.reason})` + if (String(kind).toLowerCase().includes("throttl")) return "rate limited: " + msg + return kind ? `${kind}: ${msg}` : msg +} + +// ThinkParser splits a reply that opens with … into its +// thinking and the text after, however the tags fall across pieces. +const OPEN = "" +const CLOSE = "" +function partialSuffix(s, tag) { + for (let n = Math.min(tag.length - 1, s.length); n > 0; n--) if (s.endsWith(tag.slice(0, n))) return n + return 0 +} +class ThinkParser { + state = 0 // 0: not yet known, 1: thinking, 2: text + buf = "" + said = false + feed(s) { + this.buf += s + const out = [] + for (;;) { + if (this.state === 0) { + const t = this.buf.replace(/^[ \t\r\n]+/, "") + if (t.startsWith(OPEN)) { + ;[this.buf, this.state] = [t.slice(OPEN.length), 1] + continue + } + if (t.length < OPEN.length && OPEN.startsWith(t)) return out // may yet be the tag + this.state = 2 + continue + } + if (this.state === 1) { + if (!this.said) { + this.buf = this.buf.replace(/^[\r\n]+/, "") + if (!this.buf) return out + } + const i = this.buf.indexOf(CLOSE) + if (i >= 0) { + if (i > 0) { + out.push({ kind: "think", text: this.buf.slice(0, i) }) + this.said = true + } + ;[this.buf, this.state] = [this.buf.slice(i + CLOSE.length).replace(/^[\r\n]+/, ""), 2] + continue + } + // hold back what may be the start of the closing tag + const n = this.buf.length - partialSuffix(this.buf, CLOSE) + if (n > 0) { + out.push({ kind: "think", text: this.buf.slice(0, n) }) + ;[this.buf, this.said] = [this.buf.slice(n), true] + } + return out + } + if (this.buf) out.push({ kind: "text", text: this.buf }) + this.buf = "" + return out + } + } + end() { + if (!this.buf) return [] + const ev = { kind: this.state === 1 ? "think" : "text", text: this.buf } + this.buf = "" + if (this.state === 0) this.state = 2 + return [ev] + } +} + +// events turns Kiro's reply into events: text, thinking, tool calls (the +// arguments in pieces), then the usage and why it stopped — or an error. +async function* events(body, model, budget) { + const think = new ThinkParser() + let tool = "" + let tools = 0 + let stop = "" + const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 } + let pct = 0 + let said = 0 + let failed = "" + const dec = new TextDecoder() + try { + for await (const f of frames(body)) { + const type = f.headers[":message-type"] + if (type === "exception" || type === "error") { + failed = exception(f.headers[":exception-type"] || f.headers[":error-code"], f.payload) + break + } + let m + try { + m = JSON.parse(dec.decode(f.payload)) + } catch { + continue + } + const kind = f.headers[":event-type"] + if (kind === "assistantResponseEvent") { + const text = typeof m.content === "string" ? m.content : "" + said += text.length + if (budget) yield* think.feed(text) + else if (text) yield { kind: "text", text } + } else if (kind === "reasoningContentEvent") { + if (m.text) { + said += m.text.length + yield { kind: "think", text: m.text } + } else 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 + tool = m.toolUseId + tools++ + yield { kind: "toolStart", id: m.toolUseId, name: m.name ?? "" } + } + if (m.input !== undefined && m.input !== null) { + if (typeof m.input === "string") { + if (m.input) yield { kind: "toolArgs", text: m.input } + said += m.input.length + } else { + const s = JSON.stringify(m.input) + if (s !== "{}") yield { kind: "toolArgs", text: s } + said += s.length + } + } + } else if (kind === "metadataEvent" || kind === "messageMetadataEvent") { + if (m.stopReason) stop = m.stopReason + const tu = m.tokenUsage + if (tu && typeof tu === "object") { + usage.input += tu.uncachedInputTokens || tu.inputTokens || 0 + usage.output += tu.outputTokens || 0 + usage.cacheRead += tu.cacheReadInputTokens || 0 + usage.cacheWrite += tu.cacheWriteInputTokens || 0 + if (tu.contextUsagePercentage > 0) pct = tu.contextUsagePercentage + } + } else if (kind === "contextUsageEvent") { + if (m.contextUsagePercentage > 0) pct = m.contextUsagePercentage + } else if (["error", "throttlingError", "validationError", "serviceUnavailableError", "internalServerException"].includes(kind)) { + failed = exception(kind, f.payload) + break + } + } + } catch (e) { + if (e?.name !== "AbortError") failed = "the reply broke off: " + e.message + } + yield* think.end() + if (failed) { + yield { kind: "error", text: failed } + return + } + const window = windows.get(model) ?? 0 + // Kiro says how full the context is rather than how many tokens + if (!usage.input && pct > 0 && window > 0) usage.input = Math.floor((pct / 100) * window) + if (!usage.output) usage.output = Math.floor((said + 3) / 4) + let reason = "end_turn" + if (tools > 0) reason = "tool_use" + else if (/^max_tokens$/i.test(stop)) reason = "max_tokens" + else if (/^content_filtered$/i.test(stop)) reason = "refusal" + yield { kind: "stop", reason, usage } +} + +const anthropicUsage = (u) => ({ + input_tokens: u.input, + output_tokens: u.output, + cache_read_input_tokens: u.cacheRead, + cache_creation_input_tokens: u.cacheWrite, +}) + +// reply answers the Messages request from Kiro's events: a stream of +// Messages' events, or one message. +async function reply(it, model, stream) { + const id = "msg_" + randomBytes(12).toString("hex") + if (!stream) { + const content = [] + let last = null + const add = (block) => { + content.push(block) + last = block + } + for await (const e of it) { + if (e.kind === "text") { + if (last?.type === "text") last.text += e.text + else add({ type: "text", text: e.text }) + } else if (e.kind === "think") { + if (last?.type === "thinking") last.thinking += e.text + else add({ type: "thinking", thinking: e.text, signature: "" }) + } else if (e.kind === "sig") { + if (last?.type === "thinking") last.signature += e.text + } else if (e.kind === "toolStart") add({ type: "tool_use", id: e.id, name: e.name, input: "" }) + else if (e.kind === "toolArgs") { + if (last?.type === "tool_use") last.input += e.text + } else if (e.kind === "error") return errorResponse(502, e.text) + else if (e.kind === "stop") { + for (const b of content) + if (b.type === "tool_use") { + try { + b.input = b.input ? JSON.parse(b.input) : {} + } catch { + b.input = {} + } + } + return Response.json({ id, type: "message", role: "assistant", model, content, stop_reason: e.reason, stop_sequence: null, + usage: anthropicUsage(e.usage) }) + } + } + return errorResponse(502, "the reply ended before it was complete") + } + + const enc = new TextEncoder() + const sse = (type, data) => enc.encode(`event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`) + let index = -1 + let open = "" // the kind of block open + let started = false + const body = new ReadableStream({ + async pull(ctl) { + if (!started) { + started = true + ctl.enqueue(sse("message_start", { message: { id, type: "message", role: "assistant", model, content: [], stop_reason: null, + stop_sequence: null, usage: { input_tokens: 0, output_tokens: 0 } } })) + return + } + const close = () => { + if (open) ctl.enqueue(sse("content_block_stop", { index })) + open = "" + } + const begin = (kind, block) => { + close() + index++ + open = kind + ctl.enqueue(sse("content_block_start", { index, content_block: block })) + } + const r = await it.next() + if (r.done) return ctl.close() + const e = r.value + switch (e.kind) { + case "text": + if (open !== "text") begin("text", { type: "text", text: "" }) + ctl.enqueue(sse("content_block_delta", { index, delta: { type: "text_delta", text: e.text } })) + break + case "think": + if (open !== "thinking") begin("thinking", { type: "thinking", thinking: "", signature: "" }) + ctl.enqueue(sse("content_block_delta", { index, delta: { type: "thinking_delta", thinking: e.text } })) + break + case "sig": + if (open === "thinking") ctl.enqueue(sse("content_block_delta", { index, delta: { type: "signature_delta", signature: e.text } })) + break + case "toolStart": + begin("tool", { type: "tool_use", id: e.id, name: e.name, input: {} }) + break + case "toolArgs": + if (open === "tool") ctl.enqueue(sse("content_block_delta", { index, delta: { type: "input_json_delta", partial_json: e.text } })) + break + case "error": + close() + ctl.enqueue(sse("error", { error: { type: "api_error", message: "Kiro: " + e.text } })) + return ctl.close() + case "stop": + close() + ctl.enqueue(sse("message_delta", { delta: { stop_reason: e.reason, stop_sequence: null }, usage: anthropicUsage(e.usage) })) + ctl.enqueue(sse("message_stop", {})) + return ctl.close() + } + }, + cancel() { + it.return?.() + }, + }) + return new Response(body, { status: 200, headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache" } }) +} + +// generate answers a Messages request through Kiro's API: once more, +// refreshed, when Kiro turns the token down (403). +async function generate(creds, auth, req, signal) { + const model = String(req.model ?? "auto") + const budget = thinking(req, model) + let a + try { + a = await creds(auth) + } catch (e) { + return errorResponse(401, e.message) + } + let res + try { + res = await sendKiro(a, buildKiro(req, model, a.profile, budget), signal) + } catch (e) { + if (e?.name === "AbortError") throw e + return errorResponse(502, e.message) + } + if (res.status === 403) { + await res.body?.cancel() + try { + a = await creds(auth, true) + } catch (e) { + return errorResponse(401, e.message) + } + try { + res = await sendKiro(a, buildKiro(req, model, a.profile, budget), signal) + } catch (e) { + if (e?.name === "AbortError") throw e + return errorResponse(502, e.message) + } + } + if (!res.ok) { + const f = failure(res.status, (await res.text()).slice(0, 1 << 20)) + return errorResponse(f.status, f.message) + } + return reply(events(res.body, model, budget), model, req.stream === true) +} + +// ---- signing in ---------------------------------------------------------------------- + +function page(ok, title, text) { + const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]) + return `${esc(title)} + +
${ok ? "✓" : "✕"}

${esc(title)}

${esc(text)}

` +} + +// signInPost posts JSON to Kiro's auth service or AWS's sign-in. +async function signInPost(url, body) { + const res = await fetch(url, { + method: "POST", + headers: { "Content-Type": "application/json", Accept: "application/json", "User-Agent": IDE_UA }, + body: JSON.stringify(body), + signal: AbortSignal.timeout(30_000), + }) + const text = await res.text() + if (!res.ok) { + let e = {} + try { + e = JSON.parse(text) + } catch {} + throw new Error(`the sign-in was refused (${res.status}): ${e.error_description || e.message || e.error || res.statusText}`) + } + return JSON.parse(text) +} + +const expiry = (s) => Date.now() + (s > 0 ? s : 3600) * 1000 + +// whoIs names a new sign-in with the email Kiro has for it, and its plan. +async function whoIs(c) { + try { + const profile = c.profile || (await profileOf(c)) + const a = { token: c.access, tokenType: tokenType(c), profile, region: regionOf(profile, c.region) } + const l = await usageLimits(a, true) + return { profile, email: l?.userInfo?.email ?? "", plan: planName(l?.subscriptionInfo?.subscriptionTitle) } + } catch { + return { profile: c.profile ?? "", email: "", plan: "" } + } +} + +async function listen() { + for (const port of CALLBACK_PORTS) { + const server = createServer() + const ok = await new Promise((resolve) => { + server.once("error", () => resolve(false)) + server.listen(port, "127.0.0.1", () => resolve(true)) + }) + if (ok) return { server, port } + } + throw new Error("the ports Kiro's sign-in comes back to are all busy; close the Kiro IDE's sign-in and try again") +} + +// browserSignIn is the IDE's: Kiro's sign-in page, back to a port here. +async function browserSignIn() { + const { server, port } = await listen() + const state = randomBytes(24).toString("base64url") + const verifier = randomBytes(48).toString("base64url") + const redirect = `http://localhost:${port}` + const aws = {} // once Kiro's page has sent the browser on to AWS + const awsRedirect = `http://127.0.0.1:${port}/oauth/callback` + let waiting = true + let settle + const done = new Promise((r) => (settle = r)) + const finish = (result) => { + if (!waiting) return + waiting = false + settle(result) + } + + server.on("request", async (req, res) => { + const url = new URL(req.url ?? "/", redirect) + const html = (code, body) => { + res.writeHead(code, { "Content-Type": "text/html; charset=utf-8" }) + res.end(body) + } + if (url.pathname !== "/oauth/callback" && url.pathname !== "/signin/callback") return res.writeHead(404).end() + const q = url.searchParams + if (!waiting) return html(200, page(false, "This sign-in is over", "Start it again.")) + const fail = (msg) => { + finish({ type: "failed" }) + html(200, page(false, "Sign-in didn't finish", msg)) + } + if (q.get("error")) return fail(q.get("error_description") || q.get("error")) + let c + try { + if (aws.state && q.get("state") === aws.state) { + // back from AWS + if (!q.get("code")) return fail("AWS sent back no code") + const t = await signInPost(oidc(aws.region) + "/token", { clientId: aws.clientId, clientSecret: aws.clientSecret, + grantType: "authorization_code", redirectUri: awsRedirect, code: q.get("code"), codeVerifier: aws.verifier }) + if (!t.accessToken) throw new Error("AWS sent back no token") + c = { access: t.accessToken, refresh: t.refreshToken ?? "", expires: expiry(t.expiresIn), method: "idc", loginProvider: aws.provider, + region: aws.region, clientId: aws.clientId, clientSecret: aws.clientSecret, profile: "" } + } else if (q.get("state") !== state) { + // not ours: someone else's page, or a stale tab + return html(200, page(false, "This link isn't from this sign-in", "Start it again.")) + } else { + const opt = q.get("login_option") ?? "" + if (opt === "google" || opt === "github") { + const t = await signInPost(AUTH_SERVICE + "/oauth/token", { code: q.get("code"), code_verifier: verifier, + redirect_uri: redirect + url.pathname + "?login_option=" + opt }) + if (!t.accessToken) throw new Error("Kiro sent back no token") + c = { access: t.accessToken, refresh: t.refreshToken ?? "", expires: expiry(t.expiresIn), method: "social", + loginProvider: opt === "google" ? "Google" : "Github", region: "us-east-1", profile: t.profileArn ?? "" } + } else if (opt === "builderid" || opt === "awsidc" || opt === "internal") { + // on to AWS, with a client registered for this sign-in + const issuer = q.get("issuer_url") + const region = q.get("idc_region") + if (!issuer || !region) return fail("Kiro's page didn't say where to sign in at AWS") + const reg = await signInPost(oidc(region) + "/client/register", { clientName: "Kiro IDE", clientType: "public", scopes: SCOPES, + grantTypes: ["authorization_code", "refresh_token"], redirectUris: ["http://127.0.0.1/oauth/callback"], issuerUrl: issuer }) + if (!reg.clientId) throw new Error("AWS registered no client") + Object.assign(aws, { region, clientId: reg.clientId, clientSecret: reg.clientSecret, + provider: { builderid: "BuilderId", awsidc: "Enterprise", internal: "Internal" }[opt], + state: randomBytes(24).toString("base64url"), verifier: randomBytes(48).toString("base64url") }) + const a = new URLSearchParams({ response_type: "code", client_id: aws.clientId, redirect_uri: awsRedirect, scopes: SCOPES.join(","), + state: aws.state, code_challenge: createHash("sha256").update(aws.verifier).digest("base64url"), code_challenge_method: "S256" }) + res.writeHead(302, { Location: `${oidc(region)}/authorize?${a}` }) + return res.end() + } else if (opt === "external_idp") { + return fail("A company's own identity provider can't be signed in to here yet; sign in with `kiro-cli login`, and use Kiro CLI's sign-in") + } else return fail(`Kiro's page came back with a sign-in this plugin doesn't know (${JSON.stringify(opt)})`) + } + } catch (e) { + return fail(e.message) + } + const who = await whoIs(c) + const user = who.email || "Kiro account" + finish({ type: "success", provider: ID, refresh: c.refresh, access: c.access, expires: c.expires, accountId: user, + method: c.method, loginProvider: c.loginProvider, region: c.region, profileArn: who.profile, + ...(c.clientId ? { clientId: c.clientId, clientSecret: c.clientSecret } : {}), ...(who.plan ? { plan: who.plan } : {}) }) + html(200, page(true, "You're signed in", `${user} is signed in. You can close this tab.`)) + }) + + const timer = setTimeout(() => finish({ type: "failed" }), SIGN_IN_TIMEOUT) + done.then(() => { + clearTimeout(timer) + setTimeout(() => server.close(), 10_000).unref?.() + }) + const challenge = createHash("sha256").update(verifier).digest("base64url") + const q = new URLSearchParams({ state, code_challenge: challenge, code_challenge_method: "S256", redirect_uri: redirect, redirect_from: "KiroIDE" }) + return { + url: `${PORTAL}/signin?${q}`, + instructions: "Sign in to Kiro with Google, GitHub, AWS Builder ID or IAM Identity Center.", + method: "auto", + callback: () => done, + } +} + +// ownSignIn uses kiro-cli's sign-in, else the Kiro IDE's: nothing is copied, +// they are read where they keep it each time. +async function ownSignIn() { + return { + url: "", + instructions: "Uses the account kiro-cli or the Kiro IDE is signed in to.", + method: "auto", + callback: async () => { + const cli = await readCLI() + const c = cli ?? (await readIDE()) + if (!c) return { type: "failed" } + const who = fresh(c) ? await whoIs(c) : { email: "", plan: "" } + return { type: "success", provider: ID, refresh: "", access: "", expires: 0, source: cli ? "kiro-cli" : "kiro-ide", + accountId: who.email || (cli ? "kiro-cli's account" : "Kiro IDE's account"), ...(who.plan ? { plan: who.plan } : {}) } + }, + } +} + +// ---- the plugin ------------------------------------------------------------------------ + +async function bodyOf(input, init) { + const b = init.body ?? (input instanceof Request ? await input.clone().text() : undefined) + return JSON.parse(typeof b === "string" ? b : new TextDecoder().decode(b)) +} + +export async function KiroAuthPlugin({ client } = {}) { + const creds = account(client) + return { + auth: { + provider: ID, + async loader(getAuth) { + const auth = await getAuth() + if (!(auth?.type === "api" && auth.key) && auth?.type !== "oauth") return {} + return { + baseURL: BASE, + apiKey: "kiro", // the fetch signs each request itself + // every Messages request answered through Kiro's own API + async fetch(input, init = {}) { + const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url + if (!/\/messages$/.test(new URL(url).pathname)) + return errorResponse(404, `this plugin only answers Anthropic Messages requests, not ${new URL(url).pathname}`) + let req + try { + req = await bodyOf(input, init) + } catch { + return errorResponse(400, "a request that isn't JSON") + } + return generate(creds, (await getAuth()) ?? auth, req, init.signal) + }, + } + }, + methods: [ + { type: "oauth", label: "Kiro (Google, GitHub, AWS Builder ID, IAM Identity Center)", authorize: browserSignIn }, + { type: "oauth", label: "Kiro CLI's or Kiro IDE's sign-in", authorize: ownSignIn }, + { type: "api", label: "Kiro API key (ksk_…)" }, + ], + }, + async config(config) { + config.provider ??= {} + const was = config.provider[ID] ?? {} + config.provider[ID] = { + name: "Kiro", + npm: MESSAGES, + api: BASE, + ...was, + models: { ...Object.fromEntries(MODELS.map((m) => [m.id, configModel(m)])), ...(was.models ?? {}) }, + } + }, + // the account's own list, the default first + provider: { + id: ID, + async models(provider, { auth } = {}) { + if (!(auth?.type === "api" && auth.key) && auth?.type !== "oauth") return provider.models + try { + const list = await listModels(await creds(auth)) + return Object.fromEntries(list.map((m) => [m.id, runtimeModel(m)])) + } catch { + return provider.models + } + }, + }, + } +} + +// for tests +export const _internal = { buildKiro, events, reply, failure, toolID, thinking, readCLI, readIDE, regionOf, planName, frames } diff --git a/packages/kiro/package.json b/packages/kiro/package.json new file mode 100644 index 0000000..8d2f4a9 --- /dev/null +++ b/packages/kiro/package.json @@ -0,0 +1,11 @@ +{ + "name": "@magpie-community/opencode-kiro-auth", + "version": "0.1.0", + "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", + "type": "module", + "main": "./index.mjs", + "exports": "./index.mjs", + "files": ["index.mjs", "README.md"], + "keywords": ["opencode", "opencode-plugin", "magpie", "kiro"], + "license": "MIT" +}