UNPKG

@lit-protocol/vincent-scaffold-sdk

Version:

Shared build configuration and utilities for Vincent abilities and policies

214 lines (155 loc) โ€ข 7.12 kB
# Vincent Framework: AI Development Guidelines ## Overview The Vincent Scaffold SDK is a specialized framework for creating **Vincent Abilities** and **Vincent Policies** that execute on Lit Actions - a blockchain-based execution environment with strict constraints. This document provides essential guidance for AI agents working with Vincent projects. ## ๐Ÿšจ CRITICAL CONSTRAINTS ### Lit Actions Environment Limitations Vincent abilities and policies execute in a restricted environment: - **โŒ NO `globalThis`** - Standard global objects are not available - **โŒ NO `process.env`** - Environment variables cannot be accessed - **โŒ NO persistent memory** - State doesn't persist between executions - **โŒ NO file system access** - Cannot read/write files during execution - **โŒ NO standard Node.js APIs** - Limited runtime environment ### Forbidden Patterns - **NEVER** use mock or fake data in implementations - **NEVER** assume environment variables are available in abilities/policies - **NEVER** rely on persistent state within the execution context - **NEVER** use standard Node.js modules in ability/policy logic ## ๐Ÿ—๏ธ Vincent Architecture ### Core Components 1. **Vincent Abilities** - Executable actions (e.g., token transfers, API calls) 2. **Vincent Policies** - Governance rules that control ability execution 3. **Lit Actions** - Secure execution environment for abilities/policies 4. **E2E Testing Framework** - Integrated testing with blockchain simulation ### Three-Phase Execution Model #### Abilities 1. **Precheck** - Validate inputs (runs outside Lit Actions) 2. **Execute** - Perform the operation (runs in Lit Actions) #### Policies 1. **Precheck** - Early validation (runs outside Lit Actions) 2. **Evaluate** - Runtime checks (runs in Lit Actions) 3. **Commit** - Record state changes (runs in Lit Actions) ## ๐Ÿ“‹ Schema-Driven Development ### Required Patterns All Vincent components MUST use Zod schemas for type safety: ```typescript // Ability parameters export const abilityParamsSchema = z.object({ to: z.string().regex(/^0x[a-fA-F0-9]{40}$/, "Invalid address"), amount: z.string().regex(/^\d*\.?\d+$/, "Invalid amount") }); // Result schemas for each phase export const precheckSuccessSchema = z.object({...}); export const precheckFailSchema = z.object({...}); export const executeSuccessSchema = z.object({...}); export const executeFailSchema = z.object({...}); ``` ### Schema Validation Rules - Define schemas BEFORE implementation - Use descriptive error messages - Export type definitions: `export type AbilityParams = z.infer<typeof abilityParamsSchema>;` - Validate ALL inputs and outputs ## ๐Ÿ”ง laUtils API Usage ### Available APIs This are available in the: - execute() hook inside the vincent policy - evaluate() hook inside the vincent ability ```typescript import { laUtils } from "@lit-protocol/vincent-scaffold-sdk/la-utils"; laUtils.transaction.handler.contractCall(); // Execute contract calls laUtils.transaction.handler.nativeSend(); // Send native tokens laUtils.helpers.toEthAddress(); // Address utilities ``` ### Usage Constraints - **โœ… CAN use** in ability `execute` hooks - **โœ… CAN use** in policy `evaluate` and `commit` hooks - **โŒ CANNOT use** in `precheck` hooks (not in Lit Actions context) ## ๐Ÿ—๏ธ Project Structure ### Directory Layout ``` vincent-packages/ โ”œโ”€โ”€ abilities/ โ”‚ โ””โ”€โ”€ my-ability/ โ”‚ โ”œโ”€โ”€ src/lib/ โ”‚ โ”‚ โ”œโ”€โ”€ schemas.ts โ”‚ โ”‚ โ”œโ”€โ”€ vincent-ability.ts โ”‚ โ”‚ โ””โ”€โ”€ helpers/index.ts โ”‚ โ”œโ”€โ”€ package.json โ”‚ โ””โ”€โ”€ .gitignore โ””โ”€โ”€ policies/ โ””โ”€โ”€ my-policy/ โ”œโ”€โ”€ src/lib/ โ”‚ โ”œโ”€โ”€ schemas.ts โ”‚ โ”œโ”€โ”€ vincent-policy.ts โ”‚ โ””โ”€โ”€ helpers/index.ts โ”œโ”€โ”€ package.json โ””โ”€โ”€ .gitignore ``` ## ๐Ÿ› ๏ธ Development Workflow ### Creating New Abilities/Policies 1. Use CLI: `npx @lit-protocol/vincent-scaffold-sdk add ability my-ability` 2. CD into the ability or policy directory 3. Update schemas in `src/lib/schemas.ts` 4. Implement logic in `src/lib/vincent-ability.ts` or `src/lib/vincent-policy.ts` 5. Add helpers in `src/lib/helpers/index.ts` if needed 6. Update root `package.json` build script 7. Build: `npm run vincent:build` 8. Test: `npm run vincent:e2e` ## ๐Ÿงช Testing & Validation ### E2E Testing Pattern ```typescript // Import built abilities/policies import { bundledVincentAbility } from "../../vincent-packages/abilities/my-ability/dist/index.js"; import { vincentPolicyMetadata } from "../../vincent-packages/policies/my-policy/dist/index.js"; // Register and test const result = await chainClient.executeAbilities({ abilities: [myAbilityConfig], // ... test configuration }); ``` ### Test Commands ```bash npm run vincent:hardreset # Reset all state and rebuild npm run vincent:build # Build all abilities and policies npm run vincent:e2e:reset # Reset E2E test state only npm run vincent:e2e # Run native transfer E2E tests npm run vincent:e2e:erc20 # Run ERC-20 transfer E2E tests ``` ## ๐ŸŽฏ Best Practices ### Code Quality - Follow existing code patterns exactly - Use TypeScript strictly - Implement comprehensive error handling - Add detailed logging for debugging - Maintain consistent naming conventions ### Security - Validate all inputs with Zod schemas - Never expose sensitive data in logs - Use proper error messages without revealing internals - Follow principle of least privilege ### Performance - Minimize computation in Lit Actions - Use efficient data structures - Cache expensive operations when possible - Keep ability/policy logic focused and minimal ## ๐Ÿšซ Common Pitfalls 1. **Using forbidden APIs** - Check Lit Actions constraints 2. **Missing schema validation** - Always validate inputs/outputs 3. **Forgetting build script updates** - New abilities won't build 4. **Incorrect import paths** - Use relative paths to dist/ 5. **Mock data usage** - Never use fake data in implementations 6. **Environment variable access** - Not available in Lit Actions 7. **State persistence assumptions** - Memory doesn't persist ## ๐Ÿ“š Reference Implementation Always refer to existing working examples: - **Ability example**: `vincent-packages/abilities/native-send/` - **Policy example**: `vincent-packages/policies/send-counter-limit/` - **E2E tests**: `vincent-e2e/src/e2e.ts` - **Template files**: `src/templates/ability/` and `src/templates/policy/` ## ๐Ÿ†˜ When You Need Help If implementation details are unclear: 1. **DO NOT** create mock or placeholder data 2. **DO NOT** make assumptions about missing information 3. **DO** ask for specific clarification about requirements 4. **DO** provide concrete examples of what information is needed 5. **DO** suggest proper solutions rather than workarounds Remember: Vincent development requires precision due to blockchain execution constraints. When in doubt, ask for clarification rather than implementing potentially incorrect solutions.