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 echo "m/model="
29 echo "e/editor-model="
30end
31
32function _synu_agent_aider_env_vars --description "Return list of environment variables set by configure"
33 echo OPENAI_API_BASE
34 echo OPENAI_API_KEY
35end
36
37function _synu_agent_aider_configure --description "Configure Aider environment variables and model selection"
38 # Parse flags passed from main synu
39 argparse 'm/model=' 'e/editor-model=' -- $argv
40 or return 1
41
42 # Start with defaults (from cache or fallback)
43 set -g _synu_aider_selected_model (_synu_aider_default model)
44 set -g _synu_aider_selected_editor_model (_synu_aider_default editor_model)
45
46 # Apply overrides if provided
47 if set -q _flag_model
48 set -g _synu_aider_selected_model $_flag_model
49 end
50 if set -q _flag_editor_model
51 set -g _synu_aider_selected_editor_model $_flag_editor_model
52 end
53
54 # Export environment variables for Aider
55 set -gx OPENAI_API_BASE "https://api.synthetic.new/openai/v1"
56 set -gx OPENAI_API_KEY $SYNTHETIC_API_KEY
57end
58
59function _synu_agent_aider_args --description "Return CLI arguments to pass to aider"
60 # Always return --model
61 echo --model
62 echo "openai/$_synu_aider_selected_model"
63
64 # Return --editor-model if set
65 if test -n "$_synu_aider_selected_editor_model"
66 echo --editor-model
67 echo "openai/$_synu_aider_selected_editor_model"
68 end
69end
70
71function _synu_agent_aider_interactive --description "Interactive model selection using gum"
72 # Check for gum
73 if not command -q gum
74 echo "Error: gum is required for interactive mode. Install: https://github.com/charmbracelet/gum" >&2
75 return 1
76 end
77
78 # Fetch available models
79 set -l models_json (gum spin --spinner dot --title "Fetching models..." -- \
80 curl -s -H "Authorization: Bearer $SYNTHETIC_API_KEY" \
81 "https://api.synthetic.new/openai/v1/models")
82 or return 1
83
84 set -l model_names (echo $models_json | jq -r '.data[].name')
85 or return 1
86
87 # Ask if editor model should be set
88 set -l use_editor_model (gum choose --limit 1 \
89 --header "Aider has two modes. Set editor model?" \
90 "No (single model mode)" "Yes (architect + editor mode)")
91 or return 1
92
93 # Build flags array
94 set -l flags
95
96 # Get current models for display
97 set -l current_model_id (_synu_aider_default model)
98 set -l current_model_name (echo $models_json | \
99 jq -r --arg id "$current_model_id" '.data[] | select(.id == $id) | .name // "unknown"')
100 set -l current_editor_id (_synu_aider_default editor_model)
101 set -l current_editor_name (echo $models_json | \
102 jq -r --arg id "$current_editor_id" '.data[] | select(.id == $id) | .name // "unknown"')
103
104 # Select main model
105 set -l model_name (printf "%s\n" $model_names | \
106 gum filter --limit 1 --header "Select main model for Aider (current: $current_model_id)" \
107 --placeholder "Filter models...")
108 or return 1
109
110 set -l model_id (echo $models_json | \
111 jq -r --arg name "$model_name" '.data[] | select(.name == $name) | .id')
112
113 if test -z "$model_id"
114 echo "Error: Could not find model ID" >&2
115 return 1
116 end
117
118 set flags $flags --model=$model_id
119
120 # Select editor model if requested
121 if test "$use_editor_model" = "Yes (architect + editor mode)"
122 set -l editor_model_name (printf "%s\n" $model_names | \
123 gum filter --limit 1 --header "Select editor model for Aider (current: $current_editor_id)" \
124 --placeholder "Filter models...")
125 or return 1
126
127 set -l editor_model_id (echo $models_json | \
128 jq -r --arg name "$editor_model_name" '.data[] | select(.name == $name) | .id')
129
130 if test -z "$editor_model_id"
131 echo "Error: Could not find editor model ID" >&2
132 return 1
133 end
134
135 set flags $flags --editor-model=$editor_model_id
136 end
137
138 # Offer to save as defaults
139 if gum confirm "Save as default for 'aider'?"
140 for flag in $flags
141 # Parse --key=value format
142 set -l parts (string match -r -- '^--([^=]+)=(.+)$' $flag)
143 if test -n "$parts[2]"
144 set -l key $parts[2]
145 set -l value $parts[3]
146 # Replace hyphens with underscores for cache keys
147 set key (string replace -a '-' '_' $key)
148 _synu_cache_set aider $key $value
149 end
150 end
151 end
152
153 # Output flags for caller to use
154 printf '%s\n' $flags
155end