mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2026-02-16 16:42:29 +01:00
refactor(ai): replace empty buffer with min input
- Replace ZSH_AUTOSUGGEST_ALLOW_EMPTY_BUFFER with AI_MIN_INPUT - Add ZSH_AUTOSUGGEST_AI_DEBUG environment variable - Add debug logging function to diagnose failures - Update history lines default from 20 to 5 - Update pwd history preference default to no Min input provides clearer semantics: set to 0 for empty-buffer suggestions or higher to require minimum input. Debug logging helps diagnose missing suggestions by showing API request flow. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
81672cc7fe
commit
cd66c5695a
7 changed files with 142 additions and 52 deletions
|
|
@ -35,8 +35,8 @@ _zsh_autosuggest_strategy_ai_gather_context() {
|
|||
# Reset options to defaults and enable LOCAL_OPTIONS
|
||||
emulate -L zsh
|
||||
|
||||
local max_lines="${ZSH_AUTOSUGGEST_AI_HISTORY_LINES:-20}"
|
||||
local prefer_pwd="${ZSH_AUTOSUGGEST_AI_PREFER_PWD_HISTORY:-yes}"
|
||||
local max_lines="${ZSH_AUTOSUGGEST_AI_HISTORY_LINES:-5}"
|
||||
local prefer_pwd="${ZSH_AUTOSUGGEST_AI_PREFER_PWD_HISTORY:-no}"
|
||||
local pwd_basename="${PWD:t}"
|
||||
local -a context_lines
|
||||
local -a pwd_lines
|
||||
|
|
@ -141,6 +141,18 @@ _zsh_autosuggest_strategy_ai_normalize() {
|
|||
printf '%s' "$result"
|
||||
}
|
||||
|
||||
_zsh_autosuggest_strategy_ai_debug_log() {
|
||||
# Reset options to defaults and enable LOCAL_OPTIONS
|
||||
emulate -L zsh
|
||||
|
||||
local debug="${ZSH_AUTOSUGGEST_AI_DEBUG:-0}"
|
||||
case "${debug:l}" in
|
||||
0|false|no|off) return ;;
|
||||
esac
|
||||
|
||||
print -ru2 -- "[zsh-autosuggestions ai] $1"
|
||||
}
|
||||
|
||||
_zsh_autosuggest_strategy_ai() {
|
||||
# Reset options to defaults and enable LOCAL_OPTIONS
|
||||
emulate -L zsh
|
||||
|
|
@ -149,14 +161,23 @@ _zsh_autosuggest_strategy_ai() {
|
|||
local buffer="$1"
|
||||
|
||||
# Early return if API key not set (opt-in gate)
|
||||
[[ -z "$ZSH_AUTOSUGGEST_AI_API_KEY" ]] && return
|
||||
if [[ -z "$ZSH_AUTOSUGGEST_AI_API_KEY" ]]; then
|
||||
_zsh_autosuggest_strategy_ai_debug_log "API key not set; skipping AI request."
|
||||
return
|
||||
fi
|
||||
|
||||
# Early return if curl or jq not available
|
||||
[[ -z "${commands[curl]}" ]] || [[ -z "${commands[jq]}" ]] && return
|
||||
if [[ -z "${commands[curl]}" ]] || [[ -z "${commands[jq]}" ]]; then
|
||||
_zsh_autosuggest_strategy_ai_debug_log "Missing dependency: curl and jq are required."
|
||||
return
|
||||
fi
|
||||
|
||||
# Early return if input too short
|
||||
local min_input="${ZSH_AUTOSUGGEST_AI_MIN_INPUT:-0}"
|
||||
[[ ${#buffer} -lt $min_input ]] && return
|
||||
local min_input="${ZSH_AUTOSUGGEST_AI_MIN_INPUT:-1}"
|
||||
if [[ ${#buffer} -lt $min_input ]]; then
|
||||
_zsh_autosuggest_strategy_ai_debug_log "Input shorter than ZSH_AUTOSUGGEST_AI_MIN_INPUT=$min_input."
|
||||
return
|
||||
fi
|
||||
|
||||
# Gather history context
|
||||
local -a context
|
||||
|
|
@ -222,6 +243,8 @@ _zsh_autosuggest_strategy_ai() {
|
|||
local timeout="${ZSH_AUTOSUGGEST_AI_TIMEOUT:-5}"
|
||||
local response
|
||||
|
||||
_zsh_autosuggest_strategy_ai_debug_log "Requesting $endpoint (model=${ZSH_AUTOSUGGEST_AI_MODEL:-gpt-3.5-turbo}, input_len=${#buffer})."
|
||||
|
||||
response=$(curl --silent --max-time "$timeout" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $ZSH_AUTOSUGGEST_AI_API_KEY" \
|
||||
|
|
@ -230,26 +253,39 @@ _zsh_autosuggest_strategy_ai() {
|
|||
"$endpoint" 2>/dev/null)
|
||||
|
||||
# Check curl exit status
|
||||
[[ $? -ne 0 ]] && return
|
||||
local curl_status=$?
|
||||
if [[ $curl_status -ne 0 ]]; then
|
||||
_zsh_autosuggest_strategy_ai_debug_log "curl failed with exit code $curl_status."
|
||||
return
|
||||
fi
|
||||
|
||||
# Split response body from HTTP status
|
||||
local http_code="${response##*$'\n'}"
|
||||
local body="${response%$'\n'*}"
|
||||
|
||||
# Early return on non-2xx status
|
||||
[[ "$http_code" != 2* ]] && return
|
||||
if [[ "$http_code" != 2* ]]; then
|
||||
_zsh_autosuggest_strategy_ai_debug_log "HTTP $http_code from AI endpoint."
|
||||
return
|
||||
fi
|
||||
|
||||
# Extract content from JSON response
|
||||
local content
|
||||
content=$(printf '%s' "$body" | jq -r '.choices[0].message.content // empty' 2>/dev/null)
|
||||
|
||||
# Early return if extraction failed
|
||||
[[ -z "$content" ]] && return
|
||||
if [[ -z "$content" ]]; then
|
||||
_zsh_autosuggest_strategy_ai_debug_log "No suggestion content in API response."
|
||||
return
|
||||
fi
|
||||
|
||||
# Normalize response
|
||||
local normalized
|
||||
normalized="$(_zsh_autosuggest_strategy_ai_normalize "$content" "$buffer")"
|
||||
|
||||
# Set suggestion
|
||||
[[ -n "$normalized" ]] && suggestion="$normalized"
|
||||
if [[ -n "$normalized" ]]; then
|
||||
suggestion="$normalized"
|
||||
_zsh_autosuggest_strategy_ai_debug_log "AI suggestion accepted: '$normalized'."
|
||||
fi
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue