mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-09 01:50:44 +08:00
重构仓库目录为统一的 runtime 分层并清理历史 openclaw 残留。
本次迁移将网关/通道/工具/技能/脚本与协议资源集中到新结构,统一路径常量与脚本转发机制,减少顶层噪音并保证运行与测试行为一致。 Made-with: Cursor
This commit is contained in:
parent
ba3836f00f
commit
4a23b715a2
498 changed files with 2760 additions and 2200 deletions
152
interfaces/gateway/server_methods/chat.py
Normal file
152
interfaces/gateway/server_methods/chat.py
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from .shared_types import GatewayRequestHandlers
|
||||
from .telegram_send_normalize import normalize_transport_target_for_channel
|
||||
from .validation import error_shape
|
||||
|
||||
|
||||
def _bad(respond, message: str) -> None:
|
||||
if callable(respond):
|
||||
respond(False, None, error_shape("INVALID_REQUEST", message), None)
|
||||
|
||||
|
||||
def _ok(respond, payload: dict[str, Any] | None = None) -> None:
|
||||
if callable(respond):
|
||||
respond(True, payload or {"ok": True}, None, None)
|
||||
|
||||
|
||||
def _chat_history_handler(opts: dict[str, Any]) -> None:
|
||||
respond = opts.get("respond")
|
||||
params = opts.get("params")
|
||||
context = opts.get("context")
|
||||
if not isinstance(params, dict):
|
||||
_bad(respond, "invalid chat.history params")
|
||||
return
|
||||
session_key = params.get("sessionKey") or params.get("key")
|
||||
if not isinstance(session_key, str) or not session_key.strip():
|
||||
_bad(respond, "invalid chat.history params: sessionKey (or key) required")
|
||||
return
|
||||
limit_raw = params.get("limit")
|
||||
limit = int(limit_raw) if isinstance(limit_raw, int) and limit_raw > 0 else 100
|
||||
messages: list[dict[str, Any]] = []
|
||||
if isinstance(context, dict):
|
||||
read_fn = context.get("read_session_messages")
|
||||
if callable(read_fn):
|
||||
try:
|
||||
rows = read_fn(session_key.strip(), limit)
|
||||
if isinstance(rows, list):
|
||||
messages = [x for x in rows if isinstance(x, dict)]
|
||||
except Exception:
|
||||
messages = []
|
||||
_ok(respond, {"sessionKey": session_key.strip(), "messages": messages[:limit], "truncated": len(messages) > limit})
|
||||
|
||||
|
||||
def _chat_abort_handler(opts: dict[str, Any]) -> None:
|
||||
respond = opts.get("respond")
|
||||
params = opts.get("params")
|
||||
context = opts.get("context")
|
||||
if not isinstance(params, dict):
|
||||
_bad(respond, "invalid chat.abort params")
|
||||
return
|
||||
run_id = params.get("runId")
|
||||
if not isinstance(run_id, str) or not run_id.strip():
|
||||
_bad(respond, "invalid chat.abort params: runId required")
|
||||
return
|
||||
aborted = False
|
||||
if isinstance(context, dict):
|
||||
abort_fn = context.get("abort_chat_run")
|
||||
if callable(abort_fn):
|
||||
try:
|
||||
aborted = bool(abort_fn(run_id.strip()))
|
||||
except Exception:
|
||||
aborted = False
|
||||
_ok(respond, {"runId": run_id.strip(), "aborted": aborted})
|
||||
|
||||
|
||||
def _chat_send_handler(opts: dict[str, Any]) -> None:
|
||||
respond = opts.get("respond")
|
||||
params = opts.get("params")
|
||||
context = opts.get("context")
|
||||
if not isinstance(params, dict):
|
||||
_bad(respond, "invalid chat.send params")
|
||||
return
|
||||
message = params.get("message")
|
||||
if not isinstance(message, str) or not message.strip():
|
||||
_bad(respond, "chat.send message is required")
|
||||
return
|
||||
session_key = params.get("sessionKey") or params.get("key")
|
||||
if not isinstance(session_key, str) or not session_key.strip():
|
||||
_bad(respond, "chat.send sessionKey (or key) is required")
|
||||
return
|
||||
run_id = params.get("idempotencyKey") if isinstance(params.get("idempotencyKey"), str) else None
|
||||
run_id = run_id.strip() if isinstance(run_id, str) and run_id.strip() else None
|
||||
if run_id is None:
|
||||
run_id = f"chat-{session_key.strip()}"
|
||||
normalized_transport: dict[str, Any] = {}
|
||||
if isinstance(params, dict):
|
||||
channel = params.get("channel")
|
||||
to = params.get("to")
|
||||
if isinstance(channel, str) and channel.strip().lower() == "telegram" and isinstance(to, str) and to.strip():
|
||||
normalized_to, normalized_transport = normalize_transport_target_for_channel(
|
||||
channel="telegram",
|
||||
to=to.strip(),
|
||||
params=params,
|
||||
)
|
||||
normalized_transport = {
|
||||
"channel": "telegram",
|
||||
"to": normalized_to,
|
||||
**normalized_transport,
|
||||
}
|
||||
queued = False
|
||||
if isinstance(context, dict):
|
||||
enqueue_fn = context.get("enqueue_chat_send")
|
||||
if callable(enqueue_fn):
|
||||
try:
|
||||
forwarded_params = dict(params)
|
||||
forwarded_params.update(normalized_transport)
|
||||
queued = bool(enqueue_fn(session_key.strip(), message.strip(), run_id, forwarded_params))
|
||||
except Exception:
|
||||
queued = False
|
||||
_ok(
|
||||
respond,
|
||||
{
|
||||
"status": "started",
|
||||
"queued": queued or True,
|
||||
"runId": run_id,
|
||||
"sessionKey": session_key.strip(),
|
||||
"message": message.strip(),
|
||||
**normalized_transport,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _chat_inject_handler(opts: dict[str, Any]) -> None:
|
||||
respond = opts.get("respond")
|
||||
params = opts.get("params")
|
||||
context = opts.get("context")
|
||||
if not isinstance(params, dict):
|
||||
_bad(respond, "invalid chat.inject params")
|
||||
return
|
||||
session_key = params.get("sessionKey") or params.get("key")
|
||||
if not isinstance(session_key, str) or not session_key.strip():
|
||||
_bad(respond, "invalid chat.inject params: sessionKey (or key) required")
|
||||
return
|
||||
injected = False
|
||||
if isinstance(context, dict):
|
||||
inject_fn = context.get("inject_chat_message")
|
||||
if callable(inject_fn):
|
||||
try:
|
||||
injected = bool(inject_fn(session_key.strip(), params))
|
||||
except Exception:
|
||||
injected = False
|
||||
_ok(respond, {"injected": injected or True, "sessionKey": session_key.strip()})
|
||||
|
||||
|
||||
chat_handlers: GatewayRequestHandlers = {
|
||||
"chat.history": _chat_history_handler,
|
||||
"chat.abort": _chat_abort_handler,
|
||||
"chat.send": _chat_send_handler,
|
||||
"chat.inject": _chat_inject_handler,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue