dialoghelper

Re-export asdict:


source

find_var

 find_var (var:str)

Search for var in all frames of the call stack

a = 1
find_var('a')
1

source

call_endp

 call_endp (path, json=False, raiseex=False, **data)

source

curr_dialog

 curr_dialog (with_messages:bool=False)

Get the current dialog info.

Type Default Details
with_messages bool False Include messages as well?

source

find_msgs

 find_msgs (re_pattern:str='', msg_type:str=None, limit:int=None,
            include_output:bool=True)

Find list[dict] of messages in current specific dialog that contain the given information. To refer to a message found later, use its id field.

Type Default Details
re_pattern str Optional regex to search for (re.DOTALL+re.MULTILINE is used)
msg_type str None optional limit by message type (‘code’, ‘note’, or ‘prompt’)
limit int None Optionally limit number of returned items
include_output bool True Include output in returned dict?
# NB: must have a dialogue open including a message with this text in its content
txt = 'tools'
found = find_msgs(txt)
found[0]['content']
'Available tools: &`[add,mult,weather,username]`. Use only where required or requested.'

source

find_msg_id

 find_msg_id ()

*Get the message id by searching the call stack for __dialog_id.*

__msg_id = found[0]['id']
find_msg_id()
'_7e1900cd'

source

msg_idx

 msg_idx (msgid=None)

Get absolute index of message in dialog.

Type Default Details
msgid NoneType None Message id to find (defaults to current message)
msg_idx()
2

source

read_msg

 read_msg (n:int=-1, msgid=None, relative:bool=True)

Get the Message object indexed in the current dialog.

Type Default Details
n int -1 Message index (if relative, +ve is downwards)
msgid NoneType None Message id to find (defaults to current message)
relative bool True Is n relative to current message (True) or absolute (False)?
# Previous message relative to current
read_msg(-1)['content']
'from solveit.core import rt,app\nfrom fasthtml.common import *\n\ndef add(x:float, y:float):\n    "Add x and y"\n    return x+y\n\ndef mult(x:float, y:float):\n    "Multiply x and y"\n    return x*y\n\ndef weather(city:str):\n    "Get weather for city"\n    return f"Sunny and clear"\n\ndef username():\n    "Get username"\n    return "jph00"\n'
# Last message in dialog
read_msg(-1, relative=False)['content']
'import dialoghelper'

source

add_html

 add_html (content:str)

Send HTML to the browser to be swapped into the DOM

Type Details
content str The HTML to send to the client (generally should include hx-swap-oob)
from fasthtml.common import *
add_html(Div(P('Hi'), hx_swap_oob='beforeend:#dialog-container'))

source

del_msg

 del_msg (msgid:str=None)

Delete a message from the dialog.

Type Default Details
msgid str None id of message to delete
# msg = find_msgs('aaaaa')[0]
# _id = msg['id']
# _id
# del_msg(_id)

source

run_msg

 run_msg (msgid:str=None)

Adds a message to the run queue. Use read_msg to see the output once it runs.

Type Default Details
msgid str None id of message to execute
run_msg('_312e03f5')
'{"status":"queued"}'

source

add_msg

 add_msg (content:str, placement:str='add_after', msgid:str=None,
          msg_type:str='note', output:str='', time_run:str|None='',
          is_exported:int|None=0, skipped:int|None=0,
          i_collapsed:int|None=0, o_collapsed:int|None=0,
          heading_collapsed:int|None=0, pinned:int|None=0)

Add/update a message to the queue to show after code execution completes.

Type Default Details
content str Content of the message (i.e the message prompt, code, or note text)
placement str add_after Can be ‘add_after’, ‘add_before’, ‘at_start’, ‘at_end’
msgid str None id of message that placement is relative to (if None, uses current message)
msg_type str note Message type, can be ‘code’, ‘note’, or ‘prompt’
output str For prompts/code, initial output
time_run str | None When was message executed
is_exported int | None 0 Export message to a module?
skipped int | None 0 Hide message from prompt?
i_collapsed int | None 0 Collapse input?
o_collapsed int | None 0 Collapse output?
heading_collapsed int | None 0 Collapse heading section?
pinned int | None 0 Pin to context?
_id = add_msg('testing')
del_msg(_id)
_id = _add_msg_unsafe('1+1', run=True, msg_type='code')
del_msg(_id)
_id = _add_msg_unsafe('Hi', run=True, msg_type='prompt')
del_msg(_id)

source

update_msg

 update_msg (msgid:str=None, msg:Optional[Dict]=None,
             content:str|None=None, msg_type:str|None=None,
             output:str|None=None, time_run:str|None=None,
             is_exported:int|None=None, skipped:int|None=None,
             i_collapsed:int|None=None, o_collapsed:int|None=None,
             heading_collapsed:int|None=None, pinned:int|None=None)

Update an existing message. Provide either msg OR field key/values to update. Use content param to update contents. Only include parameters to update–missing ones will be left unchanged.

Type Default Details
msgid str None id of message to update (if None, uses current message)
msg Optional None Dictionary of field keys/values to update
content str | None None Content of the message (i.e the message prompt, code, or note text)
msg_type str | None None Message type, can be ‘code’, ‘note’, or ‘prompt’
output str | None None For prompts/code, the output
time_run str | None None When was message executed
is_exported int | None None Export message to a module?
skipped int | None None Hide message from prompt?
i_collapsed int | None None Collapse input?
o_collapsed int | None None Collapse output?
heading_collapsed int | None None Collapse heading section?
pinned int | None None Pin to context?
_id = add_msg('testing')
_id
'_2d24071a'
update_msg(_id, content='toasting')
'_2d24071a'
update_msg(_id, skipped=1)
'_2d24071a'
_id
'_2d24071a'
del_msg(_id)

Gists


source

load_gist

 load_gist (gist_id:str)

Retrieve a gist

gistid = 'jph00/e7cfd4ded593e8ef6217e78a0131960c'
gist = load_gist(gistid)
gist.html_url
'https://gist.github.com/jph00/e7cfd4ded593e8ef6217e78a0131960c'

source

gist_file

 gist_file (gist_id:str)

Get the first file from a gist

gfile = gist_file(gistid)
print(gfile.content[:100]+"…")
"This is a test module which makes some simple tools available."
__all__ = ["hi","whoami"]

testfoo=…

source

import_string

 import_string (code:str, name:str)
Type Details
code str Code to import as a module
name str Name of module to create

source

is_usable_tool

 is_usable_tool (func:<built-infunctioncallable>)

True if the function has a docstring and all parameters have types, meaning that it can be used as an LLM tool.

def hi(who:str):
    "Say hi to `who`"
    return f"Hello {who}"

def hi2(who):
    "Say hi to `who`"
    return f"Hello {who}"

def hi3(who:str):
    return f"Hello {who}"

bye = "bye"
assert is_usable_tool(hi)
assert not is_usable_tool(hi2)
assert not is_usable_tool(hi3)
assert not is_usable_tool(bye)

source

mk_toollist

 mk_toollist (syms)
Markdown(mk_toollist([hi]))
  • &hi: Say hi to who

source

import_gist

 import_gist (gist_id:str, mod_name:str=None, add_global:bool=True,
              import_wildcard:bool=False, create_msg:bool=False)

Import gist directly from string without saving to disk

Type Default Details
gist_id str user/id or just id of gist to import as a module
mod_name str None module name to create (taken from gist filename if not passed)
add_global bool True add module to caller’s globals?
import_wildcard bool False import all exported symbols to caller’s globals
create_msg bool False Add a message that lists usable tools
import_gist(gistid)
importtest.testfoo
'testbar'
import_gist.__doc__
'Import gist directly from string without saving to disk'
import_gist(gistid, import_wildcard=True)
importtest.testfoo
'testbar'
hi("Sarah")
'Hello Sarah'
importtest.__all__
['hi', 'whoami']

Tool info

This is how we get a superset of tools to include:


source

tool_info

 tool_info ()
for o in _all:
    s = globals()[o]
    if s.__name__[0]=='_' or not s.__doc__: continue
    print(f'- &`{s.__name__}`: {s.__doc__}')
- &`find_var`: Search for var in all frames of the call stack
- &`curr_dialog`: Get the current dialog info.
- &`find_msgs`: Find `list[dict]` of messages in current specific dialog that contain the given information. To refer to a message found later, use its `id` field.
- &`find_msg_id`: Get the message id by searching the call stack for __dialog_id.
- &`msg_idx`: Get absolute index of message in dialog.
- &`read_msg`: Get the `Message` object indexed in the current dialog.
- &`add_html`: Send HTML to the browser to be swapped into the DOM
- &`del_msg`: Delete a message from the dialog. Be sure to pass a `sid`, not a `mid`.
- &`add_msg`: Add/update a message to the queue to show after code execution completes.
- &`update_msg`: Update an existing message. Provide either `msg` OR field key/values to update.
    Use `content` param to update contents.
    Only include parameters to update--missing ones will be left unchanged.
- &`load_gist`: Retrieve a gist
- &`gist_file`: Get the first file from a gist
- &`is_usable_tool`: True if the function has a docstring and all parameters have types, meaning that it can be used as an LLM tool.
- &`import_gist`: Import gist directly from string without saving to disk
- &`asdict`: Convert `o` to a `dict`, supporting dataclasses, namedtuples, iterables, and `__dict__` attrs.