ToCode: post #6027 โ€” TG.ME

๐Ÿ“Œ ื™ื•ื 7: ื‘ื•ืื• ื ื›ืชื•ื‘ ืกื•ื›ืŸ ืงื™ื“ื•ื“

ืื•ื”ื‘ื™ื ืืช ืงืœื•ื“ ืงื•ื“? ื”ื™ื•ื ื ื›ืชื•ื‘ ืื—ื“ ื›ื“ื™ ืœื”ื‘ื™ืŸ ืื™ืš ื”ื•ื ืขื•ื‘ื“ - ื•ื–ื” ื™ื”ื™ื” ื”ืจื‘ื” ื™ื•ืชืจ ืงืœ ืžืžื” ืฉื“ืžื™ื™ื ืชื. ืงื•ื“ ื”ื“ื•ื’ืžื” ื”ืžืœื ื ืžืฆื ื‘ืชื™ืงื™ื™ืช ื”ื“ื•ื’ืžืื•ืช ื‘ืงื™ืฉื•ืจ:

https://github.com/ynonp/pydanticai-demos/tree/main/07-minicoder

โœ ืžื” ื–ื” ืกื•ื›ืŸ ืงื™ื“ื•ื“

ืกื•ื›ืŸ ืงื™ื“ื•ื“ ื”ื•ื ืชื•ื›ื ื” ืฉืžืฉืชืžืฉืช ื‘ืžื•ื“ืœ ืฉืคื” ื›ื“ื™ ืœื›ืชื•ื‘ ืงื•ื“. ืื ื—ื ื• ื›ื‘ืจ ื™ื•ื“ืขื™ื ืื™ืš ืœื›ืชื•ื‘ ืกื•ื›ื ื™ื ื•ืื™ืš ืœืชืช ืœื”ื ื’ื™ืฉื” ืœื›ืœื™ื - ืฉื–ื” ื‘ื’ื“ื•ืœ ื›ืœ ืžื” ืฉืื ื—ื ื• ืฆืจื™ื›ื™ื ื‘ืฉื‘ื™ืœ ืœื‘ื ื•ืช ืกื•ื›ืŸ ืงื™ื“ื•ื“.

ื”ืกื•ื›ืŸ ืฉื ื‘ื ื” ืžืชื—ื™ืœ ื‘ืชื™ืงื™ื™ืช ืขื‘ื•ื“ื” ื•ืžื—ื›ื” ืœืคืจื•ืžืคื˜ ืžื”ืžืฉืชืžืฉ. ื”ื”ื•ื“ืขื” ืžืชืืจืช ืžื•ืฆืจ ืื• ืคื™ืฆ'ืจ ืฉื”ืžืฉืชืžืฉ ืจื•ืฆื” ืœื‘ื ื•ืช. ื”ืกื•ื›ืŸ ื‘ืชื’ื•ื‘ื” ืฆืจื™ืš ืœืขื“ื›ืŸ ืืช ื”ืงื•ื“ ืฉื‘ืชื™ืงื™ื™ื” ืื• ืœื™ืฆื•ืจ ืงื‘ืฆื™ ืงื•ื“ ื—ื“ืฉื™ื ื›ื“ื™ ืฉื‘ืกื•ืฃ ื ืงื‘ืœ ืžืขืจื›ืช ืขื•ื‘ื“ืช.

ืื—ืจื™ ืฉื”ืกื•ื›ืŸ ืกื™ื™ื ืกื‘ื‘ ื‘ื ื™ื™ื” ื—ื•ื–ืจื™ื ืœืคืจื•ืžืคื˜ ื›ื“ื™ ืœืงื‘ืœ ืžื”ืžื”ืฉืชืžืฉ ืืช ื”ื‘ืงืฉื” ื”ื‘ืื”.

ืขืœ ื”ื“ื‘ืจ ื”ื–ื” ืงืœื•ื“ ืงื•ื“ ื›ืžื•ื‘ืŸ ื”ื•ืกื™ืคื• ื”ืžื•ืŸ ื“ื‘ืจื™ื: ื ื™ื”ื•ืœ Sessions, ืืคืฉืจื•ืช ืœื—ื–ื•ืจ ืœืฉื™ื—ื•ืช ื™ืฉื ื•ืช, ืกื™ื›ื•ื ืฉื™ื—ื” ื›ืฉื”ื™ื ืžืชืืจื›ืช, ื‘ื—ื™ืจืช ืžื•ื“ืœ, ืคืœืื’ื™ื ื™ื, ืงื‘ืฆื™ ื”ื•ืจืื•ืช ื•ืขื•ื“ ื”ืžื•ืŸ ืคื™ืฆ'ืจื™ื ืฉื”ื•ืคื›ื™ื ืื•ืชื• ืœืžื•ืฆืจ ืžืฆื•ื™ืŸ. ืื ื™ ืœื ืžืžืœื™ืฅ ืฉืชืขื–ื‘ื• ืืช ืงืœื•ื“ ืงื•ื“ ื‘ืฉื‘ื™ืœ ืกื•ื›ืŸ ืฉืืชื ื›ื•ืชื‘ื™ื. ืื ื™ ื›ืŸ ืžืืžื™ืŸ ืฉืœื›ืชื•ื‘ ืกื•ื›ืŸ ืงื™ื“ื•ื“ ืžืืคืก ืขื•ื–ืจ ื‘ืฉื‘ื™ืœ ืœื”ื‘ื™ืŸ ื›ืœื™ื ื›ืžื• ืงืœื•ื“ ืงื•ื“ ื•ืœืขื‘ื•ื“ ืื™ืชื ื‘ืฆื•ืจื” ื™ืขื™ืœื” ื™ื•ืชืจ.

โœ ืื™ืš ื–ื” ืขื•ื‘ื“

ืกื•ื›ืŸ ืงื™ื“ื•ื“ ืžื•ืจื›ื‘ ืžืœื•ืœืื” ืžืจื›ื–ื™ืช ืฉื ืงืจืืช ืœื•ืœืืช ื”ืกื•ื›ืŸ. ื”ืœื•ืœืื” ืžืงื‘ืœืช ืคืจื•ืžืคื˜ ืžื”ืžืฉืชืžืฉ ื•ืื– ืžืคืขื™ืœื” run_sync (ืื• ืจื™ืฆื” ืืกื™ื ื›ืจื•ื ื™ืช ืื ืืชื ืจื•ืฆื™ื ืœืจืื•ืช ืืช ื”ืคืœื˜ ื‘ื”ื–ืจืžื”). ื”ืกื•ื›ืŸ ืžืงื‘ืœ ืืช ื›ืœ ื”ื™ืกื˜ื•ืจื™ื™ืช ื”ืฉื™ื—ื” ื•-4 ื›ืœื™ื:

1. ื›ืœื™ ืงืจื™ืืช ืงื•ื‘ืฅ.
2. ื›ืœื™ ื›ืชื™ื‘ืช ืงื•ื‘ืฅ ืžืืคืก.
3. ื›ืœื™ ืฉื™ื ื•ื™ ื˜ืงืกื˜ ื‘ืงื•ื‘ืฅ.
4. ื›ืœื™ ื”ืคืขืœืช ืคืงื•ื“ื” ืžืฉื•ืจืช ื”ืคืงื•ื“ื” (bash).

ืืจื‘ืขืช ื”ื›ืœื™ื ื”ืืœื” ืžืกืคื™ืงื™ื ื›ื“ื™ ืฉื”ืกื•ื›ืŸ ื™ื•ื›ืœ ืœื—ืงื•ืจ ืืช ื”ืคืจื•ื™ืงื˜ ื•ืœื‘ื ื•ืช ื›ืœ ืคื™ืฆ'ืจ ืฉืืชื ืฆืจื™ื›ื™ื.

โœ ืœื•ืœืืช ื”ืกื•ื›ืŸ

ื”ืœื•ืœืื” ื”ืจืืฉื™ืช ืฉืœ ื”ืกื•ื›ืŸ ื”ื™ื:

    while True:
try:
user_input = input("you> ").strip()
except (EOFError, KeyboardInterrupt):
print()
break

if not user_input:
continue
if user_input in ("exit", "quit"):
break

try:
result = agent.run_sync(user_input, deps=deps, message_history=history)
history = result.all_messages()
print(f"\n{result.output}\n")
except Exception as exc: # keep the session alive on any single failure
print(f"\n[error: {exc}]\n")

ื•ื”ื™ื ื‘ืกืš ื”ื›ืœ ืงื•ืจืืช ืคืงื•ื“ื” ืžื”ืžืฉืชืžืฉ, ืžืคืขื™ืœื” run_sync, ืฉื•ืžืจืช ืืช ื”ืชื•ืฆืื” ื•ืžืžืฉื™ื›ื” ืœืื™ื˜ืจืฆื™ื” ื”ื‘ืื”. ืคืจืžื˜ืจ ื”ืชืœื•ื™ื•ืช deps ืžื›ื™ืœ ืืช ืชื™ืงื™ื™ืช ื”ืขื‘ื•ื“ื” ื‘ื” ืžื•ืชืจ ืœืกื•ื›ืŸ ืœืขื‘ื•ื“:

work_dir = Path(sys.argv[1]).resolve()
work_dir.mkdir(parents=True, exist_ok=True)

deps = Deps(work_dir=work_dir)


ื‘ืชื•ืš ื”ื›ืœื™ื ื ื•ื•ื“ื ืฉื›ืœ ืžื” ืฉืื ื—ื ื• ืขื•ืฉื™ื ื™ืชื‘ืฆืข ื‘ืื•ืชื” ืชื™ืงื™ื™ืช ืขื‘ื•ื“ื”.

โœ ื›ืœื™ ืงืจื™ืืช ืงื•ื‘ืฅ

ื›ืœื™ ืงืจื™ืืช ื”ืงื•ื‘ืฅ ืžืงื‘ืœ ืืช ืฉื ื”ืงื•ื‘ืฅ, ืžืื™ืคื” ืžืชื—ื™ืœื™ื ืœืงืจื•ื ื•ื›ืžื” ืžื™ื“ืข ืžืงืกื™ืžื•ื ืื ื—ื ื• ืžื•ื›ื ื™ื ืœืงืจื•ื ื•ืžื—ื–ื™ืจ ืืช ื”ืชื•ื›ืŸ:

def read_file(
ctx: RunContext[Deps], file: str, offset: int = 0, limit: int = MAX_OUTPUT
) -> str:
"""Read a file and return its text from ``offset`` for ``limit`` chars."""
path = _resolve(ctx.deps.work_dir, file)
if not path.is_file():
return f"Error: no such file: {file}"
text = path.read_text(encoding="utf-8", errors="replace")
return text[offset : offset + limit]


ื”ื’ื‘ืœืช ืื•ืจืš ื”ืคืœื˜ ื—ืฉื•ื‘ื” ื›ื™ ื”ื™ื ืžืืคืฉืจืช ืœืกื•ื›ืŸ ืœืฉืœื•ื˜ ืขืœ ื—ืœื•ืŸ ื”ืงื•ื ื˜ืงืกื˜ ื•ืžื•ื ืขืช ื‘ื–ื‘ื•ื– ื˜ื•ืงื ื™ื.

โœ ื›ืชื™ื‘ืช ืงื•ื‘ืฅ

ื”ื›ืœื™ ื”ืฉื ื™ ืœื›ืชื™ื‘ืช ืงื•ื‘ืฅ ื ืจืื” ื›ืš:

def write_file(ctx: RunContext[Deps], file: str, new_content: str) -> str:
"""Create ``file`` or replace its entire contents with ``new_content``."""
path = _resolve(ctx.deps.work_dir, file)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(new_content, encoding="utf-8")
return f"Wrote {len(new_content)} chars to {file}"


ื”ื•ื ืœื•ืงื— ืฉื ืงื•ื‘ืฅ ื•ืชื•ื›ืŸ ื•ื›ื•ืชื‘ ื”ื›ืœ ืœืงื•ื‘ืฅ. ื ืฉื™ื ืœื‘ ืฉื›ืชื™ื‘ืช ืงื•ื‘ืฅ ืฉืœื ืžื›ื ื™ืกื” ืืช ื›ืœ ืชื•ื›ืŸ ื”ืงื•ื‘ืฅ ืœ Context ื‘ืชื•ืจ ืคืจืžื˜ืจ ืฉืœ ื›ืœื™ ืฉื™ืžืฉื™ืš ืœืœื•ื•ืช ืื•ืชื ื• ื‘ื”ื™ืกื˜ื•ืจื™ื™ืช ื”ื”ื•ื“ืขื•ืช. ื‘ืžืขืจื›ื•ืช ืจื‘ื•ืช ืžืกื ื ื™ื ืžื”ื™ืกื˜ื•ืจื™ื™ืช ื”ื”ื•ื“ืขื•ืช ืืช ื”ืคืขืœื•ืช ื”ื›ืœื™ื ื›ื“ื™ ืœื ืœื”ืขืžื™ืก ืขืœ ื”ืงื•ื ื˜ืงืกื˜. ืื ื”ืกื•ื›ืŸ ื™ืจืฆื” ืœื“ืขืช ืžื” ื™ืฉ ื‘ืงื•ื‘ืฅ ืฉื™ืฉืชืžืฉ ื‘ื›ืœื™ read file.

โœ ืขืจื™ื›ืช ืงื•ื‘ืฅ

ืžื™ืžื•ืฉ ืคืฉื•ื˜ ืœืคืขื•ืœืช ื”ืขืจื™ื›ื” ืžืงื‘ืœ ืฉืชื™ ืžื—ืจื•ื–ื•ืช - ื˜ืงืกื˜ ืœื”ื—ืœืคื” ื•ื”ื˜ืงืกื˜ ืœื›ืชื•ื‘ ื‘ืžืงื•ืžื•. ื–ื” ื ืฉืžืข ืžืขื˜ ืื‘ืœ ื–ื” ืžืกืคื™ืง. ืื ื”ื˜ืงืกื˜ ืœื”ื—ืœืคื” ืžื•ืคื™ืข ื›ืžื” ืคืขืžื™ื ื‘ืงื•ื‘ืฅ ื ื—ื–ื™ืจ ืœืกื•ื›ืŸ ืฉื’ื™ืื” ื•ื ื‘ืงืฉ ืœืงื‘ืœ ื˜ืงืกื˜ ืœื”ื—ืœืคื” ื™ื•ืชืจ ืืจื•ืš ื•ื›ืš ื™ื™ื—ื•ื“ื™:

def edit_file(
GitHub
pydanticai-demos/07-minicoder at main ยท ynonp/pydanticai-demos
Contribute to ynonp/pydanticai-demos development by creating an account on GitHub.
August 19, 2026 90 1