Environment utils
InstanceBuilder
Source code in sweagent/environment/utils.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
|
__init__(token=None)
This helper class is used to build the data for an instance object, retrieving problem statements from github issues or local files and setting repo paths from github urls or local paths.
Source code in sweagent/environment/utils.py
510 511 512 513 514 515 516 517 518 |
|
set_problem_statement(data_path)
Get problem statement for a single instance from a github issue url or a path to a markdown or text file.
Source code in sweagent/environment/utils.py
539 540 541 542 543 544 545 546 547 548 549 550 |
|
copy_anything_to_container(container, host_path, container_path)
Copy files or directories from host to container
Note: Will need to set ownership on the copied files in the container.
Source code in sweagent/environment/utils.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
copy_file_to_container(container, contents, container_path)
Copies a given string into a Docker container at a specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container |
Container
|
Docker SDK container object. |
required |
contents |
str
|
The string to copy into the container. |
required |
container_path |
str
|
The path inside the container where the string should be copied to. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in sweagent/environment/utils.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
format_trajectory_markdown(trajectory)
Format a trajectory as a markdown string for use in gh PR description.
Source code in sweagent/environment/utils.py
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
|
get_associated_commit_urls(org, repo, issue_number, *, token='')
Return the URLs of commits that would close an issue.
Source code in sweagent/environment/utils.py
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 |
|
get_commit(api, owner, repo, ref=None)
Get commit object from github api
Parameters:
Name | Type | Description | Default |
---|---|---|---|
api |
GhApi
|
|
required |
owner |
str
|
Repo owner, e.g., "princeton-nlp" |
required |
repo |
str
|
Repo, e.g., "SWE-agent" |
required |
ref |
str
|
Branch, tag or commit hash |
None
|
Returns:
Name | Type | Description |
---|---|---|
_type_ |
description |
Source code in sweagent/environment/utils.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
get_container(ctr_name, image_name, persistent=False)
Get a container object for a given container name and image name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctr_name |
str
|
Name of container |
required |
image_name |
str
|
Name of image |
required |
persistent |
bool
|
Whether to use a persistent container or not |
False
|
Returns: Container object
Source code in sweagent/environment/utils.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
|
get_data_path_name(data_path)
if data_path is a file, return the file stem elif it's a github url, return the owner__repo_name
Source code in sweagent/environment/utils.py
35 36 37 38 39 40 41 42 43 44 45 |
|
get_gh_issue_data(issue_url, *, token='')
Returns github issue data in the form of a dictionary. See https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#get-an-issue for return format
Source code in sweagent/environment/utils.py
490 491 492 493 494 495 496 497 |
|
get_instances(file_path, base_commit=None, split=None, token=None, *, repo_path='')
Getter function for handling json, jsonl files
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
Path to file |
required |
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
List of instances as dictionaries |
Source code in sweagent/environment/utils.py
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
|
get_problem_statement_from_github_issue(owner, repo, issue_number, *, token='')
Return problem statement from github issue
Source code in sweagent/environment/utils.py
500 501 502 503 504 505 506 |
|
image_exists(image_name)
Check that the image exists and give some better error messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_name |
str
|
Name of image |
required |
Returns: bool: True if image exists
Source code in sweagent/environment/utils.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
|
is_github_issue_url(data_path)
Check if data_path is an URL pointing to a github issue
Source code in sweagent/environment/utils.py
48 49 50 |
|
is_github_repo_url(data_path)
Check if data_path is an URL pointing to a github repository. Paths to issues or PRs will also match this pattern.
Source code in sweagent/environment/utils.py
53 54 55 56 57 |
|
parse_gh_issue_url(issue_url)
Returns:
Name | Type | Description |
---|---|---|
owner |
str
|
Repo owner |
repo |
str
|
Repo name |
str
|
issue number: Issue number as str |
Raises:
Type | Description |
---|---|
InvalidGithubURL
|
If the URL is not a valid github issue URL |
Source code in sweagent/environment/utils.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
|
parse_gh_repo_url(repo_url)
Returns:
Name | Type | Description |
---|---|---|
owner |
str
|
Repo owner/org |
repo |
str
|
Repo name |
Raises:
Type | Description |
---|---|
InvalidGithubURL
|
If the URL is not a valid github repo URL |
Source code in sweagent/environment/utils.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
|
read_with_timeout(container, pid_func, timeout_duration)
Read data from a subprocess with a timeout. This function uses a file descriptor to read data from the subprocess in a non-blocking way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container |
Popen
|
The subprocess container. |
required |
pid_func |
Callable
|
A function that returns a list of process IDs (except the PID of the main process). |
required |
timeout_duration |
int
|
The timeout duration in seconds. |
required |
Returns:
Name | Type | Description |
---|---|---|
output |
str
|
The data read from the subprocess, stripped of trailing newline characters. |
Raises:
Type | Description |
---|---|
TimeoutError
|
If the timeout duration is reached while reading from the subprocess. |
Source code in sweagent/environment/utils.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
read_with_timeout_experimental(container, timeout_duration)
Read data from a subprocess with a timeout. This function uses a file descriptor to read data from the subprocess in a non-blocking way.
NOTE: This is an experimental implementation that is faster than read_with_timeout
, but
has not been thoroughly tested.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container |
Popen
|
The subprocess container. |
required |
timeout_duration |
int
|
The timeout duration in seconds. |
required |
Returns:
Type | Description |
---|---|
tuple[str, str]
|
Output and exit code, both as strings (!) |
Raises:
Type | Description |
---|---|
TimeoutError
|
If the timeout duration is reached while reading from the subprocess. |
Source code in sweagent/environment/utils.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|