Add runtime abstraction and agent loop
Co-authored-by: codegirl007 <codegirl-007@users.noreply.github.com>
This commit is contained in:
co-authored by
codegirl007
parent
54dc5658c6
commit
94146fcae2
@@ -0,0 +1,64 @@
|
||||
package appspec
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"agentbox/internal/environment"
|
||||
)
|
||||
|
||||
type manifest struct {
|
||||
Command string `json:"command"`
|
||||
Args []string `json:"args"`
|
||||
Env map[string]string `json:"env"`
|
||||
WindowTitle string `json:"window_title"`
|
||||
}
|
||||
|
||||
func Resolve(path string) (environment.Command, error) {
|
||||
absolute, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return environment.Command{}, fmt.Errorf("resolve application path: %w", err)
|
||||
}
|
||||
info, err := os.Stat(absolute)
|
||||
if err != nil {
|
||||
return environment.Command{}, fmt.Errorf("inspect application path: %w", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
if !info.Mode().IsRegular() {
|
||||
return environment.Command{}, errors.New("application must be a regular executable file")
|
||||
}
|
||||
return environment.Command{Path: absolute}, nil
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(filepath.Join(absolute, "agentbox.json"))
|
||||
if err != nil {
|
||||
return environment.Command{}, fmt.Errorf("read directory manifest agentbox.json: %w", err)
|
||||
}
|
||||
var config manifest
|
||||
if err := json.Unmarshal(data, &config); err != nil {
|
||||
return environment.Command{}, fmt.Errorf("parse agentbox.json: %w", err)
|
||||
}
|
||||
if config.Command == "" {
|
||||
return environment.Command{}, errors.New("agentbox.json requires command")
|
||||
}
|
||||
commandPath := config.Command
|
||||
if !filepath.IsAbs(commandPath) {
|
||||
commandPath = filepath.Join(absolute, commandPath)
|
||||
}
|
||||
commandInfo, err := os.Stat(commandPath)
|
||||
if err != nil {
|
||||
return environment.Command{}, fmt.Errorf("inspect manifest command: %w", err)
|
||||
}
|
||||
if !commandInfo.Mode().IsRegular() {
|
||||
return environment.Command{}, errors.New("manifest command must be a regular executable file")
|
||||
}
|
||||
return environment.Command{
|
||||
Path: commandPath,
|
||||
Args: config.Args,
|
||||
Env: config.Env,
|
||||
WindowTitle: config.WindowTitle,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package appspec
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResolveDirectoryManifest(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
executable := filepath.Join(directory, "game")
|
||||
if err := os.WriteFile(executable, []byte("binary"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifest := `{"command":"game","args":["--demo"],"window_title":"Demo"}`
|
||||
if err := os.WriteFile(filepath.Join(directory, "agentbox.json"), []byte(manifest), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
command, err := Resolve(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if command.Path != executable || command.WindowTitle != "Demo" ||
|
||||
len(command.Args) != 1 || command.Args[0] != "--demo" {
|
||||
t.Fatalf("command = %#v", command)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user