Files
hsa/Html/apps/services/backboard_service.py
2025-09-14 17:13:00 +08:00

57 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# myapp/services/backboard_service.py
import os
from flask import current_app
from backboardManager import Backboard # 你已有的类
from ..utils.sudoUtil import sudo_open
def _to_wsl_path_if_needed(path: str) -> str:
cfg = current_app.config["VSCODE_WEB_PATH"]
if cfg.get("is_wsl") and cfg.get("windows_path") and cfg.get("wsl_path"):
return path.replace("\\", "/").replace(cfg["windows_path"], cfg["wsl_path"])
return path
def realtime_response(config: dict, realtime_action: dict) -> None:
"""
业务:根据 VSCode 上报的动作,更新 Backboard黑板状态。
"""
backboard_manager = current_app.extensions["backboard_manager"]
user_uuid = config["user_uuid"]
chatmanager = current_app.extensions["user_uuid2chatmanager"][user_uuid]
bb = backboard_manager.get_backboard(user_uuid)
assert isinstance(bb, Backboard)
# 统一记历史
bb.add_history(realtime_action)
rtype = realtime_action.get("type")
if rtype == "workspaceFolders":
bb.file_tree = realtime_action.get("fileTree")
elif rtype == "activeFile":
file_path = realtime_action.get("filePath")
assert isinstance(file_path, str)
with sudo_open(file_path, "r", encoding="utf-8") as f:
bb.active_file_content = f.read()
bb.active_file_path = file_path
elif rtype == "paste":
file_path = realtime_action.get("filePath")
assert isinstance(file_path, str)
bb.pasted_file_path = file_path
bb.pasted_content = realtime_action.get("content")
bb.active_file_path = file_path
with sudo_open(file_path, "r", encoding="utf-8") as f:
bb.active_file_content = f.read()
elif rtype == "fileEdit":
file_path = realtime_action.get("filePath")
assert isinstance(file_path, str)
bb.active_file_path = file_path
with sudo_open(file_path, "r", encoding="utf-8") as f:
bb.active_file_content = f.read()
chatmanager.upload_bb()