mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-17 08:33:27 +08:00
初始化:独立 oclaw 仓库首提交
- 在 oclaw/ 下重新初始化 Git 仓库 - 补齐子仓库 .gitignore,避免提交本地运行态数据(_local、node_modules、logs 等) - 提交当前工程代码与配置 Made-with: Cursor
This commit is contained in:
commit
ba3836f00f
579 changed files with 83112 additions and 0 deletions
2
interfaces/gateway/__init__.py
Normal file
2
interfaces/gateway/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
"""Gateway interface adapters and method dispatch."""
|
||||
|
||||
92
interfaces/gateway/context_builder.py
Normal file
92
interfaces/gateway/context_builder.py
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from oclaw.openclaw_runtime.skill_installer import install_skill_from_registry_archive
|
||||
from oclaw.tools.skills.clawhub_client import get_skill_detail as clawhub_get_skill_detail
|
||||
from oclaw.tools.skills.clawhub_client import search_skills as clawhub_search_skills
|
||||
|
||||
|
||||
def build_common_gateway_context(*, store: Any) -> dict[str, Any]:
|
||||
def _read_session_messages(session_key: str, limit: int) -> list[dict[str, Any]]:
|
||||
rows = store.get_messages(
|
||||
session_id=str(session_key or "").strip(),
|
||||
limit=max(1, min(int(limit or 100), 1000)),
|
||||
)
|
||||
return [
|
||||
{
|
||||
"id": int(getattr(m, "id", 0) or 0),
|
||||
"role": str(getattr(m, "role", "") or ""),
|
||||
"content": str(getattr(m, "content", "") or ""),
|
||||
"tool_calls": getattr(m, "tool_calls", None),
|
||||
"attachments": getattr(m, "attachments", None),
|
||||
"timestamp": str(getattr(m, "timestamp", "") or ""),
|
||||
}
|
||||
for m in rows
|
||||
]
|
||||
|
||||
def _list_sessions() -> list[dict[str, Any]]:
|
||||
rows = store.list_sessions(limit=200, offset=0)
|
||||
return [
|
||||
{
|
||||
"key": str(s.id),
|
||||
"sessionId": str(s.id),
|
||||
"title": str(s.title or ""),
|
||||
"createdAt": str(s.created_at or ""),
|
||||
"lastMessageAt": str(getattr(s, "last_message_at", "") or ""),
|
||||
}
|
||||
for s in rows
|
||||
]
|
||||
|
||||
def _get_session(session_key: str) -> dict[str, Any]:
|
||||
sess = store.get_session(str(session_key or "").strip())
|
||||
if not sess:
|
||||
return {}
|
||||
return {
|
||||
"key": str(sess.id),
|
||||
"sessionId": str(sess.id),
|
||||
"title": str(sess.title or ""),
|
||||
"createdAt": str(sess.created_at or ""),
|
||||
"lastMessageAt": str(getattr(sess, "last_message_at", "") or ""),
|
||||
}
|
||||
|
||||
def _search_clawhub_skills(p: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
q = str((p or {}).get("query") or "").strip()
|
||||
lim = int((p or {}).get("limit") or 20)
|
||||
return clawhub_search_skills(q, limit=lim)
|
||||
|
||||
def _fetch_clawhub_skill_detail(p: dict[str, Any]) -> dict[str, Any]:
|
||||
slug = str((p or {}).get("slug") or "").strip()
|
||||
return clawhub_get_skill_detail(slug)
|
||||
|
||||
def _install_skill_from_clawhub(p: dict[str, Any]) -> dict[str, Any]:
|
||||
slug = str((p or {}).get("slug") or "").strip()
|
||||
version = str((p or {}).get("version") or "").strip()
|
||||
force = bool((p or {}).get("force"))
|
||||
archive_url = str((p or {}).get("archiveUrl") or "").strip()
|
||||
if not archive_url and slug:
|
||||
d = clawhub_get_skill_detail(slug)
|
||||
if version:
|
||||
for v in (d.get("versions") or []):
|
||||
if isinstance(v, dict) and str(v.get("version") or "").strip() == version:
|
||||
archive_url = str(v.get("archiveUrl") or "").strip()
|
||||
break
|
||||
if not archive_url:
|
||||
archive_url = str(d.get("archiveUrl") or "").strip()
|
||||
if not archive_url:
|
||||
return {"ok": False, "error": "archive_url_unavailable"}
|
||||
out = install_skill_from_registry_archive(store=store, archive_url=archive_url, overwrite=force)
|
||||
return {"ok": bool(out.ok), "result": out.__dict__}
|
||||
|
||||
return {
|
||||
"store": store,
|
||||
"search_clawhub_skills": _search_clawhub_skills,
|
||||
"fetch_clawhub_skill_detail": _fetch_clawhub_skill_detail,
|
||||
"install_skill_from_clawhub": _install_skill_from_clawhub,
|
||||
"read_session_messages": _read_session_messages,
|
||||
"list_sessions": _list_sessions,
|
||||
"get_session": _get_session,
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["build_common_gateway_context"]
|
||||
36
interfaces/gateway/dispatcher.py
Normal file
36
interfaces/gateway/dispatcher.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from oclaw.gateway.server_methods.agent import agent_handlers
|
||||
from oclaw.gateway.server_methods.chat import chat_handlers
|
||||
from oclaw.gateway.server_methods.commands import commands_handlers
|
||||
from oclaw.gateway.server_methods.connect import connect_handlers
|
||||
from oclaw.gateway.server_methods.sessions import sessions_handlers
|
||||
from oclaw.gateway.server_methods.skills import skills_handlers
|
||||
from oclaw.gateway.server_methods.shared_types import GatewayRequestHandlers
|
||||
|
||||
|
||||
def build_gateway_method_handlers() -> GatewayRequestHandlers:
|
||||
"""Build a flat method->handler map for gateway dispatch."""
|
||||
merged: GatewayRequestHandlers = {}
|
||||
for group in (
|
||||
connect_handlers,
|
||||
commands_handlers,
|
||||
chat_handlers,
|
||||
sessions_handlers,
|
||||
skills_handlers,
|
||||
agent_handlers,
|
||||
):
|
||||
for method, handler in (group or {}).items():
|
||||
merged[str(method)] = handler
|
||||
# Backward-compatible alias.
|
||||
if "agent" in merged and "agent.run" not in merged:
|
||||
merged["agent.run"] = merged["agent"]
|
||||
return merged
|
||||
|
||||
|
||||
def method_names() -> list[str]:
|
||||
return sorted(build_gateway_method_handlers().keys())
|
||||
|
||||
|
||||
__all__ = ["build_gateway_method_handlers", "method_names"]
|
||||
|
||||
50
interfaces/gateway/http_adapter.py
Normal file
50
interfaces/gateway/http_adapter.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from oclaw.platform.config.paths import db_path
|
||||
from oclaw.platform.persistence.sqlite_store import SqliteStore
|
||||
|
||||
from .context_builder import build_common_gateway_context
|
||||
from .dispatcher import build_gateway_method_handlers
|
||||
|
||||
|
||||
def _build_http_context() -> dict[str, Any]:
|
||||
store = SqliteStore(db_path())
|
||||
context = build_common_gateway_context(store=store)
|
||||
context["session_event_subscribers"] = set()
|
||||
context["session_message_subscribers"] = {}
|
||||
return context
|
||||
|
||||
|
||||
def dispatch_gateway_http_method(method: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
handlers = build_gateway_method_handlers()
|
||||
m = str(method or "").strip()
|
||||
handler = handlers.get(m)
|
||||
if not callable(handler):
|
||||
return {"ok": False, "payload": None, "error": {"code": "INVALID_REQUEST", "message": f"unknown method: {m}"}}
|
||||
|
||||
result: dict[str, Any] = {"ok": False, "payload": None, "error": {"code": "UNAVAILABLE", "message": "no response"}}
|
||||
|
||||
def _respond(ok: bool, payload: Any | None, error: dict[str, Any] | None, _meta: dict[str, Any] | None) -> None:
|
||||
result["ok"] = bool(ok)
|
||||
result["payload"] = payload
|
||||
result["error"] = error
|
||||
|
||||
opts = {
|
||||
"req": {"id": "http", "method": m, "params": dict(params or {})},
|
||||
"params": dict(params or {}),
|
||||
"client": {"conn_id": "http", "internal": {}},
|
||||
"respond": _respond,
|
||||
"context": _build_http_context(),
|
||||
"is_webchat_connect": lambda _p: False,
|
||||
}
|
||||
try:
|
||||
handler(opts)
|
||||
except Exception as exc:
|
||||
return {"ok": False, "payload": None, "error": {"code": "UNAVAILABLE", "message": f"{type(exc).__name__}: {exc}"}}
|
||||
return result
|
||||
|
||||
|
||||
__all__ = ["dispatch_gateway_http_method"]
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue