oclaw/openclaw_runtime/command_parser.py
oliver ba3836f00f 初始化:独立 oclaw 仓库首提交
- 在 oclaw/ 下重新初始化 Git 仓库
- 补齐子仓库 .gitignore,避免提交本地运行态数据(_local、node_modules、logs 等)
- 提交当前工程代码与配置

Made-with: Cursor
2026-04-24 22:31:22 +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"]