-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathsecrets.ts
More file actions
220 lines (203 loc) · 6.33 KB
/
Copy pathsecrets.ts
File metadata and controls
220 lines (203 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { z } from "zod/v3";
import { ModelSchema } from "./models";
export const BaseMetadataSchema = z
.object({
models: z.array(z.string()).nullish(),
customModels: z.record(ModelSchema).nullish(),
excludeDefaultModels: z.boolean().nullish(),
additionalHeaders: z.record(z.string(), z.string()).nullish(),
supportsStreaming: z.boolean().default(true),
})
.strict();
export const AzureMetadataSchema = BaseMetadataSchema.merge(
z.object({
api_base: z.string().url(),
api_version: z.string().default("2023-07-01-preview"),
deployment: z.string().nullish(),
auth_type: z.enum(["api_key", "entra_api"]).default("api_key"),
no_named_deployment: z
.boolean()
.default(false)
.describe(
"If true, the deployment name will not be used in the request path.",
),
}),
).strict();
export const AzureEntraSecretSchema = z.object({
client_id: z.string().min(1, "Client ID cannot be empty"),
client_secret: z.string().min(1, "Client secret cannot be empty"),
tenant_id: z.string().min(1, "Tenant ID cannot be empty"),
scope: z.string().min(1, "Scope cannot be empty"),
});
export type AzureEntraSecret = z.infer<typeof AzureEntraSecretSchema>;
export const BedrockMetadataSchema = BaseMetadataSchema.merge(
z.object({
region: z.string().min(1, "Region cannot be empty"),
access_key: z.string().min(1, "Access key cannot be empty"),
session_token: z.string().nullish(),
api_base: z.union([z.string().url(), z.string().length(0)]).nullish(),
}),
).strict();
export type BedrockMetadata = z.infer<typeof BedrockMetadataSchema>;
export const VertexMetadataSchema = BaseMetadataSchema.merge(
z.object({
project: z.string().min(1, "Project cannot be empty"),
authType: z.enum(["access_token", "service_account_key"]),
api_base: z.union([z.string().url(), z.string().length(0)]).nullish(),
}),
).strict();
export const DatabricksMetadataSchema = BaseMetadataSchema.merge(
z.object({
api_base: z.string().url(),
auth_type: z.enum(["pat", "service_principal_oauth"]).default("pat"),
}),
).strict();
export const DatabricksOAuthSecretSchema = z.object({
client_id: z.string().min(1, "Client ID cannot be empty"),
client_secret: z.string().min(1, "Client secret cannot be empty"),
});
export type DatabricksOAuthSecret = z.infer<typeof DatabricksOAuthSecretSchema>;
export const OpenAIMetadataSchema = BaseMetadataSchema.merge(
z.object({
api_base: z.union([
z.string().url().optional(),
z.string().length(0),
z.null(),
]),
organization_id: z.string().nullish(),
}),
).strict();
export const MistralMetadataSchema = BaseMetadataSchema.merge(
z.object({
api_base: z.union([z.string().url(), z.string().length(0)]).nullish(),
}),
).strict();
const APISecretBaseSchema = z
.object({
id: z.string().uuid().nullish(),
org_name: z.string().nullish(),
name: z.string().nullish(),
secret: z.string(),
metadata: z.record(z.unknown()).nullish(),
})
.strict();
export const APISecretSchema = z.union([
APISecretBaseSchema.merge(
z.object({
type: z.enum([
"perplexity",
"anthropic",
"google",
"replicate",
"together",
"baseten",
"ollama",
"groq",
"lepton",
"fireworks",
"cerebras",
"xAI",
"js",
]),
metadata: BaseMetadataSchema.nullish(),
}),
),
APISecretBaseSchema.merge(
z.object({
type: z.literal("openai"),
metadata: OpenAIMetadataSchema.nullish(),
}),
),
APISecretBaseSchema.merge(
z.object({
type: z.literal("azure"),
metadata: AzureMetadataSchema.nullish(),
}),
),
APISecretBaseSchema.merge(
z.object({
type: z.literal("bedrock"),
metadata: BedrockMetadataSchema.nullish(),
}),
),
APISecretBaseSchema.merge(
z.object({
type: z.literal("vertex"),
metadata: VertexMetadataSchema.nullish(),
}),
),
APISecretBaseSchema.merge(
z.object({
type: z.literal("databricks"),
metadata: DatabricksMetadataSchema.nullish(),
}),
),
APISecretBaseSchema.merge(
z.object({
type: z.literal("mistral"),
metadata: MistralMetadataSchema.nullish(),
}),
),
]);
export type APISecret = z.infer<typeof APISecretSchema>;
export const proxyLoggingParamSchema = z
.object({
parent: z.string().optional(),
project_name: z.string().optional(),
compress_audio: z.boolean().default(true),
})
.refine((data) => data.parent || data.project_name, {
message: "Either 'parent' or 'project_name' must be provided",
})
.describe(
"If present, proxy will log requests to the given Braintrust project or parent span.",
);
export type ProxyLoggingParam = z.infer<typeof proxyLoggingParamSchema>;
export const credentialsRequestSchema = z
.object({
model: z
.string()
.nullish()
.describe(
"Granted model name. Null/undefined to grant usage of all models.",
),
ttl_seconds: z
.number()
.max(60 * 60 * 24)
.default(60 * 10)
.describe("TTL of the temporary credential. 10 minutes by default."),
logging: proxyLoggingParamSchema.nullish(),
})
.describe("Payload for requesting temporary credentials.");
export type CredentialsRequest = z.infer<typeof credentialsRequestSchema>;
export const tempCredentialsCacheValueSchema = z
.object({
authToken: z.string().describe("Braintrust API key."),
})
.describe("Schema for the proxy's internal credential cache.");
export type TempCredentialsCacheValue = z.infer<
typeof tempCredentialsCacheValueSchema
>;
export const tempCredentialJwtPayloadSchema = z
.object({
iss: z.literal("braintrust_proxy"),
aud: z.literal("braintrust_proxy"),
jti: z
.string()
.min(1)
.describe("JWT ID, a unique identifier for this token."),
exp: z.number().describe("Standard JWT expiration field."),
iat: z.number().describe("Standard JWT issued-at field"),
bt: z
.object({
org_name: z.string().nullish(),
model: z.string().nullish(),
secret: z.string().min(1),
logging: proxyLoggingParamSchema.nullish(),
})
.describe("Braintrust-specific grants. See credentialsRequestSchema."),
})
.describe("Braintrust Proxy JWT payload.");
export type TempCredentialJwtPayload = z.infer<
typeof tempCredentialJwtPayloadSchema
>;