-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWorkspaceHttpServer.cs
More file actions
314 lines (288 loc) · 13.4 KB
/
WorkspaceHttpServer.cs
File metadata and controls
314 lines (288 loc) · 13.4 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Microsoft.Extensions.Logging;
using SharpClaw.Code.Protocol.Commands;
using SharpClaw.Code.Protocol.Enums;
using SharpClaw.Code.Protocol.Models;
using SharpClaw.Code.Protocol.Serialization;
using SharpClaw.Code.Runtime.Abstractions;
namespace SharpClaw.Code.Runtime.Server;
/// <summary>
/// Hosts a minimal JSON and SSE HTTP surface over the existing runtime command services.
/// </summary>
public sealed class WorkspaceHttpServer(
IRuntimeCommandService runtimeCommandService,
IShareSessionService shareSessionService,
ISharpClawConfigService sharpClawConfigService,
IHookDispatcher hookDispatcher,
ILogger<WorkspaceHttpServer> logger) : IWorkspaceHttpServer
{
/// <inheritdoc />
public async Task RunAsync(
string workspaceRoot,
string? host,
int? port,
RuntimeCommandContext context,
CancellationToken cancellationToken)
{
var config = await sharpClawConfigService.GetConfigAsync(workspaceRoot, cancellationToken).ConfigureAwait(false);
var effectiveHost = string.IsNullOrWhiteSpace(host) ? config.Document.Server?.Host ?? "127.0.0.1" : host!;
var effectivePort = port is > 0 ? port.Value : config.Document.Server?.Port ?? 7345;
var prefix = $"http://{effectiveHost}:{effectivePort}/";
using var listener = new HttpListener();
listener.Prefixes.Add(prefix);
listener.Start();
logger.LogInformation("SharpClaw server listening on {Prefix}", prefix);
try
{
while (!cancellationToken.IsCancellationRequested)
{
HttpListenerContext httpContext;
try
{
httpContext = await listener.GetContextAsync().WaitAsync(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
break;
}
catch (HttpListenerException) when (cancellationToken.IsCancellationRequested)
{
break;
}
_ = Task.Run(
() => HandleRequestAsync(httpContext, workspaceRoot, context, cancellationToken),
CancellationToken.None);
}
}
finally
{
if (listener.IsListening)
{
listener.Stop();
}
}
}
private async Task HandleRequestAsync(
HttpListenerContext httpContext,
string workspaceRoot,
RuntimeCommandContext defaultContext,
CancellationToken cancellationToken)
{
var request = httpContext.Request;
var response = httpContext.Response;
var requestSucceeded = false;
var statusCode = 500;
var path = request.Url?.AbsolutePath ?? "/";
try
{
if (request.HttpMethod == "GET" && path == "/v1/status")
{
var result = await runtimeCommandService.GetStatusAsync(defaultContext, cancellationToken).ConfigureAwait(false);
statusCode = await WriteCommandResultAsync(response, result, cancellationToken).ConfigureAwait(false);
}
else if (request.HttpMethod == "GET" && path == "/v1/doctor")
{
var result = await runtimeCommandService.RunDoctorAsync(defaultContext, cancellationToken).ConfigureAwait(false);
statusCode = await WriteCommandResultAsync(response, result, cancellationToken).ConfigureAwait(false);
}
else if (request.HttpMethod == "GET" && path == "/v1/sessions")
{
var result = await runtimeCommandService.ListSessionsAsync(defaultContext, cancellationToken).ConfigureAwait(false);
statusCode = await WriteCommandResultAsync(response, result, cancellationToken).ConfigureAwait(false);
}
else if (request.HttpMethod == "GET" && path.StartsWith("/v1/sessions/", StringComparison.Ordinal))
{
var sessionId = Uri.UnescapeDataString(path["/v1/sessions/".Length..]);
var result = await runtimeCommandService.InspectSessionAsync(sessionId, defaultContext, cancellationToken).ConfigureAwait(false);
statusCode = await WriteCommandResultAsync(response, result, cancellationToken).ConfigureAwait(false);
}
else if (request.HttpMethod == "POST" && path == "/v1/prompt")
{
statusCode = await HandlePromptAsync(request, response, workspaceRoot, defaultContext, cancellationToken).ConfigureAwait(false);
}
else if (request.HttpMethod == "POST" && path.StartsWith("/v1/share/", StringComparison.Ordinal))
{
var sessionId = Uri.UnescapeDataString(path["/v1/share/".Length..]);
var result = await runtimeCommandService.ShareSessionAsync(sessionId, defaultContext, cancellationToken).ConfigureAwait(false);
statusCode = await WriteCommandResultAsync(response, result, cancellationToken).ConfigureAwait(false);
}
else if (request.HttpMethod == "DELETE" && path.StartsWith("/v1/share/", StringComparison.Ordinal))
{
var sessionId = Uri.UnescapeDataString(path["/v1/share/".Length..]);
var result = await runtimeCommandService.UnshareSessionAsync(sessionId, defaultContext, cancellationToken).ConfigureAwait(false);
statusCode = await WriteCommandResultAsync(response, result, cancellationToken).ConfigureAwait(false);
}
else if (request.HttpMethod == "GET" && path.StartsWith("/s/", StringComparison.Ordinal))
{
var shareId = Uri.UnescapeDataString(path["/s/".Length..]);
var snapshot = await shareSessionService.GetSharedSnapshotAsync(workspaceRoot, shareId, cancellationToken).ConfigureAwait(false);
if (snapshot is null)
{
statusCode = 404;
await WriteJsonAsync(response, 404, new ErrorEnvelope("Share not found."), cancellationToken).ConfigureAwait(false);
}
else
{
statusCode = 200;
await WriteJsonAsync(response, 200, snapshot, cancellationToken).ConfigureAwait(false);
}
}
else
{
statusCode = 404;
await WriteJsonAsync(response, 404, new ErrorEnvelope("Not found."), cancellationToken).ConfigureAwait(false);
}
requestSucceeded = statusCode < 500;
}
catch (Exception exception)
{
logger.LogError(exception, "Embedded server request handling failed for {Method} {Path}.", request.HttpMethod, path);
statusCode = 500;
await WriteJsonAsync(response, 500, new ErrorEnvelope(exception.Message), cancellationToken).ConfigureAwait(false);
}
finally
{
await DispatchServerHookAsync(workspaceRoot, request, path, statusCode, requestSucceeded).ConfigureAwait(false);
response.Close();
}
}
private async Task<int> HandlePromptAsync(
HttpListenerRequest request,
HttpListenerResponse response,
string workspaceRoot,
RuntimeCommandContext defaultContext,
CancellationToken cancellationToken)
{
await using var body = request.InputStream;
var payload = await JsonSerializer.DeserializeAsync(body, ProtocolJsonContext.Default.ServerPromptRequest, cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException("Request body is required.");
if (string.IsNullOrWhiteSpace(payload.Prompt))
{
throw new InvalidOperationException("The 'prompt' field is required.");
}
var commandContext = new RuntimeCommandContext(
WorkingDirectory: workspaceRoot,
Model: payload.Model ?? defaultContext.Model,
PermissionMode: payload.PermissionMode ?? defaultContext.PermissionMode,
OutputFormat: payload.OutputFormat ?? OutputFormat.Json,
PrimaryMode: payload.PrimaryMode ?? defaultContext.PrimaryMode,
SessionId: payload.SessionId ?? defaultContext.SessionId,
AgentId: payload.AgentId ?? defaultContext.AgentId,
IsInteractive: false);
var result = await runtimeCommandService.ExecutePromptAsync(payload.Prompt, commandContext, cancellationToken).ConfigureAwait(false);
var accept = request.Headers["Accept"];
var wantsSse = (accept?.Contains("text/event-stream", StringComparison.OrdinalIgnoreCase) ?? false)
|| string.Equals(request.QueryString["stream"], "true", StringComparison.OrdinalIgnoreCase);
if (wantsSse)
{
response.StatusCode = 200;
response.ContentType = "text/event-stream";
response.ContentEncoding = Encoding.UTF8;
await using var writer = new StreamWriter(response.OutputStream, new UTF8Encoding(false), leaveOpen: true);
foreach (var runtimeEvent in result.Events)
{
await WriteSseAsync(
writer,
"runtime-event",
JsonSerializer.Serialize(runtimeEvent, runtimeEvent.GetType(), ProtocolJsonContext.Default))
.ConfigureAwait(false);
}
await WriteSseAsync(
writer,
"result",
JsonSerializer.Serialize(result, ProtocolJsonContext.Default.TurnExecutionResult))
.ConfigureAwait(false);
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
return 200;
}
await WriteJsonAsync(response, 200, result, cancellationToken).ConfigureAwait(false);
return 200;
}
private static async Task WriteSseAsync(StreamWriter writer, string eventName, string payload)
{
await writer.WriteLineAsync($"event: {eventName}").ConfigureAwait(false);
foreach (var line in payload.Split('\n'))
{
await writer.WriteLineAsync($"data: {line.TrimEnd('\r')}").ConfigureAwait(false);
}
await writer.WriteLineAsync().ConfigureAwait(false);
}
private static async Task<int> WriteCommandResultAsync(HttpListenerResponse response, CommandResult result, CancellationToken cancellationToken)
{
var parsedData = TryParseData(result.DataJson);
var envelope = new ServerCommandEnvelope(
result.Succeeded,
result.ExitCode,
result.Message,
parsedData,
result.DataJson is not null && parsedData is null ? result.DataJson : null);
var statusCode = result.Succeeded ? 200 : 400;
await WriteJsonAsync(response, statusCode, envelope, cancellationToken).ConfigureAwait(false);
return statusCode;
}
private static JsonElement? TryParseData(string? dataJson)
{
if (string.IsNullOrWhiteSpace(dataJson))
{
return null;
}
try
{
using var document = JsonDocument.Parse(dataJson);
return document.RootElement.Clone();
}
catch (JsonException)
{
return null;
}
}
private static readonly JsonSerializerOptions ServerJsonOptions = CreateServerJsonOptions();
private static async Task WriteJsonAsync(HttpListenerResponse response, int statusCode, object payload, CancellationToken cancellationToken)
{
response.StatusCode = statusCode;
response.ContentType = "application/json";
response.ContentEncoding = Encoding.UTF8;
var json = JsonSerializer.Serialize(payload, payload.GetType(), ServerJsonOptions);
await using var writer = new StreamWriter(response.OutputStream, new UTF8Encoding(false), leaveOpen: true);
await writer.WriteAsync(json.AsMemory(), cancellationToken).ConfigureAwait(false);
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
}
private Task DispatchServerHookAsync(string workspaceRoot, HttpListenerRequest request, string path, int statusCode, bool succeeded)
{
var payload = JsonSerializer.Serialize(
new ServerRequestCompletedPayload(
request.HttpMethod,
path,
statusCode,
succeeded,
DateTimeOffset.UtcNow),
ServerJsonOptions);
return hookDispatcher.DispatchAsync(workspaceRoot, HookTriggerKind.ServerRequestCompleted, payload, CancellationToken.None);
}
private static JsonSerializerOptions CreateServerJsonOptions()
{
var options = new JsonSerializerOptions(ProtocolJsonContext.Default.Options)
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(
ProtocolJsonContext.Default,
new DefaultJsonTypeInfoResolver()),
};
return options;
}
private sealed record ServerCommandEnvelope(
bool Succeeded,
int ExitCode,
string Message,
JsonElement? Data,
string? DataRaw);
private sealed record ErrorEnvelope(string Error);
private sealed record ServerRequestCompletedPayload(
string Method,
string Path,
int StatusCode,
bool Succeeded,
DateTimeOffset CompletedAtUtc);
}