-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_opencode.sh
More file actions
executable file
·61 lines (52 loc) · 2.4 KB
/
Copy pathpatch_opencode.sh
File metadata and controls
executable file
·61 lines (52 loc) · 2.4 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
#!/usr/bin/env bash
# patch_opencode.sh — point a cloned OpenCode at the local M365 Copilot proxy.
#
# You almost certainly DON'T need this: drop ./opencode.json into your project
# (or ~/.config/opencode/opencode.json) and OpenCode uses the proxy as a
# provider with zero source changes. This script exists for the case where you
# truly want a hard fork with no escape hatch to OpenAI/GitHub Copilot.
#
# Handles both:
# * sst/opencode (TypeScript, current)
# * opencode-ai/opencode (Go, archived)
#
# Usage: ./patch_opencode.sh /path/to/opencode-clone [http://localhost:8080/v1]
set -euo pipefail
REPO="${1:?usage: $0 /path/to/opencode [proxy-base-url]}"
PROXY="${2:-http://localhost:8080/v1}"
[ -d "$REPO" ] || { echo "not a directory: $REPO" >&2; exit 1; }
# Every upstream base URL OpenCode is known to embed. Add to this list if a
# grep after patching still shows a stray host.
UPSTREAMS=(
"https://api.openai.com/v1"
"https://api.openai.com"
"https://api.githubcopilot.com"
"https://api.individual.githubcopilot.com"
"https://api.business.githubcopilot.com"
"https://api.enterprise.githubcopilot.com"
"https://copilot-proxy.githubusercontent.com"
)
FILES=$(grep -rIl --exclude-dir=node_modules --exclude-dir=.git --exclude-dir=dist \
--include='*.ts' --include='*.tsx' --include='*.js' --include='*.go' --include='*.json' \
-e 'openai\.com' -e 'githubcopilot\.com' -e 'copilot-proxy\.githubusercontent\.com' "$REPO" || true)
if [ -z "$FILES" ]; then
echo "no hardcoded OpenAI/GitHub Copilot URLs found in $REPO — nothing to patch."
echo "(That's normal for sst/opencode: use opencode.json instead.)"
exit 0
fi
echo "== files to patch =="; echo "$FILES"; echo
for f in $FILES; do
cp "$f" "$f.bak" # keep a backup next to every touched file
for u in "${UPSTREAMS[@]}"; do
# '|' delimiter because the URLs contain '/'
sed -i "s|${u}|${PROXY}|g" "$f"
done
done
echo "== remaining upstream references (should be empty) =="
grep -rIn --exclude-dir=node_modules --exclude-dir=.git --exclude='*.bak' \
-e 'api\.openai\.com' -e 'githubcopilot\.com' "$REPO" || echo "(none)"
echo
echo "patched. rebuild:"
echo " sst/opencode: cd $REPO && bun install && bun run --cwd packages/opencode build"
echo " Go opencode: cd $REPO && go build -o opencode ."
echo "restore: find $REPO -name '*.bak' -exec sh -c 'mv \"\$1\" \"\${1%.bak}\"' _ {} \;"