Files
TerminalPolyjuice/nushell/modules/pj_deepseek.nu
T
gwbeip deb4e45c4e fix(nushell): improve robustness and clarify abstraction boundaries
- pj_completions: merge into $env.config instead of replacing it wholesale
- pj_completions: use parse-time const for cache dir instead of pwd fallback
- pj_fzf / pj_historyhintcomplete: null-safe keybindings access via ? and default
- pj_helix: replace $env.OS (Windows-only) with $nu.os-info.name; extract shared launcher
- pj_prompt: fix off-by-one in git OID substring (0..7 -> 0..<7)
- pj_prompt: use $nu.os-info.name for OS detection instead of sys host hostname
- pj_prompt: cache git availability in static base, skip per-render which call
- pj_prompt: bind LAST_EXIT_CODE once in right-prompt closure
- pj_brew / pj_uv: add --ignore-errors to hide-env calls
- pj_aliases: convert quote/dadjoke to defs with try/catch error handling
- pj_deepseek: guard curl availability before invoking
- pj_zoxide: add ? to $nu.os-info.name access
- pj_starship: fix null-to-nuon producing "null" string; broaden detection
- nu_polyjuice: load pj_starship module
2026-07-31 00:31:04 +08:00

80 lines
2.6 KiB
Nu

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')
# Read the stored API key from cache, printing an error and returning null on failure.
def _pj_deepseek_readkey [] {
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 null
}
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 null
}
$api_key
}
# 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)"
}
# Print the stored DeepSeek API key.
export def "pj deepseek printkey" [] {
let api_key = (_pj_deepseek_readkey)
if ($api_key == null) { return }
print $api_key
}
# Query DeepSeek account balance information.
export def "pj deepseek" [] {
if (which ^curl | is-empty) {
print "curl is required for `pj deepseek`."
return
}
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 api_key = (_pj_deepseek_readkey)
if ($api_key == null) { 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 ""
}
}