Skip to content

Commit aa512cb

Browse files
committed
feat(plant): support custom dedication message on plantings
Add an optional message argument to /green:plant (and the treenation.sh plant command). The message is sent to the Tree-Nation plant endpoint and appears on the public certificate; it is also stored in usage.json. When no message is given, a localized default is used: "Offsetting {co2} kg CO2 - Claude Code AI usage - {date}" (fr/en). The request body is now built with jq -n so messages with quotes, accents or spaces are escaped safely. Bump to 1.1.0; docs and README updated.
1 parent cb872e5 commit aa512cb

5 files changed

Lines changed: 40 additions & 13 deletions

File tree

.claude-plugin/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "green-code",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Track your AI carbon footprint and plant trees to compensate via Tree-Nation",
55
"author": {
66
"name": "Jeremie Samson"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ On first launch, the plugin asks for your Tree-Nation API token and preferences.
2828
| `/green:status` | View your carbon footprint and tree balance |
2929
| `/green:plant` | Plant trees to offset accumulated CO2 |
3030
| `/green:plant 3` | Plant a specific number of trees |
31+
| `/green:plant 3 "message"` | Plant trees with a custom dedication message (shown on the certificate) |
3132
| `/green:config` | View current configuration |
3233
| `/green:config mode auto` | Switch to auto-plant mode |
3334

@@ -63,7 +64,7 @@ Per-model base cost in Wh per **output** token. Other token types are derived as
6364

6465
### Tree-Nation
6566

66-
Trees are planted via the [Tree-Nation REST API](https://kb.tree-nation.com/knowledge/api-availability). You need a Tree-Nation account and API token.
67+
Trees are planted via the [Tree-Nation REST API](https://kb.tree-nation.com/knowledge/api-availability). You need a Tree-Nation account and API token. Each planting carries a dedication message shown on the public certificate: pass your own (`/green:plant 3 "message"`) or let the plugin fill in a default (`Offsetting {co2} kg CO2 - Claude Code AI usage - {date}`).
6768

6869
## Sources
6970

scripts/i18n.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ if [ "$GREEN_LANG" = "fr" ]; then
5151
T_TN_NO_FOREST="Aucun forest ID Tree-Nation configuré. Lance /green:config"
5252
T_TN_TREES_PLANTED="arbres plantés"
5353
T_TN_OFFSET="kg CO2 compensés"
54-
T_TN_USAGE="Usage : treenation.sh {plant [N]|forest|species [project_id]}"
54+
# %s = CO2 compensé (kg), %s = date
55+
T_TN_DEFAULT_MSG="Compensation %s kg CO2 - usage IA Claude Code - %s"
56+
T_TN_USAGE="Usage : treenation.sh {plant [N] [message]|forest|species [project_id]}"
5557
else
5658
# session-start.sh
5759
T_EMITTED="emitted"
@@ -88,5 +90,7 @@ else
8890
T_TN_NO_FOREST="No Tree-Nation forest ID configured. Run /green:config"
8991
T_TN_TREES_PLANTED="trees planted"
9092
T_TN_OFFSET="kg CO2 offset"
91-
T_TN_USAGE="Usage: treenation.sh {plant [N]|forest|species [project_id]}"
93+
# %s = CO2 offset (kg), %s = date
94+
T_TN_DEFAULT_MSG="Offsetting %s kg CO2 - Claude Code AI usage - %s"
95+
T_TN_USAGE="Usage: treenation.sh {plant [N] [message]|forest|species [project_id]}"
9296
fi

scripts/treenation.sh

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,31 @@ BASE_URL="https://tree-nation.com/api"
2121

2222
cmd_plant() {
2323
local count="${1:-1}"
24+
local message="${2:-}"
2425
local threshold=$(jq '.threshold_co2_kg // 10' "$CONFIG_FILE")
2526
local co2_offset=$(echo "scale=2; $count * $threshold" | bc)
2627

28+
# Default dedication message (shown on the public Tree-Nation certificate)
29+
# when the caller did not provide one.
30+
if [ -z "$message" ]; then
31+
message="$(printf "$T_TN_DEFAULT_MSG" "$co2_offset" "$(date -u +%Y-%m-%d)")"
32+
fi
33+
34+
# Build the JSON body with jq so the message is safely escaped (quotes,
35+
# accents, spaces). The Tree-Nation plant endpoint accepts an optional
36+
# "message" field that is attached to each planting.
37+
local payload
38+
payload=$(jq -n \
39+
--argjson forest_id "$FOREST_ID" \
40+
--argjson quantity "$count" \
41+
--arg message "$message" \
42+
'{forest_id: $forest_id, quantity: $quantity, message: $message}')
43+
2744
response=$(curl -s -w "\n%{http_code}" \
2845
-X POST "${BASE_URL}/plant" \
2946
-H "Authorization: Bearer ${API_KEY}" \
3047
-H "Content-Type: application/json" \
31-
-d "{
32-
\"forest_id\": ${FOREST_ID},
33-
\"quantity\": ${count}
34-
}")
48+
-d "$payload")
3549

3650
http_code=$(echo "$response" | tail -1)
3751
body=$(echo "$response" | sed '$d')
@@ -47,12 +61,14 @@ cmd_plant() {
4761
--arg date "$NOW" \
4862
--argjson offset "$co2_offset" \
4963
--arg certs "$cert_urls" \
64+
--arg message "$message" \
5065
'
5166
.trees.total += $count |
5267
.trees.planted += [{
5368
"date": $date,
5469
"count": $count,
5570
"co2_offset_kg": $offset,
71+
"message": $message,
5672
"certificates": ($certs | split("\n") | map(select(. != "")))
5773
}]
5874
' "$USAGE_FILE" > "${USAGE_FILE}.tmp" && mv "${USAGE_FILE}.tmp" "$USAGE_FILE"
@@ -85,7 +101,7 @@ cmd_species() {
85101
}
86102

87103
case "${1:-help}" in
88-
plant) cmd_plant "${2:-1}" ;;
104+
plant) cmd_plant "${2:-1}" "${3:-}" ;;
89105
forest) cmd_forest ;;
90106
species) cmd_species "${2:-}" ;;
91107
*)

skills/plant/SKILL.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: plant
3-
description: Plant trees via Tree-Nation to offset your AI carbon footprint. Use when the user wants to compensate their CO2 emissions. Accepts optional tree count argument.
3+
description: Plant trees via Tree-Nation to offset your AI carbon footprint. Use when the user wants to compensate their CO2 emissions. Accepts optional tree count and message arguments.
44
---
55

66
# green-code: Plant Trees
@@ -21,9 +21,14 @@ Plant trees via Tree-Nation to compensate the user's AI carbon footprint.
2121
4. Confirm with the user before planting:
2222
"You're about to plant {N} tree(s) via Tree-Nation to offset {co2} kg of CO2. Proceed?"
2323

24-
5. If confirmed, run the planting script:
24+
4b. Determine the dedication message (attached to each planting and shown on the public Tree-Nation certificate):
25+
- If the user provided a message (e.g., `/green:plant 3 "merci pour la forêt"`), use it verbatim.
26+
- If no message is provided, leave it empty: the script fills in a default
27+
(`Compensation {co2} kg CO2 - usage IA Claude Code - {date}` / `Offsetting {co2} kg CO2 - Claude Code AI usage - {date}`).
28+
29+
5. If confirmed, run the planting script (the message is optional; wrap it in quotes):
2530
```bash
26-
"${CLAUDE_PLUGIN_ROOT}/scripts/treenation.sh" plant {N}
31+
"${CLAUDE_PLUGIN_ROOT}/scripts/treenation.sh" plant {N} "{message}"
2732
```
2833

2934
6. Parse the output:
@@ -45,5 +50,6 @@ Plant trees via Tree-Nation to compensate the user's AI carbon footprint.
4550
## Important
4651

4752
- Always confirm before planting (it costs real money via Tree-Nation credits)
48-
- The script handles logging the planting in usage.json automatically
53+
- The script handles logging the planting in usage.json automatically (including the `message`)
54+
- The message appears on the public certificate, so keep it clean; the default is safe when in doubt
4955
- If the API call fails, do NOT modify usage.json

0 commit comments

Comments
 (0)