abyss-cli 0.1.3__tar.gz → 0.1.5__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 (108) hide show
  1. {abyss_cli-0.1.3/src/abyss_cli.egg-info → abyss_cli-0.1.5}/PKG-INFO +2 -1
  2. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/pyproject.toml +3 -2
  3. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/__init__.py +1 -1
  4. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/ansi_menu.py +230 -100
  5. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/config.py +24 -1
  6. abyss_cli-0.1.5/src/abyss/interrupt.py +53 -0
  7. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/main.py +346 -74
  8. abyss_cli-0.1.5/src/abyss/mcp/manager.py +268 -0
  9. abyss_cli-0.1.5/src/abyss/runtime/__init__.py +9 -0
  10. abyss_cli-0.1.5/src/abyss/runtime/host.py +180 -0
  11. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/session.py +5 -0
  12. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/shell_exec.py +116 -19
  13. {abyss_cli-0.1.3 → abyss_cli-0.1.5/src/abyss_cli.egg-info}/PKG-INFO +2 -1
  14. abyss_cli-0.1.5/src/abyss_cli.egg-info/SOURCES.txt +53 -0
  15. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss_cli.egg-info/requires.txt +1 -0
  16. abyss_cli-0.1.5/test/test_custom_cmd_completion.py +46 -0
  17. abyss_cli-0.1.5/test/test_init_dirs.py +79 -0
  18. abyss_cli-0.1.5/tests/test_file_spinner.py +137 -0
  19. abyss_cli-0.1.5/tests/test_interrupt.py +71 -0
  20. abyss_cli-0.1.5/tests/test_mcp_manager.py +76 -0
  21. abyss_cli-0.1.5/tests/test_shell_exec.py +112 -0
  22. abyss_cli-0.1.5/tests/test_tool_calls_spinner.py +144 -0
  23. abyss_cli-0.1.3/src/abyss/mcp/manager.py +0 -189
  24. abyss_cli-0.1.3/src/abyss_cli.egg-info/SOURCES.txt +0 -94
  25. abyss_cli-0.1.3/test/test_abyss_interactive.py +0 -125
  26. abyss_cli-0.1.3/test/test_acceptance_smoke.py +0 -302
  27. abyss_cli-0.1.3/test/test_ansi.py +0 -80
  28. abyss_cli-0.1.3/test/test_ansi2.py +0 -79
  29. abyss_cli-0.1.3/test/test_ansi3.py +0 -111
  30. abyss_cli-0.1.3/test/test_ansi_menu_max_visible.py +0 -80
  31. abyss_cli-0.1.3/test/test_ansi_prompt_clear_line.py +0 -124
  32. abyss_cli-0.1.3/test/test_ansi_prompt_history.py +0 -208
  33. abyss_cli-0.1.3/test/test_ansi_prompt_line_clear.py +0 -166
  34. abyss_cli-0.1.3/test/test_ansi_prompt_menu.py +0 -188
  35. abyss_cli-0.1.3/test/test_ansi_select.py +0 -270
  36. abyss_cli-0.1.3/test/test_api.py +0 -60
  37. abyss_cli-0.1.3/test/test_batch_execution_rule.py +0 -44
  38. abyss_cli-0.1.3/test/test_bug_reproduction.py +0 -182
  39. abyss_cli-0.1.3/test/test_config_delete.py +0 -125
  40. abyss_cli-0.1.3/test/test_consume_stream.py +0 -198
  41. abyss_cli-0.1.3/test/test_debug.py +0 -103
  42. abyss_cli-0.1.3/test/test_debug_position.py +0 -118
  43. abyss_cli-0.1.3/test/test_edge_cases.py +0 -204
  44. abyss_cli-0.1.3/test/test_execute_tool_arg_validation.py +0 -91
  45. abyss_cli-0.1.3/test/test_extension_cli.py +0 -272
  46. abyss_cli-0.1.3/test/test_extension_registry.py +0 -128
  47. abyss_cli-0.1.3/test/test_help_command.py +0 -117
  48. abyss_cli-0.1.3/test/test_hook_runner.py +0 -145
  49. abyss_cli-0.1.3/test/test_input_edge_cases.py +0 -81
  50. abyss_cli-0.1.3/test/test_mcp_manager.py +0 -191
  51. abyss_cli-0.1.3/test/test_methodologies.py +0 -70
  52. abyss_cli-0.1.3/test/test_methodology_table.py +0 -240
  53. abyss_cli-0.1.3/test/test_misc.py +0 -58
  54. abyss_cli-0.1.3/test/test_multi_round_ui.py +0 -124
  55. abyss_cli-0.1.3/test/test_no_tight_polling.py +0 -26
  56. abyss_cli-0.1.3/test/test_package_installer.py +0 -435
  57. abyss_cli-0.1.3/test/test_reasoning_only_bug.py +0 -92
  58. abyss_cli-0.1.3/test/test_rich_completer.py +0 -295
  59. abyss_cli-0.1.3/test/test_session_compress.py +0 -100
  60. abyss_cli-0.1.3/test/test_shell.py +0 -30
  61. abyss_cli-0.1.3/test/test_shell_chcp_leak.py +0 -63
  62. abyss_cli-0.1.3/test/test_shell_retry_encoding.py +0 -81
  63. abyss_cli-0.1.3/test/test_shell_safety.py +0 -65
  64. abyss_cli-0.1.3/test/test_shell_timeout_fix.py +0 -155
  65. abyss_cli-0.1.3/test/test_show_reasoning_persist.py +0 -101
  66. abyss_cli-0.1.3/test/test_skill_loader.py +0 -162
  67. abyss_cli-0.1.3/test/test_slash_commands.py +0 -125
  68. abyss_cli-0.1.3/test/test_slash_methodology.py +0 -142
  69. abyss_cli-0.1.3/test/test_spawn_subagents_tool.py +0 -85
  70. abyss_cli-0.1.3/test/test_subagent.py +0 -195
  71. abyss_cli-0.1.3/test/test_subagent_methodology.py +0 -102
  72. abyss_cli-0.1.3/test/test_tool_registry.py +0 -176
  73. abyss_cli-0.1.3/test/test_unbounded_rounds.py +0 -45
  74. abyss_cli-0.1.3/test/test_url_parser.py +0 -107
  75. abyss_cli-0.1.3/test/test_web_search_rules.py +0 -54
  76. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/MANIFEST.in +0 -0
  77. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/setup.cfg +0 -0
  78. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/api_client.py +0 -0
  79. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/commands/__init__.py +0 -0
  80. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/commands/slash.py +0 -0
  81. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/extensions/__init__.py +0 -0
  82. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/extensions/cli.py +0 -0
  83. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/extensions/installer.py +0 -0
  84. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/extensions/registry.py +0 -0
  85. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/extensions/url_parser.py +0 -0
  86. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/hooks/__init__.py +0 -0
  87. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/hooks/runner.py +0 -0
  88. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/logger.py +0 -0
  89. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/mcp/__init__.py +0 -0
  90. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/prompts/__init__.py +0 -0
  91. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/prompts/methodologies.py +0 -0
  92. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/prompts/system_prompt.md +0 -0
  93. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/skills/__init__.py +0 -0
  94. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/skills/loader.py +0 -0
  95. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/subagent/__init__.py +0 -0
  96. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/subagent/runner.py +0 -0
  97. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/__init__.py +0 -0
  98. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/base.py +0 -0
  99. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/file_edit.py +0 -0
  100. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/file_read.py +0 -0
  101. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/file_write.py +0 -0
  102. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/registry.py +0 -0
  103. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/spawn_subagents.py +0 -0
  104. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/web_fetch.py +0 -0
  105. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss/tools/web_search.py +0 -0
  106. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss_cli.egg-info/dependency_links.txt +0 -0
  107. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/src/abyss_cli.egg-info/entry_points.txt +0 -0
  108. {abyss_cli-0.1.3 → abyss_cli-0.1.5}/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.3
3
+ Version: 0.1.5
4
4
  Summary: 终端AI开发助手
5
5
  Author: Abyss Team
6
6
  Requires-Python: >=3.9
@@ -9,3 +9,4 @@ Requires-Dist: click>=8.0.0
9
9
  Requires-Dist: prompt-toolkit>=3.0.0
10
10
  Requires-Dist: ddgs>=9.0.0
11
11
  Requires-Dist: httpx>=0.20.0
12
+ Requires-Dist: mcp>=1.0.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "abyss-cli"
7
- version = "0.1.3"
7
+ version = "0.1.5"
8
8
  description = "终端AI开发助手"
9
9
  authors = [{name = "Abyss Team"}]
10
10
  requires-python = ">=3.9"
@@ -14,6 +14,7 @@ dependencies = [
14
14
  "prompt-toolkit>=3.0.0",
15
15
  "ddgs>=9.0.0",
16
16
  "httpx>=0.20.0",
17
+ "mcp>=1.0.0",
17
18
  ]
18
19
 
19
20
  [project.scripts]
@@ -25,7 +26,7 @@ packages = [
25
26
  "abyss", "abyss.tools", "abyss.prompts",
26
27
  "abyss.commands", "abyss.extensions",
27
28
  "abyss.hooks", "abyss.mcp", "abyss.skills",
28
- "abyss.subagent",
29
+ "abyss.subagent", "abyss.runtime",
29
30
  ]
30
31
  include-package-data = true
31
32
 
@@ -1,4 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  """Abyss - 终端AI开发助手"""
3
- __version__ = "0.1.2"
3
+ __version__ = "0.1.5"
4
4
 
@@ -34,6 +34,9 @@ COMMANDS = [
34
34
  ("/model", "查看/切换模型"),
35
35
  ("/thinking", "查看/开关思考模式"),
36
36
  ("/show-reasoning", "开关思考过程显示"),
37
+ ("/skills", "列出已加载的 Skill"),
38
+ ("/mcp", "列出 MCP server 和工具"),
39
+ ("/hooks", "列出已配置的 Hook"),
37
40
  ("/diagnose", "启动调试纪律流程"),
38
41
  ("/tdd", "启动 TDD 红-绿-重构流程"),
39
42
  ("/review", "审查当前 diff 的过度工程"),
@@ -43,12 +46,28 @@ COMMANDS = [
43
46
  ("/domain", "领域建模,提炼术语和决策"),
44
47
  ]
45
48
 
49
+ # 用户自定义命令(从 ~/.abyss/commands/*.md 加载,启动时注入)
50
+ _CUSTOM_COMMANDS: List[Tuple[str, str]] = []
51
+
52
+
53
+ def register_custom_commands(names: List[str]) -> None:
54
+ """注册用户自定义 slash 命令名到补全列表(去重,避免与内置命令重复)"""
55
+ builtin = {name for name, _ in COMMANDS}
56
+ _CUSTOM_COMMANDS.clear()
57
+ for n in names:
58
+ full = f"/{n}"
59
+ if full not in builtin:
60
+ _CUSTOM_COMMANDS.append((full, "自定义命令"))
61
+
46
62
 
47
63
  # ─── 输入历史(↑↓ 翻看用)────────────────────────────────
48
64
  # 模块级变量,跨多次 ansi_prompt 调用共享。
49
65
  HISTORY: list = []
50
66
  HISTORY_MAX = 100
51
67
 
68
+ # ─── 粘贴序号 ─────────────────────────────────────────
69
+ _PASTE_ID: int = 0
70
+
52
71
 
53
72
  # ─── 补全菜单最大可见行数(根据终端高度动态计算)────────────
54
73
  # 旧版本写死 8,COMMANDS 列表有 14 个 → 后 6 个被截断看不见。
@@ -102,7 +121,7 @@ def get_file_completions(partial: str) -> List[Tuple[str, str]]:
102
121
 
103
122
  def get_command_completions(partial: str) -> List[Tuple[str, str]]:
104
123
  results = []
105
- for cmd_name, cmd_desc in COMMANDS:
124
+ for cmd_name, cmd_desc in COMMANDS + _CUSTOM_COMMANDS:
106
125
  if _fuzzy_match(partial, cmd_name):
107
126
  results.append((cmd_name, cmd_desc))
108
127
  return results
@@ -248,10 +267,12 @@ def ansi_select(prompt: str, options: list, default_idx: int = 0) -> "str | None
248
267
  # ── 主输入函数 ────────────────────────────────────────────
249
268
  def ansi_prompt(prompt_str: str = "> ") -> str:
250
269
  """
251
- 自定义输入循环,支持 / 和 @ 补全菜单(ANSI 渲染)。
252
- 返回用户输入的字符串。
270
+ 自定义输入循环,支持 / 和 @ 补全菜单(ANSI 渲染)、Claude 风格多行粘贴。
271
+ 粘贴多行文本时显示 [Pasted text #N +M lines],不展开渲染。
253
272
  Ctrl+C 抛出 KeyboardInterrupt。
254
273
  """
274
+ global _PASTE_ID
275
+
255
276
  # Windows: 清空控制台输入缓冲,避免上次输出污染导致首次按键被吞
256
277
  if sys.platform == "win32":
257
278
  try:
@@ -262,31 +283,38 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
262
283
  except Exception:
263
284
  pass
264
285
 
286
+ # 启用括号粘贴模式(Bracketed Paste Mode):粘贴内容用 \x1b[200~ ... \x1b[201~] 包裹
287
+ sys.stdout.write('\x1b[?2004h')
288
+ sys.stdout.flush()
289
+
290
+ prompt_len: int = len(prompt_str)
265
291
  buf: list = list(prompt_str)
266
292
  cursor_pos: int = len(buf)
267
- prev_line_len: int = len(buf)
268
293
  completions: List[Tuple[str, str]] = []
269
294
  selected_idx: int = 0
270
295
  showing_menu: bool = False
271
- max_visible: int = compute_max_visible() # 根据终端高度动态算,避免截断
296
+ max_visible: int = compute_max_visible()
272
297
  menu_row_count: int = 0
273
- max_rows_ever: int = 0 # 追踪渲染过的最大行数,确保清除干净
274
- # 历史翻看:history_idx = -1 表示不在导航态(停在草稿)
298
+ max_rows_ever: int = 0
275
299
  history_idx: int = -1
276
- history_draft: str = "" # 进入导航前保存的当前 buffer,便于 ↓ 回到
300
+ history_draft: str = ""
301
+ in_bracket_paste: bool = False
302
+ paste_text: str = ""
303
+ paste_texts: list = []
304
+ esc_seq: list = []
305
+ last_key_time: float = 0.0
306
+ fast_paste_chars: int = 0
307
+ PASTE_THRESHOLD_MS: float = 30.0
308
+ PASTE_EXIT_MS: float = 150.0
309
+
310
+ def is_pasting() -> bool:
311
+ return in_bracket_paste or fast_paste_chars >= 2
277
312
 
278
313
  def redraw_line():
279
- """重绘当前输入行。
280
- 关键:必须 \r\x1b[K(回行首 + 清行尾),不能只 \r。
281
- 否则本行原本有内容(如上一轮的 ',请重试)' 残留)会被新输入部分覆盖,
282
- 用户看到 '> /show-reasoning,请重试)' 这种新旧混杂的乱象。
283
- """
284
- nonlocal cursor_pos, buf, prev_line_len
285
- # 回行首 + 清行尾:本行原内容(残留的 ',请重试)' 之类)彻底抹掉
314
+ nonlocal cursor_pos
286
315
  sys.stdout.write('\r\x1b[K')
287
316
  line = ''.join(buf)
288
317
  sys.stdout.write(line)
289
- prev_line_len = len(buf)
290
318
  target_col = cursor_pos
291
319
  current_col = len(buf)
292
320
  if target_col < current_col:
@@ -294,18 +322,13 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
294
322
  sys.stdout.flush()
295
323
 
296
324
  def clear_menu_area():
297
- """清除菜单区域的所有行,光标回到输入行 cursor_pos 位置。
298
- 只在已有菜单空间内操作(假定 show_menu 已用 \\n 创建了空间),使用 \\x1b[B 避免额外滚动。
299
- """
300
325
  nonlocal menu_row_count
301
326
  if menu_row_count > 0:
302
- # \x1b[B 到菜单首行(空间已由 show_menu 首次激活时创建)
303
327
  sys.stdout.write('\x1b[B')
304
328
  for i in range(menu_row_count):
305
329
  sys.stdout.write('\r\x1b[K')
306
330
  if i < menu_row_count - 1:
307
331
  sys.stdout.write('\x1b[B')
308
- # 上移回输入行
309
332
  sys.stdout.write(ansi_move_up(menu_row_count))
310
333
  sys.stdout.write('\r')
311
334
  sys.stdout.write(ansi_move_right(cursor_pos))
@@ -314,34 +337,29 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
314
337
 
315
338
  def hide_menu():
316
339
  nonlocal showing_menu, menu_row_count, max_rows_ever, completions, selected_idx
317
- # 用 menu_row_count 判断是否有菜单需要清除(showing_menu 可能已被 update_completions 置 False)
318
340
  if menu_row_count > 0:
319
341
  clear_menu_area()
320
342
  showing_menu = False
321
343
  completions = []
322
344
  selected_idx = 0
323
- max_rows_ever = 0 # 重置,下次激活重新滚动创建空间
345
+ max_rows_ever = 0
324
346
 
325
347
  def show_menu():
326
348
  nonlocal showing_menu, menu_row_count, completions, selected_idx, max_rows_ever
327
349
  if not completions:
328
350
  hide_menu()
329
351
  return
330
-
331
352
  showing_menu = True
332
353
  visible_count = min(len(completions), max_visible)
333
354
  start_idx = 0
334
355
  if selected_idx >= max_visible:
335
356
  start_idx = selected_idx - max_visible + 1
336
-
337
- # 仅在需要更多空间时滚动(首次激活或匹配项增多)
338
357
  if visible_count > max_rows_ever:
339
358
  extra = visible_count - max_rows_ever
340
359
  for _ in range(extra):
341
360
  sys.stdout.write('\n')
342
361
  sys.stdout.write(ansi_move_up(extra))
343
362
  max_rows_ever = visible_count
344
- # 菜单已存在:清除旧内容(在已有空间内,用 \x1b[B 避免额外滚动)
345
363
  elif menu_row_count > 0:
346
364
  sys.stdout.write('\x1b[B')
347
365
  for i in range(menu_row_count):
@@ -351,11 +369,7 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
351
369
  sys.stdout.write(ansi_move_up(menu_row_count))
352
370
  sys.stdout.write('\r')
353
371
  sys.stdout.write(ansi_move_right(cursor_pos))
354
-
355
- # 重绘输入行
356
372
  sys.stdout.write('\r\x1b[K' + ''.join(buf))
357
-
358
- # 绘制菜单项(在已有空间内,用 \x1b[B)
359
373
  sys.stdout.write('\x1b[B')
360
374
  for i in range(visible_count):
361
375
  idx = start_idx + i
@@ -370,19 +384,15 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
370
384
  sys.stdout.write('\r\x1b[K' + line)
371
385
  if i < visible_count - 1:
372
386
  sys.stdout.write('\x1b[B')
373
-
374
- # 上移回输入行
375
387
  sys.stdout.write(ansi_move_up(visible_count))
376
388
  sys.stdout.write('\r')
377
389
  sys.stdout.write(ansi_move_right(cursor_pos))
378
390
  sys.stdout.flush()
379
-
380
391
  menu_row_count = visible_count
381
392
 
382
393
  def update_completions():
383
394
  nonlocal completions, showing_menu, selected_idx
384
- text_before = ''.join(buf[len(prompt_str):cursor_pos])
385
-
395
+ text_before = ''.join(buf[prompt_len:cursor_pos])
386
396
  at_ctx = find_at_context(text_before)
387
397
  if at_ctx:
388
398
  _, partial = at_ctx
@@ -396,7 +406,6 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
396
406
  completions = []
397
407
  showing_menu = False
398
408
  return
399
-
400
409
  if text_before.startswith('/'):
401
410
  new_comps = get_command_completions(text_before)
402
411
  if new_comps:
@@ -408,53 +417,189 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
408
417
  completions = []
409
418
  showing_menu = False
410
419
  return
411
-
412
420
  completions = []
413
421
  showing_menu = False
414
422
 
415
- # 打印初始提示符
416
- sys.stdout.write(prompt_str)
423
+ def submit() -> str:
424
+ global _PASTE_ID
425
+ hide_menu()
426
+ result = ''.join(buf[prompt_len:])
427
+ if paste_text:
428
+ result += paste_text
429
+ for pt in paste_texts:
430
+ result += pt
431
+ if result and (not HISTORY or HISTORY[-1] != result):
432
+ HISTORY.append(result)
433
+ if len(HISTORY) > HISTORY_MAX:
434
+ HISTORY.pop(0)
435
+ sys.stdout.write('\x1b[?2004l')
436
+ sys.stdout.write('\n')
437
+ sys.stdout.flush()
438
+ return result
439
+
440
+ def flush_paste():
441
+ """粘贴结束,把 paste_text 存入 paste_texts 并显示摘要。不插入 buf。"""
442
+ global _PASTE_ID
443
+ nonlocal paste_text
444
+ if not paste_text.strip():
445
+ paste_text = ""
446
+ return
447
+ _PASTE_ID += 1
448
+ newline_count = paste_text.count('\n')
449
+ if newline_count == 0:
450
+ prefix = f" {ansi_dim(f'[Pasted text #{_PASTE_ID}]')}"
451
+ else:
452
+ prefix = f" {ansi_dim(f'[Pasted text #{_PASTE_ID} +{newline_count} lines]')}"
453
+ paste_texts.append(paste_text)
454
+ paste_text = ""
455
+ hide_menu()
456
+ redraw_line()
457
+ sys.stdout.write('\n' + prefix + '\n')
458
+ sys.stdout.write('\r\x1b[K' + ''.join(buf))
459
+ sys.stdout.flush()
460
+
461
+ def handle_enter():
462
+ nonlocal fast_paste_chars, cursor_pos
463
+ if showing_menu and completions:
464
+ sel_text = completions[selected_idx][0]
465
+ text_before = ''.join(buf[prompt_len:cursor_pos])
466
+ at_ctx = find_at_context(text_before)
467
+ line_start_pos = cursor_pos - len(text_before)
468
+ if at_ctx:
469
+ start = line_start_pos + at_ctx[0]
470
+ elif text_before.startswith('/'):
471
+ start = line_start_pos
472
+ else:
473
+ start = line_start_pos
474
+ buf[start:cursor_pos] = list(sel_text)
475
+ cursor_pos = start + len(sel_text)
476
+ hide_menu()
477
+ redraw_line()
478
+ return
479
+ if is_pasting():
480
+ return
481
+ if paste_text:
482
+ return
483
+ return submit()
484
+
485
+ sys.stdout.write('\r\x1b[K' + prompt_str)
417
486
  sys.stdout.flush()
418
487
 
419
488
  while True:
420
- # 直接阻塞读取 msvcrt.getwch(),不要用 kbhit() + 紧密轮询。
421
- # 5ms 紧密轮询会和 Windows 中文 IME 冲突,导致 IME 提交字符后
422
- # 程序仍然读不到 Enter,从而"按回车无反应、卡死"。
423
- # getwch() 在 Windows 上 Ctrl+C 不会自动抛异常,需靠 \x03 字符判断(已处理)。
424
489
  try:
425
490
  key = msvcrt.getwch()
426
491
  except (OSError, ValueError):
427
492
  time.sleep(0.01)
428
493
  continue
429
494
 
430
- # 特殊键(两字节序列)
495
+ now = time.time()
496
+ delta_ms = (now - last_key_time) * 1000 if last_key_time > 0 else 1000.0
497
+ last_key_time = now
498
+
499
+ if not in_bracket_paste and delta_ms < PASTE_THRESHOLD_MS:
500
+ fast_paste_chars += 1
501
+ if fast_paste_chars >= 2 and not paste_text:
502
+ n = fast_paste_chars
503
+ while n > 0 and cursor_pos > prompt_len:
504
+ cursor_pos -= 1
505
+ paste_text = buf.pop(cursor_pos) + paste_text
506
+ n -= 1
507
+ elif delta_ms > PASTE_EXIT_MS and not in_bracket_paste:
508
+ if fast_paste_chars >= 2 and paste_text:
509
+ flush_paste()
510
+ fast_paste_chars = 0
511
+
512
+ if esc_seq:
513
+ esc_seq.append(key)
514
+ seq_str = ''.join(esc_seq)
515
+ if seq_str == '\x1b[200~':
516
+ in_bracket_paste = True
517
+ esc_seq = []
518
+ continue
519
+ elif seq_str == '\x1b[201~':
520
+ in_bracket_paste = False
521
+ esc_seq = []
522
+ fast_paste_chars = 0
523
+ if paste_text:
524
+ flush_paste()
525
+ continue
526
+ elif len(esc_seq) > 10:
527
+ for ch in esc_seq:
528
+ if ch.isprintable():
529
+ buf.insert(cursor_pos, ch)
530
+ cursor_pos += 1
531
+ esc_seq = []
532
+ redraw_line()
533
+ continue
534
+ elif key.isalpha() or key == '~':
535
+ esc_seq = []
536
+ continue
537
+ continue
538
+
539
+ if key == '\x1b':
540
+ if showing_menu:
541
+ hide_menu()
542
+ redraw_line()
543
+ else:
544
+ esc_seq = [key]
545
+ continue
546
+
547
+ if in_bracket_paste:
548
+ if key == '\r' or key == '\n':
549
+ paste_text += '\n'
550
+ continue
551
+ elif key == '\x00' or key == '\xe0':
552
+ msvcrt.getwch()
553
+ continue
554
+ elif len(key) == 1 and key.isprintable():
555
+ paste_text += key
556
+ continue
557
+ elif key == '\t':
558
+ paste_text += '\t'
559
+ continue
560
+ else:
561
+ continue
562
+
563
+ if is_pasting():
564
+ if key == '\r' or key == '\n':
565
+ paste_text += '\n'
566
+ fast_paste_chars = max(fast_paste_chars, 2)
567
+ continue
568
+ elif key == '\x08' or key == '\x7f':
569
+ if paste_text:
570
+ paste_text = paste_text[:-1]
571
+ continue
572
+ elif key == '\t':
573
+ paste_text += '\t'
574
+ continue
575
+ elif len(key) == 1 and key.isprintable():
576
+ paste_text += key
577
+ continue
578
+ else:
579
+ continue
580
+
431
581
  if key == '\x00' or key == '\xe0':
432
582
  special = msvcrt.getwch()
433
- if special == 'H': # 上箭头
583
+ if special == 'H':
434
584
  if showing_menu and completions:
435
585
  selected_idx = max(0, selected_idx - 1)
436
586
  show_menu()
437
587
  elif HISTORY:
438
- # 历史翻看:↑ 调到更早一条
439
588
  if history_idx == -1:
440
- # 第一次按 ↑:保存当前 buffer 为草稿
441
- history_draft = ''.join(buf[len(prompt_str):])
589
+ history_draft = ''.join(buf[prompt_len:])
442
590
  history_idx = max(0, history_idx - 1) if history_idx != -1 else len(HISTORY) - 1
443
591
  recall = HISTORY[history_idx]
444
592
  buf = list(prompt_str) + list(recall)
445
593
  cursor_pos = len(buf)
446
594
  redraw_line()
447
595
  continue
448
- elif special == 'P': # 下箭头
596
+ elif special == 'P':
449
597
  if showing_menu and completions:
450
- vis = min(len(completions), max_visible)
451
- selected_idx = min(vis - 1, selected_idx + 1)
598
+ selected_idx = min(len(completions) - 1, selected_idx + 1)
452
599
  show_menu()
453
600
  elif history_idx != -1:
454
- # 历史翻看:↓ 调到更新一条(越界则回草稿)
455
601
  history_idx += 1
456
602
  if history_idx >= len(HISTORY):
457
- # 越界:恢复草稿
458
603
  buf = list(prompt_str) + list(history_draft)
459
604
  cursor_pos = len(buf)
460
605
  history_idx = -1
@@ -464,57 +609,50 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
464
609
  cursor_pos = len(buf)
465
610
  redraw_line()
466
611
  continue
467
- elif special == 'K': # 左箭头
468
- if cursor_pos > len(prompt_str):
612
+ elif special == 'K':
613
+ if cursor_pos > prompt_len:
469
614
  cursor_pos -= 1
615
+ buf.pop(cursor_pos)
470
616
  redraw_line()
617
+ update_completions()
618
+ if showing_menu:
619
+ show_menu()
620
+ else:
621
+ hide_menu()
471
622
  continue
472
- elif special == 'M': # 右箭头
623
+ elif special == 'M':
473
624
  if cursor_pos < len(buf):
474
625
  cursor_pos += 1
475
626
  redraw_line()
476
627
  continue
628
+ elif special == 'G':
629
+ while cursor_pos > prompt_len:
630
+ cursor_pos -= 1
631
+ redraw_line()
632
+ continue
633
+ elif special == 'O':
634
+ cursor_pos = len(buf)
635
+ redraw_line()
636
+ continue
477
637
  continue
478
638
 
479
- # Ctrl+C
480
639
  if key == '\x03':
640
+ sys.stdout.write('\x1b[?2004l')
481
641
  sys.stdout.write('\r\x1b[K\n')
642
+ sys.stdout.flush()
482
643
  raise KeyboardInterrupt
483
644
 
484
- # Enter
485
645
  if key == '\r' or key == '\n':
486
- if showing_menu and completions:
487
- sel_text = completions[selected_idx][0]
488
- text_before = ''.join(buf[len(prompt_str):cursor_pos])
489
- at_ctx = find_at_context(text_before)
490
- if at_ctx:
491
- start = len(prompt_str) + at_ctx[0]
492
- elif text_before.startswith('/'):
493
- start = len(prompt_str)
494
- else:
495
- start = len(prompt_str)
496
- buf[start:cursor_pos] = list(sel_text)
497
- cursor_pos = start + len(sel_text)
498
- hide_menu()
499
- redraw_line()
500
- else:
501
- result = ''.join(buf[len(prompt_str):])
502
- # 入历史(非空 + 不与最后一条重复)
503
- if result and (not HISTORY or HISTORY[-1] != result):
504
- HISTORY.append(result)
505
- if len(HISTORY) > HISTORY_MAX:
506
- HISTORY.pop(0)
507
- # 重置历史导航状态
508
- history_idx = -1
509
- history_draft = ""
510
- sys.stdout.write('\n')
511
- sys.stdout.flush()
646
+ if paste_text:
647
+ flush_paste()
648
+ continue
649
+ result = handle_enter()
650
+ if result is not None:
512
651
  return result
513
652
  continue
514
653
 
515
- # Backspace
516
654
  if key == '\x08' or key == '\x7f':
517
- if cursor_pos > len(prompt_str):
655
+ if cursor_pos > prompt_len:
518
656
  cursor_pos -= 1
519
657
  buf.pop(cursor_pos)
520
658
  redraw_line()
@@ -525,32 +663,24 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
525
663
  hide_menu()
526
664
  continue
527
665
 
528
- # Tab - 确认补全
529
666
  if key == '\t':
530
667
  if showing_menu and completions:
531
668
  sel_text = completions[selected_idx][0]
532
- text_before = ''.join(buf[len(prompt_str):cursor_pos])
669
+ text_before = ''.join(buf[prompt_len:cursor_pos])
533
670
  at_ctx = find_at_context(text_before)
671
+ line_start_pos = cursor_pos - len(text_before)
534
672
  if at_ctx:
535
- start = len(prompt_str) + at_ctx[0]
673
+ start = line_start_pos + at_ctx[0]
536
674
  elif text_before.startswith('/'):
537
- start = len(prompt_str)
675
+ start = line_start_pos
538
676
  else:
539
- start = len(prompt_str)
677
+ start = line_start_pos
540
678
  buf[start:cursor_pos] = list(sel_text)
541
679
  cursor_pos = start + len(sel_text)
542
680
  hide_menu()
543
681
  redraw_line()
544
682
  continue
545
683
 
546
- # Escape - 关闭菜单
547
- if key == '\x1b':
548
- if showing_menu:
549
- hide_menu()
550
- redraw_line()
551
- continue
552
-
553
- # 普通可打印字符
554
684
  if len(key) == 1 and key.isprintable():
555
685
  buf.insert(cursor_pos, key)
556
686
  cursor_pos += 1
@@ -119,4 +119,27 @@ class Config:
119
119
  def get_model_info(self) -> dict:
120
120
  """获取当前模型信息"""
121
121
  model = self.get("model")
122
- return self.MODELS.get(model, {})
122
+ return self.MODELS.get(model, {})
123
+
124
+
125
+ # ---- 子目录骨架 ----
126
+ _SKEL_DIRS = ["skills", "hooks", "commands", "logs"]
127
+ _HOOK_EVENT_DIRS = ["PreToolUse", "PostToolUse"]
128
+
129
+
130
+ def init_user_dirs(base_dir: Path = None) -> None:
131
+ """首次启动时创建 ~/.abyss/ 骨架目录和 mcp.json 模板。
132
+ 幂等:目录已存在不报错,mcp.json 已存在不覆盖。
133
+ """
134
+ base = base_dir or CONFIG_DIR
135
+ base.mkdir(parents=True, exist_ok=True)
136
+ for sub in _SKEL_DIRS:
137
+ (base / sub).mkdir(exist_ok=True)
138
+ for ev in _HOOK_EVENT_DIRS:
139
+ (base / "hooks" / ev).mkdir(exist_ok=True)
140
+ mcp_file = base / "mcp.json"
141
+ if not mcp_file.exists():
142
+ mcp_file.write_text(
143
+ json.dumps({"mcp_servers": []}, indent=2, ensure_ascii=False),
144
+ encoding="utf-8",
145
+ )
@@ -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