Add a document or directory to the knowledge base from the chat REPL.
(arg: str, kb_dir: Path, style: Style)
| 475 | |
| 476 | |
| 477 | async def _run_add(arg: str, kb_dir: Path, style: Style) -> None: |
| 478 | """Add a document or directory to the knowledge base from the chat REPL.""" |
| 479 | from openkb.cli import SUPPORTED_EXTENSIONS, add_single_file |
| 480 | |
| 481 | target = Path(arg).expanduser() |
| 482 | if not target.is_absolute(): |
| 483 | target = Path.cwd() / target |
| 484 | target = target.resolve() |
| 485 | |
| 486 | if not target.exists(): |
| 487 | _fmt(style, ("class:error", f"Path does not exist: {arg}\n")) |
| 488 | return |
| 489 | |
| 490 | if target.is_dir(): |
| 491 | files = [ |
| 492 | f |
| 493 | for f in sorted(target.rglob("*")) |
| 494 | if f.is_file() and f.suffix.lower() in SUPPORTED_EXTENSIONS |
| 495 | ] |
| 496 | if not files: |
| 497 | _fmt(style, ("class:error", f"No supported files found in {arg}.\n")) |
| 498 | return |
| 499 | total = len(files) |
| 500 | _fmt(style, ("class:slash.help", f"Found {total} supported file(s) in {arg}.\n")) |
| 501 | for i, f in enumerate(files, 1): |
| 502 | _fmt(style, ("class:slash.help", f"\n[{i}/{total}] ")) |
| 503 | await asyncio.to_thread(add_single_file, f, kb_dir) |
| 504 | else: |
| 505 | if target.suffix.lower() not in SUPPORTED_EXTENSIONS: |
| 506 | _fmt(style, ("class:error", f"Unsupported file type: {target.suffix}\n")) |
| 507 | return |
| 508 | await asyncio.to_thread(add_single_file, target, kb_dir) |
| 509 | |
| 510 | |
| 511 | async def _handle_slash_skill(arg: str, kb_dir: Path, style: Style) -> None: |