Coverage for aipyapp/cli/cli_python.py: 0%
52 statements
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-11 12:02 +0200
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-11 12:02 +0200
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import code
4import builtins
6from rich.console import Console
7from prompt_toolkit import PromptSession
8from prompt_toolkit.formatted_text import HTML
9from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
10from prompt_toolkit.completion import WordCompleter
11from prompt_toolkit.lexers import PygmentsLexer
12from prompt_toolkit.history import FileHistory
13from pygments.lexers.python import PythonLexer
15from ..aipy import TaskManager
16from .. import T, __version__
18class PythonCompleter(WordCompleter):
19 def __init__(self, ai):
20 names = ['exit()']
21 names += [name for name in dir(builtins)]
22 names += [f"ai.{attr}" for attr in dir(ai) if not attr.startswith('_')]
23 super().__init__(names, ignore_case=True)
25def main(settings):
26 console = Console(record=True)
27 console.print(f"[bold cyan]🚀 Python use - AIPython ({__version__}) [[green]https://aipy.app[/green]]")
29 try:
30 ai = TaskManager(settings, console=console)
31 except Exception as e:
32 console.print_exception(e)
33 return
35 update = ai.get_update(True)
36 if update and update.get('has_update'):
37 console.print(f"[bold red]🔔 号外❗ {T('Update available')}: {update.get('latest_version')}")
39 if not ai.client_manager:
40 console.print(f"[bold red]{T('No available LLM, please check the configuration file')}")
41 return
43 names = ai.client_manager.names
44 console.print(f"{T('Please use ai(task) to enter the task to be processed by AI (enter ai.use(llm) to switch to the following LLM:')}", style="green")
45 console.print(f"[cyan]{T('Default')}: [green]{names['default']},[cyan]{T('Enabled')}: [yellow]{' '.join(names['enabled'])}")
47 interp = code.InteractiveConsole({'ai': ai})
49 completer = PythonCompleter(ai)
50 lexer = PygmentsLexer(PythonLexer)
51 auto_suggest = AutoSuggestFromHistory()
52 history = FileHistory(str(settings['config_dir'] / '.history.py'))
53 session = PromptSession(history=history, completer=completer, lexer=lexer, auto_suggest=auto_suggest)
54 while True:
55 try:
56 user_input = session.prompt(HTML('<ansiblue>>> </ansiblue>'))
57 if user_input.strip() in {"exit()", "quit()"}:
58 break
59 interp.push(user_input)
60 except EOFError:
61 console.print("[bold yellow]Exiting...")
62 break
63 except Exception as e:
64 console.print(f"[bold red]Error: {e}")