Spaces:
Running
Running
File size: 6,359 Bytes
f680f3e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | from __future__ import annotations
from helpers.api import ApiHandler, Request, Response
from plugins._commands.helpers import commands as commands_helper
class Commands(ApiHandler):
async def process(self, input: dict, request: Request) -> dict | Response:
action = str(input.get("action", "") or "").strip()
if action == "list_effective":
return self._list_effective(input)
if action == "list_scope":
return self._list_scope(input)
if action == "get":
return self._get(input)
if action == "save":
return self._save(input)
if action == "delete":
return self._delete(input)
if action == "duplicate":
return self._duplicate(input)
if action == "scope_info":
return self._scope_info(input)
if action == "resolve":
return await self._resolve(input)
return Response(status=400, response=f"Unknown action: {action}")
def _list_effective(self, input: dict) -> dict | Response:
context_scope = commands_helper.get_context_scope(str(input.get("context_id", "") or ""))
commands, scope = commands_helper.list_effective_commands(
project_name=context_scope["project_name"],
)
commands = [
command
for command in commands
if not command.get("frontmatter_extra", {}).get("webui_hidden")
]
return {
"ok": True,
"commands": commands,
"scope": scope,
}
def _list_scope(self, input: dict) -> dict | Response:
commands, scope = commands_helper.list_scope_commands(
project_name=str(input.get("project_name", "") or ""),
)
return {
"ok": True,
"commands": commands,
"builtin_commands": commands_helper.list_builtin_commands(),
"scope": scope,
}
def _get(self, input: dict) -> dict | Response:
path = str(input.get("path", "") or "")
if not path:
return Response(status=400, response="Missing path")
try:
command = commands_helper.get_command(
path,
project_name=str(input.get("project_name", "") or ""),
)
except FileNotFoundError:
return Response(status=404, response="Command not found")
except ValueError as error:
return Response(status=400, response=str(error))
return {"ok": True, "command": command}
def _save(self, input: dict) -> dict | Response:
try:
command = commands_helper.save_command(
project_name=str(input.get("project_name", "") or ""),
existing_path=str(input.get("existing_path", "") or ""),
name=str(input.get("name", "") or ""),
description=str(input.get("description", "") or ""),
argument_hint=str(input.get("argument_hint", "") or ""),
command_type=str(input.get("command_type", "text") or "text"),
body=str(input.get("body", "") or ""),
include_history=bool(input.get("include_history", False)),
extra_frontmatter=input.get("extra_frontmatter", {}) or {},
)
except FileExistsError as error:
return Response(status=409, response=str(error))
except ValueError as error:
return Response(status=400, response=str(error))
return {"ok": True, "command": command}
def _delete(self, input: dict) -> dict | Response:
path = str(input.get("path", "") or "")
if not path:
return Response(status=400, response="Missing path")
try:
commands_helper.delete_command(
path,
project_name=str(input.get("project_name", "") or ""),
)
except FileNotFoundError:
return Response(status=404, response="Command not found")
except ValueError as error:
return Response(status=400, response=str(error))
return {"ok": True}
def _duplicate(self, input: dict) -> dict | Response:
path = str(input.get("path", "") or "")
if not path:
return Response(status=400, response="Missing path")
try:
command = commands_helper.duplicate_command(
path,
project_name=str(input.get("project_name", "") or ""),
)
except FileNotFoundError:
return Response(status=404, response="Command not found")
except ValueError as error:
return Response(status=400, response=str(error))
return {"ok": True, "command": command}
def _scope_info(self, input: dict) -> dict | Response:
explicit_project = str(input.get("project_name", "") or "")
context_scope = commands_helper.get_context_scope(str(input.get("context_id", "") or ""))
project_name = explicit_project if "project_name" in input else context_scope["project_name"]
scope = commands_helper.get_scope_payload(
project_name=project_name,
ensure_directory=bool(input.get("ensure_directory", False)),
)
return {
"ok": True,
"scope": commands_helper.strip_private_scope(scope),
"context_scope": context_scope,
}
async def _resolve(self, input: dict) -> dict | Response:
path = str(input.get("path", "") or "")
if not path:
return Response(status=400, response="Missing path")
slash_text = str(input.get("slash_text", "") or "")
if not slash_text:
return Response(status=400, response="Missing slash_text")
try:
resolution = await commands_helper.resolve_command_invocation(
path=path,
slash_text=slash_text,
project_name=str(input.get("project_name", "") or ""),
context_id=str(input.get("context_id", "") or ""),
)
except FileNotFoundError:
return Response(status=404, response="Command not found")
except ValueError as error:
return Response(status=400, response=str(error))
return {"ok": True, "resolution": resolution}
|