fix(tools): added transform response to handle non-json responses for internal tools (#1400)

This commit is contained in:
Waleed
2025-09-20 14:17:47 -07:00
committed by GitHub
parent e4d35afe1f
commit 708321d0bf
2 changed files with 48 additions and 7 deletions
+11 -7
View File
@@ -516,13 +516,17 @@ async function handleInternalRequest(
// Many APIs (e.g., Microsoft Graph) return 202 with empty body
responseData = { status }
} else {
try {
responseData = await response.json()
} catch (jsonError) {
logger.error(`[${requestId}] JSON parse error for ${toolId}:`, {
error: jsonError instanceof Error ? jsonError.message : String(jsonError),
})
throw new Error(`Failed to parse response from ${toolId}: ${jsonError}`)
if (tool.transformResponse) {
responseData = null
} else {
try {
responseData = await response.json()
} catch (jsonError) {
logger.error(`[${requestId}] JSON parse error for ${toolId}:`, {
error: jsonError instanceof Error ? jsonError.message : String(jsonError),
})
throw new Error(`Failed to parse response from ${toolId}: ${jsonError}`)
}
}
}
+37
View File
@@ -527,6 +527,43 @@ describe('executeRequest', () => {
error: 'Server Error', // Should use statusText in the error message
})
})
it('should handle transformResponse with non-JSON response', async () => {
const toolWithTransform = {
...mockTool,
transformResponse: async (response: Response) => {
const xmlText = await response.text()
return {
success: true,
output: {
parsedData: 'mocked xml parsing result',
originalXml: xmlText,
},
}
},
}
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
statusText: 'OK',
text: async () => '<xml><test>Mock XML response</test></xml>',
})
const result = await executeRequest('test-tool', toolWithTransform, {
url: 'https://api.example.com',
method: 'GET',
headers: {},
})
expect(result).toEqual({
success: true,
output: {
parsedData: 'mocked xml parsing result',
originalXml: '<xml><test>Mock XML response</test></xml>',
},
})
})
})
describe('createParamSchema', () => {