|
14 | 14 | # os.environ["ANTHROPIC_API_KEY"] = "" |
15 | 15 |
|
16 | 16 | N_MESSAGES_MEMORIZE = 2 |
| 17 | +RUNNING_MEMORIZATION: asyncio.Task | None = None |
17 | 18 |
|
18 | 19 |
|
19 | 20 | async def trigger_memorize(messages: list[dict[str, any]]) -> bool: |
20 | | - """Background task to memorize conversation messages.""" |
| 21 | + """Create a background task to memorize conversation messages. |
| 22 | +
|
| 23 | + Returns True if the task was successfully created and registered. |
| 24 | + """ |
| 25 | + global RUNNING_MEMORIZATION |
21 | 26 | try: |
22 | | - await memorize(messages) |
| 27 | + memorize_awaitable = memorize(messages) |
| 28 | + RUNNING_MEMORIZATION = asyncio.create_task(memorize_awaitable) |
23 | 29 | except Exception as e: |
24 | | - print(f"\n[Background] Memorization failed: {e!r}") |
| 30 | + print(f"\n[Memory] Memorization initialization failed: {e!r}") |
25 | 31 | return False |
26 | 32 | else: |
27 | | - print("\n[Background] Memorization submitted.") |
| 33 | + print("\n[Memory] Memorization task submitted.") |
28 | 34 | return True |
29 | 35 |
|
30 | 36 |
|
@@ -85,12 +91,31 @@ async def process_response(client: ClaudeSDKClient) -> list[str]: |
85 | 91 |
|
86 | 92 |
|
87 | 93 | async def check_and_memorize(conversation_messages: list[dict[str, any]]) -> None: |
88 | | - """Check if memorization threshold is reached and trigger if needed.""" |
89 | | - if len(conversation_messages) >= N_MESSAGES_MEMORIZE: |
90 | | - print(f"\n[Info] Reached {N_MESSAGES_MEMORIZE} messages, triggering memorization...") |
91 | | - success = await trigger_memorize(conversation_messages.copy()) |
92 | | - if success: |
93 | | - conversation_messages.clear() |
| 94 | + """Check if memorization threshold is reached and trigger if needed. |
| 95 | +
|
| 96 | + Skips triggering if a previous memorization task is still running. |
| 97 | + """ |
| 98 | + global RUNNING_MEMORIZATION |
| 99 | + |
| 100 | + if len(conversation_messages) < N_MESSAGES_MEMORIZE: |
| 101 | + return |
| 102 | + |
| 103 | + # Check if there's a running memorization task |
| 104 | + if RUNNING_MEMORIZATION is not None: |
| 105 | + if not RUNNING_MEMORIZATION.done(): |
| 106 | + print("\n[Info] Have running memorization, skipping...") |
| 107 | + return |
| 108 | + # Previous task completed, check for exceptions |
| 109 | + try: |
| 110 | + RUNNING_MEMORIZATION.result() |
| 111 | + except Exception as e: |
| 112 | + print(f"\n[Memory] Memorization failed: {e!r}") |
| 113 | + RUNNING_MEMORIZATION = None |
| 114 | + |
| 115 | + print(f"\n[Info] Reached {N_MESSAGES_MEMORIZE} messages, triggering memorization...") |
| 116 | + success = await trigger_memorize(conversation_messages.copy()) |
| 117 | + if success: |
| 118 | + conversation_messages.clear() |
94 | 119 |
|
95 | 120 |
|
96 | 121 | async def run_conversation_loop(client: ClaudeSDKClient) -> list[dict[str, any]]: |
@@ -139,9 +164,28 @@ async def main(): |
139 | 164 | async with ClaudeSDKClient(options=options) as client: |
140 | 165 | remaining_messages = await run_conversation_loop(client) |
141 | 166 |
|
| 167 | + # Wait for any running memorization task to complete |
| 168 | + global RUNNING_MEMORIZATION |
| 169 | + if RUNNING_MEMORIZATION is not None and not RUNNING_MEMORIZATION.done(): |
| 170 | + print("\n[Info] Waiting for running memorization task to complete...") |
| 171 | + try: |
| 172 | + await RUNNING_MEMORIZATION |
| 173 | + print("\n[Memory] Running memorization completed successfully.") |
| 174 | + except Exception as e: |
| 175 | + print(f"\n[Memory] Running memorization failed: {e!r}") |
| 176 | + RUNNING_MEMORIZATION = None |
| 177 | + |
| 178 | + # Memorize remaining messages and wait for completion |
142 | 179 | if remaining_messages: |
143 | 180 | print("\n[Info] Session ended, memorizing remaining messages...") |
144 | | - await trigger_memorize(remaining_messages.copy()) |
| 181 | + success = await trigger_memorize(remaining_messages.copy()) |
| 182 | + if success and RUNNING_MEMORIZATION is not None: |
| 183 | + print("\n[Info] Waiting for final memorization to complete...") |
| 184 | + try: |
| 185 | + await RUNNING_MEMORIZATION |
| 186 | + print("\n[Memory] Final memorization completed successfully.") |
| 187 | + except Exception as e: |
| 188 | + print(f"\n[Memory] Final memorization failed: {e!r}") |
145 | 189 |
|
146 | 190 | print("\nDone") |
147 | 191 |
|
|
0 commit comments