1# SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2#
3# SPDX-License-Identifier: Unlicense
4
5# Cache management for synu model preferences
6# File format: agent.slot = model_id (one per line, # comments allowed)
7
8set -g _synu_cache_file (set -q XDG_CONFIG_HOME; and echo "$XDG_CONFIG_HOME"; or echo "$HOME/.config")"/synu/models.conf"
9
10function _synu_cache_get --description "Get a cached value: _synu_cache_get agent slot"
11 set -l agent $argv[1]
12 set -l slot $argv[2]
13 set -l key "$agent.$slot"
14
15 if not test -f "$_synu_cache_file"
16 return 1
17 end
18
19 # Match "key = value" or "key=value", ignoring comments and whitespace
20 set -l match (string match -r "^$key\\s*=\\s*(.+)" < "$_synu_cache_file")
21 if test -n "$match[2]"
22 string trim "$match[2]"
23 return 0
24 end
25 return 1
26end
27
28function _synu_cache_set --description "Set a cached value: _synu_cache_set agent slot value"
29 set -l agent $argv[1]
30 set -l slot $argv[2]
31 set -l value $argv[3]
32 set -l key "$agent.$slot"
33
34 # Ensure directory exists
35 mkdir -p (dirname "$_synu_cache_file")
36
37 if not test -f "$_synu_cache_file"
38 # Create new file with header
39 echo "# synu model preferences" > "$_synu_cache_file"
40 echo "# Format: agent.slot = model_id" >> "$_synu_cache_file"
41 echo "" >> "$_synu_cache_file"
42 end
43
44 # Check if key already exists
45 if string match -rq "^$key\\s*=" < "$_synu_cache_file"
46 # Replace existing line
47 set -l tmp (mktemp)
48 string replace -r "^$key\\s*=.*" "$key = $value" < "$_synu_cache_file" > "$tmp"
49 mv "$tmp" "$_synu_cache_file"
50 else
51 # Append new line
52 echo "$key = $value" >> "$_synu_cache_file"
53 end
54end