Skip to content

Commit 2c45c90

Browse files
committed
fixes #218
1 parent 972039b commit 2c45c90

10 files changed

Lines changed: 15 additions & 266 deletions

File tree

dialoghelper/_modidx.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@
127127
'dialoghelper.utils': { 'dialoghelper.utils.InputBtn': ('utils.html#inputbtn', 'dialoghelper/utils.py'),
128128
'dialoghelper.utils.InputForm': ('utils.html#inputform', 'dialoghelper/utils.py'),
129129
'dialoghelper.utils._ast_replace': ('utils.html#_ast_replace', 'dialoghelper/utils.py'),
130-
'dialoghelper.utils._filter_diff': ('utils.html#_filter_diff', 'dialoghelper/utils.py'),
131-
'dialoghelper.utils._fmt_replies': ('utils.html#_fmt_replies', 'dialoghelper/utils.py'),
132-
'dialoghelper.utils._reduce_ctx': ('utils.html#_reduce_ctx', 'dialoghelper/utils.py'),
133130
'dialoghelper.utils.add_styles': ('utils.html#add_styles', 'dialoghelper/utils.py'),
134131
'dialoghelper.utils.ast_grep': ('utils.html#ast_grep', 'dialoghelper/utils.py'),
135132
'dialoghelper.utils.ast_py': ('utils.html#ast_py', 'dialoghelper/utils.py'),
@@ -138,11 +135,7 @@
138135
'dialoghelper.utils.ctx_symfile': ('utils.html#ctx_symfile', 'dialoghelper/utils.py'),
139136
'dialoghelper.utils.ctx_symfolder': ('utils.html#ctx_symfolder', 'dialoghelper/utils.py'),
140137
'dialoghelper.utils.ctx_sympkg': ('utils.html#ctx_sympkg', 'dialoghelper/utils.py'),
141-
'dialoghelper.utils.gist_file': ('utils.html#gist_file', 'dialoghelper/utils.py'),
142138
'dialoghelper.utils.import_gist': ('utils.html#import_gist', 'dialoghelper/utils.py'),
143139
'dialoghelper.utils.import_string': ('utils.html#import_string', 'dialoghelper/utils.py'),
144140
'dialoghelper.utils.input': ('utils.html#input', 'dialoghelper/utils.py'),
145-
'dialoghelper.utils.load_gist': ('utils.html#load_gist', 'dialoghelper/utils.py'),
146-
'dialoghelper.utils.mk_toollist': ('utils.html#mk_toollist', 'dialoghelper/utils.py'),
147-
'dialoghelper.utils.read_pr': ('utils.html#read_pr', 'dialoghelper/utils.py'),
148-
'dialoghelper.utils.update_gist': ('utils.html#update_gist', 'dialoghelper/utils.py')}}}
141+
'dialoghelper.utils.mk_toollist': ('utils.html#mk_toollist', 'dialoghelper/utils.py')}}}

dialoghelper/capture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Screen Capture.
22
3-
Docs: https://AnswerDotAI.github.io/dialoghelpercapture.html.md"""
3+
Docs: https://AnswerDotAI.github.io/dialoghelper/capture.html.md"""
44

55
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_capture.ipynb.
66

dialoghelper/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Docs: https://AnswerDotAI.github.io/dialoghelpercore.html.md"""
1+
"""Docs: https://AnswerDotAI.github.io/dialoghelper/core.html.md"""
22

33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.
44

@@ -859,7 +859,7 @@ def solveit_docs():
859859
_ref_gist_id = '9e7b444aba5ecf6d14295ba2cee890c3'
860860
pre = f"""⚠️ This content will be truncated in your next turn. Re-call this function if you need it again.
861861
If the user wants more info, give them a link to https://gist.github.com/jph00/{_ref_gist_id}."""
862-
return pre + gist_file(_ref_gist_id)['content']
862+
return pre + GhApi().gist_file(_ref_gist_id)['content']
863863

864864
# %% ../nbs/00_core.ipynb #70ec67db
865865
def dialog_link(

dialoghelper/solve_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Google Sign-In for apps hosted on SolveIt
22
3-
Docs: https://AnswerDotAI.github.io/dialoghelpersolve_auth.html.md"""
3+
Docs: https://AnswerDotAI.github.io/dialoghelper/solve_auth.html.md"""
44

55
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/05_solve_auth.ipynb.
66

dialoghelper/tmux.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
found = {path: content for path, content in flatten_dict(windows(n=2000)) if 'curl' in content}
2626
```
2727
28-
Docs: https://AnswerDotAI.github.io/dialoghelpertmux.html.md"""
28+
Docs: https://AnswerDotAI.github.io/dialoghelper/tmux.html.md"""
2929

3030
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_tmux.ipynb.
3131

dialoghelper/tracetools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Solveit tools for tracing function execution
22
3-
Docs: https://AnswerDotAI.github.io/dialoghelpertracetools.html.md"""
3+
Docs: https://AnswerDotAI.github.io/dialoghelper/tracetools.html.md"""
44

55
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_tracetools.ipynb.
66

dialoghelper/utils.py

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
"""Docs: https://AnswerDotAI.github.io/dialoghelperutils.html.md"""
1+
"""Docs: https://AnswerDotAI.github.io/dialoghelper/utils.html.md"""
22

33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/06_utils.ipynb.
44

55
# %% auto #0
66
__all__ = ['md_cls_d', 'msg_ast_replace', 'add_styles', 'ast_py', 'ast_grep', 'ctx_folder', 'ctx_repo', 'ctx_symfile',
7-
'ctx_symfolder', 'ctx_sympkg', 'load_gist', 'gist_file', 'import_string', 'mk_toollist', 'import_gist',
8-
'update_gist', 'read_pr', 'InputBtn', 'input', 'InputForm']
7+
'ctx_symfolder', 'ctx_sympkg', 'import_string', 'mk_toollist', 'import_gist', 'InputBtn', 'input',
8+
'InputForm']
99

1010
# %% ../nbs/06_utils.ipynb #4dd4b925
1111
import os,re,inspect,ast,collections,time,asyncio,json,linecache,importlib,difflib,uuid,builtins,subprocess
@@ -173,21 +173,6 @@ async def ctx_sympkg(
173173
return await add_msg(sym2pkgctx(sym, **kwargs), msg_type='raw');
174174

175175

176-
# %% ../nbs/06_utils.ipynb #eb4c6bf4
177-
@allow
178-
def load_gist(gist_id:str):
179-
"Retrieve a gist"
180-
api = GhApi()
181-
if '/' in gist_id: *_,user,gist_id = gist_id.split('/')
182-
else: user = None
183-
return api.gists.get(gist_id, user=user)
184-
185-
# %% ../nbs/06_utils.ipynb #3b151f8f
186-
def gist_file(gist_id:str):
187-
"Get the first file from a gist"
188-
gist = load_gist(gist_id)
189-
return first(gist.files.values())
190-
191176
# %% ../nbs/06_utils.ipynb #1ccfc95a
192177
def import_string(
193178
code:str, # Code to import as a module
@@ -217,7 +202,7 @@ def import_gist(
217202
create_msg:bool=False # Add a message that lists usable tools
218203
):
219204
"Import gist directly from string without saving to disk"
220-
fil = gist_file(gist_id)
205+
fil = GhApi().gist_file(gist_id)
221206
mod_name = mod_name or Path(fil['filename']).stem
222207
module = import_string(fil['content'], mod_name)
223208
glbs = currentframe().f_back.f_globals
@@ -232,71 +217,6 @@ def import_gist(
232217
asyncio.ensure_future(add_msg(f"{pref}\n\n{mk_toollist(syms)}"))
233218
return module
234219

235-
# %% ../nbs/06_utils.ipynb #f790682a
236-
def update_gist(gist_id:str, content:str):
237-
"Update the first file in a gist with new content"
238-
api = GhApi()
239-
if '/' in gist_id: *_,user,gist_id = gist_id.split('/')
240-
gist = api.gists.get(gist_id)
241-
fname = first(gist.files.keys())
242-
res = api.gists.update(gist_id, files={fname: {'content': content}})
243-
return res['html_url']
244-
245-
# %% ../nbs/06_utils.ipynb #3afa7bdb
246-
def _filter_diff(diff, folder='solveit/', skip_files=('_modidx.py',)):
247-
"Filter unified diff to only include files under `folder`, skipping `skip_files`"
248-
sections = re.split(r'(?=^diff --git )', diff, flags=re.MULTILINE)
249-
return ''.join(s for s in sections
250-
if s.startswith(f'diff --git a/{folder}')
251-
and not any(f'/{f} ' in s.split('\n')[0] for f in skip_files))
252-
253-
def _reduce_ctx(diff):
254-
"Keep only diff headers and changed lines (optionally with `n` context lines)"
255-
lines = diff.splitlines(True)
256-
return ''.join(l for l in lines if l[0:1] in ('+','-','d','i','@') or not l.strip())
257-
258-
def _fmt_replies(api, owner, repo, num):
259-
"Format issue/PR comment replies"
260-
comments = api.issues.list_comments(owner, repo, num)
261-
if not comments: return ''
262-
return '\n\n## Replies\n' + '\n\n---\n'.join(f"**@{c.user.login}** ({c.created_at[:10]}):\n{c.body}" for c in comments)
263-
264-
# %% ../nbs/06_utils.ipynb #6d26d0ed
265-
@allow
266-
def read_pr(
267-
pr_number:int|str, # Issue/PR number, or GitHub issue/PR URL
268-
owner:str='answerdotai', # Owner
269-
repo:str=None, # Repo
270-
folder:str='', # For diffs, limit to only to files in `folder`
271-
replies:bool=False # Include replies
272-
):
273-
"Fetch a GitHub PR or issue's title, body, optionally replies, and diff (if PR)"
274-
if '/' in str(pr_number): *_,owner,repo,typ,pr_number = str(pr_number).rstrip('/').split('/')
275-
pr_number = int(pr_number)
276-
if folder: folder = f"{folder}/"
277-
api = GhApi()
278-
res = None
279-
try: pr = api.pulls.get(owner, repo, pr_number)
280-
except:
281-
try: iss = api.issues.get(owner, repo, pr_number)
282-
except Exception as e: return f"Err: {e}"
283-
title,body = iss.title, iss.body or ''
284-
evts = api.issues.list_events(owner, repo, pr_number)
285-
sha = first(e.commit_id for e in evts if e.commit_id)
286-
if not sha: res = f"# {title}\n\n{body}"
287-
else:
288-
try: diff = api.repos.get_commit(owner, repo, sha, headers={'Accept': 'application/vnd.github.v3.diff'})
289-
except Exception as e: res = f"# {title}\n\n{body}\n\n(No diff: {e})"
290-
else:
291-
title,body = pr.title, pr.body or ''
292-
try: diff = api.pulls.get(owner, repo, pr_number, headers={'Accept': 'application/vnd.github.v3.diff'})
293-
except Exception as e: res = f"# {title}\n\n{body}\n\n(No diff: {e})"
294-
if res is None:
295-
diff = _reduce_ctx(_filter_diff(diff, folder=folder))
296-
res = f"# {title}\n\n{body}\n\n## Diff\n```diff\n{diff}\n```"
297-
if replies: res += _fmt_replies(api, owner, repo, pr_number)
298-
return res
299-
300220
# %% ../nbs/06_utils.ipynb #aab4330e
301221
def InputBtn(txt, value=None, btncls=(), **kw):
302222
btncls = ' '.join(f'uk-btn-{o}' for o in listify(btncls))

nbs/00_core.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3225,7 +3225,7 @@
32253225
" _ref_gist_id = '9e7b444aba5ecf6d14295ba2cee890c3'\n",
32263226
" pre = f\"\"\"⚠️ This content will be truncated in your next turn. Re-call this function if you need it again.\n",
32273227
"If the user wants more info, give them a link to https://gist.github.com/jph00/{_ref_gist_id}.\"\"\"\n",
3228-
" return pre + gist_file(_ref_gist_id)['content']"
3228+
" return pre + GhApi().gist_file(_ref_gist_id)['content']"
32293229
]
32303230
},
32313231
{

nbs/06_utils.ipynb

Lines changed: 2 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -658,82 +658,6 @@
658658
"## Gists and github"
659659
]
660660
},
661-
{
662-
"cell_type": "code",
663-
"execution_count": null,
664-
"id": "eb4c6bf4",
665-
"metadata": {},
666-
"outputs": [],
667-
"source": [
668-
"#| export\n",
669-
"@allow\n",
670-
"def load_gist(gist_id:str):\n",
671-
" \"Retrieve a gist\"\n",
672-
" api = GhApi()\n",
673-
" if '/' in gist_id: *_,user,gist_id = gist_id.split('/')\n",
674-
" else: user = None\n",
675-
" return api.gists.get(gist_id, user=user)"
676-
]
677-
},
678-
{
679-
"cell_type": "code",
680-
"execution_count": null,
681-
"id": "3ab7586f",
682-
"metadata": {},
683-
"outputs": [
684-
{
685-
"data": {
686-
"text/plain": [
687-
"'https://gist.github.com/jph00/e7cfd4ded593e8ef6217e78a0131960c'"
688-
]
689-
},
690-
"execution_count": null,
691-
"metadata": {},
692-
"output_type": "execute_result"
693-
}
694-
],
695-
"source": [
696-
"gistid = 'jph00/e7cfd4ded593e8ef6217e78a0131960c'\n",
697-
"gist = load_gist(gistid)\n",
698-
"gist.html_url"
699-
]
700-
},
701-
{
702-
"cell_type": "code",
703-
"execution_count": null,
704-
"id": "3b151f8f",
705-
"metadata": {},
706-
"outputs": [],
707-
"source": [
708-
"#| export\n",
709-
"def gist_file(gist_id:str):\n",
710-
" \"Get the first file from a gist\"\n",
711-
" gist = load_gist(gist_id)\n",
712-
" return first(gist.files.values())"
713-
]
714-
},
715-
{
716-
"cell_type": "code",
717-
"execution_count": null,
718-
"id": "ea33969d",
719-
"metadata": {},
720-
"outputs": [
721-
{
722-
"name": "stdout",
723-
"output_type": "stream",
724-
"text": [
725-
"\"This is a test module which makes some simple tools available.\"\n",
726-
"__all__ = [\"hi\",\"whoami\"]\n",
727-
"\n",
728-
"testfoo=…\n"
729-
]
730-
}
731-
],
732-
"source": [
733-
"gfile = gist_file(gistid)\n",
734-
"print(gfile.content[:100]+\"\")"
735-
]
736-
},
737661
{
738662
"cell_type": "code",
739663
"execution_count": null,
@@ -838,7 +762,7 @@
838762
" create_msg:bool=False # Add a message that lists usable tools\n",
839763
"):\n",
840764
" \"Import gist directly from string without saving to disk\"\n",
841-
" fil = gist_file(gist_id)\n",
765+
" fil = GhApi().gist_file(gist_id)\n",
842766
" mod_name = mod_name or Path(fil['filename']).stem\n",
843767
" module = import_string(fil['content'], mod_name)\n",
844768
" glbs = currentframe().f_back.f_globals\n",
@@ -872,6 +796,7 @@
872796
}
873797
],
874798
"source": [
799+
"gistid = 'jph00/e7cfd4ded593e8ef6217e78a0131960c'\n",
875800
"import_gist(gistid)\n",
876801
"importtest.testfoo"
877802
]
@@ -961,95 +886,6 @@
961886
"importtest.__all__"
962887
]
963888
},
964-
{
965-
"cell_type": "code",
966-
"execution_count": null,
967-
"id": "f790682a",
968-
"metadata": {},
969-
"outputs": [],
970-
"source": [
971-
"#| export\n",
972-
"def update_gist(gist_id:str, content:str):\n",
973-
" \"Update the first file in a gist with new content\"\n",
974-
" api = GhApi()\n",
975-
" if '/' in gist_id: *_,user,gist_id = gist_id.split('/')\n",
976-
" gist = api.gists.get(gist_id)\n",
977-
" fname = first(gist.files.keys())\n",
978-
" res = api.gists.update(gist_id, files={fname: {'content': content}})\n",
979-
" return res['html_url']"
980-
]
981-
},
982-
{
983-
"cell_type": "code",
984-
"execution_count": null,
985-
"id": "3afa7bdb",
986-
"metadata": {},
987-
"outputs": [],
988-
"source": [
989-
"#| export\n",
990-
"def _filter_diff(diff, folder='solveit/', skip_files=('_modidx.py',)):\n",
991-
" \"Filter unified diff to only include files under `folder`, skipping `skip_files`\"\n",
992-
" sections = re.split(r'(?=^diff --git )', diff, flags=re.MULTILINE)\n",
993-
" return ''.join(s for s in sections\n",
994-
" if s.startswith(f'diff --git a/{folder}')\n",
995-
" and not any(f'/{f} ' in s.split('\\n')[0] for f in skip_files))\n",
996-
"\n",
997-
"def _reduce_ctx(diff):\n",
998-
" \"Keep only diff headers and changed lines (optionally with `n` context lines)\"\n",
999-
" lines = diff.splitlines(True)\n",
1000-
" return ''.join(l for l in lines if l[0:1] in ('+','-','d','i','@') or not l.strip())\n",
1001-
"\n",
1002-
"def _fmt_replies(api, owner, repo, num):\n",
1003-
" \"Format issue/PR comment replies\"\n",
1004-
" comments = api.issues.list_comments(owner, repo, num)\n",
1005-
" if not comments: return ''\n",
1006-
" return '\\n\\n## Replies\\n' + '\\n\\n---\\n'.join(f\"**@{c.user.login}** ({c.created_at[:10]}):\\n{c.body}\" for c in comments)"
1007-
]
1008-
},
1009-
{
1010-
"cell_type": "code",
1011-
"execution_count": null,
1012-
"id": "6d26d0ed",
1013-
"metadata": {},
1014-
"outputs": [],
1015-
"source": [
1016-
"#| export\n",
1017-
"@allow\n",
1018-
"def read_pr(\n",
1019-
" pr_number:int|str, # Issue/PR number, or GitHub issue/PR URL\n",
1020-
" owner:str='answerdotai', # Owner\n",
1021-
" repo:str=None, # Repo\n",
1022-
" folder:str='', # For diffs, limit to only to files in `folder`\n",
1023-
" replies:bool=False # Include replies\n",
1024-
"):\n",
1025-
" \"Fetch a GitHub PR or issue's title, body, optionally replies, and diff (if PR)\"\n",
1026-
" if '/' in str(pr_number): *_,owner,repo,typ,pr_number = str(pr_number).rstrip('/').split('/')\n",
1027-
" pr_number = int(pr_number)\n",
1028-
" if folder: folder = f\"{folder}/\"\n",
1029-
" api = GhApi()\n",
1030-
" res = None\n",
1031-
" try: pr = api.pulls.get(owner, repo, pr_number)\n",
1032-
" except:\n",
1033-
" try: iss = api.issues.get(owner, repo, pr_number)\n",
1034-
" except Exception as e: return f\"Err: {e}\"\n",
1035-
" title,body = iss.title, iss.body or ''\n",
1036-
" evts = api.issues.list_events(owner, repo, pr_number)\n",
1037-
" sha = first(e.commit_id for e in evts if e.commit_id)\n",
1038-
" if not sha: res = f\"# {title}\\n\\n{body}\"\n",
1039-
" else:\n",
1040-
" try: diff = api.repos.get_commit(owner, repo, sha, headers={'Accept': 'application/vnd.github.v3.diff'})\n",
1041-
" except Exception as e: res = f\"# {title}\\n\\n{body}\\n\\n(No diff: {e})\"\n",
1042-
" else:\n",
1043-
" title,body = pr.title, pr.body or ''\n",
1044-
" try: diff = api.pulls.get(owner, repo, pr_number, headers={'Accept': 'application/vnd.github.v3.diff'})\n",
1045-
" except Exception as e: res = f\"# {title}\\n\\n{body}\\n\\n(No diff: {e})\"\n",
1046-
" if res is None:\n",
1047-
" diff = _reduce_ctx(_filter_diff(diff, folder=folder))\n",
1048-
" res = f\"# {title}\\n\\n{body}\\n\\n## Diff\\n```diff\\n{diff}\\n```\"\n",
1049-
" if replies: res += _fmt_replies(api, owner, repo, pr_number)\n",
1050-
" return res"
1051-
]
1052-
},
1053889
{
1054890
"cell_type": "markdown",
1055891
"id": "3d8cb87c",

0 commit comments

Comments
 (0)