Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {
StrategyModes,
type SuperAgentsConfig,
type SuperAgentsTarget,
} from '@shared/types/api/request/headers';
type Query = {
[key: string]: unknown;
};
interface RouterContext {
metadata?: Record<string, unknown>;
params?: Record<string, unknown>;
}
enum Operator {
// Comparison Operators
Equal = '$eq',
NotEqual = '$ne',
GreaterThan = '$gt',
GreaterThanOrEqual = '$gte',
LessThan = '$lt',
LessThanOrEqual = '$lte',
In = '$in',
NotIn = '$nin',
Regex = '$regex',
// Logical Operators
And = '$and',
Or = '$or',
}
export class ConditionalRouter {
private saConfig: SuperAgentsConfig;
private context: RouterContext;
constructor(config: SuperAgentsConfig, context: RouterContext) {
this.saConfig = config;
this.context = context;
if (this.saConfig.strategy.mode !== StrategyModes.CONDITIONAL) {
throw new Error('Unsupported strategy mode');
}
}
resolveTarget(): SuperAgentsTarget {
if (!this.saConfig.strategy.conditions) {
throw new Error('No conditions passed in the query router');
}
for (const condition of this.saConfig.strategy.conditions) {
if (this.evaluateQuery(condition.query)) {
const cond = condition as unknown as Record<string, unknown>;
const targetName = (cond.target as string) ?? (cond.then as string);
return this.findTarget(targetName);
}
}
// If no conditions matched and a default is specified, return the default target
if (this.saConfig.strategy.default) {
return this.findTarget(this.saConfig.strategy.default);
}
throw new Error('Query router did not resolve to any valid target');
}
private evaluateQuery(query: Query): boolean {
for (const [key, value] of Object.entries(query)) {
if (key === Operator.Or && Array.isArray(value)) {
return value.some((subCondition: Query) =>
this.evaluateQuery(subCondition),
);
}
if (key === Operator.And && Array.isArray(value)) {
return value.every((subCondition: Query) =>
this.evaluateQuery(subCondition),
);
}
const contextValue = this.getContextValue(key);
if (typeof value === 'object' && value !== null) {
if (!this.evaluateOperator(value, contextValue)) {
return false;
}
} else if (contextValue !== value) {
return false;
}
}
return true;
}
private evaluateOperator(operator: object, value: unknown): boolean {
for (const [op, compareValue] of Object.entries(operator)) {
switch (op) {
case Operator.Equal:
if (value !== compareValue) return false;
break;
case Operator.NotEqual:
if (value === compareValue) return false;
break;
case Operator.GreaterThan:
if (
!(parseFloat(value as string) > parseFloat(compareValue as string))
)
return false;
break;
case Operator.GreaterThanOrEqual:
if (
!(parseFloat(value as string) >= parseFloat(compareValue as string))
)
return false;
break;
case Operator.LessThan:
if (
!(parseFloat(value as string) < parseFloat(compareValue as string))
)
return false;
break;
case Operator.LessThanOrEqual:
if (
!(parseFloat(value as string) <= parseFloat(compareValue as string))
)
return false;
break;
case Operator.In:
if (!Array.isArray(compareValue) || !compareValue.includes(value))
return false;
break;
case Operator.NotIn:
if (!Array.isArray(compareValue) || compareValue.includes(value))
return false;
break;
case Operator.Regex:
try {
const regex = new RegExp(compareValue);
return regex.test(value as string);
} catch (_e) {
return false;
}
default:
throw new Error(
`Unsupported operator used in the query router: ${op}`,
);
}
}
return true;
}
private findTarget(id: string): SuperAgentsTarget {
const index =
this.saConfig.targets?.findIndex((target) => target.id === id) ?? -1;
if (index === -1) {
throw new Error(`Invalid target id found in the query router: ${id}`);
}
const target = this.saConfig.targets?.[index];
if (!target) {
throw new Error(`Invalid target id found in the query router: ${id}`);
}
const targets: SuperAgentsTarget = {
...target,
index,
};
return targets;
}
private getContextValue(key: string): unknown {
const parts = key.split('.');
const context = this.context as Record<string, unknown>;
const firstKey = parts[0];
const secondKey = parts[1];
let contextValue: unknown;
if (firstKey && secondKey) {
const firstValue = context[firstKey] as Record<string, unknown>;
contextValue = firstValue[secondKey];
} else if (firstKey) {
contextValue = context[firstKey] as Record<string, unknown>;
}
return contextValue;
}
}
|