2025-12-31 14:07:14 +09:00
|
|
|
# SHARED UTILITIES KNOWLEDGE BASE
|
|
|
|
|
|
|
|
|
|
## OVERVIEW
|
2026-02-01 19:26:57 +09:00
|
|
|
|
2026-02-10 14:53:39 +09:00
|
|
|
84 cross-cutting utilities across 6 subdirectories. Import via barrel: `import { log, deepMerge } from "../../shared"`
|
2025-12-31 14:07:14 +09:00
|
|
|
|
|
|
|
|
## STRUCTURE
|
|
|
|
|
```
|
|
|
|
|
shared/
|
2026-02-10 14:53:39 +09:00
|
|
|
├── logger.ts # File logging (/tmp/oh-my-opencode.log) — 62 imports
|
|
|
|
|
├── dynamic-truncator.ts # Token-aware context window management (201 lines)
|
|
|
|
|
├── model-resolver.ts # 3-step resolution (Override → Fallback → Default)
|
|
|
|
|
├── model-availability.ts # Provider model fetching & fuzzy matching (358 lines)
|
|
|
|
|
├── model-requirements.ts # Agent/category fallback chains (160 lines)
|
|
|
|
|
├── model-resolution-pipeline.ts # Pipeline orchestration (175 lines)
|
|
|
|
|
├── model-resolution-types.ts # Resolution request/provenance types
|
|
|
|
|
├── model-sanitizer.ts # Model name sanitization
|
|
|
|
|
├── model-name-matcher.ts # Model name matching (91 lines)
|
|
|
|
|
├── model-suggestion-retry.ts # Suggest models on failure (129 lines)
|
|
|
|
|
├── model-cache-availability.ts # Cache availability checking
|
|
|
|
|
├── fallback-model-availability.ts # Fallback model logic (67 lines)
|
|
|
|
|
├── available-models-fetcher.ts # Fetch models from providers (114 lines)
|
|
|
|
|
├── models-json-cache-reader.ts # Read models.json cache
|
|
|
|
|
├── provider-models-cache-model-reader.ts # Provider cache reader
|
|
|
|
|
├── connected-providers-cache.ts # Provider caching (196 lines)
|
|
|
|
|
├── system-directive.ts # Unified message prefix & types (61 lines) — 11 imports
|
|
|
|
|
├── session-utils.ts # Session cursor, orchestrator detection
|
|
|
|
|
├── session-cursor.ts # Message cursor tracking (85 lines)
|
|
|
|
|
├── session-injected-paths.ts # Injected file path tracking
|
|
|
|
|
├── permission-compat.ts # Tool restriction enforcement (86 lines)
|
|
|
|
|
├── agent-tool-restrictions.ts # Tool restriction definitions
|
|
|
|
|
├── agent-variant.ts # Agent variant from config (91 lines)
|
|
|
|
|
├── agent-display-names.ts # Agent display name mapping
|
|
|
|
|
├── first-message-variant.ts # First message variant types
|
|
|
|
|
├── opencode-config-dir.ts # ~/.config/opencode resolution (138 lines)
|
|
|
|
|
├── claude-config-dir.ts # ~/.claude resolution
|
|
|
|
|
├── data-path.ts # XDG-compliant storage (47 lines)
|
|
|
|
|
├── jsonc-parser.ts # JSONC with comment support (66 lines)
|
|
|
|
|
├── frontmatter.ts # YAML frontmatter extraction (31 lines) — 10 imports
|
|
|
|
|
├── deep-merge.ts # Recursive merge (proto-pollution safe, MAX_DEPTH=50)
|
|
|
|
|
├── shell-env.ts # Cross-platform shell environment (111 lines)
|
|
|
|
|
├── opencode-version.ts # Semantic version comparison (74 lines)
|
|
|
|
|
├── external-plugin-detector.ts # Plugin conflict detection (137 lines)
|
|
|
|
|
├── opencode-server-auth.ts # Authentication utilities (69 lines)
|
|
|
|
|
├── safe-create-hook.ts # Hook error wrapper (24 lines)
|
|
|
|
|
├── pattern-matcher.ts # Pattern matching (40 lines)
|
|
|
|
|
├── file-utils.ts # File operations (40 lines) — 9 imports
|
|
|
|
|
├── file-reference-resolver.ts # File reference resolution (85 lines)
|
|
|
|
|
├── snake-case.ts # Case conversion (44 lines)
|
|
|
|
|
├── tool-name.ts # Tool naming conventions
|
|
|
|
|
├── truncate-description.ts # Description truncation
|
|
|
|
|
├── port-utils.ts # Port management (48 lines)
|
|
|
|
|
├── zip-extractor.ts # ZIP extraction (83 lines)
|
|
|
|
|
├── binary-downloader.ts # Binary download (60 lines)
|
|
|
|
|
├── skill-path-resolver.ts # Skill path resolution
|
|
|
|
|
├── hook-disabled.ts # Hook disable checking
|
|
|
|
|
├── config-errors.ts # Config error types
|
|
|
|
|
├── disabled-tools.ts # Disabled tools tracking
|
|
|
|
|
├── record-type-guard.ts # Record type guard
|
|
|
|
|
├── open-code-client-accessors.ts # Client accessor utilities
|
|
|
|
|
├── open-code-client-shapes.ts # Client shape types
|
|
|
|
|
├── command-executor/ # Shell execution (6 files, 213 lines)
|
|
|
|
|
├── git-worktree/ # Git status/diff parsing (8 files, 311 lines)
|
|
|
|
|
├── migration/ # Legacy config migration (5 files, 341 lines)
|
|
|
|
|
│ ├── config-migration.ts # Migration orchestration (126 lines)
|
|
|
|
|
│ ├── agent-names.ts # Agent name mapping (70 lines)
|
|
|
|
|
│ ├── hook-names.ts # Hook name mapping (36 lines)
|
|
|
|
|
│ └── model-versions.ts # Model version migration (49 lines)
|
|
|
|
|
└── tmux/ # Tmux TUI integration (12 files, 427 lines)
|
|
|
|
|
└── tmux-utils/ # Pane spawn, close, replace, layout, health
|
2025-12-31 14:07:14 +09:00
|
|
|
```
|
|
|
|
|
|
2026-01-26 11:48:30 +09:00
|
|
|
## MOST IMPORTED
|
2026-02-10 14:53:39 +09:00
|
|
|
|
2026-02-03 16:21:31 +09:00
|
|
|
| Utility | Imports | Purpose |
|
|
|
|
|
|---------|---------|---------|
|
2026-02-10 14:53:39 +09:00
|
|
|
| logger.ts | 62 | Background task visibility |
|
|
|
|
|
| data-path.ts | 11 | XDG storage resolution |
|
|
|
|
|
| model-requirements.ts | 11 | Agent fallback chains |
|
|
|
|
|
| system-directive.ts | 11 | System message filtering |
|
|
|
|
|
| frontmatter.ts | 10 | YAML metadata extraction |
|
|
|
|
|
| permission-compat.ts | 9 | Tool restrictions |
|
|
|
|
|
| file-utils.ts | 9 | File operations |
|
|
|
|
|
| dynamic-truncator.ts | 7 | Token-aware truncation |
|
2026-01-17 22:01:56 +09:00
|
|
|
|
2026-01-26 11:48:30 +09:00
|
|
|
## KEY PATTERNS
|
2026-01-17 22:01:56 +09:00
|
|
|
|
2026-02-10 14:53:39 +09:00
|
|
|
**3-Step Model Resolution** (Override → Fallback → Default):
|
2025-12-31 14:07:14 +09:00
|
|
|
```typescript
|
2026-02-10 14:53:39 +09:00
|
|
|
resolveModelWithFallback({ userModel, fallbackChain, availableModels })
|
2026-01-26 11:48:30 +09:00
|
|
|
```
|
2026-01-15 15:53:51 +09:00
|
|
|
|
2026-01-26 11:48:30 +09:00
|
|
|
**System Directive Filtering**:
|
|
|
|
|
```typescript
|
|
|
|
|
if (isSystemDirective(message)) return // Skip system-generated
|
|
|
|
|
const directive = createSystemDirective("TODO CONTINUATION")
|
2025-12-31 14:07:14 +09:00
|
|
|
```
|
|
|
|
|
|
2026-01-02 10:42:38 +09:00
|
|
|
## ANTI-PATTERNS
|
2026-02-10 14:53:39 +09:00
|
|
|
|
2026-01-26 11:48:30 +09:00
|
|
|
- **Raw JSON.parse**: Use `jsonc-parser.ts` for comment support
|
2026-02-10 14:53:39 +09:00
|
|
|
- **Hardcoded paths**: Use `opencode-config-dir.ts` or `data-path.ts`
|
2026-01-26 11:48:30 +09:00
|
|
|
- **console.log**: Use `logger.ts` for background task visibility
|
2026-02-10 14:53:39 +09:00
|
|
|
- **Unbounded output**: Use `dynamic-truncator.ts` to prevent overflow
|
|
|
|
|
- **Manual version check**: Use `opencode-version.ts` for semver safety
|