2026-04-02 00:14:43 +08:00
|
|
|
import {Command, CommandSchema, parseCommand, parseCommandSchema, applyCommandSchema} from "../utils/command";
|
2026-04-01 22:20:38 +08:00
|
|
|
|
2026-04-01 23:58:07 +08:00
|
|
|
export type RuleState = 'running' | 'yielded' | 'waiting' | 'invoking' | 'done';
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
export type SchemaYield = { type: 'schema'; value: string | CommandSchema };
|
|
|
|
|
export type InvokeYield = { type: 'invoke'; rule: string; command: Command };
|
|
|
|
|
export type RuleYield = SchemaYield | InvokeYield;
|
2026-04-01 22:20:38 +08:00
|
|
|
|
|
|
|
|
export type RuleContext<T = unknown> = {
|
|
|
|
|
type: string;
|
|
|
|
|
schema?: CommandSchema;
|
2026-04-01 23:58:07 +08:00
|
|
|
generator: Generator<RuleYield, T, Command | RuleContext<unknown>>;
|
2026-04-01 22:20:38 +08:00
|
|
|
parent?: RuleContext<unknown>;
|
|
|
|
|
children: RuleContext<unknown>[];
|
|
|
|
|
state: RuleState;
|
2026-04-01 13:36:16 +08:00
|
|
|
resolution?: T;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
export type RuleDef<T = unknown, H extends RuleEngineHost = RuleEngineHost> = {
|
2026-04-01 22:20:38 +08:00
|
|
|
schema: CommandSchema;
|
2026-04-02 00:44:29 +08:00
|
|
|
create: (this: H, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>;
|
2026-04-01 22:20:38 +08:00
|
|
|
};
|
2026-04-01 17:34:21 +08:00
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
export type RuleRegistry = Map<string, RuleDef<unknown, RuleEngineHost>>;
|
|
|
|
|
|
|
|
|
|
export type RuleEngineHost = {
|
|
|
|
|
rules: RuleRegistry;
|
|
|
|
|
ruleContexts: RuleContext<unknown>[];
|
|
|
|
|
addRuleContext: (ctx: RuleContext<unknown>) => void;
|
|
|
|
|
removeRuleContext: (ctx: RuleContext<unknown>) => void;
|
|
|
|
|
};
|
2026-04-01 21:44:20 +08:00
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
export function createRule<T, H extends RuleEngineHost = RuleEngineHost>(
|
2026-04-01 22:20:38 +08:00
|
|
|
schemaStr: string,
|
2026-04-02 00:44:29 +08:00
|
|
|
fn: (this: H, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>
|
|
|
|
|
): RuleDef<T, H> {
|
2026-04-01 22:20:38 +08:00
|
|
|
return {
|
2026-04-01 22:31:07 +08:00
|
|
|
schema: parseCommandSchema(schemaStr, ''),
|
2026-04-02 00:44:29 +08:00
|
|
|
create: fn as RuleDef<T, H>['create'],
|
2026-04-01 22:20:38 +08:00
|
|
|
};
|
|
|
|
|
}
|
2026-04-01 21:44:20 +08:00
|
|
|
|
2026-04-01 22:20:38 +08:00
|
|
|
function parseYieldedSchema(value: string | CommandSchema): CommandSchema {
|
|
|
|
|
if (typeof value === 'string') {
|
2026-04-01 22:31:07 +08:00
|
|
|
return parseCommandSchema(value, '');
|
2026-04-01 22:20:38 +08:00
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2026-04-01 21:44:20 +08:00
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
function addContextToHost(host: RuleEngineHost, ctx: RuleContext<unknown>) {
|
|
|
|
|
host.addRuleContext(ctx);
|
2026-04-01 23:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
function discardChildren(host: RuleEngineHost, parent: RuleContext<unknown>) {
|
2026-04-01 22:20:38 +08:00
|
|
|
for (const child of parent.children) {
|
2026-04-02 00:44:29 +08:00
|
|
|
host.removeRuleContext(child);
|
2026-04-01 22:20:38 +08:00
|
|
|
}
|
|
|
|
|
parent.children = [];
|
|
|
|
|
parent.state = 'yielded';
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
function commandMatchesSchema(command: Command, schema: CommandSchema): boolean {
|
2026-04-01 22:31:07 +08:00
|
|
|
const requiredParams = schema.params.filter(p => p.required);
|
|
|
|
|
const variadicParam = schema.params.find(p => p.variadic);
|
|
|
|
|
|
|
|
|
|
if (command.params.length < requiredParams.length) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!variadicParam && command.params.length > schema.params.length) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 08:29:40 +08:00
|
|
|
const requiredOptions = Object.values(schema.options).filter(o => o.required);
|
2026-04-01 22:31:07 +08:00
|
|
|
for (const opt of requiredOptions) {
|
|
|
|
|
const hasOption = opt.name in command.options || (opt.short && opt.short in command.options);
|
|
|
|
|
if (!hasOption) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
function applySchemaToCommand(command: Command, schema: CommandSchema): Command {
|
|
|
|
|
return applyCommandSchema(command, schema).command;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findYieldedContext(contexts: RuleContext<unknown>[]): RuleContext<unknown> | undefined {
|
|
|
|
|
for (let i = contexts.length - 1; i >= 0; i--) {
|
|
|
|
|
const ctx = contexts[i];
|
|
|
|
|
if (ctx.state === 'yielded') {
|
|
|
|
|
return ctx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createContext<T>(
|
2026-04-01 22:20:38 +08:00
|
|
|
command: Command,
|
2026-04-02 00:44:29 +08:00
|
|
|
ruleDef: RuleDef<T>,
|
|
|
|
|
host: RuleEngineHost,
|
|
|
|
|
parent?: RuleContext<unknown>
|
|
|
|
|
): RuleContext<T> {
|
|
|
|
|
return {
|
2026-04-01 22:20:38 +08:00
|
|
|
type: ruleDef.schema.name,
|
|
|
|
|
schema: undefined,
|
2026-04-02 00:44:29 +08:00
|
|
|
generator: ruleDef.create.call(host, command),
|
2026-04-01 22:20:38 +08:00
|
|
|
parent,
|
|
|
|
|
children: [],
|
|
|
|
|
state: 'running',
|
|
|
|
|
resolution: undefined,
|
2026-04-01 17:34:21 +08:00
|
|
|
};
|
2026-04-01 23:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
function handleGeneratorResult<T>(
|
|
|
|
|
host: RuleEngineHost,
|
|
|
|
|
ctx: RuleContext<T>,
|
|
|
|
|
result: IteratorResult<RuleYield, T>
|
2026-04-01 23:58:07 +08:00
|
|
|
): RuleContext<unknown> | undefined {
|
2026-04-01 22:20:38 +08:00
|
|
|
if (result.done) {
|
|
|
|
|
ctx.resolution = result.value;
|
|
|
|
|
ctx.state = 'done';
|
2026-04-02 00:44:29 +08:00
|
|
|
return resumeParentAfterChildComplete(host, ctx as RuleContext<unknown>);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const yielded = result.value;
|
|
|
|
|
if (yielded.type === 'invoke') {
|
|
|
|
|
const childRuleDef = host.rules.get(yielded.rule);
|
2026-04-01 23:58:07 +08:00
|
|
|
if (childRuleDef) {
|
|
|
|
|
ctx.state = 'invoking';
|
2026-04-02 00:44:29 +08:00
|
|
|
return invokeChildRule(host, yielded.rule, yielded.command, ctx as RuleContext<unknown>);
|
2026-04-01 23:58:07 +08:00
|
|
|
} else {
|
2026-04-02 08:29:40 +08:00
|
|
|
ctx.schema = parseYieldedSchema({ name: '', params: [], options: {}, flags: {} });
|
2026-04-01 23:58:07 +08:00
|
|
|
ctx.state = 'yielded';
|
|
|
|
|
}
|
2026-04-01 22:20:38 +08:00
|
|
|
} else {
|
2026-04-02 00:44:29 +08:00
|
|
|
ctx.schema = parseYieldedSchema(yielded.value);
|
2026-04-01 22:20:38 +08:00
|
|
|
ctx.state = 'yielded';
|
|
|
|
|
}
|
2026-04-01 17:34:21 +08:00
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stepGenerator<T>(
|
|
|
|
|
host: RuleEngineHost,
|
|
|
|
|
ctx: RuleContext<T>
|
|
|
|
|
): RuleContext<T> {
|
|
|
|
|
const result = ctx.generator.next();
|
|
|
|
|
const resumed = handleGeneratorResult(host, ctx, result);
|
|
|
|
|
if (resumed) return resumed as RuleContext<T>;
|
2026-04-01 17:34:21 +08:00
|
|
|
return ctx;
|
2026-04-01 13:36:16 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
function invokeChildRule<T>(
|
|
|
|
|
host: RuleEngineHost,
|
|
|
|
|
ruleName: string,
|
|
|
|
|
command: Command,
|
|
|
|
|
parent: RuleContext<unknown>
|
|
|
|
|
): RuleContext<T> {
|
|
|
|
|
const ruleDef = host.rules.get(ruleName)!;
|
|
|
|
|
const ctx = createContext(command, ruleDef, host, parent);
|
|
|
|
|
|
|
|
|
|
parent.children.push(ctx as RuleContext<unknown>);
|
|
|
|
|
addContextToHost(host, ctx as RuleContext<unknown>);
|
|
|
|
|
|
|
|
|
|
return stepGenerator(host, ctx) as RuleContext<T>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resumeParentAfterChildComplete(
|
|
|
|
|
host: RuleEngineHost,
|
|
|
|
|
childCtx: RuleContext<unknown>
|
|
|
|
|
): RuleContext<unknown> | undefined {
|
|
|
|
|
const parent = childCtx.parent;
|
|
|
|
|
if (!parent || parent.state !== 'invoking') return undefined;
|
|
|
|
|
|
|
|
|
|
parent.children = parent.children.filter(c => c !== childCtx);
|
|
|
|
|
|
|
|
|
|
const result = parent.generator.next(childCtx);
|
|
|
|
|
const resumed = handleGeneratorResult(host, parent, result);
|
|
|
|
|
if (resumed) return resumed;
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 23:58:07 +08:00
|
|
|
function invokeRule<T>(
|
2026-04-02 00:44:29 +08:00
|
|
|
host: RuleEngineHost,
|
2026-04-01 23:58:07 +08:00
|
|
|
command: Command,
|
|
|
|
|
ruleDef: RuleDef<T>,
|
|
|
|
|
parent?: RuleContext<unknown>
|
|
|
|
|
): RuleContext<T> {
|
2026-04-02 00:44:29 +08:00
|
|
|
const ctx = createContext(command, ruleDef, host, parent);
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
if (parent) {
|
2026-04-02 00:44:29 +08:00
|
|
|
discardChildren(host, parent);
|
2026-04-01 23:58:07 +08:00
|
|
|
parent.children.push(ctx as RuleContext<unknown>);
|
|
|
|
|
parent.state = 'waiting';
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
addContextToHost(host, ctx as RuleContext<unknown>);
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
return stepGenerator(host, ctx);
|
2026-04-01 23:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
function feedYieldedContext(
|
|
|
|
|
host: RuleEngineHost,
|
|
|
|
|
ctx: RuleContext<unknown>,
|
|
|
|
|
command: Command
|
|
|
|
|
): RuleContext<unknown> {
|
|
|
|
|
const typedCommand = applySchemaToCommand(command, ctx.schema!);
|
|
|
|
|
const result = ctx.generator.next(typedCommand);
|
|
|
|
|
const resumed = handleGeneratorResult(host, ctx, result);
|
|
|
|
|
return resumed ?? ctx;
|
|
|
|
|
}
|
2026-04-01 22:31:07 +08:00
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
export function dispatchCommand(host: RuleEngineHost, input: string): RuleContext<unknown> | undefined {
|
|
|
|
|
const command = parseCommand(input);
|
2026-04-01 22:31:07 +08:00
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
const matchedRule = host.rules.get(command.name);
|
|
|
|
|
if (matchedRule) {
|
|
|
|
|
const typedCommand = applySchemaToCommand(command, matchedRule.schema);
|
|
|
|
|
const parent = findYieldedContext(host.ruleContexts);
|
|
|
|
|
return invokeRule(host, typedCommand, matchedRule, parent);
|
2026-04-01 22:20:38 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 00:44:29 +08:00
|
|
|
for (let i = host.ruleContexts.length - 1; i >= 0; i--) {
|
|
|
|
|
const ctx = host.ruleContexts[i];
|
|
|
|
|
if (ctx.state === 'yielded' && ctx.schema && commandMatchesSchema(command, ctx.schema)) {
|
|
|
|
|
return feedYieldedContext(host, ctx, command);
|
2026-04-01 22:20:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
2026-04-01 17:34:21 +08:00
|
|
|
}
|