@premai/api-sdk 1.0.41 → 1.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.mjs ADDED
@@ -0,0 +1,2959 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire } from "node:module";
3
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
4
+
5
+ // src/cli.ts
6
+ import { parseArgs } from "node:util";
7
+
8
+ // src/server/create-app.ts
9
+ import express from "express";
10
+
11
+ // src/anthropic/http.ts
12
+ import { randomBytes } from "node:crypto";
13
+ var ANTHROPIC_VERSION_DEFAULT = "2023-06-01";
14
+ var ANTHROPIC_VERSION_DATE = /^\d{4}-\d{2}-\d{2}$/;
15
+ function isAnthropicApiVersionSupported(version) {
16
+ if (version === ANTHROPIC_VERSION_DEFAULT) {
17
+ return true;
18
+ }
19
+ return ANTHROPIC_VERSION_DATE.test(version);
20
+ }
21
+ function newAnthropicRequestId() {
22
+ return `req_${randomBytes(12).toString("hex")}`;
23
+ }
24
+ function newAnthropicMessageId() {
25
+ return `msg_${randomBytes(12).toString("hex")}`;
26
+ }
27
+ function extractAnthropicApiKey(req) {
28
+ const raw = req.headers["x-api-key"];
29
+ if (typeof raw === "string" && raw.length > 0) {
30
+ return raw;
31
+ }
32
+ if (Array.isArray(raw) && raw[0]) {
33
+ return raw[0];
34
+ }
35
+ const authHeader = req.headers.authorization;
36
+ if (!authHeader) {
37
+ return null;
38
+ }
39
+ if (authHeader.startsWith("Bearer ")) {
40
+ return authHeader.slice(7);
41
+ }
42
+ return authHeader;
43
+ }
44
+ function getAnthropicVersionHeader(req) {
45
+ const raw = req.headers["anthropic-version"];
46
+ if (typeof raw === "string" && raw.length > 0) {
47
+ return raw;
48
+ }
49
+ if (Array.isArray(raw) && raw[0]) {
50
+ return raw[0];
51
+ }
52
+ return null;
53
+ }
54
+ function resolveAnthropicVersion(req) {
55
+ const header = getAnthropicVersionHeader(req);
56
+ const version = header ?? ANTHROPIC_VERSION_DEFAULT;
57
+ if (!isAnthropicApiVersionSupported(version)) {
58
+ return {
59
+ ok: false,
60
+ message: `Unsupported anthropic-version: ${version}. Expected a dated version (YYYY-MM-DD) or ${ANTHROPIC_VERSION_DEFAULT}.`
61
+ };
62
+ }
63
+ return { ok: true, version };
64
+ }
65
+ function sendAnthropicHttpError(res, status, errorType, message, requestId) {
66
+ res.setHeader("request-id", requestId);
67
+ res.status(status).json({
68
+ type: "error",
69
+ error: { type: errorType, message },
70
+ request_id: requestId
71
+ });
72
+ }
73
+ function httpStatusToAnthropicErrorType(status) {
74
+ if (status === 401) {
75
+ return "authentication_error";
76
+ }
77
+ if (status === 402) {
78
+ return "billing_error";
79
+ }
80
+ if (status === 403) {
81
+ return "permission_error";
82
+ }
83
+ if (status === 404) {
84
+ return "not_found_error";
85
+ }
86
+ if (status === 413) {
87
+ return "request_too_large";
88
+ }
89
+ if (status === 429) {
90
+ return "rate_limit_error";
91
+ }
92
+ if (status === 504) {
93
+ return "timeout_error";
94
+ }
95
+ if (status === 529) {
96
+ return "overloaded_error";
97
+ }
98
+ if (status >= 400 && status < 500) {
99
+ return "invalid_request_error";
100
+ }
101
+ return "api_error";
102
+ }
103
+ function extractErrorMessage(err) {
104
+ if (!err || typeof err !== "object") {
105
+ return null;
106
+ }
107
+ const o = err;
108
+ if (typeof o.message === "string" && o.message.length > 0) {
109
+ return o.message;
110
+ }
111
+ if (typeof o.error === "string" && o.error.length > 0) {
112
+ return o.error;
113
+ }
114
+ if (o.error && typeof o.error === "object") {
115
+ const nested = o.error.message;
116
+ if (typeof nested === "string" && nested.length > 0) {
117
+ return nested;
118
+ }
119
+ }
120
+ return null;
121
+ }
122
+ function looksLikeApiErrorResponse(err) {
123
+ if (!err || typeof err !== "object")
124
+ return false;
125
+ const o = err;
126
+ if (typeof o.status !== "number")
127
+ return false;
128
+ return "error" in o || "message" in o;
129
+ }
130
+ function mapUnknownErrorToAnthropicResponse(err, res, requestId) {
131
+ if (looksLikeApiErrorResponse(err)) {
132
+ const status = err.status >= 400 && err.status < 600 ? err.status : 500;
133
+ const message2 = extractErrorMessage(err) ?? "Request failed";
134
+ const errorType = httpStatusToAnthropicErrorType(status);
135
+ sendAnthropicHttpError(res, status, errorType, message2, requestId);
136
+ return;
137
+ }
138
+ const message = extractErrorMessage(err) ?? (err instanceof Error ? err.message : "Internal server error");
139
+ sendAnthropicHttpError(res, 500, "api_error", message, requestId);
140
+ }
141
+ function writeAnthropicSseEvent(res, event, data) {
142
+ res.write(`event: ${event}
143
+ data: ${JSON.stringify(data)}
144
+
145
+ `);
146
+ }
147
+
148
+ // src/anthropic/to-openai.ts
149
+ class AnthropicRequestValidationError extends Error {
150
+ status = 400;
151
+ anthropicType = "invalid_request_error";
152
+ constructor(message) {
153
+ super(message);
154
+ this.name = "AnthropicRequestValidationError";
155
+ }
156
+ }
157
+ function systemToOpenAiMessages(system) {
158
+ if (typeof system === "string") {
159
+ if (system.length === 0) {
160
+ return [];
161
+ }
162
+ return [{ role: "system", content: system }];
163
+ }
164
+ if (Array.isArray(system)) {
165
+ const parts = [];
166
+ for (const block of system) {
167
+ if (block && block.type === "text" && typeof block.text === "string") {
168
+ parts.push(block.text);
169
+ } else if (block && typeof block === "object") {
170
+ console.warn(`[proxy] system block type "${block.type}" is not supported and will be ignored.`);
171
+ }
172
+ }
173
+ if (parts.length === 0) {
174
+ return [];
175
+ }
176
+ return [{ role: "system", content: parts.join(`
177
+
178
+ `) }];
179
+ }
180
+ if (system.type === "text" && typeof system.text === "string") {
181
+ return [{ role: "system", content: system.text }];
182
+ }
183
+ throw new AnthropicRequestValidationError("Invalid system parameter shape.");
184
+ }
185
+ function toolResultContentToString(content) {
186
+ if (typeof content === "string") {
187
+ return content;
188
+ }
189
+ if (content === null || content === undefined) {
190
+ return "";
191
+ }
192
+ if (Array.isArray(content)) {
193
+ const parts = [];
194
+ for (const block of content) {
195
+ if (block && typeof block === "object" && "type" in block && block.type === "text" && typeof block.text === "string") {
196
+ parts.push(block.text);
197
+ } else {
198
+ parts.push(JSON.stringify(block));
199
+ }
200
+ }
201
+ return parts.join(`
202
+ `);
203
+ }
204
+ return JSON.stringify(content);
205
+ }
206
+ function anthropicImageBlockToOpenAIPart(part) {
207
+ const source = part.source;
208
+ if (!source || typeof source !== "object") {
209
+ return null;
210
+ }
211
+ const s = source;
212
+ if (s.type === "base64" && typeof s.data === "string" && s.data.length > 0) {
213
+ const mediaType = typeof s.media_type === "string" && s.media_type.length > 0 ? s.media_type : "image/png";
214
+ return {
215
+ type: "image_url",
216
+ image_url: { url: `data:${mediaType};base64,${s.data}` }
217
+ };
218
+ }
219
+ if (s.type === "url" && typeof s.url === "string" && s.url.length > 0) {
220
+ return { type: "image_url", image_url: { url: s.url } };
221
+ }
222
+ return null;
223
+ }
224
+ function anthropicUserContentToOpenAIMessages(content) {
225
+ if (typeof content === "string") {
226
+ return [{ role: "user", content }];
227
+ }
228
+ const out = [];
229
+ const partsBuf = [];
230
+ const flushParts = () => {
231
+ if (partsBuf.length === 0) {
232
+ return;
233
+ }
234
+ if (partsBuf.length === 1 && partsBuf[0].type === "text") {
235
+ out.push({ role: "user", content: partsBuf[0].text });
236
+ } else {
237
+ out.push({ role: "user", content: [...partsBuf] });
238
+ }
239
+ partsBuf.length = 0;
240
+ };
241
+ for (const part of content) {
242
+ if (!part || typeof part !== "object") {
243
+ throw new AnthropicRequestValidationError("Invalid message content entry.");
244
+ }
245
+ if (part.type === "text" && typeof part.text === "string") {
246
+ partsBuf.push({
247
+ type: "text",
248
+ text: part.text
249
+ });
250
+ continue;
251
+ }
252
+ if (part.type === "image") {
253
+ const imgPart = anthropicImageBlockToOpenAIPart(part);
254
+ if (imgPart) {
255
+ partsBuf.push(imgPart);
256
+ }
257
+ continue;
258
+ }
259
+ if (part.type === "tool_result") {
260
+ flushParts();
261
+ const id = part.tool_use_id;
262
+ const rawContent = part.content;
263
+ if (typeof id !== "string" || id.length === 0) {
264
+ throw new AnthropicRequestValidationError("tool_result blocks require a non-empty tool_use_id.");
265
+ }
266
+ out.push({
267
+ role: "tool",
268
+ tool_call_id: id,
269
+ content: toolResultContentToString(rawContent)
270
+ });
271
+ }
272
+ }
273
+ flushParts();
274
+ return out;
275
+ }
276
+ function anthropicAssistantContentToOpenAI(content) {
277
+ if (typeof content === "string") {
278
+ return { role: "assistant", content };
279
+ }
280
+ const textParts = [];
281
+ const toolCalls = [];
282
+ for (const part of content) {
283
+ if (!part || typeof part !== "object") {
284
+ throw new AnthropicRequestValidationError("Invalid message content entry.");
285
+ }
286
+ if (part.type === "text" && typeof part.text === "string") {
287
+ textParts.push(part.text);
288
+ continue;
289
+ }
290
+ if (part.type === "tool_use") {
291
+ const p = part;
292
+ if (typeof p.id !== "string" || p.id.length === 0) {
293
+ throw new AnthropicRequestValidationError("tool_use blocks require a non-empty id.");
294
+ }
295
+ if (typeof p.name !== "string" || p.name.length === 0) {
296
+ throw new AnthropicRequestValidationError("tool_use blocks require a non-empty name.");
297
+ }
298
+ const args = typeof p.input === "string" ? p.input : JSON.stringify(p.input ?? {});
299
+ toolCalls.push({
300
+ id: p.id,
301
+ type: "function",
302
+ function: { name: p.name, arguments: args }
303
+ });
304
+ }
305
+ }
306
+ const msg = {
307
+ role: "assistant",
308
+ content: textParts.length > 0 ? textParts.join(`
309
+ `) : null
310
+ };
311
+ if (toolCalls.length > 0) {
312
+ msg.tool_calls = toolCalls;
313
+ }
314
+ return msg;
315
+ }
316
+ function anthropicToolsToOpenAI(tools) {
317
+ if (tools === undefined) {
318
+ return;
319
+ }
320
+ if (!Array.isArray(tools)) {
321
+ throw new AnthropicRequestValidationError("tools must be an array.");
322
+ }
323
+ const out = [];
324
+ for (const t of tools) {
325
+ if (!t || typeof t !== "object") {
326
+ throw new AnthropicRequestValidationError("Invalid tool entry.");
327
+ }
328
+ const name = t.name;
329
+ const desc = t.description;
330
+ const schema = t.input_schema;
331
+ if (typeof name !== "string" || name.length === 0) {
332
+ throw new AnthropicRequestValidationError("Each tool must include a non-empty name.");
333
+ }
334
+ if (schema !== undefined && (typeof schema !== "object" || schema === null)) {
335
+ throw new AnthropicRequestValidationError("tool input_schema must be an object when provided.");
336
+ }
337
+ out.push({
338
+ type: "function",
339
+ function: {
340
+ name,
341
+ ...typeof desc === "string" ? { description: desc } : {},
342
+ parameters: schema ?? {
343
+ type: "object",
344
+ properties: {}
345
+ }
346
+ }
347
+ });
348
+ }
349
+ return out;
350
+ }
351
+ function anthropicToolChoiceToOpenAI(toolChoice) {
352
+ if (toolChoice === undefined) {
353
+ return;
354
+ }
355
+ if (typeof toolChoice !== "object" || toolChoice === null || !("type" in toolChoice)) {
356
+ throw new AnthropicRequestValidationError("Invalid tool_choice shape.");
357
+ }
358
+ const tc = toolChoice;
359
+ switch (tc.type) {
360
+ case "auto":
361
+ return "auto";
362
+ case "none":
363
+ return "none";
364
+ case "any":
365
+ return "required";
366
+ case "tool": {
367
+ if (typeof tc.name !== "string" || tc.name.length === 0) {
368
+ throw new AnthropicRequestValidationError('tool_choice type "tool" requires a non-empty name.');
369
+ }
370
+ return { type: "function", function: { name: tc.name } };
371
+ }
372
+ default:
373
+ throw new AnthropicRequestValidationError(`Unsupported tool_choice type "${tc.type}".`);
374
+ }
375
+ }
376
+ function anthropicMessagesCreateToOpenAI(body) {
377
+ if (typeof body.model !== "string" || !body.model) {
378
+ throw new AnthropicRequestValidationError("model is required.");
379
+ }
380
+ if (typeof body.max_tokens !== "number" || !Number.isFinite(body.max_tokens)) {
381
+ throw new AnthropicRequestValidationError("max_tokens is required and must be a number.");
382
+ }
383
+ if (!Array.isArray(body.messages)) {
384
+ throw new AnthropicRequestValidationError("messages must be an array.");
385
+ }
386
+ const messages = [];
387
+ if (body.system !== undefined) {
388
+ messages.push(...systemToOpenAiMessages(body.system));
389
+ }
390
+ for (const m of body.messages) {
391
+ if (m.role !== "user" && m.role !== "assistant") {
392
+ throw new AnthropicRequestValidationError(`Invalid message role "${m.role}".`);
393
+ }
394
+ if (m.role === "user") {
395
+ messages.push(...anthropicUserContentToOpenAIMessages(m.content));
396
+ } else {
397
+ messages.push(anthropicAssistantContentToOpenAI(m.content));
398
+ }
399
+ }
400
+ const isStreaming = Boolean(body.stream);
401
+ const params = {
402
+ model: body.model,
403
+ messages,
404
+ max_tokens: body.max_tokens,
405
+ stream: isStreaming
406
+ };
407
+ if (isStreaming) {
408
+ params.stream_options = { include_usage: true };
409
+ }
410
+ const tools = anthropicToolsToOpenAI(body.tools);
411
+ if (tools !== undefined && tools.length > 0) {
412
+ params.tools = tools;
413
+ }
414
+ const toolChoice = anthropicToolChoiceToOpenAI(body.tool_choice);
415
+ if (toolChoice !== undefined) {
416
+ params.tool_choice = toolChoice;
417
+ }
418
+ if (body.stop_sequences !== undefined) {
419
+ if (!Array.isArray(body.stop_sequences) || !body.stop_sequences.every((s) => typeof s === "string")) {
420
+ throw new AnthropicRequestValidationError("stop_sequences must be an array of strings.");
421
+ }
422
+ params.stop = body.stop_sequences;
423
+ }
424
+ if (typeof body.temperature === "number") {
425
+ params.temperature = body.temperature;
426
+ }
427
+ if (typeof body.top_p === "number") {
428
+ params.top_p = body.top_p;
429
+ }
430
+ if (typeof body.top_k === "number") {
431
+ console.warn("[proxy] top_k is not supported by the OpenAI API and will be ignored.");
432
+ }
433
+ return params;
434
+ }
435
+
436
+ // src/anthropic/count-tokens-route.ts
437
+ function extractTextCharCount(body) {
438
+ let len = 0;
439
+ if (typeof body.system === "string") {
440
+ len += body.system.length;
441
+ } else if (Array.isArray(body.system)) {
442
+ for (const block of body.system) {
443
+ if (block && block.type === "text" && typeof block.text === "string") {
444
+ len += block.text.length;
445
+ }
446
+ }
447
+ } else if (body.system && typeof body.system === "object" && body.system.type === "text") {
448
+ len += body.system.text.length;
449
+ }
450
+ for (const msg of body.messages) {
451
+ if (typeof msg.content === "string") {
452
+ len += msg.content.length;
453
+ } else if (Array.isArray(msg.content)) {
454
+ for (const part of msg.content) {
455
+ if (!part || typeof part !== "object")
456
+ continue;
457
+ if (part.type === "text" && typeof part.text === "string") {
458
+ len += part.text.length;
459
+ } else if (part.type === "tool_result") {
460
+ const c = part.content;
461
+ if (typeof c === "string") {
462
+ len += c.length;
463
+ }
464
+ }
465
+ }
466
+ }
467
+ }
468
+ if (Array.isArray(body.tools)) {
469
+ len += JSON.stringify(body.tools).length;
470
+ }
471
+ return len;
472
+ }
473
+ function registerAnthropicCountTokensRoute(router, _deps) {
474
+ router.post("/v1/messages/count_tokens", async (req, res) => {
475
+ const requestId = newAnthropicRequestId();
476
+ res.setHeader("request-id", requestId);
477
+ const versionResult = resolveAnthropicVersion(req);
478
+ if (!versionResult.ok) {
479
+ return sendAnthropicHttpError(res, 400, "invalid_request_error", versionResult.message, requestId);
480
+ }
481
+ const apiKey = extractAnthropicApiKey(req);
482
+ if (!apiKey) {
483
+ return sendAnthropicHttpError(res, 401, "authentication_error", "Missing x-api-key header (or Authorization with API key).", requestId);
484
+ }
485
+ try {
486
+ const raw = req.body;
487
+ const body = {
488
+ ...raw,
489
+ max_tokens: typeof raw.max_tokens === "number" && Number.isFinite(raw.max_tokens) ? raw.max_tokens : 4096,
490
+ stream: false
491
+ };
492
+ anthropicMessagesCreateToOpenAI(body);
493
+ const input_tokens = Math.max(1, Math.ceil(extractTextCharCount(body) / 4));
494
+ res.json({ input_tokens });
495
+ } catch (err) {
496
+ if (err instanceof AnthropicRequestValidationError) {
497
+ return sendAnthropicHttpError(res, err.status, err.anthropicType, err.message, requestId);
498
+ }
499
+ mapUnknownErrorToAnthropicResponse(err, res, requestId);
500
+ }
501
+ });
502
+ }
503
+
504
+ // src/anthropic/from-openai.ts
505
+ function openAiFinishReasonToAnthropic(finish) {
506
+ if (!finish) {
507
+ return { stop_reason: null, stop_sequence: null };
508
+ }
509
+ switch (finish) {
510
+ case "stop":
511
+ return { stop_reason: "end_turn", stop_sequence: null };
512
+ case "length":
513
+ return { stop_reason: "max_tokens", stop_sequence: null };
514
+ case "tool_calls":
515
+ return { stop_reason: "tool_use", stop_sequence: null };
516
+ case "content_filter":
517
+ return { stop_reason: "refusal", stop_sequence: null };
518
+ default:
519
+ return { stop_reason: "end_turn", stop_sequence: null };
520
+ }
521
+ }
522
+ function extractTextFromAssistantContent(content) {
523
+ if (content == null) {
524
+ return "";
525
+ }
526
+ if (typeof content === "string") {
527
+ return content;
528
+ }
529
+ if (!Array.isArray(content)) {
530
+ return "";
531
+ }
532
+ const parts = [];
533
+ for (const p of content) {
534
+ if (typeof p === "string") {
535
+ parts.push(p);
536
+ continue;
537
+ }
538
+ if (p && typeof p === "object" && "type" in p && p.type === "text" && "text" in p) {
539
+ parts.push(String(p.text));
540
+ }
541
+ }
542
+ return parts.join("");
543
+ }
544
+ function openAIChatCompletionToAnthropicMessage(completion, requestModel) {
545
+ const choice = completion.choices[0];
546
+ const message = choice?.message;
547
+ const contentText = message ? extractTextFromAssistantContent(message.content) : "";
548
+ const content = [];
549
+ if (contentText.length > 0) {
550
+ content.push({ type: "text", text: contentText });
551
+ }
552
+ if (message?.tool_calls?.length) {
553
+ for (const tc of message.tool_calls) {
554
+ if (tc.type !== "function") {
555
+ continue;
556
+ }
557
+ let input = {};
558
+ try {
559
+ input = JSON.parse(tc.function.arguments || "{}");
560
+ } catch {
561
+ input = { _raw_arguments: tc.function.arguments ?? "" };
562
+ }
563
+ content.push({
564
+ type: "tool_use",
565
+ id: tc.id,
566
+ name: tc.function.name,
567
+ input
568
+ });
569
+ }
570
+ }
571
+ if (content.length === 0) {
572
+ content.push({ type: "text", text: "" });
573
+ }
574
+ const { stop_reason, stop_sequence } = openAiFinishReasonToAnthropic(choice?.finish_reason);
575
+ const u = completion.usage;
576
+ const usage = {
577
+ input_tokens: u?.prompt_tokens ?? 0,
578
+ output_tokens: u?.completion_tokens ?? 0
579
+ };
580
+ return {
581
+ id: newAnthropicMessageId(),
582
+ type: "message",
583
+ role: "assistant",
584
+ content,
585
+ model: requestModel,
586
+ stop_reason,
587
+ stop_sequence,
588
+ usage
589
+ };
590
+ }
591
+ function chunkFinishToAnthropic(finish) {
592
+ if (!finish) {
593
+ return null;
594
+ }
595
+ return openAiFinishReasonToAnthropic(finish).stop_reason;
596
+ }
597
+ async function pipeOpenAIChunkStreamToAnthropicSse(res, stream, options) {
598
+ const { anthropicModel, messageId } = options;
599
+ let textBlockOpen = false;
600
+ let inputTokens = 0;
601
+ let outputTokens = 0;
602
+ let stopReason = null;
603
+ const toolStates = new Map;
604
+ let nextAnthropicIndex = 0;
605
+ let textBlockIndex = null;
606
+ writeAnthropicSseEvent(res, "message_start", {
607
+ type: "message_start",
608
+ message: {
609
+ id: messageId,
610
+ type: "message",
611
+ role: "assistant",
612
+ content: [],
613
+ model: anthropicModel,
614
+ stop_reason: null,
615
+ stop_sequence: null,
616
+ usage: { input_tokens: inputTokens, output_tokens: outputTokens }
617
+ }
618
+ });
619
+ const ensureTextBlock = () => {
620
+ if (textBlockOpen) {
621
+ return;
622
+ }
623
+ textBlockIndex = nextAnthropicIndex++;
624
+ textBlockOpen = true;
625
+ writeAnthropicSseEvent(res, "content_block_start", {
626
+ type: "content_block_start",
627
+ index: textBlockIndex,
628
+ content_block: { type: "text", text: "" }
629
+ });
630
+ };
631
+ const closeTextBlockIfOpen = () => {
632
+ if (!textBlockOpen || textBlockIndex === null) {
633
+ return;
634
+ }
635
+ writeAnthropicSseEvent(res, "content_block_stop", {
636
+ type: "content_block_stop",
637
+ index: textBlockIndex
638
+ });
639
+ textBlockOpen = false;
640
+ };
641
+ const getOrCreateTool = (openAiIdx) => {
642
+ let st = toolStates.get(openAiIdx);
643
+ if (!st) {
644
+ st = {
645
+ anthropicIndex: nextAnthropicIndex++,
646
+ id: "",
647
+ name: "",
648
+ lastArgs: "",
649
+ argsEmittedLen: 0,
650
+ started: false,
651
+ stopped: false
652
+ };
653
+ toolStates.set(openAiIdx, st);
654
+ }
655
+ return st;
656
+ };
657
+ const flushToolArgs = (st) => {
658
+ if (!st.started || st.lastArgs.length <= st.argsEmittedLen) {
659
+ return;
660
+ }
661
+ const partial = st.lastArgs.slice(st.argsEmittedLen);
662
+ st.argsEmittedLen = st.lastArgs.length;
663
+ writeAnthropicSseEvent(res, "content_block_delta", {
664
+ type: "content_block_delta",
665
+ index: st.anthropicIndex,
666
+ delta: {
667
+ type: "input_json_delta",
668
+ partial_json: partial
669
+ }
670
+ });
671
+ };
672
+ try {
673
+ for await (const chunk of stream) {
674
+ if (chunk.usage) {
675
+ const u = chunk.usage;
676
+ inputTokens = u.prompt_tokens ?? inputTokens;
677
+ outputTokens = u.completion_tokens ?? outputTokens;
678
+ }
679
+ const choice = chunk.choices?.[0];
680
+ if (!choice) {
681
+ continue;
682
+ }
683
+ const delta = choice.delta;
684
+ if (typeof delta?.content === "string" && delta.content.length > 0) {
685
+ ensureTextBlock();
686
+ if (textBlockIndex !== null) {
687
+ writeAnthropicSseEvent(res, "content_block_delta", {
688
+ type: "content_block_delta",
689
+ index: textBlockIndex,
690
+ delta: { type: "text_delta", text: delta.content }
691
+ });
692
+ }
693
+ }
694
+ if (delta?.tool_calls?.length) {
695
+ closeTextBlockIfOpen();
696
+ for (const tc of delta.tool_calls) {
697
+ const idx = typeof tc.index === "number" && Number.isFinite(tc.index) ? tc.index : 0;
698
+ const st = getOrCreateTool(idx);
699
+ if (typeof tc.id === "string" && tc.id.length > 0) {
700
+ st.id = tc.id;
701
+ }
702
+ const fn = tc.function;
703
+ if (fn?.name && fn.name.length > 0) {
704
+ st.name = fn.name;
705
+ }
706
+ if (typeof fn?.arguments === "string") {
707
+ st.lastArgs += fn.arguments;
708
+ }
709
+ if (!st.started && st.id.length > 0 && st.name.length > 0) {
710
+ writeAnthropicSseEvent(res, "content_block_start", {
711
+ type: "content_block_start",
712
+ index: st.anthropicIndex,
713
+ content_block: {
714
+ type: "tool_use",
715
+ id: st.id,
716
+ name: st.name
717
+ }
718
+ });
719
+ st.started = true;
720
+ }
721
+ if (st.started) {
722
+ flushToolArgs(st);
723
+ }
724
+ }
725
+ }
726
+ if (choice.finish_reason) {
727
+ const mapped = chunkFinishToAnthropic(choice.finish_reason);
728
+ if (mapped) {
729
+ stopReason = mapped;
730
+ }
731
+ }
732
+ }
733
+ closeTextBlockIfOpen();
734
+ const sortedTools = [...toolStates.values()].sort((a, b) => a.anthropicIndex - b.anthropicIndex);
735
+ for (const st of sortedTools) {
736
+ if (st.started && !st.stopped) {
737
+ writeAnthropicSseEvent(res, "content_block_stop", {
738
+ type: "content_block_stop",
739
+ index: st.anthropicIndex
740
+ });
741
+ st.stopped = true;
742
+ }
743
+ }
744
+ writeAnthropicSseEvent(res, "message_delta", {
745
+ type: "message_delta",
746
+ delta: { stop_reason: stopReason, stop_sequence: null },
747
+ usage: {
748
+ input_tokens: inputTokens,
749
+ output_tokens: outputTokens
750
+ }
751
+ });
752
+ writeAnthropicSseEvent(res, "message_stop", { type: "message_stop" });
753
+ res.end();
754
+ } catch (err) {
755
+ const message = err instanceof Error ? err.message : "Stream error";
756
+ writeAnthropicSseEvent(res, "error", {
757
+ type: "error",
758
+ error: { type: "api_error", message }
759
+ });
760
+ res.end();
761
+ }
762
+ }
763
+
764
+ // src/anthropic/messages-route.ts
765
+ function registerAnthropicMessagesRoute(router, deps) {
766
+ router.post("/v1/messages", async (req, res) => {
767
+ const requestId = newAnthropicRequestId();
768
+ res.setHeader("request-id", requestId);
769
+ const versionResult = resolveAnthropicVersion(req);
770
+ if (!versionResult.ok) {
771
+ return sendAnthropicHttpError(res, 400, "invalid_request_error", versionResult.message, requestId);
772
+ }
773
+ const apiKey = extractAnthropicApiKey(req);
774
+ if (!apiKey) {
775
+ return sendAnthropicHttpError(res, 401, "authentication_error", "Missing x-api-key header (or Authorization with API key).", requestId);
776
+ }
777
+ try {
778
+ const body = req.body;
779
+ const openaiParams = anthropicMessagesCreateToOpenAI(body);
780
+ const client = await deps.getOrCreateClient(apiKey);
781
+ const completion = await client.chat.completions.create(openaiParams);
782
+ if (body.stream) {
783
+ res.status(200);
784
+ res.setHeader("Content-Type", "text/event-stream; charset=utf-8");
785
+ res.setHeader("Cache-Control", "no-cache");
786
+ res.setHeader("Connection", "keep-alive");
787
+ if (completion && typeof completion === "object" && Symbol.asyncIterator in completion) {
788
+ const messageId = newAnthropicMessageId();
789
+ await pipeOpenAIChunkStreamToAnthropicSse(res, completion, {
790
+ anthropicModel: body.model,
791
+ messageId
792
+ });
793
+ } else {
794
+ sendAnthropicHttpError(res, 500, "api_error", "Expected streamed completion", requestId);
795
+ }
796
+ return;
797
+ }
798
+ const message = openAIChatCompletionToAnthropicMessage(completion, body.model);
799
+ res.json(message);
800
+ } catch (err) {
801
+ if (err instanceof AnthropicRequestValidationError) {
802
+ return sendAnthropicHttpError(res, err.status, err.anthropicType, err.message, requestId);
803
+ }
804
+ mapUnknownErrorToAnthropicResponse(err, res, requestId);
805
+ }
806
+ });
807
+ }
808
+
809
+ // src/anthropic/models-route.ts
810
+ function toAnthropicModel(model) {
811
+ return {
812
+ type: "model",
813
+ id: model.model,
814
+ display_name: model.name || model.model,
815
+ created_at: model.created_at
816
+ };
817
+ }
818
+ function filterEnabled(models) {
819
+ return models.filter((m) => m.enabled !== 0);
820
+ }
821
+ function parseLimit(raw) {
822
+ if (typeof raw !== "string" || raw.length === 0) {
823
+ return 20;
824
+ }
825
+ const n = Number.parseInt(raw, 10);
826
+ if (!Number.isFinite(n) || n <= 0) {
827
+ return 20;
828
+ }
829
+ return Math.min(n, 1000);
830
+ }
831
+ function paginate(all, beforeId, afterId, limit) {
832
+ let start = 0;
833
+ let end = all.length;
834
+ if (afterId) {
835
+ const idx = all.findIndex((m) => m.id === afterId);
836
+ if (idx >= 0) {
837
+ start = idx + 1;
838
+ }
839
+ }
840
+ if (beforeId) {
841
+ const idx = all.findIndex((m) => m.id === beforeId);
842
+ if (idx >= 0) {
843
+ end = idx;
844
+ }
845
+ }
846
+ const window = all.slice(start, end);
847
+ const items = window.slice(0, limit);
848
+ return { items, hasMore: window.length > items.length };
849
+ }
850
+ function registerAnthropicModelsRoute(router, deps) {
851
+ router.get("/v1/models", async (req, res) => {
852
+ const requestId = newAnthropicRequestId();
853
+ res.setHeader("request-id", requestId);
854
+ const versionResult = resolveAnthropicVersion(req);
855
+ if (!versionResult.ok) {
856
+ return sendAnthropicHttpError(res, 400, "invalid_request_error", versionResult.message, requestId);
857
+ }
858
+ const apiKey = extractAnthropicApiKey(req);
859
+ if (!apiKey) {
860
+ return sendAnthropicHttpError(res, 401, "authentication_error", "Missing x-api-key header (or Authorization with API key).", requestId);
861
+ }
862
+ try {
863
+ const client = await deps.getOrCreateClient(apiKey);
864
+ const type = typeof req.query.type === "string" ? req.query.type : undefined;
865
+ const all = filterEnabled(await client.models.list({ type })).map(toAnthropicModel);
866
+ const beforeId = typeof req.query.before_id === "string" ? req.query.before_id : undefined;
867
+ const afterId = typeof req.query.after_id === "string" ? req.query.after_id : undefined;
868
+ const limit = parseLimit(req.query.limit);
869
+ const { items, hasMore } = paginate(all, beforeId, afterId, limit);
870
+ res.json({
871
+ data: items,
872
+ first_id: items.length > 0 ? items[0].id : null,
873
+ last_id: items.length > 0 ? items[items.length - 1].id : null,
874
+ has_more: hasMore
875
+ });
876
+ } catch (err) {
877
+ mapUnknownErrorToAnthropicResponse(err, res, requestId);
878
+ }
879
+ });
880
+ router.get("/v1/models/:model_id", async (req, res) => {
881
+ const requestId = newAnthropicRequestId();
882
+ res.setHeader("request-id", requestId);
883
+ const versionResult = resolveAnthropicVersion(req);
884
+ if (!versionResult.ok) {
885
+ return sendAnthropicHttpError(res, 400, "invalid_request_error", versionResult.message, requestId);
886
+ }
887
+ const apiKey = extractAnthropicApiKey(req);
888
+ if (!apiKey) {
889
+ return sendAnthropicHttpError(res, 401, "authentication_error", "Missing x-api-key header (or Authorization with API key).", requestId);
890
+ }
891
+ const modelId = req.params.model_id;
892
+ if (!modelId) {
893
+ return sendAnthropicHttpError(res, 400, "invalid_request_error", "Missing model id.", requestId);
894
+ }
895
+ try {
896
+ const client = await deps.getOrCreateClient(apiKey);
897
+ const found = filterEnabled(await client.models.list()).find((m) => m.model === modelId);
898
+ if (!found) {
899
+ return sendAnthropicHttpError(res, 404, "not_found_error", `Model "${modelId}" not found.`, requestId);
900
+ }
901
+ res.json(toAnthropicModel(found));
902
+ } catch (err) {
903
+ mapUnknownErrorToAnthropicResponse(err, res, requestId);
904
+ }
905
+ });
906
+ }
907
+
908
+ // src/server/runtime.ts
909
+ import multer from "multer";
910
+
911
+ // src/audio/index.ts
912
+ import { bytesToHex as bytesToHex2, hexToBytes as hexToBytes2 } from "@noble/ciphers/utils.js";
913
+
914
+ // src/config.ts
915
+ var endpoints = {
916
+ enclave: process.env.ENCLAVE_URL,
917
+ proxy: process.env.PROXY_URL
918
+ };
919
+ var DEFAULT_REQUEST_TIMEOUT_MS = 600000;
920
+ var DEFAULT_MAX_BUFFER_SIZE = 10 * 1024 * 1024;
921
+
922
+ // src/utils/attestation.ts
923
+ var cachedPrem;
924
+ async function loadPrem() {
925
+ if (cachedPrem)
926
+ return cachedPrem;
927
+ const isBare = typeof globalThis.Bare !== "undefined";
928
+ if (isBare) {
929
+ cachedPrem = await (async (s, y) => await import(s, y))("@premai/reticle", { with: { type: "script" } });
930
+ return cachedPrem;
931
+ }
932
+ cachedPrem = await import("@premai/reticle");
933
+ return cachedPrem;
934
+ }
935
+ function isAttestationError(err) {
936
+ return err instanceof Error && err.name === "AttestationError";
937
+ }
938
+ var ATTEST_TTL_MS = 30000;
939
+ var ATTEST_CACHE_MAX = 500;
940
+ var ATTEST_MAX_ATTEMPTS = 4;
941
+ var ATTEST_RETRY_BASE_MS = 250;
942
+ var ATTEST_RETRY_MAX_MS = 2000;
943
+ var TRANSIENT_PATTERNS = [
944
+ /EOF while parsing/i,
945
+ /error decoding response body/i,
946
+ /connection (reset|closed|refused)/i,
947
+ /socket hang up/i,
948
+ /ETIMEDOUT/i
949
+ ];
950
+ var attestCache = new Map;
951
+ var attestInflight = new Map;
952
+ function attestCacheKey(apiKey, model) {
953
+ return `${apiKey}|${model ?? ""}`;
954
+ }
955
+ function pruneExpired(now) {
956
+ for (const [key, entry] of attestCache) {
957
+ if (entry.expires <= now) {
958
+ attestCache.delete(key);
959
+ } else {
960
+ break;
961
+ }
962
+ }
963
+ }
964
+ function isTransientError(err) {
965
+ const messages = [];
966
+ if (err instanceof Error) {
967
+ messages.push(err.message);
968
+ }
969
+ if (isAttestationError(err) && Array.isArray(err.cause)) {
970
+ messages.push(...err.cause);
971
+ }
972
+ return messages.some((m) => TRANSIENT_PATTERNS.some((re) => re.test(m)));
973
+ }
974
+ function backoffDelayMs(attempt) {
975
+ const exp = ATTEST_RETRY_BASE_MS * 2 ** (attempt - 1);
976
+ const capped = Math.min(exp, ATTEST_RETRY_MAX_MS);
977
+ const jitter = Math.floor(Math.random() * (capped / 2));
978
+ return capped + jitter;
979
+ }
980
+ function delay(ms) {
981
+ return new Promise((resolve) => setTimeout(resolve, ms));
982
+ }
983
+ function safeFree(obj) {
984
+ if (typeof obj?.free !== "function")
985
+ return;
986
+ try {
987
+ obj.free();
988
+ } catch {}
989
+ }
990
+ async function attemptAttest(apiKey, options) {
991
+ const prem = await loadPrem();
992
+ let client;
993
+ let attested;
994
+ let headers;
995
+ let sessionId;
996
+ try {
997
+ client = await new prem.ClientBuilder(endpoints.proxy ?? "").with_authorization(apiKey).build();
998
+ if (options.model) {
999
+ client.set_query(new prem.QueryParams().with("model", options.model));
1000
+ }
1001
+ attested = await client.attest();
1002
+ headers = attested.headers();
1003
+ sessionId = headers.cpu()?.get("x-session-id") ?? headers.gpu()?.get("x-session-id") ?? null;
1004
+ } finally {
1005
+ safeFree(headers);
1006
+ safeFree(attested);
1007
+ safeFree(client);
1008
+ }
1009
+ if (sessionId === null) {
1010
+ throw new Error("missing x-session-id issued by attestation");
1011
+ }
1012
+ return sessionId;
1013
+ }
1014
+ async function runAttest(apiKey, options) {
1015
+ let lastErr;
1016
+ for (let attempt = 1;attempt <= ATTEST_MAX_ATTEMPTS; attempt++) {
1017
+ try {
1018
+ return await attemptAttest(apiKey, options);
1019
+ } catch (err) {
1020
+ lastErr = err;
1021
+ if (attempt === ATTEST_MAX_ATTEMPTS || !isTransientError(err)) {
1022
+ throw err;
1023
+ }
1024
+ await delay(backoffDelayMs(attempt));
1025
+ }
1026
+ }
1027
+ throw lastErr;
1028
+ }
1029
+ async function attest(apiKey, options = { enabled: true }) {
1030
+ if (!options.enabled)
1031
+ return null;
1032
+ const key = attestCacheKey(apiKey, options.model);
1033
+ const now = Date.now();
1034
+ const cached = attestCache.get(key);
1035
+ if (cached) {
1036
+ if (cached.expires > now)
1037
+ return cached.sessionId;
1038
+ attestCache.delete(key);
1039
+ }
1040
+ const inflight = attestInflight.get(key);
1041
+ if (inflight) {
1042
+ return inflight;
1043
+ }
1044
+ const work = runAttest(apiKey, options).then((sessionId) => {
1045
+ const insertTime = Date.now();
1046
+ pruneExpired(insertTime);
1047
+ attestCache.set(key, { sessionId, expires: insertTime + ATTEST_TTL_MS });
1048
+ if (attestCache.size > ATTEST_CACHE_MAX) {
1049
+ const oldest = attestCache.keys().next().value;
1050
+ if (oldest)
1051
+ attestCache.delete(oldest);
1052
+ }
1053
+ return sessionId;
1054
+ }).finally(() => {
1055
+ attestInflight.delete(key);
1056
+ });
1057
+ attestInflight.set(key, work);
1058
+ return work;
1059
+ }
1060
+
1061
+ // src/utils/crypto.ts
1062
+ import { aeskwp } from "@noble/ciphers/aes.js";
1063
+ import { xchacha20poly1305 } from "@noble/ciphers/chacha.js";
1064
+ import { bytesToHex, hexToBytes, managedNonce, randomBytes as randomBytes2 } from "@noble/ciphers/utils.js";
1065
+ import { sha256 } from "@noble/hashes/sha2.js";
1066
+ import { sha3_256 } from "@noble/hashes/sha3.js";
1067
+ import { XWing } from "@noble/post-quantum/hybrid.js";
1068
+ function createMLKEMEncapsulation(publicKeyHex) {
1069
+ return XWing.encapsulate(hexToBytes(publicKeyHex));
1070
+ }
1071
+ function encryptPayload(sharedSecret, data) {
1072
+ const nonce = randomBytes2(24);
1073
+ const chacha = xchacha20poly1305(sharedSecret, nonce);
1074
+ let encodedData;
1075
+ if (data instanceof Uint8Array) {
1076
+ encodedData = data;
1077
+ } else if (typeof data === "string") {
1078
+ encodedData = new TextEncoder().encode(data);
1079
+ } else {
1080
+ encodedData = new TextEncoder().encode(JSON.stringify(data));
1081
+ }
1082
+ const encrypted = chacha.encrypt(encodedData);
1083
+ return { encrypted, nonce };
1084
+ }
1085
+ function decryptPayload(encryptedData, sharedSecret, nonce) {
1086
+ const chacha = xchacha20poly1305(sharedSecret, nonce);
1087
+ const encrypted = hexToBytes(encryptedData);
1088
+ const decrypted = chacha.decrypt(encrypted);
1089
+ const str = new TextDecoder().decode(decrypted);
1090
+ try {
1091
+ return JSON.parse(str);
1092
+ } catch {
1093
+ return str;
1094
+ }
1095
+ }
1096
+ async function getEnclavePublicKey(timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1097
+ const controller = new AbortController;
1098
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
1099
+ try {
1100
+ const response = await fetch(`${endpoints.enclave}/publicKey`, {
1101
+ signal: controller.signal
1102
+ });
1103
+ if (!response.ok) {
1104
+ throw new Error(`Failed to fetch enclave public key: ${response.status} ${response.statusText}`);
1105
+ }
1106
+ const data = await response.json();
1107
+ if (!data.publicKey || typeof data.publicKey !== "string") {
1108
+ throw new Error("Invalid public key response from enclave");
1109
+ }
1110
+ return data.publicKey;
1111
+ } catch (error) {
1112
+ if (error instanceof Error && error.name === "AbortError") {
1113
+ throw new Error(`Enclave public key request timed out after ${timeoutMs}ms`);
1114
+ }
1115
+ throw new Error(`Failed to get enclave public key: ${error instanceof Error ? error.message : error}`);
1116
+ } finally {
1117
+ clearTimeout(timeoutId);
1118
+ }
1119
+ }
1120
+ async function generateEncryptionKeys(timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1121
+ const enclavePublicKey = await getEnclavePublicKey(timeoutMs);
1122
+ return createMLKEMEncapsulation(enclavePublicKey);
1123
+ }
1124
+ function keyIdFromKEK(kek, context = "kek:v1", length = 16) {
1125
+ const ctx = new TextEncoder().encode(context);
1126
+ const input = new Uint8Array(kek.length + ctx.length);
1127
+ input.set(kek, 0);
1128
+ input.set(ctx, kek.length);
1129
+ const digest = sha256(input);
1130
+ return digest.slice(0, length);
1131
+ }
1132
+ function encryptWithDEK(dek, plaintext) {
1133
+ const aead = managedNonce(xchacha20poly1305)(dek);
1134
+ return aead.encrypt(plaintext);
1135
+ }
1136
+ function encryptMetadataWithDEK(dek, metadata) {
1137
+ const encoded = new TextEncoder().encode(metadata);
1138
+ const encrypted = encryptWithDEK(dek, encoded);
1139
+ return bytesToHex(encrypted);
1140
+ }
1141
+ function wrapDEK(kek, dek) {
1142
+ const kw = aeskwp(kek);
1143
+ return kw.encrypt(dek);
1144
+ }
1145
+ function unwrapDEK(kek, wrappedDEK) {
1146
+ const kw = aeskwp(kek);
1147
+ return kw.decrypt(wrappedDEK);
1148
+ }
1149
+ function decryptWithDEK(dek, encryptedContent) {
1150
+ const aead = managedNonce(xchacha20poly1305)(dek);
1151
+ return aead.decrypt(encryptedContent);
1152
+ }
1153
+
1154
+ // src/utils/error.ts
1155
+ async function throwIfErrorResponse(response) {
1156
+ let raw;
1157
+ try {
1158
+ raw = await response.json();
1159
+ if (!raw.status)
1160
+ raw = { ...raw, status: response.status };
1161
+ } catch {
1162
+ raw = {
1163
+ status: response.status,
1164
+ data: null,
1165
+ error: response.statusText || `HTTP ${response.status}`,
1166
+ message: null
1167
+ };
1168
+ }
1169
+ throw raw;
1170
+ }
1171
+
1172
+ // src/utils/files.ts
1173
+ var getFileName = (file) => {
1174
+ if (file instanceof File) {
1175
+ return file.name;
1176
+ }
1177
+ if (file instanceof Blob) {
1178
+ return;
1179
+ }
1180
+ const fileAny = file;
1181
+ if (fileAny.path) {
1182
+ const path = typeof fileAny.path === "string" ? fileAny.path : fileAny.path.toString();
1183
+ return path.split("/").pop() || path.split("\\").pop() || path;
1184
+ }
1185
+ if (file instanceof Uint8Array || file instanceof ArrayBuffer) {
1186
+ return;
1187
+ }
1188
+ return;
1189
+ };
1190
+
1191
+ // src/audio/index.ts
1192
+ async function readUploadableToUint8Array(file) {
1193
+ if (file instanceof Uint8Array) {
1194
+ return file;
1195
+ }
1196
+ if (file instanceof ArrayBuffer) {
1197
+ return new Uint8Array(file);
1198
+ }
1199
+ if (typeof file.arrayBuffer === "function") {
1200
+ const blob = file;
1201
+ const buffer = await blob.arrayBuffer();
1202
+ return new Uint8Array(buffer);
1203
+ }
1204
+ const fileAny = file;
1205
+ if (typeof fileAny.on === "function" && (typeof fileAny.read === "function" || typeof fileAny.pipe === "function")) {
1206
+ const chunks = [];
1207
+ return new Promise((resolve, reject) => {
1208
+ fileAny.on("data", (chunk) => {
1209
+ if (Buffer.isBuffer(chunk)) {
1210
+ chunks.push(new Uint8Array(chunk));
1211
+ } else if (chunk instanceof Uint8Array) {
1212
+ chunks.push(chunk);
1213
+ } else if (typeof chunk === "object" && chunk !== null) {
1214
+ chunks.push(new Uint8Array(Buffer.from(chunk)));
1215
+ }
1216
+ });
1217
+ fileAny.on("end", () => {
1218
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
1219
+ const result = new Uint8Array(totalLength);
1220
+ let offset = 0;
1221
+ for (const chunk of chunks) {
1222
+ result.set(chunk, offset);
1223
+ offset += chunk.length;
1224
+ }
1225
+ resolve(result);
1226
+ });
1227
+ fileAny.on("error", (err) => reject(err));
1228
+ });
1229
+ }
1230
+ throw new Error("Unsupported file type for audio transcription");
1231
+ }
1232
+ async function preprocessAudioRequest(body, encryptionKeys) {
1233
+ const { cipherText, sharedSecret } = encryptionKeys;
1234
+ const audioData = await readUploadableToUint8Array(body.file);
1235
+ const isDeepgram = body.model.startsWith("deepgram/");
1236
+ const requestBody = isDeepgram ? {
1237
+ model: body.model,
1238
+ diarize: body.diarize,
1239
+ smart_format: body.smart_format
1240
+ } : {
1241
+ model: body.model,
1242
+ language: body.language,
1243
+ prompt: body.prompt,
1244
+ response_format: body.response_format,
1245
+ temperature: body.temperature,
1246
+ timestamp_granularities: body.timestamp_granularities
1247
+ };
1248
+ const cleanedBody = Object.fromEntries(Object.entries(requestBody).filter(([_, v]) => v !== undefined));
1249
+ const { encrypted, nonce } = encryptPayload(sharedSecret, cleanedBody);
1250
+ const { encrypted: encryptedFile, nonce: fileNonce } = encryptPayload(sharedSecret, audioData);
1251
+ const fileName = getFileName(body.file) || "audio.mp3";
1252
+ const { encrypted: encryptedFileName, nonce: fileNameNonce } = encryptPayload(sharedSecret, fileName);
1253
+ return {
1254
+ body: {
1255
+ cipherText: bytesToHex2(cipherText),
1256
+ encryptedInference: bytesToHex2(encrypted),
1257
+ nonce: bytesToHex2(nonce),
1258
+ fileNameNonce: bytesToHex2(fileNameNonce),
1259
+ encryptedFileName: bytesToHex2(encryptedFileName),
1260
+ fileNonce: bytesToHex2(fileNonce),
1261
+ encryptedFile: bytesToHex2(encryptedFile),
1262
+ model: body.model
1263
+ },
1264
+ sharedSecret
1265
+ };
1266
+ }
1267
+ async function postprocessTranscriptionResponse(response, sharedSecret) {
1268
+ const responseData = await response.json();
1269
+ const data = responseData.data;
1270
+ if (!data.encryptedResponse || !data.nonce) {
1271
+ throw new Error("Invalid transcription response: missing encryptedResponse or nonce");
1272
+ }
1273
+ const responseNonce = hexToBytes2(data.nonce);
1274
+ return decryptPayload(data.encryptedResponse, sharedSecret, responseNonce);
1275
+ }
1276
+ async function postprocessTranslationResponse(response, sharedSecret) {
1277
+ const responseData = await response.json();
1278
+ const data = responseData.data;
1279
+ if (!data.encryptedResponse || !data.nonce) {
1280
+ throw new Error("Invalid translation response: missing encryptedResponse or nonce");
1281
+ }
1282
+ const responseNonce = hexToBytes2(data.nonce);
1283
+ return decryptPayload(data.encryptedResponse, sharedSecret, responseNonce);
1284
+ }
1285
+ async function preprocessAudioTranslationRequest(body, encryptionKeys) {
1286
+ const { cipherText, sharedSecret } = encryptionKeys;
1287
+ const audioData = await readUploadableToUint8Array(body.file);
1288
+ const requestBody = {
1289
+ model: body.model,
1290
+ prompt: body.prompt,
1291
+ response_format: body.response_format,
1292
+ temperature: body.temperature
1293
+ };
1294
+ const cleanedBody = Object.fromEntries(Object.entries(requestBody).filter(([_, v]) => v !== undefined));
1295
+ const { encrypted, nonce } = encryptPayload(sharedSecret, cleanedBody);
1296
+ const { encrypted: encryptedFile, nonce: fileNonce } = encryptPayload(sharedSecret, audioData);
1297
+ const fileName = getFileName(body.file) || "audio.mp3";
1298
+ const { encrypted: encryptedFileName, nonce: fileNameNonce } = encryptPayload(sharedSecret, fileName);
1299
+ return {
1300
+ body: {
1301
+ cipherText: bytesToHex2(cipherText),
1302
+ encryptedInference: bytesToHex2(encrypted),
1303
+ nonce: bytesToHex2(nonce),
1304
+ fileNameNonce: bytesToHex2(fileNameNonce),
1305
+ encryptedFileName: bytesToHex2(encryptedFileName),
1306
+ fileNonce: bytesToHex2(fileNonce),
1307
+ encryptedFile: bytesToHex2(encryptedFile),
1308
+ model: body.model
1309
+ },
1310
+ sharedSecret
1311
+ };
1312
+ }
1313
+ function createAudioClient(apiKey, encryptionKeys, requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, attest2 = true) {
1314
+ async function createTranscription(body) {
1315
+ const controller = new AbortController;
1316
+ const timeoutId = setTimeout(() => controller.abort(), requestTimeoutMs);
1317
+ try {
1318
+ const sessionId = await attest(apiKey, { model: body.model, enabled: attest2 });
1319
+ const encryptedRequest = await preprocessAudioRequest(body, encryptionKeys);
1320
+ const response = await fetch(`${endpoints.proxy}/rvenc/audio/transcriptions`, {
1321
+ method: "POST",
1322
+ headers: {
1323
+ "Content-Type": "application/json",
1324
+ Authorization: apiKey,
1325
+ ...sessionId && { "X-Session-Id": sessionId }
1326
+ },
1327
+ body: JSON.stringify(encryptedRequest.body),
1328
+ signal: controller.signal
1329
+ });
1330
+ if (!response.ok) {
1331
+ await throwIfErrorResponse(response);
1332
+ }
1333
+ clearTimeout(timeoutId);
1334
+ return await postprocessTranscriptionResponse(response, encryptedRequest.sharedSecret);
1335
+ } catch (error) {
1336
+ clearTimeout(timeoutId);
1337
+ if (error instanceof Error && error.name === "AbortError") {
1338
+ throw new Error(`Request timed out after ${requestTimeoutMs}ms`);
1339
+ }
1340
+ throw error;
1341
+ }
1342
+ }
1343
+ const transcriptionsClient = {
1344
+ create: createTranscription
1345
+ };
1346
+ async function createTranslation(body) {
1347
+ const controller = new AbortController;
1348
+ const timeoutId = setTimeout(() => controller.abort(), requestTimeoutMs);
1349
+ try {
1350
+ const sessionId = await attest(apiKey, { model: body.model, enabled: attest2 });
1351
+ const encryptedRequest = await preprocessAudioTranslationRequest(body, encryptionKeys);
1352
+ const response = await fetch(`${endpoints.proxy}/rvenc/audio/translations`, {
1353
+ method: "POST",
1354
+ headers: {
1355
+ "Content-Type": "application/json",
1356
+ Authorization: apiKey,
1357
+ ...sessionId && { "X-Session-Id": sessionId }
1358
+ },
1359
+ body: JSON.stringify(encryptedRequest.body),
1360
+ signal: controller.signal
1361
+ });
1362
+ if (!response.ok) {
1363
+ await throwIfErrorResponse(response);
1364
+ }
1365
+ clearTimeout(timeoutId);
1366
+ return await postprocessTranslationResponse(response, encryptedRequest.sharedSecret);
1367
+ } catch (error) {
1368
+ clearTimeout(timeoutId);
1369
+ if (error instanceof Error && error.name === "AbortError") {
1370
+ throw new Error(`Request timed out after ${requestTimeoutMs}ms`);
1371
+ }
1372
+ throw error;
1373
+ }
1374
+ }
1375
+ const translationsClient = {
1376
+ create: createTranslation
1377
+ };
1378
+ return {
1379
+ transcriptions: transcriptionsClient,
1380
+ translations: translationsClient
1381
+ };
1382
+ }
1383
+
1384
+ // src/files/index.ts
1385
+ import { bytesToHex as bytesToHex4, hexToBytes as hexToBytes4, randomBytes as randomBytes4 } from "@noble/ciphers/utils.js";
1386
+ import { sha256 as sha2562 } from "@noble/hashes/sha2.js";
1387
+ import { isValid, parseISO } from "date-fns";
1388
+ import { z } from "zod";
1389
+
1390
+ // src/utils/dek-store.ts
1391
+ import { bytesToHex as bytesToHex3, hexToBytes as hexToBytes3, randomBytes as randomBytes3 } from "@noble/ciphers/utils.js";
1392
+ function initializeDEKStore(clientKEK) {
1393
+ const ragDEK = randomBytes3(32);
1394
+ const _clientKEK = clientKEK ? hexToBytes3(clientKEK) : getClientKEK();
1395
+ const wrappedRagDEK = wrapDEK(_clientKEK, ragDEK);
1396
+ return {
1397
+ fileDEKs: new Map,
1398
+ ragDEK: wrappedRagDEK,
1399
+ ragVersion: "2"
1400
+ };
1401
+ }
1402
+ function getClientKEK() {
1403
+ if (!process.env.CLIENT_KEK) {
1404
+ throw new Error("CLIENT_KEK environment variable is not set.");
1405
+ }
1406
+ return hexToBytes3(process.env.CLIENT_KEK);
1407
+ }
1408
+ function getClientKID(clientKEK) {
1409
+ if (clientKEK) {
1410
+ return bytesToHex3(keyIdFromKEK(hexToBytes3(clientKEK)));
1411
+ }
1412
+ const _clientKEK = getClientKEK();
1413
+ return bytesToHex3(keyIdFromKEK(_clientKEK));
1414
+ }
1415
+
1416
+ // src/files/index.ts
1417
+ var MAX_FILENAME_LENGTH = 255;
1418
+ var MIN_FILENAME_LENGTH = 1;
1419
+ var ALLOWED_MIME_TYPES = new Set([
1420
+ "image/jpeg",
1421
+ "image/png",
1422
+ "image/gif",
1423
+ "image/webp",
1424
+ "application/pdf",
1425
+ "application/msword",
1426
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
1427
+ "application/vnd.ms-excel",
1428
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
1429
+ "text/plain",
1430
+ "text/csv",
1431
+ "text/markdown",
1432
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation",
1433
+ "video/mp4",
1434
+ "video/webm",
1435
+ "video/quicktime",
1436
+ "audio/mpeg",
1437
+ "audio/wav",
1438
+ "audio/ogg",
1439
+ "application/zip",
1440
+ "application/x-rar-compressed",
1441
+ "application/x-7z-compressed",
1442
+ "application/octet-stream"
1443
+ ]);
1444
+ var ApiKeySchema = z.string().trim().min(1, "API key cannot be empty");
1445
+ var TimeoutSchema = z.number().int().min(1, "Timeout must be at least 1ms").max(600000, "Timeout must not exceed 600000ms (10 minutes)");
1446
+ var DEKStoreSchema = z.object({
1447
+ ragDEK: z.instanceof(Uint8Array).optional(),
1448
+ fileDEKs: z.instanceof(Map).optional()
1449
+ });
1450
+ var ISO8601DateSchema = z.string().refine((val) => {
1451
+ const date = parseISO(val);
1452
+ return isValid(date);
1453
+ }, { message: "Must be a valid ISO8601 date" });
1454
+ var MimeTypeSchema = z.string().refine((val) => ALLOWED_MIME_TYPES.has(val), {
1455
+ message: "MIME type is not allowed"
1456
+ });
1457
+ var FileUploadOptionsSchema = z.object({
1458
+ file: z.instanceof(Uint8Array),
1459
+ fileName: z.string().min(MIN_FILENAME_LENGTH, "File name cannot be empty").max(MAX_FILENAME_LENGTH, `File name cannot exceed ${MAX_FILENAME_LENGTH} characters`).refine((name) => !/[<>:"|?*\x00-\x1F]/.test(name), "Invalid characters").refine((name) => !name.includes("..") && !name.includes("/") && !name.includes("\\"), "No path separators allowed"),
1460
+ mimeType: z.string().optional(),
1461
+ ragIndex: z.boolean().optional()
1462
+ });
1463
+ var ListFilesOptionsSchema = z.object({
1464
+ limit: z.number().int().positive("Limit must be a positive integer").optional(),
1465
+ offset: z.number().int().nonnegative("Offset must be a non-negative integer").optional(),
1466
+ search: z.string().optional(),
1467
+ from: ISO8601DateSchema.optional(),
1468
+ to: ISO8601DateSchema.optional()
1469
+ }).optional();
1470
+ var GetFileOptionsSchema = z.object({
1471
+ id: z.string().trim().min(1, "File ID cannot be empty"),
1472
+ url: z.boolean().optional()
1473
+ });
1474
+ var DeleteFileOptionsSchema = z.object({
1475
+ id: z.string().min(1, "File ID is required")
1476
+ });
1477
+ var IndexFileInputSchema = z.object({
1478
+ fileId: z.string().min(1, "File ID is required"),
1479
+ filePath: z.string().min(1, "File path is required"),
1480
+ fileDEK: z.instanceof(Uint8Array).optional()
1481
+ });
1482
+ var IndexFilesOptionsSchema = z.object({
1483
+ files: z.array(IndexFileInputSchema).min(1, "Files array must not be empty"),
1484
+ ragDEK: z.instanceof(Uint8Array).optional()
1485
+ });
1486
+ var DeleteIndexOptionsSchema = z.object({
1487
+ fileIds: z.array(z.string().min(1)).min(1, "File IDs array must not be empty"),
1488
+ ragDEK: z.instanceof(Uint8Array).optional()
1489
+ });
1490
+ function validateAPIKey(apiKey) {
1491
+ ApiKeySchema.parse(apiKey);
1492
+ }
1493
+ function validateDEKStore(dekStore) {
1494
+ DEKStoreSchema.parse(dekStore);
1495
+ }
1496
+ function validateMimeType(mimeType) {
1497
+ MimeTypeSchema.parse(mimeType);
1498
+ }
1499
+ function validateFileUploadOptions(options) {
1500
+ FileUploadOptionsSchema.parse(options);
1501
+ }
1502
+ function validateListFilesOptions(options) {
1503
+ ListFilesOptionsSchema.parse(options);
1504
+ }
1505
+ function validateGetFileOptions(options) {
1506
+ GetFileOptionsSchema.parse(options);
1507
+ }
1508
+ function guessMimeType(fileName) {
1509
+ const ext = fileName.toLowerCase().split(".").pop() || "";
1510
+ const mimeTypeMap = {
1511
+ jpg: "image/jpeg",
1512
+ jpeg: "image/jpeg",
1513
+ png: "image/png",
1514
+ gif: "image/gif",
1515
+ webp: "image/webp",
1516
+ pdf: "application/pdf",
1517
+ doc: "application/msword",
1518
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
1519
+ xls: "application/vnd.ms-excel",
1520
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
1521
+ txt: "text/plain",
1522
+ csv: "text/csv",
1523
+ md: "text/markdown",
1524
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
1525
+ mp4: "video/mp4",
1526
+ webm: "video/webm",
1527
+ mov: "video/quicktime",
1528
+ mp3: "audio/mpeg",
1529
+ wav: "audio/wav",
1530
+ ogg: "audio/ogg",
1531
+ zip: "application/zip",
1532
+ rar: "application/x-rar-compressed",
1533
+ "7z": "application/x-7z-compressed"
1534
+ };
1535
+ return mimeTypeMap[ext] || "application/octet-stream";
1536
+ }
1537
+ async function saveRagDEKToBackend(apiKey, wrappedRagDEK, timeoutMs) {
1538
+ const controller = new AbortController;
1539
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
1540
+ try {
1541
+ const response = await fetch(`${endpoints.proxy}/users/save_rag_dek`, {
1542
+ method: "POST",
1543
+ headers: {
1544
+ Authorization: apiKey,
1545
+ "Content-Type": "application/json"
1546
+ },
1547
+ body: JSON.stringify({
1548
+ data: {
1549
+ wrappedRagDEK,
1550
+ confirmReplaceRagDEK: true
1551
+ }
1552
+ }),
1553
+ signal: controller.signal
1554
+ });
1555
+ if (!response.ok) {
1556
+ throw new Error(`Failed to save RAG DEK: HTTP ${response.status}`);
1557
+ }
1558
+ const result = await response.json();
1559
+ if (result.error) {
1560
+ throw new Error(result.error);
1561
+ }
1562
+ } catch (error) {
1563
+ if (error instanceof Error && error.name === "AbortError") {
1564
+ throw new Error(`Save RAG DEK request timed out after ${timeoutMs}ms`);
1565
+ }
1566
+ throw error;
1567
+ } finally {
1568
+ clearTimeout(timeoutId);
1569
+ }
1570
+ }
1571
+ async function prepareEncryptedPayload(dekStore, options, apiKey, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1572
+ const fileBytes = options.file;
1573
+ const mimeType = options.mimeType || guessMimeType(options.fileName);
1574
+ validateMimeType(mimeType);
1575
+ const dek = randomBytes4(32);
1576
+ const encryptedFile = encryptWithDEK(dek, fileBytes);
1577
+ const encryptedName = encryptMetadataWithDEK(dek, options.fileName);
1578
+ const encryptedMimeType = encryptMetadataWithDEK(dek, mimeType);
1579
+ const _clientKEK = clientKEK ? hexToBytes4(clientKEK) : getClientKEK();
1580
+ const wrappedDEK = wrapDEK(_clientKEK, dek);
1581
+ const clientKID = clientKEK ? getClientKID(clientKEK) : getClientKID();
1582
+ const filePayload = {
1583
+ client_hash: bytesToHex4(sha2562(fileBytes)),
1584
+ encrypted_content: bytesToHex4(encryptedFile),
1585
+ encrypted_name: encryptedName,
1586
+ kid: clientKID,
1587
+ mime_type: encryptedMimeType,
1588
+ version: "2",
1589
+ wrapped_dek: bytesToHex4(wrappedDEK)
1590
+ };
1591
+ if (options.ragIndex) {
1592
+ await addRagIndexToPayload(dekStore, dek, filePayload, apiKey, clientKEK, timeoutMs);
1593
+ }
1594
+ return { dek, filePayload };
1595
+ }
1596
+ async function addRagIndexToPayload(dekStore, dek, filePayload, apiKey, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1597
+ let ragDEK = dekStore.ragDEK;
1598
+ const _clientKEK = clientKEK ? hexToBytes4(clientKEK) : getClientKEK();
1599
+ if (!ragDEK) {
1600
+ ragDEK = randomBytes4(32);
1601
+ const wrappedRagDEK = wrapDEK(_clientKEK, ragDEK);
1602
+ dekStore.ragDEK = wrappedRagDEK;
1603
+ try {
1604
+ await saveRagDEKToBackend(apiKey, bytesToHex4(wrappedRagDEK), timeoutMs);
1605
+ } catch (error) {
1606
+ console.error("Warning: Failed to save RAG DEK to backend:", error);
1607
+ }
1608
+ } else {
1609
+ ragDEK = unwrapDEK(_clientKEK, ragDEK);
1610
+ }
1611
+ const enclavePublicKey = await getEnclavePublicKey(timeoutMs);
1612
+ const { cipherText, sharedSecret } = createMLKEMEncapsulation(enclavePublicKey);
1613
+ const { encrypted: encryptedFileDEK, nonce: fileNonce } = encryptPayload(sharedSecret, dek);
1614
+ const { encrypted: encryptedRagDEK, nonce: ragDEKNonce } = encryptPayload(sharedSecret, ragDEK);
1615
+ filePayload.encrypted_file_dek = bytesToHex4(encryptedFileDEK);
1616
+ filePayload.encrypted_rag_dek = bytesToHex4(encryptedRagDEK);
1617
+ filePayload.file_nonce = bytesToHex4(fileNonce);
1618
+ filePayload.rag_dek_nonce = bytesToHex4(ragDEKNonce);
1619
+ filePayload.cipher_text = bytesToHex4(cipherText);
1620
+ }
1621
+ async function performUpload(apiKey, filePayload, controller) {
1622
+ const uploadResponse = await fetch(`${endpoints.proxy}/files/encrypted/upload`, {
1623
+ method: "POST",
1624
+ headers: {
1625
+ Authorization: apiKey,
1626
+ "Content-Type": "application/json"
1627
+ },
1628
+ body: JSON.stringify(filePayload),
1629
+ signal: controller.signal
1630
+ });
1631
+ if (!uploadResponse.ok) {
1632
+ let errorMessage = `Upload request failed with status ${uploadResponse.status}`;
1633
+ try {
1634
+ const body = await uploadResponse.json();
1635
+ if (body.error) {
1636
+ errorMessage = body.error;
1637
+ }
1638
+ } catch {}
1639
+ throw new Error(errorMessage);
1640
+ }
1641
+ const uploadResult = await uploadResponse.json();
1642
+ if (uploadResult.status !== 200) {
1643
+ throw new Error(uploadResult.error || "Upload failed");
1644
+ }
1645
+ if (!uploadResult.data) {
1646
+ throw new Error("Upload response missing data");
1647
+ }
1648
+ return uploadResult.data;
1649
+ }
1650
+ function storeDEKForFile(dekStore, fileId, dek, clientKEK) {
1651
+ if (!dekStore.fileDEKs) {
1652
+ dekStore.fileDEKs = new Map;
1653
+ }
1654
+ const _clientKEK = clientKEK ? hexToBytes4(clientKEK) : getClientKEK();
1655
+ const wrappedDEK = wrapDEK(_clientKEK, dek);
1656
+ dekStore.fileDEKs.set(fileId, wrappedDEK);
1657
+ }
1658
+ async function uploadFile(apiKey, dekStore, options, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1659
+ validateAPIKey(apiKey);
1660
+ validateDEKStore(dekStore);
1661
+ validateFileUploadOptions(options);
1662
+ TimeoutSchema.parse(timeoutMs);
1663
+ const controller = new AbortController;
1664
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
1665
+ try {
1666
+ const { dek, filePayload } = await prepareEncryptedPayload(dekStore, options, apiKey, clientKEK, timeoutMs);
1667
+ const uploadedFile = await performUpload(apiKey, filePayload, controller);
1668
+ storeDEKForFile(dekStore, uploadedFile.id, dek, clientKEK);
1669
+ return uploadedFile;
1670
+ } catch (error) {
1671
+ if (error instanceof Error && error.name === "AbortError") {
1672
+ throw new Error(`File upload timed out after ${timeoutMs}ms`);
1673
+ }
1674
+ throw error;
1675
+ } finally {
1676
+ clearTimeout(timeoutId);
1677
+ }
1678
+ }
1679
+ async function listFiles(apiKey, options, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1680
+ validateAPIKey(apiKey);
1681
+ validateListFilesOptions(options);
1682
+ TimeoutSchema.parse(timeoutMs);
1683
+ const controller = new AbortController;
1684
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
1685
+ const queryParams = new URLSearchParams;
1686
+ if (options?.limit !== undefined) {
1687
+ queryParams.append("limit", options.limit.toString());
1688
+ }
1689
+ if (options?.offset !== undefined) {
1690
+ queryParams.append("offset", options.offset.toString());
1691
+ }
1692
+ if (options?.search) {
1693
+ queryParams.append("search", options.search);
1694
+ }
1695
+ if (options?.from) {
1696
+ queryParams.append("from", options.from);
1697
+ }
1698
+ if (options?.to) {
1699
+ queryParams.append("to", options.to);
1700
+ }
1701
+ const queryString = queryParams.toString();
1702
+ const url = `${endpoints.proxy}/files/encrypted${queryString ? `?${queryString}` : ""}`;
1703
+ try {
1704
+ const response = await fetch(url, {
1705
+ method: "GET",
1706
+ headers: {
1707
+ Authorization: apiKey,
1708
+ "Content-Type": "application/json"
1709
+ },
1710
+ signal: controller.signal
1711
+ });
1712
+ if (!response.ok) {
1713
+ throw new Error(`List files request failed with status ${response.status}`);
1714
+ }
1715
+ const result = await response.json();
1716
+ if (result.status !== 200) {
1717
+ throw new Error(result.error || "List files failed");
1718
+ }
1719
+ if (!result.data) {
1720
+ throw new Error("List files response missing data");
1721
+ }
1722
+ return result.data;
1723
+ } catch (error) {
1724
+ if (error instanceof Error && error.name === "AbortError") {
1725
+ throw new Error(`List files request timed out after ${timeoutMs}ms`);
1726
+ }
1727
+ throw error;
1728
+ } finally {
1729
+ clearTimeout(timeoutId);
1730
+ }
1731
+ }
1732
+ async function getFile(apiKey, options, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1733
+ validateAPIKey(apiKey);
1734
+ validateGetFileOptions(options);
1735
+ TimeoutSchema.parse(timeoutMs);
1736
+ const controller = new AbortController;
1737
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
1738
+ const queryParams = new URLSearchParams;
1739
+ if (options.url !== undefined) {
1740
+ queryParams.append("url", options.url ? "true" : "false");
1741
+ }
1742
+ const queryString = queryParams.toString();
1743
+ const url = `${endpoints.proxy}/files/encrypted/${options.id}${queryString ? `?${queryString}` : ""}`;
1744
+ try {
1745
+ const response = await fetch(url, {
1746
+ method: "GET",
1747
+ headers: {
1748
+ Authorization: apiKey,
1749
+ "Content-Type": "application/json"
1750
+ },
1751
+ signal: controller.signal
1752
+ });
1753
+ if (!response.ok) {
1754
+ if (response.status === 404) {
1755
+ throw new Error(`File not found: ${options.id}`);
1756
+ }
1757
+ throw new Error(`Get file request failed with status ${response.status}`);
1758
+ }
1759
+ const result = await response.json();
1760
+ if (result.status !== 200) {
1761
+ throw new Error(result.error || "Get file failed");
1762
+ }
1763
+ if (!result.data) {
1764
+ throw new Error("Get file response missing data");
1765
+ }
1766
+ return result.data;
1767
+ } catch (error) {
1768
+ if (error instanceof Error && error.name === "AbortError") {
1769
+ throw new Error(`Get file request timed out after ${timeoutMs}ms`);
1770
+ }
1771
+ throw error;
1772
+ } finally {
1773
+ clearTimeout(timeoutId);
1774
+ }
1775
+ }
1776
+ async function deleteFile(apiKey, options, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1777
+ validateAPIKey(apiKey);
1778
+ DeleteFileOptionsSchema.parse(options);
1779
+ TimeoutSchema.parse(timeoutMs);
1780
+ const controller = new AbortController;
1781
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
1782
+ try {
1783
+ const response = await fetch(`${endpoints.proxy}/files/encrypted/${options.id}`, {
1784
+ method: "DELETE",
1785
+ headers: {
1786
+ Authorization: apiKey,
1787
+ "Content-Type": "application/json"
1788
+ },
1789
+ signal: controller.signal
1790
+ });
1791
+ if (!response.ok) {
1792
+ if (response.status === 404) {
1793
+ throw new Error(`File not found: ${options.id}`);
1794
+ }
1795
+ throw new Error(`Delete file request failed with status ${response.status}`);
1796
+ }
1797
+ await response.json();
1798
+ } catch (error) {
1799
+ if (error instanceof Error && error.name === "AbortError") {
1800
+ throw new Error(`Delete file request timed out after ${timeoutMs}ms`);
1801
+ }
1802
+ throw error;
1803
+ } finally {
1804
+ clearTimeout(timeoutId);
1805
+ }
1806
+ }
1807
+ async function indexFiles(apiKey, dekStore, options, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1808
+ validateAPIKey(apiKey);
1809
+ validateDEKStore(dekStore);
1810
+ IndexFilesOptionsSchema.parse(options);
1811
+ TimeoutSchema.parse(timeoutMs);
1812
+ const wrappedRagDEK = options.ragDEK || dekStore.ragDEK;
1813
+ if (!wrappedRagDEK) {
1814
+ throw new Error("RAG DEK not found. Provide ragDEK in options or upload at least one file with ragIndex: true.");
1815
+ }
1816
+ const _clientKEK = clientKEK ? hexToBytes4(clientKEK) : getClientKEK();
1817
+ const ragDEK = unwrapDEK(_clientKEK, wrappedRagDEK);
1818
+ const enclavePublicKey = await getEnclavePublicKey(timeoutMs);
1819
+ const { cipherText, sharedSecret } = createMLKEMEncapsulation(enclavePublicKey);
1820
+ const encryptedFiles = options.files.map((file) => {
1821
+ const wrappedFileDEK = file.fileDEK || dekStore.fileDEKs?.get(file.fileId);
1822
+ if (!wrappedFileDEK) {
1823
+ throw new Error(`File DEK not found for file: ${file.fileId}. Provide fileDEK or ensure file was uploaded with this DEK store.`);
1824
+ }
1825
+ const fileDEK = unwrapDEK(_clientKEK, wrappedFileDEK);
1826
+ const { encrypted: encryptedFileDEK, nonce: fileNonce } = encryptPayload(sharedSecret, fileDEK);
1827
+ const { encrypted: encryptedRagDEK, nonce: ragDEKNonce } = encryptPayload(sharedSecret, ragDEK);
1828
+ return {
1829
+ file_id: file.fileId,
1830
+ encrypted_file_dek: bytesToHex4(encryptedFileDEK),
1831
+ encrypted_rag_dek: bytesToHex4(encryptedRagDEK),
1832
+ file_nonce: bytesToHex4(fileNonce),
1833
+ rag_dek_nonce: bytesToHex4(ragDEKNonce),
1834
+ s3_r2_path: file.filePath,
1835
+ cipher_text: bytesToHex4(cipherText)
1836
+ };
1837
+ });
1838
+ const controller = new AbortController;
1839
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
1840
+ try {
1841
+ const response = await fetch(`${endpoints.proxy}/files/encrypted/index`, {
1842
+ method: "POST",
1843
+ headers: {
1844
+ Authorization: apiKey,
1845
+ "Content-Type": "application/json"
1846
+ },
1847
+ body: JSON.stringify({ files: encryptedFiles }),
1848
+ signal: controller.signal
1849
+ });
1850
+ if (!response.ok) {
1851
+ throw new Error(`Index files request failed with status ${response.status}`);
1852
+ }
1853
+ const result = await response.json();
1854
+ return result;
1855
+ } catch (error) {
1856
+ if (error instanceof Error && error.name === "AbortError") {
1857
+ throw new Error(`Index files request timed out after ${timeoutMs}ms`);
1858
+ }
1859
+ throw error;
1860
+ } finally {
1861
+ clearTimeout(timeoutId);
1862
+ }
1863
+ }
1864
+ async function deleteIndex(apiKey, dekStore, options, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1865
+ validateAPIKey(apiKey);
1866
+ validateDEKStore(dekStore);
1867
+ DeleteIndexOptionsSchema.parse(options);
1868
+ TimeoutSchema.parse(timeoutMs);
1869
+ const wrappedRagDEK = options.ragDEK || dekStore.ragDEK;
1870
+ if (!wrappedRagDEK) {
1871
+ throw new Error("RAG DEK not found. Provide ragDEK in options or ensure dekStore has a ragDEK.");
1872
+ }
1873
+ const _clientKEK = clientKEK ? hexToBytes4(clientKEK) : getClientKEK();
1874
+ const ragDEK = unwrapDEK(_clientKEK, wrappedRagDEK);
1875
+ const enclavePublicKey = await getEnclavePublicKey(timeoutMs);
1876
+ const { cipherText, sharedSecret } = createMLKEMEncapsulation(enclavePublicKey);
1877
+ const { encrypted: encryptedRagDEK, nonce: ragDEKNonce } = encryptPayload(sharedSecret, ragDEK);
1878
+ const controller = new AbortController;
1879
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
1880
+ try {
1881
+ const response = await fetch(`${endpoints.proxy}/files/encrypted/delete-index`, {
1882
+ method: "POST",
1883
+ headers: {
1884
+ Authorization: apiKey,
1885
+ "Content-Type": "application/json"
1886
+ },
1887
+ body: JSON.stringify({
1888
+ cipher_text: bytesToHex4(cipherText),
1889
+ encrypted_rag_dek: bytesToHex4(encryptedRagDEK),
1890
+ rag_dek_nonce: bytesToHex4(ragDEKNonce),
1891
+ fileIds: options.fileIds
1892
+ }),
1893
+ signal: controller.signal
1894
+ });
1895
+ if (!response.ok) {
1896
+ throw new Error(`Delete index request failed with status ${response.status}`);
1897
+ }
1898
+ const result = await response.json();
1899
+ return result;
1900
+ } catch (error) {
1901
+ if (error instanceof Error && error.name === "AbortError") {
1902
+ throw new Error(`Delete index request timed out after ${timeoutMs}ms`);
1903
+ }
1904
+ throw error;
1905
+ } finally {
1906
+ clearTimeout(timeoutId);
1907
+ }
1908
+ }
1909
+ function createFilesClient(apiKey, dekStore, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1910
+ return {
1911
+ upload: (options) => uploadFile(apiKey, dekStore, options, clientKEK, timeoutMs),
1912
+ list: (options) => listFiles(apiKey, options, timeoutMs),
1913
+ get: (options) => getFile(apiKey, options, timeoutMs),
1914
+ delete: (options) => deleteFile(apiKey, options, timeoutMs),
1915
+ index: (options) => indexFiles(apiKey, dekStore, options, clientKEK, timeoutMs),
1916
+ deleteIndex: (options) => deleteIndex(apiKey, dekStore, options, clientKEK, timeoutMs)
1917
+ };
1918
+ }
1919
+
1920
+ // src/models/index.ts
1921
+ async function listModels(params, apiKey, timeoutMs) {
1922
+ const controller = new AbortController;
1923
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
1924
+ const queryParams = new URLSearchParams;
1925
+ if (params?.type !== undefined) {
1926
+ queryParams.append("type", params.type);
1927
+ }
1928
+ const queryString = queryParams.toString();
1929
+ const url = `${endpoints.proxy}/models${queryString ? `?${queryString}` : ""}`;
1930
+ try {
1931
+ const response = await fetch(url, {
1932
+ method: "GET",
1933
+ headers: {
1934
+ Authorization: apiKey,
1935
+ "Content-Type": "application/json"
1936
+ },
1937
+ signal: controller.signal
1938
+ });
1939
+ if (!response.ok) {
1940
+ throw new Error(`List models request failed with status ${response.status}`);
1941
+ }
1942
+ const result = await response.json();
1943
+ if (result.status !== 200) {
1944
+ throw new Error(result.error || "List models failed");
1945
+ }
1946
+ if (!result.data) {
1947
+ throw new Error("List models response missing data");
1948
+ }
1949
+ return result.data;
1950
+ } catch (error) {
1951
+ if (error instanceof Error && error.name === "AbortError") {
1952
+ throw new Error(`List models request timed out after ${timeoutMs}ms`);
1953
+ }
1954
+ throw error;
1955
+ } finally {
1956
+ clearTimeout(timeoutId);
1957
+ }
1958
+ }
1959
+ function createModelsClient(apiKey, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS) {
1960
+ return {
1961
+ list: (params) => listModels(params, apiKey, timeoutMs)
1962
+ };
1963
+ }
1964
+
1965
+ // src/rvenc/index.ts
1966
+ import { bytesToHex as bytesToHex5, hexToBytes as hexToBytes5 } from "@noble/ciphers/utils.js";
1967
+ import OpenAI from "openai";
1968
+ function preprocessRequest(body, encryptionKeys) {
1969
+ const { cipherText, sharedSecret } = encryptionKeys;
1970
+ const { encrypted, nonce } = encryptPayload(sharedSecret, body);
1971
+ return {
1972
+ body: {
1973
+ cipherText: bytesToHex5(cipherText),
1974
+ encryptedInference: bytesToHex5(encrypted),
1975
+ nonce: bytesToHex5(nonce),
1976
+ model: body.model,
1977
+ stream: body.stream === true
1978
+ },
1979
+ sharedSecret,
1980
+ nonce
1981
+ };
1982
+ }
1983
+ async function postprocessStreamingResponse(response, sharedSecret, nonce, maxBufferSize) {
1984
+ if (!response.body) {
1985
+ throw new Error("Response body is null");
1986
+ }
1987
+ const reader = response.body.getReader();
1988
+ const generator = createDecryptedStreamGenerator(reader, sharedSecret, nonce, maxBufferSize);
1989
+ return {
1990
+ [Symbol.asyncIterator]() {
1991
+ return generator;
1992
+ }
1993
+ };
1994
+ }
1995
+ async function postprocessNonStreamingResponse(response, sharedSecret) {
1996
+ const data = await response.json();
1997
+ if (!data.encryptedResponse || !data.nonce) {
1998
+ throw new Error("Invalid non-streaming response: missing encryptedResponse or nonce");
1999
+ }
2000
+ const responseNonce = hexToBytes5(data.nonce);
2001
+ return decryptPayload(data.encryptedResponse, sharedSecret, responseNonce);
2002
+ }
2003
+ function createRvencChatClient(apiKey, encryptionKeys, requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, maxBufferSize = DEFAULT_MAX_BUFFER_SIZE, attest2 = true, OpenAIClientParams) {
2004
+ const client = new OpenAI({ apiKey: "not-used", ...OpenAIClientParams });
2005
+ const originalChatCreate = client.chat.completions.create.bind(client.chat.completions);
2006
+ client.chat.completions.create = async (body) => {
2007
+ const isStreaming = body.stream === true;
2008
+ const controller = new AbortController;
2009
+ const timeoutId = setTimeout(() => controller.abort(), requestTimeoutMs);
2010
+ try {
2011
+ const sessionId = await attest(apiKey, { model: body.model, enabled: attest2 });
2012
+ const encryptedRequest = preprocessRequest(body, encryptionKeys);
2013
+ const response = await fetch(`${endpoints.proxy}/rvenc/chat/completions`, {
2014
+ method: "POST",
2015
+ headers: {
2016
+ "Content-Type": "application/json",
2017
+ Accept: isStreaming ? "text/event-stream" : "application/json",
2018
+ Authorization: apiKey,
2019
+ ...sessionId && { "X-Session-Id": sessionId }
2020
+ },
2021
+ body: JSON.stringify(encryptedRequest.body),
2022
+ signal: controller.signal
2023
+ });
2024
+ if (!response.ok) {
2025
+ await throwIfErrorResponse(response);
2026
+ }
2027
+ clearTimeout(timeoutId);
2028
+ if (isStreaming) {
2029
+ const contentType = response.headers.get("content-type") ?? "";
2030
+ if (contentType.includes("text/event-stream")) {
2031
+ return await postprocessStreamingResponse(response, encryptedRequest.sharedSecret, encryptedRequest.nonce, maxBufferSize);
2032
+ }
2033
+ const completion = await postprocessNonStreamingResponse(response, encryptedRequest.sharedSecret);
2034
+ return completionToChunkStream(completion);
2035
+ }
2036
+ return await postprocessNonStreamingResponse(response, encryptedRequest.sharedSecret);
2037
+ } catch (error) {
2038
+ clearTimeout(timeoutId);
2039
+ if (error instanceof Error && error.name === "AbortError") {
2040
+ throw new Error(`Request timed out after ${requestTimeoutMs}ms`);
2041
+ }
2042
+ throw error;
2043
+ }
2044
+ };
2045
+ return client;
2046
+ }
2047
+ async function* completionToChunkStream(completion) {
2048
+ const choice = completion.choices[0];
2049
+ const message = choice?.message;
2050
+ const content = typeof message?.content === "string" ? message.content : "";
2051
+ const toolCalls = message?.tool_calls?.filter((tc) => tc.type === "function").map((tc, i) => ({
2052
+ index: i,
2053
+ id: tc.id,
2054
+ type: "function",
2055
+ function: {
2056
+ name: tc.function.name,
2057
+ arguments: tc.function.arguments
2058
+ }
2059
+ }));
2060
+ yield {
2061
+ id: completion.id,
2062
+ object: "chat.completion.chunk",
2063
+ created: completion.created,
2064
+ model: completion.model,
2065
+ choices: [
2066
+ {
2067
+ index: choice?.index ?? 0,
2068
+ delta: {
2069
+ role: "assistant",
2070
+ content,
2071
+ ...toolCalls && toolCalls.length > 0 && { tool_calls: toolCalls }
2072
+ },
2073
+ finish_reason: choice?.finish_reason ?? "stop",
2074
+ logprobs: null
2075
+ }
2076
+ ],
2077
+ usage: completion.usage ?? null
2078
+ };
2079
+ }
2080
+ async function* createDecryptedStreamGenerator(reader, sharedSecret, nonce, maxBufferSize) {
2081
+ const decoder = new TextDecoder;
2082
+ let buffer = "";
2083
+ try {
2084
+ while (true) {
2085
+ const { value, done } = await reader.read();
2086
+ if (done)
2087
+ break;
2088
+ buffer += decoder.decode(value, { stream: true });
2089
+ if (buffer.length > maxBufferSize) {
2090
+ throw new Error(`Stream buffer exceeded maximum size of ${maxBufferSize} bytes`);
2091
+ }
2092
+ const parts = buffer.split(`
2093
+
2094
+ `);
2095
+ for (let i = 0;i < parts.length - 1; i++) {
2096
+ const part = parts[i];
2097
+ const lines = part.split(`
2098
+ `);
2099
+ let event;
2100
+ let data;
2101
+ if (lines[0]) {
2102
+ const eventSplit = lines[0].split(": ");
2103
+ event = eventSplit[1];
2104
+ }
2105
+ if (lines[1]) {
2106
+ const dataSplit = lines[1].split(": ");
2107
+ data = dataSplit.slice(1).join(": ");
2108
+ }
2109
+ if (event === "done" && data === "[DONE]") {
2110
+ return;
2111
+ }
2112
+ if (event === "error") {
2113
+ const errorObj = JSON.parse(data || "{}");
2114
+ throw new Error(errorObj.error?.message || data || "Stream error");
2115
+ }
2116
+ if (event === "data" && data && data !== "[DONE]") {
2117
+ const chunk = decryptPayload(data, sharedSecret, nonce);
2118
+ if (chunk.error) {
2119
+ throw new Error(chunk.error.message || "Stream error");
2120
+ }
2121
+ yield chunk;
2122
+ }
2123
+ }
2124
+ buffer = parts[parts.length - 1];
2125
+ }
2126
+ } finally {
2127
+ reader.releaseLock();
2128
+ }
2129
+ }
2130
+
2131
+ // src/tools/index.ts
2132
+ import { bytesToHex as bytesToHex6, hexToBytes as hexToBytes6, randomBytes as randomBytes5 } from "@noble/ciphers/utils.js";
2133
+ var FILE_OUTPUT_TOOLS = ["generateImage", "audioGenerateFromText", "createFileForUser"];
2134
+ var FILE_INPUT_TOOLS = [
2135
+ "imageDescribeAndCaption",
2136
+ "imageDescribeAndCaptionFallback",
2137
+ "videoDescribeAndCaption",
2138
+ "getPDFContent",
2139
+ "getTextDocumentContent",
2140
+ "transcribeAudioToText",
2141
+ "transcribeAudioWithDiarization",
2142
+ "audioDiarization",
2143
+ "getSpreadsheetContent",
2144
+ "getPowerPointContent",
2145
+ "getDataFileContent",
2146
+ "getFileContentOCR"
2147
+ ];
2148
+ var RAG_TOOLS = ["searchRag"];
2149
+ async function callToolRequest(toolName, body, apiKey, timeoutMs, attest2) {
2150
+ const controller = new AbortController;
2151
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
2152
+ try {
2153
+ const response = await fetch(`${endpoints.proxy}/tools/${toolName}`, {
2154
+ method: "POST",
2155
+ headers: {
2156
+ "Content-Type": "application/json",
2157
+ Authorization: apiKey
2158
+ },
2159
+ body: JSON.stringify(body),
2160
+ signal: controller.signal
2161
+ });
2162
+ clearTimeout(timeoutId);
2163
+ if (!response.ok) {
2164
+ await throwIfErrorResponse(response);
2165
+ }
2166
+ const data = await response.json();
2167
+ return data.data;
2168
+ } catch (error) {
2169
+ clearTimeout(timeoutId);
2170
+ if (error instanceof Error && error.name === "AbortError") {
2171
+ throw new Error(`Tool request timed out after ${timeoutMs}ms`);
2172
+ }
2173
+ throw new Error(`Tool request failed: ${error instanceof Error ? error.message : error}`);
2174
+ }
2175
+ }
2176
+ async function downloadEncryptedFile(fileId, apiKey, timeoutMs) {
2177
+ const controller = new AbortController;
2178
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
2179
+ try {
2180
+ const metadataResponse = await fetch(`${endpoints.proxy}/files/encrypted/${fileId}?url=true`, {
2181
+ headers: { Authorization: apiKey },
2182
+ signal: controller.signal
2183
+ });
2184
+ if (!metadataResponse.ok) {
2185
+ throw new Error(`Failed to get file metadata: ${metadataResponse.status}`);
2186
+ }
2187
+ const metadata = await metadataResponse.json();
2188
+ const downloadUrl = metadata.data?.url;
2189
+ if (!downloadUrl) {
2190
+ throw new Error("No download URL in response");
2191
+ }
2192
+ const fileResponse = await fetch(downloadUrl, { signal: controller.signal });
2193
+ if (!fileResponse.ok) {
2194
+ throw new Error(`Failed to download file: ${fileResponse.status}`);
2195
+ }
2196
+ clearTimeout(timeoutId);
2197
+ const arrayBuffer = await fileResponse.arrayBuffer();
2198
+ return new Uint8Array(arrayBuffer);
2199
+ } catch (error) {
2200
+ clearTimeout(timeoutId);
2201
+ if (error instanceof Error && error.name === "AbortError") {
2202
+ throw new Error(`File download timed out after ${timeoutMs}ms`);
2203
+ }
2204
+ throw error;
2205
+ }
2206
+ }
2207
+ async function downloadAndDecryptFile(response, dek, apiKey, timeoutMs) {
2208
+ if (!response.success || !response.fileId) {
2209
+ return null;
2210
+ }
2211
+ const decryptFileName = (encryptedHex) => {
2212
+ const encrypted = hexToBytes6(encryptedHex);
2213
+ const decrypted = decryptWithDEK(dek, encrypted);
2214
+ return new TextDecoder().decode(decrypted);
2215
+ };
2216
+ const fileName = decryptFileName(response.fileName);
2217
+ const mimeType = decryptFileName(response.mimeType);
2218
+ const encryptedFile = await downloadEncryptedFile(response.fileId, apiKey, timeoutMs);
2219
+ const decryptedFile = decryptWithDEK(dek, encryptedFile);
2220
+ return {
2221
+ fileId: response.fileId,
2222
+ fileName,
2223
+ mimeType,
2224
+ content: decryptedFile,
2225
+ fileSize: decryptedFile.length
2226
+ };
2227
+ }
2228
+ async function callSimpleTool(toolName, params, apiKey, timeoutMs, attest2) {
2229
+ const enclavePublicKey = await getEnclavePublicKey(timeoutMs);
2230
+ const { cipherText, sharedSecret } = createMLKEMEncapsulation(enclavePublicKey);
2231
+ const { encrypted, nonce } = encryptPayload(sharedSecret, params);
2232
+ const body = {
2233
+ cipherText: bytesToHex6(cipherText),
2234
+ encryptedParams: bytesToHex6(encrypted),
2235
+ nonce: bytesToHex6(nonce)
2236
+ };
2237
+ const response = await callToolRequest(toolName, body, apiKey, timeoutMs, attest2);
2238
+ return decryptPayload(response.encryptedResponse, sharedSecret, hexToBytes6(response.nonce));
2239
+ }
2240
+ async function callFileOutputTool(toolName, params, apiKey, dekStore, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, attest2 = true) {
2241
+ const enclavePublicKey = await getEnclavePublicKey(timeoutMs);
2242
+ const { cipherText, sharedSecret } = createMLKEMEncapsulation(enclavePublicKey);
2243
+ const { encrypted, nonce } = encryptPayload(sharedSecret, params);
2244
+ const dek = randomBytes5(32);
2245
+ const { encrypted: encryptedDEK, nonce: dekNonce } = encryptPayload(sharedSecret, dek);
2246
+ const _clientKEK = clientKEK ? hexToBytes6(clientKEK) : getClientKEK();
2247
+ const wrappedDEK = wrapDEK(_clientKEK, dek);
2248
+ const clientKID = clientKEK ? getClientKID(clientKEK) : getClientKID();
2249
+ const body = {
2250
+ cipherText: bytesToHex6(cipherText),
2251
+ encryptedParams: bytesToHex6(encrypted),
2252
+ nonce: bytesToHex6(nonce),
2253
+ encryptedDEK: bytesToHex6(encryptedDEK),
2254
+ dekNonce: bytesToHex6(dekNonce),
2255
+ kid: clientKID,
2256
+ wrappedDEK: bytesToHex6(wrappedDEK)
2257
+ };
2258
+ const response = await callToolRequest(toolName, body, apiKey, timeoutMs, attest2);
2259
+ const result = await downloadAndDecryptFile(response, dek, apiKey, timeoutMs);
2260
+ if (result?.fileId) {
2261
+ if (!dekStore.fileDEKs) {
2262
+ dekStore.fileDEKs = new Map;
2263
+ }
2264
+ dekStore.fileDEKs.set(result.fileId, wrappedDEK);
2265
+ }
2266
+ return result;
2267
+ }
2268
+ async function callFileInputTool(toolName, params, apiKey, dekStore, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, attest2 = true) {
2269
+ if (!params.fileId) {
2270
+ throw new Error(`Tool ${toolName} requires fileId parameter`);
2271
+ }
2272
+ const enclavePublicKey = await getEnclavePublicKey(timeoutMs);
2273
+ const { cipherText, sharedSecret } = createMLKEMEncapsulation(enclavePublicKey);
2274
+ const dek = randomBytes5(32);
2275
+ const { encrypted: encryptedDEK, nonce: dekNonce } = encryptPayload(sharedSecret, dek);
2276
+ const nonce = randomBytes5(24);
2277
+ if (!dekStore.fileDEKs) {
2278
+ dekStore.fileDEKs = new Map;
2279
+ }
2280
+ const _clientKEK = clientKEK ? hexToBytes6(clientKEK) : getClientKEK();
2281
+ let fileDEK = dekStore.fileDEKs.get(params.fileId);
2282
+ if (!fileDEK) {
2283
+ fileDEK = randomBytes5(32);
2284
+ const wrappedFileDEK = wrapDEK(_clientKEK, fileDEK);
2285
+ dekStore.fileDEKs.set(params.fileId, wrappedFileDEK);
2286
+ } else {
2287
+ fileDEK = unwrapDEK(_clientKEK, fileDEK);
2288
+ }
2289
+ const { encrypted: encryptedFileDEK, nonce: fileDEKNonce } = encryptPayload(sharedSecret, fileDEK);
2290
+ const body = {
2291
+ cipherText: bytesToHex6(cipherText),
2292
+ nonce: bytesToHex6(nonce),
2293
+ fileId: params.fileId,
2294
+ encryptedDEK: bytesToHex6(encryptedDEK),
2295
+ dekNonce: bytesToHex6(dekNonce),
2296
+ encryptedFileDEK: bytesToHex6(encryptedFileDEK),
2297
+ fileDEKNonce: bytesToHex6(fileDEKNonce)
2298
+ };
2299
+ const response = await callToolRequest(toolName, body, apiKey, timeoutMs, attest2);
2300
+ return decryptPayload(response.encryptedResponse, sharedSecret, hexToBytes6(response.nonce));
2301
+ }
2302
+ async function callRagTool(toolName, params, apiKey, dekStore, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, attest2 = true) {
2303
+ const enclavePublicKey = await getEnclavePublicKey(timeoutMs);
2304
+ const { cipherText, sharedSecret } = createMLKEMEncapsulation(enclavePublicKey);
2305
+ const { encrypted, nonce } = encryptPayload(sharedSecret, params);
2306
+ const dek = randomBytes5(32);
2307
+ const { encrypted: encryptedDEK, nonce: dekNonce } = encryptPayload(sharedSecret, dek);
2308
+ if (!dekStore.fileDEKs) {
2309
+ dekStore.fileDEKs = new Map;
2310
+ }
2311
+ let fileIds = [];
2312
+ if (dekStore.fileDEKs.size > 0) {
2313
+ fileIds = Array.from(dekStore.fileDEKs.keys());
2314
+ }
2315
+ const _clientKEK = clientKEK ? hexToBytes6(clientKEK) : getClientKEK();
2316
+ const encryptedFileDEKs = fileIds.reduce((acc, fileId) => {
2317
+ const fileDEK = dekStore.fileDEKs?.get(fileId);
2318
+ if (!fileDEK) {
2319
+ return acc;
2320
+ }
2321
+ const unwrappedFileDEK = unwrapDEK(_clientKEK, fileDEK);
2322
+ const { encrypted: encryptedFileDEK, nonce: fileDEKNonce } = encryptPayload(sharedSecret, unwrappedFileDEK);
2323
+ acc.push({
2324
+ fileId,
2325
+ encryptedDEK: bytesToHex6(encryptedFileDEK),
2326
+ nonce: bytesToHex6(fileDEKNonce)
2327
+ });
2328
+ return acc;
2329
+ }, []);
2330
+ if (!dekStore.ragDEK) {
2331
+ throw new Error("RAG DEK not found in dekStore. Please upload at least one file with ragIndex: true to initialize RAG.");
2332
+ }
2333
+ if (!dekStore.ragVersion) {
2334
+ throw new Error("RAG Version not found in dekStore. Please upload at least one file with ragIndex: true to initialize RAG.");
2335
+ }
2336
+ const ragDEK = unwrapDEK(_clientKEK, dekStore.ragDEK);
2337
+ const { encrypted: encryptedRagDEK, nonce: ragDEKNonce } = encryptPayload(sharedSecret, ragDEK);
2338
+ const { encrypted: encryptedRagVersion, nonce: ragVersionNonce } = encryptPayload(sharedSecret, dekStore.ragVersion);
2339
+ const body = {
2340
+ cipherText: bytesToHex6(cipherText),
2341
+ encryptedParams: bytesToHex6(encrypted),
2342
+ nonce: bytesToHex6(nonce),
2343
+ encryptedDEK: bytesToHex6(encryptedDEK),
2344
+ dekNonce: bytesToHex6(dekNonce),
2345
+ encryptedFileDEKs,
2346
+ encryptedRagDEK: bytesToHex6(encryptedRagDEK),
2347
+ ragDEKNonce: bytesToHex6(ragDEKNonce),
2348
+ encryptedRagVersion: bytesToHex6(encryptedRagVersion),
2349
+ ragVersionNonce: bytesToHex6(ragVersionNonce)
2350
+ };
2351
+ const response = await callToolRequest(toolName, body, apiKey, timeoutMs, attest2);
2352
+ return decryptPayload(response.encryptedResponse, sharedSecret, hexToBytes6(response.nonce));
2353
+ }
2354
+ async function callTool(toolName, params, apiKey, dekStore, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, attest2 = true) {
2355
+ if (FILE_OUTPUT_TOOLS.includes(toolName)) {
2356
+ return callFileOutputTool(toolName, params, apiKey, dekStore, clientKEK, timeoutMs, attest2);
2357
+ } else if (FILE_INPUT_TOOLS.includes(toolName)) {
2358
+ return callFileInputTool(toolName, params, apiKey, dekStore, clientKEK, timeoutMs, attest2);
2359
+ } else if (RAG_TOOLS.includes(toolName)) {
2360
+ return callRagTool(toolName, params, apiKey, dekStore, clientKEK, timeoutMs, attest2);
2361
+ } else {
2362
+ return callSimpleTool(toolName, params, apiKey, timeoutMs, attest2);
2363
+ }
2364
+ }
2365
+ function createToolsClient(apiKey, dekStore, clientKEK, timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, attest2 = true) {
2366
+ return {
2367
+ generateImage: (params) => callTool("generateImage", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2368
+ audioGenerateFromText: (params) => callTool("audioGenerateFromText", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2369
+ createFileForUser: (params) => callTool("createFileForUser", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2370
+ imageDescribeAndCaption: (params) => callTool("imageDescribeAndCaption", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2371
+ imageDescribeAndCaptionFallback: (params) => callTool("imageDescribeAndCaptionFallback", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2372
+ videoDescribeAndCaption: (params) => callTool("videoDescribeAndCaption", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2373
+ getPDFContent: (params) => callTool("getPDFContent", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2374
+ getTextDocumentContent: (params) => callTool("getTextDocumentContent", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2375
+ transcribeAudioToText: (params) => callTool("transcribeAudioToText", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2376
+ transcribeAudioWithDiarization: (params) => callTool("transcribeAudioWithDiarization", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2377
+ audioDiarization: (params) => callTool("audioDiarization", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2378
+ getFileContentOCR: (params) => callTool("getFileContentOCR", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2379
+ getSpreadsheetContent: (params) => callTool("getSpreadsheetContent", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2380
+ getDataFileContent: (params) => callTool("getDataFileContent", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2381
+ getPowerPointContent: (params) => callTool("getPowerPointContent", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2382
+ getTime: (params) => callTool("getTime", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2383
+ webSearchTool: (params) => callTool("webSearchTool", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2384
+ webPageScraperTool: (params) => callTool("webPageScraperTool", params, apiKey, dekStore, clientKEK, timeoutMs, attest2),
2385
+ searchRag: (params) => callTool("searchRag", params, apiKey, dekStore, clientKEK, timeoutMs, attest2)
2386
+ };
2387
+ }
2388
+
2389
+ // src/core.ts
2390
+ async function createRvencClient(options) {
2391
+ const {
2392
+ apiKey,
2393
+ clientKEK,
2394
+ requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS,
2395
+ maxBufferSize = DEFAULT_MAX_BUFFER_SIZE,
2396
+ attest: attest2 = true
2397
+ } = options;
2398
+ if (options.config?.endpoints !== undefined) {
2399
+ Object.assign(endpoints, options.config.endpoints);
2400
+ }
2401
+ let encryptionKeys;
2402
+ try {
2403
+ encryptionKeys = options.encryptionKeys ?? await generateEncryptionKeys(requestTimeoutMs);
2404
+ } catch (error) {
2405
+ throw new Error(`Failed to initialize encryption keys: ${error instanceof Error ? error.message : error}`);
2406
+ }
2407
+ const dekStore = options.dekStore ?? initializeDEKStore(clientKEK);
2408
+ const client = createRvencChatClient(apiKey, encryptionKeys, requestTimeoutMs, maxBufferSize, attest2, options.config?.openAIClientOptions ?? {});
2409
+ client.files = createFilesClient(apiKey, dekStore, clientKEK, requestTimeoutMs);
2410
+ client.tools = createToolsClient(apiKey, dekStore, clientKEK, requestTimeoutMs, attest2);
2411
+ client.audio = createAudioClient(apiKey, encryptionKeys, requestTimeoutMs, attest2);
2412
+ client.models = createModelsClient(apiKey, requestTimeoutMs);
2413
+ client.dekStore = dekStore;
2414
+ return client;
2415
+ }
2416
+ var core_default = createRvencClient;
2417
+
2418
+ // src/server/runtime.ts
2419
+ var DEFAULT_HOST = process.env.HOST ?? "127.0.0.1";
2420
+ var DEFAULT_PORT = process.env.PORT ? Number.parseInt(process.env.PORT, 10) : 8000;
2421
+ var CLIENT_CACHE_MAX = (() => {
2422
+ let cacheTTL = 256;
2423
+ const raw = process.env.CLIENT_CACHE_MAX;
2424
+ if (raw) {
2425
+ const _cacheTTL = Number.parseInt(raw, 10);
2426
+ if (Number.isSafeInteger(_cacheTTL) && _cacheTTL > 0)
2427
+ cacheTTL = _cacheTTL;
2428
+ }
2429
+ return cacheTTL;
2430
+ })();
2431
+ var serverProxyUrl = process.env.PROXY_URL;
2432
+ var serverEnclaveUrl = process.env.ENCLAVE_URL;
2433
+ var serverKek = process.env.CLIENT_KEK;
2434
+ var serverAttest = true;
2435
+ var clientCache = new Map;
2436
+ var storage = multer.memoryStorage();
2437
+ var audioUpload = multer({
2438
+ storage,
2439
+ limits: { fileSize: 25 * 1024 * 1024 }
2440
+ });
2441
+ function applyServerOptions(options) {
2442
+ const { proxyUrl, enclaveUrl, kek, attest: attest2 } = options;
2443
+ serverAttest = attest2 !== false;
2444
+ if (proxyUrl) {
2445
+ serverProxyUrl = proxyUrl;
2446
+ }
2447
+ if (enclaveUrl) {
2448
+ serverEnclaveUrl = enclaveUrl;
2449
+ }
2450
+ if (kek) {
2451
+ serverKek = kek;
2452
+ }
2453
+ }
2454
+ async function getOrCreateRvencClient(apiKey) {
2455
+ const existing = clientCache.get(apiKey);
2456
+ if (existing)
2457
+ return existing;
2458
+ const client = await core_default({
2459
+ apiKey,
2460
+ clientKEK: serverKek,
2461
+ attest: serverAttest,
2462
+ config: {
2463
+ endpoints: {
2464
+ enclave: serverEnclaveUrl,
2465
+ proxy: serverProxyUrl
2466
+ }
2467
+ }
2468
+ });
2469
+ clientCache.set(apiKey, client);
2470
+ if (clientCache.size > CLIENT_CACHE_MAX) {
2471
+ const oldest = clientCache.keys().next().value;
2472
+ if (oldest !== undefined)
2473
+ clientCache.delete(oldest);
2474
+ }
2475
+ return client;
2476
+ }
2477
+
2478
+ // src/openai/routes.ts
2479
+ function extractApiKey(req) {
2480
+ const authHeader = req.headers.authorization;
2481
+ if (!authHeader) {
2482
+ return null;
2483
+ }
2484
+ if (authHeader.startsWith("Bearer ")) {
2485
+ return authHeader.slice(7);
2486
+ }
2487
+ return authHeader;
2488
+ }
2489
+ function sendUnauthorized(res) {
2490
+ res.status(401).json({
2491
+ error: {
2492
+ message: 'Missing Authorization header. Expected format: "Bearer <api-key>" or "<api-key>"',
2493
+ type: "invalid_request_error",
2494
+ code: "invalid_api_key"
2495
+ }
2496
+ });
2497
+ }
2498
+ function sendServerError(res, error) {
2499
+ const err = error;
2500
+ res.status(err.status ?? 500).json({
2501
+ error: {
2502
+ message: err.message ?? "Internal server error",
2503
+ type: err.type ?? "server_error",
2504
+ code: err.code
2505
+ }
2506
+ });
2507
+ }
2508
+ function openAIOwnedBy(modelId) {
2509
+ const slash = modelId.indexOf("/");
2510
+ if (slash > 0) {
2511
+ return modelId.slice(0, slash);
2512
+ }
2513
+ return "prem";
2514
+ }
2515
+ function isoToUnix(iso) {
2516
+ const t = Date.parse(iso);
2517
+ if (!Number.isFinite(t)) {
2518
+ return 0;
2519
+ }
2520
+ return Math.floor(t / 1000);
2521
+ }
2522
+ function registerOpenAICompatRoutes(router, deps) {
2523
+ router.get("/v1/models", async (req, res) => {
2524
+ try {
2525
+ const apiKey = extractApiKey(req);
2526
+ if (!apiKey) {
2527
+ return sendUnauthorized(res);
2528
+ }
2529
+ const client = await deps.getOrCreateClient(apiKey);
2530
+ const all = await client.models.list();
2531
+ const data = all.filter((m) => m.enabled !== 0).map((m) => ({
2532
+ id: m.model,
2533
+ object: "model",
2534
+ created: isoToUnix(m.created_at),
2535
+ owned_by: openAIOwnedBy(m.model)
2536
+ }));
2537
+ res.json({ object: "list", data });
2538
+ } catch (error) {
2539
+ sendServerError(res, error);
2540
+ }
2541
+ });
2542
+ router.post("/v1/chat/completions", async (req, res) => {
2543
+ try {
2544
+ const apiKey = extractApiKey(req);
2545
+ if (!apiKey) {
2546
+ return sendUnauthorized(res);
2547
+ }
2548
+ const client = await deps.getOrCreateClient(apiKey);
2549
+ const params = req.body;
2550
+ const completion = await client.chat.completions.create(params);
2551
+ if (params.stream) {
2552
+ res.setHeader("Content-Type", "text/event-stream");
2553
+ res.setHeader("Cache-Control", "no-cache");
2554
+ res.setHeader("Connection", "keep-alive");
2555
+ if (completion && typeof completion === "object" && Symbol.asyncIterator in completion) {
2556
+ try {
2557
+ for await (const chunk of completion) {
2558
+ res.write(`data: ${JSON.stringify(chunk)}
2559
+
2560
+ `);
2561
+ }
2562
+ res.write(`data: [DONE]
2563
+
2564
+ `);
2565
+ res.end();
2566
+ } catch (streamErr) {
2567
+ if (!res.headersSent) {
2568
+ sendServerError(res, streamErr);
2569
+ } else {
2570
+ res.end();
2571
+ }
2572
+ }
2573
+ } else {
2574
+ res.write(`data: ${JSON.stringify(completion)}
2575
+
2576
+ `);
2577
+ res.write(`data: [DONE]
2578
+
2579
+ `);
2580
+ res.end();
2581
+ }
2582
+ } else {
2583
+ res.json(completion);
2584
+ }
2585
+ } catch (error) {
2586
+ sendServerError(res, error);
2587
+ }
2588
+ });
2589
+ router.post("/v1/audio/transcriptions", audioUpload.single("file"), async (req, res) => {
2590
+ try {
2591
+ const apiKey = extractApiKey(req);
2592
+ if (!apiKey) {
2593
+ return sendUnauthorized(res);
2594
+ }
2595
+ if (!req.file) {
2596
+ return res.status(400).json({
2597
+ error: {
2598
+ message: "Missing required file parameter",
2599
+ type: "invalid_request_error"
2600
+ }
2601
+ });
2602
+ }
2603
+ const client = await deps.getOrCreateClient(apiKey);
2604
+ const file = new File([req.file.buffer], req.file.originalname, {
2605
+ type: req.file.mimetype
2606
+ });
2607
+ const params = {
2608
+ file,
2609
+ model: req.body.model
2610
+ };
2611
+ if (req.body.language) {
2612
+ params.language = req.body.language;
2613
+ }
2614
+ if (req.body.prompt) {
2615
+ params.prompt = req.body.prompt;
2616
+ }
2617
+ if (req.body.response_format) {
2618
+ params.response_format = req.body.response_format;
2619
+ }
2620
+ if (req.body.temperature) {
2621
+ params.temperature = parseFloat(req.body.temperature);
2622
+ }
2623
+ if (req.body.timestamp_granularities) {
2624
+ params.timestamp_granularities = Array.isArray(req.body.timestamp_granularities) ? req.body.timestamp_granularities : JSON.parse(req.body.timestamp_granularities);
2625
+ }
2626
+ const transcription = await client.audio.transcriptions.create(params);
2627
+ res.json(transcription);
2628
+ } catch (error) {
2629
+ sendServerError(res, error);
2630
+ }
2631
+ });
2632
+ router.post("/v1/audio/translations", audioUpload.single("file"), async (req, res) => {
2633
+ try {
2634
+ const apiKey = extractApiKey(req);
2635
+ if (!apiKey) {
2636
+ return sendUnauthorized(res);
2637
+ }
2638
+ if (!req.file) {
2639
+ return res.status(400).json({
2640
+ error: {
2641
+ message: "Missing required file parameter",
2642
+ type: "invalid_request_error"
2643
+ }
2644
+ });
2645
+ }
2646
+ const client = await deps.getOrCreateClient(apiKey);
2647
+ const file = new File([req.file.buffer], req.file.originalname, {
2648
+ type: req.file.mimetype
2649
+ });
2650
+ const params = {
2651
+ file,
2652
+ model: req.body.model
2653
+ };
2654
+ if (req.body.prompt) {
2655
+ params.prompt = req.body.prompt;
2656
+ }
2657
+ if (req.body.response_format) {
2658
+ params.response_format = req.body.response_format;
2659
+ }
2660
+ if (req.body.temperature) {
2661
+ params.temperature = parseFloat(req.body.temperature);
2662
+ }
2663
+ const translation = await client.audio.translations.create(params);
2664
+ res.json(translation);
2665
+ } catch (error) {
2666
+ sendServerError(res, error);
2667
+ }
2668
+ });
2669
+ }
2670
+
2671
+ // src/server/route-prefix.ts
2672
+ function normalizeRoutePrefix(raw) {
2673
+ if (raw == null) {
2674
+ return "";
2675
+ }
2676
+ return new URL(String(raw).trim(), "http://localhost").pathname || "";
2677
+ }
2678
+ var DEFAULT_OPENAI_ROUTE_PREFIX_BOTH = "/openai";
2679
+ var DEFAULT_ANTHROPIC_ROUTE_PREFIX_BOTH = "/anthropic";
2680
+ function resolvePrefixesForCompat(compat, openaiRaw, anthropicRaw) {
2681
+ const oNorm = normalizeRoutePrefix(openaiRaw);
2682
+ const aNorm = normalizeRoutePrefix(anthropicRaw);
2683
+ if (compat === "both") {
2684
+ const openaiPrefix = oNorm || DEFAULT_OPENAI_ROUTE_PREFIX_BOTH;
2685
+ const anthropicPrefix = aNorm || DEFAULT_ANTHROPIC_ROUTE_PREFIX_BOTH;
2686
+ if (openaiPrefix === anthropicPrefix) {
2687
+ throw new Error(`When compat is "both", openaiRoutePrefix and anthropicRoutePrefix must differ (both resolved to "${openaiPrefix}").`);
2688
+ }
2689
+ return { openaiPrefix, anthropicPrefix };
2690
+ }
2691
+ if (compat === "openai") {
2692
+ return { openaiPrefix: oNorm, anthropicPrefix: "" };
2693
+ }
2694
+ return { openaiPrefix: "", anthropicPrefix: aNorm };
2695
+ }
2696
+ function prefixedRoute(prefix, path) {
2697
+ const s = path.startsWith("/") ? path : `/${path}`;
2698
+ if (!prefix) {
2699
+ return s;
2700
+ }
2701
+ return `${prefix}${s}`;
2702
+ }
2703
+
2704
+ // src/server/discovery.ts
2705
+ function registerApiDiscoveryRoute(app, mount) {
2706
+ const {
2707
+ openai: mountOpenAI,
2708
+ anthropic: mountAnthropic,
2709
+ openaiPrefix,
2710
+ anthropicPrefix
2711
+ } = mount;
2712
+ app.get("/", (_, res) => {
2713
+ const endpoints2 = {};
2714
+ if (mountOpenAI) {
2715
+ endpoints2.chat_completions = `POST ${prefixedRoute(openaiPrefix, "/v1/chat/completions")}`;
2716
+ endpoints2.audio_transcriptions = `POST ${prefixedRoute(openaiPrefix, "/v1/audio/transcriptions")}`;
2717
+ endpoints2.audio_translations = `POST ${prefixedRoute(openaiPrefix, "/v1/audio/translations")}`;
2718
+ endpoints2.models = `GET ${prefixedRoute(openaiPrefix, "/v1/models")}`;
2719
+ }
2720
+ if (mountAnthropic) {
2721
+ endpoints2.messages = `POST ${prefixedRoute(anthropicPrefix, "/v1/messages")}`;
2722
+ endpoints2.messages_count_tokens = `POST ${prefixedRoute(anthropicPrefix, "/v1/messages/count_tokens")}`;
2723
+ endpoints2.anthropic_models = `GET ${prefixedRoute(anthropicPrefix, "/v1/models")}`;
2724
+ endpoints2.anthropic_model_get = `GET ${prefixedRoute(anthropicPrefix, "/v1/models/{model_id}")}`;
2725
+ }
2726
+ const labels = [];
2727
+ if (mountOpenAI) {
2728
+ labels.push("OpenAI-compatible");
2729
+ }
2730
+ if (mountAnthropic) {
2731
+ labels.push("Anthropic Messages-compatible");
2732
+ }
2733
+ res.json({
2734
+ message: `Rvenc API Server (${labels.join(" + ")})`,
2735
+ version: "1.0.0",
2736
+ compat: resolveCompatLabel(mount),
2737
+ route_prefixes: buildRoutePrefixesPayload(mountOpenAI, mountAnthropic, openaiPrefix, anthropicPrefix),
2738
+ endpoints: endpoints2
2739
+ });
2740
+ });
2741
+ }
2742
+ function buildRoutePrefixesPayload(mountOpenAI, mountAnthropic, openaiPrefix, anthropicPrefix) {
2743
+ const out = {};
2744
+ if (mountOpenAI) {
2745
+ out.openai = openaiPrefix || "/";
2746
+ }
2747
+ if (mountAnthropic) {
2748
+ out.anthropic = anthropicPrefix || "/";
2749
+ }
2750
+ if (Object.keys(out).length === 0) {
2751
+ return;
2752
+ }
2753
+ return out;
2754
+ }
2755
+ function resolveCompatLabel(mount) {
2756
+ if (mount.openai && mount.anthropic) {
2757
+ return "both";
2758
+ }
2759
+ if (mount.anthropic) {
2760
+ return "anthropic";
2761
+ }
2762
+ return "openai";
2763
+ }
2764
+
2765
+ // src/server/create-app.ts
2766
+ var rvencDeps = {
2767
+ getOrCreateClient: getOrCreateRvencClient
2768
+ };
2769
+ function resolveJsonBodyLimit(override) {
2770
+ if (override != null && String(override).trim() !== "") {
2771
+ return String(override).trim();
2772
+ }
2773
+ const env = process.env.JSON_BODY_LIMIT;
2774
+ if (env != null && env !== "") {
2775
+ return env;
2776
+ }
2777
+ return "32mb";
2778
+ }
2779
+ function resolveCreateServerInput(compatOrOptions) {
2780
+ if (typeof compatOrOptions === "string") {
2781
+ const compat2 = compatOrOptions;
2782
+ const { openaiPrefix: openaiPrefix2, anthropicPrefix: anthropicPrefix2 } = resolvePrefixesForCompat(compat2, undefined, undefined);
2783
+ return {
2784
+ compat: compat2,
2785
+ openaiPrefix: openaiPrefix2,
2786
+ anthropicPrefix: anthropicPrefix2,
2787
+ jsonBodyLimit: resolveJsonBodyLimit()
2788
+ };
2789
+ }
2790
+ const compat = compatOrOptions.compat ?? "openai";
2791
+ const { openaiPrefix, anthropicPrefix } = resolvePrefixesForCompat(compat, compatOrOptions.openaiRoutePrefix, compatOrOptions.anthropicRoutePrefix);
2792
+ return {
2793
+ compat,
2794
+ openaiPrefix,
2795
+ anthropicPrefix,
2796
+ jsonBodyLimit: resolveJsonBodyLimit(compatOrOptions.jsonBodyLimit)
2797
+ };
2798
+ }
2799
+ function httpErrorStatus(err) {
2800
+ if (err && typeof err === "object") {
2801
+ const o = err;
2802
+ const s = o.status ?? o.statusCode;
2803
+ if (typeof s === "number" && s >= 400 && s < 600) {
2804
+ return s;
2805
+ }
2806
+ }
2807
+ return 500;
2808
+ }
2809
+ function mountRouter(app, prefix, router) {
2810
+ app.use(prefix || "/", router);
2811
+ }
2812
+ function createServerApp(compatOrOptions = "openai") {
2813
+ const { compat, openaiPrefix, anthropicPrefix, jsonBodyLimit } = resolveCreateServerInput(compatOrOptions);
2814
+ const mountOpenAI = compat === "openai" || compat === "both";
2815
+ const mountAnthropic = compat === "anthropic" || compat === "both";
2816
+ const app = express();
2817
+ app.use(express.json({ limit: jsonBodyLimit }));
2818
+ registerApiDiscoveryRoute(app, {
2819
+ openai: mountOpenAI,
2820
+ anthropic: mountAnthropic,
2821
+ openaiPrefix,
2822
+ anthropicPrefix
2823
+ });
2824
+ if (mountOpenAI) {
2825
+ const router = express.Router();
2826
+ registerOpenAICompatRoutes(router, rvencDeps);
2827
+ mountRouter(app, openaiPrefix, router);
2828
+ }
2829
+ if (mountAnthropic) {
2830
+ const router = express.Router();
2831
+ registerAnthropicMessagesRoute(router, rvencDeps);
2832
+ registerAnthropicCountTokensRoute(router, rvencDeps);
2833
+ registerAnthropicModelsRoute(router, rvencDeps);
2834
+ mountRouter(app, anthropicPrefix, router);
2835
+ }
2836
+ const isAnthropicRequest = (req) => {
2837
+ if (!mountAnthropic) {
2838
+ return false;
2839
+ }
2840
+ if (!mountOpenAI) {
2841
+ return true;
2842
+ }
2843
+ return req.path === anthropicPrefix || req.path.startsWith(`${anthropicPrefix}/`);
2844
+ };
2845
+ app.use((err, req, res, _next) => {
2846
+ const status = httpErrorStatus(err);
2847
+ const message = err instanceof Error ? err.message : "Internal server error";
2848
+ if (isAnthropicRequest(req)) {
2849
+ const requestId = newAnthropicRequestId();
2850
+ res.setHeader("request-id", requestId);
2851
+ res.status(status).json({
2852
+ type: "error",
2853
+ error: {
2854
+ type: httpStatusToAnthropicErrorType(status),
2855
+ message
2856
+ },
2857
+ request_id: requestId
2858
+ });
2859
+ return;
2860
+ }
2861
+ res.status(status).json({
2862
+ error: {
2863
+ message,
2864
+ type: "server_error"
2865
+ }
2866
+ });
2867
+ });
2868
+ app.use((req, res) => {
2869
+ const message = `Route ${req.method} ${req.path} not found`;
2870
+ if (isAnthropicRequest(req)) {
2871
+ const requestId = newAnthropicRequestId();
2872
+ res.setHeader("request-id", requestId);
2873
+ res.status(404).json({
2874
+ type: "error",
2875
+ error: { type: "not_found_error", message },
2876
+ request_id: requestId
2877
+ });
2878
+ return;
2879
+ }
2880
+ res.status(404).json({
2881
+ error: {
2882
+ message,
2883
+ type: "invalid_request_error"
2884
+ }
2885
+ });
2886
+ });
2887
+ return app;
2888
+ }
2889
+ // src/server/start.ts
2890
+ async function startServer(options = {}) {
2891
+ const {
2892
+ host,
2893
+ port,
2894
+ compat: compatOpt,
2895
+ openaiRoutePrefix,
2896
+ anthropicRoutePrefix,
2897
+ jsonBodyLimit
2898
+ } = options;
2899
+ const serverHost = host || DEFAULT_HOST;
2900
+ const serverPort = port || DEFAULT_PORT;
2901
+ const compat = compatOpt ?? "openai";
2902
+ applyServerOptions(options);
2903
+ resolvePrefixesForCompat(compat, openaiRoutePrefix, anthropicRoutePrefix);
2904
+ const app = createServerApp({
2905
+ compat,
2906
+ openaiRoutePrefix,
2907
+ anthropicRoutePrefix,
2908
+ jsonBodyLimit
2909
+ });
2910
+ return new Promise((resolve, reject) => {
2911
+ const server = app.listen(serverPort, serverHost, () => {
2912
+ resolve({ close: () => server.close() });
2913
+ });
2914
+ server.on("error", (error) => {
2915
+ if (error && typeof error === "object" && "code" in error && error.code === "EADDRINUSE") {
2916
+ reject(new Error(`Port ${serverPort} is already in use`));
2917
+ } else {
2918
+ reject(error);
2919
+ }
2920
+ });
2921
+ });
2922
+ }
2923
+ // src/server.ts
2924
+ var server_default = createServerApp("both");
2925
+
2926
+ // src/cli.ts
2927
+ var { values } = parseArgs({
2928
+ args: process.argv.slice(2),
2929
+ options: {
2930
+ host: { type: "string", default: "127.0.0.1" },
2931
+ "proxy-url": { type: "string" },
2932
+ "enclave-url": { type: "string" },
2933
+ kek: { type: "string" },
2934
+ port: { type: "string", default: "8000" },
2935
+ "no-attest": { type: "boolean", default: false },
2936
+ compat: { type: "string", default: "openai" },
2937
+ "openai-prefix": { type: "string" },
2938
+ "anthropic-prefix": { type: "string" },
2939
+ "json-body-limit": { type: "string" }
2940
+ },
2941
+ strict: true
2942
+ });
2943
+ var compat = values.compat;
2944
+ if (compat !== "openai" && compat !== "anthropic" && compat !== "both") {
2945
+ console.error(`Invalid --compat "${compat}". Use openai, anthropic, or both.`);
2946
+ process.exit(1);
2947
+ }
2948
+ startServer({
2949
+ host: values.host,
2950
+ proxyUrl: values["proxy-url"],
2951
+ enclaveUrl: values["enclave-url"],
2952
+ kek: values.kek,
2953
+ port: Number(values.port),
2954
+ attest: !values["no-attest"],
2955
+ compat,
2956
+ openaiRoutePrefix: values["openai-prefix"],
2957
+ anthropicRoutePrefix: values["anthropic-prefix"],
2958
+ jsonBodyLimit: values["json-body-limit"]
2959
+ });