refactor: improve robustness, eliminate duplication, and align with Go idioms#1
Merged
Merged
Conversation
- message/msg.go: add TimestampFormat constant; clean up NewMsg type-check with type-switch; use strings.Join in GetTextContent - internal/httpx/httpx.go: extract defaultMaxAttempts/defaultBaseBackoff constants; replace custom errorAs with stdlib errors.As (correctness fix for error-chain unwrapping; remove deprecated net.Error.Temporary) - session/session.go: use strings.HasPrefix for prefix matching in both MemorySession and JSONSession (removes operator-precedence ambiguity) - model/http.go: new shared file with defaultModelTimeout constant and defaultHTTPClient helper to eliminate triple duplication - model/openai.go: use defaultHTTPClient helper; add defaultOpenAIBaseURL - model/dashscope.go: use defaultHTTPClient helper; add defaultDashScopeBaseURL - model/anthropic.go: use defaultHTTPClient helper; add defaultAnthropicBaseURL, defaultAnthropicVersion, defaultAnthropicMaxOutputTokens constants - agent/react_agent.go: add defaultMaxIters and defaultTopK constants; log memory-save and print errors via logrus.Warn instead of silently discarding; use strings.Builder in retrieveKnowledge for efficient string building Co-authored-by: AlanFokCo <49893220+AlanFokCo@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor project implementation for elegance and robustness
refactor: improve robustness, eliminate duplication, and align with Go idioms
Mar 13, 2026
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 codebase had several recurring issues: magic numbers, copy-pasted HTTP client setup across three model constructors, a non-standards-conforming error helper that skipped error-chain unwrapping, operator-precedence ambiguity in prefix matching, and silently discarded errors in the agent's hot path.
Correctness
httpx: Replaced home-grownerrorAs()(no chain unwrapping, used deprecatednet.Error.Temporary()) with stdliberrors.Assession:len(k) >= len(prefix) && k[:len(prefix)] == prefix→strings.HasPrefix(k, prefix)(operator-precedence bug, applied to bothMemorySessionandJSONSession)Duplication → shared abstraction
All three model constructors repeated identical HTTP client initialization. Extracted into
model/http.go:Default base URLs and API version strings promoted to named constants (
defaultOpenAIBaseURL,defaultDashScopeBaseURL,defaultAnthropicBaseURL,defaultAnthropicVersion,defaultAnthropicMaxOutputTokens).Silent failures → observable warnings
react_agent.gowas discarding memory-save and print errors with_ =. These are now logged vialogrus.Warnso failures surface in production logs.Miscellaneous
message/msg.go:TimestampFormatconstant replaces duplicated layout string;NewMsgtype guard simplified to aswitch;strings.Joinreplaces manual loop inGetTextContentreact_agent.go: magic4and3→defaultMaxIters/defaultTopK;retrieveKnowledgeusesstrings.Builderinstead of+=concatenationhttpx.go:defaultMaxAttempts/defaultBaseBackoffconstants replace inline literals🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.