-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCONTEXT_COMMANDS_IMPLEMENTATION.sh
More file actions
591 lines (490 loc) · 17.5 KB
/
Copy pathCONTEXT_COMMANDS_IMPLEMENTATION.sh
File metadata and controls
591 lines (490 loc) · 17.5 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#!/usr/bin/env bash
# CONTEXT_COMMANDS_IMPLEMENTATION.sh
# Sample command implementations for context persistence
# These functions should be added to the main 'aiwb' script
# Source the context state library
# Add this near the top of aiwb script with other library sources:
# source "${AIWB_LIB_DIR}/context_state.sh"
# ============================================================================
# COMMAND: /contextload
# Load saved context into current session
# ============================================================================
cmd_contextload() {
local workspace
workspace=$(get_workspace)
if ! context_state_exists; then
echo "❌ No saved context found"
echo ""
echo "Tip: Run /scanrepo or /smartscan to build context first"
return 1
fi
# Check age and warn if stale
context_state_check_age
# Show what will be loaded
echo ""
context_state_show
echo ""
# Prompt to confirm
if command -v gum &>/dev/null; then
if ! gum confirm "Load this context into current session?"; then
echo "Cancelled"
return 0
fi
else
read -p "Load this context into current session? [Y/n] " -r
if [[ $REPLY =~ ^[Nn] ]]; then
echo "Cancelled"
return 0
fi
fi
# Load context files into MODE_UPLOADS
context_state_load_into_mode
echo ""
echo "✅ Context loaded. Use /make, /debug, or /tweak to use it."
}
# ============================================================================
# COMMAND: /contextsave
# Save current context (MODE_UPLOADS) to state file
# ============================================================================
cmd_contextsave() {
if [[ ${#MODE_UPLOADS[@]} -eq 0 ]]; then
echo "❌ No files in context to save"
echo ""
echo "Tip: Use /context to add files, or run /scanrepo first"
return 1
fi
# Save MODE_UPLOADS to context state
context_state_save_from_mode
# Show summary
echo ""
context_state_show
}
# ============================================================================
# COMMAND: /contextshow
# Display current context state
# ============================================================================
cmd_contextshow() {
if ! context_state_exists; then
echo "❌ No active context state"
echo ""
echo "Tip: Run /scanrepo or add files with /context to create context"
return 1
fi
context_state_show
# Also check size
echo ""
context_state_check_size
}
# ============================================================================
# COMMAND: /contextclear
# Clear all context and start fresh
# ============================================================================
cmd_contextclear() {
if ! context_state_exists && [[ ${#MODE_UPLOADS[@]} -eq 0 ]]; then
echo "❌ No context to clear"
return 0
fi
# Confirm before clearing
echo "⚠️ This will clear:"
echo " • All context files"
echo " • Conversation history"
echo " • Scan results"
echo ""
if command -v gum &>/dev/null; then
if ! gum confirm "Clear all context?"; then
echo "Cancelled"
return 0
fi
else
read -p "Clear all context? [y/N] " -r
if [[ ! $REPLY =~ ^[Yy] ]]; then
echo "Cancelled"
return 0
fi
fi
# Clear context state
context_state_clear
# Clear MODE_UPLOADS
MODE_UPLOADS=()
# Clear mode settings
MODE_PROMPT=""
MODE_INSTRUCT_FILE=""
echo "✅ All context cleared"
}
# ============================================================================
# COMMAND: /contextrefresh
# Re-scan repository and update context
# ============================================================================
cmd_contextrefresh() {
echo "🔄 Refreshing context..."
echo ""
# Detect current scan type from context state
local scan_type="full"
if context_state_exists; then
scan_type=$(context_state_get "last_scan.type")
if [[ -z "$scan_type" || "$scan_type" == "null" ]]; then
scan_type="full"
fi
fi
echo "Previous scan type: $scan_type"
echo ""
# Prompt for scan type
if command -v gum &>/dev/null; then
scan_type=$(gum choose "full" "selective" --header "Choose scan type:")
else
echo "Choose scan type:"
echo " 1) full - Scan entire repository"
echo " 2) selective - Scan key files only"
read -p "Enter choice [1-2]: " -r choice
case "$choice" in
1) scan_type="full" ;;
2) scan_type="selective" ;;
*) scan_type="full" ;;
esac
fi
# Clear old context first
context_state_clear
MODE_UPLOADS=()
# Run appropriate scan
if [[ "$scan_type" == "selective" ]]; then
cmd_smartscan
else
cmd_scanrepo
fi
echo ""
echo "✅ Context refreshed"
context_state_show
}
# ============================================================================
# COMMAND: /contextremove
# Remove specific file from context
# ============================================================================
cmd_contextremove() {
if ! context_state_exists; then
echo "❌ No context to remove from"
return 1
fi
# Get list of context files
local files
mapfile -t files < <(context_state_list_files)
if [[ ${#files[@]} -eq 0 ]]; then
echo "❌ No files in context"
return 1
fi
# Interactive selection
local file_to_remove
if command -v gum &>/dev/null; then
file_to_remove=$(printf '%s\n' "${files[@]}" | gum filter --placeholder "Select file to remove")
else
echo "Files in context:"
local i=1
for file in "${files[@]}"; do
echo " $i) $file"
((i++))
done
echo ""
read -p "Enter number to remove: " -r choice
file_to_remove="${files[$((choice-1))]}"
fi
if [[ -z "$file_to_remove" ]]; then
echo "Cancelled"
return 0
fi
# Remove from context state
context_state_remove_file "$file_to_remove"
# Also remove from MODE_UPLOADS if present
local new_uploads=()
for file in "${MODE_UPLOADS[@]}"; do
if [[ "$file" != "$file_to_remove" ]]; then
new_uploads+=("$file")
fi
done
MODE_UPLOADS=("${new_uploads[@]}")
echo "✅ Removed: $file_to_remove"
}
# ============================================================================
# MODIFIED: cmd_scanrepo
# Add context persistence after scan
# ============================================================================
cmd_scanrepo_with_persistence() {
# ... existing scanrepo code ...
# After the scan completes and output file is created:
local output_file="$WORKSPACE/outputs/repo_analysis_$(date +%Y%m%d_%H%M%S).md"
local file_count=127 # This would be the actual count from build_repo_context
# Save scan result to context state
context_state_save_scan "full" "$output_file" "$file_count"
echo ""
echo "✅ Scan saved to context state"
echo " Run /contextshow to view details"
}
# ============================================================================
# MODIFIED: cmd_smartscan
# Add context persistence after scan
# ============================================================================
cmd_smartscan_with_persistence() {
# ... existing smartscan code ...
# After the scan completes and output file is created:
local output_file="$WORKSPACE/outputs/repo_analysis_$(date +%Y%m%d_%H%M%S).md"
local file_count=42 # This would be the actual count from build_repo_context
# Save scan result to context state
context_state_save_scan "selective" "$output_file" "$file_count"
echo ""
echo "✅ Scan saved to context state"
echo " Run /contextshow to view details"
}
# ============================================================================
# MODIFIED: handle_chat_message
# Add conversation history to context
# ============================================================================
handle_chat_message_with_history() {
local message="$1"
local provider model
provider=$(config_get model_provider)
model=$(config_get model_name)
# Add user message to context history
context_state_add_message "user" "$message"
# Call API (original behavior)
local response
response=$(call_api "$message" "$provider" "$model")
# Add assistant response to context history
context_state_add_message "assistant" "$response"
# Track usage
track_usage "$provider" "$model" "$message" "$response"
echo "$response"
}
# ============================================================================
# NEW: handle_chat_message_with_threading
# Enhanced version with conversation threading (Phase 2)
# ============================================================================
handle_chat_message_with_threading() {
local message="$1"
local provider model
provider=$(config_get model_provider)
model=$(config_get model_name)
# Check if threading is enabled
local threading_enabled
threading_enabled=$(config_get conversation.threading_enabled)
if [[ "$threading_enabled" == "true" ]]; then
# Build conversation payload with history
local conversation_payload
conversation_payload=$(build_conversation_payload "$message")
# Call API with history
local response
response=$(call_api_with_history "$conversation_payload" "$provider" "$model")
# Add to history
context_state_add_message "user" "$message"
context_state_add_message "assistant" "$response"
echo "$response"
else
# Fallback to simple message (backward compatible)
handle_chat_message_with_history "$message"
fi
}
# ============================================================================
# NEW: build_conversation_payload
# Build multi-turn conversation payload for API
# ============================================================================
build_conversation_payload() {
local current_message="$1"
local max_turns
max_turns=$(config_get conversation.max_turns)
max_turns=${max_turns:-10}
# Get conversation history
local history
mapfile -t history < <(context_state_get_history "$max_turns")
# Build payload based on provider
local provider
provider=$(config_get model_provider)
case "$provider" in
gemini)
build_gemini_conversation_payload "$current_message" "${history[@]}"
;;
anthropic|claude)
build_claude_conversation_payload "$current_message" "${history[@]}"
;;
openai|deepseek|groq|openrouter)
build_openai_conversation_payload "$current_message" "${history[@]}"
;;
*)
# Fallback: just return current message
echo "$current_message"
;;
esac
}
# ============================================================================
# NEW: build_gemini_conversation_payload
# Build Gemini-style conversation payload
# ============================================================================
build_gemini_conversation_payload() {
local current_message="$1"
shift
local history=("$@")
# Start building JSON payload
local payload='{"contents":['
# Add history messages
for msg in "${history[@]}"; do
local role content
role=$(echo "$msg" | cut -d':' -f1)
content=$(echo "$msg" | cut -d':' -f2-)
# Map role names (user/assistant -> user/model for Gemini)
if [[ "$role" == "assistant" ]]; then
role="model"
fi
# Escape content for JSON
content=$(echo "$content" | jq -Rs .)
payload+="{\"role\":\"$role\",\"parts\":[{\"text\":$content}]},"
done
# Add current message
local escaped_message
escaped_message=$(echo "$current_message" | jq -Rs .)
payload+="{\"role\":\"user\",\"parts\":[{\"text\":$escaped_message}]}"
payload+=']}'
echo "$payload"
}
# ============================================================================
# NEW: build_claude_conversation_payload
# Build Claude-style conversation payload
# ============================================================================
build_claude_conversation_payload() {
local current_message="$1"
shift
local history=("$@")
local payload='{"messages":['
# Add history messages
for msg in "${history[@]}"; do
local role content
role=$(echo "$msg" | cut -d':' -f1)
content=$(echo "$msg" | cut -d':' -f2-)
content=$(echo "$content" | jq -Rs .)
payload+="{\"role\":\"$role\",\"content\":$content},"
done
# Add current message
local escaped_message
escaped_message=$(echo "$current_message" | jq -Rs .)
payload+="{\"role\":\"user\",\"content\":$escaped_message}"
payload+=']}'
echo "$payload"
}
# ============================================================================
# NEW: build_openai_conversation_payload
# Build OpenAI-style conversation payload
# ============================================================================
build_openai_conversation_payload() {
local current_message="$1"
shift
local history=("$@")
local payload='{"messages":['
# Add history messages
for msg in "${history[@]}"; do
local role content
role=$(echo "$msg" | cut -d':' -f1)
content=$(echo "$msg" | cut -d':' -f2-)
content=$(echo "$content" | jq -Rs .)
payload+="{\"role\":\"$role\",\"content\":$content},"
done
# Add current message
local escaped_message
escaped_message=$(echo "$current_message" | jq -Rs .)
payload+="{\"role\":\"user\",\"content\":$escaped_message}"
payload+=']}'
echo "$payload"
}
# ============================================================================
# MODIFIED: Main script initialization
# Check for existing context on startup
# ============================================================================
main_with_context_check() {
# ... existing initialization code ...
# Check if context state exists from previous session
if context_state_exists; then
local resume_enabled
resume_enabled=$(config_get context_management.resume_on_start)
resume_enabled=${resume_enabled:-prompt}
if [[ "$resume_enabled" == "prompt" ]]; then
# Calculate age
local created_at age_msg
created_at=$(context_state_get "created_at")
if [[ -n "$created_at" ]]; then
local created_epoch now_epoch age_days
created_epoch=$(date -d "$created_at" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%S" "${created_at:0:19}" +%s 2>/dev/null || echo "0")
now_epoch=$(date +%s)
age_days=$(( (now_epoch - created_epoch) / 86400 ))
if [[ $age_days -eq 0 ]]; then
age_msg="today"
elif [[ $age_days -eq 1 ]]; then
age_msg="yesterday"
else
age_msg="$age_days days ago"
fi
echo ""
echo "⚠️ Found context from previous session ($age_msg)"
if command -v gum &>/dev/null; then
if gum confirm "Resume with previous context?"; then
context_state_load_into_mode
echo ""
context_state_show
fi
else
read -p " Resume with previous context? [Y/n] " -r
if [[ ! $REPLY =~ ^[Nn] ]]; then
context_state_load_into_mode
echo ""
context_state_show
fi
fi
echo ""
fi
elif [[ "$resume_enabled" == "true" ]]; then
# Auto-resume
context_state_load_into_mode
context_state_show
echo ""
fi
fi
# ... continue with existing main menu ...
}
# ============================================================================
# COMMAND MAP ADDITIONS
# Add these to the command handling switch statement
# ============================================================================
handle_slash_command_additions() {
local cmd="$1"
case "$cmd" in
/contextload)
cmd_contextload
;;
/contextsave)
cmd_contextsave
;;
/contextshow)
cmd_contextshow
;;
/contextclear)
cmd_contextclear
;;
/contextrefresh)
cmd_contextrefresh
;;
/contextremove)
cmd_contextremove
;;
*)
# ... existing command handling ...
;;
esac
}
# ============================================================================
# HELP TEXT ADDITIONS
# Add to the help command output
# ============================================================================
help_text_context_commands() {
cat <<'HELP'
Context Management:
/contextload Load saved context into current session
/contextsave Save current context to state file
/contextshow Display current context state
/contextclear Clear all context and start fresh
/contextrefresh Re-scan repository and update context
/contextremove Remove specific file from context
HELP
}