dialoghelper

Re-export asdict:


source

get_db

 get_db (ns:dict=None)
# db.conn.filename
db = get_db(globals())
dlg = db.t.dialog.fetchone()
dlg
Dialog(id=1, name='test dialog', mode=2)

source

find_var

 find_var (var:str)

Search for var in all frames of the call stack

a = 1
find_var('a')
1

source

find_dialog_id

 find_dialog_id ()

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

__dialog_id = dlg.id
find_dialog_id()
1

source

find_msgs

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

Find messages in current specific dialog that contain the given information. To refer to a message found later, use its sid field (which is the pk).

Type Default Details
pattern str Optional text to search for
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?
found = find_msgs('hello')
found[0]['content']
'hello world'
find_msgs(msg_type='prompt', include_output=False)
[{'sid': '_W_YSjhjCR5ehQup9F74xEQ',
  'mid': 'msg-a0',
  'content': 'Concisely, what is *one* simple way to reverse a list?',
  'input_tokens': 24,
  'output_tokens': 54,
  'msg_type': 'prompt',
  'time_run': '',
  'is_exported': 0,
  'skipped': 0,
  'did': 1,
  'i_collapsed': 0,
  'o_collapsed': 0,
  'header_collapsed': 0,
  'pinned': 0}]

source

find_msg_id

 find_msg_id ()

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

__msg_id = found[0]['sid']
find_msg_id()
'_Q7ejppqNSgOYDXtx2PVkEw'

source

read_msg_ids

 read_msg_ids ()

Get all ids in current dialog.


source

msg_idx

 msg_idx ()

Get relative index of current message in dialog.

ids,idx = msg_idx()
idx
1

source

read_msg

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

Get the message indexed in the current dialog.

Type Default Details
n int -1 Message index (if relative, +ve is downwards)
relative bool True Is n relative to current message (True) or absolute (False)?
# Previous message relative to current
read_msg(-1)
Message(sid='_W_YSjhjCR5ehQup9F74xEQ', mid='msg-a0', content='Concisely, what is *one* simple way to reverse a list?', output='You can reverse a list using the slice notation with a step of -1:\n\n```python\nmy_list[::-1]\n```\n\nWould you like me to explain how this works?', input_tokens=24, output_tokens=54, msg_type='prompt', time_run='', is_exported=0, skipped=0, did=1, i_collapsed=0, o_collapsed=0, header_collapsed=0, pinned=0)
# Last message in dialog
read_msg(-1, relative=False)
Message(sid='_7BuMofkeTUyf9Jt4iUY_hQ', mid='msg-a3', content='1+1 / 12', output='[{"output_type": "execute_result", "metadata": {}, "data": {"text/plain": ["1.0833333333333333"]}, "execution_count": 6}]', input_tokens=9, output_tokens=30, msg_type='code', time_run='', is_exported=0, skipped=0, did=1, i_collapsed=0, o_collapsed=0, header_collapsed=0, pinned=0)

source

add_msg

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

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).

Type Default Details
content str Content of the message (i.e the message prompt, code, or note text)
msg_type str note Message type, can be ‘code’, ‘note’, or ‘prompt’
output str For prompts/code, initial output
placement str add_after Can be ‘add_after’, ‘add_before’, ‘update’, ‘at_start’, ‘at_end’
sid str None sid (stable id – pk) of message that placement is relative to (if None, uses current message)
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?
header_collapsed int | None 0 Collapse heading section?
pinned int | None 0 Pin to context?

source

update_msg

 update_msg (msg:Optional[Dict]=None, sid:str=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, header_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. Be sure to pass a sid (stable id – the pk) not a mid (which is used only for sorting, and can change). Only include parameters to update–missing ones will be left unchanged.

Type Default Details
msg Optional None Dictionary of field keys/values to update
sid str None sid (stable id – pk) of message to update (if None, uses current message)
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?
header_collapsed int | None None Collapse heading section?
pinned int | None None Pin to context?

source

add_html

 add_html (html:str)

Dynamically add HTML to the current web page. Supports HTMX attrs too.

Type Details
html str HTML to add to the DOM

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)
testfoo='testbar'

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

import_gist

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

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_gist(gistid)
importtest.testfoo
'testbar'

Importing and Exporting Dialogs

We use json serialization of list[dict] to represent a dialogue where dict is a serialized solveit.db_dc Message.

Supported Fields

from dialoghelper.db_dc import Message
Message.__annotations__
{'sid': str | None,
 'mid': str | None,
 'content': str | None,
 'output': str | None,
 'input_tokens': int | None,
 'output_tokens': int | None,
 'msg_type': str | None,
 'time_run': str | None,
 'is_exported': int | None,
 'skipped': int | None,
 'did': int | None,
 'i_collapsed': int | None,
 'o_collapsed': int | None,
 'header_collapsed': int | None,
 'pinned': int | None}

When exporting and importing we intentionally leave out these fields: - sid - to avoid duplicating sids across dialogues - mid - to ensure mid respects ordering in the new dialogue - did - since the dialogue will be imported into a different dialogue - time_run - since a dialogue may be imported/exported to other environments where execution time is not comparable.

The following are required: - content - msg_type - output

All other fields (i_collapsed, header_collapsed, … etc) are technically optional. By default we read them on import if they are available. However, there may be other dialog-consuming applications (shell sage, discord buddy, others) we want to share dialogues with where these fields are not supported by the interface.

Attachments

For now attachments are not included in the importing and exporting.

Attachments could be added as a future enhanement but would add some additional design considerations - if attachments refer to external artifacts, do the artifacts get copied? Do they get bundled as part of the export artifact? Do we create a new artifact entry with a new sid for the new dialogue or do we share artifacts?

Implementation


source

export_dialog

 export_dialog (filename:str, did:int=None)

Export dialog messages and optionally attachments to JSON

try:
    test_path = Path('test.json')
    export_dialog(test_path)
    test_msgs = test_path.read_json()
    print(test_msgs['messages'][1]['content'])
finally: test_path.unlink(missing_ok=True)
hello world

source

import_dialog

 import_dialog (fname, add_header=True)

Import dialog messages from JSON file using add_msg