2025-12-31 14:07:14 +09:00
|
|
|
# SHARED UTILITIES KNOWLEDGE BASE
|
|
|
|
|
|
|
|
|
|
## OVERVIEW
|
2026-01-17 19:16:49 +09:00
|
|
|
Core cross-cutting utilities for path resolution, token-safe text processing, and Claude Code compatibility.
|
2025-12-31 14:07:14 +09:00
|
|
|
|
|
|
|
|
## STRUCTURE
|
|
|
|
|
```
|
|
|
|
|
shared/
|
2026-01-17 19:16:49 +09:00
|
|
|
├── logger.ts # Persistent file-based logging (tmpdir/oh-my-opencode.log)
|
|
|
|
|
├── permission-compat.ts # Agent tool restrictions (ask/allow/deny)
|
|
|
|
|
├── dynamic-truncator.ts # Token-aware truncation with context headroom
|
|
|
|
|
├── frontmatter.ts # YAML frontmatter parsing with JSON_SCHEMA safety
|
|
|
|
|
├── jsonc-parser.ts # JSON with Comments support for config files
|
|
|
|
|
├── data-path.ts # XDG-compliant storage paths (~/.local/share)
|
|
|
|
|
├── opencode-config-dir.ts # Resolve ~/.config/opencode for CLI/Desktop
|
|
|
|
|
├── claude-config-dir.ts # Resolve ~/.claude for compatibility
|
|
|
|
|
├── migration.ts # Legacy name mapping (omo -> Sisyphus)
|
|
|
|
|
└── opencode-version.ts # Version comparison logic (e.g., >= 1.0.150)
|
2025-12-31 14:07:14 +09:00
|
|
|
```
|
|
|
|
|
|
2026-01-02 10:42:38 +09:00
|
|
|
## WHEN TO USE
|
|
|
|
|
| Task | Utility |
|
|
|
|
|
|------|---------|
|
2026-01-17 19:16:49 +09:00
|
|
|
| Debugging/Auditing | `log(message, data)` in `logger.ts` |
|
|
|
|
|
| Limit agent context | `dynamicTruncate(ctx, sessionId, output)` |
|
|
|
|
|
| Parse rule meta | `parseFrontmatter(content)` |
|
|
|
|
|
| Load user configs | `parseJsonc(text)` or `readJsoncFile(path)` |
|
|
|
|
|
| Restrict tools | `createAgentToolAllowlist(tools)` |
|
|
|
|
|
| Resolve app paths | `getOpenCodeConfigDir()` or `getClaudeConfigDir()` |
|
|
|
|
|
| Update legacy config | `migrateConfigFile(path, rawConfig)` |
|
2025-12-31 14:07:14 +09:00
|
|
|
|
|
|
|
|
## CRITICAL PATTERNS
|
|
|
|
|
```typescript
|
2026-01-17 19:16:49 +09:00
|
|
|
// Truncate large output based on 50% remaining context window
|
|
|
|
|
const { result } = await dynamicTruncate(ctx, sessionID, largeBuffer);
|
2025-12-31 14:07:14 +09:00
|
|
|
|
2026-01-17 19:16:49 +09:00
|
|
|
// Safe config loading with comment/trailing comma support
|
|
|
|
|
const settings = readJsoncFile<Settings>(configPath);
|
2025-12-31 14:07:14 +09:00
|
|
|
|
2026-01-17 19:16:49 +09:00
|
|
|
// Version-gated logic for OpenCode 1.1.0+
|
|
|
|
|
if (isOpenCodeVersionAtLeast("1.1.0")) { /* ... */ }
|
2026-01-15 15:53:51 +09:00
|
|
|
|
2026-01-17 19:16:49 +09:00
|
|
|
// Permission normalization for agent tools
|
|
|
|
|
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 19:16:49 +09:00
|
|
|
- Raw `JSON.parse` for configs (use `jsonc-parser.ts`)
|
|
|
|
|
- Hardcoded `~/.claude` (use `claude-config-dir.ts`)
|
|
|
|
|
- `console.log` for background agents (use `logger.ts`)
|
|
|
|
|
- Unbounded tool output (always use `dynamic-truncator.ts`)
|
|
|
|
|
- Manual version parsing (use `opencode-version.ts`)
|