-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathask
More file actions
executable file
·90 lines (80 loc) · 2.92 KB
/
ask
File metadata and controls
executable file
·90 lines (80 loc) · 2.92 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
ask() {
local model="anthropic/claude-sonnet-4.5"
local use_search=false
local use_explain=false
# --- flag parsing ---
while [[ "$1" == -* ]]; do
case "$1" in
-m) shift
case "$1" in
claude) model="anthropic/claude-sonnet-4.5" ;;
gpt4) model="openai/gpt-4o-mini" ;;
gpt) model="openai/gpt-3.5-turbo" ;;
*) model="$1" ;;
esac
shift ;;
-s|--search) use_search=true; shift ;;
-e|--explain) use_explain=true; shift ;;
*) break ;;
esac
done
# --- query input ---
local query
if [[ $# -gt 0 ]]; then
query="$*"
elif [[ ! -t 0 ]]; then
query="$(cat)"
else
echo "Usage: ask [-s] [-e] [-m model] <question>"
echo " echo <question> | ask"
echo ""
echo "Flags:"
echo " -m <model> claude | gpt4 | gpt | or any full model string"
echo " -s Web search (via Exa, ~\$0.02/req)"
echo " -e Explain mode — detailed reasoning and gotchas"
return 1
fi
# --- append :online to model if search requested ---
$use_search && model="${model}:online"
# --- system prompt ---
local base_prompt="You are a concise CLI assistant for an experienced macOS user (macOS 15.3.1, zsh, homebrew, vim). Stack: bash, TypeScript, Node.js, Go. Focus areas: web and infra. If something can be answered with a one-liner command, just give the command — nothing else. No preamble, no sign-off. Output plain markdown with inline code, no walls of text."
local explain_directive=""
$use_explain && explain_directive=" The user wants a thorough explanation this time: walk through the reasoning, flags, and any gotchas."
local system_prompt="${base_prompt}${explain_directive}"
# --- build payload ---
local payload
payload=$(jq -n \
--arg q "$query" \
--arg m "$model" \
--arg s "$system_prompt" \
'{
model: $m,
messages: [
{role: "system", content: $s},
{role: "user", content: $q}
]
}')
# --- call api ---
local response
response=$(curl -s https://openrouter.ai/api/v1/chat/completions \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d "$payload")
# --- error check ---
local error
error=$(printf "%s" "$response" | jq -r '.error.message // empty')
if [[ -n "$error" ]]; then
echo "Error: $error" >&2
return 1
fi
# --- render output ---
printf "%s" "$response" \
| tr -d '\000-\031' \
| jq -r '
.choices[0].message.content
| if type == "string" then .
else map(.text) | join("")
end
' \
| glow -
}