usePlanIdentitySnapshot.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { buildStartPlanEntitlementOptions } from "@/lib/startPlanEntitlementOptions.js";
  2. import { useCallback } from "react";
  3. import {
  4. getModelProviderFamilySpec,
  5. normalizeProviderFamilyDomain,
  6. resolvePlanIdentitySnapshot,
  7. type PlanIdentitySnapshot,
  8. type ProviderFamilyConnectionSelection,
  9. type ProviderFamilyDomain,
  10. } from "@zcode/shared";
  11. import type { IUsageStatsService } from "@zcode/services";
  12. import { useUsageEntitlementWithService } from "@/hooks/useUsageEntitlement.js";
  13. import { useProviderSettingsView } from "@/hooks/useProviderSettingsView.js";
  14. import { resolveEntitledAccountProviderAccessFingerprint } from "@/lib/accountProviderAccess.js";
  15. import {
  16. buildUsageEntitlementCacheKey,
  17. USAGE_ENTITLEMENT_CACHE_TTL_MS,
  18. } from "@/lib/usageEntitlementCache.js";
  19. export function usePlanIdentitySnapshot(
  20. providerFamilyDomain: ProviderFamilyDomain | null | undefined,
  21. connectionSelection: ProviderFamilyConnectionSelection | null | undefined,
  22. usageStatsService?: IUsageStatsService,
  23. ): () => PlanIdentitySnapshot {
  24. const normalizedDomain = normalizeProviderFamilyDomain(providerFamilyDomain);
  25. const providerSettingsRead = useProviderSettingsView();
  26. const providerSettingsView =
  27. providerSettingsRead.state.status === "ready" ? providerSettingsRead.state.view : null;
  28. const familySpec = normalizedDomain ? getModelProviderFamilySpec(normalizedDomain) : null;
  29. const codingPlanProviderId = familySpec
  30. ? connectionSelection?.kind === "team-coding-plan"
  31. ? familySpec.teamCodingPlanProviderId
  32. : familySpec.individualCodingPlanProviderId
  33. : "";
  34. const startPlanProviderId = familySpec?.startPlanProviderId ?? "";
  35. const codingPlanRefreshFingerprint = resolveEntitledAccountProviderAccessFingerprint(
  36. providerSettingsView,
  37. codingPlanProviderId,
  38. );
  39. const codingPlanEntitlement = useUsageEntitlementWithService(usageStatsService, {
  40. enabled: Boolean(normalizedDomain && codingPlanRefreshFingerprint),
  41. includeSubscription: true,
  42. preferredProviderId: codingPlanProviderId,
  43. accountAccess: normalizedDomain
  44. ? {
  45. type: "zhipu-account",
  46. family: normalizedDomain,
  47. ...(connectionSelection?.kind === "team-coding-plan"
  48. ? ({
  49. planKind: "team-coding-plan",
  50. productId: connectionSelection.productId,
  51. organizationId: connectionSelection.organizationId,
  52. projectId: connectionSelection.projectId,
  53. } as const)
  54. : ({ planKind: "individual-coding-plan" } as const)),
  55. }
  56. : undefined,
  57. allowDisabledPreferredProvider: true,
  58. requirePreferredProvider: true,
  59. allowEnvApiKey: false,
  60. cacheKey: buildUsageEntitlementCacheKey({
  61. providerId: codingPlanProviderId,
  62. providerFingerprint: codingPlanRefreshFingerprint,
  63. }),
  64. refreshOnMount: false,
  65. });
  66. const startPlanEntitlement = useUsageEntitlementWithService(
  67. usageStatsService,
  68. buildStartPlanEntitlementOptions(providerSettingsView, startPlanProviderId),
  69. );
  70. return useCallback(
  71. () =>
  72. resolvePlanIdentitySnapshot({
  73. providerFamilyDomain: normalizedDomain,
  74. codingPlanEntitlement: codingPlanEntitlement.snapshot,
  75. startPlanEntitlement: startPlanEntitlement.snapshot,
  76. now: Date.now(),
  77. entitlementCacheTtlMs: USAGE_ENTITLEMENT_CACHE_TTL_MS,
  78. }),
  79. [
  80. codingPlanEntitlement.snapshot,
  81. connectionSelection,
  82. normalizedDomain,
  83. startPlanEntitlement.snapshot,
  84. ],
  85. );
  86. }