1 package io.github.magwas.coder;
2
3 import java.io.IOException;
4
5 import javax.xml.parsers.ParserConfigurationException;
6
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Service;
9 import org.xml.sax.SAXException;
10
11 import com.fasterxml.jackson.databind.ObjectMapper;
12
13 @Service
14 public class OpenRouterResponseService implements ErrorMessages, FormattingConstants {
15 @Autowired
16 private ObjectMapper objectMapper;
17
18 @Autowired
19 private XMLFileWriterService xmlFileWriterService;
20
21 @Autowired
22 private FileWriterService fileWriterService;
23
24 public String apply(String responseBody) throws IOException, ParserConfigurationException, SAXException {
25 OpenRouterResponseData response = objectMapper.readValue(responseBody, OpenRouterResponseData.class);
26 StringBuilder result = new StringBuilder();
27
28 if (response.choices() != null && response.choices().length > 0) {
29 MessageData message = response.choices()[0].message();
30 if (message != null) {
31 if (message.reasoning() != null) {
32 result.append(message.reasoning()).append(SECTION_DIVIDER);
33 }
34 if (message.content() != null) {
35 fileWriterService.apply("target/ai.xml", message.content());
36 xmlFileWriterService.apply(message.content());
37 }
38 }
39 }
40 return result.toString();
41 }
42 }