Skip to content

Commit 043c14f

Browse files
authored
Merge pull request #32 from aditya0yadav/main
Add logging of queries and response to Sqlite #27
2 parents 74e6abb + 65f9471 commit 043c14f

File tree

7 files changed

+189
-327
lines changed

7 files changed

+189
-327
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,18 @@ ssage --pid %3 what is happening in this pane?
105105
106106
# Automatically fill in the command to run
107107
ssage --c how can I list all files including the hidden ones?
108+
109+
# Log the model, timestamp, query and response to a local SQLite database
110+
ssage --log "how can i remove the file"
108111
```
109112

110113
The `--pid` flag is particularly useful when you want to analyze content
111114
from a different pane. The pane ID is visible in your tmux status bar
112115
(configured earlier).
113116

117+
The `--log` option saves log data to an SQLite database located at
118+
`~/.shell_sage/log_db/logs.db`.
119+
114120
### Using Alternative Model Providers
115121

116122
ShellSage supports using different LLM providers through base URL
@@ -156,6 +162,7 @@ history_lines = -1 # -1 for all history
156162
# Code display preferences
157163
code_theme = monokai # syntax highlighting theme
158164
code_lexer = python # default code lexer
165+
log = False # Set to true to enable logging by default
159166
```
160167

161168
You can find all of the code theme and code lexer options here:

nbs/00_core.ipynb

Lines changed: 119 additions & 277 deletions
Large diffs are not rendered by default.

nbs/01_config.ipynb

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@
149149
" api_key: str = ''\n",
150150
" history_lines: int = -1\n",
151151
" code_theme: str = \"monokai\"\n",
152-
" code_lexer: str = \"python\""
152+
" code_lexer: str = \"python\"\n",
153+
" log: bool = False"
153154
]
154155
},
155156
{
@@ -161,7 +162,7 @@
161162
{
162163
"data": {
163164
"text/plain": [
164-
"ShellSageConfig(provider='anthropic', model='claude-3-5-sonnet-20241022', base_url='', api_key='', history_lines=-1, code_theme='monokai', code_lexer='python')"
165+
"ShellSageConfig(provider='anthropic', model='claude-3-5-sonnet-20241022', base_url='', api_key='', history_lines=-1, code_theme='monokai', code_lexer='python', log=False)"
165166
]
166167
},
167168
"execution_count": null,
@@ -195,30 +196,11 @@
195196
"execution_count": null,
196197
"id": "efd3d92c",
197198
"metadata": {},
198-
"outputs": [
199-
{
200-
"data": {
201-
"text/plain": [
202-
"{'provider': 'anthropic', 'model': 'claude-3-5-sonnet-20241022', 'base_url': '', 'api_key': '', 'history_lines': '-1', 'code_theme': 'monokai', 'code_lexer': 'python'}"
203-
]
204-
},
205-
"execution_count": null,
206-
"metadata": {},
207-
"output_type": "execute_result"
208-
}
209-
],
199+
"outputs": [],
210200
"source": [
211-
"cfg = get_cfg()\n",
212-
"cfg"
201+
"# cfg = get_cfg()\n",
202+
"# cfg"
213203
]
214-
},
215-
{
216-
"cell_type": "code",
217-
"execution_count": null,
218-
"id": "d5483a5d",
219-
"metadata": {},
220-
"outputs": [],
221-
"source": []
222204
}
223205
],
224206
"metadata": {

nbs/index.ipynb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,14 @@
125125
"\n",
126126
"# Automatically fill in the command to run\n",
127127
"ssage --c how can I list all files including the hidden ones?\n",
128+
"\n",
129+
"# Log the model, timestamp, query and response to a local SQLite database\n",
130+
"ssage --log \"how can i remove the file\"\n",
128131
"```\n",
129132
"\n",
130-
"The `--pid` flag is particularly useful when you want to analyze content from a different pane. The pane ID is visible in your tmux status bar (configured earlier)."
133+
"The `--pid` flag is particularly useful when you want to analyze content from a different pane. The pane ID is visible in your tmux status bar (configured earlier).\n",
134+
"\n",
135+
"The `--log` option saves log data to an SQLite database located at `~/.shell_sage/log_db/logs.db`."
131136
]
132137
},
133138
{
@@ -180,6 +185,7 @@
180185
"# Code display preferences\n",
181186
"code_theme = monokai # syntax highlighting theme\n",
182187
"code_lexer = python # default code lexer\n",
188+
"log = False # Set to true to enable logging by default\n",
183189
"```\n",
184190
"\n",
185191
"You can find all of the code theme and code lexer options here: https://pygments.org/styles/\n",

shell_sage/_modidx.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
'syms': { 'shell_sage.config': { 'shell_sage.config.ShellSageConfig': ('config.html#shellsageconfig', 'shell_sage/config.py'),
99
'shell_sage.config._cfg_path': ('config.html#_cfg_path', 'shell_sage/config.py'),
1010
'shell_sage.config.get_cfg': ('config.html#get_cfg', 'shell_sage/config.py')},
11-
'shell_sage.core': { 'shell_sage.core._aliases': ('core.html#_aliases', 'shell_sage/core.py'),
11+
'shell_sage.core': { 'shell_sage.core.Log': ('core.html#log', 'shell_sage/core.py'),
12+
'shell_sage.core._aliases': ('core.html#_aliases', 'shell_sage/core.py'),
1213
'shell_sage.core._sys_info': ('core.html#_sys_info', 'shell_sage/core.py'),
1314
'shell_sage.core.get_history': ('core.html#get_history', 'shell_sage/core.py'),
1415
'shell_sage.core.get_opts': ('core.html#get_opts', 'shell_sage/core.py'),
@@ -17,4 +18,5 @@
1718
'shell_sage.core.get_res': ('core.html#get_res', 'shell_sage/core.py'),
1819
'shell_sage.core.get_sage': ('core.html#get_sage', 'shell_sage/core.py'),
1920
'shell_sage.core.main': ('core.html#main', 'shell_sage/core.py'),
21+
'shell_sage.core.mk_db': ('core.html#mk_db', 'shell_sage/core.py'),
2022
'shell_sage.core.tmux_history_lim': ('core.html#tmux_history_lim', 'shell_sage/core.py')}}}

shell_sage/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ShellSageConfig:
3434
history_lines: int = -1
3535
code_theme: str = "monokai"
3636
code_lexer: str = "python"
37+
log: bool = False
3738

3839
# %% ../nbs/01_config.ipynb 11
3940
def get_cfg():

shell_sage/core.py

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.
22

33
# %% auto 0
4-
__all__ = ['print', 'sp', 'csp', 'ssp', 'default_cfg', 'clis', 'sps', 'conts', 'p', 'get_pane', 'get_panes', 'tmux_history_lim',
5-
'get_history', 'get_opts', 'get_sage', 'get_res', 'main']
4+
__all__ = ['print', 'sp', 'csp', 'ssp', 'default_cfg', 'clis', 'sps', 'conts', 'p', 'log_path', 'get_pane', 'get_panes',
5+
'tmux_history_lim', 'get_history', 'get_opts', 'get_sage', 'get_res', 'Log', 'mk_db', 'main']
66

77
# %% ../nbs/00_core.ipynb 3
88
from datetime import datetime
@@ -16,6 +16,7 @@
1616
from . import __version__
1717
from .config import *
1818
from subprocess import check_output as co
19+
from fastlite import database
1920

2021
import os,re,subprocess,sys
2122
import claudette as cla, cosette as cos
@@ -200,26 +201,37 @@ def get_res(sage, q, provider, is_command=False):
200201
else: return conts[provider](sage(q))
201202

202203
# %% ../nbs/00_core.ipynb 34
204+
class Log: id:int; timestamp:str; query:str; response:str; model:str; mode:str
205+
206+
log_path = Path("~/.shell_sage/logs/").expanduser()
207+
def mk_db():
208+
log_path.mkdir(parents=True, exist_ok=True)
209+
db = database(log_path / "logs.db")
210+
db.logs = db.create(Log)
211+
return db
212+
213+
# %% ../nbs/00_core.ipynb 37
203214
@call_parse
204215
def main(
205216
query: Param('The query to send to the LLM', str, nargs='+'),
206217
v: Param("Print version", action='version') = '%(prog)s ' + __version__,
207-
pid: str = 'current', # `current`, `all` or tmux pane_id (e.g. %0) for context
208-
skip_system: bool = False, # Whether to skip system information in the AI's context
209-
history_lines: int = None, # Number of history lines. Defaults to tmux scrollback history length
210-
s: bool = False, # Enable sassy mode
211-
c: bool = False, # Enable command mode
212-
provider: str = None, # The LLM Provider
213-
model: str = None, # The LLM model that will be invoked on the LLM provider
218+
pid: str = 'current', # `current`, `all` or tmux pane_id (e.g. %0) for context
219+
skip_system: bool = False, # Whether to skip system information in the AI's context
220+
history_lines: int = None, # Number of history lines. Defaults to tmux scrollback history length
221+
s: bool = False, # Enable sassy mode
222+
c: bool = False, # Enable command mode
223+
log: bool = False, # Enable logging
224+
provider: str = None, # The LLM Provider
225+
model: str = None, # The LLM model that will be invoked on the LLM provider
214226
base_url: str = None,
215227
api_key: str = None,
216-
code_theme: str = None, # The code theme to use when rendering ShellSage's responses
217-
code_lexer: str = None, # The lexer to use for inline code markdown blocks
218-
verbosity: int = 0 # Level of verbosity (0 or 1)
228+
code_theme: str = None, # The code theme to use when rendering ShellSage's responses
229+
code_lexer: str = None, # The lexer to use for inline code markdown blocks
230+
verbosity: int = 0 # Level of verbosity (0 or 1)
219231
):
220232
opts = get_opts(history_lines=history_lines, provider=provider, model=model,
221233
base_url=base_url, api_key=api_key, code_theme=code_theme,
222-
code_lexer=code_lexer)
234+
code_lexer=code_lexer, log=log)
223235

224236
mode = 'default'
225237
if s: mode = 'sassy'
@@ -228,32 +240,42 @@ def main(
228240
raise Exception('Must be in a tmux session to use command mode.')
229241
mode = 'command'
230242

231-
if verbosity>0:
232-
print(f"{datetime.now()} | Starting ShellSage request with options {opts}")
233-
md = partial(Markdown, code_theme=opts.code_theme, inline_code_lexer=opts.code_lexer, inline_code_theme=opts.code_theme)
243+
if verbosity > 0: print(f"{datetime.now()} | Starting ShellSage request with options {opts}")
244+
245+
md = partial(Markdown, code_theme=opts.code_theme, inline_code_lexer=opts.code_lexer,
246+
inline_code_theme=opts.code_theme)
234247
query = ' '.join(query)
235248
ctxt = '' if skip_system else _sys_info()
236249

237250
# Get tmux history if in a tmux session
238251
if os.environ.get('TMUX'):
239-
if verbosity>0: print(f"{datetime.now()} | Adding TMUX history to prompt")
252+
if verbosity > 0: print(f"{datetime.now()} | Adding TMUX history to prompt")
240253
if opts.history_lines is None or opts.history_lines < 0:
241254
opts.history_lines = tmux_history_lim()
242-
history = get_history(opts.history_lines,pid)
255+
history = get_history(opts.history_lines, pid)
243256
if history: ctxt += f'<terminal_history>\n{history}\n</terminal_history>'
244257

245258
# Read from stdin if available
246-
if not sys.stdin.isatty():
247-
if verbosity>0: print(f"{datetime.now()} | Adding stdin to prompt")
259+
if not sys.stdin.isatty():
260+
if verbosity > 0: print(f"{datetime.now()} | Adding stdin to prompt")
248261
ctxt += f'\n<context>\n{sys.stdin.read()}</context>'
249262

250-
if verbosity>0: print(f"{datetime.now()} | Finalizing prompt")
263+
if verbosity > 0: print(f"{datetime.now()} | Finalizing prompt")
264+
251265
query = f'{ctxt}\n<query>\n{query}\n</query>'
252266
query = [mk_msg(query)] if opts.provider == 'openai' else query
253267

254-
if verbosity>0: print(f"{datetime.now()} | Sending prompt to model")
255-
sage = get_sage(opts.provider, opts.model, opts.base_url, opts.api_key, mode)
256-
res = get_res(sage, query, opts.provider, is_command=c)
268+
if verbosity > 0:
269+
print(f"{datetime.now()} | Sending prompt to model")
270+
271+
sage = get_sage(opts.provider, opts.model, opts.base_url, opts.api_key, mode)
272+
res = get_res(sage, query, opts.provider, is_command=c)
257273

274+
# Handle logging if the log flag is set
275+
if opts.log:
276+
db = mk_db()
277+
db.logs.insert(Log(timestamp=datetime.now().isoformat(), query=query,
278+
response=res, model=opts.model, mode=mode))
279+
258280
if c: co(['tmux', 'send-keys', res], text=True)
259281
else: print(md(res))

0 commit comments

Comments
 (0)