mirror of
https://github.com/lukaszraczylo/harness-sync.git
synced 2026-10-17 01:17:35 +00:00
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
This commit is contained in:
@@ -3,15 +3,36 @@ package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/lukaszraczylo/harness-sync/internal/canonical"
|
||||
)
|
||||
|
||||
const gatewayProviderID = "harness-sync-gateway"
|
||||
const fallbackGatewayProviderID = "harness-sync-gateway"
|
||||
|
||||
// GatewayProviderID is the canonical provider key used across all adapters.
|
||||
const GatewayProviderID = gatewayProviderID
|
||||
// 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.
|
||||
//
|
||||
// https://llmgw.h.raczylo.com/v1 → hs-llmgw-h-raczylo-com
|
||||
func GatewayProviderKey(gatewayURL string) string {
|
||||
if gatewayURL == "" {
|
||||
return fallbackGatewayProviderID
|
||||
}
|
||||
u, err := url.Parse(gatewayURL)
|
||||
if err != nil || u.Hostname() == "" {
|
||||
return fallbackGatewayProviderID
|
||||
}
|
||||
host := u.Hostname()
|
||||
// Replace dots, underscores, and colons (port separators) with dashes.
|
||||
r := strings.NewReplacer(".", "-", "_", "-", ":", "-")
|
||||
return "hs-" + r.Replace(host)
|
||||
}
|
||||
|
||||
// ProviderEntry is a generic provider record. Adapters may massage further.
|
||||
type ProviderEntry = map[string]any
|
||||
@@ -22,8 +43,9 @@ type ProviderEntry = map[string]any
|
||||
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": gatewayProviderID,
|
||||
"id": key,
|
||||
"name": "harness-sync gateway",
|
||||
"base_url": p.Gateway.URL,
|
||||
"api_key": p.Gateway.Token,
|
||||
@@ -89,7 +111,7 @@ func ProvidersAsCrushMap(p *canonical.Profile) map[string]any {
|
||||
if len(models) == 0 && p.Gateway.DefaultModel != "" {
|
||||
models = append(models, crushModel(p.Gateway.DefaultModel))
|
||||
}
|
||||
out[gatewayProviderID] = map[string]any{
|
||||
out[GatewayProviderKey(p.Gateway.URL)] = map[string]any{
|
||||
"type": "openai-compat",
|
||||
"base_url": p.Gateway.URL,
|
||||
"api_key": p.Gateway.Token,
|
||||
@@ -106,7 +128,7 @@ func CrushRoleModels(p *canonical.Profile) map[string]any {
|
||||
}
|
||||
sel := map[string]any{
|
||||
"model": p.Gateway.DefaultModel,
|
||||
"provider": gatewayProviderID,
|
||||
"provider": GatewayProviderKey(p.Gateway.URL),
|
||||
}
|
||||
return map[string]any{
|
||||
"large": sel,
|
||||
@@ -143,7 +165,7 @@ func ProvidersAsMap(p *canonical.Profile) map[string]any {
|
||||
if len(models) > 0 {
|
||||
entry["models"] = models
|
||||
}
|
||||
out[gatewayProviderID] = entry
|
||||
out[GatewayProviderKey(p.Gateway.URL)] = entry
|
||||
}
|
||||
for _, up := range p.Upstreams {
|
||||
entry := map[string]any{
|
||||
@@ -169,7 +191,7 @@ func KiloModelString(p *canonical.Profile) string {
|
||||
if p.Gateway.DefaultModel == "" {
|
||||
return ""
|
||||
}
|
||||
return gatewayProviderID + "/" + p.Gateway.DefaultModel
|
||||
return GatewayProviderKey(p.Gateway.URL) + "/" + p.Gateway.DefaultModel
|
||||
}
|
||||
|
||||
// ProvidersAsCagentMap returns the providers block for cagent:
|
||||
@@ -179,7 +201,7 @@ func ProvidersAsCagentMap(p *canonical.Profile) map[string]any {
|
||||
if p.Gateway.URL == "" {
|
||||
return out
|
||||
}
|
||||
out[gatewayProviderID] = map[string]any{
|
||||
out[GatewayProviderKey(p.Gateway.URL)] = map[string]any{
|
||||
"base_url": p.Gateway.URL,
|
||||
"api_key": p.Gateway.Token,
|
||||
"provider": "openai",
|
||||
@@ -227,7 +249,7 @@ func GooseCustomProviderFile(p *canonical.Profile) ([]byte, string) {
|
||||
if p.Gateway.URL == "" {
|
||||
return nil, ""
|
||||
}
|
||||
providerID := gatewayProviderID
|
||||
providerID := GatewayProviderKey(p.Gateway.URL)
|
||||
providerName := "custom_" + providerID
|
||||
|
||||
models := make([]map[string]any, 0)
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestBuildProvidersGatewayFirst(t *testing.T) {
|
||||
}
|
||||
out := BuildProviders(p)
|
||||
require.Len(t, out, 2)
|
||||
assert.Equal(t, "harness-sync-gateway", out[0]["id"])
|
||||
assert.Equal(t, "hs-gw", out[0]["id"])
|
||||
assert.Equal(t, "anthropic", out[1]["id"])
|
||||
assert.NotNil(t, out[0]["models"])
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package common
|
||||
|
||||
import "maps"
|
||||
|
||||
// AbsorbDuplicateProviders scans provMap for entries (other than ourKey) whose
|
||||
// options.baseURL matches gatewayURL and absorbs their models into our entry.
|
||||
//
|
||||
// Merge strategy:
|
||||
// - Models only in the duplicate: added to our provider.
|
||||
// - Models only in our canonical entry: kept as-is.
|
||||
// - Models in both: the DUPLICATE's entry wins (preserves user-crafted display
|
||||
// names and custom limits that are often more descriptive than the profile
|
||||
// alias).
|
||||
//
|
||||
// Absorbed entries are deleted from the returned map. Returns provMap unchanged
|
||||
// when gatewayURL is empty or our entry is absent.
|
||||
func AbsorbDuplicateProviders(provMap map[string]any, ourKey, gatewayURL string) map[string]any {
|
||||
if gatewayURL == "" || provMap == nil {
|
||||
return provMap
|
||||
}
|
||||
ourEntry, ok := provMap[ourKey].(map[string]any)
|
||||
if !ok {
|
||||
return provMap
|
||||
}
|
||||
|
||||
// Clone top-level so we don't mutate the caller's map.
|
||||
result := make(map[string]any, len(provMap))
|
||||
maps.Copy(result, provMap)
|
||||
|
||||
ourModels, _ := ourEntry["models"].(map[string]any)
|
||||
if ourModels == nil {
|
||||
ourModels = map[string]any{}
|
||||
}
|
||||
|
||||
// merged starts with our canonical models; duplicates may override per-model.
|
||||
merged := make(map[string]any, len(ourModels))
|
||||
maps.Copy(merged, ourModels)
|
||||
|
||||
for k, v := range result {
|
||||
if k == ourKey {
|
||||
continue
|
||||
}
|
||||
entry, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
opts, _ := entry["options"].(map[string]any)
|
||||
if opts == nil {
|
||||
continue
|
||||
}
|
||||
if opts["baseURL"] != gatewayURL {
|
||||
continue
|
||||
}
|
||||
// Same gateway URL — absorb and delete.
|
||||
if dupModels, ok := entry["models"].(map[string]any); ok {
|
||||
// Duplicate (user-crafted) wins for overlapping IDs.
|
||||
maps.Copy(merged, dupModels)
|
||||
}
|
||||
delete(result, k)
|
||||
}
|
||||
|
||||
updated := make(map[string]any, len(ourEntry))
|
||||
maps.Copy(updated, ourEntry)
|
||||
if len(merged) > 0 {
|
||||
updated["models"] = merged
|
||||
}
|
||||
result[ourKey] = updated
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user