opencode.fish

 1# SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2#
 3# SPDX-License-Identifier: Unlicense
 4
 5# OpenCode agent definition for synu
 6# Provides model configuration for routing through Synthetic API
 7# OpenCode only accepts model via -m flag, not environment variables
 8
 9# Source cache functions
10source (status dirname)/../_synu_cache.fish
11
12# Fallback default (used when no cache entry exists)
13set -g _synu_opencode_fallback_model "hf:zai-org/GLM-4.6"
14
15function _synu_opencode_default --description "Get default model"
16    set -l cached (_synu_cache_get opencode model)
17    if test $status -eq 0
18        echo $cached
19    else
20        echo $_synu_opencode_fallback_model
21    end
22end
23
24function _synu_agent_opencode_flags --description "Return argparse-compatible flag specification"
25    echo "m/model="
26end
27
28function _synu_agent_opencode_configure --description "Configure OpenCode model selection"
29    # Parse flags passed from main synu
30    argparse 'm/model=' -- $argv
31    or return 1
32
33    # Start with default (from cache or fallback)
34    set -g _synu_opencode_selected_model (_synu_opencode_default)
35
36    # Apply override if provided
37    if set -q _flag_model
38        set -g _synu_opencode_selected_model $_flag_model
39    end
40end
41
42function _synu_agent_opencode_args --description "Return CLI arguments to pass to opencode"
43    # Return -m flag with selected model, prefixed with synthetic/ for provider routing
44    echo -m
45    echo "synthetic/$_synu_opencode_selected_model"
46end
47
48function _synu_agent_opencode_interactive --description "Interactive model selection using gum"
49    # Check for gum
50    if not command -q gum
51        echo "Error: gum is required for interactive mode. Install: https://github.com/charmbracelet/gum" >&2
52        return 1
53    end
54
55    # Fetch available models
56    set -l models_json (gum spin --spinner dot --title "Fetching models..." -- \
57        curl -s -H "Authorization: Bearer $SYNTHETIC_API_KEY" \
58        "https://api.synthetic.new/openai/v1/models")
59    or return 1
60
61    set -l model_names (echo $models_json | jq -r '.data[].name')
62    or return 1
63
64    # Get current model for display
65    set -l current_id (_synu_opencode_default)
66    set -l current_name (echo $models_json | \
67        jq -r --arg id "$current_id" '.data[] | select(.id == $id) | .name // "unknown"')
68
69    # Select model
70    set -l model_name (printf "%s\n" $model_names | \
71        gum filter --limit 1 --header "Select model for OpenCode (current: $current_id)" \
72        --placeholder "Filter models...")
73    or return 1
74
75    set -l model_id (echo $models_json | \
76        jq -r --arg name "$model_name" '.data[] | select(.name == $name) | .id')
77
78    if test -z "$model_id"
79        echo "Error: Could not find model ID" >&2
80        return 1
81    end
82
83    # Build flags
84    set -l flags --model=$model_id
85
86    # Offer to save as default
87    if gum confirm "Save as default for 'opencode'?"
88        _synu_cache_set opencode model $model_id
89    end
90
91    # Output flags for caller to use
92    printf '%s\n' $flags
93end