Skip to content

Commit 28996a4

Browse files
byongshintvcodex
andcommitted
fix(kiro): use CLI wire contract without profiles
Co-authored-by: OpenAI Codex <[email protected]>
1 parent c7e48fb commit 28996a4

2 files changed

Lines changed: 95 additions & 9 deletions

File tree

‎src/adapters/kiro.ts‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ const AMZ_TARGET = "AmazonCodeWhispererStreamingService.GenerateAssistantRespons
5252
const SDK_VERSION = "1.0.27";
5353
const NODE_VERSION = "22.21.1";
5454
const KIRO_IDE_VERSION = "1.0.0";
55+
type KiroWireClient = "ide" | "cli";
56+
57+
function kiroCliPlatform(): "linux" | "macos" | "windows" {
58+
return process.platform === "win32" ? "windows" : process.platform === "darwin" ? "macos" : "linux";
59+
}
60+
61+
function kiroCliUserAgent(includeAppVersion: boolean): string {
62+
return [
63+
"aws-sdk-rust/1.3.15",
64+
"ua/2.1",
65+
"api/codewhispererstreaming/0.1.17975",
66+
`os/${kiroCliPlatform()}`,
67+
"lang/rust/1.92.0",
68+
...(includeAppVersion ? ["md/appVersion-2.14.2"] : []),
69+
"m/F",
70+
"app/AmazonQ-For-CLI",
71+
].join(" ");
72+
}
5573

5674
// Payload construction (conversationState)
5775
interface KiroToolUse {
@@ -68,7 +86,11 @@ interface KiroUserInputMessage {
6886
content: string;
6987
modelId?: string;
7088
origin?: string;
71-
userInputMessageContext?: { tools?: unknown[]; toolResults?: KiroToolResult[] };
89+
userInputMessageContext?: {
90+
tools?: unknown[];
91+
toolResults?: KiroToolResult[];
92+
envState?: { operatingSystem: string; currentWorkingDirectory: string };
93+
};
7294
images?: KiroImage[];
7395
}
7496
interface KiroHistoryEntry {
@@ -385,6 +407,7 @@ export function buildKiroPayload(
385407
parsed: OcxParsedRequest,
386408
profileArn: string | undefined,
387409
forcedCompletionMode?: KiroCompletionMode,
410+
wireClient: KiroWireClient = "ide",
388411
): {
389412
payload: Record<string, unknown>;
390413
nameMap: Map<string, string>;
@@ -509,7 +532,7 @@ export function buildKiroPayload(
509532
userInputMessage: {
510533
content: turn.content,
511534
modelId,
512-
origin: "AI_EDITOR",
535+
origin: wireClient === "cli" ? "KIRO_CLI" : "AI_EDITOR",
513536
...(turn.images.length > 0 ? { images: turn.images } : {}),
514537
...(turn.toolResults.length > 0 ? { userInputMessageContext: { toolResults: turn.toolResults } } : {}),
515538
},
@@ -526,6 +549,12 @@ export function buildKiroPayload(
526549
if (kiroTools.length > 0) {
527550
currentUim.userInputMessageContext = { ...(currentUim.userInputMessageContext ?? {}), tools: kiroTools };
528551
}
552+
if (wireClient === "cli") {
553+
currentUim.userInputMessageContext = {
554+
...(currentUim.userInputMessageContext ?? {}),
555+
envState: { operatingSystem: kiroCliPlatform(), currentWorkingDirectory: process.cwd() },
556+
};
557+
}
529558
if (completionMode === "text_fallback") {
530559
if (currentUim.content !== KIRO_COMPLETION_RETRY_MESSAGE) {
531560
currentUim.content = appendTurnText(currentUim.content, KIRO_COMPLETION_RETRY_MESSAGE);
@@ -539,6 +568,10 @@ export function buildKiroPayload(
539568
const payload: Record<string, unknown> = {
540569
conversationState: {
541570
chatTriggerType: "MANUAL",
571+
...(wireClient === "cli" ? {
572+
agentContinuationId: crypto.randomUUID(),
573+
agentTaskType: "vibe",
574+
} : {}),
542575
conversationId,
543576
currentMessage: { userInputMessage: currentUim },
544577
...(history.length > 0 ? { history } : {}),
@@ -1447,8 +1480,23 @@ export function createKiroAdapter(provider: OcxProviderConfig): ProviderAdapter
14471480
}
14481481
const region = resolveKiroApiRegion(parsed._kiroAuthContext);
14491482
const profileArn = resolveKiroProfileArn(parsed._kiroAuthContext);
1483+
// Builder ID and Kiro API keys have no profile ARN and are accepted only on Kiro's CLI
1484+
// request path. Enterprise profiles retain the existing IDE-shaped request.
1485+
const wireClient: KiroWireClient = profileArn ? "ide" : "cli";
1486+
const isApiKey = provider.apiKey.trim().startsWith("ksk_");
14501487
const fp = fingerprint().slice(0, 64);
1451-
const headers: Record<string, string> = {
1488+
const headers: Record<string, string> = wireClient === "cli" ? {
1489+
authorization: `Bearer ${provider.apiKey}`,
1490+
"content-type": "application/x-amz-json-1.0",
1491+
accept: "*/*",
1492+
"x-amz-target": AMZ_TARGET,
1493+
"user-agent": kiroCliUserAgent(true),
1494+
"x-amz-user-agent": kiroCliUserAgent(false),
1495+
"x-amzn-codewhisperer-optout": "false",
1496+
"amz-sdk-request": "attempt=1; max=3",
1497+
"amz-sdk-invocation-id": invocationId(),
1498+
...(isApiKey ? { tokentype: "API_KEY" } : {}),
1499+
} : {
14521500
authorization: `Bearer ${provider.apiKey}`,
14531501
"content-type": "application/x-amz-json-1.0",
14541502
accept: "application/vnd.amazon.eventstream",
@@ -1460,7 +1508,7 @@ export function createKiroAdapter(provider: OcxProviderConfig): ProviderAdapter
14601508
"amz-sdk-invocation-id": invocationId(),
14611509
};
14621510
if (profileArn) headers["x-amzn-kiro-profile-arn"] = profileArn;
1463-
const built = buildKiroPayload(parsed, profileArn, forcedCompletionMode);
1511+
const built = buildKiroPayload(parsed, profileArn, forcedCompletionMode, wireClient);
14641512
await normalizeKiroImages(built.payload);
14651513
const contextInputEstimate = estimateKiroPayloadInputTokens(built.payload, parsed.modelId);
14661514
const body = JSON.stringify(built.payload);
@@ -1472,6 +1520,7 @@ export function createKiroAdapter(provider: OcxProviderConfig): ProviderAdapter
14721520
messageCount: kiroPayloadMessages(parsed).length,
14731521
toolCount: parsed.context.tools?.length ?? 0,
14741522
hasProfileArn: Boolean(profileArn),
1523+
wireClient,
14751524
hasPreviousResponseId: Boolean(parsed.previousResponseId),
14761525
});
14771526
return {

‎tests/kiro-adapter.test.ts‎

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,49 @@ describe("kiro adapter — buildRequest", () => {
7373
}
7474
});
7575

76-
test("headers carry Bearer token + CW targets", async () => {
77-
const { url, method, headers } = await createKiroAdapter(provider).buildRequest(parsedWith([{ role: "user", content: "hi" }]));
76+
test("Builder ID requests without a profile ARN use the Kiro CLI wire contract", async () => {
77+
const { url, method, headers, body } = await createKiroAdapter(provider).buildRequest(parsedWith([{ role: "user", content: "hi" }]));
78+
const payload = JSON.parse(body) as {
79+
profileArn?: string;
80+
conversationState: {
81+
agentContinuationId?: string;
82+
agentTaskType?: string;
83+
currentMessage: { userInputMessage: Record<string, unknown> };
84+
};
85+
};
7886
expect(url).toBe("https://runtime.us-east-1.kiro.dev/");
7987
expect(method).toBe("POST");
8088
expect(headers.authorization).toBe("Bearer tok-123");
8189
expect(headers["x-amz-target"]).toBe("AmazonCodeWhispererStreamingService.GenerateAssistantResponse");
82-
expect(headers.accept).toBe("application/vnd.amazon.eventstream");
83-
expect(headers["x-amzn-kiro-agent-mode"]).toBe("vibe");
90+
expect(headers.accept).toBe("*/*");
91+
expect(headers["user-agent"]).toContain("app/AmazonQ-For-CLI");
92+
expect(headers["x-amzn-kiro-agent-mode"]).toBeUndefined();
93+
expect(headers["x-amzn-kiro-profile-arn"]).toBeUndefined();
94+
expect(headers.tokentype).toBeUndefined();
95+
expect(payload.profileArn).toBeUndefined();
96+
expect(payload.conversationState.agentTaskType).toBe("vibe");
97+
expect(payload.conversationState.agentContinuationId).toMatch(/^[0-9a-f-]{36}$/);
98+
expect(payload.conversationState.currentMessage.userInputMessage).toMatchObject({
99+
content: "hi",
100+
origin: "KIRO_CLI",
101+
userInputMessageContext: {
102+
envState: {
103+
operatingSystem: process.platform === "win32" ? "windows" : process.platform === "darwin" ? "macos" : "linux",
104+
currentWorkingDirectory: process.cwd(),
105+
},
106+
},
107+
});
108+
});
109+
110+
test("Kiro API keys use the CLI token type without exposing a profile ARN", async () => {
111+
const apiKeyProvider = { ...provider, authMode: "key", apiKey: "ksk_example" } as unknown as OcxProviderConfig;
112+
const request = await createKiroAdapter(apiKeyProvider).buildRequest(parsedWith([{ role: "user", content: "hi" }]));
113+
const body = JSON.parse(request.body) as { profileArn?: string };
114+
115+
expect(request.headers.authorization).toBe("Bearer ksk_example");
116+
expect(request.headers.tokentype).toBe("API_KEY");
117+
expect(request.headers["x-amzn-kiro-profile-arn"]).toBeUndefined();
118+
expect(body.profileArn).toBeUndefined();
84119
});
85120

86121
test("runtime URL uses KIRO_API_REGION separately from auth region", async () => {
@@ -104,6 +139,8 @@ describe("kiro adapter — buildRequest", () => {
104139

105140
expect(request.url).toBe("https://runtime.eu-central-1.kiro.dev/");
106141
expect(request.headers["x-amzn-kiro-profile-arn"]).toBe(parsed._kiroAuthContext.profileArn);
142+
expect(request.headers.accept).toBe("application/vnd.amazon.eventstream");
143+
expect(request.headers["x-amzn-kiro-agent-mode"]).toBe("vibe");
107144
expect(body.profileArn).toBe(parsed._kiroAuthContext.profileArn);
108145
});
109146

@@ -668,7 +705,7 @@ describe("kiro adapter — buildRequest", () => {
668705
];
669706
const cs = JSON.parse((await createKiroAdapter(provider).buildRequest(parsedWith(messages))).body).conversationState;
670707
expect(cs.history).toEqual([
671-
{ userInputMessage: { content: "first\n\nsecond", modelId: "claude-sonnet-4.5", origin: "AI_EDITOR" } },
708+
{ userInputMessage: { content: "first\n\nsecond", modelId: "claude-sonnet-4.5", origin: "KIRO_CLI" } },
672709
{ assistantResponseMessage: { content: "one\n\ntwo" } },
673710
]);
674711
expect(cs.currentMessage.userInputMessage.content).toBe("third");

0 commit comments

Comments
 (0)