-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-airole
More file actions
executable file
·172 lines (139 loc) · 4.49 KB
/
get-airole
File metadata and controls
executable file
·172 lines (139 loc) · 4.49 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bash
# Copies my AI Prompts from local directory into the clipboard
# Environment setup
# -----------------------------------------------------------------------------
set -o pipefail # set -o errexit hides errors, don't use it
[[ ${DEBUG-} ]] && set -o xtrace
SCRIPT_DIR="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)"
[[ ":${PATH}:" != *:"${SCRIPT_DIR}":* ]] && export PATH="${SCRIPT_DIR}:${PATH}"
source "${SCRIPT_DIR}/bash_modules/terminal.sh"
source "${SCRIPT_DIR}/bash_modules/desktop.sh"
[[ -z ${BASH_MODULES_DIR-} ]] && echo "ERROR: terminal.sh module missing" && exit 1
function print_usage() {
cat <<EOF
Usage: $(basename "$0") [file_filter] [-i|--include-initial-response] [-h]
Copies Grant Carthew's AI role prompts from local directory into the
clipboard or a specified file.
Repository: https://github.com/grantcarthew/scripts/tree/main
Dependencies:
rg For regex matching
fzf For interactive file selection
OS Dependent:
- For copying content to the clipboard
- Linux: xclip or xsel
- macOS: pbcopy
Optional arguments:
file_filter The file name or part thereof
-i, --include-initial-response Include the initial response template
-h, --help Show this help message and exit
EOF
}
file_filter=""
include_initial_response=false
for arg in "$@"; do
case "$arg" in
-i|--include-initial-response)
include_initial_response=true
;;
-h|--help)
print_usage
exit 0
;;
*)
if [[ -z "$file_filter" ]]; then
file_filter="$arg"
else
log_error "ERROR: Too many arguments"
print_usage
exit 1
fi
;;
esac
done
function ctrlc_trap() {
log_newline
log_warning "Script interrupted. Exiting."
exit 130
}
trap ctrlc_trap SIGINT
# Title and Dependency Checks
# -----------------------------------------------------------------------------
log_title "Grant Carthew's Local AI Prompts"
dependencies=(rg fzf)
for cmd in "${dependencies[@]}"; do
if ! command -v "${cmd}" >/dev/null; then
log_error "ERROR: Missing dependency - '${cmd}'"
exit 1
fi
done
# Load Local Prompts
# -----------------------------------------------------------------------------
log_heading "Loading Local Prompts"
local_path="${SCRIPT_DIR}/agents/roles"
if [[ ! -d "${local_path}" ]]; then
log_error "ERROR: Local AI roles directory not found at '${local_path}'"
exit 1
fi
log_message "Loading from: ${local_path}"
readarray -t files < <(find "${local_path}" -name "*.md" -not -name "README.md" -exec basename {} \; | sort)
if [[ -n "$file_filter" ]]; then
log_message "Filtering for: ${file_filter}"
readarray -t files < <(printf '%s\n' "${files[@]}" | rg -i "$file_filter")
fi
# File Selection
# -----------------------------------------------------------------------------
log_heading "File Selection"
case ${#files[@]} in
0)
log_error "No files match your filter."
exit 1
;;
1)
selected_file="${files[0]}"
log_success "File: ${selected_file}"
;;
*)
log_message "Select a file using fzf (use arrow keys, type to filter, Enter to select):"
selected_file=$(printf '%s\n' "${files[@]}" | fzf --height 40% --layout=reverse --border)
if [[ -z "${selected_file}" ]]; then
log_error "No file selected. Operation canceled."
exit 1
fi
log_success "Selected: ${selected_file}"
;;
esac
# Load Prompt Content
# -----------------------------------------------------------------------------
log_heading "Loading Prompt"
local_file="${local_path}/${selected_file}"
log_message "Loading from: ${local_file}"
if [[ ! -f "${local_file}" ]]; then
log_error "ERROR: File not found at '${local_file}'"
exit 1
fi
raw_content="$(cat "${local_file}")"
if [[ -z "${raw_content}" ]]; then
log_error "ERROR: Failed to read content from '${local_file}'"
exit 1
fi
log_message "Prompt title: $(echo "${raw_content}" | rg -m 1 '^#')"
if [[ "$include_initial_response" == true ]]; then
log_message "Adding initial response template"
raw_content+="$(
cat <<EOT
## Initial Response
Respond only once to this message with "I am an expert in {{subject}}, let's get working!"
EOT
)"
fi
# Output Result
# -----------------------------------------------------------------------------
# If we are outputting to a terminal, copy to clipboard.
if [[ -t 1 ]]; then
send_to_clipboard "${raw_content}"
log_success "File contents copied to clipboard!"
else
echo -n "${raw_content}"
log_success "File contents written to stdout!"
fi
log_done