Skip to content

Commit 2235e4f

Browse files
committed
fixes #16
1 parent 4fc4b50 commit 2235e4f

3 files changed

Lines changed: 268 additions & 59 deletions

File tree

dialoghelper/_modidx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
'syms': { 'dialoghelper.core': { 'dialoghelper.core._add_msg_unsafe': ('core.html#_add_msg_unsafe', 'dialoghelper/core.py'),
99
'dialoghelper.core._msg': ('core.html#_msg', 'dialoghelper/core.py'),
1010
'dialoghelper.core._umsg': ('core.html#_umsg', 'dialoghelper/core.py'),
11-
'dialoghelper.core.add_html': ('core.html#add_html', 'dialoghelper/core.py'),
1211
'dialoghelper.core.add_msg': ('core.html#add_msg', 'dialoghelper/core.py'),
12+
'dialoghelper.core.del_msg': ('core.html#del_msg', 'dialoghelper/core.py'),
1313
'dialoghelper.core.export_dialog': ('core.html#export_dialog', 'dialoghelper/core.py'),
1414
'dialoghelper.core.find_dialog_id': ('core.html#find_dialog_id', 'dialoghelper/core.py'),
1515
'dialoghelper.core.find_msg_id': ('core.html#find_msg_id', 'dialoghelper/core.py'),
@@ -26,5 +26,6 @@
2626
'dialoghelper.core.msg_idx': ('core.html#msg_idx', 'dialoghelper/core.py'),
2727
'dialoghelper.core.read_msg': ('core.html#read_msg', 'dialoghelper/core.py'),
2828
'dialoghelper.core.read_msg_ids': ('core.html#read_msg_ids', 'dialoghelper/core.py'),
29+
'dialoghelper.core.tool_info': ('core.html#tool_info', 'dialoghelper/core.py'),
2930
'dialoghelper.core.update_msg': ('core.html#update_msg', 'dialoghelper/core.py')},
3031
'dialoghelper.db_dc': {}}}

dialoghelper/core.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
# %% auto 0
44
__all__ = ['Placements', 'empty', 'get_db', 'find_var', 'find_dialog_id', 'find_msgs', 'find_msg_id', 'read_msg_ids', 'msg_idx',
5-
'read_msg', 'add_msg', 'update_msg', 'add_html', 'load_gist', 'gist_file', 'import_string', 'is_usable_tool',
6-
'mk_toollist', 'import_gist', 'export_dialog', 'import_dialog', 'asdict']
5+
'read_msg', 'del_msg', 'add_msg', 'update_msg', 'load_gist', 'gist_file', 'import_string', 'is_usable_tool',
6+
'mk_toollist', 'import_gist', 'export_dialog', 'import_dialog', 'tool_info', 'asdict']
77

88
# %% ../nbs/00_core.ipynb
99
import json, importlib, linecache
@@ -18,6 +18,9 @@
1818
from fastlite import *
1919
from fastcore.xtras import asdict
2020
from inspect import currentframe,Parameter,signature
21+
from httpx import get as xget, post as xpost
22+
from .core import __all__ as _all
23+
from IPython.display import display,Markdown
2124

2225
# %% ../nbs/00_core.ipynb
2326
_all_ = ["asdict"]
@@ -93,7 +96,14 @@ def read_msg(n:int=-1, # Message index (if relative, +ve is downwards)
9396
if not 0<=idx<len(ids): return None
9497
else: idx = n
9598
db = get_db()
96-
return db.t.message.fetchone('sid=?', [ids[idx]])
99+
return db.t.message.selectone('sid=?', [ids[idx]])
100+
101+
# %% ../nbs/00_core.ipynb
102+
def del_msg(
103+
sid:str=None, # sid (stable id -- pk) of message that placement is relative to (if None, uses current message)
104+
):
105+
"Delete a message from the dialog. Be sure to pass a `sid`, not a `mid`."
106+
xpost('http://localhost:5001/rm_msg_', data=dict(msid=sid)).raise_for_status()
97107

98108
# %% ../nbs/00_core.ipynb
99109
def _msg(
@@ -119,9 +129,11 @@ def add_msg(
119129
**kwargs
120130
):
121131
"Add/update a message to the queue to show after code execution completes. Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change)."
122-
assert msg_type in ('note', 'code', 'prompt'), "msg_type must be 'code', 'note', or 'prompt'."
123-
assert msg_type not in ('note') or not output, "'note' messages cannot have an output."
124-
run_cmd('add_msg', content=content, msg_type=msg_type, output=output, placement=placement, sid=sid, **kwargs)
132+
if msg_type not in ('note', 'code', 'prompt'): return "msg_type must be 'code', 'note', or 'prompt'."
133+
if msg_type=='note' and output: return "note messages cannot have an output."
134+
if not sid: sid = find_msg_id()
135+
data = dict(content=content, msg_type=msg_type, output=output, placement=placement, sid=sid, **kwargs)
136+
return xpost('http://localhost:5001/add_relative_', data=data).text
125137

126138
# %% ../nbs/00_core.ipynb
127139
@delegates(add_msg)
@@ -136,7 +148,6 @@ def _add_msg_unsafe(
136148

137149
# %% ../nbs/00_core.ipynb
138150
def _umsg(
139-
content:str|None = None, # Content of the message (i.e the message prompt, code, or note text)
140151
msg_type: str|None = None, # Message type, can be 'code', 'note', or 'prompt'
141152
output:str|None = None, # For prompts/code, the output
142153
time_run: str | None = None, # When was message executed
@@ -151,8 +162,9 @@ def _umsg(
151162
# %% ../nbs/00_core.ipynb
152163
@delegates(_umsg)
153164
def update_msg(
154-
msg:Optional[Dict]=None, # Dictionary of field keys/values to update
155165
sid:str=None, # sid (stable id -- pk) of message to update (if None, uses current message)
166+
content:str|None = None, # Content of the message (i.e the message prompt, code, or note text)
167+
msg:Optional[Dict]=None, # Dictionary of field keys/values to update
156168
**kwargs):
157169
"""Update an existing message. Provide either `msg` OR field key/values to update.
158170
Use `content` param to update contents. Be sure to pass a `sid` (stable id -- the pk) not a `mid`
@@ -162,14 +174,7 @@ def update_msg(
162174
sid = kw.pop('sid', sid)
163175
if not sid: raise TypeError("update_msg needs either a dict message or `sid=...`")
164176
kw.pop('did', None)
165-
run_cmd('add_msg', placement='update', sid=sid, **kw)
166-
167-
# %% ../nbs/00_core.ipynb
168-
def add_html(
169-
html:str, # HTML to add to the DOM
170-
):
171-
"Dynamically add HTML to the current web page. Supports HTMX attrs too."
172-
run_cmd('add_ft', html=html)
177+
add_msg(content, placement='update', sid=sid, **kw)
173178

174179
# %% ../nbs/00_core.ipynb
175180
def load_gist(gist_id:str):
@@ -262,3 +267,16 @@ def import_dialog(fname, add_header=True):
262267
add_msg(msg.get('content',''), msg.get('msg_type','note'), msg.get('output',''), 'at_end', **opts)
263268
if add_header: add_msg(f"# Imported Dialog `{fname}`", 'note', placement='at_end')
264269
return f"Imported {len(data['messages'])} messages"
270+
271+
# %% ../nbs/00_core.ipynb
272+
def tool_info():
273+
cts='''Tools available from `dialoghelper`:
274+
275+
- &`find_dialog_id`: Get the current dialog id.
276+
- &`find_msg_id`: Get the current message id.
277+
- &`find_msgs`: Find messages in current specific dialog that contain the given information.
278+
- &`read_msg`: Get the message indexed in the current dialog.
279+
- &`del_msg`: Delete a message from the dialog.
280+
- &`add_msg`: Add/update a message to the queue to show after code execution completes.
281+
- &`update_msg`: Update an existing message.'''
282+
add_msg(cts)

0 commit comments

Comments
 (0)