The Codex app's work mode on a project whose run environment is WSL starts
its app-server inside the distro, on the distro's ~/.codex (the app resolves
CODEX_HOME there with `bash -lc 'printf %s "${CODEX_HOME:-$HOME/.codex}"'`).
magpie wired Codex on Windows and, as a separate Agents row, the distro's
twin (codex@wsl:<distro>) only when the user connected that too, so the
picker listed OpenAI's models alone there.
ConnectCodexTwins, run at the start of each SyncCatalog, connects the twin
of each running distro once while Windows' Codex is connected. It leaves a
twin alone that is connected or disconnected already, whose config names a
provider or base URL of the user's own, or whose distro is stopped (asking
one starts it). A stash mark (<id>.autoconnected) keeps it to once, so a
later Disconnect stands. docs/subsystems/agent-wiring.md says so.
Run: TestConnectCodexTwins and TestConnectCodexTwinsKeepsOwnProvider; go
test ./internal/agent on mote (ok, 305s); TestWSL* and the new tests on the
Windows box (ok); gofmt; go vet darwin, linux and windows (nogui).
Broken by hand: Windows-connected check removed (red), own-provider check
removed (red). The Disconnected check is covered by the mark alone.
Not tried: the Codex app itself with a WSL project (read its bundle for the
CODEX_HOME resolution only); the reporter's distro and networking.
80 lines
2.4 KiB
Go
80 lines
2.4 KiB
Go
package agent
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
// codexTwinAutoKey, after a twin's id, marks in the stash that magpie
|
|
// connected it once from Windows' Codex being connected (or found it
|
|
// connected or disconnected already): it is never done again, so a Disconnect
|
|
// or a change the user makes in the distro stands.
|
|
const codexTwinAutoKey = ".autoconnected"
|
|
|
|
// ConnectCodexTwins connects Codex in each running WSL distro when Codex on
|
|
// Windows is connected. The Codex app's work mode on a project whose run
|
|
// environment is WSL starts its app-server inside the distro, on the
|
|
// distro's ~/.codex, not Windows' (the app resolves CODEX_HOME there with
|
|
// `bash -lc 'printf %s "${CODEX_HOME:-$HOME/.codex}"'`): a Codex connected
|
|
// only on Windows listed OpenAI's models alone there (@not_4O4 on X).
|
|
//
|
|
// It runs once per distro, and not at all for one the user disconnected, one
|
|
// not running (asking a stopped distro starts it), one whose config names a
|
|
// provider or base URL that isn't magpie's, or while Windows' Codex isn't
|
|
// connected. Off Windows there are no distros and nothing happens.
|
|
func ConnectCodexTwins() {
|
|
if !wslLooks() {
|
|
return
|
|
}
|
|
var win *Agent
|
|
for _, a := range All() {
|
|
if a.ID == "codex" {
|
|
win = a
|
|
break
|
|
}
|
|
}
|
|
if win == nil || !win.Wired() {
|
|
return
|
|
}
|
|
for _, d := range wslDistros() {
|
|
k := wslKindOf("codex")
|
|
if !d.Running || !k.found(d) {
|
|
continue
|
|
}
|
|
a := wslAgent(k, d)
|
|
mark := a.ID + codexTwinAutoKey
|
|
if stashLoad()[mark] == "1" {
|
|
continue
|
|
}
|
|
if a.Disconnected() || a.Wired() {
|
|
stash(map[string]string{mark: "1"})
|
|
continue
|
|
}
|
|
if codexTwinHasOwn(d.place(a.ID)) {
|
|
continue
|
|
}
|
|
if _, err := a.ConnectHow(); err != nil {
|
|
// no model to connect to yet, or a file that can't be written
|
|
// now: tried again at the next sync
|
|
continue
|
|
}
|
|
stash(map[string]string{mark: "1"})
|
|
}
|
|
}
|
|
|
|
// codexTwinHasOwn is whether the distro's Codex config names a provider or a
|
|
// base URL that isn't magpie's: the user's own, which magpie doesn't take
|
|
// over without being asked.
|
|
func codexTwinHasOwn(at place) bool {
|
|
path := filepath.Join(at.codexHome(), "config.toml")
|
|
if p, _ := edit.GetTOMLTop(path, "model_provider"); p != "" && p != magpieID {
|
|
return true
|
|
}
|
|
if u, _ := edit.GetTOMLTop(path, "openai_base_url"); u != "" && !strings.Contains(u, "/backend-api/codex") {
|
|
return true
|
|
}
|
|
return false
|
|
}
|