-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patham-i-ai.sh
More file actions
executable file
·630 lines (575 loc) · 24.6 KB
/
Copy patham-i-ai.sh
File metadata and controls
executable file
·630 lines (575 loc) · 24.6 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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#!/bin/bash
# am-i-ai.sh - A shared shell library for detecting AI coding agents
# Part of the AI Ecoverse: https://github.com/ai-ecoverse/.github
#
# This library provides functions to detect whether the current execution context
# is being driven by an AI coding agent (Claude, Gemini, Cursor, etc.)
#
# Usage:
# source /path/to/am-i-ai.sh
# if ami_is_ai; then
# echo "Running under AI control: $(ami_detect)"
# fi
#
# Or for simple detection:
# AI_TOOL=$(ami_detect)
# if [ "$AI_TOOL" != "none" ]; then
# echo "AI detected: $AI_TOOL"
# fi
# Version of the am-i-ai library
AMI_VERSION="1.5.0"
# Debug mode - set AMI_DEBUG=true to enable
AMI_DEBUG="${AMI_DEBUG:-false}"
# Internal debug logging function
_ami_debug() {
if [ "$AMI_DEBUG" = "true" ]; then
echo "[am-i-ai] $*" >&2
fi
}
# Function to check if a process name contains a pattern (case-insensitive)
# Arguments: $1 = pid, $2 = pattern
# Returns: 0 if pattern found, 1 otherwise
ami_process_contains() {
local pid=$1
local pattern=$2
# Get process command and name
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
ps -p "$pid" -o comm= 2>/dev/null | grep -qi "$pattern" || \
ps -p "$pid" -o command= 2>/dev/null | grep -qi "$pattern"
else
# Linux
ps -p "$pid" -o comm= 2>/dev/null | grep -qi "$pattern" || \
ps -p "$pid" -o cmd= 2>/dev/null | grep -qi "$pattern"
fi
}
# Phase 1: Environment variable detection
# Returns space-separated list of detected AI tools
ami_check_env() {
local detected=""
# Claude Code detection
# Detect via CLAUDECODE or CLAUDE_CODE_ENTRYPOINT env vars
# ACP mode sets CLAUDECODE=1 without CLAUDE_CODE_ENTRYPOINT
# CLI/SDK mode sets both CLAUDECODE and CLAUDE_CODE_ENTRYPOINT
if [ -n "$CLAUDECODE" ] || [ "$CLAUDE_CODE_ENTRYPOINT" = "cli" ] || [ "$CLAUDE_CODE_ENTRYPOINT" = "sdk-ts" ]; then
detected="$detected claude"
_ami_debug "Detected Claude via environment variable"
fi
# Grok Build detection (xAI)
# Primary detection via process tree (the 'grok' binary or 'grok-macos-*' is the TUI/agent parent)
# GROK_CODE_XAI_API_KEY for API key auth in headless/agent mode (not always exported to child processes)
# GROK_MODEL=grok-build or GROK_BUILD may be set in some contexts
if [ -n "$GROK_CODE_XAI_API_KEY" ] || [ -n "$GROK_BUILD" ] || [ "$GROK_MODEL" = "grok-build" ]; then
detected="$detected grok"
_ami_debug "Detected Grok Build via environment variable"
fi
# Pi Coding Agent detection (package @earendil-works/pi-coding-agent, https://pi.dev)
# Primary: PI_CODING_AGENT env var (set e.g. to true); process tree uses "pi-coding-agent"
# (npm-installed path fragment) to avoid false-positives on bare "pi" (pip/mpi/etc.)
if [ -n "$PI_CODING_AGENT" ]; then
detected="$detected pi"
_ami_debug "Detected Pi via environment variable"
fi
# Gemini detection
if [ -n "$GEMINI_CLI" ]; then
detected="$detected gemini"
_ami_debug "Detected Gemini via environment variable"
fi
# Qwen detection
if [ -n "$QWEN_CODE" ]; then
detected="$detected qwen"
_ami_debug "Detected Qwen via environment variable"
fi
# Cursor detection - use CURSOR_AGENT to avoid false positives in Cursor IDE terminals
if [ -n "$CURSOR_AGENT" ]; then
detected="$detected cursor"
_ami_debug "Detected Cursor via environment variable"
fi
# OpenCode detection
if [ -n "$OPENCODE_AI" ]; then
detected="$detected opencode"
_ami_debug "Detected OpenCode via environment variable"
fi
# Codex detection
# CLI sets CODEX_CLI, sandboxed ACP sessions set CODEX_SANDBOX,
# and Codex Desktop shells expose CODEX_SHELL/CODEX_THREAD_ID.
if [ -n "$CODEX_CLI" ] || [ -n "$CODEX_SANDBOX" ] || \
[ -n "$CODEX_SHELL" ] || [ -n "$CODEX_THREAD_ID" ]; then
detected="$detected codex"
_ami_debug "Detected Codex via environment variable"
fi
# Aider detection
if [ "$OR_APP_NAME" = "Aider" ]; then
detected="$detected aider"
_ami_debug "Detected Aider via environment variable"
fi
# Zed detection - complex logic to distinguish human from AI
# Observed patterns:
# 1. Human via git panel: ZED_ENVIRONMENT + NO terminal vars + SHLVL=1
# 2. Human via terminal: ZED_ENVIRONMENT + parent is interactive shell (elvish, zsh, bash, fish)
# 3. Zed's native agent: ZED_ENVIRONMENT + HAS terminal vars + SHLVL>1 + parent NOT interactive shell
# 4. ACP integrations: Have their own tool markers (handled above)
#
# Key insight: If the direct parent process is an interactive shell,
# the user is typing commands manually, not the Zed agent.
local term_program_lower
term_program_lower=$(echo "$TERM_PROGRAM" | tr '[:upper:]' '[:lower:]')
if [ -n "$ZED_ENVIRONMENT" ]; then
if { [ "$term_program_lower" = "zed" ] || [ -n "$ZED_TERM" ]; } && [ "${SHLVL:-1}" -gt 1 ]; then
# Has terminal vars and SHLVL>1 - could be agent or human in terminal
# Check if parent process is an interactive shell (human typing)
local parent_comm
parent_comm=$(ps -p "$PPID" -o comm= 2>/dev/null | tr '[:upper:]' '[:lower:]')
case "$parent_comm" in
bash|elvish|zsh|fish|ksh|tcsh|dash|-bash|-elvish|-zsh|-fish|-ksh|-tcsh|-dash)
# Parent is interactive shell - human typing in terminal
_ami_debug "Zed detected but parent is interactive shell ($parent_comm) - human typing"
;;
*)
# Parent is not an interactive shell - likely Zed agent
detected="$detected zed"
_ami_debug "Detected Zed AI agent via environment (parent: $parent_comm)"
;;
esac
fi
# else: Human git panel (SHLVL=1, no terminal vars)
fi
# Copilot detection
if [ "$GITHUB_COPILOT_CLI_MODE" = "true" ]; then
detected="$detected copilot"
_ami_debug "Detected Copilot via environment variable"
fi
# Droid detection (Factory AI)
if [ -n "$DROID_CLI" ]; then
detected="$detected droid"
_ami_debug "Detected Droid via environment variable"
fi
# Amp detection (Sourcegraph)
if [ "$AGENT" = "amp" ] || [ -n "$AMP_HOME" ]; then
detected="$detected amp"
_ami_debug "Detected Amp via environment variable"
fi
# Kimi CLI detection
if [ -n "$KIMI_CLI" ]; then
detected="$detected kimi"
_ami_debug "Detected Kimi CLI via environment variable"
fi
# OpenHands detection
if [ "$OR_APP_NAME" = "OpenHands" ] || [ -n "$OR_SITE_URL" ]; then
detected="$detected openhands"
_ami_debug "Detected OpenHands via environment variable"
fi
# Goose detection (Block)
if [ -n "$GOOSE_TERMINAL" ]; then
detected="$detected goose"
_ami_debug "Detected Goose via environment variable"
fi
# Auggie detection (Augment Code)
# Intent by Augment / ACP mode sets AUGMENT_AGENT=1 without AUGMENT_API_TOKEN.
if [ -n "$AUGMENT_API_TOKEN" ] || [ -n "$AUGMENT_AGENT" ]; then
detected="$detected auggie"
_ami_debug "Detected Auggie via environment variable"
fi
# Cline detection (VS Code extension)
if [ -n "$CLINE_TASK_ID" ]; then
detected="$detected cline"
_ami_debug "Detected Cline via environment variable"
fi
# Roo Code detection (VS Code extension)
if [ -n "$ROO_CODE_TASK_ID" ]; then
detected="$detected roo"
_ami_debug "Detected Roo Code via environment variable"
fi
# Windsurf/Cascade detection
if [ -n "$WINDSURF_SESSION" ] || [ "$TERM_PROGRAM" = "windsurf" ]; then
detected="$detected windsurf"
_ami_debug "Detected Windsurf via environment variable"
fi
echo "$detected"
}
# Phase 2: Process tree detection
# Returns space-separated list of detected AI tools
ami_check_ps_tree() {
local detected=""
local current_pid=$$
local max_depth=10
local depth=0
_ami_debug "Starting process tree detection from PID $current_pid"
while [ $depth -lt $max_depth ]; do
# Get parent PID
if [[ "$OSTYPE" == "darwin"* ]]; then
current_pid=$(ps -p "$current_pid" -o ppid= 2>/dev/null | tr -d ' ')
else
current_pid=$(ps -p "$current_pid" -o ppid= 2>/dev/null | tr -d ' ')
fi
# Check if we've reached the top
if [ -z "$current_pid" ] || [ "$current_pid" -eq 1 ] || [ "$current_pid" -eq 0 ]; then
_ami_debug "Reached top of process tree at depth $depth"
break
fi
_ami_debug "Checking PID $current_pid at depth $depth"
# Check for AI tool patterns
if ami_process_contains "$current_pid" "claude"; then
detected="$detected claude"
_ami_debug "Detected Claude in process tree at depth $depth"
fi
if ami_process_contains "$current_pid" "gemini"; then
detected="$detected gemini"
_ami_debug "Detected Gemini in process tree at depth $depth"
fi
# Detect Codex CLI/Desktop by process name
if ami_process_contains "$current_pid" "codex"; then
detected="$detected codex"
_ami_debug "Detected Codex in process tree at depth $depth"
fi
if ami_process_contains "$current_pid" "aider"; then
detected="$detected aider"
_ami_debug "Detected Aider in process tree at depth $depth"
fi
if ami_process_contains "$current_pid" "qwen"; then
detected="$detected qwen"
_ami_debug "Detected Qwen in process tree at depth $depth"
fi
if ami_process_contains "$current_pid" "zed"; then
# Attribute to Zed's native agent when terminal vars ARE set and SHLVL > 1
# BUT not if parent is an interactive shell (human typing)
local tp_lower
tp_lower=$(echo "$TERM_PROGRAM" | tr '[:upper:]' '[:lower:]')
if { [ "$tp_lower" = "zed" ] || [ -n "$ZED_TERM" ]; } && [ "${SHLVL:-1}" -gt 1 ]; then
local parent_comm
parent_comm=$(ps -p "$PPID" -o comm= 2>/dev/null | tr '[:upper:]' '[:lower:]')
case "$parent_comm" in
bash|elvish|zsh|fish|ksh|tcsh|dash|-bash|-elvish|-zsh|-fish|-ksh|-tcsh|-dash)
# Parent is interactive shell - human typing
_ami_debug "Zed in tree but parent is interactive shell - human typing"
;;
*)
detected="$detected zed"
_ami_debug "Detected Zed AI in process tree at depth $depth"
;;
esac
fi
fi
if ami_process_contains "$current_pid" "opencode"; then
detected="$detected opencode"
_ami_debug "Detected OpenCode in process tree at depth $depth"
fi
# Cursor detection - use cursor-agent to avoid false positives in Cursor IDE terminals
if ami_process_contains "$current_pid" "cursor-agent"; then
detected="$detected cursor"
_ami_debug "Detected Cursor in process tree at depth $depth"
fi
# Kimi CLI detection - look for kimi in process command
if ami_process_contains "$current_pid" "kimi"; then
detected="$detected kimi"
_ami_debug "Detected Kimi CLI in process tree at depth $depth"
fi
# Crush detection - look for crush in process command
if ami_process_contains "$current_pid" "crush"; then
detected="$detected crush"
_ami_debug "Detected Crush in process tree at depth $depth"
fi
# Goose detection - look for goose in process command
if ami_process_contains "$current_pid" "goose"; then
detected="$detected goose"
_ami_debug "Detected Goose in process tree at depth $depth"
fi
# Grok Build detection - look for grok in process command (binary is grok or grok-macos-*)
if ami_process_contains "$current_pid" "grok"; then
detected="$detected grok"
_ami_debug "Detected Grok Build in process tree at depth $depth"
fi
# Pi Coding Agent detection (package @earendil-works/pi-coding-agent, https://pi.dev)
# Safe whole-token match: pad both sides of ps output with spaces, then test for the
# space-bounded token " pi " (or " pi-coding-agent" path fragment). Simplest approach,
# no regex. Catches the launched `pi` binary (shows as COMM/COMMAND="pi") without
# false positives on pip/mpi/picocom/etc. Env var PI_CODING_AGENT is still primary.
local pi_out
if [[ "$OSTYPE" == "darwin"* ]]; then
pi_out="$(ps -p "$current_pid" -o comm= 2>/dev/null) $(ps -p "$current_pid" -o command= 2>/dev/null)"
else
pi_out="$(ps -p "$current_pid" -o comm= 2>/dev/null) $(ps -p "$current_pid" -o cmd= 2>/dev/null)"
fi
if echo " $pi_out " | grep -qi " pi " || echo " $pi_out " | grep -qi " pi-coding-agent "; then
detected="$detected pi"
_ami_debug "Detected Pi in process tree at depth $depth (space-bounded token)"
fi
# Auggie detection - look for auggie in process command
if ami_process_contains "$current_pid" "auggie"; then
detected="$detected auggie"
_ami_debug "Detected Auggie in process tree at depth $depth"
fi
# Cline detection
if ami_process_contains "$current_pid" "cline"; then
detected="$detected cline"
_ami_debug "Detected Cline in process tree at depth $depth"
fi
# Roo Code detection - use word boundary to avoid matching "kangaroo", etc.
if [[ "$OSTYPE" == "darwin"* ]]; then
if ps -p "$current_pid" -o comm= 2>/dev/null | grep -qiw "roo" || \
ps -p "$current_pid" -o command= 2>/dev/null | grep -qiw "roo"; then
detected="$detected roo"
_ami_debug "Detected Roo Code in process tree at depth $depth"
fi
else
if ps -p "$current_pid" -o comm= 2>/dev/null | grep -qiw "roo" || \
ps -p "$current_pid" -o cmd= 2>/dev/null | grep -qiw "roo"; then
detected="$detected roo"
_ami_debug "Detected Roo Code in process tree at depth $depth"
fi
fi
# Windsurf detection
if ami_process_contains "$current_pid" "windsurf"; then
detected="$detected windsurf"
_ami_debug "Detected Windsurf in process tree at depth $depth"
fi
# Droid detection - use word boundary to avoid matching android-studio, etc.
if [[ "$OSTYPE" == "darwin"* ]]; then
if ps -p "$current_pid" -o comm= 2>/dev/null | grep -qiw "droid" || \
ps -p "$current_pid" -o command= 2>/dev/null | grep -qiw "droid"; then
detected="$detected droid"
_ami_debug "Detected Droid in process tree at depth $depth"
fi
else
if ps -p "$current_pid" -o comm= 2>/dev/null | grep -qiw "droid" || \
ps -p "$current_pid" -o cmd= 2>/dev/null | grep -qiw "droid"; then
detected="$detected droid"
_ami_debug "Detected Droid in process tree at depth $depth"
fi
fi
depth=$((depth + 1))
done
echo "$detected"
}
# Main AI detection function with proper two-phase approach
# Returns the detected AI tool name, or "none" if no AI detected
# Priority order ensures more specific tools take precedence over generic ones
ami_detect() {
# Allow callers to opt out of AI detection (e.g., read-only tools)
# Set AMI_PASSTHROUGH=true to force "none" result
if [[ "${AMI_PASSTHROUGH:-}" == "true" ]]; then
_ami_debug "AMI_PASSTHROUGH is set, returning none"
echo "none"
return 0
fi
_ami_debug "Starting AI detection"
# Phase 1: Environment variable detection
local env_detected
env_detected=$(ami_check_env)
# Phase 2: Process tree detection
local ps_detected
ps_detected=$(ami_check_ps_tree)
# Combine results and apply priority order
local all_detected="$env_detected $ps_detected"
_ami_debug "Environment detected: '$env_detected'"
_ami_debug "Process tree detected: '$ps_detected'"
_ami_debug "Combined detected: '$all_detected'"
# Priority order: Amp > Codex > Aider > Grok > Pi > Claude > Gemini > Qwen > Droid > OpenCode > Cursor > Copilot > Kimi > OpenHands > Cline > Roo > Windsurf > Crush > Goose > Auggie > Zed
# Zed is last because it often hosts other AI tools
# More specific AI tools take precedence over IDE-level tools
if [[ "$all_detected" =~ "amp" ]]; then
_ami_debug "Final result: amp"
echo "amp"
elif [[ "$all_detected" =~ "codex" ]]; then
_ami_debug "Final result: codex"
echo "codex"
elif [[ "$all_detected" =~ "aider" ]]; then
_ami_debug "Final result: aider"
echo "aider"
elif [[ "$all_detected" =~ "grok" ]]; then
_ami_debug "Final result: grok"
echo "grok"
elif [[ " $all_detected " =~ " pi " ]]; then
_ami_debug "Final result: pi"
echo "pi"
elif [[ "$all_detected" =~ "claude" ]]; then
_ami_debug "Final result: claude"
echo "claude"
elif [[ "$all_detected" =~ "gemini" ]]; then
_ami_debug "Final result: gemini"
echo "gemini"
elif [[ "$all_detected" =~ "qwen" ]]; then
_ami_debug "Final result: qwen"
echo "qwen"
elif [[ "$all_detected" =~ "droid" ]]; then
_ami_debug "Final result: droid"
echo "droid"
elif [[ "$all_detected" =~ "opencode" ]]; then
_ami_debug "Final result: opencode"
echo "opencode"
elif [[ "$all_detected" =~ "cursor" ]]; then
_ami_debug "Final result: cursor"
echo "cursor"
elif [[ "$all_detected" =~ "copilot" ]]; then
_ami_debug "Final result: copilot"
echo "copilot"
elif [[ "$all_detected" =~ "kimi" ]]; then
_ami_debug "Final result: kimi"
echo "kimi"
elif [[ "$all_detected" =~ "openhands" ]]; then
_ami_debug "Final result: openhands"
echo "openhands"
elif [[ "$all_detected" =~ "cline" ]]; then
_ami_debug "Final result: cline"
echo "cline"
elif [[ "$all_detected" =~ "roo" ]]; then
_ami_debug "Final result: roo"
echo "roo"
elif [[ "$all_detected" =~ "windsurf" ]]; then
_ami_debug "Final result: windsurf"
echo "windsurf"
elif [[ "$all_detected" =~ "crush" ]]; then
_ami_debug "Final result: crush"
echo "crush"
elif [[ "$all_detected" =~ "goose" ]]; then
_ami_debug "Final result: goose"
echo "goose"
elif [[ "$all_detected" =~ "auggie" ]]; then
_ami_debug "Final result: auggie"
echo "auggie"
elif [[ "$all_detected" =~ "zed" ]]; then
_ami_debug "Final result: zed"
echo "zed"
else
_ami_debug "Final result: none"
echo "none"
fi
}
# Convenience function: returns 0 (true) if AI is detected, 1 (false) otherwise
# Note: Set AMI_PASSTHROUGH=true to force this to return false (no AI detected)
ami_is_ai() {
local result
result=$(ami_detect)
[ "$result" != "none" ]
}
# Get all detected AI tools (not just the highest priority one)
# Returns space-separated list or empty string
ami_detect_all() {
# Allow callers to opt out of AI detection (e.g., read-only tools)
# Set AMI_PASSTHROUGH=true to force empty result
if [[ "${AMI_PASSTHROUGH:-}" == "true" ]]; then
_ami_debug "AMI_PASSTHROUGH is set, returning empty"
echo ""
return 0
fi
local env_detected
env_detected=$(ami_check_env)
local ps_detected
ps_detected=$(ami_check_ps_tree)
# Combine and deduplicate
local all_detected="$env_detected $ps_detected"
# Remove leading/trailing spaces and deduplicate
echo "$all_detected" | tr ' ' '\n' | sort -u | tr '\n' ' ' | sed 's/^ *//;s/ *$//'
}
# Get the email address for a detected AI tool
# Arguments: $1 = AI tool name (optional, uses ami_detect if not provided)
ami_get_email() {
local tool="${1:-$(ami_detect)}"
case "$tool" in
"claude") echo "noreply@anthropic.com" ;;
"gemini") echo "noreply@google.com" ;;
"codex") echo "noreply@openai.com" ;;
"aider") echo "aider@aider.chat" ;;
"qwen") echo "noreply@alibaba.com" ;;
"cursor") echo "cursoragent@cursor.com" ;;
"opencode") echo "noreply@opencode.ai" ;;
"zed") echo "noreply@zed.dev" ;;
"copilot") echo "copilot@github.com" ;;
"droid") echo "droid@factory.ai" ;;
"amp") echo "noreply@sourcegraph.com" ;;
"kimi") echo "noreply@kimi.com" ;;
"openhands") echo "openhands@all-hands.dev" ;;
"crush") echo "crush@charm.land" ;;
"goose") echo "goose@opensource.block.xyz" ;;
"grok") echo "grok@x.ai" ;;
"pi") echo "pi@earendil.works" ;;
"auggie") echo "noreply@augmentcode.com" ;;
"cline") echo "cline@cline.bot" ;;
"roo") echo "roo@roocode.dev" ;;
"windsurf") echo "cascade@codeium.com" ;;
*) echo "" ;;
esac
}
# Get the display name for a detected AI tool
# Arguments: $1 = AI tool name (optional, uses ami_detect if not provided)
ami_get_name() {
local tool="${1:-$(ami_detect)}"
case "$tool" in
"claude") echo "Claude Code" ;;
"gemini") echo "Gemini" ;;
"codex") echo "Codex" ;;
"aider") echo "Aider" ;;
"qwen") echo "Qwen Code" ;;
"cursor") echo "Cursor AI" ;;
"opencode") echo "opencode AI" ;;
"zed") echo "Zed AI" ;;
"copilot") echo "GitHub Copilot" ;;
"droid") echo "Droid" ;;
"amp") echo "Amp" ;;
"kimi") echo "Kimi CLI" ;;
"openhands") echo "OpenHands" ;;
"crush") echo "Crush" ;;
"goose") echo "Goose User" ;;
"grok") echo "Grok Build" ;;
"pi") echo "Pi" ;;
"auggie") echo "Augment Code" ;;
"cline") echo "Cline" ;;
"roo") echo "Roo Code" ;;
"windsurf") echo "Windsurf Cascade" ;;
*) echo "" ;;
esac
}
# Print version information
ami_version() {
echo "am-i-ai version $AMI_VERSION"
echo "https://github.com/ai-ecoverse/am-i-ai"
}
# Quick test cases for Pi detection (space-padded whole-token approach; run after `source am-i-ai.sh`):
# all_detected=" copilot " ; [[ " $all_detected " =~ " pi " ]] && echo FAIL || echo "ok: copilot not matched as pi"
# all_detected=" pi " ; [[ " $all_detected " =~ " pi " ]] && echo "ok: pi matched" || echo FAIL
# all_detected=" copilot pi " ; [[ " $all_detected " =~ " pi " ]] && echo "ok: pi-and-copilot (pi wins)" || echo FAIL
# AMI_DEBUG=false ami_check_ps_tree | grep -q pi && echo "ok: ps tree detects pi (via space-bounded token)"
# If this script is run directly (not sourced), run the detection
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
case "${1:-}" in
--version|-v)
ami_version
;;
--help|-h)
echo "am-i-ai - AI coding agent detection library"
echo ""
echo "Usage as script:"
echo " am-i-ai.sh # Detect AI and print result"
echo " am-i-ai.sh --all # Show all detected AIs"
echo " am-i-ai.sh --check # Exit 0 if AI, 1 if not"
echo " am-i-ai.sh --debug # Run with debug output"
echo ""
echo "Usage as library:"
echo " source am-i-ai.sh"
echo " if ami_is_ai; then"
echo " echo \"AI: \$(ami_detect)\""
echo " fi"
echo ""
echo "Functions:"
echo " ami_detect - Returns detected AI tool or 'none'"
echo " ami_is_ai - Returns 0 if AI detected, 1 otherwise"
echo " ami_detect_all - Returns all detected AI tools"
echo " ami_get_name - Returns display name for AI tool"
echo " ami_get_email - Returns email for AI tool"
echo " ami_check_env - Phase 1: environment variable detection"
echo " ami_check_ps_tree - Phase 2: process tree detection"
;;
--all|-a)
ami_detect_all
;;
--check|-c)
ami_is_ai
;;
--debug|-d)
AMI_DEBUG=true ami_detect
;;
*)
ami_detect
;;
esac
fi