using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; namespace AxCopilot.Services.Agent; public class ClipboardTool : IAgentTool { public string Name => "clipboard_tool"; public string Description => "Read or write the Windows clipboard. Actions: 'read' — get current clipboard text content; 'write' — set clipboard text content; 'has_text' — check if clipboard contains text; 'has_image' — check if clipboard contains an image."; public ToolParameterSchema Parameters { get { ToolParameterSchema toolParameterSchema = new ToolParameterSchema(); Dictionary dictionary = new Dictionary(); ToolProperty obj = new ToolProperty { Type = "string", Description = "Action to perform" }; int num = 4; List list = new List(num); CollectionsMarshal.SetCount(list, num); Span span = CollectionsMarshal.AsSpan(list); span[0] = "read"; span[1] = "write"; span[2] = "has_text"; span[3] = "has_image"; obj.Enum = list; dictionary["action"] = obj; dictionary["text"] = new ToolProperty { Type = "string", Description = "Text to write to clipboard (required for 'write' action)" }; toolParameterSchema.Properties = dictionary; num = 1; List list2 = new List(num); CollectionsMarshal.SetCount(list2, num); CollectionsMarshal.AsSpan(list2)[0] = "action"; toolParameterSchema.Required = list2; return toolParameterSchema; } } public Task ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct = default(CancellationToken)) { string action = args.GetProperty("action").GetString() ?? ""; try { ToolResult result = null; ((DispatcherObject)Application.Current).Dispatcher.Invoke((Action)delegate { if (1 == 0) { } ToolResult toolResult = action switch { "read" => ReadClipboard(), "write" => WriteClipboard(args), "has_text" => ToolResult.Ok(Clipboard.ContainsText() ? "true" : "false"), "has_image" => ToolResult.Ok(Clipboard.ContainsImage() ? "true" : "false"), _ => ToolResult.Fail("Unknown action: " + action), }; if (1 == 0) { } result = toolResult; }); return Task.FromResult(result ?? ToolResult.Fail("클립보드 접근 실패")); } catch (Exception ex) { return Task.FromResult(ToolResult.Fail("클립보드 오류: " + ex.Message)); } } private static ToolResult ReadClipboard() { if (!Clipboard.ContainsText()) { return ToolResult.Ok("(clipboard is empty or contains non-text data)"); } string text = Clipboard.GetText(); if (string.IsNullOrEmpty(text)) { return ToolResult.Ok("(empty)"); } if (text.Length > 10000) { return ToolResult.Ok(text.Substring(0, 10000) + $"\n\n... (truncated, total {text.Length} chars)"); } return ToolResult.Ok(text); } private static ToolResult WriteClipboard(JsonElement args) { if (!args.TryGetProperty("text", out var value)) { return ToolResult.Fail("'text' parameter is required for write action"); } string text = value.GetString() ?? ""; Clipboard.SetText(text); return ToolResult.Ok($"✓ Clipboard updated ({text.Length} chars)"); } }