dialoghelper 0.0.16__tar.gz → 0.0.18__tar.gz
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-0.0.16/dialoghelper.egg-info → dialoghelper-0.0.18}/PKG-INFO +1 -1
- dialoghelper-0.0.18/dialoghelper/__init__.py +2 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/dialoghelper/_modidx.py +1 -6
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/dialoghelper/core.py +47 -112
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/dialoghelper/db_dc.py +1 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18/dialoghelper.egg-info}/PKG-INFO +1 -1
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/settings.ini +1 -1
- dialoghelper-0.0.16/dialoghelper/__init__.py +0 -2
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/LICENSE +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/MANIFEST.in +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/README.md +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/dialoghelper.egg-info/SOURCES.txt +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/dialoghelper.egg-info/dependency_links.txt +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/dialoghelper.egg-info/entry_points.txt +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/dialoghelper.egg-info/not-zip-safe +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/dialoghelper.egg-info/requires.txt +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/dialoghelper.egg-info/top_level.txt +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/pyproject.toml +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/setup.cfg +0 -0
- {dialoghelper-0.0.16 → dialoghelper-0.0.18}/setup.py +0 -0
|
@@ -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': {}}}
|
|
@@ -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', '
|
|
5
|
-
'
|
|
6
|
-
'
|
|
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
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
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 `
|
|
63
|
-
|
|
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,48 +61,45 @@ def find_msg_id():
|
|
|
74
61
|
return find_var('__msg_id')
|
|
75
62
|
|
|
76
63
|
# %% ../nbs/00_core.ipynb
|
|
77
|
-
def
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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(
|
|
91
|
-
|
|
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
|
-
|
|
95
|
-
|
|
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
|
-
|
|
108
|
-
xpost('http://localhost:5001/add_html_', data=dict(content=res))
|
|
86
|
+
call_endp('add_html_', content=to_xml(content))
|
|
109
87
|
|
|
110
88
|
# %% ../nbs/00_core.ipynb
|
|
111
89
|
def del_msg(
|
|
112
|
-
|
|
90
|
+
msgid:str=None, # id of message that placement is relative to (if None, uses current message)
|
|
113
91
|
):
|
|
114
92
|
"Delete a message from the dialog. Be sure to pass a `sid`, not a `mid`."
|
|
115
|
-
|
|
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')
|
|
116
97
|
|
|
117
98
|
# %% ../nbs/00_core.ipynb
|
|
118
|
-
def
|
|
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)
|
|
119
103
|
msg_type: str='note', # Message type, can be 'code', 'note', or 'prompt'
|
|
120
104
|
output:str='', # For prompts/code, initial output
|
|
121
105
|
time_run: str | None = '', # When was message executed
|
|
@@ -123,34 +107,15 @@ def _msg(
|
|
|
123
107
|
skipped: int | None = 0, # Hide message from prompt?
|
|
124
108
|
i_collapsed: int | None = 0, # Collapse input?
|
|
125
109
|
o_collapsed: int | None = 0, # Collapse output?
|
|
126
|
-
|
|
127
|
-
pinned: int | None = 0 # Pin to context?
|
|
128
|
-
):
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
content:str, # Content of the message (i.e the message prompt, code, or note text)
|
|
136
|
-
placement:str='add_after', # Can be 'add_after', 'add_before', 'update', 'at_start', 'at_end'
|
|
137
|
-
sid:str=None, # sid (stable id -- pk) of message that placement is relative to (if None, uses current message)
|
|
138
|
-
**kwargs
|
|
139
|
-
):
|
|
140
|
-
"""Add/update a message to the queue to show after code execution completes.
|
|
141
|
-
Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change).
|
|
142
|
-
Sets msg_type to 'note' by default if not update placement."""
|
|
143
|
-
if 'msg_type' not in kwargs and placement!='update': kwargs['msg_type']='note'
|
|
144
|
-
mt = kwargs.get('msg_type',None)
|
|
145
|
-
ot = kwargs.get('output',None)
|
|
146
|
-
if mt and mt not in ('note', 'code', 'prompt'): return "msg_type must be 'code', 'note', or 'prompt'."
|
|
147
|
-
if mt=='note' and ot: return "note messages cannot have an output."
|
|
148
|
-
if mt=='code':
|
|
149
|
-
try: json.loads(ot or '[]')
|
|
150
|
-
except: return "Code output must be valid json"
|
|
151
|
-
if not sid: sid = find_msg_id()
|
|
152
|
-
data = dict(content=content, placement=placement, sid=sid, **kwargs)
|
|
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
|
-
|
|
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
|
-
|
|
183
|
-
content:str|None
|
|
147
|
+
msgid:str=None, # id of message to update (if None, uses current message)
|
|
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.
|
|
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
|
-
|
|
191
|
-
|
|
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`:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|