17 // agentInstructionsFileName is the file of the user's instructions in
18 // the directory of an agent.
19 agentInstructionsFileName = "instructions.md"
21 // agentIDBytes is the number of random bytes of an agent ID.
25 // openAgent reads the config.json of the agent in dir. When the file does not
26 // exist, it makes the directory and writes the file with the defaults and a new
27 // ID. A missing field of an existing file takes the default, but the ID must be
28 // there. An unknown field is an error, so a typo does not become a silent
30 func openAgent(dir string) (*Agent, error) {
31 err := os.MkdirAll(dir, 0o700)
33 return nil, fmt.Errorf("create agent directory: %w", err)
36 path := filepath.Join(dir, "config.json")
37 data, err := os.ReadFile(path)
38 if errors.Is(err, os.ErrNotExist) {
39 return createAgent(dir)
43 return nil, fmt.Errorf("read agent config: %w", err)
46 config, err := defaultAgentConfig()
51 decoder := json.NewDecoder(bytes.NewReader(data))
52 decoder.DisallowUnknownFields()
54 err = decoder.Decode(&config)
55 if err == nil && !errors.Is(decoder.Decode(&struct{}{}), io.EOF) {
56 err = errors.New("data after the JSON object")
60 err = config.validate()
64 return nil, fmt.Errorf("%s: %w", path, err)
73 // Agent is one agent and its directory. The directory holds config.json, the
74 // user's instructions in instructions.md, the chat, and the usage file.
80 // CacheKey returns the prompt cache key of every model call of the agent.
81 func (a *Agent) CacheKey() string {
82 return "onechat-" + a.config.ID
85 // Prompt returns the system prompt of the agent with the user's instructions
86 // from instructions.md. It reads the file on each call, so a change applies at
87 // the next call. A missing file means no instructions.
88 func (a *Agent) Prompt() (string, error) {
89 instructions, err := a.instructions()
91 return "", fmt.Errorf("read prompt instructions: %w", err)
94 path, err := filepath.Abs(filepath.Join(a.dir, agentInstructionsFileName))
96 return "", fmt.Errorf("find instructions path: %w", err)
99 return prompt(a.config.Name, path, instructions), nil
102 // SubagentPrompt returns the system prompt of a subagent of the agent, with the
103 // user's instructions as Prompt reads them.
104 func (a *Agent) SubagentPrompt() (string, error) {
105 instructions, err := a.instructions()
110 return promptSubagent(a.config.Name, instructions), nil
113 // instructions reads the user's instructions from instructions.md. A missing
114 // file means no instructions.
115 func (a *Agent) instructions() (string, error) {
116 data, err := os.ReadFile(filepath.Join(a.dir, agentInstructionsFileName))
117 if err != nil && !errors.Is(err, os.ErrNotExist) {
118 return "", fmt.Errorf("read instructions: %w", err)
121 return string(data), nil
124 // AgentConfig holds the settings of the config.json of an agent. ID is random,
125 // made once when the file is made, and never changes. The cache key comes from
126 // the ID, so a rename keeps the prompt cache and two agents with one name do
127 // not share a key. Workdir is the directory where the commands of the agent
129 type AgentConfig struct {
130 Name string `json:"name"`
131 ID string `json:"id"`
132 Model string `json:"model"`
133 Effort string `json:"effort"`
134 Workdir string `json:"workdir"`
137 // validate makes sure that each setting has a valid value.
138 func (c AgentConfig) validate() error {
139 if c.Name == "" || strings.ContainsAny(c.Name, "\r\n") {
140 return errors.New("name must be one line of text")
143 id, err := hex.DecodeString(c.ID)
144 if err != nil || len(id) != agentIDBytes {
145 return fmt.Errorf("id must be %d hexadecimal digits", 2*agentIDBytes)
148 if c.Model == "" || c.Effort == "" {
149 return errors.New("agent needs a model and an effort")
152 if !filepath.IsAbs(c.Workdir) {
153 return fmt.Errorf("workdir %q is not an absolute path", c.Workdir)
159 // createAgent writes the config.json of a new agent in dir with the defaults
161 func createAgent(dir string) (*Agent, error) {
162 config, err := defaultAgentConfig()
167 id := make([]byte, agentIDBytes)
168 _, err = rand.Read(id)
170 return nil, fmt.Errorf("make agent ID: %w", err)
173 config.ID = hex.EncodeToString(id)
174 data, err := json.MarshalIndent(config, "", " ")
176 return nil, fmt.Errorf("encode agent config: %w", err)
179 err = writeFileAtomic(filepath.Join(dir, "config.json"), append(data, '\n'))
181 return nil, fmt.Errorf("write agent config: %w", err)
190 // defaultAgentConfig returns the settings of a new agent, without an ID. The
191 // commands run in the home directory of the user.
192 func defaultAgentConfig() (AgentConfig, error) {
193 home, err := os.UserHomeDir()
195 return AgentConfig{}, fmt.Errorf("find working directory: %w", err)
200 Model: "gpt-6.1-sol",