ConnectOnionConnectOnion
DocsUseful ToolsDiff Writer

DiffWriter

Human-in-the-loop file writing with diff display and approval.

Installation

main.py
1from connectonion import DiffWriter 2 3writer = DiffWriter()

API

write(path, content)

Write content to a file with diff display and user approval.

main.py
1result = writer.write("hello.py", "print('hello')") 2# Shows colorized diff 3# Asks user to choose: 1=Yes, 2=Yes to all, 3=No + feedback 4# Returns: "Wrote 15 bytes to hello.py" or feedback message

diff(path, content)

Show diff without writing (preview mode).

main.py
1diff_text = writer.diff("hello.py", "print('hello')") 2# Returns the diff string without writing

read(path)

Read file contents.

main.py
1content = writer.read("hello.py") 2# Returns: "print('hello')"

Approval Options

When a file change is proposed, user sees:

╭─── Changes to hello.py ────────────────────────╮
│ --- a/hello.py │
│ +++ b/hello.py │
│ @@ -1,2 +1,3 @@ │
│ def hello(): │
│ - pass │
│ + print("Hello!") │
╰────────────────────────────────────────────────╯
Choose an option:
  1 - Yes, apply this change
  2 - Yes to all (auto-approve for this session)
  3 - No, and tell agent what to do instead

Apply changes to hello.py? [1/2/3]:
OptionEffect
1Apply this change, ask again for next change
2Apply this and all future changes (session-wide)
3Reject + provide feedback for agent to try again

Options

auto_approve

Skip approval prompts (for automation).

main.py
1# Ask for approval (default) 2writer = DiffWriter(auto_approve=False) 3 4# Auto-approve all writes 5writer = DiffWriter(auto_approve=True)

Use with Agent

main.py
1from connectonion import Agent, DiffWriter 2 3writer = DiffWriter() 4agent = Agent("coder", tools=[writer]) 5 6agent.input("create a hello.py file with a hello world function") 7# Agent calls writer.write() 8# User sees diff and chooses 1, 2, or 3 9# If 3: User provides feedback, agent receives it and tries again

Feedback Flow

When user chooses option 3 (reject):

  1. User is prompted: "What should the agent do instead?"
  2. User types feedback, e.g., "use snake_case for function names"
  3. Agent receives: "User rejected changes to hello.py. Feedback: use snake_case for function names"
  4. Agent can retry with the feedback

Common Use Cases

main.py
1# Interactive coding with approval 2writer = DiffWriter() 3agent = Agent("coder", tools=[writer]) 4 5# CI/CD automation - skip prompts 6writer = DiffWriter(auto_approve=True) 7agent = Agent("automation", tools=[writer]) 8 9# Preview changes only 10diff = writer.diff("config.py", new_config) 11print(diff)