66# Enhanced version supporting large prompts, special characters, and model selection
77#
88# Usage:
9- # ./ask_sam.sh < conversation-id> [--model model-name] [--token api-token] [query]
10- # ./ask_sam.sh <conversation-id> -- model gpt-4 'query'
11- # ./ask_sam.sh <conversation-id> --token YOUR-TOKEN 'query'
12- # ./ask_sam.sh <conversation-id> --model gpt-4 < file.txt
13- # echo "query" | ./ask_sam.sh <conversation-id> --model gpt-4
9+ # ./ask_sam.sh [--uuid conversation-id] [--model model-name] [--token api-token] [query]
10+ # ./ask_sam.sh -- model gpt-4 'query' # Auto-generates UUID
11+ # ./ask_sam.sh --uuid ABC123 --token YOUR-TOKEN 'query'
12+ # ./ask_sam.sh --model gpt-4 < file.txt
13+ # echo "query" | ./ask_sam.sh --model gpt-4
1414#
1515# Environment Variables:
1616# SAM_API_TOKEN - API token for authentication (alternative to --token)
1717
18- CONVERSATION_ID=" ${1:- } "
19- MODEL=" gpt-5-mini" # Default model
18+ # Default values
19+ CONVERSATION_ID=" " # Will auto-generate if not provided
20+ MODEL=" gpt-4o-mini" # Default model
2021API_TOKEN=" ${SAM_API_TOKEN:- } " # Read from environment variable if set
2122TEMP_FILE=$( mktemp)
22- trap " rm -f $TEMP_FILE " EXIT
23+ OUTPUT_FILE=$( mktemp)
24+ trap " rm -f $TEMP_FILE $OUTPUT_FILE " EXIT
2325
24- # Check if conversation ID provided
25- if [ -z " $CONVERSATION_ID " ] || [ " $CONVERSATION_ID " = " --help " ]; then
26+ # Check for help flag first
27+ if [ " $1 " = " --help " ] || [ " $1 " = " -h " ]; then
2628 cat << 'USAGE '
27- Usage: ./ask_sam.sh <conversation-id> [--model model-name] [--token api-token ] [query]
29+ Usage: ./ask_sam.sh [OPTIONS ] [query]
2830
2931OPTIONS:
3032--------
31- --model MODEL Specify model to use (default: gpt-4)
32- Examples: gpt-4, gpt-3.5-turbo, llama/model, mlx/model
33+ --uuid UUID Conversation UUID (auto-generated if not provided)
34+ --model MODEL Specify model to use (default: gpt-4o-mini)
35+ Examples: gpt-4o-mini, gpt-4o, claude-3-5-sonnet-20241022, o1-mini
3336--token TOKEN API authentication token (can also use SAM_API_TOKEN env var)
3437 Get token from: SAM Preferences → API Server → API Authentication
3538
3639AUTHENTICATION:
3740---------------
38- SAM now requires authentication for all API requests. You can provide the token via:
41+ SAM requires authentication for all API requests. Provide the token via:
3942
40431. Command line: --token YOUR-TOKEN
41442. Environment variable: export SAM_API_TOKEN="YOUR-TOKEN"
42453. Get your token from SAM Preferences → API Server → Copy button
4346
44- Example: ./ask_sam.sh CA706D86 --token abc123-def456 'Which tools did you use?'
45-
4647METHODS:
4748--------
48- 1. Direct query:
49- ./ask_sam.sh CA706D86 --token abc123 'Which tools did you use?'
50- ./ask_sam.sh CA706D86 --model gpt-3.5-turbo --token abc123 'Quick test'
49+ 1. Auto-generated conversation (recommended):
50+ ./ask_sam.sh --token abc123 'Which tools did you use?'
51+ ./ask_sam.sh --model gpt-4o --token abc123 'Quick test'
52+
53+ 2. Specific conversation UUID:
54+ ./ask_sam.sh --uuid CA706D86 --token abc123 'Follow-up question'
5155
52- 2 . From file:
53- SAM_API_TOKEN=abc123 ./ask_sam.sh CA706D86 < prompt.txt
54- ./ask_sam.sh CA706D86 --model gpt-4 --token abc123 < analysis.txt
56+ 3 . From file:
57+ SAM_API_TOKEN=abc123 ./ask_sam.sh < prompt.txt
58+ ./ask_sam.sh --model gpt-4o --token abc123 < analysis.txt
5559
56- 3 . Pipe input:
57- echo "query" | SAM_API_TOKEN=abc123 ./ask_sam.sh CA706D86
58- cat file | ./ask_sam.sh CA706D86 --model gpt-4 --token abc123
60+ 4 . Pipe input:
61+ echo "query" | SAM_API_TOKEN=abc123 ./ask_sam.sh
62+ cat file | ./ask_sam.sh --model gpt-4o --token abc123
5963
60- 4 . Here-doc:
61- ./ask_sam.sh CA706D86 --model gpt-4 --token abc123 <<'QUERY'
64+ 5 . Here-doc:
65+ ./ask_sam.sh --model gpt-4o --token abc123 <<'QUERY'
6266 Multi-line query with special characters
6367 QUERY
6468
@@ -70,14 +74,20 @@ COMMON DIAGNOSTIC QUERIES:
7074- 'Based on what you observed, what do you think is causing this issue?'
7175
7276USAGE
73- exit 1
77+ exit 0
7478fi
7579
76- shift # Remove conversation ID from args
77-
78- # Parse --model and --token if provided
80+ # Parse command line options
7981while [ $# -gt 0 ]; do
8082 case " $1 " in
83+ --uuid)
84+ if [ -z " $2 " ]; then
85+ echo " ERROR: --uuid requires a UUID value"
86+ exit 1
87+ fi
88+ CONVERSATION_ID=" $2 "
89+ shift 2
90+ ;;
8191 --model)
8292 if [ -z " $2 " ]; then
8393 echo " ERROR: --model requires a model name"
@@ -94,13 +104,48 @@ while [ $# -gt 0 ]; do
94104 API_TOKEN=" $2 "
95105 shift 2
96106 ;;
107+ -* )
108+ echo " ERROR: Unknown option: $1 "
109+ echo " Use: ./ask_sam.sh --help for usage information"
110+ exit 1
111+ ;;
97112 * )
98113 # First non-option argument is the query
99114 break
100115 ;;
101116 esac
102117done
103118
119+ # Auto-generate conversation UUID if not provided
120+ if [ -z " $CONVERSATION_ID " ]; then
121+ CONVERSATION_ID=$( uuidgen)
122+ echo " Auto-generated conversation UUID: $CONVERSATION_ID "
123+ echo " "
124+ fi
125+
126+ # Validate API token
127+ if [ -z " $API_TOKEN " ]; then
128+ echo " ERROR: API token required for authentication"
129+ echo " "
130+ echo " SAM requires authentication for all API requests."
131+ echo " Get your token from: SAM Preferences → API Server → API Authentication"
132+ echo " "
133+ echo " Provide token via:"
134+ echo " --token YOUR-TOKEN (command line option)"
135+ echo " export SAM_API_TOKEN=YOUR-TOKEN (environment variable)"
136+ echo " "
137+ echo " Example: ./ask_sam.sh --token abc123-def456 'query'"
138+ exit 1
139+ fi
140+
141+ # Validate conversation UUID format
142+ if ! echo " $CONVERSATION_ID " | grep -qE ' ^[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}$|^[A-F0-9]{8}$' ; then
143+ echo " WARNING: Conversation UUID '$CONVERSATION_ID ' doesn't match expected format"
144+ echo " Expected: 8-character hex (ABC12345) or full UUID (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)"
145+ echo " Continuing anyway..."
146+ echo " "
147+ fi
148+
104149# Get query from remaining args, stdin, or file
105150if [ -n " $1 " ]; then
106151 # Query provided as argument
@@ -119,21 +164,6 @@ if [ -z "$QUERY" ]; then
119164 exit 1
120165fi
121166
122- # Check if API token is provided
123- if [ -z " $API_TOKEN " ]; then
124- echo " ERROR: API token required for authentication"
125- echo " "
126- echo " SAM now requires authentication for all API requests."
127- echo " Get your token from: SAM Preferences → API Server → API Authentication"
128- echo " "
129- echo " Provide token via:"
130- echo " --token YOUR-TOKEN (command line option)"
131- echo " export SAM_API_TOKEN=YOUR-TOKEN (environment variable)"
132- echo " "
133- echo " Example: ./ask_sam.sh $CONVERSATION_ID --token abc123-def456 'query'"
134- exit 1
135- fi
136-
137167# Check if SAM server is running
138168if ! curl -s http://127.0.0.1:8080/health > /dev/null 2>&1 ; then
139169 echo " ERROR: SAM server is not running or not responding"
@@ -160,12 +190,23 @@ echo "SAM'S RESPONSE:"
160190echo " ========================================="
161191
162192# Create JSON payload with proper escaping (jq handles all special characters)
163- PAYLOAD=$( jq -n \
164- # SAM returns streaming responses, parse SSE format
165- # Write to output file to detect if we got any content
166- OUTPUT_FILE=$( mktemp)
167- trap " rm -f $TEMP_FILE $OUTPUT_FILE " EXIT
193+ jq -n \
194+ --arg conv_id " $CONVERSATION_ID " \
195+ --arg model " $MODEL " \
196+ --arg query " $QUERY " \
197+ ' {
198+ conversation_id: $conv_id,
199+ model: $model,
200+ messages: [
201+ {
202+ role: "user",
203+ content: $query
204+ }
205+ ],
206+ stream: true
207+ }' > " $TEMP_FILE "
168208
209+ # SAM returns streaming responses, parse SSE format
169210curl -s -X POST http://127.0.0.1:8080/api/chat/completions \
170211 -H " Content-Type: application/json" \
171212 -H " Authorization: Bearer $API_TOKEN " \
@@ -188,7 +229,9 @@ curl -s -X POST http://127.0.0.1:8080/api/chat/completions \
188229if [ ! -s " $OUTPUT_FILE " ]; then
189230 echo " "
190231 echo " "
232+ echo " ========================================="
191233 echo " WARNING: No response received from SAM"
234+ echo " ========================================="
192235 echo " This could mean:"
193236 echo " - Invalid API token (check token in SAM Preferences)"
194237 echo " - SAM server crashed or stopped responding"
@@ -198,24 +241,12 @@ if [ ! -s "$OUTPUT_FILE" ]; then
198241 echo " "
199242 echo " Check sam_server.log for details:"
200243 echo " tail -50 sam_server.log | grep -i error"
244+ exit 1
201245else
202246 echo " "
247+ echo " ========================================="
248+ echo " Conversation UUID: $CONVERSATION_ID "
249+ echo " Model: $MODEL "
250+ echo " ========================================="
203251fi
204252
205- echo " "
206- echo " ========================================="
207- echo " WARNING: No response received from SAM"
208- echo " This could mean:"
209- echo " - SAM server crashed or stopped responding"
210- echo " - Conversation ID '$CONVERSATION_ID ' not found"
211- echo " - SAM encountered an error processing the query"
212- echo " - Model '$MODEL ' not available (check API keys in Preferences)"
213- echo " "
214- echo " Check sam_server.log for details:"
215- echo " tail -50 sam_server.log | grep -i error"
216- else
217- echo " "
218- fi
219-
220- echo " "
221- echo " ========================================="
0 commit comments