Skip to content

feat: add timeout guard for tool execution #37

@anurag629

Description

@anurag629

Context

PR #34 wired up tool calling execution across all providers. Currently, if tool.execute() hangs (e.g., a slow webhook target or external API), the entire SSE stream blocks indefinitely with no timeout.

What to do

Add a configurable timeout wrapper around tool execution in packages/core/src/providers/tool-execution.ts:

const TOOL_TIMEOUT_MS = 30_000; // 30 seconds default

export async function executeToolCall(
  params: ProviderChatParams,
  call: ProviderToolCall
): Promise<ToolResult> {
  if (!params.toolExecutor) {
    return { success: false, message: `No tool executor for "${call.name}".` };
  }

  try {
    const result = await Promise.race([
      params.toolExecutor(call),
      new Promise<never>((_, reject) =>
        setTimeout(() => reject(new Error('Tool execution timed out')), TOOL_TIMEOUT_MS)
      ),
    ]);
    return result;
  } catch (error) {
    return toToolFailure(error);
  }
}

On timeout, the tool returns a failure result and the AI can respond gracefully (e.g., "Sorry, I couldn't complete that action").

Optionally, make the timeout configurable via ProviderChatParams or ChatCopsServerConfig.

Files to modify

File Change
packages/core/src/providers/tool-execution.ts Add Promise.race timeout wrapper
packages/core/src/types.ts Optionally add toolTimeout to ProviderChatParams

Tests to add

  • Tool execution that exceeds timeout returns failure result
  • Tool execution within timeout returns normal result
  • Timeout value is configurable

Metadata

Metadata

Assignees

Labels

coreAffects @chatcops/core packageenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions