|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import contextlib |
| 4 | +import importlib |
4 | 5 | import io |
5 | 6 | import logging |
6 | 7 | import os |
|
15 | 16 | import click |
16 | 17 |
|
17 | 18 | try: |
18 | | - import llm |
19 | | - |
20 | | - LLM_IMPORTED = True |
| 19 | + import llm as llm_module |
21 | 20 | except ImportError: |
22 | | - llm = None |
23 | | - LLM_IMPORTED = False |
| 21 | + llm_module = None |
24 | 22 |
|
25 | 23 | try: |
26 | | - from llm.cli import cli |
27 | | - |
28 | | - LLM_CLI_IMPORTED = True |
| 24 | + llm_cli_module = importlib.import_module("llm.cli") |
29 | 25 | except ImportError: |
30 | | - cli = None |
31 | | - LLM_CLI_IMPORTED = False |
| 26 | + llm_cli_module = None |
32 | 27 |
|
33 | 28 | from . import export |
34 | 29 | from .main import Verbosity, parse_special_command |
35 | 30 | from .types import DBCursor |
36 | 31 |
|
| 32 | +LLM_IMPORTED = llm_module is not None |
| 33 | + |
| 34 | +cli: click.Command | None |
| 35 | +if llm_cli_module is not None: |
| 36 | + llm_cli = getattr(llm_cli_module, "cli", None) |
| 37 | + cli = llm_cli if isinstance(llm_cli, click.Command) else None |
| 38 | +else: |
| 39 | + cli = None |
| 40 | + |
| 41 | +LLM_CLI_IMPORTED = cli is not None |
| 42 | + |
37 | 43 | log = logging.getLogger(__name__) |
38 | 44 |
|
39 | 45 | LLM_TEMPLATE_NAME = "litecli-llm-template" |
40 | | -LLM_CLI_COMMANDS: list[str] = list(cli.commands.keys()) if LLM_CLI_IMPORTED else [] |
| 46 | +LLM_CLI_COMMANDS: list[str] = list(cli.commands.keys()) if isinstance(cli, click.Group) else [] |
41 | 47 | # Mapping of model_id to None used for completion tree leaves. |
42 | | -# the file name is llm.py and module name is llm, hence ty is complaining that get_models is missing. |
43 | | -MODELS: dict[str, None] = {x.model_id: None for x in llm.get_models()} if LLM_IMPORTED else {} # type: ignore[attr-defined] |
| 48 | +if llm_module is not None: |
| 49 | + get_models = getattr(llm_module, "get_models", None) |
| 50 | + MODELS: dict[str, None] = {x.model_id: None for x in get_models()} if callable(get_models) else {} |
| 51 | +else: |
| 52 | + MODELS = {} |
44 | 53 |
|
45 | 54 |
|
46 | 55 | def run_external_cmd( |
@@ -124,7 +133,7 @@ def build_command_tree(cmd: click.Command) -> dict[str, Any] | None: |
124 | 133 |
|
125 | 134 |
|
126 | 135 | # Generate the tree |
127 | | -COMMAND_TREE: dict[str, Any] | None = build_command_tree(cli) if LLM_CLI_IMPORTED else {} |
| 136 | +COMMAND_TREE: dict[str, Any] | None = build_command_tree(cli) if cli is not None else {} |
128 | 137 |
|
129 | 138 |
|
130 | 139 | def get_completions(tokens: list[str], tree: dict[str, Any] | None = COMMAND_TREE) -> list[str]: |
|
0 commit comments