wony617 commited on
Commit
732763a
·
1 Parent(s): 769eab6

Fix toctree issue

Browse files
Files changed (1) hide show
  1. agent/toctree_handler.py +81 -67
agent/toctree_handler.py CHANGED
@@ -91,93 +91,107 @@ Korean title:"""
91
  'title': en_title
92
  }
93
 
94
- def create_updated_toctree_with_llm(self, en_toctree_yaml: str, ko_toctree_yaml: str, target_local: str) -> dict:
95
- """Use LLM to create updated Korean toctree with new entry at correct position"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  try:
97
- from translator.content import llm_translate
 
 
98
 
99
- prompt = f"""You are given English and Korean toctree YAML structures. You need to:
100
-
101
- 1. Find the entry(local, title) with `- local: {target_local}` in the English toctree
102
- 2. Translate its title to Korean
103
- 3. Insert this new entry into the Korean toctree at the same position as it appears in the English toctree
104
- 4. Return the complete updated Korean toctree
105
-
106
- English toctree YAML:
107
- ```yaml
108
- {en_toctree_yaml}
109
- ```
110
-
111
- Current Korean toctree YAML:
112
- ```yaml
113
- {ko_toctree_yaml}
114
- ```
115
-
116
- Target local path to add: "{target_local}"
117
-
118
- Return the complete updated Korean toctree in YAML format:
119
- ```yaml
120
- # Updated Korean toctree with new entry inserted at correct position
121
- [complete toctree structure here]
122
- ```
123
-
124
- Important positioning rules:
125
- - Find the exact position (index and nesting level) of the target entry in the English toctree
126
- - Count from the beginning: if it's the 5th item in English toctree, it should be the 5th item in Korean toctree
127
- - If it's inside a 'sections' array, maintain that nesting structure
128
- - Keep all existing Korean entries in their current positions
129
- - Insert the new Korean entry at the exact same position as the English entry
130
- - If there are gaps in positions (missing entries), maintain those gaps
131
- - Preserve the exact YAML structure: {{local: "path", title: "title"}} or {{local: "path", title: "title", sections: [...]}}
132
-
133
- Example: If English entry is at position [2] (3rd item), insert Korean entry at position [2] in Korean toctree
134
- Example: If English entry is at position [1]['sections'][0] (1st item in sections of 2nd entry), insert at same nested position"""
135
 
136
- callback_result, response = llm_translate(prompt)
137
 
138
- # Parse YAML response
139
- response = response.strip()
 
140
 
141
- try:
142
- # Extract YAML content between ```yaml and ```
143
- if "```yaml" in response:
144
- yaml_start = response.find("```yaml") + 7
145
- yaml_end = response.find("```", yaml_start)
146
- yaml_content = response[yaml_start:yaml_end].strip()
147
- else:
148
- yaml_content = response
149
-
150
- updated_ko_toctree = yaml.safe_load(yaml_content)
151
- return updated_ko_toctree
152
- except Exception as e:
153
- print(f"Failed to parse LLM YAML response: {e}")
154
- print(f"Response was: {response}")
155
- return None
156
 
157
  except Exception as e:
158
- print(f"Error using LLM to create updated toctree: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  return None
 
 
 
 
 
 
 
160
 
161
  def process_pr_commit(self, filepath: str):
162
- """Process PR commit by using LLM to create complete updated Korean toctree"""
163
  # Get filepath without prefix
164
  filepath_without_prefix = filepath.replace("docs/source/en/", "").replace(".md", "")
165
 
166
- # Get English and Korean toctrees as YAML strings
167
- en_toctree = self.get_en_toctree()
168
  ko_toctree = self.get_ko_toctree()
169
 
170
- en_toctree_yaml = yaml.dump(en_toctree, allow_unicode=True, default_flow_style=False)
171
- ko_toctree_yaml = yaml.dump(ko_toctree, allow_unicode=True, default_flow_style=False)
172
-
173
- # Use LLM to create updated Korean toctree
174
- updated_ko_toctree = self.create_updated_toctree_with_llm(en_toctree_yaml, ko_toctree_yaml, filepath_without_prefix)
175
 
176
  if not updated_ko_toctree:
177
  print(f"Failed to create updated Korean toctree for local: {filepath_without_prefix}")
178
  return
179
 
180
- print(f"LLM successfully updated Korean toctree")
181
 
182
  # Store the updated toctree for commit
183
  self.updated_ko_toctree = updated_ko_toctree
 
91
  'title': en_title
92
  }
93
 
94
+ def find_and_update_translation_entry(self, ko_toctree_data, target_local: str, english_title: str, korean_title: str):
95
+ """Find entry with '(번역중) 영어제목' and update it"""
96
+ target_title_pattern = f"(번역중) {english_title}"
97
+
98
+ def process_item(item):
99
+ if isinstance(item, dict):
100
+ # Check if title matches the pattern
101
+ if item.get('title') == target_title_pattern:
102
+ # Update local path and title
103
+ item['local'] = target_local
104
+ item['title'] = korean_title
105
+ return True
106
+
107
+ # Process sections recursively
108
+ if 'sections' in item:
109
+ for section in item['sections']:
110
+ if process_item(section):
111
+ return True
112
+ return False
113
+
114
+ # Process the toctree data
115
+ if isinstance(ko_toctree_data, list):
116
+ for item in ko_toctree_data:
117
+ if process_item(item):
118
+ return True
119
+ return False
120
+
121
+ def create_updated_toctree_with_replacement(self, ko_toctree: list, target_local: str) -> list:
122
+ """Update Korean toctree by finding and updating translation entry"""
123
  try:
124
+ # Step 1: Get English toctree and find the English title for target_local
125
+ en_toctree = self.get_en_toctree()
126
+ english_title = self.find_title_for_local(en_toctree, target_local)
127
 
128
+ if not english_title:
129
+ print(f"Could not find English title for local: {target_local}")
130
+ return ko_toctree
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
+ print(f"Found English title: {english_title} for local: {target_local}")
133
 
134
+ # Step 2: Translate the English title to Korean
135
+ korean_title = self.translate_title(english_title)
136
+ print(f"Translated Korean title: {korean_title}")
137
 
138
+ # Step 3: Make a deep copy to avoid modifying original
139
+ import copy
140
+ updated_toctree = copy.deepcopy(ko_toctree)
141
+
142
+ # Step 4: Find and update the "(번역중) 영어제목" entry
143
+ updated = self.find_and_update_translation_entry(
144
+ updated_toctree, target_local, english_title, korean_title
145
+ )
146
+
147
+ if updated:
148
+ print(f"Successfully updated translation entry: local={target_local}, title={korean_title}")
149
+ return updated_toctree
150
+ else:
151
+ print(f"Could not find '(번역중) {english_title}' entry to update")
152
+ return ko_toctree
153
 
154
  except Exception as e:
155
+ print(f"Error creating updated toctree: {e}")
156
+ return ko_toctree
157
+
158
+ def find_title_for_local(self, toctree_data, target_local: str):
159
+ """Find title for given local path in toctree"""
160
+ def search_item(item):
161
+ if isinstance(item, dict):
162
+ if item.get('local') == target_local:
163
+ return item.get('title', '')
164
+
165
+ if 'sections' in item:
166
+ for section in item['sections']:
167
+ result = search_item(section)
168
+ if result:
169
+ return result
170
  return None
171
+
172
+ if isinstance(toctree_data, list):
173
+ for item in toctree_data:
174
+ result = search_item(item)
175
+ if result:
176
+ return result
177
+ return None
178
 
179
  def process_pr_commit(self, filepath: str):
180
+ """Process PR commit by updating Korean toctree with translated entry"""
181
  # Get filepath without prefix
182
  filepath_without_prefix = filepath.replace("docs/source/en/", "").replace(".md", "")
183
 
184
+ # Get Korean toctree
 
185
  ko_toctree = self.get_ko_toctree()
186
 
187
+ # Update Korean toctree with replacement logic
188
+ updated_ko_toctree = self.create_updated_toctree_with_replacement(ko_toctree, filepath_without_prefix)
 
 
 
189
 
190
  if not updated_ko_toctree:
191
  print(f"Failed to create updated Korean toctree for local: {filepath_without_prefix}")
192
  return
193
 
194
+ print(f"Successfully updated Korean toctree")
195
 
196
  # Store the updated toctree for commit
197
  self.updated_ko_toctree = updated_ko_toctree