@nahisaho/musubix-yata-client 1.0.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.
Files changed (49) hide show
  1. package/dist/__tests__/index.test.d.ts +2 -0
  2. package/dist/__tests__/index.test.d.ts.map +1 -0
  3. package/dist/__tests__/index.test.js +58 -0
  4. package/dist/__tests__/index.test.js.map +1 -0
  5. package/dist/index.d.ts +42 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +45 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/knowledge/index.d.ts +11 -0
  10. package/dist/knowledge/index.d.ts.map +1 -0
  11. package/dist/knowledge/index.js +11 -0
  12. package/dist/knowledge/index.js.map +1 -0
  13. package/dist/knowledge/ontology.d.ts +192 -0
  14. package/dist/knowledge/ontology.d.ts.map +1 -0
  15. package/dist/knowledge/ontology.js +261 -0
  16. package/dist/knowledge/ontology.js.map +1 -0
  17. package/dist/knowledge/query.d.ts +111 -0
  18. package/dist/knowledge/query.d.ts.map +1 -0
  19. package/dist/knowledge/query.js +207 -0
  20. package/dist/knowledge/query.js.map +1 -0
  21. package/dist/mcp/client.d.ts +128 -0
  22. package/dist/mcp/client.d.ts.map +1 -0
  23. package/dist/mcp/client.js +294 -0
  24. package/dist/mcp/client.js.map +1 -0
  25. package/dist/mcp/index.d.ts +8 -0
  26. package/dist/mcp/index.d.ts.map +1 -0
  27. package/dist/mcp/index.js +8 -0
  28. package/dist/mcp/index.js.map +1 -0
  29. package/dist/reasoning/confidence.d.ts +175 -0
  30. package/dist/reasoning/confidence.d.ts.map +1 -0
  31. package/dist/reasoning/confidence.js +278 -0
  32. package/dist/reasoning/confidence.js.map +1 -0
  33. package/dist/reasoning/contradiction.d.ts +236 -0
  34. package/dist/reasoning/contradiction.d.ts.map +1 -0
  35. package/dist/reasoning/contradiction.js +487 -0
  36. package/dist/reasoning/contradiction.js.map +1 -0
  37. package/dist/reasoning/index.d.ts +12 -0
  38. package/dist/reasoning/index.d.ts.map +1 -0
  39. package/dist/reasoning/index.js +15 -0
  40. package/dist/reasoning/index.js.map +1 -0
  41. package/dist/reasoning/integrator.d.ts +230 -0
  42. package/dist/reasoning/integrator.d.ts.map +1 -0
  43. package/dist/reasoning/integrator.js +364 -0
  44. package/dist/reasoning/integrator.js.map +1 -0
  45. package/dist/types.d.ts +171 -0
  46. package/dist/types.d.ts.map +1 -0
  47. package/dist/types.js +22 -0
  48. package/dist/types.js.map +1 -0
  49. package/package.json +68 -0
@@ -0,0 +1,278 @@
1
+ /**
2
+ * Confidence Evaluator
3
+ *
4
+ * Evaluates and combines confidence scores from multiple sources
5
+ *
6
+ * @packageDocumentation
7
+ * @module reasoning/confidence
8
+ *
9
+ * @see REQ-NS-101 - Symbolic Reasoning
10
+ * @see REQ-NS-103 - Confidence Scoring
11
+ */
12
+ /**
13
+ * Default evaluator configuration
14
+ */
15
+ export const DEFAULT_EVALUATOR_CONFIG = {
16
+ defaultMethod: 'weighted-average',
17
+ defaultWeights: {
18
+ neural: 0.6,
19
+ symbolic: 0.9,
20
+ pattern: 0.8,
21
+ historical: 0.7,
22
+ consensus: 0.85,
23
+ 'user-feedback': 0.95,
24
+ },
25
+ minimumThreshold: 0.5,
26
+ enableDecay: false,
27
+ decayHalfLife: 86400000, // 24 hours
28
+ };
29
+ /**
30
+ * Confidence Evaluator
31
+ *
32
+ * Evaluates, combines, and calibrates confidence scores
33
+ */
34
+ export class ConfidenceEvaluator {
35
+ config;
36
+ calibrationHistory = [];
37
+ calibrationStats = new Map();
38
+ constructor(config) {
39
+ this.config = { ...DEFAULT_EVALUATOR_CONFIG, ...config };
40
+ }
41
+ /**
42
+ * Create a confidence score
43
+ */
44
+ createScore(value, source, metadata) {
45
+ const weight = this.config.defaultWeights[source] ?? 0.5;
46
+ return {
47
+ value: this.clamp(value),
48
+ source,
49
+ weight,
50
+ timestamp: Date.now(),
51
+ metadata,
52
+ };
53
+ }
54
+ /**
55
+ * Combine multiple confidence scores
56
+ */
57
+ combine(scores, method) {
58
+ const combinationMethod = method ?? this.config.defaultMethod;
59
+ if (scores.length === 0) {
60
+ return {
61
+ score: 0,
62
+ components: [],
63
+ method: combinationMethod,
64
+ explanation: 'No scores to combine',
65
+ };
66
+ }
67
+ // Apply time decay if enabled
68
+ const adjustedScores = this.config.enableDecay
69
+ ? scores.map((s) => this.applyDecay(s))
70
+ : scores;
71
+ let combinedScore;
72
+ switch (combinationMethod) {
73
+ case 'weighted-average':
74
+ combinedScore = this.weightedAverage(adjustedScores);
75
+ break;
76
+ case 'geometric-mean':
77
+ combinedScore = this.geometricMean(adjustedScores);
78
+ break;
79
+ case 'harmonic-mean':
80
+ combinedScore = this.harmonicMean(adjustedScores);
81
+ break;
82
+ case 'minimum':
83
+ combinedScore = Math.min(...adjustedScores.map((s) => s.value));
84
+ break;
85
+ case 'maximum':
86
+ combinedScore = Math.max(...adjustedScores.map((s) => s.value));
87
+ break;
88
+ case 'bayesian':
89
+ combinedScore = this.bayesianCombine(adjustedScores);
90
+ break;
91
+ default:
92
+ combinedScore = this.weightedAverage(adjustedScores);
93
+ }
94
+ return {
95
+ score: this.clamp(combinedScore),
96
+ components: adjustedScores,
97
+ method: combinationMethod,
98
+ explanation: this.generateExplanation(adjustedScores, combinedScore, combinationMethod),
99
+ };
100
+ }
101
+ /**
102
+ * Evaluate if confidence meets threshold
103
+ */
104
+ meetsThreshold(score, threshold) {
105
+ const minThreshold = threshold ?? this.config.minimumThreshold;
106
+ const value = typeof score === 'number'
107
+ ? score
108
+ : 'score' in score
109
+ ? score.score
110
+ : score.value;
111
+ return value >= minThreshold;
112
+ }
113
+ /**
114
+ * Record outcome for calibration
115
+ */
116
+ recordOutcome(predicted, actual, source) {
117
+ this.calibrationHistory.push({
118
+ predicted,
119
+ actual: actual ? 1 : 0,
120
+ timestamp: Date.now(),
121
+ source,
122
+ });
123
+ // Update stats periodically
124
+ if (this.calibrationHistory.length % 100 === 0) {
125
+ this.updateCalibrationStats();
126
+ }
127
+ }
128
+ /**
129
+ * Get calibration statistics
130
+ */
131
+ getCalibrationStats(source) {
132
+ if (source) {
133
+ const stats = this.calibrationStats.get(source);
134
+ return stats ? [stats] : [];
135
+ }
136
+ return Array.from(this.calibrationStats.values());
137
+ }
138
+ /**
139
+ * Calibrate a score based on historical performance
140
+ */
141
+ calibrate(score) {
142
+ const stats = this.calibrationStats.get(score.source);
143
+ if (!stats || stats.sampleCount < 10) {
144
+ return score; // Not enough data
145
+ }
146
+ // Apply calibration adjustment
147
+ const calibrationFactor = stats.actualRate / stats.meanPredicted;
148
+ const calibratedValue = this.clamp(score.value * calibrationFactor);
149
+ return {
150
+ ...score,
151
+ value: calibratedValue,
152
+ metadata: {
153
+ ...score.metadata,
154
+ calibrated: true,
155
+ originalValue: score.value,
156
+ calibrationFactor,
157
+ },
158
+ };
159
+ }
160
+ /**
161
+ * Apply time decay to score
162
+ */
163
+ applyDecay(score) {
164
+ const age = Date.now() - score.timestamp;
165
+ const decayFactor = Math.pow(0.5, age / this.config.decayHalfLife);
166
+ return {
167
+ ...score,
168
+ value: score.value * decayFactor,
169
+ metadata: {
170
+ ...score.metadata,
171
+ decayed: true,
172
+ originalValue: score.value,
173
+ decayFactor,
174
+ },
175
+ };
176
+ }
177
+ /**
178
+ * Weighted average combination
179
+ */
180
+ weightedAverage(scores) {
181
+ const totalWeight = scores.reduce((sum, s) => sum + s.weight, 0);
182
+ if (totalWeight === 0)
183
+ return 0;
184
+ const weightedSum = scores.reduce((sum, s) => sum + s.value * s.weight, 0);
185
+ return weightedSum / totalWeight;
186
+ }
187
+ /**
188
+ * Geometric mean combination
189
+ */
190
+ geometricMean(scores) {
191
+ if (scores.some((s) => s.value === 0))
192
+ return 0;
193
+ const product = scores.reduce((prod, s) => prod * s.value, 1);
194
+ return Math.pow(product, 1 / scores.length);
195
+ }
196
+ /**
197
+ * Harmonic mean combination
198
+ */
199
+ harmonicMean(scores) {
200
+ if (scores.some((s) => s.value === 0))
201
+ return 0;
202
+ const sumReciprocals = scores.reduce((sum, s) => sum + 1 / s.value, 0);
203
+ return scores.length / sumReciprocals;
204
+ }
205
+ /**
206
+ * Bayesian combination
207
+ */
208
+ bayesianCombine(scores) {
209
+ // Convert confidences to odds, multiply, convert back
210
+ let combinedOdds = 1;
211
+ for (const score of scores) {
212
+ // Avoid division by zero and log of zero
213
+ const clamped = Math.max(0.001, Math.min(0.999, score.value));
214
+ const odds = clamped / (1 - clamped);
215
+ combinedOdds *= Math.pow(odds, score.weight);
216
+ }
217
+ // Convert back to probability
218
+ return combinedOdds / (1 + combinedOdds);
219
+ }
220
+ /**
221
+ * Update calibration statistics
222
+ */
223
+ updateCalibrationStats() {
224
+ const bySource = new Map();
225
+ for (const data of this.calibrationHistory) {
226
+ const existing = bySource.get(data.source) ?? [];
227
+ existing.push(data);
228
+ bySource.set(data.source, existing);
229
+ }
230
+ for (const [source, data] of bySource) {
231
+ const n = data.length;
232
+ const meanPredicted = data.reduce((s, d) => s + d.predicted, 0) / n;
233
+ const actualRate = data.reduce((s, d) => s + d.actual, 0) / n;
234
+ const calibrationError = Math.abs(meanPredicted - actualRate);
235
+ const brierScore = data.reduce((s, d) => s + Math.pow(d.predicted - d.actual, 2), 0) / n;
236
+ this.calibrationStats.set(source, {
237
+ source,
238
+ sampleCount: n,
239
+ meanPredicted,
240
+ actualRate,
241
+ calibrationError,
242
+ brierScore,
243
+ });
244
+ }
245
+ }
246
+ /**
247
+ * Generate explanation for combination
248
+ */
249
+ generateExplanation(scores, combined, method) {
250
+ const sourceDescriptions = scores.map((s) => `${s.source}: ${(s.value * 100).toFixed(1)}% (weight: ${s.weight})`);
251
+ return [
252
+ `Combined ${scores.length} confidence scores using ${method}.`,
253
+ `Sources: ${sourceDescriptions.join(', ')}.`,
254
+ `Result: ${(combined * 100).toFixed(1)}% confidence.`,
255
+ ].join(' ');
256
+ }
257
+ /**
258
+ * Clamp value to valid range
259
+ */
260
+ clamp(value) {
261
+ return Math.max(0, Math.min(1, value));
262
+ }
263
+ }
264
+ /**
265
+ * Create confidence evaluator instance
266
+ */
267
+ export function createConfidenceEvaluator(config) {
268
+ return new ConfidenceEvaluator(config);
269
+ }
270
+ /**
271
+ * Quick confidence combination helper
272
+ */
273
+ export function combineConfidence(scores, method) {
274
+ const evaluator = new ConfidenceEvaluator();
275
+ const fullScores = scores.map((s) => evaluator.createScore(s.value, s.source));
276
+ return evaluator.combine(fullScores, method).score;
277
+ }
278
+ //# sourceMappingURL=confidence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"confidence.js","sourceRoot":"","sources":["../../src/reasoning/confidence.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAsEH;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAA8B;IACjE,aAAa,EAAE,kBAAkB;IACjC,cAAc,EAAE;QACd,MAAM,EAAE,GAAG;QACX,QAAQ,EAAE,GAAG;QACb,OAAO,EAAE,GAAG;QACZ,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,IAAI;QACf,eAAe,EAAE,IAAI;KACtB;IACD,gBAAgB,EAAE,GAAG;IACrB,WAAW,EAAE,KAAK;IAClB,aAAa,EAAE,QAAQ,EAAE,WAAW;CACrC,CAAC;AAkCF;;;;GAIG;AACH,MAAM,OAAO,mBAAmB;IACtB,MAAM,CAA4B;IAClC,kBAAkB,GAAsB,EAAE,CAAC;IAC3C,gBAAgB,GAA4C,IAAI,GAAG,EAAE,CAAC;IAE9E,YAAY,MAA2C;QACrD,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,wBAAwB,EAAE,GAAG,MAAM,EAAE,CAAC;IAC3D,CAAC;IAED;;OAEG;IACH,WAAW,CACT,KAAa,EACb,MAAwB,EACxB,QAAkC;QAElC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC;QAEzD,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;YACxB,MAAM;YAC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CAAC,IAAI,CAAC,GAAG;YAC5C,WAAW,CAAC,QAAQ,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe;SACtD,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACd,CAAC;IAED;;OAEG;IACK,KAAK,CAAC,KAAa;QACzB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;IACzC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,yBAAyB,CACvC,MAA2C;IAE3C,OAAO,IAAI,mBAAmB,CAAC,MAAM,CAAC,CAAC;AACzC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAA0D,EAC1D,MAA0B;IAE1B,MAAM,SAAS,GAAG,IAAI,mBAAmB,EAAE,CAAC;IAC5C,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAClC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,CACzC,CAAC;IACF,OAAO,SAAS,CAAC,OAAO,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC;AACrD,CAAC"}
@@ -0,0 +1,236 @@
1
+ /**
2
+ * Contradiction Detector
3
+ *
4
+ * Detects and resolves contradictions between knowledge sources
5
+ *
6
+ * @packageDocumentation
7
+ * @module reasoning/contradiction
8
+ *
9
+ * @see REQ-NS-101 - Symbolic Reasoning
10
+ * @see REQ-NS-104 - Contradiction Detection
11
+ */
12
+ import type { KnowledgeNode, KnowledgeEdge } from '../types.js';
13
+ /**
14
+ * Contradiction types
15
+ */
16
+ export type ContradictionType = 'logical' | 'temporal' | 'cardinality' | 'semantic' | 'structural' | 'requirement' | 'priority';
17
+ /**
18
+ * Contradiction severity levels
19
+ */
20
+ export type ContradictionSeverity = 'critical' | 'high' | 'medium' | 'low';
21
+ /**
22
+ * Detected contradiction
23
+ */
24
+ export interface Contradiction {
25
+ /** Unique ID */
26
+ id: string;
27
+ /** Contradiction type */
28
+ type: ContradictionType;
29
+ /** Severity level */
30
+ severity: ContradictionSeverity;
31
+ /** Description */
32
+ description: string;
33
+ /** First conflicting element */
34
+ element1: ConflictingElement;
35
+ /** Second conflicting element */
36
+ element2: ConflictingElement;
37
+ /** Suggested resolutions */
38
+ resolutions: Resolution[];
39
+ /** Detected timestamp */
40
+ detectedAt: number;
41
+ /** Confidence in detection */
42
+ confidence: number;
43
+ }
44
+ /**
45
+ * Conflicting element
46
+ */
47
+ export interface ConflictingElement {
48
+ /** Element ID */
49
+ id: string;
50
+ /** Element type */
51
+ type: string;
52
+ /** Element content/value */
53
+ content: string;
54
+ /** Source document/location */
55
+ source: string;
56
+ /** Priority if applicable */
57
+ priority?: number;
58
+ /** Timestamp */
59
+ timestamp?: number;
60
+ }
61
+ /**
62
+ * Resolution suggestion
63
+ */
64
+ export interface Resolution {
65
+ /** Resolution ID */
66
+ id: string;
67
+ /** Resolution strategy */
68
+ strategy: ResolutionStrategy;
69
+ /** Description */
70
+ description: string;
71
+ /** Which element to prefer */
72
+ preferElement?: 'element1' | 'element2' | 'neither' | 'merge';
73
+ /** Confidence this will resolve */
74
+ confidence: number;
75
+ /** Side effects of resolution */
76
+ sideEffects?: string[];
77
+ }
78
+ /**
79
+ * Resolution strategies
80
+ */
81
+ export type ResolutionStrategy = 'prefer-newer' | 'prefer-higher-priority' | 'prefer-more-specific' | 'merge-both' | 'escalate-to-user' | 'invalidate-both' | 'use-knowledge-graph' | 'majority-vote';
82
+ /**
83
+ * Detection rule
84
+ */
85
+ export interface DetectionRule {
86
+ /** Rule ID */
87
+ id: string;
88
+ /** Rule name */
89
+ name: string;
90
+ /** Contradiction types this rule detects */
91
+ types: ContradictionType[];
92
+ /** Detection function */
93
+ detect: (nodes: KnowledgeNode[], edges: KnowledgeEdge[], context: DetectionContext) => Contradiction[];
94
+ }
95
+ /**
96
+ * Detection context
97
+ */
98
+ export interface DetectionContext {
99
+ /** Current timestamp */
100
+ now: number;
101
+ /** Configuration */
102
+ config: ContradictionDetectorConfig;
103
+ /** Helper functions */
104
+ helpers: DetectionHelpers;
105
+ }
106
+ /**
107
+ * Detection helpers
108
+ */
109
+ export interface DetectionHelpers {
110
+ /** Generate unique ID */
111
+ generateId: () => string;
112
+ /** Calculate semantic similarity */
113
+ semanticSimilarity: (text1: string, text2: string) => number;
114
+ /** Parse requirement pattern */
115
+ parseRequirement: (text: string) => RequirementParts | null;
116
+ }
117
+ /**
118
+ * Parsed requirement parts
119
+ */
120
+ export interface RequirementParts {
121
+ condition?: string;
122
+ subject: string;
123
+ action: string;
124
+ object?: string;
125
+ constraint?: string;
126
+ }
127
+ /**
128
+ * Detector configuration
129
+ */
130
+ export interface ContradictionDetectorConfig {
131
+ /** Enabled detection types */
132
+ enabledTypes: ContradictionType[];
133
+ /** Semantic similarity threshold */
134
+ similarityThreshold: number;
135
+ /** Minimum confidence to report */
136
+ minimumConfidence: number;
137
+ /** Maximum results */
138
+ maxResults: number;
139
+ /** Enable auto-resolution */
140
+ autoResolve: boolean;
141
+ /** Default resolution strategy */
142
+ defaultStrategy: ResolutionStrategy;
143
+ }
144
+ /**
145
+ * Default configuration
146
+ */
147
+ export declare const DEFAULT_DETECTOR_CONFIG: ContradictionDetectorConfig;
148
+ /**
149
+ * Detection report
150
+ */
151
+ export interface DetectionReport {
152
+ /** Detection timestamp */
153
+ timestamp: number;
154
+ /** Total contradictions found */
155
+ totalFound: number;
156
+ /** Contradictions by type */
157
+ byType: Record<ContradictionType, number>;
158
+ /** Contradictions by severity */
159
+ bySeverity: Record<ContradictionSeverity, number>;
160
+ /** Contradictions list */
161
+ contradictions: Contradiction[];
162
+ /** Auto-resolved count */
163
+ autoResolved: number;
164
+ }
165
+ /**
166
+ * Contradiction Detector
167
+ *
168
+ * Detects contradictions in knowledge and requirements
169
+ */
170
+ export declare class ContradictionDetector {
171
+ private config;
172
+ private rules;
173
+ private idCounter;
174
+ constructor(config?: Partial<ContradictionDetectorConfig>);
175
+ /**
176
+ * Initialize built-in detection rules
177
+ */
178
+ private initializeBuiltinRules;
179
+ /**
180
+ * Add detection rule
181
+ */
182
+ addRule(rule: DetectionRule): void;
183
+ /**
184
+ * Remove detection rule
185
+ */
186
+ removeRule(ruleId: string): boolean;
187
+ /**
188
+ * Detect contradictions in knowledge
189
+ */
190
+ detect(nodes: KnowledgeNode[], edges: KnowledgeEdge[]): DetectionReport;
191
+ /**
192
+ * Check if one statement negates another
193
+ */
194
+ private isNegation;
195
+ /**
196
+ * Check if actions conflict
197
+ */
198
+ private actionsConflict;
199
+ /**
200
+ * Simple semantic similarity (Jaccard-like)
201
+ */
202
+ private semanticSimilarity;
203
+ /**
204
+ * Parse EARS requirement
205
+ */
206
+ private parseRequirement;
207
+ /**
208
+ * Find temporal cycles using DFS
209
+ */
210
+ private findTemporalCycles;
211
+ /**
212
+ * Convert node to conflicting element
213
+ */
214
+ private nodeToElement;
215
+ /**
216
+ * Generate resolution suggestions
217
+ */
218
+ private generateResolutions;
219
+ /**
220
+ * Auto-resolve contradictions
221
+ */
222
+ private autoResolve;
223
+ /**
224
+ * Count contradictions by type
225
+ */
226
+ private countByType;
227
+ /**
228
+ * Count contradictions by severity
229
+ */
230
+ private countBySeverity;
231
+ }
232
+ /**
233
+ * Create contradiction detector instance
234
+ */
235
+ export declare function createContradictionDetector(config?: Partial<ContradictionDetectorConfig>): ContradictionDetector;
236
+ //# sourceMappingURL=contradiction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contradiction.d.ts","sourceRoot":"","sources":["../../src/reasoning/contradiction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEhE;;GAEG;AACH,MAAM,MAAM,iBAAiB,GACzB,SAAS,GACT,UAAU,GACV,aAAa,GACb,UAAU,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