feat(role): 支援角色推斷與匯出

This commit is contained in:
Jeffery
2026-07-28 16:07:20 +08:00
parent f302adea1b
commit 3c7c3d16b0
3 changed files with 116 additions and 25 deletions
+79 -4
View File
@@ -5,9 +5,9 @@
# NREM 鞏固(分類/去噪/去重/合併/優先度)→ REM 整合(跨記憶
# 連結/抽象化/提取線索)→ 壓縮歸檔 → 日常與其他依使用頻率與優先度遺忘。
# 另提供 --nap(CLI 閒置時的小睡整理)、--catchup(cron 未執行時的補跑)、
# --force(手動立即整理)、--install-cron--remove-cron(排程安裝與移除)、
# --status(狀態)。
# 更新時間:2026/07/28 14:36:00
# --force(手動立即整理)、--export(匯出角色壓縮檔)、
# --install-cron--remove-cron(排程安裝與移除)、--status(狀態)。
# 更新時間:2026/07/28 16:02:10
# 相依:bash、node、任一 headless CLI、crontab(僅排程安裝需要)、
# 同目錄的 role_lib.sh 與 memory.js。
# 退出碼:0 成功或無事可做;1 參數錯誤或整理失敗(cron 觸發時不影響使用者)。
@@ -32,9 +32,12 @@ usage() {
--nap 小睡觸發:CLI 閒置一段時間且 inbox 達門檻時整理記憶
--catchup 補跑:cron 未執行時,由 SessionStart hook 於背景呼叫
--force 立即整理一次(忽略時段與 AI 運行檢查)
--export <路徑> 匯出目前角色定義、資產與記憶為 .tar.gz
--export <角色 ID> <路徑>
--install-cron 安裝/更新睡眠排程(每小時檢查一次)
--remove-cron 移除睡眠排程
--status 顯示角色、睡眠時段、排程與記憶統計
--diagnose 同 --status
EOF_USAGE
}
@@ -319,6 +322,67 @@ show_status() {
printf '\n'
}
export_role_archive() {
# 匯出目前角色定義、專屬資產與記憶目錄,供備份或轉移使用
local destination="$1" stamp role_def role_assets memory_dir archive_dir archive tmp
[ -n "$destination" ] || { role_log "ERR" "缺少匯出路徑"; return 1; }
command -v tar >/dev/null 2>&1 || { role_log "ERR" "找不到 tar,無法建立壓縮檔"; return 1; }
command -v mktemp >/dev/null 2>&1 || { role_log "ERR" "找不到 mktemp,無法建立暫存目錄"; return 1; }
stamp="$(TZ='Asia/Taipei' date +'%Y%m%d-%H%M%S')"
case "$destination" in
*/)
archive_dir="${destination%/}"
archive="${archive_dir}/${ROLE}-role-export-${stamp}.tar.gz"
;;
*.tar.gz|*.tgz)
archive="$destination"
archive_dir="$(dirname "$archive")"
;;
*)
if [ -d "$destination" ]; then
archive_dir="$destination"
archive="${archive_dir}/${ROLE}-role-export-${stamp}.tar.gz"
else
archive="$destination"
archive_dir="$(dirname "$archive")"
fi
;;
esac
mkdir -p "$archive_dir" 2>/dev/null || { role_log "ERR" "無法建立匯出目錄:${archive_dir}"; return 1; }
role_def="$(role_file "$ROLE")"
role_assets="$(role_home)/${ROLE}.assets"
memory_dir="$(role_memory_home)/${ROLE}"
tmp="$(mktemp -d)" || { role_log "ERR" "無法建立暫存目錄"; return 1; }
mkdir -p "$tmp/.roles" "$tmp/.memory"
cp "$role_def" "$tmp/.roles/${ROLE}.md" || { rm -rf "$tmp"; role_log "ERR" "無法複製角色定義檔"; return 1; }
[ -d "$role_assets" ] && cp -a "$role_assets" "$tmp/.roles/"
[ -d "$memory_dir" ] && cp -a "$memory_dir" "$tmp/.memory/"
cat > "$tmp/role-export.json" <<EOF_EXPORT
{
"role": "${ROLE}",
"exported_at": "$(role_now)",
"format": "jsc-role-export-v1",
"includes": [
".roles/${ROLE}.md",
".roles/${ROLE}.assets",
".memory/${ROLE}"
]
}
EOF_EXPORT
if ! tar -C "$tmp" -czf "$archive" .; then
rm -rf "$tmp"
role_log "ERR" "建立壓縮檔失敗:${archive}"
return 1
fi
rm -rf "$tmp"
role_log "INF" "已匯出角色 ${ROLE}${archive}"
printf '%s\n' "$archive"
return 0
}
# ------------------------------------------------------------------------------
# 進入點
# ------------------------------------------------------------------------------
@@ -368,6 +432,17 @@ case "$MODE" in
require_role
sleep_cycle "手動"
;;
--export)
if [ -n "${3:-}" ]; then
ROLE="$2"
[ -n "$ROLE" ] || { role_log "WRN" "缺少角色 ID"; exit 1; }
[ -f "$(role_file "$ROLE")" ] || { role_log "WRN" "找不到角色定義檔:$(role_file "$ROLE")"; exit 1; }
export_role_archive "$3"
else
require_role
export_role_archive "${2:-}"
fi
;;
--install-cron)
require_role
install_cron
@@ -375,7 +450,7 @@ case "$MODE" in
--remove-cron)
remove_cron
;;
--status)
--status|--diagnose)
require_role
show_status
;;