Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target/
# Claude Code local artifacts (not shared)
.claude/settings.local.json
.claude/sessions/
.port_sessions/

# Editor and OS artifacts
*.swp
Expand Down
79 changes: 56 additions & 23 deletions src/commands.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from __future__ import annotations

import json
from collections.abc import Mapping
from dataclasses import dataclass
from functools import lru_cache
from pathlib import Path
from types import MappingProxyType

from .models import PortingBacklog, PortingModule

SNAPSHOT_PATH = Path(__file__).resolve().parent / 'reference_data' / 'commands_snapshot.json'
SNAPSHOT_PATH = (
Path(__file__).resolve().parent / "reference_data" / "commands_snapshot.json"
)


@dataclass(frozen=True)
Expand All @@ -24,10 +28,10 @@ def load_command_snapshot() -> tuple[PortingModule, ...]:
raw_entries = json.loads(SNAPSHOT_PATH.read_text())
return tuple(
PortingModule(
name=entry['name'],
responsibility=entry['responsibility'],
source_hint=entry['source_hint'],
status='mirrored',
name=entry["name"],
responsibility=entry["responsibility"],
source_hint=entry["source_hint"],
status="mirrored",
)
for entry in raw_entries
)
Expand All @@ -42,49 +46,78 @@ def built_in_command_names() -> frozenset[str]:


def build_command_backlog() -> PortingBacklog:
return PortingBacklog(title='Command surface', modules=list(PORTED_COMMANDS))
return PortingBacklog(title="Command surface", modules=list(PORTED_COMMANDS))


def command_names() -> list[str]:
return [module.name for module in PORTED_COMMANDS]


def get_command(name: str) -> PortingModule | None:
needle = name.lower()
@lru_cache(maxsize=1)
def _get_command_lookup() -> Mapping[str, PortingModule]:
lookup: dict[str, PortingModule] = {}
for module in PORTED_COMMANDS:
if module.name.lower() == needle:
return module
return None
name_lower = module.name.lower()
if name_lower not in lookup:
lookup[name_lower] = module
return MappingProxyType(lookup)


def get_commands(cwd: str | None = None, include_plugin_commands: bool = True, include_skill_commands: bool = True) -> tuple[PortingModule, ...]:
def get_command(name: str) -> PortingModule | None:
return _get_command_lookup().get(name.lower())


def get_commands(
include_plugin_commands: bool = True,
include_skill_commands: bool = True,
) -> tuple[PortingModule, ...]:
commands = list(PORTED_COMMANDS)
if not include_plugin_commands:
commands = [module for module in commands if 'plugin' not in module.source_hint.lower()]
commands = [
module for module in commands if "plugin" not in module.source_hint.lower()
]
if not include_skill_commands:
commands = [module for module in commands if 'skills' not in module.source_hint.lower()]
commands = [
module for module in commands if "skills" not in module.source_hint.lower()
]
return tuple(commands)
Comment thread
badMade marked this conversation as resolved.


def find_commands(query: str, limit: int = 20) -> list[PortingModule]:
needle = query.lower()
matches = [module for module in PORTED_COMMANDS if needle in module.name.lower() or needle in module.source_hint.lower()]
matches = [
module
for module in PORTED_COMMANDS
if needle in module.name.lower() or needle in module.source_hint.lower()
]
return matches[:limit]


def execute_command(name: str, prompt: str = '') -> CommandExecution:
def execute_command(name: str, prompt: str = "") -> CommandExecution:
module = get_command(name)
if module is None:
return CommandExecution(name=name, source_hint='', prompt=prompt, handled=False, message=f'Unknown mirrored command: {name}')
return CommandExecution(
name=name,
source_hint="",
prompt=prompt,
handled=False,
message=f"Unknown mirrored command: {name}",
)
action = f"Mirrored command '{module.name}' from {module.source_hint} would handle prompt {prompt!r}."
return CommandExecution(name=module.name, source_hint=module.source_hint, prompt=prompt, handled=True, message=action)
return CommandExecution(
name=module.name,
source_hint=module.source_hint,
prompt=prompt,
handled=True,
message=action,
)


def render_command_index(limit: int = 20, query: str | None = None) -> str:
modules = find_commands(query, limit) if query else list(PORTED_COMMANDS[:limit])
lines = [f'Command entries: {len(PORTED_COMMANDS)}', '']
lines = [f"Command entries: {len(PORTED_COMMANDS)}", ""]
if query:
lines.append(f'Filtered by: {query}')
lines.append('')
lines.extend(f'- {module.name} — {module.source_hint}' for module in modules)
return '\n'.join(lines)
lines.append(f"Filtered by: {query}")
lines.append("")
lines.extend(f"- {module.name} — {module.source_hint}" for module in modules)
return "\n".join(lines)
Loading