Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions services/chat_with_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AgentResult:
token_input: int
token_output: int
is_completed: bool
completion_reason: str
p: int
is_planned: bool

Expand Down Expand Up @@ -89,13 +90,25 @@ async def chat_with_agent(
token_input=token_input,
token_output=token_output,
is_completed=False,
completion_reason="",
p=p,
is_planned=False,
)

# Append assistant message before processing tool calls
messages.append(response_message)

# Extract text from the assistant message for completion context
content = response_message["content"]
if isinstance(content, str):
assistant_text = content
else:
assistant_text = "".join(
block.get("text", "")
for block in content
if isinstance(block, dict) and block.get("type") == "text"
)

# Process all tool calls
tool_result_blocks: list[ToolResultBlockParam] = []
log_msgs: list[str] = []
Expand Down Expand Up @@ -411,6 +424,7 @@ async def chat_with_agent(
token_input=token_input,
token_output=token_output,
is_completed=is_completed,
completion_reason=assistant_text,
p=p + 5 * len(tool_calls),
is_planned=False,
)
24 changes: 19 additions & 5 deletions services/webhook/setup_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
get_latest_remote_commit_sha,
)
from services.claude.tools.tools import TOOLS_FOR_SETUP
from services.github.pulls.create_pull_request import create_pull_request
from services.github.pulls.close_pull_request import close_pull_request
from services.github.pulls.create_pull_request import create_pull_request
from services.github.pulls.get_pull_request_files import get_pull_request_files
from services.github.types.github_types import BaseArgs
from services.slack.slack_notify import slack_notify
from services.supabase.usage.insert_usage import insert_usage
Expand Down Expand Up @@ -182,6 +183,7 @@ async def setup_handler(
total_token_output = 0
is_completed = False

completion_reason = ""
for _iteration in range(MAX_ITERATIONS):
result = await chat_with_agent(
messages=messages,
Expand All @@ -197,6 +199,7 @@ async def setup_handler(
messages = result.messages
total_token_input += result.token_input
total_token_output += result.token_output
completion_reason = result.completion_reason

if result.is_completed:
is_completed = True
Expand All @@ -217,10 +220,21 @@ async def setup_handler(
token_output=total_token_output,
)

if is_completed:
slack_notify(f"Setup completed for {owner_name}/{repo_name}#{pr_number}\n{pr_url}", thread_ts=thread_ts)
# Check if the PR has actual file changes
pr_files = get_pull_request_files(
owner=owner_name, repo=repo_name, pull_number=pr_number, token=token
)

if is_completed and pr_files:
slack_notify(
f"Setup completed for {owner_name}/{repo_name}#{pr_number}\n{pr_url}",
thread_ts=thread_ts,
)
else:
logger.warning("Setup agent did not complete, closing PR and deleting branch")
logger.info("Closing PR, no file changes. Agent: %s", completion_reason)
close_pull_request(pull_number=pr_number, base_args=base_args)
delete_remote_branch(base_args=base_args)
slack_notify(f"Setup did not complete for {owner_name}/{repo_name}, PR closed", thread_ts=thread_ts)
slack_notify(
f"Setup closed for {owner_name}/{repo_name}: {completion_reason}",
thread_ts=thread_ts,
)
Loading