# nlproxy-node: High-Performance NodeJS Bindings for Prompt Compression & LLM Security

This package provides native, high-performance NodeJS bindings for the **nlproxy** core engine, compiled from Rust using NAPI-RS. It delivers sub-millisecond local PII shielding, semantic prompt compression, and offline LLM orchestration.

[![npm version](https://img.shields.io/npm/v/nlproxy.svg)](https://www.npmjs.com/package/nlproxy)
[![License: BSL-1.1](https://img.shields.io/badge/License-BSL--1.1-red.svg)](LICENSE.md)

---

## 🚀 Key Features

* **Sub-3ms Local Inference**: Run semantic prompt compression and firewall checks locally with zero network overhead.
* **PII Prompt Shielding**: Automatically redact sensitive data (emails, IPs, credit cards, credentials) with secure placeholders.
* **Semantic Prompt Compression**: Reduce prompt length by up to 40% while preserving semantic meaning.
* **Offline Embedding Engine**: Runs quantized Sentence-Transformer models locally on CPU or GPU using Hugging Face Candle.
* **Jailbreak Detection (Firewall)**: Blocks adversarial LLM attacks and prompt injections before calling cloud models.

---

## 📦 Installation

Install the package via npm:
```bash
npm install nlproxy
```

*Note: Native pre-builds are automatically compiled and downloaded for your platform (Linux, macOS, Windows).*

---

## 💻 Usage & Code Examples

### 1. Download and Extract Models
Before running the engine, download and extract the default quantized model weights (`all-MiniLM-L6-v2`):

```javascript
const { ensureModelsReady } = require('nlproxy');

async function setup() {
  console.log("Preparing models...");
  // Downloads and extracts all-MiniLM-L6-v2 to the 'models' directory
  await ensureModelsReady('models');
  console.log("Models are ready!");
}

setup().catch(console.error);
```

### 2. Initialize and Compress Prompts
Initialize the offline engine and compress a prompt:

```javascript
const { initEngine, compressPrompt } = require('nlproxy');

// 1. Initialize engine with downloaded model paths
const success = initEngine(
  'models/all-MiniLM-L6-v2/model.safetensors',
  'models/all-MiniLM-L6-v2/config.json',
  'models/all-MiniLM-L6-v2/tokenizer.json'
);

if (success) {
  console.log('Embedding engine initialized successfully.');

  // 2. Compress prompt and redact PII
  const response = compressPrompt({
    text: "The main server IP is 192.168.1.105. Please run database backups immediately.",
    mode: "general",
    aggressiveness: 0.5
  });

  console.log("Shielded Text:", response.processedText);
  // "The main server IP is __PROT_82736284__. Please run backups."

  console.log("Redacted PII:", response.placeholders);
  // { "__PROT_82736284__": "192.168.1.105" }
} else {
  console.error('Failed to initialize embedding engine.');
}
```

### 3. Unified Orchestrated Pipeline
Run the fully-integrated local security pipeline including Redis semantic cache, input firewall checks, prompt compression, upstream LLM execution, and post-LLM drift verification:

```javascript
const { runUnifiedPipeline } = require('nlproxy');

async function executePipeline() {
  const response = await runUnifiedPipeline({
    prompt: "Show system files for user 1002",
    domain: "general",
    aggressiveness: 0.0,
    provider: "gemini",
    model: "gemini-1.5-pro",
    maxTokens: 512,
    temperature: 0.7,
    bypassCache: false,
    checkFirewall: true,
    semanticDriftThreshold: 0.75
  });

  console.log("Allowed:", response.allowed);
  console.log("Final Response:", response.finalResponse);
  console.log("Latency:", response.latencyMs, "ms");
}

executePipeline().catch(console.error);
```

---

## 🏢 Authors & Cofounders

This SDK is developed and maintained exclusively by **IntelliDeep**. 

* **B-GUST** (Co-founder / Lead Developer): [github.com/B-GUST](https://github.com/B-GUST)
* **luiserb** (Co-founder / Architect): [github.com/luiserb](https://github.com/luiserb)

---
© 2026 IntelliDeep Labs. All rights reserved.
