abyss-cli 0.1.4__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 (55) hide show
  1. {abyss_cli-0.1.4/src/abyss_cli.egg-info → abyss_cli-0.1.5}/PKG-INFO +1 -1
  2. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/pyproject.toml +1 -1
  3. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/__init__.py +1 -1
  4. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/ansi_menu.py +225 -97
  5. {abyss_cli-0.1.4 → 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.4 → abyss_cli-0.1.5}/src/abyss/main.py +210 -82
  8. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/mcp/manager.py +9 -1
  9. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/shell_exec.py +116 -19
  10. {abyss_cli-0.1.4 → abyss_cli-0.1.5/src/abyss_cli.egg-info}/PKG-INFO +1 -1
  11. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss_cli.egg-info/SOURCES.txt +9 -1
  12. abyss_cli-0.1.5/test/test_custom_cmd_completion.py +46 -0
  13. abyss_cli-0.1.5/test/test_init_dirs.py +79 -0
  14. abyss_cli-0.1.5/tests/test_file_spinner.py +137 -0
  15. abyss_cli-0.1.5/tests/test_interrupt.py +71 -0
  16. abyss_cli-0.1.5/tests/test_mcp_manager.py +76 -0
  17. abyss_cli-0.1.5/tests/test_shell_exec.py +112 -0
  18. abyss_cli-0.1.5/tests/test_tool_calls_spinner.py +144 -0
  19. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/MANIFEST.in +0 -0
  20. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/setup.cfg +0 -0
  21. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/api_client.py +0 -0
  22. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/commands/__init__.py +0 -0
  23. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/commands/slash.py +0 -0
  24. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/extensions/__init__.py +0 -0
  25. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/extensions/cli.py +0 -0
  26. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/extensions/installer.py +0 -0
  27. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/extensions/registry.py +0 -0
  28. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/extensions/url_parser.py +0 -0
  29. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/hooks/__init__.py +0 -0
  30. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/hooks/runner.py +0 -0
  31. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/logger.py +0 -0
  32. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/mcp/__init__.py +0 -0
  33. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/prompts/__init__.py +0 -0
  34. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/prompts/methodologies.py +0 -0
  35. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/prompts/system_prompt.md +0 -0
  36. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/runtime/__init__.py +0 -0
  37. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/runtime/host.py +0 -0
  38. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/session.py +0 -0
  39. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/skills/__init__.py +0 -0
  40. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/skills/loader.py +0 -0
  41. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/subagent/__init__.py +0 -0
  42. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/subagent/runner.py +0 -0
  43. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/__init__.py +0 -0
  44. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/base.py +0 -0
  45. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/file_edit.py +0 -0
  46. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/file_read.py +0 -0
  47. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/file_write.py +0 -0
  48. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/registry.py +0 -0
  49. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/spawn_subagents.py +0 -0
  50. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/web_fetch.py +0 -0
  51. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss/tools/web_search.py +0 -0
  52. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss_cli.egg-info/dependency_links.txt +0 -0
  53. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss_cli.egg-info/entry_points.txt +0 -0
  54. {abyss_cli-0.1.4 → abyss_cli-0.1.5}/src/abyss_cli.egg-info/requires.txt +0 -0
  55. {abyss_cli-0.1.4 → 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.4
3
+ Version: 0.1.5
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.5"
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.5"
4
4
 
@@ -46,12 +46,28 @@ COMMANDS = [
46
46
  ("/domain", "领域建模,提炼术语和决策"),
47
47
  ]
48
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
+
49
62
 
50
63
  # ─── 输入历史(↑↓ 翻看用)────────────────────────────────
51
64
  # 模块级变量,跨多次 ansi_prompt 调用共享。
52
65
  HISTORY: list = []
53
66
  HISTORY_MAX = 100
54
67
 
68
+ # ─── 粘贴序号 ─────────────────────────────────────────
69
+ _PASTE_ID: int = 0
70
+
55
71
 
56
72
  # ─── 补全菜单最大可见行数(根据终端高度动态计算)────────────
57
73
  # 旧版本写死 8,COMMANDS 列表有 14 个 → 后 6 个被截断看不见。
@@ -105,7 +121,7 @@ def get_file_completions(partial: str) -> List[Tuple[str, str]]:
105
121
 
106
122
  def get_command_completions(partial: str) -> List[Tuple[str, str]]:
107
123
  results = []
108
- for cmd_name, cmd_desc in COMMANDS:
124
+ for cmd_name, cmd_desc in COMMANDS + _CUSTOM_COMMANDS:
109
125
  if _fuzzy_match(partial, cmd_name):
110
126
  results.append((cmd_name, cmd_desc))
111
127
  return results
@@ -251,10 +267,12 @@ def ansi_select(prompt: str, options: list, default_idx: int = 0) -> "str | None
251
267
  # ── 主输入函数 ────────────────────────────────────────────
252
268
  def ansi_prompt(prompt_str: str = "> ") -> str:
253
269
  """
254
- 自定义输入循环,支持 / 和 @ 补全菜单(ANSI 渲染)。
255
- 返回用户输入的字符串。
270
+ 自定义输入循环,支持 / 和 @ 补全菜单(ANSI 渲染)、Claude 风格多行粘贴。
271
+ 粘贴多行文本时显示 [Pasted text #N +M lines],不展开渲染。
256
272
  Ctrl+C 抛出 KeyboardInterrupt。
257
273
  """
274
+ global _PASTE_ID
275
+
258
276
  # Windows: 清空控制台输入缓冲,避免上次输出污染导致首次按键被吞
259
277
  if sys.platform == "win32":
260
278
  try:
@@ -265,31 +283,38 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
265
283
  except Exception:
266
284
  pass
267
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)
268
291
  buf: list = list(prompt_str)
269
292
  cursor_pos: int = len(buf)
270
- prev_line_len: int = len(buf)
271
293
  completions: List[Tuple[str, str]] = []
272
294
  selected_idx: int = 0
273
295
  showing_menu: bool = False
274
- max_visible: int = compute_max_visible() # 根据终端高度动态算,避免截断
296
+ max_visible: int = compute_max_visible()
275
297
  menu_row_count: int = 0
276
- max_rows_ever: int = 0 # 追踪渲染过的最大行数,确保清除干净
277
- # 历史翻看:history_idx = -1 表示不在导航态(停在草稿)
298
+ max_rows_ever: int = 0
278
299
  history_idx: int = -1
279
- 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
280
312
 
281
313
  def redraw_line():
282
- """重绘当前输入行。
283
- 关键:必须 \r\x1b[K(回行首 + 清行尾),不能只 \r。
284
- 否则本行原本有内容(如上一轮的 ',请重试)' 残留)会被新输入部分覆盖,
285
- 用户看到 '> /show-reasoning,请重试)' 这种新旧混杂的乱象。
286
- """
287
- nonlocal cursor_pos, buf, prev_line_len
288
- # 回行首 + 清行尾:本行原内容(残留的 ',请重试)' 之类)彻底抹掉
314
+ nonlocal cursor_pos
289
315
  sys.stdout.write('\r\x1b[K')
290
316
  line = ''.join(buf)
291
317
  sys.stdout.write(line)
292
- prev_line_len = len(buf)
293
318
  target_col = cursor_pos
294
319
  current_col = len(buf)
295
320
  if target_col < current_col:
@@ -297,18 +322,13 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
297
322
  sys.stdout.flush()
298
323
 
299
324
  def clear_menu_area():
300
- """清除菜单区域的所有行,光标回到输入行 cursor_pos 位置。
301
- 只在已有菜单空间内操作(假定 show_menu 已用 \\n 创建了空间),使用 \\x1b[B 避免额外滚动。
302
- """
303
325
  nonlocal menu_row_count
304
326
  if menu_row_count > 0:
305
- # \x1b[B 到菜单首行(空间已由 show_menu 首次激活时创建)
306
327
  sys.stdout.write('\x1b[B')
307
328
  for i in range(menu_row_count):
308
329
  sys.stdout.write('\r\x1b[K')
309
330
  if i < menu_row_count - 1:
310
331
  sys.stdout.write('\x1b[B')
311
- # 上移回输入行
312
332
  sys.stdout.write(ansi_move_up(menu_row_count))
313
333
  sys.stdout.write('\r')
314
334
  sys.stdout.write(ansi_move_right(cursor_pos))
@@ -317,34 +337,29 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
317
337
 
318
338
  def hide_menu():
319
339
  nonlocal showing_menu, menu_row_count, max_rows_ever, completions, selected_idx
320
- # 用 menu_row_count 判断是否有菜单需要清除(showing_menu 可能已被 update_completions 置 False)
321
340
  if menu_row_count > 0:
322
341
  clear_menu_area()
323
342
  showing_menu = False
324
343
  completions = []
325
344
  selected_idx = 0
326
- max_rows_ever = 0 # 重置,下次激活重新滚动创建空间
345
+ max_rows_ever = 0
327
346
 
328
347
  def show_menu():
329
348
  nonlocal showing_menu, menu_row_count, completions, selected_idx, max_rows_ever
330
349
  if not completions:
331
350
  hide_menu()
332
351
  return
333
-
334
352
  showing_menu = True
335
353
  visible_count = min(len(completions), max_visible)
336
354
  start_idx = 0
337
355
  if selected_idx >= max_visible:
338
356
  start_idx = selected_idx - max_visible + 1
339
-
340
- # 仅在需要更多空间时滚动(首次激活或匹配项增多)
341
357
  if visible_count > max_rows_ever:
342
358
  extra = visible_count - max_rows_ever
343
359
  for _ in range(extra):
344
360
  sys.stdout.write('\n')
345
361
  sys.stdout.write(ansi_move_up(extra))
346
362
  max_rows_ever = visible_count
347
- # 菜单已存在:清除旧内容(在已有空间内,用 \x1b[B 避免额外滚动)
348
363
  elif menu_row_count > 0:
349
364
  sys.stdout.write('\x1b[B')
350
365
  for i in range(menu_row_count):
@@ -354,11 +369,7 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
354
369
  sys.stdout.write(ansi_move_up(menu_row_count))
355
370
  sys.stdout.write('\r')
356
371
  sys.stdout.write(ansi_move_right(cursor_pos))
357
-
358
- # 重绘输入行
359
372
  sys.stdout.write('\r\x1b[K' + ''.join(buf))
360
-
361
- # 绘制菜单项(在已有空间内,用 \x1b[B)
362
373
  sys.stdout.write('\x1b[B')
363
374
  for i in range(visible_count):
364
375
  idx = start_idx + i
@@ -373,19 +384,15 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
373
384
  sys.stdout.write('\r\x1b[K' + line)
374
385
  if i < visible_count - 1:
375
386
  sys.stdout.write('\x1b[B')
376
-
377
- # 上移回输入行
378
387
  sys.stdout.write(ansi_move_up(visible_count))
379
388
  sys.stdout.write('\r')
380
389
  sys.stdout.write(ansi_move_right(cursor_pos))
381
390
  sys.stdout.flush()
382
-
383
391
  menu_row_count = visible_count
384
392
 
385
393
  def update_completions():
386
394
  nonlocal completions, showing_menu, selected_idx
387
- text_before = ''.join(buf[len(prompt_str):cursor_pos])
388
-
395
+ text_before = ''.join(buf[prompt_len:cursor_pos])
389
396
  at_ctx = find_at_context(text_before)
390
397
  if at_ctx:
391
398
  _, partial = at_ctx
@@ -399,7 +406,6 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
399
406
  completions = []
400
407
  showing_menu = False
401
408
  return
402
-
403
409
  if text_before.startswith('/'):
404
410
  new_comps = get_command_completions(text_before)
405
411
  if new_comps:
@@ -411,52 +417,189 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
411
417
  completions = []
412
418
  showing_menu = False
413
419
  return
414
-
415
420
  completions = []
416
421
  showing_menu = False
417
422
 
418
- # 打印初始提示符(先清行,避免上一轮输出残留)
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
+
419
485
  sys.stdout.write('\r\x1b[K' + prompt_str)
420
486
  sys.stdout.flush()
421
487
 
422
488
  while True:
423
- # 直接阻塞读取 msvcrt.getwch(),不要用 kbhit() + 紧密轮询。
424
- # 5ms 紧密轮询会和 Windows 中文 IME 冲突,导致 IME 提交字符后
425
- # 程序仍然读不到 Enter,从而"按回车无反应、卡死"。
426
- # getwch() 在 Windows 上 Ctrl+C 不会自动抛异常,需靠 \x03 字符判断(已处理)。
427
489
  try:
428
490
  key = msvcrt.getwch()
429
491
  except (OSError, ValueError):
430
492
  time.sleep(0.01)
431
493
  continue
432
494
 
433
- # 特殊键(两字节序列)
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
+
434
581
  if key == '\x00' or key == '\xe0':
435
582
  special = msvcrt.getwch()
436
- if special == 'H': # 上箭头
583
+ if special == 'H':
437
584
  if showing_menu and completions:
438
585
  selected_idx = max(0, selected_idx - 1)
439
586
  show_menu()
440
587
  elif HISTORY:
441
- # 历史翻看:↑ 调到更早一条
442
588
  if history_idx == -1:
443
- # 第一次按 ↑:保存当前 buffer 为草稿
444
- history_draft = ''.join(buf[len(prompt_str):])
589
+ history_draft = ''.join(buf[prompt_len:])
445
590
  history_idx = max(0, history_idx - 1) if history_idx != -1 else len(HISTORY) - 1
446
591
  recall = HISTORY[history_idx]
447
592
  buf = list(prompt_str) + list(recall)
448
593
  cursor_pos = len(buf)
449
594
  redraw_line()
450
595
  continue
451
- elif special == 'P': # 下箭头
596
+ elif special == 'P':
452
597
  if showing_menu and completions:
453
598
  selected_idx = min(len(completions) - 1, selected_idx + 1)
454
599
  show_menu()
455
600
  elif history_idx != -1:
456
- # 历史翻看:↓ 调到更新一条(越界则回草稿)
457
601
  history_idx += 1
458
602
  if history_idx >= len(HISTORY):
459
- # 越界:恢复草稿
460
603
  buf = list(prompt_str) + list(history_draft)
461
604
  cursor_pos = len(buf)
462
605
  history_idx = -1
@@ -466,57 +609,50 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
466
609
  cursor_pos = len(buf)
467
610
  redraw_line()
468
611
  continue
469
- elif special == 'K': # 左箭头
470
- if cursor_pos > len(prompt_str):
612
+ elif special == 'K':
613
+ if cursor_pos > prompt_len:
471
614
  cursor_pos -= 1
615
+ buf.pop(cursor_pos)
472
616
  redraw_line()
617
+ update_completions()
618
+ if showing_menu:
619
+ show_menu()
620
+ else:
621
+ hide_menu()
473
622
  continue
474
- elif special == 'M': # 右箭头
623
+ elif special == 'M':
475
624
  if cursor_pos < len(buf):
476
625
  cursor_pos += 1
477
626
  redraw_line()
478
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
479
637
  continue
480
638
 
481
- # Ctrl+C
482
639
  if key == '\x03':
640
+ sys.stdout.write('\x1b[?2004l')
483
641
  sys.stdout.write('\r\x1b[K\n')
642
+ sys.stdout.flush()
484
643
  raise KeyboardInterrupt
485
644
 
486
- # Enter
487
645
  if key == '\r' or key == '\n':
488
- if showing_menu and completions:
489
- sel_text = completions[selected_idx][0]
490
- text_before = ''.join(buf[len(prompt_str):cursor_pos])
491
- at_ctx = find_at_context(text_before)
492
- if at_ctx:
493
- start = len(prompt_str) + at_ctx[0]
494
- elif text_before.startswith('/'):
495
- start = len(prompt_str)
496
- else:
497
- start = len(prompt_str)
498
- buf[start:cursor_pos] = list(sel_text)
499
- cursor_pos = start + len(sel_text)
500
- hide_menu()
501
- redraw_line()
502
- else:
503
- result = ''.join(buf[len(prompt_str):])
504
- # 入历史(非空 + 不与最后一条重复)
505
- if result and (not HISTORY or HISTORY[-1] != result):
506
- HISTORY.append(result)
507
- if len(HISTORY) > HISTORY_MAX:
508
- HISTORY.pop(0)
509
- # 重置历史导航状态
510
- history_idx = -1
511
- history_draft = ""
512
- sys.stdout.write('\n')
513
- sys.stdout.flush()
646
+ if paste_text:
647
+ flush_paste()
648
+ continue
649
+ result = handle_enter()
650
+ if result is not None:
514
651
  return result
515
652
  continue
516
653
 
517
- # Backspace
518
654
  if key == '\x08' or key == '\x7f':
519
- if cursor_pos > len(prompt_str):
655
+ if cursor_pos > prompt_len:
520
656
  cursor_pos -= 1
521
657
  buf.pop(cursor_pos)
522
658
  redraw_line()
@@ -527,32 +663,24 @@ def ansi_prompt(prompt_str: str = "> ") -> str:
527
663
  hide_menu()
528
664
  continue
529
665
 
530
- # Tab - 确认补全
531
666
  if key == '\t':
532
667
  if showing_menu and completions:
533
668
  sel_text = completions[selected_idx][0]
534
- text_before = ''.join(buf[len(prompt_str):cursor_pos])
669
+ text_before = ''.join(buf[prompt_len:cursor_pos])
535
670
  at_ctx = find_at_context(text_before)
671
+ line_start_pos = cursor_pos - len(text_before)
536
672
  if at_ctx:
537
- start = len(prompt_str) + at_ctx[0]
673
+ start = line_start_pos + at_ctx[0]
538
674
  elif text_before.startswith('/'):
539
- start = len(prompt_str)
675
+ start = line_start_pos
540
676
  else:
541
- start = len(prompt_str)
677
+ start = line_start_pos
542
678
  buf[start:cursor_pos] = list(sel_text)
543
679
  cursor_pos = start + len(sel_text)
544
680
  hide_menu()
545
681
  redraw_line()
546
682
  continue
547
683
 
548
- # Escape - 关闭菜单
549
- if key == '\x1b':
550
- if showing_menu:
551
- hide_menu()
552
- redraw_line()
553
- continue
554
-
555
- # 普通可打印字符
556
684
  if len(key) == 1 and key.isprintable():
557
685
  buf.insert(cursor_pos, key)
558
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