using System.Diagnostics; using System.Windows.Forms; if (args.Length == 0) { ApplicationConfiguration.Initialize(); Application.Run(new Gui()); return 0; } var psi = NewClaude(string.Join(' ', args)); try { using var p = Process.Start(psi)!; // stdout/stderr ereditati: streaming in tempo reale p.StandardInput.Close(); p.WaitForExit(); return p.ExitCode; } catch (System.ComponentModel.Win32Exception) { Console.Error.WriteLine("'claude' non trovato nel PATH."); return 127; } static ProcessStartInfo NewClaude(string prompt) { var psi = new ProcessStartInfo("claude") { WorkingDirectory = AppContext.BaseDirectory, UseShellExecute = false, RedirectStandardInput = true, // evita l'attesa di stdin di claude }; psi.ArgumentList.Add("-p"); psi.ArgumentList.Add(prompt); return psi; } class Gui : Form { readonly TextBox prompt = new() { Dock = DockStyle.Top, Multiline = true, Height = 80, AcceptsReturn = true }; readonly Button send = new() { Dock = DockStyle.Top, Text = "Invia a Claude", Height = 32 }; readonly TextBox output = new() { Dock = DockStyle.Fill, Multiline = true, ReadOnly = true, ScrollBars = ScrollBars.Vertical }; public Gui() { Text = "LlmRun"; Width = 700; Height = 500; Controls.Add(output); Controls.Add(send); Controls.Add(prompt); // Fill per primo: l'ordine inverso dei Dock send.Click += async (_, _) => await Run(); } async Task Run() { if (string.IsNullOrWhiteSpace(prompt.Text)) return; send.Enabled = false; output.Clear(); try { var psi = Program_NewClaude(prompt.Text); using var p = Process.Start(psi)!; p.StandardInput.Close(); void Append(string? l) { if (l != null) BeginInvoke(() => output.AppendText(l + Environment.NewLine)); } p.OutputDataReceived += (_, e) => Append(e.Data); p.ErrorDataReceived += (_, e) => Append(e.Data); p.BeginOutputReadLine(); p.BeginErrorReadLine(); await p.WaitForExitAsync(); output.AppendText($"[exit {p.ExitCode}]"); } catch (System.ComponentModel.Win32Exception) { output.Text = "'claude' non trovato nel PATH."; } finally { send.Enabled = true; } } static ProcessStartInfo Program_NewClaude(string prompt) { var psi = new ProcessStartInfo("claude") { WorkingDirectory = AppContext.BaseDirectory, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true, }; psi.ArgumentList.Add("-p"); psi.ArgumentList.Add(prompt); return psi; } }