dialoghelper 0.0.17__py3-none-any.whl → 0.0.18__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dialoghelper might be problematic. Click here for more details.

dialoghelper/__init__.py CHANGED
@@ -1,2 +1,2 @@
1
- __version__ = "0.0.17"
1
+ __version__ = "0.0.18"
2
2
  from .core import *
dialoghelper/_modidx.py CHANGED
@@ -6,19 +6,15 @@ d = { 'settings': { 'branch': 'main',
6
6
  'git_url': 'https://github.com/AnswerDotAI/dialoghelper',
7
7
  'lib_path': 'dialoghelper'},
8
8
  'syms': { 'dialoghelper.core': { 'dialoghelper.core._add_msg_unsafe': ('core.html#_add_msg_unsafe', 'dialoghelper/core.py'),
9
- 'dialoghelper.core._msg': ('core.html#_msg', 'dialoghelper/core.py'),
10
9
  'dialoghelper.core._umsg': ('core.html#_umsg', 'dialoghelper/core.py'),
11
10
  'dialoghelper.core.add_html': ('core.html#add_html', 'dialoghelper/core.py'),
12
11
  'dialoghelper.core.add_msg': ('core.html#add_msg', 'dialoghelper/core.py'),
12
+ 'dialoghelper.core.call_endp': ('core.html#call_endp', 'dialoghelper/core.py'),
13
13
  'dialoghelper.core.del_msg': ('core.html#del_msg', 'dialoghelper/core.py'),
14
- 'dialoghelper.core.export_dialog': ('core.html#export_dialog', 'dialoghelper/core.py'),
15
- 'dialoghelper.core.find_dialog_id': ('core.html#find_dialog_id', 'dialoghelper/core.py'),
16
14
  'dialoghelper.core.find_msg_id': ('core.html#find_msg_id', 'dialoghelper/core.py'),
17
15
  'dialoghelper.core.find_msgs': ('core.html#find_msgs', 'dialoghelper/core.py'),
18
16
  'dialoghelper.core.find_var': ('core.html#find_var', 'dialoghelper/core.py'),
19
- 'dialoghelper.core.get_db': ('core.html#get_db', 'dialoghelper/core.py'),
20
17
  'dialoghelper.core.gist_file': ('core.html#gist_file', 'dialoghelper/core.py'),
21
- 'dialoghelper.core.import_dialog': ('core.html#import_dialog', 'dialoghelper/core.py'),
22
18
  'dialoghelper.core.import_gist': ('core.html#import_gist', 'dialoghelper/core.py'),
23
19
  'dialoghelper.core.import_string': ('core.html#import_string', 'dialoghelper/core.py'),
24
20
  'dialoghelper.core.is_usable_tool': ('core.html#is_usable_tool', 'dialoghelper/core.py'),
@@ -26,7 +22,6 @@ d = { 'settings': { 'branch': 'main',
26
22
  'dialoghelper.core.mk_toollist': ('core.html#mk_toollist', 'dialoghelper/core.py'),
27
23
  'dialoghelper.core.msg_idx': ('core.html#msg_idx', 'dialoghelper/core.py'),
28
24
  'dialoghelper.core.read_msg': ('core.html#read_msg', 'dialoghelper/core.py'),
29
- 'dialoghelper.core.read_msg_ids': ('core.html#read_msg_ids', 'dialoghelper/core.py'),
30
25
  'dialoghelper.core.tool_info': ('core.html#tool_info', 'dialoghelper/core.py'),
31
26
  'dialoghelper.core.update_msg': ('core.html#update_msg', 'dialoghelper/core.py')},
32
27
  'dialoghelper.db_dc': {}}}
dialoghelper/core.py CHANGED
@@ -1,9 +1,9 @@
1
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.
2
2
 
3
3
  # %% auto 0
4
- __all__ = ['Placements', 'empty', 'get_db', 'find_var', 'find_dialog_id', 'find_msgs', 'find_msg_id', 'read_msg_ids', 'msg_idx',
5
- 'read_msg', 'add_html', 'del_msg', 'add_msg', 'update_msg', 'load_gist', 'gist_file', 'import_string',
6
- 'is_usable_tool', 'mk_toollist', 'import_gist', 'export_dialog', 'import_dialog', 'tool_info', 'asdict']
4
+ __all__ = ['Placements', 'empty', 'find_var', 'call_endp', 'find_msgs', 'find_msg_id', 'msg_idx', 'read_msg', 'add_html',
5
+ 'del_msg', 'add_msg', 'update_msg', 'load_gist', 'gist_file', 'import_string', 'is_usable_tool',
6
+ 'mk_toollist', 'import_gist', 'tool_info', 'asdict']
7
7
 
8
8
  # %% ../nbs/00_core.ipynb
9
9
  import json, importlib, linecache
@@ -26,17 +26,6 @@ from IPython.display import display,Markdown
26
26
  # %% ../nbs/00_core.ipynb
27
27
  _all_ = ["asdict"]
28
28
 
29
- # %% ../nbs/00_core.ipynb
30
- def get_db(ns:dict=None):
31
- app_path = Path('/app') if Path('/.dockerenv').exists() else Path('.')
32
- if os.environ.get('IN_SOLVEIT', False): dataparent,nm = app_path, 'data.db'
33
- else: dataparent,nm = Path('..'),'dev_data.db'
34
- db = database(dataparent/'data'/nm)
35
- dcs = [o for o in all_dcs(db) if o.__name__[0]!='_']
36
- if ns:
37
- for o in dcs: ns[o.__name__]=o
38
- return db
39
-
40
29
  # %% ../nbs/00_core.ipynb
41
30
  def find_var(var:str):
42
31
  "Search for var in all frames of the call stack"
@@ -48,22 +37,20 @@ def find_var(var:str):
48
37
  raise ValueError(f"Could not find {var} in any scope")
49
38
 
50
39
  # %% ../nbs/00_core.ipynb
51
- def find_dialog_id():
52
- "Get the dialog id by searching the call stack for __dialog_id."
53
- return find_var('__dialog_id')
40
+ def call_endp(path, json=False, raiseex=False, **data):
41
+ res = xpost(f'http://localhost:5001/{path}', data=data)
42
+ if raiseex: res.raise_for_status()
43
+ return res.json() if json else res.text
54
44
 
55
45
  # %% ../nbs/00_core.ipynb
56
46
  def find_msgs(
57
- pattern:str='', # Optional text to search for
47
+ re_pattern:str='', # Optional regex to search for (re.DOTALL+re.MULTILINE is used)
58
48
  msg_type:str=None, # optional limit by message type ('code', 'note', or 'prompt')
59
49
  limit:int=None, # Optionally limit number of returned items
60
50
  include_output:bool=True # Include output in returned dict?
61
51
  )->list[dict]:
62
- "Find `list[dict]` of 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)."
63
- did = find_dialog_id()
64
- db = get_db()
65
- res = db.t.message('did=? AND content LIKE ? ORDER BY mid', [did, f'%{pattern}%'], limit=limit)
66
- res = [asdict(o) for o in res if not msg_type or (msg_type==o.msg_type)]
52
+ "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."
53
+ res = call_endp('find_msgs_', json=True, re_pattern=re_pattern, msg_type=msg_type, limit=limit)['msgs']
67
54
  if not include_output:
68
55
  for o in res: o.pop('output', None)
69
56
  return res
@@ -74,47 +61,45 @@ def find_msg_id():
74
61
  return find_var('__msg_id')
75
62
 
76
63
  # %% ../nbs/00_core.ipynb
77
- def read_msg_ids()->list[str]:
78
- "Get all ids in current dialog."
79
- did = find_dialog_id()
80
- db = get_db()
81
- return [o.sid for o in db.t.message('did=?', [did], select='sid', order_by='mid')]
82
-
83
- # %% ../nbs/00_core.ipynb
84
- def msg_idx():
85
- "Get relative index of current message in dialog."
86
- ids = read_msg_ids()
87
- return ids,ids.index(find_msg_id())
64
+ def msg_idx(
65
+ msgid=None # Message id to find (defaults to current message)
66
+ ):
67
+ "Get relative index of message in dialog."
68
+ if not msgid: msgid = find_msg_id()
69
+ return call_endp('msg_idx_', json=True, msgid=msgid)['msgid']
88
70
 
89
71
  # %% ../nbs/00_core.ipynb
90
- def read_msg(n:int=-1, # Message index (if relative, +ve is downwards)
91
- relative:bool=True # Is `n` relative to current message (True) or absolute (False)?
72
+ def read_msg(
73
+ n:int=-1, # Message index (if relative, +ve is downwards)
74
+ msgid=None, # Message id to find (defaults to current message)
75
+ relative:bool=True # Is `n` relative to current message (True) or absolute (False)?
92
76
  ):
93
77
  "Get the `Message` object indexed in the current dialog."
94
- ids,idx = msg_idx()
95
- if relative:
96
- idx = idx+n
97
- if not 0<=idx<len(ids): return None
98
- else: idx = n
99
- db = get_db()
100
- return db.t.message.selectone('sid=?', [ids[idx]])
78
+ if not msgid: msgid = find_msg_id()
79
+ return call_endp('read_msg_', json=True, msgid=msgid, n=n, relative=relative)['msg']
101
80
 
102
81
  # %% ../nbs/00_core.ipynb
103
82
  def add_html(
104
83
  content:str, # The HTML to send to the client (generally should include hx-swap-oob)
105
84
  ):
106
85
  "Send HTML to the browser to be swapped into the DOM"
107
- xpost('http://localhost:5001/add_html_', data=dict(content=to_xml(content)))
86
+ call_endp('add_html_', content=to_xml(content))
108
87
 
109
88
  # %% ../nbs/00_core.ipynb
110
89
  def del_msg(
111
- sid:str=None, # sid (stable id -- pk) of message that placement is relative to (if None, uses current message)
90
+ msgid:str=None, # id of message that placement is relative to (if None, uses current message)
112
91
  ):
113
92
  "Delete a message from the dialog. Be sure to pass a `sid`, not a `mid`."
114
- xpost('http://localhost:5001/rm_msg_', data=dict(msid=sid)).raise_for_status()
93
+ call_endp('rm_msg_', raiseex=True, msid=msgid)
94
+
95
+ # %% ../nbs/00_core.ipynb
96
+ Placements = str_enum('Placements', 'add_after', 'add_before', 'at_start', 'at_end')
115
97
 
116
98
  # %% ../nbs/00_core.ipynb
117
- def _msg(
99
+ def add_msg(
100
+ content:str, # Content of the message (i.e the message prompt, code, or note text)
101
+ placement:str='add_after', # Can be 'add_after', 'add_before', 'at_start', 'at_end'
102
+ msgid:str=None, # id of message that placement is relative to (if None, uses current message)
118
103
  msg_type: str='note', # Message type, can be 'code', 'note', or 'prompt'
119
104
  output:str='', # For prompts/code, initial output
120
105
  time_run: str | None = '', # When was message executed
@@ -122,35 +107,15 @@ def _msg(
122
107
  skipped: int | None = 0, # Hide message from prompt?
123
108
  i_collapsed: int | None = 0, # Collapse input?
124
109
  o_collapsed: int | None = 0, # Collapse output?
125
- header_collapsed: int | None = 0, # Collapse heading section?
126
- pinned: int | None = 0 # Pin to context?
127
- ): ...
128
-
129
- Placements = str_enum('Placements', 'add_after', 'add_before', 'update', 'at_start', 'at_end')
130
-
131
- # %% ../nbs/00_core.ipynb
132
- @delegates(_msg)
133
- def add_msg(
134
- content:str, # Content of the message (i.e the message prompt, code, or note text)
135
- placement:str='add_after', # Can be 'add_after', 'add_before', 'update', 'at_start', 'at_end'
136
- sid:str=None, # sid (stable id -- pk) of message that placement is relative to (if None, uses current message)
137
- **kwargs
138
- ):
139
- """Add/update a message to the queue to show after code execution completes.
140
- Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change).
141
- Sets msg_type to 'note' by default if not update placement."""
142
- if 'msg_type' not in kwargs and placement!='update': kwargs['msg_type']='note'
143
- mt = kwargs.get('msg_type',None)
144
- ot = kwargs.get('output',None)
145
- if mt and mt not in ('note', 'code', 'prompt'): return "msg_type must be 'code', 'note', or 'prompt'."
146
- if mt=='note' and ot: return "note messages cannot have an output."
147
- if mt=='code':
148
- try: json.loads(ot or '[]')
149
- except: return "Code output must be valid json"
150
- if not sid: sid = find_msg_id()
151
- data = dict(placement=placement, sid=sid, **kwargs)
152
- if content is not None: data['content'] = content
153
- return xpost('http://localhost:5001/add_relative_', data=data).text
110
+ heading_collapsed: int | None = 0, # Collapse heading section?
111
+ pinned: int | None = 0, # Pin to context?
112
+ **kwargs):
113
+ "Add/update a message to the queue to show after code execution completes."
114
+ if placement not in ('at_start','at_end') and not msgid: msgid = find_msg_id()
115
+ return call_endp(
116
+ 'add_relative_', content=content, placement=placement, msgid=msgid, msg_type=msg_type, output=output,
117
+ time_run=time_run, is_exported=is_exported, skipped=skipped, pinned=pinned,
118
+ i_collapsed=i_collapsed, o_collapsed=o_collapsed, heading_collapsed=heading_collapsed, **kwargs)
154
119
 
155
120
  # %% ../nbs/00_core.ipynb
156
121
  @delegates(add_msg)
@@ -172,26 +137,22 @@ def _umsg(
172
137
  skipped: int | None = None, # Hide message from prompt?
173
138
  i_collapsed: int | None = None, # Collapse input?
174
139
  o_collapsed: int | None = None, # Collapse output?
175
- header_collapsed: int | None = None, # Collapse heading section?
140
+ heading_collapsed: int | None = None, # Collapse heading section?
176
141
  pinned: int | None = None # Pin to context?
177
142
  ): ...
178
143
 
179
144
  # %% ../nbs/00_core.ipynb
180
145
  @delegates(_umsg)
181
146
  def update_msg(
182
- sid:str=None, # sid (stable id -- pk) of message to update (if None, uses current message)
147
+ msgid:str=None, # id of message to update (if None, uses current message)
183
148
  content:str|None=None, # Content of the message (i.e the message prompt, code, or note text)
184
149
  msg:Optional[Dict]=None, # Dictionary of field keys/values to update
185
150
  **kwargs):
186
151
  """Update an existing message. Provide either `msg` OR field key/values to update.
187
- Use `content` param to update contents. Be sure to pass a `sid` (stable id -- the pk) not a `mid`
188
- (which is used only for sorting, and can change).
152
+ Use `content` param to update contents.
189
153
  Only include parameters to update--missing ones will be left unchanged."""
190
- kw = (msg or {}) | kwargs
191
- sid = kw.pop('sid', sid)
192
- if not sid: raise TypeError("update_msg needs either a dict message or `sid=...`")
193
- kw.pop('did', None)
194
- return add_msg(content, placement='update', sid=sid, **kw)
154
+ if not msgid and not msg: raise TypeError("update_msg needs either a dict message or `msgid=`")
155
+ return call_endp('add_relative_', content=content, placement='update', msgid=msgid, **kwargs)
195
156
 
196
157
  # %% ../nbs/00_core.ipynb
197
158
  def load_gist(gist_id:str):
@@ -259,32 +220,6 @@ def import_gist(
259
220
  add_msg(f"{pref}\n\n{mk_toollist(syms)}")
260
221
  return module
261
222
 
262
- # %% ../nbs/00_core.ipynb
263
- __EXPORT_FIELDS = set('content output input_tokens output_tokens msg_type is_exported skipped pinned i_collapsed o_collapsed header_collapsed'.split())
264
-
265
- __REQUIRED_FIELDS = set('content output msg_type'.split())
266
-
267
- # %% ../nbs/00_core.ipynb
268
- def export_dialog(filename: str, did:int=None):
269
- "Export dialog messages and optionally attachments to JSON"
270
- if did is None: did = find_dialog_id()
271
- db = get_db()
272
- msgs = db.t.message('did=? and (pinned=0 or pinned is null)', [did], order_by='mid')
273
- msg_data = [{k:getattr(msg,k) for k in __EXPORT_FIELDS if hasattr(msg, k)}
274
- for msg in msgs]
275
- result = {'messages': msg_data, 'dialog_name': db.t.dialog[did].name}
276
- with open(filename, 'w') as f: json.dump(result, f, indent=2)
277
-
278
- # %% ../nbs/00_core.ipynb
279
- def import_dialog(fname, add_header=True):
280
- "Import dialog messages from JSON file using `add_msg`"
281
- data = json.loads(Path(fname).read_text())
282
- for msg in data['messages'][::-1]:
283
- opts = {k:msg[k] for k in __EXPORT_FIELDS - __REQUIRED_FIELDS if k in msg}
284
- add_msg(msg.get('content',''), msg.get('msg_type','note'), msg.get('output',''), 'at_end', **opts)
285
- if add_header: add_msg(f"# Imported Dialog `{fname}`", 'note', placement='at_end')
286
- return f"Imported {len(data['messages'])} messages"
287
-
288
223
  # %% ../nbs/00_core.ipynb
289
224
  def tool_info():
290
225
  cts='''Tools available from `dialoghelper`:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dialoghelper
3
- Version: 0.0.17
3
+ Version: 0.0.18
4
4
  Summary: Helper functions for solveit dialogs
5
5
  Home-page: https://github.com/AnswerDotAI/dialoghelper
6
6
  Author: Jeremy Howard
@@ -0,0 +1,10 @@
1
+ dialoghelper/__init__.py,sha256=V4rULLJEoYYsILwNA9eg9WxKpado6anMzsERu_XUspI,43
2
+ dialoghelper/_modidx.py,sha256=72qT7cHwYbkCIIGIxbikkMQjS0W0zuFeFvkh-Qp4iFE,2535
3
+ dialoghelper/core.py,sha256=4TsEKRQlSfG5JZqnbMKVnpJbsS1JaNz5qL0Ch0Pinxc,10291
4
+ dialoghelper/db_dc.py,sha256=mi2Q2am_SoAPGpNQg7KPFS5pR9WEapRXT8ypkNmMfw0,2330
5
+ dialoghelper-0.0.18.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
6
+ dialoghelper-0.0.18.dist-info/METADATA,sha256=0a4NdXRHAMrz6C9fhO0zU6qL3d3R6ieqibdSpDygvW0,2565
7
+ dialoghelper-0.0.18.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ dialoghelper-0.0.18.dist-info/entry_points.txt,sha256=wvDeX-XTS_XVjWiiPQe6yVfmyNwy9eCr36ewp9baFIg,46
9
+ dialoghelper-0.0.18.dist-info/top_level.txt,sha256=VXLlkgltFs_q-XB9imt2G64I_-MPm1RnxlpvUWPuLKM,13
10
+ dialoghelper-0.0.18.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- dialoghelper/__init__.py,sha256=iwcIRnY-hzTvlvcCnwyLfa-0GCtmC0fPSKlBQgXUyBo,43
2
- dialoghelper/_modidx.py,sha256=il4I8H0rrleInprpaXaQ3Lyy4_C1ipPVQvaZ8WopN7I,3126
3
- dialoghelper/core.py,sha256=2GdSvvEFWMAEJ_FNRUj9LoiB1x8zQZcYbMOJfFyvClM,12999
4
- dialoghelper/db_dc.py,sha256=mi2Q2am_SoAPGpNQg7KPFS5pR9WEapRXT8ypkNmMfw0,2330
5
- dialoghelper-0.0.17.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
6
- dialoghelper-0.0.17.dist-info/METADATA,sha256=XET3SvzannEw7Hq_CP8Qs1rEzGwT2l16Xvwo2YGHFZo,2565
7
- dialoghelper-0.0.17.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
- dialoghelper-0.0.17.dist-info/entry_points.txt,sha256=wvDeX-XTS_XVjWiiPQe6yVfmyNwy9eCr36ewp9baFIg,46
9
- dialoghelper-0.0.17.dist-info/top_level.txt,sha256=VXLlkgltFs_q-XB9imt2G64I_-MPm1RnxlpvUWPuLKM,13
10
- dialoghelper-0.0.17.dist-info/RECORD,,