Line data Source code
1 : /**
2 : * The plans read: the catalog a workspace may subscribe to, cached per scope.
3 : *
4 : * Availability, seat costs, and the credit-stop ladder are all resolved by the
5 : * server for the actor that asked, so the response is cached under the scope it
6 : * was resolved for and no plan is filtered, ranked, or priced here.
7 : */
8 : import {
9 : zGetBillingPlansResponse,
10 : zPlanFeatureItem,
11 : zSubscriptionPaymentRecovery
12 : } from '@comfyorg/ingest-types/zod'
13 : import { z } from 'zod'
14 :
15 : import type { BillingResult, BillingTransport } from './billingContracts'
16 : import type { BillingScope, BillingScopeSource } from './billingScope'
17 : import { createScopedReader } from './scopedReader'
18 :
19 2356 : export const PLANS_ROUTE = '/billing/plans'
20 :
21 2356 : const CompatiblePlanSchema = z.looseObject({
22 : feature_items: z
23 : .array(z.unknown())
24 : .transform((items) =>
25 : items.filter((item) => zPlanFeatureItem.safeParse(item).success)
26 : )
27 : .optional()
28 : })
29 :
30 2356 : const RecognizedPaymentRecoverySchema = z
31 : .unknown()
32 : .transform((recovery) =>
33 : zSubscriptionPaymentRecovery.safeParse(recovery).success
34 : ? recovery
35 : : undefined
36 : )
37 : .optional()
38 :
39 2356 : const BillingPlansBodySchema = z
40 : .looseObject({
41 : plans: z.array(CompatiblePlanSchema),
42 : subscription_payment_recovery: RecognizedPaymentRecoverySchema
43 : })
44 : .pipe(zGetBillingPlansResponse)
45 :
46 : export type BillingPlansData = z.infer<typeof BillingPlansBodySchema>
47 :
48 : export type PlansScope = BillingScope
49 :
50 : export interface PlansSnapshot {
51 : readonly scope: PlansScope
52 : readonly data: BillingPlansData
53 : /** ms since epoch at which this read settled. */
54 : readonly readAt: number
55 : }
56 :
57 : export interface PlansReadOptions {
58 : readonly signal?: AbortSignal
59 : /** Bounds the request, when this caller is the one that issues it. */
60 : readonly timeoutMs?: number
61 : }
62 :
63 : export interface PlansReader {
64 : /**
65 : * Always requests. The catalog changes rarely, but nothing in the response
66 : * declares how long it stays valid, so the host decides when to re-read
67 : * rather than the core inventing a freshness window nobody published.
68 : * Concurrent callers for one scope still share a single request.
69 : */
70 : read: (options?: PlansReadOptions) => Promise<BillingResult<PlansSnapshot>>
71 : /** The last catalog published for the current scope. */
72 : getSnapshot: () => PlansSnapshot | undefined
73 : dispose: () => void
74 : }
75 :
76 : export interface PlansReaderOptions {
77 : readonly transport: BillingTransport
78 : /** Where the core learns which user, workspace, and role it runs as. */
79 : readonly scopeSource: BillingScopeSource
80 : readonly now?: () => number
81 : }
82 :
83 0 : export function createPlansReader(options: PlansReaderOptions): PlansReader {
84 0 : const { transport, scopeSource, now = Date.now } = options
85 :
86 0 : const reader = createScopedReader<
87 : BillingPlansData,
88 : PlansSnapshot,
89 : PlansReadOptions
90 : >({
91 : transport,
92 : scopeSource,
93 : route: PLANS_ROUTE,
94 : parse: (body) => BillingPlansBodySchema.safeParse(body),
95 : timeoutMs: (readOptions) => readOptions?.timeoutMs,
96 : project: ({ data }, scope) => ({
97 : status: 'ok',
98 : value: { scope, data, readAt: now() }
99 : })
100 : })
101 :
102 0 : return {
103 : read: reader.read,
104 : getSnapshot: reader.getSnapshot,
105 : dispose: reader.dispose
106 : }
107 : }
|