Skip to content

Conversation

@tianzhou
Copy link
Member

Extract common patterns from tool handlers into shared utilities to reduce duplication and improve maintainability.

Changes:

  • Add src/utils/tool-handler-helpers.ts with shared utilities:

    • getEffectiveSourceId(): Normalize source ID handling
    • validateReadonlySQL(): Validate SQL against readonly constraints
    • trackToolRequest(): Centralize request tracking logic
    • withRequestTracking(): HOF wrapper (available for future use)
  • Refactor custom-tool-handler.ts to use new utilities

  • Refactor execute-sql.ts to use new utilities

  • Refactor search-objects.ts to use new utilities

Benefits:

  • Each handler reduced by ~20-30 lines
  • Centralizes cross-cutting concerns (tracking, validation)
  • Handlers focus on core logic
  • Sets pattern for future tool handlers

All tests passing (70/70 tool tests). Zero behavior changes.

Generated with Claude Code (https://claude.com/claude-code)

Extract common patterns from tool handlers into shared utilities to
reduce duplication and improve maintainability.

Changes:
- Add src/utils/tool-handler-helpers.ts with shared utilities:
  - getEffectiveSourceId(): Normalize source ID handling
  - validateReadonlySQL(): Validate SQL against readonly constraints
  - trackToolRequest(): Centralize request tracking logic
  - withRequestTracking(): HOF wrapper (available for future use)

- Refactor custom-tool-handler.ts to use new utilities
- Refactor execute-sql.ts to use new utilities
- Refactor search-objects.ts to use new utilities

Benefits:
- Each handler reduced by ~20-30 lines
- Centralizes cross-cutting concerns (tracking, validation)
- Handlers focus on core logic
- Sets pattern for future tool handlers

All tests passing (70/70 tool tests). Zero behavior changes.

Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Copilot AI review requested due to automatic review settings December 26, 2025 16:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors tool handler utilities by extracting common patterns into a shared tool-handler-helpers.ts file. The goal is to reduce code duplication across tool handlers (custom-tool-handler.ts, execute-sql.ts, and search-objects.ts) by centralizing source ID normalization, request tracking, and SQL validation logic.

Key Changes:

  • Created new utility functions for common patterns: getEffectiveSourceId(), validateReadonlySQL(), trackToolRequest(), and withRequestTracking()
  • Refactored three tool handlers to use the new shared utilities
  • Reduced code duplication by ~20-30 lines per handler

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/utils/tool-handler-helpers.ts New utility file with shared helper functions for tool handlers (source ID normalization, readonly validation, request tracking)
src/tools/custom-tool-handler.ts Refactored to use getEffectiveSourceId(), validateReadonlySQL(), and trackToolRequest() utilities
src/tools/execute-sql.ts Refactored to use getEffectiveSourceId() and trackToolRequest() utilities (kept custom readonly validation)
src/tools/search-objects.ts Refactored to use getEffectiveSourceId() and trackToolRequest() utilities

Address all three issues from code review:

1. Preserve READONLY_VIOLATION error code
   - Replace validateReadonlySQL() with two separate functions:
     - isAllowedInReadonlyMode(): Check if SQL is allowed
     - createReadonlyViolationMessage(): Format error message
   - Keep error handling inline to preserve error code

2. Remove redundant readonly parameter
   - New functions don't need the boolean flag
   - Validation only called when readonly=true

3. Remove unused import
   - Removed getEffectiveSourceId from custom-tool-handler.ts
   - Not needed since sourceId comes from toolConfig

All tests passing (70/70). Build successful. True zero behavior changes.

Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Address code review feedback on type safety and clarity:

1. Fix RequestMetadata type safety
   - Change sql from optional (sql?) to required (sql: string)
   - Aligns with Request interface in requestStore
   - All callers already provide sql, preventing runtime errors

2. Simplify isAllowedInReadonlyMode wrapper
   - Use re-export instead of wrapper function
   - Reduces indirection while maintaining clear naming
   - No added value from thin wrapper

All tests passing (70/70). Build successful.

Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@tianzhou tianzhou merged commit 4b6f571 into main Dec 26, 2025
8 checks passed
@tianzhou tianzhou deleted the refactor branch December 27, 2025 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants