View Javadoc
1   package io.github.magwas.coder;
2   
3   import org.jline.reader.LineReader;
4   import org.springframework.beans.factory.annotation.Autowired;
5   import org.springframework.stereotype.Service;
6   
7   import io.github.magwas.coder.config.ConfigLoadService;
8   import io.github.magwas.coder.conversation.ConversationClearService;
9   import io.github.magwas.coder.conversation.ConversationHasSystemInstructionsService;
10  import io.github.magwas.coder.conversation.ConversationSetupService;
11  import io.github.magwas.coder.conversation.ConversationSizeService;
12  
13  @Service
14  public class MainLoopService implements UIConstants {
15  	@Autowired
16  	private OpenRouterClientService openRouterClientService;
17  
18  	@Autowired
19  	private ConsoleInputService consoleInputService;
20  
21  	@Autowired
22  	private ConversationClearService conversationClearService;
23  
24  	@Autowired
25  	private ConversationSizeService conversationSizeService;
26  
27  	@Autowired
28  	private ConversationHasSystemInstructionsService conversationHasSystemInstructionsService;
29  
30  	@Autowired
31  	private ConversationSetupService conversationSetupService;
32  
33  	@Autowired
34  	private LineReaderDependency lineReaderDependency;
35  
36  	@Autowired
37  	private SystemDependency systemDependency;
38  
39  	@Autowired
40  	private ConfigLoadService configLoadService;
41  
42  	@Autowired
43  	private PersonalityService personalityService;
44  
45  	@Autowired
46  	private QuestionHandlerService questionHandlerService;
47  
48  	public Void apply() throws Exception {
49  		configLoadService.apply();
50  		PersonalityData personality = personalityService.apply("coder");
51  
52  		LineReader lineReader = lineReaderDependency.lineReader;
53  		systemDependency.println.accept(PROMPT_MESSAGE);
54  		conversationSetupService.apply(personality.name());
55  
56  		while (true) {
57  			String userInput = consoleInputService.apply(lineReader);
58  			if (userInput == null) break;
59  			if (userInput.isEmpty()) continue;
60  			systemDependency.println.accept(GOT_INPUT);
61  			switch (userInput.toLowerCase()) {
62  				case "/clear" -> handleClear();
63  				case "/history" -> handleHistory();
64  				case "/instructions" -> handleInstructions();
65  				default -> questionHandlerService.apply(personality.name(), userInput);
66  			}
67  		}
68  		systemDependency.println.accept(GOODBYE_MESSAGE);
69  		systemDependency.exit.accept(0);
70  		return null;
71  	}
72  
73  	private void handleClear() {
74  		conversationClearService.apply();
75  		systemDependency.println.accept(CLEAR_CONFIRMATION);
76  	}
77  
78  	private void handleHistory() {
79  		systemDependency.println.accept(HISTORY_MESSAGE + conversationSizeService.apply());
80  	}
81  
82  	private void handleInstructions() {
83  		systemDependency.println.accept(INSTRUCTIONS_STATUS
84  				+ (conversationHasSystemInstructionsService.apply() ? INSTRUCTIONS_LOADED : INSTRUCTIONS_MISSING));
85  	}
86  }