WIP: A simple cli for daily tangled use cases and AI integration. This is for my personal use right now, but happy if others get mileage from it! :)
at main 168 lines 5.8 kB view raw view rendered
1# Generated Lexicon Code 2 3This directory contains automatically generated TypeScript code from AT Protocol lexicon definitions for the Tangled.org service. 4 5## ⚠️ Do Not Edit 6 7**IMPORTANT**: All files in this directory are generated by `@atproto/lex-cli` and should not be manually edited. Any changes will be overwritten the next time code generation runs. 8 9## Source 10 11The lexicon definitions are vendored in `/lexicons/` directory from: 12- **Source**: `https://tangled.org/tangled.org/core/raw/master/lexicons/` 13- **Repository**: `https://tangled.org/tangled.org/core` 14 15## Code Generation 16 17To regenerate this code after updating lexicons: 18 19```bash 20npm run codegen 21``` 22 23This command: 241. Runs `@atproto/lex-cli` to generate TypeScript types from lexicons 252. Automatically fixes ES module imports by adding `.js` extensions 263. Rewrites `multiformats/cid` imports to use the main package export 27 28## Dependencies 29 30The generated code requires: 31- `@atproto/api` - AT Protocol client library 32- `@atproto/lexicon` - Lexicon validation runtime 33- `multiformats` - CID (Content Identifier) support for content addressing 34 35These are already included in `package.json` dependencies. 36 37**Note on multiformats**: The code generator produces `import { CID } from 'multiformats/cid'` which TypeScript NodeNext module resolution cannot find. Our post-processing script (`scripts/fix-lexicon-imports.js`) automatically rewrites this to `import { CID } from 'multiformats'`, which uses the main package export and resolves correctly. 38 39## Using Generated Types 40 41### Recommended Approach 42 43**Define your own interfaces** based on the lexicon schemas rather than importing from generated code. This avoids TypeScript compatibility issues with the generated code. 44 45Example: 46 47```typescript 48// src/lib/issues-api.ts 49/** 50 * Issue record type based on sh.tangled.repo.issue lexicon 51 * @see lexicons/sh/tangled/issue/issue.json 52 */ 53export interface IssueRecord { 54 $type: 'sh.tangled.repo.issue'; 55 repo: string; 56 title: string; 57 body?: string; 58 createdAt: string; 59 mentions?: string[]; 60 references?: string[]; 61 [key: string]: unknown; // Required for AT Protocol record compatibility 62} 63``` 64 65### Direct Import (Not Recommended) 66 67If you must import directly from generated code: 68 69```typescript 70import type { Record as IssueRecord } from './lexicon/types/sh/tangled/repo/issue.js'; 71``` 72 73**Note**: Direct imports may cause TypeScript compilation issues. The generated code is excluded from TypeScript compilation (`tsconfig.json` excludes `src/lexicon/**`) to avoid compatibility problems. 74 75## Known Issues 76 77The generated code has some compatibility issues with strict TypeScript configurations: 78 791. **Unused imports**: Generated code imports types that aren't always used (CID, BlobRef, $Typed, OmitKey) 802. **Property compatibility**: `needsCbor` properties may not match current `@atproto/lexicon` types 81 82These issues are inherent to the code generator. We work around them by: 83- Excluding `src/lexicon/**` from TypeScript compilation in `tsconfig.json` 84- Defining our own interfaces based on lexicon schemas for use in application code 85- Post-processing generated files to fix module resolution issues 86 87## Post-Processing 88 89After generation, `scripts/fix-lexicon-imports.js` automatically: 90- Adds `.js` extensions to relative imports for ES module compatibility 91- Rewrites `import { CID } from 'multiformats/cid'` to `import { CID } from 'multiformats'` 92- Ensures proper module resolution with TypeScript NodeNext 93 94## Structure 95 96``` 97src/lexicon/ 98├── README.md # This file 99├── index.ts # Main exports (generated) 100├── lexicons.ts # Lexicon schemas (generated) 101├── util.ts # Utility types (generated) 102└── types/ 103 └── sh/ 104 └── tangled/ 105 ├── repo/ 106 │ ├── issue.ts # Issue record types 107 │ ├── pull.ts # Pull request types 108 │ └── ... 109 ├── actor/ 110 │ └── profile.ts 111 ├── feed/ 112 ├── graph/ 113 └── ... 114``` 115 116## Updating Lexicons 117 118When Tangled.org updates their lexicon schemas: 119 1201. **Fetch latest lexicons**: 121 ```bash 122 npm run update-lexicons 123 ``` 124 1252. **Regenerate TypeScript code**: 126 ```bash 127 npm run codegen 128 ``` 129 1303. **Review changes**: 131 - Check `git diff` to see what changed 132 - Update any custom interfaces if lexicon schemas changed 133 - Run tests: `npm test` 134 - Verify TypeScript compiles: `npm run typecheck` 135 1364. **Commit both lexicon and generated code changes**: 137 ```bash 138 git add lexicons/ src/lexicon/ 139 git commit -m "Update Tangled lexicons and regenerate client code" 140 ``` 141 142## Troubleshooting 143 144### TypeScript Errors 145 146If you see TypeScript errors from `src/lexicon/**`, this is expected. The generated code is excluded from compilation. As long as your application code (outside `src/lexicon/`) compiles, you're good. 147 148### Build Failures 149 150If the build fails after regenerating lexicons: 151 1521. Check if any lexicon schemas changed structure 1532. Update your custom interfaces to match new schemas 1543. Ensure `scripts/fix-lexicon-imports.js` ran successfully 155 156### Import Errors 157 158If you see module resolution errors: 159 1601. Ensure imports use `.js` extensions 1612. Run `npm run codegen` to regenerate and fix imports 1623. Check that `multiformats` is installed: `npm ls multiformats` 163 164## Reference 165 166- [AT Protocol Lexicon Specification](https://atproto.com/specs/lexicon) 167- [Tangled.org Core Repository](https://tangled.org/tangled.org/core) 168- [@atproto/lex-cli Documentation](https://github.com/bluesky-social/atproto/tree/main/packages/lex-cli)