From 37d185fa70cc36f40d6c1cb87c9995a84c5164a6 Mon Sep 17 00:00:00 2001 From: OLC Date: Mon, 22 Sep 2025 01:33:55 +0800 Subject: [PATCH] add explain image preprocessing action --- .../AI/AiInteractions.Preprocessing.cs | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/RitsukageBotForDiscord/Modules/AI/AiInteractions.Preprocessing.cs b/src/RitsukageBotForDiscord/Modules/AI/AiInteractions.Preprocessing.cs index b1bb398..8e2f436 100644 --- a/src/RitsukageBotForDiscord/Modules/AI/AiInteractions.Preprocessing.cs +++ b/src/RitsukageBotForDiscord/Modules/AI/AiInteractions.Preprocessing.cs @@ -82,15 +82,16 @@ private async Task ProgressPreprocessingActions(strin foreach (var actionData in actionArrayData) { if (actionData is not JObject data) continue; - try + + var actionType = data.Value("action"); + if (string.IsNullOrWhiteSpace(actionType)) { - var actionType = data.Value("action"); - if (string.IsNullOrWhiteSpace(actionType)) - { - Logger.LogWarning("Unable to parse the JSON: {Json}", data.ToString()); - continue; - } + Logger.LogWarning("Unable to parse the JSON: {Json}", data.ToString()); + continue; + } + try + { var resultData = actionType switch { "web_search" => await PreprocessingWebSearch(data).ConfigureAwait(false), @@ -104,6 +105,7 @@ private async Task ProgressPreprocessingActions(strin .ConfigureAwait(false), "pixiv_user_info" => await PreprocessingPixivQueryUserInfo(data).ConfigureAwait(false), "pixiv_illust_info" => await PreprocessingPixivQueryIllustInfo(data).ConfigureAwait(false), + "explain_image" => await PreprocessingExplainImage(data).ConfigureAwait(false), _ => null, }; @@ -113,6 +115,10 @@ private async Task ProgressPreprocessingActions(strin catch (Exception ex) { Logger.LogError(ex, "Error while processing the JSON action: {Json}", data.ToString()); + result.Add(new(actionType, $""" + [Failed Action: {actionType}] + {ex.Message} + """)); } } } @@ -407,6 +413,25 @@ await ChatClientProvider.ExplainImageAsync( return new($"Get Pixiv Illust Info: {param.Id}", sb.ToString()); } + private async Task PreprocessingExplainImage(JObject? data) + { + if (data is null || !data.TryGetValue("param", out var paramValue) || paramValue is not JObject paramToken) + throw new InvalidDataException("Invalid JSON data for explain image action"); + var param = paramToken.ToObject() + ?? throw new InvalidDataException("Invalid JSON data for explain image action"); + if (string.IsNullOrWhiteSpace(param.Url)) + throw new InvalidDataException("Invalid URL for explain image action"); + var (success, result) = await ChatClientProvider.ExplainImageAsync(param.Url); + return !success + ? throw new InvalidDataException($"Unable to explain image: {result}") + : new($"Explain Image: {param.Url}", $""" + [Explain Image: {param.Url}] + ``` + {result} + ``` + """); + } + private record PreprocessingActionData(string Action, string Result); private static class PreprocessingActionParam @@ -481,6 +506,11 @@ internal class PixivUserInfoActionParam { [JsonProperty("id")] public string Id { get; set; } = string.Empty; } + + internal class ExplainImageActionParam + { + [JsonProperty("url")] public string Url { get; set; } = string.Empty; + } } } } \ No newline at end of file