-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-completions.sh
116 lines (98 loc) · 3.33 KB
/
bash-completions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# Cloud endpoint URL
CLOUD_ENDPOINT="http://localhost:8000/chat"
API_KEY="****"
# Prompt for token at the time of sourcing this script
echo -n "Enter API key: "
read -s API_KEY
echo
echo "Token set for session."
# Base function to get completion using curl
# TODO: Look at replacing the uggly HEREDOC EOF
_fetch_completions() {
# Prepare the JSON payload
read -r -d '' PAYLOAD <<EOF
{
"user_message": "$1"
}
EOF
# Make the POST request
curl -s -X POST "$CLOUD_ENDPOINT" \
-H "Content-Type: application/json" \
-H "x-api-key: $API_KEY" \
-d "$PAYLOAD"
}
# Function to fetch completions from the cloud using the token
_fetch_completions_from_cloud() {
local cur=${COMP_WORDS[*]:1} # Get all words typed so far, excluding the command itself
# Make a request to the cloud endpoint. Adjust this as needed for your API.
# Here, we're sending the current command line input as a query parameter.
_f_completion "$cur"
}
# The actual completion function
_f_completion() {
# Fetch completion suggestions from the cloud using the pre-entered token
local completions=$(_fetch_completions_from_cloud)
# Use the response to generate completion options
# This assumes the cloud endpoint returns a space-separated list of suggestions
COMPREPLY=( $(compgen -W "$completions" -- "${COMP_WORDS[COMP_CWORD]}") )
}
# Register the completion function for the command `f`
complete -F _f_completion f
ai() {
case "$1" in
login)
echo "Authenticating against the AI endpoint..."
# Authentication logic goes here
;;
aug|augment)
echo "Enter command to augment: "
read cmd_to_augment
echo "Augmenting command: $cmd_to_augment"
# Send $cmd_to_augment to backend for augmentation
_fetch_completions "$cmd_to_augment" >> augment_history.txt
;;
ask)
echo "Enter your question: "
read question
echo "Asking: $question"
# Send $question to backend and display the answer
;;
run)
if [[ -n "$2" ]]; then
echo "Running command from augment history: $2"
# Logic to execute the command from augment history
else
echo "No command specified."
fi
;;
*)
echo "Usage: ai [login|augment|ask|run]"
;;
esac
}
_ai_completion() {
local current=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
case "$prev" in
ai)
local opts="login aug augment ask run"
COMPREPLY=($(compgen -W "${opts}" -- ${current}))
;;
run)
# Dynamically generate completion options based on the augment history file
local IFS=$'\n' # Change IFS to newline for accurate word splitting
local commands=()
while IFS= read -r line; do
commands+=("$line")
done < "augment_history.txt"
# Use compgen to match against the array of commands
COMPREPLY=($(compgen -W "${commands[*]}" -- "$current"))
;;
*)
COMPREPLY=()
;;
esac
}
# Register the completion function
complete -F _ai_completion ai