Files
my-project/config/openclaw/scripts/hybrid-learning-cycle.sh
T

111 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# 混合学习循环 - hybrid-learning-cycle.sh
# 根据配额使用率自动控制学习节奏
# 每5小时一个周期,根据周期内不同时间的阈值执行学习
set -e
LOG_FILE="/root/.openclaw/logs/hybrid-learning.log"
API_URL="http://192.168.0.66:9527/api/quota"
# 阈值配置(周期内小时数 -> 阈值)
declare -A THRESHOLDS
THRESHOLDS[1]=15
THRESHOLDS[2]=30
THRESHOLDS[3]=45
THRESHOLDS[4]=60
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# 获取配额使用率
get_quota_usage() {
local response
response=$(curl -s --max-time 5 "$API_URL" 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$response" ]; then
echo "$response" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('usage',d.get('percent',0)))" 2>/dev/null || echo "0"
else
echo "0"
fi
}
# 获取当前时间在周期内的位置(1-5)
get_cycle_position() {
local hour minute cycle_minutes
hour=$(date '+%-H') # 不带前导零
minute=$(date '+%-M')
cycle_minutes=$((hour % 5 * 60 + minute))
position=$((cycle_minutes / 60 + 1))
echo "$position"
}
# 检查今天是否已经学习过(避免同一天多次学习)
check_today_already_learned() {
local today
today=$(date '+%Y-%m-%d')
if grep -q "$today.*学习执行完成" "$LOG_FILE" 2>/dev/null; then
return 0 # 已学习
else
return 1 # 未学习
fi
}
main() {
log "========== 混合学习周期开始 =========="
# 获取当前周期位置
local cycle_pos
cycle_pos=$(get_cycle_position)
log "当前周期位置: 第${cycle_pos}小时 (每5小时一个周期)"
# 获取对应阈值
local threshold
threshold=${THRESHOLDS[$cycle_pos]:-60}
log "当前阈值: ${threshold}%"
# 检查今天是否已学习
if check_today_already_learned; then
log "⚠️ 今天已学习过,跳过本次执行"
log "========== 混合学习周期结束 =========="
echo ""
return
fi
# 获取配额使用率
local quota_usage
quota_usage=$(get_quota_usage)
log "当前配额使用率: ${quota_usage}%"
# 检查是否需要学习
local needs_learning=false
if [ "$quota_usage" -lt "$threshold" ]; then
needs_learning=true
log "配额使用率低于阈值,准备执行学习"
else
log "配额使用率高于阈值,跳过本次学习"
fi
# 如果需要学习,执行自动学习脚本
if [ "$needs_learning" = true ]; then
log "执行自动学习..."
cd /root/.openclaw/scripts
# 保守执行:使用较短超时
timeout 300 python3 auto-learning.py >> "$LOG_FILE" 2>&1
if [ $? -eq 0 ]; then
log "学习执行完成"
else
log "学习执行遇到问题(可能超时)"
fi
fi
log "========== 混合学习周期结束 =========="
echo ""
}
main "$@"