2025-12-31 14:07:14 +09:00
|
|
|
# SHARED UTILITIES KNOWLEDGE BASE
|
|
|
|
|
|
|
|
|
|
## OVERVIEW
|
2026-01-17 22:01:56 +09:00
|
|
|
|
2026-01-23 02:14:08 +09:00
|
|
|
50 cross-cutting utilities: path resolution, token truncation, config parsing, model resolution.
|
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
|
|
|
├── env-expander.ts # ${VAR} expansion
|
|
|
|
|
├── model-requirements.ts # Agent/Category requirements
|
|
|
|
|
├── model-availability.ts # Models fetch + fuzzy match
|
|
|
|
|
├── model-resolver.ts # 3-step resolution
|
|
|
|
|
├── shell-env.ts # Cross-platform shell
|
|
|
|
|
├── prompt-parts-helper.ts # Prompt manipulation
|
|
|
|
|
└── *.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-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`
|