Skip to content

Commit da71f35

Browse files
committed
feat(streaming): robust SDK tool-call parsing and thinking passthrough
Handle inline content_block_start tool calls and bracket-format tool calls without double-emitting, track per-tool stop state, and map shortened Kiro tool names back to their original names on the way out. Shorten long tool names when building history to stay within Kiro's limits, and thread thinkingRequested through the response handler so thinking output is surfaced when requested.
1 parent 0c0320c commit da71f35

6 files changed

Lines changed: 299 additions & 60 deletions

File tree

‎src/core/request/response-handler.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ export class ResponseHandler {
1919
sdkResponse: any,
2020
model: string,
2121
conversationId: string,
22-
streaming: boolean
22+
streaming: boolean,
23+
toolNameMapper?: (name: string) => string,
24+
thinkingRequested = false
2325
): Promise<Response> {
2426
if (streaming) {
25-
return this.handleSdkStreaming(sdkResponse, model, conversationId)
27+
return this.handleSdkStreaming(
28+
sdkResponse,
29+
model,
30+
conversationId,
31+
toolNameMapper,
32+
thinkingRequested
33+
)
2634
}
2735
return this.handleSdkNonStreaming(sdkResponse, model, conversationId)
2836
}
@@ -33,12 +41,13 @@ export class ResponseHandler {
3341
conversationId: string
3442
): Promise<Response> {
3543
const s = transformKiroStream(response, model, conversationId)
44+
const enc = new TextEncoder()
3645
return new Response(
3746
new ReadableStream({
3847
async start(c) {
3948
try {
4049
for await (const e of s) {
41-
c.enqueue(new TextEncoder().encode(`data: ${JSON.stringify(e)}\n\n`))
50+
c.enqueue(enc.encode(`data: ${JSON.stringify(e)}\n\n`))
4251
}
4352
c.close()
4453
} catch (err) {
@@ -53,15 +62,24 @@ export class ResponseHandler {
5362
private async handleSdkStreaming(
5463
sdkResponse: any,
5564
model: string,
56-
conversationId: string
65+
conversationId: string,
66+
toolNameMapper?: (name: string) => string,
67+
thinkingRequested = false
5768
): Promise<Response> {
58-
const s = transformSdkStream(sdkResponse, model, conversationId)
69+
const s = transformSdkStream(
70+
sdkResponse,
71+
model,
72+
conversationId,
73+
toolNameMapper,
74+
thinkingRequested
75+
)
76+
const enc = new TextEncoder()
5977
return new Response(
6078
new ReadableStream({
6179
async start(c) {
6280
try {
6381
for await (const e of s) {
64-
c.enqueue(new TextEncoder().encode(`data: ${JSON.stringify(e)}\n\n`))
82+
c.enqueue(enc.encode(`data: ${JSON.stringify(e)}\n\n`))
6583
}
6684
c.close()
6785
} catch (err) {

‎src/infrastructure/transformers/history-builder.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '../../plugin/image-handler.js'
77
import type { CodeWhispererMessage } from '../../plugin/types'
88
import { getContentText } from './message-transformer.js'
9-
import { deduplicateToolResults } from './tool-transformer.js'
9+
import { deduplicateToolResults, shortenToolName } from './tool-transformer.js'
1010

1111
/**
1212
* Collapse agentic loop sequences in the built history.
@@ -154,7 +154,7 @@ export function buildHistory(msgs: any[], resolved: string): CodeWhispererMessag
154154
if (p.type === 'text') arm.content += p.text || ''
155155
else if (p.type === 'thinking') th += p.thinking || p.text || ''
156156
else if (p.type === 'tool_use')
157-
tus.push({ input: p.input, name: p.name, toolUseId: p.id })
157+
tus.push({ input: p.input, name: shortenToolName(p.name), toolUseId: p.id })
158158
}
159159
} else arm.content = getContentText(m)
160160
if (m.tool_calls && Array.isArray(m.tool_calls)) {
@@ -164,7 +164,7 @@ export function buildHistory(msgs: any[], resolved: string): CodeWhispererMessag
164164
typeof tc.function?.arguments === 'string'
165165
? JSON.parse(tc.function.arguments)
166166
: tc.function?.arguments,
167-
name: tc.function?.name,
167+
name: shortenToolName(tc.function?.name),
168168
toolUseId: tc.id
169169
})
170170
}

‎src/infrastructure/transformers/tool-transformer.ts‎

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,103 @@
1+
import * as crypto from 'crypto'
2+
3+
const MAX_TOOL_NAME_LENGTH = 64
4+
5+
// Slice without breaking surrogate pairs.
6+
function safeSlice(s: string, end: number): string {
7+
if (end <= 0) return ''
8+
if (end >= s.length) return s
9+
const code = s.charCodeAt(end - 1)
10+
if (code >= 0xd800 && code <= 0xdbff) end -= 1
11+
return s.slice(0, end)
12+
}
13+
14+
export function shortenToolName(name: string): string {
15+
if (!name || name.length <= MAX_TOOL_NAME_LENGTH) return name
16+
const hash = crypto.createHash('sha256').update(name).digest('hex').slice(0, 12)
17+
const prefix = safeSlice(name, MAX_TOOL_NAME_LENGTH - hash.length - 1)
18+
return `${prefix}_${hash}`
19+
}
20+
21+
export function buildToolNameMaps(tools: any[]): {
22+
toKiroName: (name: string) => string
23+
fromKiroName: (name: string) => string
24+
} {
25+
const originalToAlias = new Map<string, string>()
26+
const aliasToOriginal = new Map<string, string>()
27+
28+
for (const t of tools) {
29+
const name = t.name || t.function?.name
30+
if (!name) continue
31+
const alias = shortenToolName(name)
32+
originalToAlias.set(name, alias)
33+
if (alias !== name) aliasToOriginal.set(alias, name)
34+
}
35+
36+
return {
37+
toKiroName: (name: string) => originalToAlias.get(name) || shortenToolName(name),
38+
fromKiroName: (name: string) => aliasToOriginal.get(name) || name
39+
}
40+
}
41+
42+
function sanitizeToolInput(input: any): any {
43+
if (!input || typeof input !== 'object' || Array.isArray(input)) return input
44+
const result: Record<string, any> = {}
45+
for (const [key, value] of Object.entries(input)) {
46+
if (key === '') continue
47+
result[key] = value
48+
}
49+
return result
50+
}
51+
52+
function sanitizeSchema(schema: any, seen: WeakSet<object> = new WeakSet()): any {
53+
if (!schema || typeof schema !== 'object' || Array.isArray(schema)) return schema
54+
if (seen.has(schema)) return {}
55+
seen.add(schema)
56+
57+
const result: Record<string, any> = {}
58+
for (const [key, value] of Object.entries(schema)) {
59+
if (key === 'additionalProperties') continue
60+
if (key === 'required' && Array.isArray(value) && value.length === 0) continue
61+
62+
if (
63+
(key === 'properties' ||
64+
key === 'patternProperties' ||
65+
key === '$defs' ||
66+
key === 'definitions') &&
67+
typeof value === 'object' &&
68+
value !== null &&
69+
!Array.isArray(value)
70+
) {
71+
const props: Record<string, any> = {}
72+
for (const [pk, pv] of Object.entries(value)) {
73+
props[pk] = sanitizeSchema(pv, seen)
74+
}
75+
result[key] = props
76+
} else if (
77+
(key === 'anyOf' || key === 'oneOf' || key === 'allOf' || key === 'prefixItems') &&
78+
Array.isArray(value)
79+
) {
80+
result[key] = value.map((v) => sanitizeSchema(v, seen))
81+
} else if (
82+
(key === 'items' || key === 'not' || key === 'contains') &&
83+
typeof value === 'object'
84+
) {
85+
result[key] = sanitizeSchema(value, seen)
86+
} else {
87+
result[key] = value
88+
}
89+
}
90+
return result
91+
}
92+
193
export function convertToolsToCodeWhisperer(tools: any[]): any[] {
294
return tools.map((t) => ({
395
toolSpecification: {
4-
name: t.name || t.function?.name,
96+
name: shortenToolName(t.name || t.function?.name || ''),
597
description: (t.description || t.function?.description || '').substring(0, 9216),
6-
inputSchema: { json: t.input_schema || t.function?.parameters || {} }
98+
inputSchema: {
99+
json: sanitizeSchema(sanitizeToolInput(t.input_schema || t.function?.parameters || {}))
100+
}
7101
}
8102
}))
9103
}
@@ -19,3 +113,19 @@ export function deduplicateToolResults(trs: any[]): any[] {
19113
}
20114
return u
21115
}
116+
117+
export function deduplicateToolCallsByContent(toolCalls: any[]): any[] {
118+
const seen = new Set<string>()
119+
const unique: any[] = []
120+
for (const tc of toolCalls) {
121+
// \x00 as separator (can't appear in a tool name)
122+
const name = tc.name || tc.function?.name || ''
123+
const input = tc.input || tc.function?.arguments || ''
124+
const key = `${name}\x00${typeof input === 'string' ? input : JSON.stringify(input)}`
125+
if (!seen.has(key)) {
126+
seen.add(key)
127+
unique.push(tc)
128+
}
129+
}
130+
return unique
131+
}

0 commit comments

Comments
 (0)