Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ all: gen build fmt lint test

gen: src/codersdk.gen.ts

src/codersdk.gen.ts: scripts/typegen/main.go scripts/typegen/zod.go scripts/typegen/go.mod
src/codersdk.gen.ts: scripts/typegen/main.go scripts/typegen/zod/zod.go scripts/typegen/go.mod
@cd scripts/typegen && go run . > ../../src/codersdk.gen.ts.tmp
@mv src/codersdk.gen.ts.tmp src/codersdk.gen.ts
@bun run format -- src/codersdk.gen.ts || true
Expand Down
40 changes: 20 additions & 20 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion scripts/typegen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ require (
github.com/buger/jsonparser v1.1.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coder/coder/v2 v2.34.0-rc.0.0.20260528065010-cfa343e45666 // indirect
github.com/coder/guts v1.7.0 // indirect
github.com/coder/guts v1.7.1 // indirect
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 // indirect
github.com/coder/serpent v0.15.0 // indirect
github.com/coder/websocket v1.8.14 // indirect
github.com/coreos/go-oidc/v3 v3.18.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.12.0 // indirect
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd // indirect
github.com/fatih/structtag v1.2.0 // indirect
Expand All @@ -36,9 +37,11 @@ require (
github.com/pb33f/ordered-map/v2 v2.3.1 // indirect
github.com/pion/transport/v2 v2.2.10 // indirect
github.com/pion/udp v0.1.4 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/stretchr/testify v1.11.1 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
github.com/zclconf/go-cty v1.18.1 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions scripts/typegen/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/coder/coder/v2 v2.34.0-rc.0.0.20260528065010-cfa343e45666 h1:imJX12Fi
github.com/coder/coder/v2 v2.34.0-rc.0.0.20260528065010-cfa343e45666/go.mod h1:5UG9p30Gqu3UoUz0fo18JzD5DBQj2S9ArDtbI8yhIr8=
github.com/coder/guts v1.7.0 h1:TaZ/PR9wgN8dlbcckaWV1MxkkuEFZRwSRwBBEm8dYXs=
github.com/coder/guts v1.7.0/go.mod h1:30SShdvpmsauNlsNjECRB5AppScjYk08rf2ZVpH3MFg=
github.com/coder/guts v1.7.1 h1:NrxGxV1TeyOW7+1vBMZjJI1pYHTvlqWEh66GOZ3lsHI=
github.com/coder/guts v1.7.1/go.mod h1:30SShdvpmsauNlsNjECRB5AppScjYk08rf2ZVpH3MFg=
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs=
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
github.com/coder/serpent v0.15.0 h1:jobR7DnPsxzEMD0cRiailwlY+4v6HAPS/8emIgBpaIU=
Expand All @@ -26,6 +28,8 @@ github.com/coreos/go-oidc/v3 v3.18.0 h1:V9orjXynvu5wiC9SemFTWnG4F45v403aIcjWo0d4
github.com/coreos/go-oidc/v3 v3.18.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.12.0 h1:0j4c5qQmnC6XOWNjP3PIXURXN2gWx76rd3KvgdPkCz8=
github.com/dlclark/regexp2 v1.12.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd h1:QMSNEh9uQkDjyPwu/J541GgSH+4hw+0skJDIj9HJ3mE=
Expand Down Expand Up @@ -72,6 +76,8 @@ github.com/pion/transport/v2 v2.2.10/go.mod h1:sq1kSLWs+cHW9E+2fJP95QudkzbK7wscs
github.com/pion/udp v0.1.4 h1:OowsTmu1Od3sD6i3fQUJxJn2fEvJO6L1TidgadtbTI8=
github.com/pion/udp v0.1.4/go.mod h1:G8LDo56HsFwC24LIcnT4YIDU5qcB6NepqqjP0keL2us=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
Expand All @@ -85,6 +91,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/wlynxg/anet v0.0.3/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
Expand Down
146 changes: 41 additions & 105 deletions scripts/typegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ package main
import (
"fmt"
"log"
"maps"
"os"
"slices"
"strings"

"github.com/coder/guts"
"github.com/coder/guts/bindings"
"github.com/coder/guts/bindings/walk"
"github.com/coder/guts/config"

"github.com/coder/agents-chat-action/scripts/typegen/zod"
)

// Types the action needs from the Coder API. Only root types that
Expand Down Expand Up @@ -63,38 +64,47 @@ func main() {
log.Fatalf("to typescript: %v", err)
}

// Pre-Zod mutations that reshape enums and optional fields.
ts.ApplyMutations(
config.EnumAsTypes,
config.SimplifyOmitEmpty,
)

// Collect all nodes then filter to wanted types plus any
// types they reference that are also in the full set.
// Compute wanted types before Zod rewrites the AST
// (collectRefs works on Interface/Alias).
allNodes := make(map[string]bindings.Node)
ts.ForEach(func(name string, node bindings.Node) {
allNodes[name] = node
})

// Resolve transitive references so we don't emit broken
// schema references.
included := resolveTransitive(allNodes, wantedTypes)
wantedNames := make(map[string]bool, len(included))
for name := range included {
wantedNames[name] = true
}

// Topological sort: emit dependencies before dependents.
names := topoSort(included)

var b strings.Builder
b.WriteString("// Code generated by 'make gen'. DO NOT EDIT.\n")
b.WriteString("import { z } from \"zod\";\n\n")
// Rewrite Interface/Alias into Zod schemas, then export.
ts.ApplyMutations(
zod.AsSchemas,
config.ExportTypes,
)

for _, name := range names {
s := serializeNode(name, included[name])
if s != "" {
b.WriteString(s)
b.WriteString("\n")
// Serialize only the wanted types, sorted by dependencies.
output, err := ts.SerializeInOrder(func(nodes map[string]bindings.Node) []bindings.Node {
// Filter to wanted types and their schema bindings.
filtered := make(map[string]bindings.Node, len(wantedNames)*2)
for name, node := range nodes {
baseName := strings.TrimSuffix(name, "Schema")
if wantedNames[baseName] {
filtered[name] = node
}
}
return zod.SortByDependencies(filtered)
})
if err != nil {
log.Fatalf("serialize: %v", err)
}

_, _ = fmt.Fprint(os.Stdout, b.String())
_, _ = fmt.Fprint(os.Stdout, output)
}

// resolveTransitive starts from the seeds and pulls in any
Expand All @@ -121,95 +131,21 @@ func resolveTransitive(allNodes map[string]bindings.Node, seeds map[string]bool)
return result
}

// collectRefs extracts all type reference names from a node.
// collectRefs extracts all type reference names from a node
// using the generic AST walker.
func collectRefs(node bindings.Node) []string {
var refs []string
var walkExpr func(bindings.ExpressionType)
walkExpr = func(expr bindings.ExpressionType) {
if expr == nil {
return
}
switch e := expr.(type) {
case *bindings.ReferenceType:
refs = append(refs, e.Name.Ref())
for _, arg := range e.Arguments {
walkExpr(arg)
}
case *bindings.ArrayType:
walkExpr(e.Node)
case *bindings.ArrayLiteralType:
for _, el := range e.Elements {
walkExpr(el)
}
case *bindings.UnionType:
for _, t := range e.Types {
walkExpr(t)
}
case *bindings.TypeLiteralNode:
for _, m := range e.Members {
walkExpr(m.Type)
}
case *bindings.TypeIntersection:
for _, t := range e.Types {
walkExpr(t)
}
case *bindings.ExpressionWithTypeArguments:
walkExpr(e.Expression)
for _, arg := range e.Arguments {
walkExpr(arg)
}
case *bindings.OperatorNodeType:
walkExpr(e.Type)
case *bindings.TupleType:
walkExpr(e.Node)
}
}
switch n := node.(type) {
case *bindings.Interface:
for _, f := range n.Fields {
walkExpr(f.Type)
}
for _, h := range n.Heritage {
for _, arg := range h.Args {
walkExpr(arg)
}
}
case *bindings.Alias:
walkExpr(n.Type)
}
return refs
v := &refVisitor{}
walk.Walk(v, node)
return v.refs
}

// topoSort returns names in dependency order (dependencies first).
// Ties within the same depth level are broken alphabetically.
func topoSort(nodes map[string]bindings.Node) []string {
visited := make(map[string]bool)
var order []string

var visit func(string)
visit = func(name string) {
if visited[name] {
return
}
visited[name] = true
node, ok := nodes[name]
if !ok {
return
}
// Visit dependencies first.
deps := collectRefs(node)
slices.Sort(deps)
for _, dep := range deps {
if _, exists := nodes[dep]; exists {
visit(dep)
}
}
order = append(order, name)
}
type refVisitor struct {
refs []string
}

// Start visits in alphabetical order for stable output.
for _, name := range slices.Sorted(maps.Keys(nodes)) {
visit(name)
func (v *refVisitor) Visit(node bindings.Node) walk.Visitor {
if ref, ok := node.(*bindings.ReferenceType); ok {
v.refs = append(v.refs, ref.Name.Ref())
}
return order
return v
}
Loading