1# SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2#
3# SPDX-License-Identifier: Unlicense
4
5# Qwen Code agent definition for synu
6# Provides model configuration for routing through Synthetic API
7# Qwen Code only accepts configuration via environment variables
8
9# Fallback default (used when no cache entry exists)
10typeset -g _SYNU_QWEN_FALLBACK_MODEL="hf:zai-org/GLM-4.6"
11
12_synu_qwen_default() {
13 local cached
14 cached=$(_synu_cache_get qwen model)
15 if [[ $? -eq 0 ]]; then
16 echo "${cached}"
17 else
18 echo "${_SYNU_QWEN_FALLBACK_MODEL}"
19 fi
20}
21
22_synu_agent_qwen_flags() {
23 echo "m/model="
24}
25
26_synu_agent_qwen_env_vars() {
27 echo OPENAI_API_KEY
28 echo OPENAI_BASE_URL
29 echo OPENAI_MODEL
30}
31
32_synu_agent_qwen_configure() {
33 local -A opts
34
35 # Parse flags passed from main synu
36 while [[ $# -gt 0 ]]; do
37 case "$1" in
38 --model=*) opts[model]="${1#*=}" ;;
39 esac
40 shift
41 done
42
43 # Start with default (from cache or fallback)
44 local model=$(_synu_qwen_default)
45
46 # Apply override if provided
47 [[ -n "${opts[model]}" ]] && model="${opts[model]}"
48
49 # Export environment variables for Qwen Code
50 export OPENAI_API_KEY="${SYNTHETIC_API_KEY}"
51 export OPENAI_BASE_URL="https://api.synthetic.new/openai/v1"
52 export OPENAI_MODEL="${model}"
53}
54
55_synu_agent_qwen_interactive() {
56 # Check for gum
57 if ! (( $+commands[gum] )); then
58 print -u2 "Error: gum is required for interactive mode. Install: https://github.com/charmbracelet/gum"
59 return 1
60 fi
61
62 # Fetch available models
63 local models_json
64 models_json=$(gum spin --spinner dot --title "Fetching models..." -- \
65 curl -s -H "Authorization: Bearer ${SYNTHETIC_API_KEY}" \
66 "https://api.synthetic.new/openai/v1/models")
67 [[ $? -ne 0 ]] && return 1
68
69 local -a model_names
70 model_names=("${(@f)$(echo "${models_json}" | jq -r '.data[].name')}")
71 [[ $? -ne 0 ]] && return 1
72
73 # Get current model for display
74 local current_id=$(_synu_qwen_default)
75 local current_name=$(echo "${models_json}" | \
76 jq -r --arg id "${current_id}" '.data[] | select(.id == $id) | .name // "unknown"')
77
78 # Select model
79 local model_name
80 model_name=$(printf "%s\n" "${model_names[@]}" | \
81 gum filter --limit 1 --header "Select model for Qwen Code (current: ${current_name})" \
82 --placeholder "Filter models...")
83 [[ $? -ne 0 ]] && return 1
84
85 local model_id
86 model_id=$(echo "${models_json}" | \
87 jq -r --arg name "${model_name}" '.data[] | select(.name == $name) | .id')
88
89 if [[ -z "${model_id}" ]]; then
90 print -u2 "Error: Could not find model ID"
91 return 1
92 fi
93
94 # Build flags
95 local flags="--model=${model_id}"
96
97 # Offer to save as default
98 if gum confirm "Save as default for 'qwen'?"; then
99 _synu_cache_set qwen model "${model_id}"
100 fi
101
102 # Output flags for caller to use
103 printf '%s\n' "${flags}"
104}