Backend API 模块¶
本模块提供 FastAPI 后端服务。
FastAPI 应用¶
FastAPI backend service for AI Social Scientist VSCode extension
关联文件: - @packages/agentsociety2/agentsociety2/backend/run.py - 服务启动脚本 - @extension/src/services/backendManager.ts - VSCode插件后端进程管理 - @extension/src/apiClient.ts - VSCode插件API客户端
路由注册: - @packages/agentsociety2/agentsociety2/backend/routers/prefill_params.py - /api/v1/prefill-params - @packages/agentsociety2/agentsociety2/backend/routers/experiments.py - /api/v1/experiments - @packages/agentsociety2/agentsociety2/backend/routers/replay.py - /api/v1/replay - @packages/agentsociety2/agentsociety2/backend/routers/custom.py - /api/v1/custom - @packages/agentsociety2/agentsociety2/backend/routers/modules.py - /api/v1/modules - @packages/agentsociety2/agentsociety2/backend/routers/agent_skills.py - /api/v1/agent-skills
路由模块¶
预填充参数路由¶
预填充参数查询API路由(只读)
关联文件: - @packages/agentsociety2/agentsociety2/backend/app.py - 主应用,注册此路由 (/api/v1/prefill-params) - @extension/src/prefillParamsViewProvider.ts - VSCode插件前端调用此API - @extension/src/webview/prefillParams/ - 前端展示组件
读取文件: - {workspace}/.agentsociety/prefill_params.json - 预填充参数配置
- async agentsociety2.backend.routers.prefill_params.get_prefill_params(workspace_path=Query(PydanticUndefined))[源代码]¶
获取全局预填充参数
返回工作区中所有类(Agent和环境模块)的预填充参数配置。
- 参数:
workspace_path (str) -- 工作区根目录路径
- 返回:
Dict[str, Any]: 预填充参数配置,包含: - success: 是否成功 - data: 参数数据,结构为: - version: 配置版本 - env_modules: 环境模块预填充参数字典 - agents: Agent预填充参数字典
- 抛出:
HTTPException -- 500 - 读取配置文件失败
- 返回类型:
备注
如果配置文件不存在,返回空配置结构。
- async agentsociety2.backend.routers.prefill_params.get_class_prefill_params(class_kind=Path(PydanticUndefined), class_name=Path(PydanticUndefined), workspace_path=Query(PydanticUndefined))[源代码]¶
获取特定类的预填充参数
返回指定类(Agent或环境模块)的预填充参数配置。
- 参数:
- 返回:
Dict[str, Any]: 类的预填充参数,包含: - success: 是否成功 - class_kind: 类类型 - class_name: 类名 - params: 该类的预填充参数字典(如无配置则为空字典)
- 抛出:
HTTPException -- 500 - 读取配置文件失败
- 返回类型:
示例
GET /api/v1/prefill-params/env_module/mobility_space?workspace_path=/path/to/workspace
实验路由¶
实验数据 API。
提供实验基本信息与产出文件查询。新 replay 写入格式为
run/replay/_schema.json + sharded JSONL;实验统计通过
agentsociety2.storage.ReplayReader 读取,不再访问 sqlite.db。
- class agentsociety2.backend.routers.experiments.ExperimentInfo(*, experiment_id, hypothesis_id, status, start_time=None, end_time=None, agent_count, step_count)[源代码]¶
实验信息。
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- async agentsociety2.backend.routers.experiments.get_experiment_info(hypothesis_id, experiment_id, workspace_path=Query(PydanticUndefined))[源代码]¶
获取实验基本信息。
返回指定实验的状态、开始/结束时间、Agent 数量和已执行 step 数。
回放路由¶
Replay data query API for simulation playback.
关联文件: - @extension/src/replayWebviewProvider.ts - VSCode Replay Webview provider - @extension/src/webview/replay/ - VSCode Replay Webview React app
- class agentsociety2.backend.routers.replay.ExperimentInfo(*, hypothesis_id, experiment_id, total_steps, start_time, end_time, agent_count)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.TimelinePoint(*, step, t)[源代码]¶
- t: datetime¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.AgentProfile(*, id, name, profile=<factory>)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayDatasetColumn(*, column_name, sqlite_type, logical_type=None, analysis_role=None, title=None, description=None, unit=None, nullable, enum_values=None, example=None, tags=<factory>)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayDatasetInfo(*, dataset_id, table_name, module_name, kind, title='', description='', entity_key=None, step_key=None, time_key=None, default_order=<factory>, capabilities=<factory>, version, created_at, columns=<factory>)[源代码]¶
- created_at: datetime¶
- columns: List[ReplayDatasetColumn]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayDatasetList(*, datasets)[源代码]¶
- datasets: List[ReplayDatasetInfo]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayDatasetRows(*, dataset_id, columns, rows, total)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayPanelSchema(*, agent_profile_dataset=None, agent_state_datasets=<factory>, env_state_datasets=<factory>, geo_dataset=None, primary_agent_state_dataset_id=None, layout_hint='random', supports_map=False)[源代码]¶
- agent_profile_dataset: ReplayDatasetInfo | None¶
- agent_state_datasets: List[ReplayDatasetInfo]¶
- env_state_datasets: List[ReplayDatasetInfo]¶
- geo_dataset: ReplayDatasetInfo | None¶
- layout_hint: Literal['map', 'random']¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayDatasetPanelRef(*, dataset_id, module_name, title='')[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayPosition(*, agent_id, lng=None, lat=None)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayAgentStateAtStep(*, dataset, rows_by_agent_id=<factory>)[源代码]¶
- dataset: ReplayDatasetPanelRef¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayEnvStateAtStep(*, dataset, row=None)[源代码]¶
- dataset: ReplayDatasetPanelRef¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.replay.ReplayStepBundle(*, step, t=None, layout_hint='random', positions=<factory>, agent_state_rows=<factory>, env_state_rows=<factory>)[源代码]¶
- layout_hint: Literal['map', 'random']¶
- positions: List[ReplayPosition]¶
- agent_state_rows: Dict[str, ReplayAgentStateAtStep]¶
- env_state_rows: Dict[str, ReplayEnvStateAtStep]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- agentsociety2.backend.routers.replay.get_replay_dir(workspace_path, hypothesis_id, experiment_id)[源代码]¶
- async agentsociety2.backend.routers.replay.get_experiment_info(hypothesis_id, experiment_id, workspace_path=Query(PydanticUndefined))[源代码]¶
- async agentsociety2.backend.routers.replay.get_replay_datasets(hypothesis_id, experiment_id, workspace_path=Query(PydanticUndefined))[源代码]¶
- async agentsociety2.backend.routers.replay.get_replay_dataset(hypothesis_id, experiment_id, dataset_id, workspace_path=Query(PydanticUndefined))[源代码]¶
- async agentsociety2.backend.routers.replay.get_replay_dataset_rows(hypothesis_id, experiment_id, dataset_id, workspace_path=Query(PydanticUndefined), page=Query(1), page_size=Query(20), order_by=Query(None), desc_order=Query(False), step=Query(None), entity_id=Query(None), start_step=Query(None), end_step=Query(None), max_step=Query(None), columns=Query(None), latest_per_entity=Query(False))[源代码]¶
- async agentsociety2.backend.routers.replay.get_replay_panel_schema(hypothesis_id, experiment_id, workspace_path=Query(PydanticUndefined))[源代码]¶
- async agentsociety2.backend.routers.replay.get_replay_step_bundle(hypothesis_id, experiment_id, step, workspace_path=Query(PydanticUndefined))[源代码]¶
自定义模块路由¶
自定义模块 API 路由
提供扫描、清理、测试自定义模块的 API 端点。
关联文件: - @extension/src/projectStructureProvider.ts - 前端项目结构视图(调用此API) - @extension/src/apiClient.ts - API客户端
API端点: - POST /api/v1/custom/scan - 扫描自定义模块并生成JSON配置 - POST /api/v1/custom/clean - 清理自定义模块配置 - POST /api/v1/custom/test - 测试自定义模块 - GET /api/v1/custom/list - 列出已注册的自定义模块 - GET /api/v1/custom/status - 获取自定义模块状态
内部服务: - @packages/agentsociety2/agentsociety2/backend/services/custom/scanner.py - 模块扫描 - @packages/agentsociety2/agentsociety2/backend/services/custom/generator.py - JSON生成 - @packages/agentsociety2/agentsociety2/registry/ - 模块注册表
- class agentsociety2.backend.routers.custom.ScanRequest(*, workspace_path=None)[源代码]¶
扫描请求
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.custom.ScanResponse(*, success, agents_found, envs_found, agents_generated, envs_generated, errors=<factory>, agent_diagnostics=<factory>, env_diagnostics=<factory>, message=None)[源代码]¶
扫描响应
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.custom.CleanResponse(*, success, removed_count, message)[源代码]¶
清理响应
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.custom.TestRequest(*, workspace_path=None, module_kind=None, module_class_name=None)[源代码]¶
测试请求
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.custom.ModuleTestResult(*, name, module_kind='env_module', success, output, error=None, checks=<factory>, metadata=<factory>)[源代码]¶
单个模块测试结果
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.custom.TestResponse(*, success, test_output, error=None, returncode=None, results=<factory>, total_tests=None, passed_tests=None, failed_tests=None)[源代码]¶
测试响应
- results: List[ModuleTestResult]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.custom.ListResponse(*, success, agents, envs, total_agents, total_envs)[源代码]¶
列表响应
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- async agentsociety2.backend.routers.custom.scan_custom_modules(request)[源代码]¶
扫描自定义模块并注册到内存
扫描工作区的 custom/agents/ 和 custom/envs/ 目录(跳过 examples/ 子目录), 验证发现的模块并将其直接注册到内存中的 registry。
- 参数:
request (ScanRequest) -- 扫描请求,包含: - workspace_path: 工作区路径(可选,不提供则使用环境变量)
- 返回:
ScanResponse: 扫描结果,包含: - success: 是否成功 - agents_found: 发现的Agent数量 - envs_found: 发现的环境模块数量 - agents_generated: 成功注册的Agent数量 - envs_generated: 成功注册的环境模块数量 - errors: 错误信息列表 - message: 结果消息
- 抛出:
HTTPException -- 400 - 未提供工作区路径
HTTPException -- 500 - 扫描失败
备注
此接口不会生成JSON配置文件,模块仅注册到内存中。 如需持久化配置,请使用 /api/v1/custom/classes 端点。
- async agentsociety2.backend.routers.custom.clean_custom_modules(request)[源代码]¶
清理自定义模块的JSON配置
删除所有标记为 is_custom=true 的JSON配置文件。
- 参数:
request (ScanRequest) -- 清理请求,包含: - workspace_path: 工作区路径(可选)
- 返回:
CleanResponse: 清理结果,包含: - success: 是否成功 - removed_count: 删除的配置数量 - message: 结果消息
- 抛出:
HTTPException -- 400 - 未提供工作区路径
HTTPException -- 500 - 清理失败
- async agentsociety2.backend.routers.custom.test_custom_modules(request)[源代码]¶
测试自定义模块
扫描并测试自定义模块,验证其能否正常工作。可以测试所有模块或指定特定模块。
- 参数:
request (TestRequest) -- 测试请求,包含: - workspace_path: 工作区路径(可选) - module_kind: 模块类型 ('agent' 或 'env_module',可选) - module_class_name: 要测试的类名(与module_kind配合使用,可选)
- 返回:
TestResponse: 测试结果,包含: - success: 是否全部通过 - test_output: 测试输出内容 - error: 错误信息(如有) - returncode: 测试进程返回码 - results: 各模块测试结果列表 - total_tests: 总测试数 - passed_tests: 通过数 - failed_tests: 失败数
- 抛出:
HTTPException -- 400 - 未提供工作区路径
HTTPException -- 500 - 测试失败
备注
如果不指定 module_kind 和 module_class_name,则测试所有发现的模块。
- async agentsociety2.backend.routers.custom.list_custom_modules()[源代码]¶
列出当前已注册的自定义模块
从内存注册表中读取所有标记为 is_custom=true 的模块信息。
- 返回:
ListResponse: 模块列表,包含: - success: 是否成功 - agents: 自定义Agent列表 - envs: 自定义环境模块列表 - total_agents: Agent总数 - total_envs: 环境模块总数
- 抛出:
HTTPException -- 500 - 获取列表失败
- async agentsociety2.backend.routers.custom.get_custom_modules_status()[源代码]¶
获取自定义模块状态概览
返回工作区自定义模块目录的状态信息。
- 返回:
Dict[str, Any]: 状态信息,包含: - custom_dir_exists: custom目录是否存在 - agents_dir_exists: agents子目录是否存在 - envs_dir_exists: envs子目录是否存在 - agent_files_count: Agent文件数量 - env_files_count: 环境模块文件数量 - registered_agents: 已注册的Agent数量 - registered_envs: 已注册的环境模块数量
- 抛出:
HTTPException -- 400 - 未设置工作区路径
- async agentsociety2.backend.routers.custom.list_available_classes(workspace_path=Query(PydanticUndefined), include_custom=Query(True))[源代码]¶
列出所有可用的Agent类和环境模块类
返回所有可用的类,并标记哪些已配置预填充参数。
- 参数:
- 返回:
Dict[str, Any]: 可用类列表,包含: - success: 是否成功 - env_modules: 环境模块字典,每个模块包含: - type, class_name, description, is_custom, has_prefill - agents: Agent字典,每个Agent包含: - type, class_name, description, is_custom, has_prefill - env_module_count: 环境模块数量 - agent_count: Agent数量
- 抛出:
HTTPException -- 500 - 获取类列表失败
- 返回类型:
模块路由¶
Modules API router
Provides endpoints for listing available agent classes and environment modules. Supports both built-in modules and custom modules from the workspace.
关联文件: - @extension/src/apiClient.ts - API客户端(调用getAgentClasses, getEnvModules) - @extension/src/prefillParamsViewProvider.ts - 预填充参数查看器 - @extension/src/simSettingsEditorProvider.ts - SIM_SETTINGS编辑器 - @packages/agentsociety2/agentsociety2/registry/ - 模块注册表
API端点: - GET /api/v1/modules/agent_classes - 获取所有可用的Agent类 - GET /api/v1/modules/env_module_classes - 获取所有可用的Environment模块类 - GET /api/v1/modules/refresh - 刷新模块列表(重新扫描)
- async agentsociety2.backend.routers.modules.get_agent_classes(include_custom=Query(True))[源代码]¶
获取所有可用的Agent类列表
返回系统中所有已注册的Agent类,包括内置和自定义模块。
- async agentsociety2.backend.routers.modules.get_env_module_classes(include_custom=Query(True))[源代码]¶
获取所有可用的环境模块类列表
返回系统中所有已注册的环境模块类,包括内置和自定义模块。
Agent Skills 路由¶
Agent Skills API 路由
提供 agent skill 的列表、扫描自定义 skill、导入/创建/上传 skill 的 API 端点。
本路由基于 v2 skill 注册表 (agentsociety2.agent.base.skill_registry),它是一个
只读元数据注册表:skill 是"始终存在"的目录,PersonAgent 在运行时按目录选择激活。
因此本路由**没有** enable/disable/reload/remove 端点(v2 模型无此概念)。
关联文件: - @packages/agentsociety2/agentsociety2/agent/base/skill_registry.py - Skill 注册表 - @extension/src/apiClient.ts - VSCode 扩展 API 客户端 - @frontend/src/pages/Skills/index.tsx - Web 前端 Skill 管理页
API 端点: - GET /api/v1/agent-skills/list — 列出所有已发现的 agent skill(built-in + custom + env) - POST /api/v1/agent-skills/scan — 扫描 workspace/custom/skills/ 下的自定义 skill - POST /api/v1/agent-skills/import — 从工作区内路径导入 skill 目录 - POST /api/v1/agent-skills/create — 在线创建新 skill(SKILL.md + 可选脚本) - POST /api/v1/agent-skills/upload — 上传 zip 包导入 skill - GET /api/v1/agent-skills/{skill_id}/info — 获取 SKILL.md 内容
- class agentsociety2.backend.routers.agent_skills.SkillItem(*, skill_id, name, namespace, description, source, source_label, path, has_skill_md, script='')[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.agent_skills.ListResponse(*, success, skills, total)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.agent_skills.ScanRequest(*, workspace_path=None)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.agent_skills.ScanResponse(*, success, new_skills, total, message)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.agent_skills.ImportRequest(*, source_path, workspace_path=None)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.agent_skills.ImportResponse(*, success, name, message)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.agent_skills.CreateRequest(*, name, description='', script='', body='', script_content='', workspace_path=None)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentsociety2.backend.routers.agent_skills.SimpleResponse(*, success, message)[源代码]¶
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- async agentsociety2.backend.routers.agent_skills.list_skills()[源代码]¶
列出所有已发现的 Agent Skill(built-in + custom + env)。
- async agentsociety2.backend.routers.agent_skills.scan_custom_skills(req)[源代码]¶
扫描工作区的自定义 Agent Skill({workspace}/custom/skills/)。
- async agentsociety2.backend.routers.agent_skills.import_skill(req)[源代码]¶
从工作区内路径导入 Agent Skill(复制到 custom/skills/)。
- async agentsociety2.backend.routers.agent_skills.create_skill(req)[源代码]¶
在线创建新的自定义 Skill。
在 custom/skills/{name}/ 下生成 SKILL.md(+ 可选脚本文件)。
请求/响应模型¶
SkillItem¶
- class agentsociety2.backend.routers.agent_skills.SkillItem(*, skill_id, name, namespace, description, source, source_label, path, has_skill_md, script='')[源代码]
- skill_id: str
- name: str
- namespace: str
- description: str
- source: str
- source_label: str
- path: str
- has_skill_md: bool
- script: str
- model_config = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].