yao/agent/sandbox/v2/opencode/platform.go
Max 04c3114344 feat(opencode): enhance platform-specific command handling and environment variable syntax
- Introduced `shellQuoteForPlatform` to build shell-safe command strings tailored for Windows and POSIX environments.
- Updated `buildSandboxEnvPrompt` to display the correct environment variable syntax based on the operating system.
- Refactored the command copying logic in the runner to use platform-aware commands for copying custom OpenCode tools.
- Removed outdated test scenarios related to the vision connector, streamlining the test suite.
2026-04-25 11:09:49 +08:00

66 lines
1.6 KiB
Go

package opencode
import (
"fmt"
"path"
"strings"
infra "github.com/yaoapp/yao/sandbox/v2"
)
// platform encapsulates OS-dependent behaviors for the target environment.
type platform interface {
OS() string
Shell() string
HomeEnv(workDir string) map[string]string
PathJoin(parts ...string) string
ShellCmd(script string) []string
KillCmd(pattern string) []string
KillSessionCmd(sessionName string) []string
}
type posixBase struct {
os string
workDir string
shell string
}
func (b *posixBase) OS() string { return b.os }
func (b *posixBase) Shell() string { return b.shell }
func (b *posixBase) PathJoin(parts ...string) string { return path.Join(parts...) }
func (b *posixBase) HomeEnv(workDir string) map[string]string {
return map[string]string{"HOME": workDir}
}
func (b *posixBase) ShellCmd(script string) []string {
return []string{"bash", "-c", script}
}
func (b *posixBase) KillCmd(pattern string) []string {
return []string{"sh", "-c", fmt.Sprintf("pkill -f '%s' || true", pattern)}
}
func (b *posixBase) KillSessionCmd(sessionName string) []string {
return []string{"sh", "-c", fmt.Sprintf("pkill -9 -f '%s' || true", sessionName)}
}
func resolvePlatform(computer infra.Computer) platform {
sys := computer.ComputerInfo().System
osName := strings.ToLower(sys.OS)
workDir := computer.GetWorkDir()
shell := sys.Shell
if osName == "windows" {
return newWindowsPlatform(workDir, shell)
}
base := posixBase{os: osName, workDir: workDir, shell: shell}
if base.shell == "" {
base.shell = "bash"
}
if base.os == "" {
base.os = "linux"
}
return &base
}