diff options
| author | Paul Buetow <paul@buetow.org> | 2025-08-17 23:06:37 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-08-17 23:06:37 +0300 |
| commit | 041d1f140436c6fdd223844b04c6592c84951878 (patch) | |
| tree | e44df5d5691408216a26d472f7e96278095319d2 /internal | |
| parent | d72f95ae4e6cd4e7a0beca2b9764511c10de8655 (diff) | |
refactor(ordering): place constructors immediately after type definitions as first functions
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/appconfig/config.go | 21 | ||||
| -rw-r--r-- | internal/llm/copilot.go | 34 | ||||
| -rw-r--r-- | internal/llm/ollama.go | 32 | ||||
| -rw-r--r-- | internal/llm/openai.go | 37 |
4 files changed, 63 insertions, 61 deletions
diff --git a/internal/appconfig/config.go b/internal/appconfig/config.go index 1f7e9d8..7027547 100644 --- a/internal/appconfig/config.go +++ b/internal/appconfig/config.go @@ -31,6 +31,17 @@ type App struct { CopilotModel string `json:"copilot_model"` } +// Constructor: defaults for App (kept first among functions) +func newDefaultConfig() App { + return App{ + MaxTokens: 4000, + ContextMode: "always-full", + ContextWindowLines: 120, + MaxContextTokens: 4000, + LogPreviewLimit: 100, + } +} + // Load reads configuration from a file and merges with defaults. // It respects the XDG Base Directory Specification. func Load(logger *log.Logger) App { @@ -55,16 +66,6 @@ func Load(logger *log.Logger) App { } // Private helpers -func newDefaultConfig() App { - return App{ - MaxTokens: 4000, - ContextMode: "always-full", - ContextWindowLines: 120, - MaxContextTokens: 4000, - LogPreviewLimit: 100, - } -} - func loadFromFile(path string, logger *log.Logger) (*App, error) { f, err := os.Open(path) if err != nil { diff --git a/internal/llm/copilot.go b/internal/llm/copilot.go index 22b0ae4..680e7ec 100644 --- a/internal/llm/copilot.go +++ b/internal/llm/copilot.go @@ -54,6 +54,23 @@ type copilotChatResponse struct { } `json:"error,omitempty"` } +// Constructor (kept among the first functions by convention) +func newCopilot(baseURL, model, apiKey string) Client { + if strings.TrimSpace(baseURL) == "" { + baseURL = "https://api.githubcopilot.com" + } + if strings.TrimSpace(model) == "" { + model = "gpt-4.1" + } + return copilotClient{ + httpClient: &http.Client{Timeout: 30 * time.Second}, + apiKey: apiKey, + baseURL: strings.TrimRight(baseURL, "/"), + defaultModel: model, + chatLogger: logging.NewChatLogger("copilot"), + } +} + func (c copilotClient) Chat(ctx context.Context, messages []Message, opts ...RequestOption) (string, error) { if strings.TrimSpace(c.apiKey) == "" { return nilStringErr("missing Copilot API key") @@ -144,20 +161,3 @@ func (c copilotClient) Chat(ctx context.Context, messages []Message, opts ...Req // Provider metadata func (c copilotClient) Name() string { return "copilot" } func (c copilotClient) DefaultModel() string { return c.defaultModel } - -// Private constructor -func newCopilot(baseURL, model, apiKey string) Client { - if strings.TrimSpace(baseURL) == "" { - baseURL = "https://api.githubcopilot.com" - } - if strings.TrimSpace(model) == "" { - model = "gpt-4.1" - } - return copilotClient{ - httpClient: &http.Client{Timeout: 30 * time.Second}, - apiKey: apiKey, - baseURL: strings.TrimRight(baseURL, "/"), - defaultModel: model, - chatLogger: logging.NewChatLogger("copilot"), - } -} diff --git a/internal/llm/ollama.go b/internal/llm/ollama.go index a796d8c..14aa558 100644 --- a/internal/llm/ollama.go +++ b/internal/llm/ollama.go @@ -40,6 +40,22 @@ type ollamaChatResponse struct { Error string `json:"error,omitempty"` } +// Constructor (kept among the first functions by convention) +func newOllama(baseURL, model string) Client { + if strings.TrimSpace(baseURL) == "" { + baseURL = "http://localhost:11434" + } + if strings.TrimSpace(model) == "" { + model = "qwen2.5-coder:latest" + } + return ollamaClient{ + httpClient: &http.Client{Timeout: 30 * time.Second}, + baseURL: strings.TrimRight(baseURL, "/"), + defaultModel: model, + chatLogger: logging.NewChatLogger("ollama"), + } +} + func (c ollamaClient) Chat(ctx context.Context, messages []Message, opts ...RequestOption) (string, error) { o := Options{Model: c.defaultModel} for _, opt := range opts { @@ -228,19 +244,3 @@ func (c ollamaClient) ChatStream(ctx context.Context, messages []Message, onDelt logging.Logf("llm/ollama ", "stream end duration=%s", time.Since(start)) return nil } - -// Private constructor -func newOllama(baseURL, model string) Client { - if strings.TrimSpace(baseURL) == "" { - baseURL = "http://localhost:11434" - } - if strings.TrimSpace(model) == "" { - model = "qwen2.5-coder:latest" - } - return ollamaClient{ - httpClient: &http.Client{Timeout: 30 * time.Second}, - baseURL: strings.TrimRight(baseURL, "/"), - defaultModel: model, - chatLogger: logging.NewChatLogger("ollama"), - } -} diff --git a/internal/llm/openai.go b/internal/llm/openai.go index ed5629e..8dc2907 100644 --- a/internal/llm/openai.go +++ b/internal/llm/openai.go @@ -72,6 +72,25 @@ type oaStreamChunk struct { } `json:"error,omitempty"` } +// Constructor (kept among the first functions by convention) +// newOpenAI constructs an OpenAI client using explicit configuration values. +// The apiKey may be empty; calls will fail until a valid key is supplied. +func newOpenAI(baseURL, model, apiKey string) Client { + if strings.TrimSpace(baseURL) == "" { + baseURL = "https://api.openai.com/v1" + } + if strings.TrimSpace(model) == "" { + model = "gpt-4.1" + } + return openAIClient{ + httpClient: &http.Client{Timeout: 30 * time.Second}, + apiKey: apiKey, + baseURL: baseURL, + defaultModel: model, + chatLogger: logging.NewChatLogger("openai"), + } +} + func (c openAIClient) Chat(ctx context.Context, messages []Message, opts ...RequestOption) (string, error) { if c.apiKey == "" { return nilStringErr("missing OpenAI API key") @@ -277,21 +296,3 @@ func (c openAIClient) ChatStream(ctx context.Context, messages []Message, onDelt // Private helpers func (c openAIClient) logf(format string, args ...any) { logging.Logf("llm/openai ", format, args...) } - -// newOpenAI constructs an OpenAI client using explicit configuration values. -// The apiKey may be empty; calls will fail until a valid key is supplied. -func newOpenAI(baseURL, model, apiKey string) Client { - if strings.TrimSpace(baseURL) == "" { - baseURL = "https://api.openai.com/v1" - } - if strings.TrimSpace(model) == "" { - model = "gpt-4.1" - } - return openAIClient{ - httpClient: &http.Client{Timeout: 30 * time.Second}, - apiKey: apiKey, - baseURL: baseURL, - defaultModel: model, - chatLogger: logging.NewChatLogger("openai"), - } -} |
