Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dialoghelper/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
'dialoghelper.core.load_gist': ('core.html#load_gist', 'dialoghelper/core.py'),
'dialoghelper.core.mk_toollist': ('core.html#mk_toollist', 'dialoghelper/core.py'),
'dialoghelper.core.msg_idx': ('core.html#msg_idx', 'dialoghelper/core.py'),
'dialoghelper.core.msg_insert_line': ('core.html#msg_insert_line', 'dialoghelper/core.py'),
'dialoghelper.core.msg_replace_lines': ('core.html#msg_replace_lines', 'dialoghelper/core.py'),
'dialoghelper.core.msg_str_replace': ('core.html#msg_str_replace', 'dialoghelper/core.py'),
'dialoghelper.core.msg_strs_replace': ('core.html#msg_strs_replace', 'dialoghelper/core.py'),
'dialoghelper.core.read_msg': ('core.html#read_msg', 'dialoghelper/core.py'),
'dialoghelper.core.run_msg': ('core.html#run_msg', 'dialoghelper/core.py'),
'dialoghelper.core.tool_info': ('core.html#tool_info', 'dialoghelper/core.py'),
Expand Down
100 changes: 77 additions & 23 deletions dialoghelper/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

# %% auto 0
__all__ = ['Placements', 'empty', 'find_var', 'call_endp', 'find_dname', 'find_msg_id', 'curr_dialog', 'find_msgs', 'msg_idx',
'read_msg', 'add_html', 'run_msg', 'add_msg', 'del_msg', 'update_msg', 'url2note', 'ast_py', 'ast_grep',
'load_gist', 'gist_file', 'import_string', 'is_usable_tool', 'mk_toollist', 'import_gist', 'tool_info',
'fc_tool_info', 'asdict']
'add_html', 'add_msg', 'del_msg', 'update_msg', 'url2note', 'ast_py', 'ast_grep', 'read_msg', 'run_msg',
'msg_insert_line', 'msg_str_replace', 'msg_strs_replace', 'msg_replace_lines', 'load_gist', 'gist_file',
'import_string', 'is_usable_tool', 'mk_toollist', 'import_gist', 'tool_info', 'fc_tool_info', 'asdict']

# %% ../nbs/00_core.ipynb
import json, importlib, linecache
Expand Down Expand Up @@ -88,17 +88,6 @@ def msg_idx(
if not msgid: msgid = find_msg_id()
return call_endp('msg_idx_', dname, json=True, msgid=msgid)['msgid']

# %% ../nbs/00_core.ipynb
def read_msg(
n:int=-1, # Message index (if relative, +ve is downwards)
msgid=None, # Message id to find (defaults to current message)
relative:bool=True, # Is `n` relative to current message (True) or absolute (False)?
dname:str='' # Running dialog to get info for; defaults to current dialog
):
"Get the `Message` object indexed in the current dialog."
if not msgid: msgid = find_msg_id()
return call_endp('read_msg_', dname, json=True, msgid=msgid, n=n, relative=relative)['msg']

# %% ../nbs/00_core.ipynb
def add_html(
content:str, # The HTML to send to the client (generally should include hx-swap-oob)
Expand All @@ -107,14 +96,6 @@ def add_html(
"Send HTML to the browser to be swapped into the DOM"
call_endp('add_html_', dname, content=to_xml(content))

# %% ../nbs/00_core.ipynb
def run_msg(
msgid:str=None, # id of message to execute
dname:str='' # Running dialog to get info for; defaults to current dialog
):
"Adds a message to the run queue. Use read_msg to see the output once it runs."
return call_endp('add_runq_', dname, msgid=msgid, api=True)

# %% ../nbs/00_core.ipynb
Placements = str_enum('Placements', 'add_after', 'add_before', 'at_start', 'at_end')

Expand Down Expand Up @@ -221,6 +202,71 @@ def ast_grep(
res = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return json.loads(res.stdout) if res.stdout else res.stderr

# %% ../nbs/00_core.ipynb
def read_msg(
n:int=-1, # Message index (if relative, +ve is downwards)
relative:bool=True, # Is `n` relative to current message (True) or absolute (False)?
msgid:str=None, # Message id to find (defaults to current message)
view_range:list[int,int]=None, # Optional 1-indexed (start, end) line range for files, end=-1 for EOF
nums:bool=False, # Whether to show line numbers
dname:str='' # Running dialog to get info for; defaults to current dialog
):
"Get the `Message` object indexed in the current dialog."
if not msgid: msgid = find_msg_id()
data = dict(n=n, relative=relative, msgid=msgid)
if view_range: data['view_range'] = view_range # None gets converted to '' so we avoid passing it to use the p.default
if nums: data['nums'] = nums
return call_endp('read_msg_', dname, json=True, **data)

# %% ../nbs/00_core.ipynb
def run_msg(
msgid:str=None, # id of message to execute
dname:str='' # Running dialog to get info for; defaults to current dialog
):
"Adds a message to the run queue. Use read_msg to see the output once it runs."
return call_endp('add_runq_', dname, msgid=msgid, api=True)

# %% ../nbs/00_core.ipynb
def msg_insert_line(
msgid:str, # Message id to edit
insert_line: int, # The line number after which to insert the text (0 for beginning of file)
Copy link
Author

@KeremTurgutlu KeremTurgutlu Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the same param comment from claude official docs for docments.

new_str: str, # The text to insert
dname:str='' # Running dialog to get info for; defaults to current dialog
):
"Insert text at a specific line number in a message"
return call_endp('msg_insert_line_', dname, json=True, msgid=msgid, insert_line=insert_line, new_str=new_str)

# %% ../nbs/00_core.ipynb
def msg_str_replace(
msgid:str, # Message id to edit
old_str: str, # Text to find and replace
new_str: str, # Text to replace with
dname:str='' # Running dialog to get info for; defaults to current dialog
):
"Replace first occurrence of old_str with new_str in a message"
return call_endp('msg_str_replace_', dname, json=True, msgid=msgid, old_str=old_str, new_str=new_str)

# %% ../nbs/00_core.ipynb
def msg_strs_replace(
msgid:str, # Message id to edit
old_strs:list[str], # List of strings to find and replace
new_strs:list[str], # List of replacement strings (must match length of old_strs)
dname:str='' # Running dialog to get info for; defaults to current dialog
):
"Replace multiple strings simultaneously in a message"
return call_endp('msg_strs_replace_', dname, json=True, msgid=msgid, old_strs=old_strs, new_strs=new_strs)

# %% ../nbs/00_core.ipynb
def msg_replace_lines(
msgid:str, # Message id to edit
start_line:int, # Starting line number to replace (1-based indexing)
end_line:int, # Ending line number to replace (1-based indexing, inclusive)
new_content:str, # New content to replace the specified lines
dname:str='' # Running dialog to get info for; defaults to current dialog
):
"Replace a range of lines with new content in a message"
return call_endp('msg_replace_lines_', dname, json=True, msgid=msgid, start_line=start_line, end_line=end_line, new_content=new_content)

# %% ../nbs/00_core.ipynb
def load_gist(gist_id:str):
"Retrieve a gist"
Expand Down Expand Up @@ -298,10 +344,18 @@ def tool_info():
- &`find_msgs`: Find messages in current specific dialog that contain the given information.
- (solveit can often get this id directly from its context, and will not need to use this if the required information is already available to it.)
- &`read_msg`: Get the message indexed in the current dialog.
- To get the exact message use `n=0` and `relative=True` together with `msgid`.
- To get a relative message use `n` (relative position index).
- To get the nth message use `n` with `relative=False`, e.g `n=0` first message, `n=-1` last message.
Comment on lines +347 to +349
Copy link
Author

@KeremTurgutlu KeremTurgutlu Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With many different ways to read msgs, this should hopefully guide AI on how to call the read_msg tool properly for a given use case.

- &`del_msg`: Delete a message from the dialog.
- &`add_msg`: Add/update a message to the queue to show after code execution completes.
- &`update_msg`: Update an existing message.
- &`url2note`: Read URL as markdown, and add a note below current message with the result'''
- &`url2note`: Read URL as markdown, and add a note below current message with the result
- &`msg_insert_line`: Insert text at a specific location in a message.
- &`msg_str_replace`: Find and replace text in a message.
- &`msg_strs_replace`: Find and replace multiple strings in a message.
- &`msg_replace_lines`: Replace a range of lines in a message with new content.
- Always first use `read_msg( msgid=msgid, n=0, relative=True, nums=True)` to view the content with line numbers.'''
Copy link
Author

@KeremTurgutlu KeremTurgutlu Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we instruct AI to first read_msg with nums=True before using msg_replace_lines so that it can see the line numbers before setting the start and end line numbers.

add_msg(cts)

# %% ../nbs/00_core.ipynb
Expand Down
Loading