Skip to content

Commit

Permalink
VSCode adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
yorevs committed Dec 6, 2024
1 parent b7f43af commit 80d2d36
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 110 deletions.
7 changes: 4 additions & 3 deletions AskAI.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
],
"settings": {
"hide-files.files": [
".gradle",
".idea",
".run",
".vscode",
"run-configs",
Expand All @@ -16,6 +14,9 @@
".gitconfig",
".gitignore",
".gitmodules"
],
"cSpell.words": [
"destfile"
]
}
}
}
36 changes: 15 additions & 21 deletions src/main/askai/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# -*- coding: utf-8 -*-

"""
@project: hspylib
@package: hspylib
@file: __main__.py
@created: Fri, 5 Jan 2024
@author: <B>H</B>ugo <B>S</B>aporetti <B>J</B>unior
@site: https://github.com/yorevs/askai
@license: MIT - Please refer to <https://opensource.org/licenses/MIT>
Copyright (c) 2024, AskAI
@project: hspylib
@package: hspylib
@file: __main__.py
@created: Fri, 5 Jan 2024
@author: <B>H</B>ugo <B>S</B>aporetti <B>J</B>unior
@site: https://github.com/yorevs/askai
@license: MIT - Please refer to <https://opensource.org/licenses/MIT>
Copyright (c) 2024, AskAI
"""

import logging as log
Expand All @@ -19,16 +19,15 @@
import sys
import typing

from askai.__classpath__ import classpath
from askai.core.support.utilities import QueryString
from clitt.core.tui.tui_application import TUIApplication
from hspylib.core.enums.charset import Charset
from hspylib.core.tools.commons import syserr, to_bool
from hspylib.modules.application.argparse.parser_action import ParserAction
from hspylib.modules.application.exit_status import ExitStatus
from hspylib.modules.application.version import Version

from askai.__classpath__ import classpath
from askai.core.support.utilities import QueryString


class Main(TUIApplication):
"""HomeSetup Ask-AI - Unleash the Power of AI in Your Terminal."""
Expand All @@ -54,11 +53,10 @@ def _execute_command(command_str: typing.AnyStr) -> ExitStatus:
:return: The exit status of the command execution.
"""
import click

from askai.core.commander import commander

try:
if command := re.search(commander.RE_ASKAI_CMD, command_str):
if command := re.search(commander.RE_ASKAI_CMD, str(command_str)):
args: list[str] = list(
filter(
lambda a: a and a != "None",
Expand Down Expand Up @@ -148,10 +146,9 @@ def _main(self, *params, **kwargs) -> ExitStatus:
"""
from textwrap import dedent

from hspylib.core.zoned_datetime import now

from askai.core.askai_configs import configs
from askai.core.enums.run_modes import RunModes
from hspylib.core.zoned_datetime import now

os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
is_new_ui: bool = to_bool(self._get_argument("ui", False))
Expand Down Expand Up @@ -192,7 +189,7 @@ def _main(self, *params, **kwargs) -> ExitStatus:
)
else:
os.environ["ASKAI_APP"] = RunModes.ASKAI_CMD.value
return self._execute_command(query_string)
return self._execute_command(query_string or "/help")

# fmt: off
log.debug(dedent(f"""\
Expand Down Expand Up @@ -252,7 +249,7 @@ def _get_mode_str(self) -> str:
"""
return self._get_argument("router", "default")

def _get_interactive(self, query_string: str) -> bool:
def _get_interactive(self, query_string: str | None) -> bool:
"""Return the interactive parameter if query_string is not specified; False otherwise.
:param query_string: The query string to check for interactivity.
:return: The value of the interactive parameter or False based on query_string presence.
Expand All @@ -263,6 +260,3 @@ def _get_interactive(self, query_string: str) -> bool:
# Application entry point
if __name__ == "__main__":
Main("AskAI").INSTANCE.run(sys.argv[1:])
# Application entry point
if __name__ == "__main__":
Main("AskAI").INSTANCE.run(sys.argv[1:])
41 changes: 20 additions & 21 deletions src/main/askai/core/askai_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
# -*- coding: utf-8 -*-

"""
@project: HsPyLib-AskAI
@package: askai.core
@file: askai_cli.py
@created: Fri, 9 Aug 2024
@author: <B>H</B>ugo <B>S</B>aporetti <B>J</B>unior
@site: https://github.com/yorevs/askai
@license: MIT - Please refer to <https://opensource.org/licenses/MIT>
@project: HsPyLib-AskAI
@package: askai.core
@file: askai_cli.py
@created: Fri, 9 Aug 2024
@author: <B>H</B>ugo <B>S</B>aporetti <B>J</B>unior
@site: https://github.com/yorevs/askai
@license: MIT - Please refer to <https://opensource.org/licenses/MIT>
Copyright (c) 2024, AskAI
Copyright (c) 2024, AskAI
"""

import logging as log
import os
import signal
Expand All @@ -22,30 +23,29 @@

import nltk
import pause
from clitt.core.term.cursor import cursor
from clitt.core.term.screen import screen
from clitt.core.tui.line_input.keyboard_input import KeyboardInput
from hspylib.core.enums.charset import Charset
from hspylib.core.tools.commons import console_out
from hspylib.core.zoned_datetime import now, TIME_FORMAT
from hspylib.modules.eventbus.event import Event
from rich.progress import Progress

from askai.core.askai import AskAi
from askai.core.askai_configs import configs
from askai.core.askai_events import *
from askai.core.askai_messages import msg
from askai.core.askai_prompt import prompt
from askai.core.commander.commander import commands
from askai.core.component.audio_player import player
from askai.core.component.cache_service import cache, CACHE_DIR
from askai.core.component.cache_service import CACHE_DIR, cache
from askai.core.component.recorder import recorder
from askai.core.component.scheduler import scheduler
from askai.core.enums.router_mode import RouterMode
from askai.core.model.ai_reply import AIReply
from askai.core.support.shared_instances import shared
from askai.core.support.utilities import display_text
from askai.tui.app_icons import AppIcons
from clitt.core.term.cursor import cursor
from clitt.core.term.screen import screen
from clitt.core.tui.line_input.keyboard_input import KeyboardInput
from hspylib.core.enums.charset import Charset
from hspylib.core.tools.commons import console_out
from hspylib.core.zoned_datetime import TIME_FORMAT, now
from hspylib.modules.eventbus.event import Event
from rich.progress import Progress


class AskAiCli(AskAi):
Expand All @@ -57,16 +57,15 @@ def __init__(
debug: bool,
cacheable: bool,
tempo: int,
query_prompt: str,
query_prompt: str | None,
engine_name: str,
model_name: str,
query_string: str | None,
mode: RouterMode,
):

super().__init__(speak, debug, cacheable, tempo, engine_name, model_name, mode)
self._ready: bool = False
self._query_prompt: str = query_prompt
self._query_prompt: str | None = query_prompt
self._query_string: str | None = query_string
self._startup()

Expand Down
74 changes: 40 additions & 34 deletions src/main/askai/core/support/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@
# -*- coding: utf-8 -*-

"""
@project: HsPyLib-AskAI
@package: askai.core.support.utilities
@file: utilities.py
@created: Wed, 10 Jan 2024
@author: <B>H</B>ugo <B>S</B>aporetti <B>J</B>unior
@site: https://github.com/yorevs/askai
@license: MIT - Please refer to <https://opensource.org/licenses/MIT>
Copyright (c) 2024, AskAI
@project: HsPyLib-AskAI
@package: askai.core.support.utilities
@file: utilities.py
@created: Wed, 10 Jan 2024
@author: <B>H</B>ugo <B>S</B>aporetti <B>J</B>unior
@site: https://github.com/yorevs/askai
@license: MIT - Please refer to <https://opensource.org/licenses/MIT>
Copyright (c) 2024, AskAI
"""

import base64
import mimetypes
import os
import re
import shlex
import shutil
import sys
from os.path import basename, dirname
from pathlib import Path
from typing import AnyStr, Optional, TypeAlias

from askai.core.support.text_formatter import text_formatter
from clitt.core.term.cursor import cursor
from hspylib.core.config.path_object import PathObject
Expand All @@ -21,17 +33,6 @@
from hspylib.core.tools.commons import file_is_not_empty
from hspylib.core.tools.text_tools import ensure_endswith, strip_escapes
from hspylib.core.zoned_datetime import now_ms
from os.path import basename, dirname
from pathlib import Path
from typing import AnyStr, Optional, TypeAlias

import base64
import mimetypes
import os
import re
import shlex
import shutil
import sys

QueryString: TypeAlias = None | str | list[str]

Expand Down Expand Up @@ -67,13 +68,13 @@ def find_file(filename: AnyPath) -> Optional[Path]:
:param filename: The name or path of the file to find.
:return: The full path to the file if found, otherwise None.
"""
prompt_path: Path = Path(filename) if filename else None
prompt_path: Path | None = Path(filename) if filename else None
if prompt_path and not prompt_path.exists():
prompt_path = Path(os.path.expandvars(os.path.expanduser(filename)))
prompt_path = Path(os.path.expandvars(os.path.expanduser(str(filename))))
if not prompt_path.exists():
prompt_path = Path(os.path.join(os.getcwd(), filename))
prompt_path = Path(os.path.join(os.getcwd(), str(filename)))
if not prompt_path.exists():
prompt_path = Path(os.path.join(Path.home(), filename))
prompt_path = Path(os.path.join(Path.home(), str(filename)))
return prompt_path if prompt_path and prompt_path.exists() else None


Expand All @@ -83,13 +84,18 @@ def copy_file(srcfile: AnyPath | Path, destfile: AnyPath | Path) -> str:
:param destfile: The destination path where the file should be copied.
:return: The path of the copied file as a string.
"""
filepath: PathObject = PathObject.of(srcfile)
dest_dir: PathObject = PathObject.of(destfile)
filepath: PathObject | None = PathObject.of(srcfile)
dest_dir: PathObject | None = PathObject.of(destfile)
check_argument(
filepath.exists and filepath.is_file and dest_dir.exists and dest_dir.is_dir
filepath is not None
and filepath.exists
and filepath.is_file
and dest_dir is not None
and dest_dir.exists
and dest_dir.is_dir
)
dest_file: str = os.path.join(dest_dir.abs_dir, filepath.filename)
shutil.copy(srcfile, dest_file)
shutil.copy(str(srcfile), str(dest_file))
return dest_file


Expand Down Expand Up @@ -127,7 +133,7 @@ def read_resource(base_dir: AnyPath, filename: AnyPath, file_ext: str = ".txt")
:param file_ext: The file extension of the file (default is ".txt").
:return: The content of the file as a string.
"""
_, ext = os.path.splitext(filename)
_, ext = os.path.splitext(str(filename))
filename = (
f"{str(base_dir)}/{ensure_endswith(basename(str(filename)), ext or file_ext)}"
)
Expand All @@ -144,10 +150,10 @@ def read_file(load_dir: AnyPath, path_name: str) -> Optional[str]:
:param path_name: The path name of the file to read.
:return: The contents of the file as a string, or None if the file cannot be read.
"""
file: Path = find_file(Path(join_path(load_dir, path_name)))
file: Path | None = find_file(Path(join_path(load_dir, path_name)))
return (
file.read_text(encoding=Charset.UTF_8.val)
if file_is_not_empty(str(file))
if file and file_is_not_empty(str(file))
else ""
)

Expand Down Expand Up @@ -205,9 +211,9 @@ def media_type_of(pathname: str) -> Optional[tuple[str | None, ...]]:
:param pathname: The file path to check.
:return: A tuple representing the media type (e.g., ("text/plain")), or None if guessing was not possible.
"""
mtype, _ = mimetypes.guess_type(os.path.basename(pathname))
if mtype:
return tuple(mtype.split("/"))
media_type, _ = mimetypes.guess_type(os.path.basename(pathname))
if media_type:
return tuple(media_type.split("/"))
return None


Expand Down
Loading

0 comments on commit 80d2d36

Please sign in to comment.