Skip to content

refactor: improve robustness, eliminate duplication, and align with Go idioms#1

Merged
AlanFokCo merged 2 commits into
mainfrom
copilot/optimize-project-implementation
Mar 13, 2026
Merged

refactor: improve robustness, eliminate duplication, and align with Go idioms#1
AlanFokCo merged 2 commits into
mainfrom
copilot/optimize-project-implementation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

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-grown errorAs() (no chain unwrapping, used deprecated net.Error.Temporary()) with stdlib errors.As
  • session: len(k) >= len(prefix) && k[:len(prefix)] == prefixstrings.HasPrefix(k, prefix) (operator-precedence bug, applied to both MemorySession and JSONSession)

Duplication → shared abstraction

All three model constructors repeated identical HTTP client initialization. Extracted into model/http.go:

// Before (repeated verbatim in openai.go, dashscope.go, anthropic.go)
if client == nil {
    client = &http.Client{Timeout: 60 * time.Second}
}

// After
const defaultModelTimeout = 60 * time.Second

func defaultHTTPClient(c *http.Client) *http.Client {
    if c != nil {
        return c
    }
    return &http.Client{Timeout: defaultModelTimeout}
}

Default base URLs and API version strings promoted to named constants (defaultOpenAIBaseURL, defaultDashScopeBaseURL, defaultAnthropicBaseURL, defaultAnthropicVersion, defaultAnthropicMaxOutputTokens).

Silent failures → observable warnings

react_agent.go was discarding memory-save and print errors with _ =. These are now logged via logrus.Warn so failures surface in production logs.

Miscellaneous

  • message/msg.go: TimestampFormat constant replaces duplicated layout string; NewMsg type guard simplified to a switch; strings.Join replaces manual loop in GetTextContent
  • react_agent.go: magic 4 and 3defaultMaxIters / defaultTopK; retrieveKnowledge uses strings.Builder instead of += concatenation
  • httpx.go: defaultMaxAttempts / defaultBaseBackoff constants 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.

- 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
Copilot AI requested a review from AlanFokCo March 13, 2026 10:16
@AlanFokCo AlanFokCo marked this pull request as ready for review March 13, 2026 10:16
@AlanFokCo AlanFokCo merged commit 6d5416a into main Mar 13, 2026
1 check passed
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