From 6f04297008b2e2e4bfad1bdf29620dc0f3044540 Mon Sep 17 00:00:00 2001 From: Timoite Date: Fri, 19 Sep 2025 14:42:44 +0800 Subject: [PATCH] Add interactive execution mode --- README.md | 9 +++++++++ ask | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 76be733..535d8f9 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,14 @@ echo "Fix this code: print('hello world)" | ask cat script.py | ask "Review this code" ``` +### Interactive execution mode + +```bash +# Show the AI response, then prompt: +# Execute this command? (y/n): +ask -x "list all files in current directory" +``` + ## Options | Option | Description | @@ -99,6 +107,7 @@ cat script.py | ask "Review this code" | `-r` | Disable system prompt | | `--stream` | Enable streaming output | | `--system` | Set custom system prompt | +| `-x` | Enable Interactive execution | | `--provider` | Set provider order (comma-separated) | | `-h, --help` | Show help message | diff --git a/ask b/ask index 14b4eb4..0abe69b 100755 --- a/ask +++ b/ask @@ -29,6 +29,7 @@ PROMPT="" STREAMING=false NO_SYSTEM=false PROVIDER_ORDER="" +EXECUTE=false # Default system prompt (direct answers) DEFAULT_PROMPT="You are a direct answer engine. Output ONLY the requested information. @@ -60,6 +61,7 @@ Options: -q Use qwen/qwen3-235b-a22b-2507 -m MODEL Use custom model -r Disable system prompt (raw model behavior) + -x Enable interactive execution (prompt y/n to execute) --stream Enable streaming output --system Set system prompt for the conversation --provider Comma-separated list of providers for routing @@ -69,6 +71,7 @@ Examples: ask "Write a hello world in Python" ask -g "Explain quantum computing" ask -m openai/gpt-4o "What is 2+2?" + ask -x "Create a backup of my home directory" echo "Fix this code" | ask ask --system "You are a pirate" "Tell me about sailing" @@ -89,6 +92,9 @@ while [ $# -gt 0 ]; do -r) NO_SYSTEM=true shift ;; + -x) + EXECUTE=true + shift ;; --stream) STREAMING=true shift ;; @@ -148,7 +154,8 @@ echo # Make API request if [ "$STREAMING" = true ]; then - # Streaming mode + # Streaming mode - use temp file to capture content for execution + temp_file=$(mktemp) curl -sS "$API_URL" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENROUTER_API_KEY" \ @@ -165,10 +172,17 @@ if [ "$STREAMING" = true ]; then [ "$json" = "" ] || [ "$json" = "[DONE]" ] && continue content=$(echo "$json" | jq -r '.choices[0].delta.content // ""' 2>/dev/null) - [ -n "$content" ] && printf '%s' "$content" + if [ -n "$content" ]; then + printf '%s' "$content" + printf '%s' "$content" >> "$temp_file" + fi fi done echo + + # Read captured content for potential execution + response_content=$(cat "$temp_file") + rm -f "$temp_file" # Show metadata ELAPSED=$(printf "%.2f" $(echo "$(date +%s.%N) - $START_TIME" | bc)) @@ -188,7 +202,8 @@ else fi # Extract and print content - echo "$response" | jq -r '.choices[0].message.content // "No response received"' + response_content=$(echo "$response" | jq -r '.choices[0].message.content // "No response received"') + echo "$response_content" # Show metadata ELAPSED=$(printf "%.2f" $(echo "$(date +%s.%N) - $START_TIME" | bc)) @@ -199,3 +214,25 @@ else echo echo "[$MODEL via $PROVIDER - ${ELAPSED}s - ${TPS} tok/s]" >&2 fi + +# Interactive execution if enabled +if [ "$EXECUTE" = true ]; then + echo >&2 + echo -n "Execute this command? (y/n): " >&2 + read -r answer + case "$answer" in + [Yy]|[Yy][Ee][Ss]) + echo "Executing..." >&2 + echo + # Execute the command using captured response content + if [ -n "$response_content" ]; then + eval "$response_content" + else + echo "Error: No command to execute" >&2 + fi + ;; + *) + echo "Command not executed." >&2 + ;; + esac +fi