@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,18 @@
1
+ import { z } from "zod";
2
+ import {
3
+ createTRPCRouter,
4
+ protectedProcedure,
5
+ } from "../../src/server/api/trpc";
6
+ import { skipPermissionCheck } from "../../src/server/api/rbac";
7
+ import { detectCurrencyFromRequest } from "./utils/currency";
8
+
9
+ export const createCurrencyRouter = () => {
10
+ return createTRPCRouter({
11
+ detectCurrency: protectedProcedure
12
+ .input(z.object({}).passthrough())
13
+ .use(skipPermissionCheck)
14
+ .query(async ({ ctx }) => {
15
+ return detectCurrencyFromRequest(ctx.req);
16
+ }),
17
+ });
18
+ };
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Custom error types for billing domain.
3
+ * Framework-agnostic — mapped to tRPC/HTTP errors by the router middleware.
4
+ *
5
+ * All billing errors extend BillingError which carries a trpcCode.
6
+ * The middleware uses a single instanceof check, so adding new errors
7
+ * never requires updating middleware.ts.
8
+ */
9
+
10
+ type BillingErrorCode =
11
+ | "NOT_FOUND"
12
+ | "UNAUTHORIZED"
13
+ | "BAD_REQUEST"
14
+ | "INTERNAL_SERVER_ERROR";
15
+
16
+ export class BillingError extends Error {
17
+ public readonly trpcCode: BillingErrorCode;
18
+
19
+ constructor({
20
+ message,
21
+ trpcCode,
22
+ }: {
23
+ message: string;
24
+ trpcCode: BillingErrorCode;
25
+ }) {
26
+ super(message);
27
+ this.trpcCode = trpcCode;
28
+ this.name = "BillingError";
29
+ }
30
+ }
31
+
32
+ export class OrganizationNotFoundError extends BillingError {
33
+ constructor() {
34
+ super({ message: "Organization not found", trpcCode: "NOT_FOUND" });
35
+ this.name = "OrganizationNotFoundError";
36
+ }
37
+ }
38
+
39
+ export class UserEmailRequiredError extends BillingError {
40
+ constructor() {
41
+ super({
42
+ message: "User email is required to create Stripe customer",
43
+ trpcCode: "UNAUTHORIZED",
44
+ });
45
+ this.name = "UserEmailRequiredError";
46
+ }
47
+ }
48
+
49
+ export class CustomerCreationRaceError extends BillingError {
50
+ constructor() {
51
+ super({
52
+ message: "Stripe customer ID missing after concurrent creation",
53
+ trpcCode: "INTERNAL_SERVER_ERROR",
54
+ });
55
+ this.name = "CustomerCreationRaceError";
56
+ }
57
+ }
58
+
59
+ export class InvalidPlanError extends BillingError {
60
+ constructor(plan: string) {
61
+ super({
62
+ message: `Plan ${plan} does not have an associated Stripe price`,
63
+ trpcCode: "BAD_REQUEST",
64
+ });
65
+ this.name = "InvalidPlanError";
66
+ }
67
+ }
68
+
69
+ export class SeatBillingUnavailableError extends BillingError {
70
+ constructor() {
71
+ super({
72
+ message: "Seat event billing is not available",
73
+ trpcCode: "INTERNAL_SERVER_ERROR",
74
+ });
75
+ this.name = "SeatBillingUnavailableError";
76
+ }
77
+ }
78
+
79
+ export class NoActiveSubscriptionError extends BillingError {
80
+ constructor() {
81
+ super({ message: "No active subscription found", trpcCode: "NOT_FOUND" });
82
+ this.name = "NoActiveSubscriptionError";
83
+ }
84
+ }
85
+
86
+ export class SubscriptionItemNotFoundError extends BillingError {
87
+ constructor(item: string) {
88
+ super({
89
+ message: `No ${item} item found on subscription`,
90
+ trpcCode: "INTERNAL_SERVER_ERROR",
91
+ });
92
+ this.name = "SubscriptionItemNotFoundError";
93
+ }
94
+ }
95
+
96
+ export class InvalidSeatCountError extends BillingError {
97
+ constructor(count: number) {
98
+ super({
99
+ message: `coreMembers must be at least 1, got ${count}`,
100
+ trpcCode: "BAD_REQUEST",
101
+ });
102
+ this.name = "InvalidSeatCountError";
103
+ }
104
+ }
105
+
106
+ export class SubscriptionCreationFailedError extends BillingError {
107
+ constructor() {
108
+ super({
109
+ message: "Failed to create pending subscription record",
110
+ trpcCode: "INTERNAL_SERVER_ERROR",
111
+ });
112
+ this.name = "SubscriptionCreationFailedError";
113
+ }
114
+ }
115
+
116
+ export class SubscriptionRecordNotFoundError extends BillingError {
117
+ constructor(identifier: string) {
118
+ super({
119
+ message: `No subscription record found for ${identifier}`,
120
+ trpcCode: "INTERNAL_SERVER_ERROR",
121
+ });
122
+ this.name = "SubscriptionRecordNotFoundError";
123
+ }
124
+ }
125
+
126
+ // Re-export from app-layer for backwards compatibility.
127
+ // The canonical definition lives in the app-layer so that Null implementations
128
+ // can throw it without depending on ee/billing.
129
+ export { SubscriptionServiceUnavailableError } from "../../src/server/app-layer/subscription/errors";
@@ -0,0 +1,67 @@
1
+ import { prisma } from "../../src/server/db";
2
+ import { createSaaSPlanProvider } from "./planProvider";
3
+ import { createCustomerService } from "./services/customerService";
4
+ import { createSeatEventSubscriptionFns } from "./services/seatEventSubscription";
5
+ import { createSeatSyncService } from "./services/seatSyncService";
6
+ import * as subscriptionItemCalculator from "./services/subscriptionItemCalculator";
7
+ import { EESubscriptionService } from "./services/subscription.service";
8
+ import { createStripeClient } from "./stripe/stripeClient";
9
+ import { createCurrencyRouter } from "./currencyRouter";
10
+ import { createSubscriptionRouterFactory } from "./subscriptionRouter";
11
+
12
+ export { PlanTypes, SubscriptionStatus } from "./planTypes";
13
+ export { PLAN_LIMITS } from "./planLimits";
14
+ export { prices } from "./services/subscriptionItemCalculator";
15
+ export type { UsageReportingService, MeterEventResult, UsageSummary } from "./services/usageReportingService";
16
+ export type {
17
+ BillingPlanProvider,
18
+ PlanLimitNotificationContext,
19
+ PlanLimitNotifierInput,
20
+ ResourceLimitNotificationContext,
21
+ ResourceLimitNotifierInput,
22
+ SubscriptionNotificationPayload,
23
+ } from "./types";
24
+
25
+ // Lazy Stripe singleton
26
+ let stripe: ReturnType<typeof createStripeClient> | null = null;
27
+ const getStripe = () => {
28
+ if (!stripe) stripe = createStripeClient();
29
+ return stripe;
30
+ };
31
+
32
+ export { createCurrencyRouter };
33
+
34
+ export const createSubscriptionRouter = () => {
35
+ const s = getStripe();
36
+ const customerService = createCustomerService({ stripe: s, db: prisma });
37
+ const seatEventFns = createSeatEventSubscriptionFns({ stripe: s, db: prisma });
38
+ const subscriptionService = EESubscriptionService.create({
39
+ stripe: s,
40
+ db: prisma,
41
+ itemCalculator: subscriptionItemCalculator,
42
+ seatEventFns,
43
+ });
44
+ return createSubscriptionRouterFactory({ customerService, subscriptionService });
45
+ };
46
+
47
+ let seatSyncServiceInstance: ReturnType<typeof createSeatSyncService> | null = null;
48
+
49
+ export const getSeatSyncService = () => {
50
+ if (!seatSyncServiceInstance) {
51
+ const s = getStripe();
52
+ const seatEventFns = createSeatEventSubscriptionFns({ stripe: s, db: prisma });
53
+ seatSyncServiceInstance = createSeatSyncService({ seatEventFns, db: prisma });
54
+ }
55
+ return seatSyncServiceInstance;
56
+ };
57
+
58
+ let saasPlanProvider: ReturnType<typeof createSaaSPlanProvider> | null = null;
59
+
60
+ export const getSaaSPlanProvider = () => {
61
+ if (!saasPlanProvider) {
62
+ saasPlanProvider = createSaaSPlanProvider(prisma);
63
+ }
64
+
65
+ return saasPlanProvider;
66
+ };
67
+
@@ -0,0 +1,17 @@
1
+ import { TRPCError } from "@trpc/server";
2
+ import { BillingError } from "./errors";
3
+
4
+ export const billingErrorHandler = async <T>({
5
+ next,
6
+ }: {
7
+ next: () => Promise<T>;
8
+ }): Promise<T> => {
9
+ try {
10
+ return await next();
11
+ } catch (error) {
12
+ if (error instanceof BillingError) {
13
+ throw new TRPCError({ code: error.trpcCode, message: error.message });
14
+ }
15
+ throw error;
16
+ }
17
+ };