2025-12-31 14:07:14 +09:00
|
|
|
# SHARED UTILITIES KNOWLEDGE BASE
|
|
|
|
|
|
|
|
|
|
## OVERVIEW
|
2026-01-17 22:01:56 +09:00
|
|
|
|
|
|
|
|
43 cross-cutting utilities: path resolution, token truncation, config parsing, Claude Code compatibility.
|
2025-12-31 14:07:14 +09:00
|
|
|
|
|
|
|
|
## STRUCTURE
|
2026-01-17 22:01:56 +09:00
|
|
|
|
2025-12-31 14:07:14 +09:00
|
|
|
```
|
|
|
|
|
shared/
|
2026-01-17 22:01:56 +09:00
|
|
|
├── logger.ts # File-based logging (tmpdir/oh-my-opencode.log)
|
2026-01-17 19:16:49 +09:00
|
|
|
├── permission-compat.ts # Agent tool restrictions (ask/allow/deny)
|
2026-01-17 22:01:56 +09:00
|
|
|
├── dynamic-truncator.ts # Token-aware truncation (50% headroom)
|
|
|
|
|
├── frontmatter.ts # YAML frontmatter parsing
|
|
|
|
|
├── jsonc-parser.ts # JSON with Comments support
|
|
|
|
|
├── data-path.ts # XDG-compliant storage (~/.local/share)
|
|
|
|
|
├── opencode-config-dir.ts # ~/.config/opencode resolution
|
|
|
|
|
├── claude-config-dir.ts # ~/.claude resolution
|
|
|
|
|
├── migration.ts # Legacy config migration (omo → Sisyphus)
|
|
|
|
|
├── opencode-version.ts # Version comparison (>= 1.0.150)
|
|
|
|
|
├── external-plugin-detector.ts # OAuth spoofing detection
|
|
|
|
|
├── env-expander.ts # ${VAR} expansion in configs
|
|
|
|
|
├── system-directive.ts # System directive types
|
|
|
|
|
├── hook-utils.ts # Hook helper functions
|
|
|
|
|
└── *.test.ts # Test files (colocated)
|
2025-12-31 14:07:14 +09:00
|
|
|
```
|
|
|
|
|
|
2026-01-02 10:42:38 +09:00
|
|
|
## WHEN TO USE
|
2026-01-17 22:01:56 +09:00
|
|
|
|
2026-01-02 10:42:38 +09:00
|
|
|
| Task | Utility |
|
|
|
|
|
|------|---------|
|
2026-01-17 22:01:56 +09:00
|
|
|
| Debug logging | `log(message, data)` in `logger.ts` |
|
|
|
|
|
| Limit context | `dynamicTruncate(ctx, sessionId, output)` |
|
|
|
|
|
| Parse frontmatter | `parseFrontmatter(content)` |
|
|
|
|
|
| Load JSONC config | `parseJsonc(text)` or `readJsoncFile(path)` |
|
|
|
|
|
| Restrict agent tools | `createAgentToolAllowlist(tools)` |
|
|
|
|
|
| Resolve paths | `getOpenCodeConfigDir()`, `getClaudeConfigDir()` |
|
|
|
|
|
| Migrate config | `migrateConfigFile(path, rawConfig)` |
|
|
|
|
|
| Compare versions | `isOpenCodeVersionAtLeast("1.1.0")` |
|
|
|
|
|
|
|
|
|
|
## KEY PATTERNS
|
|
|
|
|
|
2025-12-31 14:07:14 +09:00
|
|
|
```typescript
|
2026-01-17 22:01:56 +09:00
|
|
|
// Token-aware truncation
|
|
|
|
|
const { result } = await dynamicTruncate(ctx, sessionID, largeBuffer)
|
2025-12-31 14:07:14 +09:00
|
|
|
|
2026-01-17 22:01:56 +09:00
|
|
|
// JSONC config loading
|
|
|
|
|
const settings = readJsoncFile<Settings>(configPath)
|
2025-12-31 14:07:14 +09:00
|
|
|
|
2026-01-17 22:01:56 +09:00
|
|
|
// Version-gated features
|
|
|
|
|
if (isOpenCodeVersionAtLeast("1.1.0")) { /* new feature */ }
|
2026-01-15 15:53:51 +09:00
|
|
|
|
2026-01-17 22:01:56 +09:00
|
|
|
// Tool permission normalization
|
|
|
|
|
const permissions = migrateToolsToPermission(legacyTools)
|
2025-12-31 14:07:14 +09:00
|
|
|
```
|
|
|
|
|
|
2026-01-02 10:42:38 +09:00
|
|
|
## ANTI-PATTERNS
|
2026-01-17 22:01:56 +09:00
|
|
|
|
|
|
|
|
- **Raw JSON.parse**: Use `jsonc-parser.ts` for config files
|
|
|
|
|
- **Hardcoded paths**: Use `*-config-dir.ts` utilities
|
|
|
|
|
- **console.log**: Use `logger.ts` for background agents
|
|
|
|
|
- **Unbounded output**: Always use `dynamic-truncator.ts`
|
|
|
|
|
- **Manual version parse**: Use `opencode-version.ts`
|