//#region extensions/crypto/src/services/policy-types.d.ts /** * Policy Engine — Type Definitions * * Defines the type system for user-defined spending policies, approval rules, * and autonomy bounds. Policies are checked before every write-tool execution. * * Design principles: * - Natural language in, structured rules out. User never writes JSON. * - Multi-turn confirmation: agent proposes interpretation, user approves. * Ambiguity → clarifying questions, never silent defaults. * - Full transparency: every policy shows the original NL description AND * the exact structured rules being enforced. No hidden interpretation. * - Rules map cleanly to EIP-7710 caveats for future on-chain enforcement. * - Most restrictive rule wins: block > confirm > allow. */ type PolicyStatus = 'draft' | 'active' | 'disabled'; interface Policy { id: string; name: string; description: string; rules: PolicyRule[]; scope: PolicyScope; status: PolicyStatus; /** Why it's a draft — what the agent still needs to clarify. */ pendingClarifications?: string[]; /** Timestamp when user explicitly confirmed the draft. Set by handleConfirm. */ confirmedAt?: number; createdAt: number; updatedAt: number; userId: string; /** * On-chain delegation metadata (EIP-7710). * Present when the policy has been compiled and signed as an on-chain delegation. * Contains chain ID, delegation hash, addresses, and lifecycle status. */ delegation?: DelegationInfo; } /** * On-chain delegation info stored with a policy. * Maps to DelegationMetadata in delegation-types.ts but uses plain types * here to avoid circular imports. The delegation service converts between them. */ interface DelegationInfo { /** Chain ID where the delegation is deployed. */ chainId: number; /** Keccak256 hash of the delegation struct. */ hash: string; /** DelegationManager contract address on this chain. */ delegationManager: string; /** Current lifecycle status. */ status: 'unsigned' | 'signed' | 'active' | 'revoked' | 'expired'; /** The delegate address (agent). */ delegate: string; /** The delegator address (user). */ delegator: string; /** Salt used for uniqueness. */ salt: string; /** ISO timestamp when the delegation was created. */ createdAt: string; /** ISO timestamp when the delegation expires. Null = no time-bound. */ expiresAt?: string; /** ISO timestamp when last status check was performed. */ lastCheckedAt?: string; /** Policy rules that couldn't map to on-chain caveats (app-layer only). */ unmappedRules?: string[]; } interface PolicyScope { type: 'all_write' | 'tools' | 'categories'; tools?: string[]; categories?: string[]; } type PolicyRule = SpendingLimitRule | RateLimitRule | AllowlistRule | BlocklistRule | TimeWindowRule | ApprovalThresholdRule | MaxAmountRule | Erc20LimitRule; /** Cumulative spending cap over a time period. */ interface SpendingLimitRule { type: 'spending_limit'; maxAmountUsd: number; period: 'hourly' | 'daily' | 'weekly' | 'monthly'; } /** Maximum number of tool calls in a time window. */ interface RateLimitRule { type: 'rate_limit'; maxCalls: number; periodMs: number; } /** Only allow interactions with specific tokens/chains/addresses. */ interface AllowlistRule { type: 'allowlist'; field: 'tokens' | 'chains' | 'addresses' | 'contracts'; values: string[]; } /** Block interactions with specific tokens/chains/addresses. */ interface BlocklistRule { type: 'blocklist'; field: 'tokens' | 'chains' | 'addresses' | 'contracts'; values: string[]; } /** Restrict tool execution to certain hours / days. */ interface TimeWindowRule { type: 'time_window'; allowedHours?: { start: number; end: number; }; allowedDays?: number[]; timezone?: string; } /** Require human confirmation above a USD amount. */ interface ApprovalThresholdRule { type: 'approval_threshold'; amountUsd: number; } /** Cap cumulative ERC-20 transfer amount for a specific token. */ interface Erc20LimitRule { type: 'erc20_limit'; token: string; maxAmount: string; decimals: number; } /** Hard block on any single transaction above a USD amount. */ interface MaxAmountRule { type: 'max_amount'; maxAmountUsd: number; } type PolicyAction = 'allow' | 'confirm' | 'block'; interface PolicyDecision { action: PolicyAction; reason?: string; policyId?: string; policyName?: string; ruleSummary?: string; } /** Context about the action being evaluated. */ interface ActionContext { toolName: string; action?: string; amountUsd?: number; token?: string; chain?: number; toAddress?: string; userId: string; } interface UsageEntry { timestamp: number; toolName: string; action?: string; amountUsd?: number; } interface PolicyUsage { policyId: string; entries: UsageEntry[]; } /** * Human-readable rendering of a policy for user verification. * Both the NL description and the structured interpretation are shown * so there is zero ambiguity about what's enforced. */ interface PolicyDisplay { name: string; status: PolicyStatus; /** The user's original words. */ description: string; /** Plain-English rendering of each structured rule. */ ruleDescriptions: string[]; /** Which tools are affected. */ scopeDescription: string; /** Current period usage vs limits (for spending/rate limits). */ usageSummary?: string; } /** * Maps category names to the write tools they contain. * Every tool in WRITE_TOOL_NAMES should be in exactly one category. */ declare const TOOL_CATEGORIES: Record; /** Human-readable category labels. */ declare const CATEGORY_LABELS: Record; /** Reverse lookup: tool name → category. */ declare const TOOL_TO_CATEGORY: Record; /** Convert a named period to milliseconds. Throws on unknown periods. */ declare function periodToMs(period: string): number; /** Render a single rule as unambiguous plain English. */ declare function describeRule(rule: PolicyRule): string; /** Render a scope as plain English. */ declare function describeScope(scope: PolicyScope): string; type PolicyMode = 'delegation' | 'simple'; /** Get the current policy enforcement mode. */ declare function getPolicyMode(): PolicyMode; /** Set the policy enforcement mode. Persists to disk. */ declare function setPolicyMode(mode: PolicyMode): void; /** Check if delegation features are enabled (mode === 'delegation'). */ declare function isDelegationMode(): boolean; /** Reset cached mode (for testing). */ declare function resetPolicyMode(): void; //#endregion export { ActionContext, AllowlistRule, ApprovalThresholdRule, BlocklistRule, CATEGORY_LABELS, DelegationInfo, Erc20LimitRule, MaxAmountRule, Policy, PolicyAction, PolicyDecision, PolicyDisplay, PolicyMode, PolicyRule, PolicyScope, PolicyStatus, PolicyUsage, RateLimitRule, SpendingLimitRule, TOOL_CATEGORIES, TOOL_TO_CATEGORY, TimeWindowRule, UsageEntry, describeRule, describeScope, getPolicyMode, isDelegationMode, periodToMs, resetPolicyMode, setPolicyMode }; //# sourceMappingURL=policy-types.d.mts.map