2026-01-17 12:02:08 +01:00
|
|
|
/**
|
|
|
|
|
* OpenAI-compatible secure chat completion API
|
|
|
|
|
* Provides a drop-in replacement for OpenAI's ChatCompletion API with end-to-end encryption
|
|
|
|
|
*/
|
|
|
|
|
|
2026-03-04 11:30:44 +01:00
|
|
|
import { SecureCompletionClient, generateUUID } from '../core/SecureCompletionClient';
|
2026-01-17 12:02:08 +01:00
|
|
|
import { ChatCompletionConfig } from '../types/client';
|
|
|
|
|
import { ChatCompletionRequest, ChatCompletionResponse } from '../types/api';
|
|
|
|
|
|
|
|
|
|
export class SecureChatCompletion {
|
|
|
|
|
private client: SecureCompletionClient;
|
|
|
|
|
private apiKey?: string;
|
|
|
|
|
|
|
|
|
|
constructor(config: ChatCompletionConfig = {}) {
|
|
|
|
|
const {
|
2026-04-01 13:38:45 +02:00
|
|
|
baseUrl = 'https://api.nomyo.ai:12435',
|
2026-01-17 12:02:08 +01:00
|
|
|
allowHttp = false,
|
|
|
|
|
apiKey,
|
|
|
|
|
secureMemory = true,
|
fix:
Added DisposedError
Wrapped zeroMemory() in its own try/catch in finally
Generic error message; fixed ArrayBufferLike TypeScript type issue
Generic error message; password/salt/IV wrapped in SecureByteContext
Password ≥8 chars enforced; zeroKeys(); rotateKeys(); debug-gated logs; TS type fix
Zero source ArrayBuffer after req.write()
Added timeout, debug, keyRotationInterval, keyRotationDir, keyRotationPassword
dispose(), assertNotDisposed(), startKeyRotationTimer(), rotateKeys(); Promise-mutex on ensureKeys(); new URL() validation; CR/LF API key check; server error detail truncation; response schema validation; all console.log behind debugMode
Propagates new config fields; dispose()
Tests for dispose, timer, header injection, URL validation, error sanitization, debug flag
Tests for generic error messages, password validation, zeroKeys()
2026-04-01 14:28:05 +02:00
|
|
|
timeout,
|
|
|
|
|
debug,
|
|
|
|
|
keyRotationInterval,
|
|
|
|
|
keyRotationDir,
|
|
|
|
|
keyRotationPassword,
|
2026-01-17 12:02:08 +01:00
|
|
|
} = config;
|
|
|
|
|
|
|
|
|
|
this.apiKey = apiKey;
|
|
|
|
|
this.client = new SecureCompletionClient({
|
|
|
|
|
routerUrl: baseUrl,
|
|
|
|
|
allowHttp,
|
|
|
|
|
secureMemory,
|
fix:
Added DisposedError
Wrapped zeroMemory() in its own try/catch in finally
Generic error message; fixed ArrayBufferLike TypeScript type issue
Generic error message; password/salt/IV wrapped in SecureByteContext
Password ≥8 chars enforced; zeroKeys(); rotateKeys(); debug-gated logs; TS type fix
Zero source ArrayBuffer after req.write()
Added timeout, debug, keyRotationInterval, keyRotationDir, keyRotationPassword
dispose(), assertNotDisposed(), startKeyRotationTimer(), rotateKeys(); Promise-mutex on ensureKeys(); new URL() validation; CR/LF API key check; server error detail truncation; response schema validation; all console.log behind debugMode
Propagates new config fields; dispose()
Tests for dispose, timer, header injection, URL validation, error sanitization, debug flag
Tests for generic error messages, password validation, zeroKeys()
2026-04-01 14:28:05 +02:00
|
|
|
...(timeout !== undefined && { timeout }),
|
|
|
|
|
...(debug !== undefined && { debug }),
|
|
|
|
|
...(keyRotationInterval !== undefined && { keyRotationInterval }),
|
|
|
|
|
...(keyRotationDir !== undefined && { keyRotationDir }),
|
|
|
|
|
...(keyRotationPassword !== undefined && { keyRotationPassword }),
|
2026-01-17 12:02:08 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-04 11:30:44 +01:00
|
|
|
* Create a chat completion (matches OpenAI API).
|
|
|
|
|
*
|
|
|
|
|
* Supports additional NOMYO-specific fields:
|
|
|
|
|
* - `security_tier`: "standard" | "high" | "maximum" — controls hardware routing
|
|
|
|
|
* - `api_key`: per-request API key override (takes precedence over constructor key)
|
2026-01-17 12:02:08 +01:00
|
|
|
*/
|
|
|
|
|
async create(request: ChatCompletionRequest): Promise<ChatCompletionResponse> {
|
2026-03-04 11:30:44 +01:00
|
|
|
const payloadId = generateUUID();
|
2026-01-17 12:02:08 +01:00
|
|
|
|
2026-03-04 11:30:44 +01:00
|
|
|
// Extract NOMYO-specific fields that must not go into the encrypted payload
|
|
|
|
|
const { security_tier, api_key, ...payload } = request as ChatCompletionRequest & {
|
|
|
|
|
security_tier?: string;
|
|
|
|
|
api_key?: string;
|
|
|
|
|
};
|
2026-01-17 12:02:08 +01:00
|
|
|
|
2026-03-04 11:30:44 +01:00
|
|
|
const apiKey = api_key ?? this.apiKey;
|
2026-01-17 12:02:08 +01:00
|
|
|
|
|
|
|
|
if (!payload.model) {
|
|
|
|
|
throw new Error('Missing required field: model');
|
|
|
|
|
}
|
|
|
|
|
if (!payload.messages || !Array.isArray(payload.messages)) {
|
|
|
|
|
throw new Error('Missing or invalid required field: messages');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await this.client.sendSecureRequest(
|
|
|
|
|
payload,
|
|
|
|
|
payloadId,
|
2026-03-04 11:30:44 +01:00
|
|
|
apiKey,
|
|
|
|
|
security_tier
|
2026-01-17 12:02:08 +01:00
|
|
|
);
|
|
|
|
|
|
2026-03-04 11:30:44 +01:00
|
|
|
return response as unknown as ChatCompletionResponse;
|
2026-01-17 12:02:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Async alias for create() (for compatibility with OpenAI SDK)
|
|
|
|
|
*/
|
|
|
|
|
async acreate(request: ChatCompletionRequest): Promise<ChatCompletionResponse> {
|
|
|
|
|
return this.create(request);
|
|
|
|
|
}
|
fix:
Added DisposedError
Wrapped zeroMemory() in its own try/catch in finally
Generic error message; fixed ArrayBufferLike TypeScript type issue
Generic error message; password/salt/IV wrapped in SecureByteContext
Password ≥8 chars enforced; zeroKeys(); rotateKeys(); debug-gated logs; TS type fix
Zero source ArrayBuffer after req.write()
Added timeout, debug, keyRotationInterval, keyRotationDir, keyRotationPassword
dispose(), assertNotDisposed(), startKeyRotationTimer(), rotateKeys(); Promise-mutex on ensureKeys(); new URL() validation; CR/LF API key check; server error detail truncation; response schema validation; all console.log behind debugMode
Propagates new config fields; dispose()
Tests for dispose, timer, header injection, URL validation, error sanitization, debug flag
Tests for generic error messages, password validation, zeroKeys()
2026-04-01 14:28:05 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Release resources: stop key rotation timer and zero in-memory key material.
|
|
|
|
|
*/
|
|
|
|
|
dispose(): void {
|
|
|
|
|
this.client.dispose();
|
|
|
|
|
}
|
2026-01-17 12:02:08 +01:00
|
|
|
}
|