klaude-code 1.2.22__py3-none-any.whl → 1.2.24__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.
Files changed (56) hide show
  1. klaude_code/command/prompt-jj-describe.md +32 -0
  2. klaude_code/command/status_cmd.py +1 -1
  3. klaude_code/{const/__init__.py → const.py} +11 -2
  4. klaude_code/core/executor.py +1 -1
  5. klaude_code/core/manager/sub_agent_manager.py +1 -1
  6. klaude_code/core/reminders.py +51 -0
  7. klaude_code/core/task.py +37 -18
  8. klaude_code/core/tool/__init__.py +1 -4
  9. klaude_code/core/tool/file/read_tool.py +23 -1
  10. klaude_code/core/tool/file/write_tool.py +7 -3
  11. klaude_code/core/tool/skill/__init__.py +0 -0
  12. klaude_code/core/tool/{memory → skill}/skill_tool.py +16 -39
  13. klaude_code/llm/openai_compatible/client.py +29 -102
  14. klaude_code/llm/openai_compatible/stream.py +272 -0
  15. klaude_code/llm/openrouter/client.py +29 -109
  16. klaude_code/llm/openrouter/{reasoning_handler.py → reasoning.py} +24 -2
  17. klaude_code/protocol/model.py +15 -2
  18. klaude_code/session/export.py +1 -1
  19. klaude_code/session/store.py +4 -2
  20. klaude_code/skill/__init__.py +27 -0
  21. klaude_code/skill/assets/deslop/SKILL.md +17 -0
  22. klaude_code/skill/assets/dev-docs/SKILL.md +108 -0
  23. klaude_code/skill/assets/handoff/SKILL.md +39 -0
  24. klaude_code/skill/assets/jj-workspace/SKILL.md +20 -0
  25. klaude_code/skill/assets/skill-creator/SKILL.md +139 -0
  26. klaude_code/{core/tool/memory/skill_loader.py → skill/loader.py} +60 -24
  27. klaude_code/skill/manager.py +70 -0
  28. klaude_code/skill/system_skills.py +192 -0
  29. klaude_code/ui/core/stage_manager.py +0 -3
  30. klaude_code/ui/modes/repl/completers.py +103 -3
  31. klaude_code/ui/modes/repl/event_handler.py +101 -49
  32. klaude_code/ui/modes/repl/input_prompt_toolkit.py +55 -6
  33. klaude_code/ui/modes/repl/renderer.py +24 -17
  34. klaude_code/ui/renderers/assistant.py +7 -2
  35. klaude_code/ui/renderers/developer.py +12 -0
  36. klaude_code/ui/renderers/diffs.py +1 -1
  37. klaude_code/ui/renderers/metadata.py +6 -8
  38. klaude_code/ui/renderers/sub_agent.py +28 -5
  39. klaude_code/ui/renderers/thinking.py +16 -10
  40. klaude_code/ui/renderers/tools.py +83 -34
  41. klaude_code/ui/renderers/user_input.py +32 -2
  42. klaude_code/ui/rich/markdown.py +40 -20
  43. klaude_code/ui/rich/status.py +15 -19
  44. klaude_code/ui/rich/theme.py +70 -17
  45. {klaude_code-1.2.22.dist-info → klaude_code-1.2.24.dist-info}/METADATA +18 -13
  46. {klaude_code-1.2.22.dist-info → klaude_code-1.2.24.dist-info}/RECORD +49 -45
  47. klaude_code/command/prompt-deslop.md +0 -14
  48. klaude_code/command/prompt-dev-docs-update.md +0 -56
  49. klaude_code/command/prompt-dev-docs.md +0 -46
  50. klaude_code/command/prompt-handoff.md +0 -33
  51. klaude_code/command/prompt-jj-workspace.md +0 -18
  52. klaude_code/core/tool/memory/__init__.py +0 -5
  53. klaude_code/llm/openai_compatible/stream_processor.py +0 -83
  54. /klaude_code/core/tool/{memory → skill}/skill_tool.md +0 -0
  55. {klaude_code-1.2.22.dist-info → klaude_code-1.2.24.dist-info}/WHEEL +0 -0
  56. {klaude_code-1.2.22.dist-info → klaude_code-1.2.24.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,32 @@
1
+ ---
2
+ description: Add description for current jj change
3
+ ---
4
+
5
+ Run `jj status` and `jj diff --git` to see the current changes and add a description for the it.
6
+
7
+ In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example:<example>
8
+ jj describe -m "$(cat <<'EOF'
9
+ Commit message here.
10
+ EOF
11
+ )"
12
+ </example>
13
+
14
+ Follow the [Conventional Commits](https://www.conventionalcommits.org/) specification:
15
+ ```
16
+ <type>(<scope>): <description>
17
+ ```
18
+
19
+ Types:
20
+ - `feat`: New feature
21
+ - `fix`: Bug fix
22
+ - `docs`: Documentation changes
23
+ - `style`: Code style changes (formatting, no logic change)
24
+ - `refactor`: Code refactoring (no feature or fix)
25
+ - `test`: Adding or updating tests
26
+ - `chore`: Build process, dependencies, or tooling changes
27
+
28
+ Examples:
29
+ - `feat(cli): add --verbose flag for debug output`
30
+ - `fix(llm): handle API timeout errors gracefully`
31
+ - `docs(readme): update installation instructions`
32
+ - `refactor(core): simplify session state management`
@@ -22,7 +22,7 @@ def accumulate_session_usage(session: Session) -> AggregatedUsage:
22
22
  for item in session.conversation_history:
23
23
  if isinstance(item, model.TaskMetadataItem):
24
24
  task_count += 1
25
- all_metadata.append(item.main)
25
+ all_metadata.append(item.main_agent)
26
26
  all_metadata.extend(item.sub_agent_task_metadata)
27
27
 
28
28
  # Aggregate by model+provider
@@ -90,10 +90,10 @@ INVALID_TOOL_CALL_MAX_LENGTH = 500
90
90
  TRUNCATE_DISPLAY_MAX_LINE_LENGTH = 1000
91
91
 
92
92
  # Maximum lines for truncated display output
93
- TRUNCATE_DISPLAY_MAX_LINES = 20
93
+ TRUNCATE_DISPLAY_MAX_LINES = 8
94
94
 
95
95
  # Maximum lines for sub-agent result display
96
- SUB_AGENT_RESULT_MAX_LINES = 12
96
+ SUB_AGENT_RESULT_MAX_LINES = 50
97
97
 
98
98
 
99
99
  # UI refresh rate (frames per second) for debounced content streaming
@@ -102,9 +102,18 @@ UI_REFRESH_RATE_FPS = 20
102
102
  # Number of lines to keep visible at bottom of markdown streaming window
103
103
  MARKDOWN_STREAM_LIVE_WINDOW = 6
104
104
 
105
+ # Left margin (columns) to reserve when rendering markdown
106
+ MARKDOWN_LEFT_MARGIN = 2
107
+
108
+ # Right margin (columns) to reserve when rendering markdown
109
+ MARKDOWN_RIGHT_MARGIN = 2
110
+
105
111
  # Status hint text shown after spinner status
106
112
  STATUS_HINT_TEXT = " (esc to interrupt)"
107
113
 
114
+ # Default spinner status text when idle/thinking
115
+ STATUS_DEFAULT_TEXT = "Thinking …"
116
+
108
117
  # Status shimmer animation
109
118
  # Horizontal padding used when computing shimmer band position
110
119
  STATUS_SHIMMER_PADDING = 10
@@ -327,7 +327,7 @@ class ExecutorContext:
327
327
  log_debug(traceback.format_exc(), style="red", debug_type=DebugType.EXECUTION)
328
328
  await self.emit_event(
329
329
  events.ErrorEvent(
330
- error_message=f"Agent task failed: [{e.__class__.__name__}] {e!s}",
330
+ error_message=f"Agent task failed: [{e.__class__.__name__}] {e!s} {traceback.format_exc()}",
331
331
  can_retry=False,
332
332
  )
333
333
  )
@@ -89,7 +89,7 @@ Only the content passed to `report_back` will be returned to user.\
89
89
  result = event.task_result
90
90
  # Capture TaskMetadataEvent for metadata propagation
91
91
  elif isinstance(event, events.TaskMetadataEvent):
92
- task_metadata = event.metadata.main
92
+ task_metadata = event.metadata.main_agent
93
93
  await self.emit_event(event)
94
94
  return SubAgentResult(
95
95
  task_result=result,
@@ -12,6 +12,7 @@ from klaude_code.core.tool import BashTool, ReadTool, reset_tool_context, set_to
12
12
  from klaude_code.core.tool.file._utils import hash_text_sha256
13
13
  from klaude_code.protocol import model, tools
14
14
  from klaude_code.session import Session
15
+ from klaude_code.skill import get_skill
15
16
 
16
17
  type Reminder = Callable[[Session], Awaitable[model.DeveloperMessageItem | None]]
17
18
 
@@ -19,6 +20,9 @@ type Reminder = Callable[[Session], Awaitable[model.DeveloperMessageItem | None]
19
20
  # Match @ preceded by whitespace, start of line, or → (ReadTool line number arrow)
20
21
  AT_FILE_PATTERN = re.compile(r'(?:(?<!\S)|(?<=\u2192))@("(?P<quoted>[^\"]+)"|(?P<plain>\S+))')
21
22
 
23
+ # Match $skill or ¥skill at the beginning of the first line
24
+ SKILL_PATTERN = re.compile(r"^[$¥](?P<skill>\S+)")
25
+
22
26
 
23
27
  def get_last_new_user_input(session: Session) -> str | None:
24
28
  """Get last user input & developer message (CLAUDE.md) from conversation history. if there's a tool result after user input, return None"""
@@ -73,6 +77,21 @@ def get_at_patterns_with_source(session: Session) -> list[AtPatternSource]:
73
77
  return patterns
74
78
 
75
79
 
80
+ def get_skill_from_user_input(session: Session) -> str | None:
81
+ """Get $skill reference from the first line of last user input."""
82
+ for item in reversed(session.conversation_history):
83
+ if isinstance(item, model.ToolResultItem):
84
+ return None
85
+ if isinstance(item, model.UserMessageItem):
86
+ content = item.content or ""
87
+ first_line = content.split("\n", 1)[0]
88
+ m = SKILL_PATTERN.match(first_line)
89
+ if m:
90
+ return m.group("skill")
91
+ return None
92
+ return None
93
+
94
+
76
95
  async def _load_at_file_recursive(
77
96
  session: Session,
78
97
  pattern: str,
@@ -373,6 +392,36 @@ async def image_reminder(session: Session) -> model.DeveloperMessageItem | None:
373
392
  )
374
393
 
375
394
 
395
+ async def skill_reminder(session: Session) -> model.DeveloperMessageItem | None:
396
+ """Load skill content when user references a skill with $skill syntax."""
397
+ skill_name = get_skill_from_user_input(session)
398
+ if not skill_name:
399
+ return None
400
+
401
+ # Get the skill from skill module
402
+ skill = get_skill(skill_name)
403
+ if not skill:
404
+ return None
405
+
406
+ # Get base directory from skill_path
407
+ base_dir = str(skill.skill_path.parent) if skill.skill_path else "unknown"
408
+
409
+ content = f"""<system-reminder>The user activated the "{skill.name}" skill. Here is the skill content:
410
+
411
+ <skill>
412
+ <name>{skill.name}</name>
413
+ <base_dir>{base_dir}</base_dir>
414
+
415
+ {skill.to_prompt()}
416
+ </skill>
417
+ </system-reminder>"""
418
+
419
+ return model.DeveloperMessageItem(
420
+ content=content,
421
+ skill_name=skill.name,
422
+ )
423
+
424
+
376
425
  def _is_memory_loaded(session: Session, path: str) -> bool:
377
426
  """Check if a memory file has already been loaded (tracked with is_memory=True)."""
378
427
  status = session.file_tracker.get(path)
@@ -510,6 +559,7 @@ ALL_REMINDERS = [
510
559
  last_path_memory_reminder,
511
560
  at_file_reader_reminder,
512
561
  image_reminder,
562
+ skill_reminder,
513
563
  ]
514
564
 
515
565
 
@@ -540,6 +590,7 @@ def load_agent_reminders(
540
590
  last_path_memory_reminder,
541
591
  file_changed_externally_reminder,
542
592
  image_reminder,
593
+ skill_reminder,
543
594
  ]
544
595
  )
545
596
 
klaude_code/core/task.py CHANGED
@@ -25,7 +25,7 @@ class MetadataAccumulator:
25
25
  """
26
26
 
27
27
  def __init__(self, model_name: str) -> None:
28
- self._main = model.TaskMetadata(model_name=model_name)
28
+ self._main_agent = model.TaskMetadata(model_name=model_name) # Main agent metadata
29
29
  self._sub_agent_metadata: list[model.TaskMetadata] = []
30
30
  self._throughput_weighted_sum: float = 0.0
31
31
  self._throughput_tracked_tokens: int = 0
@@ -36,13 +36,12 @@ class MetadataAccumulator:
36
36
  def add(self, turn_metadata: model.ResponseMetadataItem) -> None:
37
37
  """Merge a turn's metadata into the accumulated state."""
38
38
  self._turn_count += 1
39
- main = self._main
40
39
  usage = turn_metadata.usage
41
40
 
42
41
  if usage is not None:
43
- if main.usage is None:
44
- main.usage = model.Usage()
45
- acc_usage = main.usage
42
+ if self._main_agent.usage is None:
43
+ self._main_agent.usage = model.Usage()
44
+ acc_usage = self._main_agent.usage
46
45
 
47
46
  model.TaskMetadata.merge_usage(acc_usage, usage)
48
47
  acc_usage.currency = usage.currency
@@ -63,9 +62,9 @@ class MetadataAccumulator:
63
62
  self._throughput_tracked_tokens += current_output
64
63
 
65
64
  if turn_metadata.provider is not None:
66
- main.provider = turn_metadata.provider
65
+ self._main_agent.provider = turn_metadata.provider
67
66
  if turn_metadata.model_name:
68
- main.model_name = turn_metadata.model_name
67
+ self._main_agent.model_name = turn_metadata.model_name
69
68
 
70
69
  def add_sub_agent_metadata(self, sub_agent_metadata: model.TaskMetadata) -> None:
71
70
  """Add sub-agent task metadata to the accumulated state."""
@@ -73,21 +72,22 @@ class MetadataAccumulator:
73
72
 
74
73
  def finalize(self, task_duration_s: float) -> model.TaskMetadataItem:
75
74
  """Return the final accumulated metadata with computed throughput and duration."""
76
- main = self._main
77
- if main.usage is not None:
75
+ if self._main_agent.usage is not None:
78
76
  if self._throughput_tracked_tokens > 0:
79
- main.usage.throughput_tps = self._throughput_weighted_sum / self._throughput_tracked_tokens
77
+ self._main_agent.usage.throughput_tps = self._throughput_weighted_sum / self._throughput_tracked_tokens
80
78
  else:
81
- main.usage.throughput_tps = None
79
+ self._main_agent.usage.throughput_tps = None
82
80
 
83
81
  if self._first_token_latency_count > 0:
84
- main.usage.first_token_latency_ms = self._first_token_latency_sum / self._first_token_latency_count
82
+ self._main_agent.usage.first_token_latency_ms = (
83
+ self._first_token_latency_sum / self._first_token_latency_count
84
+ )
85
85
  else:
86
- main.usage.first_token_latency_ms = None
86
+ self._main_agent.usage.first_token_latency_ms = None
87
87
 
88
- main.task_duration_s = task_duration_s
89
- main.turn_count = self._turn_count
90
- return model.TaskMetadataItem(main=main, sub_agent_task_metadata=self._sub_agent_metadata)
88
+ self._main_agent.task_duration_s = task_duration_s
89
+ self._main_agent.turn_count = self._turn_count
90
+ return model.TaskMetadataItem(main_agent=self._main_agent, sub_agent_task_metadata=self._sub_agent_metadata)
91
91
 
92
92
 
93
93
  @dataclass
@@ -126,17 +126,28 @@ class TaskExecutor:
126
126
  self._context = context
127
127
  self._current_turn: TurnExecutor | None = None
128
128
  self._started_at: float = 0.0
129
+ self._metadata_accumulator: MetadataAccumulator | None = None
129
130
 
130
131
  @property
131
132
  def current_turn(self) -> TurnExecutor | None:
132
133
  return self._current_turn
133
134
 
134
135
  def cancel(self) -> list[events.Event]:
135
- """Cancel the current turn and return any resulting events."""
136
+ """Cancel the current turn and return any resulting events including metadata."""
136
137
  ui_events: list[events.Event] = []
137
138
  if self._current_turn is not None:
138
139
  ui_events.extend(self._current_turn.cancel())
139
140
  self._current_turn = None
141
+
142
+ # Emit partial metadata on cancellation
143
+ if self._metadata_accumulator is not None and self._started_at > 0:
144
+ task_duration_s = time.perf_counter() - self._started_at
145
+ accumulated = self._metadata_accumulator.finalize(task_duration_s)
146
+ if accumulated.main_agent.usage is not None:
147
+ session_id = self._context.session_ctx.session_id
148
+ ui_events.append(events.TaskMetadataEvent(metadata=accumulated, session_id=session_id))
149
+ self._context.session_ctx.append_history([accumulated])
150
+
140
151
  return ui_events
141
152
 
142
153
  async def run(self, user_input: model.UserInputPayload) -> AsyncGenerator[events.Event]:
@@ -152,7 +163,8 @@ class TaskExecutor:
152
163
  del user_input # Persisted by the operation handler before launching the task.
153
164
 
154
165
  profile = ctx.profile
155
- metadata_accumulator = MetadataAccumulator(model_name=profile.llm_client.model_name)
166
+ self._metadata_accumulator = MetadataAccumulator(model_name=profile.llm_client.model_name)
167
+ metadata_accumulator = self._metadata_accumulator
156
168
 
157
169
  while True:
158
170
  # Process reminders at the start of each turn
@@ -226,6 +238,13 @@ class TaskExecutor:
226
238
  return
227
239
 
228
240
  if turn is None or turn.task_finished:
241
+ # Sub-agent with empty result should retry instead of finishing
242
+ if ctx.sub_agent_state is not None and turn is not None and not turn.task_result.strip():
243
+ yield events.ErrorEvent(
244
+ error_message="Sub-agent returned empty result, retrying...",
245
+ can_retry=True,
246
+ )
247
+ continue
229
248
  break
230
249
 
231
250
  # Finalize metadata
@@ -3,11 +3,10 @@ from .file.apply_patch_tool import ApplyPatchTool
3
3
  from .file.edit_tool import EditTool
4
4
  from .file.read_tool import ReadTool
5
5
  from .file.write_tool import WriteTool
6
- from .memory.skill_loader import Skill, SkillLoader
7
- from .memory.skill_tool import SkillTool
8
6
  from .report_back_tool import ReportBackTool
9
7
  from .shell.bash_tool import BashTool
10
8
  from .shell.command_safety import SafetyCheckResult, is_safe_command
9
+ from .skill.skill_tool import SkillTool
11
10
  from .sub_agent_tool import SubAgentTool
12
11
  from .todo.todo_write_tool import TodoWriteTool
13
12
  from .todo.update_plan_tool import UpdatePlanTool
@@ -40,8 +39,6 @@ __all__ = [
40
39
  "ReportBackTool",
41
40
  "SafetyCheckResult",
42
41
  "SimpleTruncationStrategy",
43
- "Skill",
44
- "SkillLoader",
45
42
  "SkillTool",
46
43
  "SubAgentTool",
47
44
  "TodoContext",
@@ -25,6 +25,18 @@ _IMAGE_MIME_TYPES: dict[str, str] = {
25
25
  ".webp": "image/webp",
26
26
  }
27
27
 
28
+ _BINARY_CHECK_SIZE = 8192
29
+
30
+
31
+ def _is_binary_file(file_path: str) -> bool:
32
+ """Check if a file is binary by looking for null bytes in the first chunk."""
33
+ try:
34
+ with open(file_path, "rb") as f:
35
+ chunk = f.read(_BINARY_CHECK_SIZE)
36
+ return b"\x00" in chunk
37
+ except OSError:
38
+ return False
39
+
28
40
 
29
41
  def _format_numbered_line(line_no: int, content: str) -> str:
30
42
  # 6-width right-aligned line number followed by a right arrow
@@ -218,12 +230,22 @@ class ReadTool(ToolABC):
218
230
  ),
219
231
  )
220
232
 
233
+ is_image_file = _is_supported_image_file(file_path)
234
+ # Check for binary files (skip for images which are handled separately)
235
+ if not is_image_file and _is_binary_file(file_path):
236
+ return model.ToolResultItem(
237
+ status="error",
238
+ output=(
239
+ "<tool_use_error>This appears to be a binary file and cannot be read as text. "
240
+ "Use appropriate tools or libraries to handle binary files.</tool_use_error>"
241
+ ),
242
+ )
243
+
221
244
  try:
222
245
  size_bytes = Path(file_path).stat().st_size
223
246
  except OSError:
224
247
  size_bytes = 0
225
248
 
226
- is_image_file = _is_supported_image_file(file_path)
227
249
  if is_image_file:
228
250
  if size_bytes > const.READ_MAX_IMAGE_BYTES:
229
251
  size_mb = size_bytes / (1024 * 1024)
@@ -124,9 +124,13 @@ class WriteTool(ToolABC):
124
124
  is_memory=is_mem,
125
125
  )
126
126
 
127
- # Build diff between previous and new content
128
- after = args.content
129
- ui_extra = build_structured_diff(before, after, file_path=file_path)
127
+ # For markdown files, use MarkdownDocUIExtra to render content as markdown
128
+ # Otherwise, build diff between previous and new content
129
+ ui_extra: model.ToolResultUIExtra | None
130
+ if file_path.endswith(".md"):
131
+ ui_extra = model.MarkdownDocUIExtra(file_path=file_path, content=args.content)
132
+ else:
133
+ ui_extra = build_structured_diff(before, args.content, file_path=file_path)
130
134
 
131
135
  message = f"File {'overwritten' if exists else 'created'} successfully at: {file_path}"
132
136
  return model.ToolResultItem(status="success", output=message, ui_extra=ui_extra)
File without changes
@@ -1,38 +1,22 @@
1
+ """SkillTool - Tool for agent to activate and load skills."""
2
+
1
3
  from pathlib import Path
2
4
 
3
5
  from pydantic import BaseModel
4
6
 
5
- from klaude_code.core.tool.memory.skill_loader import SkillLoader
6
7
  from klaude_code.core.tool.tool_abc import ToolABC, load_desc
7
8
  from klaude_code.core.tool.tool_registry import register
8
9
  from klaude_code.protocol import llm_param, model, tools
10
+ from klaude_code.skill import get_available_skills, get_skill, list_skill_names
9
11
 
10
12
 
11
13
  @register(tools.SKILL)
12
14
  class SkillTool(ToolABC):
13
- """Tool to execute/load a skill within the main conversation"""
14
-
15
- _skill_loader: SkillLoader | None = None
16
- _discovery_done: bool = False
17
-
18
- @classmethod
19
- def set_skill_loader(cls, loader: SkillLoader) -> None:
20
- """Set the skill loader instance"""
21
- cls._skill_loader = loader
22
- cls._discovery_done = False
23
-
24
- @classmethod
25
- def _ensure_skills_discovered(cls) -> None:
26
- if cls._discovery_done:
27
- return
28
- if cls._skill_loader is not None:
29
- cls._skill_loader.discover_skills()
30
- cls._discovery_done = True
15
+ """Tool to execute/load a skill within the main conversation."""
31
16
 
32
17
  @classmethod
33
18
  def schema(cls) -> llm_param.ToolSchema:
34
- """Generate schema with embedded available skills metadata"""
35
- cls._ensure_skills_discovered()
19
+ """Generate schema with embedded available skills metadata."""
36
20
  skills_xml = cls._generate_skills_xml()
37
21
 
38
22
  return llm_param.ToolSchema(
@@ -53,16 +37,17 @@ class SkillTool(ToolABC):
53
37
 
54
38
  @classmethod
55
39
  def _generate_skills_xml(cls) -> str:
56
- """Generate XML format skills metadata"""
57
- if not cls._skill_loader:
40
+ """Generate XML format skills metadata."""
41
+ skills = get_available_skills()
42
+ if not skills:
58
43
  return ""
59
44
 
60
45
  xml_parts: list[str] = []
61
- for skill in cls._skill_loader.loaded_skills.values():
46
+ for name, description, location in skills:
62
47
  xml_parts.append(f"""<skill>
63
- <name>{skill.name}</name>
64
- <description>{skill.description}</description>
65
- <location>{skill.location}</location>
48
+ <name>{name}</name>
49
+ <description>{description}</description>
50
+ <location>{location}</location>
66
51
  </skill>""")
67
52
  return "\n".join(xml_parts)
68
53
 
@@ -71,7 +56,7 @@ class SkillTool(ToolABC):
71
56
 
72
57
  @classmethod
73
58
  async def call(cls, arguments: str) -> model.ToolResultItem:
74
- """Load and return full skill content"""
59
+ """Load and return full skill content."""
75
60
  try:
76
61
  args = cls.SkillArguments.model_validate_json(arguments)
77
62
  except ValueError as e:
@@ -80,18 +65,10 @@ class SkillTool(ToolABC):
80
65
  output=f"Invalid arguments: {e}",
81
66
  )
82
67
 
83
- cls._ensure_skills_discovered()
84
-
85
- if not cls._skill_loader:
86
- return model.ToolResultItem(
87
- status="error",
88
- output="Skill loader not initialized",
89
- )
90
-
91
- skill = cls._skill_loader.get_skill(args.command)
68
+ skill = get_skill(args.command)
92
69
 
93
70
  if not skill:
94
- available = ", ".join(cls._skill_loader.list_skills())
71
+ available = ", ".join(list_skill_names())
95
72
  return model.ToolResultItem(
96
73
  status="error",
97
74
  output=f"Skill '{args.command}' does not exist. Available skills: {available}",
@@ -101,7 +78,7 @@ class SkillTool(ToolABC):
101
78
  base_dir = str(skill.skill_path.parent) if skill.skill_path else "unknown"
102
79
 
103
80
  # Return with loading message format
104
- result = f"""<command-message>The "{skill.name}" skill is running</command-message>
81
+ result = f"""<command-message>The "{skill.name}" skill is activated</command-message>
105
82
  <command-name>{skill.name}</command-name>
106
83
 
107
84
  Base directory for this skill: {base_dir}
@@ -1,6 +1,6 @@
1
1
  import json
2
2
  from collections.abc import AsyncGenerator
3
- from typing import override
3
+ from typing import Any, override
4
4
 
5
5
  import httpx
6
6
  import openai
@@ -9,9 +9,9 @@ from openai.types.chat.completion_create_params import CompletionCreateParamsStr
9
9
  from klaude_code.llm.client import LLMClientABC
10
10
  from klaude_code.llm.input_common import apply_config_defaults
11
11
  from klaude_code.llm.openai_compatible.input import convert_history_to_input, convert_tool_schema
12
- from klaude_code.llm.openai_compatible.stream_processor import StreamStateManager
12
+ from klaude_code.llm.openai_compatible.stream import DefaultReasoningHandler, parse_chat_completions_stream
13
13
  from klaude_code.llm.registry import register
14
- from klaude_code.llm.usage import MetadataTracker, convert_usage
14
+ from klaude_code.llm.usage import MetadataTracker
15
15
  from klaude_code.protocol import llm_param, model
16
16
  from klaude_code.trace import DebugType, log_debug
17
17
 
@@ -86,107 +86,34 @@ class OpenAICompatibleClient(LLMClientABC):
86
86
  debug_type=DebugType.LLM_PAYLOAD,
87
87
  )
88
88
 
89
- stream = self.client.chat.completions.create(
90
- **payload,
91
- extra_body=extra_body,
92
- extra_headers=extra_headers,
93
- )
94
-
95
- state = StreamStateManager(param_model=str(param.model))
96
-
97
89
  try:
98
- async for event in await stream:
99
- log_debug(
100
- event.model_dump_json(exclude_none=True),
101
- style="blue",
102
- debug_type=DebugType.LLM_STREAM,
103
- )
104
- if not state.response_id and event.id:
105
- state.set_response_id(event.id)
106
- yield model.StartItem(response_id=event.id)
107
- if event.usage is not None:
108
- metadata_tracker.set_usage(convert_usage(event.usage, param.context_limit, param.max_tokens))
109
- if event.model:
110
- metadata_tracker.set_model_name(event.model)
111
- if provider := getattr(event, "provider", None):
112
- metadata_tracker.set_provider(str(provider))
113
-
114
- if len(event.choices) == 0:
115
- continue
116
-
117
- # Support Moonshot Kimi K2's usage field in choice
118
- if usage := getattr(event.choices[0], "usage", None):
119
- metadata_tracker.set_usage(
120
- convert_usage(
121
- openai.types.CompletionUsage.model_validate(usage),
122
- param.context_limit,
123
- param.max_tokens,
124
- )
125
- )
126
-
127
- delta = event.choices[0].delta
128
-
129
- # Reasoning
130
- if (
131
- reasoning_content := getattr(delta, "reasoning_content", None)
132
- or getattr(delta, "reasoning", None)
133
- or ""
134
- ):
135
- metadata_tracker.record_token()
136
- state.stage = "reasoning"
137
- state.accumulated_reasoning.append(reasoning_content)
138
- yield model.ReasoningTextDelta(
139
- content=reasoning_content,
140
- response_id=state.response_id,
141
- )
142
-
143
- # Assistant
144
- if delta.content and (
145
- state.stage == "assistant" or delta.content.strip()
146
- ): # Process all content in assistant stage, filter empty content in reasoning stage
147
- metadata_tracker.record_token()
148
- if state.stage == "reasoning":
149
- for item in state.flush_reasoning():
150
- yield item
151
- elif state.stage == "tool":
152
- for item in state.flush_tool_calls():
153
- yield item
154
- state.stage = "assistant"
155
- state.accumulated_content.append(delta.content)
156
- yield model.AssistantMessageDelta(
157
- content=delta.content,
158
- response_id=state.response_id,
159
- )
160
-
161
- # Tool
162
- if delta.tool_calls and len(delta.tool_calls) > 0:
163
- metadata_tracker.record_token()
164
- if state.stage == "reasoning":
165
- for item in state.flush_reasoning():
166
- yield item
167
- elif state.stage == "assistant":
168
- for item in state.flush_assistant():
169
- yield item
170
- state.stage = "tool"
171
- # Emit ToolCallStartItem for new tool calls
172
- for tc in delta.tool_calls:
173
- if tc.index not in state.emitted_tool_start_indices and tc.function and tc.function.name:
174
- state.emitted_tool_start_indices.add(tc.index)
175
- yield model.ToolCallStartItem(
176
- response_id=state.response_id,
177
- call_id=tc.id or "",
178
- name=tc.function.name,
179
- )
180
- state.accumulated_tool_calls.add(delta.tool_calls)
90
+ stream = await self.client.chat.completions.create(
91
+ **payload,
92
+ extra_body=extra_body,
93
+ extra_headers=extra_headers,
94
+ )
181
95
  except (openai.OpenAIError, httpx.HTTPError) as e:
182
96
  yield model.StreamErrorItem(error=f"{e.__class__.__name__} {e!s}")
97
+ yield metadata_tracker.finalize()
98
+ return
183
99
 
184
- # Finalize
185
- flushed_items = state.flush_all()
186
- if flushed_items:
187
- metadata_tracker.record_token()
188
- for item in flushed_items:
189
- yield item
100
+ reasoning_handler = DefaultReasoningHandler(
101
+ param_model=str(param.model),
102
+ response_id=None,
103
+ )
104
+
105
+ def on_event(event: Any) -> None:
106
+ log_debug(
107
+ event.model_dump_json(exclude_none=True),
108
+ style="blue",
109
+ debug_type=DebugType.LLM_STREAM,
110
+ )
190
111
 
191
- metadata_tracker.set_response_id(state.response_id)
192
- yield metadata_tracker.finalize()
112
+ async for item in parse_chat_completions_stream(
113
+ stream,
114
+ param=param,
115
+ metadata_tracker=metadata_tracker,
116
+ reasoning_handler=reasoning_handler,
117
+ on_event=on_event,
118
+ ):
119
+ yield item