[PR #6987] feat: add JSON Schema as source of truth for type definitions #12191

Open
opened 2026-02-16 18:17:07 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6987

State: open
Merged: No


Summary

This PR implements JSON Schema as the canonical source of truth for OpenCode's type definitions, addressing #6879.

72+ JSON Schema files now define all major type domains, with Zod validators auto-generated from them.

What's Included

Domain Schemas Key Types
Model 8 files ModelInfo, ModelCost, ModelCapabilities, ModelLimits
Provider 6 files ProviderInfo, ProviderOptions, ProviderSource
Auth 4 files Auth, ApiAuth, OAuth, WellKnownAuth
Session 10 files SessionInfo, SessionTime, SessionSummary, FileDiff
Message 20 files Message, UserMessage, AssistantMessage, all Part types, Error types
Tool 9 files ToolState, ToolPart, PermissionRequest, PermissionReply
Agent 2 files AgentInfo, AgentModel
Event 13 files Event, GlobalEvent, SessionStatus, all event types

Implementation

  1. Custom code generator (script/generate-from-schemas.ts)

    • Validates schemas with AJV
    • Generates Zod validators + TypeScript types
    • Handles cross-file $ref correctly (existing tools don't)
    • Integrated with bun run generate workflow
  2. Refactored source files to use generated validators:

    • provider.ts, message-v2.ts, permission/next.ts
    • agent/agent.ts, session/status.ts, server/server.ts
  3. Documentation in schema/README.md

Results

  • All 87 schemas validate
  • All 544 tests pass
  • TypeScript compilation passes
  • Build succeeds (11 platforms)
  • Runtime behavior unchanged

Benefits for External Clients

# Rust
typify schema/modelInfo.schema.json

# Python
datamodel-codegen --input schema/ --output opencode_types/

# C#
NJsonSchema.CodeGeneration schema/modelInfo.schema.json

Technical Notes

  • Generated files are gitignored (build artifacts)
  • Zod 4.x compatible
  • z.union()z.discriminatedUnion() where schema has discriminator (behavioral equivalence)

Closes #6879

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6987 **State:** open **Merged:** No --- ## Summary This PR implements JSON Schema as the canonical source of truth for OpenCode's type definitions, addressing #6879. **72+ JSON Schema files** now define all major type domains, with Zod validators auto-generated from them. ## What's Included | Domain | Schemas | Key Types | |--------|---------|-----------| | Model | 8 files | `ModelInfo`, `ModelCost`, `ModelCapabilities`, `ModelLimits` | | Provider | 6 files | `ProviderInfo`, `ProviderOptions`, `ProviderSource` | | Auth | 4 files | `Auth`, `ApiAuth`, `OAuth`, `WellKnownAuth` | | Session | 10 files | `SessionInfo`, `SessionTime`, `SessionSummary`, `FileDiff` | | Message | 20 files | `Message`, `UserMessage`, `AssistantMessage`, all Part types, Error types | | Tool | 9 files | `ToolState`, `ToolPart`, `PermissionRequest`, `PermissionReply` | | Agent | 2 files | `AgentInfo`, `AgentModel` | | Event | 13 files | `Event`, `GlobalEvent`, `SessionStatus`, all event types | ## Implementation 1. **Custom code generator** (`script/generate-from-schemas.ts`) - Validates schemas with AJV - Generates Zod validators + TypeScript types - Handles cross-file `$ref` correctly (existing tools don't) - Integrated with `bun run generate` workflow 2. **Refactored source files** to use generated validators: - `provider.ts`, `message-v2.ts`, `permission/next.ts` - `agent/agent.ts`, `session/status.ts`, `server/server.ts` 3. **Documentation** in `schema/README.md` ## Results - ✅ All 87 schemas validate - ✅ All 544 tests pass - ✅ TypeScript compilation passes - ✅ Build succeeds (11 platforms) - ✅ Runtime behavior unchanged ## Benefits for External Clients ```bash # Rust typify schema/modelInfo.schema.json # Python datamodel-codegen --input schema/ --output opencode_types/ # C# NJsonSchema.CodeGeneration schema/modelInfo.schema.json ``` ## Technical Notes - Generated files are gitignored (build artifacts) - Zod 4.x compatible - `z.union()` → `z.discriminatedUnion()` where schema has discriminator (behavioral equivalence) Closes #6879
yindo added the pull-request label 2026-02-16 18:17:07 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12191