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