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
99import json , importlib , linecache
1818from fastlite import *
1919from fastcore .xtras import asdict
2020from 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
99109def _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
138150def _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 )
153164def 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
175180def 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