-
-
Save tyjak/f384e52142920b5d3954fa3096c222d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Interactive CLI for tgpt that sanitizes filenames, handles provider-specific | |
| # constraints, renders markdown responses with glow, and saves entries to a | |
| # categorized notes directory. | |
| # GistID: f384e52142920b5d3954fa3096c222d1 | |
| max=30 | |
| half=$((max / 2)) | |
| target_dir="$HOME/share/personal_notes/ia/gpt-cli" | |
| provider="pollinations" # Default provider | |
| # Ensure directory exists | |
| mkdir -p "$target_dir" | |
| echo -n "Enter prompt: " | |
| read -r prompt | |
| # Allow overriding the provider/model with /model <name> | |
| if [[ "$prompt" =~ ^/model\ ([^[:space:]]+)\ (.*) ]]; then | |
| provider="${BASH_REMATCH[1]}" | |
| prompt="${BASH_REMATCH[2]}" | |
| fi | |
| # Prepare the prompt and arguments | |
| tgpt_args=("-q" "--provider" "$provider") | |
| tgpt_prompt="$prompt" | |
| if [ "$provider" = "pollinations" ]; then | |
| # Inject instructions directly into the prompt for reliability | |
| preprompt="Provide a dense, high-information response. Prioritize essential details and use a concise writing style. Prefer bulleted lists over tables for structure; use tables only if absolutely necessary for complex data comparisons. Crucially, ensure your entire response is complete and concludes fully within 3,000 tokens to prevent truncation." | |
| tgpt_prompt="[SYSTEM INSTRUCTION: $preprompt]\n\nUSER PROMPT: $prompt" | |
| fi | |
| # Create a safe filename slug | |
| clean=$(echo -n "$prompt" | iconv -f UTF-8 -t ASCII//TRANSLIT | tr -dc '[:alnum:] ' | tr ' ' '-' | sed 's/-\{2,\}/-/g' | sed 's/^-//;s/-$//') | |
| if [ ${#clean} -le $max ]; then | |
| slug="$clean" | |
| else | |
| slug="${clean:0:$half}---${clean: -$half}" | |
| fi | |
| filename="$(date -I)-$slug.md" | |
| filepath="$target_dir/$filename" | |
| tmp_res="/tmp/tgpt_response.md" | |
| # Debug info | |
| if [ "$DEBUG" = "1" ]; then | |
| echo -e "\n--- DEBUG INFO ---" | |
| echo "Provider: $provider" | |
| if [ "$provider" = "pollinations" ]; then | |
| echo -e "System Instruction: $preprompt" | |
| fi | |
| echo "Slug: $slug" | |
| echo "File: $filepath" | |
| echo "------------------\n" | |
| fi | |
| echo "Thinking (Provider: $provider)..." | |
| # We use echo -e to ensure \n are interpreted in the tgpt_prompt | |
| echo -e "$tgpt_prompt" | tgpt "${tgpt_args[@]}" > "$tmp_res" | |
| # Check if we got anything | |
| if [ ! -s "$tmp_res" ]; then | |
| echo "Error: Received empty response from provider." | |
| exit 1 | |
| fi | |
| # Show the response in paged view | |
| { | |
| echo "# Prompt: $prompt" | |
| echo -e "\n---\n" | |
| cat "$tmp_res" | |
| } | glow -p | |
| # Ask for confirmation | |
| echo -n "Save to file? (y/N): " | |
| read -r confirm | |
| if [[ "$confirm" =~ ^[yY]$ ]]; then | |
| { | |
| echo "Prompt: $prompt" | |
| echo -e "\n---\n" | |
| cat "$tmp_res" | |
| } > "$filepath" | |
| echo "Saved to $filepath" | |
| else | |
| echo "Discarded." | |
| fi | |
| # Cleanup | |
| rm -f "$tmp_res" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment