mirror of
https://github.com/lukaszraczylo/harness-sync.git
synced 2026-10-12 00:45:22 +00:00
refactor: remove dead code, deduplicate defaultHome, simplify mcpEntry + kilo render
Dead code removed: - BuildProviders, buildModels, ProviderEntry (no production callers) - ZedLanguageModels (shadowed by adapter-local version, wrong schema) - GatewayProviderID deprecated constant (zero references) - Corresponding dead tests for BuildProviders Deduplication: - Extract common.DefaultHome() to internal/adapter/common/home.go - All 7 adapters call common.DefaultHome() instead of local copies (-49 lines) Simplification: - mcp.go: merge MCPClaudeStyle/MCPCrushStyle branches (identical URL path, one-line difference in stdio path) — clearer intent, fewer lines - kilo.go: extract opencode.jsonc merge to mergeOpenCodeJSONC() helper, Render() drops from 92 to ~55 lines - goose.go: strings.IndexByte → strings.Cut in splitProviderModel
This commit is contained in:
@@ -11,10 +11,6 @@ import (
|
||||
|
||||
const fallbackGatewayProviderID = "harness-sync-gateway"
|
||||
|
||||
// GatewayProviderID is the canonical provider key used across all adapters.
|
||||
// Deprecated: prefer GatewayProviderKey(gatewayURL) for a stable URL-derived key.
|
||||
const GatewayProviderID = fallbackGatewayProviderID
|
||||
|
||||
// GatewayProviderKey returns a stable provider key derived from the gateway URL.
|
||||
// Format: "hs-<hostname>" with dots and special chars normalised to dashes.
|
||||
// Falls back to "harness-sync-gateway" when the URL is empty or unparseable.
|
||||
@@ -34,52 +30,6 @@ func GatewayProviderKey(gatewayURL string) string {
|
||||
return "hs-" + r.Replace(host)
|
||||
}
|
||||
|
||||
// ProviderEntry is a generic provider record. Adapters may massage further.
|
||||
type ProviderEntry = map[string]any
|
||||
|
||||
// BuildProviders returns the canonical-to-generic provider list: gateway first
|
||||
// (if configured), then any upstreams. Each entry has id, name, base_url,
|
||||
// api_key, and (for the gateway) a models list.
|
||||
func BuildProviders(p *canonical.Profile) []ProviderEntry {
|
||||
out := make([]ProviderEntry, 0, len(p.Upstreams)+1)
|
||||
if p.Gateway.URL != "" {
|
||||
key := GatewayProviderKey(p.Gateway.URL)
|
||||
gw := ProviderEntry{
|
||||
"id": key,
|
||||
"name": "harness-sync gateway",
|
||||
"base_url": p.Gateway.URL,
|
||||
"api_key": p.Gateway.Token,
|
||||
}
|
||||
if models := buildModels(p.Models); len(models) > 0 {
|
||||
gw["models"] = models
|
||||
}
|
||||
out = append(out, gw)
|
||||
}
|
||||
for _, up := range p.Upstreams {
|
||||
e := ProviderEntry{"id": up.Name, "name": up.Name}
|
||||
if up.BaseURL != "" {
|
||||
e["base_url"] = up.BaseURL
|
||||
}
|
||||
if up.APIKey != "" {
|
||||
e["api_key"] = up.APIKey
|
||||
}
|
||||
out = append(out, e)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func buildModels(models []canonical.Model) []map[string]any {
|
||||
out := make([]map[string]any, 0, len(models))
|
||||
for _, m := range models {
|
||||
e := map[string]any{"id": m.ID}
|
||||
if m.Alias != "" {
|
||||
e["alias"] = m.Alias
|
||||
}
|
||||
out = append(out, e)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// crushModel builds a 10-field model entry required by the crush schema.
|
||||
func crushModel(id string) map[string]any {
|
||||
return map[string]any{
|
||||
@@ -209,40 +159,6 @@ func ProvidersAsCagentMap(p *canonical.Profile) map[string]any {
|
||||
return out
|
||||
}
|
||||
|
||||
// ZedLanguageModels returns the language_models.openai block for Zed when a
|
||||
// gateway URL is configured.
|
||||
func ZedLanguageModels(p *canonical.Profile) map[string]any {
|
||||
if p.Gateway.URL == "" {
|
||||
return map[string]any{}
|
||||
}
|
||||
models := make([]map[string]any, 0)
|
||||
for _, m := range p.Models {
|
||||
models = append(models, map[string]any{
|
||||
"name": m.ID,
|
||||
"display_name": func() string {
|
||||
if m.Alias != "" {
|
||||
return m.Alias
|
||||
}
|
||||
return m.ID
|
||||
}(),
|
||||
"max_tokens": 8192,
|
||||
})
|
||||
}
|
||||
if len(models) == 0 && p.Gateway.DefaultModel != "" {
|
||||
models = append(models, map[string]any{
|
||||
"name": p.Gateway.DefaultModel,
|
||||
"display_name": p.Gateway.DefaultModel,
|
||||
"max_tokens": 8192,
|
||||
})
|
||||
}
|
||||
return map[string]any{
|
||||
"openai": map[string]any{
|
||||
"api_url": p.Gateway.URL,
|
||||
"available_models": models,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GooseCustomProviderFile returns the JSON body and provider ID for the goose
|
||||
// custom_providers/<id>.json file. Returns nil body when Gateway.URL is empty.
|
||||
func GooseCustomProviderFile(p *canonical.Profile) ([]byte, string) {
|
||||
|
||||
@@ -9,32 +9,6 @@ import (
|
||||
"github.com/lukaszraczylo/harness-sync/internal/canonical"
|
||||
)
|
||||
|
||||
func TestBuildProvidersGatewayFirst(t *testing.T) {
|
||||
p := &canonical.Profile{
|
||||
Gateway: canonical.Gateway{URL: "https://gw", Token: "t", DefaultModel: "x"},
|
||||
Models: []canonical.Model{{ID: "m1", Alias: "a1"}},
|
||||
Upstreams: []canonical.Upstream{
|
||||
{Name: "anthropic", APIKey: "k", BaseURL: "https://api.anthropic.com"},
|
||||
},
|
||||
}
|
||||
out := BuildProviders(p)
|
||||
require.Len(t, out, 2)
|
||||
assert.Equal(t, "hs-gw", out[0]["id"])
|
||||
assert.Equal(t, "anthropic", out[1]["id"])
|
||||
assert.NotNil(t, out[0]["models"])
|
||||
}
|
||||
|
||||
func TestBuildProvidersNoGateway(t *testing.T) {
|
||||
p := &canonical.Profile{
|
||||
Upstreams: []canonical.Upstream{
|
||||
{Name: "anthropic", APIKey: "k"},
|
||||
},
|
||||
}
|
||||
out := BuildProviders(p)
|
||||
require.Len(t, out, 1)
|
||||
assert.Equal(t, "anthropic", out[0]["id"])
|
||||
}
|
||||
|
||||
func TestBuildMCPMap(t *testing.T) {
|
||||
reg := &canonical.MCPRegistry{Servers: []canonical.MCPServer{
|
||||
{Name: "filepuff", Command: "/bin/x", Transport: "stdio"},
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package common
|
||||
|
||||
import "os"
|
||||
|
||||
// DefaultHome returns the current user's home directory, or "" on error.
|
||||
func DefaultHome() string {
|
||||
h, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return h
|
||||
}
|
||||
@@ -31,32 +31,7 @@ func BuildMCPMapStyled(reg *canonical.MCPRegistry, style MCPStyle) map[string]an
|
||||
func mcpEntry(s canonical.MCPServer, style MCPStyle) map[string]any {
|
||||
e := map[string]any{}
|
||||
switch style {
|
||||
case MCPClaudeStyle:
|
||||
// Claude Code's actual entry shape uses "type" (not "transport").
|
||||
// Default to stdio when no URL is set.
|
||||
t := s.Transport
|
||||
if s.URL != "" {
|
||||
if t == "" {
|
||||
t = "http"
|
||||
}
|
||||
e["type"] = t
|
||||
e["url"] = s.URL
|
||||
} else {
|
||||
if t == "" {
|
||||
t = "stdio"
|
||||
}
|
||||
e["type"] = t
|
||||
if s.Command != "" {
|
||||
e["command"] = s.Command
|
||||
}
|
||||
if len(s.Args) > 0 {
|
||||
e["args"] = s.Args
|
||||
}
|
||||
if len(s.Env) > 0 {
|
||||
e["env"] = s.Env
|
||||
}
|
||||
}
|
||||
case MCPCrushStyle:
|
||||
case MCPClaudeStyle, MCPCrushStyle:
|
||||
if s.URL != "" {
|
||||
t := s.Transport
|
||||
if t == "" {
|
||||
@@ -65,7 +40,12 @@ func mcpEntry(s canonical.MCPServer, style MCPStyle) map[string]any {
|
||||
e["type"] = t
|
||||
e["url"] = s.URL
|
||||
} else {
|
||||
e["type"] = "stdio"
|
||||
// Claude honours Transport field; Crush always uses "stdio".
|
||||
t := "stdio"
|
||||
if style == MCPClaudeStyle && s.Transport != "" {
|
||||
t = s.Transport
|
||||
}
|
||||
e["type"] = t
|
||||
if s.Command != "" {
|
||||
e["command"] = s.Command
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user