fix(worklog): 修正 auto 模式的 CLI 判斷

This commit is contained in:
Jeffery
2026-07-27 17:30:50 +08:00
parent 01b890c21e
commit 2a24910790
3 changed files with 38 additions and 5 deletions
+36 -3
View File
@@ -3,7 +3,7 @@
# 用途:工作證明自動記錄(worklog)。由支援 hook 的 CLI 觸發,
# 抽出本輪工作內容 → 呼叫已安裝 CLI 濃縮成精簡條目 → 機密遮蔽 →
# 追加到 Gitea wiki 的當週工作紀錄頁。工作內容全程不落地。
# 更新時間:2026/07/27 22:16:00
# 更新時間:2026/07/27 17:27:54
# 相依:python3、README 定義的任一 headless CLI、curlwiki 走 Python urllib,不需 curl 亦可)。
# 機密:token 僅由環境變數/本機憑證讀取,不 echo、不寫檔;輸出前套用遮蔽規則。
# 退出碼:一律 0 —— hook 絕不可阻斷使用者的工作流程。
@@ -51,8 +51,8 @@ die_quiet() {
command -v python3 >/dev/null 2>&1 || die_quiet "找不到 python3,略過記錄" "WRN"
select_worklog_cli() {
# 依 README 定義的 headless CLI 選擇摘要執行器;可用 WORKLOG_CLI 強制指定。
local requested="${WORKLOG_CLI:-auto}" cli
# 依目前 hook/session 環境優先選擇摘要執行器;可用 WORKLOG_CLI 強制指定。
local requested="${WORKLOG_CLI:-auto}" cli current_cli
if [ "$requested" != "auto" ]; then
case " ${SUPPORTED_CLIS} " in
*" ${requested} "*) ;;
@@ -62,6 +62,14 @@ select_worklog_cli() {
printf '%s' "$requested"
return 0
fi
current_cli="$(detect_current_cli)"
if [ -n "$current_cli" ]; then
if command -v "$current_cli" >/dev/null 2>&1; then
printf '%s' "$current_cli"
return 0
fi
log "WRN" "目前環境判定為 ${current_cli},但找不到 ${current_cli} CLI,改用可用摘要 CLI"
fi
for cli in $SUPPORTED_CLIS; do
if command -v "$cli" >/dev/null 2>&1; then
printf '%s' "$cli"
@@ -71,6 +79,31 @@ select_worklog_cli() {
die_quiet "找不到可用摘要 CLI(需要其一:${SUPPORTED_CLIS}" "WRN"
}
detect_current_cli() {
# 判斷實際觸發本輪 hook 的助理環境,避免 auto 因 PATH 順序誤顯其他 CLI。
if [ -n "${CODEX_THREAD_ID:-}" ] || [ -n "${CODEX_CI:-}" ] || [ -n "${CODEX_MANAGED_PACKAGE_ROOT:-}" ]; then
printf 'codex'
return 0
fi
if [ -n "${CLAUDE_PLUGIN_ROOT:-}" ] || [ -n "${CLAUDE_CODE_SSE_PORT:-}" ]; then
printf 'claude'
return 0
fi
if [ -n "${AGY_SESSION_ID:-}" ] || [ -n "${AGY_WORKSPACE_ID:-}" ]; then
printf 'agy'
return 0
fi
if [ -n "${OPENCODE_SESSION_ID:-}" ] || [ -n "${OPENCODE_CONFIG:-}" ]; then
printf 'opencode'
return 0
fi
if [ -n "${COPILOT_AGENT_ID:-}" ] || [ -n "${GITHUB_COPILOT_TOKEN:-}" ]; then
printf 'copilot'
return 0
fi
return 0
}
run_summary_cli() {
# 各 CLI 依 README 的 headless 指令呼叫;不把工作內容寫入檔案。
local cli="$1" prompt="$2" model="$3"