refactor(core): Centralize tool response formatting (#743)

This commit is contained in:
N. Taylor Mullen
2025-06-04 00:24:25 -07:00
committed by GitHub
parent 4b2af10b04
commit d179b3aae4
8 changed files with 300 additions and 222 deletions

View File

@@ -138,12 +138,7 @@ describe('DiscoveredMCPTool', () => {
const stringifiedResponseContent = JSON.stringify(
mockToolSuccessResultObject,
);
// getStringifiedResultForDisplay joins text parts, then wraps the array of processed parts in JSON
const expectedDisplayOutput =
'```json\n' +
JSON.stringify([stringifiedResponseContent], null, 2) +
'\n```';
expect(toolResult.returnDisplay).toBe(expectedDisplayOutput);
expect(toolResult.returnDisplay).toBe(stringifiedResponseContent);
});
it('should handle empty result from getStringifiedResultForDisplay', async () => {

View File

@@ -149,6 +149,13 @@ function getStringifiedResultForDisplay(result: Part[]) {
return part; // Fallback for unexpected structure or non-FunctionResponsePart
};
const processedResults = result.map(processFunctionResponse);
const processedResults =
result.length === 1
? processFunctionResponse(result[0])
: result.map(processFunctionResponse);
if (typeof processedResults === 'string') {
return processedResults;
}
return '```json\n' + JSON.stringify(processedResults, null, 2) + '\n```';
}