Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions llm/main_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,22 @@ async def chat(self, user_message):

app = FastAPI(title="FastMCP GitHub Assistant")

# ===== 新增:文件下载接口 =====
from fastapi.responses import FileResponse
from pathlib import Path

@app.get("/download/{filename}")
async def download_file(filename: str):
"""下载生成的文件"""
file_path = Path(__file__).parent / "output" / filename
if file_path.exists():
return FileResponse(
path=file_path,
filename=filename,
media_type="application/octet-stream"
)
return {"error": "文件不存在"}
# ===== 新增结束 =====

def get_web_interface():
"""生成AI对话Web界面HTML"""
Expand Down Expand Up @@ -3244,7 +3260,6 @@ async def chat(message: str = Form(...)):
# 创建全局AI助手实例
assistant = FastMCPGitHubAssistant()


def main():
"""主函数 - 支持 Web界面、标准MCP、SSE-MCP 三种启动模式"""
import sys
Expand Down Expand Up @@ -3296,14 +3311,19 @@ async def generate_sumo_network_impl(
if not sumo_home:
return "❌ 错误:未设置 SUMO_HOME 环境变量。请在终端中设置:`set SUMO_HOME=D:\\mcp\\sumo\\sumo_install\\sumo-win64-1.27.0\\sumo-1.27.0`"

# 创建输出目录
output_dir = os.path.join(os.path.dirname(__file__), "output")
os.makedirs(output_dir, exist_ok=True)

bin_dir = os.path.join(sumo_home, "bin")
tools_dir = os.path.join(sumo_home, "tools")

prefix = "web_generated"
prefix = os.path.join(output_dir, "web_generated")
net_file = f"{prefix}.net.xml"
trips_file = f"{prefix}.trips.xml"
rou_file = f"{prefix}.rou.xml"
cfg_file = f"{prefix}.sumocfg"
xodr_file = f"{prefix}.xodr"

try:
# 1. 生成路网
Expand Down Expand Up @@ -3352,19 +3372,38 @@ async def generate_sumo_network_impl(
</configuration>
''')

# 4.5 转换为 OpenDRIVE 格式
xodr_file = f"{prefix}.xodr"
xodr_msg = ""
try:
subprocess.run([
os.path.join(bin_dir, "netconvert"),
f"--sumo-net-file={net_file}",
f"--opendrive-output={xodr_file}"
], check=True, capture_output=True, text=True)
xodr_msg = f"- OpenDRIVE: {xodr_file}"
except subprocess.CalledProcessError as e:
xodr_msg = f"- OpenDRIVE 转换失败:{e.stderr}"

# 5. 返回结果(修改返回信息,添加 xodr_msg)
download_url = f"/download/{os.path.basename(xodr_file)}"
return f"""✅ SUMO 路网和车流生成成功!

📁 生成的文件:
- 路网: {net_file}
- 出行: {trips_file}
- 路由: {rou_file}
- 配置: {cfg_file}
{xodr_msg}

📊 参数:
- 网格: {grid_x}x{grid_y}
- 仿真时长: {duration} 秒
- 发车间隔: {rate} 秒/辆

📥 下载链接:
- [点击下载 OpenDRIVE 文件]({download_url})

▶️ 在终端中运行以下命令查看:
cd D:\\mcp\\sumo
sumo-gui -c {cfg_file}
Expand Down
Loading