abyss-cli 0.1.4__tar.gz → 0.1.6__tar.gz

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.
Files changed (56) hide show
  1. {abyss_cli-0.1.4/src/abyss_cli.egg-info → abyss_cli-0.1.6}/PKG-INFO +1 -1
  2. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/pyproject.toml +1 -1
  3. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/__init__.py +1 -1
  4. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/ansi_menu.py +168 -19
  5. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/config.py +27 -1
  6. abyss_cli-0.1.6/src/abyss/interrupt.py +53 -0
  7. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/main.py +343 -136
  8. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/mcp/manager.py +9 -1
  9. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/prompts/system_prompt.md +15 -3
  10. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/session.py +67 -4
  11. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/shell_exec.py +116 -19
  12. {abyss_cli-0.1.4 → abyss_cli-0.1.6/src/abyss_cli.egg-info}/PKG-INFO +1 -1
  13. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss_cli.egg-info/SOURCES.txt +10 -1
  14. abyss_cli-0.1.6/test/test_custom_cmd_completion.py +46 -0
  15. abyss_cli-0.1.6/test/test_init_dirs.py +79 -0
  16. abyss_cli-0.1.6/tests/test_ansi_menu_cjk.py +114 -0
  17. abyss_cli-0.1.6/tests/test_file_spinner.py +137 -0
  18. abyss_cli-0.1.6/tests/test_interrupt.py +71 -0
  19. abyss_cli-0.1.6/tests/test_mcp_manager.py +76 -0
  20. abyss_cli-0.1.6/tests/test_shell_exec.py +112 -0
  21. abyss_cli-0.1.6/tests/test_tool_calls_spinner.py +144 -0
  22. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/MANIFEST.in +0 -0
  23. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/setup.cfg +0 -0
  24. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/api_client.py +0 -0
  25. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/commands/__init__.py +0 -0
  26. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/commands/slash.py +0 -0
  27. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/extensions/__init__.py +0 -0
  28. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/extensions/cli.py +0 -0
  29. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/extensions/installer.py +0 -0
  30. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/extensions/registry.py +0 -0
  31. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/extensions/url_parser.py +0 -0
  32. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/hooks/__init__.py +0 -0
  33. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/hooks/runner.py +0 -0
  34. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/logger.py +0 -0
  35. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/mcp/__init__.py +0 -0
  36. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/prompts/__init__.py +0 -0
  37. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/prompts/methodologies.py +0 -0
  38. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/runtime/__init__.py +0 -0
  39. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/runtime/host.py +0 -0
  40. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/skills/__init__.py +0 -0
  41. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/skills/loader.py +0 -0
  42. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/subagent/__init__.py +0 -0
  43. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/subagent/runner.py +0 -0
  44. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/__init__.py +0 -0
  45. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/base.py +0 -0
  46. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/file_edit.py +0 -0
  47. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/file_read.py +0 -0
  48. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/file_write.py +0 -0
  49. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/registry.py +0 -0
  50. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/spawn_subagents.py +0 -0
  51. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/web_fetch.py +0 -0
  52. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss/tools/web_search.py +0 -0
  53. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss_cli.egg-info/dependency_links.txt +0 -0
  54. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss_cli.egg-info/entry_points.txt +0 -0
  55. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss_cli.egg-info/requires.txt +0 -0
  56. {abyss_cli-0.1.4 → abyss_cli-0.1.6}/src/abyss_cli.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: abyss-cli
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: 终端AI开发助手
5
5
  Author: Abyss Team
6
6
  Requires-Python: >=3.9
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "abyss-cli"
7
- version = "0.1.4"
7
+ version = "0.1.6"
8
8
  description = "终端AI开发助手"
9
9
  authors = [{name = "Abyss Team"}]
10
10
  requires-python = ">=3.9"
@@ -1,4 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  """Abyss - 终端AI开发助手"""
3
- __version__ = "0.1.4"
3
+ __version__ = "0.1.6"
4
4
 
@@ -37,6 +37,7 @@ COMMANDS = [
37
37
  ("/skills", "列出已加载的 Skill"),
38
38
  ("/mcp", "列出 MCP server 和工具"),
39
39
  ("/hooks", "列出已配置的 Hook"),
40
+ ("/session", "会话管理(保存/加载/删除)"),
40
41
  ("/diagnose", "启动调试纪律流程"),
41
42
  ("/tdd", "启动 TDD 红-绿-重构流程"),
42
43
  ("/review", "审查当前 diff 的过度工程"),
@@ -46,6 +47,19 @@ COMMANDS = [
46
47
  ("/domain", "领域建模,提炼术语和决策"),
47
48
  ]
48
49
 
50
+ # 用户自定义命令(从 ~/.abyss/commands/*.md 加载,启动时注入)
51
+ _CUSTOM_COMMANDS: List[Tuple[str, str]] = []
52
+
53
+
54
+ def register_custom_commands(names: List[str]) -> None:
55
+ """注册用户自定义 slash 命令名到补全列表(去重,避免与内置命令重复)"""
56
+ builtin = {name for name, _ in COMMANDS}
57
+ _CUSTOM_COMMANDS.clear()
58
+ for n in names:
59
+ full = f"/{n}"
60
+ if full not in builtin:
61
+ _CUSTOM_COMMANDS.append((full, "自定义命令"))
62
+
49
63
 
50
64
  # ─── 输入历史(↑↓ 翻看用)────────────────────────────────
51
65
  # 模块级变量,跨多次 ansi_prompt 调用共享。
@@ -105,7 +119,7 @@ def get_file_completions(partial: str) -> List[Tuple[str, str]]:
105
119
 
106
120
  def get_command_completions(partial: str) -> List[Tuple[str, str]]:
107
121
  results = []
108
- for cmd_name, cmd_desc in COMMANDS:
122
+ for cmd_name, cmd_desc in COMMANDS + _CUSTOM_COMMANDS:
109
123
  if _fuzzy_match(partial, cmd_name):
110
124
  results.append((cmd_name, cmd_desc))
111
125
  return results
@@ -128,6 +142,36 @@ def find_at_context(text_before_cursor: str) -> Optional[Tuple[int, str]]:
128
142
  return last_at, partial
129
143
 
130
144
 
145
+ # ─── 显示宽度计算(CJK 字符占 2 列)──────────────────────
146
+ def _display_width(s: str) -> int:
147
+ """计算字符串在终端中的显示列宽。
148
+
149
+ CJK 字符(中文、日文、韩文等)在终端中通常占 2 列,
150
+ ASCII 字符占 1 列。此函数用于正确计算视觉行数。
151
+ """
152
+ w = 0
153
+ for ch in s:
154
+ cp = ord(ch)
155
+ if (
156
+ 0x1100 <= cp <= 0x115F or # Hangul Jamo
157
+ 0x2E80 <= cp <= 0xA4CF or # CJK Radicals .. Yi
158
+ 0xAC00 <= cp <= 0xD7A3 or # Hangul Syllables
159
+ 0xF900 <= cp <= 0xFAFF or # CJK Compatibility Ideographs
160
+ 0xFE10 <= cp <= 0xFE19 or # Vertical forms
161
+ 0xFE30 <= cp <= 0xFE6F or # CJK Compatibility Forms
162
+ 0xFF00 <= cp <= 0xFF60 or # Fullwidth Forms
163
+ 0xFFE0 <= cp <= 0xFFE6 or # Fullwidth Signs
164
+ 0x1F300 <= cp <= 0x1F64F or # Emoticons
165
+ 0x1F900 <= cp <= 0x1F9FF or # Supplemental Symbols
166
+ 0x20000 <= cp <= 0x2FFFD or # CJK Extension B+
167
+ 0x30000 <= cp <= 0x3FFFD # CJK Extension G+
168
+ ):
169
+ w += 2
170
+ else:
171
+ w += 1
172
+ return w
173
+
174
+
131
175
  # ─── ANSI 颜色工具 ─────────────────────────────────────
132
176
  def ansi_white(s: str) -> str:
133
177
  return f"\x1b[97m{s}\x1b[0m"
@@ -161,6 +205,40 @@ def ansi_clear_line() -> str:
161
205
  return "\r\x1b[K"
162
206
 
163
207
 
208
+ def _clear_visual_lines(text: str):
209
+ """清除 text 在当前终端宽度下占据的所有视觉行。
210
+ 先 \r 回到当前行行首再上移,确保清除位置正确。\n 执行后光标回到第一视觉行的行首。
211
+
212
+ 使用 _display_width() 计算显示列宽(CJK 占 2 列),而非 len()。
213
+ """
214
+ content_len = _display_width(text)
215
+ if content_len <= 0:
216
+ sys.stdout.write('\r\x1b[K')
217
+ return
218
+ try:
219
+ term_width = os.get_terminal_size().columns
220
+ except (OSError, AttributeError):
221
+ term_width = 80
222
+ visual_lines = max(1, (content_len + term_width - 1) // term_width)
223
+ # up_lines = content_len // term_width 覆盖 visual_lines=1 但内容恰好填满一行、
224
+ # 光标已换行的情况(此时 up_lines=1,仍需上移 + 清除上一行)。
225
+ up_lines = content_len // term_width
226
+ if visual_lines <= 1 and up_lines == 0:
227
+ sys.stdout.write('\r\x1b[K')
228
+ return
229
+ # \r 回到当前视觉行行首,再往上移 up_lines 行到第一视觉行。
230
+ # 不能用 visual_lines-1:当 L 恰为 tw 整数倍时,光标在 visual_lines+1 行。
231
+ sys.stdout.write('\r')
232
+ sys.stdout.write(ansi_move_up(up_lines))
233
+ # 逐行清除
234
+ for i in range(visual_lines):
235
+ sys.stdout.write('\r\x1b[K')
236
+ if i < visual_lines - 1:
237
+ sys.stdout.write('\x1b[B')
238
+ # 回到第一视觉行行首
239
+ sys.stdout.write(ansi_move_up(visual_lines - 1))
240
+
241
+
164
242
  def ansi_erase_lines_below(count: int) -> str:
165
243
  out = ""
166
244
  for _ in range(count):
@@ -268,6 +346,7 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
268
346
  buf: list = list(prompt_str)
269
347
  cursor_pos: int = len(buf)
270
348
  prev_line_len: int = len(buf)
349
+ prev_term_width: int = 0 # 追踪终端宽度变化(0 = 首次强制全刷新)
271
350
  completions: List[Tuple[str, str]] = []
272
351
  selected_idx: int = 0
273
352
  showing_menu: bool = False
@@ -278,22 +357,77 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
278
357
  history_idx: int = -1
279
358
  history_draft: str = "" # 进入导航前保存的当前 buffer,便于 ↓ 回到
280
359
 
360
+ def get_term_width():
361
+ try:
362
+ return os.get_terminal_size().columns
363
+ except (OSError, AttributeError):
364
+ return 80
365
+
366
+ def _clear_and_redraw(clear_visuals: int, up: int):
367
+ """原子操作:上移 up 行 → 清除 clear_visuals 行 → 写新内容。
368
+ 用 \x1b[J 兜底清除光标以下残留(窗口大小改变时终端自动重排可能留下散落字符)。"""
369
+ sys.stdout.write('\r')
370
+ if up > 0:
371
+ sys.stdout.write(ansi_move_up(up))
372
+ for i in range(clear_visuals):
373
+ sys.stdout.write('\r\x1b[K')
374
+ if i < clear_visuals - 1:
375
+ sys.stdout.write('\x1b[B')
376
+ # 兜底:清除光标以下的残余(窗口 resize 后终端重排可能留下额外行)
377
+ sys.stdout.write('\x1b[J')
378
+ if clear_visuals > 0:
379
+ sys.stdout.write(ansi_move_up(clear_visuals - 1))
380
+ line = ''.join(buf)
381
+ sys.stdout.write(line)
382
+ sys.stdout.flush()
383
+
281
384
  def redraw_line():
282
385
  """重绘当前输入行。
283
386
  关键:必须 \r\x1b[K(回行首 + 清行尾),不能只 \r。
284
387
  否则本行原本有内容(如上一轮的 ',请重试)' 残留)会被新输入部分覆盖,
285
388
  用户看到 '> /show-reasoning,请重试)' 这种新旧混杂的乱象。
389
+
390
+ 使用 _display_width() 计算显示列宽(CJK 占 2 列),而非 len()。
286
391
  """
287
- nonlocal cursor_pos, buf, prev_line_len
288
- # 回行首 + 清行尾:本行原内容(残留的 ',请重试)' 之类)彻底抹掉
289
- sys.stdout.write('\r\x1b[K')
392
+ nonlocal cursor_pos, buf, prev_line_len, prev_term_width
393
+ term_width = get_term_width()
394
+ width_changed = (prev_term_width != 0 and prev_term_width != term_width)
290
395
  line = ''.join(buf)
291
- sys.stdout.write(line)
292
- prev_line_len = len(buf)
293
- target_col = cursor_pos
294
- current_col = len(buf)
295
- if target_col < current_col:
296
- sys.stdout.write(ansi_move_left(current_col - target_col))
396
+ disp_w = _display_width(line) # 当前行的显示列宽
397
+ cursor_disp = _display_width(''.join(buf[:cursor_pos])) # 光标位置的显示列宽
398
+
399
+ if width_changed:
400
+ # 终端宽度变了 → Windows 已自动重排所有文字,prev_line_len 的假设全部失效。
401
+ # 改用旧宽度重新估算旧内容占的行数,保守清除(加 2 行安全余量)。
402
+ old_tw = prev_term_width if prev_term_width > 0 else term_width
403
+ safety_visuals = max(1, (prev_line_len + old_tw - 1) // old_tw) + 2
404
+ safety_up = prev_line_len // old_tw if prev_line_len > 0 else 0
405
+ _clear_and_redraw(safety_visuals, safety_up)
406
+ prev_term_width = term_width
407
+ prev_line_len = disp_w
408
+ if cursor_disp < disp_w:
409
+ sys.stdout.write(ansi_move_left(disp_w - cursor_disp))
410
+ sys.stdout.flush()
411
+ return
412
+
413
+ prev_term_width = term_width
414
+ # 回行首 + 清行尾:先清除之前占据的所有视觉行(含跨行包裹),
415
+ # 再写新内容。旧代码只清当前行(\r\x1b[K),导致输入换行时出现重复行。
416
+ old_visuals = max(1, (prev_line_len + term_width - 1) // term_width) if prev_line_len > 0 else 1
417
+ # 向上移动行数必须用 prev_line_len // term_width(整数除法),
418
+ # 不能用 old_visuals-1:当 prev_line_len 恰为 tw 整数倍时,
419
+ # 光标已自动换到下一行,old_visuals-1 少算一行 → 第一行永远残留。
420
+ up_lines = prev_line_len // term_width if prev_line_len > 0 else 0
421
+ if old_visuals > 1 or up_lines > 0:
422
+ # up_lines > 0 覆盖 old_visuals=1 但内容恰好填满一行、光标已换行的情况
423
+ _clear_and_redraw(old_visuals, up_lines)
424
+ else:
425
+ sys.stdout.write('\r\x1b[K')
426
+ sys.stdout.write(line)
427
+ sys.stdout.flush()
428
+ prev_line_len = disp_w
429
+ if cursor_disp < disp_w:
430
+ sys.stdout.write(ansi_move_left(disp_w - cursor_disp))
297
431
  sys.stdout.flush()
298
432
 
299
433
  def clear_menu_area():
@@ -310,20 +444,21 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
310
444
  sys.stdout.write('\x1b[B')
311
445
  # 上移回输入行
312
446
  sys.stdout.write(ansi_move_up(menu_row_count))
447
+ cursor_col = _display_width(''.join(buf[:cursor_pos])) % get_term_width()
313
448
  sys.stdout.write('\r')
314
- sys.stdout.write(ansi_move_right(cursor_pos))
449
+ sys.stdout.write(ansi_move_right(cursor_col))
315
450
  sys.stdout.flush()
316
451
  menu_row_count = 0
317
452
 
318
453
  def hide_menu():
319
- nonlocal showing_menu, menu_row_count, max_rows_ever, completions, selected_idx
454
+ nonlocal showing_menu, menu_row_count, completions, selected_idx
320
455
  # 用 menu_row_count 判断是否有菜单需要清除(showing_menu 可能已被 update_completions 置 False)
321
456
  if menu_row_count > 0:
322
457
  clear_menu_area()
323
458
  showing_menu = False
324
459
  completions = []
325
460
  selected_idx = 0
326
- max_rows_ever = 0 # 重置,下次激活重新滚动创建空间
461
+ # 不重置 max_rows_ever:保留已分配的空间,避免 hide→show 循环不断追加 \n 导致终端滚动错乱
327
462
 
328
463
  def show_menu():
329
464
  nonlocal showing_menu, menu_row_count, completions, selected_idx, max_rows_ever
@@ -344,7 +479,7 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
344
479
  sys.stdout.write('\n')
345
480
  sys.stdout.write(ansi_move_up(extra))
346
481
  max_rows_ever = visible_count
347
- # 菜单已存在:清除旧内容(在已有空间内,用 \x1b[B 避免额外滚动)
482
+ # 菜单正在显示:在已有空间内清除旧内容(用 \x1b[B 避免额外滚动)
348
483
  elif menu_row_count > 0:
349
484
  sys.stdout.write('\x1b[B')
350
485
  for i in range(menu_row_count):
@@ -352,11 +487,15 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
352
487
  if i < menu_row_count - 1:
353
488
  sys.stdout.write('\x1b[B')
354
489
  sys.stdout.write(ansi_move_up(menu_row_count))
490
+ cursor_col = _display_width(''.join(buf[:cursor_pos])) % get_term_width()
355
491
  sys.stdout.write('\r')
356
- sys.stdout.write(ansi_move_right(cursor_pos))
492
+ sys.stdout.write(ansi_move_right(cursor_col))
493
+ # 菜单曾隐藏(menu_row_count=0)但空间尚存(max_rows_ever>0):
494
+ # clear_menu_area() 在 hide 时已清空该空间,无需再次清除,直接绘制即可
357
495
 
358
- # 重绘输入行
359
- sys.stdout.write('\r\x1b[K' + ''.join(buf))
496
+ # 重绘输入行(先清除之前占据的所有视觉行,含跨行包裹)
497
+ _clear_visual_lines(''.join(buf))
498
+ sys.stdout.write(''.join(buf))
360
499
 
361
500
  # 绘制菜单项(在已有空间内,用 \x1b[B)
362
501
  sys.stdout.write('\x1b[B')
@@ -374,10 +513,20 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
374
513
  if i < visible_count - 1:
375
514
  sys.stdout.write('\x1b[B')
376
515
 
377
- # 上移回输入行
516
+ # 上移回输入行(光标当前在 buf 末尾,需导航到 cursor_pos)
378
517
  sys.stdout.write(ansi_move_up(visible_count))
518
+ # 计算从 buf 末尾到 cursor_pos 的视觉行/列偏移(使用显示列宽)
519
+ tw = get_term_width()
520
+ disp_w = _display_width(''.join(buf))
521
+ cursor_disp = _display_width(''.join(buf[:cursor_pos]))
522
+ end_row = disp_w // tw
523
+ end_col = disp_w % tw
524
+ cur_row = cursor_disp // tw
525
+ cur_col = cursor_disp % tw
526
+ if cur_row < end_row:
527
+ sys.stdout.write(ansi_move_up(end_row - cur_row))
379
528
  sys.stdout.write('\r')
380
- sys.stdout.write(ansi_move_right(cursor_pos))
529
+ sys.stdout.write(ansi_move_right(cur_col))
381
530
  sys.stdout.flush()
382
531
 
383
532
  menu_row_count = visible_count
@@ -30,6 +30,8 @@ class Config:
30
30
  "user_id": "default",
31
31
  "max_retries": 3,
32
32
  "retry_delay": 1.0,
33
+ "auto_save_interval": 30,
34
+ "tool_result_max_chars": 4000,
33
35
  }
34
36
 
35
37
  MODELS = {
@@ -119,4 +121,28 @@ class Config:
119
121
  def get_model_info(self) -> dict:
120
122
  """获取当前模型信息"""
121
123
  model = self.get("model")
122
- return self.MODELS.get(model, {})
124
+ return self.MODELS.get(model, {})
125
+
126
+
127
+ # ---- 子目录骨架 ----
128
+ _SKEL_DIRS = ["skills", "hooks", "commands", "logs", "sessions"]
129
+ SESSION_DIR = CONFIG_DIR / "sessions"
130
+ _HOOK_EVENT_DIRS = ["PreToolUse", "PostToolUse"]
131
+
132
+
133
+ def init_user_dirs(base_dir: Path = None) -> None:
134
+ """首次启动时创建 ~/.abyss/ 骨架目录和 mcp.json 模板。
135
+ 幂等:目录已存在不报错,mcp.json 已存在不覆盖。
136
+ """
137
+ base = base_dir or CONFIG_DIR
138
+ base.mkdir(parents=True, exist_ok=True)
139
+ for sub in _SKEL_DIRS:
140
+ (base / sub).mkdir(exist_ok=True)
141
+ for ev in _HOOK_EVENT_DIRS:
142
+ (base / "hooks" / ev).mkdir(exist_ok=True)
143
+ mcp_file = base / "mcp.json"
144
+ if not mcp_file.exists():
145
+ mcp_file.write_text(
146
+ json.dumps({"mcp_servers": []}, indent=2, ensure_ascii=False),
147
+ encoding="utf-8",
148
+ )
@@ -0,0 +1,53 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Esc 键中断监听器
4
+ 任务执行期间后台监听 Esc 键,主线程在检查点轮询取消标志。
5
+ """
6
+ import sys
7
+ import threading
8
+
9
+
10
+ class EscInterrupt:
11
+ """后台监听 Esc 键的中断信号源。
12
+
13
+ 任务开始时调用 start() 启动监听线程;
14
+ 主线程在检查点调用 is_cancelled() 轮询;
15
+ 任务结束调用 stop() 停止监听并重置状态。
16
+ """
17
+
18
+ def __init__(self):
19
+ self._cancel_event = threading.Event()
20
+ self._thread = None
21
+ self._running = False
22
+
23
+ def start(self):
24
+ """启动 Esc 监听线程(仅 Windows 有效)。"""
25
+ if sys.platform != "win32":
26
+ return
27
+ self._cancel_event.clear()
28
+ self._running = True
29
+ self._thread = threading.Thread(target=self._listen, daemon=True)
30
+ self._thread.start()
31
+
32
+ def stop(self):
33
+ """停止监听并重置取消标志。"""
34
+ self._running = False
35
+ self._cancel_event.clear()
36
+ # 监听线程在 getwch() 阻塞,daemon=True 会在主线程退出时自动结束
37
+
38
+ def is_cancelled(self) -> bool:
39
+ """主线程检查是否收到中断信号。"""
40
+ return self._cancel_event.is_set()
41
+
42
+ def _listen(self):
43
+ """后台线程:阻塞读 Esc 键。仅在 Windows 实现有效。"""
44
+ import msvcrt
45
+ while self._running:
46
+ try:
47
+ ch = msvcrt.getwch()
48
+ if ch == '\x1b': # Esc
49
+ self._cancel_event.set()
50
+ self._running = False
51
+ return
52
+ except (OSError, ValueError):
53
+ break