Fastapi agent module skill

From jiushiwon/wg-skills · View interactive version

Fastapi agent module skill is an AI agent skill from the GitHub repository jiushiwon/wg-skills. FastAPI AI Agent 模块。基于 LangGraph 实现对话 Agent,支持 Tool 扩展、模型接入、上下文记忆、多轮对话、限流、审计、Pydantic 参数校验、tenacity 重试。面向已安装 fastapi-init-skill + fastapi-auth-module-skill 的项目。触发词:"FastAPI AI Agent"、"LangGraph Agent"、"AI 对话模块"、"FastAPI Tool"、"帮我加一个 AI 对话模块"。

How to use this skill

Skill file

FastAPI Agent Module Skill

为 FastAPI 项目叠加一套 AI Agent 能力,基于 LangGraph 框架实现。

定位

骨架依赖

本模块依赖 fastapi-init-skill 骨架 + fastapi-auth-module-skill 鉴权模块。

使用前必须满足:

  1. ✅ 已安装 fastapi-init-skill(项目骨架)
  2. ✅ 已安装 fastapi-auth-module-skill(用户鉴权)
  3. ✅ 骨架包含:JWT、统一响应(EnvelopeRoute)、SQLModel、分页、异常处理(BusinessException)

对接规范(必须遵循):

  1. ✅ 使用骨架的 app.config.settings 配置系统,不单独定义配置
  2. ✅ 使用骨架的 app.response.EnvelopeRoute 统一响应
  3. ✅ 使用骨架的 app.exceptions.BusinessException 异常处理
  4. ✅ 使用骨架的 settings.db_prefix 表前缀
  5. ✅ 使用骨架的 database.get_session 数据库连接
  6. ✅ 使用骨架的 auth.dependencies.get_current_user 鉴权
  7. ✅ lifespan 中初始化 AgentContainer,通过 app.state.agent 注入

用户问题(最多 3 个)

1. 用什么模型?(默认 OpenAI gpt-4o-mini,可选 Claude/Anthropic/本地模型)
2. 需要哪些内置 Tool?(默认:查用户信息、查角色、查菜单、查组织)
3. 是否开启历史消息?(默认开启,保留 20 轮)

核心能力清单

| # | 能力 | 说明 |

|---|------|------|

| 1 | LangGraph Agent | 基于 LangGraph 的对话 Agent,支持节点扩展 |

| 2 | Tool 系统 | 声明式 Tool 定义,Pydantic 参数校验,自动权限注入 |

| 3 | 模型接入 | 统一模型接口,支持 OpenAI/Claude,tenacity 退避重试 |

| 4 | 多轮对话 | 支持上下文记忆,会话级别历史,LRU 内存缓冲 |

| 5 | 对话管理 | 创建/查询/删除/清除会话 |

| 6 | 租户隔离 | org_tools 全部按 tenant 过滤,多租户安全 |

| 7 | 流式输出 | SSE 流式返回 token,含断连检测 + 代理缓冲禁用 |

| 8 | Rate Limiting | slowapi 实现,每用户 10 次/分钟(防 LLM 财务风险) |

| 9 | 审计日志 | 结构化审计 logger,记录 user/tool/token/失败事件 |

| 10 | 异常脱敏 | 客户端仅收到固定话术,真实异常写入服务端日志 |

| 11 | 数据库索引 | migration 自动创建关键索引(user_id / session_id / created_at) |

| 12 | 事务一致性 | chat() 单事务写入用户/助手消息,无幽灵消息 |

| 13 | 测试基建 | tests/ 目录提供 20+ 安全/一致性/性能测试 |

内置 Tools(全部带 current_user_id 强制注入)

| Tool | 功能 | 参数 | 权限 |

|------|------|------|------|

| get_user_info | 查询当前用户基本信息(脱敏) | current_user_id(系统注入) | 仅查自己 |

| get_user_roles | 查询当前用户角色 | current_user_id(系统注入) | 仅查自己 |

| get_user_menus | 查询当前用户菜单 | current_user_id(系统注入) | 仅查自己 |

| search_users | 搜索用户(脱敏:ID/用户名/昵称) | keyword?, limit ∈ [1,50] | 限流 |

| get_org_tree | 查询当前租户组织架构树 | current_user_id | 租户隔离 |

| get_org_detail | 查询当前租户部门详情 | org_id, current_user_id | 租户隔离 |

| get_post_list | 查询当前租户岗位(分页) | current_user_id, status?, page?, page_size ∈ [1,50] | 租户隔离 |

| get_tenant_info | 查询当前用户所属租户 | current_user_id | 仅查自己 |

| safe_read_file | 读取项目内受限文本文件(编程助手) | path, max_size?, current_user_id | 路径白名单 + 大小限制 + UTF-8 + 审计 |

| safe_write_file | 写入项目内文件(编程助手) | path, new_content, confirm, current_user_id | diff 预览 + 路径白名单 + 大小限制 + 审计 |

文件操作 Tools(编程助手场景标准实现)

safe_read_file / safe_write_file 是编程助手场景(DeepSeek/Cursor 风格)的标准安全实现,提供四重防护:

| 防护维度 | 实现 |

|----------|------|

| 路径白名单 | 仅允许访问 PROJECT_ROOT 下的文件,拦截 /etc /root ~/.ssh /var/log 等敏感目录 |

| 大小限制 | 读默认 100KB、写默认 1MB(可通过参数覆盖) |

| 编码限制 | 仅支持 UTF-8 文本(防二进制崩溃/越权读二进制配置) |

| 审计日志 | 每次读写均记录 user_id / path / size / success |

写文件流程(必须两步调用):

1. confirm=False → 返回 diff 预览,不写盘
2. confirm=True  → 二次调用(带相同 path+new_content)才真正落盘

使用前必须配置 PROJECT_ROOT:

# 必须设置环境变量,限定文件操作 Tool 的访问范围
export PROJECT_ROOT=/path/to/your/project
# 容器化部署建议:
#   docker run -e PROJECT_ROOT=/app -v $(pwd):/app your-image
# 不设置则默认当前工作目录(开发模式)

⚠️ 严禁 把 PROJECT_ROOT 设置为 /、/home 等宽泛目录,否则任何 path 都能访问。

生成的模块结构

src/agent/
├── __init__.py                # 导出核心组件
├── audit.py                   # ✅ 结构化审计日志
├── rate_limiter.py            # ✅ slowapi 限流 + 降级 token bucket
├── schemas.py                 # Pydantic 模型
├── models.py                  # SQLModel(会话/消息)
├── migration.py               # ✅ Alembic 迁移(含 5 个关键索引)
├── database.py                # 数据库适配层(兼容 3 级 import 路径)
├── graph/
│   ├── __init__.py
│   ├── agent.py              # ✅ AgentContainer(DI 容器)+ 加锁懒加载
│   ├── nodes.py              # ✅ Tool 并发执行 + XML 标签隔离 Prompt Injection
│   └── state.py              # Agent State
├── tools/
│   ├── __init__.py
│   ├── base.py               # ✅ Pydantic 校验 + 异常脱敏 + 审计
│   ├── user_tools.py         # ✅ current_user_id 必需参数
│   ├── org_tools.py          # ✅ 多租户隔离
│   ├── file_tools.py         # ✅ 编程助手场景:路径白名单 + diff 预览 + 审计
│   └── registry.py           # 线程安全注册表
├── llm/
│   ├── __init__.py
│   ├── base.py               # LLM 接口
│   ├── openai.py             # ✅ tenacity 重试 + timeout
│   └── anthropic.py          # ✅ tenacity 重试 + timeout
├── memory/
│   ├── __init__.py
│   ├── buffer.py             # 对话记忆(deque 自动裁剪)
│   └── store.py              # ✅ DI 工厂 + LRU + 显式清空
└── routers/
    ├── __init__.py
    ├── chat.py               # ✅ Rate Limit + SSE 断连检测
    ├── session.py            # ✅ Rate Limit + 分页边界 + clear-memory
    └── tools.py              # Tool 管理

tests/                          # ✅ 测试基建
    test_tool_validation.py     # Pydantic / 参数安全
    test_message_security.py     # 注入防护 / 事务 / 限流 / 索引

api-contract-agent.md           # 接口契约(含 SSE Tool 限制说明)
docs/deployment-guide.md       # 多 Worker 部署指南

Agent 工作流

用户输入 → Agent State 初始化(user_id 注入)
    ↓
LLM 生成思考 + Tool Call
    ↓
并发执行 Tools(asyncio.gather,user_id 强制注入防越权)
    ↓
Tool 结果用 <tool_result> XML 标签包裹(防 Prompt Injection)
    ↓
LLM 总结 → 流式返回(含断连检测)
    ↓
单事务持久化用户消息 + 助手消息(保证一致性)

安全特性(v2 - P0 修复后)

1. 会话归属校验

2. Tool 权限注入

3. Tool 参数 Pydantic 校验

4. 异常脱敏

5. 日志脱敏

6. 审计日志

7. Rate Limiting

8. 事务一致性

9. SSE 断连检测

10. 多租户隔离

11. Prompt Injection 防护

12. 数据库索引(migration.py)

13. LLM 调用可靠性

Tool 定义示例

from src.agent.tools import tool

@tool(description="获取当前用户信息", name="get_user_info")
async def get_user_info(current_user_id: int) -> dict:
    """查询当前用户基本信息(不含手机/邮箱)"""
    from src.auth.services.user_service import UserService
    user = await UserService.get_user(current_user_id)
    return {
        "id": user.id,
        "username": user.username,
        "nickname": user.nickname,
    }

接口契约

| 路径 | 说明 |

|------|------|

| POST /api/agent/chat | 对话(stream=true 流式 / stream=false 同步) |

| GET /api/agent/sessions | 会话列表 |

| POST /api/agent/sessions | 创建会话 |

| GET /api/agent/sessions/{id} | 获取会话 |

| DELETE /api/agent/sessions/{id} | 删除会话(软删除) |

| GET /api/agent/sessions/{id}/messages | 获取会话消息 |

| POST /api/agent/sessions/clear-memory | 清除会话内存记忆 |

| GET /api/agent/tools | 可用工具列表 |

流式模式不支持 Tools(受 SSE 特性限制),Tools 必须使用同步模式。

配置扩展

在骨架的 app/config.py 中添加以下字段:

# Agent 模块配置
agent_model: str = "gpt-4o-mini"
agent_temperature: float = 0.7
agent_max_tokens: int = 2048
agent_memory_turns: int = 20
agent_system_prompt: str | None = None  # 自定义 System Prompt

# LLM API 配置
openai_api_key: str | None = None
openai_base_url: str | None = None
anthropic_api_key: str | None = None

生成流程

  1. 确认已存在 FastAPI 骨架 + Auth 模块。
  2. 询问用户模型选择、内置 Tool、记忆轮数。
  3. 在骨架的 app/config.py 中扩展 Agent 配置字段。
  4. 按模板生成 agent/ 下全部源码(含 audit.py / rate_limiter.py)。
  5. 生成 tests/ 测试文件(覆盖 P0 安全约束)。
  6. 生成 api-contract-agent.md 与 docs/deployment-guide.md。
  7. 提示用户安装依赖:pip install langgraph langchain langchain-openai langchain-anthropic tenacity slowapi 等。

依赖安装

pip install langgraph langchain langchain-openai langchain-anthropic tenacity slowapi

lifespan 集成示例

from contextlib import asynccontextmanager
from fastapi import FastAPI
from src.agent.graph.agent import AgentContainer, init_llm, init_agent_graph
from src.agent.memory.store import MemoryStore

@asynccontextmanager
async def lifespan(app: FastAPI):
    # ✅ 启动:初始化依赖容器
    container = AgentContainer()
    container.init_llm()
    container.init_graph()
    app.state.agent = container
    app.state.memory_store = MemoryStore()
    yield
    # 关闭:清理资源(如 Redis 连接、线程池)

app = FastAPI(lifespan=lifespan)

红线

  1. 不重复生成 FastAPI 骨架和 Auth 模块。
  2. 使用骨架的 EnvelopeRoute 统一响应,不手动包装 { code, message, data }。
  3. 使用骨架的 BusinessException 异常处理,不返回原始 dict 错误。
  4. 使用骨架的 settings.db_prefix 表前缀,不硬编码。
  5. 使用骨架的 settings 配置系统,不单独定义 AgentSettings。
  6. Tool 定义使用 @tool 装饰器 + current_user_id 必需参数。
  7. 对话历史默认保留 20 轮,可配置。
  8. 流式输出使用 SSE,含代理缓冲禁用头。
  9. 所有注释、文档用中文。
  10. Tool 不在 arguments 中传 current_user_id,由系统统一注入。
  11. 异常不在客户端返回原始堆栈,必须用 logger.exception() + 固定话术。
  12. Tool/对话失败必须写审计日志(audit_logger)。

后续迭代

v2.1 增强(P1 修复)

P1-S2 模型白名单
P1-S4 PII 加密模块
P1-S8 配置驱动历史消息 limit
P1-P7 ToolRegistry 去锁
P1-P21 MemoryBuffer 改用 deque
P1-P15 chat 同步总超时
P1-P9 response_content 硬截断
P1-P22 list_sessions 单查询
P1-U 错误码标准化
P2-2 / P2-6 兼容性

v2.2 清理(P2 修复)

P2-1 会话消息级联清理
P2-7 依赖版本固定
P2-11 安全响应头
P2-13 trace_id 关联日志
P2-19 / P2-25 死代码清理

后续迭代


【考拉搞AI】,带你全面进入 VibeCoding 的世界~

触发关键词

FastAPI AI Agent、LangGraph Agent、AI 对话模块、FastAPI Tool、
帮我加一个 AI 对话模块、Agent 模块、LLM 对话、LangChain
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.