Tool catalog: saved files and source

Run setup to create a temporary Python project and notes. Follow the sections in order. Inspect existing files, search the source, read an outline, then make checked edits using paths, section addresses, and digests returned in this run. Every write stays in the temporary directory. Its files disappear when this kernel exits.

Tool catalog: saved files and source

Run setup to create a temporary Python project and notes. Follow the sections in order. Inspect existing files, search the source, read an outline, then make checked edits using paths, section addresses, and digests returned in this run. Every write stays in the temporary directory. Its files disappear when this kernel exits.

from pathlib import Path
from tempfile import TemporaryDirectory
import hashlib
from nbinlineai.tools import tool_catalog
from nbinlineai.tools import (
    path_info, list_files, view_file, create_file, file_str_replace, file_insert_line, file_replace_lines, search_files, source_doc, document_outline, read_document_section, file_strs_replace, view_file_hashes, file_replace_checked
)

catalog_temp = TemporaryDirectory(prefix="nbinlineai-catalog-")
catalog_root = Path(catalog_temp.name)
catalog_source = catalog_root / "sample.py"
catalog_source.write_text('"""Disposable source."""\n\ndef double(value: int) -> int:\n    """Double one value."""\n    return value * 2\n', encoding="utf-8")
catalog_notes = catalog_root / "notes.md"
catalog_notes.write_text("# Field notes\nOne observation.\n## Counts\nvisits = 2\n", encoding="utf-8")
catalog_edit = catalog_root / "edit.txt"
catalog_edit.write_text("alpha\nbeta\ngamma\n", encoding="utf-8")
print("Disposable folder:", catalog_root)
# Independent paths keep the AI edits separate from the direct-call examples.
catalog_ai_create_path = str(catalog_root / "ai-created.txt")
catalog_ai_replace_path = str(catalog_root / "ai-replace.txt")
catalog_ai_insert_path = str(catalog_root / "ai-insert.txt")
catalog_ai_lines_path = str(catalog_root / "ai-lines.txt")
catalog_ai_multi_path = str(catalog_root / "ai-multi.txt")
catalog_ai_checked_path = str(catalog_root / "ai-checked.txt")
for path in (catalog_ai_replace_path, catalog_ai_insert_path,
             catalog_ai_lines_path, catalog_ai_multi_path, catalog_ai_checked_path):
    Path(path).write_text("alpha\nbeta\ngamma\n", encoding="utf-8")
print("Five separate disposable files are ready for AI edits.")
Disposable folder: [temporary local path]
Five separate disposable files are ready for AI edits.
print(tool_catalog("files"))
files (13 tools): path_info, list_files, view_file, create_file, file_str_replace, file_insert_line, file_replace_lines, file_strs_replace, view_file_hashes, file_replace_checked, search_files, document_outline, read_document_section

path_info

print(path_info(str(catalog_root)))
Kernel cwd: [temporary local path]
Resolved path: [temporary local path]
Type: directory
Size: not applicable

list_files

print(list_files(str(catalog_root), pattern="*.py"))
Files under [temporary local path]
sample.py

view_file

print(view_file(str(catalog_source), 1, 5))
File: [temporary local path]
1: """Disposable source."""
2: 
3: def double(value: int) -> int:
4:     """Double one value."""
5:     return value * 2

create_file

print(create_file(str(catalog_root / "created.txt"), "Created by the example.\n"))
Created: [temporary local path] (24 bytes)

file_str_replace

print(file_str_replace(str(catalog_edit), "alpha", "first", expected_matches=1))
Updated: [temporary local path]
@@ -1,2 +1,2 @@
-alpha
+first
 beta

file_insert_line

print(file_insert_line(str(catalog_edit), 1, "between"))
Updated: [temporary local path]
@@ -1,2 +1,3 @@
 first
+between
 beta

file_replace_lines

print(file_replace_lines(str(catalog_edit), 2, 2, "second"))
Updated: [temporary local path]
@@ -1,3 +1,3 @@
 first
-between
+second
 beta

search_files

print(search_files("double", path=str(catalog_root), pattern="*.py"))
Saved-file matches under [temporary local path]
[temporary local path] def double(value: int) -> int:

source_doc

print(source_doc(str(catalog_source), symbol="double"))
[temporary local path] double
def double(value: int) -> int:
Double one value.

document_outline

catalog_outline = document_outline(str(catalog_notes))
print(catalog_outline)
Outline: [temporary local path]
s:9ebb77061ede41e957aa6e3152e0473351bddf4e5733c0f12020db058adcca64:0 Entire document (lines 1-4)
s:9ebb77061ede41e957aa6e3152e0473351bddf4e5733c0f12020db058adcca64:1   Field notes (lines 1-4)
s:9ebb77061ede41e957aa6e3152e0473351bddf4e5733c0f12020db058adcca64:2     Counts (lines 3-4)

read_document_section

catalog_counts_address = next(line.split()[0] for line in catalog_outline.splitlines() if "Counts (lines" in line)
print(read_document_section(str(catalog_notes), section=catalog_counts_address))
[temporary local path] s:9ebb77061ede41e957aa6e3152e0473351bddf4e5733c0f12020db058adcca64:2
## Counts
visits = 2

file_strs_replace

print(file_strs_replace(str(catalog_edit), ["first", "gamma"], ["one", "three"]))
Updated: [temporary local path]
@@ -1,4 +1,4 @@
-first
+one
 second
 beta
-gamma
+three

view_file_hashes

catalog_hashes = view_file_hashes(str(catalog_edit), 1, 6)
print(catalog_hashes)
[temporary local path]
sha256: c72ba935833cf59189b20b93360524b1cc695f8756272bae8856f1d7c7d0933c
1: one
2: second
3: beta
4: three

file_replace_checked

catalog_digest = catalog_hashes.splitlines()[1].split(": ", 1)[1]
print(file_replace_checked(str(catalog_edit), "three", "3", catalog_digest))
Updated: [temporary local path]
@@ -3,2 +3,2 @@
 beta
-three
+3

Ask AI to use the same file tools

Run these questions after the direct code examples. They use separate temporary files, so every edit has a fresh, checkable starting state. Each question explicitly names its tools and reports the returned evidence.

On the disposable folder $catalog_root, use &path_info for the folder, &list_files for its *.py files, and &view_file for $catalog_source lines 1–5. Name the file and quote its function signature from the returned text.

The folder is a directory containing sample.py. Its function signature is def double(value: int) -> int:.

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
path_info completed {“path”:“[local path]”} ‘Kernel cwd: [local path] path: [local path] directory\nSize: not applicable’
list_files completed {“path”:“[local path]”,“pattern”:“*.py”} ‘Files under [local path]’
view_file completed {“path”:“[local path]”,“start_line”:1,“end_line”:5} ‘File: [local path] “”“Disposable source.”“”\n2: \n3: def double(value: int) -> int:\n4: “”“Double one value.”“”\n5: return value * 2’

double is defined in sample.py at line 3. It doubles an integer value.

Use &document_outline on $catalog_notes and copy the fresh address of its Counts section. Pass that address to &read_document_section, then report the visit count from the returned section.

Counts address: s:9ebb77061ede41e957aa6e3152e0473351bddf4e5733c0f12020db058adcca64:2. The section reports visits = 2.

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
document_outline completed {“path”:“[local path]”} ‘Outline: [local path] Entire document (lines 1-4)\ns:9ebb77061ede41e957aa6e3152e0473351bddf4e5733c0f12020db058adcca64:1 Field notes (lines 1-4)\ns:9ebb77061ede41e957aa6e3152e0473351bddf4e5733c0f12020db058adcca64:2 Counts (lines 3-4)’
read_document_section completed {“path”:“[local path]”,“section”:“s:9ebb77061ede41e957aa6e3152e0473351bddf4e5733c0f12020db058adcca64:2”} ‘[local path] s:9ebb77061ede41e957aa6e3152e0473351bddf4e5733c0f12020db058adcca64:2\n## Counts\nvisits = 2\n’

Use &create_file at the new path $catalog_ai_create_path with content AI note: three visits.\n. Then use &view_file on that exact path and quote the line it returned.

1: AI note: three visits.

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
create_file completed {“path”:“[local path]”,“contents”:“AI note: three visits.\n”} ‘Created: [local path] (23 bytes)’
view_file completed {“path”:“[local path]”,“start_line”:1,“end_line”:1} ‘File: [local path] AI note: three visits.’

Make three independent, checked changes: use &file_str_replace on $catalog_ai_replace_path to replace the one alpha with first (expected_matches=1); use &file_insert_line on $catalog_ai_insert_path before line 1 to add heading; use &file_replace_lines on $catalog_ai_lines_path to replace line 2 with second. Report each returned result briefly.

All three edits succeeded: ai-replace.txt changed alpha to first; ai-insert.txt gained heading before line 1; and ai-lines.txt changed line 2 from beta to second.

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
file_str_replace completed {“path”:“[local path]”,“old_str”:“alpha”,“new_str”:“first”,“expected_matches”:1} ‘Updated: [local path] -1,2 +1,2 @@\n-alpha\n+first\n beta’
file_insert_line completed {“path”:“[local path]”,“line”:0,“new_str”:“heading”} ‘Updated: [local path] -1 +1,2 @@\n+heading\n alpha’
file_replace_lines completed {“path”:“[local path]”,“start_line”:2,“end_line”:2,“new_content”:“second”} ‘Updated: [local path] -1,3 +1,3 @@\n alpha\n-beta\n+second\n gamma’

On $catalog_ai_multi_path, use &file_strs_replace to change alpha→first and gamma→third. Separately, use &view_file_hashes on $catalog_ai_checked_path for lines 1–3, copy its actual digest, then &file_replace_checked there to change gamma to third with that digest. Report the two edits and digest source.

Both edits succeeded: ai-multi.txt changed alpha → first and gamma → third; ai-checked.txt changed gamma → third. The checked edit used digest 4fdbc441ea7b546100e086ac1e4fc5ae6749b7314311c99db05be450eca12996 returned by view_file_hashes for ai-checked.txt.

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
file_strs_replace completed {“path”:“[local path]”,“old_strings”:[“alpha”,“gamma”],“new_strings”:[“first”,“third”]} ‘Updated: [local path] -1,3 +1,3 @@\n-alpha\n+first\n beta\n-gamma\n+third’
view_file_hashes completed {“path”:“[local path]”,“start_line”:1,“end_line”:3} ‘[local path] 4fdbc441ea7b546100e086ac1e4fc5ae6749b7314311c99db05be450eca12996\n1: alpha\n2: beta\n3: gamma’
file_replace_checked completed {“path”:“[local path]”,“old_str”:“gamma”,“new_str”:“third”,“expected_sha256”:“4fdbc441ea7b546100e086ac1e4fc5ae6749b7314311c99db05be450eca12996”} ‘Updated: [local path] -2,2 +2,2 @@\n beta\n-gamma\n+third’
for label, path in [("created", catalog_ai_create_path), ("replace", catalog_ai_replace_path),
                    ("insert", catalog_ai_insert_path), ("lines", catalog_ai_lines_path),
                    ("multi", catalog_ai_multi_path), ("checked", catalog_ai_checked_path)]:
    item = Path(path)
    if item.exists():
        print(label, item.read_text(encoding="utf-8").strip().replace("\n", " | "))
    else:
        print(label, "pending AI create_file result")
created AI note: three visits.
replace first | beta | gamma
insert heading | alpha | beta | gamma
lines alpha | second | gamma
multi first | beta | third
checked alpha | beta | third

← Examples guide · Download this notebook