update
Some checks failed
Docker Build and Push / build-and-push (push) Failing after 24s
Tests / test (bash) (push) Successful in 10s
Tests / test (zsh) (push) Successful in 10s
Tests / lint (push) Successful in 9s
Tests / docker (push) Successful in 19s

This commit is contained in:
mike
2025-12-11 08:27:03 +01:00
parent 3f9acb08ec
commit 074a2b1f5f
5 changed files with 169 additions and 53 deletions

View File

@@ -259,16 +259,21 @@ log_request() {
openai_completion() {
local content status_code response_body default_user_input user_input api_key payload endpoint timeout attempt max_attempts
local log_file debug_log
# Ensure configuration (provider, endpoint, etc.) is loaded before checks
acsh_load_config
log_file=${ACSH_LOG_FILE:-"$HOME/.finish/finish.log"}
debug_log="$HOME/.finish/debug.log"
endpoint=${ACSH_ENDPOINT:-"http://plato.lan:1234/v1/chat/completions"}
timeout=${ACSH_TIMEOUT:-30}
default_user_input="Write two to six most likely commands given the provided information"
user_input=${*:-$default_user_input}
log_file=${ACSH_LOG_FILE:-"$HOME/.finish/finish.log"}
debug_log="$HOME/.finish/debug.log"
# Only check for API key if not using local providers that don't require it
if [[ -z "$ACSH_ACTIVE_API_KEY" && ${ACSH_PROVIDER^^} != "OLLAMA" && ${ACSH_PROVIDER^^} != "LMSTUDIO" ]]; then
echo_error "ACSH_ACTIVE_API_KEY not set. Please set it with: export ${ACSH_PROVIDER^^}_API_KEY=<your-api-key>"
return
return 1
fi
api_key="${ACSH_ACTIVE_API_KEY}"
payload=$(_build_payload "$user_input")
@@ -300,9 +305,16 @@ openai_completion() {
if [[ "${ACSH_PROVIDER^^}" == "OLLAMA" ]]; then
\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" --data "$payload" -N > "$temp_file"
else
\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" \
-H "Content-Type: application/json" \
-d "$payload" -N > "$temp_file"
if [[ -n "$api_key" ]]; then
\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
-d "$payload" -N > "$temp_file"
else
\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" \
-H "Content-Type: application/json" \
-d "$payload" -N > "$temp_file"
fi
fi
status_code=$(tail -n1 "$temp_file")
@@ -330,9 +342,16 @@ openai_completion() {
if [[ "${ACSH_PROVIDER^^}" == "OLLAMA" ]]; then
response=$(\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" --data "$payload")
else
response=$(\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" \
-H "Content-Type: application/json" \
-d "$payload")
if [[ -n "$api_key" ]]; then
response=$(\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
-d "$payload")
else
response=$(\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" \
-H "Content-Type: application/json" \
-d "$payload")
fi
fi
status_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$d')
@@ -362,8 +381,8 @@ openai_completion() {
500) echo_error "Internal Server Error: An unexpected error occurred on the API server." ;;
*) echo_error "Unknown Error: Unexpected status code $status_code received. Response: $response_body" ;;
esac
return
fi
return 1
fi
if [[ "${ACSH_PROVIDER^^}" == "OLLAMA" ]]; then
content=$(echo "$response_body" | jq -r '.message.content')
@@ -393,7 +412,7 @@ openai_completion() {
if [[ -z "$completions" ]]; then
echo_error "Failed to parse completions from API response. Check $debug_log for details."
return
return 1
fi
echo -n "$completions"
@@ -446,13 +465,6 @@ _finishsh() {
if [[ ${#COMPREPLY[@]} -eq 0 && $COMP_TYPE -eq 63 ]]; then
local completions user_input user_input_hash
acsh_load_config
if [[ -z "$ACSH_ACTIVE_API_KEY" && ${ACSH_PROVIDER^^} != "OLLAMA" && ${ACSH_PROVIDER^^} != "LMSTUDIO" ]]; then
local provider_key="${ACSH_PROVIDER}_API_KEY"
provider_key=$(echo "$provider_key" | tr '[:lower:]' '[:upper:]')
echo_error "${provider_key} is not set. Please set it using: export ${provider_key}=<your-api-key> or disable finish via: finish disable"
echo
return
fi
if [[ -n "${COMP_WORDS[*]}" ]]; then
command="${COMP_WORDS[0]}"
if [[ -n "$COMP_CWORD" && "$COMP_CWORD" -lt "${#COMP_WORDS[@]}" ]]; then