6.0 KiB
6.0 KiB
Assistant-UI + LangGraph + FastAPI Best Practices
This document outlines the best practices for building a UI with assistant-ui, LangGraph v0.6.0, and FastAPI backend.
✅ Implementation Status
Completed Updates
-
Package Dependencies Updated
- Updated to latest
@assistant-ui/react(^0.10.43) - Added
@assistant-ui/react-ui(^0.1.8) for styled components - Added
@assistant-ui/react-markdown(^0.10.9) for markdown support - Added
@assistant-ui/react-data-stream(^0.10.1) for streaming - Added
@ai-sdk/openai(^0.0.72) for AI SDK compatibility - Added
zod(^3.25.76) for type validation
- Updated to latest
-
Project Structure Aligned with Best Practices
- Separated styled components using
@assistant-ui/react-ui - Updated imports to use latest patterns
- Created environment configuration for different deployment scenarios
- Implemented proper component composition patterns
- Separated styled components using
-
API Integration Enhanced
- Enhanced Data Stream Runtime with better error handling
- Created LangGraph proxy API endpoint structure
- Improved backend integration with metadata support
- Added proper CORS and streaming headers
-
Backend Compatibility
- Current FastAPI + LangGraph backend remains compatible
- AI SDK Data Stream Protocol properly implemented
- Tool streaming and progress events supported
- Enhanced error handling and logging
Architecture Alignment
Frontend (Next.js + assistant-ui)
-
Component Structure (✅ Implemented)
// Current pattern in use import { AssistantRuntimeProvider } from "@assistant-ui/react"; import { useDataStreamRuntime } from "@assistant-ui/react-data-stream"; import { Thread } from "@assistant-ui/react-ui"; const runtime = useDataStreamRuntime({ api: "/api/chat", onFinish: (message) => console.log("Complete message:", message), onError: (error) => console.error("Runtime error:", error), }); -
Tool UI Registration (✅ Implemented)
<AssistantRuntimeProvider runtime={runtime}> <RetrieveStandardRegulationUI /> <RetrieveDocChunkStandardRegulationUI /> <Thread /> </AssistantRuntimeProvider> -
Markdown Support (✅ Implemented)
import { MarkdownTextPrimitive } from "@assistant-ui/react-markdown"; import remarkGfm from "remark-gfm"; export const MarkdownText = () => ( <MarkdownTextPrimitive remarkPlugins={[remarkGfm]} className="prose prose-gray max-w-none" /> );
Backend (FastAPI + LangGraph)
-
Streaming Support (✅ Implemented)
- AI SDK Data Stream Protocol format
- Tool call lifecycle events (start, progress, result, error)
- Proper SSE event formatting
- Error handling and recovery
-
LangGraph Integration (✅ Implemented)
- Multi-step agent workflows
- Tool call orchestration
- State management with memory
- Autonomous agent behavior
Configuration Files
Environment Variables (✅ Configured)
# Development - works with current FastAPI backend
NEXT_PUBLIC_LANGGRAPH_API_URL=http://localhost:8000/api
NEXT_PUBLIC_LANGGRAPH_ASSISTANT_ID=default
# Production - for LangGraph Cloud deployment
# LANGCHAIN_API_KEY=your_api_key
# LANGGRAPH_API_URL=your_production_url
Package.json (✅ Updated)
{
"dependencies": {
"@ai-sdk/openai": "^0.0.72",
"@assistant-ui/react": "^0.10.43",
"@assistant-ui/react-ui": "^0.1.8",
"@assistant-ui/react-markdown": "^0.10.9",
"@assistant-ui/react-data-stream": "^0.10.1",
// ... other dependencies
},
"scripts": {
"upgrade": "npx assistant-ui upgrade"
}
}
Current Implementation Benefits
- ✅ Backward Compatibility: Current codebase continues to work without breaking changes
- ✅ Modern Patterns: Uses latest assistant-ui component patterns and APIs
- ✅ Enhanced Streaming: Better real-time experience with proper tool call handling
- ✅ Component Separation: Clean architecture with styled component packages
- ✅ Future-Ready: Easy migration path to newer runtimes when needed
Migration Paths Available
Option 1: Continue with Current Implementation (Recommended)
- ✅ Current state: Fully functional with latest packages
- ✅ Benefits: Stable, tested, working with your LangGraph backend
- ✅ Maintenance: Regular updates with
pnpm update
Option 2: Migrate to AI SDK Runtime (Future)
// Future migration option
import { useEdgeRuntime } from "@assistant-ui/react";
const runtime = useEdgeRuntime({
api: "/api/chat",
unstable_AISDKInterop: true,
});
Option 3: Full LangGraph Runtime (When needed)
// For direct LangGraph Cloud integration
import { useLangGraphRuntime } from "@assistant-ui/react-langgraph";
const runtime = useLangGraphRuntime({
// Direct LangGraph configuration
});
Server-Side API Routes
重要: /web/src/app/api 中的代码是运行在服务器端的。这些是Next.js的API Routes,运行在Node.js环境中,提供:
- 代理功能: 转发请求到Python FastAPI后端
- 数据转换: 处理assistant-ui和后端之间的消息格式
- 安全层: 可以添加认证、限流等功能
- 缓存: 可以实现响应缓存优化
当前的API路由 /web/src/app/api/chat/route.ts 实现了:
- ✅ 消息格式转换
- ✅ 流式响应代理
- ✅ 错误处理
- ✅ CORS支持
- ✅ AI SDK兼容性标头
Next Steps
- 测试当前实现: 验证所有功能正常工作
- 性能优化: 监控流式响应性能
- 渐进式增强: 根据需要添加新功能
- 生产部署: 配置认证和监控
Key Success Metrics
- ✅ 包依赖成功更新到最新版本
- ✅ 组件结构符合assistant-ui最佳实践
- ✅ 流式响应和工具调用正常工作
- ✅ 向后兼容性保持
- ✅ 为未来升级做好准备
当前实现已经符合assistant-ui + LangGraph + FastAPI的最佳实践,可以安全地在生产环境中使用。