-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
531 lines (469 loc) · 15 KB
/
Copy pathserver.ts
File metadata and controls
531 lines (469 loc) · 15 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/**
* Bun Text Intelligence Starter - Backend Server
*
* This is a simple Bun HTTP server that provides a text intelligence API endpoint
* powered by Deepgram's Text Intelligence service. It's designed to be easily
* modified and extended for your own projects.
*
* Key Features:
* - API endpoint: POST /api/text-intelligence
* - Accepts text or URL in JSON body
* - Supports multiple intelligence features: summarization, topics, sentiment, intents
* - JWT session auth for API protection
* - Native TypeScript support
* - No external web framework needed
*/
import { createClient } from "@deepgram/sdk";
import { parse as parseTOML } from "@iarna/toml";
import { readFileSync } from "fs";
import { SignJWT, jwtVerify } from "jose";
// ============================================================================
// CONFIGURATION - Customize these values for your needs
// ============================================================================
/**
* Server configuration - These can be overridden via environment variables
*/
interface ServerConfig {
port: number;
host: string;
}
const config: ServerConfig = {
port: parseInt(Bun.env.PORT || "8081"),
host: Bun.env.HOST || "0.0.0.0",
};
// ============================================================================
// SESSION AUTH - JWT tokens for API protection
// ============================================================================
/**
* Session secret for signing JWTs.
* Auto-generated in dev mode; set SESSION_SECRET env var in production.
*/
const SESSION_SECRET =
Bun.env.SESSION_SECRET ||
crypto.randomUUID().replace(/-/g, "") +
crypto.randomUUID().replace(/-/g, "");
/** Encoded secret key for jose JWT operations */
const SESSION_SECRET_KEY = new TextEncoder().encode(SESSION_SECRET);
/** JWT expiry time (1 hour) */
const JWT_EXPIRY = "1h";
/**
* Creates a signed JWT session token
*/
async function createSessionToken(): Promise<string> {
return await new SignJWT({ iat: Math.floor(Date.now() / 1000) })
.setProtectedHeader({ alg: "HS256" })
.setExpirationTime(JWT_EXPIRY)
.sign(SESSION_SECRET_KEY);
}
/**
* Verifies a JWT session token
*/
async function verifySessionToken(token: string): Promise<boolean> {
try {
await jwtVerify(token, SESSION_SECRET_KEY);
return true;
} catch {
return false;
}
}
// ============================================================================
// API KEY LOADING - Load Deepgram API key from environment
// ============================================================================
/**
* Loads the Deepgram API key from environment variables.
* Exits with helpful message if not found.
*/
function loadApiKey(): string {
const apiKey = Bun.env.DEEPGRAM_API_KEY;
if (!apiKey) {
console.error("\n\u274C ERROR: Deepgram API key not found!\n");
console.error("Please set your API key using one of these methods:\n");
console.error("1. Create a .env file (recommended):");
console.error(" DEEPGRAM_API_KEY=your_api_key_here\n");
console.error("2. Environment variable:");
console.error(" export DEEPGRAM_API_KEY=your_api_key_here\n");
console.error("Get your API key at: https://console.deepgram.com\n");
process.exit(1);
}
return apiKey;
}
const apiKey = loadApiKey();
// ============================================================================
// SETUP - Initialize Deepgram client
// ============================================================================
const deepgram = createClient(apiKey);
// ============================================================================
// CORS CONFIGURATION
// ============================================================================
/**
* Returns standard CORS headers for cross-origin requests.
* Bun uses the CORS pattern (backend=8081, frontend=8080).
*/
function getCorsHeaders(): HeadersInit {
return {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
}
// ============================================================================
// TYPES - TypeScript interfaces for request/response
// ============================================================================
interface ErrorResponse {
error: {
type: "validation_error" | "processing_error";
code: string;
message: string;
details: Record<string, unknown>;
};
}
// ============================================================================
// HELPER FUNCTIONS
// ============================================================================
/**
* Validates text intelligence input — must have text XOR url
*/
function validateAnalysisInput(
text: string | undefined,
url: string | undefined
): string | null {
if (!text && !url) {
return "Request must contain either 'text' or 'url' field";
}
if (text && url) {
return "Request must contain only one of 'text' or 'url', not both";
}
return null;
}
/**
* Formats error responses in a consistent structure
*/
function formatErrorResponse(
error: Error,
statusCode: number = 500,
code?: string,
type?: string
): Response {
const errorBody: ErrorResponse = {
error: {
type: (type ||
(statusCode === 400
? "validation_error"
: "processing_error")) as ErrorResponse["error"]["type"],
code: code || (statusCode === 400 ? "INVALID_TEXT" : "ANALYSIS_FAILED"),
message: error.message || "An error occurred during analysis",
details: {},
},
};
return Response.json(errorBody, {
status: statusCode,
headers: getCorsHeaders(),
});
}
// ============================================================================
// SESSION ROUTE HANDLERS
// ============================================================================
/**
* GET /api/session
* Issues a signed JWT session token.
*/
async function handleGetSession(): Promise<Response> {
const token = await createSessionToken();
return Response.json({ token }, { headers: getCorsHeaders() });
}
/**
* Validates JWT from Authorization header. Returns error Response or null if OK.
*/
async function checkAuth(req: Request): Promise<Response | null> {
const authHeader = req.headers.get("Authorization") || "";
if (!authHeader.startsWith("Bearer ")) {
return Response.json(
{
error: {
type: "AuthenticationError",
code: "MISSING_TOKEN",
message: "Authorization header with Bearer token is required",
},
},
{ status: 401, headers: getCorsHeaders() }
);
}
const token = authHeader.slice(7);
if (!(await verifySessionToken(token))) {
return Response.json(
{
error: {
type: "AuthenticationError",
code: "INVALID_TOKEN",
message: "Invalid or expired session token",
},
},
{ status: 401, headers: getCorsHeaders() }
);
}
return null;
}
// ============================================================================
// API ROUTE HANDLERS
// ============================================================================
/**
* POST /api/text-intelligence
* Main text intelligence endpoint.
* Accepts JSON body with text or url, plus query params for features.
*/
async function handleAnalysis(req: Request): Promise<Response> {
try {
const url = new URL(req.url);
const body = await req.json();
const { text, url: textUrl } = body;
const headers: Record<string, string> = {
"content-type": "application/json",
...getCorsHeaders(),
};
// Validate input — must have text XOR url
const validationError = validateAnalysisInput(text, textUrl);
if (validationError) {
return new Response(
JSON.stringify({
error: {
type: "validation_error",
code: "INVALID_TEXT",
message: validationError,
details: {},
},
}),
{ status: 400, headers }
);
}
// If URL provided, validate format
if (textUrl) {
try {
new URL(textUrl);
} catch {
return new Response(
JSON.stringify({
error: {
type: "validation_error",
code: "INVALID_URL",
message: "Invalid URL format",
details: {},
},
}),
{ status: 400, headers }
);
}
}
// Resolve text content: fetch from URL if needed
let textContent: string;
if (textUrl) {
try {
const response = await fetch(textUrl);
if (!response.ok) {
return new Response(
JSON.stringify({
error: {
type: "validation_error",
code: "INVALID_URL",
message: `Failed to fetch URL: ${response.statusText}`,
details: {},
},
}),
{ status: 400, headers }
);
}
textContent = await response.text();
} catch (e) {
return new Response(
JSON.stringify({
error: {
type: "validation_error",
code: "INVALID_URL",
message: `Failed to fetch URL: ${(e as Error).message}`,
details: {},
},
}),
{ status: 400, headers }
);
}
} else {
textContent = text;
}
// Check for empty text
if (!textContent || textContent.trim().length === 0) {
return new Response(
JSON.stringify({
error: {
type: "validation_error",
code: "EMPTY_TEXT",
message: "Text content cannot be empty",
details: {},
},
}),
{ status: 400, headers }
);
}
// Extract query parameters for intelligence features
const options: Record<string, unknown> = {
language: url.searchParams.get("language") || "en",
};
const summarize = url.searchParams.get("summarize");
if (summarize === "true") {
options.summarize = true;
} else if (summarize === "v2") {
options.summarize = "v2";
} else if (summarize === "v1") {
return new Response(
JSON.stringify({
error: {
type: "validation_error",
code: "INVALID_TEXT",
message:
"Summarization v1 is no longer supported. Please use v2 or true.",
details: {},
},
}),
{ status: 400, headers }
);
}
const topics = url.searchParams.get("topics");
if (topics === "true") options.topics = true;
const sentiment = url.searchParams.get("sentiment");
if (sentiment === "true") options.sentiment = true;
const intents = url.searchParams.get("intents");
if (intents === "true") options.intents = true;
// Call Deepgram API (SDK v4 returns { result, error })
const { result, error } = await deepgram.read.analyzeText(
{ text: textContent },
options
);
// Handle SDK errors
if (error) {
console.error("Deepgram API Error:", error);
return new Response(
JSON.stringify({
error: {
type: "processing_error",
code: "INVALID_TEXT",
message: error.message || "Failed to process text",
details: {},
},
}),
{ status: 400, headers }
);
}
// Return full results object (includes all requested features)
return new Response(
JSON.stringify({ results: result.results || {} }),
{ status: 200, headers }
);
} catch (err) {
console.error("Analysis error:", err);
return formatErrorResponse(err as Error);
}
}
/**
* GET /api/metadata
* Returns metadata about this starter application from deepgram.toml
*/
function handleMetadata(): Response {
try {
const tomlContent = readFileSync("./deepgram.toml", "utf-8");
const tomlConfig = parseTOML(tomlContent) as Record<string, unknown>;
if (!tomlConfig.meta) {
return Response.json(
{
error: "INTERNAL_SERVER_ERROR",
message: "Missing [meta] section in deepgram.toml",
},
{ status: 500, headers: getCorsHeaders() }
);
}
return Response.json(tomlConfig.meta, { headers: getCorsHeaders() });
} catch (error) {
console.error("Error reading metadata:", error);
return Response.json(
{
error: "INTERNAL_SERVER_ERROR",
message: "Failed to read metadata from deepgram.toml",
},
{ status: 500, headers: getCorsHeaders() }
);
}
}
// ============================================================================
// HEALTH CHECK
// ============================================================================
/**
* GET /health
* Returns service health status
*/
function handleHealth(): Response {
return Response.json(
{ status: "ok", service: "text-intelligence" },
{ headers: getCorsHeaders() }
);
}
// ============================================================================
// CORS PREFLIGHT HANDLER
// ============================================================================
/**
* Handle CORS preflight OPTIONS requests
*/
function handlePreflight(): Response {
return new Response(null, {
status: 204,
headers: getCorsHeaders(),
});
}
// ============================================================================
// MAIN REQUEST HANDLER
// ============================================================================
/**
* Main request router — dispatches to individual handlers based on method + path
*/
async function handleRequest(req: Request): Promise<Response> {
const url = new URL(req.url);
// Handle CORS preflight
if (req.method === "OPTIONS") {
return handlePreflight();
}
// Session route (unprotected)
if (req.method === "GET" && url.pathname === "/api/session") {
return await handleGetSession();
}
// Text intelligence route (auth required)
if (req.method === "POST" && url.pathname === "/api/text-intelligence") {
const authError = await checkAuth(req);
if (authError) return authError;
return handleAnalysis(req);
}
// Metadata route (unprotected)
if (req.method === "GET" && url.pathname === "/api/metadata") {
return handleMetadata();
}
// Health check (unprotected)
if (req.method === "GET" && url.pathname === "/health") {
return handleHealth();
}
// 404 for all other routes
return Response.json(
{ error: "Not Found", message: "Endpoint not found" },
{ status: 404, headers: getCorsHeaders() }
);
}
// ============================================================================
// SERVER START
// ============================================================================
console.log("\n" + "=".repeat(70));
console.log(`\uD83D\uDE80 Backend API Server running at http://localhost:${config.port}`);
console.log("");
console.log(`\uD83D\uDCE1 GET /api/session`);
console.log(`\uD83D\uDCE1 POST /api/text-intelligence (auth required)`);
console.log(`\uD83D\uDCE1 GET /api/metadata`);
console.log(`\uD83D\uDCE1 GET /health`);
console.log("=".repeat(70) + "\n");
Bun.serve({
port: config.port,
hostname: config.host,
fetch: handleRequest,
});