-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex-app.sh
More file actions
154 lines (131 loc) · 3.87 KB
/
codex-app.sh
File metadata and controls
154 lines (131 loc) · 3.87 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
#!/bin/bash
set -euo pipefail
appdir="${CODEX_APPDIR:-/usr/lib/codex-app}"
webview_dir="${appdir}/content/webview"
electron_candidates=()
if [[ -n "${CODEX_ELECTRON_PATH:-}" ]]; then
electron_candidates+=("${CODEX_ELECTRON_PATH}")
fi
electron_candidates+=(
"/usr/lib/electron39/electron"
"/usr/lib/electron/electron"
)
electron=""
for candidate in "${electron_candidates[@]}"; do
if [[ -x "${candidate}" ]]; then
electron="${candidate}"
break
fi
done
[[ -x "${electron}" ]] || {
echo "Missing Electron runtime. Checked: ${electron_candidates[*]}" >&2
exit 1
}
export CODEX_CLI_PATH="${CODEX_CLI_PATH:-$(command -v codex || true)}"
export BUILD_FLAVOR="${BUILD_FLAVOR:-prod}"
export NODE_ENV="${NODE_ENV:-production}"
export ELECTRON_RENDERER_URL="${ELECTRON_RENDERER_URL:-http://localhost:5175/}"
extra_flags=()
if [[ -n "${CODEX_ELECTRON_FLAGS:-}" ]]; then
read -r -a extra_flags <<<"${CODEX_ELECTRON_FLAGS}"
fi
render_profile="${CODEX_RENDER_PROFILE:-stable}"
# Electron + Wayland can produce sidebar flicker artifacts on some Arch setups.
# Default to X11 backend when running in Wayland, overridable via CODEX_OZONE_PLATFORM.
if [[ -n "${CODEX_OZONE_PLATFORM:-}" ]]; then
ozone_platform="${CODEX_OZONE_PLATFORM}"
elif [[ "${XDG_SESSION_TYPE:-}" == "wayland" ]]; then
ozone_platform="x11"
else
ozone_platform="auto"
fi
render_flags=()
if [[ "${render_profile}" == "stable" ]]; then
# Strong fallback profile for Intel/Wayland flicker issues.
render_flags=(
"--disable-gpu"
"--disable-gpu-compositing"
"--disable-gpu-rasterization"
"--disable-zero-copy"
"--use-gl=swiftshader"
)
fi
http_pid=""
electron_pid=""
tmpdir=""
cleanup() {
[[ -n "${electron_pid}" ]] && wait "${electron_pid}" 2>/dev/null || true
[[ -n "${http_pid}" ]] && kill "${http_pid}" 2>/dev/null || true
[[ -n "${http_pid}" ]] && wait "${http_pid}" 2>/dev/null || true
[[ -n "${tmpdir}" ]] && rm -rf "${tmpdir}"
}
forward_signal() {
local sig="$1"
if [[ -n "${electron_pid}" ]] && kill -0 "${electron_pid}" 2>/dev/null; then
kill -"${sig}" "${electron_pid}" 2>/dev/null || true
wait "${electron_pid}" 2>/dev/null || true
fi
exit 0
}
trap cleanup EXIT
trap 'forward_signal HUP' HUP
trap 'forward_signal INT' INT
trap 'forward_signal TERM' TERM
if [[ -d "${webview_dir}" ]] && find "${webview_dir}" -mindepth 1 -maxdepth 1 -print -quit | grep -q .; then
tmpdir="$(mktemp -d)"
ready_file="${tmpdir}/ready"
fail_file="${tmpdir}/fail"
python - 5175 "${webview_dir}" "${ready_file}" "${fail_file}" >/dev/null 2>&1 <<'PY' &
import http.server
import os
import socketserver
import sys
port = int(sys.argv[1])
root = sys.argv[2]
ready_file = sys.argv[3]
fail_file = sys.argv[4]
os.chdir(root)
class Handler(http.server.SimpleHTTPRequestHandler):
def log_message(self, fmt, *args):
pass
class TCPServer(socketserver.TCPServer):
allow_reuse_address = True
try:
with TCPServer(("127.0.0.1", port), Handler) as httpd:
with open(ready_file, "w") as f:
f.write("ok")
httpd.serve_forever()
except Exception as e:
with open(fail_file, "w") as f:
f.write(str(e))
raise
PY
http_pid=$!
for _ in {1..50}; do
[[ -f "${ready_file}" ]] && break
if [[ -f "${fail_file}" ]]; then
echo "Failed to start local webview server on 127.0.0.1:5175" >&2
cat "${fail_file}" >&2
exit 1
fi
kill -0 "${http_pid}" 2>/dev/null || {
echo "Local webview server exited before becoming ready" >&2
exit 1
}
sleep 0.1
done
[[ -f "${ready_file}" ]] || {
echo "Timed out waiting for local webview server on 127.0.0.1:5175" >&2
exit 1
}
fi
"${electron}" \
--enable-sandbox \
--ozone-platform-hint="${ozone_platform}" \
--ozone-platform="${ozone_platform}" \
"${appdir}/resources/app.asar" \
"${render_flags[@]}" \
"${extra_flags[@]}" \
"$@" &
electron_pid=$!
wait "${electron_pid}"