Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,16 @@ private async Task<PreprocessingActionData[]> ProgressPreprocessingActions(strin
foreach (var actionData in actionArrayData)
{
if (actionData is not JObject data) continue;
try

var actionType = data.Value<string>("action");
if (string.IsNullOrWhiteSpace(actionType))
{
var actionType = data.Value<string>("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),
Expand All @@ -104,6 +105,7 @@ private async Task<PreprocessingActionData[]> 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,
};

Expand All @@ -113,6 +115,10 @@ private async Task<PreprocessingActionData[]> 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}
"""));
}
}
}
Expand Down Expand Up @@ -407,6 +413,25 @@ await ChatClientProvider.ExplainImageAsync(
return new($"Get Pixiv Illust Info: {param.Id}", sb.ToString());
}

private async Task<PreprocessingActionData> 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<PreprocessingActionParam.ExplainImageActionParam>()
?? throw new InvalidDataException("Invalid JSON data for explain image action");
if (string.IsNullOrWhiteSpace(param.Url))
Copy link

Copilot AI Sep 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL validation only checks for null/empty/whitespace but doesn't validate if it's a properly formatted URL. Consider adding URL format validation using Uri.TryCreate() to catch malformed URLs early.

Suggested change
if (string.IsNullOrWhiteSpace(param.Url))
if (string.IsNullOrWhiteSpace(param.Url) ||
!Uri.TryCreate(param.Url, UriKind.Absolute, out var uriResult) ||
(uriResult.Scheme != Uri.UriSchemeHttp && uriResult.Scheme != Uri.UriSchemeHttps))

Copilot uses AI. Check for mistakes.
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}")
Copy link

Copilot AI Sep 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message 'Unable to explain image: {result}' may not be helpful if 'result' contains technical details intended for logging rather than user-facing error messages. Consider providing a more user-friendly error message or sanitizing the result content.

Suggested change
? throw new InvalidDataException($"Unable to explain image: {result}")
? throw new InvalidDataException("Unable to explain image at this time. Please try again later.")

Copilot uses AI. Check for mistakes.
: new($"Explain Image: {param.Url}", $"""
[Explain Image: {param.Url}]
```
{result}
```
""");
}

private record PreprocessingActionData(string Action, string Result);

private static class PreprocessingActionParam
Expand Down Expand Up @@ -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;
}
}
}
}
Loading