#!/bin/bash
# 任务名称：业务知识库更新
# 执行时间：每天 07:30
# 归属 Agent：xiaoxi
# 通知对象：李若松（user_id: 4aagb443）

set -e

export PATH=/root/.nvm/versions/node/v24.14.0/bin:$PATH
LOG_FILE="/var/log/xiaoxi_update_business_knowledge.log"

log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}

log "开始更新业务知识库"

# 进入工作区目录
cd /root/.openclaw/workspace

# 1. 同步业务知识到makee_vala目录
cp -r business_knowledge/* makee_vala/ 2>/dev/null || true
log "业务知识已同步到makee_vala目录"

# 2. 发送通知
APP_ID=$(jq -r '.apps[0].appId' /root/.openclaw/credentials/xiaoxi/config.json)
APP_SECRET=$(jq -r '.apps[0].appSecret' /root/.openclaw/credentials/xiaoxi/config.json)
TOKEN=$(curl -s -X POST "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" \
  -H "Content-Type: application/json" \
  -d "{\"app_id\":\"$APP_ID\",\"app_secret\":\"$APP_SECRET\"}" \
  | jq -r '.tenant_access_token')

curl -s -X POST "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=user_id" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"receive_id\":\"4aagb443\",\"msg_type\":\"text\",\"content\":\"{\\\"text\\\":\\\"✅ 每日业务知识库更新完成！更新时间：$(date +'%Y-%m-%d %H:%M:%S')\\\"}\"}" > /dev/null 2>&1

log "业务知识库更新完成"
