{{ message }}
refactor: extract generic executeBackendRequest[T] to eliminate duplicate backend call pattern#8153
Merged
Merged
Conversation
…uplicate backend call pattern
Copilot
AI
changed the title
[WIP] Fix duplicate code pattern in prompt handler
refactor: extract generic executeBackendRequest[T] to eliminate duplicate backend call pattern
Jun 26, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors unified-mode backend RPC invocation by introducing a generic executeBackendRequest[T] helper to centralize the backend connection/request/error/unmarshal lifecycle, then reuses it to remove duplicated logic in prompt handling.
Changes:
- Added
executeBackendRequest[T any]to unify backend request execution and JSON unmarshalling into typed results. - Refactored
executeBackendToolCallinto a thin wrapper aroundexecuteBackendRequest. - Simplified prompt handler registration to use the shared helper instead of duplicating request/unmarshal code.
Show a summary per file
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Low
Comment on lines
+220
to
+222
| if response.Error != nil { | ||
| logUnified.Printf("executeBackendToolCall: backend error: serverID=%s, toolName=%s, code=%d", serverID, toolName, response.Error.Code) | ||
| return nil, fmt.Errorf("backend error: code=%d, message=%s", response.Error.Code, response.Error.Message) | ||
| logUnified.Printf("executeBackendRequest: backend error: serverID=%s, method=%s, code=%d", serverID, method, response.Error.Code) | ||
| return zero, fmt.Errorf("backend error %s: code=%d, message=%s", method, response.Error.Code, response.Error.Message) |
Comment on lines
+420
to
423
| result, err := executeBackendRequest[sdk.GetPromptResult](ctx, us.launcher, serverIDCopy, sessionID, "prompts/get", params) | ||
| if err != nil { | ||
| return nil, err | ||
| } |
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

The prompt handler in
registerPromptsFromBackendduplicated the connect → send → error-check → unmarshal pattern thatexecuteBackendToolCallwas introduced to centralize, creating two diverging implementations with inconsistent error messages.Changes
internal/server/unified.go: AddsexecuteBackendRequest[T any]— a generic helper that owns the full backend RPC lifecycle for any method/result type. RefactorsexecuteBackendToolCallinto a thin wrapper delegating to it:internal/server/tool_registry.go: Replaces the ~20-line inline duplicate in the prompt handler closure with a single call:Future backend capability types (e.g.
resources/read) can now reuseexecuteBackendRequestwith zero boilerplate, and error handling improvements apply uniformly across all backend call sites.