SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]
Feat: Allow to add extra fields used for templates
Kilian Lieret3b09257a8f04
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.pyindex 380b6ed8..b30d5e7a 100644--- a/sweagent/agent/agents.py+++ b/sweagent/agent/agents.py@@ -284,6 +284,7 @@ class Agent:command_docs=self.tools.config.command_docs,**self.tools.config.env_variables,problem_statement=self._problem_statement.get_problem_statement(),+ **self._problem_statement.get_extra_fields(),)self.logger.info(f"SYSTEM ({self.name})\n{system_msg}")self._append_history({"role": "system", "content": system_msg, "agent": self.name})diff --git a/sweagent/environment/config/problem_statement.py b/sweagent/environment/config/problem_statement.pyindex 825652d6..00995f59 100644--- a/sweagent/environment/config/problem_statement.py+++ b/sweagent/environment/config/problem_statement.py@@ -19,6 +19,8 @@ class ProblemStatement(Protocol):def get_problem_statement(self) -> str: ...+ def get_extra_fields(self) -> dict[str, Any]: ...+class EmptyProblemStatement(BaseModel):id: str = Field(default_factory=lambda: str(uuid.uuid4()))@@ -30,10 +32,20 @@ class EmptyProblemStatement(BaseModel):def get_problem_statement(self) -> str:return ""+ def get_extra_fields(self) -> dict[str, Any]:+ return {}+++_EXTRA_FIELDS_DOC = """Any additional data to be added to the instance.+This data will be available when formatting prompt templates.+"""+class TextProblemStatement(BaseModel):text: str+ extra_fields: dict[str, Any] = Field(default_factory=dict, description=_EXTRA_FIELDS_DOC)+type: Literal["text"] = "text""""Discriminator for (de)serialization/CLI. Do not change."""@@ -49,6 +61,9 @@ class TextProblemStatement(BaseModel):def get_problem_statement(self) -> str:return self.text+ def get_extra_fields(self) -> dict[str, Any]:+ return self.extra_fields+def __repr__(self) -> str:return f"TextProblemStatement(id={self.id}, text={self.text[:30]}...)"@@ -59,6 +74,8 @@ class TextProblemStatement(BaseModel):class FileProblemStatement(BaseModel):path: Path+ extra_fields: dict[str, Any] = Field(default_factory=dict, description=_EXTRA_FIELDS_DOC)+type: Literal["text_file"] = "text_file""""Discriminator for (de)serialization/CLI. Do not change."""@@ -74,10 +91,15 @@ class FileProblemStatement(BaseModel):def get_problem_statement(self) -> str:return self.path.read_text()+ def get_extra_fields(self) -> dict[str, Any]:+ return self.extra_fields+class GithubIssue(BaseModel):github_url: str+ extra_fields: dict[str, Any] = Field(default_factory=dict, description=_EXTRA_FIELDS_DOC)+type: Literal["github"] = "github""""Discriminator for (de)serialization/CLI. Do not change."""@@ -95,6 +117,9 @@ class GithubIssue(BaseModel):owner, repo, issue_number = _parse_gh_issue_url(self.github_url)return _get_problem_statement_from_github_issue(owner, repo, issue_number, token=os.getenv("GITHUB_TOKEN"))+ def get_extra_fields(self) -> dict[str, Any]:+ return self.extra_fields+ProblemStatementConfig = TextProblemStatement | GithubIssue | EmptyProblemStatement | FileProblemStatementdiff --git a/sweagent/run/batch_instances.py b/sweagent/run/batch_instances.pyindex a401c619..81fca3e7 100644--- a/sweagent/run/batch_instances.py+++ b/sweagent/run/batch_instances.py@@ -115,6 +115,10 @@ class SimpleBatchInstance(BaseModel):"""base_commit: str = "HEAD""""Used to reset repo."""+ extra_fields: dict[str, Any] = Field(default_factory=dict)+ """Any additional data to be added to the instance.+ This data will be available when formatting prompt templates.+ """def to_full_batch_instance(self, deployment: DeploymentConfig) -> BatchInstance:"""Merge the deployment options into the `SimpleBatchInstance` object to get a full `BatchInstance`."""@@ -122,7 +126,9 @@ class SimpleBatchInstance(BaseModel):# subclasses, we use a TypeAdapter to validate/instantiate the object.# Very important: Make a copy of the deployment config because it will be shared among instances!!!deployment = deployment.model_copy()- problem_statement = TextProblemStatement(text=self.problem_statement, id=self.id)+ problem_statement = TextProblemStatement(+ text=self.problem_statement, id=self.id, extra_fields=self.extra_fields+ )if not self.repo_name:repo = Noneelif "github" in self.repo_name: