@ik-firewall/core 1.0.0
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.
- package/README.md +152 -0
- package/dist/index.cjs +2425 -0
- package/dist/index.d.cts +683 -0
- package/dist/index.d.ts +683 -0
- package/dist/index.js +2393 -0
- package/package.json +44 -0
- package/scripts/download-bin.js +56 -0
- package/scripts/download-model.js +51 -0
- package/scripts/master-setup.js +102 -0
- package/scripts/robust-download.js +67 -0
- package/scripts/runtime-emulator.js +104 -0
- package/scripts/setup-runtime.js +44 -0
- package/scripts/start-ai.js +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# @ik-firewall/core 🛡️🧠
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/@ik-firewall%2Fcore)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
**IK Firewall** is an advanced, semantic-driven LLM Middleware and Prompt Optimizer.
|
|
7
|
+
It sits as a protective and optimizing layer between your application and Large Language Models (like OpenAI, Anthropic, DeepSeek, or local models).
|
|
8
|
+
|
|
9
|
+
> **⚠️ DISCLAIMER: LIVE TEST PHASE**
|
|
10
|
+
> This package is currently in a live testing and benchmark phase. While it is fully functional, it is officially unannounced and documentation is subject to rapid changes. Use in production at your own discretion.
|
|
11
|
+
|
|
12
|
+
By analyzing the *Cognitive Depth*, *Metaphorical Scale*, and *Domain* of incoming user requests, the IK Firewall compresses inputs, applies rigorous semantic guardrails (Constitutional AI), and ensures that you get high-fidelity responses from cheaper models (like `gpt-4o-mini`) that rival the quality of expensive flagship models (like `gpt-4o`).
|
|
13
|
+
|
|
14
|
+
## 🚀 Key Features
|
|
15
|
+
|
|
16
|
+
* **Semantic Prompt Compression (`crystallize`):** Removes structural noise and redundant phrasing without losing critical "Immutable Entities" (anchors). Drastically cuts down token usage.
|
|
17
|
+
* **Deep Cognitive Audit (`analyze`):** Classifies the specific required Intelligence Quotient (IQ Tier), Intent Mode (Engineering, Legal, Medical, Finance), and Abstract/Directness ratios of user input.
|
|
18
|
+
* **Provider-Agnostic Routing:** Supports OpenAI, Anthropic, DeepSeek, Google Gemini, and fully offline local models (`llama.cpp`).
|
|
19
|
+
* **Heuristic Gatekeeper:** Deflects simple inputs and flags unsafe or out-of-bounds requests before hitting expensive APIs, using an active `safeMode`.
|
|
20
|
+
* **Automated Usage Tracking:** Built-in calculation of token usage, latency, and estimated cost savings with pluggable telemetry hooks.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 📦 Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @ik-firewall/core
|
|
28
|
+
# or
|
|
29
|
+
yarn add @ik-firewall/core
|
|
30
|
+
# or
|
|
31
|
+
pnpm add @ik-firewall/core
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 🛠️ Quick Start
|
|
37
|
+
|
|
38
|
+
### 1. Initialization and Configuration
|
|
39
|
+
|
|
40
|
+
Initialize the core instance anywhere in your Edge, Serverless, or Node.js environment.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { IKFirewallCore } from '@ik-firewall/core';
|
|
44
|
+
|
|
45
|
+
// Create a globally accessible singleton instance
|
|
46
|
+
const ik = IKFirewallCore.getInstance({
|
|
47
|
+
aggressiveness: 0.15, // How hard to compress the prompt (0.0 to 1.0)
|
|
48
|
+
precisionBias: 3.5, // Threshold to trigger higher IQ logic
|
|
49
|
+
balance: 0.5, // 0.0 (Speed/Efficiency) <--> 1.0 (Nuance/Depth)
|
|
50
|
+
safeMode: true, // Enable strict boundary evaluations
|
|
51
|
+
allowedScopes: ['read_only'], // Requires safeMode: true
|
|
52
|
+
providerMode: 'openai' // 'openai', 'anthropic', 'deepseek', 'gemini', 'local', 'hybrid'
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Optional: Pass telemetry hooks
|
|
56
|
+
const ikWithHooks = IKFirewallCore.getInstance(config, {
|
|
57
|
+
onStatus: (msg) => console.log(`[IK_LOG]: ${msg}`),
|
|
58
|
+
onAuditComplete: (metrics) => console.log(`Audit metrics generated!`)
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 2. The Core Pipeline (Audit -> Crystallize -> Execute)
|
|
63
|
+
|
|
64
|
+
The standard flow for processing user input through the firewall before sending it to your target LLM:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
async function handleUserChat(userMessage: string) {
|
|
68
|
+
const ik = IKFirewallCore.getInstance();
|
|
69
|
+
|
|
70
|
+
// 1. Analyze the input semantically
|
|
71
|
+
// This detects the domain, required depth, and creates a cognitive blueprint
|
|
72
|
+
const metrics = await ik.analyze(userMessage);
|
|
73
|
+
|
|
74
|
+
// 2. Compress the prompt using the metrics
|
|
75
|
+
// This removes noise while preserving domain-critical anchors
|
|
76
|
+
const optimizedPrompt = ik.crystallize(userMessage, metrics);
|
|
77
|
+
|
|
78
|
+
// 3. (Optional) Check Constitutional Guardrails
|
|
79
|
+
if (metrics.semanticInsights?.requiresHumanIntervention) {
|
|
80
|
+
throw new Error("IK_GATEKEEPER: Request exceeds permitted autonomy boundaries.");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 4. Send the optimized prompt and the generated "Agent Directive" to your LLM
|
|
84
|
+
// You now pay for fewer tokens but inject deep semantic context!
|
|
85
|
+
const llmResponse = await myLlmProvider.chat({
|
|
86
|
+
systemContext: metrics.agentDirective, // Powerful instructions generated by the IK Analysis
|
|
87
|
+
message: optimizedPrompt // The compressed user input
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return llmResponse;
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 🧬 Understanding IKMetrics
|
|
97
|
+
|
|
98
|
+
The `.analyze()` method is the heart of the engine. It returns an `IKMetrics` object that defines the genetic makeup of the request:
|
|
99
|
+
|
|
100
|
+
```json
|
|
101
|
+
{
|
|
102
|
+
"dm": 1.5,
|
|
103
|
+
"ei": 0.5,
|
|
104
|
+
"precisionBoost": true,
|
|
105
|
+
"agentDirective": "IK_ADAPTER_ACTIVE: TRUE... DOMAIN: ENGINEERING... Režim rada: ANALITIČKI...",
|
|
106
|
+
"semanticInsights": {
|
|
107
|
+
"detectedDomain": "ENGINEERING",
|
|
108
|
+
"intentMode": "ENGINEERING",
|
|
109
|
+
"cognitiveDepth": "L1-DIRECT",
|
|
110
|
+
"requiredIQTier": "TIER_1",
|
|
111
|
+
"immutableEntities": ["React", "Tailwind", "Zod", "sortiranje"],
|
|
112
|
+
"toneVector": { "abstraction": 0.1, "directness": 0.8, "density": 0.9 },
|
|
113
|
+
"boundaryScore": 0,
|
|
114
|
+
"requiresHumanIntervention": false
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### High-Stakes Domains Auto-Detection
|
|
120
|
+
The firewall is extremely conservative by default (`GENERAL` domain) but aggressively switches logic if it detects:
|
|
121
|
+
* `LEGAL`: Demands literal precision, protects arbitration/termination clauses.
|
|
122
|
+
* `MEDICAL`: Protects symptoms and differential diagnoses.
|
|
123
|
+
* `FINANCE`: Secures formulas and risk assessments.
|
|
124
|
+
* `ENGINEERING`: Secures technology stacks and logical steps.
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## 🛡️ Safe Mode & Autonomy Boundaries
|
|
129
|
+
|
|
130
|
+
By enabling `safeMode` and providing `allowedScopes`, the firewall acts as a literal shield.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
ik.setConfig({
|
|
134
|
+
safeMode: true,
|
|
135
|
+
allowedScopes: ['Customer_Support_FAQ', 'Order_Tracking']
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
If a user tries to prompt-inject the bot to drop database tables or write generic poetry, the Heuristic Gatekeeper will intercept it, setting `metrics.semanticInsights.requiresHumanIntervention = true` and logging an `authority_exceeded` event.
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## 📈 Benchmarks
|
|
143
|
+
|
|
144
|
+
In blind tests, comparing an unoptimized `gpt-4o` request against an IK-Optimized `gpt-4o-mini` request:
|
|
145
|
+
- **Cost Savings:** Up to 65% reduction in token costs (due to `crystallize` compression and `mini` routing).
|
|
146
|
+
- **Quality:** Evaluators ranked IK-Optimized responses functionally equivalent to or better than raw `gpt-4o` on strictly precise domains (Legal, Medical, Finance) because the `agentDirective` forces the smaller model to maintain high structural fidelity.
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## 📝 License
|
|
151
|
+
|
|
152
|
+
MIT © Stefan Vasic
|