MCPcopy Create free account
hub / github.com/AsyncFuncAI/deepwiki-open / content_generator

Method content_generator

api/openrouter_client.py:169–320  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

167
168 # Create a generator that yields the content
169 async def content_generator():
170 if "choices" in data and len(data["choices"]) > 0:
171 choice = data["choices"][0]
172 if "message" in choice and "content" in choice["message"]:
173 content = choice["message"]["content"]
174 log.info("Successfully retrieved response")
175
176 # Check if the content is XML and ensure it's properly formatted
177 if content.strip().startswith("<") and ">" in content:
178 # It's likely XML, let's make sure it's properly formatted
179 try:
180 # Extract the XML content
181 xml_content = content
182
183 # Check if it's a wiki_structure XML
184 if "<wiki_structure>" in xml_content:
185 log.info("Found wiki_structure XML, ensuring proper format")
186
187 # Extract just the wiki_structure XML
188 import re
189 wiki_match = re.search(r'<wiki_structure>[\s\S]*?<\/wiki_structure>', xml_content)
190 if wiki_match:
191 # Get the raw XML
192 raw_xml = wiki_match.group(0)
193
194 # Clean the XML by removing any leading/trailing whitespace
195 # and ensuring it's properly formatted
196 clean_xml = raw_xml.strip()
197
198 # Try to fix common XML issues
199 try:
200 # Replace problematic characters in XML
201 fixed_xml = clean_xml
202
203 # Replace & with &amp; if not already part of an entity
204 fixed_xml = re.sub(r'&(?!amp;|lt;|gt;|apos;|quot;)', '&amp;', fixed_xml)
205
206 # Fix other common XML issues
207 fixed_xml = fixed_xml.replace('</', '</').replace(' >', '>')
208
209 # Try to parse the fixed XML
210 from xml.dom.minidom import parseString
211 dom = parseString(fixed_xml)
212
213 # Get the pretty-printed XML with proper indentation
214 pretty_xml = dom.toprettyxml()
215
216 # Remove XML declaration
217 if pretty_xml.startswith('<?xml'):
218 pretty_xml = pretty_xml[pretty_xml.find('?>')+2:].strip()
219
220 log.info(f"Extracted and validated XML: {pretty_xml[:100]}...")
221 yield pretty_xml
222 except Exception as xml_parse_error:
223 log.warning(f"XML validation failed: {str(xml_parse_error)}, using raw XML")
224
225 # If XML validation fails, try a more aggressive approach
226 try:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected