|
| 1 | +# HPL CLI Write Operations Implementation |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +Successfully implemented write operations for the HPL CLI to enable creating and updating Lab Notes via the API. |
| 6 | + |
| 7 | +## Changes Made |
| 8 | + |
| 9 | +### 1. Intent Definitions (`src/contract/intents.ts`) |
| 10 | +Added two new intent definitions for write operations: |
| 11 | +- `create_lab_note`: For creating new Lab Notes |
| 12 | +- `update_lab_note`: For updating existing Lab Notes |
| 13 | + |
| 14 | +Both intents: |
| 15 | +- Target scope: `["lab_notes", "remote_api"]` |
| 16 | +- Side effects: `["write_remote"]` |
| 17 | +- Reversible: `false` (write operations cannot be automatically undone) |
| 18 | + |
| 19 | +### 2. Exit Codes (`src/contract/exitCodes.ts`) |
| 20 | +Added two new exit codes: |
| 21 | +- `VALIDATION: 6` - For data validation failures |
| 22 | +- `IO: 7` - For file I/O errors |
| 23 | + |
| 24 | +### 3. HTTP Client (`src/http/client.ts`) |
| 25 | +Added `postJson<T>()` function: |
| 26 | +- Supports POST requests with JSON payloads |
| 27 | +- Handles Bearer token authentication |
| 28 | +- Follows existing error handling patterns with `HttpError` |
| 29 | +- Supports unwrapping envelope responses |
| 30 | + |
| 31 | +### 4. Create Command (`src/commands/notes/create.ts`) |
| 32 | +Implements `hpl notes create` subcommand: |
| 33 | + |
| 34 | +**Features:** |
| 35 | +- Required options: `--title`, `--slug` |
| 36 | +- Content input: `--markdown` (inline) or `--file` (from file) |
| 37 | +- Optional metadata: `--locale`, `--subtitle`, `--summary`, `--tags`, `--published`, `--status`, `--type`, `--dept` |
| 38 | +- Authentication via `HPL_TOKEN` environment variable or config file |
| 39 | +- Zod validation of payload before sending |
| 40 | +- JSON and human output modes |
| 41 | +- Proper error handling with specific exit codes |
| 42 | + |
| 43 | +**Example usage:** |
| 44 | +```bash |
| 45 | +# Create from file |
| 46 | +hpl notes create --title "New Note" --slug "new-note" --file ./note.md |
| 47 | + |
| 48 | +# Create with inline content |
| 49 | +hpl notes create --title "Quick Note" --slug "quick-note" --markdown "# Content here" |
| 50 | + |
| 51 | +# With metadata |
| 52 | +hpl notes create \ |
| 53 | + --title "Research Paper" \ |
| 54 | + --slug "ai-collaboration-patterns" \ |
| 55 | + --file ./paper.md \ |
| 56 | + --type paper \ |
| 57 | + --tags "ai,research,collaboration" \ |
| 58 | + --status published |
| 59 | +``` |
| 60 | + |
| 61 | +### 5. Update Command (`src/commands/notes/update.ts`) |
| 62 | +Implements `hpl notes update` subcommand: |
| 63 | + |
| 64 | +**Features:** |
| 65 | +- Positional argument: `<slug>` (note to update) |
| 66 | +- Optional fields: All create fields (except slug is argument not option) |
| 67 | +- Requires at least `--title` and (`--markdown` or `--file`) for full updates |
| 68 | +- Uses same upsert endpoint as create |
| 69 | +- Authentication via `HPL_TOKEN` |
| 70 | +- 404 handling for non-existent notes |
| 71 | +- JSON and human output modes |
| 72 | + |
| 73 | +**Example usage:** |
| 74 | +```bash |
| 75 | +# Update from file |
| 76 | +hpl notes update my-note --title "Updated Title" --file ./updated.md |
| 77 | + |
| 78 | +# Update specific fields |
| 79 | +hpl notes update my-note --status published --tags "updated,reviewed" |
| 80 | + |
| 81 | +# Full update with inline content |
| 82 | +hpl notes update my-note \ |
| 83 | + --title "Revised Note" \ |
| 84 | + --markdown "# New content" \ |
| 85 | + --summary "Updated summary" |
| 86 | +``` |
| 87 | + |
| 88 | +### 6. Command Wiring (`src/commands/notes/notes.ts`) |
| 89 | +Integrated new commands into the notes command tree: |
| 90 | +- Imported `notesCreateSubcommand` and `notesUpdateSubcommand` |
| 91 | +- Added both to the command tree (before sync) |
| 92 | + |
| 93 | +## API Contract |
| 94 | + |
| 95 | +Both commands use the existing `/lab-notes/upsert` endpoint with the `LabNoteUpsertPayload` schema: |
| 96 | + |
| 97 | +```typescript |
| 98 | +{ |
| 99 | + slug: string; // required |
| 100 | + title: string; // required |
| 101 | + markdown: string; // required |
| 102 | + locale?: string; |
| 103 | + subtitle?: string; |
| 104 | + summary?: string; |
| 105 | + tags?: string[]; |
| 106 | + published?: string; |
| 107 | + status?: "draft" | "published" | "archived"; |
| 108 | + type?: "labnote" | "paper" | "memo" | "lore" | "weather"; |
| 109 | + dept?: string; |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Authentication |
| 114 | + |
| 115 | +Both commands require authentication via: |
| 116 | +1. `HPL_TOKEN` environment variable, OR |
| 117 | +2. Token configured in `~/.humanpatternlab/hpl.json` |
| 118 | + |
| 119 | +Returns `E_AUTH` (exit code 4) if token is missing or invalid. |
| 120 | + |
| 121 | +## Output Modes |
| 122 | + |
| 123 | +Both commands support: |
| 124 | +- **Human mode** (default): Readable text output with status messages |
| 125 | +- **JSON mode** (`--json`): Structured JSON envelope following the existing contract |
| 126 | + |
| 127 | +## Error Handling |
| 128 | + |
| 129 | +Comprehensive error handling for: |
| 130 | +- Missing authentication (`E_AUTH`, exit 4) |
| 131 | +- File not found (`E_NOT_FOUND`, exit 3) |
| 132 | +- File I/O errors (`E_IO`, exit 7) |
| 133 | +- Validation errors (`E_VALIDATION`, exit 6) |
| 134 | +- Network errors (`E_NETWORK`, exit 10) |
| 135 | +- Server errors (`E_SERVER`, exit 11) |
| 136 | +- Unknown errors (`E_UNKNOWN`, exit 1) |
| 137 | + |
| 138 | +## Design Consistency |
| 139 | + |
| 140 | +Implementation follows all existing HPL CLI patterns: |
| 141 | +- ✅ Core function returns `{ envelope, exitCode }` |
| 142 | +- ✅ Commander adapter handles mode selection and rendering |
| 143 | +- ✅ Zod schema validation before API calls |
| 144 | +- ✅ Consistent error shapes with codes and messages |
| 145 | +- ✅ Proper use of intent descriptors |
| 146 | +- ✅ File headers with lab unit attribution |
| 147 | +- ✅ Support for both JSON and human output modes |
| 148 | +- ✅ Predictable exit codes for automation |
| 149 | + |
| 150 | +## Testing Recommendations |
| 151 | + |
| 152 | +Before deployment, test: |
| 153 | + |
| 154 | +1. **Create command:** |
| 155 | + ```bash |
| 156 | + # Test with file |
| 157 | + hpl notes create --title "Test" --slug "test-note" --file ./test.md |
| 158 | + |
| 159 | + # Test with inline content |
| 160 | + hpl notes create --title "Test" --slug "test-note-2" --markdown "# Test" |
| 161 | + |
| 162 | + # Test validation error |
| 163 | + hpl notes create --title "Test" --slug "test" # Missing content |
| 164 | + |
| 165 | + # Test auth error |
| 166 | + unset HPL_TOKEN |
| 167 | + hpl notes create --title "Test" --slug "test" --file ./test.md |
| 168 | + ``` |
| 169 | + |
| 170 | +2. **Update command:** |
| 171 | + ```bash |
| 172 | + # Test successful update |
| 173 | + hpl notes update test-note --title "Updated" --file ./updated.md |
| 174 | + |
| 175 | + # Test 404 |
| 176 | + hpl notes update nonexistent --title "Test" --markdown "Test" |
| 177 | + |
| 178 | + # Test JSON mode |
| 179 | + hpl notes update test-note --title "Test" --markdown "Test" --json |
| 180 | + ``` |
| 181 | + |
| 182 | +3. **JSON contract verification:** |
| 183 | + ```bash |
| 184 | + hpl notes create --title "Test" --slug "test" --markdown "Test" --json | \ |
| 185 | + node -e "JSON.parse(require('fs').readFileSync(0,'utf8'))" |
| 186 | + ``` |
| 187 | + |
| 188 | +## Next Steps |
| 189 | + |
| 190 | +Potential enhancements: |
| 191 | +1. Add `--partial` flag to update command for true partial updates (fetch existing + merge) |
| 192 | +2. Add `hpl notes delete <slug>` command using DELETE endpoint |
| 193 | +3. Add batch operations support |
| 194 | +4. Add interactive mode for creating notes with prompts |
| 195 | +5. Add validation for slug format (kebab-case, etc.) |
| 196 | +6. Add dry-run mode like sync has |
| 197 | + |
| 198 | +## Files Modified/Created |
| 199 | + |
| 200 | +**Modified:** |
| 201 | +- `src/contract/intents.ts` - Added create/update intents |
| 202 | +- `src/contract/exitCodes.ts` - Added VALIDATION and IO exit codes |
| 203 | +- `src/http/client.ts` - Added postJson function |
| 204 | +- `src/commands/notes/notes.ts` - Wired new commands |
| 205 | + |
| 206 | +**Created:** |
| 207 | +- `src/commands/notes/create.ts` - Create command implementation |
| 208 | +- `src/commands/notes/update.ts` - Update command implementation |
| 209 | + |
| 210 | +**Total changes:** 4 modified files, 2 new files |
0 commit comments