|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | from collections.abc import Sequence |
4 | | -from string import Template |
5 | | -from typing import TYPE_CHECKING, Protocol, runtime_checkable |
| 4 | +from typing import TYPE_CHECKING, NamedTuple, Protocol, runtime_checkable |
6 | 5 |
|
7 | 6 | import kosong |
8 | | -from kosong.message import ContentPart, Message, ThinkPart |
| 7 | +from kosong.message import ContentPart, Message, TextPart, ThinkPart |
9 | 8 | from kosong.tooling.empty import EmptyToolset |
10 | 9 |
|
11 | 10 | import kimi_cli.prompts as prompts |
@@ -33,46 +32,21 @@ async def compact(self, messages: Sequence[Message], llm: LLM) -> Sequence[Messa |
33 | 32 | ... |
34 | 33 |
|
35 | 34 |
|
36 | | -class SimpleCompaction(Compaction): |
37 | | - MAX_PRESERVED_MESSAGES = 2 |
38 | | - |
39 | | - async def compact(self, messages: Sequence[Message], llm: LLM) -> Sequence[Message]: |
40 | | - history = list(messages) |
41 | | - if not history: |
42 | | - return history |
| 35 | +if TYPE_CHECKING: |
43 | 36 |
|
44 | | - preserve_start_index = len(history) |
45 | | - n_preserved = 0 |
46 | | - for index in range(len(history) - 1, -1, -1): |
47 | | - if history[index].role in {"user", "assistant"}: |
48 | | - n_preserved += 1 |
49 | | - if n_preserved == self.MAX_PRESERVED_MESSAGES: |
50 | | - preserve_start_index = index |
51 | | - break |
| 37 | + def type_check(simple: SimpleCompaction): |
| 38 | + _: Compaction = simple |
52 | 39 |
|
53 | | - if n_preserved < self.MAX_PRESERVED_MESSAGES: |
54 | | - return history |
55 | 40 |
|
56 | | - to_compact = history[:preserve_start_index] |
57 | | - to_preserve = history[preserve_start_index:] |
| 41 | +class SimpleCompaction: |
| 42 | + def __init__(self, max_preserved_messages: int = 2) -> None: |
| 43 | + self.max_preserved_messages = max_preserved_messages |
58 | 44 |
|
59 | | - if not to_compact: |
60 | | - # Let's hope this won't exceed the context size limit |
| 45 | + async def compact(self, messages: Sequence[Message], llm: LLM) -> Sequence[Message]: |
| 46 | + compact_message, to_preserve = self.prepare(messages) |
| 47 | + if compact_message is None: |
61 | 48 | return to_preserve |
62 | 49 |
|
63 | | - # Convert history to string for the compact prompt |
64 | | - history_text = "\n\n".join( |
65 | | - f"## Message {i + 1}\nRole: {msg.role}\nContent: {msg.content}" |
66 | | - for i, msg in enumerate(to_compact) |
67 | | - ) |
68 | | - |
69 | | - # Build the compact prompt using string template |
70 | | - compact_template = Template(prompts.COMPACT) |
71 | | - compact_prompt = compact_template.substitute(CONTEXT=history_text) |
72 | | - |
73 | | - # Create input message for compaction |
74 | | - compact_message = Message(role="user", content=compact_prompt) |
75 | | - |
76 | 50 | # Call kosong.step to get the compacted context |
77 | 51 | # TODO: set max completion tokens |
78 | 52 | logger.debug("Compacting context...") |
@@ -100,8 +74,42 @@ async def compact(self, messages: Sequence[Message], llm: LLM) -> Sequence[Messa |
100 | 74 | compacted_messages.extend(to_preserve) |
101 | 75 | return compacted_messages |
102 | 76 |
|
| 77 | + class PrepareResult(NamedTuple): |
| 78 | + compact_message: Message | None |
| 79 | + to_preserve: Sequence[Message] |
103 | 80 |
|
104 | | -if TYPE_CHECKING: |
| 81 | + def prepare(self, messages: Sequence[Message]) -> PrepareResult: |
| 82 | + if not messages or self.max_preserved_messages <= 0: |
| 83 | + return self.PrepareResult(compact_message=None, to_preserve=messages) |
105 | 84 |
|
106 | | - def type_check(simple: SimpleCompaction): |
107 | | - _: Compaction = simple |
| 85 | + history = list(messages) |
| 86 | + preserve_start_index = len(history) |
| 87 | + n_preserved = 0 |
| 88 | + for index in range(len(history) - 1, -1, -1): |
| 89 | + if history[index].role in {"user", "assistant"}: |
| 90 | + n_preserved += 1 |
| 91 | + if n_preserved == self.max_preserved_messages: |
| 92 | + preserve_start_index = index |
| 93 | + break |
| 94 | + |
| 95 | + if n_preserved < self.max_preserved_messages: |
| 96 | + return self.PrepareResult(compact_message=None, to_preserve=messages) |
| 97 | + |
| 98 | + to_compact = history[:preserve_start_index] |
| 99 | + to_preserve = history[preserve_start_index:] |
| 100 | + |
| 101 | + if not to_compact: |
| 102 | + # Let's hope this won't exceed the context size limit |
| 103 | + return self.PrepareResult(compact_message=None, to_preserve=to_preserve) |
| 104 | + |
| 105 | + # Create input message for compaction |
| 106 | + compact_message = Message(role="user", content=[]) |
| 107 | + for i, msg in enumerate(to_compact): |
| 108 | + compact_message.content.append( |
| 109 | + TextPart(text=f"## Message {i + 1}\nRole: {msg.role}\nContent:\n") |
| 110 | + ) |
| 111 | + compact_message.content.extend( |
| 112 | + part for part in msg.content if not isinstance(part, ThinkPart) |
| 113 | + ) |
| 114 | + compact_message.content.append(TextPart(text="\n" + prompts.COMPACT)) |
| 115 | + return self.PrepareResult(compact_message=compact_message, to_preserve=to_preserve) |
0 commit comments