Skip to content

Commit 2b9a37a

Browse files
authored
Add Nix flake devshell with bun2nix (#10)
* docs: add AGENTS.md with coding guidelines and project info * chore(build): add nix flake devshell with bun2nix Add reproducible Nix-based development environment using flakes and bun2nix for managing JavaScript dependencies offline. Provides alternative to manual Bun installation with consistent tool versions and offline support.
1 parent 46d0605 commit 2b9a37a

9 files changed

Lines changed: 534 additions & 2 deletions

File tree

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
2626
.eslintcache
2727
.cache
2828
*.tsbuildinfo
29+
.direnv
2930

3031
# IntelliJ based IDEs
3132
.idea

AGENTS.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# AGENTS.md
2+
3+
This file contains essential information for agentic coding assistants working in this repository.
4+
5+
## Project Overview
6+
7+
**opencode-pty** is an OpenCode plugin that provides interactive PTY (pseudo-terminal) management. It enables AI agents to run background processes, send interactive input, and read output on demand. The plugin supports multiple concurrent PTY sessions with features like output buffering, regex filtering, and permission integration.
8+
9+
## Build/Lint/Test Commands
10+
11+
### Type Checking
12+
```bash
13+
bun run typecheck
14+
```
15+
Runs TypeScript compiler in no-emit mode to check for type errors.
16+
17+
### Testing
18+
```bash
19+
bun test
20+
```
21+
Runs all tests using Bun's test runner.
22+
23+
### Running a Single Test
24+
```bash
25+
bun test --match "test name pattern"
26+
```
27+
Use the `--match` flag with a regex pattern to run specific tests. For example:
28+
```bash
29+
bun test --match "spawn"
30+
```
31+
32+
### Linting
33+
No dedicated linter configured. TypeScript strict mode serves as the primary code quality gate.
34+
35+
## Code Style Guidelines
36+
37+
### Language and Environment
38+
- **Language**: TypeScript 5.x with ESNext target
39+
- **Runtime**: Bun (supports TypeScript directly)
40+
- **Module System**: ES modules with explicit `.ts` extensions in imports
41+
- **JSX**: React JSX syntax (if needed, though this project is primarily backend)
42+
43+
### TypeScript Configuration
44+
- Strict mode enabled (`strict: true`)
45+
- Additional strict flags: `noFallthroughCasesInSwitch`, `noUncheckedIndexedAccess`, `noImplicitOverride`
46+
- Module resolution: bundler mode
47+
- Verbatim module syntax (no semicolons required)
48+
49+
### Imports and Dependencies
50+
- Use relative imports with `.ts` extensions: `import { foo } from "../foo.ts"`
51+
- Import types explicitly: `import type { Foo } from "./types.ts"`
52+
- Group imports: external dependencies first, then internal
53+
- Avoid wildcard imports (`import * as foo`)
54+
55+
### Naming Conventions
56+
- **Variables/Functions**: camelCase (`processData`, `spawnSession`)
57+
- **Constants**: UPPER_CASE (`DEFAULT_LIMIT`, `MAX_LINE_LENGTH`)
58+
- **Types/Interfaces**: PascalCase (`PTYSession`, `SpawnOptions`)
59+
- **Classes**: PascalCase (`PTYManager`, `RingBuffer`)
60+
- **Enums**: PascalCase (`PTYStatus`)
61+
- **Files**: kebab-case for directories, camelCase for files (`spawn.ts`, `manager.ts`)
62+
63+
### Code Structure
64+
- **Functions**: Prefer arrow functions for tools, regular functions for utilities
65+
- **Async/Await**: Use throughout for all async operations
66+
- **Error Handling**: Throw descriptive Error objects, use try/catch for expected failures
67+
- **Logging**: Use `createLogger` from `../logger.ts` for consistent logging
68+
- **Tool Functions**: Use `tool()` wrapper with schema validation for all exported tools
69+
70+
### Schema Validation
71+
All tool functions must use schema validation:
72+
```typescript
73+
export const myTool = tool({
74+
description: "Brief description",
75+
args: {
76+
param: tool.schema.string().describe("Parameter description"),
77+
optionalParam: tool.schema.boolean().optional().describe("Optional param"),
78+
},
79+
async execute(args, ctx) {
80+
// Implementation
81+
},
82+
});
83+
```
84+
85+
### Error Messages
86+
- Be descriptive and actionable
87+
- Include context like session IDs or parameter values
88+
- Suggest alternatives when possible (e.g., "Use pty_list to see active sessions")
89+
90+
### File Organization
91+
```
92+
src/
93+
├── plugin.ts # Main plugin entry point
94+
├── types.ts # Plugin-level types
95+
├── logger.ts # Logging utilities
96+
└── plugin/ # Plugin-specific code
97+
├── pty/ # PTY-specific code
98+
│ ├── types.ts # PTY types and interfaces
99+
│ ├── manager.ts # PTY session management
100+
│ ├── buffer.ts # Output buffering (RingBuffer)
101+
│ ├── permissions.ts # Permission checking
102+
│ ├── wildcard.ts # Wildcard matching utilities
103+
│ └── tools/ # Tool implementations
104+
│ ├── spawn.ts # pty_spawn tool
105+
│ ├── write.ts # pty_write tool
106+
│ ├── read.ts # pty_read tool
107+
│ ├── list.ts # pty_list tool
108+
│ ├── kill.ts # pty_kill tool
109+
│ └── *.txt # Tool descriptions
110+
└── types.ts # Plugin types
111+
```
112+
113+
### Constants and Magic Numbers
114+
- Define constants at the top of files: `const DEFAULT_LIMIT = 500;`
115+
- Use meaningful names instead of magic numbers
116+
- Group related constants together
117+
118+
### Buffer Management
119+
- Use RingBuffer for output storage (max 50,000 lines by default via `PTY_MAX_BUFFER_LINES`)
120+
- Handle line truncation at 2000 characters
121+
- Implement pagination with offset/limit for large outputs
122+
123+
### Session Management
124+
- Generate unique IDs using crypto: `pty_${hex}`
125+
- Track session lifecycle: running → exited/killed
126+
- Support cleanup on session deletion events
127+
- Include parent session ID for proper isolation
128+
129+
### Permission Integration
130+
- Always check command permissions before spawning
131+
- Validate working directory permissions
132+
- Use wildcard matching for flexible permission rules
133+
134+
### Testing
135+
- Write tests for all public APIs
136+
- Test error conditions and edge cases
137+
- Use Bun's test framework
138+
- Mock external dependencies when necessary
139+
140+
### Documentation
141+
- Include `.txt` description files for each tool in `tools/` directory
142+
- Use JSDoc sparingly, prefer `describe()` in schemas
143+
- Keep README.md updated with usage examples
144+
145+
### Security Considerations
146+
- Never log sensitive information (passwords, tokens)
147+
- Validate all user inputs, especially regex patterns
148+
- Respect permission boundaries set by OpenCode
149+
- Use secure random generation for session IDs
150+
151+
### Performance
152+
- Use efficient data structures (RingBuffer, Map for sessions)
153+
- Avoid blocking operations in main thread
154+
- Implement pagination for large outputs
155+
- Clean up resources promptly
156+
157+
### Commit Messages
158+
Follow conventional commit format:
159+
- `feat:` for new features
160+
- `fix:` for bug fixes
161+
- `refactor:` for code restructuring
162+
- `test:` for test additions
163+
- `docs:` for documentation changes
164+
165+
### Git Workflow
166+
- Use feature branches for development
167+
- Run typecheck and tests before committing
168+
- Use GitHub Actions for automated releases on main branch
169+
- Follow semantic versioning with `v` prefixed tags
170+
171+
### Dependencies
172+
- **@opencode-ai/plugin**: ^1.1.3 (Core plugin framework)
173+
- **@opencode-ai/sdk**: ^1.1.3 (SDK for client interactions)
174+
- **bun-pty**: ^0.4.2 (PTY implementation)
175+
- **@types/bun**: 1.3.1 (TypeScript definitions for Bun)
176+
- **typescript**: ^5 (peer dependency)
177+
178+
### Development Setup
179+
- Install Bun: `curl -fsSL https://bun.sh/install | bash`
180+
- Install dependencies: `bun install`
181+
- Run development commands: `bun run <script>`
182+
183+
### Nix Development (Alternative)
184+
- Install Nix: `curl -L https://nixos.org/nix/install | sh`
185+
- Enter dev shell: `nix develop`
186+
- Build with bun2nix: `nix run github:nix-community/bun2nix -- -o nix/bun.nix`
187+
- Update flake: `nix flake update`
188+
189+
### Common Patterns
190+
- Use `manager` singleton for PTY operations
191+
- Implement tools as pure functions with side effects through manager
192+
- Handle async operations with proper error propagation
193+
- Use descriptive variable names over abbreviations
194+
- Prefer functional programming where appropriate
195+
196+
### Code Review Checklist
197+
- [ ] TypeScript types are correct and complete
198+
- [ ] Error handling covers expected failure modes
199+
- [ ] Logging is appropriate and not verbose
200+
- [ ] Permission checks are in place
201+
- [ ] Tests exist for new functionality
202+
- [ ] Code follows naming conventions
203+
- [ ] No sensitive data in logs or comments
204+
- [ ] Documentation updated if public API changed
205+
206+
### Troubleshooting
207+
- **Type errors**: Run `bun run typecheck` and fix reported issues
208+
- **Test failures**: Check test output and fix assertions
209+
- **Permission denied**: Verify OpenCode configuration allows the command/directory
210+
- **Session not found**: Use `pty_list` to check active sessions
211+
- **Invalid regex**: Test patterns separately and provide user-friendly error messages
212+
213+
This guide should be updated as the codebase evolves. When adding new features or changing conventions, update this document accordingly.

bun.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)