feat: read codex agent definitions
This commit is contained in:
17
internal/agents/model.go
Normal file
17
internal/agents/model.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package agents
|
||||
|
||||
import "time"
|
||||
|
||||
type AgentDefinition struct {
|
||||
ID string `json:"id"`
|
||||
FilePath string `json:"filePath"`
|
||||
FileName string `json:"fileName"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
DeveloperInstructions string `json:"developerInstructions"`
|
||||
ExtraFields map[string]string `json:"extraFields"`
|
||||
ModifiedAt time.Time `json:"modifiedAt"`
|
||||
ParseStatus string `json:"parseStatus"`
|
||||
ParseError string `json:"parseError,omitempty"`
|
||||
DraftStatus string `json:"draftStatus"`
|
||||
}
|
||||
148
internal/agents/store.go
Normal file
148
internal/agents/store.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package agents
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"codex-agent-manager/internal/codexhome"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
CodexHome string
|
||||
}
|
||||
|
||||
func (s Store) List() ([]AgentDefinition, error) {
|
||||
agentsDir, err := codexhome.ResolveInside(s.CodexHome, "agents")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := os.ReadDir(agentsDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return []AgentDefinition{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sort.Slice(entries, func(i, j int) bool {
|
||||
return entries[i].Name() < entries[j].Name()
|
||||
})
|
||||
|
||||
result := make([]AgentDefinition, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || !strings.HasSuffix(strings.ToLower(entry.Name()), ".toml") {
|
||||
continue
|
||||
}
|
||||
result = append(result, s.readOne(entry.Name()))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Store) readOne(fileName string) AgentDefinition {
|
||||
path := filepath.Join(s.CodexHome, "agents", fileName)
|
||||
def := AgentDefinition{
|
||||
ID: strings.TrimSuffix(fileName, filepath.Ext(fileName)),
|
||||
FilePath: path,
|
||||
FileName: fileName,
|
||||
ParseStatus: "valid",
|
||||
DraftStatus: "clean",
|
||||
ExtraFields: map[string]string{},
|
||||
}
|
||||
|
||||
safePath, err := codexhome.ResolveAgentTOML(s.CodexHome, fileName)
|
||||
if err != nil {
|
||||
def.ParseStatus = "invalid"
|
||||
def.ParseError = err.Error()
|
||||
return def
|
||||
}
|
||||
info, statErr := os.Stat(safePath)
|
||||
if statErr == nil {
|
||||
def.ModifiedAt = info.ModTime()
|
||||
}
|
||||
data, err := os.ReadFile(safePath)
|
||||
if err != nil {
|
||||
def.ParseStatus = "invalid"
|
||||
def.ParseError = err.Error()
|
||||
return def
|
||||
}
|
||||
values, err := parseSimpleTOML(string(data))
|
||||
if err != nil {
|
||||
def.ParseStatus = "invalid"
|
||||
def.ParseError = err.Error()
|
||||
return def
|
||||
}
|
||||
|
||||
def.Name = values["name"]
|
||||
def.Description = values["description"]
|
||||
def.DeveloperInstructions = values["developer_instructions"]
|
||||
for key, value := range values {
|
||||
if key == "name" || key == "description" || key == "developer_instructions" {
|
||||
continue
|
||||
}
|
||||
def.ExtraFields[key] = value
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func parseSimpleTOML(input string) (map[string]string, error) {
|
||||
values := map[string]string{}
|
||||
scanner := bufio.NewScanner(strings.NewReader(input))
|
||||
lineNumber := 0
|
||||
for scanner.Scan() {
|
||||
lineNumber++
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
parts := strings.SplitN(line, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
return values, fmt.Errorf("第 %d 行不是有效的键值字段", lineNumber)
|
||||
}
|
||||
key := strings.TrimSpace(parts[0])
|
||||
raw := strings.TrimSpace(parts[1])
|
||||
if key == "" {
|
||||
return values, fmt.Errorf("第 %d 行缺少字段名", lineNumber)
|
||||
}
|
||||
|
||||
value, err := parseTOMLString(raw, scanner)
|
||||
if err != nil {
|
||||
return values, err
|
||||
}
|
||||
values[key] = value
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return values, err
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func parseTOMLString(raw string, scanner *bufio.Scanner) (string, error) {
|
||||
if strings.HasPrefix(raw, `"""`) {
|
||||
block := strings.TrimPrefix(raw, `"""`)
|
||||
for !strings.Contains(block, `"""`) && scanner.Scan() {
|
||||
block += "\n" + scanner.Text()
|
||||
}
|
||||
if !strings.Contains(block, `"""`) {
|
||||
return "", errors.New("未闭合的多行字符串")
|
||||
}
|
||||
value, trailing, _ := strings.Cut(block, `"""`)
|
||||
if strings.TrimSpace(trailing) != "" {
|
||||
return "", errors.New("多行字符串后存在不支持的内容")
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
if !strings.HasPrefix(raw, `"`) {
|
||||
return "", errors.New("仅支持字符串字段")
|
||||
}
|
||||
value, err := strconv.Unquote(raw)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
102
internal/agents/store_test.go
Normal file
102
internal/agents/store_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package agents
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListAgentsReadsTomlFiles(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
agentsDir := filepath.Join(root, "agents")
|
||||
if err := os.MkdirAll(agentsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
content := `name = "产品经理"
|
||||
description = "负责产品定义"
|
||||
developer_instructions = """
|
||||
用中文定义产品需求。
|
||||
"""
|
||||
role = "planning"
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(agentsDir, "product-manager.toml"), []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
store := Store{CodexHome: root}
|
||||
got, err := store.List()
|
||||
if err != nil {
|
||||
t.Fatalf("List returned error: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("agent count = %d, want 1", len(got))
|
||||
}
|
||||
if got[0].ID != "product-manager" || got[0].FileName != "product-manager.toml" {
|
||||
t.Fatalf("unexpected agent identity: %#v", got[0])
|
||||
}
|
||||
if got[0].Name != "产品经理" || got[0].Description != "负责产品定义" {
|
||||
t.Fatalf("unexpected agent fields: %#v", got[0])
|
||||
}
|
||||
if strings.TrimSpace(got[0].DeveloperInstructions) != "用中文定义产品需求。" {
|
||||
t.Fatalf("unexpected developer instructions: %q", got[0].DeveloperInstructions)
|
||||
}
|
||||
if got[0].ExtraFields["role"] != "planning" {
|
||||
t.Fatalf("unexpected extra fields: %#v", got[0].ExtraFields)
|
||||
}
|
||||
if got[0].ParseStatus != "valid" || got[0].DraftStatus != "clean" {
|
||||
t.Fatalf("unexpected statuses: %#v", got[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAgentsReportsParseError(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
agentsDir := filepath.Join(root, "agents")
|
||||
if err := os.MkdirAll(agentsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(agentsDir, "bad.toml"), []byte(`name = "`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
store := Store{CodexHome: root}
|
||||
got, err := store.List()
|
||||
if err != nil {
|
||||
t.Fatalf("List should return parse status, not fatal error: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("agent count = %d, want 1", len(got))
|
||||
}
|
||||
if got[0].ParseStatus != "invalid" || got[0].ParseError == "" {
|
||||
t.Fatalf("expected invalid parse status, got %#v", got[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAgentsRejectsSensitiveSymlinkTargets(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
agentsDir := filepath.Join(root, "agents")
|
||||
if err := os.MkdirAll(agentsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "auth.json"), []byte(`name = "secret"`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("../auth.json", filepath.Join(agentsDir, "leak.toml")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
store := Store{CodexHome: root}
|
||||
got, err := store.List()
|
||||
if err != nil {
|
||||
t.Fatalf("List should report unsafe files per item, not fatal error: %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("agent count = %d, want 1", len(got))
|
||||
}
|
||||
if got[0].ParseStatus != "invalid" {
|
||||
t.Fatalf("expected unsafe symlink to be invalid, got %#v", got[0])
|
||||
}
|
||||
if strings.Contains(got[0].Name, "secret") || strings.Contains(got[0].ParseError, "secret") {
|
||||
t.Fatalf("sensitive file content leaked: %#v", got[0])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user