UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

46 lines (45 loc) • 3.06 kB
/** * The `Tool(content)` rule SYNTAX — the one reader every rule face shares, as a LEAF module. * * Two modules read this grammar and, until this file existed, one imported it from the other: the rule * DSL compiler (`permission-rules.ts`) declared the parser and the persisted-rule model * (`permission-rule-model.ts`) imported it. The model's command/path grammars are now consumed by the * compiler too (a content-form `deny`/`ask` rule compiles through the model's own validator and * matcher), which would have closed a value-import cycle between the two. The syntax layer has no * dependency of its own, so it lives here and both read DOWN. * * Upstream parity: `Lh` (the split), `Dju`/`Pju` (the escape-aware paren search), `Hju` (the three * unescapes) — INCLUDING the lenient downgrades (malformed parens / trailing garbage ⇒ the WHOLE string * becomes the bare tool name). The strict complement that refuses the downgrades is the compiler's * validator, not this reader. */ /** Parse result — the tool name and, for a `Tool(content)` spelling, its unescaped content. */ export interface ParsedPermissionRule { toolName: string; ruleContent?: string; } /** First index of `ch` preceded by an EVEN number of backslashes. */ export declare function indexOfUnescaped(s: string, ch: string): number; /** Last index of `ch` preceded by an EVEN number of backslashes. */ export declare function lastIndexOfUnescaped(s: string, ch: string): number; /** * Parse one rule string, including the lenient downgrades (malformed parens / trailing garbage ⇒ the * WHOLE string becomes the bare tool name). Use the compiler's `validatePermissionRules` (or the policy * factory) to refuse the downgrades. */ export declare function parsePermissionRule(rule: string): ParsedPermissionRule; /** * Can a rule name this tool name — is it the rule syntax's name head ({@link RULE_TOOL_NAME}) and not the lone `*`? The * lone `*` is the wildcard (every tool), not a name: a tool called `*` could never be granted or refused on its own. The * mount contract for every name a caller tool answers to (its name and each alias), so each one is a name every list can * write; a protocol-minted name (`<ns>__<peer>__<tool>`, segments in `[A-Za-z0-9_-]`) is always one. */ export declare function isNameableToolName(name: string): boolean; /** * One rule entry read by the rule syntax, or `undefined` when the syntax cannot read it: blanks around it, a tool name (the * text before the first unescaped `(`, as written) outside {@link RULE_TOOL_NAME}, or a `(` the parse cannot close at the * entry's end (the lenient parser's downgrade: the whole string read as a name); otherwise {@link parsePermissionRule}'s * reading. THE test of "this is a rule spelling" — every entry list's reader asks it before any rule grammar reads the * content (what the parentheses hold, a parenthesis or a control character included, is that grammar's to judge). */ export declare function ruleSpellingOf(rule: string): ParsedPermissionRule | undefined;