txcode-sdk 0.4.1__py3-none-any.whl → 0.6.0__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.
- txcode_sdk/agent/base_agent.py +478 -478
- txcode_sdk/agent/plan/plan_agent.py +52 -30
- txcode_sdk/client.py +359 -352
- txcode_sdk/provider/openai_provider.py +225 -225
- txcode_sdk/server/config_store.py +39 -0
- txcode_sdk/server/handler.py +593 -562
- txcode_sdk/server/http.py +469 -4
- txcode_sdk/server/hub_client.py +139 -0
- txcode_sdk/server/plan_code.py +234 -0
- txcode_sdk/server/server.py +19 -0
- txcode_sdk/server/skill_manager.py +148 -0
- txcode_sdk/server/spec_manager.py +121 -0
- txcode_sdk/server/zip_utils.py +42 -0
- txcode_sdk/session/manager.py +50 -1
- {txcode_sdk-0.4.1.dist-info → txcode_sdk-0.6.0.dist-info}/METADATA +2 -7
- {txcode_sdk-0.4.1.dist-info → txcode_sdk-0.6.0.dist-info}/RECORD +20 -14
- {txcode_sdk-0.4.1.dist-info → txcode_sdk-0.6.0.dist-info}/WHEEL +0 -0
- {txcode_sdk-0.4.1.dist-info → txcode_sdk-0.6.0.dist-info}/entry_points.txt +0 -0
- {txcode_sdk-0.4.1.dist-info → txcode_sdk-0.6.0.dist-info}/licenses/LICENSE +0 -0
- {txcode_sdk-0.4.1.dist-info → txcode_sdk-0.6.0.dist-info}/top_level.txt +0 -0
txcode_sdk/agent/base_agent.py
CHANGED
|
@@ -1,478 +1,478 @@
|
|
|
1
|
-
"""BaseAgent 抽象基类(ReAct 主循环),Python 3.6 兼容。"""
|
|
2
|
-
|
|
3
|
-
import json
|
|
4
|
-
import platform as _platform
|
|
5
|
-
from abc import ABC, abstractmethod
|
|
6
|
-
from pathlib import Path
|
|
7
|
-
from typing import Any, Callable, List, Optional
|
|
8
|
-
|
|
9
|
-
from ..types import (
|
|
10
|
-
ChatMessage,
|
|
11
|
-
ChatResult,
|
|
12
|
-
ImageContent,
|
|
13
|
-
MediaFile,
|
|
14
|
-
PrintMsgFormat,
|
|
15
|
-
ProviderResponse,
|
|
16
|
-
StepInfo,
|
|
17
|
-
TextContent,
|
|
18
|
-
TokenUsage,
|
|
19
|
-
ToolCallInfo,
|
|
20
|
-
ToolResultInfo,
|
|
21
|
-
)
|
|
22
|
-
from ..provider.base import BaseProvider
|
|
23
|
-
from ..skills import build_available_skills_prompt
|
|
24
|
-
from ..specs import SpecInjector
|
|
25
|
-
from .._compat.resources import get_resource
|
|
26
|
-
from .tool_registry import ToolRegistry
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def _format_tool_args(tool_name: str, args: dict) -> str:
|
|
30
|
-
key_map = {
|
|
31
|
-
"read_file": ["file_path"],
|
|
32
|
-
"write_file": ["file_path"],
|
|
33
|
-
"edit_file": ["file_path"],
|
|
34
|
-
"glob": ["pattern"],
|
|
35
|
-
"grep": ["pattern"],
|
|
36
|
-
"bash": ["command"],
|
|
37
|
-
}
|
|
38
|
-
keys = key_map.get(tool_name)
|
|
39
|
-
if keys:
|
|
40
|
-
parts = []
|
|
41
|
-
for k in keys:
|
|
42
|
-
v = args.get(k)
|
|
43
|
-
if v:
|
|
44
|
-
s = str(v)
|
|
45
|
-
if len(s) > 80:
|
|
46
|
-
s = s[:77] + "..."
|
|
47
|
-
parts.append(s)
|
|
48
|
-
result = " ".join(parts)
|
|
49
|
-
if len(result) > 80:
|
|
50
|
-
result = result[:77] + "..."
|
|
51
|
-
return result
|
|
52
|
-
for v in args.values():
|
|
53
|
-
if v:
|
|
54
|
-
s = str(v)
|
|
55
|
-
if len(s) > 80:
|
|
56
|
-
s = s[:77] + "..."
|
|
57
|
-
return s
|
|
58
|
-
return ""
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
def _build_step_print_msg(step: StepInfo) -> Optional[str]:
|
|
62
|
-
if not step.tool_calls:
|
|
63
|
-
return None
|
|
64
|
-
lines: List[str] = []
|
|
65
|
-
if step.reasoning:
|
|
66
|
-
lines.append(step.reasoning)
|
|
67
|
-
for i, tc in enumerate(step.tool_calls):
|
|
68
|
-
result = step.results[i] if i < len(step.results) else None
|
|
69
|
-
mark = PrintMsgFormat.UNKNOWN_MARK
|
|
70
|
-
if result:
|
|
71
|
-
mark = PrintMsgFormat.SUCCESS_MARK if result.success else PrintMsgFormat.FAILURE_MARK
|
|
72
|
-
args_summary = _format_tool_args(tc.name, tc.arguments)
|
|
73
|
-
lines.append(f"{{tool:'{mark} {tc.name} {args_summary}'}}")
|
|
74
|
-
return "\n".join(lines) if lines else None
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
PROMPT_TEMPLATE = """
|
|
78
|
-
## 可用 Skills
|
|
79
|
-
|
|
80
|
-
{skills}
|
|
81
|
-
|
|
82
|
-
**注意**
|
|
83
|
-
读取skill 可以通过read_file工具读取内容
|
|
84
|
-
|
|
85
|
-
## 重要规则
|
|
86
|
-
|
|
87
|
-
- 如果工具执行失败,思考原因并尝试其他方法
|
|
88
|
-
- 最大迭代次数: {maxIterations}
|
|
89
|
-
|
|
90
|
-
## 运行环境
|
|
91
|
-
- 操作系统: {platform}
|
|
92
|
-
- 工作目录: {workdir}
|
|
93
|
-
"""
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
class BaseAgent(ABC):
|
|
97
|
-
def __init__(
|
|
98
|
-
self,
|
|
99
|
-
provider: BaseProvider,
|
|
100
|
-
tool_registry: ToolRegistry,
|
|
101
|
-
session_manager: Any = None,
|
|
102
|
-
max_iterations: int = 50,
|
|
103
|
-
work_dir: str = "",
|
|
104
|
-
system_prompt: Optional[str] = None,
|
|
105
|
-
log_enabled: bool = False,
|
|
106
|
-
):
|
|
107
|
-
self._provider = provider
|
|
108
|
-
self._registry = tool_registry
|
|
109
|
-
self._session_manager = session_manager
|
|
110
|
-
self._max_iterations = max_iterations
|
|
111
|
-
self._work_dir = work_dir or "."
|
|
112
|
-
self._system_prompt = system_prompt
|
|
113
|
-
self._log_enabled = log_enabled
|
|
114
|
-
self._round_count = 0
|
|
115
|
-
self._session_id: Optional[str] = None
|
|
116
|
-
|
|
117
|
-
@abstractmethod
|
|
118
|
-
def agent_type(self) -> str:
|
|
119
|
-
...
|
|
120
|
-
|
|
121
|
-
def _get_platform_name(self) -> str:
|
|
122
|
-
system = _platform.system()
|
|
123
|
-
if system == "Windows":
|
|
124
|
-
return "Windows"
|
|
125
|
-
elif system == "Darwin":
|
|
126
|
-
return "macOS"
|
|
127
|
-
else:
|
|
128
|
-
return "Linux"
|
|
129
|
-
|
|
130
|
-
def _load_role_template(self, package: str, filename: str = "role.txt") -> str:
|
|
131
|
-
raw = get_resource(package, filename)
|
|
132
|
-
return raw.replace("{platform}", self._get_platform_name()).replace("{workdir}", self._work_dir)
|
|
133
|
-
|
|
134
|
-
def _load_project_context(self) -> str:
|
|
135
|
-
project_md = Path(self._work_dir) / ".txcode" / "project" / "PROJECT.md"
|
|
136
|
-
if project_md.exists():
|
|
137
|
-
content = project_md.read_text("utf-8", errors="replace")
|
|
138
|
-
return f"\n<project-context>\n{content}\n</project-context>\n"
|
|
139
|
-
return ""
|
|
140
|
-
|
|
141
|
-
def _load_memory_context(self) -> str:
|
|
142
|
-
memory_dir = Path(self._work_dir) / ".txcode" / "memory"
|
|
143
|
-
if not memory_dir.exists():
|
|
144
|
-
return ""
|
|
145
|
-
parts: List[str] = []
|
|
146
|
-
for f in sorted(memory_dir.iterdir()):
|
|
147
|
-
if f.suffix in (".md", ".txt"):
|
|
148
|
-
parts.append(f.read_text("utf-8", errors="replace"))
|
|
149
|
-
if parts:
|
|
150
|
-
return f"\n<memory-context>\n" + "\n---\n".join(parts) + "\n</memory-context>\n"
|
|
151
|
-
return ""
|
|
152
|
-
|
|
153
|
-
@abstractmethod
|
|
154
|
-
def build_system_prompt(self, tools: List[dict]) -> str:
|
|
155
|
-
...
|
|
156
|
-
|
|
157
|
-
def _assemble_system_prompt(
|
|
158
|
-
self,
|
|
159
|
-
role: str,
|
|
160
|
-
tools: List[dict],
|
|
161
|
-
include_project: bool = True,
|
|
162
|
-
include_memory: bool = True,
|
|
163
|
-
) -> str:
|
|
164
|
-
if self._system_prompt:
|
|
165
|
-
return self._system_prompt
|
|
166
|
-
|
|
167
|
-
project_ctx = self._load_project_context() if include_project else ""
|
|
168
|
-
memory_ctx = self._load_memory_context() if include_memory else ""
|
|
169
|
-
skills_prompt = build_available_skills_prompt(self._work_dir)
|
|
170
|
-
|
|
171
|
-
prompt = PROMPT_TEMPLATE.format(
|
|
172
|
-
maxIterations=str(self._max_iterations),
|
|
173
|
-
skills=skills_prompt,
|
|
174
|
-
platform=self._get_platform_name(),
|
|
175
|
-
workdir=self._work_dir,
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
return role + project_ctx + memory_ctx + prompt
|
|
179
|
-
|
|
180
|
-
async def run(
|
|
181
|
-
self,
|
|
182
|
-
user_message: str,
|
|
183
|
-
session_id: Optional[str] = None,
|
|
184
|
-
history: Optional[List[dict]] = None,
|
|
185
|
-
media_files: Optional[List[MediaFile]] = None,
|
|
186
|
-
on_step: Optional[Callable[[StepInfo], None]] = None,
|
|
187
|
-
on_compact: Optional[Callable[[dict], None]] = None,
|
|
188
|
-
abort_signal: Any = None,
|
|
189
|
-
) -> ChatResult:
|
|
190
|
-
self._session_id = session_id
|
|
191
|
-
self._round_count = 0
|
|
192
|
-
tools = await self._registry.get_definitions()
|
|
193
|
-
system_prompt = self.build_system_prompt(tools)
|
|
194
|
-
self._base_messages: List[ChatMessage] = []
|
|
195
|
-
self._original_user_index: Optional[int] = None
|
|
196
|
-
self._injected_user_message: str = user_message
|
|
197
|
-
|
|
198
|
-
message_count = len(history) if history else 0
|
|
199
|
-
|
|
200
|
-
spec_injector = SpecInjector()
|
|
201
|
-
spec_injector.set_project_path(self._work_dir)
|
|
202
|
-
|
|
203
|
-
if spec_injector.should_inject(message_count):
|
|
204
|
-
injected = spec_injector.inject_into_message(user_message, self._work_dir)
|
|
205
|
-
self._injected_user_message = injected
|
|
206
|
-
else:
|
|
207
|
-
self._injected_user_message = user_message
|
|
208
|
-
|
|
209
|
-
if history:
|
|
210
|
-
for msg in history:
|
|
211
|
-
if msg.get("role") != "system":
|
|
212
|
-
self._base_messages.append(ChatMessage(
|
|
213
|
-
role=msg["role"],
|
|
214
|
-
content=msg.get("content", ""),
|
|
215
|
-
tool_call_id=msg.get("tool_call_id"),
|
|
216
|
-
tool_calls=msg.get("tool_calls"),
|
|
217
|
-
reasoning_content=msg.get("reasoning_content"),
|
|
218
|
-
))
|
|
219
|
-
|
|
220
|
-
user_msg = self._build_user_message(self._injected_user_message, media_files)
|
|
221
|
-
self._base_messages.append(user_msg)
|
|
222
|
-
self._original_user_index = len(self._base_messages) - 1
|
|
223
|
-
self._save_message(session_id, "user", user_message, keep_context=True, is_original=True, media_files=media_files)
|
|
224
|
-
|
|
225
|
-
steps: List[StepInfo] = []
|
|
226
|
-
iteration = 0
|
|
227
|
-
final_answer = ""
|
|
228
|
-
total_usage = TokenUsage()
|
|
229
|
-
|
|
230
|
-
while iteration < self._max_iterations:
|
|
231
|
-
if abort_signal and getattr(abort_signal, "aborted", False):
|
|
232
|
-
return ChatResult(
|
|
233
|
-
answer="", steps=steps, session_id=session_id or "",
|
|
234
|
-
iterations=iteration, success=False, error="Aborted",
|
|
235
|
-
)
|
|
236
|
-
|
|
237
|
-
iteration += 1
|
|
238
|
-
self._round_count += 1
|
|
239
|
-
if self._round_count >= 10:
|
|
240
|
-
self._check_hooks("round")
|
|
241
|
-
self._round_count = 0
|
|
242
|
-
|
|
243
|
-
messages: List[ChatMessage] = [
|
|
244
|
-
ChatMessage(role="system", content=system_prompt),
|
|
245
|
-
*self._base_messages,
|
|
246
|
-
]
|
|
247
|
-
|
|
248
|
-
response = await self._provider.chat(
|
|
249
|
-
messages, tools=tools, abort_signal=abort_signal,
|
|
250
|
-
log_context={
|
|
251
|
-
"session_id": session_id,
|
|
252
|
-
"agent_type": self.agent_type(),
|
|
253
|
-
"iteration": iteration,
|
|
254
|
-
"work_dir": self._work_dir,
|
|
255
|
-
} if self._log_enabled else None,
|
|
256
|
-
)
|
|
257
|
-
|
|
258
|
-
if response.usage:
|
|
259
|
-
total_usage.prompt_tokens
|
|
260
|
-
total_usage.completion_tokens
|
|
261
|
-
total_usage.total_tokens
|
|
262
|
-
|
|
263
|
-
if response.finish_reason == "stop" and response.content:
|
|
264
|
-
final_answer = response.content
|
|
265
|
-
self._save_message(session_id, "assistant", final_answer, keep_context=True)
|
|
266
|
-
break
|
|
267
|
-
|
|
268
|
-
if response.finish_reason == "tool_calls" and response.tool_calls:
|
|
269
|
-
tool_calls = ToolRegistry.parse_tool_calls(response.tool_calls)
|
|
270
|
-
results: List[ToolResultInfo] = []
|
|
271
|
-
|
|
272
|
-
for tc in tool_calls:
|
|
273
|
-
result = await self._registry.execute(
|
|
274
|
-
tc.name, tc.arguments,
|
|
275
|
-
self._build_tool_context(session_id or ""),
|
|
276
|
-
)
|
|
277
|
-
results.append(ToolResultInfo(
|
|
278
|
-
name=tc.name,
|
|
279
|
-
success=result.success,
|
|
280
|
-
output=result.output,
|
|
281
|
-
error=result.error,
|
|
282
|
-
))
|
|
283
|
-
|
|
284
|
-
step = StepInfo(
|
|
285
|
-
reasoning=response.reasoning,
|
|
286
|
-
tool_calls=tool_calls,
|
|
287
|
-
results=results,
|
|
288
|
-
iteration=iteration,
|
|
289
|
-
usage=response.usage,
|
|
290
|
-
)
|
|
291
|
-
step.printMsg = _build_step_print_msg(step)
|
|
292
|
-
steps.append(step)
|
|
293
|
-
|
|
294
|
-
if on_step:
|
|
295
|
-
on_step(step)
|
|
296
|
-
|
|
297
|
-
for i, tc in enumerate(tool_calls):
|
|
298
|
-
res = results[i]
|
|
299
|
-
# assistant message with tool_calls
|
|
300
|
-
assistant_msg = ChatMessage(
|
|
301
|
-
role="assistant",
|
|
302
|
-
content="",
|
|
303
|
-
tool_calls=[{
|
|
304
|
-
"id": tc.id,
|
|
305
|
-
"type": "function",
|
|
306
|
-
"function": {"name": tc.name, "arguments": json.dumps(tc.arguments)},
|
|
307
|
-
}],
|
|
308
|
-
reasoning_content=response.reasoning,
|
|
309
|
-
)
|
|
310
|
-
self._base_messages.append(assistant_msg)
|
|
311
|
-
self._save_message(session_id, "assistant", json.dumps({
|
|
312
|
-
"type": "assistant_with_tools",
|
|
313
|
-
"toolCalls": assistant_msg.tool_calls,
|
|
314
|
-
"thought": response.reasoning or "",
|
|
315
|
-
"reasoning_content": response.reasoning or "",
|
|
316
|
-
}), keep_context=True)
|
|
317
|
-
|
|
318
|
-
# tool message
|
|
319
|
-
tool_msg = ChatMessage(
|
|
320
|
-
role="tool",
|
|
321
|
-
content=res.output if res.success else f"Error: {res.error}",
|
|
322
|
-
tool_call_id=tc.id,
|
|
323
|
-
)
|
|
324
|
-
self._base_messages.append(tool_msg)
|
|
325
|
-
self._save_message(session_id, "tool", tool_msg.content, keep_context=False, tool_call_id=tc.id)
|
|
326
|
-
|
|
327
|
-
await self._check_and_compact(session_id, on_compact)
|
|
328
|
-
continue
|
|
329
|
-
|
|
330
|
-
if response.content:
|
|
331
|
-
final_answer = response.content
|
|
332
|
-
break
|
|
333
|
-
|
|
334
|
-
break
|
|
335
|
-
|
|
336
|
-
self._check_hooks("end")
|
|
337
|
-
|
|
338
|
-
success = final_answer != ""
|
|
339
|
-
return ChatResult(
|
|
340
|
-
answer=final_answer or "Unable to complete task",
|
|
341
|
-
steps=steps,
|
|
342
|
-
session_id=session_id or "",
|
|
343
|
-
iterations=iteration,
|
|
344
|
-
success=success,
|
|
345
|
-
error="Max iterations reached" if iteration >= self._max_iterations and not final_answer else None,
|
|
346
|
-
usage=total_usage,
|
|
347
|
-
)
|
|
348
|
-
|
|
349
|
-
def _check_hooks(self, trigger: str):
|
|
350
|
-
try:
|
|
351
|
-
from ..hooks import create_hooks
|
|
352
|
-
if not hasattr(self, "_hooks"):
|
|
353
|
-
self._hooks = create_hooks(
|
|
354
|
-
self._provider, self._session_manager, self._work_dir
|
|
355
|
-
)
|
|
356
|
-
messages: List[dict] = []
|
|
357
|
-
if self._session_id and self._session_manager:
|
|
358
|
-
session = self._session_manager.get(self._session_id)
|
|
359
|
-
if session:
|
|
360
|
-
messages = session.get("messages", [])
|
|
361
|
-
self._hooks.queue.emit(
|
|
362
|
-
trigger,
|
|
363
|
-
{
|
|
364
|
-
"messages": messages,
|
|
365
|
-
"trigger": trigger,
|
|
366
|
-
"metadata": {
|
|
367
|
-
"session_id": self._session_id,
|
|
368
|
-
"project_path": self._work_dir,
|
|
369
|
-
"round_count": self._round_count,
|
|
370
|
-
},
|
|
371
|
-
},
|
|
372
|
-
)
|
|
373
|
-
except Exception:
|
|
374
|
-
pass
|
|
375
|
-
|
|
376
|
-
def _build_user_message(self, text: str, media_files: Optional[List[MediaFile]]) -> ChatMessage:
|
|
377
|
-
if media_files:
|
|
378
|
-
content: list = [TextContent(text=text)]
|
|
379
|
-
for mf in media_files:
|
|
380
|
-
if mf.data_url:
|
|
381
|
-
url = mf.data_url
|
|
382
|
-
else:
|
|
383
|
-
import base64
|
|
384
|
-
import mimetypes
|
|
385
|
-
from pathlib import Path
|
|
386
|
-
path = Path(mf.file_path)
|
|
387
|
-
mime = mf.type or mimetypes.guess_type(str(path))[0] or "image/png"
|
|
388
|
-
with open(path, "rb") as f:
|
|
389
|
-
b64 = base64.b64encode(f.read()).decode("utf-8")
|
|
390
|
-
url = f"data:{mime};base64,{b64}"
|
|
391
|
-
content.append(ImageContent(url=url))
|
|
392
|
-
return ChatMessage(role="user", content=content)
|
|
393
|
-
return ChatMessage(role="user", content=text)
|
|
394
|
-
|
|
395
|
-
def _build_tool_context(self, session_id: str):
|
|
396
|
-
from ..types import ToolContext
|
|
397
|
-
return ToolContext(session_id=session_id, work_dir=self._work_dir)
|
|
398
|
-
|
|
399
|
-
def _save_message(
|
|
400
|
-
self,
|
|
401
|
-
session_id: Optional[str],
|
|
402
|
-
role: str,
|
|
403
|
-
content: str,
|
|
404
|
-
keep_context: bool = True,
|
|
405
|
-
is_original: bool = False,
|
|
406
|
-
tool_call_id: Optional[str] = None,
|
|
407
|
-
media_files: Optional[List[MediaFile]] = None,
|
|
408
|
-
):
|
|
409
|
-
if not session_id or not self._session_manager:
|
|
410
|
-
return
|
|
411
|
-
mf_list = None
|
|
412
|
-
if media_files:
|
|
413
|
-
mf_list = [{"file_path": mf.file_path, "type": mf.type} for mf in media_files]
|
|
414
|
-
self._session_manager.add_message(
|
|
415
|
-
session_id, role, content,
|
|
416
|
-
keep_context=keep_context, is_original=is_original,
|
|
417
|
-
tool_call_id=tool_call_id, media_files=mf_list,
|
|
418
|
-
)
|
|
419
|
-
|
|
420
|
-
async def _check_and_compact(
|
|
421
|
-
self,
|
|
422
|
-
session_id: Optional[str],
|
|
423
|
-
on_compact: Optional[Callable[[dict], None]],
|
|
424
|
-
):
|
|
425
|
-
if not session_id or not self._session_manager:
|
|
426
|
-
return
|
|
427
|
-
try:
|
|
428
|
-
from ..compact.compressor import ContextCompressor
|
|
429
|
-
compressor = getattr(self, "_compressor", None)
|
|
430
|
-
if compressor is None:
|
|
431
|
-
compressor = ContextCompressor(
|
|
432
|
-
max_tokens=150_000,
|
|
433
|
-
provider=self._provider,
|
|
434
|
-
session_manager=self._session_manager,
|
|
435
|
-
)
|
|
436
|
-
self._compressor = compressor
|
|
437
|
-
|
|
438
|
-
check = compressor.check_needs_compact(session_id)
|
|
439
|
-
if not check.needed:
|
|
440
|
-
return
|
|
441
|
-
|
|
442
|
-
user_msg = None
|
|
443
|
-
if self._original_user_index is not None and self._original_user_index < len(self._base_messages):
|
|
444
|
-
user_msg = self._base_messages[self._original_user_index]
|
|
445
|
-
|
|
446
|
-
current_round_start = len(self._base_messages)
|
|
447
|
-
if self._original_user_index is not None:
|
|
448
|
-
current_round_start = max(self._original_user_index + 1, 1)
|
|
449
|
-
|
|
450
|
-
current_round_msgs = self._base_messages[current_round_start:]
|
|
451
|
-
|
|
452
|
-
result = await compressor.compact(session_id)
|
|
453
|
-
|
|
454
|
-
if result.success and result.summary:
|
|
455
|
-
self._base_messages = []
|
|
456
|
-
if hasattr(self, "_injected_user_message") and self._injected_user_message:
|
|
457
|
-
self._base_messages.append(ChatMessage(
|
|
458
|
-
role="user",
|
|
459
|
-
content=self._injected_user_message,
|
|
460
|
-
))
|
|
461
|
-
self._original_user_index = 0
|
|
462
|
-
|
|
463
|
-
self._base_messages.append(ChatMessage(
|
|
464
|
-
role="assistant",
|
|
465
|
-
content=f"[SUMMARY] {result.summary}",
|
|
466
|
-
))
|
|
467
|
-
|
|
468
|
-
self._base_messages.extend(current_round_msgs)
|
|
469
|
-
|
|
470
|
-
if on_compact:
|
|
471
|
-
on_compact({
|
|
472
|
-
"needed": True,
|
|
473
|
-
"current_tokens": check.current_tokens,
|
|
474
|
-
"threshold": check.threshold,
|
|
475
|
-
"summary": result.summary,
|
|
476
|
-
})
|
|
477
|
-
except Exception:
|
|
478
|
-
pass
|
|
1
|
+
"""BaseAgent 抽象基类(ReAct 主循环),Python 3.6 兼容。"""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import platform as _platform
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any, Callable, List, Optional
|
|
8
|
+
|
|
9
|
+
from ..types import (
|
|
10
|
+
ChatMessage,
|
|
11
|
+
ChatResult,
|
|
12
|
+
ImageContent,
|
|
13
|
+
MediaFile,
|
|
14
|
+
PrintMsgFormat,
|
|
15
|
+
ProviderResponse,
|
|
16
|
+
StepInfo,
|
|
17
|
+
TextContent,
|
|
18
|
+
TokenUsage,
|
|
19
|
+
ToolCallInfo,
|
|
20
|
+
ToolResultInfo,
|
|
21
|
+
)
|
|
22
|
+
from ..provider.base import BaseProvider
|
|
23
|
+
from ..skills import build_available_skills_prompt
|
|
24
|
+
from ..specs import SpecInjector
|
|
25
|
+
from .._compat.resources import get_resource
|
|
26
|
+
from .tool_registry import ToolRegistry
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _format_tool_args(tool_name: str, args: dict) -> str:
|
|
30
|
+
key_map = {
|
|
31
|
+
"read_file": ["file_path"],
|
|
32
|
+
"write_file": ["file_path"],
|
|
33
|
+
"edit_file": ["file_path"],
|
|
34
|
+
"glob": ["pattern"],
|
|
35
|
+
"grep": ["pattern"],
|
|
36
|
+
"bash": ["command"],
|
|
37
|
+
}
|
|
38
|
+
keys = key_map.get(tool_name)
|
|
39
|
+
if keys:
|
|
40
|
+
parts = []
|
|
41
|
+
for k in keys:
|
|
42
|
+
v = args.get(k)
|
|
43
|
+
if v:
|
|
44
|
+
s = str(v)
|
|
45
|
+
if len(s) > 80:
|
|
46
|
+
s = s[:77] + "..."
|
|
47
|
+
parts.append(s)
|
|
48
|
+
result = " ".join(parts)
|
|
49
|
+
if len(result) > 80:
|
|
50
|
+
result = result[:77] + "..."
|
|
51
|
+
return result
|
|
52
|
+
for v in args.values():
|
|
53
|
+
if v:
|
|
54
|
+
s = str(v)
|
|
55
|
+
if len(s) > 80:
|
|
56
|
+
s = s[:77] + "..."
|
|
57
|
+
return s
|
|
58
|
+
return ""
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _build_step_print_msg(step: StepInfo) -> Optional[str]:
|
|
62
|
+
if not step.tool_calls:
|
|
63
|
+
return None
|
|
64
|
+
lines: List[str] = []
|
|
65
|
+
if step.reasoning:
|
|
66
|
+
lines.append(step.reasoning)
|
|
67
|
+
for i, tc in enumerate(step.tool_calls):
|
|
68
|
+
result = step.results[i] if i < len(step.results) else None
|
|
69
|
+
mark = PrintMsgFormat.UNKNOWN_MARK
|
|
70
|
+
if result:
|
|
71
|
+
mark = PrintMsgFormat.SUCCESS_MARK if result.success else PrintMsgFormat.FAILURE_MARK
|
|
72
|
+
args_summary = _format_tool_args(tc.name, tc.arguments)
|
|
73
|
+
lines.append(f"{{tool:'{mark} {tc.name} {args_summary}'}}")
|
|
74
|
+
return "\n".join(lines) if lines else None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
PROMPT_TEMPLATE = """
|
|
78
|
+
## 可用 Skills
|
|
79
|
+
|
|
80
|
+
{skills}
|
|
81
|
+
|
|
82
|
+
**注意**
|
|
83
|
+
读取skill 可以通过read_file工具读取内容
|
|
84
|
+
|
|
85
|
+
## 重要规则
|
|
86
|
+
|
|
87
|
+
- 如果工具执行失败,思考原因并尝试其他方法
|
|
88
|
+
- 最大迭代次数: {maxIterations}
|
|
89
|
+
|
|
90
|
+
## 运行环境
|
|
91
|
+
- 操作系统: {platform}
|
|
92
|
+
- 工作目录: {workdir}
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class BaseAgent(ABC):
|
|
97
|
+
def __init__(
|
|
98
|
+
self,
|
|
99
|
+
provider: BaseProvider,
|
|
100
|
+
tool_registry: ToolRegistry,
|
|
101
|
+
session_manager: Any = None,
|
|
102
|
+
max_iterations: int = 50,
|
|
103
|
+
work_dir: str = "",
|
|
104
|
+
system_prompt: Optional[str] = None,
|
|
105
|
+
log_enabled: bool = False,
|
|
106
|
+
):
|
|
107
|
+
self._provider = provider
|
|
108
|
+
self._registry = tool_registry
|
|
109
|
+
self._session_manager = session_manager
|
|
110
|
+
self._max_iterations = max_iterations
|
|
111
|
+
self._work_dir = work_dir or "."
|
|
112
|
+
self._system_prompt = system_prompt
|
|
113
|
+
self._log_enabled = log_enabled
|
|
114
|
+
self._round_count = 0
|
|
115
|
+
self._session_id: Optional[str] = None
|
|
116
|
+
|
|
117
|
+
@abstractmethod
|
|
118
|
+
def agent_type(self) -> str:
|
|
119
|
+
...
|
|
120
|
+
|
|
121
|
+
def _get_platform_name(self) -> str:
|
|
122
|
+
system = _platform.system()
|
|
123
|
+
if system == "Windows":
|
|
124
|
+
return "Windows"
|
|
125
|
+
elif system == "Darwin":
|
|
126
|
+
return "macOS"
|
|
127
|
+
else:
|
|
128
|
+
return "Linux"
|
|
129
|
+
|
|
130
|
+
def _load_role_template(self, package: str, filename: str = "role.txt") -> str:
|
|
131
|
+
raw = get_resource(package, filename)
|
|
132
|
+
return raw.replace("{platform}", self._get_platform_name()).replace("{workdir}", self._work_dir)
|
|
133
|
+
|
|
134
|
+
def _load_project_context(self) -> str:
|
|
135
|
+
project_md = Path(self._work_dir) / ".txcode" / "project" / "PROJECT.md"
|
|
136
|
+
if project_md.exists():
|
|
137
|
+
content = project_md.read_text("utf-8", errors="replace")
|
|
138
|
+
return f"\n<project-context>\n{content}\n</project-context>\n"
|
|
139
|
+
return ""
|
|
140
|
+
|
|
141
|
+
def _load_memory_context(self) -> str:
|
|
142
|
+
memory_dir = Path(self._work_dir) / ".txcode" / "memory"
|
|
143
|
+
if not memory_dir.exists():
|
|
144
|
+
return ""
|
|
145
|
+
parts: List[str] = []
|
|
146
|
+
for f in sorted(memory_dir.iterdir()):
|
|
147
|
+
if f.suffix in (".md", ".txt"):
|
|
148
|
+
parts.append(f.read_text("utf-8", errors="replace"))
|
|
149
|
+
if parts:
|
|
150
|
+
return f"\n<memory-context>\n" + "\n---\n".join(parts) + "\n</memory-context>\n"
|
|
151
|
+
return ""
|
|
152
|
+
|
|
153
|
+
@abstractmethod
|
|
154
|
+
def build_system_prompt(self, tools: List[dict]) -> str:
|
|
155
|
+
...
|
|
156
|
+
|
|
157
|
+
def _assemble_system_prompt(
|
|
158
|
+
self,
|
|
159
|
+
role: str,
|
|
160
|
+
tools: List[dict],
|
|
161
|
+
include_project: bool = True,
|
|
162
|
+
include_memory: bool = True,
|
|
163
|
+
) -> str:
|
|
164
|
+
if self._system_prompt:
|
|
165
|
+
return self._system_prompt
|
|
166
|
+
|
|
167
|
+
project_ctx = self._load_project_context() if include_project else ""
|
|
168
|
+
memory_ctx = self._load_memory_context() if include_memory else ""
|
|
169
|
+
skills_prompt = build_available_skills_prompt(self._work_dir)
|
|
170
|
+
|
|
171
|
+
prompt = PROMPT_TEMPLATE.format(
|
|
172
|
+
maxIterations=str(self._max_iterations),
|
|
173
|
+
skills=skills_prompt,
|
|
174
|
+
platform=self._get_platform_name(),
|
|
175
|
+
workdir=self._work_dir,
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
return role + project_ctx + memory_ctx + prompt
|
|
179
|
+
|
|
180
|
+
async def run(
|
|
181
|
+
self,
|
|
182
|
+
user_message: str,
|
|
183
|
+
session_id: Optional[str] = None,
|
|
184
|
+
history: Optional[List[dict]] = None,
|
|
185
|
+
media_files: Optional[List[MediaFile]] = None,
|
|
186
|
+
on_step: Optional[Callable[[StepInfo], None]] = None,
|
|
187
|
+
on_compact: Optional[Callable[[dict], None]] = None,
|
|
188
|
+
abort_signal: Any = None,
|
|
189
|
+
) -> ChatResult:
|
|
190
|
+
self._session_id = session_id
|
|
191
|
+
self._round_count = 0
|
|
192
|
+
tools = await self._registry.get_definitions()
|
|
193
|
+
system_prompt = self.build_system_prompt(tools)
|
|
194
|
+
self._base_messages: List[ChatMessage] = []
|
|
195
|
+
self._original_user_index: Optional[int] = None
|
|
196
|
+
self._injected_user_message: str = user_message
|
|
197
|
+
|
|
198
|
+
message_count = len(history) if history else 0
|
|
199
|
+
|
|
200
|
+
spec_injector = SpecInjector()
|
|
201
|
+
spec_injector.set_project_path(self._work_dir)
|
|
202
|
+
|
|
203
|
+
if spec_injector.should_inject(message_count):
|
|
204
|
+
injected = spec_injector.inject_into_message(user_message, self._work_dir)
|
|
205
|
+
self._injected_user_message = injected
|
|
206
|
+
else:
|
|
207
|
+
self._injected_user_message = user_message
|
|
208
|
+
|
|
209
|
+
if history:
|
|
210
|
+
for msg in history:
|
|
211
|
+
if msg.get("role") != "system":
|
|
212
|
+
self._base_messages.append(ChatMessage(
|
|
213
|
+
role=msg["role"],
|
|
214
|
+
content=msg.get("content", ""),
|
|
215
|
+
tool_call_id=msg.get("tool_call_id"),
|
|
216
|
+
tool_calls=msg.get("tool_calls"),
|
|
217
|
+
reasoning_content=msg.get("reasoning_content"),
|
|
218
|
+
))
|
|
219
|
+
|
|
220
|
+
user_msg = self._build_user_message(self._injected_user_message, media_files)
|
|
221
|
+
self._base_messages.append(user_msg)
|
|
222
|
+
self._original_user_index = len(self._base_messages) - 1
|
|
223
|
+
self._save_message(session_id, "user", user_message, keep_context=True, is_original=True, media_files=media_files)
|
|
224
|
+
|
|
225
|
+
steps: List[StepInfo] = []
|
|
226
|
+
iteration = 0
|
|
227
|
+
final_answer = ""
|
|
228
|
+
total_usage = TokenUsage()
|
|
229
|
+
|
|
230
|
+
while iteration < self._max_iterations:
|
|
231
|
+
if abort_signal and getattr(abort_signal, "aborted", False):
|
|
232
|
+
return ChatResult(
|
|
233
|
+
answer="", steps=steps, session_id=session_id or "",
|
|
234
|
+
iterations=iteration, success=False, error="Aborted",
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
iteration += 1
|
|
238
|
+
self._round_count += 1
|
|
239
|
+
if self._round_count >= 10:
|
|
240
|
+
self._check_hooks("round")
|
|
241
|
+
self._round_count = 0
|
|
242
|
+
|
|
243
|
+
messages: List[ChatMessage] = [
|
|
244
|
+
ChatMessage(role="system", content=system_prompt),
|
|
245
|
+
*self._base_messages,
|
|
246
|
+
]
|
|
247
|
+
|
|
248
|
+
response = await self._provider.chat(
|
|
249
|
+
messages, tools=tools, abort_signal=abort_signal,
|
|
250
|
+
log_context={
|
|
251
|
+
"session_id": session_id,
|
|
252
|
+
"agent_type": self.agent_type(),
|
|
253
|
+
"iteration": iteration,
|
|
254
|
+
"work_dir": self._work_dir,
|
|
255
|
+
} if self._log_enabled else None,
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
if response.usage:
|
|
259
|
+
total_usage.prompt_tokens = response.usage.prompt_tokens
|
|
260
|
+
total_usage.completion_tokens = response.usage.completion_tokens
|
|
261
|
+
total_usage.total_tokens = response.usage.total_tokens
|
|
262
|
+
|
|
263
|
+
if response.finish_reason == "stop" and response.content:
|
|
264
|
+
final_answer = response.content
|
|
265
|
+
self._save_message(session_id, "assistant", final_answer, keep_context=True)
|
|
266
|
+
break
|
|
267
|
+
|
|
268
|
+
if response.finish_reason == "tool_calls" and response.tool_calls:
|
|
269
|
+
tool_calls = ToolRegistry.parse_tool_calls(response.tool_calls)
|
|
270
|
+
results: List[ToolResultInfo] = []
|
|
271
|
+
|
|
272
|
+
for tc in tool_calls:
|
|
273
|
+
result = await self._registry.execute(
|
|
274
|
+
tc.name, tc.arguments,
|
|
275
|
+
self._build_tool_context(session_id or ""),
|
|
276
|
+
)
|
|
277
|
+
results.append(ToolResultInfo(
|
|
278
|
+
name=tc.name,
|
|
279
|
+
success=result.success,
|
|
280
|
+
output=result.output,
|
|
281
|
+
error=result.error,
|
|
282
|
+
))
|
|
283
|
+
|
|
284
|
+
step = StepInfo(
|
|
285
|
+
reasoning=response.reasoning,
|
|
286
|
+
tool_calls=tool_calls,
|
|
287
|
+
results=results,
|
|
288
|
+
iteration=iteration,
|
|
289
|
+
usage=response.usage,
|
|
290
|
+
)
|
|
291
|
+
step.printMsg = _build_step_print_msg(step)
|
|
292
|
+
steps.append(step)
|
|
293
|
+
|
|
294
|
+
if on_step:
|
|
295
|
+
on_step(step)
|
|
296
|
+
|
|
297
|
+
for i, tc in enumerate(tool_calls):
|
|
298
|
+
res = results[i]
|
|
299
|
+
# assistant message with tool_calls
|
|
300
|
+
assistant_msg = ChatMessage(
|
|
301
|
+
role="assistant",
|
|
302
|
+
content="",
|
|
303
|
+
tool_calls=[{
|
|
304
|
+
"id": tc.id,
|
|
305
|
+
"type": "function",
|
|
306
|
+
"function": {"name": tc.name, "arguments": json.dumps(tc.arguments)},
|
|
307
|
+
}],
|
|
308
|
+
reasoning_content=response.reasoning,
|
|
309
|
+
)
|
|
310
|
+
self._base_messages.append(assistant_msg)
|
|
311
|
+
self._save_message(session_id, "assistant", json.dumps({
|
|
312
|
+
"type": "assistant_with_tools",
|
|
313
|
+
"toolCalls": assistant_msg.tool_calls,
|
|
314
|
+
"thought": response.reasoning or "",
|
|
315
|
+
"reasoning_content": response.reasoning or "",
|
|
316
|
+
}), keep_context=True)
|
|
317
|
+
|
|
318
|
+
# tool message
|
|
319
|
+
tool_msg = ChatMessage(
|
|
320
|
+
role="tool",
|
|
321
|
+
content=res.output if res.success else f"Error: {res.error}",
|
|
322
|
+
tool_call_id=tc.id,
|
|
323
|
+
)
|
|
324
|
+
self._base_messages.append(tool_msg)
|
|
325
|
+
self._save_message(session_id, "tool", tool_msg.content, keep_context=False, tool_call_id=tc.id)
|
|
326
|
+
|
|
327
|
+
await self._check_and_compact(session_id, on_compact)
|
|
328
|
+
continue
|
|
329
|
+
|
|
330
|
+
if response.content:
|
|
331
|
+
final_answer = response.content
|
|
332
|
+
break
|
|
333
|
+
|
|
334
|
+
break
|
|
335
|
+
|
|
336
|
+
self._check_hooks("end")
|
|
337
|
+
|
|
338
|
+
success = final_answer != ""
|
|
339
|
+
return ChatResult(
|
|
340
|
+
answer=final_answer or "Unable to complete task",
|
|
341
|
+
steps=steps,
|
|
342
|
+
session_id=session_id or "",
|
|
343
|
+
iterations=iteration,
|
|
344
|
+
success=success,
|
|
345
|
+
error="Max iterations reached" if iteration >= self._max_iterations and not final_answer else None,
|
|
346
|
+
usage=total_usage,
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
def _check_hooks(self, trigger: str):
|
|
350
|
+
try:
|
|
351
|
+
from ..hooks import create_hooks
|
|
352
|
+
if not hasattr(self, "_hooks"):
|
|
353
|
+
self._hooks = create_hooks(
|
|
354
|
+
self._provider, self._session_manager, self._work_dir
|
|
355
|
+
)
|
|
356
|
+
messages: List[dict] = []
|
|
357
|
+
if self._session_id and self._session_manager:
|
|
358
|
+
session = self._session_manager.get(self._session_id)
|
|
359
|
+
if session:
|
|
360
|
+
messages = session.get("messages", [])
|
|
361
|
+
self._hooks.queue.emit(
|
|
362
|
+
trigger,
|
|
363
|
+
{
|
|
364
|
+
"messages": messages,
|
|
365
|
+
"trigger": trigger,
|
|
366
|
+
"metadata": {
|
|
367
|
+
"session_id": self._session_id,
|
|
368
|
+
"project_path": self._work_dir,
|
|
369
|
+
"round_count": self._round_count,
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
)
|
|
373
|
+
except Exception:
|
|
374
|
+
pass
|
|
375
|
+
|
|
376
|
+
def _build_user_message(self, text: str, media_files: Optional[List[MediaFile]]) -> ChatMessage:
|
|
377
|
+
if media_files:
|
|
378
|
+
content: list = [TextContent(text=text)]
|
|
379
|
+
for mf in media_files:
|
|
380
|
+
if mf.data_url:
|
|
381
|
+
url = mf.data_url
|
|
382
|
+
else:
|
|
383
|
+
import base64
|
|
384
|
+
import mimetypes
|
|
385
|
+
from pathlib import Path
|
|
386
|
+
path = Path(mf.file_path)
|
|
387
|
+
mime = mf.type or mimetypes.guess_type(str(path))[0] or "image/png"
|
|
388
|
+
with open(path, "rb") as f:
|
|
389
|
+
b64 = base64.b64encode(f.read()).decode("utf-8")
|
|
390
|
+
url = f"data:{mime};base64,{b64}"
|
|
391
|
+
content.append(ImageContent(url=url))
|
|
392
|
+
return ChatMessage(role="user", content=content)
|
|
393
|
+
return ChatMessage(role="user", content=text)
|
|
394
|
+
|
|
395
|
+
def _build_tool_context(self, session_id: str):
|
|
396
|
+
from ..types import ToolContext
|
|
397
|
+
return ToolContext(session_id=session_id, work_dir=self._work_dir)
|
|
398
|
+
|
|
399
|
+
def _save_message(
|
|
400
|
+
self,
|
|
401
|
+
session_id: Optional[str],
|
|
402
|
+
role: str,
|
|
403
|
+
content: str,
|
|
404
|
+
keep_context: bool = True,
|
|
405
|
+
is_original: bool = False,
|
|
406
|
+
tool_call_id: Optional[str] = None,
|
|
407
|
+
media_files: Optional[List[MediaFile]] = None,
|
|
408
|
+
):
|
|
409
|
+
if not session_id or not self._session_manager:
|
|
410
|
+
return
|
|
411
|
+
mf_list = None
|
|
412
|
+
if media_files:
|
|
413
|
+
mf_list = [{"file_path": mf.file_path, "type": mf.type} for mf in media_files]
|
|
414
|
+
self._session_manager.add_message(
|
|
415
|
+
session_id, role, content,
|
|
416
|
+
keep_context=keep_context, is_original=is_original,
|
|
417
|
+
tool_call_id=tool_call_id, media_files=mf_list,
|
|
418
|
+
)
|
|
419
|
+
|
|
420
|
+
async def _check_and_compact(
|
|
421
|
+
self,
|
|
422
|
+
session_id: Optional[str],
|
|
423
|
+
on_compact: Optional[Callable[[dict], None]],
|
|
424
|
+
):
|
|
425
|
+
if not session_id or not self._session_manager:
|
|
426
|
+
return
|
|
427
|
+
try:
|
|
428
|
+
from ..compact.compressor import ContextCompressor
|
|
429
|
+
compressor = getattr(self, "_compressor", None)
|
|
430
|
+
if compressor is None:
|
|
431
|
+
compressor = ContextCompressor(
|
|
432
|
+
max_tokens=150_000,
|
|
433
|
+
provider=self._provider,
|
|
434
|
+
session_manager=self._session_manager,
|
|
435
|
+
)
|
|
436
|
+
self._compressor = compressor
|
|
437
|
+
|
|
438
|
+
check = compressor.check_needs_compact(session_id)
|
|
439
|
+
if not check.needed:
|
|
440
|
+
return
|
|
441
|
+
|
|
442
|
+
user_msg = None
|
|
443
|
+
if self._original_user_index is not None and self._original_user_index < len(self._base_messages):
|
|
444
|
+
user_msg = self._base_messages[self._original_user_index]
|
|
445
|
+
|
|
446
|
+
current_round_start = len(self._base_messages)
|
|
447
|
+
if self._original_user_index is not None:
|
|
448
|
+
current_round_start = max(self._original_user_index + 1, 1)
|
|
449
|
+
|
|
450
|
+
current_round_msgs = self._base_messages[current_round_start:]
|
|
451
|
+
|
|
452
|
+
result = await compressor.compact(session_id)
|
|
453
|
+
|
|
454
|
+
if result.success and result.summary:
|
|
455
|
+
self._base_messages = []
|
|
456
|
+
if hasattr(self, "_injected_user_message") and self._injected_user_message:
|
|
457
|
+
self._base_messages.append(ChatMessage(
|
|
458
|
+
role="user",
|
|
459
|
+
content=self._injected_user_message,
|
|
460
|
+
))
|
|
461
|
+
self._original_user_index = 0
|
|
462
|
+
|
|
463
|
+
self._base_messages.append(ChatMessage(
|
|
464
|
+
role="assistant",
|
|
465
|
+
content=f"[SUMMARY] {result.summary}",
|
|
466
|
+
))
|
|
467
|
+
|
|
468
|
+
self._base_messages.extend(current_round_msgs)
|
|
469
|
+
|
|
470
|
+
if on_compact:
|
|
471
|
+
on_compact({
|
|
472
|
+
"needed": True,
|
|
473
|
+
"current_tokens": check.current_tokens,
|
|
474
|
+
"threshold": check.threshold,
|
|
475
|
+
"summary": result.summary,
|
|
476
|
+
})
|
|
477
|
+
except Exception:
|
|
478
|
+
pass
|