refactor(adapters): extract common helpers + fix kilo native schema

This commit is contained in:
2026-05-23 03:06:35 +01:00
parent 6dafb43c43
commit a18cb1a4b7
13 changed files with 377 additions and 442 deletions
+79
View File
@@ -0,0 +1,79 @@
// Package common provides shared helpers for harness adapters.
package common
import "github.com/lukaszraczylo/harness-sync/internal/canonical"
// 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 != "" {
gw := ProviderEntry{
"id": "harness-sync-gateway",
"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
}
// BuildMCPMap returns canonical MCP servers as a name -> object map.
// Empty map (not nil) is returned when there are no servers so the caller
// can decide whether to include the key.
func BuildMCPMap(reg *canonical.MCPRegistry) map[string]any {
out := map[string]any{}
if reg == nil {
return out
}
for _, s := range reg.Servers {
e := map[string]any{}
if s.Command != "" {
e["command"] = s.Command
}
if len(s.Args) > 0 {
e["args"] = s.Args
}
if s.URL != "" {
e["url"] = s.URL
}
if s.Transport != "" {
e["transport"] = s.Transport
}
if len(s.Env) > 0 {
e["env"] = s.Env
}
out[s.Name] = e
}
return out
}
+92
View File
@@ -0,0 +1,92 @@
package common
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"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, "harness-sync-gateway", 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"},
}}
out := BuildMCPMap(reg)
assert.Contains(t, out, "filepuff")
entry := out["filepuff"].(map[string]any)
assert.Equal(t, "/bin/x", entry["command"])
}
func TestBuildMCPMapNil(t *testing.T) {
out := BuildMCPMap(nil)
assert.NotNil(t, out)
assert.Len(t, out, 0)
}
func TestStripJSONComments(t *testing.T) {
s := "// hi\n{\"a\":1, /* x */ \"b\":2}"
assert.Equal(t, `{"a":1, "b":2}`, StripJSONComments(s))
}
func TestParseFrontmatter(t *testing.T) {
body := []byte("---\nname: foo\ndescription: bar\n---\nbody")
name, desc := ParseFrontmatter(body)
assert.Equal(t, "foo", name)
assert.Equal(t, "bar", desc)
}
func TestParseFrontmatterMissing(t *testing.T) {
name, desc := ParseFrontmatter([]byte("no frontmatter"))
assert.Empty(t, name)
assert.Empty(t, desc)
}
func TestImportMarkdownTreeMissingDir(t *testing.T) {
docs, err := ImportMarkdownTree("/nonexistent/path", "")
require.NoError(t, err)
assert.Empty(t, docs)
}
func TestParseMCPFromJSON(t *testing.T) {
body := []byte(`{"mcpServers": {"filepuff": {"command": "/bin/fp", "args": ["--flag"]}}}`)
servers, err := ParseMCPFromJSON(body, "mcpServers")
require.NoError(t, err)
require.Len(t, servers, 1)
assert.Equal(t, "filepuff", servers[0].Name)
assert.Equal(t, "/bin/fp", servers[0].Command)
}
func TestParseMCPFromJSONMissingKey(t *testing.T) {
body := []byte(`{"other": {}}`)
servers, err := ParseMCPFromJSON(body, "mcpServers")
require.NoError(t, err)
assert.Nil(t, servers)
}
+88
View File
@@ -0,0 +1,88 @@
package common
import (
"encoding/json"
"os"
"regexp"
"strings"
"github.com/lukaszraczylo/harness-sync/internal/canonical"
)
// ReadIfExists returns the file body or empty string when missing.
func ReadIfExists(path string) (string, error) {
b, err := os.ReadFile(path)
if os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", err
}
return string(b), nil
}
// DirExists reports whether path is an existing directory.
func DirExists(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}
var (
blockComment = regexp.MustCompile(`(?s)/\*.*?\*/`)
lineComment = regexp.MustCompile(`(?m)//[^\n]*`)
)
// StripJSONComments is a naive JSONC -> JSON stripper. It does not understand
// strings containing // or /* and is therefore only safe for hand-edited
// config files that don't put comment markers inside string literals.
func StripJSONComments(s string) string {
s = blockComment.ReplaceAllString(s, "")
s = lineComment.ReplaceAllString(s, "")
return strings.TrimSpace(s)
}
// ImportMCPFromJSONFile reads a JSON file and extracts an MCP server map
// nested under the given top-level key (e.g. "mcpServers", "mcp").
// Returns nil with no error when the file is missing.
func ImportMCPFromJSONFile(path, key string) ([]canonical.MCPServer, error) {
body, err := os.ReadFile(path)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
return ParseMCPFromJSON(body, key)
}
// ParseMCPFromJSON extracts MCP servers from a JSON body. The body may be
// raw JSON or JSONC (comments are stripped first).
func ParseMCPFromJSON(body []byte, key string) ([]canonical.MCPServer, error) {
clean := StripJSONComments(string(body))
var raw map[string]json.RawMessage
if err := json.Unmarshal([]byte(clean), &raw); err != nil {
return nil, nil // tolerate malformed config
}
nested, ok := raw[key]
if !ok {
return nil, nil
}
var entries map[string]struct { //nolint:govet // fieldalignment: map+slice+3 strings irreducible
Env map[string]string `json:"env"`
Args []string `json:"args"`
Command string `json:"command"`
URL string `json:"url"`
Transport string `json:"transport"`
}
if err := json.Unmarshal(nested, &entries); err != nil {
return nil, nil
}
out := make([]canonical.MCPServer, 0, len(entries))
for n, v := range entries {
out = append(out, canonical.MCPServer{
Name: n, Command: v.Command, Args: v.Args, URL: v.URL,
Transport: v.Transport, Env: v.Env,
})
}
return out, nil
}
+77
View File
@@ -0,0 +1,77 @@
package common
import (
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
// Doc mirrors canonical.Skill / canonical.Agent without importing them.
type Doc struct {
Name string
Description string
Body string
Path string
}
// ParseFrontmatter extracts the name and description fields from a markdown
// file's YAML frontmatter block. Returns ("", "") when there is no frontmatter.
func ParseFrontmatter(b []byte) (name, description string) {
s := string(b)
if !strings.HasPrefix(s, "---\n") {
return "", ""
}
end := strings.Index(s[4:], "\n---")
if end == -1 {
return "", ""
}
var meta struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
}
_ = yaml.Unmarshal([]byte(s[4:4+end]), &meta)
return meta.Name, meta.Description
}
// ImportMarkdownTree walks dir and returns one Doc per markdown file. When
// requiredFilename is non-empty (e.g. "SKILL.md"), only files with that exact
// basename are included; otherwise any *.md.
func ImportMarkdownTree(dir, requiredFilename string) ([]Doc, error) {
var docs []Doc
if !DirExists(dir) {
return docs, nil
}
err := filepath.Walk(dir, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if requiredFilename != "" && filepath.Base(p) != requiredFilename {
return nil
}
if requiredFilename == "" && !strings.HasSuffix(p, ".md") {
return nil
}
body, err := os.ReadFile(p)
if err != nil {
return err
}
name, desc := ParseFrontmatter(body)
if name == "" {
name = strings.TrimSuffix(filepath.Base(p), ".md")
}
rel, _ := filepath.Rel(dir, p)
docs = append(docs, Doc{
Name: name,
Description: desc,
Body: string(body),
Path: rel,
})
return nil
})
return docs, err
}