@relayplane/proxy 0.1.10 → 0.2.1

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/dist/cli.mjs DELETED
@@ -1,3134 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- // src/proxy.ts
4
- import * as http from "http";
5
- import * as url from "url";
6
-
7
- // src/storage/store.ts
8
- import Database from "better-sqlite3";
9
-
10
- // node_modules/nanoid/index.js
11
- import crypto from "crypto";
12
-
13
- // node_modules/nanoid/url-alphabet/index.js
14
- var urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
15
-
16
- // node_modules/nanoid/index.js
17
- var POOL_SIZE_MULTIPLIER = 128;
18
- var pool;
19
- var poolOffset;
20
- var fillPool = (bytes) => {
21
- if (!pool || pool.length < bytes) {
22
- pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER);
23
- crypto.randomFillSync(pool);
24
- poolOffset = 0;
25
- } else if (poolOffset + bytes > pool.length) {
26
- crypto.randomFillSync(pool);
27
- poolOffset = 0;
28
- }
29
- poolOffset += bytes;
30
- };
31
- var nanoid = (size = 21) => {
32
- fillPool(size |= 0);
33
- let id = "";
34
- for (let i = poolOffset - size; i < poolOffset; i++) {
35
- id += urlAlphabet[pool[i] & 63];
36
- }
37
- return id;
38
- };
39
-
40
- // src/storage/store.ts
41
- import * as fs from "fs";
42
- import * as path from "path";
43
- import * as os from "os";
44
-
45
- // src/storage/schema.ts
46
- var SCHEMA_SQL = `
47
- -- Runs table: stores all LLM invocations
48
- CREATE TABLE IF NOT EXISTS runs (
49
- id TEXT PRIMARY KEY,
50
- prompt TEXT NOT NULL,
51
- system_prompt TEXT,
52
- task_type TEXT NOT NULL,
53
- model TEXT NOT NULL,
54
- success INTEGER NOT NULL,
55
- output TEXT,
56
- error TEXT,
57
- duration_ms INTEGER NOT NULL,
58
- tokens_in INTEGER,
59
- tokens_out INTEGER,
60
- cost_usd REAL,
61
- metadata TEXT,
62
- created_at TEXT NOT NULL DEFAULT (datetime('now'))
63
- );
64
-
65
- -- Index for task type queries
66
- CREATE INDEX IF NOT EXISTS idx_runs_task_type ON runs(task_type);
67
-
68
- -- Index for model queries
69
- CREATE INDEX IF NOT EXISTS idx_runs_model ON runs(model);
70
-
71
- -- Index for time-based queries
72
- CREATE INDEX IF NOT EXISTS idx_runs_created_at ON runs(created_at);
73
-
74
- -- Outcomes table: stores user feedback on runs
75
- CREATE TABLE IF NOT EXISTS outcomes (
76
- id TEXT PRIMARY KEY,
77
- run_id TEXT NOT NULL REFERENCES runs(id) ON DELETE CASCADE,
78
- success INTEGER NOT NULL,
79
- quality TEXT,
80
- latency_satisfactory INTEGER,
81
- cost_satisfactory INTEGER,
82
- feedback TEXT,
83
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
84
- UNIQUE(run_id)
85
- );
86
-
87
- -- Index for run lookups
88
- CREATE INDEX IF NOT EXISTS idx_outcomes_run_id ON outcomes(run_id);
89
-
90
- -- Routing rules table: stores routing preferences
91
- CREATE TABLE IF NOT EXISTS routing_rules (
92
- id TEXT PRIMARY KEY,
93
- task_type TEXT NOT NULL UNIQUE,
94
- preferred_model TEXT NOT NULL,
95
- source TEXT NOT NULL DEFAULT 'default',
96
- confidence REAL,
97
- sample_count INTEGER,
98
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
99
- updated_at TEXT NOT NULL DEFAULT (datetime('now'))
100
- );
101
-
102
- -- Index for task type lookups
103
- CREATE INDEX IF NOT EXISTS idx_routing_rules_task_type ON routing_rules(task_type);
104
-
105
- -- Suggestions table: stores routing improvement suggestions
106
- CREATE TABLE IF NOT EXISTS suggestions (
107
- id TEXT PRIMARY KEY,
108
- task_type TEXT NOT NULL,
109
- current_model TEXT NOT NULL,
110
- suggested_model TEXT NOT NULL,
111
- reason TEXT NOT NULL,
112
- confidence REAL NOT NULL,
113
- expected_improvement TEXT NOT NULL,
114
- sample_count INTEGER NOT NULL,
115
- accepted INTEGER,
116
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
117
- accepted_at TEXT
118
- );
119
-
120
- -- Index for task type lookups
121
- CREATE INDEX IF NOT EXISTS idx_suggestions_task_type ON suggestions(task_type);
122
-
123
- -- Index for pending suggestions
124
- CREATE INDEX IF NOT EXISTS idx_suggestions_accepted ON suggestions(accepted);
125
-
126
- -- Schema version table for migrations
127
- CREATE TABLE IF NOT EXISTS schema_version (
128
- version INTEGER PRIMARY KEY,
129
- applied_at TEXT NOT NULL DEFAULT (datetime('now'))
130
- );
131
-
132
- -- Insert initial schema version
133
- INSERT OR IGNORE INTO schema_version (version) VALUES (1);
134
- `;
135
- var DEFAULT_ROUTING_RULES = [
136
- // Complex tasks → Sonnet (need reasoning & quality)
137
- { taskType: "code_generation", preferredModel: "anthropic:claude-sonnet-4-20250514" },
138
- { taskType: "code_review", preferredModel: "anthropic:claude-sonnet-4-20250514" },
139
- { taskType: "analysis", preferredModel: "anthropic:claude-sonnet-4-20250514" },
140
- { taskType: "creative_writing", preferredModel: "anthropic:claude-sonnet-4-20250514" },
141
- // Simple tasks → Haiku (cost efficient)
142
- { taskType: "summarization", preferredModel: "anthropic:claude-3-5-haiku-latest" },
143
- { taskType: "data_extraction", preferredModel: "anthropic:claude-3-5-haiku-latest" },
144
- { taskType: "translation", preferredModel: "anthropic:claude-3-5-haiku-latest" },
145
- { taskType: "question_answering", preferredModel: "anthropic:claude-3-5-haiku-latest" },
146
- { taskType: "general", preferredModel: "anthropic:claude-3-5-haiku-latest" }
147
- ];
148
- function generateSeedSQL() {
149
- const values = DEFAULT_ROUTING_RULES.map((rule, index) => {
150
- const id = `default-${rule.taskType}`;
151
- return `('${id}', '${rule.taskType}', '${rule.preferredModel}', 'default', NULL, NULL, datetime('now'), datetime('now'))`;
152
- }).join(",\n ");
153
- return `
154
- INSERT OR IGNORE INTO routing_rules (id, task_type, preferred_model, source, confidence, sample_count, created_at, updated_at)
155
- VALUES
156
- ${values};
157
- `;
158
- }
159
-
160
- // src/storage/store.ts
161
- function getDefaultDbPath() {
162
- return path.join(os.homedir(), ".relayplane", "data.db");
163
- }
164
- var Store = class {
165
- db;
166
- dbPath;
167
- /**
168
- * Creates a new Store instance.
169
- *
170
- * @param dbPath - Path to the SQLite database file. Defaults to ~/.relayplane/data.db
171
- */
172
- constructor(dbPath) {
173
- this.dbPath = dbPath ?? getDefaultDbPath();
174
- const dir = path.dirname(this.dbPath);
175
- if (!fs.existsSync(dir)) {
176
- fs.mkdirSync(dir, { recursive: true });
177
- }
178
- this.db = new Database(this.dbPath);
179
- this.db.pragma("journal_mode = WAL");
180
- this.db.pragma("foreign_keys = ON");
181
- this.initializeSchema();
182
- }
183
- /**
184
- * Initializes the database schema.
185
- */
186
- initializeSchema() {
187
- this.db.exec(SCHEMA_SQL);
188
- this.db.exec(generateSeedSQL());
189
- }
190
- /**
191
- * Closes the database connection.
192
- */
193
- close() {
194
- this.db.close();
195
- }
196
- /**
197
- * Gets the database path.
198
- */
199
- getDbPath() {
200
- return this.dbPath;
201
- }
202
- // ============================================================================
203
- // Runs
204
- // ============================================================================
205
- /**
206
- * Records a new run.
207
- */
208
- recordRun(run) {
209
- const id = nanoid();
210
- const stmt = this.db.prepare(`
211
- INSERT INTO runs (id, prompt, system_prompt, task_type, model, success, output, error, duration_ms, tokens_in, tokens_out, cost_usd, metadata, created_at)
212
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))
213
- `);
214
- stmt.run(
215
- id,
216
- run.prompt,
217
- run.systemPrompt,
218
- run.taskType,
219
- run.model,
220
- run.success ? 1 : 0,
221
- run.output,
222
- run.error,
223
- run.durationMs,
224
- run.tokensIn,
225
- run.tokensOut,
226
- run.costUsd,
227
- run.metadata
228
- );
229
- return id;
230
- }
231
- /**
232
- * Gets a run by ID.
233
- */
234
- getRun(id) {
235
- const stmt = this.db.prepare(`
236
- SELECT id, prompt, system_prompt as systemPrompt, task_type as taskType, model, success, output, error, duration_ms as durationMs, tokens_in as tokensIn, tokens_out as tokensOut, cost_usd as costUsd, metadata, created_at as createdAt
237
- FROM runs
238
- WHERE id = ?
239
- `);
240
- const row = stmt.get(id);
241
- if (!row) return null;
242
- return {
243
- ...row,
244
- success: Boolean(row.success)
245
- };
246
- }
247
- /**
248
- * Gets runs with optional filters.
249
- */
250
- getRuns(options) {
251
- const conditions = [];
252
- const params = [];
253
- if (options?.taskType) {
254
- conditions.push("task_type = ?");
255
- params.push(options.taskType);
256
- }
257
- if (options?.model) {
258
- conditions.push("model = ?");
259
- params.push(options.model);
260
- }
261
- if (options?.from) {
262
- conditions.push("created_at >= ?");
263
- params.push(options.from);
264
- }
265
- if (options?.to) {
266
- conditions.push("created_at <= ?");
267
- params.push(options.to);
268
- }
269
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
270
- const limit = options?.limit ?? 100;
271
- const offset = options?.offset ?? 0;
272
- const stmt = this.db.prepare(`
273
- SELECT id, prompt, system_prompt as systemPrompt, task_type as taskType, model, success, output, error, duration_ms as durationMs, tokens_in as tokensIn, tokens_out as tokensOut, cost_usd as costUsd, metadata, created_at as createdAt
274
- FROM runs
275
- ${whereClause}
276
- ORDER BY created_at DESC
277
- LIMIT ? OFFSET ?
278
- `);
279
- params.push(limit, offset);
280
- const rows = stmt.all(...params);
281
- return rows.map((row) => ({
282
- ...row,
283
- success: Boolean(row.success)
284
- }));
285
- }
286
- /**
287
- * Counts runs with optional filters.
288
- */
289
- countRuns(options) {
290
- const conditions = [];
291
- const params = [];
292
- if (options?.taskType) {
293
- conditions.push("task_type = ?");
294
- params.push(options.taskType);
295
- }
296
- if (options?.model) {
297
- conditions.push("model = ?");
298
- params.push(options.model);
299
- }
300
- if (options?.from) {
301
- conditions.push("created_at >= ?");
302
- params.push(options.from);
303
- }
304
- if (options?.to) {
305
- conditions.push("created_at <= ?");
306
- params.push(options.to);
307
- }
308
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
309
- const stmt = this.db.prepare(`
310
- SELECT COUNT(*) as count
311
- FROM runs
312
- ${whereClause}
313
- `);
314
- const row = stmt.get(...params);
315
- return row.count;
316
- }
317
- // ============================================================================
318
- // Outcomes
319
- // ============================================================================
320
- /**
321
- * Records an outcome for a run.
322
- */
323
- recordOutcome(outcome) {
324
- const id = nanoid();
325
- const stmt = this.db.prepare(`
326
- INSERT INTO outcomes (id, run_id, success, quality, latency_satisfactory, cost_satisfactory, feedback, created_at)
327
- VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'))
328
- ON CONFLICT(run_id) DO UPDATE SET
329
- success = excluded.success,
330
- quality = excluded.quality,
331
- latency_satisfactory = excluded.latency_satisfactory,
332
- cost_satisfactory = excluded.cost_satisfactory,
333
- feedback = excluded.feedback,
334
- created_at = datetime('now')
335
- `);
336
- stmt.run(
337
- id,
338
- outcome.runId,
339
- outcome.success ? 1 : 0,
340
- outcome.quality,
341
- outcome.latencySatisfactory != null ? outcome.latencySatisfactory ? 1 : 0 : null,
342
- outcome.costSatisfactory != null ? outcome.costSatisfactory ? 1 : 0 : null,
343
- outcome.feedback
344
- );
345
- return id;
346
- }
347
- /**
348
- * Gets an outcome for a run.
349
- */
350
- getOutcome(runId) {
351
- const stmt = this.db.prepare(`
352
- SELECT id, run_id as runId, success, quality, latency_satisfactory as latencySatisfactory, cost_satisfactory as costSatisfactory, feedback, created_at as createdAt
353
- FROM outcomes
354
- WHERE run_id = ?
355
- `);
356
- const row = stmt.get(runId);
357
- if (!row) return null;
358
- return {
359
- ...row,
360
- success: Boolean(row.success),
361
- latencySatisfactory: row.latencySatisfactory != null ? Boolean(row.latencySatisfactory) : null,
362
- costSatisfactory: row.costSatisfactory != null ? Boolean(row.costSatisfactory) : null
363
- };
364
- }
365
- /**
366
- * Gets outcomes with optional filters.
367
- */
368
- getOutcomes(options) {
369
- const conditions = [];
370
- const params = [];
371
- if (options?.taskType) {
372
- conditions.push("r.task_type = ?");
373
- params.push(options.taskType);
374
- }
375
- if (options?.model) {
376
- conditions.push("r.model = ?");
377
- params.push(options.model);
378
- }
379
- if (options?.from) {
380
- conditions.push("o.created_at >= ?");
381
- params.push(options.from);
382
- }
383
- if (options?.to) {
384
- conditions.push("o.created_at <= ?");
385
- params.push(options.to);
386
- }
387
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
388
- const limit = options?.limit ?? 100;
389
- const stmt = this.db.prepare(`
390
- SELECT o.id, o.run_id as runId, o.success, o.quality, o.latency_satisfactory as latencySatisfactory, o.cost_satisfactory as costSatisfactory, o.feedback, o.created_at as createdAt, r.task_type as taskType, r.model
391
- FROM outcomes o
392
- JOIN runs r ON o.run_id = r.id
393
- ${whereClause}
394
- ORDER BY o.created_at DESC
395
- LIMIT ?
396
- `);
397
- params.push(limit);
398
- const rows = stmt.all(...params);
399
- return rows.map((row) => ({
400
- ...row,
401
- success: Boolean(row.success),
402
- latencySatisfactory: row.latencySatisfactory != null ? Boolean(row.latencySatisfactory) : null,
403
- costSatisfactory: row.costSatisfactory != null ? Boolean(row.costSatisfactory) : null
404
- }));
405
- }
406
- // ============================================================================
407
- // Routing Rules
408
- // ============================================================================
409
- /**
410
- * Gets a routing rule for a task type.
411
- */
412
- getRule(taskType) {
413
- const stmt = this.db.prepare(`
414
- SELECT id, task_type as taskType, preferred_model as preferredModel, source, confidence, sample_count as sampleCount, created_at as createdAt, updated_at as updatedAt
415
- FROM routing_rules
416
- WHERE task_type = ?
417
- `);
418
- return stmt.get(taskType) ?? null;
419
- }
420
- /**
421
- * Sets a routing rule for a task type.
422
- */
423
- setRule(taskType, preferredModel, source, confidence, sampleCount) {
424
- const existingRule = this.getRule(taskType);
425
- const id = existingRule?.id ?? nanoid();
426
- const stmt = this.db.prepare(`
427
- INSERT INTO routing_rules (id, task_type, preferred_model, source, confidence, sample_count, created_at, updated_at)
428
- VALUES (?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))
429
- ON CONFLICT(task_type) DO UPDATE SET
430
- preferred_model = excluded.preferred_model,
431
- source = excluded.source,
432
- confidence = excluded.confidence,
433
- sample_count = excluded.sample_count,
434
- updated_at = datetime('now')
435
- `);
436
- stmt.run(id, taskType, preferredModel, source, confidence ?? null, sampleCount ?? null);
437
- return id;
438
- }
439
- /**
440
- * Lists all routing rules.
441
- */
442
- listRules() {
443
- const stmt = this.db.prepare(`
444
- SELECT id, task_type as taskType, preferred_model as preferredModel, source, confidence, sample_count as sampleCount, created_at as createdAt, updated_at as updatedAt
445
- FROM routing_rules
446
- ORDER BY task_type
447
- `);
448
- return stmt.all();
449
- }
450
- /**
451
- * Deletes a routing rule and resets to default.
452
- */
453
- deleteRule(taskType) {
454
- const defaultRule = DEFAULT_ROUTING_RULES.find((r) => r.taskType === taskType);
455
- if (!defaultRule) return false;
456
- this.setRule(taskType, defaultRule.preferredModel, "default");
457
- return true;
458
- }
459
- // ============================================================================
460
- // Suggestions
461
- // ============================================================================
462
- /**
463
- * Records a suggestion.
464
- */
465
- recordSuggestion(suggestion) {
466
- const id = nanoid();
467
- const stmt = this.db.prepare(`
468
- INSERT INTO suggestions (id, task_type, current_model, suggested_model, reason, confidence, expected_improvement, sample_count, accepted, created_at)
469
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))
470
- `);
471
- stmt.run(
472
- id,
473
- suggestion.taskType,
474
- suggestion.currentModel,
475
- suggestion.suggestedModel,
476
- suggestion.reason,
477
- suggestion.confidence,
478
- suggestion.expectedImprovement,
479
- suggestion.sampleCount,
480
- suggestion.accepted ?? null
481
- );
482
- return id;
483
- }
484
- /**
485
- * Gets a suggestion by ID.
486
- */
487
- getSuggestion(id) {
488
- const stmt = this.db.prepare(`
489
- SELECT id, task_type as taskType, current_model as currentModel, suggested_model as suggestedModel, reason, confidence, expected_improvement as expectedImprovement, sample_count as sampleCount, accepted, created_at as createdAt, accepted_at as acceptedAt
490
- FROM suggestions
491
- WHERE id = ?
492
- `);
493
- const row = stmt.get(id);
494
- if (!row) return null;
495
- return {
496
- ...row,
497
- accepted: row.accepted != null ? Boolean(row.accepted) : null
498
- };
499
- }
500
- /**
501
- * Gets pending (unaccepted) suggestions.
502
- */
503
- getPendingSuggestions() {
504
- const stmt = this.db.prepare(`
505
- SELECT id, task_type as taskType, current_model as currentModel, suggested_model as suggestedModel, reason, confidence, expected_improvement as expectedImprovement, sample_count as sampleCount, accepted, created_at as createdAt, accepted_at as acceptedAt
506
- FROM suggestions
507
- WHERE accepted IS NULL
508
- ORDER BY confidence DESC
509
- `);
510
- const rows = stmt.all();
511
- return rows.map((row) => ({
512
- ...row,
513
- accepted: row.accepted != null ? Boolean(row.accepted) : null
514
- }));
515
- }
516
- /**
517
- * Accepts a suggestion.
518
- */
519
- acceptSuggestion(id) {
520
- const suggestion = this.getSuggestion(id);
521
- if (!suggestion) return false;
522
- const updateStmt = this.db.prepare(`
523
- UPDATE suggestions
524
- SET accepted = 1, accepted_at = datetime('now')
525
- WHERE id = ?
526
- `);
527
- updateStmt.run(id);
528
- this.setRule(
529
- suggestion.taskType,
530
- suggestion.suggestedModel,
531
- "learned",
532
- suggestion.confidence,
533
- suggestion.sampleCount
534
- );
535
- return true;
536
- }
537
- /**
538
- * Rejects a suggestion.
539
- */
540
- rejectSuggestion(id) {
541
- const stmt = this.db.prepare(`
542
- UPDATE suggestions
543
- SET accepted = 0, accepted_at = datetime('now')
544
- WHERE id = ?
545
- `);
546
- const result = stmt.run(id);
547
- return result.changes > 0;
548
- }
549
- // ============================================================================
550
- // Statistics
551
- // ============================================================================
552
- /**
553
- * Gets aggregated statistics.
554
- */
555
- getStats(options) {
556
- const conditions = [];
557
- const params = [];
558
- if (options?.from) {
559
- conditions.push("created_at >= ?");
560
- params.push(options.from);
561
- }
562
- if (options?.to) {
563
- conditions.push("created_at <= ?");
564
- params.push(options.to);
565
- }
566
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
567
- const overallStmt = this.db.prepare(`
568
- SELECT
569
- COUNT(*) as totalRuns,
570
- SUM(success) as successfulRuns,
571
- AVG(duration_ms) as avgDurationMs
572
- FROM runs
573
- ${whereClause}
574
- `);
575
- const overall = overallStmt.get(...params);
576
- const byTaskTypeStmt = this.db.prepare(`
577
- SELECT
578
- task_type as taskType,
579
- COUNT(*) as runs,
580
- AVG(success) as successRate,
581
- AVG(duration_ms) as avgDurationMs
582
- FROM runs
583
- ${whereClause}
584
- GROUP BY task_type
585
- `);
586
- const byTaskTypeRows = byTaskTypeStmt.all(...params);
587
- const byTaskType = {};
588
- for (const row of byTaskTypeRows) {
589
- byTaskType[row.taskType] = {
590
- runs: row.runs,
591
- successRate: row.successRate,
592
- avgDurationMs: row.avgDurationMs
593
- };
594
- }
595
- const byModelStmt = this.db.prepare(`
596
- SELECT
597
- model,
598
- COUNT(*) as runs,
599
- AVG(success) as successRate,
600
- AVG(duration_ms) as avgDurationMs
601
- FROM runs
602
- ${whereClause}
603
- GROUP BY model
604
- `);
605
- const byModelRows = byModelStmt.all(...params);
606
- const byModel = {};
607
- for (const row of byModelRows) {
608
- byModel[row.model] = {
609
- runs: row.runs,
610
- successRate: row.successRate,
611
- avgDurationMs: row.avgDurationMs
612
- };
613
- }
614
- return {
615
- totalRuns: overall.totalRuns,
616
- successfulRuns: overall.successfulRuns ?? 0,
617
- avgDurationMs: overall.avgDurationMs ?? 0,
618
- byTaskType,
619
- byModel
620
- };
621
- }
622
- /**
623
- * Gets statistics for learning (outcomes joined with runs).
624
- */
625
- getLearningStats(taskType) {
626
- const stmt = this.db.prepare(`
627
- SELECT
628
- r.model,
629
- COUNT(*) as runs,
630
- AVG(r.success) as successRate,
631
- AVG(r.duration_ms) as avgDurationMs,
632
- AVG(CASE WHEN o.success IS NOT NULL THEN o.success ELSE r.success END) as outcomeSuccessRate
633
- FROM runs r
634
- LEFT JOIN outcomes o ON r.id = o.run_id
635
- WHERE r.task_type = ?
636
- GROUP BY r.model
637
- HAVING runs >= 5
638
- `);
639
- return stmt.all(taskType);
640
- }
641
- };
642
-
643
- // src/routing/inference.ts
644
- var TASK_PATTERNS = {
645
- code_generation: [
646
- { pattern: /\b(write|create|generate|implement|build|code|develop|make)\b.{0,50}\b(function|class|code|script|program|method|module|api|endpoint|component)\b/i, weight: 10 },
647
- { pattern: /\b(write|create|generate)\b.{0,30}\b(python|javascript|typescript|java|go|rust|c\+\+|ruby|php|swift)\b/i, weight: 10 },
648
- { pattern: /\bcreate a.{0,30}(that|which|to)\b/i, weight: 5 },
649
- { pattern: /\bimplement\b.{0,50}\b(algorithm|logic|feature)\b/i, weight: 8 },
650
- { pattern: /\bcode\s+for\b/i, weight: 7 },
651
- { pattern: /\bwrite me\b.{0,30}\b(code|script|function)\b/i, weight: 9 },
652
- { pattern: /```[\w]*\n/i, weight: 3 }
653
- // Code blocks suggest code context
654
- ],
655
- code_review: [
656
- { pattern: /\b(review|analyze|check|audit|inspect|evaluate|assess|critique)\b.{0,30}\b(code|function|class|script|implementation|pull request|pr|diff)\b/i, weight: 10 },
657
- { pattern: /\b(what'?s? wrong|find\s+(bugs?|issues?|problems?|errors?))\b.{0,30}\b(code|function|this)\b/i, weight: 9 },
658
- { pattern: /\b(improve|optimize|refactor)\b.{0,30}\b(code|function|this)\b/i, weight: 7 },
659
- { pattern: /\blook\s+(at|over)\s+(this|my)\s+code\b/i, weight: 8 },
660
- { pattern: /\bcode\s+review\b/i, weight: 10 },
661
- { pattern: /\bcan you (check|review)\b/i, weight: 5 }
662
- ],
663
- summarization: [
664
- { pattern: /\b(summarize|summarise|summary|tldr|tl;dr|recap|condense|brief|overview)\b/i, weight: 10 },
665
- { pattern: /\b(give|provide|write)\s+(me\s+)?(a\s+)?(brief|short|quick|concise)\s+(summary|overview)\b/i, weight: 9 },
666
- { pattern: /\bshorten\s+(this|the)\b/i, weight: 6 },
667
- { pattern: /\bin\s+(brief|short|a nutshell)\b/i, weight: 7 },
668
- { pattern: /\bkey\s+(points?|takeaways?)\b/i, weight: 8 },
669
- { pattern: /\bmain\s+(ideas?|points?)\b/i, weight: 7 }
670
- ],
671
- analysis: [
672
- { pattern: /\b(analyze|analyse|analysis|examine|investigate|assess|evaluate|study)\b/i, weight: 8 },
673
- { pattern: /\b(compare|contrast|differentiate|distinguish)\b.{0,30}\b(between|and)\b/i, weight: 9 },
674
- { pattern: /\b(pros?\s+and\s+cons?|advantages?\s+and\s+disadvantages?|strengths?\s+and\s+weaknesses?)\b/i, weight: 9 },
675
- { pattern: /\b(what\s+are|explain)\s+(the\s+)?(implications?|consequences?|effects?|impacts?)\b/i, weight: 8 },
676
- { pattern: /\bbreak\s*down\b/i, weight: 6 },
677
- { pattern: /\bdeep\s*dive\b/i, weight: 7 },
678
- { pattern: /\bcritical(ly)?\s+(analysis|evaluation|assessment)\b/i, weight: 9 }
679
- ],
680
- creative_writing: [
681
- { pattern: /\b(write|create|compose|craft|author)\b.{0,30}\b(story|poem|essay|article|blog|post|narrative|fiction|novel|song|lyrics)\b/i, weight: 10 },
682
- { pattern: /\b(creative|imaginative|fictional)\s+(writing|story|piece)\b/i, weight: 10 },
683
- { pattern: /\bonce upon a time\b/i, weight: 8 },
684
- { pattern: /\b(write|tell)\s+(me\s+)?(a\s+)?(short\s+)?story\b/i, weight: 9 },
685
- { pattern: /\b(brainstorm|ideate)\b.{0,30}\b(ideas?|concepts?|themes?)\b/i, weight: 7 },
686
- { pattern: /\bwrite\s+(in|like)\s+(the\s+)?style\s+of\b/i, weight: 8 },
687
- { pattern: /\b(catchy|creative|engaging)\s+(title|headline|tagline|slogan)\b/i, weight: 7 }
688
- ],
689
- data_extraction: [
690
- { pattern: /\b(extract|parse|pull|get|retrieve|find|identify)\b.{0,30}\b(data|information|details?|values?|fields?|entities?|names?|numbers?|dates?|emails?|phones?|addresses?)\b/i, weight: 10 },
691
- { pattern: /\b(convert|transform)\b.{0,30}\b(to|into)\s+(json|csv|xml|yaml|table|structured)\b/i, weight: 9 },
692
- { pattern: /\bstructured\s+(data|output|format)\b/i, weight: 8 },
693
- { pattern: /\bnamed\s+entity\s+(recognition|extraction)\b/i, weight: 10 },
694
- { pattern: /\b(scrape|crawl)\b/i, weight: 6 },
695
- { pattern: /\bjson\s+(output|format|schema)\b/i, weight: 7 }
696
- ],
697
- translation: [
698
- { pattern: /\b(translate|translation|translator)\b/i, weight: 10 },
699
- { pattern: /\b(convert|change)\b.{0,20}\b(to|into)\s+(english|spanish|french|german|chinese|japanese|korean|portuguese|italian|russian|arabic|hindi|dutch)\b/i, weight: 9 },
700
- { pattern: /\b(in|to)\s+(english|spanish|french|german|chinese|japanese|korean|portuguese|italian|russian|arabic|hindi|dutch)\b/i, weight: 6 },
701
- { pattern: /\bfrom\s+(english|spanish|french|german|chinese|japanese|korean|portuguese|italian|russian|arabic|hindi|dutch)\s+to\b/i, weight: 10 },
702
- { pattern: /\blocalize|localization\b/i, weight: 7 }
703
- ],
704
- question_answering: [
705
- { pattern: /^(what|who|where|when|why|how|which|is|are|does|do|can|could|would|should|will|did)\s/i, weight: 7 },
706
- { pattern: /\?$/i, weight: 5 },
707
- { pattern: /\b(explain|describe|define|what\s+is|what\s+are|tell\s+me\s+about)\b/i, weight: 8 },
708
- { pattern: /\b(answer|respond|reply)\b.{0,20}\b(question|query)\b/i, weight: 9 },
709
- { pattern: /\bfaq\b/i, weight: 8 },
710
- { pattern: /\bi\s+(want|need)\s+to\s+know\b/i, weight: 6 },
711
- { pattern: /\bcan\s+you\s+(tell|explain|help)\b/i, weight: 5 }
712
- ],
713
- general: [
714
- // Catch-all patterns with low weights
715
- { pattern: /./i, weight: 1 }
716
- ]
717
- };
718
- function inferTaskType(prompt) {
719
- const normalizedPrompt = prompt.trim().toLowerCase();
720
- const scores = {
721
- code_generation: 0,
722
- code_review: 0,
723
- summarization: 0,
724
- analysis: 0,
725
- creative_writing: 0,
726
- data_extraction: 0,
727
- translation: 0,
728
- question_answering: 0,
729
- general: 0
730
- };
731
- for (const [taskType, patterns] of Object.entries(TASK_PATTERNS)) {
732
- for (const { pattern, weight } of patterns) {
733
- if (pattern.test(prompt)) {
734
- scores[taskType] += weight;
735
- }
736
- }
737
- }
738
- let maxScore = 0;
739
- let inferredType = "general";
740
- for (const [taskType, score] of Object.entries(scores)) {
741
- if (score > maxScore) {
742
- maxScore = score;
743
- inferredType = taskType;
744
- }
745
- }
746
- if (maxScore <= 1) {
747
- return "general";
748
- }
749
- return inferredType;
750
- }
751
- function getInferenceConfidence(prompt, taskType) {
752
- const patterns = TASK_PATTERNS[taskType];
753
- if (!patterns) return 0;
754
- let totalWeight = 0;
755
- let maxPossibleWeight = 0;
756
- for (const { pattern, weight } of patterns) {
757
- maxPossibleWeight += weight;
758
- if (pattern.test(prompt)) {
759
- totalWeight += weight;
760
- }
761
- }
762
- if (maxPossibleWeight === 0) return 0;
763
- return Math.min(totalWeight / maxPossibleWeight, 0.95);
764
- }
765
-
766
- // src/routing/engine.ts
767
- var RoutingEngine = class {
768
- store;
769
- /**
770
- * Creates a new RoutingEngine.
771
- *
772
- * @param store - The storage instance to use
773
- */
774
- constructor(store) {
775
- this.store = store;
776
- }
777
- /**
778
- * Infers the task type from a prompt.
779
- *
780
- * @param prompt - The prompt to analyze
781
- * @returns The inferred task type
782
- */
783
- inferTaskType(prompt) {
784
- return inferTaskType(prompt);
785
- }
786
- /**
787
- * Gets the inference confidence for a task type.
788
- *
789
- * @param prompt - The prompt to analyze
790
- * @param taskType - The task type to check
791
- * @returns Confidence score (0-1)
792
- */
793
- getInferenceConfidence(prompt, taskType) {
794
- return getInferenceConfidence(prompt, taskType);
795
- }
796
- /**
797
- * Gets the routing rule for a task type.
798
- *
799
- * @param taskType - The task type to get the rule for
800
- * @returns The routing rule, or null if not found
801
- */
802
- get(taskType) {
803
- const record = this.store.getRule(taskType);
804
- if (!record) return null;
805
- return {
806
- id: record.id,
807
- taskType: record.taskType,
808
- preferredModel: record.preferredModel,
809
- source: record.source,
810
- confidence: record.confidence ?? void 0,
811
- sampleCount: record.sampleCount ?? void 0,
812
- createdAt: record.createdAt,
813
- updatedAt: record.updatedAt
814
- };
815
- }
816
- /**
817
- * Sets a routing rule for a task type.
818
- *
819
- * @param taskType - The task type to set the rule for
820
- * @param preferredModel - The preferred model (format: "provider:model")
821
- * @param source - How the rule was created
822
- * @param options - Optional confidence and sample count
823
- * @returns The rule ID
824
- */
825
- set(taskType, preferredModel, source = "user", options) {
826
- return this.store.setRule(
827
- taskType,
828
- preferredModel,
829
- source,
830
- options?.confidence,
831
- options?.sampleCount
832
- );
833
- }
834
- /**
835
- * Lists all routing rules.
836
- *
837
- * @returns Array of all routing rules
838
- */
839
- list() {
840
- const records = this.store.listRules();
841
- return records.map((record) => ({
842
- id: record.id,
843
- taskType: record.taskType,
844
- preferredModel: record.preferredModel,
845
- source: record.source,
846
- confidence: record.confidence ?? void 0,
847
- sampleCount: record.sampleCount ?? void 0,
848
- createdAt: record.createdAt,
849
- updatedAt: record.updatedAt
850
- }));
851
- }
852
- /**
853
- * Deletes a routing rule and resets to default.
854
- *
855
- * @param taskType - The task type to reset
856
- * @returns True if the rule was reset
857
- */
858
- delete(taskType) {
859
- return this.store.deleteRule(taskType);
860
- }
861
- /**
862
- * Gets the preferred model for a task type.
863
- *
864
- * @param taskType - The task type
865
- * @returns The preferred model string, or a default
866
- */
867
- getPreferredModel(taskType) {
868
- const rule = this.get(taskType);
869
- return rule?.preferredModel ?? "local:llama3.2";
870
- }
871
- /**
872
- * Parses a model string into provider and model name.
873
- *
874
- * @param modelString - The model string (format: "provider:model")
875
- * @returns Object with provider and model
876
- */
877
- parseModel(modelString) {
878
- const parts = modelString.split(":");
879
- if (parts.length < 2) {
880
- return { provider: "local", model: modelString };
881
- }
882
- return { provider: parts[0], model: parts.slice(1).join(":") };
883
- }
884
- /**
885
- * Resolves the model to use for a prompt.
886
- *
887
- * @param prompt - The prompt to analyze
888
- * @param overrideTaskType - Optional task type override
889
- * @param overrideModel - Optional model override
890
- * @returns Object with resolved taskType, model, provider, and confidence
891
- */
892
- resolve(prompt, overrideTaskType, overrideModel) {
893
- const taskType = overrideTaskType ?? this.inferTaskType(prompt);
894
- const confidence = this.getInferenceConfidence(prompt, taskType);
895
- let model;
896
- if (overrideModel) {
897
- model = overrideModel;
898
- } else {
899
- model = this.getPreferredModel(taskType);
900
- }
901
- const { provider, model: modelName } = this.parseModel(model);
902
- return {
903
- taskType,
904
- model,
905
- provider,
906
- modelName,
907
- confidence
908
- };
909
- }
910
- };
911
-
912
- // src/learning/outcomes.ts
913
- var OutcomeRecorder = class {
914
- store;
915
- /**
916
- * Creates a new OutcomeRecorder.
917
- *
918
- * @param store - The storage instance to use
919
- */
920
- constructor(store) {
921
- this.store = store;
922
- }
923
- /**
924
- * Records an outcome for a run.
925
- *
926
- * @param input - The outcome input
927
- * @returns The recorded outcome
928
- * @throws If the run ID is not found
929
- */
930
- record(input) {
931
- const run = this.store.getRun(input.runId);
932
- if (!run) {
933
- throw new Error(`Run not found: ${input.runId}`);
934
- }
935
- const id = this.store.recordOutcome({
936
- runId: input.runId,
937
- success: input.success,
938
- quality: input.quality ?? null,
939
- latencySatisfactory: input.latencySatisfactory ?? null,
940
- costSatisfactory: input.costSatisfactory ?? null,
941
- feedback: input.feedback ?? null
942
- });
943
- const outcome = this.store.getOutcome(input.runId);
944
- if (!outcome) {
945
- throw new Error("Failed to record outcome");
946
- }
947
- return {
948
- id: outcome.id,
949
- runId: outcome.runId,
950
- success: outcome.success,
951
- quality: outcome.quality ?? void 0,
952
- latencySatisfactory: outcome.latencySatisfactory ?? void 0,
953
- costSatisfactory: outcome.costSatisfactory ?? void 0,
954
- feedback: outcome.feedback ?? void 0,
955
- recordedAt: outcome.createdAt
956
- };
957
- }
958
- /**
959
- * Gets an outcome for a run.
960
- *
961
- * @param runId - The run ID
962
- * @returns The outcome, or null if not found
963
- */
964
- get(runId) {
965
- const outcome = this.store.getOutcome(runId);
966
- if (!outcome) return null;
967
- return {
968
- id: outcome.id,
969
- runId: outcome.runId,
970
- success: outcome.success,
971
- quality: outcome.quality ?? void 0,
972
- latencySatisfactory: outcome.latencySatisfactory ?? void 0,
973
- costSatisfactory: outcome.costSatisfactory ?? void 0,
974
- feedback: outcome.feedback ?? void 0,
975
- recordedAt: outcome.createdAt
976
- };
977
- }
978
- /**
979
- * Gets outcome statistics for a task type.
980
- *
981
- * @param taskType - The task type to get stats for
982
- * @returns Outcome statistics
983
- */
984
- getTaskStats(taskType) {
985
- const outcomes = this.store.getOutcomes({ taskType, limit: 1e3 });
986
- if (outcomes.length === 0) {
987
- return {
988
- totalOutcomes: 0,
989
- successRate: 0,
990
- qualityDistribution: {},
991
- latencySatisfactionRate: 0,
992
- costSatisfactionRate: 0
993
- };
994
- }
995
- let successCount = 0;
996
- let latencySatisfiedCount = 0;
997
- let latencyRatedCount = 0;
998
- let costSatisfiedCount = 0;
999
- let costRatedCount = 0;
1000
- const qualityDistribution = {};
1001
- for (const outcome of outcomes) {
1002
- if (outcome.success) successCount++;
1003
- if (outcome.quality) {
1004
- qualityDistribution[outcome.quality] = (qualityDistribution[outcome.quality] ?? 0) + 1;
1005
- }
1006
- if (outcome.latencySatisfactory != null) {
1007
- latencyRatedCount++;
1008
- if (outcome.latencySatisfactory) latencySatisfiedCount++;
1009
- }
1010
- if (outcome.costSatisfactory != null) {
1011
- costRatedCount++;
1012
- if (outcome.costSatisfactory) costSatisfiedCount++;
1013
- }
1014
- }
1015
- return {
1016
- totalOutcomes: outcomes.length,
1017
- successRate: successCount / outcomes.length,
1018
- qualityDistribution,
1019
- latencySatisfactionRate: latencyRatedCount > 0 ? latencySatisfiedCount / latencyRatedCount : 0,
1020
- costSatisfactionRate: costRatedCount > 0 ? costSatisfiedCount / costRatedCount : 0
1021
- };
1022
- }
1023
- };
1024
-
1025
- // src/learning/savings.ts
1026
- var MODEL_PRICING = {
1027
- // Anthropic models
1028
- "claude-3-5-haiku-latest": { input: 0.25, output: 1.25 },
1029
- "claude-3-5-haiku-20241022": { input: 0.25, output: 1.25 },
1030
- "claude-3-5-sonnet-latest": { input: 3, output: 15 },
1031
- "claude-3-5-sonnet-20241022": { input: 3, output: 15 },
1032
- "claude-sonnet-4-20250514": { input: 3, output: 15 },
1033
- "claude-3-opus-latest": { input: 15, output: 75 },
1034
- "claude-3-opus-20240229": { input: 15, output: 75 },
1035
- "claude-opus-4-5-20250514": { input: 15, output: 75 },
1036
- // OpenAI models
1037
- "gpt-4o": { input: 2.5, output: 10 },
1038
- "gpt-4o-mini": { input: 0.15, output: 0.6 },
1039
- "gpt-4.1": { input: 2, output: 8 },
1040
- "gpt-4-turbo": { input: 10, output: 30 },
1041
- // Google models
1042
- "gemini-1.5-flash": { input: 0.075, output: 0.3 },
1043
- "gemini-1.5-pro": { input: 1.25, output: 5 },
1044
- "gemini-2.0-flash": { input: 0.1, output: 0.4 },
1045
- // xAI models
1046
- "grok-2": { input: 2, output: 10 },
1047
- "grok-2-latest": { input: 2, output: 10 },
1048
- // Moonshot models
1049
- "moonshot-v1-8k": { input: 0.1, output: 0.1 },
1050
- "moonshot-v1-32k": { input: 0.2, output: 0.2 }
1051
- };
1052
- var BASELINE_MODEL = "claude-3-opus-latest";
1053
- function calculateCost(model, tokensIn, tokensOut) {
1054
- const modelName = model.includes(":") ? model.split(":")[1] : model;
1055
- const pricing = MODEL_PRICING[modelName] ?? MODEL_PRICING[model] ?? { input: 1, output: 3 };
1056
- const inputCost = tokensIn / 1e6 * pricing.input;
1057
- const outputCost = tokensOut / 1e6 * pricing.output;
1058
- return inputCost + outputCost;
1059
- }
1060
- function calculateSavings(store, days = 30) {
1061
- const to = /* @__PURE__ */ new Date();
1062
- const from = /* @__PURE__ */ new Date();
1063
- from.setDate(from.getDate() - days);
1064
- const fromStr = from.toISOString();
1065
- const toStr = to.toISOString();
1066
- const runs = store.getRuns({
1067
- from: fromStr,
1068
- to: toStr,
1069
- limit: 1e5
1070
- // Get all runs
1071
- });
1072
- const byModel = {};
1073
- const byTaskType = {};
1074
- let totalTokensIn = 0;
1075
- let totalTokensOut = 0;
1076
- let actualCost = 0;
1077
- let baselineCost = 0;
1078
- const baselinePricing = MODEL_PRICING[BASELINE_MODEL] ?? { input: 15, output: 75 };
1079
- for (const run of runs) {
1080
- const tokensIn = run.tokensIn ?? 0;
1081
- const tokensOut = run.tokensOut ?? 0;
1082
- const modelName = run.model.includes(":") ? run.model.split(":")[1] ?? run.model : run.model;
1083
- const runCost = calculateCost(run.model, tokensIn, tokensOut);
1084
- actualCost += runCost;
1085
- const baselineRunCost = tokensIn / 1e6 * (baselinePricing?.input ?? 15) + tokensOut / 1e6 * (baselinePricing?.output ?? 75);
1086
- baselineCost += baselineRunCost;
1087
- totalTokensIn += tokensIn;
1088
- totalTokensOut += tokensOut;
1089
- if (!byModel[modelName]) {
1090
- byModel[modelName] = {
1091
- runs: 0,
1092
- tokensIn: 0,
1093
- tokensOut: 0,
1094
- cost: 0,
1095
- successRate: 0,
1096
- avgLatencyMs: 0
1097
- };
1098
- }
1099
- const modelStats = byModel[modelName];
1100
- modelStats.runs++;
1101
- modelStats.tokensIn += tokensIn;
1102
- modelStats.tokensOut += tokensOut;
1103
- modelStats.cost += runCost;
1104
- modelStats.avgLatencyMs += run.durationMs;
1105
- if (run.success) {
1106
- modelStats.successRate++;
1107
- }
1108
- if (!byTaskType[run.taskType]) {
1109
- byTaskType[run.taskType] = { runs: 0, cost: 0, totalCost: 0 };
1110
- }
1111
- const taskStats = byTaskType[run.taskType];
1112
- taskStats.runs++;
1113
- taskStats.totalCost += runCost;
1114
- }
1115
- for (const model of Object.keys(byModel)) {
1116
- const stats = byModel[model];
1117
- stats.successRate = stats.runs > 0 ? stats.successRate / stats.runs : 0;
1118
- stats.avgLatencyMs = stats.runs > 0 ? stats.avgLatencyMs / stats.runs : 0;
1119
- }
1120
- const byTaskTypeFinal = {};
1121
- for (const [taskType, stats] of Object.entries(byTaskType)) {
1122
- byTaskTypeFinal[taskType] = {
1123
- runs: stats.runs,
1124
- cost: stats.totalCost,
1125
- avgCostPerRun: stats.runs > 0 ? stats.totalCost / stats.runs : 0
1126
- };
1127
- }
1128
- const savings = baselineCost - actualCost;
1129
- const savingsPercent = baselineCost > 0 ? savings / baselineCost * 100 : 0;
1130
- return {
1131
- periodDays: days,
1132
- period: {
1133
- from: fromStr,
1134
- to: toStr
1135
- },
1136
- totalRuns: runs.length,
1137
- totalTokensIn,
1138
- totalTokensOut,
1139
- actualCost,
1140
- baselineCost,
1141
- savings: Math.max(0, savings),
1142
- // Don't report negative savings
1143
- savingsPercent: Math.max(0, savingsPercent),
1144
- byModel,
1145
- byTaskType: byTaskTypeFinal
1146
- };
1147
- }
1148
-
1149
- // src/learning/patterns.ts
1150
- var MIN_RUNS_FOR_SUGGESTION = 10;
1151
- var MIN_CONFIDENCE_THRESHOLD = 0.6;
1152
- var MIN_IMPROVEMENT_THRESHOLD = 0.1;
1153
- var MIN_COST_IMPROVEMENT_THRESHOLD = 0.2;
1154
- var PatternDetector = class {
1155
- store;
1156
- /**
1157
- * Creates a new PatternDetector.
1158
- *
1159
- * @param store - The storage instance to use
1160
- */
1161
- constructor(store) {
1162
- this.store = store;
1163
- }
1164
- /**
1165
- * Analyzes a task type and generates suggestions if appropriate.
1166
- *
1167
- * @param taskType - The task type to analyze
1168
- * @returns Array of suggestions
1169
- */
1170
- analyzeTaskType(taskType) {
1171
- const stats = this.store.getLearningStats(taskType);
1172
- if (stats.length < 2) {
1173
- return [];
1174
- }
1175
- const currentRule = this.store.getRule(taskType);
1176
- if (!currentRule) return [];
1177
- const currentModel = currentRule.preferredModel;
1178
- const currentStats = stats.find((s) => s.model === currentModel);
1179
- if (!currentStats) return [];
1180
- const currentModelName = currentModel.includes(":") ? currentModel.split(":")[1] : currentModel;
1181
- const currentPricing = MODEL_PRICING[currentModelName];
1182
- const suggestions = [];
1183
- for (const modelStats of stats) {
1184
- if (modelStats.model === currentModel) continue;
1185
- if (modelStats.runs < MIN_RUNS_FOR_SUGGESTION) continue;
1186
- const successImprovement = modelStats.outcomeSuccessRate - currentStats.outcomeSuccessRate;
1187
- const latencyImprovement = (currentStats.avgDurationMs - modelStats.avgDurationMs) / currentStats.avgDurationMs;
1188
- const suggestedModelName = modelStats.model.includes(":") ? modelStats.model.split(":")[1] : modelStats.model;
1189
- const suggestedPricing = MODEL_PRICING[suggestedModelName];
1190
- let costImprovement = 0;
1191
- if (currentPricing && suggestedPricing) {
1192
- const currentAvgCost = (currentPricing.input + currentPricing.output) / 2;
1193
- const suggestedAvgCost = (suggestedPricing.input + suggestedPricing.output) / 2;
1194
- costImprovement = (currentAvgCost - suggestedAvgCost) / currentAvgCost;
1195
- }
1196
- const isSignificantlyBetter = successImprovement > MIN_IMPROVEMENT_THRESHOLD || successImprovement >= 0 && latencyImprovement > MIN_IMPROVEMENT_THRESHOLD || successImprovement >= -0.05 && costImprovement > MIN_COST_IMPROVEMENT_THRESHOLD;
1197
- if (!isSignificantlyBetter) continue;
1198
- const sampleConfidence = Math.min(modelStats.runs / 50, 1);
1199
- const improvementConfidence = Math.min(
1200
- Math.abs(successImprovement) / 0.3 + Math.abs(latencyImprovement) / 0.5 + Math.abs(costImprovement) / 0.5,
1201
- 1
1202
- );
1203
- const confidence = (sampleConfidence + improvementConfidence) / 2;
1204
- if (confidence < MIN_CONFIDENCE_THRESHOLD) continue;
1205
- const reasons = [];
1206
- if (successImprovement > 0) {
1207
- reasons.push(`${(successImprovement * 100).toFixed(0)}% higher success rate`);
1208
- }
1209
- if (latencyImprovement > 0) {
1210
- reasons.push(`${(latencyImprovement * 100).toFixed(0)}% faster`);
1211
- }
1212
- if (costImprovement > 0) {
1213
- reasons.push(`${(costImprovement * 100).toFixed(0)}% cheaper`);
1214
- }
1215
- const suggestion = {
1216
- id: nanoid(),
1217
- taskType,
1218
- currentModel,
1219
- suggestedModel: modelStats.model,
1220
- reason: reasons.join(", "),
1221
- confidence,
1222
- expectedImprovement: {
1223
- successRate: successImprovement > 0 ? successImprovement : void 0,
1224
- latency: latencyImprovement > 0 ? latencyImprovement : void 0,
1225
- cost: costImprovement > 0 ? costImprovement : void 0
1226
- },
1227
- sampleCount: modelStats.runs,
1228
- createdAt: (/* @__PURE__ */ new Date()).toISOString()
1229
- };
1230
- suggestions.push(suggestion);
1231
- }
1232
- suggestions.sort((a, b) => b.confidence - a.confidence);
1233
- return suggestions;
1234
- }
1235
- /**
1236
- * Analyzes all task types and generates suggestions.
1237
- *
1238
- * @returns Array of all suggestions across task types
1239
- */
1240
- analyzeAll() {
1241
- const taskTypes = [
1242
- "code_generation",
1243
- "code_review",
1244
- "summarization",
1245
- "analysis",
1246
- "creative_writing",
1247
- "data_extraction",
1248
- "translation",
1249
- "question_answering",
1250
- "general"
1251
- ];
1252
- const allSuggestions = [];
1253
- for (const taskType of taskTypes) {
1254
- const suggestions = this.analyzeTaskType(taskType);
1255
- allSuggestions.push(...suggestions);
1256
- }
1257
- allSuggestions.sort((a, b) => b.confidence - a.confidence);
1258
- return allSuggestions;
1259
- }
1260
- /**
1261
- * Stores suggestions in the database.
1262
- *
1263
- * @param suggestions - The suggestions to store
1264
- * @returns Array of suggestion IDs
1265
- */
1266
- storeSuggestions(suggestions) {
1267
- const ids = [];
1268
- for (const suggestion of suggestions) {
1269
- const id = this.store.recordSuggestion({
1270
- taskType: suggestion.taskType,
1271
- currentModel: suggestion.currentModel,
1272
- suggestedModel: suggestion.suggestedModel,
1273
- reason: suggestion.reason,
1274
- confidence: suggestion.confidence,
1275
- expectedImprovement: JSON.stringify(suggestion.expectedImprovement),
1276
- sampleCount: suggestion.sampleCount,
1277
- accepted: null
1278
- });
1279
- ids.push(id);
1280
- }
1281
- return ids;
1282
- }
1283
- /**
1284
- * Generates and stores new suggestions, returning only new ones.
1285
- *
1286
- * @returns Array of new suggestions
1287
- */
1288
- generateSuggestions() {
1289
- const suggestions = this.analyzeAll();
1290
- const pending = this.store.getPendingSuggestions();
1291
- const existingKeys = new Set(
1292
- pending.map((s) => `${s.taskType}:${s.suggestedModel}`)
1293
- );
1294
- const newSuggestions = suggestions.filter(
1295
- (s) => !existingKeys.has(`${s.taskType}:${s.suggestedModel}`)
1296
- );
1297
- this.storeSuggestions(newSuggestions);
1298
- return newSuggestions;
1299
- }
1300
- };
1301
-
1302
- // src/relay.ts
1303
- var RelayPlane = class {
1304
- store;
1305
- _routing;
1306
- outcomeRecorder;
1307
- patternDetector;
1308
- config;
1309
- adapterRegistry = null;
1310
- /**
1311
- * Creates a new RelayPlane instance.
1312
- *
1313
- * @param config - Configuration options
1314
- */
1315
- constructor(config = {}) {
1316
- this.config = {
1317
- dbPath: config.dbPath ?? getDefaultDbPath(),
1318
- defaultProvider: config.defaultProvider ?? "local",
1319
- defaultModel: config.defaultModel ?? "llama3.2",
1320
- providers: config.providers ?? {}
1321
- };
1322
- this.store = new Store(this.config.dbPath);
1323
- this._routing = new RoutingEngine(this.store);
1324
- this.outcomeRecorder = new OutcomeRecorder(this.store);
1325
- this.patternDetector = new PatternDetector(this.store);
1326
- }
1327
- /**
1328
- * Gets the routing engine for direct access.
1329
- */
1330
- get routing() {
1331
- return this._routing;
1332
- }
1333
- /**
1334
- * Runs a prompt through the appropriate model.
1335
- *
1336
- * @param input - The run input
1337
- * @returns The run result
1338
- */
1339
- async run(input) {
1340
- const startTime = Date.now();
1341
- const resolved = this._routing.resolve(input.prompt, input.taskType, input.model);
1342
- const adapter = await this.getAdapter(resolved.provider);
1343
- if (!adapter) {
1344
- const runId2 = this.store.recordRun({
1345
- prompt: input.prompt,
1346
- systemPrompt: input.systemPrompt ?? null,
1347
- taskType: resolved.taskType,
1348
- model: resolved.model,
1349
- success: false,
1350
- output: null,
1351
- error: `No adapter configured for provider: ${resolved.provider}`,
1352
- durationMs: Date.now() - startTime,
1353
- tokensIn: null,
1354
- tokensOut: null,
1355
- costUsd: null,
1356
- metadata: input.metadata ? JSON.stringify(input.metadata) : null
1357
- });
1358
- return {
1359
- runId: runId2,
1360
- success: false,
1361
- error: `No adapter configured for provider: ${resolved.provider}`,
1362
- taskType: resolved.taskType,
1363
- model: resolved.model,
1364
- durationMs: Date.now() - startTime,
1365
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
1366
- };
1367
- }
1368
- const providerConfig = this.config.providers?.[resolved.provider];
1369
- const apiKey = providerConfig?.apiKey ?? this.getEnvApiKey(resolved.provider);
1370
- const fullInput = input.systemPrompt ? `${input.systemPrompt}
1371
-
1372
- ${input.prompt}` : input.prompt;
1373
- const result = await adapter.execute({
1374
- model: resolved.modelName,
1375
- input: fullInput,
1376
- apiKey: apiKey ?? "",
1377
- baseUrl: providerConfig?.baseUrl
1378
- });
1379
- const durationMs = Date.now() - startTime;
1380
- const tokensIn = result.tokensIn ?? 0;
1381
- const tokensOut = result.tokensOut ?? 0;
1382
- const costUsd = calculateCost(resolved.model, tokensIn, tokensOut);
1383
- const runId = this.store.recordRun({
1384
- prompt: input.prompt,
1385
- systemPrompt: input.systemPrompt ?? null,
1386
- taskType: resolved.taskType,
1387
- model: resolved.model,
1388
- success: result.success,
1389
- output: result.output ?? null,
1390
- error: result.error?.message ?? null,
1391
- durationMs,
1392
- tokensIn: result.tokensIn ?? null,
1393
- tokensOut: result.tokensOut ?? null,
1394
- costUsd: costUsd > 0 ? costUsd : null,
1395
- metadata: input.metadata ? JSON.stringify(input.metadata) : null
1396
- });
1397
- return {
1398
- runId,
1399
- success: result.success,
1400
- output: result.output,
1401
- error: result.error?.message,
1402
- taskType: resolved.taskType,
1403
- model: resolved.model,
1404
- durationMs,
1405
- tokensIn: result.tokensIn,
1406
- tokensOut: result.tokensOut,
1407
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
1408
- };
1409
- }
1410
- /**
1411
- * Gets an adapter for a provider.
1412
- * Note: In the standalone proxy package, adapters are not used.
1413
- * The proxy handles provider calls directly via HTTP.
1414
- */
1415
- async getAdapter(_provider) {
1416
- return null;
1417
- }
1418
- /**
1419
- * Gets an API key from environment variables.
1420
- */
1421
- getEnvApiKey(provider) {
1422
- const envVars = {
1423
- openai: "OPENAI_API_KEY",
1424
- anthropic: "ANTHROPIC_API_KEY",
1425
- google: "GOOGLE_API_KEY",
1426
- xai: "XAI_API_KEY",
1427
- moonshot: "MOONSHOT_API_KEY",
1428
- local: ""
1429
- };
1430
- const envVar = envVars[provider];
1431
- return envVar ? process.env[envVar] : void 0;
1432
- }
1433
- /**
1434
- * Records an outcome for a run.
1435
- *
1436
- * @param runId - The run ID
1437
- * @param outcome - The outcome details
1438
- * @returns The recorded outcome
1439
- */
1440
- recordOutcome(runId, outcome) {
1441
- return this.outcomeRecorder.record({
1442
- runId,
1443
- ...outcome
1444
- });
1445
- }
1446
- /**
1447
- * Gets an outcome for a run.
1448
- *
1449
- * @param runId - The run ID
1450
- * @returns The outcome, or null if not found
1451
- */
1452
- getOutcome(runId) {
1453
- return this.outcomeRecorder.get(runId);
1454
- }
1455
- /**
1456
- * Gets statistics for runs.
1457
- *
1458
- * @param options - Optional filters
1459
- * @returns Statistics object
1460
- */
1461
- stats(options) {
1462
- const raw = this.store.getStats(options);
1463
- const byTaskType = {};
1464
- const taskTypes = [
1465
- "code_generation",
1466
- "code_review",
1467
- "summarization",
1468
- "analysis",
1469
- "creative_writing",
1470
- "data_extraction",
1471
- "translation",
1472
- "question_answering",
1473
- "general"
1474
- ];
1475
- for (const taskType of taskTypes) {
1476
- const taskStats = raw.byTaskType[taskType];
1477
- byTaskType[taskType] = {
1478
- taskType,
1479
- totalRuns: taskStats?.runs ?? 0,
1480
- successfulRuns: Math.round((taskStats?.runs ?? 0) * (taskStats?.successRate ?? 0)),
1481
- successRate: taskStats?.successRate ?? 0,
1482
- avgDurationMs: taskStats?.avgDurationMs ?? 0,
1483
- byModel: {}
1484
- };
1485
- }
1486
- for (const [model, modelStats] of Object.entries(raw.byModel)) {
1487
- for (const taskType of taskTypes) {
1488
- if (!byTaskType[taskType].byModel[model]) {
1489
- byTaskType[taskType].byModel[model] = {
1490
- runs: 0,
1491
- successRate: 0,
1492
- avgDurationMs: 0
1493
- };
1494
- }
1495
- }
1496
- }
1497
- return {
1498
- totalRuns: raw.totalRuns,
1499
- overallSuccessRate: raw.totalRuns > 0 ? raw.successfulRuns / raw.totalRuns : 0,
1500
- byTaskType,
1501
- period: {
1502
- from: options?.from ?? "",
1503
- to: options?.to ?? (/* @__PURE__ */ new Date()).toISOString()
1504
- }
1505
- };
1506
- }
1507
- /**
1508
- * Gets a savings report.
1509
- *
1510
- * @param days - Number of days to include (default: 30)
1511
- * @returns Savings report
1512
- */
1513
- savingsReport(days = 30) {
1514
- return calculateSavings(this.store, days);
1515
- }
1516
- /**
1517
- * Gets routing improvement suggestions.
1518
- *
1519
- * @returns Array of suggestions
1520
- */
1521
- getSuggestions() {
1522
- const pending = this.store.getPendingSuggestions();
1523
- return pending.map((record) => ({
1524
- id: record.id,
1525
- taskType: record.taskType,
1526
- currentModel: record.currentModel,
1527
- suggestedModel: record.suggestedModel,
1528
- reason: record.reason,
1529
- confidence: record.confidence,
1530
- expectedImprovement: JSON.parse(record.expectedImprovement),
1531
- sampleCount: record.sampleCount,
1532
- createdAt: record.createdAt,
1533
- accepted: record.accepted ?? void 0,
1534
- acceptedAt: record.acceptedAt ?? void 0
1535
- }));
1536
- }
1537
- /**
1538
- * Generates new suggestions based on current data.
1539
- *
1540
- * @returns Array of newly generated suggestions
1541
- */
1542
- generateSuggestions() {
1543
- return this.patternDetector.generateSuggestions();
1544
- }
1545
- /**
1546
- * Accepts a suggestion and updates routing.
1547
- *
1548
- * @param suggestionId - The suggestion ID to accept
1549
- * @returns True if successful
1550
- */
1551
- acceptSuggestion(suggestionId) {
1552
- return this.store.acceptSuggestion(suggestionId);
1553
- }
1554
- /**
1555
- * Rejects a suggestion.
1556
- *
1557
- * @param suggestionId - The suggestion ID to reject
1558
- * @returns True if successful
1559
- */
1560
- rejectSuggestion(suggestionId) {
1561
- return this.store.rejectSuggestion(suggestionId);
1562
- }
1563
- /**
1564
- * Closes the RelayPlane instance and releases resources.
1565
- */
1566
- close() {
1567
- this.store.close();
1568
- }
1569
- };
1570
-
1571
- // src/config.ts
1572
- import * as fs2 from "fs";
1573
- import * as path2 from "path";
1574
- import * as os2 from "os";
1575
- import { z } from "zod";
1576
- var StrategySchema = z.object({
1577
- model: z.string(),
1578
- minConfidence: z.number().min(0).max(1).optional(),
1579
- fallback: z.string().optional()
1580
- });
1581
- var AuthSchema = z.object({
1582
- anthropicApiKey: z.string().optional(),
1583
- anthropicMaxToken: z.string().optional(),
1584
- useMaxForModels: z.array(z.string()).optional()
1585
- // Default: ['opus']
1586
- }).optional();
1587
- var ConfigSchema = z.object({
1588
- strategies: z.record(z.string(), StrategySchema).optional(),
1589
- defaults: z.object({
1590
- qualityModel: z.string().optional(),
1591
- costModel: z.string().optional()
1592
- }).optional(),
1593
- auth: AuthSchema
1594
- });
1595
- var DEFAULT_CONFIG = {
1596
- strategies: {
1597
- code_review: { model: "anthropic:claude-sonnet-4-20250514" },
1598
- code_generation: { model: "anthropic:claude-3-5-haiku-latest" },
1599
- analysis: { model: "anthropic:claude-sonnet-4-20250514" },
1600
- summarization: { model: "anthropic:claude-3-5-haiku-latest" },
1601
- creative_writing: { model: "anthropic:claude-sonnet-4-20250514" },
1602
- data_extraction: { model: "anthropic:claude-3-5-haiku-latest" },
1603
- translation: { model: "anthropic:claude-3-5-haiku-latest" },
1604
- question_answering: { model: "anthropic:claude-3-5-haiku-latest" },
1605
- general: { model: "anthropic:claude-3-5-haiku-latest" }
1606
- },
1607
- defaults: {
1608
- qualityModel: "claude-sonnet-4-20250514",
1609
- costModel: "claude-3-5-haiku-latest"
1610
- }
1611
- };
1612
- function getConfigPath() {
1613
- return path2.join(os2.homedir(), ".relayplane", "config.json");
1614
- }
1615
- function writeDefaultConfig() {
1616
- const configPath = getConfigPath();
1617
- const dir = path2.dirname(configPath);
1618
- if (!fs2.existsSync(dir)) {
1619
- fs2.mkdirSync(dir, { recursive: true });
1620
- }
1621
- if (!fs2.existsSync(configPath)) {
1622
- fs2.writeFileSync(
1623
- configPath,
1624
- JSON.stringify(DEFAULT_CONFIG, null, 2) + "\n",
1625
- "utf-8"
1626
- );
1627
- console.log(`[relayplane] Created default config at ${configPath}`);
1628
- }
1629
- }
1630
- function loadConfig() {
1631
- const configPath = getConfigPath();
1632
- writeDefaultConfig();
1633
- try {
1634
- const raw = fs2.readFileSync(configPath, "utf-8");
1635
- const parsed = JSON.parse(raw);
1636
- const validated = ConfigSchema.parse(parsed);
1637
- return validated;
1638
- } catch (err) {
1639
- if (err instanceof z.ZodError) {
1640
- console.error(`[relayplane] Invalid config: ${err.message}`);
1641
- } else if (err instanceof SyntaxError) {
1642
- console.error(`[relayplane] Config JSON parse error: ${err.message}`);
1643
- } else {
1644
- console.error(`[relayplane] Failed to load config: ${err}`);
1645
- }
1646
- console.log("[relayplane] Using default config");
1647
- return DEFAULT_CONFIG;
1648
- }
1649
- }
1650
- function getStrategy(config, taskType) {
1651
- return config.strategies?.[taskType] ?? null;
1652
- }
1653
- function getAnthropicAuth(config, model) {
1654
- const auth = config.auth;
1655
- const useMaxForModels = auth?.useMaxForModels ?? ["opus"];
1656
- const shouldUseMax = useMaxForModels.some((m) => model.toLowerCase().includes(m.toLowerCase()));
1657
- if (shouldUseMax && auth?.anthropicMaxToken) {
1658
- return { type: "max", value: auth.anthropicMaxToken };
1659
- }
1660
- const apiKey = auth?.anthropicApiKey ?? process.env["ANTHROPIC_API_KEY"];
1661
- if (apiKey) {
1662
- return { type: "apiKey", value: apiKey };
1663
- }
1664
- return null;
1665
- }
1666
- function watchConfig(onChange) {
1667
- const configPath = getConfigPath();
1668
- const dir = path2.dirname(configPath);
1669
- if (!fs2.existsSync(dir)) {
1670
- fs2.mkdirSync(dir, { recursive: true });
1671
- }
1672
- let debounceTimer = null;
1673
- fs2.watch(dir, (eventType, filename) => {
1674
- if (filename === "config.json") {
1675
- if (debounceTimer) clearTimeout(debounceTimer);
1676
- debounceTimer = setTimeout(() => {
1677
- console.log("[relayplane] Config file changed, reloading...");
1678
- const newConfig = loadConfig();
1679
- onChange(newConfig);
1680
- }, 100);
1681
- }
1682
- });
1683
- }
1684
-
1685
- // src/proxy.ts
1686
- var VERSION = "0.1.9";
1687
- var recentRuns = [];
1688
- var MAX_RECENT_RUNS = 100;
1689
- var modelCounts = {};
1690
- var tokenStats = {};
1691
- var MODEL_PRICING2 = {
1692
- // Anthropic
1693
- "claude-3-haiku-20240307": { input: 0.25, output: 1.25 },
1694
- "claude-3-5-haiku-20241022": { input: 1, output: 5 },
1695
- "claude-3-5-haiku-latest": { input: 1, output: 5 },
1696
- "claude-3-5-sonnet-20241022": { input: 3, output: 15 },
1697
- "claude-sonnet-4-20250514": { input: 3, output: 15 },
1698
- "claude-3-opus-20240229": { input: 15, output: 75 },
1699
- "claude-opus-4-5-20250514": { input: 15, output: 75 },
1700
- // OpenAI
1701
- "gpt-4o": { input: 2.5, output: 10 },
1702
- "gpt-4o-mini": { input: 0.15, output: 0.6 },
1703
- "gpt-4-turbo": { input: 10, output: 30 },
1704
- // Defaults for unknown models
1705
- "default-cheap": { input: 1, output: 5 },
1706
- "default-expensive": { input: 15, output: 75 }
1707
- };
1708
- function trackTokens(model, inputTokens, outputTokens) {
1709
- if (!tokenStats[model]) {
1710
- tokenStats[model] = { inputTokens: 0, outputTokens: 0, requests: 0 };
1711
- }
1712
- tokenStats[model].inputTokens += inputTokens;
1713
- tokenStats[model].outputTokens += outputTokens;
1714
- tokenStats[model].requests += 1;
1715
- }
1716
- function calculateCosts() {
1717
- let totalInputTokens = 0;
1718
- let totalOutputTokens = 0;
1719
- let actualCostUsd = 0;
1720
- const byModel = {};
1721
- for (const [model, stats] of Object.entries(tokenStats)) {
1722
- totalInputTokens += stats.inputTokens;
1723
- totalOutputTokens += stats.outputTokens;
1724
- const pricing = MODEL_PRICING2[model] || MODEL_PRICING2["default-cheap"];
1725
- const cost = stats.inputTokens / 1e6 * pricing.input + stats.outputTokens / 1e6 * pricing.output;
1726
- actualCostUsd += cost;
1727
- byModel[model] = {
1728
- inputTokens: stats.inputTokens,
1729
- outputTokens: stats.outputTokens,
1730
- costUsd: parseFloat(cost.toFixed(4))
1731
- };
1732
- }
1733
- const opusPricing = MODEL_PRICING2["claude-opus-4-5-20250514"];
1734
- const opusCostUsd = totalInputTokens / 1e6 * opusPricing.input + totalOutputTokens / 1e6 * opusPricing.output;
1735
- const savingsUsd = opusCostUsd - actualCostUsd;
1736
- const savingsPercent = opusCostUsd > 0 ? (savingsUsd / opusCostUsd * 100).toFixed(1) + "%" : "0%";
1737
- return {
1738
- totalInputTokens,
1739
- totalOutputTokens,
1740
- actualCostUsd: parseFloat(actualCostUsd.toFixed(4)),
1741
- opusCostUsd: parseFloat(opusCostUsd.toFixed(4)),
1742
- savingsUsd: parseFloat(savingsUsd.toFixed(4)),
1743
- savingsPercent,
1744
- byModel
1745
- };
1746
- }
1747
- var serverStartTime = 0;
1748
- var currentConfig = loadConfig();
1749
- var DEFAULT_ENDPOINTS = {
1750
- anthropic: {
1751
- baseUrl: "https://api.anthropic.com/v1",
1752
- apiKeyEnv: "ANTHROPIC_API_KEY"
1753
- },
1754
- openai: {
1755
- baseUrl: "https://api.openai.com/v1",
1756
- apiKeyEnv: "OPENAI_API_KEY"
1757
- },
1758
- google: {
1759
- baseUrl: "https://generativelanguage.googleapis.com/v1beta",
1760
- apiKeyEnv: "GEMINI_API_KEY"
1761
- },
1762
- xai: {
1763
- baseUrl: "https://api.x.ai/v1",
1764
- apiKeyEnv: "XAI_API_KEY"
1765
- },
1766
- moonshot: {
1767
- baseUrl: "https://api.moonshot.cn/v1",
1768
- apiKeyEnv: "MOONSHOT_API_KEY"
1769
- }
1770
- };
1771
- var MODEL_MAPPING = {
1772
- // Anthropic models (using correct API model IDs)
1773
- "claude-opus-4-5": { provider: "anthropic", model: "claude-opus-4-5-20250514" },
1774
- "claude-sonnet-4": { provider: "anthropic", model: "claude-sonnet-4-20250514" },
1775
- "claude-3-5-sonnet": { provider: "anthropic", model: "claude-3-5-sonnet-20241022" },
1776
- "claude-3-5-haiku": { provider: "anthropic", model: "claude-3-5-haiku-20241022" },
1777
- haiku: { provider: "anthropic", model: "claude-3-5-haiku-20241022" },
1778
- sonnet: { provider: "anthropic", model: "claude-3-5-sonnet-20241022" },
1779
- opus: { provider: "anthropic", model: "claude-3-opus-20240229" },
1780
- // OpenAI models
1781
- "gpt-4o": { provider: "openai", model: "gpt-4o" },
1782
- "gpt-4o-mini": { provider: "openai", model: "gpt-4o-mini" },
1783
- "gpt-4.1": { provider: "openai", model: "gpt-4.1" }
1784
- };
1785
- var DEFAULT_ROUTING = {
1786
- // Complex tasks → Sonnet (need reasoning & quality)
1787
- code_review: { provider: "anthropic", model: "claude-sonnet-4-20250514" },
1788
- analysis: { provider: "anthropic", model: "claude-sonnet-4-20250514" },
1789
- creative_writing: { provider: "anthropic", model: "claude-sonnet-4-20250514" },
1790
- // Medium tasks → Sonnet (benefit from better model)
1791
- code_generation: { provider: "anthropic", model: "claude-sonnet-4-20250514" },
1792
- // Simple tasks → Haiku (cost efficient)
1793
- summarization: { provider: "anthropic", model: "claude-3-5-haiku-latest" },
1794
- data_extraction: { provider: "anthropic", model: "claude-3-5-haiku-latest" },
1795
- translation: { provider: "anthropic", model: "claude-3-5-haiku-latest" },
1796
- question_answering: { provider: "anthropic", model: "claude-3-5-haiku-latest" },
1797
- general: { provider: "anthropic", model: "claude-3-5-haiku-latest" }
1798
- };
1799
- function extractPromptText(messages) {
1800
- return messages.map((msg) => {
1801
- if (typeof msg.content === "string") return msg.content;
1802
- if (Array.isArray(msg.content)) {
1803
- return msg.content.map((c) => {
1804
- const part = c;
1805
- return part.type === "text" ? part.text ?? "" : "";
1806
- }).join(" ");
1807
- }
1808
- return "";
1809
- }).join("\n");
1810
- }
1811
- async function forwardToAnthropic(request, targetModel, auth, betaHeaders) {
1812
- const anthropicBody = buildAnthropicBody(request, targetModel, false);
1813
- const headers = {
1814
- "Content-Type": "application/json",
1815
- "anthropic-version": "2023-06-01"
1816
- };
1817
- if (auth.type === "max") {
1818
- headers["Authorization"] = `Bearer ${auth.value}`;
1819
- } else {
1820
- headers["x-api-key"] = auth.value;
1821
- }
1822
- if (betaHeaders) {
1823
- headers["anthropic-beta"] = betaHeaders;
1824
- }
1825
- const response = await fetch("https://api.anthropic.com/v1/messages", {
1826
- method: "POST",
1827
- headers,
1828
- body: JSON.stringify(anthropicBody)
1829
- });
1830
- return response;
1831
- }
1832
- async function forwardToAnthropicStream(request, targetModel, auth, betaHeaders) {
1833
- const anthropicBody = buildAnthropicBody(request, targetModel, true);
1834
- const headers = {
1835
- "Content-Type": "application/json",
1836
- "anthropic-version": "2023-06-01"
1837
- };
1838
- if (auth.type === "max") {
1839
- headers["Authorization"] = `Bearer ${auth.value}`;
1840
- } else {
1841
- headers["x-api-key"] = auth.value;
1842
- }
1843
- if (betaHeaders) {
1844
- headers["anthropic-beta"] = betaHeaders;
1845
- }
1846
- const response = await fetch("https://api.anthropic.com/v1/messages", {
1847
- method: "POST",
1848
- headers,
1849
- body: JSON.stringify(anthropicBody)
1850
- });
1851
- return response;
1852
- }
1853
- function convertMessagesToAnthropic(messages) {
1854
- const result = [];
1855
- for (const msg of messages) {
1856
- const m = msg;
1857
- if (m.role === "system") continue;
1858
- if (m.role === "tool") {
1859
- result.push({
1860
- role: "user",
1861
- content: [
1862
- {
1863
- type: "tool_result",
1864
- tool_use_id: m.tool_call_id,
1865
- content: typeof m.content === "string" ? m.content : JSON.stringify(m.content)
1866
- }
1867
- ]
1868
- });
1869
- continue;
1870
- }
1871
- if (m.role === "assistant" && m.tool_calls && m.tool_calls.length > 0) {
1872
- const content = [];
1873
- if (m.content && typeof m.content === "string") {
1874
- content.push({ type: "text", text: m.content });
1875
- }
1876
- for (const tc of m.tool_calls) {
1877
- content.push({
1878
- type: "tool_use",
1879
- id: tc.id,
1880
- name: tc.function.name,
1881
- input: JSON.parse(tc.function.arguments || "{}")
1882
- });
1883
- }
1884
- result.push({ role: "assistant", content });
1885
- continue;
1886
- }
1887
- result.push({
1888
- role: m.role === "assistant" ? "assistant" : "user",
1889
- content: m.content
1890
- });
1891
- }
1892
- return result;
1893
- }
1894
- function buildAnthropicBody(request, targetModel, stream) {
1895
- const anthropicMessages = convertMessagesToAnthropic(request.messages);
1896
- const systemMessage = request.messages.find((m) => m.role === "system");
1897
- const anthropicBody = {
1898
- model: targetModel,
1899
- messages: anthropicMessages,
1900
- max_tokens: request.max_tokens ?? 4096,
1901
- stream
1902
- };
1903
- if (systemMessage) {
1904
- anthropicBody["system"] = systemMessage.content;
1905
- }
1906
- if (request.temperature !== void 0) {
1907
- anthropicBody["temperature"] = request.temperature;
1908
- }
1909
- if (request.tools && Array.isArray(request.tools)) {
1910
- anthropicBody["tools"] = convertToolsToAnthropic(request.tools);
1911
- }
1912
- if (request.tool_choice) {
1913
- anthropicBody["tool_choice"] = convertToolChoiceToAnthropic(request.tool_choice);
1914
- }
1915
- return anthropicBody;
1916
- }
1917
- function convertToolsToAnthropic(tools) {
1918
- return tools.map((tool) => {
1919
- const t = tool;
1920
- if (t.type === "function" && t.function) {
1921
- return {
1922
- name: t.function.name,
1923
- description: t.function.description,
1924
- input_schema: t.function.parameters || { type: "object", properties: {} }
1925
- };
1926
- }
1927
- return tool;
1928
- });
1929
- }
1930
- function convertToolChoiceToAnthropic(toolChoice) {
1931
- if (toolChoice === "auto") return { type: "auto" };
1932
- if (toolChoice === "none") return { type: "none" };
1933
- if (toolChoice === "required") return { type: "any" };
1934
- const tc = toolChoice;
1935
- if (tc.type === "function" && tc.function?.name) {
1936
- return { type: "tool", name: tc.function.name };
1937
- }
1938
- return toolChoice;
1939
- }
1940
- async function forwardToOpenAI(request, targetModel, apiKey) {
1941
- const openaiBody = {
1942
- ...request,
1943
- model: targetModel,
1944
- stream: false
1945
- };
1946
- const response = await fetch("https://api.openai.com/v1/chat/completions", {
1947
- method: "POST",
1948
- headers: {
1949
- "Content-Type": "application/json",
1950
- Authorization: `Bearer ${apiKey}`
1951
- },
1952
- body: JSON.stringify(openaiBody)
1953
- });
1954
- return response;
1955
- }
1956
- async function forwardToOpenAIStream(request, targetModel, apiKey) {
1957
- const openaiBody = {
1958
- ...request,
1959
- model: targetModel,
1960
- stream: true
1961
- };
1962
- const response = await fetch("https://api.openai.com/v1/chat/completions", {
1963
- method: "POST",
1964
- headers: {
1965
- "Content-Type": "application/json",
1966
- Authorization: `Bearer ${apiKey}`
1967
- },
1968
- body: JSON.stringify(openaiBody)
1969
- });
1970
- return response;
1971
- }
1972
- async function forwardToXAI(request, targetModel, apiKey) {
1973
- const xaiBody = {
1974
- ...request,
1975
- model: targetModel,
1976
- stream: false
1977
- };
1978
- const response = await fetch("https://api.x.ai/v1/chat/completions", {
1979
- method: "POST",
1980
- headers: {
1981
- "Content-Type": "application/json",
1982
- Authorization: `Bearer ${apiKey}`
1983
- },
1984
- body: JSON.stringify(xaiBody)
1985
- });
1986
- return response;
1987
- }
1988
- async function forwardToXAIStream(request, targetModel, apiKey) {
1989
- const xaiBody = {
1990
- ...request,
1991
- model: targetModel,
1992
- stream: true
1993
- };
1994
- const response = await fetch("https://api.x.ai/v1/chat/completions", {
1995
- method: "POST",
1996
- headers: {
1997
- "Content-Type": "application/json",
1998
- Authorization: `Bearer ${apiKey}`
1999
- },
2000
- body: JSON.stringify(xaiBody)
2001
- });
2002
- return response;
2003
- }
2004
- async function forwardToMoonshot(request, targetModel, apiKey) {
2005
- const moonshotBody = {
2006
- ...request,
2007
- model: targetModel,
2008
- stream: false
2009
- };
2010
- const response = await fetch("https://api.moonshot.cn/v1/chat/completions", {
2011
- method: "POST",
2012
- headers: {
2013
- "Content-Type": "application/json",
2014
- Authorization: `Bearer ${apiKey}`
2015
- },
2016
- body: JSON.stringify(moonshotBody)
2017
- });
2018
- return response;
2019
- }
2020
- async function forwardToMoonshotStream(request, targetModel, apiKey) {
2021
- const moonshotBody = {
2022
- ...request,
2023
- model: targetModel,
2024
- stream: true
2025
- };
2026
- const response = await fetch("https://api.moonshot.cn/v1/chat/completions", {
2027
- method: "POST",
2028
- headers: {
2029
- "Content-Type": "application/json",
2030
- Authorization: `Bearer ${apiKey}`
2031
- },
2032
- body: JSON.stringify(moonshotBody)
2033
- });
2034
- return response;
2035
- }
2036
- function convertMessagesToGemini(messages) {
2037
- const geminiContents = [];
2038
- for (const msg of messages) {
2039
- if (msg.role === "system") continue;
2040
- const role = msg.role === "assistant" ? "model" : "user";
2041
- if (typeof msg.content === "string") {
2042
- geminiContents.push({
2043
- role,
2044
- parts: [{ text: msg.content }]
2045
- });
2046
- } else if (Array.isArray(msg.content)) {
2047
- const parts = msg.content.map((part) => {
2048
- const p = part;
2049
- if (p.type === "text") {
2050
- return { text: p.text };
2051
- }
2052
- if (p.type === "image_url" && p.image_url?.url) {
2053
- const url2 = p.image_url.url;
2054
- if (url2.startsWith("data:")) {
2055
- const match = url2.match(/^data:([^;]+);base64,(.+)$/);
2056
- if (match) {
2057
- return {
2058
- inline_data: {
2059
- mime_type: match[1],
2060
- data: match[2]
2061
- }
2062
- };
2063
- }
2064
- }
2065
- return { text: `[Image: ${url2}]` };
2066
- }
2067
- return { text: "" };
2068
- });
2069
- geminiContents.push({ role, parts });
2070
- }
2071
- }
2072
- return geminiContents;
2073
- }
2074
- async function forwardToGemini(request, targetModel, apiKey) {
2075
- const systemMessage = request.messages.find((m) => m.role === "system");
2076
- const geminiContents = convertMessagesToGemini(request.messages);
2077
- const geminiBody = {
2078
- contents: geminiContents,
2079
- generationConfig: {
2080
- maxOutputTokens: request.max_tokens ?? 4096
2081
- }
2082
- };
2083
- if (request.temperature !== void 0) {
2084
- geminiBody["generationConfig"]["temperature"] = request.temperature;
2085
- }
2086
- if (systemMessage && typeof systemMessage.content === "string") {
2087
- geminiBody["systemInstruction"] = {
2088
- parts: [{ text: systemMessage.content }]
2089
- };
2090
- }
2091
- const response = await fetch(
2092
- `https://generativelanguage.googleapis.com/v1beta/models/${targetModel}:generateContent?key=${apiKey}`,
2093
- {
2094
- method: "POST",
2095
- headers: {
2096
- "Content-Type": "application/json"
2097
- },
2098
- body: JSON.stringify(geminiBody)
2099
- }
2100
- );
2101
- return response;
2102
- }
2103
- async function forwardToGeminiStream(request, targetModel, apiKey) {
2104
- const systemMessage = request.messages.find((m) => m.role === "system");
2105
- const geminiContents = convertMessagesToGemini(request.messages);
2106
- const geminiBody = {
2107
- contents: geminiContents,
2108
- generationConfig: {
2109
- maxOutputTokens: request.max_tokens ?? 4096
2110
- }
2111
- };
2112
- if (request.temperature !== void 0) {
2113
- geminiBody["generationConfig"]["temperature"] = request.temperature;
2114
- }
2115
- if (systemMessage && typeof systemMessage.content === "string") {
2116
- geminiBody["systemInstruction"] = {
2117
- parts: [{ text: systemMessage.content }]
2118
- };
2119
- }
2120
- const response = await fetch(
2121
- `https://generativelanguage.googleapis.com/v1beta/models/${targetModel}:streamGenerateContent?alt=sse&key=${apiKey}`,
2122
- {
2123
- method: "POST",
2124
- headers: {
2125
- "Content-Type": "application/json"
2126
- },
2127
- body: JSON.stringify(geminiBody)
2128
- }
2129
- );
2130
- return response;
2131
- }
2132
- function convertGeminiResponse(geminiData, model) {
2133
- const candidate = geminiData.candidates?.[0];
2134
- const text = candidate?.content?.parts?.map((p) => p.text ?? "").join("") ?? "";
2135
- let finishReason = "stop";
2136
- if (candidate?.finishReason === "MAX_TOKENS") {
2137
- finishReason = "length";
2138
- } else if (candidate?.finishReason === "SAFETY") {
2139
- finishReason = "content_filter";
2140
- }
2141
- return {
2142
- id: `chatcmpl-${Date.now()}`,
2143
- object: "chat.completion",
2144
- created: Math.floor(Date.now() / 1e3),
2145
- model,
2146
- choices: [
2147
- {
2148
- index: 0,
2149
- message: {
2150
- role: "assistant",
2151
- content: text
2152
- },
2153
- finish_reason: finishReason
2154
- }
2155
- ],
2156
- usage: {
2157
- prompt_tokens: geminiData.usageMetadata?.promptTokenCount ?? 0,
2158
- completion_tokens: geminiData.usageMetadata?.candidatesTokenCount ?? 0,
2159
- total_tokens: (geminiData.usageMetadata?.promptTokenCount ?? 0) + (geminiData.usageMetadata?.candidatesTokenCount ?? 0)
2160
- }
2161
- };
2162
- }
2163
- function convertGeminiStreamEvent(eventData, messageId, model, isFirst) {
2164
- const candidate = eventData.candidates?.[0];
2165
- const text = candidate?.content?.parts?.map((p) => p.text ?? "").join("") ?? "";
2166
- const choice = {
2167
- index: 0,
2168
- delta: {},
2169
- finish_reason: null
2170
- };
2171
- if (isFirst) {
2172
- choice["delta"] = { role: "assistant", content: text };
2173
- } else if (text) {
2174
- choice["delta"] = { content: text };
2175
- }
2176
- if (candidate?.finishReason) {
2177
- let finishReason = "stop";
2178
- if (candidate.finishReason === "MAX_TOKENS") {
2179
- finishReason = "length";
2180
- } else if (candidate.finishReason === "SAFETY") {
2181
- finishReason = "content_filter";
2182
- }
2183
- choice["finish_reason"] = finishReason;
2184
- }
2185
- const chunk = {
2186
- id: messageId,
2187
- object: "chat.completion.chunk",
2188
- created: Math.floor(Date.now() / 1e3),
2189
- model,
2190
- choices: [choice]
2191
- };
2192
- return `data: ${JSON.stringify(chunk)}
2193
-
2194
- `;
2195
- }
2196
- async function* convertGeminiStream(response, model) {
2197
- const reader = response.body?.getReader();
2198
- if (!reader) {
2199
- throw new Error("No response body");
2200
- }
2201
- const decoder = new TextDecoder();
2202
- let buffer = "";
2203
- const messageId = `chatcmpl-${Date.now()}`;
2204
- let isFirst = true;
2205
- try {
2206
- while (true) {
2207
- const { done, value } = await reader.read();
2208
- if (done) break;
2209
- buffer += decoder.decode(value, { stream: true });
2210
- const lines = buffer.split("\n");
2211
- buffer = lines.pop() || "";
2212
- for (const line of lines) {
2213
- if (line.startsWith("data: ")) {
2214
- const jsonStr = line.slice(6);
2215
- if (jsonStr.trim() === "[DONE]") {
2216
- yield "data: [DONE]\n\n";
2217
- continue;
2218
- }
2219
- try {
2220
- const parsed = JSON.parse(jsonStr);
2221
- const converted = convertGeminiStreamEvent(parsed, messageId, model, isFirst);
2222
- if (converted) {
2223
- yield converted;
2224
- isFirst = false;
2225
- }
2226
- } catch {
2227
- }
2228
- }
2229
- }
2230
- }
2231
- yield "data: [DONE]\n\n";
2232
- } finally {
2233
- reader.releaseLock();
2234
- }
2235
- }
2236
- function convertAnthropicResponse(anthropicData) {
2237
- const textBlocks = anthropicData.content?.filter((c) => c.type === "text") ?? [];
2238
- const toolBlocks = anthropicData.content?.filter((c) => c.type === "tool_use") ?? [];
2239
- const textContent = textBlocks.map((c) => c.text ?? "").join("");
2240
- const message = {
2241
- role: "assistant",
2242
- content: textContent || null
2243
- };
2244
- if (toolBlocks.length > 0) {
2245
- message["tool_calls"] = toolBlocks.map((block) => ({
2246
- id: block.id || `call_${Date.now()}`,
2247
- type: "function",
2248
- function: {
2249
- name: block.name,
2250
- arguments: typeof block.input === "string" ? block.input : JSON.stringify(block.input ?? {})
2251
- }
2252
- }));
2253
- }
2254
- let finishReason = "stop";
2255
- if (anthropicData.stop_reason === "tool_use") {
2256
- finishReason = "tool_calls";
2257
- } else if (anthropicData.stop_reason === "end_turn") {
2258
- finishReason = "stop";
2259
- } else if (anthropicData.stop_reason) {
2260
- finishReason = anthropicData.stop_reason;
2261
- }
2262
- return {
2263
- id: anthropicData.id || `chatcmpl-${Date.now()}`,
2264
- object: "chat.completion",
2265
- created: Math.floor(Date.now() / 1e3),
2266
- model: anthropicData.model,
2267
- choices: [
2268
- {
2269
- index: 0,
2270
- message,
2271
- finish_reason: finishReason
2272
- }
2273
- ],
2274
- usage: {
2275
- prompt_tokens: anthropicData.usage?.input_tokens ?? 0,
2276
- completion_tokens: anthropicData.usage?.output_tokens ?? 0,
2277
- total_tokens: (anthropicData.usage?.input_tokens ?? 0) + (anthropicData.usage?.output_tokens ?? 0)
2278
- }
2279
- };
2280
- }
2281
- function convertAnthropicStreamEvent(eventType, eventData, messageId, model, toolState) {
2282
- const choice = { index: 0, delta: {}, finish_reason: null };
2283
- const baseChunk = {
2284
- id: messageId,
2285
- object: "chat.completion.chunk",
2286
- created: Math.floor(Date.now() / 1e3),
2287
- model,
2288
- choices: [choice]
2289
- };
2290
- switch (eventType) {
2291
- case "message_start": {
2292
- const msg = eventData["message"];
2293
- baseChunk.id = msg?.["id"] || messageId;
2294
- choice.delta = { role: "assistant", content: "" };
2295
- return `data: ${JSON.stringify(baseChunk)}
2296
-
2297
- `;
2298
- }
2299
- case "content_block_start": {
2300
- const contentBlock = eventData["content_block"];
2301
- const blockIndex = eventData["index"];
2302
- if (contentBlock?.["type"] === "tool_use") {
2303
- const toolId = contentBlock["id"];
2304
- const toolName = contentBlock["name"];
2305
- toolState.tools.set(blockIndex ?? toolState.currentToolIndex, {
2306
- id: toolId,
2307
- name: toolName,
2308
- arguments: ""
2309
- });
2310
- toolState.currentToolIndex = blockIndex ?? toolState.currentToolIndex;
2311
- choice.delta = {
2312
- tool_calls: [{
2313
- index: blockIndex ?? 0,
2314
- id: toolId,
2315
- type: "function",
2316
- function: { name: toolName, arguments: "" }
2317
- }]
2318
- };
2319
- return `data: ${JSON.stringify(baseChunk)}
2320
-
2321
- `;
2322
- }
2323
- return null;
2324
- }
2325
- case "content_block_delta": {
2326
- const delta = eventData["delta"];
2327
- const blockIndex = eventData["index"];
2328
- if (delta?.["type"] === "text_delta") {
2329
- choice.delta = { content: delta["text"] };
2330
- return `data: ${JSON.stringify(baseChunk)}
2331
-
2332
- `;
2333
- }
2334
- if (delta?.["type"] === "input_json_delta") {
2335
- const partialJson = delta["partial_json"] || "";
2336
- const tool = toolState.tools.get(blockIndex ?? toolState.currentToolIndex);
2337
- if (tool) {
2338
- tool.arguments += partialJson;
2339
- }
2340
- choice.delta = {
2341
- tool_calls: [{
2342
- index: blockIndex ?? 0,
2343
- function: { arguments: partialJson }
2344
- }]
2345
- };
2346
- return `data: ${JSON.stringify(baseChunk)}
2347
-
2348
- `;
2349
- }
2350
- return null;
2351
- }
2352
- case "message_delta": {
2353
- const delta = eventData["delta"];
2354
- const stopReason = delta?.["stop_reason"];
2355
- if (stopReason === "tool_use") {
2356
- choice.finish_reason = "tool_calls";
2357
- } else if (stopReason === "end_turn") {
2358
- choice.finish_reason = "stop";
2359
- } else {
2360
- choice.finish_reason = stopReason || "stop";
2361
- }
2362
- choice.delta = {};
2363
- return `data: ${JSON.stringify(baseChunk)}
2364
-
2365
- `;
2366
- }
2367
- case "message_stop": {
2368
- return "data: [DONE]\n\n";
2369
- }
2370
- default:
2371
- return null;
2372
- }
2373
- }
2374
- var lastStreamingUsage = null;
2375
- async function* convertAnthropicStream(response, model) {
2376
- const reader = response.body?.getReader();
2377
- if (!reader) {
2378
- throw new Error("No response body");
2379
- }
2380
- const decoder = new TextDecoder();
2381
- let buffer = "";
2382
- let messageId = `chatcmpl-${Date.now()}`;
2383
- const toolState = {
2384
- currentToolIndex: 0,
2385
- tools: /* @__PURE__ */ new Map()
2386
- };
2387
- let streamInputTokens = 0;
2388
- let streamOutputTokens = 0;
2389
- try {
2390
- while (true) {
2391
- const { done, value } = await reader.read();
2392
- if (done) break;
2393
- buffer += decoder.decode(value, { stream: true });
2394
- const lines = buffer.split("\n");
2395
- buffer = lines.pop() || "";
2396
- let eventType = "";
2397
- let eventData = "";
2398
- for (const line of lines) {
2399
- if (line.startsWith("event: ")) {
2400
- eventType = line.slice(7).trim();
2401
- } else if (line.startsWith("data: ")) {
2402
- eventData = line.slice(6);
2403
- } else if (line === "" && eventType && eventData) {
2404
- try {
2405
- const parsed = JSON.parse(eventData);
2406
- if (eventType === "message_start") {
2407
- const msg = parsed["message"];
2408
- if (msg?.usage?.input_tokens) {
2409
- streamInputTokens = msg.usage.input_tokens;
2410
- }
2411
- } else if (eventType === "message_delta") {
2412
- const usage = parsed["usage"];
2413
- if (usage?.output_tokens) {
2414
- streamOutputTokens = usage.output_tokens;
2415
- }
2416
- }
2417
- const converted = convertAnthropicStreamEvent(eventType, parsed, messageId, model, toolState);
2418
- if (converted) {
2419
- yield converted;
2420
- }
2421
- } catch {
2422
- }
2423
- eventType = "";
2424
- eventData = "";
2425
- }
2426
- }
2427
- }
2428
- lastStreamingUsage = { inputTokens: streamInputTokens, outputTokens: streamOutputTokens };
2429
- } finally {
2430
- reader.releaseLock();
2431
- }
2432
- }
2433
- async function* pipeOpenAIStream(response) {
2434
- const reader = response.body?.getReader();
2435
- if (!reader) {
2436
- throw new Error("No response body");
2437
- }
2438
- const decoder = new TextDecoder();
2439
- try {
2440
- while (true) {
2441
- const { done, value } = await reader.read();
2442
- if (done) break;
2443
- yield decoder.decode(value, { stream: true });
2444
- }
2445
- } finally {
2446
- reader.releaseLock();
2447
- }
2448
- }
2449
- function parsePreferredModel(preferredModel) {
2450
- const [provider, model] = preferredModel.split(":");
2451
- if (!provider || !model) return null;
2452
- const validProviders = ["openai", "anthropic", "google", "xai", "moonshot", "local"];
2453
- if (!validProviders.includes(provider)) return null;
2454
- return { provider, model };
2455
- }
2456
- function resolveExplicitModel(modelName) {
2457
- if (MODEL_MAPPING[modelName]) {
2458
- return MODEL_MAPPING[modelName];
2459
- }
2460
- if (modelName.startsWith("claude-")) {
2461
- return { provider: "anthropic", model: modelName };
2462
- }
2463
- if (modelName.startsWith("gpt-") || modelName.startsWith("o1-") || modelName.startsWith("o3-") || modelName.startsWith("chatgpt-") || modelName.startsWith("text-") || modelName.startsWith("dall-e") || modelName.startsWith("whisper") || modelName.startsWith("tts-")) {
2464
- return { provider: "openai", model: modelName };
2465
- }
2466
- if (modelName.startsWith("gemini-") || modelName.startsWith("palm-")) {
2467
- return { provider: "google", model: modelName };
2468
- }
2469
- if (modelName.startsWith("grok-")) {
2470
- return { provider: "xai", model: modelName };
2471
- }
2472
- if (modelName.startsWith("moonshot-")) {
2473
- return { provider: "moonshot", model: modelName };
2474
- }
2475
- if (modelName.includes("/")) {
2476
- const [provider, model] = modelName.split("/");
2477
- const validProviders = ["openai", "anthropic", "google", "xai", "moonshot", "local"];
2478
- if (provider && model && validProviders.includes(provider)) {
2479
- return { provider, model };
2480
- }
2481
- }
2482
- return null;
2483
- }
2484
- async function startProxy(config = {}) {
2485
- const port = config.port ?? 3001;
2486
- const host = config.host ?? "127.0.0.1";
2487
- const verbose = config.verbose ?? false;
2488
- const relay = new RelayPlane({ dbPath: config.dbPath });
2489
- const log = (msg) => {
2490
- if (verbose) console.log(`[relayplane] ${msg}`);
2491
- };
2492
- const server = http.createServer(async (req, res) => {
2493
- res.setHeader("Access-Control-Allow-Origin", "*");
2494
- res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
2495
- res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
2496
- if (req.method === "OPTIONS") {
2497
- res.writeHead(204);
2498
- res.end();
2499
- return;
2500
- }
2501
- const parsedUrl = url.parse(req.url || "", true);
2502
- const pathname = parsedUrl.pathname || "";
2503
- if (req.method === "GET" && pathname === "/health") {
2504
- const uptimeMs = Date.now() - serverStartTime;
2505
- const uptimeSecs = Math.floor(uptimeMs / 1e3);
2506
- const hours = Math.floor(uptimeSecs / 3600);
2507
- const mins = Math.floor(uptimeSecs % 3600 / 60);
2508
- const secs = uptimeSecs % 60;
2509
- const providers = {};
2510
- for (const [name, config2] of Object.entries(DEFAULT_ENDPOINTS)) {
2511
- providers[name] = !!process.env[config2.apiKeyEnv];
2512
- }
2513
- res.writeHead(200, { "Content-Type": "application/json" });
2514
- res.end(JSON.stringify({
2515
- status: "ok",
2516
- version: VERSION,
2517
- uptime: `${hours}h ${mins}m ${secs}s`,
2518
- uptimeMs,
2519
- providers,
2520
- totalRuns: recentRuns.length > 0 ? Object.values(modelCounts).reduce((a, b) => a + b, 0) : 0
2521
- }));
2522
- return;
2523
- }
2524
- if (req.method === "GET" && pathname === "/stats") {
2525
- const stats = relay.stats();
2526
- const costs = calculateCosts();
2527
- const totalRuns = Object.values(modelCounts).reduce((a, b) => a + b, 0);
2528
- const modelDistribution = {};
2529
- for (const [model, count] of Object.entries(modelCounts)) {
2530
- const modelName = model.split("/")[1] || model;
2531
- const tokenData = costs.byModel[modelName];
2532
- modelDistribution[model] = {
2533
- count,
2534
- percentage: totalRuns > 0 ? (count / totalRuns * 100).toFixed(1) + "%" : "0%",
2535
- tokens: tokenData ? { input: tokenData.inputTokens, output: tokenData.outputTokens } : void 0,
2536
- costUsd: tokenData?.costUsd
2537
- };
2538
- }
2539
- res.writeHead(200, { "Content-Type": "application/json" });
2540
- res.end(JSON.stringify({
2541
- totalRuns,
2542
- tokens: {
2543
- input: costs.totalInputTokens,
2544
- output: costs.totalOutputTokens,
2545
- total: costs.totalInputTokens + costs.totalOutputTokens
2546
- },
2547
- costs: {
2548
- actualUsd: costs.actualCostUsd,
2549
- opusBaselineUsd: costs.opusCostUsd,
2550
- savingsUsd: costs.savingsUsd,
2551
- savingsPercent: costs.savingsPercent
2552
- },
2553
- modelDistribution,
2554
- byTaskType: stats.byTaskType,
2555
- period: stats.period
2556
- }));
2557
- return;
2558
- }
2559
- if (req.method === "GET" && pathname === "/runs") {
2560
- const limitParam = parsedUrl.query["limit"];
2561
- const parsedLimit = limitParam ? parseInt(String(limitParam), 10) : 20;
2562
- const limit = Math.min(Number.isNaN(parsedLimit) ? 20 : parsedLimit, MAX_RECENT_RUNS);
2563
- res.writeHead(200, { "Content-Type": "application/json" });
2564
- res.end(JSON.stringify({
2565
- runs: recentRuns.slice(0, limit),
2566
- total: recentRuns.length
2567
- }));
2568
- return;
2569
- }
2570
- if (req.method === "GET" && pathname.includes("/models")) {
2571
- res.writeHead(200, { "Content-Type": "application/json" });
2572
- res.end(
2573
- JSON.stringify({
2574
- object: "list",
2575
- data: [
2576
- { id: "relayplane:auto", object: "model", owned_by: "relayplane" },
2577
- { id: "relayplane:cost", object: "model", owned_by: "relayplane" },
2578
- { id: "relayplane:quality", object: "model", owned_by: "relayplane" }
2579
- ]
2580
- })
2581
- );
2582
- return;
2583
- }
2584
- if (req.method !== "POST" || !pathname.includes("/chat/completions")) {
2585
- res.writeHead(404, { "Content-Type": "application/json" });
2586
- res.end(JSON.stringify({ error: "Not found" }));
2587
- return;
2588
- }
2589
- let body = "";
2590
- for await (const chunk of req) {
2591
- body += chunk;
2592
- }
2593
- let request;
2594
- try {
2595
- request = JSON.parse(body);
2596
- } catch {
2597
- res.writeHead(400, { "Content-Type": "application/json" });
2598
- res.end(JSON.stringify({ error: "Invalid JSON" }));
2599
- return;
2600
- }
2601
- const isStreaming = request.stream === true;
2602
- const requestedModel = request.model;
2603
- let routingMode = "auto";
2604
- let targetModel = "";
2605
- let targetProvider = "anthropic";
2606
- if (requestedModel.startsWith("relayplane:")) {
2607
- if (requestedModel.includes(":cost")) {
2608
- routingMode = "cost";
2609
- } else if (requestedModel.includes(":quality")) {
2610
- routingMode = "quality";
2611
- }
2612
- } else {
2613
- routingMode = "passthrough";
2614
- const resolved = resolveExplicitModel(requestedModel);
2615
- if (resolved) {
2616
- targetProvider = resolved.provider;
2617
- targetModel = resolved.model;
2618
- log(`Pass-through mode: ${requestedModel} \u2192 ${targetProvider}/${targetModel}`);
2619
- } else {
2620
- res.writeHead(400, { "Content-Type": "application/json" });
2621
- res.end(JSON.stringify({ error: `Unknown model: ${requestedModel}` }));
2622
- return;
2623
- }
2624
- }
2625
- log(`Received request for model: ${requestedModel} (mode: ${routingMode}, stream: ${isStreaming})`);
2626
- const promptText = extractPromptText(request.messages);
2627
- const taskType = inferTaskType(promptText);
2628
- const confidence = getInferenceConfidence(promptText, taskType);
2629
- log(`Inferred task: ${taskType} (confidence: ${confidence.toFixed(2)})`);
2630
- if (routingMode !== "passthrough") {
2631
- const configStrategy = getStrategy(currentConfig, taskType);
2632
- if (configStrategy) {
2633
- const parsed = parsePreferredModel(configStrategy.model);
2634
- if (parsed) {
2635
- targetProvider = parsed.provider;
2636
- targetModel = parsed.model;
2637
- log(`Using config strategy: ${configStrategy.model}`);
2638
- }
2639
- }
2640
- if (!configStrategy) {
2641
- const rule = relay.routing.get(taskType);
2642
- if (rule && rule.preferredModel) {
2643
- const parsed = parsePreferredModel(rule.preferredModel);
2644
- if (parsed) {
2645
- targetProvider = parsed.provider;
2646
- targetModel = parsed.model;
2647
- log(`Using learned rule: ${rule.preferredModel}`);
2648
- } else {
2649
- const defaultRoute = DEFAULT_ROUTING[taskType];
2650
- targetProvider = defaultRoute.provider;
2651
- targetModel = defaultRoute.model;
2652
- }
2653
- } else {
2654
- const defaultRoute = DEFAULT_ROUTING[taskType];
2655
- targetProvider = defaultRoute.provider;
2656
- targetModel = defaultRoute.model;
2657
- }
2658
- }
2659
- if (routingMode === "cost") {
2660
- const costModel = currentConfig.defaults?.costModel || "claude-3-5-haiku-latest";
2661
- targetModel = costModel;
2662
- targetProvider = "anthropic";
2663
- log(`Cost mode: using ${costModel}`);
2664
- } else if (routingMode === "quality") {
2665
- const qualityModel = currentConfig.defaults?.qualityModel || process.env["RELAYPLANE_QUALITY_MODEL"] || "claude-sonnet-4-20250514";
2666
- targetModel = qualityModel;
2667
- targetProvider = "anthropic";
2668
- log(`Quality mode: using ${qualityModel}`);
2669
- }
2670
- }
2671
- log(`Routing to: ${targetProvider}/${targetModel}`);
2672
- let apiKey;
2673
- let anthropicAuth = null;
2674
- if (targetProvider === "anthropic") {
2675
- anthropicAuth = getAnthropicAuth(currentConfig, targetModel);
2676
- if (!anthropicAuth) {
2677
- res.writeHead(500, { "Content-Type": "application/json" });
2678
- res.end(JSON.stringify({ error: "No Anthropic auth configured (set ANTHROPIC_API_KEY or config.auth.anthropicMaxToken)" }));
2679
- return;
2680
- }
2681
- log(`Using ${anthropicAuth.type === "max" ? "MAX token" : "API key"} auth for ${targetModel}`);
2682
- } else {
2683
- const apiKeyEnv = DEFAULT_ENDPOINTS[targetProvider]?.apiKeyEnv ?? `${targetProvider.toUpperCase()}_API_KEY`;
2684
- apiKey = process.env[apiKeyEnv];
2685
- if (!apiKey) {
2686
- res.writeHead(500, { "Content-Type": "application/json" });
2687
- res.end(JSON.stringify({ error: `Missing ${apiKeyEnv} environment variable` }));
2688
- return;
2689
- }
2690
- }
2691
- const startTime = Date.now();
2692
- const betaHeaders = req.headers["anthropic-beta"];
2693
- if (isStreaming) {
2694
- await handleStreamingRequest(
2695
- res,
2696
- request,
2697
- targetProvider,
2698
- targetModel,
2699
- apiKey,
2700
- anthropicAuth,
2701
- relay,
2702
- promptText,
2703
- taskType,
2704
- confidence,
2705
- routingMode,
2706
- startTime,
2707
- log,
2708
- betaHeaders
2709
- );
2710
- } else {
2711
- await handleNonStreamingRequest(
2712
- res,
2713
- request,
2714
- targetProvider,
2715
- targetModel,
2716
- apiKey,
2717
- anthropicAuth,
2718
- relay,
2719
- promptText,
2720
- taskType,
2721
- confidence,
2722
- routingMode,
2723
- startTime,
2724
- log,
2725
- betaHeaders
2726
- );
2727
- }
2728
- });
2729
- watchConfig((newConfig) => {
2730
- currentConfig = newConfig;
2731
- console.log("[relayplane] Config reloaded");
2732
- });
2733
- return new Promise((resolve, reject) => {
2734
- server.on("error", reject);
2735
- server.listen(port, host, () => {
2736
- serverStartTime = Date.now();
2737
- console.log(`RelayPlane proxy listening on http://${host}:${port}`);
2738
- console.log(` Models: relayplane:auto, relayplane:cost, relayplane:quality`);
2739
- console.log(` Endpoint: POST /v1/chat/completions`);
2740
- console.log(` Stats: GET /stats, /runs, /health`);
2741
- console.log(` Config: ~/.relayplane/config.json (hot-reload enabled)`);
2742
- console.log(` Streaming: \u2705 Enabled`);
2743
- resolve(server);
2744
- });
2745
- });
2746
- }
2747
- async function handleStreamingRequest(res, request, targetProvider, targetModel, apiKey, anthropicAuth, relay, promptText, taskType, confidence, routingMode, startTime, log, betaHeaders) {
2748
- let providerResponse;
2749
- try {
2750
- switch (targetProvider) {
2751
- case "anthropic":
2752
- if (!anthropicAuth) throw new Error("No Anthropic auth");
2753
- providerResponse = await forwardToAnthropicStream(request, targetModel, anthropicAuth, betaHeaders);
2754
- break;
2755
- case "google":
2756
- providerResponse = await forwardToGeminiStream(request, targetModel, apiKey);
2757
- break;
2758
- case "xai":
2759
- providerResponse = await forwardToXAIStream(request, targetModel, apiKey);
2760
- break;
2761
- case "moonshot":
2762
- providerResponse = await forwardToMoonshotStream(request, targetModel, apiKey);
2763
- break;
2764
- default:
2765
- providerResponse = await forwardToOpenAIStream(request, targetModel, apiKey);
2766
- }
2767
- if (!providerResponse.ok) {
2768
- const errorData = await providerResponse.json();
2769
- res.writeHead(providerResponse.status, { "Content-Type": "application/json" });
2770
- res.end(JSON.stringify(errorData));
2771
- return;
2772
- }
2773
- } catch (err) {
2774
- const errorMsg = err instanceof Error ? err.message : String(err);
2775
- res.writeHead(500, { "Content-Type": "application/json" });
2776
- res.end(JSON.stringify({ error: `Provider error: ${errorMsg}` }));
2777
- return;
2778
- }
2779
- res.writeHead(200, {
2780
- "Content-Type": "text/event-stream",
2781
- "Cache-Control": "no-cache",
2782
- "Connection": "keep-alive"
2783
- });
2784
- try {
2785
- switch (targetProvider) {
2786
- case "anthropic":
2787
- for await (const chunk of convertAnthropicStream(providerResponse, targetModel)) {
2788
- res.write(chunk);
2789
- }
2790
- break;
2791
- case "google":
2792
- for await (const chunk of convertGeminiStream(providerResponse, targetModel)) {
2793
- res.write(chunk);
2794
- }
2795
- break;
2796
- default:
2797
- for await (const chunk of pipeOpenAIStream(providerResponse)) {
2798
- res.write(chunk);
2799
- }
2800
- }
2801
- } catch (err) {
2802
- log(`Streaming error: ${err}`);
2803
- }
2804
- const durationMs = Date.now() - startTime;
2805
- const modelKey = `${targetProvider}/${targetModel}`;
2806
- modelCounts[modelKey] = (modelCounts[modelKey] || 0) + 1;
2807
- if (lastStreamingUsage && (lastStreamingUsage.inputTokens > 0 || lastStreamingUsage.outputTokens > 0)) {
2808
- trackTokens(targetModel, lastStreamingUsage.inputTokens, lastStreamingUsage.outputTokens);
2809
- log(`Tokens: ${lastStreamingUsage.inputTokens} in, ${lastStreamingUsage.outputTokens} out`);
2810
- lastStreamingUsage = null;
2811
- }
2812
- relay.run({
2813
- prompt: promptText.slice(0, 500),
2814
- taskType,
2815
- model: `${targetProvider}:${targetModel}`
2816
- }).then((runResult) => {
2817
- recentRuns.unshift({
2818
- runId: runResult.runId,
2819
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2820
- model: modelKey,
2821
- taskType,
2822
- confidence,
2823
- mode: routingMode,
2824
- durationMs,
2825
- promptPreview: promptText.slice(0, 100) + (promptText.length > 100 ? "..." : "")
2826
- });
2827
- if (recentRuns.length > MAX_RECENT_RUNS) {
2828
- recentRuns.pop();
2829
- }
2830
- log(`Completed streaming in ${durationMs}ms, runId: ${runResult.runId}`);
2831
- }).catch((err) => {
2832
- log(`Failed to record run: ${err}`);
2833
- });
2834
- res.end();
2835
- }
2836
- async function handleNonStreamingRequest(res, request, targetProvider, targetModel, apiKey, anthropicAuth, relay, promptText, taskType, confidence, routingMode, startTime, log, betaHeaders) {
2837
- let providerResponse;
2838
- let responseData;
2839
- try {
2840
- switch (targetProvider) {
2841
- case "anthropic": {
2842
- if (!anthropicAuth) throw new Error("No Anthropic auth");
2843
- providerResponse = await forwardToAnthropic(request, targetModel, anthropicAuth, betaHeaders);
2844
- const rawData = await providerResponse.json();
2845
- if (!providerResponse.ok) {
2846
- res.writeHead(providerResponse.status, { "Content-Type": "application/json" });
2847
- res.end(JSON.stringify(rawData));
2848
- return;
2849
- }
2850
- responseData = convertAnthropicResponse(rawData);
2851
- break;
2852
- }
2853
- case "google": {
2854
- providerResponse = await forwardToGemini(request, targetModel, apiKey);
2855
- const rawData = await providerResponse.json();
2856
- if (!providerResponse.ok) {
2857
- res.writeHead(providerResponse.status, { "Content-Type": "application/json" });
2858
- res.end(JSON.stringify(rawData));
2859
- return;
2860
- }
2861
- responseData = convertGeminiResponse(rawData, targetModel);
2862
- break;
2863
- }
2864
- case "xai": {
2865
- providerResponse = await forwardToXAI(request, targetModel, apiKey);
2866
- responseData = await providerResponse.json();
2867
- if (!providerResponse.ok) {
2868
- res.writeHead(providerResponse.status, { "Content-Type": "application/json" });
2869
- res.end(JSON.stringify(responseData));
2870
- return;
2871
- }
2872
- break;
2873
- }
2874
- case "moonshot": {
2875
- providerResponse = await forwardToMoonshot(request, targetModel, apiKey);
2876
- responseData = await providerResponse.json();
2877
- if (!providerResponse.ok) {
2878
- res.writeHead(providerResponse.status, { "Content-Type": "application/json" });
2879
- res.end(JSON.stringify(responseData));
2880
- return;
2881
- }
2882
- break;
2883
- }
2884
- default: {
2885
- providerResponse = await forwardToOpenAI(request, targetModel, apiKey);
2886
- responseData = await providerResponse.json();
2887
- if (!providerResponse.ok) {
2888
- res.writeHead(providerResponse.status, { "Content-Type": "application/json" });
2889
- res.end(JSON.stringify(responseData));
2890
- return;
2891
- }
2892
- }
2893
- }
2894
- } catch (err) {
2895
- const errorMsg = err instanceof Error ? err.message : String(err);
2896
- res.writeHead(500, { "Content-Type": "application/json" });
2897
- res.end(JSON.stringify({ error: `Provider error: ${errorMsg}` }));
2898
- return;
2899
- }
2900
- const durationMs = Date.now() - startTime;
2901
- const modelKey = `${targetProvider}/${targetModel}`;
2902
- modelCounts[modelKey] = (modelCounts[modelKey] || 0) + 1;
2903
- const usage = responseData["usage"];
2904
- if (usage?.prompt_tokens || usage?.completion_tokens) {
2905
- trackTokens(targetModel, usage.prompt_tokens ?? 0, usage.completion_tokens ?? 0);
2906
- log(`Tokens: ${usage.prompt_tokens ?? 0} in, ${usage.completion_tokens ?? 0} out`);
2907
- }
2908
- try {
2909
- const runResult = await relay.run({
2910
- prompt: promptText.slice(0, 500),
2911
- taskType,
2912
- model: `${targetProvider}:${targetModel}`
2913
- });
2914
- recentRuns.unshift({
2915
- runId: runResult.runId,
2916
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2917
- model: modelKey,
2918
- taskType,
2919
- confidence,
2920
- mode: routingMode,
2921
- durationMs,
2922
- promptPreview: promptText.slice(0, 100) + (promptText.length > 100 ? "..." : "")
2923
- });
2924
- if (recentRuns.length > MAX_RECENT_RUNS) {
2925
- recentRuns.pop();
2926
- }
2927
- responseData["_relayplane"] = {
2928
- runId: runResult.runId,
2929
- routedTo: modelKey,
2930
- taskType,
2931
- confidence,
2932
- durationMs,
2933
- mode: routingMode
2934
- };
2935
- log(`Completed in ${durationMs}ms, runId: ${runResult.runId}`);
2936
- } catch (err) {
2937
- log(`Failed to record run: ${err}`);
2938
- }
2939
- res.writeHead(200, { "Content-Type": "application/json" });
2940
- res.end(JSON.stringify(responseData));
2941
- }
2942
-
2943
- // src/cli.ts
2944
- import Database2 from "better-sqlite3";
2945
- function printHelp() {
2946
- console.log(`
2947
- RelayPlane Proxy - Intelligent AI Model Routing
2948
-
2949
- Usage:
2950
- npx @relayplane/proxy [command] [options]
2951
- relayplane-proxy [command] [options]
2952
-
2953
- Commands:
2954
- (default) Start the proxy server
2955
- stats Show routing statistics
2956
-
2957
- Server Options:
2958
- --port <number> Port to listen on (default: 3001)
2959
- --host <string> Host to bind to (default: 127.0.0.1)
2960
- -v, --verbose Enable verbose logging
2961
-
2962
- Stats Options:
2963
- --days <number> Days of history to show (default: 7)
2964
-
2965
- General:
2966
- -h, --help Show this help message
2967
-
2968
- Environment Variables:
2969
- ANTHROPIC_API_KEY Anthropic API key
2970
- OPENAI_API_KEY OpenAI API key
2971
- GEMINI_API_KEY Google Gemini API key (optional)
2972
- XAI_API_KEY xAI/Grok API key (optional)
2973
- MOONSHOT_API_KEY Moonshot API key (optional)
2974
-
2975
- Examples:
2976
- # Start proxy on default port
2977
- npx @relayplane/proxy
2978
-
2979
- # Start on custom port with verbose logging
2980
- npx @relayplane/proxy --port 8080 -v
2981
-
2982
- # View routing stats for last 7 days
2983
- npx @relayplane/proxy stats
2984
-
2985
- # View stats for last 30 days
2986
- npx @relayplane/proxy stats --days 30
2987
-
2988
- Learn more: https://relayplane.com/integrations/openclaw
2989
- `);
2990
- }
2991
- function showStats(days) {
2992
- const dbPath = getDefaultDbPath();
2993
- try {
2994
- const db = new Database2(dbPath, { readonly: true });
2995
- const cutoff = new Date(Date.now() - days * 24 * 60 * 60 * 1e3).toISOString();
2996
- const runs = db.prepare(`
2997
- SELECT
2998
- model,
2999
- task_type,
3000
- COUNT(*) as count,
3001
- SUM(tokens_in) as total_in,
3002
- SUM(tokens_out) as total_out,
3003
- AVG(duration_ms) as avg_duration,
3004
- SUM(CASE WHEN success = 1 THEN 1 ELSE 0 END) as successes
3005
- FROM runs
3006
- WHERE created_at >= ?
3007
- GROUP BY model
3008
- ORDER BY count DESC
3009
- `).all(cutoff);
3010
- const totalRuns = runs.reduce((sum, r) => sum + r.count, 0);
3011
- const totalTokensIn = runs.reduce((sum, r) => sum + (r.total_in || 0), 0);
3012
- const totalTokensOut = runs.reduce((sum, r) => sum + (r.total_out || 0), 0);
3013
- console.log("");
3014
- console.log(` \u256D\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256E`);
3015
- console.log(` \u2502 RelayPlane Routing Stats \u2502`);
3016
- console.log(` \u2502 Last ${String(days).padStart(2)} days \u2502`);
3017
- console.log(` \u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256F`);
3018
- console.log("");
3019
- if (totalRuns === 0) {
3020
- console.log(" No routing data found for this period.");
3021
- console.log(" Start using the proxy to collect stats!");
3022
- console.log("");
3023
- return;
3024
- }
3025
- console.log(" Summary:");
3026
- console.log(` Total requests: ${totalRuns.toLocaleString()}`);
3027
- console.log(` Total tokens in: ${totalTokensIn.toLocaleString()}`);
3028
- console.log(` Total tokens out: ${totalTokensOut.toLocaleString()}`);
3029
- console.log("");
3030
- console.log(" By Model:");
3031
- console.log(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500");
3032
- for (const row of runs) {
3033
- const pct = (row.count / totalRuns * 100).toFixed(1);
3034
- const successRate = row.count > 0 ? (row.successes / row.count * 100).toFixed(0) : "0";
3035
- console.log(` ${row.model.padEnd(35)} ${String(row.count).padStart(6)} (${pct.padStart(5)}%) ${successRate}% ok`);
3036
- }
3037
- console.log("");
3038
- const haikuRuns = runs.filter((r) => r.model.includes("haiku"));
3039
- const haikuTokensIn = haikuRuns.reduce((sum, r) => sum + (r.total_in || 0), 0);
3040
- const haikuTokensOut = haikuRuns.reduce((sum, r) => sum + (r.total_out || 0), 0);
3041
- const opusCost = totalTokensIn * 15 / 1e6 + totalTokensOut * 75 / 1e6;
3042
- const haikuCost = haikuTokensIn * 0.25 / 1e6 + haikuTokensOut * 1.25 / 1e6;
3043
- const nonHaikuCost = (totalTokensIn - haikuTokensIn) * 3 / 1e6 + (totalTokensOut - haikuTokensOut) * 15 / 1e6;
3044
- const actualCost = haikuCost + nonHaikuCost;
3045
- const savings = opusCost - actualCost;
3046
- if (savings > 0) {
3047
- console.log(" Estimated Savings:");
3048
- console.log(` If all Opus: $${opusCost.toFixed(2)}`);
3049
- console.log(` With routing: $${actualCost.toFixed(2)}`);
3050
- console.log(` Saved: $${savings.toFixed(2)} (${(savings / opusCost * 100).toFixed(0)}%)`);
3051
- console.log("");
3052
- }
3053
- db.close();
3054
- } catch (err) {
3055
- console.error("Error reading stats:", err);
3056
- console.log("");
3057
- console.log(" No data found. The proxy stores data at:");
3058
- console.log(` ${dbPath}`);
3059
- console.log("");
3060
- }
3061
- }
3062
- async function main() {
3063
- const args = process.argv.slice(2);
3064
- if (args.includes("-h") || args.includes("--help")) {
3065
- printHelp();
3066
- process.exit(0);
3067
- }
3068
- if (args[0] === "stats") {
3069
- let days = 7;
3070
- const daysIdx = args.indexOf("--days");
3071
- if (daysIdx !== -1 && args[daysIdx + 1]) {
3072
- days = parseInt(args[daysIdx + 1], 10) || 7;
3073
- }
3074
- showStats(days);
3075
- process.exit(0);
3076
- }
3077
- let port = 3001;
3078
- let host = "127.0.0.1";
3079
- let verbose = false;
3080
- for (let i = 0; i < args.length; i++) {
3081
- const arg = args[i];
3082
- if (arg === "--port" && args[i + 1]) {
3083
- port = parseInt(args[i + 1], 10);
3084
- if (isNaN(port) || port < 1 || port > 65535) {
3085
- console.error("Error: Invalid port number");
3086
- process.exit(1);
3087
- }
3088
- i++;
3089
- } else if (arg === "--host" && args[i + 1]) {
3090
- host = args[i + 1];
3091
- i++;
3092
- } else if (arg === "-v" || arg === "--verbose") {
3093
- verbose = true;
3094
- }
3095
- }
3096
- const hasAnthropicKey = !!process.env["ANTHROPIC_API_KEY"];
3097
- const hasOpenAIKey = !!process.env["OPENAI_API_KEY"];
3098
- const hasGeminiKey = !!process.env["GEMINI_API_KEY"];
3099
- const hasXAIKey = !!process.env["XAI_API_KEY"];
3100
- const hasMoonshotKey = !!process.env["MOONSHOT_API_KEY"];
3101
- if (!hasAnthropicKey && !hasOpenAIKey && !hasGeminiKey && !hasXAIKey && !hasMoonshotKey) {
3102
- console.error("Error: No API keys found. Set at least one of:");
3103
- console.error(" ANTHROPIC_API_KEY, OPENAI_API_KEY, GEMINI_API_KEY, XAI_API_KEY, MOONSHOT_API_KEY");
3104
- process.exit(1);
3105
- }
3106
- console.log("");
3107
- console.log(" \u256D\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256E");
3108
- console.log(" \u2502 RelayPlane Proxy v0.1.0 \u2502");
3109
- console.log(" \u2502 Intelligent AI Model Routing \u2502");
3110
- console.log(" \u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256F");
3111
- console.log("");
3112
- console.log(" Providers:");
3113
- if (hasAnthropicKey) console.log(" \u2713 Anthropic");
3114
- if (hasOpenAIKey) console.log(" \u2713 OpenAI");
3115
- if (hasGeminiKey) console.log(" \u2713 Google Gemini");
3116
- if (hasXAIKey) console.log(" \u2713 xAI (Grok)");
3117
- if (hasMoonshotKey) console.log(" \u2713 Moonshot");
3118
- console.log("");
3119
- try {
3120
- await startProxy({ port, host, verbose });
3121
- console.log("");
3122
- console.log(" To use, set these environment variables:");
3123
- console.log(` export ANTHROPIC_BASE_URL=http://${host}:${port}`);
3124
- console.log(` export OPENAI_BASE_URL=http://${host}:${port}`);
3125
- console.log("");
3126
- console.log(" Then run your agent (OpenClaw, Cursor, Aider, etc.)");
3127
- console.log("");
3128
- } catch (err) {
3129
- console.error("Failed to start proxy:", err);
3130
- process.exit(1);
3131
- }
3132
- }
3133
- main();
3134
- //# sourceMappingURL=cli.mjs.map