Elffuss BraidHub

kikocis/continue public

9df0fa5d79c7 · 4.7 KiBRawPermalink
1import { FQSN, PackageSlug, encodePackageSlug } from "./slugs.js";
2
3export enum SecretType {
4 User = "user",
5 Package = "package",
6 Organization = "organization",
7 NotFound = "not_found",
8 ModelsAddOn = "models_add_on",
9 FreeTrial = "free_trial",
10 LocalEnv = "local_env",
11}
12
13export interface OrgSecretLocation {
14 secretType: SecretType.Organization;
15 orgSlug: string;
16 secretName: string;
17}
18
19export interface PackageSecretLocation {
20 secretType: SecretType.Package;
21 packageSlug: PackageSlug;
22 secretName: string;
23}
24
25export interface UserSecretLocation {
26 secretType: SecretType.User;
27 userSlug: string;
28 secretName: string;
29}
30
31export interface ModelsAddOnSecretLocation {
32 secretType: SecretType.ModelsAddOn;
33 blockSlug: PackageSlug;
34 secretName: string;
35}
36
37export interface FreeTrialSecretLocation {
38 secretType: SecretType.FreeTrial;
39 blockSlug: PackageSlug;
40 secretName: string;
41}
42
43export interface LocalEnvSecretLocation {
44 secretType: SecretType.LocalEnv;
45 secretName: string;
46}
47
48/**
49 * If not found in user/package/org secrets, then there's a chance it's in
50 * - the on-prem proxy
51 * - models add-on
52 * - free trial
53 */
54export interface NotFoundSecretLocation {
55 secretType: SecretType.NotFound;
56 secretName: string;
57}
58
59export type SecretLocation =
60 | OrgSecretLocation
61 | PackageSecretLocation
62 | UserSecretLocation
63 | NotFoundSecretLocation
64 | ModelsAddOnSecretLocation
65 | FreeTrialSecretLocation
66 | LocalEnvSecretLocation;
67
68export function encodeSecretLocation(secretLocation: SecretLocation): string {
69 if (secretLocation.secretType === SecretType.Organization) {
70 return `${SecretType.Organization}:${secretLocation.orgSlug}/${secretLocation.secretName}`;
71 } else if (secretLocation.secretType === SecretType.User) {
72 return `${SecretType.User}:${secretLocation.userSlug}/${secretLocation.secretName}`;
73 } else if (secretLocation.secretType === SecretType.Package) {
74 return `${SecretType.Package}:${encodePackageSlug(secretLocation.packageSlug)}/${secretLocation.secretName}`;
75 } else if (secretLocation.secretType === SecretType.NotFound) {
76 return `${SecretType.NotFound}:${secretLocation.secretName}`;
77 } else if (secretLocation.secretType === SecretType.ModelsAddOn) {
78 return `${SecretType.ModelsAddOn}:${encodePackageSlug(secretLocation.blockSlug)}/${secretLocation.secretName}`;
79 } else if (secretLocation.secretType === SecretType.FreeTrial) {
80 return `${SecretType.FreeTrial}:${encodePackageSlug(secretLocation.blockSlug)}/${secretLocation.secretName}`;
81 } else if (secretLocation.secretType === SecretType.LocalEnv) {
82 return `${SecretType.LocalEnv}:${secretLocation.secretName}`;
83 } else {
84 throw new Error(`Invalid secret type: ${secretLocation}`);
85 }
86}
87
88export function decodeSecretLocation(secretLocation: string): SecretLocation {
89 const [secretType, rest] = secretLocation.split(":");
90 const parts = rest.split("/");
91 const secretName = parts[parts.length - 1];
92
93 switch (secretType) {
94 case SecretType.Organization:
95 return {
96 secretType: SecretType.Organization,
97 orgSlug: parts[0],
98 secretName,
99 };
100 case SecretType.User:
101 return {
102 secretType: SecretType.User,
103 userSlug: parts[0],
104 secretName,
105 };
106 case SecretType.Package:
107 return {
108 secretType: SecretType.Package,
109 packageSlug: { ownerSlug: parts[0], packageSlug: parts[1] },
110 secretName,
111 };
112 case SecretType.NotFound:
113 return {
114 secretType: SecretType.NotFound,
115 secretName,
116 };
117 case SecretType.ModelsAddOn:
118 return {
119 secretType: SecretType.ModelsAddOn,
120 secretName,
121 blockSlug: {
122 ownerSlug: parts[0],
123 packageSlug: parts[1],
124 },
125 };
126 case SecretType.FreeTrial:
127 return {
128 secretType: SecretType.FreeTrial,
129 secretName,
130 blockSlug: {
131 ownerSlug: parts[0],
132 packageSlug: parts[1],
133 },
134 };
135 case SecretType.LocalEnv:
136 return {
137 secretType: SecretType.LocalEnv,
138 secretName,
139 };
140 default:
141 throw new Error(`Invalid secret type: ${secretType}`);
142 }
143}
144
145export interface NotFoundSecretResult {
146 found: false;
147 secretLocation: NotFoundSecretLocation;
148 fqsn: FQSN;
149}
150
151export interface FoundSecretResult {
152 found: true;
153 secretLocation:
154 | OrgSecretLocation
155 | PackageSecretLocation
156 | ModelsAddOnSecretLocation
157 | FreeTrialSecretLocation
158 | LocalEnvSecretLocation;
159 fqsn: FQSN;
160}
161
162export interface FoundUserSecretResult {
163 found: true;
164 secretLocation: UserSecretLocation;
165 value: string;
166 fqsn: FQSN;
167}
168
169export type SecretResult =
170 | FoundSecretResult
171 | FoundUserSecretResult
172 | NotFoundSecretResult;