1# SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2#
3# SPDX-License-Identifier: Unlicense
4
5# Aider agent definition for synu
6# Provides model configuration for routing through Synthetic API
7# Aider accepts model via CLI flags and API config via environment variables
8
9# Source cache functions
10source (status dirname)/../_synu_cache.fish
11
12# Fallback defaults (used when no cache entry exists)
13set -g _synu_aider_fallback_model "hf:zai-org/GLM-4.6"
14set -g _synu_aider_fallback_editor_model "hf:deepseek-ai/DeepSeek-V3.1-Terminus"
15
16function _synu_aider_default --description "Get default model: _synu_aider_default slot"
17 set -l slot $argv[1]
18 set -l cached (_synu_cache_get aider $slot)
19 if test $status -eq 0
20 echo $cached
21 else
22 set -l var_name _synu_aider_fallback_$slot
23 echo $$var_name
24 end
25end
26
27function _synu_agent_aider_flags --description "Return argparse-compatible flag specification"
28 # Note: no short flags to avoid collision with aider's -m (--message) and -e (--env-file)
29 echo "model="
30 echo "editor-model="
31end
32
33function _synu_agent_aider_env_vars --description "Return list of environment variables set by configure"
34 echo OPENAI_API_BASE
35 echo OPENAI_API_KEY
36end
37
38function _synu_agent_aider_configure --description "Configure Aider environment variables and model selection"
39 # Parse flags passed from main synu
40 # Note: no short flags to avoid collision with aider's -m (--message) and -e (--env-file)
41 argparse 'model=' 'editor-model=' -- $argv
42 or return 1
43
44 # Start with defaults (from cache or fallback)
45 set -g _synu_aider_selected_model (_synu_aider_default model)
46 set -g _synu_aider_selected_editor_model (_synu_aider_default editor_model)
47
48 # Apply overrides if provided
49 if set -q _flag_model
50 set -g _synu_aider_selected_model $_flag_model
51 end
52 if set -q _flag_editor_model
53 set -g _synu_aider_selected_editor_model $_flag_editor_model
54 end
55
56 # Export environment variables for Aider
57 set -gx OPENAI_API_BASE "https://api.synthetic.new/openai/v1"
58 set -gx OPENAI_API_KEY $SYNTHETIC_API_KEY
59end
60
61function _synu_agent_aider_args --description "Return CLI arguments to pass to aider"
62 # Always return --model
63 echo --model
64 echo "openai/$_synu_aider_selected_model"
65
66 # Return --editor-model if set
67 if test -n "$_synu_aider_selected_editor_model"
68 echo --editor-model
69 echo "openai/$_synu_aider_selected_editor_model"
70 end
71end
72
73function _synu_agent_aider_interactive --description "Interactive model selection using gum"
74 # Check for gum
75 if not command -q gum
76 echo "Error: gum is required for interactive mode. Install: https://github.com/charmbracelet/gum" >&2
77 return 1
78 end
79
80 # Fetch available models
81 set -l models_json (gum spin --spinner dot --title "Fetching models..." -- \
82 curl -s -H "Authorization: Bearer $SYNTHETIC_API_KEY" \
83 "https://api.synthetic.new/openai/v1/models")
84 or return 1
85
86 set -l model_names (echo $models_json | jq -r '.data[].name')
87 or return 1
88
89 # Ask if editor model should be set
90 set -l use_editor_model (gum choose --limit 1 \
91 --header "Aider has two modes. Set editor model?" \
92 "No (single model mode)" "Yes (architect + editor mode)")
93 or return 1
94
95 # Build flags array
96 set -l flags
97
98 # Get current models for display
99 set -l current_model_id (_synu_aider_default model)
100 set -l current_model_name (echo $models_json | \
101 jq -r --arg id "$current_model_id" '.data[] | select(.id == $id) | .name // "unknown"')
102 set -l current_editor_id (_synu_aider_default editor_model)
103 set -l current_editor_name (echo $models_json | \
104 jq -r --arg id "$current_editor_id" '.data[] | select(.id == $id) | .name // "unknown"')
105
106 # Select main model
107 set -l model_name (printf "%s\n" $model_names | \
108 gum filter --limit 1 --header "Select main model for Aider (current: $current_model_id)" \
109 --placeholder "Filter models...")
110 or return 1
111
112 set -l model_id (echo $models_json | \
113 jq -r --arg name "$model_name" '.data[] | select(.name == $name) | .id')
114
115 if test -z "$model_id"
116 echo "Error: Could not find model ID" >&2
117 return 1
118 end
119
120 set flags $flags --model=$model_id
121
122 # Select editor model if requested
123 if test "$use_editor_model" = "Yes (architect + editor mode)"
124 set -l editor_model_name (printf "%s\n" $model_names | \
125 gum filter --limit 1 --header "Select editor model for Aider (current: $current_editor_id)" \
126 --placeholder "Filter models...")
127 or return 1
128
129 set -l editor_model_id (echo $models_json | \
130 jq -r --arg name "$editor_model_name" '.data[] | select(.name == $name) | .id')
131
132 if test -z "$editor_model_id"
133 echo "Error: Could not find editor model ID" >&2
134 return 1
135 end
136
137 set flags $flags --editor-model=$editor_model_id
138 end
139
140 # Offer to save as defaults
141 if gum confirm "Save as default for 'aider'?"
142 for flag in $flags
143 # Parse --key=value format
144 set -l parts (string match -r -- '^--([^=]+)=(.+)$' $flag)
145 if test -n "$parts[2]"
146 set -l key $parts[2]
147 set -l value $parts[3]
148 # Replace hyphens with underscores for cache keys
149 set key (string replace -a '-' '_' $key)
150 _synu_cache_set aider $key $value
151 end
152 end
153 end
154
155 # Output flags for caller to use
156 printf '%s\n' $flags
157end