feat(deepseek api): add balance query and API key management

Add pj deepseek command to query account balance and
pj deepseek setkey to store API key in local cache.

Also extract Python launcher into its own module (pj_python.nu)
and add common aliases (g, c, tree, myip, ports, df, reload, edit).
This commit is contained in:
2026-06-14 11:10:16 +08:00
parent 1b801ef121
commit 727e4977f8
4 changed files with 90 additions and 11 deletions
+24 -11
View File
@@ -14,18 +14,31 @@ export alias ....... = cd ../../../../../..
export alias ........ = cd ../../../../../../.. export alias ........ = cd ../../../../../../..
export alias .......... = cd ../../../../../../../.. export alias .......... = cd ../../../../../../../..
export def py [...rest: string] {
if (which ^py | is-not-empty) {
^py ...$rest
} else if (which ^python3 | is-not-empty) {
^python3 ...$rest
} else {
print "Error: Python is not installed or could not be found."
}
}
export alias weather = curl wttr.in export alias weather = curl wttr.in
export alias moon = curl wttr.in/Moon export alias moon = curl wttr.in/Moon
export alias quote = curl https://api.quotable.io/random | from json | get content export alias quote = curl https://api.quotable.io/random | from json | get content
export alias dadjoke = curl -H "Accept: application/json" https://icanhazdadjoke.com/ | from json | get joke export alias dadjoke = curl -H "Accept: application/json" https://icanhazdadjoke.com/ | from json | get joke
# --- Git ---
export alias g = git
# --- Navigation & Shell ---
export alias c = clear
export def reload [] { exec nu }
export def edit [] { ^$env.EDITOR ($env.NU_POLYJUICE_PATH | path join 'nu_polyjuice.nu') }
# --- File system ---
export alias tree = ls **/*
# --- Network ---
export alias myip = curl -s ifconfig.me
export def ports [] { ^netstat -ano | lines | where {|row| $row | str contains 'LISTENING'} | str trim }
# --- System ---
export def df [] {
if (which ^df | is-not-empty) {
^df -h
} else {
^wmic logicaldisk get size,freespace,caption
}
}
+54
View File
@@ -0,0 +1,54 @@
const pj_module_dir = (path self | path dirname)
const pj_cache_dir = ($pj_module_dir | path join '..' 'cache')
const key_file = ($pj_cache_dir | path join 'deepseek_apikey.toml')
# Save your DeepSeek API key to local cache.
export def "pj deepseek setkey" [api_key: string] {
mkdir $pj_cache_dir
let content = $"[apikey]
key = \"($api_key)\"
"
$content | save -f $key_file
print $"(char --unicode '2714') DeepSeek API key saved to ($key_file)"
}
# Query DeepSeek account balance information.
export def "pj deepseek" [] {
if not ($key_file | path exists) {
print $"(char --unicode '274c') No DeepSeek API key found. Use `pj deepseek setkey <api-key>` to set it."
return
}
let config = (open $key_file)
let api_key = ($config.apikey?.key? | default "")
if ($api_key | is-empty) {
print $"(char --unicode '274c') Invalid API key in config file. Use `pj deepseek setkey <api-key>` to set a new one."
return
}
let response = (curl -L -X GET 'https://api.deepseek.com/user/balance' -H 'Accept: application/json' -H $"Authorization: Bearer ($api_key)" | complete)
if ($response.exit_code != 0) {
print $"(char --unicode '274c') Failed to query balance: ($response.stderr)"
return
}
let balance = ($response.stdout | from json)
let available_text = if $balance.is_available { $'(char --unicode '2705') Yes' } else { $'(char --unicode '274c') No' }
let separator = ('' | fill --character (char --unicode '2501') --width 25 --alignment l)
print ""
print " DeepSeek Account Balance"
print $" ($separator)"
print $" Available : ($available_text)"
print ""
for info in $balance.balance_infos {
print $" Currency : ($info.currency)"
print $" Total : ($info.total_balance)"
print $" Granted : ($info.granted_balance)"
print $" Topped-up : ($info.topped_up_balance)"
print ""
}
}
+10
View File
@@ -0,0 +1,10 @@
# Python launcher: prefer `py` (Windows launcher), fallback to `python3`
export def py [...rest: string] {
if (which ^py | is-not-empty) {
^py ...$rest
} else if (which ^python3 | is-not-empty) {
^python3 ...$rest
} else {
print "Error: Python is not installed or could not be found."
}
}
+2
View File
@@ -6,6 +6,7 @@ $env.NU_POLYJUICE_PATH = $env.FILE_PWD
# modules # modules
use modules/pj_aliases.nu * use modules/pj_aliases.nu *
use modules/pj_python.nu *
use modules/pj_brew.nu * use modules/pj_brew.nu *
use modules/pj_uv.nu * use modules/pj_uv.nu *
use modules/pj_completions.nu * use modules/pj_completions.nu *
@@ -13,4 +14,5 @@ use modules/pj_helix.nu *
use modules/pj_fzf.nu * use modules/pj_fzf.nu *
use modules/pj_zoxide.nu * use modules/pj_zoxide.nu *
use modules/pj_historyhintcomplete.nu * use modules/pj_historyhintcomplete.nu *
use modules/pj_deepseek.nu *
use modules/pj_prompt.nu * use modules/pj_prompt.nu *