mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
fix(tools): added transform response to handle non-json responses for internal tools (#1400)
This commit is contained in:
+11
-7
@@ -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}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user