Merge pull request #140 from clansty/fix/kiro-tool-use-input-parse

fix(kiro): 修复 tool_use 的 input 字段序列化问题
This commit is contained in:
何夕2077 2025-12-27 11:53:14 +08:00 committed by GitHub
commit 16b7ee454b

View file

@ -1639,8 +1639,9 @@ async initializeAuth(forceRefresh = false) {
toolCalls.forEach((tc, index) => { toolCalls.forEach((tc, index) => {
let inputObject; let inputObject;
try { try {
// Arguments should be a stringified JSON object. // Arguments should be a stringified JSON object, need to parse it
inputObject = tc.function.arguments; const args = tc.function.arguments;
inputObject = typeof args === 'string' ? JSON.parse(args) : args;
} catch (e) { } catch (e) {
console.warn(`[Kiro] Invalid JSON for tool call arguments. Wrapping in raw_arguments. Error: ${e.message}`, tc.function.arguments); console.warn(`[Kiro] Invalid JSON for tool call arguments. Wrapping in raw_arguments. Error: ${e.message}`, tc.function.arguments);
// If parsing fails, wrap the raw string in an object as a fallback, // If parsing fails, wrap the raw string in an object as a fallback,
@ -1658,7 +1659,7 @@ async initializeAuth(forceRefresh = false) {
input: {} // input is streamed via input_json_delta input: {} // input is streamed via input_json_delta
} }
}); });
// 3. content_block_delta for each tool_use // 3. content_block_delta for each tool_use
// Since Kiro is not truly streaming, we send the full arguments as one delta. // Since Kiro is not truly streaming, we send the full arguments as one delta.
events.push({ events.push({
@ -1666,10 +1667,10 @@ async initializeAuth(forceRefresh = false) {
index: index, index: index,
delta: { delta: {
type: "input_json_delta", type: "input_json_delta",
partial_json: inputObject partial_json: JSON.stringify(inputObject)
} }
}); });
// 4. content_block_stop for each tool_use // 4. content_block_stop for each tool_use
events.push({ events.push({
type: "content_block_stop", type: "content_block_stop",
@ -1706,8 +1707,9 @@ async initializeAuth(forceRefresh = false) {
for (const tc of toolCalls) { for (const tc of toolCalls) {
let inputObject; let inputObject;
try { try {
// Arguments should be a stringified JSON object. // Arguments should be a stringified JSON object, need to parse it
inputObject = tc.function.arguments; const args = tc.function.arguments;
inputObject = typeof args === 'string' ? JSON.parse(args) : args;
} catch (e) { } catch (e) {
console.warn(`[Kiro] Invalid JSON for tool call arguments. Wrapping in raw_arguments. Error: ${e.message}`, tc.function.arguments); console.warn(`[Kiro] Invalid JSON for tool call arguments. Wrapping in raw_arguments. Error: ${e.message}`, tc.function.arguments);
// If parsing fails, wrap the raw string in an object as a fallback, // If parsing fails, wrap the raw string in an object as a fallback,