|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Shared DashScope SDK Expert adapter for both console entry points.""" |
| 3 | + |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from rich.console import Console |
| 8 | + |
| 9 | +import dashscope |
| 10 | + |
| 11 | +err_console = Console(stderr=True) |
| 12 | + |
| 13 | +_MD_MARKER = "<!-- managed-by: dashscope-expert -->" |
| 14 | +_PY_MARKER = "# managed-by: dashscope-expert" |
| 15 | +_EXPERT_EXAMPLE = "dashscope-sdk-expert" |
| 16 | + |
| 17 | + |
| 18 | +def _cleanup_legacy_expert_sync(): |
| 19 | + """Remove files the deleted expert bundling synced into ~/.acli. |
| 20 | +
|
| 21 | + Only files carrying the managed marker are deleted — without it the file |
| 22 | + is user-owned and left untouched. Needed so stale global copies don't |
| 23 | + shadow the workspace ./.acli/ configs installed via example download. |
| 24 | + """ |
| 25 | + try: |
| 26 | + from dashscope.acli.config import CONFIG_DIR |
| 27 | + except ImportError: |
| 28 | + return |
| 29 | + md_targets = [] |
| 30 | + for subdir in ("skills", "references"): |
| 31 | + target_dir = CONFIG_DIR / subdir |
| 32 | + if target_dir.is_dir(): |
| 33 | + md_targets.extend(target_dir.glob("*.md")) |
| 34 | + for path in md_targets: |
| 35 | + try: |
| 36 | + if _MD_MARKER in path.read_text(encoding="utf-8"): |
| 37 | + path.unlink() |
| 38 | + except OSError: |
| 39 | + pass |
| 40 | + build = CONFIG_DIR / "build_sdk_index.py" |
| 41 | + try: |
| 42 | + if build.is_file() and _PY_MARKER in build.read_text(encoding="utf-8"): |
| 43 | + build.unlink() |
| 44 | + except OSError: |
| 45 | + pass |
| 46 | + |
| 47 | + |
| 48 | +def _maybe_offer_example_download(): |
| 49 | + """First interactive run without ./.acli: offer the SDK-expert example. |
| 50 | +
|
| 51 | + Accepting merges the example into ./.acli/ via the same code path as |
| 52 | + `example download` (copied from the packaged install dir when present); |
| 53 | + declining writes a marker into ./.acli/ so we never ask again here. |
| 54 | + """ |
| 55 | + if not sys.stdin.isatty() or (Path.cwd() / ".acli").is_dir(): |
| 56 | + return |
| 57 | + try: |
| 58 | + from dashscope.acli.cli.examples import _handle_example_command |
| 59 | + from dashscope.acli.cli.handlers_key import _GUIDE_DOC, _doc_locale |
| 60 | + except ImportError: |
| 61 | + return |
| 62 | + err_console.print( |
| 63 | + "[cyan]First run:[/cyan] download the dashscope-sdk-expert " |
| 64 | + "example config into ./.acli/\n" |
| 65 | + "(SDK Q&A expert persona + skill templates + SDK knowledge " |
| 66 | + "index; pure config, editable anytime).", |
| 67 | + ) |
| 68 | + err_console.print( |
| 69 | + f"[dim]Guide: {_GUIDE_DOC.format(_doc_locale())}[/dim]", |
| 70 | + ) |
| 71 | + try: |
| 72 | + answer = input("Download the example config? [Y/n] ").strip().lower() |
| 73 | + except (EOFError, KeyboardInterrupt): |
| 74 | + err_console.print() |
| 75 | + return |
| 76 | + if answer in ("n", "no"): |
| 77 | + try: |
| 78 | + marker = Path.cwd() / ".acli" / ".dashscope-example-declined" |
| 79 | + marker.parent.mkdir(parents=True, exist_ok=True) |
| 80 | + marker.write_text("declined\n", encoding="utf-8") |
| 81 | + except OSError: |
| 82 | + pass |
| 83 | + err_console.print( |
| 84 | + "[dim]Skipped. To customize later, run: " |
| 85 | + "python -m dashscope.acli example download " |
| 86 | + "dashscope-sdk-expert[/dim]", |
| 87 | + ) |
| 88 | + return |
| 89 | + _handle_example_command(["download", _EXPERT_EXAMPLE]) |
| 90 | + |
| 91 | + |
| 92 | +def _route_to_expert(command, tui=None): |
| 93 | + """Run the vendored acli agent (bare ``dashscope`` or ``dashscope expert``). |
| 94 | +
|
| 95 | + ``command`` is a one-shot prompt, or None for the interactive REPL. |
| 96 | + """ |
| 97 | + try: |
| 98 | + from dashscope.acli.cli.handlers_key import _GUIDE_DOC, _doc_locale |
| 99 | + from dashscope.acli.ui.embedded import run |
| 100 | + except ImportError as exception: |
| 101 | + err_console.print( |
| 102 | + "[yellow]Tip:[/yellow] Install dashscope[acli] to enable " |
| 103 | + "the agent " |
| 104 | + f"({exception}).", |
| 105 | + ) |
| 106 | + return |
| 107 | + |
| 108 | + if command is None and not sys.stdin.isatty(): |
| 109 | + # The interactive REPL needs a real terminal; prompt_toolkit crashes |
| 110 | + # on non-tty stdin (e.g. kqueue rejects /dev/null with EINVAL). |
| 111 | + # Treat piped text as a one-shot prompt instead. |
| 112 | + try: |
| 113 | + piped = sys.stdin.read().strip() |
| 114 | + except OSError: |
| 115 | + piped = "" |
| 116 | + if not piped: |
| 117 | + err_console.print( |
| 118 | + "[red]Error:[/red] Interactive mode requires a terminal. " |
| 119 | + 'Pass a prompt instead, e.g. dashscope expert "your question".', |
| 120 | + ) |
| 121 | + sys.exit(2) |
| 122 | + command = piped |
| 123 | + |
| 124 | + _cleanup_legacy_expert_sync() |
| 125 | + if command is None: |
| 126 | + _maybe_offer_example_download() |
| 127 | + |
| 128 | + try: |
| 129 | + run( |
| 130 | + app_name="DashScope SDK Expert", |
| 131 | + prompt_symbol="dashscope> ", |
| 132 | + api_key=dashscope.api_key or None, |
| 133 | + command=command, |
| 134 | + tui=tui, |
| 135 | + module="expert", |
| 136 | + guide_url=_GUIDE_DOC.format(_doc_locale()), |
| 137 | + ) |
| 138 | + except SystemExit: |
| 139 | + pass |
| 140 | + except Exception as exception: # pylint: disable=broad-except |
| 141 | + err_console.print( |
| 142 | + f"[yellow]Tip:[/yellow] Agent failed to start ({exception}).", |
| 143 | + ) |
0 commit comments