oclaw/runtime/command_parser.py
oliver 4a23b715a2 重构仓库目录为统一的 runtime 分层并清理历史 openclaw 残留。
本次迁移将网关/通道/工具/技能/脚本与协议资源集中到新结构,统一路径常量与脚本转发机制,减少顶层噪音并保证运行与测试行为一致。

Made-with: Cursor
2026-04-25 01:24:23 +08:00

46 lines
1.2 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.

from __future__ import annotations
from dataclasses import dataclass
from typing import Literal
CommandAction = Literal["new", "reset"]
@dataclass(frozen=True)
class ParsedCommand:
action: CommandAction
raw: str
command: str
args: tuple[str, ...]
def parse_internal_command(text: str) -> ParsedCommand | None:
"""
Parse top-level slash/plain reset commands.
Supported:
- /new, new
- /reset, reset
"""
t = str(text or "").strip()
if not t:
return None
parts = [x for x in t.split() if str(x or "").strip()]
if not parts:
return None
first_raw = parts[0].strip()
first_norm = first_raw.lower()
if first_norm.startswith(("/", "/")):
first_norm = first_norm[1:]
# English aliases + localized aliases
new_aliases = {"new", "n", "新建", "重开"}
reset_aliases = {"reset", "r", "重置", "清空"}
if first_norm in new_aliases:
return ParsedCommand(action="new", raw=t, command=first_norm, args=tuple(parts[1:]))
if first_norm in reset_aliases:
return ParsedCommand(action="reset", raw=t, command=first_norm, args=tuple(parts[1:]))
return None
__all__ = ["ParsedCommand", "parse_internal_command"]