auto-coder 0.1.363__py3-none-any.whl → 0.1.365__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.

Potentially problematic release.


This version of auto-coder might be problematic. Click here for more details.

Files changed (39) hide show
  1. {auto_coder-0.1.363.dist-info → auto_coder-0.1.365.dist-info}/METADATA +2 -2
  2. {auto_coder-0.1.363.dist-info → auto_coder-0.1.365.dist-info}/RECORD +39 -23
  3. autocoder/agent/base_agentic/tools/execute_command_tool_resolver.py +1 -1
  4. autocoder/auto_coder.py +46 -2
  5. autocoder/auto_coder_runner.py +2 -0
  6. autocoder/common/__init__.py +5 -0
  7. autocoder/common/file_checkpoint/__init__.py +21 -0
  8. autocoder/common/file_checkpoint/backup.py +264 -0
  9. autocoder/common/file_checkpoint/conversation_checkpoint.py +182 -0
  10. autocoder/common/file_checkpoint/examples.py +217 -0
  11. autocoder/common/file_checkpoint/manager.py +611 -0
  12. autocoder/common/file_checkpoint/models.py +156 -0
  13. autocoder/common/file_checkpoint/store.py +383 -0
  14. autocoder/common/file_checkpoint/test_backup.py +242 -0
  15. autocoder/common/file_checkpoint/test_manager.py +570 -0
  16. autocoder/common/file_checkpoint/test_models.py +360 -0
  17. autocoder/common/file_checkpoint/test_store.py +327 -0
  18. autocoder/common/file_checkpoint/test_utils.py +297 -0
  19. autocoder/common/file_checkpoint/utils.py +119 -0
  20. autocoder/common/rulefiles/autocoderrules_utils.py +114 -55
  21. autocoder/common/save_formatted_log.py +76 -5
  22. autocoder/common/utils_code_auto_generate.py +2 -1
  23. autocoder/common/v2/agent/agentic_edit.py +545 -225
  24. autocoder/common/v2/agent/agentic_edit_tools/list_files_tool_resolver.py +83 -43
  25. autocoder/common/v2/agent/agentic_edit_tools/read_file_tool_resolver.py +116 -29
  26. autocoder/common/v2/agent/agentic_edit_tools/replace_in_file_tool_resolver.py +179 -48
  27. autocoder/common/v2/agent/agentic_edit_tools/search_files_tool_resolver.py +101 -56
  28. autocoder/common/v2/agent/agentic_edit_tools/test_write_to_file_tool_resolver.py +322 -0
  29. autocoder/common/v2/agent/agentic_edit_tools/write_to_file_tool_resolver.py +173 -132
  30. autocoder/common/v2/agent/agentic_edit_types.py +4 -0
  31. autocoder/compilers/normal_compiler.py +64 -0
  32. autocoder/events/event_manager_singleton.py +133 -4
  33. autocoder/linters/normal_linter.py +373 -0
  34. autocoder/linters/python_linter.py +4 -2
  35. autocoder/version.py +1 -1
  36. {auto_coder-0.1.363.dist-info → auto_coder-0.1.365.dist-info}/LICENSE +0 -0
  37. {auto_coder-0.1.363.dist-info → auto_coder-0.1.365.dist-info}/WHEEL +0 -0
  38. {auto_coder-0.1.363.dist-info → auto_coder-0.1.365.dist-info}/entry_points.txt +0 -0
  39. {auto_coder-0.1.363.dist-info → auto_coder-0.1.365.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,611 @@
1
+ """
2
+ 文件变更管理器
3
+
4
+ 整个模块的主入口,提供高层次的API接口,用于应用、记录和撤销文件变更。
5
+ """
6
+
7
+ import os
8
+ import uuid
9
+ import logging
10
+ import difflib
11
+ from typing import Dict, List, Optional, Tuple, Any
12
+ from datetime import datetime
13
+
14
+ from autocoder.common.file_checkpoint.models import (
15
+ FileChange, ChangeRecord, ApplyResult, UndoResult, DiffResult
16
+ )
17
+ from autocoder.common.file_checkpoint.backup import FileBackupManager
18
+ from autocoder.common.file_checkpoint.store import FileChangeStore
19
+ from autocoder.common.file_checkpoint.conversation_checkpoint import ConversationCheckpointStore,ConversationCheckpoint
20
+
21
+ logger = logging.getLogger(__name__)
22
+
23
+
24
+ class FileChangeManager:
25
+ """文件变更管理器,提供高层次的API接口"""
26
+
27
+ def __init__(self, project_dir: str, backup_dir: Optional[str] = None,
28
+ store_dir: Optional[str] = None, max_history: int = 50,
29
+ conversation_store_dir: Optional[str] = None):
30
+ """
31
+ 初始化文件变更管理器
32
+
33
+ Args:
34
+ project_dir: 用户项目的根目录
35
+ backup_dir: 备份文件存储目录,默认为用户主目录下的.autocoder/backups
36
+ store_dir: 变更记录存储目录,默认为用户主目录下的.autocoder/changes
37
+ max_history: 最大保存的历史版本数量
38
+ conversation_store_dir: 对话检查点存储目录
39
+ """
40
+ self.project_dir = os.path.abspath(project_dir)
41
+ self.backup_manager = FileBackupManager(backup_dir)
42
+ self.change_store = FileChangeStore(store_dir, max_history)
43
+
44
+ # 初始化对话检查点存储
45
+ if conversation_store_dir is None and store_dir is not None:
46
+ # 默认在变更记录存储目录的同级目录创建 conversation_checkpoints 目录
47
+ parent_dir = os.path.dirname(store_dir)
48
+ conversation_store_dir = os.path.join(parent_dir, "conversation_checkpoints")
49
+
50
+ try:
51
+ self.conversation_store = ConversationCheckpointStore(conversation_store_dir, max_history)
52
+ logger.info(f"对话检查点存储初始化成功: {conversation_store_dir}")
53
+ except ImportError as e:
54
+ logger.warning(f"对话检查点存储初始化失败: {str(e)}")
55
+ self.conversation_store = None
56
+
57
+ def apply_changes_with_conversation(self, changes: Dict[str, FileChange],
58
+ conversations: List[Dict[str, Any]],
59
+ change_group_id: Optional[str] = None,
60
+ metadata: Optional[Dict[str, Any]] = None) -> ApplyResult:
61
+ """
62
+ 应用文件变更并保存对话状态
63
+
64
+ Args:
65
+ changes: 文件变更字典,格式为 {file_path: FileChange}
66
+ conversations: 当前对话历史
67
+ change_group_id: 变更组ID,用于将相关变更归为一组
68
+ metadata: 元数据,可包含额外信息
69
+
70
+ Returns:
71
+ ApplyResult: 应用结果对象
72
+ """
73
+ # 应用文件变更
74
+ result = self.apply_changes(changes, change_group_id)
75
+
76
+ if result.success and self.conversation_store is not None:
77
+ try:
78
+
79
+ # 创建并保存对话检查点
80
+ checkpoint_id = change_group_id or result.change_ids[0] if result.change_ids else str(uuid.uuid4())
81
+ checkpoint = ConversationCheckpoint(
82
+ checkpoint_id=checkpoint_id,
83
+ timestamp=time.time(),
84
+ conversations=conversations,
85
+ metadata=metadata
86
+ )
87
+ self.conversation_store.save_checkpoint(checkpoint)
88
+ logger.info(f"已保存对话检查点: {checkpoint_id}")
89
+ except Exception as e:
90
+ logger.error(f"保存对话检查点失败: {str(e)}")
91
+
92
+ return result
93
+
94
+ def apply_changes(self, changes: Dict[str, FileChange], change_group_id: Optional[str] = None) -> ApplyResult:
95
+ """
96
+ 应用一组文件变更
97
+
98
+ Args:
99
+ changes: 文件变更字典,格式为 {file_path: FileChange}
100
+ change_group_id: 变更组ID,用于将相关变更归为一组
101
+
102
+ Returns:
103
+ ApplyResult: 应用结果对象
104
+ """
105
+ # 如果没有提供变更组ID,生成一个新的
106
+ if change_group_id is None:
107
+ change_group_id = str(uuid.uuid4())
108
+
109
+ result = ApplyResult(success=True)
110
+
111
+ # 处理每个文件的变更
112
+ for file_path, change in changes.items():
113
+ try:
114
+ # 获取文件的绝对路径
115
+ abs_file_path = self._get_absolute_path(file_path)
116
+
117
+ # 确定文件是否是新文件
118
+ is_new = not os.path.exists(abs_file_path)
119
+
120
+ # 备份原文件(如果存在)
121
+ backup_id = None
122
+ if not is_new and not change.is_deletion:
123
+ backup_id = self.backup_manager.backup_file(abs_file_path)
124
+
125
+ # 处理文件变更
126
+ if change.is_deletion:
127
+ # 如果是删除操作,先备份再删除
128
+ backup_id = self.backup_manager.backup_file(abs_file_path)
129
+ if os.path.exists(abs_file_path):
130
+ os.remove(abs_file_path)
131
+ else:
132
+ # 确保目录存在
133
+ dir_path = os.path.dirname(abs_file_path)
134
+ if dir_path:
135
+ os.makedirs(dir_path, exist_ok=True)
136
+
137
+ # 写入文件内容
138
+ with open(abs_file_path, 'w', encoding='utf-8') as f:
139
+ f.write(change.content)
140
+
141
+ # 创建变更记录
142
+ change_record = ChangeRecord.create(
143
+ file_path=file_path,
144
+ backup_id=backup_id,
145
+ is_new=is_new,
146
+ is_deletion=change.is_deletion,
147
+ group_id=change_group_id
148
+ )
149
+
150
+ # 保存变更记录
151
+ change_id = self.change_store.save_change(change_record)
152
+ result.add_change_id(change_id)
153
+
154
+ logger.info(f"已应用变更到文件 {file_path}")
155
+
156
+ except Exception as e:
157
+ error_message = f"应用变更到文件 {file_path} 失败: {str(e)}"
158
+ logger.error(error_message)
159
+ result.add_error(file_path, error_message)
160
+
161
+ return result
162
+
163
+ def preview_changes(self, changes: Dict[str, FileChange]) -> Dict[str, DiffResult]:
164
+ """
165
+ 预览变更的差异
166
+
167
+ Args:
168
+ changes: 文件变更字典
169
+
170
+ Returns:
171
+ Dict[str, DiffResult]: 每个文件的差异结果
172
+ """
173
+ diff_results = {}
174
+
175
+ for file_path, change in changes.items():
176
+ try:
177
+ # 获取文件的绝对路径
178
+ abs_file_path = self._get_absolute_path(file_path)
179
+
180
+ # 确定文件是否是新文件
181
+ is_new = not os.path.exists(abs_file_path)
182
+
183
+ # 获取原文件内容
184
+ old_content = None
185
+ if not is_new and not change.is_deletion:
186
+ try:
187
+ with open(abs_file_path, 'r', encoding='utf-8') as f:
188
+ old_content = f.read()
189
+ except Exception as e:
190
+ logger.error(f"读取文件 {abs_file_path} 失败: {str(e)}")
191
+
192
+ # 创建差异结果
193
+ diff_result = DiffResult(
194
+ file_path=file_path,
195
+ old_content=old_content,
196
+ new_content=change.content,
197
+ is_new=is_new,
198
+ is_deletion=change.is_deletion
199
+ )
200
+
201
+ diff_results[file_path] = diff_result
202
+
203
+ except Exception as e:
204
+ logger.error(f"预览文件 {file_path} 的变更差异失败: {str(e)}")
205
+
206
+ return diff_results
207
+
208
+ def undo_last_change_with_conversation(self) -> Tuple[UndoResult, Optional[ConversationCheckpoint]]:
209
+ """
210
+ 撤销最近的一次变更并恢复对话状态
211
+
212
+ Returns:
213
+ Tuple[UndoResult, Optional[ConversationCheckpoint]]: 撤销结果和恢复的对话检查点
214
+ """
215
+ # 获取最近的变更记录
216
+ latest_changes = self.change_store.get_latest_changes(limit=1)
217
+ if not latest_changes:
218
+ return UndoResult(success=False, errors={"general": "没有找到最近的变更记录"}), None
219
+
220
+ latest_change = latest_changes[0]
221
+
222
+ # 如果最近的变更属于一个组,撤销整个组
223
+ if latest_change.group_id:
224
+ return self.undo_change_group_with_conversation(latest_change.group_id)
225
+ else:
226
+ # 否则只撤销这一个变更
227
+ return self.undo_change_with_conversation(latest_change.change_id)
228
+
229
+ def undo_last_change(self) -> UndoResult:
230
+ """
231
+ 撤销最近的一次变更
232
+
233
+ Returns:
234
+ UndoResult: 撤销结果对象
235
+ """
236
+ # 获取最近的变更记录
237
+ latest_changes = self.change_store.get_latest_changes(limit=1)
238
+ if not latest_changes:
239
+ return UndoResult(success=False, errors={"general": "没有找到最近的变更记录"})
240
+
241
+ latest_change = latest_changes[0]
242
+
243
+ # 如果最近的变更属于一个组,撤销整个组
244
+ if latest_change.group_id:
245
+ return self.undo_change_group(latest_change.group_id)
246
+ else:
247
+ # 否则只撤销这一个变更
248
+ return self.undo_change(latest_change.change_id)
249
+
250
+ def undo_change_with_conversation(self, change_id: str) -> Tuple[UndoResult, Optional[ConversationCheckpoint]]:
251
+ """
252
+ 撤销指定的变更并恢复对话状态
253
+
254
+ Args:
255
+ change_id: 变更记录ID
256
+
257
+ Returns:
258
+ Tuple[UndoResult, Optional[ConversationCheckpoint]]: 撤销结果和恢复的对话检查点
259
+ """
260
+ # 获取变更记录
261
+ change_record = self.change_store.get_change(change_id)
262
+ if change_record is None:
263
+ return UndoResult(success=False, errors={"general": f"变更记录 {change_id} 不存在"}), None
264
+
265
+ # 获取关联的对话检查点
266
+ checkpoint = None
267
+ checkpoint_id = change_record.group_id or change_id
268
+ if self.conversation_store is not None:
269
+ try:
270
+ checkpoint = self.conversation_store.get_checkpoint(checkpoint_id)
271
+ if checkpoint:
272
+ logger.info(f"找到关联的对话检查点: {checkpoint_id}")
273
+ else:
274
+ logger.info(f"未找到关联的对话检查点: {checkpoint_id}")
275
+ except Exception as e:
276
+ logger.error(f"获取对话检查点失败: {str(e)}")
277
+
278
+ # 撤销文件变更
279
+ undo_result = self.undo_change(change_id)
280
+
281
+ return undo_result, checkpoint
282
+
283
+ def undo_change(self, change_id: str) -> UndoResult:
284
+ """
285
+ 撤销指定的变更
286
+
287
+ Args:
288
+ change_id: 变更记录ID
289
+
290
+ Returns:
291
+ UndoResult: 撤销结果对象
292
+ """
293
+ # 获取变更记录
294
+ change_record = self.change_store.get_change(change_id)
295
+ if change_record is None:
296
+ return UndoResult(success=False, errors={"general": f"变更记录 {change_id} 不存在"})
297
+
298
+ result = UndoResult(success=True)
299
+
300
+ try:
301
+ # 获取文件的绝对路径
302
+ abs_file_path = self._get_absolute_path(change_record.file_path)
303
+
304
+ # 根据变更类型执行撤销操作
305
+ if change_record.is_new:
306
+ # 如果是新建文件的变更,删除该文件
307
+ if os.path.exists(abs_file_path):
308
+ os.remove(abs_file_path)
309
+ result.add_restored_file(change_record.file_path)
310
+ elif change_record.is_deletion:
311
+ # 如果是删除文件的变更,从备份恢复
312
+ if change_record.backup_id:
313
+ success = self.backup_manager.restore_file(abs_file_path, change_record.backup_id)
314
+ if success:
315
+ result.add_restored_file(change_record.file_path)
316
+ else:
317
+ result.add_error(change_record.file_path, "从备份恢复文件失败")
318
+ else:
319
+ result.add_error(change_record.file_path, "没有找到文件备份")
320
+ else:
321
+ # 如果是修改文件的变更,从备份恢复
322
+ if change_record.backup_id:
323
+ success = self.backup_manager.restore_file(abs_file_path, change_record.backup_id)
324
+ if success:
325
+ result.add_restored_file(change_record.file_path)
326
+ else:
327
+ result.add_error(change_record.file_path, "从备份恢复文件失败")
328
+ else:
329
+ result.add_error(change_record.file_path, "没有找到文件备份")
330
+
331
+ # 删除变更记录
332
+ self.change_store.delete_change(change_id)
333
+
334
+ logger.info(f"已撤销变更 {change_id}")
335
+
336
+ except Exception as e:
337
+ error_message = f"撤销变更 {change_id} 失败: {str(e)}"
338
+ logger.error(error_message)
339
+ result.add_error(change_record.file_path, error_message)
340
+ result.success = False
341
+
342
+ return result
343
+
344
+ def undo_change_group_with_conversation(self, group_id: str) -> Tuple[UndoResult, Optional[ConversationCheckpoint]]:
345
+ """
346
+ 撤销指定组的所有变更并恢复对话状态
347
+
348
+ Args:
349
+ group_id: 变更组ID
350
+
351
+ Returns:
352
+ Tuple[UndoResult, Optional[ConversationCheckpoint]]: 撤销结果和恢复的对话检查点
353
+ """
354
+ # 获取组内的所有变更记录
355
+ changes = self.change_store.get_changes_by_group(group_id)
356
+ if not changes:
357
+ return UndoResult(success=False, errors={"general": f"变更组 {group_id} 不存在或为空"}), None
358
+
359
+ # 获取关联的对话检查点
360
+ checkpoint = None
361
+ if self.conversation_store is not None:
362
+ try:
363
+ checkpoint = self.conversation_store.get_checkpoint(group_id)
364
+ if checkpoint:
365
+ logger.info(f"找到关联的对话检查点: {group_id}")
366
+ else:
367
+ logger.info(f"未找到关联的对话检查点: {group_id}")
368
+ except Exception as e:
369
+ logger.error(f"获取对话检查点失败: {str(e)}")
370
+
371
+ # 撤销文件变更
372
+ undo_result = self.undo_change_group(group_id)
373
+
374
+ return undo_result, checkpoint
375
+
376
+ def undo_change_group(self, group_id: str) -> UndoResult:
377
+ """
378
+ 撤销指定组的所有变更
379
+
380
+ Args:
381
+ group_id: 变更组ID
382
+
383
+ Returns:
384
+ UndoResult: 撤销结果对象
385
+ """
386
+ # 获取组内的所有变更记录
387
+ changes = self.change_store.get_changes_by_group(group_id)
388
+ if not changes:
389
+ return UndoResult(success=False, errors={"general": f"变更组 {group_id} 不存在或为空"})
390
+
391
+ result = UndoResult(success=True)
392
+
393
+ # 按时间戳降序排序,确保按照相反的顺序撤销
394
+ changes.sort(key=lambda x: x.timestamp, reverse=True)
395
+
396
+ # 逐个撤销变更
397
+ for change in changes:
398
+ change_result = self.undo_change(change.change_id)
399
+
400
+ # 合并结果
401
+ result.success = result.success and change_result.success
402
+ result.restored_files.extend(change_result.restored_files)
403
+ result.errors.update(change_result.errors)
404
+
405
+ return result
406
+
407
+ def undo_to_version_with_conversation(self, version_id: str) -> Tuple[UndoResult, Optional[ConversationCheckpoint]]:
408
+ """
409
+ 撤销到指定的历史版本并恢复对话状态
410
+
411
+ Args:
412
+ version_id: 目标版本ID(变更记录ID)
413
+
414
+ Returns:
415
+ Tuple[UndoResult, Optional[ConversationCheckpoint]]: 撤销结果和恢复的对话检查点
416
+ """
417
+ # 获取目标版本的变更记录
418
+ target_change = self.change_store.get_change(version_id)
419
+ if target_change is None:
420
+ return UndoResult(success=False, errors={"general": f"变更记录 {version_id} 不存在"}), None
421
+
422
+ # 获取关联的对话检查点
423
+ checkpoint = None
424
+ checkpoint_id = target_change.group_id or version_id
425
+ if self.conversation_store is not None:
426
+ try:
427
+ checkpoint = self.conversation_store.get_checkpoint(checkpoint_id)
428
+ if checkpoint:
429
+ logger.info(f"找到关联的对话检查点: {checkpoint_id}")
430
+ else:
431
+ logger.info(f"未找到关联的对话检查点: {checkpoint_id}")
432
+ except Exception as e:
433
+ logger.error(f"获取对话检查点失败: {str(e)}")
434
+
435
+ # 撤销文件变更
436
+ undo_result = self.undo_to_version(version_id)
437
+
438
+ return undo_result, checkpoint
439
+
440
+ def undo_to_version(self, version_id: str) -> UndoResult:
441
+ """
442
+ 撤销到指定的历史版本
443
+
444
+ Args:
445
+ version_id: 目标版本ID(变更记录ID)
446
+
447
+ Returns:
448
+ UndoResult: 撤销结果对象
449
+ """
450
+ # 获取目标版本的变更记录
451
+ target_change = self.change_store.get_change(version_id)
452
+ if target_change is None:
453
+ return UndoResult(success=False, errors={"general": f"变更记录 {version_id} 不存在"})
454
+
455
+ # 获取最近的变更记录
456
+ latest_changes = self.change_store.get_latest_changes()
457
+ if not latest_changes:
458
+ return UndoResult(success=False, errors={"general": "没有找到最近的变更记录"})
459
+
460
+ # 找出需要撤销的变更记录
461
+ changes_to_undo = []
462
+ for change in latest_changes:
463
+ if change.timestamp > target_change.timestamp:
464
+ changes_to_undo.append(change)
465
+
466
+ if not changes_to_undo:
467
+ return UndoResult(success=True, restored_files=[])
468
+
469
+ result = UndoResult(success=True)
470
+
471
+ # 按时间戳降序排序,确保按照相反的顺序撤销
472
+ changes_to_undo.sort(key=lambda x: x.timestamp, reverse=True)
473
+
474
+ # 逐个撤销变更
475
+ for change in changes_to_undo:
476
+ change_result = self.undo_change(change.change_id)
477
+
478
+ # 合并结果
479
+ result.success = result.success and change_result.success
480
+ result.restored_files.extend(change_result.restored_files)
481
+ result.errors.update(change_result.errors)
482
+
483
+ return result
484
+
485
+ def get_change_history(self, limit: int = 10) -> List[ChangeRecord]:
486
+ """
487
+ 获取变更历史记录
488
+
489
+ Args:
490
+ limit: 返回的历史记录数量限制
491
+
492
+ Returns:
493
+ List[ChangeRecord]: 变更记录列表
494
+ """
495
+ return self.change_store.get_latest_changes(limit)
496
+
497
+ def get_file_history(self, file_path: str, limit: int = 10) -> List[ChangeRecord]:
498
+ """
499
+ 获取指定文件的变更历史
500
+
501
+ Args:
502
+ file_path: 文件路径
503
+ limit: 返回的历史记录数量限制
504
+
505
+ Returns:
506
+ List[ChangeRecord]: 变更记录列表
507
+ """
508
+ return self.change_store.get_changes_by_file(file_path, limit)
509
+
510
+ def get_changes_by_group(self, group_id: str) -> List[ChangeRecord]:
511
+ """
512
+ 获取指定变更组的所有变更记录
513
+
514
+ Args:
515
+ group_id: 变更组ID
516
+
517
+ Returns:
518
+ List[ChangeRecord]: 变更记录列表
519
+ """
520
+ return self.change_store.get_changes_by_group(group_id)
521
+
522
+ def get_change_groups(self, limit: int = 10) -> List[Tuple[str, float, int]]:
523
+ """
524
+ 获取变更组列表
525
+
526
+ Args:
527
+ limit: 返回的组数量限制
528
+
529
+ Returns:
530
+ List[Tuple[str, float, int]]: 变更组ID、最新时间戳和变更数量的列表
531
+ """
532
+ return self.change_store.get_change_groups(limit)
533
+
534
+ def get_available_checkpoints(self, limit: int = 10) -> List[Dict[str, Any]]:
535
+ """
536
+ 获取可用的检查点列表,包含对话状态信息
537
+
538
+ Args:
539
+ limit: 返回的检查点数量限制
540
+
541
+ Returns:
542
+ List[Dict[str, Any]]: 检查点信息列表
543
+ """
544
+ # 获取变更组列表
545
+ change_groups = self.get_change_groups(limit)
546
+
547
+ # 构建检查点信息
548
+ checkpoints = []
549
+ for group_id, timestamp, count in change_groups:
550
+ has_conversation = False
551
+
552
+ # 检查是否有对话检查点
553
+ if self.conversation_store is not None:
554
+ try:
555
+ checkpoint = self.conversation_store.get_checkpoint(group_id)
556
+ has_conversation = checkpoint is not None
557
+ except Exception as e:
558
+ logger.error(f"获取对话检查点失败: {str(e)}")
559
+
560
+ checkpoints.append({
561
+ "id": group_id,
562
+ "timestamp": timestamp,
563
+ "changes_count": count,
564
+ "has_conversation": has_conversation
565
+ })
566
+
567
+ return checkpoints
568
+
569
+ def get_diff_text(self, old_content: str, new_content: str) -> str:
570
+ """
571
+ 获取两个文本内容的差异文本
572
+
573
+ Args:
574
+ old_content: 原始内容
575
+ new_content: 新内容
576
+
577
+ Returns:
578
+ str: 差异文本
579
+ """
580
+ if old_content is None:
581
+ return "新文件"
582
+
583
+ if new_content is None:
584
+ return "文件已删除"
585
+
586
+ old_lines = old_content.splitlines()
587
+ new_lines = new_content.splitlines()
588
+
589
+ diff = difflib.unified_diff(
590
+ old_lines,
591
+ new_lines,
592
+ lineterm='',
593
+ n=3 # 上下文行数
594
+ )
595
+
596
+ return '\n'.join(diff)
597
+
598
+ def _get_absolute_path(self, file_path: str) -> str:
599
+ """
600
+ 获取文件的绝对路径
601
+
602
+ Args:
603
+ file_path: 文件相对路径或绝对路径
604
+
605
+ Returns:
606
+ str: 文件的绝对路径
607
+ """
608
+ if os.path.isabs(file_path):
609
+ return file_path
610
+ else:
611
+ return os.path.join(self.project_dir, file_path)