-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcopresenter
More file actions
executable file
·270 lines (227 loc) · 7.41 KB
/
copresenter
File metadata and controls
executable file
·270 lines (227 loc) · 7.41 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env bash
set -euo pipefail
# Directory for storing cached outputs
CACHE_DIR=".copresenter"
SESSION_FILE="$CACHE_DIR/session"
SLIDES_FILE="slides.md"
COPRESENTER_MD="COPRESENTER.md"
# Initialize cache directory
mkdir -p "$CACHE_DIR"
# Function to compute hash of prompt
hash_prompt() {
echo -n "$1" | shasum -a 256 | cut -d' ' -f1
}
# Function to simulate typing output word by word
simulate_typing() {
local text="$1"
local word
# Read word by word and print with random delays
{
echo "$text" | while read -r line; do
for word in $line; do
echo -n "$word "
# Random delay between 0.01 and 0.04 seconds (much faster)
sleep "0.0$(( RANDOM % 4 + 1 ))"
done
echo # newline at end of line
done
} | bat --force-colorization --paging=never --language=markdown --plain
}
# Function to extract slides content up to the exec block containing the prompt
extract_slides_context() {
local prompt="$1"
local in_exec_block=0
local result=""
while IFS= read -r line; do
result+="$line"$'\n'
# Check if we're entering an exec block
if [[ "$line" =~ ^\`\`\`bash.*\+exec ]]; then
in_exec_block=1
continue
fi
# If we're in an exec block, check if it contains our copresenter call
if [[ $in_exec_block -eq 1 ]]; then
if [[ "$line" =~ ^\`\`\` ]]; then
in_exec_block=0
elif [[ "$line" =~ \./copresenter ]]; then
# Found the copresenter call, return everything up to here
echo "$result"
return 0
fi
fi
done < "$SLIDES_FILE"
# If we didn't find a matching exec block, return all slides
echo "$result"
}
# Function to call Claude Code
call_claude() {
local new_session="$1"
local prompt="$2"
local slides_context="$3"
# Build the full prompt
local full_prompt
full_prompt="$(cat "$COPRESENTER_MD")"$'\n\n'
full_prompt+="## Slides Context"$'\n\n'
full_prompt+="$slides_context"$'\n\n'
full_prompt+="## Presenter's Request"$'\n\n'
full_prompt+="$prompt"
local claude_args=("-p" "--output-format" "json")
if [[ "$new_session" == "true" ]]; then
# New session - don't add resume
:
else
# Resume existing session if we're not forcing new
if [[ -f "$SESSION_FILE" ]]; then
local session_id
session_id=$(cat "$SESSION_FILE")
claude_args+=("--resume" "$session_id")
fi
fi
# Call claude and capture JSON output
local json_output
json_output=$(claude "${claude_args[@]}" "$full_prompt" 2>&1)
# Extract session ID and result from JSON using jq
local session_id result
if command -v jq &> /dev/null; then
# Use jq if available
session_id=$(echo "$json_output" | jq -r '.session_id // empty')
result=$(echo "$json_output" | jq -r '.result // empty')
else
# Fallback to python if jq is not available
session_id=$(echo "$json_output" | python3 -c 'import sys, json; data=json.load(sys.stdin); print(data.get("session_id", ""))' 2>/dev/null || echo "")
result=$(echo "$json_output" | python3 -c 'import sys, json; data=json.load(sys.stdin); print(data.get("result", ""))' 2>/dev/null || echo "")
fi
# Save the session ID for future use
if [[ -n "$session_id" ]]; then
echo "$session_id" > "$SESSION_FILE"
fi
echo "$result"
}
# Function to handle --prepare mode
prepare_mode() {
echo "Preparing cached responses for all copresenter calls in slides..."
# Extract all copresenter calls from slides
local calls=()
local in_exec_block=0
while IFS= read -r line; do
if [[ "$line" =~ ^\`\`\`bash.*\+exec ]]; then
in_exec_block=1
continue
fi
if [[ $in_exec_block -eq 1 ]]; then
if [[ "$line" =~ ^\`\`\` ]]; then
in_exec_block=0
elif [[ "$line" =~ \./copresenter[[:space:]]+(.+) ]]; then
# Extract the prompt (everything after ./copresenter, excluding --new if present)
local call="${BASH_REMATCH[1]}"
# Remove --new flag if present
call="${call//--new /}"
call="${call//--new/}"
call=$(echo "$call" | xargs) # trim whitespace
if [[ -n "$call" ]]; then
calls+=("$call")
fi
fi
fi
done < "$SLIDES_FILE"
echo "Found ${#calls[@]} copresenter call(s)"
# Process each call
local count=0
for prompt in "${calls[@]}"; do
((count++))
echo "[$count/${#calls[@]}] Processing: $prompt"
local hash
hash=$(hash_prompt "$prompt")
local cache_file="$CACHE_DIR/$hash"
if [[ -f "$cache_file" ]]; then
echo " → Already cached, skipping"
else
echo " → Generating response..."
local slides_context
slides_context=$(extract_slides_context "$prompt")
local output
output=$(call_claude "true" "$prompt" "$slides_context")
echo "$output" > "$cache_file"
echo " → Cached to $hash"
fi
done
echo "Preparation complete!"
}
# Function to handle --clear mode
clear_mode() {
echo "Clearing cached copresenter responses..."
rm -f "$CACHE_DIR"/*
echo "Cache cleared!"
}
# Main script logic
main() {
local new_session=false
local prepare=false
local clear=false
local prompt=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--new)
new_session=true
shift
;;
--prepare)
prepare=true
shift
;;
--clear)
clear=true
shift
;;
*)
if [[ -z "$prompt" ]]; then
prompt="$1"
else
prompt="$prompt $1"
fi
shift
;;
esac
done
# Handle special modes
if [[ "$clear" == "true" ]]; then
clear_mode
exit 0
fi
if [[ "$prepare" == "true" ]]; then
prepare_mode
exit 0
fi
# Normal mode: need a prompt
if [[ -z "$prompt" ]]; then
echo "Error: No prompt provided" >&2
echo "Usage: $0 [--new] <prompt>" >&2
echo " $0 --prepare" >&2
echo " $0 --clear" >&2
exit 1
fi
# Check if we have a cached response
local hash
hash=$(hash_prompt "$prompt")
local cache_file="$CACHE_DIR/$hash"
if [[ -f "$cache_file" ]]; then
# Return cached response with simulated typing
simulate_typing "$(cat "$cache_file")"
exit 0
fi
# No cached response, generate new one
# If --new flag is set, clear the session file to start fresh
if [[ "$new_session" == "true" ]]; then
rm -f "$SESSION_FILE"
fi
local slides_context
slides_context=$(extract_slides_context "$prompt")
local output
output=$(call_claude "$new_session" "$prompt" "$slides_context")
# Cache the output
echo "$output" > "$cache_file"
# Return the output with bat highlighting
echo "$output" | bat --force-colorization --paging=never --language=markdown --plain
}
main "$@"