klaude-code 1.2.16__py3-none-any.whl → 1.2.18__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- klaude_code/cli/config_cmd.py +1 -1
- klaude_code/cli/debug.py +1 -1
- klaude_code/cli/main.py +3 -9
- klaude_code/cli/runtime.py +20 -13
- klaude_code/command/__init__.py +7 -1
- klaude_code/command/clear_cmd.py +2 -7
- klaude_code/command/command_abc.py +33 -5
- klaude_code/command/debug_cmd.py +79 -0
- klaude_code/command/diff_cmd.py +2 -6
- klaude_code/command/export_cmd.py +7 -7
- klaude_code/command/export_online_cmd.py +145 -0
- klaude_code/command/help_cmd.py +4 -9
- klaude_code/command/model_cmd.py +10 -6
- klaude_code/command/prompt_command.py +2 -6
- klaude_code/command/refresh_cmd.py +2 -7
- klaude_code/command/registry.py +2 -4
- klaude_code/command/release_notes_cmd.py +2 -6
- klaude_code/command/status_cmd.py +2 -7
- klaude_code/command/terminal_setup_cmd.py +2 -6
- klaude_code/command/thinking_cmd.py +13 -8
- klaude_code/config/config.py +16 -17
- klaude_code/config/select_model.py +81 -5
- klaude_code/const/__init__.py +1 -1
- klaude_code/core/executor.py +236 -109
- klaude_code/core/manager/__init__.py +2 -4
- klaude_code/core/manager/sub_agent_manager.py +1 -1
- klaude_code/core/prompts/prompt-claude-code.md +1 -1
- klaude_code/core/prompts/prompt-sub-agent-oracle.md +0 -1
- klaude_code/core/prompts/prompt-sub-agent-web.md +51 -0
- klaude_code/core/reminders.py +9 -35
- klaude_code/core/task.py +8 -0
- klaude_code/core/tool/__init__.py +2 -0
- klaude_code/core/tool/file/read_tool.py +38 -10
- klaude_code/core/tool/report_back_tool.py +28 -2
- klaude_code/core/tool/shell/bash_tool.py +22 -2
- klaude_code/core/tool/tool_runner.py +26 -23
- klaude_code/core/tool/truncation.py +23 -9
- klaude_code/core/tool/web/web_fetch_tool.md +1 -1
- klaude_code/core/tool/web/web_fetch_tool.py +36 -1
- klaude_code/core/tool/web/web_search_tool.md +23 -0
- klaude_code/core/tool/web/web_search_tool.py +126 -0
- klaude_code/core/turn.py +28 -0
- klaude_code/protocol/commands.py +2 -0
- klaude_code/protocol/events.py +8 -0
- klaude_code/protocol/sub_agent/__init__.py +1 -1
- klaude_code/protocol/sub_agent/explore.py +1 -1
- klaude_code/protocol/sub_agent/web.py +79 -0
- klaude_code/protocol/tools.py +1 -0
- klaude_code/session/session.py +2 -2
- klaude_code/session/templates/export_session.html +123 -37
- klaude_code/trace/__init__.py +20 -2
- klaude_code/ui/modes/repl/completers.py +19 -2
- klaude_code/ui/modes/repl/event_handler.py +44 -15
- klaude_code/ui/modes/repl/renderer.py +3 -3
- klaude_code/ui/renderers/metadata.py +2 -4
- klaude_code/ui/renderers/sub_agent.py +14 -10
- klaude_code/ui/renderers/thinking.py +24 -8
- klaude_code/ui/renderers/tools.py +83 -20
- klaude_code/ui/rich/code_panel.py +112 -0
- klaude_code/ui/rich/markdown.py +3 -4
- klaude_code/ui/rich/status.py +30 -6
- klaude_code/ui/rich/theme.py +10 -1
- {klaude_code-1.2.16.dist-info → klaude_code-1.2.18.dist-info}/METADATA +126 -25
- {klaude_code-1.2.16.dist-info → klaude_code-1.2.18.dist-info}/RECORD +67 -63
- klaude_code/core/manager/agent_manager.py +0 -132
- klaude_code/core/prompts/prompt-sub-agent-webfetch.md +0 -46
- klaude_code/protocol/sub_agent/web_fetch.py +0 -74
- /klaude_code/{config → cli}/list_model.py +0 -0
- {klaude_code-1.2.16.dist-info → klaude_code-1.2.18.dist-info}/WHEEL +0 -0
- {klaude_code-1.2.16.dist-info → klaude_code-1.2.18.dist-info}/entry_points.txt +0 -0
klaude_code/cli/config_cmd.py
CHANGED
|
@@ -12,7 +12,7 @@ from klaude_code.trace import log
|
|
|
12
12
|
|
|
13
13
|
def list_models() -> None:
|
|
14
14
|
"""List all models and providers configuration"""
|
|
15
|
-
from klaude_code.
|
|
15
|
+
from klaude_code.cli.list_model import display_models_and_providers
|
|
16
16
|
from klaude_code.ui.terminal.color import is_light_terminal_background
|
|
17
17
|
|
|
18
18
|
config = load_config()
|
klaude_code/cli/debug.py
CHANGED
|
@@ -48,7 +48,7 @@ def open_log_file_in_editor(path: Path) -> None:
|
|
|
48
48
|
editor = os.environ.get("EDITOR")
|
|
49
49
|
|
|
50
50
|
if not editor:
|
|
51
|
-
for cmd in ["open", "xdg-open", "code", "
|
|
51
|
+
for cmd in ["open", "xdg-open", "code", "TextEdit", "notepad"]:
|
|
52
52
|
try:
|
|
53
53
|
subprocess.run(["which", cmd], check=True, capture_output=True)
|
|
54
54
|
editor = cmd
|
klaude_code/cli/main.py
CHANGED
|
@@ -11,7 +11,6 @@ from klaude_code.cli.auth_cmd import register_auth_commands
|
|
|
11
11
|
from klaude_code.cli.config_cmd import register_config_commands
|
|
12
12
|
from klaude_code.cli.debug import DEBUG_FILTER_HELP, open_log_file_in_editor, resolve_debug_settings
|
|
13
13
|
from klaude_code.cli.session_cmd import register_session_commands
|
|
14
|
-
from klaude_code.config import load_config
|
|
15
14
|
from klaude_code.session import Session, resume_select_session
|
|
16
15
|
from klaude_code.trace import DebugType, prepare_debug_log_file
|
|
17
16
|
|
|
@@ -157,13 +156,8 @@ def exec_command(
|
|
|
157
156
|
from klaude_code.config.select_model import select_model_from_config
|
|
158
157
|
|
|
159
158
|
chosen_model = model
|
|
160
|
-
if select_model:
|
|
161
|
-
|
|
162
|
-
config = load_config()
|
|
163
|
-
if config is None:
|
|
164
|
-
raise typer.Exit(1)
|
|
165
|
-
default_name = model or config.main_model
|
|
166
|
-
chosen_model = select_model_from_config(preferred=default_name)
|
|
159
|
+
if model or select_model:
|
|
160
|
+
chosen_model = select_model_from_config(preferred=model)
|
|
167
161
|
if chosen_model is None:
|
|
168
162
|
return
|
|
169
163
|
|
|
@@ -243,7 +237,7 @@ def main_callback(
|
|
|
243
237
|
setup_terminal_title()
|
|
244
238
|
|
|
245
239
|
chosen_model = model
|
|
246
|
-
if select_model:
|
|
240
|
+
if model or select_model:
|
|
247
241
|
chosen_model = select_model_from_config(preferred=model)
|
|
248
242
|
if chosen_model is None:
|
|
249
243
|
return
|
klaude_code/cli/runtime.py
CHANGED
|
@@ -10,7 +10,7 @@ from rich.text import Text
|
|
|
10
10
|
from klaude_code import ui
|
|
11
11
|
from klaude_code.command import has_interactive_command
|
|
12
12
|
from klaude_code.config import Config, load_config
|
|
13
|
-
from klaude_code.core.agent import
|
|
13
|
+
from klaude_code.core.agent import DefaultModelProfileProvider, VanillaModelProfileProvider
|
|
14
14
|
from klaude_code.core.executor import Executor
|
|
15
15
|
from klaude_code.core.manager import build_llm_clients
|
|
16
16
|
from klaude_code.protocol import events, op
|
|
@@ -98,8 +98,10 @@ async def initialize_app_components(init_config: AppInitConfig) -> AppComponents
|
|
|
98
98
|
executor_task = asyncio.create_task(executor.start())
|
|
99
99
|
|
|
100
100
|
theme: str | None = config.theme
|
|
101
|
-
if theme is None:
|
|
101
|
+
if theme is None and not init_config.is_exec_mode:
|
|
102
102
|
# Auto-detect theme from terminal background when config does not specify a theme.
|
|
103
|
+
# Skip detection in exec mode to avoid TTY race conditions with parent process's
|
|
104
|
+
# ESC monitor when running as a subprocess.
|
|
103
105
|
detected = is_light_terminal_background()
|
|
104
106
|
if detected is True:
|
|
105
107
|
theme = "light"
|
|
@@ -145,8 +147,8 @@ async def initialize_session(
|
|
|
145
147
|
await executor.submit_and_wait(op.InitAgentOperation(session_id=session_id))
|
|
146
148
|
await event_queue.join()
|
|
147
149
|
|
|
148
|
-
|
|
149
|
-
return
|
|
150
|
+
active_session_id = executor.context.current_session_id()
|
|
151
|
+
return active_session_id or session_id
|
|
150
152
|
|
|
151
153
|
|
|
152
154
|
async def cleanup_app_components(components: AppComponents) -> None:
|
|
@@ -214,16 +216,12 @@ async def run_interactive(init_config: AppInitConfig, session_id: str | None = N
|
|
|
214
216
|
|
|
215
217
|
# Create status provider for bottom toolbar
|
|
216
218
|
def _status_provider() -> REPLStatusSnapshot:
|
|
217
|
-
agent: Agent | None = None
|
|
218
|
-
# Get the first active agent (there should only be one in interactive mode)
|
|
219
|
-
active_agents = components.executor.context.active_agents
|
|
220
|
-
if active_agents:
|
|
221
|
-
agent = next(iter(active_agents.values()), None)
|
|
222
|
-
|
|
223
219
|
# Check for updates (returns None if uv not available)
|
|
224
220
|
update_message = get_update_message()
|
|
225
221
|
|
|
226
|
-
return build_repl_status_snapshot(
|
|
222
|
+
return build_repl_status_snapshot(
|
|
223
|
+
agent=components.executor.context.current_agent, update_message=update_message
|
|
224
|
+
)
|
|
227
225
|
|
|
228
226
|
# Set up input provider for interactive mode
|
|
229
227
|
input_provider: ui.InputProviderABC = ui.PromptToolkitInput(status_provider=_status_provider)
|
|
@@ -261,7 +259,14 @@ async def run_interactive(init_config: AppInitConfig, session_id: str | None = N
|
|
|
261
259
|
restore_sigint = install_sigint_double_press_exit(_show_toast_once, _hide_progress)
|
|
262
260
|
|
|
263
261
|
try:
|
|
264
|
-
|
|
262
|
+
await initialize_session(components.executor, components.event_queue, session_id=session_id)
|
|
263
|
+
|
|
264
|
+
def _get_active_session_id() -> str | None:
|
|
265
|
+
"""Get the current active session ID dynamically.
|
|
266
|
+
|
|
267
|
+
This is necessary because /clear command creates a new session with a different ID.
|
|
268
|
+
"""
|
|
269
|
+
return components.executor.context.current_session_id()
|
|
265
270
|
|
|
266
271
|
# Input
|
|
267
272
|
await input_provider.start()
|
|
@@ -272,6 +277,8 @@ async def run_interactive(init_config: AppInitConfig, session_id: str | None = N
|
|
|
272
277
|
elif user_input.text.strip() == "":
|
|
273
278
|
continue
|
|
274
279
|
# Submit user input operation - directly use the payload from iter_inputs
|
|
280
|
+
# Use dynamic session_id lookup to handle /clear creating new sessions
|
|
281
|
+
active_session_id = _get_active_session_id()
|
|
275
282
|
submission_id = await components.executor.submit(
|
|
276
283
|
op.UserInputOperation(input=user_input, session_id=active_session_id)
|
|
277
284
|
)
|
|
@@ -282,7 +289,7 @@ async def run_interactive(init_config: AppInitConfig, session_id: str | None = N
|
|
|
282
289
|
else:
|
|
283
290
|
# Esc monitor for long-running, interruptible operations
|
|
284
291
|
async def _on_esc_interrupt() -> None:
|
|
285
|
-
await components.executor.submit(op.InterruptOperation(target_session_id=
|
|
292
|
+
await components.executor.submit(op.InterruptOperation(target_session_id=_get_active_session_id()))
|
|
286
293
|
|
|
287
294
|
stop_event, esc_task = start_esc_interrupt_monitor(_on_esc_interrupt)
|
|
288
295
|
# Wait for this specific task to complete before accepting next input
|
klaude_code/command/__init__.py
CHANGED
|
@@ -28,8 +28,10 @@ def ensure_commands_loaded() -> None:
|
|
|
28
28
|
|
|
29
29
|
# Import and register commands in display order
|
|
30
30
|
from .clear_cmd import ClearCommand
|
|
31
|
+
from .debug_cmd import DebugCommand
|
|
31
32
|
from .diff_cmd import DiffCommand
|
|
32
33
|
from .export_cmd import ExportCommand
|
|
34
|
+
from .export_online_cmd import ExportOnlineCommand
|
|
33
35
|
from .help_cmd import HelpCommand
|
|
34
36
|
from .model_cmd import ModelCommand
|
|
35
37
|
from .refresh_cmd import RefreshTerminalCommand
|
|
@@ -40,16 +42,18 @@ def ensure_commands_loaded() -> None:
|
|
|
40
42
|
|
|
41
43
|
# Register in desired display order
|
|
42
44
|
register(ExportCommand())
|
|
45
|
+
register(ExportOnlineCommand())
|
|
43
46
|
register(RefreshTerminalCommand())
|
|
44
47
|
register(ThinkingCommand())
|
|
45
48
|
register(ModelCommand())
|
|
46
49
|
load_prompt_commands()
|
|
47
|
-
register(ClearCommand())
|
|
48
50
|
register(StatusCommand())
|
|
49
51
|
register(DiffCommand())
|
|
50
52
|
register(HelpCommand())
|
|
51
53
|
register(ReleaseNotesCommand())
|
|
52
54
|
register(TerminalSetupCommand())
|
|
55
|
+
register(DebugCommand())
|
|
56
|
+
register(ClearCommand())
|
|
53
57
|
|
|
54
58
|
# Load prompt-based commands (appended after built-in commands)
|
|
55
59
|
|
|
@@ -58,8 +62,10 @@ def ensure_commands_loaded() -> None:
|
|
|
58
62
|
def __getattr__(name: str) -> object:
|
|
59
63
|
_commands_map = {
|
|
60
64
|
"ClearCommand": "clear_cmd",
|
|
65
|
+
"DebugCommand": "debug_cmd",
|
|
61
66
|
"DiffCommand": "diff_cmd",
|
|
62
67
|
"ExportCommand": "export_cmd",
|
|
68
|
+
"ExportOnlineCommand": "export_online_cmd",
|
|
63
69
|
"HelpCommand": "help_cmd",
|
|
64
70
|
"ModelCommand": "model_cmd",
|
|
65
71
|
"RefreshTerminalCommand": "refresh_cmd",
|
klaude_code/command/clear_cmd.py
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
from
|
|
2
|
-
|
|
3
|
-
from klaude_code.command.command_abc import CommandABC, CommandResult, InputAction
|
|
1
|
+
from klaude_code.command.command_abc import Agent, CommandABC, CommandResult, InputAction
|
|
4
2
|
from klaude_code.protocol import commands
|
|
5
3
|
|
|
6
|
-
if TYPE_CHECKING:
|
|
7
|
-
from klaude_code.core.agent import Agent
|
|
8
|
-
|
|
9
4
|
|
|
10
5
|
class ClearCommand(CommandABC):
|
|
11
6
|
"""Clear current session and start a new conversation"""
|
|
@@ -18,5 +13,5 @@ class ClearCommand(CommandABC):
|
|
|
18
13
|
def summary(self) -> str:
|
|
19
14
|
return "Clear conversation history and free up context"
|
|
20
15
|
|
|
21
|
-
async def run(self, raw: str, agent:
|
|
16
|
+
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
22
17
|
return CommandResult(actions=[InputAction.clear()])
|
|
@@ -1,14 +1,37 @@
|
|
|
1
1
|
from abc import ABC, abstractmethod
|
|
2
2
|
from enum import Enum
|
|
3
|
-
from typing import
|
|
3
|
+
from typing import Protocol
|
|
4
4
|
|
|
5
5
|
from pydantic import BaseModel
|
|
6
6
|
|
|
7
|
-
from klaude_code.
|
|
7
|
+
from klaude_code.llm import LLMClientABC
|
|
8
|
+
from klaude_code.protocol import commands, llm_param
|
|
8
9
|
from klaude_code.protocol import events as protocol_events
|
|
10
|
+
from klaude_code.session.session import Session
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
|
|
13
|
+
class AgentProfile(Protocol):
|
|
14
|
+
"""Protocol for the agent's active model profile."""
|
|
15
|
+
|
|
16
|
+
@property
|
|
17
|
+
def llm_client(self) -> LLMClientABC: ...
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def system_prompt(self) -> str | None: ...
|
|
21
|
+
|
|
22
|
+
@property
|
|
23
|
+
def tools(self) -> list[llm_param.ToolSchema]: ...
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Agent(Protocol):
|
|
27
|
+
"""Protocol for Agent objects passed to commands."""
|
|
28
|
+
|
|
29
|
+
session: Session
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def profile(self) -> AgentProfile | None: ...
|
|
33
|
+
|
|
34
|
+
def get_llm_client(self) -> LLMClientABC: ...
|
|
12
35
|
|
|
13
36
|
|
|
14
37
|
class InputActionType(str, Enum):
|
|
@@ -80,8 +103,13 @@ class CommandABC(ABC):
|
|
|
80
103
|
"""Whether this command support additional parameters."""
|
|
81
104
|
return False
|
|
82
105
|
|
|
106
|
+
@property
|
|
107
|
+
def placeholder(self) -> str:
|
|
108
|
+
"""Placeholder text for additional parameters in help display."""
|
|
109
|
+
return "additional instructions"
|
|
110
|
+
|
|
83
111
|
@abstractmethod
|
|
84
|
-
async def run(self, raw: str, agent:
|
|
112
|
+
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
85
113
|
"""
|
|
86
114
|
Execute the command.
|
|
87
115
|
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
from klaude_code.command.command_abc import Agent, CommandABC, CommandResult
|
|
2
|
+
from klaude_code.protocol import commands, events, model
|
|
3
|
+
from klaude_code.trace import DebugType, get_current_log_file, is_debug_enabled, set_debug_logging
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _format_status() -> str:
|
|
7
|
+
"""Format the current debug status for display."""
|
|
8
|
+
if not is_debug_enabled():
|
|
9
|
+
return "Debug: OFF"
|
|
10
|
+
|
|
11
|
+
log_file = get_current_log_file()
|
|
12
|
+
log_path_str = str(log_file) if log_file else "(console)"
|
|
13
|
+
return f"Debug: ON\nLog file: {log_path_str}"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _parse_debug_filters(raw: str) -> set[DebugType] | None:
|
|
17
|
+
filters: set[DebugType] = set()
|
|
18
|
+
for chunk in raw.split(","):
|
|
19
|
+
normalized = chunk.strip().lower().replace("-", "_")
|
|
20
|
+
if not normalized:
|
|
21
|
+
continue
|
|
22
|
+
try:
|
|
23
|
+
filters.add(DebugType(normalized))
|
|
24
|
+
except ValueError as exc:
|
|
25
|
+
raise ValueError(normalized) from exc
|
|
26
|
+
return filters or None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class DebugCommand(CommandABC):
|
|
30
|
+
"""Toggle debug mode and configure debug filters."""
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def name(self) -> commands.CommandName:
|
|
34
|
+
return commands.CommandName.DEBUG
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def summary(self) -> str:
|
|
38
|
+
return "Toggle debug mode (optional: filter types)"
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def support_addition_params(self) -> bool:
|
|
42
|
+
return True
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def placeholder(self) -> str:
|
|
46
|
+
return "filter types"
|
|
47
|
+
|
|
48
|
+
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
49
|
+
raw = raw.strip()
|
|
50
|
+
|
|
51
|
+
# /debug (no args) - enable debug
|
|
52
|
+
if not raw:
|
|
53
|
+
set_debug_logging(True, write_to_file=True)
|
|
54
|
+
return self._message_result(agent, _format_status())
|
|
55
|
+
|
|
56
|
+
# /debug <filters> - enable with filters
|
|
57
|
+
try:
|
|
58
|
+
filters = _parse_debug_filters(raw)
|
|
59
|
+
if filters:
|
|
60
|
+
set_debug_logging(True, write_to_file=True, filters=filters)
|
|
61
|
+
filter_names = ", ".join(sorted(dt.value for dt in filters))
|
|
62
|
+
return self._message_result(agent, f"Filters: {filter_names}\n{_format_status()}")
|
|
63
|
+
except ValueError:
|
|
64
|
+
pass
|
|
65
|
+
|
|
66
|
+
return self._message_result(agent, f"Invalid filter: {raw}\nValid: {', '.join(dt.value for dt in DebugType)}")
|
|
67
|
+
|
|
68
|
+
def _message_result(self, agent: "Agent", content: str) -> CommandResult:
|
|
69
|
+
return CommandResult(
|
|
70
|
+
events=[
|
|
71
|
+
events.DeveloperMessageEvent(
|
|
72
|
+
session_id=agent.session.id,
|
|
73
|
+
item=model.DeveloperMessageItem(
|
|
74
|
+
content=content,
|
|
75
|
+
command_output=model.CommandOutput(command_name=self.name),
|
|
76
|
+
),
|
|
77
|
+
)
|
|
78
|
+
]
|
|
79
|
+
)
|
klaude_code/command/diff_cmd.py
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import subprocess
|
|
2
2
|
from pathlib import Path
|
|
3
|
-
from typing import TYPE_CHECKING
|
|
4
3
|
|
|
5
|
-
from klaude_code.command.command_abc import CommandABC, CommandResult
|
|
4
|
+
from klaude_code.command.command_abc import Agent, CommandABC, CommandResult
|
|
6
5
|
from klaude_code.protocol import commands, events, model
|
|
7
6
|
|
|
8
|
-
if TYPE_CHECKING:
|
|
9
|
-
from klaude_code.core.agent import Agent
|
|
10
|
-
|
|
11
7
|
|
|
12
8
|
class DiffCommand(CommandABC):
|
|
13
9
|
"""Show git diff for the current repository."""
|
|
@@ -20,7 +16,7 @@ class DiffCommand(CommandABC):
|
|
|
20
16
|
def summary(self) -> str:
|
|
21
17
|
return "Show git diff"
|
|
22
18
|
|
|
23
|
-
async def run(self, raw: str, agent:
|
|
19
|
+
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
24
20
|
try:
|
|
25
21
|
# Check if current directory is in a git repository
|
|
26
22
|
git_check = subprocess.run(
|
|
@@ -2,15 +2,11 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import subprocess
|
|
4
4
|
from pathlib import Path
|
|
5
|
-
from typing import TYPE_CHECKING
|
|
6
5
|
|
|
7
|
-
from klaude_code.command.command_abc import CommandABC, CommandResult
|
|
6
|
+
from klaude_code.command.command_abc import Agent, CommandABC, CommandResult
|
|
8
7
|
from klaude_code.protocol import commands, events, model
|
|
9
8
|
from klaude_code.session.export import build_export_html, get_default_export_path
|
|
10
9
|
|
|
11
|
-
if TYPE_CHECKING:
|
|
12
|
-
from klaude_code.core.agent import Agent
|
|
13
|
-
|
|
14
10
|
|
|
15
11
|
class ExportCommand(CommandABC):
|
|
16
12
|
"""Export the current session into a standalone HTML transcript."""
|
|
@@ -25,7 +21,11 @@ class ExportCommand(CommandABC):
|
|
|
25
21
|
|
|
26
22
|
@property
|
|
27
23
|
def support_addition_params(self) -> bool:
|
|
28
|
-
return
|
|
24
|
+
return True
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def placeholder(self) -> str:
|
|
28
|
+
return "output path"
|
|
29
29
|
|
|
30
30
|
@property
|
|
31
31
|
def is_interactive(self) -> bool:
|
|
@@ -33,7 +33,7 @@ class ExportCommand(CommandABC):
|
|
|
33
33
|
|
|
34
34
|
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
35
35
|
try:
|
|
36
|
-
output_path = self._resolve_output_path(
|
|
36
|
+
output_path = self._resolve_output_path(raw, agent)
|
|
37
37
|
html_doc = self._build_html(agent)
|
|
38
38
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
39
39
|
output_path.write_text(html_doc, encoding="utf-8")
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import secrets
|
|
4
|
+
import shutil
|
|
5
|
+
import subprocess
|
|
6
|
+
import tempfile
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from klaude_code.command.command_abc import Agent, CommandABC, CommandResult
|
|
10
|
+
from klaude_code.protocol import commands, events, model
|
|
11
|
+
from klaude_code.session.export import build_export_html
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ExportOnlineCommand(CommandABC):
|
|
15
|
+
"""Export and deploy the current session to surge.sh as a static webpage."""
|
|
16
|
+
|
|
17
|
+
@property
|
|
18
|
+
def name(self) -> commands.CommandName:
|
|
19
|
+
return commands.CommandName.EXPORT_ONLINE
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def summary(self) -> str:
|
|
23
|
+
return "Export and deploy session to surge.sh"
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def support_addition_params(self) -> bool:
|
|
27
|
+
return False
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def is_interactive(self) -> bool:
|
|
31
|
+
return False
|
|
32
|
+
|
|
33
|
+
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
34
|
+
# Check if npx or surge is available
|
|
35
|
+
surge_cmd = self._get_surge_command()
|
|
36
|
+
if not surge_cmd:
|
|
37
|
+
event = events.DeveloperMessageEvent(
|
|
38
|
+
session_id=agent.session.id,
|
|
39
|
+
item=model.DeveloperMessageItem(
|
|
40
|
+
content="surge.sh CLI not found. Install with: npm install -g surge",
|
|
41
|
+
command_output=model.CommandOutput(command_name=self.name, is_error=True),
|
|
42
|
+
),
|
|
43
|
+
)
|
|
44
|
+
return CommandResult(events=[event])
|
|
45
|
+
|
|
46
|
+
# Check if user is logged in to surge
|
|
47
|
+
if not self._is_surge_logged_in(surge_cmd):
|
|
48
|
+
login_cmd = " ".join([*surge_cmd, "login"])
|
|
49
|
+
event = events.DeveloperMessageEvent(
|
|
50
|
+
session_id=agent.session.id,
|
|
51
|
+
item=model.DeveloperMessageItem(
|
|
52
|
+
content=f"Not logged in to surge.sh. Please run: {login_cmd}",
|
|
53
|
+
command_output=model.CommandOutput(command_name=self.name, is_error=True),
|
|
54
|
+
),
|
|
55
|
+
)
|
|
56
|
+
return CommandResult(events=[event])
|
|
57
|
+
|
|
58
|
+
try:
|
|
59
|
+
html_doc = self._build_html(agent)
|
|
60
|
+
domain = self._generate_domain()
|
|
61
|
+
url = self._deploy_to_surge(surge_cmd, html_doc, domain)
|
|
62
|
+
|
|
63
|
+
event = events.DeveloperMessageEvent(
|
|
64
|
+
session_id=agent.session.id,
|
|
65
|
+
item=model.DeveloperMessageItem(
|
|
66
|
+
content=f"Session deployed to: {url}",
|
|
67
|
+
command_output=model.CommandOutput(command_name=self.name),
|
|
68
|
+
),
|
|
69
|
+
)
|
|
70
|
+
return CommandResult(events=[event])
|
|
71
|
+
except Exception as exc:
|
|
72
|
+
import traceback
|
|
73
|
+
|
|
74
|
+
event = events.DeveloperMessageEvent(
|
|
75
|
+
session_id=agent.session.id,
|
|
76
|
+
item=model.DeveloperMessageItem(
|
|
77
|
+
content=f"Failed to deploy session: {exc}\n{traceback.format_exc()}",
|
|
78
|
+
command_output=model.CommandOutput(command_name=self.name, is_error=True),
|
|
79
|
+
),
|
|
80
|
+
)
|
|
81
|
+
return CommandResult(events=[event])
|
|
82
|
+
|
|
83
|
+
def _get_surge_command(self) -> list[str] | None:
|
|
84
|
+
"""Check if surge CLI is available, prefer npx if available."""
|
|
85
|
+
# Check for npx first (more common)
|
|
86
|
+
if shutil.which("npx"):
|
|
87
|
+
return ["npx", "surge"]
|
|
88
|
+
# Check for globally installed surge
|
|
89
|
+
if shutil.which("surge"):
|
|
90
|
+
return ["surge"]
|
|
91
|
+
return None
|
|
92
|
+
|
|
93
|
+
def _is_surge_logged_in(self, surge_cmd: list[str]) -> bool:
|
|
94
|
+
"""Check if user is logged in to surge.sh via 'surge whoami'."""
|
|
95
|
+
try:
|
|
96
|
+
cmd = [*surge_cmd, "whoami"]
|
|
97
|
+
result = subprocess.run(
|
|
98
|
+
cmd,
|
|
99
|
+
capture_output=True,
|
|
100
|
+
text=True,
|
|
101
|
+
timeout=30,
|
|
102
|
+
)
|
|
103
|
+
# If logged in, whoami returns 0 and prints the email
|
|
104
|
+
# If not logged in, it returns non-zero or prints "Not Authenticated"
|
|
105
|
+
if result.returncode != 0:
|
|
106
|
+
return False
|
|
107
|
+
output = (result.stdout + result.stderr).lower()
|
|
108
|
+
if "not authenticated" in output or "not logged in" in output:
|
|
109
|
+
return False
|
|
110
|
+
return bool(result.stdout.strip())
|
|
111
|
+
except (subprocess.TimeoutExpired, OSError):
|
|
112
|
+
return False
|
|
113
|
+
|
|
114
|
+
def _generate_domain(self) -> str:
|
|
115
|
+
"""Generate a random subdomain for surge.sh."""
|
|
116
|
+
random_suffix = secrets.token_hex(4)
|
|
117
|
+
return f"klaude-session-{random_suffix}.surge.sh"
|
|
118
|
+
|
|
119
|
+
def _deploy_to_surge(self, surge_cmd: list[str], html_content: str, domain: str) -> str:
|
|
120
|
+
"""Deploy HTML content to surge.sh and return the URL."""
|
|
121
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
122
|
+
html_path = Path(tmpdir) / "index.html"
|
|
123
|
+
html_path.write_text(html_content, encoding="utf-8")
|
|
124
|
+
|
|
125
|
+
# Run surge with --domain flag
|
|
126
|
+
cmd = [*surge_cmd, tmpdir, "--domain", domain]
|
|
127
|
+
result = subprocess.run(
|
|
128
|
+
cmd,
|
|
129
|
+
capture_output=True,
|
|
130
|
+
text=True,
|
|
131
|
+
timeout=60,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
if result.returncode != 0:
|
|
135
|
+
error_msg = result.stderr or result.stdout or "Unknown error"
|
|
136
|
+
raise RuntimeError(f"Surge deployment failed: {error_msg}")
|
|
137
|
+
|
|
138
|
+
return f"https://{domain}"
|
|
139
|
+
|
|
140
|
+
def _build_html(self, agent: Agent) -> str:
|
|
141
|
+
profile = agent.profile
|
|
142
|
+
system_prompt = (profile.system_prompt if profile else "") or ""
|
|
143
|
+
tools = profile.tools if profile else []
|
|
144
|
+
model_name = profile.llm_client.model_name if profile else "unknown"
|
|
145
|
+
return build_export_html(agent.session, system_prompt, tools, model_name)
|
klaude_code/command/help_cmd.py
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
from
|
|
2
|
-
|
|
3
|
-
from klaude_code.command.command_abc import CommandABC, CommandResult
|
|
1
|
+
from klaude_code.command.command_abc import Agent, CommandABC, CommandResult
|
|
4
2
|
from klaude_code.protocol import commands, events, model
|
|
5
3
|
|
|
6
|
-
if TYPE_CHECKING:
|
|
7
|
-
from klaude_code.core.agent import Agent
|
|
8
|
-
|
|
9
4
|
|
|
10
5
|
class HelpCommand(CommandABC):
|
|
11
6
|
"""Display help information for all available slash commands."""
|
|
@@ -18,7 +13,7 @@ class HelpCommand(CommandABC):
|
|
|
18
13
|
def summary(self) -> str:
|
|
19
14
|
return "Show help and available commands"
|
|
20
15
|
|
|
21
|
-
async def run(self, raw: str, agent:
|
|
16
|
+
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
22
17
|
lines: list[str] = [
|
|
23
18
|
"""
|
|
24
19
|
Usage:
|
|
@@ -39,8 +34,8 @@ Available slash commands:"""
|
|
|
39
34
|
|
|
40
35
|
if commands:
|
|
41
36
|
for cmd_name, cmd_obj in sorted(commands.items()):
|
|
42
|
-
|
|
43
|
-
lines.append(f" [b]/{cmd_name}[/b]{
|
|
37
|
+
placeholder = f" \\[{cmd_obj.placeholder}]" if cmd_obj.support_addition_params else ""
|
|
38
|
+
lines.append(f" [b]/{cmd_name}[/b]{placeholder} — {cmd_obj.summary}")
|
|
44
39
|
|
|
45
40
|
event = events.DeveloperMessageEvent(
|
|
46
41
|
session_id=agent.session.id,
|
klaude_code/command/model_cmd.py
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import asyncio
|
|
2
|
-
from typing import TYPE_CHECKING
|
|
3
2
|
|
|
4
|
-
from klaude_code.command.command_abc import CommandABC, CommandResult, InputAction
|
|
3
|
+
from klaude_code.command.command_abc import Agent, CommandABC, CommandResult, InputAction
|
|
5
4
|
from klaude_code.config.select_model import select_model_from_config
|
|
6
5
|
from klaude_code.protocol import commands, events, model
|
|
7
6
|
|
|
8
|
-
if TYPE_CHECKING:
|
|
9
|
-
from klaude_code.core.agent import Agent
|
|
10
|
-
|
|
11
7
|
|
|
12
8
|
class ModelCommand(CommandABC):
|
|
13
9
|
"""Display or change the model configuration."""
|
|
@@ -24,7 +20,15 @@ class ModelCommand(CommandABC):
|
|
|
24
20
|
def is_interactive(self) -> bool:
|
|
25
21
|
return True
|
|
26
22
|
|
|
27
|
-
|
|
23
|
+
@property
|
|
24
|
+
def support_addition_params(self) -> bool:
|
|
25
|
+
return True
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def placeholder(self) -> str:
|
|
29
|
+
return "model name"
|
|
30
|
+
|
|
31
|
+
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
28
32
|
selected_model = await asyncio.to_thread(select_model_from_config, preferred=raw)
|
|
29
33
|
|
|
30
34
|
current_model = agent.profile.llm_client.model_name if agent.profile else None
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
from importlib.resources import files
|
|
2
|
-
from typing import TYPE_CHECKING
|
|
3
2
|
|
|
4
3
|
import yaml
|
|
5
4
|
|
|
6
|
-
from klaude_code.command.command_abc import CommandABC, CommandResult, InputAction
|
|
5
|
+
from klaude_code.command.command_abc import Agent, CommandABC, CommandResult, InputAction
|
|
7
6
|
from klaude_code.protocol import commands
|
|
8
7
|
from klaude_code.trace import log_debug
|
|
9
8
|
|
|
10
|
-
if TYPE_CHECKING:
|
|
11
|
-
from klaude_code.core.agent import Agent
|
|
12
|
-
|
|
13
9
|
|
|
14
10
|
class PromptCommand(CommandABC):
|
|
15
11
|
"""Command that loads a prompt from a markdown file."""
|
|
@@ -59,7 +55,7 @@ class PromptCommand(CommandABC):
|
|
|
59
55
|
def support_addition_params(self) -> bool:
|
|
60
56
|
return True
|
|
61
57
|
|
|
62
|
-
async def run(self, raw: str, agent:
|
|
58
|
+
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
63
59
|
self._ensure_loaded()
|
|
64
60
|
template_content = self._content or ""
|
|
65
61
|
user_input = raw.strip() or "<none>"
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
from
|
|
2
|
-
|
|
3
|
-
from klaude_code.command.command_abc import CommandABC, CommandResult
|
|
1
|
+
from klaude_code.command.command_abc import Agent, CommandABC, CommandResult
|
|
4
2
|
from klaude_code.protocol import commands, events
|
|
5
3
|
|
|
6
|
-
if TYPE_CHECKING:
|
|
7
|
-
from klaude_code.core.agent import Agent
|
|
8
|
-
|
|
9
4
|
|
|
10
5
|
class RefreshTerminalCommand(CommandABC):
|
|
11
6
|
"""Refresh terminal display"""
|
|
@@ -22,7 +17,7 @@ class RefreshTerminalCommand(CommandABC):
|
|
|
22
17
|
def is_interactive(self) -> bool:
|
|
23
18
|
return True
|
|
24
19
|
|
|
25
|
-
async def run(self, raw: str, agent:
|
|
20
|
+
async def run(self, raw: str, agent: Agent) -> CommandResult:
|
|
26
21
|
import os
|
|
27
22
|
|
|
28
23
|
os.system("cls" if os.name == "nt" else "clear")
|