]> git.giacomolli.uk - onechat/blobdiff - agent.go
Write private data archives with safe extraction
[onechat] / agent.go
index 4892840786da9a7ff4567d4c4321cd9e45172dd8..0d97cdf4af176d51fcde931e38d7288f003be1ca 100644 (file)
--- 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,12 +86,39 @@ 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)
+       }
+
+       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,