OpenManus系列(04): 3月10日更新解析,从文档优化到 FastAPI 集成

319 阅读2分钟

From 1dfc2c41 To 57609043

🔎 版本更新概览

今日 OpenManus 代码库进行了重要的更新,主要涉及 文档优化功能增强配置调整运行支持,其中最显著的改动是新增了基于 FastAPIapp.py,支持任务管理与流式任务执行。

核心更新摘要

模块变更类型关键更新
文档 (Docs)修改README.md 头部优化,新增 Cite 引用
功能 (Features)新增app.py (FastAPI Web API)
前端 (Frontend)新增static/main.js & static/style.css
配置 (Config)修改requirements.txt 增加 fastapi~=0.115.11
运行支持 (Run Support)新增run.bat Windows 运行脚本

📖 文档优化 (Docs)

1. README.md & README_zh.md

  • 统一语言选择格式: English | [中文]
  • 增加 GitHub Stars & License 徽章,提升项目展示效果:
    [![GitHub stars](https://img.shields.io/github/stars/mannaandpoem/OpenManus?style=social)](https://github.com/mannaandpoem/OpenManus)
    [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
    
  • 增加 Cite 引用部分,提供 bibtex 格式的学术引用信息:
    @misc{openmanus2025,
      author = {Xinbin Liang and Jinyu Xiang and Zhaoyang Yu and Jiayi Zhang and Sirui Hong},
      title = {OpenManus: An open-source framework for building general AI agents},
      year = {2025},
      publisher = {GitHub},
      journal = {GitHub repository},
      howpublished = {\url{https://github.com/mannaandpoem/OpenManus}},
    }
    
  • 精简 Roadmap 计划,删除较为笼统的规划,仅保留 UI 相关改进。

2. examples/readme.md

  • 换行优化,提升可读性,减少大段文本。

🌐 FastAPI 任务管理系统

1. 新增 app.py (FastAPI Web API)

关键功能:

  • 任务管理 (TaskManager),基于 asyncio 处理并发任务。
  • SSE 实时任务更新,前端可监听任务状态变更。
  • Manus 代理集成,执行智能任务。

🎨 代码结构

classDiagram
    class Task {
      +id: str
      +prompt: str
      +created_at: datetime
      +status: str
      +steps: list
    }

    class TaskManager {
      +create_task(prompt: str) -> Task
      +update_task_step(task_id: str, step: int, result: str)
      +complete_task(task_id: str)
      +fail_task(task_id: str, error: str)
    }

    TaskManager --> Task
    FastAPI --|> TaskManager
    FastAPI --|> SSE Stream

📚 代码示例

from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import asyncio

app = FastAPI()

task_manager = TaskManager()

@app.post("/tasks")
async def create_task(prompt: str):
    task = task_manager.create_task(prompt)
    asyncio.create_task(run_task(task.id, prompt))
    return {"task_id": task.id}

@app.get("/tasks/{task_id}/events")
async def task_events(task_id: str):
    async def event_generator():
        queue = task_manager.queues.get(task_id)
        while True:
            event = await queue.get()
            yield f"event: {event['type']}\ndata: {event}\n\n"

    return StreamingResponse(event_generator(), media_type="text/event-stream")

🛠️ 前端 & 交互改进

1. static/main.js

  • 新增 SSE 监听,实时更新任务状态:
    function setupSSE(taskId) {
        const eventSource = new EventSource(`/tasks/${taskId}/events`);
        eventSource.addEventListener('status', (event) => {
            const data = JSON.parse(event.data);
            document.getElementById('task-container').innerHTML += `<p>${data.status}</p>`;
        });
    }
    

2. static/style.css

  • 优化 UI 体验,增强任务列表样式:
    .task-item.active {
        background-color: var(--primary-color);
        color: white;
    }
    

📃 依赖更新 & 运行支持

1. requirements.txt

  • 新增 fastapi~=0.115.11 依赖。

2. run.bat (Windows 运行脚本)

@echo off
venv\Scripts\python.exe app.py
pause

3. run_flow.py

  • 优化日志,提升超时处理逻辑:
    logger.info("Operation terminated due to timeout. Please try a simpler request.")