mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-05-22 04:51:12 +02:00
deepseek plugin to help user write shell command
This commit is contained in:
parent
3604dc23e0
commit
e9232db8bc
2 changed files with 140 additions and 0 deletions
60
plugins/deepseek/README.md
Normal file
60
plugins/deepseek/README.md
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
# deepseek
|
||||
|
||||
This plugin adds the `ds` command, which turns natural language queries into
|
||||
shell commands using the DeepSeek API. The generated command is echoed to the
|
||||
terminal, pushed onto the next prompt line (ready to execute or edit), and
|
||||
copied to the system clipboard.
|
||||
|
||||
To use it, add `deepseek` to the plugins array in your `.zshrc` file:
|
||||
|
||||
```zsh
|
||||
plugins=(... deepseek)
|
||||
```
|
||||
|
||||
## Requirements
|
||||
|
||||
- [curl](https://curl.se/)
|
||||
- [jq](https://jqlang.github.io/jq/)
|
||||
- A [DeepSeek API key](https://platform.deepseek.com/api_keys)
|
||||
|
||||
## Configuration
|
||||
|
||||
Set your API key as an environment variable (e.g. in `.zshrc` or `.zprofile`):
|
||||
|
||||
```zsh
|
||||
export DEEPSEEK_API_KEY="sk-..."
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```zsh
|
||||
# Ask for a command in plain English (or any language)
|
||||
ds find the 10 largest files in the current directory
|
||||
|
||||
# The output looks like:
|
||||
# find . -type f -exec du -h {} + | sort -rh | head -10
|
||||
# $ █
|
||||
#
|
||||
# The command is:
|
||||
# 1. Printed to the terminal for review
|
||||
# 2. Copied to the system clipboard
|
||||
# 3. Pre-filled on the next prompt line — press Enter to run it,
|
||||
# or edit it first
|
||||
```
|
||||
|
||||
The system prompt instructs the model to respond with **only** the shell command
|
||||
or code — no explanations, no markdown formatting.
|
||||
|
||||
## Commands
|
||||
|
||||
| Command | Description |
|
||||
| ------- | ---------------------------------------- |
|
||||
| `ds` | Query DeepSeek for a shell command |
|
||||
|
||||
## Cross-platform clipboard
|
||||
|
||||
| Platform | Clipboard tool |
|
||||
| ------------- | ----------------------- |
|
||||
| macOS | `pbcopy` |
|
||||
| Linux (X11) | `xclip` |
|
||||
| Linux (Wayland)| `wl-copy` |
|
||||
80
plugins/deepseek/deepseek.plugin.zsh
Normal file
80
plugins/deepseek/deepseek.plugin.zsh
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
#!/bin/zsh
|
||||
|
||||
ds() {
|
||||
# --- preflight checks ---
|
||||
if [[ ! $+commands[curl] ]]; then
|
||||
echo "ds: curl must be installed." >&2
|
||||
return 1
|
||||
fi
|
||||
if [[ ! $+commands[jq] ]]; then
|
||||
echo "ds: jq must be installed." >&2
|
||||
return 1
|
||||
fi
|
||||
if [[ -z "$DEEPSEEK_API_KEY" ]]; then
|
||||
echo "ds: DEEPSEEK_API_KEY is not set." >&2
|
||||
return 1
|
||||
fi
|
||||
if [[ $# -eq 0 ]]; then
|
||||
echo "ds: usage — ds <natural language query>" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
# --- 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.
|
||||
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 ---
|
||||
local payload
|
||||
payload=$(jq -n \
|
||||
--arg system "$sys_prompt" \
|
||||
--arg user "$*" \
|
||||
'{
|
||||
model: "deepseek-v4-flash",
|
||||
messages: [
|
||||
{role: "system", content: $system},
|
||||
{role: "user", content: $user}
|
||||
],
|
||||
stream: false
|
||||
}') || {
|
||||
echo "ds: failed to build JSON payload." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
# --- call DeepSeek API ---
|
||||
local response
|
||||
response=$(curl https://api.deepseek.com/chat/completions -s \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer ${DEEPSEEK_API_KEY}" \
|
||||
-d "$payload" 2>&1) || {
|
||||
echo "ds: API request failed (network error)." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
# --- extract result ---
|
||||
local result
|
||||
result=$(jq -r '.choices[0].message.content // empty' <<< "$response" 2>/dev/null)
|
||||
|
||||
if [[ -z "$result" ]]; then
|
||||
local err_msg
|
||||
err_msg=$(jq -r '.error.message // "unknown error"' <<< "$response" 2>/dev/null)
|
||||
echo "ds: ${err_msg}" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
# --- copy to system clipboard (cross-platform) ---
|
||||
if [[ "$OSTYPE" == darwin* ]]; then
|
||||
print -r -- "$result" | pbcopy
|
||||
elif command -v wl-copy &>/dev/null; then
|
||||
print -r -- "$result" | wl-copy
|
||||
elif command -v xclip &>/dev/null; then
|
||||
print -r -- "$result" | xclip -selection clipboard
|
||||
fi
|
||||
|
||||
# --- echo result to terminal for visibility ---
|
||||
echo "$result"
|
||||
|
||||
# --- push into ZLE buffer so it sits on the next prompt line ---
|
||||
# user can edit it or press Enter to execute immediately.
|
||||
print -z "$result"
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue