X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/7cc0014244124aea59672904de65a979a8e5fb2c..77f29e78734dc16cfcea5f41b8e0185f8b33534a:/agent.go?ds=inline diff --git a/agent.go b/agent.go index 4892840..790e18b 100644 --- a/agent.go +++ b/agent.go @@ -50,6 +50,7 @@ func openAgent(dir string) (*Agent, error) { decoder := json.NewDecoder(bytes.NewReader(data)) decoder.DisallowUnknownFields() + err = decoder.Decode(&config) if err == nil && !errors.Is(decoder.Decode(&struct{}{}), io.EOF) { err = errors.New("data after the JSON object") @@ -85,25 +86,58 @@ func (a *Agent) CacheKey() string { // from instructions.md. It reads the file on each call, so a change applies at // the next call. A missing file means no instructions. func (a *Agent) Prompt() (string, error) { + instructions, err := a.instructions() + if err != nil { + return "", fmt.Errorf("read prompt instructions: %w", err) + } + + path, err := filepath.Abs(filepath.Join(a.dir, agentInstructionsFileName)) + if err != nil { + return "", fmt.Errorf("find instructions path: %w", err) + } + + if a.config.Parent != "" { + return promptChild(a.config.Name, a.config.Parent, a.config.Responsibility, path, instructions), nil + } + + return prompt(a.config.Name, path, instructions), nil +} + +// SubagentPrompt returns the system prompt of a subagent of the agent, with the +// user's instructions as Prompt reads them. +func (a *Agent) SubagentPrompt() (string, error) { + instructions, err := a.instructions() + if err != nil { + return "", err + } + + return promptSubagent(a.config.Name, instructions), nil +} + +// instructions reads the user's instructions from instructions.md. A missing +// file means no instructions. +func (a *Agent) instructions() (string, error) { data, err := os.ReadFile(filepath.Join(a.dir, agentInstructionsFileName)) if err != nil && !errors.Is(err, os.ErrNotExist) { return "", fmt.Errorf("read instructions: %w", err) } - return prompt(a.config.Name, string(data)), nil + return string(data), nil } // AgentConfig holds the settings of the config.json of an agent. ID is random, // made once when the file is made, and never changes. The cache key comes from // the ID, so a rename keeps the prompt cache and two agents with one name do // not share a key. Workdir is the directory where the commands of the agent -// run. +// run. Responsibility and Parent are set together for a child agent. type AgentConfig struct { - Name string `json:"name"` - ID string `json:"id"` - Model string `json:"model"` - Effort string `json:"effort"` - Workdir string `json:"workdir"` + Name string `json:"name"` + ID string `json:"id"` + Model string `json:"model"` + Effort string `json:"effort"` + Workdir string `json:"workdir"` + Responsibility string `json:"responsibility,omitempty"` + Parent string `json:"parent,omitempty"` } // validate makes sure that each setting has a valid value. @@ -121,6 +155,14 @@ func (c AgentConfig) validate() error { return errors.New("agent needs a model and an effort") } + if (strings.TrimSpace(c.Responsibility) == "") != (c.Parent == "") { + return errors.New("a child needs both responsibility and parent") + } + + if strings.ContainsAny(c.Parent, "\r\n") { + return errors.New("parent must be one line of text") + } + if !filepath.IsAbs(c.Workdir) { return fmt.Errorf("workdir %q is not an absolute path", c.Workdir) }