-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialMemoryClient.cs
More file actions
354 lines (305 loc) · 10.7 KB
/
SerialMemoryClient.cs
File metadata and controls
354 lines (305 loc) · 10.7 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace SerialMemory.Sdk;
/// <summary>
/// Lightweight SerialMemory client SDK for .NET applications.
/// Provides typed methods for memory operations with built-in retry and rate limit handling.
/// Dependency-light: uses only System.Text.Json (built-in).
/// </summary>
public sealed class SerialMemoryClient : IDisposable
{
private readonly HttpClient _httpClient;
private readonly SerialMemoryClientOptions _options;
private readonly JsonSerializerOptions _jsonOptions;
private bool _disposed;
/// <summary>
/// Event raised when usage crosses a threshold (75%, 90%).
/// </summary>
public event Action<UsageWarning>? OnUsageWarning;
/// <summary>
/// Creates a new SerialMemory client.
/// </summary>
/// <param name="options">Client configuration options</param>
public SerialMemoryClient(SerialMemoryClientOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
if (string.IsNullOrWhiteSpace(options.BaseUrl))
throw new ArgumentException("BaseUrl is required", nameof(options));
_httpClient = new HttpClient
{
BaseAddress = new Uri(options.BaseUrl.TrimEnd('/') + "/"),
Timeout = options.Timeout
};
// Configure authentication
if (!string.IsNullOrEmpty(options.ApiKey))
{
if (options.ApiKey.StartsWith("sm_", StringComparison.OrdinalIgnoreCase))
{
// API key format - use X-API-Key header
_httpClient.DefaultRequestHeaders.Add("X-API-Key", options.ApiKey);
}
else
{
// Assume JWT token
_httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", options.ApiKey);
}
}
_jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true
};
}
#region Core Memory Operations
/// <summary>
/// Search memories using semantic, text, or hybrid search.
/// </summary>
public async Task<SearchResult> SearchAsync(
string query,
SearchMode mode = SearchMode.Hybrid,
int limit = 10,
float threshold = 0.7f,
bool includeEntities = true,
CancellationToken ct = default)
{
var request = new
{
query,
mode = mode.ToString().ToLowerInvariant(),
limit,
threshold,
include_entities = includeEntities
};
return await PostAsync<SearchResult>("tools/memory_search", request, ct);
}
/// <summary>
/// Ingest a new memory. Automatically extracts entities and generates embeddings.
/// </summary>
public async Task<IngestResult> IngestAsync(
string content,
string? source = null,
Dictionary<string, object>? metadata = null,
bool extractEntities = true,
CancellationToken ct = default)
{
var request = new
{
content,
source = source ?? "sdk-dotnet",
metadata,
extract_entities = extractEntities
};
return await PostAsync<IngestResult>("tools/memory_ingest", request, ct);
}
/// <summary>
/// Update an existing memory's content (creates new version).
/// </summary>
public async Task<UpdateResult> UpdateAsync(
string memoryId,
string newContent,
string? reason = null,
CancellationToken ct = default)
{
var request = new
{
memory_id = memoryId,
new_content = newContent,
reason
};
return await PostAsync<UpdateResult>("tools/memory_update", request, ct);
}
/// <summary>
/// Soft delete (invalidate) a memory.
/// </summary>
public async Task<DeleteResult> DeleteAsync(
string memoryId,
string reason,
CancellationToken ct = default)
{
var request = new
{
memory_id = memoryId,
reason
};
return await PostAsync<DeleteResult>("tools/memory_delete", request, ct);
}
#endregion
#region User Persona
/// <summary>
/// Get user persona information (preferences, skills, goals, background).
/// </summary>
public async Task<UserPersona> GetUserPersonaAsync(
string userId = "default_user",
CancellationToken ct = default)
{
var request = new { user_id = userId };
return await PostAsync<UserPersona>("tools/memory_about_user", request, ct);
}
/// <summary>
/// Set or update a user persona attribute.
/// </summary>
public async Task<SetPersonaResult> SetUserPersonaAsync(
string attributeType,
string attributeKey,
string attributeValue,
float confidence = 1.0f,
string userId = "default_user",
CancellationToken ct = default)
{
var request = new
{
attribute_type = attributeType,
attribute_key = attributeKey,
attribute_value = attributeValue,
confidence,
user_id = userId
};
return await PostAsync<SetPersonaResult>("tools/set_user_persona", request, ct);
}
#endregion
#region Limits & Usage
/// <summary>
/// Get current tenant limits and usage information.
/// Useful for displaying usage to users or making decisions about operations.
/// </summary>
public async Task<TenantLimits> GetLimitsAsync(CancellationToken ct = default)
{
var limits = await GetAsync<TenantLimits>("tenant/limits", ct);
// Check for warnings and raise events
CheckUsageWarnings(limits);
return limits;
}
private void CheckUsageWarnings(TenantLimits limits)
{
if (OnUsageWarning == null) return;
foreach (var warning in limits.Warnings)
{
OnUsageWarning.Invoke(new UsageWarning
{
Type = warning.Type,
Message = warning.Message,
PercentUsed = warning.PercentUsed,
Severity = warning.Severity
});
}
}
#endregion
#region HTTP Helpers
private async Task<T> PostAsync<T>(string endpoint, object request, CancellationToken ct)
{
return await ExecuteWithRetryAsync(async () =>
{
var response = await _httpClient.PostAsJsonAsync(endpoint, request, _jsonOptions, ct);
return await HandleResponseAsync<T>(response, ct);
}, ct);
}
private async Task<T> GetAsync<T>(string endpoint, CancellationToken ct)
{
return await ExecuteWithRetryAsync(async () =>
{
var response = await _httpClient.GetAsync(endpoint, ct);
return await HandleResponseAsync<T>(response, ct);
}, ct);
}
private async Task<T> HandleResponseAsync<T>(HttpResponseMessage response, CancellationToken ct)
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadFromJsonAsync<T>(_jsonOptions, ct);
return result ?? throw new SerialMemoryException("Empty response received");
}
var errorContent = await response.Content.ReadAsStringAsync(ct);
// Handle specific error codes
switch (response.StatusCode)
{
case HttpStatusCode.TooManyRequests:
var retryAfter = response.Headers.RetryAfter?.Delta ?? TimeSpan.FromSeconds(60);
throw new RateLimitException(retryAfter, errorContent);
case (HttpStatusCode)402: // Payment Required - Usage limit exceeded
throw new UsageLimitException(errorContent);
case HttpStatusCode.Unauthorized:
case HttpStatusCode.Forbidden:
throw new AuthenticationException(errorContent);
default:
throw new SerialMemoryException($"Request failed ({(int)response.StatusCode}): {errorContent}");
}
}
private async Task<T> ExecuteWithRetryAsync<T>(Func<Task<T>> operation, CancellationToken ct)
{
var maxRetries = _options.MaxRetries;
var delay = TimeSpan.FromMilliseconds(500);
for (int attempt = 0; attempt <= maxRetries; attempt++)
{
try
{
return await operation();
}
catch (RateLimitException ex) when (attempt < maxRetries)
{
// Use retry-after header if available
await Task.Delay(ex.RetryAfter, ct);
}
catch (HttpRequestException) when (attempt < maxRetries)
{
// Transient error - exponential backoff with jitter
var jitter = TimeSpan.FromMilliseconds(Random.Shared.Next(0, 100));
await Task.Delay(delay + jitter, ct);
delay *= 2; // Exponential backoff
}
catch (TaskCanceledException) when (!ct.IsCancellationRequested && attempt < maxRetries)
{
// Timeout - retry with backoff
await Task.Delay(delay, ct);
delay *= 2;
}
}
// Should not reach here, but just in case
throw new SerialMemoryException("Max retries exceeded");
}
#endregion
public void Dispose()
{
if (_disposed) return;
_httpClient.Dispose();
_disposed = true;
}
}
/// <summary>
/// Client configuration options.
/// </summary>
public sealed class SerialMemoryClientOptions
{
/// <summary>
/// Base URL of the SerialMemory API (e.g., "http://localhost:5000").
/// </summary>
public required string BaseUrl { get; init; }
/// <summary>
/// API key (sm_live_xxx format) or JWT token for authentication.
/// </summary>
public required string ApiKey { get; init; }
/// <summary>
/// HTTP request timeout (default: 30 seconds).
/// </summary>
public TimeSpan Timeout { get; init; } = TimeSpan.FromSeconds(30);
/// <summary>
/// Maximum retry attempts for transient failures (default: 3).
/// </summary>
public int MaxRetries { get; init; } = 3;
}
/// <summary>
/// Search mode for memory queries.
/// </summary>
public enum SearchMode
{
/// <summary>Vector similarity search using embeddings.</summary>
Semantic,
/// <summary>Full-text search.</summary>
Text,
/// <summary>Combined semantic + text search (recommended).</summary>
Hybrid
}