-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodex-save
More file actions
executable file
·92 lines (80 loc) · 3.01 KB
/
codex-save
File metadata and controls
executable file
·92 lines (80 loc) · 3.01 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
#!/bin/bash
# Codex CLI 래퍼: 실행 후 세션을 Obsidian에 저장
#
# 사용법:
# ~/.zshrc에 alias 등록: alias codex='~/.claude/codex-save'
# 이후 평소처럼 codex 명령 사용
#
# 필수:
# - codex CLI 설치 (npm install -g @openai/codex)
# - save-session-to-obsidian.sh가 ~/.claude/에 설치됨
set -euo pipefail
# ── 설정 ──────────────────────────────────────────────────
# Codex 바이너리 경로 (본인 환경에 맞게 수정)
CODEX_BIN="${CODEX_REAL_BIN:-$(which codex 2>/dev/null || echo "codex")}"
WORK_DIR="$(pwd)"
COMMAND_ARGS="$*"
STARTED_AT_MS=$(python3 -c "import time; print(int(time.time()*1000))")
# ── Codex 실행 ────────────────────────────────────────────
TMPFILE=$(mktemp)
EXIT_CODE=0
script -q "$TMPFILE" "$CODEX_BIN" "$@" || EXIT_CODE=$?
# ── 세션 ID 해석 ─────────────────────────────────────────
resolve_codex_session_id() {
local started_sec completed_sec
started_sec=$((STARTED_AT_MS / 1000))
completed_sec=$(python3 -c "import time; print(int(time.time()))")
python3 - "$started_sec" "$completed_sec" <<'PY'
import json
import sys
from collections import Counter
from pathlib import Path
started = int(sys.argv[1])
completed = int(sys.argv[2])
path = Path.home() / ".codex" / "history.jsonl"
if not path.exists():
print("unknown")
raise SystemExit(0)
window_start = started - 30
window_end = completed + 30
hits = []
fallback = []
with path.open("r", encoding="utf-8", errors="ignore") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
obj = json.loads(line)
except json.JSONDecodeError:
continue
sid = obj.get("session_id")
ts = obj.get("ts")
if not sid or not ts:
continue
try:
ts = int(ts)
except Exception:
continue
if window_start <= ts <= window_end:
hits.append((sid, ts))
if completed - 600 <= ts <= completed + 10:
fallback.append((sid, ts))
if hits:
counts = Counter(s for s, _ in hits)
top_count = counts.most_common(1)[0][1]
candidates = {s for s, c in counts.items() if c == top_count}
best_sid = max((item for item in hits if item[0] in candidates), key=lambda x: x[1])[0]
print(best_sid)
elif fallback:
print(max(fallback, key=lambda x: x[1])[0])
else:
print("unknown")
PY
}
# ── Obsidian에 세션 저장 ─────────────────────────────────
SAVE_SESSION_ID="$(resolve_codex_session_id)"
printf '%s' "{\"session_id\":\"${SAVE_SESSION_ID}\"}" \
| CLAUDE_PROJECT_DIR="$WORK_DIR" CLAUDE_CODE_TOOL="codex" bash ~/.claude/save-session-to-obsidian.sh || true
rm -f "$TMPFILE"
exit "$EXIT_CODE"