@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
229 lines (228 loc) • 6.02 kB
TypeScript
/**
* 策略配置主结构
*/
export interface StrategyConfig<T = Record<string, any>> {
/** 元信息 */
metadata: StrategyMetadata<T>;
/** 条件组(可嵌套,每层可绑定动作) */
conditions: ConditionGroup;
/** 动作列表(带唯一ID) */
actions: ActionEffect[];
/** 展示配置(可选) */
display?: DisplayConfig;
}
/**
* 策略元信息
*/
export interface StrategyMetadata<T = Record<string, any>> {
/** 策略ID */
id: string;
/** 策略名称 */
name: string | Record<string, string>;
/** 策略类型(完全可扩展) */
type: string;
/** 计算类型 */
calculationType?: 'single' | 'cumulative';
/** 版本 */
version?: string;
/** 描述 */
description?: string | Record<string, string>;
/** 自定义元数据 */
custom?: T;
}
/**
* 条件组(支持嵌套)
*/
export interface ConditionGroup {
/** 逻辑运算符 */
operator: 'and' | 'or' | 'not';
/** 条件规则列表(可以是单条件或嵌套的条件组) */
rules: Array<ConditionRule | ConditionGroup>;
/** 关联的动作ID列表(必填,可以为空数组) */
actionIds: string[];
}
/**
* 条件规则(原子条件)
* 支持 code 模式使用 eval 执行
*/
export interface ConditionRule {
/** 类型 */
type: 'code' | 'operator';
/** 代码(code模式使用,使用eval执行) */
code?: string;
/** 维度(完全可扩展) */
dimension?: string;
/** 字段名 */
field?: string;
/** 运算符 */
operator?: string;
/** 比较值 */
value?: any;
/** 值类型 */
valueType?: 'number' | 'string' | 'boolean' | 'array' | 'object' | 'date';
/** 值单位 */
valueUnit?: string;
/** 额外配置 */
config?: Record<string, any>;
}
/**
* 动作效果
*/
export interface ActionEffect {
/** 动作的唯一标识 */
id: string;
/** 效果类型(完全可扩展) */
type: string;
/** 运算符(可选) */
operator?: string;
/** 值 */
value: any;
/** 值类型 */
valueType?: 'number' | 'string' | 'boolean' | 'array' | 'object';
/** 值单位 */
valueUnit?: string;
/** 作用目标 */
target: string;
/** 优先级(数字越大优先级越高) */
priority?: number;
/** 配置(完全可扩展) */
config?: Record<string, any>;
}
/**
* 展示配置
*/
export interface DisplayConfig {
/** 商品卡片展示 */
productCard?: {
text: string | Record<string, string>;
type: 'tag' | 'badge' | 'label';
image?: string;
style?: Record<string, any>;
};
/** 详情页展示 */
detail?: {
title: string | Record<string, string>;
description: string | Record<string, string>;
image?: string;
};
/** 自定义展示配置 */
custom?: Record<string, any>;
}
/**
* 运行时上下文结构
*/
export interface RuntimeContext {
/** 实体数据(完全开放,由业务层定义) */
entities: Record<string, any>;
/** 属性数据(扁平化的计算值,用于条件判断) */
attributes: Record<string, any>;
/** 上下文元数据 */
metadata: ContextMetadata;
}
/**
* 上下文元数据
*/
export interface ContextMetadata {
/** 请求ID(用于追踪) */
requestId?: string;
/** 时间戳 */
timestamp: number;
/** 语言 */
locale?: string;
/** 自定义元数据 */
custom?: Record<string, any>;
}
/**
* 评估结果结构
*/
export interface EvaluationResult {
/** 是否成功执行(引擎层面) */
success: boolean;
/** 策略是否适用(业务层面) */
applicable: boolean;
/** 结果码 */
code: string;
/** 消息(多语言key或直接文本) */
message?: string;
/** 匹配信息 */
matched: MatchedInfo;
/** 匹配的动作列表(按priority排序) */
matchedActions: ActionEffect[];
/** 输出结果(完全开放,由适配器定义) */
outputs: Record<string, any>;
/** 执行轨迹(可选,用于调试) */
trace?: ExecutionTrace;
/** 策略配置 */
config: StrategyConfig;
}
/**
* 匹配信息
*/
export interface MatchedInfo {
/** 条件是否满足 */
conditions: boolean;
/** 收集到的 actionIds */
actionIds: string[];
/** 详细匹配信息 */
details: Record<string, any>;
}
/**
* 执行轨迹
*/
export interface ExecutionTrace {
/** 执行步骤 */
steps: TraceStep[];
/** 总耗时(毫秒) */
duration: number;
/** 错误信息 */
errors?: Array<{
step: string;
error: string;
timestamp: number;
}>;
}
/**
* 追踪步骤
*/
export interface TraceStep {
/** 步骤名称 */
step: string;
/** 步骤状态 */
status: 'success' | 'failed' | 'skipped';
/** 耗时(毫秒) */
duration: number;
/** 详细信息 */
details?: Record<string, any>;
}
/** 成功码 */
export declare const SUCCESS_CODES: {
readonly SUCCESS: "SUCCESS";
};
/** 不适用码 */
export declare const NOT_APPLICABLE_CODES: {
readonly CONDITION_NOT_MET: "CONDITION_NOT_MET";
};
/** 错误码 */
export declare const ERROR_CODES: {
readonly INVALID_CONFIG: "INVALID_CONFIG";
readonly INVALID_CONTEXT: "INVALID_CONTEXT";
readonly EVALUATION_ERROR: "EVALUATION_ERROR";
readonly UNKNOWN_ERROR: "UNKNOWN_ERROR";
};
/**
* 引擎配置选项
*/
export interface EngineOptions {
/** 是否启用调试模式 */
debug?: boolean;
/** 是否启用追踪 */
enableTrace?: boolean;
/** 运算符处理器映射 */
operatorHandlers?: Record<string, OperatorHandler>;
/** 自定义错误处理器 */
errorHandler?: (error: Error, context: RuntimeContext) => void;
}
/**
* 运算符处理器
*/
export type OperatorHandler = (fieldValue: any, compareValue: any, rule: ConditionRule) => boolean;