@databricks/sdk-modelservingquery 0.0.0-dev → 0.1.0-dev.2
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.
- package/LICENSE +203 -0
- package/dist/v1/client.d.ts +14 -0
- package/dist/v1/client.d.ts.map +1 -0
- package/dist/v1/client.js +66 -0
- package/dist/v1/client.js.map +1 -0
- package/dist/v1/index.d.ts +4 -0
- package/dist/v1/index.d.ts.map +1 -0
- package/dist/v1/index.js +4 -0
- package/dist/v1/index.js.map +1 -0
- package/dist/v1/model.d.ts +178 -0
- package/dist/v1/model.d.ts.map +1 -0
- package/dist/v1/model.js +171 -0
- package/dist/v1/model.js.map +1 -0
- package/dist/v1/transport.d.ts +5 -0
- package/dist/v1/transport.d.ts.map +1 -0
- package/dist/v1/transport.js +57 -0
- package/dist/v1/transport.js.map +1 -0
- package/dist/v1/utils.d.ts +21 -0
- package/dist/v1/utils.d.ts.map +1 -0
- package/dist/v1/utils.js +113 -0
- package/dist/v1/utils.js.map +1 -0
- package/package.json +38 -4
- package/src/v1/client.ts +87 -0
- package/src/v1/index.ts +21 -0
- package/src/v1/model.ts +345 -0
- package/src/v1/transport.ts +73 -0
- package/src/v1/utils.ts +156 -0
- package/README.md +0 -1
- package/index.js +0 -1
package/src/v1/model.ts
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
// Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
import type {JsonValue} from '@databricks/sdk-core/wkt';
|
|
4
|
+
import {z} from 'zod';
|
|
5
|
+
|
|
6
|
+
const jsonValueSchema: z.ZodType<JsonValue> = z.lazy(() =>
|
|
7
|
+
z.union([
|
|
8
|
+
z.null(),
|
|
9
|
+
z.number(),
|
|
10
|
+
z.string(),
|
|
11
|
+
z.boolean(),
|
|
12
|
+
z.record(z.string(), jsonValueSchema),
|
|
13
|
+
z.array(jsonValueSchema),
|
|
14
|
+
])
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
/** The role of the message. One of [system, user, assistant]. */
|
|
18
|
+
export enum ChatMessageRole {
|
|
19
|
+
CHAT_MESSAGE_ROLE_UNSPECIFIED = 'CHAT_MESSAGE_ROLE_UNSPECIFIED',
|
|
20
|
+
SYSTEM = 'system',
|
|
21
|
+
USER = 'user',
|
|
22
|
+
ASSISTANT = 'assistant',
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** This will always be 'embedding'. */
|
|
26
|
+
export enum EmbeddingsV1ResponseEmbeddingElementObject {
|
|
27
|
+
EMBEDDINGS_V1_RESPONSE_EMBEDDING_ELEMENT_OBJECT_UNSPECIFIED = 'EMBEDDINGS_V1_RESPONSE_EMBEDDING_ELEMENT_OBJECT_UNSPECIFIED',
|
|
28
|
+
EMBEDDING = 'embedding',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The type of object returned by the __external/foundation model__ serving endpoint, one of
|
|
33
|
+
* [text_completion, chat.completion, list (of embeddings)].
|
|
34
|
+
*/
|
|
35
|
+
export enum QueryEndpointResponseObject {
|
|
36
|
+
QUERY_ENDPOINT_RESPONSE_OBJECT_UNSPECIFIED = 'QUERY_ENDPOINT_RESPONSE_OBJECT_UNSPECIFIED',
|
|
37
|
+
TEXT_COMPLETION = 'text_completion',
|
|
38
|
+
CHAT_COMPLETION = 'chat.completion',
|
|
39
|
+
LIST = 'list',
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ChatMessage {
|
|
43
|
+
/** The role of the message. One of [system, user, assistant]. */
|
|
44
|
+
role?: ChatMessageRole | undefined;
|
|
45
|
+
/** The content of the message. */
|
|
46
|
+
content?: string | undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface DataframeSplitInput {
|
|
50
|
+
/** Index array for the dataframe */
|
|
51
|
+
index?: number[] | undefined;
|
|
52
|
+
/** Columns array for the dataframe */
|
|
53
|
+
columns?: JsonValue[] | undefined;
|
|
54
|
+
/** Data array for the dataframe */
|
|
55
|
+
data?: JsonValue[] | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface EmbeddingsV1ResponseEmbeddingElement {
|
|
59
|
+
/** The embedding vector */
|
|
60
|
+
embedding?: number[] | undefined;
|
|
61
|
+
/** The index of the embedding in the response. */
|
|
62
|
+
index?: number | undefined;
|
|
63
|
+
/** This will always be 'embedding'. */
|
|
64
|
+
object?: EmbeddingsV1ResponseEmbeddingElementObject | undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ExternalModelUsageElement {
|
|
68
|
+
/** The number of tokens in the prompt. */
|
|
69
|
+
promptTokens?: number | undefined;
|
|
70
|
+
/** The number of tokens in the chat/completions response. */
|
|
71
|
+
completionTokens?: number | undefined;
|
|
72
|
+
/** The total number of tokens in the prompt and response. */
|
|
73
|
+
totalTokens?: number | undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface QueryEndpointInputRequest {
|
|
77
|
+
/** The name of the serving endpoint. This field is required and is provided via the path parameter. */
|
|
78
|
+
name?: string | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* The prompt string (or array of strings) field used ONLY for __completions external & foundation model__
|
|
81
|
+
* serving endpoints and should only be used with other completions query fields.
|
|
82
|
+
*/
|
|
83
|
+
prompt?: JsonValue | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* The input string (or array of strings) field used ONLY for __embeddings external & foundation model__
|
|
86
|
+
* serving endpoints and is the only field (along with extra_params if needed) used by embeddings queries.
|
|
87
|
+
*/
|
|
88
|
+
input?: JsonValue | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* The messages field used ONLY for __chat external & foundation model__ serving endpoints.
|
|
91
|
+
* This is an array of ChatMessage objects and should only be used with other chat query fields.
|
|
92
|
+
*/
|
|
93
|
+
messages?: ChatMessage[] | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* The temperature field used ONLY for __completions__ and __chat external & foundation model__ serving
|
|
96
|
+
* endpoints. This is a float between 0.0 and 2.0 with a default of 1.0 and should only be used with other
|
|
97
|
+
* chat/completions query fields.
|
|
98
|
+
*/
|
|
99
|
+
temperature?: number | undefined;
|
|
100
|
+
/**
|
|
101
|
+
* The stop sequences field used ONLY for __completions__ and __chat external & foundation model__ serving
|
|
102
|
+
* endpoints. This is a list of strings and should only be used with other chat/completions query fields.
|
|
103
|
+
*/
|
|
104
|
+
stop?: string[] | undefined;
|
|
105
|
+
/**
|
|
106
|
+
* The max tokens field used ONLY for __completions__ and __chat external & foundation model__ serving
|
|
107
|
+
* endpoints. This is an integer and should only be used with other chat/completions query fields.
|
|
108
|
+
*/
|
|
109
|
+
maxTokens?: number | undefined;
|
|
110
|
+
/**
|
|
111
|
+
* The n (number of candidates) field used ONLY for __completions__ and __chat external & foundation model__
|
|
112
|
+
* serving endpoints. This is an integer between 1 and 5 with a default of 1 and should only be used with
|
|
113
|
+
* other chat/completions query fields.
|
|
114
|
+
*/
|
|
115
|
+
n?: number | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* The stream field used ONLY for __completions__ and __chat external & foundation model__ serving endpoints.
|
|
118
|
+
* This is a boolean defaulting to false and should only be used with other chat/completions query fields.
|
|
119
|
+
*/
|
|
120
|
+
stream?: boolean | undefined;
|
|
121
|
+
/**
|
|
122
|
+
* The extra parameters field used ONLY for __completions, chat,__ and __embeddings external & foundation
|
|
123
|
+
* model__ serving endpoints. This is a map of strings and should only be used with other external/foundation
|
|
124
|
+
* model query fields.
|
|
125
|
+
*/
|
|
126
|
+
extraParams?: Record<string, string> | undefined;
|
|
127
|
+
/** Pandas Dataframe input in the records orientation. */
|
|
128
|
+
dataframeRecords?: JsonValue[] | undefined;
|
|
129
|
+
/** Pandas Dataframe input in the split orientation. */
|
|
130
|
+
dataframeSplit?: DataframeSplitInput | undefined;
|
|
131
|
+
/** Tensor-based input in row format. */
|
|
132
|
+
instances?: JsonValue[] | undefined;
|
|
133
|
+
/** Tensor-based input in columnar format. */
|
|
134
|
+
inputs?: JsonValue | undefined;
|
|
135
|
+
/** Optional user-provided request identifier that will be recorded in the inference table and the usage tracking table. */
|
|
136
|
+
clientRequestId?: string | undefined;
|
|
137
|
+
/** Optional user-provided context that will be recorded in the usage tracking table. */
|
|
138
|
+
usageContext?: Record<string, string> | undefined;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
|
|
142
|
+
export interface QueryEndpointInputRequest_ExtraParamsEntry {
|
|
143
|
+
key?: string | undefined;
|
|
144
|
+
value?: string | undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention -- Proto-style nested message name.
|
|
148
|
+
export interface QueryEndpointInputRequest_UsageContextEntry {
|
|
149
|
+
key?: string | undefined;
|
|
150
|
+
value?: string | undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface QueryEndpointResponse {
|
|
154
|
+
/** The list of choices returned by the __chat or completions external/foundation model__ serving endpoint. */
|
|
155
|
+
choices?: V1ResponseChoiceElement[] | undefined;
|
|
156
|
+
/** The list of the embeddings returned by the __embeddings external/foundation model__ serving endpoint. */
|
|
157
|
+
data?: EmbeddingsV1ResponseEmbeddingElement[] | undefined;
|
|
158
|
+
/**
|
|
159
|
+
* The name of the __external/foundation model__ used for querying. This is the name of the model that was
|
|
160
|
+
* specified in the endpoint config.
|
|
161
|
+
*/
|
|
162
|
+
model?: string | undefined;
|
|
163
|
+
/**
|
|
164
|
+
* The usage object that may be returned by the __external/foundation model__ serving endpoint. This
|
|
165
|
+
* contains information about the number of tokens used in the prompt and response.
|
|
166
|
+
*/
|
|
167
|
+
usage?: ExternalModelUsageElement | undefined;
|
|
168
|
+
/** The ID of the query that may be returned by a __completions or chat external/foundation model__ serving endpoint. */
|
|
169
|
+
id?: string | undefined;
|
|
170
|
+
/** The timestamp in seconds when the query was created in Unix time returned by a __completions or chat external/foundation model__ serving endpoint. */
|
|
171
|
+
created?: bigint | undefined;
|
|
172
|
+
/**
|
|
173
|
+
* The type of object returned by the __external/foundation model__ serving endpoint, one of
|
|
174
|
+
* [text_completion, chat.completion, list (of embeddings)].
|
|
175
|
+
*/
|
|
176
|
+
object?: QueryEndpointResponseObject | undefined;
|
|
177
|
+
/** The predictions returned by the serving endpoint. */
|
|
178
|
+
predictions?: JsonValue[] | undefined;
|
|
179
|
+
/** The outputs of the feature serving endpoint. */
|
|
180
|
+
outputs?: JsonValue[] | undefined;
|
|
181
|
+
/** The name of the served model that served the request. This is useful when there are multiple models behind the same endpoint with traffic split. */
|
|
182
|
+
servedModelName?: string | undefined;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface V1ResponseChoiceElement {
|
|
186
|
+
/** The text response from the __completions__ endpoint. */
|
|
187
|
+
text?: string | undefined;
|
|
188
|
+
/** The message response from the __chat__ endpoint. */
|
|
189
|
+
message?: ChatMessage | undefined;
|
|
190
|
+
/** The index of the choice in the __chat or completions__ response. */
|
|
191
|
+
index?: number | undefined;
|
|
192
|
+
/** The finish reason returned by the endpoint. */
|
|
193
|
+
finishReason?: string | undefined;
|
|
194
|
+
/** The logprobs returned only by the __completions__ endpoint. */
|
|
195
|
+
logprobs?: number | undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export const unmarshalChatMessageSchema: z.ZodType<ChatMessage> = z
|
|
199
|
+
.object({
|
|
200
|
+
role: z.enum(ChatMessageRole).optional(),
|
|
201
|
+
content: z.string().optional(),
|
|
202
|
+
})
|
|
203
|
+
.transform(d => ({
|
|
204
|
+
role: d.role,
|
|
205
|
+
content: d.content,
|
|
206
|
+
}));
|
|
207
|
+
|
|
208
|
+
export const unmarshalEmbeddingsV1ResponseEmbeddingElementSchema: z.ZodType<EmbeddingsV1ResponseEmbeddingElement> =
|
|
209
|
+
z
|
|
210
|
+
.object({
|
|
211
|
+
embedding: z.array(z.number()).optional(),
|
|
212
|
+
index: z.number().optional(),
|
|
213
|
+
object: z.enum(EmbeddingsV1ResponseEmbeddingElementObject).optional(),
|
|
214
|
+
})
|
|
215
|
+
.transform(d => ({
|
|
216
|
+
embedding: d.embedding,
|
|
217
|
+
index: d.index,
|
|
218
|
+
object: d.object,
|
|
219
|
+
}));
|
|
220
|
+
|
|
221
|
+
export const unmarshalExternalModelUsageElementSchema: z.ZodType<ExternalModelUsageElement> =
|
|
222
|
+
z
|
|
223
|
+
.object({
|
|
224
|
+
prompt_tokens: z.number().optional(),
|
|
225
|
+
completion_tokens: z.number().optional(),
|
|
226
|
+
total_tokens: z.number().optional(),
|
|
227
|
+
})
|
|
228
|
+
.transform(d => ({
|
|
229
|
+
promptTokens: d.prompt_tokens,
|
|
230
|
+
completionTokens: d.completion_tokens,
|
|
231
|
+
totalTokens: d.total_tokens,
|
|
232
|
+
}));
|
|
233
|
+
|
|
234
|
+
export const unmarshalQueryEndpointResponseSchema: z.ZodType<QueryEndpointResponse> =
|
|
235
|
+
z
|
|
236
|
+
.object({
|
|
237
|
+
choices: z
|
|
238
|
+
.array(z.lazy(() => unmarshalV1ResponseChoiceElementSchema))
|
|
239
|
+
.optional(),
|
|
240
|
+
data: z
|
|
241
|
+
.array(
|
|
242
|
+
z.lazy(() => unmarshalEmbeddingsV1ResponseEmbeddingElementSchema)
|
|
243
|
+
)
|
|
244
|
+
.optional(),
|
|
245
|
+
model: z.string().optional(),
|
|
246
|
+
usage: z.lazy(() => unmarshalExternalModelUsageElementSchema).optional(),
|
|
247
|
+
id: z.string().optional(),
|
|
248
|
+
created: z
|
|
249
|
+
.union([z.number(), z.bigint()])
|
|
250
|
+
.transform(v => BigInt(v))
|
|
251
|
+
.optional(),
|
|
252
|
+
object: z.enum(QueryEndpointResponseObject).optional(),
|
|
253
|
+
predictions: z.array(jsonValueSchema).optional(),
|
|
254
|
+
outputs: z.array(jsonValueSchema).optional(),
|
|
255
|
+
'served-model-name': z.string().optional(),
|
|
256
|
+
})
|
|
257
|
+
.transform(d => ({
|
|
258
|
+
choices: d.choices,
|
|
259
|
+
data: d.data,
|
|
260
|
+
model: d.model,
|
|
261
|
+
usage: d.usage,
|
|
262
|
+
id: d.id,
|
|
263
|
+
created: d.created,
|
|
264
|
+
object: d.object,
|
|
265
|
+
predictions: d.predictions,
|
|
266
|
+
outputs: d.outputs,
|
|
267
|
+
servedModelName: d['served-model-name'],
|
|
268
|
+
}));
|
|
269
|
+
|
|
270
|
+
export const unmarshalV1ResponseChoiceElementSchema: z.ZodType<V1ResponseChoiceElement> =
|
|
271
|
+
z
|
|
272
|
+
.object({
|
|
273
|
+
text: z.string().optional(),
|
|
274
|
+
message: z.lazy(() => unmarshalChatMessageSchema).optional(),
|
|
275
|
+
index: z.number().optional(),
|
|
276
|
+
finishReason: z.string().optional(),
|
|
277
|
+
logprobs: z.number().optional(),
|
|
278
|
+
})
|
|
279
|
+
.transform(d => ({
|
|
280
|
+
text: d.text,
|
|
281
|
+
message: d.message,
|
|
282
|
+
index: d.index,
|
|
283
|
+
finishReason: d.finishReason,
|
|
284
|
+
logprobs: d.logprobs,
|
|
285
|
+
}));
|
|
286
|
+
|
|
287
|
+
export const marshalChatMessageSchema: z.ZodType = z
|
|
288
|
+
.object({
|
|
289
|
+
role: z.enum(ChatMessageRole).optional(),
|
|
290
|
+
content: z.string().optional(),
|
|
291
|
+
})
|
|
292
|
+
.transform(d => ({
|
|
293
|
+
role: d.role,
|
|
294
|
+
content: d.content,
|
|
295
|
+
}));
|
|
296
|
+
|
|
297
|
+
export const marshalDataframeSplitInputSchema: z.ZodType = z
|
|
298
|
+
.object({
|
|
299
|
+
index: z.array(z.number()).optional(),
|
|
300
|
+
columns: z.array(jsonValueSchema).optional(),
|
|
301
|
+
data: z.array(jsonValueSchema).optional(),
|
|
302
|
+
})
|
|
303
|
+
.transform(d => ({
|
|
304
|
+
index: d.index,
|
|
305
|
+
columns: d.columns,
|
|
306
|
+
data: d.data,
|
|
307
|
+
}));
|
|
308
|
+
|
|
309
|
+
export const marshalQueryEndpointInputRequestSchema: z.ZodType = z
|
|
310
|
+
.object({
|
|
311
|
+
name: z.string().optional(),
|
|
312
|
+
prompt: jsonValueSchema.optional(),
|
|
313
|
+
input: jsonValueSchema.optional(),
|
|
314
|
+
messages: z.array(z.lazy(() => marshalChatMessageSchema)).optional(),
|
|
315
|
+
temperature: z.number().optional(),
|
|
316
|
+
stop: z.array(z.string()).optional(),
|
|
317
|
+
maxTokens: z.number().optional(),
|
|
318
|
+
n: z.number().optional(),
|
|
319
|
+
stream: z.boolean().optional(),
|
|
320
|
+
extraParams: z.record(z.string(), z.string()).optional(),
|
|
321
|
+
dataframeRecords: z.array(jsonValueSchema).optional(),
|
|
322
|
+
dataframeSplit: z.lazy(() => marshalDataframeSplitInputSchema).optional(),
|
|
323
|
+
instances: z.array(jsonValueSchema).optional(),
|
|
324
|
+
inputs: jsonValueSchema.optional(),
|
|
325
|
+
clientRequestId: z.string().optional(),
|
|
326
|
+
usageContext: z.record(z.string(), z.string()).optional(),
|
|
327
|
+
})
|
|
328
|
+
.transform(d => ({
|
|
329
|
+
name: d.name,
|
|
330
|
+
prompt: d.prompt,
|
|
331
|
+
input: d.input,
|
|
332
|
+
messages: d.messages,
|
|
333
|
+
temperature: d.temperature,
|
|
334
|
+
stop: d.stop,
|
|
335
|
+
max_tokens: d.maxTokens,
|
|
336
|
+
n: d.n,
|
|
337
|
+
stream: d.stream,
|
|
338
|
+
extra_params: d.extraParams,
|
|
339
|
+
dataframe_records: d.dataframeRecords,
|
|
340
|
+
dataframe_split: d.dataframeSplit,
|
|
341
|
+
instances: d.instances,
|
|
342
|
+
inputs: d.inputs,
|
|
343
|
+
client_request_id: d.clientRequestId,
|
|
344
|
+
usage_context: d.usageContext,
|
|
345
|
+
}));
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
import type {Credentials} from '@databricks/sdk-auth';
|
|
4
|
+
import {defaultCredentials} from '@databricks/sdk-auth/credentials';
|
|
5
|
+
import type {
|
|
6
|
+
HttpClient,
|
|
7
|
+
HttpRequest,
|
|
8
|
+
HttpResponse,
|
|
9
|
+
} from '@databricks/sdk-core/http';
|
|
10
|
+
import {newFetchHttpClient} from '@databricks/sdk-core/http';
|
|
11
|
+
import type {ClientOptions} from '@databricks/sdk-options/client';
|
|
12
|
+
|
|
13
|
+
/** Creates a new HTTP client with the given options. */
|
|
14
|
+
export function newHttpClient(options?: ClientOptions): HttpClient {
|
|
15
|
+
const opts = options ?? {};
|
|
16
|
+
|
|
17
|
+
// If an HTTP client is provided, use it as-is. Throw if other options are
|
|
18
|
+
// also set, since they would be silently ignored.
|
|
19
|
+
if (opts.httpClient !== undefined) {
|
|
20
|
+
if (opts.credentials !== undefined || opts.timeout !== undefined) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
'httpClient cannot be combined with credentials or timeout'
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
return opts.httpClient;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const credentials = opts.credentials ?? defaultCredentials();
|
|
29
|
+
|
|
30
|
+
const base = newFetchHttpClient();
|
|
31
|
+
let client: HttpClient = new AuthHttpClient(base, credentials);
|
|
32
|
+
|
|
33
|
+
if (opts.timeout !== undefined) {
|
|
34
|
+
client = new TimeoutHttpClient(client, opts.timeout);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return client;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Wraps an HttpClient and adds authentication headers to requests. */
|
|
41
|
+
class AuthHttpClient implements HttpClient {
|
|
42
|
+
constructor(
|
|
43
|
+
private readonly base: HttpClient,
|
|
44
|
+
private readonly credentials: Credentials
|
|
45
|
+
) {}
|
|
46
|
+
|
|
47
|
+
async send(request: HttpRequest): Promise<HttpResponse> {
|
|
48
|
+
const authHeaders = await this.credentials.authHeaders();
|
|
49
|
+
// Do not modify the original request.
|
|
50
|
+
const headers = new Headers(request.headers);
|
|
51
|
+
for (const h of authHeaders) {
|
|
52
|
+
headers.set(h.key, h.value);
|
|
53
|
+
}
|
|
54
|
+
return this.base.send({...request, headers});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Wraps an HttpClient and applies a default timeout to requests. */
|
|
59
|
+
class TimeoutHttpClient implements HttpClient {
|
|
60
|
+
constructor(
|
|
61
|
+
private readonly base: HttpClient,
|
|
62
|
+
private readonly timeout: number
|
|
63
|
+
) {}
|
|
64
|
+
|
|
65
|
+
async send(request: HttpRequest): Promise<HttpResponse> {
|
|
66
|
+
const timeoutSignal = AbortSignal.timeout(this.timeout);
|
|
67
|
+
const signal =
|
|
68
|
+
request.signal !== undefined
|
|
69
|
+
? AbortSignal.any([request.signal, timeoutSignal])
|
|
70
|
+
: timeoutSignal;
|
|
71
|
+
return this.base.send({...request, signal});
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/v1/utils.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Code generated from API definition by Databricks SDK Generator. DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
import type {Options} from '@databricks/sdk-core/ops';
|
|
4
|
+
import {execute} from '@databricks/sdk-core/ops';
|
|
5
|
+
import {ApiError} from '@databricks/sdk-core/apierror';
|
|
6
|
+
import type {
|
|
7
|
+
HttpClient,
|
|
8
|
+
HttpRequest,
|
|
9
|
+
HttpResponse,
|
|
10
|
+
} from '@databricks/sdk-core/http';
|
|
11
|
+
import type {Logger} from '@databricks/sdk-core/logger';
|
|
12
|
+
import type {CallOptions} from '@databricks/sdk-options/call';
|
|
13
|
+
import JSONBig from 'json-bigint';
|
|
14
|
+
import type {z} from 'zod';
|
|
15
|
+
|
|
16
|
+
// JSON codec that preserves int64 precision. On the way in, large integer
|
|
17
|
+
// literals come back as bigint instead of being rounded to JS Number. On the
|
|
18
|
+
// way out, bigint values are emitted as raw JSON number digits.
|
|
19
|
+
const jsonBigint = JSONBig({useNativeBigInt: true});
|
|
20
|
+
|
|
21
|
+
export interface HttpCallOptions {
|
|
22
|
+
readonly request: HttpRequest;
|
|
23
|
+
readonly httpClient: HttpClient;
|
|
24
|
+
readonly logger: Logger;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Translates public CallOptions to the internal Options shape accepted by
|
|
29
|
+
* execute(). Even though the shapes match today, this isolates the public
|
|
30
|
+
* API from the executor's internal type so they can diverge.
|
|
31
|
+
*/
|
|
32
|
+
export async function executeCall(
|
|
33
|
+
call: (signal?: AbortSignal) => Promise<void>,
|
|
34
|
+
options?: CallOptions
|
|
35
|
+
): Promise<void> {
|
|
36
|
+
const opts: Options = {
|
|
37
|
+
...(options?.retrier !== undefined && {retrier: options.retrier}),
|
|
38
|
+
...(options?.rateLimiter !== undefined && {
|
|
39
|
+
rateLimiter: options.rateLimiter,
|
|
40
|
+
}),
|
|
41
|
+
...(options?.timeout !== undefined && {timeout: options.timeout}),
|
|
42
|
+
};
|
|
43
|
+
return execute(options?.signal, call, opts);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function readAll(
|
|
47
|
+
body: ReadableStream<Uint8Array> | null
|
|
48
|
+
): Promise<Uint8Array> {
|
|
49
|
+
if (body === null) {
|
|
50
|
+
return new Uint8Array(0);
|
|
51
|
+
}
|
|
52
|
+
const reader = body.getReader();
|
|
53
|
+
const chunks: Uint8Array[] = [];
|
|
54
|
+
for (;;) {
|
|
55
|
+
const {done, value} = await reader.read();
|
|
56
|
+
if (done) {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
chunks.push(value);
|
|
60
|
+
}
|
|
61
|
+
const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
|
|
62
|
+
const result = new Uint8Array(totalLength);
|
|
63
|
+
let offset = 0;
|
|
64
|
+
for (const chunk of chunks) {
|
|
65
|
+
result.set(chunk, offset);
|
|
66
|
+
offset += chunk.length;
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function executeHttpCall(
|
|
72
|
+
opts: HttpCallOptions
|
|
73
|
+
): Promise<Uint8Array> {
|
|
74
|
+
opts.logger.debug('HTTP request', {
|
|
75
|
+
method: opts.request.method,
|
|
76
|
+
url: opts.request.url,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
let resp: HttpResponse;
|
|
80
|
+
try {
|
|
81
|
+
resp = await opts.httpClient.send(opts.request);
|
|
82
|
+
} catch (e: unknown) {
|
|
83
|
+
opts.logger.debug('HTTP request failed');
|
|
84
|
+
throw e;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const body = await readAll(resp.body);
|
|
88
|
+
|
|
89
|
+
opts.logger.debug('HTTP response', {
|
|
90
|
+
statusCode: resp.statusCode,
|
|
91
|
+
body: new TextDecoder().decode(body),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const apiErr = ApiError.fromHttpError(resp.statusCode, resp.headers, body);
|
|
95
|
+
if (apiErr !== undefined) {
|
|
96
|
+
throw apiErr;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return body;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function buildHttpRequest(
|
|
103
|
+
method: string,
|
|
104
|
+
url: string,
|
|
105
|
+
headers: Headers,
|
|
106
|
+
signal?: AbortSignal,
|
|
107
|
+
body?: string | ReadableStream<Uint8Array>
|
|
108
|
+
): HttpRequest {
|
|
109
|
+
const req: HttpRequest = {url, method, headers};
|
|
110
|
+
if (body !== undefined) {
|
|
111
|
+
req.body = body;
|
|
112
|
+
}
|
|
113
|
+
if (signal !== undefined) {
|
|
114
|
+
req.signal = signal;
|
|
115
|
+
}
|
|
116
|
+
return req;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function parseResponse<T>(body: Uint8Array, schema: z.ZodType<T>): T {
|
|
120
|
+
const text = new TextDecoder().decode(body);
|
|
121
|
+
const parsed: unknown = jsonBigint.parse(text);
|
|
122
|
+
return schema.parse(parsed);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function marshalRequest(data: unknown, schema: z.ZodType): string {
|
|
126
|
+
return jsonBigint.stringify(schema.parse(data));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function flattenQueryParams(
|
|
130
|
+
prefix: string,
|
|
131
|
+
value: unknown,
|
|
132
|
+
params: URLSearchParams
|
|
133
|
+
): void {
|
|
134
|
+
if (value === null || value === undefined) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (Array.isArray(value)) {
|
|
138
|
+
// arrays of objects are not yet supported
|
|
139
|
+
for (const item of value) {
|
|
140
|
+
params.append(prefix, String(item));
|
|
141
|
+
}
|
|
142
|
+
} else if (typeof value === 'object') {
|
|
143
|
+
for (const [key, val] of Object.entries(value as Record<string, unknown>)) {
|
|
144
|
+
flattenQueryParams(`${prefix}.${key}`, val, params);
|
|
145
|
+
}
|
|
146
|
+
} else if (
|
|
147
|
+
typeof value === 'string' ||
|
|
148
|
+
typeof value === 'number' ||
|
|
149
|
+
typeof value === 'boolean' ||
|
|
150
|
+
typeof value === 'bigint'
|
|
151
|
+
) {
|
|
152
|
+
params.append(prefix, String(value));
|
|
153
|
+
} else {
|
|
154
|
+
throw new Error(`Unsupported query parameter type: ${typeof value}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
package/README.md
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
This is a placeholder release used to enable OIDC trusted publishing. Real code lands in a later version.
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = {};
|