mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-04-25 11:18:22 +00:00
* feat(gateway): implement LangGraph Platform API in Gateway, replace langgraph-cli
Implement all core LangGraph Platform API endpoints in the Gateway,
allowing it to fully replace the langgraph-cli dev server for local
development. This eliminates a heavyweight dependency and simplifies
the development stack.
Changes:
- Add runs lifecycle endpoints (create, stream, wait, cancel, join)
- Add threads CRUD and search endpoints
- Add assistants compatibility endpoints (search, get, graph, schemas)
- Add StreamBridge (in-memory pub/sub for SSE) and async provider
- Add RunManager with atomic create_or_reject (eliminates TOCTOU race)
- Add worker with interrupt/rollback cancel actions and runtime context injection
- Route /api/langgraph/* to Gateway in nginx config
- Skip langgraph-cli startup by default (SKIP_LANGGRAPH_SERVER=0 to restore)
- Add unit tests for RunManager, SSE format, and StreamBridge
* fix: drain bridge queue on client disconnect to prevent backpressure
When on_disconnect=continue, keep consuming events from the bridge
without yielding, so the worker is not blocked by a full queue.
Only on_disconnect=cancel breaks out immediately.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* fix: remove pytest import
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* fix: Fix default stream_mode to ["values", "messages-tuple"]
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* fix: Remove unused if_exists field from ThreadCreateRequest
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* fix: address review comments on gateway LangGraph API
- Mount runs.py router in app.py (missing include_router)
- Normalize interrupt_before/after "*" to node list before run_agent()
- Use entry.id for SSE event ID instead of counter
- Drain bridge queue on disconnect when on_disconnect=continue
- Reuse serialization helper in wait_run() for consistent wire format
- Reject unsupported multitask_strategy with 400
- Remove SKIP_LANGGRAPH_SERVER fallback, always use Gateway
* feat: extract app.state access into deps.py
Encapsulate read/write operations for singleton objects (RunManager,
StreamBridge, checkpointer) held in app.state into a shared utility,
reducing repeated access patterns across router modules.
* feat: extract deerflow.runtime.serialization module with tests
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* refactor: replace duplicated serialization with deerflow.runtime.serialization
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: extract app/gateway/services.py with run lifecycle logic
Create a service layer that centralizes SSE formatting, input/config
normalization, and run lifecycle management. Router modules will delegate
to these functions instead of using private cross-imported helpers.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* refactor: wire routers to use services layer, remove cross-module private imports
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* style: apply ruff formatting to refactored files
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat(runtime): support LangGraph dev server and add compat route
- Enable official LangGraph dev server for local development workflow
- Decouple runtime components from agents package for better separation
- Provide gateway-backed fallback route when dev server is skipped
- Simplify lifecycle management using context manager in gateway
* feat(runtime): add Store providers with auto-backend selection
- Add async_provider.py and provider.py under deerflow/runtime/store/
- Support memory, sqlite, postgres backends matching checkpointer config
- Integrate into FastAPI lifespan via AsyncExitStack in deps.py
- Replace hardcoded InMemoryStore with config-driven factory
* refactor(gateway): migrate thread management from checkpointer to Store and resolve multiple endpoint failures
- Add Store-backed CRUD helpers (_store_get, _store_put, _store_upsert)
- Replace checkpoint-scanning search with two-phase strategy:
phase 1 reads Store (O(threads)), phase 2 backfills from checkpointer
for legacy/LangGraph Server threads with lazy migration
- Extend Store record schema with values field for title persistence
- Sync thread title from checkpoint to Store after run completion
- Fix /threads/{id}/runs/{run_id}/stream 405 by accepting both
GET and POST methods; POST handles interrupt/rollback actions
- Fix /threads/{id}/state 500 by separating read_config and
write_config, adding checkpoint_ns to configurable, and
shallow-copying checkpoint/metadata before mutation
- Sync title to Store on state update for immediate search reflection
- Move _upsert_thread_in_store into services.py, remove duplicate logic
- Add _sync_thread_title_after_run: await run task, read final
checkpoint title, write back to Store record
- Spawn title sync as background task from start_run when Store exists
* refactor(runtime): deduplicate store and checkpointer provider logic
Extract _ensure_sqlite_parent_dir() helper into checkpointer/provider.py
and use it in all three places that previously inlined the same mkdir logic.
Consolidate duplicate error constants in store/async_provider.py by importing
from store/provider.py instead of redefining them.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* refactor(runtime): move SQLite helpers to runtime/store, checkpointer imports from store
_resolve_sqlite_conn_str and _ensure_sqlite_parent_dir now live in
runtime/store/provider.py. agents/checkpointer/provider and
agents/checkpointer/async_provider import from there, reversing the
previous dependency direction (store → checkpointer becomes
checkpointer → store).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* refactor(runtime): extract SQLite helpers into runtime/store/_sqlite_utils.py
Move resolve_sqlite_conn_str and ensure_sqlite_parent_dir out of
checkpointer/provider.py into a dedicated _sqlite_utils module.
Functions are now public (no underscore prefix), making cross-module
imports semantically correct. All four provider files import from
the single shared location.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix(gateway): use adelete_thread to fully remove thread checkpoints on delete
AsyncSqliteSaver has no adelete method — the previous hasattr check
always evaluated to False, silently leaving all checkpoint rows in the
database. Switch to adelete_thread(thread_id) which deletes every
checkpoint and pending-write row for the thread across all namespaces
(including sub-graph checkpoints).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix(gateway): remove dead bridge_cm/ckpt_cm code and fix StrEnum lint
app.py had unreachable code after the async-with lifespan refactor:
bridge_cm and ckpt_cm were referenced but never defined (F821), and
the channel service startup/shutdown was outside the langgraph_runtime
block so it never ran. Move channel service lifecycle inside the
async-with block where it belongs.
Replace str+Enum inheritance in RunStatus and DisconnectMode with
StrEnum as suggested by UP042.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* style: format with ruff
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: JeffJiang <for-eleven@hotmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
220 lines
8.9 KiB
Bash
Executable File
220 lines
8.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# start.sh - Start all DeerFlow development services
|
|
#
|
|
# Must be run from the repo root directory.
|
|
|
|
set -e
|
|
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
cd "$REPO_ROOT"
|
|
|
|
# ── Load environment variables from .env ──────────────────────────────────────
|
|
if [ -f "$REPO_ROOT/.env" ]; then
|
|
set -a
|
|
source "$REPO_ROOT/.env"
|
|
set +a
|
|
fi
|
|
|
|
# ── Argument parsing ─────────────────────────────────────────────────────────
|
|
|
|
DEV_MODE=true
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--dev) DEV_MODE=true ;;
|
|
--prod) DEV_MODE=false ;;
|
|
*) echo "Unknown argument: $arg"; echo "Usage: $0 [--dev|--prod]"; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
if $DEV_MODE; then
|
|
FRONTEND_CMD="pnpm run dev"
|
|
else
|
|
FRONTEND_CMD="env BETTER_AUTH_SECRET=$(python3 -c 'import secrets; print(secrets.token_hex(16))') pnpm run preview"
|
|
fi
|
|
|
|
# ── Stop existing services ────────────────────────────────────────────────────
|
|
|
|
echo "Stopping existing services if any..."
|
|
pkill -f "langgraph dev" 2>/dev/null || true
|
|
pkill -f "uvicorn app.gateway.app:app" 2>/dev/null || true
|
|
pkill -f "next dev" 2>/dev/null || true
|
|
pkill -f "next-server" 2>/dev/null || true
|
|
nginx -c "$REPO_ROOT/docker/nginx/nginx.local.conf" -p "$REPO_ROOT" -s quit 2>/dev/null || true
|
|
sleep 1
|
|
pkill -9 nginx 2>/dev/null || true
|
|
killall -9 nginx 2>/dev/null || true
|
|
./scripts/cleanup-containers.sh deer-flow-sandbox 2>/dev/null || true
|
|
sleep 1
|
|
|
|
# ── Banner ────────────────────────────────────────────────────────────────────
|
|
|
|
echo ""
|
|
echo "=========================================="
|
|
echo " Starting DeerFlow Development Server"
|
|
echo "=========================================="
|
|
echo ""
|
|
if $DEV_MODE; then
|
|
echo " Mode: DEV (hot-reload enabled)"
|
|
echo " Tip: run \`make start\` in production mode"
|
|
else
|
|
echo " Mode: PROD (hot-reload disabled)"
|
|
echo " Tip: run \`make dev\` to start in development mode"
|
|
fi
|
|
echo ""
|
|
echo "Services starting up..."
|
|
echo " → Backend: LangGraph + Gateway"
|
|
echo " → Frontend: Next.js"
|
|
echo " → Nginx: Reverse Proxy"
|
|
echo ""
|
|
|
|
# ── Config check ─────────────────────────────────────────────────────────────
|
|
|
|
if ! { \
|
|
[ -n "$DEER_FLOW_CONFIG_PATH" ] && [ -f "$DEER_FLOW_CONFIG_PATH" ] || \
|
|
[ -f backend/config.yaml ] || \
|
|
[ -f config.yaml ]; \
|
|
}; then
|
|
echo "✗ No DeerFlow config file found."
|
|
echo " Checked these locations:"
|
|
echo " - $DEER_FLOW_CONFIG_PATH (when DEER_FLOW_CONFIG_PATH is set)"
|
|
echo " - backend/config.yaml"
|
|
echo " - ./config.yaml"
|
|
echo ""
|
|
echo " Run 'make config' from the repo root to generate ./config.yaml, then set required model API keys in .env or your config file."
|
|
exit 1
|
|
fi
|
|
|
|
# ── Auto-upgrade config ──────────────────────────────────────────────────
|
|
|
|
"$REPO_ROOT/scripts/config-upgrade.sh"
|
|
|
|
# ── Cleanup trap ─────────────────────────────────────────────────────────────
|
|
|
|
cleanup() {
|
|
trap - INT TERM
|
|
echo ""
|
|
echo "Shutting down services..."
|
|
if [ "${SKIP_LANGGRAPH_SERVER:-0}" != "1" ]; then
|
|
pkill -f "langgraph dev" 2>/dev/null || true
|
|
fi
|
|
pkill -f "uvicorn app.gateway.app:app" 2>/dev/null || true
|
|
pkill -f "next dev" 2>/dev/null || true
|
|
pkill -f "next start" 2>/dev/null || true
|
|
pkill -f "next-server" 2>/dev/null || true
|
|
# Kill nginx using the captured PID first (most reliable),
|
|
# then fall back to pkill/killall for any stray nginx workers.
|
|
if [ -n "${NGINX_PID:-}" ] && kill -0 "$NGINX_PID" 2>/dev/null; then
|
|
kill -TERM "$NGINX_PID" 2>/dev/null || true
|
|
sleep 1
|
|
kill -9 "$NGINX_PID" 2>/dev/null || true
|
|
fi
|
|
pkill -9 nginx 2>/dev/null || true
|
|
killall -9 nginx 2>/dev/null || true
|
|
echo "Cleaning up sandbox containers..."
|
|
./scripts/cleanup-containers.sh deer-flow-sandbox 2>/dev/null || true
|
|
echo "✓ All services stopped"
|
|
exit 0
|
|
}
|
|
trap cleanup INT TERM
|
|
|
|
# ── Start services ────────────────────────────────────────────────────────────
|
|
|
|
mkdir -p logs
|
|
|
|
if $DEV_MODE; then
|
|
LANGGRAPH_EXTRA_FLAGS="--no-reload"
|
|
GATEWAY_EXTRA_FLAGS="--reload --reload-include='*.yaml' --reload-include='.env' --reload-exclude='*.pyc' --reload-exclude='__pycache__' --reload-exclude='sandbox/' --reload-exclude='.deer-flow/'"
|
|
else
|
|
LANGGRAPH_EXTRA_FLAGS="--no-reload"
|
|
GATEWAY_EXTRA_FLAGS=""
|
|
fi
|
|
|
|
if [ "${SKIP_LANGGRAPH_SERVER:-0}" != "1" ]; then
|
|
echo "Starting LangGraph server..."
|
|
# Read log_level from config.yaml, fallback to env var, then to "info"
|
|
CONFIG_LOG_LEVEL=$(grep -m1 '^log_level:' config.yaml 2>/dev/null | awk '{print $2}' | tr -d ' ')
|
|
LANGGRAPH_LOG_LEVEL="${LANGGRAPH_LOG_LEVEL:-${CONFIG_LOG_LEVEL:-info}}"
|
|
(cd backend && NO_COLOR=1 uv run langgraph dev --no-browser --allow-blocking --server-log-level $LANGGRAPH_LOG_LEVEL $LANGGRAPH_EXTRA_FLAGS > ../logs/langgraph.log 2>&1) &
|
|
./scripts/wait-for-port.sh 2024 60 "LangGraph" || {
|
|
echo " See logs/langgraph.log for details"
|
|
tail -20 logs/langgraph.log
|
|
if grep -qE "config_version|outdated|Environment variable .* not found|KeyError|ValidationError|config\.yaml" logs/langgraph.log 2>/dev/null; then
|
|
echo ""
|
|
echo " Hint: This may be a configuration issue. Try running 'make config-upgrade' to update your config.yaml."
|
|
fi
|
|
cleanup
|
|
}
|
|
echo "✓ LangGraph server started on localhost:2024"
|
|
else
|
|
echo "⏩ Skipping LangGraph server (SKIP_LANGGRAPH_SERVER=1)"
|
|
echo " Use /api/langgraph-compat/* via Gateway instead"
|
|
fi
|
|
|
|
echo "Starting Gateway API..."
|
|
(cd backend && PYTHONPATH=. uv run uvicorn app.gateway.app:app --host 0.0.0.0 --port 8001 $GATEWAY_EXTRA_FLAGS > ../logs/gateway.log 2>&1) &
|
|
./scripts/wait-for-port.sh 8001 30 "Gateway API" || {
|
|
echo "✗ Gateway API failed to start. Last log output:"
|
|
tail -60 logs/gateway.log
|
|
echo ""
|
|
echo "Likely configuration errors:"
|
|
grep -E "Failed to load configuration|Environment variable .* not found|config\.yaml.*not found" logs/gateway.log | tail -5 || true
|
|
echo ""
|
|
echo " Hint: Try running 'make config-upgrade' to update your config.yaml with the latest fields."
|
|
cleanup
|
|
}
|
|
echo "✓ Gateway API started on localhost:8001"
|
|
|
|
echo "Starting Frontend..."
|
|
(cd frontend && $FRONTEND_CMD > ../logs/frontend.log 2>&1) &
|
|
./scripts/wait-for-port.sh 3000 120 "Frontend" || {
|
|
echo " See logs/frontend.log for details"
|
|
tail -20 logs/frontend.log
|
|
cleanup
|
|
}
|
|
echo "✓ Frontend started on localhost:3000"
|
|
|
|
echo "Starting Nginx reverse proxy..."
|
|
nginx -g 'daemon off;' -c "$REPO_ROOT/docker/nginx/nginx.local.conf" -p "$REPO_ROOT" > logs/nginx.log 2>&1 &
|
|
NGINX_PID=$!
|
|
./scripts/wait-for-port.sh 2026 10 "Nginx" || {
|
|
echo " See logs/nginx.log for details"
|
|
tail -10 logs/nginx.log
|
|
cleanup
|
|
}
|
|
echo "✓ Nginx started on localhost:2026"
|
|
|
|
# ── Ready ─────────────────────────────────────────────────────────────────────
|
|
|
|
echo ""
|
|
echo "=========================================="
|
|
if $DEV_MODE; then
|
|
echo " ✓ DeerFlow development server is running!"
|
|
else
|
|
echo " ✓ DeerFlow production server is running!"
|
|
fi
|
|
echo "=========================================="
|
|
echo ""
|
|
echo " 🌐 Application: http://localhost:2026"
|
|
echo " 📡 API Gateway: http://localhost:2026/api/*"
|
|
if [ "${SKIP_LANGGRAPH_SERVER:-0}" = "1" ]; then
|
|
echo " 🤖 LangGraph: skipped (SKIP_LANGGRAPH_SERVER=1)"
|
|
else
|
|
echo " 🤖 LangGraph: http://localhost:2026/api/langgraph/* (served by langgraph dev)"
|
|
fi
|
|
echo " 🧪 LangGraph Compat (experimental): http://localhost:2026/api/langgraph-compat/* (served by Gateway)"
|
|
if [ "${SKIP_LANGGRAPH_SERVER:-0}" = "1" ]; then
|
|
echo ""
|
|
echo " 💡 Set NEXT_PUBLIC_LANGGRAPH_BASE_URL=/api/langgraph-compat in frontend/.env.local"
|
|
fi
|
|
echo ""
|
|
echo " 📋 Logs:"
|
|
echo " - LangGraph: logs/langgraph.log"
|
|
echo " - Gateway: logs/gateway.log"
|
|
echo " - Frontend: logs/frontend.log"
|
|
echo " - Nginx: logs/nginx.log"
|
|
echo ""
|
|
echo "Press Ctrl+C to stop all services"
|
|
|
|
wait
|