Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sqlite logging to cmd plugin if configured #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions llm_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def register_commands(cli):
@click.option("--key", help="API key to use")
def cmd(args, model, system, key):
"""Generate and execute commands in your shell"""
from llm.cli import get_default_model
from llm.cli import get_default_model, logs_on, logs_db_path

prompt = " ".join(args)

Expand All @@ -39,19 +39,54 @@ def cmd(args, model, system, key):

result = model_obj.prompt(prompt, system=system or SYSTEM_PROMPT)

interactive_exec(str(result))
output = interactive_exec(str(result))
print(output)

if logs_on():
import sqlite_utils
from llm.migrations import migrate

log_path = logs_db_path()
(log_path.parent).mkdir(parents=True, exist_ok=True)
db = sqlite_utils.Database(log_path)
migrate(db)
_log_cmd(db, model_id, result, output, prompt)


def interactive_exec(command) -> str:
try:
if "\n" in command:
print("Multiline command - Meta-Enter or Esc Enter to execute")
edited_command = prompt(
"> ", default=command, lexer=PygmentsLexer(BashLexer), multiline=True
)
else:
edited_command = prompt(
"> ", default=command, lexer=PygmentsLexer(BashLexer)
)
except KeyboardInterrupt:
return "Aborted!"

def interactive_exec(command):
if '\n' in command:
print("Multiline command - Meta-Enter or Esc Enter to execute")
edited_command = prompt("> ", default=command, lexer=PygmentsLexer(BashLexer), multiline=True)
else:
edited_command = prompt("> ", default=command, lexer=PygmentsLexer(BashLexer))
try:
output = subprocess.check_output(
edited_command, shell=True, stderr=subprocess.STDOUT
)
print(output.decode())
return output.decode()
except subprocess.CalledProcessError as e:
print(f"Command failed with error (exit status {e.returncode}): {e.output.decode()}")
return f"Command failed with error (exit status {e.returncode}): {e.output.decode()}"


def _log_cmd(db, model_id, result, output, prompt):
failed = "Command failed with error" in output
canceled = output == "Aborted!"
db["cmds"].insert(
{
"model": model_id,
"prompt": prompt,
"cmd": str(result),
"output": output,
"failed": failed,
"canceled": canceled,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

americans 🙄

},
ignore=True,
)