ai-patterns
Version:
Production-ready TypeScript patterns to build solid and robust AI applications. Retry logic, circuit breakers, rate limiting, human-in-the-loop escalation, prompt versioning, response validation, context window management, and more—all with complete type
149 lines • 4.28 kB
JavaScript
;
/**
* Types for Human-in-the-Loop Pattern
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommonEscalationRules = exports.TimeoutFallback = exports.ReviewStatus = exports.EscalationReason = void 0;
/**
* Escalation reasons
*/
var EscalationReason;
(function (EscalationReason) {
/**
* AI confidence is too low
*/
EscalationReason["LOW_CONFIDENCE"] = "LOW_CONFIDENCE";
/**
* Operation timed out
*/
EscalationReason["TIMEOUT"] = "TIMEOUT";
/**
* Operation encountered an error
*/
EscalationReason["ERROR"] = "ERROR";
/**
* Sensitive content detected
*/
EscalationReason["SENSITIVE_CONTENT"] = "SENSITIVE_CONTENT";
/**
* Keyword detected in input/output
*/
EscalationReason["KEYWORD_DETECTED"] = "KEYWORD_DETECTED";
/**
* Manual escalation requested
*/
EscalationReason["MANUAL_REQUEST"] = "MANUAL_REQUEST";
/**
* Custom escalation reason
*/
EscalationReason["CUSTOM"] = "CUSTOM";
})(EscalationReason || (exports.EscalationReason = EscalationReason = {}));
/**
* Review status
*/
var ReviewStatus;
(function (ReviewStatus) {
/**
* Review is pending
*/
ReviewStatus["PENDING"] = "PENDING";
/**
* Review approved
*/
ReviewStatus["APPROVED"] = "APPROVED";
/**
* Review rejected
*/
ReviewStatus["REJECTED"] = "REJECTED";
/**
* Review modified
*/
ReviewStatus["MODIFIED"] = "MODIFIED";
})(ReviewStatus || (exports.ReviewStatus = ReviewStatus = {}));
/**
* Timeout fallback behavior
*/
var TimeoutFallback;
(function (TimeoutFallback) {
/**
* Use AI output on timeout
*/
TimeoutFallback["USE_AI"] = "USE_AI";
/**
* Throw error on timeout
*/
TimeoutFallback["THROW"] = "THROW";
/**
* Retry escalation on timeout
*/
TimeoutFallback["RETRY"] = "RETRY";
})(TimeoutFallback || (exports.TimeoutFallback = TimeoutFallback = {}));
/**
* Common escalation rules for typical scenarios
*/
exports.CommonEscalationRules = {
/**
* Escalate if confidence is low
*/
lowConfidence: (threshold = 0.7) => ({
name: "low-confidence",
shouldEscalate: (_, output) => output?.confidence !== undefined && output.confidence < threshold,
reason: EscalationReason.LOW_CONFIDENCE,
priority: 5,
}),
/**
* Escalate if sensitive keywords detected in input or output
*/
sensitiveKeywords: (keywords) => ({
name: "sensitive-keywords",
shouldEscalate: (input, output) => {
// Check input if it's a string
if (typeof input === 'string') {
const inputLower = input.toLowerCase();
if (keywords.some((keyword) => inputLower.includes(keyword.toLowerCase()))) {
return true;
}
}
// Check output if it exists
if (output) {
// Check all string properties of output
const checkObject = (obj) => {
for (const value of Object.values(obj)) {
if (typeof value === 'string') {
const valueLower = value.toLowerCase();
if (keywords.some((keyword) => valueLower.includes(keyword.toLowerCase()))) {
return true;
}
}
}
return false;
};
if (typeof output === 'object') {
return checkObject(output);
}
}
return false;
},
reason: EscalationReason.KEYWORD_DETECTED,
priority: 10,
}),
/**
* Escalate on error
*/
onError: () => ({
name: "on-error",
shouldEscalate: (_, __, error) => error !== undefined,
reason: EscalationReason.ERROR,
priority: 8,
}),
/**
* Custom escalation rule
*/
custom: (name, fn, priority = 5) => ({
name,
shouldEscalate: fn,
reason: EscalationReason.CUSTOM,
priority,
}),
};
//# sourceMappingURL=human-in-the-loop.js.map