75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package codex
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/codexhost"
|
|
)
|
|
|
|
var hostConfig struct {
|
|
sync.Mutex
|
|
dir, binary string
|
|
}
|
|
|
|
// ConfigureHosts enables a private detachable App Server connection bridge for
|
|
// new Chats. Empty dir or PIPELINES_CODEX_HOST=off keeps direct mode for tests
|
|
// and installations that intentionally disable this facility.
|
|
func ConfigureHosts(dir string) {
|
|
hostConfig.Lock()
|
|
defer hostConfig.Unlock()
|
|
hostConfig.dir, hostConfig.binary = "", ""
|
|
if strings.TrimSpace(dir) == "" {
|
|
return
|
|
}
|
|
binary := strings.TrimSpace(os.Getenv("PIPELINES_CODEX_HOST"))
|
|
if binary == "off" {
|
|
return
|
|
}
|
|
if binary == "" {
|
|
if executable, err := os.Executable(); err == nil {
|
|
candidate := filepath.Join(filepath.Dir(executable), "pipelines-codex-host")
|
|
if info, err := os.Stat(candidate); err == nil && !info.IsDir() {
|
|
binary = candidate
|
|
}
|
|
}
|
|
}
|
|
if binary == "" {
|
|
binary, _ = exec.LookPath("pipelines-codex-host")
|
|
}
|
|
if binary == "" {
|
|
log.Print("codex chat: pipelines-codex-host was not found; pending approvals and questions cannot survive a server restart")
|
|
return
|
|
}
|
|
hostConfig.dir, hostConfig.binary = dir, binary
|
|
}
|
|
|
|
// DefaultHostDir scopes bridge ownership to the server's database identity.
|
|
func DefaultHostDir(scope string) string {
|
|
base, err := os.UserCacheDir()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
if absolute, err := filepath.Abs(scope); err == nil {
|
|
scope = absolute
|
|
}
|
|
sum := sha256.Sum256([]byte(scope))
|
|
return filepath.Join(base, "pipelines", "codex-hosts", hex.EncodeToString(sum[:6]))
|
|
}
|
|
func connectionSocket(ctx context.Context, upstream string) (string, error) {
|
|
hostConfig.Lock()
|
|
dir, binary := hostConfig.dir, hostConfig.binary
|
|
hostConfig.Unlock()
|
|
if dir == "" {
|
|
return upstream, nil
|
|
}
|
|
return codexhost.Ensure(ctx, binary, dir, upstream)
|
|
}
|