Initial codex telegram bot source

This commit is contained in:
Codex
2026-05-21 08:40:16 +00:00
commit ad61f7eeed
275 changed files with 101972 additions and 0 deletions

94
scripts/start-codex-app-server Executable file
View File

@@ -0,0 +1,94 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ENV_FILE="$ROOT/.env"
RUN_DIR="$ROOT/run"
PID_FILE="$RUN_DIR/codex-app-server.pid"
LOG_FILE="$RUN_DIR/codex-app-server.log"
STDIN_FIFO="$RUN_DIR/codex-app-server.stdin"
read_env_value() {
local key="$1"
if [[ -f "$ENV_FILE" ]]; then
awk -F= -v key="$key" '$1 == key { sub(/^[^=]*=/, ""); print; exit }' "$ENV_FILE"
fi
}
HOST_CODEX_SOCKET="${HOST_CODEX_SOCKET:-$(read_env_value HOST_CODEX_SOCKET)}"
HOST_CODEX_SOCKET="${HOST_CODEX_SOCKET:-$RUN_DIR/codex.sock}"
mkdir -p "$RUN_DIR"
chmod 700 "$RUN_DIR"
if [[ -f "$PID_FILE" ]]; then
old_pid="$(tr -cd '0-9' < "$PID_FILE" || true)"
if [[ -n "$old_pid" ]] && kill -0 "$old_pid" 2>/dev/null; then
if [[ -S "$HOST_CODEX_SOCKET" ]]; then
echo "codex app-server already running: pid=$old_pid socket=$HOST_CODEX_SOCKET"
exit 0
fi
echo "pid $old_pid is running but socket is missing; refusing to start a second app-server" >&2
exit 1
fi
rm -f "$PID_FILE"
fi
if [[ -e "$HOST_CODEX_SOCKET" ]]; then
if [[ -S "$HOST_CODEX_SOCKET" ]]; then
rm -f "$HOST_CODEX_SOCKET"
else
echo "socket path exists and is not a Unix socket: $HOST_CODEX_SOCKET" >&2
exit 1
fi
fi
rm -f "$STDIN_FIFO"
mkfifo "$STDIN_FIFO"
chmod 600 "$STDIN_FIFO"
: > "$LOG_FILE"
# Codex app-server currently exits if detached with stdin closed. A detached
# wrapper keeps a private FIFO writer open and then runs Codex on the host.
setsid -f bash -c '
echo "$$" > "$3"
tail -f /dev/null > "$1" &
writer=$!
trap "kill $writer 2>/dev/null || true" EXIT
codex app-server --listen "$2" < "$1"
' codex-app-server "$STDIN_FIFO" "unix://$HOST_CODEX_SOCKET" "$PID_FILE" >> "$LOG_FILE" 2>&1
for _ in $(seq 1 50); do
if [[ -f "$PID_FILE" ]]; then
break
fi
sleep 0.1
done
pid="$(tr -cd '0-9' < "$PID_FILE" 2>/dev/null || true)"
if [[ -z "$pid" ]]; then
echo "codex app-server did not write a pid file; log follows:" >&2
sed -n '1,120p' "$LOG_FILE" >&2 || true
exit 1
fi
for _ in $(seq 1 100); do
if [[ -S "$HOST_CODEX_SOCKET" ]]; then
sleep 0.5
if kill -0 "$pid" 2>/dev/null; then
echo "codex app-server started: pid=$pid socket=$HOST_CODEX_SOCKET log=$LOG_FILE"
exit 0
fi
fi
if ! kill -0 "$pid" 2>/dev/null; then
echo "codex app-server exited before staying ready; log follows:" >&2
sed -n '1,120p' "$LOG_FILE" >&2 || true
rm -f "$PID_FILE"
exit 1
fi
sleep 0.1
done
echo "codex app-server did not create socket within 10 seconds; log follows:" >&2
sed -n '1,120p' "$LOG_FILE" >&2 || true
exit 1