Skip to content

Commit 0bdcf45

Browse files
committed
Simpler code
Signed-off-by: David Gageot <[email protected]>
1 parent b4ddb0f commit 0bdcf45

File tree

3 files changed

+17
-36
lines changed

3 files changed

+17
-36
lines changed

docs/USAGE.md

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,41 +114,27 @@ agents:
114114
commands: # Either mapping or list of singleton maps
115115
df: "check how much free space i have on my disk"
116116
ls: "list the files in the current directory"
117+
greet: "Say hello to $USER and ask how their day is going"
118+
analyze: "Analyze the project named $PROJECT_NAME in the $ENVIRONMENT environment"
117119
```
118120
119-
### Running with named commands
120-
121-
- Example YAML forms supported:
122-
123-
```yaml
124-
commands:
125-
df: "check how much free space i have on my disk"
126-
ls: "list the files in the current directory"
127-
greet: "Say hello to $USER and ask how their day is going"
128-
analyze: "Analyze the project named $PROJECT_NAME in the $ENVIRONMENT environment"
129-
```
130-
131-
```yaml
132-
commands:
133-
- df: "check how much free space i have on my disk"
134-
- ls: "list the files in the current directory"
135-
- greet: "Say hello to $USER and ask how their day is going"
136-
```
137-
138-
Commands support environment variable placeholders using `$VARIABLE_NAME` or `${VARIABLE_NAME}` syntax.
139-
140-
Run:
121+
### Running named commands
141122
142123
```bash
143124
cagent run ./agent.yaml /df
144125
cagent run ./agent.yaml /ls
126+
127+
export USER=alice
128+
cagent run ./agent.yaml /greet
129+
130+
export PROJECT_NAME=myproject
131+
export ENVIRONMENT=production
132+
cagent run ./agent.yaml /analyze
145133
```
146134

147-
**Environment Variable Expansion:**
148135
- Placeholders are expanded during agent loading using available environment variables
149136
- Undefined variables expand to empty strings (no error is thrown)
150137
- Both `$VAR` and `${VAR}` syntax are supported
151-
- This allows dynamic command configuration without manual string interpolation
152138

153139
### Model Properties
154140

examples/env_placeholders.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
version: "2"
33

44
# Example demonstrating environment variable placeholder expansion in commands
5-
#
5+
#
66
# This example shows how to use $VARIABLE_NAME or ${VARIABLE_NAME} placeholders
77
# in command definitions, which are automatically replaced with environment variable
88
# values when the agent is loaded.
@@ -11,7 +11,7 @@ version: "2"
1111
# export USER=alice
1212
# export PROJECT_NAME=myproject
1313
# export ENVIRONMENT=production
14-
#
14+
#
1515
# cagent run env_placeholders.yaml -c greet
1616
# cagent run env_placeholders.yaml -c analyze
1717
# cagent run env_placeholders.yaml -c status
@@ -26,15 +26,15 @@ agents:
2626
instruction: |
2727
You are a helpful assistant that responds to user requests.
2828
Provide clear, concise, and actionable responses.
29-
29+
3030
commands:
3131
greet: "Say hello to $USER and ask how their day is going"
3232
analyze: "Analyze the project named $PROJECT_NAME in the $ENVIRONMENT environment and provide insights"
3333
status: "Check the status of the service ${SERVICE_NAME} and report any issues"
3434
report: "Generate a comprehensive report for project $PROJECT_NAME owned by $OWNER deployed in $LOCATION"
3535
inspect: "Inspect the contents of the directory at $WORKING_DIR and summarize what you find"
3636
help: "Explain what you can do and how to use environment variable placeholders in commands"
37-
37+
3838
toolsets:
3939
- type: filesystem
4040
- type: shell

pkg/teamloader/teamloader.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,12 @@ func createThinkTool(ctx context.Context, toolset latest.Toolset, parentDir stri
115115
// Undefined variables are allowed and will expand to empty strings, enabling users to
116116
// only define the environment variables needed for the commands they actually use.
117117
func expandCommandPlaceholders(ctx context.Context, commands map[string]string, envProvider environment.Provider) map[string]string {
118-
if len(commands) == 0 {
119-
return commands
120-
}
118+
expanded := map[string]string{}
121119

122-
expanded := make(map[string]string, len(commands))
123120
for name, value := range commands {
124121
expanded[name] = environment.ExpandLenient(ctx, value, envProvider)
125122
}
123+
126124
return expanded
127125
}
128126

@@ -350,9 +348,6 @@ func Load(ctx context.Context, p string, runtimeConfig config.RuntimeConfig, opt
350348
agentsByName := make(map[string]*agent.Agent)
351349

352350
for name, agentConfig := range cfg.Agents {
353-
// Expand environment variable placeholders in commands
354-
expandedCommands := expandCommandPlaceholders(ctx, agentConfig.Commands, env)
355-
356351
opts := []agent.Opt{
357352
agent.WithName(name),
358353
agent.WithDescription(agentConfig.Description),
@@ -361,7 +356,7 @@ func Load(ctx context.Context, p string, runtimeConfig config.RuntimeConfig, opt
361356
agent.WithAddPromptFiles(agentConfig.AddPromptFiles),
362357
agent.WithMaxIterations(agentConfig.MaxIterations),
363358
agent.WithNumHistoryItems(agentConfig.NumHistoryItems),
364-
agent.WithCommands(expandedCommands),
359+
agent.WithCommands(expandCommandPlaceholders(ctx, agentConfig.Commands, env)),
365360
}
366361

367362
models, err := getModelsForAgent(ctx, cfg, &agentConfig, env, runtimeConfig)

0 commit comments

Comments
 (0)