prepia 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/LICENSE +21 -0
- package/README.md +312 -0
- package/bin/prepia.mjs +119 -0
- package/package.json +53 -0
- package/skill/SKILL.md +148 -0
- package/skill/config.json +29 -0
- package/src/analytics/dashboard.mjs +84 -0
- package/src/analytics/tracker.mjs +131 -0
- package/src/api/middleware.mjs +219 -0
- package/src/api/routes.mjs +142 -0
- package/src/api/server.mjs +150 -0
- package/src/cache/disk-store.mjs +199 -0
- package/src/cache/manager.mjs +142 -0
- package/src/cache/memory-store.mjs +205 -0
- package/src/chain/dag.mjs +209 -0
- package/src/chain/executor.mjs +103 -0
- package/src/chain/scheduler.mjs +89 -0
- package/src/client/adapters.mjs +483 -0
- package/src/client/connector.mjs +391 -0
- package/src/client/index.mjs +483 -0
- package/src/client/websocket.mjs +353 -0
- package/src/core/context-packager.mjs +169 -0
- package/src/core/engine.mjs +338 -0
- package/src/core/event-bus.mjs +84 -0
- package/src/core/prepimshot.mjs +120 -0
- package/src/core/task-decomposer.mjs +158 -0
- package/src/edge/lite.mjs +90 -0
- package/src/guard/checker.mjs +123 -0
- package/src/guard/fact-checker.mjs +105 -0
- package/src/guard/hallucination.mjs +108 -0
- package/src/index.mjs +67 -0
- package/src/models/local-model.mjs +171 -0
- package/src/models/provider.mjs +192 -0
- package/src/models/router.mjs +156 -0
- package/src/morph/optimizer.mjs +142 -0
- package/src/network/p2p.mjs +146 -0
- package/src/persona/detector.mjs +118 -0
- package/src/plugins/loader.mjs +120 -0
- package/src/plugins/registry.mjs +164 -0
- package/src/plugins/sandbox.mjs +79 -0
- package/src/rate/limiter.mjs +145 -0
- package/src/rate/shield.mjs +150 -0
- package/src/script/executor.mjs +164 -0
- package/src/script/parser.mjs +134 -0
- package/src/security/privacy.mjs +108 -0
- package/src/security/sanitizer.mjs +133 -0
- package/src/shadow/daemon.mjs +128 -0
- package/src/stream/handler.mjs +204 -0
- package/src/tools/calculator.mjs +312 -0
- package/src/tools/file-ops.mjs +138 -0
- package/src/tools/http-client.mjs +127 -0
- package/src/tools/orchestrator.mjs +205 -0
- package/src/tools/web-scraper.mjs +159 -0
- package/src/tools/web-search.mjs +129 -0
- package/src/vault/knowledge-base.mjs +207 -0
- package/src/vault/pattern-learner.mjs +192 -0
- package/workflows/analyze.json +32 -0
- package/workflows/automate.json +32 -0
- package/workflows/research.json +37 -0
- package/workflows/summarize.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Prepia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
# Prepia
|
|
2
|
+
|
|
3
|
+
**AI Middleware that reduces LLM quota usage by 80-95%**
|
|
4
|
+
|
|
5
|
+
Prepia is a smart middleware layer that sits between AI agents and LLMs. Instead of making many LLM calls for complex tasks, Prepia handles the heavy lifting (searching, scraping, processing, caching) and sends ONE optimized call to the LLM.
|
|
6
|
+
|
|
7
|
+
## Architecture
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
┌─────────────────────────────────────┐
|
|
11
|
+
│ AI Agent / Client │
|
|
12
|
+
└──────────────┬──────────────────────┘
|
|
13
|
+
│
|
|
14
|
+
┌──────────────▼──────────────────────┐
|
|
15
|
+
│ Prepia API Server │
|
|
16
|
+
│ (REST / CLI / Programmatic) │
|
|
17
|
+
└──────────────┬──────────────────────┘
|
|
18
|
+
│
|
|
19
|
+
┌──────────────▼──────────────────────┐
|
|
20
|
+
│ Core Engine │
|
|
21
|
+
│ ┌─────────┬──────────┬───────────┐ │
|
|
22
|
+
│ │ Task │ Context │ PrepiShot │ │
|
|
23
|
+
│ │Decompose│ Packager │ Optimizer │ │
|
|
24
|
+
│ └────┬────┴────┬─────┴─────┬─────┘ │
|
|
25
|
+
└───────┼─────────┼───────────┼───────┘
|
|
26
|
+
│ │ │
|
|
27
|
+
┌──────────────────┼─────────┼───────────┼──────────────────┐
|
|
28
|
+
│ │ │ │ │
|
|
29
|
+
┌────▼────┐ ┌─────▼────┐ ┌──▼───┐ ┌────▼─────┐ ┌───────▼──────┐
|
|
30
|
+
│ Tools │ │ Cache │ │Models│ │ Chain │ │ Security │
|
|
31
|
+
│Orchestr.│ │ Manager │ │Router│ │Scheduler │ │ Sanitizer │
|
|
32
|
+
└────┬────┘ └──────────┘ └──────┘ └──────────┘ └──────────────┘
|
|
33
|
+
│
|
|
34
|
+
┌────┼────┬────────┬──────────┐
|
|
35
|
+
│ │ │ │ │
|
|
36
|
+
Web Web File Calc HTTP
|
|
37
|
+
Search Scraper Ops ulator Client
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# Clone or copy the prepia directory
|
|
44
|
+
cd prepia
|
|
45
|
+
|
|
46
|
+
# No npm install needed - uses only Node.js built-in modules!
|
|
47
|
+
# Requires Node.js >= 22.0.0
|
|
48
|
+
|
|
49
|
+
# Make CLI executable
|
|
50
|
+
chmod +x bin/prepia.mjs
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Quick Start
|
|
54
|
+
|
|
55
|
+
### CLI
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# Process a simple query (flash mode - instant, no LLM)
|
|
59
|
+
node bin/prepia.mjs process "What is 15% of 200?" --mode flash
|
|
60
|
+
|
|
61
|
+
# Process with one LLM call (shot mode - default)
|
|
62
|
+
node bin/prepia.mjs process "Explain quantum computing"
|
|
63
|
+
|
|
64
|
+
# Start the API server
|
|
65
|
+
node bin/prepia.mjs serve --port 3000
|
|
66
|
+
|
|
67
|
+
# View analytics
|
|
68
|
+
node bin/prepia.mjs analytics
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Programmatic
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
import { PrepiaEngine } from './src/index.mjs';
|
|
75
|
+
|
|
76
|
+
const engine = new PrepiaEngine();
|
|
77
|
+
|
|
78
|
+
// Flash mode - handles locally, no LLM needed
|
|
79
|
+
const result = await engine.process('Calculate 15% of 200', { mode: 'flash' });
|
|
80
|
+
console.log(result.answer); // "30"
|
|
81
|
+
|
|
82
|
+
// Shot mode - one optimized LLM call
|
|
83
|
+
const result2 = await engine.process('Summarize the latest AI trends', { mode: 'shot' });
|
|
84
|
+
console.log(result2.answer);
|
|
85
|
+
|
|
86
|
+
// Get analytics
|
|
87
|
+
const analytics = engine.getAnalytics();
|
|
88
|
+
console.log(analytics.metrics);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### API
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
# Start server
|
|
95
|
+
node bin/prepia.mjs serve
|
|
96
|
+
|
|
97
|
+
# Submit a task
|
|
98
|
+
curl -X POST http://localhost:3000/task \
|
|
99
|
+
-H "Content-Type: application/json" \
|
|
100
|
+
-d '{"query": "What is machine learning?", "mode": "shot"}'
|
|
101
|
+
|
|
102
|
+
# Health check
|
|
103
|
+
curl http://localhost:3000/status
|
|
104
|
+
|
|
105
|
+
# Analytics
|
|
106
|
+
curl http://localhost:3000/analytics
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## API Reference
|
|
110
|
+
|
|
111
|
+
### POST /task
|
|
112
|
+
Submit a task for processing.
|
|
113
|
+
|
|
114
|
+
**Body:**
|
|
115
|
+
```json
|
|
116
|
+
{
|
|
117
|
+
"query": "Your question or task",
|
|
118
|
+
"mode": "flash|shot|stream",
|
|
119
|
+
"options": {}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**Response:**
|
|
124
|
+
```json
|
|
125
|
+
{
|
|
126
|
+
"answer": "The response text",
|
|
127
|
+
"mode": "shot",
|
|
128
|
+
"usedLLM": true,
|
|
129
|
+
"taskId": "task_123_abc",
|
|
130
|
+
"provider": "openai",
|
|
131
|
+
"tokens": { "prompt": 100, "completion": 50, "total": 150 },
|
|
132
|
+
"quality": { "overall": 0.85 },
|
|
133
|
+
"duration": 1234
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### GET /status
|
|
138
|
+
Health check endpoint.
|
|
139
|
+
|
|
140
|
+
### GET /analytics
|
|
141
|
+
Usage statistics and cost savings.
|
|
142
|
+
|
|
143
|
+
### POST /cache/clear
|
|
144
|
+
Clear all caches.
|
|
145
|
+
|
|
146
|
+
### GET /plugins
|
|
147
|
+
List registered plugins.
|
|
148
|
+
|
|
149
|
+
### POST /config
|
|
150
|
+
Update engine configuration.
|
|
151
|
+
|
|
152
|
+
## Processing Modes
|
|
153
|
+
|
|
154
|
+
| Mode | Description | LLM Calls | Best For |
|
|
155
|
+
|------|-------------|-----------|----------|
|
|
156
|
+
| `flash` | Instant local answers | 0 | Math, greetings, time queries |
|
|
157
|
+
| `shot` | One optimized LLM call | 1 | Most tasks (default) |
|
|
158
|
+
| `stream` | Progressive updates | 1 | Long-running tasks |
|
|
159
|
+
|
|
160
|
+
## Configuration
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
const engine = new PrepiaEngine({
|
|
164
|
+
cache: {
|
|
165
|
+
memoryMaxSize: 1000, // Max memory cache entries
|
|
166
|
+
memoryTTL: 300000, // Memory TTL (5 min)
|
|
167
|
+
diskTTL: 3600000, // Disk TTL (1 hour)
|
|
168
|
+
enableDisk: true, // Enable disk persistence
|
|
169
|
+
cacheDir: '.prepia/cache' // Cache directory
|
|
170
|
+
},
|
|
171
|
+
config: {
|
|
172
|
+
maxContextTokens: 4000, // Max context for LLM
|
|
173
|
+
defaultMode: 'shot', // Default processing mode
|
|
174
|
+
enableLocalModel: true, // Use local pattern matching
|
|
175
|
+
enableCache: true, // Enable caching
|
|
176
|
+
enableQualityCheck: true // Check output quality
|
|
177
|
+
},
|
|
178
|
+
rate: {
|
|
179
|
+
providers: {
|
|
180
|
+
openai: { requestsPerMinute: 60, tokensPerMinute: 100000 }
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Plugin Development
|
|
187
|
+
|
|
188
|
+
### Plugin Structure
|
|
189
|
+
|
|
190
|
+
```
|
|
191
|
+
my-plugin/
|
|
192
|
+
├── manifest.json # Plugin metadata
|
|
193
|
+
└── index.mjs # Plugin code
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### manifest.json
|
|
197
|
+
|
|
198
|
+
```json
|
|
199
|
+
{
|
|
200
|
+
"name": "my-plugin",
|
|
201
|
+
"version": "1.0.0",
|
|
202
|
+
"description": "My custom plugin",
|
|
203
|
+
"main": "index.mjs",
|
|
204
|
+
"dependencies": []
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Plugin Interface
|
|
209
|
+
|
|
210
|
+
```javascript
|
|
211
|
+
// index.mjs
|
|
212
|
+
export async function init(context) {
|
|
213
|
+
// Called when plugin is initialized
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export async function execute(params) {
|
|
217
|
+
// Called when plugin is executed
|
|
218
|
+
return { result: 'done' };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export async function cleanup() {
|
|
222
|
+
// Called when plugin is cleaned up
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## PrepiScript
|
|
227
|
+
|
|
228
|
+
Custom task definition language:
|
|
229
|
+
|
|
230
|
+
```
|
|
231
|
+
TASK "Research and Summarize"
|
|
232
|
+
SEARCH "quantum computing breakthroughs 2024"
|
|
233
|
+
EXTRACT key_findings
|
|
234
|
+
FILTER relevance > 0.7
|
|
235
|
+
FORMAT markdown
|
|
236
|
+
DELIVER output
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Testing
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
# Run all tests
|
|
243
|
+
node --test tests/**/*.test.mjs
|
|
244
|
+
|
|
245
|
+
# Run unit tests only
|
|
246
|
+
node --test tests/core/*.test.mjs tests/tools/*.test.mjs tests/cache/*.test.mjs
|
|
247
|
+
|
|
248
|
+
# Run integration tests
|
|
249
|
+
node --test tests/integration/*.test.mjs
|
|
250
|
+
|
|
251
|
+
# Run API tests
|
|
252
|
+
node --test tests/api/*.test.mjs
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## Modules
|
|
256
|
+
|
|
257
|
+
| Module | Description |
|
|
258
|
+
|--------|-------------|
|
|
259
|
+
| `core/engine` | Main orchestrator |
|
|
260
|
+
| `core/task-decomposer` | Breaks tasks into sub-tasks |
|
|
261
|
+
| `core/context-packager` | Compresses context for LLM |
|
|
262
|
+
| `core/prepimshot` | One-shot prompt optimization |
|
|
263
|
+
| `tools/orchestrator` | Tool routing and execution |
|
|
264
|
+
| `tools/web-search` | Web search (DuckDuckGo, Wikipedia) |
|
|
265
|
+
| `tools/web-scraper` | Content extraction |
|
|
266
|
+
| `tools/calculator` | Safe math evaluation |
|
|
267
|
+
| `cache/manager` | Cache orchestration |
|
|
268
|
+
| `cache/memory-store` | LRU memory cache |
|
|
269
|
+
| `cache/disk-store` | Persistent disk cache |
|
|
270
|
+
| `models/router` | Multi-LLM routing |
|
|
271
|
+
| `models/local-model` | Local pattern matching |
|
|
272
|
+
| `models/provider` | LLM provider abstraction |
|
|
273
|
+
| `rate/shield` | Rate limit protection |
|
|
274
|
+
| `rate/limiter` | Token bucket / sliding window |
|
|
275
|
+
| `chain/dag` | Task dependency graph |
|
|
276
|
+
| `chain/scheduler` | Parallel task scheduler |
|
|
277
|
+
| `chain/executor` | Task execution engine |
|
|
278
|
+
| `vault/knowledge-base` | Persistent knowledge store |
|
|
279
|
+
| `vault/pattern-learner` | Query pattern learning |
|
|
280
|
+
| `plugins/loader` | Plugin discovery |
|
|
281
|
+
| `plugins/registry` | Plugin lifecycle management |
|
|
282
|
+
| `plugins/sandbox` | Plugin execution sandbox |
|
|
283
|
+
| `guard/checker` | Output quality verification |
|
|
284
|
+
| `guard/fact-checker` | Cross-reference verification |
|
|
285
|
+
| `guard/hallucination` | Hallucination detection |
|
|
286
|
+
| `persona/detector` | Context-aware persona selection |
|
|
287
|
+
| `analytics/tracker` | Usage tracking |
|
|
288
|
+
| `analytics/dashboard` | Cost/efficiency reporting |
|
|
289
|
+
| `stream/handler` | Real-time progress updates |
|
|
290
|
+
| `security/sanitizer` | Input/output sanitization |
|
|
291
|
+
| `security/privacy` | PII detection and redaction |
|
|
292
|
+
| `shadow/daemon` | Background task daemon |
|
|
293
|
+
| `edge/lite` | Lightweight edge mode |
|
|
294
|
+
| `morph/optimizer` | Workflow optimization |
|
|
295
|
+
| `network/p2p` | Distributed task sharing |
|
|
296
|
+
| `script/parser` | PrepiScript parser |
|
|
297
|
+
| `script/executor` | PrepiScript executor |
|
|
298
|
+
| `api/server` | HTTP API server |
|
|
299
|
+
| `api/routes` | API route handlers |
|
|
300
|
+
| `api/middleware` | Request/response middleware |
|
|
301
|
+
|
|
302
|
+
## Contributing
|
|
303
|
+
|
|
304
|
+
1. Fork the repository
|
|
305
|
+
2. Create a feature branch
|
|
306
|
+
3. Write tests for new functionality
|
|
307
|
+
4. Ensure all tests pass: `node --test tests/**/*.test.mjs`
|
|
308
|
+
5. Submit a pull request
|
|
309
|
+
|
|
310
|
+
## License
|
|
311
|
+
|
|
312
|
+
MIT
|
package/bin/prepia.mjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Prepia CLI - command-line interface.
|
|
5
|
+
* @module bin/prepia
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { PrepiaEngine } from '../src/core/engine.mjs';
|
|
9
|
+
import { generateReport, generateSummary } from '../src/analytics/dashboard.mjs';
|
|
10
|
+
|
|
11
|
+
const HELP = `
|
|
12
|
+
Prepia - AI Middleware CLI
|
|
13
|
+
Usage: prepia [command] [options]
|
|
14
|
+
|
|
15
|
+
Commands:
|
|
16
|
+
process <query> Process a task through Prepia
|
|
17
|
+
status Show engine status
|
|
18
|
+
analytics Show usage analytics
|
|
19
|
+
cache clear Clear all caches
|
|
20
|
+
serve Start API server
|
|
21
|
+
help Show this help
|
|
22
|
+
|
|
23
|
+
Options:
|
|
24
|
+
--mode <flash|shot|stream> Processing mode (default: shot)
|
|
25
|
+
--port <number> API server port (default: 3000)
|
|
26
|
+
--json Output as JSON
|
|
27
|
+
|
|
28
|
+
Examples:
|
|
29
|
+
prepia process "What is the capital of France?"
|
|
30
|
+
prepia process "Calculate 15% of 200" --mode flash
|
|
31
|
+
prepia serve --port 8080
|
|
32
|
+
prepia analytics --json
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
async function main() {
|
|
36
|
+
const args = process.argv.slice(2);
|
|
37
|
+
const command = args[0];
|
|
38
|
+
|
|
39
|
+
if (!command || command === 'help' || command === '--help') {
|
|
40
|
+
console.log(HELP);
|
|
41
|
+
process.exit(0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const engine = new PrepiaEngine({ cache: { enableDisk: false } });
|
|
45
|
+
|
|
46
|
+
switch (command) {
|
|
47
|
+
case 'process': {
|
|
48
|
+
const query = args.slice(1).find(a => !a.startsWith('--'));
|
|
49
|
+
if (!query) {
|
|
50
|
+
console.error('Error: Missing query. Usage: prepia process "<query>"');
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
const modeIdx = args.indexOf('--mode');
|
|
54
|
+
const mode = modeIdx >= 0 ? args[modeIdx + 1] : 'shot';
|
|
55
|
+
const json = args.includes('--json');
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const result = await engine.process(query, { mode });
|
|
59
|
+
if (json) {
|
|
60
|
+
console.log(JSON.stringify(result, null, 2));
|
|
61
|
+
} else {
|
|
62
|
+
console.log('\n' + result.answer);
|
|
63
|
+
console.log(`\n[${result.mode}] ${result.usedLLM ? 'LLM used' : 'Local'} | ${result.duration}ms`);
|
|
64
|
+
}
|
|
65
|
+
} catch (err) {
|
|
66
|
+
console.error(`Error: ${err.message}`);
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
case 'status': {
|
|
73
|
+
const metrics = engine.getAnalytics().metrics;
|
|
74
|
+
console.log(`Prepia v1.0.0`);
|
|
75
|
+
console.log(`Tasks processed: ${metrics.tasksProcessed}`);
|
|
76
|
+
console.log(`LLM efficiency: ${(metrics.efficiency * 100).toFixed(0)}%`);
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
case 'analytics': {
|
|
81
|
+
const data = engine.getAnalytics();
|
|
82
|
+
if (args.includes('--json')) {
|
|
83
|
+
console.log(JSON.stringify(data, null, 2));
|
|
84
|
+
} else {
|
|
85
|
+
console.log(generateReport(data.metrics, data.savings));
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
case 'cache': {
|
|
91
|
+
if (args[1] === 'clear') {
|
|
92
|
+
await engine.clearCache();
|
|
93
|
+
console.log('Cache cleared.');
|
|
94
|
+
} else {
|
|
95
|
+
console.log('Usage: prepia cache clear');
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
case 'serve': {
|
|
101
|
+
const portIdx = args.indexOf('--port');
|
|
102
|
+
const port = portIdx >= 0 ? parseInt(args[portIdx + 1]) : 3000;
|
|
103
|
+
const { createServer } = await import('../src/api/server.mjs');
|
|
104
|
+
await createServer(engine, { port });
|
|
105
|
+
console.log(`Prepia API server running on http://localhost:${port}`);
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
default:
|
|
110
|
+
console.error(`Unknown command: ${command}`);
|
|
111
|
+
console.log(HELP);
|
|
112
|
+
process.exit(1);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
main().catch(err => {
|
|
117
|
+
console.error(`Fatal: ${err.message}`);
|
|
118
|
+
process.exit(1);
|
|
119
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "prepia",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "AI middleware that reduces LLM quota usage by 80-95% through smart caching, task decomposition, and context optimization",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.mjs",
|
|
7
|
+
"bin": {
|
|
8
|
+
"prepia": "bin/prepia.mjs"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "node src/api/server.mjs",
|
|
12
|
+
"test": "node --test tests/**/*.test.mjs",
|
|
13
|
+
"test:unit": "node --test tests/core/*.test.mjs tests/tools/*.test.mjs tests/cache/*.test.mjs tests/models/*.test.mjs tests/rate/*.test.mjs tests/chain/*.test.mjs tests/vault/*.test.mjs tests/plugins/*.test.mjs tests/guard/*.test.mjs tests/analytics/*.test.mjs tests/security/*.test.mjs tests/script/*.test.mjs",
|
|
14
|
+
"test:integration": "node --test tests/integration/*.test.mjs",
|
|
15
|
+
"test:api": "node --test tests/api/*.test.mjs",
|
|
16
|
+
"cli": "node bin/prepia.mjs"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"ai",
|
|
20
|
+
"middleware",
|
|
21
|
+
"llm",
|
|
22
|
+
"cache",
|
|
23
|
+
"optimization",
|
|
24
|
+
"agent",
|
|
25
|
+
"openai",
|
|
26
|
+
"anthropic",
|
|
27
|
+
"token-saving",
|
|
28
|
+
"quota",
|
|
29
|
+
"ai-agent",
|
|
30
|
+
"prepia"
|
|
31
|
+
],
|
|
32
|
+
"author": "Atum246",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=22.0.0"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/Atum246/prepia.git"
|
|
40
|
+
},
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/Atum246/prepia/issues"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/Atum246/prepia#readme",
|
|
45
|
+
"files": [
|
|
46
|
+
"src/",
|
|
47
|
+
"bin/",
|
|
48
|
+
"skill/",
|
|
49
|
+
"workflows/",
|
|
50
|
+
"README.md",
|
|
51
|
+
"LICENSE"
|
|
52
|
+
]
|
|
53
|
+
}
|
package/skill/SKILL.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# Prepia - AI Middleware Skill
|
|
2
|
+
|
|
3
|
+
## What is Prepia?
|
|
4
|
+
|
|
5
|
+
Prepia is a smart middleware layer that sits between AI agents and LLMs. It reduces LLM quota usage by 80-95% by:
|
|
6
|
+
|
|
7
|
+
- **Caching** results to avoid redundant LLM calls
|
|
8
|
+
- **Decomposing** complex tasks into sub-tasks
|
|
9
|
+
- **Handling locally** what doesn't need an LLM (calculations, searches, file ops)
|
|
10
|
+
- **Optimizing context** to minimize token usage
|
|
11
|
+
- **Quality checking** outputs for reliability
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cd prepia && npm install # No external deps - just for package.json scripts
|
|
17
|
+
# Or use directly:
|
|
18
|
+
node bin/prepia.mjs --help
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## How to Submit Tasks
|
|
22
|
+
|
|
23
|
+
### Via CLI
|
|
24
|
+
```bash
|
|
25
|
+
node bin/prepia.mjs process "What is the capital of France?"
|
|
26
|
+
node bin/prepia.mjs process "Calculate 15% of 200" --mode flash
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Via API
|
|
30
|
+
```bash
|
|
31
|
+
curl -X POST http://localhost:3000/task \
|
|
32
|
+
-H "Content-Type: application/json" \
|
|
33
|
+
-d '{"query": "What is machine learning?", "mode": "shot"}'
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Via Code
|
|
37
|
+
```javascript
|
|
38
|
+
import { PrepiaEngine } from 'prepia';
|
|
39
|
+
|
|
40
|
+
const engine = new PrepiaEngine();
|
|
41
|
+
const result = await engine.process("Summarize quantum computing");
|
|
42
|
+
console.log(result.answer);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Processing Modes
|
|
46
|
+
|
|
47
|
+
| Mode | Description | LLM Calls | Best For |
|
|
48
|
+
|------|-------------|-----------|----------|
|
|
49
|
+
| `flash` | Instant local answers | 0 | Simple questions, math, greetings |
|
|
50
|
+
| `shot` | One optimized LLM call | 1 | Most tasks (default) |
|
|
51
|
+
| `stream` | Progressive with updates | 1 | Long-running tasks |
|
|
52
|
+
|
|
53
|
+
## How to Read Responses
|
|
54
|
+
|
|
55
|
+
Response object structure:
|
|
56
|
+
```javascript
|
|
57
|
+
{
|
|
58
|
+
answer: "The answer text", // Main response
|
|
59
|
+
mode: "shot", // Processing mode used
|
|
60
|
+
usedLLM: true, // Whether LLM was called
|
|
61
|
+
taskId: "task_123_abc", // Unique task ID
|
|
62
|
+
provider: "openai", // LLM provider used (if any)
|
|
63
|
+
tokens: { prompt: 100, completion: 50, total: 150 },
|
|
64
|
+
quality: { overall: 0.85, completeness: 0.9, coherence: 0.8 },
|
|
65
|
+
duration: 1234 // Processing time in ms
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Configuration
|
|
70
|
+
|
|
71
|
+
### Engine Options
|
|
72
|
+
```javascript
|
|
73
|
+
const engine = new PrepiaEngine({
|
|
74
|
+
cache: { memoryMaxSize: 1000, enableDisk: true },
|
|
75
|
+
config: { maxContextTokens: 4000, enableLocalModel: true },
|
|
76
|
+
model: { providers: [{ name: 'openai', type: 'openai', apiKey: '...' }] }
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Model Providers
|
|
81
|
+
```javascript
|
|
82
|
+
import { ModelRouter } from 'prepia';
|
|
83
|
+
|
|
84
|
+
const router = new ModelRouter({
|
|
85
|
+
providers: [
|
|
86
|
+
{ name: 'openai', type: 'openai', apiKey: '...', priority: 10 },
|
|
87
|
+
{ name: 'anthropic', type: 'anthropic', apiKey: '...', priority: 5 },
|
|
88
|
+
]
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Workflow Examples
|
|
93
|
+
|
|
94
|
+
### Web Research
|
|
95
|
+
```javascript
|
|
96
|
+
const result = await engine.process(
|
|
97
|
+
"Research the latest developments in quantum computing 2024",
|
|
98
|
+
{ mode: 'shot' }
|
|
99
|
+
);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Data Analysis
|
|
103
|
+
```javascript
|
|
104
|
+
const result = await engine.process(
|
|
105
|
+
"Calculate the compound interest on $10000 at 5% for 10 years",
|
|
106
|
+
{ mode: 'flash' }
|
|
107
|
+
);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Content Summarization
|
|
111
|
+
```javascript
|
|
112
|
+
const result = await engine.process(
|
|
113
|
+
"Summarize the key points from this article: [paste article text]",
|
|
114
|
+
{ mode: 'shot' }
|
|
115
|
+
);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## PrepiScript
|
|
119
|
+
|
|
120
|
+
Custom task definition language:
|
|
121
|
+
```
|
|
122
|
+
TASK "Research and Summarize"
|
|
123
|
+
SEARCH "quantum computing breakthroughs 2024"
|
|
124
|
+
EXTRACT key_findings
|
|
125
|
+
FORMAT markdown
|
|
126
|
+
DELIVER output
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Troubleshooting
|
|
130
|
+
|
|
131
|
+
| Issue | Solution |
|
|
132
|
+
|-------|----------|
|
|
133
|
+
| "No providers configured" | Set up a model router with API keys |
|
|
134
|
+
| Cache not working | Check disk permissions for `.prepia/cache/` |
|
|
135
|
+
| Slow responses | Use `flash` mode for simple queries |
|
|
136
|
+
| Rate limited | Prepia auto-throttles; wait or add more providers |
|
|
137
|
+
| Low quality output | Check quality score; may need better context |
|
|
138
|
+
|
|
139
|
+
## API Endpoints
|
|
140
|
+
|
|
141
|
+
| Method | Path | Description |
|
|
142
|
+
|--------|------|-------------|
|
|
143
|
+
| POST | /task | Submit a task |
|
|
144
|
+
| GET | /status | Health check |
|
|
145
|
+
| GET | /analytics | Usage stats |
|
|
146
|
+
| POST | /cache/clear | Clear cache |
|
|
147
|
+
| GET | /plugins | List plugins |
|
|
148
|
+
| POST | /config | Update config |
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "prepia",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "AI middleware that reduces LLM quota usage by 80-95%",
|
|
5
|
+
"triggers": [
|
|
6
|
+
"research",
|
|
7
|
+
"search",
|
|
8
|
+
"find",
|
|
9
|
+
"look up",
|
|
10
|
+
"calculate",
|
|
11
|
+
"compute",
|
|
12
|
+
"summarize",
|
|
13
|
+
"analyze",
|
|
14
|
+
"automate",
|
|
15
|
+
"prepia"
|
|
16
|
+
],
|
|
17
|
+
"defaultMode": "shot",
|
|
18
|
+
"enableCache": true,
|
|
19
|
+
"enableLocalModel": true,
|
|
20
|
+
"enableQualityCheck": true,
|
|
21
|
+
"maxContextTokens": 4000,
|
|
22
|
+
"supportedModes": ["flash", "shot", "stream"],
|
|
23
|
+
"workflows": {
|
|
24
|
+
"research": "workflows/research.json",
|
|
25
|
+
"summarize": "workflows/summarize.json",
|
|
26
|
+
"analyze": "workflows/analyze.json",
|
|
27
|
+
"automate": "workflows/automate.json"
|
|
28
|
+
}
|
|
29
|
+
}
|