Files

123 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace AxCopilot.Services.Agent;
public class Base64Tool : IAgentTool
{
public string Name => "base64_tool";
public string Description => "Encode or decode Base64 and URL strings. Actions: 'b64encode' — encode text to Base64; 'b64decode' — decode Base64 to text; 'urlencode' — URL-encode text; 'urldecode' — URL-decode text; 'b64file' — encode a file to Base64 (max 5MB).";
public ToolParameterSchema Parameters
{
get
{
ToolParameterSchema toolParameterSchema = new ToolParameterSchema();
Dictionary<string, ToolProperty> dictionary = new Dictionary<string, ToolProperty>();
ToolProperty obj = new ToolProperty
{
Type = "string",
Description = "Action to perform"
};
int num = 5;
List<string> list = new List<string>(num);
CollectionsMarshal.SetCount(list, num);
Span<string> span = CollectionsMarshal.AsSpan(list);
span[0] = "b64encode";
span[1] = "b64decode";
span[2] = "urlencode";
span[3] = "urldecode";
span[4] = "b64file";
obj.Enum = list;
dictionary["action"] = obj;
dictionary["text"] = new ToolProperty
{
Type = "string",
Description = "Text to encode/decode"
};
dictionary["file_path"] = new ToolProperty
{
Type = "string",
Description = "File path for b64file action"
};
toolParameterSchema.Properties = dictionary;
num = 1;
List<string> list2 = new List<string>(num);
CollectionsMarshal.SetCount(list2, num);
CollectionsMarshal.AsSpan(list2)[0] = "action";
toolParameterSchema.Required = list2;
return toolParameterSchema;
}
}
public Task<ToolResult> ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct = default(CancellationToken))
{
string text = args.GetProperty("action").GetString() ?? "";
JsonElement value;
string text2 = (args.TryGetProperty("text", out value) ? (value.GetString() ?? "") : "");
try
{
if (1 == 0)
{
}
ToolResult result = text switch
{
"b64encode" => ToolResult.Ok(Convert.ToBase64String(Encoding.UTF8.GetBytes(text2))),
"b64decode" => ToolResult.Ok(Encoding.UTF8.GetString(Convert.FromBase64String(text2))),
"urlencode" => ToolResult.Ok(Uri.EscapeDataString(text2)),
"urldecode" => ToolResult.Ok(Uri.UnescapeDataString(text2)),
"b64file" => EncodeFile(args, context),
_ => ToolResult.Fail("Unknown action: " + text),
};
if (1 == 0)
{
}
return Task.FromResult(result);
}
catch (FormatException)
{
return Task.FromResult(ToolResult.Fail("Invalid Base64 string"));
}
catch (Exception ex2)
{
return Task.FromResult(ToolResult.Fail("오류: " + ex2.Message));
}
}
private static ToolResult EncodeFile(JsonElement args, AgentContext context)
{
if (!args.TryGetProperty("file_path", out var value))
{
return ToolResult.Fail("'file_path' is required for b64file action");
}
string text = value.GetString() ?? "";
if (!Path.IsPathRooted(text))
{
text = Path.Combine(context.WorkFolder, text);
}
if (!File.Exists(text))
{
return ToolResult.Fail("File not found: " + text);
}
FileInfo fileInfo = new FileInfo(text);
if (fileInfo.Length > 5242880)
{
return ToolResult.Fail($"File too large ({fileInfo.Length / 1024}KB). Max 5MB.");
}
byte[] inArray = File.ReadAllBytes(text);
string text2 = Convert.ToBase64String(inArray);
if (text2.Length > 10000)
{
return ToolResult.Ok($"Base64 ({text2.Length} chars, first 500):\n{text2.Substring(0, 500)}...");
}
return ToolResult.Ok(text2);
}
}