#!/bin/bash
# AI 头条工坊（C4.8 封板候选包）启动器：从包内启动工作台并打开浏览器
export PATH="$HOME/.local/bin:/opt/homebrew/bin:/usr/local/bin:$PATH"
APP_DIR="$(cd "$(dirname "$0")/../../.." && pwd)"  # 工作台根目录（.app 的上级）
PORT="${TA_PORT:-8666}"
URL="http://localhost:$PORT"
if curl -s --max-time 2 "$URL/api/health" >/dev/null 2>&1; then
  open "$URL"
  exit 0
fi
cd "$APP_DIR" || exit 1
mkdir -p "$APP_DIR/ai_output"
nohup python3 "$APP_DIR/workbench/server.py" >> "$APP_DIR/ai_output/workbench.log" 2>&1 &
for i in $(seq 1 30); do
  sleep 1
  if curl -s --max-time 2 "$URL/api/health" >/dev/null 2>&1; then break; fi
done
open "$URL"
