@relayplane/proxy 0.1.0

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