fix:
- AES GCM protocol mismatch - better, granular error handling - UUID now uses crypto.randomUUID() - added native mlock addon to improve security - ZeroBuffer uses explicit_bzero now - fixed imports feat: - added unit tests
This commit is contained in:
parent
0b09b9a9c3
commit
c7601b2270
17 changed files with 12600 additions and 164 deletions
|
|
@ -3,7 +3,7 @@
|
|||
* Provides a drop-in replacement for OpenAI's ChatCompletion API with end-to-end encryption
|
||||
*/
|
||||
|
||||
import { SecureCompletionClient } from '../core/SecureCompletionClient';
|
||||
import { SecureCompletionClient, generateUUID } from '../core/SecureCompletionClient';
|
||||
import { ChatCompletionConfig } from '../types/client';
|
||||
import { ChatCompletionRequest, ChatCompletionResponse } from '../types/api';
|
||||
|
||||
|
|
@ -28,22 +28,23 @@ export class SecureChatCompletion {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a chat completion (matches OpenAI API)
|
||||
* @param request Chat completion request
|
||||
* @returns Chat completion response
|
||||
* 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)
|
||||
*/
|
||||
async create(request: ChatCompletionRequest): Promise<ChatCompletionResponse> {
|
||||
// Generate unique payload ID
|
||||
const payloadId = `openai-compat-${this.generatePayloadId()}`;
|
||||
const payloadId = generateUUID();
|
||||
|
||||
// Extract API key from request or use instance key
|
||||
const apiKey = (request as any).api_key || this.apiKey;
|
||||
// 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;
|
||||
};
|
||||
|
||||
// Remove api_key from payload if present (it's in headers)
|
||||
const payload = { ...request };
|
||||
delete (payload as any).api_key;
|
||||
const apiKey = api_key ?? this.apiKey;
|
||||
|
||||
// Validate required fields
|
||||
if (!payload.model) {
|
||||
throw new Error('Missing required field: model');
|
||||
}
|
||||
|
|
@ -51,14 +52,14 @@ export class SecureChatCompletion {
|
|||
throw new Error('Missing or invalid required field: messages');
|
||||
}
|
||||
|
||||
// Send secure request
|
||||
const response = await this.client.sendSecureRequest(
|
||||
payload,
|
||||
payloadId,
|
||||
apiKey
|
||||
apiKey,
|
||||
security_tier
|
||||
);
|
||||
|
||||
return response as ChatCompletionResponse;
|
||||
return response as unknown as ChatCompletionResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -67,13 +68,4 @@ export class SecureChatCompletion {
|
|||
async acreate(request: ChatCompletionRequest): Promise<ChatCompletionResponse> {
|
||||
return this.create(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique payload ID
|
||||
*/
|
||||
private generatePayloadId(): string {
|
||||
const timestamp = Date.now();
|
||||
const random = Math.random().toString(36).substring(2, 15);
|
||||
return `${timestamp}-${random}`;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue