-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
417 lines (367 loc) · 13.9 KB
/
Program.cs
File metadata and controls
417 lines (367 loc) · 13.9 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* C# Text Intelligence Starter - Backend Server
*
* This is a minimal API server providing text intelligence analysis
* powered by Deepgram's Text Intelligence service. It's designed to be
* easily modified and extended for your own projects.
*
* Key Features:
* - Contract-compliant API endpoint: POST /api/text-intelligence
* - Accepts text or URL in JSON body
* - Supports multiple intelligence features: summarization, topics, sentiment, intents
* - JWT session auth with rate limiting (production only)
* - CORS enabled for frontend communication
*/
using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography;
using System.Text.Json;
using Deepgram;
using Deepgram.Models.Analyze.v1;
using Microsoft.IdentityModel.Tokens;
using Tomlyn;
using Tomlyn.Model;
using HttpResults = Microsoft.AspNetCore.Http.Results;
// ============================================================================
// ENVIRONMENT LOADING
// ============================================================================
DotNetEnv.Env.Load();
// ============================================================================
// CONFIGURATION
// ============================================================================
var port = int.TryParse(Environment.GetEnvironmentVariable("PORT"), out var p) ? p : 8081;
var host = Environment.GetEnvironmentVariable("HOST") ?? "0.0.0.0";
// ============================================================================
// SESSION AUTH - JWT tokens for production security
// ============================================================================
var sessionSecretEnv = Environment.GetEnvironmentVariable("SESSION_SECRET");
var sessionSecret = sessionSecretEnv ?? Convert.ToHexString(RandomNumberGenerator.GetBytes(32)).ToLowerInvariant();
var sessionSecretKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(sessionSecret));
const int JwtExpirySeconds = 3600;
string CreateSessionToken()
{
var handler = new JwtSecurityTokenHandler();
var descriptor = new SecurityTokenDescriptor
{
Expires = DateTime.UtcNow.AddSeconds(JwtExpirySeconds),
SigningCredentials = new SigningCredentials(sessionSecretKey, SecurityAlgorithms.HmacSha256Signature),
};
var token = handler.CreateToken(descriptor);
return handler.WriteToken(token);
}
bool ValidateSessionToken(string token)
{
try
{
var handler = new JwtSecurityTokenHandler();
handler.ValidateToken(token, new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = sessionSecretKey,
ValidateIssuer = false,
ValidateAudience = false,
ClockSkew = TimeSpan.Zero,
}, out _);
return true;
}
catch
{
return false;
}
}
// ============================================================================
// API KEY LOADING
// ============================================================================
static string LoadApiKey()
{
var apiKey = Environment.GetEnvironmentVariable("DEEPGRAM_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
Console.Error.WriteLine("\n❌ ERROR: Deepgram API key not found!\n");
Console.Error.WriteLine("Please set your API key in .env file:");
Console.Error.WriteLine(" DEEPGRAM_API_KEY=your_api_key_here\n");
Console.Error.WriteLine("Get your API key at: https://console.deepgram.com\n");
Environment.Exit(1);
}
return apiKey;
}
var apiKey = LoadApiKey();
// ============================================================================
// SETUP
// ============================================================================
Library.Initialize();
var deepgramClient = ClientFactory.CreateAnalyzeClient(apiKey);
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls($"http://{host}:{port}");
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
app.UseCors();
// ============================================================================
// SESSION ROUTES
// ============================================================================
app.MapGet("/api/session", () =>
{
var token = CreateSessionToken();
return HttpResults.Json(new Dictionary<string, string> { ["token"] = token });
});
// ============================================================================
// API ROUTES
// ============================================================================
/// POST /api/text-intelligence
///
/// Contract-compliant text intelligence endpoint.
/// Accepts:
/// - Query parameters: summarize, topics, sentiment, intents, language (all optional)
/// - Body: JSON with either text or url field (required, not both)
app.MapPost("/api/text-intelligence", async (HttpRequest request) =>
{
// Validate JWT session token
var authHeader = request.Headers.Authorization.FirstOrDefault() ?? "";
if (!authHeader.StartsWith("Bearer "))
{
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "AuthenticationError",
["code"] = "MISSING_TOKEN",
["message"] = "Authorization header with Bearer token is required",
}
}, statusCode: 401);
}
if (!ValidateSessionToken(authHeader[7..]))
{
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "AuthenticationError",
["code"] = "INVALID_TOKEN",
["message"] = "Invalid or expired session token",
}
}, statusCode: 401);
}
try
{
// Read JSON body
var body = await request.ReadFromJsonAsync<Dictionary<string, string>>();
var text = body?.GetValueOrDefault("text");
var url = body?.GetValueOrDefault("url");
// Validate that exactly one of text or url is provided
if (string.IsNullOrEmpty(text) && string.IsNullOrEmpty(url))
{
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "validation_error",
["code"] = "INVALID_TEXT",
["message"] = "Request must contain either 'text' or 'url' field",
}
}, statusCode: 400);
}
if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(url))
{
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "validation_error",
["code"] = "INVALID_TEXT",
["message"] = "Request must contain either 'text' or 'url', not both",
}
}, statusCode: 400);
}
// Get the text content (either directly or from URL)
string textContent;
if (!string.IsNullOrEmpty(url))
{
// Validate URL format
if (!Uri.TryCreate(url, UriKind.Absolute, out _))
{
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "validation_error",
["code"] = "INVALID_URL",
["message"] = "Invalid URL format",
}
}, statusCode: 400);
}
// Fetch text from URL
try
{
using var httpClient = new HttpClient();
var response = await httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "validation_error",
["code"] = "INVALID_URL",
["message"] = $"Failed to fetch URL: {response.StatusCode}",
}
}, statusCode: 400);
}
textContent = await response.Content.ReadAsStringAsync();
}
catch (Exception e)
{
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "validation_error",
["code"] = "INVALID_URL",
["message"] = $"Failed to fetch URL: {e.Message}",
}
}, statusCode: 400);
}
}
else
{
textContent = text!;
}
// Check for empty text
if (string.IsNullOrWhiteSpace(textContent))
{
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "validation_error",
["code"] = "EMPTY_TEXT",
["message"] = "Text content cannot be empty",
}
}, statusCode: 400);
}
// Extract query parameters for intelligence features
var language = request.Query["language"].FirstOrDefault() ?? "en";
var summarize = request.Query["summarize"].FirstOrDefault();
var topics = request.Query["topics"].FirstOrDefault();
var sentiment = request.Query["sentiment"].FirstOrDefault();
var intents = request.Query["intents"].FirstOrDefault();
// Build Deepgram options
var schema = new AnalyzeSchema
{
Language = language,
};
if (summarize == "true" || summarize == "v2")
{
schema.Summarize = true;
}
else if (summarize == "v1")
{
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "validation_error",
["code"] = "INVALID_TEXT",
["message"] = "Summarization v1 is no longer supported. Please use v2 or true.",
}
}, statusCode: 400);
}
if (topics == "true") schema.Topics = true;
if (sentiment == "true") schema.Sentiment = true;
if (intents == "true") schema.Intents = true;
// Call Deepgram API
var result = await deepgramClient.AnalyzeText(
new TextSource(textContent),
schema);
if (result == null)
{
throw new InvalidOperationException("Deepgram returned null response");
}
// Return results using raw JSON serialization for proper formatting
var resultJson = JsonSerializer.Serialize(new
{
results = result.Results ?? new object()
});
return HttpResults.Content(resultJson, "application/json");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Text Intelligence Error: {ex}");
var errorCode = "INVALID_TEXT";
var statusCode = 500;
var msg = ex.Message.ToLowerInvariant();
if (msg.Contains("text"))
{
errorCode = "INVALID_TEXT";
statusCode = 400;
}
else if (msg.Contains("too long"))
{
errorCode = "TEXT_TOO_LONG";
statusCode = 400;
}
return HttpResults.Json(new Dictionary<string, object>
{
["error"] = new Dictionary<string, string>
{
["type"] = "processing_error",
["code"] = errorCode,
["message"] = ex.Message,
}
}, statusCode: statusCode);
}
});
// Health check endpoint
app.MapGet("/health", () => HttpResults.Json(new { status = "ok", service = "text-intelligence" }));
// Metadata endpoint
app.MapGet("/api/metadata", () =>
{
try
{
var tomlPath = Path.Combine(Directory.GetCurrentDirectory(), "deepgram.toml");
var tomlContent = File.ReadAllText(tomlPath);
var tomlModel = Toml.ToModel(tomlContent);
if (!tomlModel.ContainsKey("meta") || tomlModel["meta"] is not TomlTable metaTable)
{
return HttpResults.Json(new Dictionary<string, string>
{
["error"] = "INTERNAL_SERVER_ERROR",
["message"] = "Missing [meta] section in deepgram.toml",
}, statusCode: 500);
}
var meta = new Dictionary<string, object?>();
foreach (var kvp in metaTable)
{
meta[kvp.Key] = kvp.Value;
}
return HttpResults.Json(meta);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error reading metadata: {ex}");
return HttpResults.Json(new Dictionary<string, string>
{
["error"] = "INTERNAL_SERVER_ERROR",
["message"] = "Failed to read metadata from deepgram.toml",
}, statusCode: 500);
}
});
// ============================================================================
// SERVER START
// ============================================================================
Console.WriteLine();
Console.WriteLine(new string('=', 70));
Console.WriteLine($"🚀 Backend API Server running at http://localhost:{port}");
Console.WriteLine($"📡 CORS enabled for all origins");
Console.WriteLine($"📡 GET /api/session");
Console.WriteLine($"📡 POST /api/text-intelligence (auth required)");
Console.WriteLine($"📡 GET /api/metadata");
Console.WriteLine(new string('=', 70));
Console.WriteLine();
app.Run();