Files
harness-sync/internal/adapters/opencode/opencode.go
T
lukaszraczylo fb02b165d7 feat: URL-derived provider keys (hs-<hostname>) + dedup providers sharing same gateway URL
- GatewayProviderKey() derives stable key from gateway URL hostname
  e.g. https://llmgw.h.raczylo.com/v1 → hs-llmgw-h-raczylo-com
- AbsorbDuplicateProviders() merges providers sharing the same baseURL
  into our canonical entry; user-crafted model metadata wins for overlaps
- kilo: renders to opencode.jsonc (primary config), deep-merges providers,
  deletes enabled_providers filter that was hiding our gateway
- opencode: absorbs duplicate providers from existing config
- All adapters use GatewayProviderKey() instead of hardcoded string
- Fix goose URL doubling (was /v1/v1/chat/completions → /chat/completions)
- Fix token propagation: api_key written directly (not env var ref)
- Zed: adds prompt_cache_key capability, deletes context_servers key
  to avoid ContextServerSettingsContent serde untagged-enum parse error
2026-05-23 21:13:16 +01:00

133 lines
3.7 KiB
Go

// Package opencode is the opencode harness adapter.
package opencode
import (
"encoding/json"
"os"
"path/filepath"
"github.com/lukaszraczylo/harness-sync/internal/adapter"
"github.com/lukaszraczylo/harness-sync/internal/adapter/common"
"github.com/lukaszraczylo/harness-sync/internal/canonical"
)
const name = "opencode"
// Adapter implements adapter.Adapter for the opencode 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 }
// Capabilities declares what opencode harness-sync manages.
func (a *Adapter) Capabilities() adapter.HarnessCapabilities {
return adapter.HarnessCapabilities{
ManagesProviders: true,
ManagesModels: true,
ManagesMCP: true,
ManagesSkills: true,
ManagesInstructions: true,
HasBuiltInSub: false,
}
}
// Detect returns true when ~/.config/opencode exists.
func (a *Adapter) Detect() bool {
_, err := os.Stat(filepath.Join(a.home, ".config", "opencode"))
return err == nil
}
// Render produces the FileSet that applies the canonical bundle to opencode.
// opencode.jsonc is MERGED to preserve user-managed keys ($schema, agent,
// instructions, …). Correct keys: "provider" (singular, map), "model" (string),
// "mcp" (map with type: local|remote entries).
func (a *Adapter) Render(b *canonical.Bundle) (*adapter.FileSet, error) {
fs := adapter.NewFileSet()
base := filepath.Join(a.home, ".config", "opencode")
// opencode reads skills from ~/.config/opencode/skills/<name>/SKILL.md
fs.Add(adapter.File{
Dest: filepath.Join(base, "skills"),
Kind: adapter.SymlinkDir,
SymlinkTarget: filepath.Join(b.Root, "skills"),
})
cfgPath := filepath.Join(base, "opencode.jsonc")
existing, _ := os.ReadFile(cfgPath)
// Build the provider map, then absorb any existing providers at the same
// gateway URL so duplicates don't accumulate across apply runs.
provMap := common.ProvidersAsMap(&b.Profile)
if len(existing) > 0 {
var base map[string]any
clean := common.StripJSONComments(string(existing))
if json.Unmarshal([]byte(clean), &base) == nil {
if existingProv, ok := base["provider"].(map[string]any); ok {
for k, v := range existingProv {
if _, ours := provMap[k]; !ours {
provMap[k] = v
}
}
provMap = common.AbsorbDuplicateProviders(provMap, common.GatewayProviderKey(b.Profile.Gateway.URL), b.Profile.Gateway.URL)
}
}
}
overlay := map[string]any{
"provider": provMap,
"model": common.KiloModelString(&b.Profile),
}
if mcp := common.BuildMCPMapStyled(&b.MCP, common.MCPOpencodeStyle); len(mcp) > 0 {
overlay["mcp"] = mcp
}
merged, err := common.MergeJSONKeys(existing, overlay)
if err != nil {
return nil, err
}
fs.Add(adapter.File{
Dest: cfgPath,
Kind: adapter.RenderedFile,
Content: merged,
})
instructions := b.Instructions.Global
if override, ok := b.Instructions.PerHarness[name]; ok && override != "" {
instructions = override
}
fs.Add(adapter.File{
Dest: filepath.Join(base, "AGENTS.md"),
Kind: adapter.RenderedFile,
Content: []byte(instructions),
})
return fs, nil
}
// Import reads opencode config from home and returns a canonical ImportResult.
func (a *Adapter) Import(home string) (*adapter.ImportResult, error) {
return importFrom(home)
}