wony617
commited on
Commit
Β·
b02590a
1
Parent(s):
b7c7d1c
refactor(log): migrate logging to GitHubLogger and cleanup
Browse files- Add HF Space and logging secrets to example.env
- Improve UX message when existing translation is detected
- Remove leftover comments/duplicate imports; ensure pr_success.log is pure JSONL (remove header)
- agent/handler.py +0 -6
- agent/workflow.py +5 -3
- example.env +12 -1
- pr_generator/agent.py +0 -99
- pr_success.log +0 -2
agent/handler.py
CHANGED
@@ -150,12 +150,6 @@ def start_translation_process():
|
|
150 |
""
|
151 |
f"{original_file_link}\n"
|
152 |
"**π Translated Content:**\n"
|
153 |
-
# f"\n```\n\n{_extract_content_for_display(translated)}\n```"
|
154 |
-
# "\n```\n\n"
|
155 |
-
# f"\n{translated}\n"
|
156 |
-
# f"```"
|
157 |
-
# f"{status}\n"
|
158 |
-
# "β
Translation completed. The code block will be added when generating PR."
|
159 |
)
|
160 |
return response, translated
|
161 |
|
|
|
150 |
""
|
151 |
f"{original_file_link}\n"
|
152 |
"**π Translated Content:**\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
)
|
154 |
return response, translated
|
155 |
|
agent/workflow.py
CHANGED
@@ -14,6 +14,9 @@ from translator.content import (
|
|
14 |
from translator.retriever import report, get_github_issue_open_pr
|
15 |
from pr_generator.agent import GitHubPRAgent
|
16 |
|
|
|
|
|
|
|
17 |
|
18 |
def report_translation_target_files(
|
19 |
translate_lang: str, top_k: int = 1
|
@@ -61,7 +64,7 @@ def translate_docs(lang: str, file_path: str, additional_instruction: str = "")
|
|
61 |
with open(translation_file_path, "r", encoding="utf-8") as f:
|
62 |
existing_content = f.read()
|
63 |
if existing_content.strip():
|
64 |
-
return "Existing translation loaded (no tokens used)", existing_content
|
65 |
|
66 |
# step 1. Get content from file path
|
67 |
content = get_content(file_path)
|
@@ -105,6 +108,7 @@ def translate_docs_interactive(
|
|
105 |
callback_result, translated_content = translate_docs(translate_lang, current_file, additional_instruction)
|
106 |
status += f"π° Used token and cost: \n```\n{callback_result}\n```"
|
107 |
|
|
|
108 |
print(status)
|
109 |
|
110 |
return translated_content
|
@@ -206,8 +210,6 @@ def generate_github_pr(
|
|
206 |
|
207 |
# Append full result JSON to dedicated GitHub logging repository (always)
|
208 |
try:
|
209 |
-
import json
|
210 |
-
from logger.github_logger import GitHubLogger
|
211 |
log_entry = json.dumps(result, ensure_ascii=False) + "\n"
|
212 |
log_res = GitHubLogger().append_jsonl(log_entry)
|
213 |
print(f"π Log append result: {log_res}")
|
|
|
14 |
from translator.retriever import report, get_github_issue_open_pr
|
15 |
from pr_generator.agent import GitHubPRAgent
|
16 |
|
17 |
+
import json
|
18 |
+
from logger.github_logger import GitHubLogger
|
19 |
+
|
20 |
|
21 |
def report_translation_target_files(
|
22 |
translate_lang: str, top_k: int = 1
|
|
|
64 |
with open(translation_file_path, "r", encoding="utf-8") as f:
|
65 |
existing_content = f.read()
|
66 |
if existing_content.strip():
|
67 |
+
return "Existing translation loaded (no tokens used). If you want to translate again, please restart the gradio app.", existing_content
|
68 |
|
69 |
# step 1. Get content from file path
|
70 |
content = get_content(file_path)
|
|
|
108 |
callback_result, translated_content = translate_docs(translate_lang, current_file, additional_instruction)
|
109 |
status += f"π° Used token and cost: \n```\n{callback_result}\n```"
|
110 |
|
111 |
+
print(callback_result)
|
112 |
print(status)
|
113 |
|
114 |
return translated_content
|
|
|
210 |
|
211 |
# Append full result JSON to dedicated GitHub logging repository (always)
|
212 |
try:
|
|
|
|
|
213 |
log_entry = json.dumps(result, ensure_ascii=False) + "\n"
|
214 |
log_res = GitHubLogger().append_jsonl(log_entry)
|
215 |
print(f"π Log append result: {log_res}")
|
example.env
CHANGED
@@ -4,4 +4,15 @@ ANTHROPIC_API_KEY=<your api key>
|
|
4 |
GITHUB_TOKEN=<your github token>
|
5 |
GITHUB_OWNER=<your github username>
|
6 |
GITHUB_REPO=<your repository name>
|
7 |
-
REFERENCE_PR_URL=<reference pr url for style analysis>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
GITHUB_TOKEN=<your github token>
|
5 |
GITHUB_OWNER=<your github username>
|
6 |
GITHUB_REPO=<your repository name>
|
7 |
+
REFERENCE_PR_URL=<reference pr url for style analysis>
|
8 |
+
|
9 |
+
# Secrets for deployment to HF space
|
10 |
+
HF_TOKEN=
|
11 |
+
HF_USERNAME=
|
12 |
+
HF_SPACE_NAME=
|
13 |
+
|
14 |
+
# Secrets for logging to Github
|
15 |
+
LOG_REPO=
|
16 |
+
LOG_GITHUB_TOKEN=
|
17 |
+
LOG_BRANCH=
|
18 |
+
LOG_FILE_PATH=
|
pr_generator/agent.py
CHANGED
@@ -37,7 +37,6 @@ class GitHubPRAgent:
|
|
37 |
def __init__(self):
|
38 |
self._github_client = None
|
39 |
self._llm = None
|
40 |
-
self._log_github_client = None
|
41 |
|
42 |
@property
|
43 |
def github_client(self) -> Optional[Github]:
|
@@ -67,104 +66,6 @@ class GitHubPRAgent:
|
|
67 |
)
|
68 |
return self._llm
|
69 |
|
70 |
-
@property
|
71 |
-
def logging_github_client(self) -> Optional[Github]:
|
72 |
-
"""Return GitHub API client for logging with optional separate token.
|
73 |
-
|
74 |
-
Uses LOG_GITHUB_TOKEN if set; otherwise falls back to GITHUB_TOKEN.
|
75 |
-
"""
|
76 |
-
if not REQUIRED_LIBS_AVAILABLE:
|
77 |
-
raise ImportError("Required libraries not found.")
|
78 |
-
|
79 |
-
if self._log_github_client is None:
|
80 |
-
token = os.environ.get("LOG_GITHUB_TOKEN") or os.environ.get("GITHUB_TOKEN")
|
81 |
-
if not token:
|
82 |
-
print("Warning: LOG_GITHUB_TOKEN/GITHUB_TOKEN not set for logging.")
|
83 |
-
return Github() # Limited access
|
84 |
-
self._log_github_client = Github(token)
|
85 |
-
|
86 |
-
return self._log_github_client
|
87 |
-
|
88 |
-
def append_to_log_file(
|
89 |
-
self,
|
90 |
-
log_entry: str,
|
91 |
-
commit_message: str = "chore(log): append PR result entry",
|
92 |
-
) -> str:
|
93 |
-
"""Append a log entry to a file on a specific branch using GitHub API.
|
94 |
-
|
95 |
-
Target repository/branch/path are read from environment variables:
|
96 |
-
- LOG_REPO or LOG_REPO_OWNER + LOG_REPO_NAME
|
97 |
-
- LOG_BRANCH (default: 'log_event')
|
98 |
-
- LOG_FILE_PATH (default: 'pr_success.log')
|
99 |
-
"""
|
100 |
-
try:
|
101 |
-
# Resolve target repo and path from environment/static settings
|
102 |
-
repo_spec = os.environ.get("LOG_REPO")
|
103 |
-
owner = None
|
104 |
-
repo_name = None
|
105 |
-
if repo_spec and "/" in repo_spec:
|
106 |
-
owner, repo_name = repo_spec.split("/", 1)
|
107 |
-
else:
|
108 |
-
owner = os.environ.get("LOG_REPO_OWNER")
|
109 |
-
repo_name = os.environ.get("LOG_REPO_NAME")
|
110 |
-
|
111 |
-
if not owner or not repo_name:
|
112 |
-
return (
|
113 |
-
"log file append failed: 400 Missing LOG_REPO or LOG_REPO_OWNER/LOG_REPO_NAME"
|
114 |
-
)
|
115 |
-
|
116 |
-
branch_name = os.environ.get("LOG_BRANCH", "log_event")
|
117 |
-
path = os.environ.get("LOG_FILE_PATH", "pr_success.log")
|
118 |
-
|
119 |
-
repo = self.logging_github_client.get_repo(f"{owner}/{repo_name}")
|
120 |
-
|
121 |
-
# Ensure branch exists; if not, create from default branch
|
122 |
-
try:
|
123 |
-
repo.get_branch(branch_name)
|
124 |
-
except GithubException as e:
|
125 |
-
if e.status == 404:
|
126 |
-
try:
|
127 |
-
base_branch = repo.default_branch
|
128 |
-
base = repo.get_branch(base_branch)
|
129 |
-
repo.create_git_ref(
|
130 |
-
ref=f"refs/heads/{branch_name}", sha=base.commit.sha
|
131 |
-
)
|
132 |
-
except Exception as ce:
|
133 |
-
return self._handle_github_error(ce, "branch ensure for logging")
|
134 |
-
else:
|
135 |
-
return self._handle_github_error(e, "branch check for logging")
|
136 |
-
|
137 |
-
# Try to get existing file content on the branch and append
|
138 |
-
try:
|
139 |
-
existing_file = repo.get_contents(path, ref=branch_name)
|
140 |
-
import base64
|
141 |
-
|
142 |
-
existing_content = base64.b64decode(existing_file.content).decode(
|
143 |
-
"utf-8"
|
144 |
-
)
|
145 |
-
new_content = existing_content + log_entry
|
146 |
-
repo.update_file(
|
147 |
-
path=path,
|
148 |
-
message=commit_message,
|
149 |
-
content=new_content,
|
150 |
-
sha=existing_file.sha,
|
151 |
-
branch=branch_name,
|
152 |
-
)
|
153 |
-
return "SUCCESS: Log appended"
|
154 |
-
except GithubException as e:
|
155 |
-
if e.status == 404:
|
156 |
-
# File does not exist; create with first entry (pure JSONL line)
|
157 |
-
repo.create_file(
|
158 |
-
path=path,
|
159 |
-
message=commit_message,
|
160 |
-
content=log_entry,
|
161 |
-
branch=branch_name,
|
162 |
-
)
|
163 |
-
return "SUCCESS: Log file created and first entry appended"
|
164 |
-
return self._handle_github_error(e, "log file append")
|
165 |
-
except Exception as e:
|
166 |
-
return self._handle_github_error(e, "log file append")
|
167 |
-
|
168 |
def _handle_github_error(self, e: Exception, operation: str) -> str:
|
169 |
"""Handle GitHub API errors consistently."""
|
170 |
if isinstance(e, GithubException):
|
|
|
37 |
def __init__(self):
|
38 |
self._github_client = None
|
39 |
self._llm = None
|
|
|
40 |
|
41 |
@property
|
42 |
def github_client(self) -> Optional[Github]:
|
|
|
66 |
)
|
67 |
return self._llm
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
def _handle_github_error(self, e: Exception, operation: str) -> str:
|
70 |
"""Handle GitHub API errors consistently."""
|
71 |
if isinstance(e, GithubException):
|
pr_success.log
CHANGED
@@ -1,2 +0,0 @@
|
|
1 |
-
# PR Success Log
|
2 |
-
# Format: [timestamp] file_path -> pr_url
|
|
|
|
|
|