Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
171 lines (152 loc) · 6.22 KB
/
Copy pathProgram.cs
File metadata and controls
171 lines (152 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Linq;
using System.Threading.Tasks;
using ChromeSidecarStandalone.Services;
using ChromeSidecarStandalone.Util;
namespace ChromeSidecarStandalone
{
internal static class Program
{
private static int Main(string[] args)
{
Log.Init();
try
{
return RunAsync(args ?? new string[0]).GetAwaiter().GetResult();
}
catch (Exception ex)
{
Console.Error.WriteLine("AI Chat Bridge failed: " + ex.Message);
return 1;
}
finally
{
Log.Shutdown();
}
}
private static async Task<int> RunAsync(string[] args)
{
if (args.Any(a => string.Equals(a, "--help", StringComparison.OrdinalIgnoreCase)
|| string.Equals(a, "-h", StringComparison.OrdinalIgnoreCase)))
{
PrintUsage();
return 0;
}
var provider = GetOptionValue(args, "--provider");
if (args.Any(a => string.Equals(a, "--start", StringComparison.OrdinalIgnoreCase)))
{
using (var sidecar = CreateSidecar())
{
await sidecar.StartChromeAsync(provider).ConfigureAwait(false);
Console.WriteLine("started=true");
return 0;
}
}
if (args.Any(a => string.Equals(a, "--stop", StringComparison.OrdinalIgnoreCase)))
{
using (var sidecar = CreateSidecar())
{
sidecar.StopChrome();
Console.WriteLine("stopped=true");
return 0;
}
}
if (args.Any(a => string.Equals(a, "--new", StringComparison.OrdinalIgnoreCase)))
{
using (var sidecar = CreateSidecar())
{
var created = await sidecar.StartNewChatAsync(provider).ConfigureAwait(false);
Console.WriteLine("newChat=" + created);
return created ? 0 : 1;
}
}
if (args.Any(a => string.Equals(a, "--close", StringComparison.OrdinalIgnoreCase)))
{
using (var sidecar = CreateSidecar())
{
var closed = await sidecar.CloseAllTabsAsync().ConfigureAwait(false);
Console.WriteLine("closedTabs=" + closed);
return 0;
}
}
if (args.Any(a => string.Equals(a, "--show", StringComparison.OrdinalIgnoreCase)))
return SetVisibility(true);
if (args.Any(a => string.Equals(a, "--hide", StringComparison.OrdinalIgnoreCase)))
return SetVisibility(false);
using (var sidecar = CreateSidecar())
{
sidecar.OnLog += message => Console.Error.WriteLine(message);
if (args.Any(a => string.Equals(a, "--status", StringComparison.OrdinalIgnoreCase)))
{
Console.WriteLine("connected=" + sidecar.IsConnected);
Console.WriteLine("visible=" + sidecar.IsManagedChromeVisible());
foreach (var snapshot in ChromeSidecar.GetProviderRateLimitSnapshots())
Console.WriteLine(snapshot.provider + "=" + snapshot.state);
return 0;
}
var prompt = GetOptionValue(args, "--query");
if (!string.IsNullOrWhiteSpace(prompt))
{
var response = string.IsNullOrWhiteSpace(provider)
? await sidecar.QueryAIAsync(prompt).ConfigureAwait(false)
: await sidecar.QueryAIAsync(prompt, provider).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(response))
{
Console.Error.WriteLine("No AI response received: " + sidecar.LastFailureReason);
return 1;
}
Console.WriteLine(response);
return 0;
}
if (args.Any(a => string.Equals(a, "--connect", StringComparison.OrdinalIgnoreCase)))
{
var connected = string.IsNullOrWhiteSpace(provider)
? await sidecar.ConnectAsync().ConfigureAwait(false)
: await sidecar.ConnectAsync(provider).ConfigureAwait(false);
Console.WriteLine("connected=" + connected);
return connected ? 0 : 1;
}
}
PrintUsage();
return 2;
}
private static int SetVisibility(bool visible)
{
using (var sidecar = CreateSidecar())
{
var applied = sidecar.SetManagedChromeVisible(visible);
Console.WriteLine("visible=" + sidecar.IsManagedChromeVisible());
return applied ? 0 : 1;
}
}
private static string GetOptionValue(string[] args, string option)
{
for (var i = 0; i < args.Length - 1; i++)
{
if (string.Equals(args[i], option, StringComparison.OrdinalIgnoreCase))
return args[i + 1];
}
return null;
}
private static ChromeSidecar CreateSidecar()
{
return new ChromeSidecar
{
KeepManagedChromeAlive = true,
UseFreshChatForQueries = false
};
}
private static void PrintUsage()
{
Console.WriteLine("AI Chat Bridge");
Console.WriteLine(" --start [--provider chatgpt|gemini|claude]");
Console.WriteLine(" --stop");
Console.WriteLine(" --new [--provider chatgpt|gemini|claude]");
Console.WriteLine(" --close");
Console.WriteLine(" --connect [--provider chatgpt|gemini|claude]");
Console.WriteLine(" --query <prompt> [--provider chatgpt|gemini|claude]");
Console.WriteLine(" --status");
Console.WriteLine(" --show | --hide");
}
}
}