From 3154860665a3a956bcfe592b468451744050cbe4 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sat, 7 Jun 2025 18:24:24 +0300 Subject: [PATCH] Preserve AI computation parameters in the AI result. Also record spent time. --- .../MailCorrespondentCommand.java | 79 ++++++++----------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/src/main/java/eu/svjatoslav/alyverkko_cli/commands/mail_correspondant/MailCorrespondentCommand.java b/src/main/java/eu/svjatoslav/alyverkko_cli/commands/mail_correspondant/MailCorrespondentCommand.java index 469f626..dfac423 100644 --- a/src/main/java/eu/svjatoslav/alyverkko_cli/commands/mail_correspondant/MailCorrespondentCommand.java +++ b/src/main/java/eu/svjatoslav/alyverkko_cli/commands/mail_correspondant/MailCorrespondentCommand.java @@ -207,21 +207,53 @@ public class MailCorrespondentCommand implements Command { // Read the mail content String inputFileContent = getFileContentsAsString(file); - // Parse the relevant data into a MailQuery object - MailQuery mailQuery = parseInputFileContent(inputFileContent); + // 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 settings = parseSettings(firstLine); + + // Create MailQuery + MailQuery mailQuery = new MailQuery(); + mailQuery.systemPrompt = configuration.getPromptByAlias(settings.getOrDefault("prompt", "default")); + String modelAlias = settings.getOrDefault("model", "default"); + Optional modelOptional = modelLibrary.findModelByAlias(modelAlias); + if (!modelOptional.isPresent()) { + throw new IllegalArgumentException("Model with alias '" + modelAlias + "' not found."); + } + mailQuery.model = modelOptional.get(); + mailQuery.userPrompt = userPromptOriginal; + + // Record start time + long startTime = System.currentTimeMillis(); // Create an AiTask and run the query 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 + // 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"); // Ensure the user prompt block is labeled if it isn't already if (!mailQuery.userPrompt.startsWith("* USER:\n")) { resultFileContent.append("* USER:\n"); } - resultFileContent.append(mailQuery.userPrompt).append("\n"); + // Append the original user prompt (after the first line) + resultFileContent.append(userPromptOriginal).append("\n"); // Append the AI response block resultFileContent @@ -233,47 +265,6 @@ public class MailCorrespondentCommand implements Command { saveToFile(file, resultFileContent.toString()); } - /** - * Converts the raw file content (including the line beginning with "TOCOMPUTE:") - * into a {@link MailQuery} object that the AI can process. - * - * @param inputFileContent the raw contents of the mail file. - * @return a {@link MailQuery} containing the system prompt, user prompt, and the selected model. - * @throws IOException if reading prompt files fails. - */ - private MailQuery parseInputFileContent(String inputFileContent) throws IOException { - MailQuery mailQuery = new MailQuery(); - - // Find the newline that separates "TOCOMPUTE: ..." from the rest - int firstNewLineIndex = inputFileContent.indexOf('\n'); - if (firstNewLineIndex == -1) { - throw new IllegalArgumentException("Input file is only one line long. Content: " + inputFileContent); - } else { - // The user prompt is everything after the first line - mailQuery.userPrompt = inputFileContent.substring(firstNewLineIndex + 1); - } - - // The first line will look like "TOCOMPUTE: model=... prompt=... etc." - String firstLine = inputFileContent.substring(0, firstNewLineIndex); - - // Parse out the key/value pairs - Map settings = parseSettings(firstLine); - - // Look up system prompt from the "prompt" alias - String promptAlias = settings.getOrDefault("prompt", "default"); - mailQuery.systemPrompt = configuration.getPromptByAlias(promptAlias); - - // Resolve model from the "model" alias - String modelAlias = settings.getOrDefault("model", "default"); - Optional modelOptional = modelLibrary.findModelByAlias(modelAlias); - if (!modelOptional.isPresent()) { - throw new IllegalArgumentException("Model with alias '" + modelAlias + "' not found."); - } - mailQuery.model = modelOptional.get(); - - return mailQuery; - } - /** * Parses the "TOCOMPUTE:" line, which should look like: *
TOCOMPUTE: key1=value1 key2=value2 ...
-- 2.20.1