diff --git a/data/scripts/acme/AddTests b/data/scripts/acme/AddTests index e97980f..2bde558 100755 --- a/data/scripts/acme/AddTests +++ b/data/scripts/acme/AddTests @@ -22,18 +22,20 @@ if [[ -z "$selection" ]]; then exit 1 fi +# Ensure acme session/agent exists +eval $(acme-ollie-ensure) + # Build location string location="" if [[ -n "$filename" && "$filename" != *"+Errors" && "$filename" != "+*" ]]; then location=" in \`$filename\`" fi -# Build the prompt +# Build and send the prompt prompt="Write unit tests for this code${location}: \`\`\` $selection \`\`\`" -# Send to ollie echo "$prompt" | o prompt diff --git a/data/scripts/acme/AskFile b/data/scripts/acme/AskFile index 1f64e1d..8efe10c 100755 --- a/data/scripts/acme/AskFile +++ b/data/scripts/acme/AskFile @@ -19,8 +19,9 @@ if [[ -z "$filename" || "$filename" == *"+Errors" || "$filename" == "+*" ]]; the exit 1 fi -# Build the prompt -prompt="Describe and explain this file: $filename" +# Ensure acme session/agent exists +eval $(acme-ollie-ensure) -# Send to ollie via the o script +# Build and send the prompt +prompt="Describe and explain this file: $filename" echo "$prompt" | o prompt diff --git a/data/scripts/acme/Document b/data/scripts/acme/Document index ccd58d9..86096e5 100755 --- a/data/scripts/acme/Document +++ b/data/scripts/acme/Document @@ -22,18 +22,20 @@ if [[ -z "$selection" ]]; then exit 1 fi +# Ensure acme session/agent exists +eval $(acme-ollie-ensure) + # Build location string location="" if [[ -n "$filename" && "$filename" != *"+Errors" && "$filename" != "+*" ]]; then location=" in \`$filename\`" fi -# Build the prompt +# Build and send the prompt prompt="Add documentation comments to this code${location}: \`\`\` $selection \`\`\`" -# Send to ollie echo "$prompt" | o prompt diff --git a/data/scripts/acme/Explain b/data/scripts/acme/Explain index d64ceef..0593f2c 100755 --- a/data/scripts/acme/Explain +++ b/data/scripts/acme/Explain @@ -2,7 +2,6 @@ # Explain - Ask ollie to explain the selected code in acme # # Usage: Select code, then middle-click Explain in the tag -# Sends: "Explain this code (from ):\n\n```\n\n```" set -euo pipefail @@ -14,7 +13,7 @@ fi # Get the window name (filename) from the tag filename=$(9p read acme/$winid/tag | awk '{print $1}') -# Read the current selection by setting addr to dot and reading xdata +# Read the current selection echo 'addr=dot' | 9p write acme/$winid/ctl selection=$(9p read acme/$winid/xdata 2>/dev/null || true) @@ -23,18 +22,20 @@ if [[ -z "$selection" ]]; then exit 1 fi +# Ensure acme session/agent exists +eval $(acme-ollie-ensure) + # Build context string context="" if [[ -n "$filename" && "$filename" != *"+Errors" && "$filename" != "+*" ]]; then context=" (from $filename)" fi -# Build the prompt +# Build and send the prompt prompt="Explain this code${context}: \`\`\` $selection \`\`\`" -# Send to ollie echo "$prompt" | o prompt diff --git a/data/scripts/acme/Fix b/data/scripts/acme/Fix index d8ad140..13187c7 100755 --- a/data/scripts/acme/Fix +++ b/data/scripts/acme/Fix @@ -22,18 +22,20 @@ if [[ -z "$selection" ]]; then exit 1 fi +# Ensure acme session/agent exists +eval $(acme-ollie-ensure) + # Build location string location="" if [[ -n "$filename" && "$filename" != *"+Errors" && "$filename" != "+*" ]]; then location=" in \`$filename\`" fi -# Build the prompt +# Build and send the prompt prompt="Fix this code${location}: \`\`\` $selection \`\`\`" -# Send to ollie echo "$prompt" | o prompt diff --git a/data/scripts/acme/Refactor b/data/scripts/acme/Refactor index faeb35a..967821e 100755 --- a/data/scripts/acme/Refactor +++ b/data/scripts/acme/Refactor @@ -22,18 +22,20 @@ if [[ -z "$selection" ]]; then exit 1 fi +# Ensure acme session/agent exists +eval $(acme-ollie-ensure) + # Build location string location="" if [[ -n "$filename" && "$filename" != *"+Errors" && "$filename" != "+*" ]]; then location=" in \`$filename\`" fi -# Build the prompt +# Build and send the prompt prompt="Refactor this code${location}: \`\`\` $selection \`\`\`" -# Send to ollie echo "$prompt" | o prompt diff --git a/data/scripts/acme/SendVerbatim b/data/scripts/acme/SendVerbatim index b7639e8..3becbfd 100755 --- a/data/scripts/acme/SendVerbatim +++ b/data/scripts/acme/SendVerbatim @@ -2,7 +2,6 @@ # SendVerbatim - Send the selected text to ollie as-is (wrapped in code fence) # # Usage: Select text, then middle-click SendVerbatim in the tag -# The selection is sent wrapped in a code fence with language detection set -euo pipefail @@ -23,6 +22,9 @@ if [[ -z "$selection" ]]; then exit 1 fi +# Ensure acme session/agent exists +eval $(acme-ollie-ensure) + # Detect language from file extension lang="" if [[ -n "$filename" && "$filename" != *"+Errors" && "$filename" != "+*" ]]; then @@ -55,10 +57,9 @@ if [[ -n "$filename" && "$filename" != *"+Errors" && "$filename" != "+*" ]]; the esac fi -# Build the prompt - just the code fence with selection +# Build and send the prompt prompt="\`\`\`${lang} $selection \`\`\`" -# Send to ollie echo "$prompt" | o prompt diff --git a/data/scripts/acme/acme-ollie-ensure b/data/scripts/acme/acme-ollie-ensure new file mode 100755 index 0000000..9f9ec97 --- /dev/null +++ b/data/scripts/acme/acme-ollie-ensure @@ -0,0 +1,50 @@ +#!/bin/bash +# acme-ollie-ensure - Ensure acme session and agent exist +# +# Creates session "acme" if needed, creates/selects agent based on cwd hash. +# Outputs the agent path for use by other scripts. +# +# Usage: eval $(acme-ollie-ensure) +# Then use $OLLIE_AGENT_PATH + +set -euo pipefail + +SESSION="acme" + +# Get cwd from the acme window's directory context +if [[ -n "${winid:-}" ]]; then + # Get window name and extract directory + wname=$(9p read acme/$winid/tag | awk '{print $1}') + if [[ -d "$wname" ]]; then + cwd="$wname" + elif [[ -f "$wname" ]]; then + cwd=$(dirname "$wname") + else + cwd="$HOME" + fi +else + cwd="$PWD" +fi + +# Create a short hash from the cwd for the agent name +# Use first 8 chars of md5sum for uniqueness +agent_hash=$(echo -n "$cwd" | md5sum | cut -c1-8) +AGENT="$agent_hash" + +# Ensure session exists +if ! ollie-9p read "session/$SESSION/id" >/dev/null 2>&1; then + printf 'name=%s\ncwd=%s\n' "$SESSION" "$cwd" | ollie-9p write "session/new" 2>/dev/null || true +fi + +# Ensure agent exists +if ! ollie-9p read "session/$SESSION/agent/$AGENT/id" >/dev/null 2>&1; then + printf 'name=%s\ncwd=%s\n' "$AGENT" "$cwd" | ollie-9p write "session/$SESSION/agent/new" 2>/dev/null || true +else + # Update cwd if agent already exists + echo "cwd $cwd" | ollie-9p write "session/$SESSION/agent/$AGENT/ctl" 2>/dev/null || true +fi + +# Output for eval +echo "export session=$SESSION" +echo "export agent=$AGENT" +echo "export OLLIE_AGENT_PATH=session/$SESSION/agent/$AGENT" diff --git a/justfile b/justfile index befab8e..5830d35 100644 --- a/justfile +++ b/justfile @@ -189,6 +189,7 @@ install-data: install -m755 data/scripts/logseq-cli {{bin}}/logseq-cli install -m755 data/scripts/o {{bin}}/o # Acme scripts + install -m755 data/scripts/acme/acme-ollie-ensure {{bin}}/acme-ollie-ensure install -m755 data/scripts/acme/AskFile {{bin}}/AskFile install -m755 data/scripts/acme/Explain {{bin}}/Explain install -m755 data/scripts/acme/Document {{bin}}/Document @@ -214,7 +215,7 @@ uninstall: rm -f {{bin}}/Ollie {{bin}}/Kmpl {{bin}}/Oi {{bin}}/Theo rm -f {{bin}}/ollie-remount {{bin}}/o rm -f {{bin}}/prompt {{bin}}/ollieread {{bin}}/sendctl {{bin}}/ollie-ls - rm -f {{bin}}/AskFile {{bin}}/Explain {{bin}}/Document {{bin}}/Fix {{bin}}/Refactor {{bin}}/AddTests {{bin}}/SendVerbatim + rm -f {{bin}}/AskFile {{bin}}/Explain {{bin}}/Document {{bin}}/Fix {{bin}}/Refactor {{bin}}/AddTests {{bin}}/SendVerbatim {{bin}}/acme-ollie-ensure rm -rf {{cfg}} # Service files rm -f ~/.config/systemd/user/ollie.service @@ -232,7 +233,7 @@ uninstall-kf5: rm -f {{bin}}/Ollie {{bin}}/Kmpl {{bin}}/Oi {{bin}}/Theo rm -f {{bin}}/ollie-remount {{bin}}/o rm -f {{bin}}/prompt {{bin}}/ollieread {{bin}}/sendctl {{bin}}/ollie-ls - rm -f {{bin}}/AskFile {{bin}}/Explain {{bin}}/Document {{bin}}/Fix {{bin}}/Refactor {{bin}}/AddTests {{bin}}/SendVerbatim + rm -f {{bin}}/AskFile {{bin}}/Explain {{bin}}/Document {{bin}}/Fix {{bin}}/Refactor {{bin}}/AddTests {{bin}}/SendVerbatim {{bin}}/acme-ollie-ensure rm -rf {{cfg}} # Service files rm -f ~/.config/systemd/user/ollie.service