feat(kilo): adapter with kilo.json render + agent symlink

This commit is contained in:
2026-05-23 02:59:50 +01:00
parent fe9206716e
commit 7013bada1c
9 changed files with 779 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
package kilo
import (
"encoding/json"
"os"
"path/filepath"
"github.com/lukaszraczylo/harness-sync/internal/adapter"
"github.com/lukaszraczylo/harness-sync/internal/canonical"
)
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
}
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
}
+143
View File
@@ -0,0 +1,143 @@
// Package kilo is the kilo harness adapter.
package kilo
import (
"os"
"path/filepath"
"github.com/lukaszraczylo/harness-sync/internal/adapter"
"github.com/lukaszraczylo/harness-sync/internal/canonical"
"github.com/lukaszraczylo/harness-sync/internal/render"
)
const name = "kilo"
// Adapter implements adapter.Adapter for the kilo harness.
type Adapter struct {
home string
}
// Option configures an Adapter.
type Option func(*Adapter)
// WithHome overrides the home directory (defaults to os.UserHomeDir).
func WithHome(h string) Option { return func(a *Adapter) { a.home = h } }
// New returns a new Adapter with the given options applied.
func New(opts ...Option) *Adapter {
a := &Adapter{home: defaultHome()}
for _, o := range opts {
o(a)
}
return a
}
func defaultHome() string {
h, err := os.UserHomeDir()
if err != nil {
return ""
}
return h
}
// Name returns the harness identifier.
func (a *Adapter) Name() string { return name }
// Detect returns true when ~/.config/kilo exists.
func (a *Adapter) Detect() bool {
_, err := os.Stat(filepath.Join(a.home, ".config", "kilo"))
return err == nil
}
// Render produces the FileSet that applies the canonical bundle to kilo.
func (a *Adapter) Render(b *canonical.Bundle) (*adapter.FileSet, error) {
fs := adapter.NewFileSet()
base := filepath.Join(a.home, ".config", "kilo")
fs.Add(adapter.File{
Dest: filepath.Join(base, "agent"),
Kind: adapter.SymlinkDir,
SymlinkTarget: filepath.Join(b.Root, "agents"),
})
cfg, err := renderConfig(b)
if err != nil {
return nil, err
}
fs.Add(adapter.File{
Dest: filepath.Join(base, "kilo.json"),
Kind: adapter.RenderedFile,
Content: cfg,
})
return fs, nil
}
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,
}
if len(mcp) > 0 {
out["mcpServers"] = mcp
}
return render.JSON(out)
}
// Import reads kilo config from home and returns a canonical ImportResult.
func (a *Adapter) Import(home string) (*adapter.ImportResult, error) {
return importFrom(home)
}
+55
View File
@@ -0,0 +1,55 @@
package kilo
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/lukaszraczylo/harness-sync/internal/adapter"
"github.com/lukaszraczylo/harness-sync/internal/canonical"
)
func TestKiloRender(t *testing.T) {
home := t.TempDir()
ad := New(WithHome(home))
b := &canonical.Bundle{
Root: "/canon",
Profile: canonical.Profile{
Name: "home",
Gateway: canonical.Gateway{URL: "https://gw", Token: "dummy", DefaultModel: "sonnet"},
Models: []canonical.Model{{ID: "claude-sonnet-4-6", Alias: "sonnet"}},
},
}
fs, err := ad.Render(b)
require.NoError(t, err)
seen := map[string]adapter.File{}
fs.ForEach(func(f adapter.File) { seen[f.Dest] = f })
agentDest := filepath.Join(home, ".config", "kilo", "agent")
assert.Equal(t, adapter.SymlinkDir, seen[agentDest].Kind)
assert.Equal(t, "/canon/agents", seen[agentDest].SymlinkTarget)
cfgDest := filepath.Join(home, ".config", "kilo", "kilo.json")
assert.Equal(t, adapter.RenderedFile, seen[cfgDest].Kind)
var parsed map[string]any
require.NoError(t, json.Unmarshal(seen[cfgDest].Content, &parsed))
}
func TestKiloImport(t *testing.T) {
home := t.TempDir()
base := filepath.Join(home, ".config", "kilo")
require.NoError(t, os.MkdirAll(base, 0o750))
body, _ := json.Marshal(map[string]any{"providers": []map[string]any{{"id": "x"}}})
require.NoError(t, os.WriteFile(filepath.Join(base, "kilo.json"), body, 0o600))
ad := New(WithHome(home))
res, err := ad.Import(home)
require.NoError(t, err)
assert.NotNil(t, res)
}