From bcd393b492f7685f91869238350ed41e820fec2e Mon Sep 17 00:00:00 2001 From: ncoop57 Date: Mon, 18 Nov 2024 17:47:42 -0600 Subject: [PATCH] Refactor core.py with BashBuddy AI assistant implementation --- bash_buddy/core.py | 76 ++++++++++++++++-- nbs/00_core.ipynb | 61 -------------- nbs/_quarto.yml | 22 ------ nbs/index.ipynb | 192 --------------------------------------------- nbs/nbdev.yml | 9 --- nbs/styles.css | 37 --------- settings.ini | 8 +- 7 files changed, 74 insertions(+), 331 deletions(-) mode change 100644 => 100755 bash_buddy/core.py delete mode 100644 nbs/00_core.ipynb delete mode 100644 nbs/_quarto.yml delete mode 100644 nbs/index.ipynb delete mode 100644 nbs/nbdev.yml delete mode 100644 nbs/styles.css diff --git a/bash_buddy/core.py b/bash_buddy/core.py old mode 100644 new mode 100755 index 0843519..7f5320e --- a/bash_buddy/core.py +++ b/bash_buddy/core.py @@ -1,9 +1,73 @@ -"""Fill in a module description here""" +from claudette import * +from fastcore.script import * +from functools import partial +from rich.markdown import Markdown -# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb. +import psutil,subprocess,sys -# %% auto 0 -__all__ = ['foo'] +sp = '''You are BashBuddy, a command-line teaching assistant created to help users learn and master bash commands and system administration. Your knowledge is current as of April 2024. -# %% ../nbs/00_core.ipynb 3 -def foo(): pass + +- Receive queries that may include file contents or command output as context +- Maintain a concise, educational tone +- Focus on teaching while solving immediate problems + + + +1. For direct command queries: + - Start with the exact command needed + - Provide a brief, clear explanation + - Show practical examples + - Mention relevant documentation + +2. For queries with context: + - Analyze the provided content first + - Address the specific question about that content + - Suggest relevant commands or actions + - Explain your reasoning briefly + + + + + +- Always warn about destructive operations +- Note when commands require special permissions (e.g., sudo) +- Link to documentation with `man command_name` or `-h`/`--help` +''' + +cli = Client(models[1]) +bb = partial(cli, sp=sp) + +def get_history(n): + try: return subprocess.check_output(['tmux', 'capture-pane', '-p', '-S', f'-{n}'], text=True) + except subprocess.CalledProcessError: return None + +@call_parse +def main( + query: Param('The query to send to the LLM', str, nargs="+"), + NH: bool = False, # Don't include terminal history + n: int = 100, # Number of history lines + o: int = 30, # Number of output lines before piping to less +): + query = ' '.join(query) + ctxt = "" + # Get tmux history if requested and available + if not NH: + history = get_history(n) + if history: ctxt += f"\n{history}\n" + + # Read from stdin if available + if not sys.stdin.isatty(): ctxt += f"\n\n{sys.stdin.read()}" + + r = contents(bb(f"{ctxt}\n\n{query}\n")) + if len(r.splitlines()) > o: + p = subprocess.Popen(['less'], stdin=subprocess.PIPE) + p.communicate(input=r.encode()) + else: print(r) diff --git a/nbs/00_core.ipynb b/nbs/00_core.ipynb deleted file mode 100644 index 6671b71..0000000 --- a/nbs/00_core.ipynb +++ /dev/null @@ -1,61 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# core\n", - "\n", - "> Fill in a module description here" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#| default_exp core" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#| hide\n", - "from nbdev.showdoc import *" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#| export\n", - "def foo(): pass" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#| hide\n", - "import nbdev; nbdev.nbdev_export()" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "python3", - "language": "python", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/nbs/_quarto.yml b/nbs/_quarto.yml deleted file mode 100644 index 59a1e49..0000000 --- a/nbs/_quarto.yml +++ /dev/null @@ -1,22 +0,0 @@ -project: - type: website - -format: - html: - theme: cosmo - css: styles.css - toc: true - keep-md: true - commonmark: default - -website: - twitter-card: true - open-graph: true - repo-actions: [issue] - navbar: - background: primary - search: true - sidebar: - style: floating - -metadata-files: [nbdev.yml, sidebar.yml] \ No newline at end of file diff --git a/nbs/index.ipynb b/nbs/index.ipynb deleted file mode 100644 index 96fd10a..0000000 --- a/nbs/index.ipynb +++ /dev/null @@ -1,192 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#| hide\n", - "from bash_buddy.core import *" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# bash_buddy\n", - "\n", - "> Your favorite AI buddy right in your terminal" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This file will become your README and also the index of your documentation." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Developer Guide" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you are new to using `nbdev` here are some useful pointers to get you started." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Install bash_buddy in Development mode" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "```sh\n", - "# make sure bash_buddy package is installed in development mode\n", - "$ pip install -e .\n", - "\n", - "# make changes under nbs/ directory\n", - "# ...\n", - "\n", - "# compile to have changes apply to bash_buddy\n", - "$ nbdev_prepare\n", - "```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Usage" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Installation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Install latest from the GitHub [repository][repo]:\n", - "\n", - "```sh\n", - "$ pip install git+https://github.com/AnswerDotAI/bash_buddy.git\n", - "```\n", - "\n", - "or from [conda][conda]\n", - "\n", - "```sh\n", - "$ conda install -c AnswerDotAI bash_buddy\n", - "```\n", - "\n", - "or from [pypi][pypi]\n", - "\n", - "\n", - "```sh\n", - "$ pip install bash_buddy\n", - "```\n", - "\n", - "\n", - "[repo]: https://github.com/AnswerDotAI/bash_buddy\n", - "[docs]: https://AnswerDotAI.github.io/bash_buddy/\n", - "[pypi]: https://pypi.org/project/bash_buddy/\n", - "[conda]: https://anaconda.org/AnswerDotAI/bash_buddy" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Documentation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Documentation can be found hosted on this GitHub [repository][repo]'s [pages][docs]. Additionally you can find package manager specific guidelines on [conda][conda] and [pypi][pypi] respectively.\n", - "\n", - "[repo]: https://github.com/AnswerDotAI/bash_buddy\n", - "[docs]: https://AnswerDotAI.github.io/bash_buddy/\n", - "[pypi]: https://pypi.org/project/bash_buddy/\n", - "[conda]: https://anaconda.org/AnswerDotAI/bash_buddy" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## How to use" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Fill me in please! Don't forget code examples:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": null, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "1+1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.8" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/nbs/nbdev.yml b/nbs/nbdev.yml deleted file mode 100644 index 94600d7..0000000 --- a/nbs/nbdev.yml +++ /dev/null @@ -1,9 +0,0 @@ -project: - output-dir: _docs - -website: - title: "bash_buddy" - site-url: "https://AnswerDotAI.github.io/bash_buddy" - description: "Your favorite AI buddy right in your terminal" - repo-branch: main - repo-url: "https://github.com/AnswerDotAI/bash_buddy" diff --git a/nbs/styles.css b/nbs/styles.css deleted file mode 100644 index 66ccc49..0000000 --- a/nbs/styles.css +++ /dev/null @@ -1,37 +0,0 @@ -.cell { - margin-bottom: 1rem; -} - -.cell > .sourceCode { - margin-bottom: 0; -} - -.cell-output > pre { - margin-bottom: 0; -} - -.cell-output > pre, .cell-output > .sourceCode > pre, .cell-output-stdout > pre { - margin-left: 0.8rem; - margin-top: 0; - background: none; - border-left: 2px solid lightsalmon; - border-top-left-radius: 0; - border-top-right-radius: 0; -} - -.cell-output > .sourceCode { - border: none; -} - -.cell-output > .sourceCode { - background: none; - margin-top: 0; -} - -div.description { - padding-left: 2px; - padding-top: 5px; - font-style: italic; - font-size: 135%; - opacity: 70%; -} diff --git a/settings.ini b/settings.ini index 8856bf9..b680283 100644 --- a/settings.ini +++ b/settings.ini @@ -6,7 +6,7 @@ repo = bash_buddy lib_name = %(repo)s version = 0.0.1 -min_python = 3.7 +min_python = 3.10 license = apache2 black_formatting = False @@ -29,7 +29,7 @@ title = %(lib_name)s ### PyPI ### audience = Developers author = ncoop57 -author_email = nacooper01@email.wm.edu +author_email = nc@answer.ai copyright = 2024 onwards, %(author)s description = Your favorite AI buddy right in your terminal keywords = nbdev jupyter notebook python @@ -38,8 +38,8 @@ status = 3 user = AnswerDotAI ### Optional ### -# requirements = fastcore pandas +requirements = claudette fastcore psutil rich # dev_requirements = -# console_scripts = +console_scripts = bb8=bash_buddy.core:main # conda_user = # package_data = \ No newline at end of file