Repository navigation
Expand file tree
/
Copy pathAgentAccessClient.cs
More file actions
224 lines (176 loc) · 9.07 KB
/
Copy pathAgentAccessClient.cs
File metadata and controls
224 lines (176 loc) · 9.07 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
using System.Net.Http.Json;
using System.Text.Json;
using CodeMesh.Domain.Contracts;
namespace CodeMesh.Control.AgentAccess;
public sealed record AgentAccessClientOptions(
string BaseUrl = "http://127.0.0.1:8088",
TimeSpan? Timeout = null);
public interface IAgentAccessClient
{
Task<AgentHealthResult> GetHealthAsync(CancellationToken cancellationToken = default);
Task<AgentHealthResult> GetStatusAsync(CancellationToken cancellationToken = default);
Task<AgentContextResult> SearchContextAsync(AgentContextQuery query, CancellationToken cancellationToken = default);
Task<AgentContextPackageResult> GetContextPackageAsync(AgentContextPackageQuery query, CancellationToken cancellationToken = default);
Task<AgentSymbolSearchResult> SearchSymbolsAsync(AgentSymbolQuery query, CancellationToken cancellationToken = default);
Task<AgentNodeResult> GetNodeAsync(AgentNodeLookup lookup, CancellationToken cancellationToken = default);
Task<AgentNeighborResult> GetNeighborsAsync(AgentNeighborQuery query, CancellationToken cancellationToken = default);
Task<AgentRepositoryListResult> ListRepositoriesAsync(int limit = 50, CancellationToken cancellationToken = default);
Task<AgentRepositorySummary?> GetRepositoryAsync(string repositoryId, CancellationToken cancellationToken = default);
Task<AgentRepositoryStatusResult> GetRepositoryStatusAsync(string repositoryId, CancellationToken cancellationToken = default);
Task<AgentRepositoryDeleteResult> DeleteRepositoryAsync(string repositoryId, CancellationToken cancellationToken = default);
Task<AgentNodeSummaryCoverageResult> GetSummaryCoverageAsync(
string repositoryId,
CancellationToken cancellationToken = default);
Task<AgentIngestionRunListResult> ListIngestionRunsAsync(
string? repositoryId = null,
int limit = 50,
CancellationToken cancellationToken = default);
}
public sealed class AgentAccessClient : IAgentAccessClient, IDisposable
{
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
};
private readonly HttpClient _http;
private readonly bool _ownsHttpClient;
public AgentAccessClient(AgentAccessClientOptions options)
: this(CreateHttpClient(options), ownsHttpClient: true)
{
}
public AgentAccessClient(HttpClient httpClient, bool ownsHttpClient = false)
{
_http = httpClient;
_ownsHttpClient = ownsHttpClient;
}
public Task<AgentHealthResult> GetHealthAsync(CancellationToken cancellationToken = default)
=> GetAsync<AgentHealthResult>("health", cancellationToken);
public Task<AgentHealthResult> GetStatusAsync(CancellationToken cancellationToken = default)
=> GetAsync<AgentHealthResult>("status", cancellationToken);
public Task<AgentContextResult> SearchContextAsync(
AgentContextQuery query,
CancellationToken cancellationToken = default)
=> PostAsync<AgentContextQuery, AgentContextResult>("context/search", query, cancellationToken);
public Task<AgentContextPackageResult> GetContextPackageAsync(
AgentContextPackageQuery query,
CancellationToken cancellationToken = default)
=> PostAsync<AgentContextPackageQuery, AgentContextPackageResult>("context/package", query, cancellationToken);
public Task<AgentSymbolSearchResult> SearchSymbolsAsync(
AgentSymbolQuery query,
CancellationToken cancellationToken = default)
=> PostAsync<AgentSymbolQuery, AgentSymbolSearchResult>("symbols/search", query, cancellationToken);
public Task<AgentNodeResult> GetNodeAsync(
AgentNodeLookup lookup,
CancellationToken cancellationToken = default)
=> PostAsync<AgentNodeLookup, AgentNodeResult>("nodes/get", lookup, cancellationToken);
public Task<AgentNeighborResult> GetNeighborsAsync(
AgentNeighborQuery query,
CancellationToken cancellationToken = default)
=> PostAsync<AgentNeighborQuery, AgentNeighborResult>("graph/neighbors", query, cancellationToken);
public Task<AgentRepositoryListResult> ListRepositoriesAsync(
int limit = 50,
CancellationToken cancellationToken = default)
=> GetAsync<AgentRepositoryListResult>($"repositories?limit={limit}", cancellationToken);
public Task<AgentRepositorySummary?> GetRepositoryAsync(
string repositoryId,
CancellationToken cancellationToken = default)
=> GetAsync<AgentRepositorySummary?>($"repositories/{Uri.EscapeDataString(repositoryId)}", cancellationToken);
public Task<AgentRepositoryStatusResult> GetRepositoryStatusAsync(
string repositoryId,
CancellationToken cancellationToken = default)
=> GetAsync<AgentRepositoryStatusResult>($"repositories/{Uri.EscapeDataString(repositoryId)}/status", cancellationToken);
public Task<AgentRepositoryDeleteResult> DeleteRepositoryAsync(
string repositoryId,
CancellationToken cancellationToken = default)
=> DeleteAsync<AgentRepositoryDeleteResult>($"repositories/{Uri.EscapeDataString(repositoryId)}", cancellationToken);
public Task<AgentNodeSummaryCoverageResult> GetSummaryCoverageAsync(
string repositoryId,
CancellationToken cancellationToken = default)
=> GetAsync<AgentNodeSummaryCoverageResult>(
$"repositories/{Uri.EscapeDataString(repositoryId)}/summaries",
cancellationToken);
public Task<AgentIngestionRunListResult> ListIngestionRunsAsync(
string? repositoryId = null,
int limit = 50,
CancellationToken cancellationToken = default)
{
var path = $"ingestion-runs?limit={limit}";
if (!string.IsNullOrWhiteSpace(repositoryId))
{
path += $"&repository_id={Uri.EscapeDataString(repositoryId)}";
}
return GetAsync<AgentIngestionRunListResult>(path, cancellationToken);
}
public void Dispose()
{
if (_ownsHttpClient)
{
_http.Dispose();
}
}
private async Task<TResponse> PostAsync<TRequest, TResponse>(
string path,
TRequest request,
CancellationToken cancellationToken)
{
using var response = await _http
.PostAsJsonAsync(path, request, JsonOptions, cancellationToken)
.ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
throw new InvalidOperationException(
$"Agent Access request to '{path}' failed with HTTP {(int)response.StatusCode}: {body}");
}
var result = await response.Content
.ReadFromJsonAsync<TResponse>(JsonOptions, cancellationToken)
.ConfigureAwait(false);
return result ?? throw new InvalidOperationException(
$"Agent Access request to '{path}' returned an empty response.");
}
private async Task<TResponse> GetAsync<TResponse>(
string path,
CancellationToken cancellationToken)
{
using var response = await _http.GetAsync(path, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
throw new InvalidOperationException(
$"Agent Access request to '{path}' failed with HTTP {(int)response.StatusCode}: {body}");
}
var result = await response.Content
.ReadFromJsonAsync<TResponse>(JsonOptions, cancellationToken)
.ConfigureAwait(false);
return result ?? throw new InvalidOperationException(
$"Agent Access request to '{path}' returned an empty response.");
}
private async Task<TResponse> DeleteAsync<TResponse>(
string path,
CancellationToken cancellationToken)
{
using var response = await _http.DeleteAsync(path, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
throw new InvalidOperationException(
$"Agent Access request to '{path}' failed with HTTP {(int)response.StatusCode}: {body}");
}
var result = await response.Content
.ReadFromJsonAsync<TResponse>(JsonOptions, cancellationToken)
.ConfigureAwait(false);
return result ?? throw new InvalidOperationException(
$"Agent Access request to '{path}' returned an empty response.");
}
private static HttpClient CreateHttpClient(AgentAccessClientOptions options)
{
var baseUrl = options.BaseUrl.EndsWith("/", StringComparison.Ordinal)
? options.BaseUrl
: $"{options.BaseUrl}/";
return new HttpClient
{
BaseAddress = new Uri(baseUrl, UriKind.Absolute),
Timeout = options.Timeout ?? TimeSpan.FromSeconds(30)
};
}
}