package eu.svjatoslav.alyverkko_cli.commands.mail_correspondant;
import eu.svjatoslav.alyverkko_cli.*;
-import eu.svjatoslav.alyverkko_cli.configuration.ConfigurationHelper;
import eu.svjatoslav.alyverkko_cli.model.Model;
import eu.svjatoslav.alyverkko_cli.model.ModelLibrary;
import eu.svjatoslav.commons.cli_helper.parameter_parser.Parser;
System.out.println("\nReplying to mail: " + file.getName());
- // Read the mail content
- String inputFileContent = getFileContentsAsString(file);
-
- // Split into first line and user prompt
- int firstNewLineIndex = inputFileContent.indexOf('\n');
- if (firstNewLineIndex == -1) {
- throw new IllegalArgumentException("Input file is only one line long. Content: " + inputFileContent);
- }
- String firstLine = inputFileContent.substring(0, firstNewLineIndex);
- String userPromptOriginal = inputFileContent.substring(firstNewLineIndex + 1);
-
- // Parse the first line into settings
- Map<String, String> settings = parseSettings(firstLine);
-
- // Create MailQuery
- MailQuery mailQuery = new MailQuery();
- mailQuery.systemPrompt = configuration.getPromptByAlias(settings.getOrDefault("prompt", "default"));
- String modelAlias = settings.getOrDefault("model", "default");
- Optional<Model> modelOptional = modelLibrary.findModelByAlias(modelAlias);
- if (!modelOptional.isPresent()) {
- throw new IllegalArgumentException("Model with alias '" + modelAlias + "' not found.");
- }
- mailQuery.model = modelOptional.get();
- mailQuery.userPrompt = userPromptOriginal;
+ MailQuery mailQuery = buildMailQueryFromFile(file);
- // Record start time
- long startTime = System.currentTimeMillis();
-
- // Create an AiTask and run the query
+ // Run the query using the AI model while measuring the time taken
AiTask aiTask = new AiTask(mailQuery);
String aiGeneratedResponse = aiTask.runAiQuery();
- // Record end time and calculate duration
- long endTime = System.currentTimeMillis();
- long durationMinutes = Math.round((endTime - startTime) / 60000.0); // Convert to minutes and round
+ saveAiResponseToFile(file, mailQuery, aiGeneratedResponse);
+ }
+ private static void saveAiResponseToFile(File file, MailQuery mailQuery, String aiGeneratedResponse) throws IOException {
// Build new content
StringBuilder resultFileContent = new StringBuilder();
- // Replace the first line with DONE line
- resultFileContent.append("DONE: prompt=").append(settings.get("prompt"))
- .append(" model=").append(modelAlias)
- .append(" time=").append(durationMinutes)
- .append("\n");
+ // The First line should be "TOCOMPUTE:" with settings that were used to process this file
+ resultFileContent.append(getDoneLine(mailQuery));
+
// Ensure the user prompt block is labeled if it isn't already
if (!mailQuery.userPrompt.startsWith("* USER:\n")) {
resultFileContent.append("* USER:\n");
}
+
// Append the original user prompt (after the first line)
- resultFileContent.append(userPromptOriginal).append("\n");
+ resultFileContent.append(mailQuery.userPrompt).append("\n");
// Append the AI response block
resultFileContent
saveToFile(file, resultFileContent.toString());
}
+ private static String getDoneLine(MailQuery mailQuery) {
+ return "DONE: prompt=" + mailQuery.systemPromptName + " model="+ mailQuery.model.alias + " duration=" + getDuration(mailQuery.startTime, mailQuery.endTime) + "\n";
+ }
+
+ private static String getDuration(long startTime, long endTime) {
+
+ long durationMillis = endTime - startTime;
+ long durationSeconds = durationMillis / 1000;
+
+ if (durationSeconds < 180){
+ return String.valueOf(durationSeconds) + "s";
+ }
+
+ long durationMinutes = durationSeconds / 60;
+ if (durationMinutes < 180) {
+ return durationMinutes + "m";
+ }
+
+ long durationHours = durationMinutes / 60;
+ return durationHours + "h";
+ }
+
+ private MailQuery buildMailQueryFromFile(File file) throws IOException {
+ MailQuery result = new MailQuery();
+
+ // Read the mail content
+ String inputFileContent = getFileContentsAsString(file);
+
+ // Split into first line and user prompt
+ int firstNewLineIndex = inputFileContent.indexOf('\n');
+ if (firstNewLineIndex == -1) {
+ throw new IllegalArgumentException("Input file is only one line long. Content: " + inputFileContent);
+ }
+
+ // The First line should start with "TOCOMPUTE:" and contain settings
+ String firstLine = inputFileContent.substring(0, firstNewLineIndex);
+ Map<String, String> fileProcessingSettings = parseSettings(firstLine);
+
+ // The rest of the file is the user prompt
+ result.userPrompt = inputFileContent.substring(firstNewLineIndex + 1);
+
+ // Set system prompt
+ result.systemPromptName = fileProcessingSettings.getOrDefault("prompt", "default");
+ result.systemPrompt = configuration.getPromptByName(result.systemPromptName);
+
+ // Set AI model
+ String modelAlias = fileProcessingSettings.getOrDefault("model", "default");
+ Optional<Model> modelOptional = modelLibrary.findModelByAlias(modelAlias);
+ if (!modelOptional.isPresent()) {
+ throw new IllegalArgumentException("Model with alias '" + modelAlias + "' not found.");
+ }
+ result.model = modelOptional.get();
+
+ return result;
+ }
+
/**
* Parses the "TOCOMPUTE:" line, which should look like:
* <pre>TOCOMPUTE: key1=value1 key2=value2 ...</pre>