@langwatch/server 3.1.0-beta.3 → 3.1.0-beta.5

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 (119) hide show
  1. package/langwatch/ee/admin/ImpersonationBanner.tsx +61 -0
  2. package/langwatch/ee/admin/ImpersonationSwitchBackMenuItem.tsx +30 -0
  3. package/langwatch/ee/admin/__tests__/ImpersonationBanner.integration.test.tsx +82 -0
  4. package/langwatch/ee/admin/__tests__/impersonation.service.unit.test.ts +231 -0
  5. package/langwatch/ee/admin/backoffice/BackofficeTable.tsx +200 -0
  6. package/langwatch/ee/admin/backoffice/__tests__/adminClient.unit.test.ts +147 -0
  7. package/langwatch/ee/admin/backoffice/__tests__/dateInputToISO.unit.test.ts +42 -0
  8. package/langwatch/ee/admin/backoffice/__tests__/userVisibility.unit.test.ts +174 -0
  9. package/langwatch/ee/admin/backoffice/adminClient.ts +114 -0
  10. package/langwatch/ee/admin/backoffice/resources/OrganizationFeaturesView.tsx +373 -0
  11. package/langwatch/ee/admin/backoffice/resources/OrganizationsView.tsx +677 -0
  12. package/langwatch/ee/admin/backoffice/resources/ProjectsView.tsx +647 -0
  13. package/langwatch/ee/admin/backoffice/resources/SubscriptionsView.tsx +628 -0
  14. package/langwatch/ee/admin/backoffice/resources/UsersView.tsx +556 -0
  15. package/langwatch/ee/admin/backoffice/useAdminResource.ts +104 -0
  16. package/langwatch/ee/admin/backoffice/userVisibility.ts +105 -0
  17. package/langwatch/ee/admin/impersonation.service.ts +170 -0
  18. package/langwatch/ee/admin/isAdmin.ts +9 -0
  19. package/langwatch/ee/admin/routes/admin.ts +531 -0
  20. package/langwatch/ee/admin/safeSelects.ts +64 -0
  21. package/langwatch/ee/billing/__tests__/customerService.unit.test.ts +183 -0
  22. package/langwatch/ee/billing/__tests__/growthSeatEvent.unit.test.ts +213 -0
  23. package/langwatch/ee/billing/__tests__/licensePurchaseHandler.unit.test.ts +250 -0
  24. package/langwatch/ee/billing/__tests__/notification.service.unit.test.ts +768 -0
  25. package/langwatch/ee/billing/__tests__/planLimits.unit.test.ts +19 -0
  26. package/langwatch/ee/billing/__tests__/planProvider.unit.test.ts +440 -0
  27. package/langwatch/ee/billing/__tests__/planTypes.unit.test.ts +32 -0
  28. package/langwatch/ee/billing/__tests__/pricing.unit.test.ts +54 -0
  29. package/langwatch/ee/billing/__tests__/seatEventSubscription.unit.test.ts +717 -0
  30. package/langwatch/ee/billing/__tests__/stripe.integration.test.ts +344 -0
  31. package/langwatch/ee/billing/__tests__/stripePrices.types.unit.test.ts +27 -0
  32. package/langwatch/ee/billing/__tests__/stripePricesLoader.unit.test.ts +72 -0
  33. package/langwatch/ee/billing/__tests__/subscription.service.unit.test.ts +730 -0
  34. package/langwatch/ee/billing/__tests__/subscriptionItemCalculator.unit.test.ts +176 -0
  35. package/langwatch/ee/billing/__tests__/usage-limit.service.unit.test.ts +706 -0
  36. package/langwatch/ee/billing/__tests__/usageReportingService.integration.test.ts +421 -0
  37. package/langwatch/ee/billing/__tests__/usageReportingService.unit.test.ts +515 -0
  38. package/langwatch/ee/billing/__tests__/webhookService.unit.test.ts +958 -0
  39. package/langwatch/ee/billing/currencyRouter.ts +18 -0
  40. package/langwatch/ee/billing/errors.ts +129 -0
  41. package/langwatch/ee/billing/index.ts +67 -0
  42. package/langwatch/ee/billing/middleware.ts +17 -0
  43. package/langwatch/ee/billing/notifications/notification.service.ts +651 -0
  44. package/langwatch/ee/billing/notifications/repositories/notification.repository.ts +51 -0
  45. package/langwatch/ee/billing/notifications/types.ts +9 -0
  46. package/langwatch/ee/billing/notifications/usage-limit.service.ts +622 -0
  47. package/langwatch/ee/billing/nurturing/hooks/activityTracking.ts +90 -0
  48. package/langwatch/ee/billing/nurturing/hooks/activityTracking.unit.test.ts +237 -0
  49. package/langwatch/ee/billing/nurturing/hooks/featureAdoption.ts +120 -0
  50. package/langwatch/ee/billing/nurturing/hooks/featureAdoption.unit.test.ts +324 -0
  51. package/langwatch/ee/billing/nurturing/hooks/index.ts +17 -0
  52. package/langwatch/ee/billing/nurturing/hooks/productInterest.ts +68 -0
  53. package/langwatch/ee/billing/nurturing/hooks/productInterest.unit.test.ts +138 -0
  54. package/langwatch/ee/billing/nurturing/hooks/promptCreation.integration.test.ts +288 -0
  55. package/langwatch/ee/billing/nurturing/hooks/promptCreation.ts +131 -0
  56. package/langwatch/ee/billing/nurturing/hooks/promptCreation.unit.test.ts +134 -0
  57. package/langwatch/ee/billing/nurturing/hooks/signupIdentification.ts +101 -0
  58. package/langwatch/ee/billing/nurturing/hooks/signupIdentification.unit.test.ts +317 -0
  59. package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.ts +49 -0
  60. package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.unit.test.ts +170 -0
  61. package/langwatch/ee/billing/nurturing/hooks/userSync.ts +143 -0
  62. package/langwatch/ee/billing/nurturing/hooks/userSync.unit.test.ts +307 -0
  63. package/langwatch/ee/billing/nurturing/nurturing.service.ts +179 -0
  64. package/langwatch/ee/billing/nurturing/nurturing.service.unit.test.ts +266 -0
  65. package/langwatch/ee/billing/nurturing/nurturing.service.wiring.unit.test.ts +109 -0
  66. package/langwatch/ee/billing/nurturing/types.ts +125 -0
  67. package/langwatch/ee/billing/planLimits.ts +190 -0
  68. package/langwatch/ee/billing/planProvider.ts +119 -0
  69. package/langwatch/ee/billing/planTypes.ts +53 -0
  70. package/langwatch/ee/billing/pricing.ts +57 -0
  71. package/langwatch/ee/billing/services/billableEventsQuery.ts +248 -0
  72. package/langwatch/ee/billing/services/customerService.ts +90 -0
  73. package/langwatch/ee/billing/services/licensePurchaseHandler.ts +73 -0
  74. package/langwatch/ee/billing/services/seatEventSubscription.ts +351 -0
  75. package/langwatch/ee/billing/services/seatSyncService.ts +34 -0
  76. package/langwatch/ee/billing/services/subscription.repository.ts +203 -0
  77. package/langwatch/ee/billing/services/subscription.service.ts +549 -0
  78. package/langwatch/ee/billing/services/subscriptionItemCalculator.ts +249 -0
  79. package/langwatch/ee/billing/services/usageReportingService.ts +301 -0
  80. package/langwatch/ee/billing/services/webhookService.ts +858 -0
  81. package/langwatch/ee/billing/stripe/stripeCatalog.json +1781 -0
  82. package/langwatch/ee/billing/stripe/stripeClient.ts +13 -0
  83. package/langwatch/ee/billing/stripe/stripePriceCatalog.ts +70 -0
  84. package/langwatch/ee/billing/stripe/stripePrices.types.ts +118 -0
  85. package/langwatch/ee/billing/subscriptionRouter.ts +202 -0
  86. package/langwatch/ee/billing/types.ts +102 -0
  87. package/langwatch/ee/billing/utils/currency.ts +99 -0
  88. package/langwatch/ee/billing/utils/growthSeatEvent.ts +137 -0
  89. package/langwatch/ee/licensing/__tests__/constants.unit.test.ts +56 -0
  90. package/langwatch/ee/licensing/__tests__/defaults.unit.test.ts +192 -0
  91. package/langwatch/ee/licensing/__tests__/fixtures/testKeys.ts +86 -0
  92. package/langwatch/ee/licensing/__tests__/fixtures/testLicenses.ts +92 -0
  93. package/langwatch/ee/licensing/__tests__/licenseGenerationService.unit.test.ts +232 -0
  94. package/langwatch/ee/licensing/__tests__/licenseHandler.integration.test.ts +419 -0
  95. package/langwatch/ee/licensing/__tests__/planMapping.unit.test.ts +195 -0
  96. package/langwatch/ee/licensing/__tests__/planTemplates.unit.test.ts +193 -0
  97. package/langwatch/ee/licensing/__tests__/signing.unit.test.ts +164 -0
  98. package/langwatch/ee/licensing/__tests__/validation.unit.test.ts +281 -0
  99. package/langwatch/ee/licensing/client.ts +29 -0
  100. package/langwatch/ee/licensing/constants.ts +195 -0
  101. package/langwatch/ee/licensing/defaults.ts +55 -0
  102. package/langwatch/ee/licensing/errors.ts +11 -0
  103. package/langwatch/ee/licensing/index.ts +52 -0
  104. package/langwatch/ee/licensing/licenseGenerationService.ts +87 -0
  105. package/langwatch/ee/licensing/licenseHandler.ts +308 -0
  106. package/langwatch/ee/licensing/planInfo.ts +46 -0
  107. package/langwatch/ee/licensing/planMapping.ts +37 -0
  108. package/langwatch/ee/licensing/planTemplates.ts +109 -0
  109. package/langwatch/ee/licensing/server.ts +43 -0
  110. package/langwatch/ee/licensing/signing.ts +43 -0
  111. package/langwatch/ee/licensing/types.ts +156 -0
  112. package/langwatch/ee/licensing/validation.ts +107 -0
  113. package/langwatch/ee/managed-providers/ManagedModelProviderAlert.tsx +29 -0
  114. package/langwatch/ee/managed-providers/managedBedrockConfig.ts +226 -0
  115. package/langwatch/ee/saas/ExtraFooterComponents.tsx +169 -0
  116. package/langwatch/package.json +1 -1
  117. package/langwatch_nlp/uv.lock +179 -5
  118. package/package.json +1 -1
  119. package/packages/server/dist/cli.cjs +192 -142
@@ -0,0 +1,281 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ isExpired,
4
+ parseLicenseKey,
5
+ validateLicense,
6
+ verifySignature,
7
+ } from "../validation";
8
+ import {
9
+ BASE_LICENSE,
10
+ VALID_LICENSE_KEY,
11
+ EXPIRED_LICENSE_KEY,
12
+ TAMPERED_LICENSE_KEY,
13
+ EMPTY_SIGNATURE_KEY,
14
+ MALFORMED_BASE64,
15
+ INVALID_JSON_BASE64,
16
+ GARBAGE_DATA,
17
+ } from "./fixtures/testLicenses";
18
+ import { TEST_PUBLIC_KEY, WRONG_PUBLIC_KEY } from "./fixtures/testKeys";
19
+
20
+ describe("parseLicenseKey", () => {
21
+ it("parses valid base64-encoded license key", () => {
22
+ const result = parseLicenseKey(VALID_LICENSE_KEY);
23
+
24
+ expect(result).not.toBeNull();
25
+ expect(result?.data.organizationName).toBe("Acme Corp");
26
+ expect(result?.signature).toBeDefined();
27
+ });
28
+
29
+ it("returns null for malformed base64 input", () => {
30
+ const result = parseLicenseKey(MALFORMED_BASE64);
31
+
32
+ expect(result).toBeNull();
33
+ });
34
+
35
+ it("returns null for valid base64 but invalid JSON", () => {
36
+ const result = parseLicenseKey(INVALID_JSON_BASE64);
37
+
38
+ expect(result).toBeNull();
39
+ });
40
+
41
+ it("returns null for empty license key", () => {
42
+ const result = parseLicenseKey("");
43
+
44
+ expect(result).toBeNull();
45
+ });
46
+
47
+ it("returns null for whitespace-only license key", () => {
48
+ const result = parseLicenseKey(" ");
49
+
50
+ expect(result).toBeNull();
51
+ });
52
+
53
+ it("returns null for valid JSON but missing required fields", () => {
54
+ const invalidStructure = Buffer.from(
55
+ JSON.stringify({ foo: "bar" })
56
+ ).toString("base64");
57
+
58
+ const result = parseLicenseKey(invalidStructure);
59
+
60
+ expect(result).toBeNull();
61
+ });
62
+ });
63
+
64
+ describe("verifySignature", () => {
65
+ it("verifies valid RSA-SHA256 signature", () => {
66
+ const signedLicense = parseLicenseKey(VALID_LICENSE_KEY);
67
+
68
+ expect(signedLicense).not.toBeNull();
69
+ if (!signedLicense) throw new Error("Expected signedLicense to be defined");
70
+ const result = verifySignature(signedLicense, TEST_PUBLIC_KEY);
71
+
72
+ expect(result).toBe(true);
73
+ });
74
+
75
+ it("rejects tampered license data", () => {
76
+ const signedLicense = parseLicenseKey(TAMPERED_LICENSE_KEY);
77
+
78
+ expect(signedLicense).not.toBeNull();
79
+ if (!signedLicense) throw new Error("Expected signedLicense to be defined");
80
+ const result = verifySignature(signedLicense, TEST_PUBLIC_KEY);
81
+
82
+ expect(result).toBe(false);
83
+ });
84
+
85
+ it("rejects license verified with different public key", () => {
86
+ const signedLicense = parseLicenseKey(VALID_LICENSE_KEY);
87
+
88
+ expect(signedLicense).not.toBeNull();
89
+ if (!signedLicense) throw new Error("Expected signedLicense to be defined");
90
+ const result = verifySignature(signedLicense, WRONG_PUBLIC_KEY);
91
+
92
+ expect(result).toBe(false);
93
+ });
94
+
95
+ it("rejects license with empty signature", () => {
96
+ const signedLicense = parseLicenseKey(EMPTY_SIGNATURE_KEY);
97
+
98
+ expect(signedLicense).not.toBeNull();
99
+ if (!signedLicense) throw new Error("Expected signedLicense to be defined");
100
+ const result = verifySignature(signedLicense, TEST_PUBLIC_KEY);
101
+
102
+ expect(result).toBe(false);
103
+ });
104
+ });
105
+
106
+ describe("isExpired", () => {
107
+ it("returns false when expiration is in the future", () => {
108
+ const futureDate = "2030-12-31T23:59:59Z";
109
+
110
+ const result = isExpired(futureDate);
111
+
112
+ expect(result).toBe(false);
113
+ });
114
+
115
+ it("returns true when expiration is in the past", () => {
116
+ const pastDate = "2020-01-01T00:00:00Z";
117
+
118
+ const result = isExpired(pastDate);
119
+
120
+ expect(result).toBe(true);
121
+ });
122
+
123
+ it("returns true at exactly the expiration time", () => {
124
+ const now = new Date("2024-06-15T12:00:00Z");
125
+ const expiresAt = "2024-06-15T12:00:00Z";
126
+
127
+ const result = isExpired(expiresAt, now);
128
+
129
+ expect(result).toBe(true);
130
+ });
131
+
132
+ it("returns false one millisecond before expiration", () => {
133
+ const now = new Date("2024-06-15T11:59:59.999Z");
134
+ const expiresAt = "2024-06-15T12:00:00Z";
135
+
136
+ const result = isExpired(expiresAt, now);
137
+
138
+ expect(result).toBe(false);
139
+ });
140
+
141
+ it("returns true for invalid date string (security fallback)", () => {
142
+ const result = isExpired("not-a-valid-date");
143
+
144
+ expect(result).toBe(true);
145
+ });
146
+ });
147
+
148
+ describe("validateLicense", () => {
149
+ it("validates complete license successfully", () => {
150
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
151
+
152
+ expect(result.valid).toBe(true);
153
+ if (result.valid) {
154
+ expect(result.planInfo.maxMembers).toBe(BASE_LICENSE.plan.maxMembers);
155
+ }
156
+ });
157
+
158
+ it("fails validation for invalid format", () => {
159
+ const result = validateLicense(GARBAGE_DATA, TEST_PUBLIC_KEY);
160
+
161
+ expect(result.valid).toBe(false);
162
+ if (!result.valid) {
163
+ expect(result.error).toBe("Invalid license format");
164
+ }
165
+ });
166
+
167
+ it("fails validation for invalid signature", () => {
168
+ const result = validateLicense(TAMPERED_LICENSE_KEY, TEST_PUBLIC_KEY);
169
+
170
+ expect(result.valid).toBe(false);
171
+ if (!result.valid) {
172
+ expect(result.error).toBe("Invalid signature");
173
+ }
174
+ });
175
+
176
+ it("fails validation for expired license", () => {
177
+ const result = validateLicense(EXPIRED_LICENSE_KEY, TEST_PUBLIC_KEY);
178
+
179
+ expect(result.valid).toBe(false);
180
+ if (!result.valid) {
181
+ expect(result.error).toBe("License expired");
182
+ }
183
+ });
184
+
185
+ describe("extracts license fields correctly", () => {
186
+ it("extracts licenseId", () => {
187
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
188
+
189
+ expect(result.valid).toBe(true);
190
+ if (result.valid) {
191
+ expect(result.licenseData.licenseId).toBe(BASE_LICENSE.licenseId);
192
+ }
193
+ });
194
+
195
+ it("extracts organizationName", () => {
196
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
197
+
198
+ expect(result.valid).toBe(true);
199
+ if (result.valid) {
200
+ expect(result.licenseData.organizationName).toBe(BASE_LICENSE.organizationName);
201
+ }
202
+ });
203
+
204
+ it("extracts email", () => {
205
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
206
+
207
+ expect(result.valid).toBe(true);
208
+ if (result.valid) {
209
+ expect(result.licenseData.email).toBe(BASE_LICENSE.email);
210
+ }
211
+ });
212
+
213
+ it("extracts plan.type", () => {
214
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
215
+
216
+ expect(result.valid).toBe(true);
217
+ if (result.valid) {
218
+ expect(result.licenseData.plan.type).toBe(BASE_LICENSE.plan.type);
219
+ }
220
+ });
221
+
222
+ it("extracts plan.maxMembers", () => {
223
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
224
+
225
+ expect(result.valid).toBe(true);
226
+ if (result.valid) {
227
+ expect(result.licenseData.plan.maxMembers).toBe(BASE_LICENSE.plan.maxMembers);
228
+ }
229
+ });
230
+
231
+ it("extracts plan.maxProjects", () => {
232
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
233
+
234
+ expect(result.valid).toBe(true);
235
+ if (result.valid) {
236
+ expect(result.licenseData.plan.maxProjects).toBe(BASE_LICENSE.plan.maxProjects);
237
+ }
238
+ });
239
+
240
+ it("extracts plan.maxMessagesPerMonth", () => {
241
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
242
+
243
+ expect(result.valid).toBe(true);
244
+ if (result.valid) {
245
+ expect(result.licenseData.plan.maxMessagesPerMonth).toBe(BASE_LICENSE.plan.maxMessagesPerMonth);
246
+ }
247
+ });
248
+
249
+ it("validates old licenses that include evaluationsCredit (backward compat)", () => {
250
+ // VALID_LICENSE_KEY contains evaluationsCredit in the signed payload.
251
+ // After making the field optional, old licenses must still parse and
252
+ // pass signature verification without error.
253
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
254
+
255
+ expect(result.valid).toBe(true);
256
+ if (result.valid) {
257
+ // The field is declared as optional on the schema, so Zod preserves its value on parse.
258
+ expect(result.licenseData.plan.evaluationsCredit).toBe(BASE_LICENSE.plan.evaluationsCredit);
259
+ }
260
+ });
261
+
262
+ it("extracts plan.maxWorkflows", () => {
263
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
264
+
265
+ expect(result.valid).toBe(true);
266
+ if (result.valid) {
267
+ expect(result.licenseData.plan.maxWorkflows).toBe(BASE_LICENSE.plan.maxWorkflows);
268
+ }
269
+ });
270
+
271
+ it("extracts plan.canPublish", () => {
272
+ const result = validateLicense(VALID_LICENSE_KEY, TEST_PUBLIC_KEY);
273
+
274
+ expect(result.valid).toBe(true);
275
+ if (result.valid) {
276
+ expect(result.licenseData.plan.canPublish).toBe(BASE_LICENSE.plan.canPublish);
277
+ }
278
+ });
279
+ });
280
+ });
281
+
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Client-safe exports for the licensing module.
3
+ *
4
+ * This barrel re-exports only types and constants that do NOT depend on
5
+ * Node.js built-ins (crypto, Buffer, etc.). Use this for client-side
6
+ * imports. For server-side code, import from the main barrel or specific files.
7
+ */
8
+
9
+ // Types (always client-safe)
10
+ export type {
11
+ LicenseData,
12
+ LicensePlanLimits,
13
+ LicenseStatus,
14
+ SignedLicense,
15
+ ValidationResult,
16
+ } from "./types";
17
+
18
+ // PlanInfo type (client-safe, no server dependencies)
19
+ export type { PlanInfo } from "./planInfo";
20
+
21
+ // Constants (plain objects, no Node.js dependencies)
22
+ export { FREE_PLAN, LICENSE_ERRORS, UNLIMITED_PLAN } from "./constants";
23
+ export type { LicenseError } from "./constants";
24
+
25
+ // Plan Templates (plain objects, client-safe)
26
+ export { PRO_TEMPLATE, ENTERPRISE_TEMPLATE, getPlanTemplate } from "./planTemplates";
27
+
28
+ // Errors (plain JS classes)
29
+ export { OrganizationNotFoundError } from "./errors";
@@ -0,0 +1,195 @@
1
+ import type { PlanInfo } from "./planInfo";
2
+
3
+ export const CONTACT_SALES_URL =
4
+ "https://meetings-eu1.hubspot.com/manouk-draisma?uuid=3c29cf0c-03e5-4a53-81fd-94abb0b66cfd";
5
+
6
+ export const DEFAULT_LICENSE_PURCHASE_URL =
7
+ "https://buy.stripe.com/dRm3cwaIDgXs6yK6sX0480f";
8
+
9
+ /**
10
+ * Default limit for fields not present in older licenses.
11
+ * Using a large number instead of Infinity for JSON serialization safety.
12
+ * Note: Infinity cannot be serialized to JSON (becomes null), so we use
13
+ * Number.MAX_SAFE_INTEGER which is serializable and effectively unlimited.
14
+ */
15
+ export const DEFAULT_LIMIT = Number.MAX_SAFE_INTEGER;
16
+
17
+ /**
18
+ * Default value for maxMembersLite when not specified in license.
19
+ */
20
+ export const DEFAULT_MEMBERS_LITE = 1;
21
+
22
+ /**
23
+ * LICENSE_ERRORS: Standardized error messages for license validation.
24
+ * Used across validation.ts and types.ts to ensure consistency.
25
+ */
26
+ export const LICENSE_ERRORS = {
27
+ INVALID_FORMAT: "Invalid license format",
28
+ INVALID_SIGNATURE: "Invalid signature",
29
+ EXPIRED: "License expired",
30
+ } as const;
31
+
32
+ /**
33
+ * User-friendly error messages for display in the UI.
34
+ * Maps technical errors to human-readable messages.
35
+ */
36
+ export const LICENSE_ERROR_MESSAGES = {
37
+ [LICENSE_ERRORS.INVALID_FORMAT]:
38
+ "The license key is invalid or has been tampered with. Please check the key and try again.",
39
+ [LICENSE_ERRORS.INVALID_SIGNATURE]:
40
+ "The license key is invalid or has been tampered with. Please check the key and try again.",
41
+ [LICENSE_ERRORS.EXPIRED]:
42
+ "This license has expired. Please contact support to renew your license.",
43
+ } as const;
44
+
45
+ /**
46
+ * Returns a user-friendly error message for a given license error.
47
+ * Falls back to the original error if not found in the mapping.
48
+ */
49
+ export function getUserFriendlyLicenseError(error: string): string {
50
+ return (
51
+ LICENSE_ERROR_MESSAGES[error as keyof typeof LICENSE_ERROR_MESSAGES] ??
52
+ error
53
+ );
54
+ }
55
+
56
+ export type LicenseError = (typeof LICENSE_ERRORS)[keyof typeof LICENSE_ERRORS];
57
+
58
+ /** Free tier resource limits - designed for individual evaluation/POC use */
59
+ const FREE_TIER_LIMITS = {
60
+ /** Single operator model */
61
+ MEMBERS: 1,
62
+ /** Members Lite requires paid plan */
63
+ MEMBERS_LITE: 0,
64
+ /** Single team for individual use */
65
+ TEAMS: 1,
66
+ /** Enough for a small POC */
67
+ PROJECTS: 2,
68
+ /** Minimal experimentation allowance */
69
+ WORKFLOWS: 3,
70
+ /** Minimal experimentation allowance */
71
+ PROMPTS: 3,
72
+ /** Minimal experimentation allowance */
73
+ EVALUATORS: 3,
74
+ /** Minimal experimentation allowance */
75
+ SCENARIOS: 3,
76
+ /** Minimal experimentation allowance */
77
+ AGENTS: 3,
78
+ /** Minimal experimentation allowance */
79
+ EXPERIMENTS: 3,
80
+ /** Minimal experimentation allowance */
81
+ ONLINE_EVALUATIONS: 3,
82
+ /** Minimal experimentation allowance */
83
+ DATASETS: 3,
84
+ /** Minimal experimentation allowance */
85
+ DASHBOARDS: 3,
86
+ /** Minimal experimentation allowance */
87
+ CUSTOM_GRAPHS: 3,
88
+ /** Minimal experimentation allowance */
89
+ AUTOMATIONS: 3,
90
+ /** ~33 messages per day */
91
+ MESSAGES_PER_MONTH: 1_000,
92
+ } as const;
93
+
94
+ /**
95
+ * UNLIMITED_PLAN: Default plan for self-hosted deployments without a license.
96
+ * Maintains backward compatibility with current OSS behavior.
97
+ *
98
+ * Uses Number.MAX_SAFE_INTEGER instead of Infinity because:
99
+ * - JSON.stringify(Infinity) returns null, causing silent failures in tRPC
100
+ * - Number.MAX_SAFE_INTEGER is effectively unlimited for practical purposes
101
+ * - The value is 9,007,199,254,740,991 - far beyond any real limit
102
+ */
103
+ export const UNLIMITED_PLAN: PlanInfo = {
104
+ planSource: "free",
105
+ type: "OPEN_SOURCE",
106
+ name: "Open Source",
107
+ free: true,
108
+ overrideAddingLimitations: true,
109
+ maxMembers: Number.MAX_SAFE_INTEGER,
110
+ maxMembersLite: Number.MAX_SAFE_INTEGER,
111
+ maxTeams: Number.MAX_SAFE_INTEGER,
112
+ maxProjects: Number.MAX_SAFE_INTEGER,
113
+ maxMessagesPerMonth: Number.MAX_SAFE_INTEGER,
114
+ maxWorkflows: Number.MAX_SAFE_INTEGER,
115
+ maxPrompts: Number.MAX_SAFE_INTEGER,
116
+ maxEvaluators: Number.MAX_SAFE_INTEGER,
117
+ maxScenarios: Number.MAX_SAFE_INTEGER,
118
+ maxAgents: Number.MAX_SAFE_INTEGER,
119
+ maxExperiments: Number.MAX_SAFE_INTEGER,
120
+ maxOnlineEvaluations: Number.MAX_SAFE_INTEGER,
121
+ maxDatasets: Number.MAX_SAFE_INTEGER,
122
+ maxDashboards: Number.MAX_SAFE_INTEGER,
123
+ maxCustomGraphs: Number.MAX_SAFE_INTEGER,
124
+ maxAutomations: Number.MAX_SAFE_INTEGER,
125
+ canPublish: true,
126
+ usageUnit: "traces",
127
+ prices: {
128
+ USD: 0,
129
+ EUR: 0,
130
+ },
131
+ };
132
+
133
+ /**
134
+ * FREE_PLAN: Fallback plan for expired or invalid licenses.
135
+ * Provides minimal access to encourage license renewal.
136
+ */
137
+ export const FREE_PLAN: PlanInfo = {
138
+ planSource: "free",
139
+ type: "FREE",
140
+ name: "Free",
141
+ free: true,
142
+ overrideAddingLimitations: false,
143
+
144
+ maxMessagesPerMonth: FREE_TIER_LIMITS.MESSAGES_PER_MONTH,
145
+
146
+ maxMembers: FREE_TIER_LIMITS.MEMBERS,
147
+ maxMembersLite: FREE_TIER_LIMITS.MEMBERS_LITE,
148
+ maxTeams: FREE_TIER_LIMITS.TEAMS,
149
+ maxProjects: FREE_TIER_LIMITS.PROJECTS,
150
+
151
+ maxWorkflows: FREE_TIER_LIMITS.WORKFLOWS,
152
+ maxPrompts: FREE_TIER_LIMITS.PROMPTS,
153
+ maxEvaluators: FREE_TIER_LIMITS.EVALUATORS,
154
+ maxScenarios: FREE_TIER_LIMITS.SCENARIOS,
155
+ maxAgents: FREE_TIER_LIMITS.AGENTS,
156
+ maxExperiments: FREE_TIER_LIMITS.EXPERIMENTS,
157
+ maxOnlineEvaluations: FREE_TIER_LIMITS.ONLINE_EVALUATIONS,
158
+ maxDatasets: FREE_TIER_LIMITS.DATASETS,
159
+ maxDashboards: FREE_TIER_LIMITS.DASHBOARDS,
160
+ maxCustomGraphs: FREE_TIER_LIMITS.CUSTOM_GRAPHS,
161
+ maxAutomations: FREE_TIER_LIMITS.AUTOMATIONS,
162
+ canPublish: false,
163
+ usageUnit: "traces",
164
+ prices: {
165
+ USD: 0,
166
+ EUR: 0,
167
+ },
168
+ };
169
+
170
+ /**
171
+ * * Embedded production public key used when no env var is configured.
172
+ * * Enables license verification out-of-the-box; override via env for rotation.
173
+ * DO NOT REPLACE WITH A PLACEHOLDER, LEAVE IT AS IS.
174
+ */
175
+ // gitleaks:allow — public keys
176
+
177
+ const PLACEHOLDER_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
178
+ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvyNNiu5B0lretFaxowsu
179
+ fM907tHWnBITXVDfnpPAwUgzrODdjfTt73XW1S+EDd8AM0FzOpx0YolXipS4+SNK
180
+ axSXwNO0S0XjJGLW7wz9Nv8/PP9V23LtiLQQOj8eGol/texr5pIZy2CRjVeEYcBZ
181
+ GCNz8mT/4tEM8v/NaoTFngsRwNJTuRlro+MZF7eArdBmtIU1fNLchZEH2kojMHKj
182
+ 8vMIyZoTXB4TF/9iXL40eJQUWrVM1llGzJrZ7GhD3lIeyiZz+cQvako1BIthRuUa
183
+ 2qLWpbVP63RSjxphslVvXk1RycL3esr2cj0Pe8loWxeKoxWjnXdLJYWygQh8aUbZ
184
+ iQIDAQAB
185
+ -----END PUBLIC KEY-----`;
186
+
187
+ /**
188
+ * PUBLIC_KEY: RSA public key for license signature verification.
189
+ * This key is used to verify that licenses are signed by LangWatch.
190
+ *
191
+ * Set via LANGWATCH_LICENSE_PUBLIC_KEY environment variable in production.
192
+ * Falls back to placeholder (which will fail validation) if not set.
193
+ */
194
+ export const PUBLIC_KEY =
195
+ process.env.LANGWATCH_LICENSE_PUBLIC_KEY ?? PLACEHOLDER_PUBLIC_KEY;
@@ -0,0 +1,55 @@
1
+ import { DEFAULT_LIMIT, DEFAULT_MEMBERS_LITE } from "./constants";
2
+ import type { LicensePlanLimits } from "./types";
3
+
4
+ const KNOWN_USAGE_UNITS = ["traces", "events"] as const;
5
+
6
+ /**
7
+ * ResolvedPlanLimits has all optional fields made required after defaults are applied.
8
+ * This provides compile-time safety that all limits have defined values.
9
+ */
10
+ // TODO: remove the Omit when evaluationsCredit is dropped from LicensePlanLimitsSchema
11
+ export type ResolvedPlanLimits = Required<Omit<LicensePlanLimits, "evaluationsCredit">>;
12
+
13
+ /**
14
+ * Applies default values to optional fields in LicensePlanLimits.
15
+ *
16
+ * This consolidates scattered nullish coalescing operations into a single function.
17
+ * Optional fields that may be missing in older licenses receive sensible defaults:
18
+ * - maxMembersLite: DEFAULT_MEMBERS_LITE (1)
19
+ * - maxPrompts: DEFAULT_LIMIT (Number.MAX_SAFE_INTEGER - effectively unlimited)
20
+ * - maxEvaluators: DEFAULT_LIMIT (effectively unlimited)
21
+ * - maxScenarios: DEFAULT_LIMIT (effectively unlimited)
22
+ * - usageUnit: "traces"
23
+ *
24
+ * @param plan - License plan limits with optional fields
25
+ * @returns Plan limits with all fields guaranteed to have values
26
+ */
27
+ export function resolvePlanDefaults(plan: LicensePlanLimits): ResolvedPlanLimits {
28
+ return {
29
+ // Pass through required fields unchanged
30
+ type: plan.type,
31
+ name: plan.name,
32
+ maxMembers: plan.maxMembers,
33
+ maxProjects: plan.maxProjects,
34
+ maxMessagesPerMonth: plan.maxMessagesPerMonth,
35
+ maxWorkflows: plan.maxWorkflows,
36
+ canPublish: plan.canPublish,
37
+
38
+ // Apply defaults to optional fields
39
+ maxMembersLite: plan.maxMembersLite ?? DEFAULT_MEMBERS_LITE,
40
+ maxTeams: plan.maxTeams ?? DEFAULT_LIMIT,
41
+ maxPrompts: plan.maxPrompts ?? DEFAULT_LIMIT,
42
+ maxEvaluators: plan.maxEvaluators ?? DEFAULT_LIMIT,
43
+ maxScenarios: plan.maxScenarios ?? DEFAULT_LIMIT,
44
+ maxAgents: plan.maxAgents ?? DEFAULT_LIMIT,
45
+ maxExperiments: plan.maxExperiments ?? DEFAULT_LIMIT,
46
+ maxOnlineEvaluations: plan.maxOnlineEvaluations ?? DEFAULT_LIMIT,
47
+ maxDatasets: plan.maxDatasets ?? DEFAULT_LIMIT,
48
+ maxDashboards: plan.maxDashboards ?? DEFAULT_LIMIT,
49
+ maxCustomGraphs: plan.maxCustomGraphs ?? DEFAULT_LIMIT,
50
+ maxAutomations: plan.maxAutomations ?? DEFAULT_LIMIT,
51
+ usageUnit: KNOWN_USAGE_UNITS.includes(plan.usageUnit as any)
52
+ ? plan.usageUnit!
53
+ : "traces",
54
+ };
55
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Custom error types for licensing domain.
3
+ * These are framework-agnostic and can be mapped to tRPC/HTTP errors in the router layer.
4
+ */
5
+
6
+ export class OrganizationNotFoundError extends Error {
7
+ constructor(message = "Organization not found") {
8
+ super(message);
9
+ this.name = "OrganizationNotFoundError";
10
+ }
11
+ }
@@ -0,0 +1,52 @@
1
+ // Types
2
+ export type {
3
+ LicenseData,
4
+ LicensePlanLimits,
5
+ LicenseStatus,
6
+ RemoveLicenseResult,
7
+ SignedLicense,
8
+ StoreLicenseResult,
9
+ ValidationResult,
10
+ } from "./types";
11
+
12
+ // PlanInfo type (client-safe, no server dependencies)
13
+ export type { PlanInfo } from "./planInfo";
14
+
15
+ // Constants
16
+ export { FREE_PLAN, LICENSE_ERRORS, PUBLIC_KEY, UNLIMITED_PLAN } from "./constants";
17
+ export type { LicenseError } from "./constants";
18
+
19
+ // Validation
20
+ export {
21
+ parseLicenseKey,
22
+ verifySignature,
23
+ isExpired,
24
+ validateLicense,
25
+ } from "./validation";
26
+
27
+ // Plan Mapping
28
+ export { mapToPlanInfo } from "./planMapping";
29
+
30
+ // License Handler
31
+ export { LicenseHandler } from "./licenseHandler";
32
+ export type { ITraceUsageService } from "./licenseHandler";
33
+
34
+ // Repository type (re-exported for factory wiring)
35
+ // NOTE: The class LicenseEnforcementRepository is NOT exported here to avoid
36
+ // bundling server-side code (Prisma) into client components. Import directly
37
+ // from "~/server/license-enforcement/license-enforcement.repository" for server usage.
38
+ export type { ILicenseEnforcementRepository } from "~/server/license-enforcement/license-enforcement.repository";
39
+
40
+ // Errors
41
+ export { OrganizationNotFoundError } from "./errors";
42
+
43
+ // Plan Templates
44
+ export { PRO_TEMPLATE, ENTERPRISE_TEMPLATE, getPlanTemplate } from "./planTemplates";
45
+
46
+ // NOTE: Signing functions (signLicense, encodeLicenseKey, generateLicenseId) are NOT
47
+ // exported here to avoid bundling Node.js crypto in client bundles. Import directly
48
+ // from 'ee/licensing/signing' for server-side usage.
49
+
50
+ // NOTE: Server-side factory (createLicenseHandler) is in ./server.ts to avoid
51
+ // bundling Node.js-only dependencies (Elasticsearch, etc.) in client code.
52
+ // Import from 'ee/licensing/server' for server-side usage.