---
name: Environment Templates
description: Full CRUD management for task templates in this environment, including create, update, delete, and view operations.
argument-hint: list | view <name> | create <name> | update <name> | delete <name>
---

# Environment Templates Skill

This skill provides full management for task templates in the current environment:
- Template-level operations: list, view, create, update, delete

Use this when users want direct control over task templates and their parameters.

## Scope And Guardrails

- This skill can only modify templates in the current task's environment.
- Template names must use lowercase letters, numbers, and hyphens only.
- Use explicit user intent for destructive operations (`delete`).
- If user intent is already clear and explicit, execute directly; do not add unnecessary confirmation loops.

## Available Commands

### 1. List Templates (`/environment-templates list`)

List all templates in the current environment.

### 2. View Template (`/environment-templates view <name>`)

Fetch a template's content and metadata.

### 3. Create Template (`/environment-templates create <name>`)

Create a new task template with required fields:
- `content` - Template body (markdown with instructions)

Optional:
- `metadata` - Object containing `description` and `parameters`

### 4. Update Template (`/environment-templates update <name>`)

Update an existing template's content and/or metadata.

### 5. Delete Template (`/environment-templates delete <name>`)

Remove a template and its metadata from the environment.

## How It Works

This skill uses the task-scoped environment management API.

### Environment Variables

- `CODERFLOW_SERVER_URL` - Base URL for CoderFlow server
- `TASK_ID` - Task identifier used for auth (`X-Task-Id`)

### API Endpoints

- `GET /api/environment-management/templates` - List all templates
- `GET /api/environment-management/templates/:name` - Get specific template
- `PUT /api/environment-management/templates/:name` - Create or update template
- `DELETE /api/environment-management/templates/:name` - Delete template

## Template Structure

Templates consist of two files:
1. **`<name>.md`** - The template content (markdown with instructions)
2. **`<name>.json`** (optional) - Template metadata (description, parameters)

### Metadata JSON Structure
```json
{
  "description": "Brief description shown in template picker",
  "parameters": [
    {
      "name": "feature_name",
      "label": "Feature Name",
      "type": "string",
      "required": true
    }
  ]
}
```

### Template Content Syntax

**CRITICAL**: Template parameters must use `${parameter_name}` syntax (dollar sign + curly braces), NOT `{{parameter_name}}`.

CoderFlow's template rendering engine works in two passes:
1. **First pass**: Replaces `${variable}` with parameter values
2. **Second pass**: Processes EJS syntax (e.g., `<%= variable %>`)

**Correct syntax:**
```markdown
# Task: ${task_name}

Create a ${component_type} component with the following features:
- ${feature_description}
```

**Wrong syntax (will NOT work):**
```markdown
# Task: {{task_name}}  ❌ WRONG - parameters won't be substituted

Create a {{component_type}} component  ❌ WRONG
```

**Alternative (EJS syntax, also works):**
```markdown
# Task: <%= task_name %>

Create a <%= component_type %> component
```

## Implementation

### List Templates
```bash
curl -s "${CODERFLOW_SERVER_URL}/api/environment-management/templates" \
  -H "X-Task-Id: ${TASK_ID}"
```

**Response:**
```json
{
  "templates": [
    {"name": "bug-fix", "filename": "bug-fix.md", "description": "Fix a bug", "hasMetadata": true}
  ],
  "environment": "my-environment"
}
```

### Get Template
```bash
curl -s "${CODERFLOW_SERVER_URL}/api/environment-management/templates/bug-fix" \
  -H "X-Task-Id: ${TASK_ID}"
```

**Response:**
```json
{
  "name": "bug-fix",
  "content": "# Bug Fix Template\n\n...",
  "metadata": {"description": "Fix a bug", "parameters": []},
  "environment": "my-environment"
}
```

### Create/Update Template

**IMPORTANT**: When creating templates with parameters, always use a temporary file to avoid JSON array mangling in bash. Never embed complex JSON with arrays directly in `-d '{...}'`.

```bash
# Create a temporary file with the complete JSON payload
cat > /tmp/template-payload.json <<'EOF'
{
  "content": "# API Endpoint Implementation\n\n...",
  "metadata": {
    "description": "Create a new API endpoint",
    "parameters": [
      {"name": "endpoint_path", "label": "Endpoint Path", "type": "string", "required": true},
      {"name": "http_method", "label": "HTTP Method", "type": "string", "required": true},
      {"name": "description", "label": "Description", "type": "string", "required": false}
    ]
  }
}
EOF

# Use the temp file with curl
curl -s -X PUT "${CODERFLOW_SERVER_URL}/api/environment-management/templates/api-endpoint" \
  -H "Content-Type: application/json" \
  -H "X-Task-Id: ${TASK_ID}" \
  -d @/tmp/template-payload.json
```

**Alternative using heredoc directly:**
```bash
curl -s -X PUT "${CODERFLOW_SERVER_URL}/api/environment-management/templates/api-endpoint" \
  -H "Content-Type: application/json" \
  -H "X-Task-Id: ${TASK_ID}" \
  -d @- <<'EOF'
{
  "content": "# API Endpoint Implementation\n\n...",
  "metadata": {
    "description": "Create a new API endpoint",
    "parameters": [
      {"name": "endpoint_path", "label": "Endpoint Path", "type": "string", "required": true}
    ]
  }
}
EOF
```

### Delete Template
```bash
curl -s -X DELETE "${CODERFLOW_SERVER_URL}/api/environment-management/templates/old-template" \
  -H "X-Task-Id: ${TASK_ID}"
```

## Important Notes

1. When updating, preserve existing content that is still relevant.
2. Clearly define what parameters a template accepts in the metadata.
3. Templates affect how future tasks are created; call out impact before execution.
4. When creating templates with parameters, ALWAYS use a temp file (`cat > /tmp/file.json` + `-d @/tmp/file.json`) or heredoc (`-d @- <<'EOF'`). Never use inline `-d '{...}'` with complex nested JSON containing arrays, as bash shell escaping will corrupt the array structure.
5. If template visibility or content changes, verify with a follow-up list/view call.
