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:MiniMaxAI/MiniMax-M2"
14set -g _synu_aider_fallback_editor_model ""
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 # Select main model
97 set -l model_name (printf "%s\n" $model_names | \
98 gum filter --limit 1 --header "Select main model for Aider" \
99 --placeholder "Filter models...")
100 or return 1
101
102 set -l model_id (echo $models_json | \
103 jq -r --arg name "$model_name" '.data[] | select(.name == $name) | .id')
104
105 if test -z "$model_id"
106 echo "Error: Could not find model ID" >&2
107 return 1
108 end
109
110 set flags $flags --model=$model_id
111
112 # Select editor model if requested
113 if test "$use_editor_model" = "Yes (architect + editor mode)"
114 set -l editor_model_name (printf "%s\n" $model_names | \
115 gum filter --limit 1 --header "Select editor model for Aider" \
116 --placeholder "Filter models...")
117 or return 1
118
119 set -l editor_model_id (echo $models_json | \
120 jq -r --arg name "$editor_model_name" '.data[] | select(.name == $name) | .id')
121
122 if test -z "$editor_model_id"
123 echo "Error: Could not find editor model ID" >&2
124 return 1
125 end
126
127 set flags $flags --editor-model=$editor_model_id
128 end
129
130 # Offer to save as defaults
131 if gum confirm "Save as default for 'aider'?"
132 for flag in $flags
133 # Parse --key=value format
134 set -l parts (string match -r -- '^--([^=]+)=(.+)$' $flag)
135 if test -n "$parts[2]"
136 set -l key $parts[2]
137 set -l value $parts[3]
138 # Replace hyphens with underscores for cache keys
139 set key (string replace -a '-' '_' $key)
140 _synu_cache_set aider $key $value
141 end
142 end
143 end
144
145 # Output flags for caller to use
146 echo $flags
147end