@langwatch/server 3.1.0-beta.8 → 3.1.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/package.json +1 -6
  2. package/packages/server/dist/cli.cjs +19 -16
  3. package/cmd/service/main.go +0 -68
  4. package/go.mod +0 -117
  5. package/go.sum +0 -299
  6. package/pkg/breaker/breaker.go +0 -157
  7. package/pkg/breaker/breaker_test.go +0 -131
  8. package/pkg/clog/LICENSE +0 -26
  9. package/pkg/clog/clog.go +0 -90
  10. package/pkg/clog/clog_test.go +0 -56
  11. package/pkg/clog/panic.go +0 -39
  12. package/pkg/config/LICENSE +0 -26
  13. package/pkg/config/config.go +0 -86
  14. package/pkg/config/config_test.go +0 -70
  15. package/pkg/config/otel.go +0 -50
  16. package/pkg/config/server.go +0 -61
  17. package/pkg/config/validate.go +0 -80
  18. package/pkg/config/validate_test.go +0 -74
  19. package/pkg/contexts/contexts_test.go +0 -34
  20. package/pkg/contexts/request_id.go +0 -1
  21. package/pkg/contexts/service_info.go +0 -33
  22. package/pkg/errfuncs/errfuncs.go +0 -9
  23. package/pkg/errfuncs/errfuncs_test.go +0 -28
  24. package/pkg/forkedcontext/forkedcontext.go +0 -57
  25. package/pkg/health/health.go +0 -123
  26. package/pkg/health/health_test.go +0 -84
  27. package/pkg/herr/LICENSE +0 -26
  28. package/pkg/herr/code.go +0 -14
  29. package/pkg/herr/herr.go +0 -112
  30. package/pkg/herr/herr_test.go +0 -78
  31. package/pkg/herr/http.go +0 -133
  32. package/pkg/herr/http_test.go +0 -103
  33. package/pkg/herr/validate.go +0 -72
  34. package/pkg/herr/validate_test.go +0 -75
  35. package/pkg/httpmiddleware/bearer.go +0 -47
  36. package/pkg/httpmiddleware/httputil_test.go +0 -167
  37. package/pkg/httpmiddleware/inflight.go +0 -23
  38. package/pkg/httpmiddleware/maxbody.go +0 -30
  39. package/pkg/httpmiddleware/recover.go +0 -38
  40. package/pkg/httpmiddleware/requestid.go +0 -40
  41. package/pkg/httpmiddleware/telemetry.go +0 -96
  42. package/pkg/httpmiddleware/version.go +0 -16
  43. package/pkg/jwtverify/verifier.go +0 -72
  44. package/pkg/jwtverify/verifier_test.go +0 -134
  45. package/pkg/ksuid/LICENSE +0 -26
  46. package/pkg/ksuid/ksuid.go +0 -157
  47. package/pkg/ksuid/ksuid_test.go +0 -107
  48. package/pkg/ksuid/resources.go +0 -13
  49. package/pkg/lifecycle/group.go +0 -175
  50. package/pkg/lifecycle/group_test.go +0 -236
  51. package/pkg/lifecycle/service.go +0 -112
  52. package/pkg/netprobe/netprobe.go +0 -89
  53. package/pkg/netprobe/netprobe_test.go +0 -55
  54. package/pkg/otelsetup/otelsetup.go +0 -226
  55. package/pkg/otelsetup/otelsetup_test.go +0 -130
  56. package/pkg/ptr/LICENSE +0 -26
  57. package/pkg/ptr/ptr.go +0 -34
  58. package/pkg/ptr/ptr_test.go +0 -66
  59. package/pkg/retry/bench_test.go +0 -67
  60. package/pkg/retry/retry.go +0 -188
  61. package/pkg/retry/retry_test.go +0 -222
  62. package/pkg/slicefuncs/LICENSE +0 -26
  63. package/pkg/slicefuncs/slicefuncs.go +0 -10
  64. package/pkg/slicefuncs/slicefuncs_test.go +0 -29
  65. package/pkg/stacktrace/LICENSE +0 -26
  66. package/pkg/stacktrace/frames.go +0 -77
  67. package/pkg/stacktrace/frames_test.go +0 -73
  68. package/services/aigateway/BENCHMARKS.md +0 -110
  69. package/services/aigateway/README.md +0 -156
  70. package/services/aigateway/adapters/authresolver/bench_test.go +0 -13
  71. package/services/aigateway/adapters/authresolver/redis.go +0 -61
  72. package/services/aigateway/adapters/authresolver/service.go +0 -442
  73. package/services/aigateway/adapters/authresolver/service_test.go +0 -435
  74. package/services/aigateway/adapters/budget/bench_test.go +0 -50
  75. package/services/aigateway/adapters/budget/budget.go +0 -59
  76. package/services/aigateway/adapters/cacherules/evaluator.go +0 -134
  77. package/services/aigateway/adapters/cacherules/evaluator_test.go +0 -207
  78. package/services/aigateway/adapters/controlplane/bench_test.go +0 -40
  79. package/services/aigateway/adapters/controlplane/client.go +0 -213
  80. package/services/aigateway/adapters/controlplane/client_test.go +0 -73
  81. package/services/aigateway/adapters/controlplane/config_wire.go +0 -310
  82. package/services/aigateway/adapters/controlplane/guardrails.go +0 -88
  83. package/services/aigateway/adapters/controlplane/signer.go +0 -95
  84. package/services/aigateway/adapters/controlplane/signer_test.go +0 -64
  85. package/services/aigateway/adapters/customertracebridge/context.go +0 -48
  86. package/services/aigateway/adapters/customertracebridge/context_test.go +0 -24
  87. package/services/aigateway/adapters/customertracebridge/emitter.go +0 -329
  88. package/services/aigateway/adapters/customertracebridge/emitter_test.go +0 -93
  89. package/services/aigateway/adapters/customertracebridge/registry.go +0 -82
  90. package/services/aigateway/adapters/customertracebridge/transport.go +0 -122
  91. package/services/aigateway/adapters/gatewaytracer/attrs.go +0 -28
  92. package/services/aigateway/adapters/gatewaytracer/attrs_genai.go +0 -24
  93. package/services/aigateway/adapters/gatewaytracer/headers.go +0 -8
  94. package/services/aigateway/adapters/gatewaytracer/tracer.go +0 -101
  95. package/services/aigateway/adapters/httpapi/bench_test.go +0 -83
  96. package/services/aigateway/adapters/httpapi/middleware.go +0 -123
  97. package/services/aigateway/adapters/httpapi/router.go +0 -635
  98. package/services/aigateway/adapters/httpapi/router_test.go +0 -633
  99. package/services/aigateway/adapters/modelresolver/resolver.go +0 -107
  100. package/services/aigateway/adapters/modelresolver/resolver_test.go +0 -140
  101. package/services/aigateway/adapters/policy/matcher.go +0 -250
  102. package/services/aigateway/adapters/policy/matcher_test.go +0 -124
  103. package/services/aigateway/adapters/providers/bifrost.go +0 -798
  104. package/services/aigateway/adapters/providers/bifrost_parser.go +0 -201
  105. package/services/aigateway/adapters/providers/bifrost_test.go +0 -181
  106. package/services/aigateway/adapters/ratelimit/limiter.go +0 -122
  107. package/services/aigateway/adapters/ratelimit/limiter_test.go +0 -81
  108. package/services/aigateway/app/app.go +0 -104
  109. package/services/aigateway/app/dispatch.go +0 -89
  110. package/services/aigateway/app/dispatch_test.go +0 -475
  111. package/services/aigateway/app/handlers.go +0 -79
  112. package/services/aigateway/app/peek_bench_test.go +0 -21
  113. package/services/aigateway/app/pipeline/bench_test.go +0 -34
  114. package/services/aigateway/app/pipeline/budget.go +0 -55
  115. package/services/aigateway/app/pipeline/cache.go +0 -34
  116. package/services/aigateway/app/pipeline/cachecontrol.go +0 -86
  117. package/services/aigateway/app/pipeline/cachecontrol_test.go +0 -110
  118. package/services/aigateway/app/pipeline/guardrail.go +0 -121
  119. package/services/aigateway/app/pipeline/pipeline.go +0 -159
  120. package/services/aigateway/app/pipeline/policy.go +0 -23
  121. package/services/aigateway/app/pipeline/ratelimit.go +0 -21
  122. package/services/aigateway/app/pipeline/rawframing_test.go +0 -67
  123. package/services/aigateway/app/pipeline/resolve.go +0 -44
  124. package/services/aigateway/app/pipeline/trace.go +0 -129
  125. package/services/aigateway/app/ports.go +0 -61
  126. package/services/aigateway/cmd/root.go +0 -42
  127. package/services/aigateway/config.go +0 -86
  128. package/services/aigateway/deps.go +0 -177
  129. package/services/aigateway/domain/bundle.go +0 -203
  130. package/services/aigateway/domain/errors.go +0 -23
  131. package/services/aigateway/domain/model.go +0 -24
  132. package/services/aigateway/domain/provider.go +0 -38
  133. package/services/aigateway/domain/request.go +0 -65
  134. package/services/aigateway/domain/response.go +0 -56
  135. package/services/aigateway/domain/verdict.go +0 -55
  136. package/services/aigateway/serve.go +0 -45
  137. package/services/aigateway/tests/matrix/README.md +0 -71
  138. package/services/aigateway/tests/matrix/anthropic_test.go +0 -61
  139. package/services/aigateway/tests/matrix/azure_test.go +0 -52
  140. package/services/aigateway/tests/matrix/bedrock_test.go +0 -69
  141. package/services/aigateway/tests/matrix/gemini_test.go +0 -117
  142. package/services/aigateway/tests/matrix/matrix_test.go +0 -601
  143. package/services/aigateway/tests/matrix/openai_test.go +0 -66
  144. package/services/aigateway/tests/matrix/vertex_test.go +0 -160
@@ -1,101 +0,0 @@
1
- package gatewaytracer
2
-
3
- import (
4
- "net/http"
5
-
6
- otelapi "go.opentelemetry.io/otel"
7
- "go.opentelemetry.io/otel/attribute"
8
- "go.opentelemetry.io/otel/codes"
9
- "go.opentelemetry.io/otel/trace"
10
- "go.uber.org/zap"
11
-
12
- "github.com/langwatch/langwatch/pkg/clog"
13
- )
14
-
15
- // Middleware creates a chi middleware that wraps each request in a gateway span.
16
- // It uses the globally-registered TracerProvider (set by pkg/otelsetup).
17
- func Middleware(spanNamer func(*http.Request) string) func(http.Handler) http.Handler {
18
- tracer := otelapi.Tracer("langwatch-ai-gateway")
19
-
20
- return func(next http.Handler) http.Handler {
21
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22
- name := "lw_gateway.request"
23
- if spanNamer != nil {
24
- if n := spanNamer(r); n != "" {
25
- name = n
26
- }
27
- }
28
-
29
- // Always start a fresh root — never inherit client traceparent.
30
- // Our ops trace ID is ours; the customer's trace is separate.
31
- ctx, span := tracer.Start(r.Context(), name,
32
- trace.WithNewRoot(),
33
- trace.WithSpanKind(trace.SpanKindServer),
34
- trace.WithAttributes(
35
- attribute.String(AttrOrigin, OriginGateway),
36
- attribute.String("http.request.method", r.Method),
37
- attribute.String("url.path", r.URL.Path),
38
- ),
39
- )
40
- sc := span.SpanContext()
41
- if sc.IsValid() {
42
- w.Header().Set(HeaderTraceID, sc.TraceID().String())
43
- w.Header().Set(HeaderSpanID, sc.SpanID().String())
44
-
45
- // Stamp trace/span on the context logger for all downstream logs.
46
- ctx = clog.With(ctx,
47
- zap.String("trace_id", sc.TraceID().String()),
48
- zap.String("span_id", sc.SpanID().String()),
49
- )
50
- }
51
-
52
- rec := &statusRecorder{ResponseWriter: w, status: 200}
53
- defer func() {
54
- if rec.status >= 400 {
55
- span.SetStatus(codes.Error, http.StatusText(rec.status))
56
- } else {
57
- span.SetStatus(codes.Ok, "")
58
- }
59
- span.SetAttributes(attribute.Int("http.response.status_code", rec.status))
60
- span.End()
61
- }()
62
-
63
- next.ServeHTTP(rec, r.WithContext(ctx))
64
- })
65
- }
66
- }
67
-
68
- // DefaultSpanName maps routes to canonical span names.
69
- func DefaultSpanName(r *http.Request) string {
70
- switch r.URL.Path {
71
- case "/v1/chat/completions":
72
- return "lw_gateway.chat_completions"
73
- case "/v1/messages":
74
- return "lw_gateway.messages"
75
- case "/v1/embeddings":
76
- return "lw_gateway.embeddings"
77
- case "/v1/models":
78
- return "lw_gateway.models"
79
- }
80
- return "lw_gateway.request"
81
- }
82
-
83
- type statusRecorder struct {
84
- http.ResponseWriter
85
- status int
86
- }
87
-
88
- func (r *statusRecorder) WriteHeader(code int) {
89
- r.status = code
90
- r.ResponseWriter.WriteHeader(code)
91
- }
92
-
93
- func (r *statusRecorder) Unwrap() http.ResponseWriter {
94
- return r.ResponseWriter
95
- }
96
-
97
- func (r *statusRecorder) Flush() {
98
- if f, ok := r.ResponseWriter.(http.Flusher); ok {
99
- f.Flush()
100
- }
101
- }
@@ -1,83 +0,0 @@
1
- package httpapi
2
-
3
- import (
4
- "context"
5
- "net/http"
6
- "net/http/httptest"
7
- "strings"
8
- "testing"
9
-
10
- "go.uber.org/zap"
11
-
12
- "github.com/langwatch/langwatch/pkg/clog"
13
- "github.com/langwatch/langwatch/services/aigateway/adapters/customertracebridge"
14
- "github.com/langwatch/langwatch/services/aigateway/app"
15
- "github.com/langwatch/langwatch/services/aigateway/domain"
16
- )
17
-
18
- // stubAuth is a no-op auth resolver that returns a minimal bundle.
19
- type stubAuth struct{ bundle *domain.Bundle }
20
-
21
- func (s *stubAuth) Resolve(_ context.Context, _ string) (*domain.Bundle, error) {
22
- return s.bundle, nil
23
- }
24
-
25
- // stubProviders returns a canned response.
26
- type stubProviders struct{}
27
-
28
- func (s *stubProviders) Dispatch(_ context.Context, _ *domain.Request, _ domain.Credential) (*domain.Response, error) {
29
- return &domain.Response{
30
- StatusCode: 200,
31
- Body: []byte(`{"id":"chatcmpl-1","choices":[{"message":{"content":"pong"}}],"usage":{"prompt_tokens":4,"completion_tokens":1}}`),
32
- }, nil
33
- }
34
- func (s *stubProviders) DispatchStream(_ context.Context, _ *domain.Request, _ domain.Credential) (domain.StreamIterator, error) {
35
- return nil, nil
36
- }
37
- func (s *stubProviders) ListModels(_ context.Context, _ []domain.Credential) ([]domain.Model, error) {
38
- return nil, nil
39
- }
40
-
41
- // BenchmarkRouter_ChatCompletions measures the full HTTP round-trip through
42
- // the chi router with stubbed auth + providers (no real network).
43
- // This captures the overhead of chi routing, middleware stack, body read,
44
- // JSON peek, pipeline dispatch, and response write.
45
- func BenchmarkRouter_ChatCompletions(b *testing.B) {
46
- bundle := &domain.Bundle{
47
- VirtualKeyID: "vk_bench",
48
- ProjectID: "proj_bench",
49
- TeamID: "team_bench",
50
- Credentials: []domain.Credential{
51
- {ID: "cred_1", ProviderID: "openai", APIKey: "sk-test"},
52
- },
53
- Config: domain.BundleConfig{},
54
- }
55
-
56
- application := app.New(
57
- app.WithAuth(&stubAuth{bundle: bundle}),
58
- app.WithProviders(&stubProviders{}),
59
- app.WithLogger(zap.NewNop()),
60
- )
61
-
62
- router := NewRouter(RouterDeps{
63
- App: application,
64
- Logger: zap.NewNop(),
65
- Version: "bench",
66
- TraceRegistry: customertracebridge.NewRegistry(),
67
- })
68
-
69
- // Suppress telemetry middleware log output by setting a nop logger on the request context.
70
- ctx := clog.Set(context.Background(), zap.NewNop())
71
-
72
- body := `{"model":"gpt-5-mini","messages":[{"role":"user","content":"ping"}]}`
73
- b.ResetTimer()
74
- b.ReportAllocs()
75
-
76
- for i := 0; i < b.N; i++ {
77
- req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(body)).WithContext(ctx)
78
- req.Header.Set("Authorization", "Bearer lw_vk_live_01HZX0123456789ABCDEFGHIJ")
79
- req.Header.Set("Content-Type", "application/json")
80
- rec := httptest.NewRecorder()
81
- router.ServeHTTP(rec, req)
82
- }
83
- }
@@ -1,123 +0,0 @@
1
- package httpapi
2
-
3
- import (
4
- "context"
5
- "net/http"
6
- "strings"
7
-
8
- "go.uber.org/zap"
9
-
10
- "github.com/langwatch/langwatch/pkg/clog"
11
- "github.com/langwatch/langwatch/pkg/herr"
12
- "github.com/langwatch/langwatch/services/aigateway/adapters/customertracebridge"
13
- "github.com/langwatch/langwatch/services/aigateway/app"
14
- "github.com/langwatch/langwatch/services/aigateway/domain"
15
- )
16
-
17
- type bundleCtxKey struct{}
18
-
19
- // AuthMiddleware resolves bearer tokens and attaches the bundle to context.
20
- // If resolver is nil, all requests are rejected (fail closed).
21
- func AuthMiddleware(resolver app.AuthResolver) func(http.Handler) http.Handler {
22
- return func(next http.Handler) http.Handler {
23
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
24
- if resolver == nil {
25
- herr.WriteHTTP(w, herr.New(r.Context(), domain.ErrInternal, nil))
26
- return
27
- }
28
-
29
- token := extractToken(r)
30
- if token == "" {
31
- herr.WriteHTTP(w, herr.New(r.Context(), domain.ErrInvalidAPIKey, herr.M{
32
- "message": "missing API key; supply Authorization: Bearer <key>, x-api-key, or x-goog-api-key header",
33
- }))
34
- return
35
- }
36
-
37
- bundle, err := resolver.Resolve(r.Context(), token)
38
- if err != nil {
39
- herr.WriteHTTP(w, err)
40
- return
41
- }
42
-
43
- ctx := context.WithValue(r.Context(), bundleCtxKey{}, bundle)
44
-
45
- // Enrich context logger with identity fields.
46
- ctx = clog.With(ctx,
47
- zap.String("project_id", bundle.ProjectID),
48
- zap.String("team_id", bundle.TeamID),
49
- )
50
-
51
- next.ServeHTTP(w, r.WithContext(ctx))
52
- })
53
- }
54
- }
55
-
56
- // TraceRegistryMiddleware registers the project's OTLP endpoint after auth
57
- // resolves the bundle. Must run after AuthMiddleware.
58
- func TraceRegistryMiddleware(registry *customertracebridge.Registry, defaultEndpoint string) func(http.Handler) http.Handler {
59
- return func(next http.Handler) http.Handler {
60
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61
- bundle := BundleFromContext(r.Context())
62
- if bundle == nil {
63
- herr.WriteHTTP(w, herr.New(r.Context(), domain.ErrInternal, herr.M{
64
- "message": "TraceRegistryMiddleware requires auth to run first",
65
- }))
66
- return
67
- }
68
- if registry != nil {
69
- if err := registry.SetFromBundle(
70
- bundle.ProjectID, bundle.Config.ProjectOTLPToken, defaultEndpoint,
71
- ); err != nil {
72
- clog.Get(r.Context()).Warn("otlp_endpoint_rejected",
73
- zap.String("project_id", bundle.ProjectID), zap.Error(err))
74
- }
75
- }
76
- next.ServeHTTP(w, r)
77
- })
78
- }
79
- }
80
-
81
- // CustomerTraceMiddleware stashes the inbound traceparent for the bridge and
82
- // starts a fresh gateway-owned trace context for internal spans.
83
- func CustomerTraceMiddleware() func(http.Handler) http.Handler {
84
- return func(next http.Handler) http.Handler {
85
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
86
- ctx := r.Context()
87
-
88
- // Preserve the customer's traceparent so the bridge can link to it.
89
- ctx = customertracebridge.WithTraceParent(ctx, r.Header.Get("Traceparent"))
90
- r.Header.Del("Traceparent")
91
- r.Header.Del("Tracestate")
92
-
93
- next.ServeHTTP(w, r.WithContext(ctx))
94
- })
95
- }
96
- }
97
-
98
- // BundleFromContext returns the resolved bundle from the request context.
99
- func BundleFromContext(ctx context.Context) *domain.Bundle {
100
- if v, ok := ctx.Value(bundleCtxKey{}).(*domain.Bundle); ok {
101
- return v
102
- }
103
- return nil
104
- }
105
-
106
- func extractToken(r *http.Request) string {
107
- if a := r.Header.Get("Authorization"); a != "" {
108
- if len(a) > 7 && strings.EqualFold(a[:7], "Bearer ") {
109
- return strings.TrimSpace(a[7:])
110
- }
111
- }
112
- if k := r.Header.Get("X-Api-Key"); k != "" {
113
- return strings.TrimSpace(k)
114
- }
115
- // X-Goog-Api-Key — Gemini SDK's canonical auth header (gemini-cli,
116
- // google-genai SDK). Lets a Gemini-native client point at the gateway
117
- // without changing its auth wiring; the VK secret slots into the same
118
- // place the SDK would normally put a Google API key.
119
- if k := r.Header.Get("X-Goog-Api-Key"); k != "" {
120
- return strings.TrimSpace(k)
121
- }
122
- return ""
123
- }