refactor: replace GitHub/Linear/Slack with Gitea/Discord in server.py

This commit is contained in:
머니페니 2026-03-20 15:04:48 +09:00
parent 4382499071
commit 64e54a7392

View File

@ -1,4 +1,4 @@
"""Main entry point and CLI loop for Open SWE agent.""" """Main entry point and CLI loop for galaxis-agent."""
# ruff: noqa: E402 # ruff: noqa: E402
# Suppress deprecation warnings from langchain_core (e.g., Pydantic V1 on Python 3.14+) # Suppress deprecation warnings from langchain_core (e.g., Pydantic V1 on Python 3.14+)
@ -24,7 +24,6 @@ warnings.filterwarnings("ignore", message=".*Pydantic V1.*", category=UserWarnin
# Now safe to import agent (which imports LangChain modules) # Now safe to import agent (which imports LangChain modules)
from deepagents import create_deep_agent from deepagents import create_deep_agent
from deepagents.backends.protocol import SandboxBackendProtocol from deepagents.backends.protocol import SandboxBackendProtocol
from langsmith.sandbox import SandboxClientError
from .middleware import ( from .middleware import (
ToolErrorMiddleware, ToolErrorMiddleware,
@ -35,13 +34,12 @@ from .middleware import (
from .prompt import construct_system_prompt from .prompt import construct_system_prompt
from .tools import ( from .tools import (
commit_and_open_pr, commit_and_open_pr,
discord_reply,
fetch_url, fetch_url,
github_comment, gitea_comment,
http_request, http_request,
linear_comment,
slack_thread_reply,
) )
from .utils.auth import resolve_github_token from .utils.auth import get_gitea_token
from .utils.model import make_model from .utils.model import make_model
from .utils.sandbox import create_sandbox from .utils.sandbox import create_sandbox
@ -52,7 +50,7 @@ SANDBOX_CREATION_TIMEOUT = 180
SANDBOX_POLL_INTERVAL = 1.0 SANDBOX_POLL_INTERVAL = 1.0
from .utils.agents_md import read_agents_md_in_sandbox from .utils.agents_md import read_agents_md_in_sandbox
from .utils.github import ( from .utils.git_utils import (
_CRED_FILE_PATH, _CRED_FILE_PATH,
cleanup_git_credentials, cleanup_git_credentials,
git_has_uncommitted_changes, git_has_uncommitted_changes,
@ -64,19 +62,23 @@ from .utils.sandbox_paths import aresolve_repo_dir, aresolve_sandbox_work_dir
from .utils.sandbox_state import SANDBOX_BACKENDS, get_sandbox_id_from_metadata from .utils.sandbox_state import SANDBOX_BACKENDS, get_sandbox_id_from_metadata
class SandboxConnectionError(Exception):
"""Raised when the sandbox connection is lost or unreachable."""
async def _clone_or_pull_repo_in_sandbox( # noqa: PLR0915 async def _clone_or_pull_repo_in_sandbox( # noqa: PLR0915
sandbox_backend: SandboxBackendProtocol, sandbox_backend: SandboxBackendProtocol,
owner: str, owner: str,
repo: str, repo: str,
github_token: str | None = None, gitea_token: str | None = None,
) -> str: ) -> str:
"""Clone a GitHub repo into the sandbox, or pull if it already exists. """Clone a Gitea repo into the sandbox, or pull if it already exists.
Args: Args:
sandbox_backend: The sandbox backend to execute commands in (LangSmithBackend) sandbox_backend: The sandbox backend to execute commands in
owner: GitHub repo owner owner: Gitea repo owner
repo: GitHub repo name repo: Gitea repo name
github_token: GitHub access token (from agent auth or env var) gitea_token: Gitea access token
Returns: Returns:
Path to the cloned/updated repo directory Path to the cloned/updated repo directory
@ -84,21 +86,33 @@ async def _clone_or_pull_repo_in_sandbox( # noqa: PLR0915
logger.info("_clone_or_pull_repo_in_sandbox called for %s/%s", owner, repo) logger.info("_clone_or_pull_repo_in_sandbox called for %s/%s", owner, repo)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
token = github_token token = gitea_token
if not token: if not token:
msg = "No GitHub token provided" msg = "No Gitea token provided"
logger.error(msg) logger.error(msg)
raise ValueError(msg) raise ValueError(msg)
work_dir = await aresolve_sandbox_work_dir(sandbox_backend) work_dir = await aresolve_sandbox_work_dir(sandbox_backend)
repo_dir = await aresolve_repo_dir(sandbox_backend, repo) repo_dir = await aresolve_repo_dir(sandbox_backend, repo)
clean_url = f"https://github.com/{owner}/{repo}.git" clean_url = f"http://gitea:3000/{owner}/{repo}.git"
cred_helper_arg = f"-c credential.helper='store --file={_CRED_FILE_PATH}'" cred_helper_arg = f"-c credential.helper='store --file={_CRED_FILE_PATH}'"
safe_repo_dir = shlex.quote(repo_dir) safe_repo_dir = shlex.quote(repo_dir)
safe_clean_url = shlex.quote(clean_url) safe_clean_url = shlex.quote(clean_url)
logger.info("Resolved sandbox work dir to %s", work_dir) logger.info("Resolved sandbox work dir to %s", work_dir)
# Set up git credentials using store file
await loop.run_in_executor(
None,
sandbox_backend.execute,
f'echo "http://agent:{token}@gitea:3000" > /tmp/.git-credentials',
)
await loop.run_in_executor(
None,
sandbox_backend.execute,
"git config --global credential.helper 'store --file=/tmp/.git-credentials'",
)
is_git_repo = await loop.run_in_executor(None, is_valid_git_repo, sandbox_backend, repo_dir) is_git_repo = await loop.run_in_executor(None, is_valid_git_repo, sandbox_backend, repo_dir)
if not is_git_repo: if not is_git_repo:
@ -125,7 +139,6 @@ async def _clone_or_pull_repo_in_sandbox( # noqa: PLR0915
logger.info("Repo is clean, pulling latest changes from %s/%s", owner, repo) logger.info("Repo is clean, pulling latest changes from %s/%s", owner, repo)
await loop.run_in_executor(None, setup_git_credentials, sandbox_backend, token)
try: try:
pull_result = await loop.run_in_executor( pull_result = await loop.run_in_executor(
None, None,
@ -149,7 +162,6 @@ async def _clone_or_pull_repo_in_sandbox( # noqa: PLR0915
return repo_dir return repo_dir
logger.info("Cloning repo %s/%s to %s", owner, repo, repo_dir) logger.info("Cloning repo %s/%s to %s", owner, repo, repo_dir)
await loop.run_in_executor(None, setup_git_credentials, sandbox_backend, token)
try: try:
result = await loop.run_in_executor( result = await loop.run_in_executor(
None, None,
@ -177,13 +189,9 @@ async def _recreate_sandbox(
repo_owner: str, repo_owner: str,
repo_name: str, repo_name: str,
*, *,
github_token: str | None, gitea_token: str | None,
) -> tuple[SandboxBackendProtocol, str]: ) -> tuple[SandboxBackendProtocol, str]:
"""Recreate a sandbox and clone the repo after a connection failure. """Recreate a sandbox and clone the repo after a connection failure."""
Clears the stale cache entry, sets the SANDBOX_CREATING sentinel,
creates a fresh sandbox, and clones the repo.
"""
SANDBOX_BACKENDS.pop(thread_id, None) SANDBOX_BACKENDS.pop(thread_id, None)
await client.threads.update( await client.threads.update(
thread_id=thread_id, thread_id=thread_id,
@ -192,7 +200,7 @@ async def _recreate_sandbox(
try: try:
sandbox_backend = await asyncio.to_thread(create_sandbox) sandbox_backend = await asyncio.to_thread(create_sandbox)
repo_dir = await _clone_or_pull_repo_in_sandbox( repo_dir = await _clone_or_pull_repo_in_sandbox(
sandbox_backend, repo_owner, repo_name, github_token sandbox_backend, repo_owner, repo_name, gitea_token
) )
except Exception: except Exception:
logger.exception("Failed to recreate sandbox after connection failure") logger.exception("Failed to recreate sandbox after connection failure")
@ -202,14 +210,7 @@ async def _recreate_sandbox(
async def _wait_for_sandbox_id(thread_id: str) -> str: async def _wait_for_sandbox_id(thread_id: str) -> str:
"""Wait for sandbox_id to be set in thread metadata. """Wait for sandbox_id to be set in thread metadata."""
Polls thread metadata until sandbox_id is set to a real value
(not the creating sentinel).
Raises:
TimeoutError: If sandbox creation takes too long
"""
elapsed = 0.0 elapsed = 0.0
while elapsed < SANDBOX_CREATION_TIMEOUT: while elapsed < SANDBOX_CREATION_TIMEOUT:
sandbox_id = await get_sandbox_id_from_metadata(thread_id) sandbox_id = await get_sandbox_id_from_metadata(thread_id)
@ -251,8 +252,8 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
tools=[], tools=[],
).with_config(config) ).with_config(config)
github_token, new_encrypted = await resolve_github_token(config, thread_id) gitea_token = await get_gitea_token()
config["metadata"]["github_token_encrypted"] = new_encrypted config["metadata"]["gitea_token"] = gitea_token
sandbox_backend = SANDBOX_BACKENDS.get(thread_id) sandbox_backend = SANDBOX_BACKENDS.get(thread_id)
sandbox_id = await get_sandbox_id_from_metadata(thread_id) sandbox_id = await get_sandbox_id_from_metadata(thread_id)
@ -270,15 +271,15 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
logger.info("Pulling latest changes for repo %s/%s", repo_owner, repo_name) logger.info("Pulling latest changes for repo %s/%s", repo_owner, repo_name)
try: try:
repo_dir = await _clone_or_pull_repo_in_sandbox( repo_dir = await _clone_or_pull_repo_in_sandbox(
sandbox_backend, repo_owner, repo_name, github_token sandbox_backend, repo_owner, repo_name, gitea_token
) )
except SandboxClientError: except SandboxConnectionError:
logger.warning( logger.warning(
"Cached sandbox is no longer reachable for thread %s, recreating sandbox", "Cached sandbox is no longer reachable for thread %s, recreating sandbox",
thread_id, thread_id,
) )
sandbox_backend, repo_dir = await _recreate_sandbox( sandbox_backend, repo_dir = await _recreate_sandbox(
thread_id, repo_owner, repo_name, github_token=github_token thread_id, repo_owner, repo_name, gitea_token=gitea_token
) )
except Exception: except Exception:
logger.exception("Failed to pull repo in cached sandbox") logger.exception("Failed to pull repo in cached sandbox")
@ -297,7 +298,7 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
if repo_owner and repo_name: if repo_owner and repo_name:
logger.info("Cloning repo %s/%s into sandbox", repo_owner, repo_name) logger.info("Cloning repo %s/%s into sandbox", repo_owner, repo_name)
repo_dir = await _clone_or_pull_repo_in_sandbox( repo_dir = await _clone_or_pull_repo_in_sandbox(
sandbox_backend, repo_owner, repo_name, github_token sandbox_backend, repo_owner, repo_name, gitea_token
) )
logger.info("Repo cloned to %s", repo_dir) logger.info("Repo cloned to %s", repo_dir)
@ -342,15 +343,15 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
logger.info("Pulling latest changes for repo %s/%s", repo_owner, repo_name) logger.info("Pulling latest changes for repo %s/%s", repo_owner, repo_name)
try: try:
repo_dir = await _clone_or_pull_repo_in_sandbox( repo_dir = await _clone_or_pull_repo_in_sandbox(
sandbox_backend, repo_owner, repo_name, github_token sandbox_backend, repo_owner, repo_name, gitea_token
) )
except SandboxClientError: except SandboxConnectionError:
logger.warning( logger.warning(
"Existing sandbox is no longer reachable for thread %s, recreating sandbox", "Existing sandbox is no longer reachable for thread %s, recreating sandbox",
thread_id, thread_id,
) )
sandbox_backend, repo_dir = await _recreate_sandbox( sandbox_backend, repo_dir = await _recreate_sandbox(
thread_id, repo_owner, repo_name, github_token=github_token thread_id, repo_owner, repo_name, gitea_token=gitea_token
) )
except Exception: except Exception:
logger.exception("Failed to pull repo in existing sandbox") logger.exception("Failed to pull repo in existing sandbox")
@ -362,9 +363,6 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
msg = "Cannot proceed: no repo was cloned. Set 'repo.owner' and 'repo.name' in the configurable config" msg = "Cannot proceed: no repo was cloned. Set 'repo.owner' and 'repo.name' in the configurable config"
raise RuntimeError(msg) raise RuntimeError(msg)
linear_issue = config["configurable"].get("linear_issue", {})
linear_project_id = linear_issue.get("linear_project_id", "")
linear_issue_number = linear_issue.get("linear_issue_number", "")
agents_md = await read_agents_md_in_sandbox(sandbox_backend, repo_dir) agents_md = await read_agents_md_in_sandbox(sandbox_backend, repo_dir)
logger.info("Returning agent with sandbox for thread %s", thread_id) logger.info("Returning agent with sandbox for thread %s", thread_id)
@ -372,17 +370,14 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
model=make_model("anthropic:claude-opus-4-6", temperature=0, max_tokens=20_000), model=make_model("anthropic:claude-opus-4-6", temperature=0, max_tokens=20_000),
system_prompt=construct_system_prompt( system_prompt=construct_system_prompt(
repo_dir, repo_dir,
linear_project_id=linear_project_id,
linear_issue_number=linear_issue_number,
agents_md=agents_md, agents_md=agents_md,
), ),
tools=[ tools=[
http_request, http_request,
fetch_url, fetch_url,
commit_and_open_pr, commit_and_open_pr,
linear_comment, gitea_comment,
slack_thread_reply, discord_reply,
github_comment,
], ],
backend=sandbox_backend, backend=sandbox_backend,
middleware=[ middleware=[