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