代码管理

📎 引用文件

本文引用的文件 - runs_routes.py - security.py - helpers.py - test_security_auth_api.py - test_spa_deep_link.py

目录

  1. 简介
  2. 项目结构
  3. 核心组件
  4. 架构总览
  5. 详细组件分析
  6. 依赖关系分析
  7. 性能注意事项
  8. 故障排查指南
  9. 结论
  10. 附录:API调用示例

简介

本文件为 Vibe-Trading 回测“代码管理”API 的详细文档,聚焦两个代码获取端点: - GET /runs/{run_id}/code:返回指定运行(run)的策略源代码文件。 - GET /runs/{run_id}/pine:返回与 TradingView 集成的 Pine Script 策略文件(若存在)。

同时说明代码目录结构、Pine Script 支持方式、访问安全机制(路径参数校验、白名单控制)、错误处理规范,并提供完整的 API 调用示例(Python requests 与 JavaScript fetch)。

项目结构

graph TB A["runs/"] --> B["{run_id}/"] B --> C["code/"] C --> C1["signal_engine.py"] B --> D["artifacts/"] D --> D1["strategy.pine"]

图表来源 - runs_routes.py:262-300

章节来源 - runs_routes.py:262-300

核心组件

章节来源 - runs_routes.py:262-300 - security.py:571-588 - helpers.py:260-270

架构总览

请求从客户端进入 FastAPI 应用,经过认证中间件与路径参数校验后,路由处理器读取对应文件或返回不存在状态。

sequenceDiagram participant Client as "客户端" participant API as "FastAPI 应用" participant Auth as "require_auth(安全)" participant Route as "runs_routes 处理器" participant FS as "文件系统" Client->>API : GET /runs/{run_id}/code API->>Auth : 验证凭据 Auth-->>API : 通过/拒绝 API->>Route : 调用 get_run_code(run_id) Route->>Route : _validate_path_param(run_id) Route->>FS : 检查 runs/{run_id}/code/signal_engine.py FS-->>Route : 存在/不存在 Route-->>Client : JSON : {filename : source} 或 404 Client->>API : GET /runs/{run_id}/pine API->>Auth : 验证凭据 Auth-->>API : 通过/拒绝 API->>Route : 调用 get_run_pine(run_id) Route->>Route : _validate_path_param(run_id) Route->>FS : 检查 runs/{run_id}/artifacts/strategy.pine FS-->>Route : 存在/不存在 Route-->>Client : JSON : {exists : true/false, content : string|null}

图表来源 - runs_routes.py:262-300 - security.py:571-588 - helpers.py:260-270

详细组件分析

端点:GET /runs/{run_id}/code

flowchart TD Start(["进入 get_run_code"]) --> Validate["校验 run_id"] Validate --> CheckDir{"runs/{run_id}/code 存在?"} CheckDir -- 否 --> NotFound["返回 404"] CheckDir -- 是 --> ReadFile["读取 signal_engine.py"] ReadFile --> ReturnMap["返回 {filename: source}"]

图表来源 - runs_routes.py:262-281 - helpers.py:260-270

章节来源 - runs_routes.py:262-281 - helpers.py:260-270

端点:GET /runs/{run_id}/pine

flowchart TD Start(["进入 get_run_pine"]) --> Validate["校验 run_id"] Validate --> CheckFile{"runs/{run_id}/artifacts/strategy.pine 存在?"} CheckFile -- 否 --> ReturnFalse["返回 {exists:false, content:null}"] CheckFile -- 是 --> ReadContent["读取 Pine 脚本内容"] ReadContent --> ReturnTrue["返回 {exists:true, content:'...'}"]

图表来源 - runs_routes.py:283-300 - helpers.py:260-270

章节来源 - runs_routes.py:283-300 - helpers.py:260-270

认证与安全机制

classDiagram class Security { +require_auth(request, cred) Principal } class Helpers { +_validate_path_param(value, kind) void } class Routes { +get_run_code(run_id) +get_run_pine(run_id) } Security <.. Routes : "依赖" Helpers <.. Routes : "依赖"

图表来源 - security.py:571-588 - helpers.py:260-270 - runs_routes.py:262-300

章节来源 - security.py:571-588 - helpers.py:260-270 - test_spa_deep_link.py:33-50

依赖关系分析

graph LR R["runs_routes.py"] --> S["security.py"] R --> H["helpers.py"] T["tests/test_security_auth_api.py"] --> R

图表来源 - runs_routes.py:262-300 - security.py:571-588 - helpers.py:260-270 - test_security_auth_api.py:662-681

章节来源 - test_security_auth_api.py:662-681

性能注意事项

[本节为通用性能建议,不直接分析具体文件]

故障排查指南

章节来源 - security.py:463-504 - runs_routes.py:272-300 - test_security_auth_api.py:662-681

结论

Vibe-Trading 的代码管理 API 通过严格的认证与路径参数校验,安全地暴露回测运行对应的策略源码与 Pine Script 文件。/runs/{run_id}/code 返回 Python 策略源码,/runs/{run_id}/pine 返回 TradingView 集成所需的 Pine 脚本(以 exists 字段标识存在性)。结合清晰的错误码与响应格式,便于客户端稳定集成与调试。

[本节为总结性内容,不直接分析具体文件]

附录:API调用示例

Python requests 示例

参考实现位置: - runs_routes.py:262-300 - security.py:571-588

JavaScript fetch 示例

参考实现位置: - runs_routes.py:262-300 - security.py:571-588