adaptive-memory-multi-model-router 2.14.40 → 2.14.42

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.
@@ -0,0 +1,280 @@
1
+ "use strict";
2
+ /**
3
+ * Enhanced Shapley Value Calculator for Ensemble Credit Assignment
4
+ *
5
+ * Incorporates game theory concepts for efficient and fair credit distribution:
6
+ *
7
+ * 1. ETHNOCENTRISM (In-group Loyalty Adjustment):
8
+ * - Players (models) have historical loyalty biases toward certain partners
9
+ * - Models that collaborate successfully develop "trust bonds"
10
+ * - Loyalty increases marginal contribution of trusted partners
11
+ * - Math: L[i,j] = exponential moving avg of historical success(i with j)
12
+ *
13
+ * 2. HANDICAP PRINCIPLE (Zahavi, 1975 - Costly Signaling):
14
+ * - Honest signals require costly investment
15
+ * - Models providing correct answers despite cost signal reliability
16
+ * - Math: H[i] = cost_i * reliability_i (handicap bonus)
17
+ *
18
+ * 3. CORE SHAPLEY VALUE:
19
+ * - φ_i = Σ_{S⊆N\{i}} (|S|! * (n-|S|-1)! / n!) * (v(S∪{i}) - v(S))
20
+ *
21
+ * Combined: φ_i* = α*Shapley_i + β*Loyalty_i + γ*Handicap_i
22
+ * Where α + β + γ = 1 (normalized weights)
23
+ */
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.HandicapCalculator = exports.LoyaltyMatrix = void 0;
26
+ exports.calculateEnhancedShapley = calculateEnhancedShapley;
27
+ exports.createAccuracyFn = createAccuracyFn;
28
+ exports.applyCredit = applyCredit;
29
+ exports.summarize = summarize;
30
+ const DEFAULT_LOYALTY = { decayRate: 0.9, minInteractions: 3 };
31
+ /**
32
+ * Loyalty Matrix L[i][j] = loyalty of model i toward model j (0-1)
33
+ * Models develop trust through successful collaborations
34
+ */
35
+ class LoyaltyMatrix {
36
+ matrix = new Map();
37
+ counts = new Map();
38
+ config;
39
+ constructor(cfg = {}) {
40
+ this.config = { ...DEFAULT_LOYALTY, ...cfg };
41
+ }
42
+ /** Record successful collaboration between two models */
43
+ recordSuccess(i, j, weight = 1) {
44
+ if (!this.matrix.has(i)) {
45
+ this.matrix.set(i, new Map());
46
+ this.counts.set(i, new Map());
47
+ }
48
+ if (!this.matrix.get(i).has(j)) {
49
+ this.matrix.get(i).set(j, 0);
50
+ this.counts.get(i).set(j, 0);
51
+ }
52
+ const prev = this.matrix.get(i).get(j);
53
+ const cnt = this.counts.get(i).get(j);
54
+ const ema = this.config.decayRate * prev + (1 - this.config.decayRate) * weight;
55
+ this.matrix.get(i).set(j, ema);
56
+ this.counts.get(i).set(j, cnt + 1);
57
+ }
58
+ /** Get loyalty of model i toward model j */
59
+ getLoyalty(i, j) {
60
+ if (!this.matrix.has(i) || !this.matrix.get(i).has(j))
61
+ return 0;
62
+ if (this.counts.get(i).get(j) < this.config.minInteractions)
63
+ return 0;
64
+ return this.matrix.get(i).get(j);
65
+ }
66
+ /** Ethnocentrism = average loyalty toward all partners */
67
+ ethnoCentrism(model, allModels) {
68
+ const loyalties = allModels.map(m => this.getLoyalty(model, m));
69
+ const avg = loyalties.reduce((a, b) => a + b, 0) / Math.max(1, allModels.length);
70
+ return Math.min(1, avg);
71
+ }
72
+ }
73
+ exports.LoyaltyMatrix = LoyaltyMatrix;
74
+ const DEFAULT_HANDICAP = { costSensitivity: 0.5, reliabilityWeight: 0.5, minCostThreshold: 0.0001 };
75
+ /**
76
+ * Handicap Principle (Zahavi, 1975):
77
+ * "A handicapping signal is honest because it is costly to produce"
78
+ *
79
+ * Models spending more tokens on answers despite being correct signal reliability.
80
+ * H[i] = cost_i * reliability_i (higher = more reliable despite cost)
81
+ */
82
+ class HandicapCalculator {
83
+ costs = new Map();
84
+ correct = new Map();
85
+ totals = new Map();
86
+ config;
87
+ constructor(cfg = {}) {
88
+ this.config = { ...DEFAULT_HANDICAP, ...cfg };
89
+ }
90
+ /** Record performance: cost spent and whether answer was correct */
91
+ record(model, cost, isCorrect) {
92
+ this.costs.set(model, (this.costs.get(model) || 0) + cost);
93
+ this.totals.set(model, (this.totals.get(model) || 0) + 1);
94
+ if (isCorrect)
95
+ this.correct.set(model, (this.correct.get(model) || 0) + 1);
96
+ }
97
+ /** Reliability = correct / total (prior probability of being right) */
98
+ reliability(model) {
99
+ const total = this.totals.get(model) || 0;
100
+ if (total === 0)
101
+ return 0.5; // Unknown = neutral
102
+ return (this.correct.get(model) || 0) / total;
103
+ }
104
+ /** Average cost invested by model */
105
+ avgCost(model) {
106
+ const total = this.totals.get(model) || 0;
107
+ if (total === 0)
108
+ return 0;
109
+ return (this.costs.get(model) || 0) / total;
110
+ }
111
+ /**
112
+ * Handicap bonus = honest signal of quality
113
+ * H[i] = (cost_i - minCost) / maxCost * reliability_i
114
+ * Higher cost investment + higher reliability = higher handicap
115
+ */
116
+ handicap(model, maxCost = 0.01) {
117
+ const cost = this.avgCost(model);
118
+ if (cost < this.config.minCostThreshold)
119
+ return 0;
120
+ const rel = this.reliability(model);
121
+ const costNorm = Math.min(1, cost / maxCost);
122
+ // Honest signal: cost * reliability (costly and correct = reliable)
123
+ return this.config.costSensitivity * costNorm * rel +
124
+ this.config.reliabilityWeight * rel;
125
+ }
126
+ }
127
+ exports.HandicapCalculator = HandicapCalculator;
128
+ // ============ CORE SHAPLEY VALUE ============
129
+ function factorial(n) {
130
+ if (n <= 1)
131
+ return 1;
132
+ return n * factorial(n - 1);
133
+ }
134
+ /**
135
+ * Exact Shapley calculation (factorial enumeration)
136
+ * For n <= 6 models
137
+ */
138
+ function exactShapley(modelIds, accuracyFn) {
139
+ const n = modelIds.length;
140
+ const shapley = new Map();
141
+ modelIds.forEach(m => shapley.set(m, 0));
142
+ // All permutations via Heap's algorithm
143
+ function permute(arr, callback) {
144
+ function generate(idx) {
145
+ if (idx === arr.length) {
146
+ callback([...arr]);
147
+ return;
148
+ }
149
+ for (let i = idx; i < arr.length; i++) {
150
+ [arr[idx], arr[i]] = [arr[i], arr[idx]];
151
+ generate(idx + 1);
152
+ [arr[idx], arr[i]] = [arr[i], arr[idx]];
153
+ }
154
+ }
155
+ generate(0);
156
+ }
157
+ const permutations = [];
158
+ const work = [...modelIds];
159
+ permute(work, p => permutations.push(p));
160
+ for (const perm of permutations) {
161
+ let acc = 0;
162
+ const subset = [];
163
+ for (let i = 0; i < perm.length; i++) {
164
+ const model = perm[i];
165
+ const newAcc = accuracyFn([...subset, model]);
166
+ const marginal = newAcc - acc;
167
+ const weight = factorial(subset.length) * factorial(n - subset.length - 1) / factorial(n);
168
+ shapley.set(model, shapley.get(model) + weight * marginal);
169
+ subset.push(model);
170
+ acc = newAcc;
171
+ }
172
+ }
173
+ return shapley;
174
+ }
175
+ /**
176
+ * Monte Carlo approximation for n > 6 models
177
+ */
178
+ function monteCarloShapley(modelIds, accuracyFn, nPerms) {
179
+ const n = modelIds.length;
180
+ const shapley = new Map();
181
+ modelIds.forEach(m => shapley.set(m, 0));
182
+ for (let iter = 0; iter < nPerms; iter++) {
183
+ const perm = [...modelIds].sort(() => Math.random() - 0.5);
184
+ let acc = 0;
185
+ const subset = [];
186
+ for (const model of perm) {
187
+ const newAcc = accuracyFn([...subset, model]);
188
+ const marginal = newAcc - acc;
189
+ shapley.set(model, shapley.get(model) + marginal);
190
+ subset.push(model);
191
+ acc = newAcc;
192
+ }
193
+ }
194
+ // Average
195
+ for (const [m, v] of shapley)
196
+ shapley.set(m, v / nPerms);
197
+ return shapley;
198
+ }
199
+ /**
200
+ * Combined enhanced Shapley value with ethnocentrism and handicap
201
+ * φ_i* = α*φ_i(Shapley) + β*ε_i(Ethnocentrism) + γ*H_i(Handicap)
202
+ */
203
+ function calculateEnhancedShapley(modelIds, accuracyFn, loyalty, handicap, cfg = {}) {
204
+ const { alpha = 0.5, beta = 0.3, gamma = 0.2, nPermutations = 1000, useMonteCarlo = false } = cfg;
205
+ const results = new Map();
206
+ // 1. Core Shapley values
207
+ const shapleyValues = (modelIds.length <= 6 && !useMonteCarlo)
208
+ ? exactShapley(modelIds, accuracyFn)
209
+ : monteCarloShapley(modelIds, accuracyFn, nPermutations);
210
+ // 2. Normalize Shapley to [0,1]
211
+ const shapSum = [...shapleyValues.values()].reduce((a, b) => a + b, 0);
212
+ const maxShap = Math.max(...shapleyValues.values(), 0.001);
213
+ // 3. Compute ethnocentrism and handicap for each model
214
+ for (const model of modelIds) {
215
+ const shap = shapleyValues.get(model);
216
+ const ethn = loyalty.ethnoCentrism(model, modelIds);
217
+ const hand = handicap.handicap(model);
218
+ // Normalized values
219
+ const normShap = shap / maxShap;
220
+ const combined = alpha * normShap + beta * ethn + gamma * hand;
221
+ results.set(model, {
222
+ modelId: model,
223
+ shapleyValue: shap,
224
+ loyaltyValue: ethn,
225
+ handicapValue: hand,
226
+ combinedCredit: combined,
227
+ marginalContributions: [],
228
+ timesSelected: 0,
229
+ averageMarginal: shap / Math.max(1, modelIds.length),
230
+ reliabilityScore: handicap.reliability(model),
231
+ costInvested: handicap.avgCost(model),
232
+ ethnocentrismBias: ethn,
233
+ });
234
+ }
235
+ // 4. Normalize combined credits to sum to 1
236
+ const totalCredit = [...results.values()].reduce((s, r) => s + r.combinedCredit, 0);
237
+ if (totalCredit > 0) {
238
+ for (const [, r] of results)
239
+ r.combinedCredit /= totalCredit;
240
+ }
241
+ return results;
242
+ }
243
+ /** Create ensemble accuracy function for Shapley calculation */
244
+ function createAccuracyFn(groundTruth, getAnswer) {
245
+ return (subset) => {
246
+ if (subset.length === 0)
247
+ return 0;
248
+ const votes = {};
249
+ for (const m of subset) {
250
+ const ans = getAnswer(m);
251
+ votes[ans] = (votes[ans] || 0) + 1;
252
+ }
253
+ const majority = Object.entries(votes).sort((a, b) => b[1] - a[1])[0]?.[0] || '';
254
+ return majority === groundTruth ? 1 : 0;
255
+ };
256
+ }
257
+ /** Apply Shapley credit to voting weights */
258
+ function applyCredit(contributions, baseWeights, alpha = 0.5) {
259
+ const result = {};
260
+ for (const [model, contrib] of contributions) {
261
+ const base = baseWeights[model] || 1.0;
262
+ result[model] = (1 - alpha) * base + alpha * contrib.combinedCredit;
263
+ }
264
+ const sum = Object.values(result).reduce((a, b) => a + b, 0);
265
+ for (const k in result)
266
+ result[k] /= sum;
267
+ return result;
268
+ }
269
+ function summarize(contributions) {
270
+ const sorted = [...contributions.values()].sort((a, b) => b.combinedCredit - a.combinedCredit);
271
+ return {
272
+ totalCredit: sorted.reduce((s, c) => s + c.combinedCredit, 0),
273
+ perModel: sorted,
274
+ bestContributor: sorted[0]?.modelId || 'none',
275
+ worstContributor: sorted[sorted.length - 1]?.modelId || 'none',
276
+ };
277
+ }
278
+ // Default export
279
+ exports.default = { calculateEnhancedShapley, LoyaltyMatrix, HandicapCalculator, createAccuracyFn, applyCredit, summarize };
280
+ //# sourceMappingURL=shapleyValue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shapleyValue.js","sourceRoot":"","sources":["../../src/ensemble/shapleyValue.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;;AAiOH,4DAmDC;AAGD,4CAcC;AAGD,kCAaC;AASD,8BAUC;AAvSD,MAAM,eAAe,GAAkB,EAAE,SAAS,EAAE,GAAG,EAAE,eAAe,EAAE,CAAC,EAAE,CAAC;AAE9E;;;GAGG;AACH,MAAa,aAAa;IAChB,MAAM,GAAG,IAAI,GAAG,EAA+B,CAAC;IAChD,MAAM,GAAG,IAAI,GAAG,EAA+B,CAAC;IAChD,MAAM,CAA0B;IAExC,YAAY,MAAqB,EAAE;QACjC,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,GAAG,EAA6B,CAAC;IAC1E,CAAC;IAED,yDAAyD;IACzD,aAAa,CAAC,CAAS,EAAE,CAAS,EAAE,MAAM,GAAG,CAAC;QAC5C,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;YAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;YAAC,IAAI,CAAC,MAAM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vB,aAA6C;IAE7C,MAAM,MAAM,GAAG,CAAC,GAAG,aAAa,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,cAAc,GAAG,CAAC,CAAC,cAAc,CAAC,CAAC;IAC/F,OAAO;QACL,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,cAAc,EAAE,CAAC,CAAC;QAC7D,QAAQ,EAAE,MAAM;QAChB,eAAe,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,IAAI,MAAM;QAC7C,gBAAgB,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,OAAO,IAAI,MAAM;KAC/D,CAAC;AACJ,CAAC;AAED,iBAAiB;AACjB,kBAAe,EAAE,wBAAwB,EAAE,aAAa,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC"}
@@ -1,4 +1,6 @@
1
1
  import { createA3MRouter } from './index';
2
+ import { ShapleySummary } from './ensemble/shapleyValue';
3
+ import { dialogOptimizer } from './ensemble/multiRoundDialog';
2
4
  interface RouteDecision {
3
5
  primary_model: string;
4
6
  tier: 'free' | 'cheap' | 'mid' | 'premium';
@@ -9,7 +11,7 @@ interface RouteDecision {
9
11
  export type RouterDecision = RouteDecision;
10
12
  export declare const A3MRouter: any;
11
13
  export { createA3MRouter };
12
- export type EnsembleStrategy = 'majority' | 'weighted' | 'conservative';
14
+ export type EnsembleStrategy = 'majority' | 'weighted' | 'conservative' | 'shapley';
13
15
  export interface EnsembleResponse {
14
16
  finalAnswer: string;
15
17
  confidence: number;
@@ -20,12 +22,25 @@ export interface EnsembleResponse {
20
22
  score: number;
21
23
  }>;
22
24
  reasoning: string;
25
+ shapleySummary?: ShapleySummary;
26
+ dialogState?: ReturnType<typeof dialogOptimizer.getSummary>;
23
27
  }
24
28
  export declare class EnsembleOrchestrator {
25
29
  private router;
30
+ private loyaltyMatrix;
31
+ private handicapCalc;
26
32
  constructor(router: InstanceType<typeof A3MRouter>);
27
33
  /**
28
- * Executes a query across multiple providers in parallel and resolves the best answer.
34
+ * Execute ensemble with enhanced Shapley value credit assignment
35
+ * Incorporates ethnocentrism (loyalty) and handicap (costly signaling)
29
36
  */
30
- executeEnsemble(query: string, providers: string[], strategy?: EnsembleStrategy, weights?: Record<string, number>): Promise<EnsembleResponse>;
37
+ executeEnsemble(query: string, providers: string[], strategy?: EnsembleStrategy, weights?: Record<string, number>, dialogId?: string): Promise<EnsembleResponse>;
38
+ /** Get best model for current dialog topic */
39
+ getBestModelForTopic(dialogId: string, availableModels: string[]): string | null;
40
+ /** Build optimized context for multi-turn conversation */
41
+ buildOptimizedContext(dialogId: string, newQuery: string): string;
42
+ /** Clear dialog state */
43
+ clearDialog(dialogId: string): void;
31
44
  }
45
+ export { LoyaltyMatrix, HandicapCalculator, calculateEnhancedShapley } from './ensemble/shapleyValue';
46
+ export { dialogOptimizer, MultiRoundDialogOptimizer } from './ensemble/multiRoundDialog';
package/dist/ensemble.js CHANGED
@@ -1,20 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EnsembleOrchestrator = exports.createA3MRouter = exports.A3MRouter = void 0;
3
+ exports.MultiRoundDialogOptimizer = exports.dialogOptimizer = exports.calculateEnhancedShapley = exports.HandicapCalculator = exports.LoyaltyMatrix = exports.EnsembleOrchestrator = exports.createA3MRouter = exports.A3MRouter = void 0;
4
4
  const index_1 = require("./index");
5
5
  Object.defineProperty(exports, "createA3MRouter", { enumerable: true, get: function () { return index_1.createA3MRouter; } });
6
+ const shapleyValue_1 = require("./ensemble/shapleyValue");
7
+ const multiRoundDialog_1 = require("./ensemble/multiRoundDialog");
6
8
  // Re-export A3MRouter as the factory for backward compatibility
7
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
10
  exports.A3MRouter = index_1.createA3MRouter;
9
11
  class EnsembleOrchestrator {
10
12
  router;
13
+ loyaltyMatrix = new shapleyValue_1.LoyaltyMatrix();
14
+ handicapCalc = new shapleyValue_1.HandicapCalculator();
11
15
  constructor(router) {
12
16
  this.router = router;
13
17
  }
14
18
  /**
15
- * Executes a query across multiple providers in parallel and resolves the best answer.
19
+ * Execute ensemble with enhanced Shapley value credit assignment
20
+ * Incorporates ethnocentrism (loyalty) and handicap (costly signaling)
16
21
  */
17
- async executeEnsemble(query, providers, strategy = 'majority', weights = {}) {
22
+ async executeEnsemble(query, providers, strategy = 'majority', weights = {}, dialogId) {
18
23
  // 1. Parallel Execution
19
24
  const results = await Promise.all(providers.map(async (p) => {
20
25
  try {
@@ -26,31 +31,34 @@ class EnsembleOrchestrator {
26
31
  }
27
32
  }));
28
33
  const successful = results.filter(r => r.success);
29
- const answers = successful.map(r => r.answer.trim());
30
- if (answers.length === 0) {
34
+ if (successful.length === 0) {
31
35
  throw new Error('All ensemble providers failed.');
32
36
  }
33
37
  // 2. Voting Logic
34
38
  let winnerAnswer = '';
35
39
  let winnerProvider = '';
36
40
  let confidence = 0;
41
+ let shapleySummary;
42
+ // Multi-round: add user turn
43
+ if (dialogId)
44
+ multiRoundDialog_1.dialogOptimizer.addTurn(dialogId, 'user', query);
37
45
  if (strategy === 'majority') {
38
46
  const counts = {};
39
47
  successful.forEach(r => { counts[r.answer] = (counts[r.answer] || 0) + 1; });
40
48
  const sorted = Object.entries(counts).sort((a, b) => b[1] - a[1]);
41
49
  winnerAnswer = sorted[0][0];
42
- confidence = sorted[0][1] / (successful.length || 1);
50
+ confidence = sorted[0][1] / successful.length;
43
51
  winnerProvider = successful.find(r => r.answer === winnerAnswer)?.provider || 'unknown';
44
52
  }
45
53
  else if (strategy === 'weighted') {
46
54
  const weightedCounts = {};
47
55
  successful.forEach(r => {
48
- const weight = weights[r.provider] || 1.0;
49
- weightedCounts[r.answer] = (weightedCounts[r.answer] || 0) + weight;
56
+ const w = weights[r.provider] || 1.0;
57
+ weightedCounts[r.answer] = (weightedCounts[r.answer] || 0) + w;
50
58
  });
51
59
  const sorted = Object.entries(weightedCounts).sort((a, b) => b[1] - a[1]);
52
60
  winnerAnswer = sorted[0][0];
53
- confidence = sorted[0][1] / (successful.length || 1);
61
+ confidence = sorted[0][1] / successful.length;
54
62
  winnerProvider = successful.find(r => r.answer === winnerAnswer)?.provider || 'unknown';
55
63
  }
56
64
  else if (strategy === 'conservative') {
@@ -59,7 +67,7 @@ class EnsembleOrchestrator {
59
67
  const best = Object.entries(counts).sort((a, b) => b[1] - a[1])[0];
60
68
  if (best && best[1] >= 2) {
61
69
  winnerAnswer = best[0];
62
- confidence = best[1] / (successful.length || 1);
70
+ confidence = best[1] / successful.length;
63
71
  winnerProvider = successful.find(r => r.answer === winnerAnswer)?.provider || 'unknown';
64
72
  }
65
73
  else {
@@ -68,23 +76,89 @@ class EnsembleOrchestrator {
68
76
  winnerProvider = 'none';
69
77
  }
70
78
  }
79
+ else if (strategy === 'shapley') {
80
+ // === ENHANCED SHAPLEY WITH ETHNOCENTRISM + HANDICAP ===
81
+ const providerIds = successful.map(r => r.provider);
82
+ // Accuracy function based on majority vote as ground truth proxy
83
+ const accFn = (0, shapleyValue_1.createAccuracyFn)(winnerAnswer || successful[0].answer, // Will be updated after first pass
84
+ // Will be updated after first pass
85
+ m => successful.find(r => r.provider === m)?.answer || '');
86
+ // Calculate enhanced Shapley with loyalty and handicap
87
+ const contributions = (0, shapleyValue_1.calculateEnhancedShapley)(providerIds, accFn, this.loyaltyMatrix, this.handicapCalc);
88
+ // Get majority vote using Shapley-weighted voting
89
+ const shapleyWeights = (0, shapleyValue_1.applyCredit)(contributions, weights, 0.5);
90
+ const weightedCounts = {};
91
+ successful.forEach(r => {
92
+ weightedCounts[r.answer] = (weightedCounts[r.answer] || 0) + shapleyWeights[r.provider];
93
+ });
94
+ const sorted = Object.entries(weightedCounts).sort((a, b) => b[1] - a[1]);
95
+ winnerAnswer = sorted[0][0];
96
+ confidence = sorted[0][1];
97
+ winnerProvider = successful.find(r => r.answer === winnerAnswer)?.provider || 'unknown';
98
+ // Update Shapley summary
99
+ shapleySummary = (0, shapleyValue_1.summarize)(contributions);
100
+ // Record performance for handicap tracking
101
+ const isCorrect = (ans) => ans === winnerAnswer;
102
+ successful.forEach(r => {
103
+ const cost = weights[r.provider] || 0.001;
104
+ this.handicapCalc.record(r.provider, cost, isCorrect(r.answer));
105
+ });
106
+ }
107
+ // Record loyalty: successful collaborations build trust
108
+ if (strategy === 'shapley') {
109
+ for (const r of successful) {
110
+ if (r.answer === winnerAnswer) {
111
+ for (const other of successful) {
112
+ if (other.provider !== r.provider && other.answer === winnerAnswer) {
113
+ this.loyaltyMatrix.recordSuccess(r.provider, other.provider, 1.0);
114
+ }
115
+ }
116
+ }
117
+ }
118
+ }
119
+ // Multi-round: add assistant turn
120
+ if (dialogId && winnerAnswer !== 'UNCERTAIN') {
121
+ multiRoundDialog_1.dialogOptimizer.addTurn(dialogId, 'assistant', winnerAnswer, winnerProvider);
122
+ }
71
123
  // 3. Final Assembly
72
124
  const allResults = {};
73
125
  successful.forEach(r => {
74
- allResults[r.provider] = {
75
- answer: r.answer,
76
- score: r.answer === winnerAnswer ? 1.0 : 0.0
77
- };
126
+ allResults[r.provider] = { answer: r.answer, score: r.answer === winnerAnswer ? 1.0 : 0.0 };
78
127
  });
128
+ let dialogState;
129
+ if (dialogId)
130
+ dialogState = multiRoundDialog_1.dialogOptimizer.getSummary(dialogId);
79
131
  return {
80
132
  finalAnswer: winnerAnswer,
81
- confidence: confidence,
133
+ confidence,
82
134
  isUncertain: confidence < 0.6 || winnerAnswer === 'UNCERTAIN',
83
135
  winner: winnerProvider,
84
136
  allResults,
85
- reasoning: `Ensemble of ${successful.length} models. ${Math.round(confidence * 100)}% agreement.`
137
+ reasoning: `Ensemble of ${successful.length} models. ${Math.round(confidence * 100)}% agreement.`,
138
+ shapleySummary,
139
+ dialogState,
86
140
  };
87
141
  }
142
+ /** Get best model for current dialog topic */
143
+ getBestModelForTopic(dialogId, availableModels) {
144
+ return multiRoundDialog_1.dialogOptimizer.getBestModelForTopic(dialogId, availableModels);
145
+ }
146
+ /** Build optimized context for multi-turn conversation */
147
+ buildOptimizedContext(dialogId, newQuery) {
148
+ return multiRoundDialog_1.dialogOptimizer.buildOptimizedContext(dialogId, newQuery);
149
+ }
150
+ /** Clear dialog state */
151
+ clearDialog(dialogId) {
152
+ multiRoundDialog_1.dialogOptimizer.clearState(dialogId);
153
+ }
88
154
  }
89
155
  exports.EnsembleOrchestrator = EnsembleOrchestrator;
156
+ // Re-export enhanced utilities
157
+ var shapleyValue_2 = require("./ensemble/shapleyValue");
158
+ Object.defineProperty(exports, "LoyaltyMatrix", { enumerable: true, get: function () { return shapleyValue_2.LoyaltyMatrix; } });
159
+ Object.defineProperty(exports, "HandicapCalculator", { enumerable: true, get: function () { return shapleyValue_2.HandicapCalculator; } });
160
+ Object.defineProperty(exports, "calculateEnhancedShapley", { enumerable: true, get: function () { return shapleyValue_2.calculateEnhancedShapley; } });
161
+ var multiRoundDialog_2 = require("./ensemble/multiRoundDialog");
162
+ Object.defineProperty(exports, "dialogOptimizer", { enumerable: true, get: function () { return multiRoundDialog_2.dialogOptimizer; } });
163
+ Object.defineProperty(exports, "MultiRoundDialogOptimizer", { enumerable: true, get: function () { return multiRoundDialog_2.MultiRoundDialogOptimizer; } });
90
164
  //# sourceMappingURL=ensemble.js.map
package/dist/index.d.ts CHANGED
@@ -16,6 +16,9 @@ export { createProxyServer } from './server/proxyServer';
16
16
  export { Tracer, getTracer, createTracer, MetricsCollector, getMetrics, createMetricsCollector, observabilityMiddleware, observabilityPlugin, budgetAlertMiddleware, } from './observability';
17
17
  export type { Span, Metric, RouteTrace, ObservabilityEvent } from './observability';
18
18
  export { EnsembleOrchestrator, EnsembleStrategy, EnsembleResponse } from './ensemble';
19
+ export { LoyaltyMatrix, HandicapCalculator, calculateEnhancedShapley, createAccuracyFn, applyCredit, summarize, type ShapleyConfig, type ShapleySummary, } from './ensemble/shapleyValue';
20
+ export { MultiRoundDialogOptimizer, dialogOptimizer, type DialogTurn, type DialogState, } from './ensemble/multiRoundDialog';
21
+ export { executeScienceQuery, routeScienceQuery, isScienceQuery, detectScienceDomain, scienceTools, RESEARCH_TEMPLATES, type ScienceQuery, type ScienceResult, } from './integrations/scienceAdapter';
19
22
  import { routeQuery, routeBatch, recommendForTask } from './routing/advancedRouter';
20
23
  import { getAvailableProviders, healthCheck } from './providers/providerConfig';
21
24
  import { CostTracker } from './cost/costTracker';
package/dist/index.js CHANGED
@@ -2,7 +2,8 @@
2
2
  // A3M Router - Main Entry Point
3
3
  // Version: 2.0.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.EnsembleOrchestrator = exports.budgetAlertMiddleware = exports.observabilityPlugin = exports.observabilityMiddleware = exports.createMetricsCollector = exports.getMetrics = exports.MetricsCollector = exports.createTracer = exports.getTracer = exports.Tracer = exports.createProxyServer = exports.CostAnalytics = exports.GuardrailEngine = exports.SemanticCache = exports.estimateTokens = exports.countTokens = exports.MemoryTree = exports.createBudgetEnforcer = exports.BudgetExceededError = exports.BudgetEnforcer = exports.CostTracker = exports.saveConfig = exports.loadConfig = exports.findFastestAvailableProvider = exports.findCheapestAvailableProvider = exports.checkAllProviders = exports.healthCheck = exports.updateProvider = exports.deregisterProvider = exports.registerProvider = exports.getAvailableProviders = exports.DEFAULT_PROVIDERS = exports.PROVIDER_CONTEXT_LIMITS = exports.DEFAULT_PROVIDER_CONFIG = exports.DEFAULT_RETRY_CONFIG = exports.getDefaultRetryHandler = exports.createRetryHandler = exports.ProviderRetryHandler = exports.getProviderHealth = exports.updateModelProfile = exports.MODEL_PROFILES = exports.extractQueryFeatures = exports.recommendForTask = exports.routeBatch = exports.routeQuery = void 0;
5
+ exports.applyCredit = exports.createAccuracyFn = exports.calculateEnhancedShapley = exports.HandicapCalculator = exports.LoyaltyMatrix = exports.EnsembleOrchestrator = exports.budgetAlertMiddleware = exports.observabilityPlugin = exports.observabilityMiddleware = exports.createMetricsCollector = exports.getMetrics = exports.MetricsCollector = exports.createTracer = exports.getTracer = exports.Tracer = exports.createProxyServer = exports.CostAnalytics = exports.GuardrailEngine = exports.SemanticCache = exports.estimateTokens = exports.countTokens = exports.MemoryTree = exports.createBudgetEnforcer = exports.BudgetExceededError = exports.BudgetEnforcer = exports.CostTracker = exports.saveConfig = exports.loadConfig = exports.findFastestAvailableProvider = exports.findCheapestAvailableProvider = exports.checkAllProviders = exports.healthCheck = exports.updateProvider = exports.deregisterProvider = exports.registerProvider = exports.getAvailableProviders = exports.DEFAULT_PROVIDERS = exports.PROVIDER_CONTEXT_LIMITS = exports.DEFAULT_PROVIDER_CONFIG = exports.DEFAULT_RETRY_CONFIG = exports.getDefaultRetryHandler = exports.createRetryHandler = exports.ProviderRetryHandler = exports.getProviderHealth = exports.updateModelProfile = exports.MODEL_PROFILES = exports.extractQueryFeatures = exports.recommendForTask = exports.routeBatch = exports.routeQuery = void 0;
6
+ exports.RESEARCH_TEMPLATES = exports.scienceTools = exports.detectScienceDomain = exports.isScienceQuery = exports.routeScienceQuery = exports.executeScienceQuery = exports.dialogOptimizer = exports.MultiRoundDialogOptimizer = exports.summarize = void 0;
6
7
  exports.createA3MRouter = createA3MRouter;
7
8
  // ============================================================
8
9
  // ROUTING ENGINE
@@ -90,6 +91,32 @@ Object.defineProperty(exports, "budgetAlertMiddleware", { enumerable: true, get:
90
91
  var ensemble_1 = require("./ensemble");
91
92
  Object.defineProperty(exports, "EnsembleOrchestrator", { enumerable: true, get: function () { return ensemble_1.EnsembleOrchestrator; } });
92
93
  // ============================================================
94
+ // ENHANCED SHAPLEY VALUE (Game Theory: Ethnocentrism + Handicap)
95
+ // ============================================================
96
+ var shapleyValue_1 = require("./ensemble/shapleyValue");
97
+ Object.defineProperty(exports, "LoyaltyMatrix", { enumerable: true, get: function () { return shapleyValue_1.LoyaltyMatrix; } });
98
+ Object.defineProperty(exports, "HandicapCalculator", { enumerable: true, get: function () { return shapleyValue_1.HandicapCalculator; } });
99
+ Object.defineProperty(exports, "calculateEnhancedShapley", { enumerable: true, get: function () { return shapleyValue_1.calculateEnhancedShapley; } });
100
+ Object.defineProperty(exports, "createAccuracyFn", { enumerable: true, get: function () { return shapleyValue_1.createAccuracyFn; } });
101
+ Object.defineProperty(exports, "applyCredit", { enumerable: true, get: function () { return shapleyValue_1.applyCredit; } });
102
+ Object.defineProperty(exports, "summarize", { enumerable: true, get: function () { return shapleyValue_1.summarize; } });
103
+ // ============================================================
104
+ // MULTI-ROUND DIALOG OPTIMIZATION
105
+ // ============================================================
106
+ var multiRoundDialog_1 = require("./ensemble/multiRoundDialog");
107
+ Object.defineProperty(exports, "MultiRoundDialogOptimizer", { enumerable: true, get: function () { return multiRoundDialog_1.MultiRoundDialogOptimizer; } });
108
+ Object.defineProperty(exports, "dialogOptimizer", { enumerable: true, get: function () { return multiRoundDialog_1.dialogOptimizer; } });
109
+ // ============================================================
110
+ // SCIENCE ADAPTER (Google DeepMind Skills)
111
+ // ============================================================
112
+ var scienceAdapter_1 = require("./integrations/scienceAdapter");
113
+ Object.defineProperty(exports, "executeScienceQuery", { enumerable: true, get: function () { return scienceAdapter_1.executeScienceQuery; } });
114
+ Object.defineProperty(exports, "routeScienceQuery", { enumerable: true, get: function () { return scienceAdapter_1.routeScienceQuery; } });
115
+ Object.defineProperty(exports, "isScienceQuery", { enumerable: true, get: function () { return scienceAdapter_1.isScienceQuery; } });
116
+ Object.defineProperty(exports, "detectScienceDomain", { enumerable: true, get: function () { return scienceAdapter_1.detectScienceDomain; } });
117
+ Object.defineProperty(exports, "scienceTools", { enumerable: true, get: function () { return scienceAdapter_1.scienceTools; } });
118
+ Object.defineProperty(exports, "RESEARCH_TEMPLATES", { enumerable: true, get: function () { return scienceAdapter_1.RESEARCH_TEMPLATES; } });
119
+ // ============================================================
93
120
  // CONVENIENCE: Create a router instance
94
121
  // ============================================================
95
122
  const advancedRouter_2 = require("./routing/advancedRouter");
@@ -0,0 +1,77 @@
1
+ /**
2
+ * A3M Router — Science Adapter
3
+ *
4
+ * Wraps Google DeepMind science skills as A3M tools for research queries.
5
+ *
6
+ * Available skills (39+):
7
+ * Genomics: alphagenome_single_variant_analysis, ensembl, gnomad, dbsnp
8
+ * Proteins: alphafold, uniprot, pdb, string
9
+ * Chemistry: chembl, pubchem, openfda
10
+ * Literature: arxiv, biorxiv, openalex, pubmed
11
+ * Clinical: clinical_trials, clinvar
12
+ * Expression: gtex, human_protein_atlas
13
+ */
14
+ export interface ScienceQuery {
15
+ domain: 'genomics' | 'proteins' | 'chemistry' | 'literature' | 'clinical' | 'expression';
16
+ query: string;
17
+ species?: string;
18
+ protein?: string;
19
+ gene?: string;
20
+ disease?: string;
21
+ }
22
+ export interface ScienceResult {
23
+ success: boolean;
24
+ tool: string;
25
+ answer: string;
26
+ citations?: string[];
27
+ metadata?: Record<string, any>;
28
+ }
29
+ export declare const RESEARCH_TEMPLATES: Record<string, string>;
30
+ /**
31
+ * Route a science query to the appropriate skill
32
+ */
33
+ export declare function routeScienceQuery(query: ScienceQuery): string;
34
+ /**
35
+ * Execute a science query using A3M routing
36
+ */
37
+ export declare function executeScienceQuery(query: ScienceQuery): Promise<ScienceResult>;
38
+ /**
39
+ * Check if a query is a science/research query
40
+ */
41
+ export declare function isScienceQuery(prompt: string): boolean;
42
+ /**
43
+ * Detect science domain from query
44
+ */
45
+ export declare function detectScienceDomain(prompt: string): ScienceQuery['domain'] | null;
46
+ export declare const scienceTools: {
47
+ alphafold: string;
48
+ uniprot: string;
49
+ pdb: string;
50
+ ensembl: string;
51
+ pubmed: string;
52
+ arxiv: string;
53
+ chembl: string;
54
+ pubchem: string;
55
+ clinicalTrials: string;
56
+ gtex: string;
57
+ };
58
+ declare const _default: {
59
+ executeScienceQuery: typeof executeScienceQuery;
60
+ routeScienceQuery: typeof routeScienceQuery;
61
+ isScienceQuery: typeof isScienceQuery;
62
+ detectScienceDomain: typeof detectScienceDomain;
63
+ scienceTools: {
64
+ alphafold: string;
65
+ uniprot: string;
66
+ pdb: string;
67
+ ensembl: string;
68
+ pubmed: string;
69
+ arxiv: string;
70
+ chembl: string;
71
+ pubchem: string;
72
+ clinicalTrials: string;
73
+ gtex: string;
74
+ };
75
+ RESEARCH_TEMPLATES: Record<string, string>;
76
+ };
77
+ export default _default;