dialoghelper 0.0.17__tar.gz → 0.0.19__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.
- {dialoghelper-0.0.17/dialoghelper.egg-info → dialoghelper-0.0.19}/PKG-INFO +1 -1
- dialoghelper-0.0.19/dialoghelper/__init__.py +2 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/dialoghelper/_modidx.py +1 -6
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/dialoghelper/core.py +50 -112
- {dialoghelper-0.0.17 → dialoghelper-0.0.19/dialoghelper.egg-info}/PKG-INFO +1 -1
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/settings.ini +1 -1
- dialoghelper-0.0.17/dialoghelper/__init__.py +0 -2
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/LICENSE +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/MANIFEST.in +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/README.md +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/dialoghelper/db_dc.py +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/dialoghelper.egg-info/SOURCES.txt +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/dialoghelper.egg-info/dependency_links.txt +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/dialoghelper.egg-info/entry_points.txt +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/dialoghelper.egg-info/not-zip-safe +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/dialoghelper.egg-info/requires.txt +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/dialoghelper.egg-info/top_level.txt +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/pyproject.toml +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/setup.cfg +0 -0
- {dialoghelper-0.0.17 → dialoghelper-0.0.19}/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,47 +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
|
-
|
|
86
|
+
call_endp('add_html_', content=to_xml(content))
|
|
108
87
|
|
|
109
88
|
# %% ../nbs/00_core.ipynb
|
|
110
89
|
def del_msg(
|
|
111
|
-
|
|
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
|
-
|
|
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
|
|
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,46 +107,30 @@ 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
|
-
|
|
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
|
|
110
|
+
heading_collapsed: int | None = 0, # Collapse heading section?
|
|
111
|
+
pinned: int | None = 0, # Pin to context?
|
|
138
112
|
):
|
|
139
|
-
"
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
|
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)
|
|
154
119
|
|
|
155
120
|
# %% ../nbs/00_core.ipynb
|
|
156
121
|
@delegates(add_msg)
|
|
157
122
|
def _add_msg_unsafe(
|
|
158
123
|
content:str, # Content of the message (i.e the message prompt, code, or note text)
|
|
124
|
+
placement:str='add_after', # Can be 'add_after', 'add_before', 'at_start', 'at_end'
|
|
125
|
+
msgid:str=None, # id of message that placement is relative to (if None, uses current message)
|
|
159
126
|
run:bool=False, # For prompts, send it to the AI; for code, execute it (*DANGEROUS -- be careful of what you run!)
|
|
160
127
|
**kwargs
|
|
161
128
|
):
|
|
162
129
|
"""Add/update a message to the queue to show after code execution completes, and optionally run it. Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change).
|
|
163
130
|
*WARNING*--This can execute arbitrary code, so check carefully what you run!--*WARNING"""
|
|
164
|
-
|
|
131
|
+
if placement not in ('at_start','at_end') and not msgid: msgid = find_msg_id()
|
|
132
|
+
return call_endp(
|
|
133
|
+
'add_relative_', content=content, placement=placement, msgid=msgid, run=run, **kwargs)
|
|
165
134
|
|
|
166
135
|
# %% ../nbs/00_core.ipynb
|
|
167
136
|
def _umsg(
|
|
@@ -172,26 +141,22 @@ def _umsg(
|
|
|
172
141
|
skipped: int | None = None, # Hide message from prompt?
|
|
173
142
|
i_collapsed: int | None = None, # Collapse input?
|
|
174
143
|
o_collapsed: int | None = None, # Collapse output?
|
|
175
|
-
|
|
144
|
+
heading_collapsed: int | None = None, # Collapse heading section?
|
|
176
145
|
pinned: int | None = None # Pin to context?
|
|
177
146
|
): ...
|
|
178
147
|
|
|
179
148
|
# %% ../nbs/00_core.ipynb
|
|
180
149
|
@delegates(_umsg)
|
|
181
150
|
def update_msg(
|
|
182
|
-
|
|
151
|
+
msgid:str=None, # id of message to update (if None, uses current message)
|
|
183
152
|
content:str|None=None, # Content of the message (i.e the message prompt, code, or note text)
|
|
184
153
|
msg:Optional[Dict]=None, # Dictionary of field keys/values to update
|
|
185
154
|
**kwargs):
|
|
186
155
|
"""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).
|
|
156
|
+
Use `content` param to update contents.
|
|
189
157
|
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)
|
|
158
|
+
if not msgid and not msg: raise TypeError("update_msg needs either a dict message or `msgid=`")
|
|
159
|
+
return call_endp('add_relative_', content=content, placement='update', msgid=msgid, **kwargs)
|
|
195
160
|
|
|
196
161
|
# %% ../nbs/00_core.ipynb
|
|
197
162
|
def load_gist(gist_id:str):
|
|
@@ -259,38 +224,11 @@ def import_gist(
|
|
|
259
224
|
add_msg(f"{pref}\n\n{mk_toollist(syms)}")
|
|
260
225
|
return module
|
|
261
226
|
|
|
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
227
|
# %% ../nbs/00_core.ipynb
|
|
289
228
|
def tool_info():
|
|
290
229
|
cts='''Tools available from `dialoghelper`:
|
|
291
230
|
|
|
292
231
|
- &`add_html`: Send HTML to the browser to be swapped into the DOM using hx-swap-oob.
|
|
293
|
-
- &`find_dialog_id`: Get the current dialog id.
|
|
294
232
|
- &`find_msg_id`: Get the current message id.
|
|
295
233
|
- &`find_msgs`: Find messages in current specific dialog that contain the given information.
|
|
296
234
|
- &`read_msg`: Get the message indexed in the current dialog.
|
|
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
|
|
File without changes
|