|
| 1 | +# AGENTS.md - OpenCode Personality Plugin |
| 2 | + |
| 3 | +Guidelines for AI agents working on this codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +An OpenCode plugin that adds configurable personality and mood systems to AI assistants. The plugin injects personality traits into the system prompt and manages a mood state machine that drifts over time. |
| 8 | + |
| 9 | +## Code Style |
| 10 | + |
| 11 | +- **TypeScript**: Strict mode enabled with `noUncheckedIndexedAccess` and `exactOptionalPropertyTypes` |
| 12 | +- **Formatting**: Use Prettier defaults (no config file needed) |
| 13 | +- **Imports**: Use `.js` extensions for local imports (ESM) |
| 14 | +- **Types**: Prefer explicit types for function parameters and return values |
| 15 | +- **Null handling**: Use `!` only when value is guaranteed; prefer guards |
| 16 | +- **Comments**: Avoid inline comments; use JSDoc for public APIs only |
| 17 | + |
| 18 | +## Architecture |
| 19 | + |
| 20 | +``` |
| 21 | +src/ |
| 22 | +├── index.ts # Plugin entry point, hooks registration |
| 23 | +├── types.ts # All type definitions |
| 24 | +├── config.ts # Config loading, merging, state management |
| 25 | +├── mood.ts # Mood drift, scoring functions |
| 26 | +├── prompt.ts # Personality prompt builder |
| 27 | +├── tools/ |
| 28 | +│ ├── setMood.ts # setMood tool definition |
| 29 | +│ └── savePersonality.ts # savePersonality tool definition |
| 30 | +└── commands/ |
| 31 | + ├── mood.ts # /mood command handler |
| 32 | + └── personality.ts # /personality command handler |
| 33 | +``` |
| 34 | + |
| 35 | +## Key Files |
| 36 | + |
| 37 | +| File | Purpose | |
| 38 | +|------|---------| |
| 39 | +| `src/types.ts` | All exported types - modify here for schema changes | |
| 40 | +| `src/config.ts` | Config precedence logic (global + project merge), file I/O | |
| 41 | +| `src/mood.ts` | Mood drift algorithm - uses seeded random for testing | |
| 42 | +| `src/prompt.ts` | Builds personality section for system prompt | |
| 43 | +| `src/index.ts` | Plugin hooks registration, main entry point | |
| 44 | + |
| 45 | +## Plugin Hooks Used |
| 46 | + |
| 47 | +| Hook | Purpose | |
| 48 | +|------|---------| |
| 49 | +| `experimental.chat.system.transform` | Inject personality into system prompt | |
| 50 | +| `experimental.session.compacting` | Preserve personality during compaction | |
| 51 | +| `event` | Drift mood after assistant responses | |
| 52 | +| `command.execute.before` | Handle `/mood` and `/personality` commands | |
| 53 | + |
| 54 | +## Testing Workflow |
| 55 | + |
| 56 | +1. Run `npm run typecheck` to verify types |
| 57 | +2. Run `npm run lint` to check code style |
| 58 | +3. Run `npm run build` to verify build |
| 59 | +4. Test in OpenCode by updating `opencode.json` to point to `src/index.ts` |
| 60 | + |
| 61 | +## Making Changes |
| 62 | + |
| 63 | +### Adding a New Mood Field |
| 64 | + |
| 65 | +1. Add type to `MoodDefinition` or `MoodConfig` in `src/types.ts` |
| 66 | +2. Add default value in `DEFAULT_MOODS` or `DEFAULT_MOOD_CONFIG` in `src/config.ts` |
| 67 | +3. Use the field in `src/mood.ts` or `src/prompt.ts` |
| 68 | +4. Update README config reference |
| 69 | + |
| 70 | +### Adding a New Command |
| 71 | + |
| 72 | +1. Create handler in `src/commands/` |
| 73 | +2. Import and wire up in `src/index.ts` `command.execute.before` hook |
| 74 | +3. Register in `opencode.json` command definitions |
| 75 | +4. Document in README |
| 76 | + |
| 77 | +### Adding a New Tool |
| 78 | + |
| 79 | +1. Create tool in `src/tools/` using `tool()` from `@opencode-ai/plugin` |
| 80 | +2. Import and add to `tool:` object in `src/index.ts` |
| 81 | +3. Document in README Tools section |
| 82 | + |
| 83 | +### Adding a New Hook |
| 84 | + |
| 85 | +1. Add implementation in `src/index.ts` return object |
| 86 | +2. Follow existing patterns for hook signatures |
| 87 | +3. Document behavior in README if user-facing |
| 88 | + |
| 89 | +## Release Process |
| 90 | + |
| 91 | +1. Update version in `package.json` |
| 92 | +2. Update `CHANGELOG.md` |
| 93 | +3. Commit: `git commit -m "chore: release vX.Y.Z"` |
| 94 | +4. Tag: `git tag vX.Y.Z` |
| 95 | +5. Push: `git push && git push --tags` |
| 96 | +6. GitHub Actions handles npm publish |
| 97 | + |
| 98 | +## Common Patterns |
| 99 | + |
| 100 | +### Config Loading |
| 101 | + |
| 102 | +```typescript |
| 103 | +const configResult = loadConfigWithPrecedence(directory) |
| 104 | +if (configResult.config === null) { |
| 105 | + // No config - return minimal hooks or no-op |
| 106 | + return {} |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Type Guards for Output |
| 111 | + |
| 112 | +```typescript |
| 113 | +const output = cmdOutput as { parts: Array<{ type: string; text: string }> } |
| 114 | +output.parts.push({ type: "text", text: "Message" }) |
| 115 | +``` |
| 116 | + |
| 117 | +### Mood State Normalization |
| 118 | + |
| 119 | +Always normalize state after loading to handle config changes: |
| 120 | + |
| 121 | +```typescript |
| 122 | +const normalized = normalizeState(state, defaultMood, moods) |
| 123 | +``` |
| 124 | + |
| 125 | +## Constraints |
| 126 | + |
| 127 | +- **Backward Compatible**: Accept old config formats, warn on deprecation |
| 128 | +- **No-op Safe**: Return empty hooks if config is missing |
| 129 | +- **Deterministic**: Use `mood.seed` for reproducible tests |
| 130 | +- **Minimal Dependencies**: Only `@opencode-ai/plugin` peer dependency |
0 commit comments