2025-12-31 14:07:14 +09:00
|
|
|
# SHARED UTILITIES KNOWLEDGE BASE
|
|
|
|
|
|
|
|
|
|
## OVERVIEW
|
2026-01-17 22:01:56 +09:00
|
|
|
|
2026-01-25 13:12:40 +09:00
|
|
|
34 cross-cutting utilities: path resolution, token truncation, config parsing, model resolution, agent display names.
|
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-23 02:14:08 +09:00
|
|
|
├── logger.ts # File-based logging
|
|
|
|
|
├── permission-compat.ts # Agent tool restrictions
|
|
|
|
|
├── dynamic-truncator.ts # Token-aware truncation
|
|
|
|
|
├── frontmatter.ts # YAML frontmatter
|
|
|
|
|
├── jsonc-parser.ts # JSON with Comments
|
|
|
|
|
├── data-path.ts # XDG-compliant storage
|
|
|
|
|
├── opencode-config-dir.ts # ~/.config/opencode
|
|
|
|
|
├── claude-config-dir.ts # ~/.claude
|
|
|
|
|
├── migration.ts # Legacy config migration
|
|
|
|
|
├── opencode-version.ts # Version comparison
|
2026-01-17 22:01:56 +09:00
|
|
|
├── external-plugin-detector.ts # OAuth spoofing detection
|
2026-01-23 02:14:08 +09:00
|
|
|
├── model-requirements.ts # Agent/Category requirements
|
|
|
|
|
├── model-availability.ts # Models fetch + fuzzy match
|
|
|
|
|
├── model-resolver.ts # 3-step resolution
|
2026-01-25 13:12:40 +09:00
|
|
|
├── model-sanitizer.ts # Model ID normalization
|
2026-01-23 02:14:08 +09:00
|
|
|
├── shell-env.ts # Cross-platform shell
|
2026-01-25 13:12:40 +09:00
|
|
|
├── agent-display-names.ts # Agent display name mapping
|
|
|
|
|
├── agent-tool-restrictions.ts # Tool restriction helpers
|
|
|
|
|
├── agent-variant.ts # Agent variant detection
|
|
|
|
|
├── command-executor.ts # Subprocess execution
|
|
|
|
|
├── config-errors.ts # Config error types
|
|
|
|
|
├── deep-merge.ts # Deep object merge
|
|
|
|
|
├── file-reference-resolver.ts # File path resolution
|
|
|
|
|
├── file-utils.ts # File utilities
|
|
|
|
|
├── hook-disabled.ts # Hook enable/disable check
|
|
|
|
|
├── pattern-matcher.ts # Glob pattern matching
|
|
|
|
|
├── session-cursor.ts # Session cursor tracking
|
|
|
|
|
├── snake-case.ts # String case conversion
|
|
|
|
|
├── system-directive.ts # System prompt helpers
|
|
|
|
|
├── tool-name.ts # Tool name constants
|
|
|
|
|
├── zip-extractor.ts # ZIP file extraction
|
|
|
|
|
├── index.ts # Barrel export
|
2026-01-23 02:14:08 +09:00
|
|
|
└── *.test.ts # Colocated tests
|
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-23 02:14:08 +09:00
|
|
|
| Debug logging | `log(message, data)` |
|
2026-01-17 22:01:56 +09:00
|
|
|
| Limit context | `dynamicTruncate(ctx, sessionId, output)` |
|
|
|
|
|
| Parse frontmatter | `parseFrontmatter(content)` |
|
2026-01-23 02:14:08 +09:00
|
|
|
| Load JSONC | `parseJsonc(text)` or `readJsoncFile(path)` |
|
|
|
|
|
| Restrict tools | `createAgentToolAllowlist(tools)` |
|
|
|
|
|
| Resolve paths | `getOpenCodeConfigDir()` |
|
2026-01-17 22:01:56 +09:00
|
|
|
| Compare versions | `isOpenCodeVersionAtLeast("1.1.0")` |
|
2026-01-23 02:14:08 +09:00
|
|
|
| Resolve model | `resolveModelWithFallback()` |
|
2026-01-25 13:12:40 +09:00
|
|
|
| Agent display name | `getAgentDisplayName(agentName)` |
|
2026-01-17 22:01:56 +09:00
|
|
|
|
2026-01-23 02:14:08 +09:00
|
|
|
## PATTERNS
|
2026-01-17 22:01:56 +09:00
|
|
|
|
2025-12-31 14:07:14 +09:00
|
|
|
```typescript
|
2026-01-17 22:01:56 +09:00
|
|
|
// Token-aware truncation
|
2026-01-23 02:14:08 +09:00
|
|
|
const { result } = await dynamicTruncate(ctx, sessionID, buffer)
|
2025-12-31 14:07:14 +09:00
|
|
|
|
2026-01-23 02:14:08 +09:00
|
|
|
// JSONC config
|
2026-01-17 22:01:56 +09:00
|
|
|
const settings = readJsoncFile<Settings>(configPath)
|
2025-12-31 14:07:14 +09:00
|
|
|
|
2026-01-23 02:14:08 +09:00
|
|
|
// Version-gated
|
|
|
|
|
if (isOpenCodeVersionAtLeast("1.1.0")) { /* ... */ }
|
2026-01-15 15:53:51 +09:00
|
|
|
|
2026-01-23 02:14:08 +09:00
|
|
|
// Model resolution
|
2026-01-22 22:48:50 +09:00
|
|
|
const model = await resolveModelWithFallback(client, requirements, override)
|
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
|
|
|
|
2026-01-23 02:14:08 +09:00
|
|
|
- **Raw JSON.parse**: Use `jsonc-parser.ts`
|
|
|
|
|
- **Hardcoded paths**: Use `*-config-dir.ts`
|
|
|
|
|
- **console.log**: Use `logger.ts` for background
|
|
|
|
|
- **Unbounded output**: Use `dynamic-truncator.ts`
|