feat: add BFF backend scaffold for mobile app
This commit is contained in:
1 parent
03d21f607f
commit
591fd991fa
14 files changed
+462
No files matched your search
@@ -0,0 +1 @@
|
||||
# package marker
|
||||
@@ -0,0 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from ..models import AgentInfo, ChatMessage, SendMessageResponse, SessionSummary, StatusResponse
|
||||
|
||||
|
||||
class BackendService(ABC):
|
||||
@abstractmethod
|
||||
async def get_status(self) -> StatusResponse: ...
|
||||
|
||||
@abstractmethod
|
||||
async def list_sessions(self) -> list[SessionSummary]: ...
|
||||
|
||||
@abstractmethod
|
||||
async def get_messages(self, session_id: str) -> list[ChatMessage]: ...
|
||||
|
||||
@abstractmethod
|
||||
async def send_message(self, session_id: str, message: str) -> SendMessageResponse: ...
|
||||
|
||||
@abstractmethod
|
||||
async def list_agents(self) -> list[AgentInfo]: ...
|
||||
@@ -0,0 +1,120 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import datetime, timezone
|
||||
from uuid import uuid4
|
||||
|
||||
from ..models import AgentInfo, ChatMessage, SendMessageResponse, SessionSummary, StatusResponse
|
||||
from .base import BackendService
|
||||
|
||||
|
||||
class MockBackendService(BackendService):
|
||||
def __init__(self) -> None:
|
||||
self._lock = asyncio.Lock()
|
||||
self._sessions: list[SessionSummary] = [
|
||||
SessionSummary(
|
||||
id="agent:main:main",
|
||||
title="主会话",
|
||||
lastMessage="先把移动端的接入方案定下来。",
|
||||
updatedAt=self._now(),
|
||||
),
|
||||
SessionSummary(
|
||||
id="agent:main:rd-lead",
|
||||
title="rd-lead",
|
||||
lastMessage="架构方案已经同步。",
|
||||
updatedAt=self._now(),
|
||||
),
|
||||
]
|
||||
self._messages: dict[str, list[ChatMessage]] = {
|
||||
"agent:main:main": [
|
||||
ChatMessage(id="1", role="system", text="已连接到 OpenClaw Mobile Gateway", createdAt="20:01", status="connected"),
|
||||
ChatMessage(id="2", role="user", text="帮我规划一下移动端接入方案。", createdAt="20:03"),
|
||||
ChatMessage(id="3", role="assistant", text="建议先做 App + BFF + OpenClaw 三层结构。", createdAt="20:03", status="done"),
|
||||
],
|
||||
"agent:main:rd-lead": [
|
||||
ChatMessage(id="4", role="user", text="架构文档准备好了没?", createdAt="19:40"),
|
||||
ChatMessage(id="5", role="assistant", text="已经整理成 ARCHITECTURE.md。", createdAt="19:41", status="done"),
|
||||
],
|
||||
}
|
||||
self._agents = [
|
||||
AgentInfo(id="main", name="main"),
|
||||
AgentInfo(id="pm", name="pm"),
|
||||
AgentInfo(id="rd-lead", name="rd-lead"),
|
||||
]
|
||||
|
||||
async def get_status(self) -> StatusResponse:
|
||||
return StatusResponse(
|
||||
online=True,
|
||||
openClawVersion="mock-2026.03",
|
||||
currentModel="cliproxy/gpt-5.4",
|
||||
defaultAgent="main",
|
||||
updatedAt=self._now(),
|
||||
)
|
||||
|
||||
async def list_sessions(self) -> list[SessionSummary]:
|
||||
return self._sessions
|
||||
|
||||
async def get_messages(self, session_id: str) -> list[ChatMessage]:
|
||||
return self._messages.get(session_id, [])
|
||||
|
||||
async def send_message(self, session_id: str, message: str) -> SendMessageResponse:
|
||||
async with self._lock:
|
||||
if session_id not in self._messages:
|
||||
self._messages[session_id] = []
|
||||
|
||||
user_message = ChatMessage(
|
||||
id=str(uuid4()),
|
||||
role="user",
|
||||
text=message,
|
||||
createdAt=self._now_hm(),
|
||||
)
|
||||
assistant_message = ChatMessage(
|
||||
id=str(uuid4()),
|
||||
role="assistant",
|
||||
text=self._mock_reply(message),
|
||||
createdAt=self._now_hm(),
|
||||
status="done",
|
||||
)
|
||||
self._messages[session_id].extend([user_message, assistant_message])
|
||||
self._touch_session(session_id, message)
|
||||
return SendMessageResponse(accepted=True, messageId=user_message.id)
|
||||
|
||||
async def list_agents(self) -> list[AgentInfo]:
|
||||
return self._agents
|
||||
|
||||
def _touch_session(self, session_id: str, last_message: str) -> None:
|
||||
for index, session in enumerate(self._sessions):
|
||||
if session.id == session_id:
|
||||
self._sessions[index] = SessionSummary(
|
||||
id=session.id,
|
||||
title=session.title,
|
||||
lastMessage=last_message,
|
||||
updatedAt=self._now(),
|
||||
)
|
||||
return
|
||||
self._sessions.insert(
|
||||
0,
|
||||
SessionSummary(
|
||||
id=session_id,
|
||||
title=session_id.split(":")[-1],
|
||||
lastMessage=last_message,
|
||||
updatedAt=self._now(),
|
||||
),
|
||||
)
|
||||
|
||||
def _mock_reply(self, message: str) -> str:
|
||||
if "总结" in message:
|
||||
return "Mock BFF:当前重点是把 App 和 BFF 真正联调起来。"
|
||||
if "状态" in message:
|
||||
return "Mock BFF:当前链路在线,默认模型是 cliproxy/gpt-5.4。"
|
||||
if "下一步" in message:
|
||||
return "Mock BFF:下一步建议把 OpenClaw provider 真接进这个 BFF。"
|
||||
return "Mock BFF:消息已经收到,后面这里会替换成真实 OpenClaw 回复。"
|
||||
|
||||
@staticmethod
|
||||
def _now() -> str:
|
||||
return datetime.now(timezone.utc).isoformat()
|
||||
|
||||
@staticmethod
|
||||
def _now_hm() -> str:
|
||||
return datetime.now().strftime("%H:%M")
|
||||
@@ -0,0 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from ..config import Settings
|
||||
from ..models import AgentInfo, ChatMessage, SendMessageResponse, SessionSummary, StatusResponse
|
||||
from .base import BackendService
|
||||
|
||||
|
||||
class OpenClawBackendService(BackendService):
|
||||
def __init__(self, settings: Settings) -> None:
|
||||
self.settings = settings
|
||||
|
||||
async def get_status(self) -> StatusResponse:
|
||||
self._not_implemented()
|
||||
|
||||
async def list_sessions(self) -> list[SessionSummary]:
|
||||
self._not_implemented()
|
||||
|
||||
async def get_messages(self, session_id: str) -> list[ChatMessage]:
|
||||
self._not_implemented()
|
||||
|
||||
async def send_message(self, session_id: str, message: str) -> SendMessageResponse:
|
||||
self._not_implemented()
|
||||
|
||||
async def list_agents(self) -> list[AgentInfo]:
|
||||
self._not_implemented()
|
||||
|
||||
@staticmethod
|
||||
def _not_implemented() -> None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
||||
detail="OpenClaw backend provider is not wired yet. Implement the provider bridge here.",
|
||||
)
|
||||
Reference in new issue
Block a user