- 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
71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
/**
|
|
* OpenAI-compatible secure chat completion API
|
|
* Provides a drop-in replacement for OpenAI's ChatCompletion API with end-to-end encryption
|
|
*/
|
|
|
|
import { SecureCompletionClient, generateUUID } from '../core/SecureCompletionClient';
|
|
import { ChatCompletionConfig } from '../types/client';
|
|
import { ChatCompletionRequest, ChatCompletionResponse } from '../types/api';
|
|
|
|
export class SecureChatCompletion {
|
|
private client: SecureCompletionClient;
|
|
private apiKey?: string;
|
|
|
|
constructor(config: ChatCompletionConfig = {}) {
|
|
const {
|
|
baseUrl = 'https://api.nomyo.ai:12434',
|
|
allowHttp = false,
|
|
apiKey,
|
|
secureMemory = true,
|
|
} = config;
|
|
|
|
this.apiKey = apiKey;
|
|
this.client = new SecureCompletionClient({
|
|
routerUrl: baseUrl,
|
|
allowHttp,
|
|
secureMemory,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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> {
|
|
const payloadId = generateUUID();
|
|
|
|
// 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;
|
|
};
|
|
|
|
const apiKey = api_key ?? this.apiKey;
|
|
|
|
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,
|
|
apiKey,
|
|
security_tier
|
|
);
|
|
|
|
return response as unknown as ChatCompletionResponse;
|
|
}
|
|
|
|
/**
|
|
* Async alias for create() (for compatibility with OpenAI SDK)
|
|
*/
|
|
async acreate(request: ChatCompletionRequest): Promise<ChatCompletionResponse> {
|
|
return this.create(request);
|
|
}
|
|
}
|