Files
opencode2api/internal/config/json.go
T
Jason Xu 81ad2842c7 refactor: drop new prefixes, unify v1/v2 lane naming, audit comments
- Remove the new/New prefix from constructors; rename the colliding
  types semantically (ModelCatalog, RuntimeController, Metrics,
  LogBroker, Redactor, proxyTransports, upstreamNodes, ...).
- Unify upstream lane naming on v1 (/zen, OpenCode 1.x shape) and v2
  (/inference and /zen/go, 2.x CLI shape); add models.IsV1Upstream and
  drop the duplicated lane detection.
- Fix inaccurate, truncated and stale comments across all packages and
  document exported symbols plus non-obvious internals.
- Fix anthropicThinking passing "enabled"/"adaptive" through as an
  effort; derive the level from the budget instead.
- Delete dead code (identity.StableID, unused protocol decoders and
  stream parser wrappers).
- Export EffectiveListeners, rename modelsDev* metadata constants, and
  describe the metadata source as the OpenCode endpoint.
- Rename the forced-collapse log event to stream_collapse_failed and
  add an anonymous field.
- Correct the README upstream.zen default and add upstream.anonymous.
2026-10-19 03:46:09 +08:00

77 lines
1.8 KiB
Go

package config
import (
"bytes"
"errors"
)
// stripJSONComments removes // and /* */ comments by overwriting each comment
// byte with a space but keeping newlines, so a position in the result is on
// the same line as in config.json. Comment markers inside JSON strings (for
// example, https:// URLs) are preserved. A leading UTF-8 BOM is stripped, and
// an unterminated /* comment is an error.
func stripJSONComments(data []byte) ([]byte, error) {
data = bytes.TrimPrefix(data, []byte{0xef, 0xbb, 0xbf})
out := make([]byte, 0, len(data))
inString := false
escaped := false
lineComment := false
blockComment := false
for i := 0; i < len(data); i++ {
current := data[i]
if lineComment {
if current == '\n' || current == '\r' {
lineComment = false
out = append(out, current)
} else {
out = append(out, ' ')
}
continue
}
if blockComment {
if current == '*' && i+1 < len(data) && data[i+1] == '/' {
out = append(out, ' ', ' ')
i++
blockComment = false
} else if current == '\n' || current == '\r' {
out = append(out, current)
} else {
out = append(out, ' ')
}
continue
}
if inString {
out = append(out, current)
if escaped {
escaped = false
} else if current == '\\' {
escaped = true
} else if current == '"' {
inString = false
}
continue
}
switch {
case current == '"':
inString = true
out = append(out, current)
case current == '/' && i+1 < len(data) && data[i+1] == '/':
lineComment = true
out = append(out, ' ', ' ')
i++
case current == '/' && i+1 < len(data) && data[i+1] == '*':
blockComment = true
out = append(out, ' ', ' ')
i++
default:
out = append(out, current)
}
}
if blockComment {
return nil, errors.New("unterminated block comment")
}
return out, nil
}