-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicli
More file actions
153 lines (129 loc) · 3.56 KB
/
unicli
File metadata and controls
153 lines (129 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env zsh
HISTFILE="${HOME}/.unicli_history"
STATEFILE="${HOME}/.unicli_state"
typeset -a CMD_LIST
CMD_LIST=(
help
exit
set
show
ask
info
)
touch $HISTFILE
fc -R $HISTFILE 2> /dev/null || true
touch $STATEFILE
fc -R $STATEFILE 2> /dev/null || true
function echo_error(){
echo "$@" >&2
}
function do_help(){
echo "Available commands:"
for cmd in $CMD_LIST; do
echo " - $cmd"
done
}
function do_exit(){
exit 1
}
typeset -A STATE
function do_set(){
if [[ $# -lt 3 ]]; then
echo_error "Usage: set <key> <value>"
return 0
fi
STATE[$2]=$3
echo "Set $2 to $3"
}
function do_show(){
if [[ $# -lt 2 ]]; then
echo_error "Usage: show <key>"
return 1
fi
if [[ -z ${STATE[$2]} ]]; then
echo_error "$2 is not set"
return 0
fi
echo "$2: ${STATE[$2]}"
}
function do_ask(){
if [[ $# -ne 1 ]] || [[ $1 == "-h"]]; then
echo "Usage: ask <question> <model>"
echo '"\033[0;31m" ATTENTION: api_key and agent_id must be set before using "\033[0;33m"set"\033[0m" command "\033[0m"'
return 0
fi
if [[ -z ${STATE[agent_id]} || -z ${STATE[api_key]} ]]; then
echo_error "agent_id and api_key must be set"
return 0
fi
if ! command -v curl >/dev/null 2>&1; then
printf 'curl is not installed, please install it with: sudo apt install curl -y. Do you want to install it now? (y/n)\n'
local key
read key
if[[ $key == 'y' || $key == 'Y']]; then
sudo apt install curl -y
else
printf 'curl is not installed\n'
fi
return 2
fi
if ! command -v jq >/dev/null 2>&1; then
printf 'jq is not installed, please install it with: sudo apt install jq -y. Do you want to install it now? (y/n)\n'
local key
read key
if [[ $key == 'y' || $key == 'Y' ]]; then
sudo apt install jq -y
else
printf 'jq is required to use this function\n'
return 3
fi
fi
local response
response=$(curl -s -X PSOT "https://pr-07.theunico.it/api/agents/$agentUuid/completions" \
-H "Authorization: Bearer $apiKey" \
-H "Content-Type: application/json" \
-d '{"query": $1, "model": "gemini-2.0-flash"}'
)
local clean_response
clean_response=$(printf '%s' "$response" | tr -d '\000-\037')
printf '%s\n' "$clean_response" | jq -r '.text' | awk 'NR==1{printf "%s\n", $0} NR==2{printf "%s\n", $0} NR>2{print}'
}
do_info(){
echo "unicli version 1.0"
echo "Author: zakkmccrack"
echo "This is a simple CLI tool using zsh."
echo "histoy file: $HISTFILE"
echo "state file: $STATEFILE"
}
function handle_cmd(){
local -a parts
parts=("${(@s/ /)1}")
local cmd=$parts[1]
local arg=($parts[@]:1)
case $cmd in
help) do_help ;;
exit) do_exit ;;
set) do_set $arg ;;
show) do_show $arg ;;
ask) do_ask $arg ;;
info) do_info ;;
*) echo_error "Unknown command: $cmd" ;;
esac
return 0
}
function main_loop(){
while true; do
read "?unicli> " line
handle_cmd "$line"
echo $line >> $HISTFILE
done
}
if (( $# > 0 )); then
handle_cmd "${(j: : )argv}"
exit $?
fi
echo "Benvenuto in mycli (zsh). Digita 'help'. Ctrl-C o 'exit' per uscire."
main_loop
# salva la history
fc -W "$HISTFILE" 2>/dev/null || true
exit 0