Guides
Agent Tool Definition
Agents are continuously running LLMs that process user requests by calling a sequence of tools. Unlike workflows, they are fully autonomous and can be more flexible at coding.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
class RelaceEditToolSchema(ToolSchema, name="edit_file"):
"""Use this tool to propose an edit to an existing file or create a new file.
If you are performing an edit follow these formatting rules:
- Abbreviate sections of the code in your response that will remain the same by replacing those sections with a comment like "// ... rest of code ...", "// ... keep existing code ...", "// ... code remains the same".
- Be precise with the location of these comments within your edit snippet. A less intelligent model will use the context clues you provide to accurately merge your edit snippet.
- If applicable, it can help to include some concise information about the specific code segments you wish to retain "// ... keep calculateTotalFunction ... ".
- If you plan on deleting a section, you must provide the context to delete it. Some options:
1. If the initial code is ```code \n Block 1 \n Block 2 \n Block 3 \n code```, and you want to remove Block 2, you would output ```// ... keep existing code ... \n Block 1 \n Block 3 \n // ... rest of code ...```.
2. If the initial code is ```code \n Block \n code```, and you want to remove Block, you can also specify ```// ... keep existing code ... \n // remove Block \n // ... rest of code ...```.
- You must use the comment format applicable to the specific code provided to express these truncations.
- Preserve the indentation and code structure of exactly how you believe the final code will look (do not output lines that will not be in the final code after they are merged).
- Be as length efficient as possible without omitting key context.
To create a new file, simply specify the content of the file in the `edit` field.
"""
path: Path = Field(
...,
description="""The target file to modify. You must use an absolute path""",
)
instruction: str = Field(
...,
description="""A single sentence instruction describing the edit to be made.
This helps guide the apply model in merging the changes correctly.
Use first person perspective and focus on clarifying any ambiguous aspects of the edit.
Keep it brief and avoid repeating information from previous messages.""",
)
edit: str = Field(
...,
description="""
Only include the exact code lines that need modification.
Do not include any code that stays the same - those sections should be marked with comments appropriate for the language, like: `// ... existing code ...`
""",
)
import ApplyRequest
class RelaceEditTool(Tool[RelaceEditToolSchema], schema=RelaceEditToolSchema):
def execute_impl(
self,
tool_input: RelaceEditToolSchema,
context: Context,
) -> str:
# Write new file directly and return
if not tool_input.path.exists():
tool_input.path.parent.mkdir(parents=True, exist_ok=True)
tool_input.path.write_text(tool_input.edit)
return f"Created {tool_input.path} ({tool_input.path.stat().st_size} bytes)"
# Call Relace Apply endpoint
try:
request = ApplyRequest(
initialCode=tool_input.path.read_text(),
editSnippet=tool_input.edit,
instruction=tool_input.instruction,
)
result = self.session.post(
f"https://models.relace.ai/v1/code/apply",
json=request.model_dump(mode="json", exclude_none=True),
)
result.raise_for_status()
response = ApplyResponse.model_validate(result.json())
diff = "".join(
difflib.unified_diff(
request.initialCode.splitlines(keepends=True),
response.mergedCode.splitlines(keepends=True),
fromfile="before",
tofile="after",
)
)
# Return the diff as part of the response
if diff:
tool_input.path.write_text(response.mergedCode)
return (
f"Applied code changes using Relace API.\n\nChanges made:\n{diff}"
)
else:
return "No changes made"
except Exception as e:
raise ToolError(f"Error applying code changes: {str(e)}") from e