refactor: 重构 algorithm 为 ai-core 代码解析服务

- 新增 ai-core 目录,包含代码解析核心服务
- 添加 proto 定义、parser、service 模块
- 添加启动脚本和依赖配置

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-09 10:27:08 +08:00
parent f22f823a4a
commit 797518ec76
15 changed files with 1163 additions and 0 deletions

86
ai-core/start.sh Normal file
View File

@@ -0,0 +1,86 @@
#!/bin/bash
# AI-Core gRPC Server Startup Script
echo "Starting AI-Core Document Parser gRPC Server..."
# 配置
PORT=${1:-50051}
# 使用虚拟环境
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"
# Windows 下使用 PowerShell 的 py 命令或者直接用 venv
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" || -f "venv/Scripts/python.exe" ]]; then
if [ -f "venv/Scripts/python.exe" ]; then
echo "Using virtual environment Python..."
PYTHON_CMD="$SCRIPT_DIR/venv/Scripts/python.exe"
elif command -v py &> /dev/null; then
echo "Using py launcher..."
PYTHON_CMD="py"
else
echo "Error: Python not found"
exit 1
fi
else
# Linux/Mac
if [ -d "venv" ]; then
echo "Activating virtual environment..."
source venv/bin/activate
PYTHON_CMD="python"
else
PYTHON_CMD="python3"
fi
fi
echo "Using Python: $PYTHON_CMD"
$PYTHON_CMD --version
# Check if requirements are installed
$PYTHON_CMD -c "import grpcio" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Installing Python dependencies..."
$PYTHON_CMD -m pip install -r requirements.txt
if [ $? -ne 0 ]; then
echo "Error: Failed to install dependencies"
exit 1
fi
fi
# Generate gRPC code if needed
if [ ! -f "proto/document_parser_pb2.py" ]; then
echo "Generating gRPC code..."
$PYTHON_CMD generate_grpc.py
if [ $? -ne 0 ]; then
echo "Error: Failed to generate gRPC code"
exit 1
fi
fi
# 检查端口占用并释放
echo "Checking port $PORT..."
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
# Windows
NETSTAT_OUTPUT=$(netstat -ano 2>/dev/null | grep ":$PORT" | grep LISTENING)
if [ -n "$NETSTAT_OUTPUT" ]; then
echo "Port $PORT is in use, killing process..."
PID=$(echo "$NETSTAT_OUTPUT" | awk '{print $NF}' | head -1)
if [ -n "$PID" ]; then
taskkill //F //PID $PID 2>/dev/null
sleep 1
fi
fi
else
# Linux/Mac
PID=$(lsof -ti:$PORT 2>/dev/null)
if [ -n "$PID" ]; then
echo "Port $PORT is in use, killing process $PID..."
kill $PID 2>/dev/null
sleep 1
fi
fi
# Start the server
echo "Starting server on port $PORT..."
$PYTHON_CMD main.py --port $PORT --max-workers 10 --log-level INFO