Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(tool-router): tighten annotation routing and e2e retries
Dhawal Upadhyaydb8f08ba9529
diff --git a/python/composio/core/models/custom_tool.py b/python/composio/core/models/custom_tool.pyindex 626a423e0..154ae7dc5 100644--- a/python/composio/core/models/custom_tool.py+++ b/python/composio/core/models/custom_tool.py@@ -221,6 +221,35 @@ def _create_tool()+def _get_caller_locals(depth: int = 2) -> t.Optional[t.Mapping[str, t.Any]]:+ """Best-effort lookup of a caller frame's locals."""+ frame = inspect.currentframe()+ try:+ caller = frame+ for _ in range(depth):+ caller = caller.f_back if caller is not None else None+ return caller.f_locals if caller is not None else None+ finally:+ del frame+++def _resolve_function_annotations(+ fn: t.Callable[..., t.Any],+ *,+ localns: t.Optional[t.Mapping[str, t.Any]] = None,+) -> t.Dict[str, t.Any]:+ """Resolve annotations, including postponed string annotations when possible."""+ try:+ return t.get_type_hints(+ fn,+ globalns=getattr(fn, "__globals__", {}),+ localns=dict(localns) if localns is not None else None,+ include_extras=True,+ )+ except Exception:+ return {}++def _infer_tool_from_function(fn: t.Callable[..., t.Any],*,@@ -229,6 +258,7 @@ def _infer_tool_from_function(description: t.Optional[str] = None,extends_toolkit: t.Optional[str] = None,output_params: t.Optional[t.Type[BaseModel]] = None,+ annotation_locals: t.Optional[t.Mapping[str, t.Any]] = None,) -> CustomTool:"""Create a CustomTool by inferring metadata from a decorated function.@@ -263,6 +293,10 @@ def _infer_tool_from_function(sig = inspect.signature(fn)params = list(sig.parameters.values())+ resolved_annotations = _resolve_function_annotations(+ fn,+ localns=annotation_locals,+ )if not params:raise ValidationError(@@ -280,15 +314,16 @@ def _infer_tool_from_function(# First param must be the input modelfirst = params[0]- if first.annotation is inspect.Parameter.empty or not (- isinstance(first.annotation, type) and issubclass(first.annotation, BaseModel)+ first_annotation = resolved_annotations.get(first.name, first.annotation)+ if first_annotation is inspect.Parameter.empty or not (+ isinstance(first_annotation, type) and issubclass(first_annotation, BaseModel)):raise ValidationError(f'experimental.tool: first parameter of "{fn.__name__}" must be annotated '- f"with a Pydantic BaseModel subclass. Got: {first.annotation!r}. "+ f"with a Pydantic BaseModel subclass. Got: {first_annotation!r}. "f"Expected: def {fn.__name__}(input: MyInput, ctx): ...")- input_params: t.Type[BaseModel] = first.annotation+ input_params: t.Type[BaseModel] = first_annotation# Wrap function to match CustomToolExecuteFn: (input, ctx) -> dictif len(params) == 1:@@ -383,12 +418,14 @@ class ExperimentalToolkit:"""def decorator(f: t.Callable[..., t.Any]) -> CustomTool:+ annotation_locals = _get_caller_locals()custom_tool = _infer_tool_from_function(f,slug=slug,name=name,description=description,output_params=output_params,+ annotation_locals=annotation_locals,# No extends_toolkit for toolkit tools)_validate_slug_length(@@ -398,7 +435,21 @@ class ExperimentalToolkit:return custom_toolif fn is not None:- return decorator(fn)+ custom_tool = _infer_tool_from_function(+ fn,+ slug=slug,+ name=name,+ description=description,+ output_params=output_params,+ annotation_locals=_get_caller_locals(),+ )+ _validate_slug_length(+ custom_tool.slug,+ self.slug,+ f'experimental.Toolkit("{self.slug}").tool',+ )+ self._tools.append(custom_tool)+ return custom_toolreturn decorator@@ -467,6 +518,7 @@ class ExperimentalAPI:"""def decorator(f: t.Callable[..., t.Any]) -> CustomTool:+ annotation_locals = _get_caller_locals()return _infer_tool_from_function(f,slug=slug,@@ -474,10 +526,19 @@ class ExperimentalAPI:description=description,extends_toolkit=extends_toolkit,output_params=output_params,+ annotation_locals=annotation_locals,)if fn is not None:- return decorator(fn)+ return _infer_tool_from_function(+ fn,+ slug=slug,+ name=name,+ description=description,+ extends_toolkit=extends_toolkit,+ output_params=output_params,+ annotation_locals=_get_caller_locals(),+ )return decoratordiff --git a/python/composio/core/models/tool_router_session.py b/python/composio/core/models/tool_router_session.pyindex 9639a0510..681c57233 100644--- a/python/composio/core/models/tool_router_session.py+++ b/python/composio/core/models/tool_router_session.py@@ -336,6 +336,15 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):{**entry, "index": i}for i, entry in enumerate([*remote_results_list, *local_entries])]+ failed = sum(1 for r in all_results if r.get("error"))+ merged_data = {**remote_data, "results": all_results}+ if local_entries and any(+ key in remote_data+ for key in ("total_count", "success_count", "error_count")+ ):+ merged_data["total_count"] = len(all_results)+ merged_data["success_count"] = len(all_results) - failed+ merged_data["error_count"] = failedremote_error = (str(remote_result.get("error"))@@ -345,7 +354,6 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):has_any_error = any(r.get("error") for _, r in local_results) or bool(remote_error)- failed = sum(1 for r in all_results if r.get("error"))error_message = Noneif has_any_error:error_message = (@@ -355,7 +363,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):)return {- "data": {**remote_data, "results": all_results},+ "data": merged_data,"error": error_message,"successful": not has_any_error,}diff --git a/python/tests/test_custom_tools.py b/python/tests/test_custom_tools.pyindex f7a12cc16..9dc0850ca 100644--- a/python/tests/test_custom_tools.py+++ b/python/tests/test_custom_tools.py@@ -241,6 +241,47 @@ class TestDecoratorTool:"""Swapped order — ctx first, input second."""return {}+ def test_future_annotations_are_resolved(self):+ namespace = {"exp": exp}+ exec(+ """+from __future__ import annotations++from pydantic import BaseModel, Field+++class FutureInput(BaseModel):+ pattern: str = Field(description="Pattern to search for")+++def future_tool(input: FutureInput, ctx):+ '''Resolved from postponed annotations.'''+ return {}+""",+ namespace,+ )++ future_tool = namespace["future_tool"]+ future_input = namespace["FutureInput"]++ assert future_tool.input_params is future_input++ def test_local_string_annotations_are_resolved(self):+ def build_tool():+ class LocalInput(BaseModel):+ pattern: str = Field(description="Pattern to search for")++ @exp.tool+ def nested_search(input: "LocalInput", ctx):+ """Resolved from local string annotations."""+ return {}++ return nested_search, LocalInput++ local_tool, local_input = build_tool()+ assert local_tool.input_params is local_input+def test_async_function_rejected(self):with pytest.raises(ValidationError, match="async"):@@ -655,8 +696,11 @@ class TestMultiExecuteRouting:remote = {"data": {"results": [- {"tool_slug": "R", "response": {"successful": True, "data": {}}}- ]+ {"tool_slug": "R", "response": {"successful": True, "data": {}}},+ ],+ "total_count": 1,+ "success_count": 1,+ "error_count": 0,},"error": None,"successful": True,@@ -676,6 +720,9 @@ class TestMultiExecuteRouting:# Remotes first, locals appended (matches TS)assert results[0]["tool_slug"] == "R"assert results[1]["tool_slug"] == "GREP"+ assert result["data"]["total_count"] == 2+ assert result["data"]["success_count"] == 2+ assert result["data"]["error_count"] == 0def test_failure_propagated(self):@exp.tool()diff --git a/ts/e2e-tests/runtimes/node/tool-router-files/fixtures/index.mjs b/ts/e2e-tests/runtimes/node/tool-router-files/fixtures/index.mjsindex 3cccec97a..df343bd57 100644--- a/ts/e2e-tests/runtimes/node/tool-router-files/fixtures/index.mjs+++ b/ts/e2e-tests/runtimes/node/tool-router-files/fixtures/index.mjs@@ -32,7 +32,9 @@ async function main() {if (!uploaded.mountRelativePath || !uploaded.downloadUrl) {throw new Error('Upload failed: missing mountRelativePath or downloadUrl');}- const storedPath = uploaded.mountRelativePath.replace(/^\//, '');+ const candidatePaths = Array.from(+ new Set([uploaded.mountRelativePath, uploaded.mountRelativePath.replace(/^\/+/, '')]).values()+ ).filter(Boolean);console.log('UPLOAD_OK');// List files (retry for eventual consistency; omit path for root - SDK normalizes)@@ -40,8 +42,8 @@ async function main() {for (let attempt = 0; attempt < 5; attempt++) {const listResult = await files.list();const found = listResult.items?.some(- (item) =>- item.mountRelativePath === storedPath ||+ item =>+ candidatePaths.includes(item.mountRelativePath) ||item.mountRelativePath === testPath ||item.mountRelativePath?.endsWith(testPath) ||item.mountRelativePath?.includes(testPath)@@ -50,36 +52,50 @@ async function main() {listOk = true;break;}- await new Promise((r) => setTimeout(r, 1000));+ await new Promise(r => setTimeout(r, 1000));}if (listOk) console.log('LIST_OK');else console.log('LIST_SKIP'); // eventual consistency// Download the file (retry for eventual consistency, same as list above)let downloaded;+ let lastDownloadError;+ let resolvedPath;for (let attempt = 0; attempt < 5; attempt++) {- try {- downloaded = await files.download(storedPath);+ for (const candidatePath of candidatePaths) {+ try {+ const remoteFile = await files.download(candidatePath);+ const content = await remoteFile.text();+ if (content !== testContent) {+ throw new Error(`Download failed: expected "${testContent}", got "${content}"`);+ }+ downloaded = remoteFile;+ resolvedPath = candidatePath;+ break;+ } catch (err) {+ lastDownloadError = err;+ }+ }+ if (downloaded) {break;- } catch (err) {- if (attempt === 4) throw err;- await new Promise((r) => setTimeout(r, 1000));+ }+ if (attempt < 4) {+ await new Promise(r => setTimeout(r, 1000));}}- const content = await downloaded.text();- if (content !== testContent) {- throw new Error(`Download failed: expected "${testContent}", got "${content}"`);+ if (!downloaded) {+ throw lastDownloadError ?? new Error('Download failed after retries');}console.log('DOWNLOAD_OK');// Delete the file (use path from API response)- await files.delete(storedPath);+ await files.delete(resolvedPath ?? candidatePaths[0]);console.log('DELETE_OK');console.log('ALL_OK');}-main().catch((err) => {+main().catch(err => {console.log('ERROR:', err?.message || err);process.exit(1);});diff --git a/ts/packages/core/src/models/ToolRouterSession.ts b/ts/packages/core/src/models/ToolRouterSession.tsindex 108c5be50..53dff54d3 100644--- a/ts/packages/core/src/models/ToolRouterSession.ts+++ b/ts/packages/core/src/models/ToolRouterSession.ts@@ -469,12 +469,24 @@ export class ToolRouterSession<index: i,}));+ const failedCount = allResults.filter(r => r.error).length;+ const mergedData: Record<string, unknown> = {+ ...remoteData,+ results: allResults,+ };+ if (+ localEntries.length > 0 &&+ ['total_count', 'success_count', 'error_count'].some(key => key in remoteData)+ ) {+ mergedData.total_count = allResults.length;+ mergedData.success_count = allResults.length - failedCount;+ mergedData.error_count = failedCount;+ }const remoteError = typeof remoteResult?.error === 'string' ? remoteResult.error : null;const hasAnyError = localResults.some(r => r.result.error) || !!remoteError;- const failedCount = allResults.filter(r => r.error).length;return {- data: { ...remoteData, results: allResults },+ data: mergedData,error: hasAnyError? remoteError && failedCount === 0? remoteErrordiff --git a/ts/packages/core/test/models/customToolRouting.test.ts b/ts/packages/core/test/models/customToolRouting.test.tsindex 264fab316..4ae358688 100644--- a/ts/packages/core/test/models/customToolRouting.test.ts+++ b/ts/packages/core/test/models/customToolRouting.test.ts@@ -671,6 +671,26 @@ describe('ToolRouterSession execution routing', () => {expect(result.successful).toBe(true);});+ it('should recompute remote counters when local results are merged', async () => {+ const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);++ toolsInstance.executeMetaTool.mockResolvedValueOnce(+ backendResponse([{ tool_slug: 'GMAIL_SEND_EMAIL', data: { sent: true } }])+ );++ const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {+ tools: [+ { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'counts' } },+ { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: '[email protected]' } },+ ],+ sync_response_to_workbench: false,+ });++ expect(result.data.total_count).toBe(2);+ expect(result.data.success_count).toBe(2);+ expect(result.data.error_count).toBe(0);+ });+it('should handle remote null data without crashing', async () => {const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);