Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·118 lines (105 loc) · 3.62 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·118 lines (105 loc) · 3.62 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
#!/usr/bin/env bash
#
# Nano Claude Code — Python launcher
#
# Usage:
# ./start.sh # Interactive REPL
# ./start.sh -p "your prompt" # Non-interactive mode
# ./start.sh --help # Show help
#
# API configuration — put a .env file in the project directory:
#
# # Direct Anthropic (highest priority if sk-ant-* key detected)
# ANTHROPIC_API_KEY=sk-ant-xxx
#
# # OR OpenRouter
# OPENROUTER_API_KEY=sk-or-xxx
# OPENROUTER_MODEL=anthropic/claude-sonnet-4-6
#
# # OR generic proxy
# ANTHROPIC_API_KEY=your-key
# ANTHROPIC_BASE_URL=https://your-proxy.com
#
set -e
DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$DIR"
# ── Load project .env (highest priority, before any env normalization) ────
# Walk from $DIR upward to git root looking for .env files.
# More-specific (closer to CWD) values win by loading last.
_load_dotenv() {
local f="$1"
[ -f "$f" ] || return 0
while IFS= read -r line || [ -n "$line" ]; do
line="${line%%#*}" # strip comments
line="$(echo "$line" | sed 's/^export //')" # strip 'export '
line="$(echo "$line" | xargs)" # trim whitespace
[ -z "$line" ] && continue
case "$line" in
*=*)
local key="${line%%=*}"
local val="${line#*=}"
val="${val%\"}" ; val="${val#\"}" # strip double quotes
val="${val%\'}" ; val="${val#\'}" # strip single quotes
export "$key=$val" 2>/dev/null || true
;;
esac
done < "$f"
}
# Collect .env files from git root → CWD (CWD loaded last = highest priority)
_dotenv_files=()
_walk="$DIR"
_git_root=""
if _git_root="$(git -C "$DIR" rev-parse --show-toplevel 2>/dev/null)"; then
:
fi
while true; do
[ -f "$_walk/.env" ] && _dotenv_files=("$_walk/.env" "${_dotenv_files[@]}")
[ "$_walk" = "$_git_root" ] && break
_parent="$(dirname "$_walk")"
[ "$_parent" = "$_walk" ] && break
_walk="$_parent"
done
for _ef in "${_dotenv_files[@]}"; do
_load_dotenv "$_ef"
done
# ── Check auth (after .env is loaded) ────────────────────────────────────
_has_key=0
[ -n "${ANTHROPIC_API_KEY:-}" ] && _has_key=1
[ -n "${ANTHROPIC_AUTH_TOKEN:-}" ] && _has_key=1
[ -n "${OPENROUTER_API_KEY:-}" ] && _has_key=1
if [ "$_has_key" -eq 0 ]; then
echo "Error: no API credential detected."
echo ""
echo "Create a .env file in this directory with one of:"
echo ""
echo ' # Direct Anthropic'
echo ' ANTHROPIC_API_KEY=sk-ant-xxx'
echo ""
echo ' # OR OpenRouter'
echo ' OPENROUTER_API_KEY=sk-or-xxx'
echo ""
echo "Or export the variable in your shell."
exit 1
fi
# ── Find Python and ensure package is installed ───────────────────────────
PYTHON=""
for p in python3.13 python3.12 python3.11 python3.10 python3; do
if command -v "$p" &>/dev/null; then
PYTHON="$p"
break
fi
done
if [ -z "$PYTHON" ]; then
echo "Error: Python 3.10+ not found. Install Python first."
exit 1
fi
if [ -f "$DIR/.venv/bin/python" ]; then
PYTHON="$DIR/.venv/bin/python"
elif ! "$PYTHON" -c "import nano_claude_code" 2>/dev/null; then
echo "First run: installing nano-claude-code..."
"$PYTHON" -m pip install -e "$DIR" --quiet 2>/dev/null || \
"$PYTHON" -m pip install -e "$DIR" --user --quiet 2>/dev/null || \
echo "Warning: pip install failed. Try: pip install -e $DIR"
fi
# ── Launch ────────────────────────────────────────────────────────────────
exec "$PYTHON" -m nano_claude_code "$@"