Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

3.4 KB
Raw Blame
// Code generator that mirrors sdk/go/semconv/coding_agent.go to TypeScript
// constants under cli/gen/semconv.ts. Invoked via `go generate ./...` from
// the cli/ module so the dashboard imports stay in lockstep with the Go
// source of truth.
//
// Usage (run from anywhere; resolves paths from this file's location):
//
//	go run ./internal/semconvgen
//
// Or, transitively, from `go generate ./...`.
package main

import (
	"bytes"
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"os"
	"path/filepath"
	"runtime"
	"strconv"
	"strings"
)

func main() {
	if err := run(); err != nil {
		fmt.Fprintln(os.Stderr, "semconvgen:", err)
		os.Exit(1)
	}
}

func run() error {
	_, here, _, ok := runtime.Caller(0)
	if !ok {
		return fmt.Errorf("could not determine caller location")
	}
	cliDir := filepath.Clean(filepath.Join(filepath.Dir(here), "..", ".."))
	repoRoot := filepath.Clean(filepath.Join(cliDir, ".."))

	src := filepath.Join(repoRoot, "sdk", "go", "semconv", "coding_agent.go")
	dst := filepath.Join(cliDir, "gen", "semconv.ts")

	consts, err := parseConsts(src)
	if err != nil {
		return fmt.Errorf("parse %s: %w", src, err)
	}

	var buf bytes.Buffer
	buf.WriteString("// Code generated by cli/internal/semconvgen. DO NOT EDIT.\n")
	buf.WriteString("// Source: sdk/go/semconv/coding_agent.go\n")
	buf.WriteString("//\n")
	buf.WriteString("// Run `go generate ./...` from the cli/ module to regenerate.\n\n")

	for _, c := range consts {
		if c.doc != "" {
			for _, line := range strings.Split(strings.TrimRight(c.doc, "\n"), "\n") {
				if line == "" {
					buf.WriteString("//\n")
					continue
				}
				buf.WriteString("// ")
				buf.WriteString(line)
				buf.WriteByte('\n')
			}
		}
		fmt.Fprintf(&buf, "export const %s = %s;\n\n", c.name, strconv.Quote(c.value))
	}

	if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
		return fmt.Errorf("mkdir %s: %w", filepath.Dir(dst), err)
	}
	if err := os.WriteFile(dst, buf.Bytes(), 0o644); err != nil {
		return fmt.Errorf("write %s: %w", dst, err)
	}
	fmt.Printf("semconvgen: wrote %d constants to %s\n", len(consts), dst)
	return nil
}

type constDecl struct {
	name  string
	value string
	doc   string
}

func parseConsts(src string) ([]constDecl, error) {
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, src, nil, parser.ParseComments)
	if err != nil {
		return nil, err
	}

	var out []constDecl
	for _, decl := range f.Decls {
		gen, ok := decl.(*ast.GenDecl)
		if !ok || gen.Tok != token.CONST {
			continue
		}
		groupDoc := ""
		if gen.Doc != nil {
			groupDoc = gen.Doc.Text()
		}
		// Track the first spec we successfully emit so we can attach
		// the group's doc comment as a section header. Subsequent
		// specs without their own doc render undocumented.
		groupHeaderEmitted := false
		for _, spec := range gen.Specs {
			vs, ok := spec.(*ast.ValueSpec)
			if !ok {
				continue
			}
			for i, name := range vs.Names {
				if i >= len(vs.Values) {
					continue
				}
				lit, ok := vs.Values[i].(*ast.BasicLit)
				if !ok || lit.Kind != token.STRING {
					// Only string consts cross the wire as semconv keys.
					continue
				}
				val, err := strconv.Unquote(lit.Value)
				if err != nil {
					continue
				}
				doc := ""
				switch {
				case vs.Doc != nil:
					doc = vs.Doc.Text()
				case !groupHeaderEmitted && groupDoc != "":
					doc = groupDoc
					groupHeaderEmitted = true
				}
				out = append(out, constDecl{
					name:  name.Name,
					value: val,
					doc:   doc,
				})
			}
		}
	}
	return out, nil
}