Skip to content

Commit c7dce48

Browse files
authored
fix(toolbox-core): Simplify tool invocation response handling (#69)
* fix(toolbox-core): Simplify tool invocation response handling * chore: Fix e2e tests
1 parent bfbc4f6 commit c7dce48

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

packages/toolbox-core/src/toolbox_core/tool.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ function ToolboxTool(
177177
headers,
178178
}
179179
);
180-
return typeof response.data === 'string'
181-
? response.data
182-
: JSON.stringify(response.data);
180+
return response.data.result;
183181
} catch (error) {
184182
logApiError(`Error posting data to ${toolUrl}:`, error);
185183
throw error;

packages/toolbox-core/test/e2e/test.e2e.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ describe('ToolboxClient E2E Tests', () => {
354354
it('should run tool with optional params omitted', async () => {
355355
const response = await searchRowsTool({email: '[email protected]'});
356356
expect(typeof response).toBe('string');
357-
expect(response).toContain('\\"email\\":\\"[email protected]\\"');
357+
expect(response).toContain('"email":"[email protected]"');
358358
expect(response).not.toContain('row1');
359359
expect(response).toContain('row2');
360360
expect(response).not.toContain('row3');
@@ -369,7 +369,7 @@ describe('ToolboxClient E2E Tests', () => {
369369
data: 'row3',
370370
});
371371
expect(typeof response).toBe('string');
372-
expect(response).toContain('\\"email\\":\\"[email protected]\\"');
372+
expect(response).toContain('"email":"[email protected]"');
373373
expect(response).not.toContain('row1');
374374
expect(response).not.toContain('row2');
375375
expect(response).toContain('row3');
@@ -384,7 +384,7 @@ describe('ToolboxClient E2E Tests', () => {
384384
data: null,
385385
});
386386
expect(typeof response).toBe('string');
387-
expect(response).toContain('\\"email\\":\\"[email protected]\\"');
387+
expect(response).toContain('"email":"[email protected]"');
388388
expect(response).not.toContain('row1');
389389
expect(response).toContain('row2');
390390
expect(response).not.toContain('row3');
@@ -399,7 +399,7 @@ describe('ToolboxClient E2E Tests', () => {
399399
id: 1,
400400
});
401401
expect(typeof response).toBe('string');
402-
expect(response).toBe('{"result":"null"}');
402+
expect(response).toBe('null');
403403
});
404404

405405
it('should run tool with optional id as null', async () => {
@@ -408,7 +408,7 @@ describe('ToolboxClient E2E Tests', () => {
408408
id: null,
409409
});
410410
expect(typeof response).toBe('string');
411-
expect(response).toContain('\\"email\\":\\"[email protected]\\"');
411+
expect(response).toContain('"email":"[email protected]"');
412412
expect(response).not.toContain('row1');
413413
expect(response).toContain('row2');
414414
expect(response).not.toContain('row3');
@@ -438,7 +438,7 @@ describe('ToolboxClient E2E Tests', () => {
438438
data: 'row2',
439439
});
440440
expect(typeof response).toBe('string');
441-
expect(response).toContain('\\"email\\":\\"[email protected]\\"');
441+
expect(response).toContain('"email":"[email protected]"');
442442
expect(response).not.toContain('row1');
443443
expect(response).toContain('row2');
444444
expect(response).not.toContain('row3');
@@ -454,7 +454,7 @@ describe('ToolboxClient E2E Tests', () => {
454454
data: 'row3',
455455
});
456456
expect(typeof response).toBe('string');
457-
expect(response).toContain('\\"email\\":\\"[email protected]\\"');
457+
expect(response).toContain('"email":"[email protected]"');
458458
expect(response).not.toContain('row1');
459459
expect(response).not.toContain('row2');
460460
expect(response).toContain('row3');
@@ -470,7 +470,7 @@ describe('ToolboxClient E2E Tests', () => {
470470
data: 'row3',
471471
});
472472
expect(typeof response).toBe('string');
473-
expect(response).toBe('{"result":"null"}');
473+
expect(response).toBe('null');
474474
});
475475

476476
it('should return null when called with different data', async () => {
@@ -480,7 +480,7 @@ describe('ToolboxClient E2E Tests', () => {
480480
data: 'row4',
481481
});
482482
expect(typeof response).toBe('string');
483-
expect(response).toBe('{"result":"null"}');
483+
expect(response).toBe('null');
484484
});
485485

486486
it('should return null when called with a different id', async () => {
@@ -490,7 +490,7 @@ describe('ToolboxClient E2E Tests', () => {
490490
data: 'row3',
491491
});
492492
expect(typeof response).toBe('string');
493-
expect(response).toBe('{"result":"null"}');
493+
expect(response).toBe('null');
494494
});
495495
});
496496
});

packages/toolbox-core/test/test.tool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ describe('ToolboxTool', () => {
307307
expect(mockAxiosPost).toHaveBeenCalledWith(expectedUrl, validArgs, {
308308
headers: {},
309309
});
310-
expect(result).toEqual(JSON.stringify(mockApiResponseData));
310+
expect(result).toEqual(mockApiResponseData['result']);
311311
});
312312

313313
it('should re-throw the error and log to console.error if API call fails', async () => {
@@ -421,7 +421,7 @@ describe('ToolboxTool', () => {
421421

422422
it('should validate only the user-provided arguments, not the bound ones', async () => {
423423
const boundTool = tool.bindParams({query: 'a valid query'});
424-
mockAxiosPost.mockResolvedValueOnce({data: 'success'});
424+
mockAxiosPost.mockResolvedValueOnce({data: {result: 'success'}});
425425
// This call is valid because 'query' is bound, and no invalid args are passed
426426
await expect(boundTool()).resolves.toBe('success');
427427
});

0 commit comments

Comments
 (0)