@premai/api-sdk 1.0.47 → 1.0.48

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