mirror of
https://github.com/lukaszraczylo/harness-sync.git
synced 2026-10-16 01:07:50 +00:00
refactor(adapters): extract common helpers + fix kilo native schema
This commit is contained in:
@@ -1,46 +1,17 @@
|
||||
package kilo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/lukaszraczylo/harness-sync/internal/adapter"
|
||||
"github.com/lukaszraczylo/harness-sync/internal/canonical"
|
||||
"github.com/lukaszraczylo/harness-sync/internal/adapter/common"
|
||||
)
|
||||
|
||||
func importFrom(home string) (*adapter.ImportResult, error) {
|
||||
cfgPath := filepath.Join(home, ".config", "kilo", "kilo.json")
|
||||
body, err := os.ReadFile(cfgPath)
|
||||
if os.IsNotExist(err) {
|
||||
return &adapter.ImportResult{}, nil
|
||||
}
|
||||
servers, err := common.ImportMCPFromJSONFile(cfgPath, "mcp")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
type mcpEntry struct { //nolint:govet // fieldalignment not achievable: map+slice+3 strings = 80 bytes regardless of order
|
||||
Env map[string]string `json:"env"`
|
||||
Args []string `json:"args"`
|
||||
Command string `json:"command"`
|
||||
URL string `json:"url"`
|
||||
Transport string `json:"transport"`
|
||||
}
|
||||
var doc struct {
|
||||
MCPServers map[string]mcpEntry `json:"mcpServers"`
|
||||
}
|
||||
_ = json.Unmarshal(body, &doc)
|
||||
|
||||
res := &adapter.ImportResult{}
|
||||
for nm, v := range doc.MCPServers {
|
||||
res.MCP = append(res.MCP, canonical.MCPServer{
|
||||
Name: nm,
|
||||
Command: v.Command,
|
||||
Args: v.Args,
|
||||
URL: v.URL,
|
||||
Transport: v.Transport,
|
||||
Env: v.Env,
|
||||
})
|
||||
}
|
||||
return res, nil
|
||||
return &adapter.ImportResult{MCP: servers}, nil
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/lukaszraczylo/harness-sync/internal/adapter"
|
||||
"github.com/lukaszraczylo/harness-sync/internal/adapter/common"
|
||||
"github.com/lukaszraczylo/harness-sync/internal/canonical"
|
||||
"github.com/lukaszraczylo/harness-sync/internal/render"
|
||||
)
|
||||
@@ -74,65 +75,12 @@ func (a *Adapter) Render(b *canonical.Bundle) (*adapter.FileSet, error) {
|
||||
}
|
||||
|
||||
func renderConfig(b *canonical.Bundle) ([]byte, error) {
|
||||
providers := make([]map[string]any, 0)
|
||||
if b.Profile.Gateway.URL != "" {
|
||||
gw := map[string]any{
|
||||
"id": "harness-sync-gateway",
|
||||
"name": "harness-sync gateway",
|
||||
"base_url": b.Profile.Gateway.URL,
|
||||
"api_key": b.Profile.Gateway.Token,
|
||||
}
|
||||
models := make([]map[string]any, 0, len(b.Profile.Models))
|
||||
for _, m := range b.Profile.Models {
|
||||
entry := map[string]any{"id": m.ID}
|
||||
if m.Alias != "" {
|
||||
entry["alias"] = m.Alias
|
||||
}
|
||||
models = append(models, entry)
|
||||
}
|
||||
if len(models) > 0 {
|
||||
gw["models"] = models
|
||||
}
|
||||
providers = append(providers, gw)
|
||||
}
|
||||
for _, up := range b.Profile.Upstreams {
|
||||
p := map[string]any{"id": up.Name, "name": up.Name}
|
||||
if up.BaseURL != "" {
|
||||
p["base_url"] = up.BaseURL
|
||||
}
|
||||
if up.APIKey != "" {
|
||||
p["api_key"] = up.APIKey
|
||||
}
|
||||
providers = append(providers, p)
|
||||
}
|
||||
|
||||
mcp := map[string]any{}
|
||||
for _, s := range b.MCP.Servers {
|
||||
entry := map[string]any{}
|
||||
if s.Command != "" {
|
||||
entry["command"] = s.Command
|
||||
}
|
||||
if len(s.Args) > 0 {
|
||||
entry["args"] = s.Args
|
||||
}
|
||||
if s.URL != "" {
|
||||
entry["url"] = s.URL
|
||||
}
|
||||
if s.Transport != "" {
|
||||
entry["transport"] = s.Transport
|
||||
}
|
||||
if len(s.Env) > 0 {
|
||||
entry["env"] = s.Env
|
||||
}
|
||||
mcp[s.Name] = entry
|
||||
}
|
||||
|
||||
out := map[string]any{
|
||||
"providers": providers,
|
||||
"default_model": b.Profile.Gateway.DefaultModel,
|
||||
"model": b.Profile.Gateway.DefaultModel,
|
||||
"providers": common.BuildProviders(&b.Profile),
|
||||
}
|
||||
if len(mcp) > 0 {
|
||||
out["mcpServers"] = mcp
|
||||
if mcp := common.BuildMCPMap(&b.MCP); len(mcp) > 0 {
|
||||
out["mcp"] = mcp
|
||||
}
|
||||
return render.JSON(out)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,9 @@ func TestKiloRender(t *testing.T) {
|
||||
Gateway: canonical.Gateway{URL: "https://gw", Token: "dummy", DefaultModel: "sonnet"},
|
||||
Models: []canonical.Model{{ID: "claude-sonnet-4-6", Alias: "sonnet"}},
|
||||
},
|
||||
MCP: canonical.MCPRegistry{Servers: []canonical.MCPServer{
|
||||
{Name: "filepuff", Command: "/bin/filepuff"},
|
||||
}},
|
||||
}
|
||||
fs, err := ad.Render(b)
|
||||
require.NoError(t, err)
|
||||
@@ -39,6 +42,10 @@ func TestKiloRender(t *testing.T) {
|
||||
|
||||
var parsed map[string]any
|
||||
require.NoError(t, json.Unmarshal(seen[cfgDest].Content, &parsed))
|
||||
assert.Equal(t, "sonnet", parsed["model"])
|
||||
assert.Contains(t, parsed, "mcp")
|
||||
assert.NotContains(t, parsed, "default_model")
|
||||
assert.NotContains(t, parsed, "mcpServers")
|
||||
}
|
||||
|
||||
func TestKiloImport(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user