mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-05-22 04:51:12 +02:00
code style
This commit is contained in:
parent
e9232db8bc
commit
13bcaf81db
1 changed files with 34 additions and 31 deletions
|
|
@ -1,31 +1,35 @@
|
||||||
#!/bin/zsh
|
function ds() {
|
||||||
|
# Preflight checks
|
||||||
ds() {
|
(( $+commands[curl] )) || {
|
||||||
# --- preflight checks ---
|
|
||||||
if [[ ! $+commands[curl] ]]; then
|
|
||||||
echo "ds: curl must be installed." >&2
|
echo "ds: curl must be installed." >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
}
|
||||||
if [[ ! $+commands[jq] ]]; then
|
(( $+commands[jq] )) || {
|
||||||
echo "ds: jq must be installed." >&2
|
echo "ds: jq must be installed." >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
}
|
||||||
if [[ -z "$DEEPSEEK_API_KEY" ]]; then
|
[[ -n "$DEEPSEEK_API_KEY" ]] || {
|
||||||
echo "ds: DEEPSEEK_API_KEY is not set." >&2
|
echo "ds: DEEPSEEK_API_KEY is not set." >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
}
|
||||||
if [[ $# -eq 0 ]]; then
|
(( $# )) || {
|
||||||
echo "ds: usage — ds <natural language query>" >&2
|
echo "Usage: ds <natural language query>" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
}
|
||||||
|
|
||||||
# --- system prompt ---
|
# System prompt
|
||||||
local sys_prompt="You're an in-line zsh assistant running on ${OSTYPE}. Your task is to answer the questions without any commentation at all, providing only the code to run in terminal. You can assume that the user understands that they need to fill in placeholders like <PORT>. You're not allowed to explain anything and you're not a chatbot.
|
local sys_prompt="You're an in-line zsh assistant running on ${OSTYPE}. \
|
||||||
You only provide shell commands or code. Keep the responses to one-liner answers as much as possible. Do not decorate the answer with tickmarks."
|
Your task is to answer the questions without any commentation at all, \
|
||||||
|
providing only the code to run in terminal. \
|
||||||
|
You can assume that the user understands that they need to fill in placeholders like <PORT>. \
|
||||||
|
You're not allowed to explain anything and you're not a chatbot. \
|
||||||
|
You only provide shell commands or code. \
|
||||||
|
Keep the responses to one-liner answers as much as possible. \
|
||||||
|
Do not decorate the answer with tickmarks."
|
||||||
|
|
||||||
# --- build JSON payload safely with jq ---
|
# Build JSON payload safely with jq
|
||||||
local payload
|
local payload
|
||||||
payload=$(jq -n \
|
payload=$(command jq -n \
|
||||||
--arg system "$sys_prompt" \
|
--arg system "$sys_prompt" \
|
||||||
--arg user "$*" \
|
--arg user "$*" \
|
||||||
'{
|
'{
|
||||||
|
|
@ -40,9 +44,9 @@ You only provide shell commands or code. Keep the responses to one-liner answers
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- call DeepSeek API ---
|
# Call DeepSeek API
|
||||||
local response
|
local response
|
||||||
response=$(curl https://api.deepseek.com/chat/completions -s \
|
response=$(command curl https://api.deepseek.com/chat/completions -s \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-H "Authorization: Bearer ${DEEPSEEK_API_KEY}" \
|
-H "Authorization: Bearer ${DEEPSEEK_API_KEY}" \
|
||||||
-d "$payload" 2>&1) || {
|
-d "$payload" 2>&1) || {
|
||||||
|
|
@ -50,31 +54,30 @@ You only provide shell commands or code. Keep the responses to one-liner answers
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- extract result ---
|
# Extract result
|
||||||
local result
|
local result
|
||||||
result=$(jq -r '.choices[0].message.content // empty' <<< "$response" 2>/dev/null)
|
result=$(command jq -r '.choices[0].message.content // empty' <<< "$response" 2>/dev/null)
|
||||||
|
|
||||||
if [[ -z "$result" ]]; then
|
if [[ -z "$result" ]]; then
|
||||||
local err_msg
|
local err_msg
|
||||||
err_msg=$(jq -r '.error.message // "unknown error"' <<< "$response" 2>/dev/null)
|
err_msg=$(command jq -r '.error.message // "unknown error"' <<< "$response" 2>/dev/null)
|
||||||
echo "ds: ${err_msg}" >&2
|
echo "ds: ${err_msg}" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- copy to system clipboard (cross-platform) ---
|
# Copy to system clipboard (cross-platform)
|
||||||
if [[ "$OSTYPE" == darwin* ]]; then
|
if [[ "$OSTYPE" == darwin* ]]; then
|
||||||
print -r -- "$result" | pbcopy
|
print -r -- "$result" | command pbcopy
|
||||||
elif command -v wl-copy &>/dev/null; then
|
elif command -v wl-copy &>/dev/null; then
|
||||||
print -r -- "$result" | wl-copy
|
print -r -- "$result" | command wl-copy
|
||||||
elif command -v xclip &>/dev/null; then
|
elif command -v xclip &>/dev/null; then
|
||||||
print -r -- "$result" | xclip -selection clipboard
|
print -r -- "$result" | command xclip -selection clipboard
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# --- echo result to terminal for visibility ---
|
# Echo result to terminal for visibility
|
||||||
echo "$result"
|
echo "$result"
|
||||||
|
|
||||||
# --- push into ZLE buffer so it sits on the next prompt line ---
|
# Push into ZLE buffer so it sits on the next prompt line
|
||||||
# user can edit it or press Enter to execute immediately.
|
# User can edit it or press Enter to execute immediately.
|
||||||
print -z "$result"
|
print -z "$result"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue