klaude-code 2.7.0__py3-none-any.whl → 2.8.1__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/auth/AGENTS.md +325 -0
- klaude_code/auth/__init__.py +17 -1
- klaude_code/auth/antigravity/__init__.py +20 -0
- klaude_code/auth/antigravity/exceptions.py +17 -0
- klaude_code/auth/antigravity/oauth.py +320 -0
- klaude_code/auth/antigravity/pkce.py +25 -0
- klaude_code/auth/antigravity/token_manager.py +45 -0
- klaude_code/auth/base.py +4 -0
- klaude_code/auth/claude/oauth.py +29 -9
- klaude_code/auth/codex/exceptions.py +4 -0
- klaude_code/cli/auth_cmd.py +53 -3
- klaude_code/cli/cost_cmd.py +83 -160
- klaude_code/cli/list_model.py +50 -0
- klaude_code/cli/main.py +2 -2
- klaude_code/config/assets/builtin_config.yaml +108 -0
- klaude_code/config/builtin_config.py +5 -11
- klaude_code/config/config.py +24 -10
- klaude_code/const.py +2 -1
- klaude_code/core/agent.py +5 -1
- klaude_code/core/agent_profile.py +29 -33
- klaude_code/core/compaction/AGENTS.md +112 -0
- klaude_code/core/compaction/__init__.py +11 -0
- klaude_code/core/compaction/compaction.py +705 -0
- klaude_code/core/compaction/overflow.py +30 -0
- klaude_code/core/compaction/prompts.py +97 -0
- klaude_code/core/executor.py +121 -2
- klaude_code/core/manager/llm_clients.py +5 -0
- klaude_code/core/manager/llm_clients_builder.py +14 -2
- klaude_code/core/prompts/prompt-antigravity.md +80 -0
- klaude_code/core/prompts/prompt-codex-gpt-5-2.md +335 -0
- klaude_code/core/reminders.py +7 -2
- klaude_code/core/task.py +126 -0
- klaude_code/core/tool/file/edit_tool.py +1 -2
- klaude_code/core/tool/todo/todo_write_tool.py +1 -1
- klaude_code/core/turn.py +3 -1
- klaude_code/llm/antigravity/__init__.py +3 -0
- klaude_code/llm/antigravity/client.py +558 -0
- klaude_code/llm/antigravity/input.py +261 -0
- klaude_code/llm/registry.py +1 -0
- klaude_code/protocol/commands.py +1 -0
- klaude_code/protocol/events.py +18 -0
- klaude_code/protocol/llm_param.py +1 -0
- klaude_code/protocol/message.py +23 -1
- klaude_code/protocol/op.py +29 -1
- klaude_code/protocol/op_handler.py +10 -0
- klaude_code/session/export.py +308 -299
- klaude_code/session/session.py +36 -0
- klaude_code/session/templates/export_session.html +430 -134
- klaude_code/skill/assets/create-plan/SKILL.md +6 -6
- klaude_code/tui/command/__init__.py +6 -0
- klaude_code/tui/command/compact_cmd.py +32 -0
- klaude_code/tui/command/continue_cmd.py +34 -0
- klaude_code/tui/command/fork_session_cmd.py +110 -14
- klaude_code/tui/command/model_picker.py +5 -1
- klaude_code/tui/command/thinking_cmd.py +1 -1
- klaude_code/tui/commands.py +6 -0
- klaude_code/tui/components/rich/markdown.py +119 -12
- klaude_code/tui/components/rich/theme.py +10 -2
- klaude_code/tui/components/tools.py +39 -25
- klaude_code/tui/components/user_input.py +1 -1
- klaude_code/tui/input/__init__.py +5 -2
- klaude_code/tui/input/drag_drop.py +6 -57
- klaude_code/tui/input/key_bindings.py +10 -0
- klaude_code/tui/input/prompt_toolkit.py +19 -6
- klaude_code/tui/machine.py +25 -0
- klaude_code/tui/renderer.py +68 -4
- klaude_code/tui/runner.py +18 -2
- klaude_code/tui/terminal/image.py +72 -10
- klaude_code/tui/terminal/selector.py +31 -7
- {klaude_code-2.7.0.dist-info → klaude_code-2.8.1.dist-info}/METADATA +1 -1
- {klaude_code-2.7.0.dist-info → klaude_code-2.8.1.dist-info}/RECORD +73 -56
- klaude_code/core/prompts/prompt-codex-gpt-5-1-codex-max.md +0 -117
- {klaude_code-2.7.0.dist-info → klaude_code-2.8.1.dist-info}/WHEEL +0 -0
- {klaude_code-2.7.0.dist-info → klaude_code-2.8.1.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Compaction Module
|
|
2
|
+
|
|
3
|
+
Context window compaction for long conversations.
|
|
4
|
+
|
|
5
|
+
## File Overview
|
|
6
|
+
|
|
7
|
+
- `compaction.py`: Core compaction logic, threshold checking, summary generation
|
|
8
|
+
- `overflow.py`: Context overflow error detection patterns
|
|
9
|
+
- `prompts.py`: LLM prompts for summarization
|
|
10
|
+
|
|
11
|
+
## Triggering Compaction
|
|
12
|
+
|
|
13
|
+
### Threshold-based (`should_compact_threshold`)
|
|
14
|
+
|
|
15
|
+
Compaction triggers when:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
current_tokens > context_limit - reserve_tokens
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Default configuration (adjusted based on model's context limit):
|
|
22
|
+
- `reserve_tokens`: 16384 (or 25% of context limit)
|
|
23
|
+
- `keep_recent_tokens`: 20000 (or 35% of context limit)
|
|
24
|
+
|
|
25
|
+
Token estimation sources:
|
|
26
|
+
1. Last successful `AssistantMessage.usage.context_size`
|
|
27
|
+
2. Fallback: estimate from message text length (~4 chars per token)
|
|
28
|
+
|
|
29
|
+
### Overflow-based (`is_context_overflow`)
|
|
30
|
+
|
|
31
|
+
Detects LLM API error messages indicating context overflow:
|
|
32
|
+
- "prompt is too long"
|
|
33
|
+
- "exceeds the context window"
|
|
34
|
+
- "context length exceeded"
|
|
35
|
+
- HTTP 400/413/429 with no body
|
|
36
|
+
|
|
37
|
+
## Trigger Locations
|
|
38
|
+
|
|
39
|
+
### 1. TUI Runner (`tui/runner.py`)
|
|
40
|
+
|
|
41
|
+
Before executing a `RunAgentOperation`, checks threshold and submits `CompactSessionOperation`:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
if run_ops and should_compact_threshold(...):
|
|
45
|
+
await executor.submit_and_wait(CompactSessionOperation(...))
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 2. Task Turn Start (`core/task.py`)
|
|
49
|
+
|
|
50
|
+
At the beginning of each turn in `TaskRunner._run_inner()`. Important for multi-turn tool loops where no new user input occurs:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
# Threshold-based compaction before starting a new turn
|
|
54
|
+
if ctx.sub_agent_state is None and should_compact_threshold(...):
|
|
55
|
+
result = await run_compaction(reason=CompactionReason.THRESHOLD, ...)
|
|
56
|
+
session_ctx.append_history([result.to_entry()])
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Note: Sub-agents skip threshold compaction (handled by parent).
|
|
60
|
+
|
|
61
|
+
### 3. Context Overflow Recovery (`core/task.py`)
|
|
62
|
+
|
|
63
|
+
When LLM API returns context overflow error, triggers compaction and retries:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
except TurnError as e:
|
|
67
|
+
if is_context_overflow(str(e)):
|
|
68
|
+
result = await run_compaction(reason=CompactionReason.OVERFLOW, ...)
|
|
69
|
+
session_ctx.append_history([result.to_entry()])
|
|
70
|
+
continue # retry turn
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Compaction Process
|
|
74
|
+
|
|
75
|
+
1. **Find cut index**: Walk backwards from history end, accumulating tokens until `keep_recent_tokens` is reached
|
|
76
|
+
2. **Adjust cut index**: Never cut on `ToolResultMessage` (breaks LLM history)
|
|
77
|
+
3. **Serialize old messages**: Convert to text format `[User]:`, `[Assistant]:`, `[Tool result]:`
|
|
78
|
+
4. **Generate summary**: Call LLM with summarization prompt
|
|
79
|
+
5. **Append file operations**: Collect read/modified files from `file_tracker` and tool calls
|
|
80
|
+
6. **Create `CompactionEntry`**: Store summary, `first_kept_index`, `tokens_before`
|
|
81
|
+
|
|
82
|
+
## Post-Compaction History (`Session.get_llm_history`)
|
|
83
|
+
|
|
84
|
+
The LLM-facing history view is constructed as:
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
[UserMessage(summary)] + [kept_messages from first_kept_index:]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Implementation in `session.py`:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
def get_llm_history(self) -> list[message.HistoryEvent]:
|
|
94
|
+
# Find last CompactionEntry
|
|
95
|
+
last_compaction = find_last_compaction(history)
|
|
96
|
+
|
|
97
|
+
if last_compaction is None:
|
|
98
|
+
return [it for it in history if not isinstance(it, CompactionEntry)]
|
|
99
|
+
|
|
100
|
+
# Inject summary as UserMessage
|
|
101
|
+
summary_message = UserMessage(parts=[TextPart(text=last_compaction.summary)])
|
|
102
|
+
|
|
103
|
+
# Keep messages after cut point
|
|
104
|
+
kept = history[last_compaction.first_kept_index:]
|
|
105
|
+
|
|
106
|
+
return [summary_message, *kept]
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The summary contains:
|
|
110
|
+
- Compaction prefix: "The conversation history before this point was compacted..."
|
|
111
|
+
- Structured summary: Goal, Progress (Done/In Progress/Blocked), Key Decisions, Next Steps
|
|
112
|
+
- File operations: `<read-files>` and `<modified-files>` sections
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .compaction import CompactionConfig, CompactionReason, CompactionResult, run_compaction, should_compact_threshold
|
|
2
|
+
from .overflow import is_context_overflow
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"CompactionConfig",
|
|
6
|
+
"CompactionReason",
|
|
7
|
+
"CompactionResult",
|
|
8
|
+
"is_context_overflow",
|
|
9
|
+
"run_compaction",
|
|
10
|
+
"should_compact_threshold",
|
|
11
|
+
]
|