@databricks/sdk-core 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.
Files changed (164) hide show
  1. package/LICENSE +203 -0
  2. package/README.md +11 -1
  3. package/dist/api/execute.d.ts +12 -0
  4. package/dist/api/execute.d.ts.map +1 -0
  5. package/dist/api/execute.js +77 -0
  6. package/dist/api/execute.js.map +1 -0
  7. package/dist/api/index.d.ts +12 -0
  8. package/dist/api/index.d.ts.map +1 -0
  9. package/dist/api/index.js +8 -0
  10. package/dist/api/index.js.map +1 -0
  11. package/dist/api/limiter.d.ts +9 -0
  12. package/dist/api/limiter.d.ts.map +1 -0
  13. package/dist/api/limiter.js +2 -0
  14. package/dist/api/limiter.js.map +1 -0
  15. package/dist/api/options.d.ts +22 -0
  16. package/dist/api/options.d.ts.map +1 -0
  17. package/dist/api/options.js +2 -0
  18. package/dist/api/options.js.map +1 -0
  19. package/dist/api/retrier.d.ts +58 -0
  20. package/dist/api/retrier.d.ts.map +1 -0
  21. package/dist/api/retrier.js +70 -0
  22. package/dist/api/retrier.js.map +1 -0
  23. package/dist/apierror/apierror.d.ts +53 -0
  24. package/dist/apierror/apierror.d.ts.map +1 -0
  25. package/dist/apierror/apierror.js +217 -0
  26. package/dist/apierror/apierror.js.map +1 -0
  27. package/dist/apierror/codes/codes.d.ts +140 -0
  28. package/dist/apierror/codes/codes.d.ts.map +1 -0
  29. package/dist/apierror/codes/codes.js +167 -0
  30. package/dist/apierror/codes/codes.js.map +1 -0
  31. package/dist/apierror/codes/index.d.ts +7 -0
  32. package/dist/apierror/codes/index.d.ts.map +1 -0
  33. package/dist/apierror/codes/index.js +7 -0
  34. package/dist/apierror/codes/index.js.map +1 -0
  35. package/dist/apierror/details.d.ts +177 -0
  36. package/dist/apierror/details.d.ts.map +1 -0
  37. package/dist/apierror/details.js +250 -0
  38. package/dist/apierror/details.js.map +1 -0
  39. package/dist/apierror/index.d.ts +8 -0
  40. package/dist/apierror/index.d.ts.map +1 -0
  41. package/dist/apierror/index.js +7 -0
  42. package/dist/apierror/index.js.map +1 -0
  43. package/dist/clientinfo/agent.d.ts +56 -0
  44. package/dist/clientinfo/agent.d.ts.map +1 -0
  45. package/dist/clientinfo/agent.js +114 -0
  46. package/dist/clientinfo/agent.js.map +1 -0
  47. package/dist/clientinfo/base.d.ts +39 -0
  48. package/dist/clientinfo/base.d.ts.map +1 -0
  49. package/dist/clientinfo/base.js +62 -0
  50. package/dist/clientinfo/base.js.map +1 -0
  51. package/dist/clientinfo/clientinfo.d.ts +61 -0
  52. package/dist/clientinfo/clientinfo.d.ts.map +1 -0
  53. package/dist/clientinfo/clientinfo.js +96 -0
  54. package/dist/clientinfo/clientinfo.js.map +1 -0
  55. package/dist/clientinfo/default.browser.d.ts +17 -0
  56. package/dist/clientinfo/default.browser.d.ts.map +1 -0
  57. package/dist/clientinfo/default.browser.js +20 -0
  58. package/dist/clientinfo/default.browser.js.map +1 -0
  59. package/dist/clientinfo/default.d.ts +14 -0
  60. package/dist/clientinfo/default.d.ts.map +1 -0
  61. package/dist/clientinfo/default.js +104 -0
  62. package/dist/clientinfo/default.js.map +1 -0
  63. package/dist/clientinfo/index.browser.d.ts +5 -0
  64. package/dist/clientinfo/index.browser.d.ts.map +1 -0
  65. package/dist/clientinfo/index.browser.js +4 -0
  66. package/dist/clientinfo/index.browser.js.map +1 -0
  67. package/dist/clientinfo/index.d.ts +5 -0
  68. package/dist/clientinfo/index.d.ts.map +1 -0
  69. package/dist/clientinfo/index.js +4 -0
  70. package/dist/clientinfo/index.js.map +1 -0
  71. package/dist/http/http.d.ts +40 -0
  72. package/dist/http/http.d.ts.map +1 -0
  73. package/dist/http/http.js +37 -0
  74. package/dist/http/http.js.map +1 -0
  75. package/dist/http/index.d.ts +8 -0
  76. package/dist/http/index.d.ts.map +1 -0
  77. package/dist/http/index.js +7 -0
  78. package/dist/http/index.js.map +1 -0
  79. package/dist/index.d.ts +6 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/index.js +7 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/logger/index.d.ts +8 -0
  84. package/dist/logger/index.d.ts.map +1 -0
  85. package/dist/logger/index.js +7 -0
  86. package/dist/logger/index.js.map +1 -0
  87. package/dist/logger/logger.d.ts +49 -0
  88. package/dist/logger/logger.d.ts.map +1 -0
  89. package/dist/logger/logger.js +65 -0
  90. package/dist/logger/logger.js.map +1 -0
  91. package/dist/profiles/errors.d.ts +17 -0
  92. package/dist/profiles/errors.d.ts.map +1 -0
  93. package/dist/profiles/errors.js +19 -0
  94. package/dist/profiles/errors.js.map +1 -0
  95. package/dist/profiles/index.browser.d.ts +10 -0
  96. package/dist/profiles/index.browser.d.ts.map +1 -0
  97. package/dist/profiles/index.browser.js +8 -0
  98. package/dist/profiles/index.browser.js.map +1 -0
  99. package/dist/profiles/index.d.ts +15 -0
  100. package/dist/profiles/index.d.ts.map +1 -0
  101. package/dist/profiles/index.js +13 -0
  102. package/dist/profiles/index.js.map +1 -0
  103. package/dist/profiles/ini.d.ts +36 -0
  104. package/dist/profiles/ini.d.ts.map +1 -0
  105. package/dist/profiles/ini.js +113 -0
  106. package/dist/profiles/ini.js.map +1 -0
  107. package/dist/profiles/profile.d.ts +131 -0
  108. package/dist/profiles/profile.d.ts.map +1 -0
  109. package/dist/profiles/profile.js +307 -0
  110. package/dist/profiles/profile.js.map +1 -0
  111. package/dist/profiles/resolve.d.ts +29 -0
  112. package/dist/profiles/resolve.d.ts.map +1 -0
  113. package/dist/profiles/resolve.js +206 -0
  114. package/dist/profiles/resolve.js.map +1 -0
  115. package/dist/profiles/secret.d.ts +25 -0
  116. package/dist/profiles/secret.d.ts.map +1 -0
  117. package/dist/profiles/secret.js +38 -0
  118. package/dist/profiles/secret.js.map +1 -0
  119. package/dist/wkt/fieldmask.d.ts +32 -0
  120. package/dist/wkt/fieldmask.d.ts.map +1 -0
  121. package/dist/wkt/fieldmask.js +68 -0
  122. package/dist/wkt/fieldmask.js.map +1 -0
  123. package/dist/wkt/index.d.ts +4 -0
  124. package/dist/wkt/index.d.ts.map +1 -0
  125. package/dist/wkt/index.js +2 -0
  126. package/dist/wkt/index.js.map +1 -0
  127. package/dist/wkt/value.d.ts +13 -0
  128. package/dist/wkt/value.d.ts.map +1 -0
  129. package/dist/wkt/value.js +2 -0
  130. package/dist/wkt/value.js.map +1 -0
  131. package/package.json +82 -4
  132. package/src/api/execute.ts +102 -0
  133. package/src/api/index.ts +12 -0
  134. package/src/api/limiter.ts +8 -0
  135. package/src/api/options.ts +22 -0
  136. package/src/api/retrier.ts +108 -0
  137. package/src/apierror/apierror.ts +253 -0
  138. package/src/apierror/codes/codes.ts +189 -0
  139. package/src/apierror/codes/index.ts +7 -0
  140. package/src/apierror/details.ts +459 -0
  141. package/src/apierror/index.ts +24 -0
  142. package/src/clientinfo/agent.ts +125 -0
  143. package/src/clientinfo/base.ts +73 -0
  144. package/src/clientinfo/clientinfo.ts +129 -0
  145. package/src/clientinfo/default.browser.ts +24 -0
  146. package/src/clientinfo/default.ts +128 -0
  147. package/src/clientinfo/index.browser.ts +4 -0
  148. package/src/clientinfo/index.ts +4 -0
  149. package/src/http/http.ts +75 -0
  150. package/src/http/index.ts +8 -0
  151. package/src/index.ts +5 -0
  152. package/src/logger/index.ts +8 -0
  153. package/src/logger/logger.ts +99 -0
  154. package/src/profiles/errors.ts +28 -0
  155. package/src/profiles/index.browser.ts +10 -0
  156. package/src/profiles/index.ts +15 -0
  157. package/src/profiles/ini.ts +126 -0
  158. package/src/profiles/profile.ts +467 -0
  159. package/src/profiles/resolve.ts +251 -0
  160. package/src/profiles/secret.ts +40 -0
  161. package/src/wkt/fieldmask.ts +89 -0
  162. package/src/wkt/index.ts +3 -0
  163. package/src/wkt/value.ts +19 -0
  164. package/index.js +0 -1
@@ -0,0 +1,459 @@
1
+ /**
2
+ * Defines structured error detail types for API errors.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ import {z} from 'zod';
8
+
9
+ /**
10
+ * ErrorDetails contains the error details of an API error. It is the union of
11
+ * known error details types and unknown details.
12
+ */
13
+ export interface ErrorDetails {
14
+ errorInfo?: ErrorInfo;
15
+ requestInfo?: RequestInfo;
16
+ retryInfo?: RetryInfo;
17
+ debugInfo?: DebugInfo;
18
+ quotaFailure?: QuotaFailure;
19
+ preconditionFailure?: PreconditionFailure;
20
+ badRequest?: BadRequest;
21
+ resourceInfo?: ResourceInfo;
22
+ help?: Help;
23
+
24
+ /**
25
+ * UnknownDetails contains error details that cannot be parsed into one of
26
+ * the known types above.
27
+ */
28
+ unknownDetails: unknown[];
29
+ }
30
+
31
+ /** ErrorInfo describes the cause of the error with structured details. */
32
+ export interface ErrorInfo {
33
+ /**
34
+ * The reason of the error. This is a constant value that identifies the
35
+ * proximate cause of the error.
36
+ */
37
+ reason: string;
38
+
39
+ /** The logical grouping to which the "reason" belongs. */
40
+ domain: string;
41
+
42
+ /** Additional structured details about this error. */
43
+ metadata: Record<string, string>;
44
+ }
45
+
46
+ /**
47
+ * RequestInfo contains metadata about the request that clients can attach when
48
+ * filing a bug or providing other forms of feedback.
49
+ */
50
+ export interface RequestInfo {
51
+ /**
52
+ * An opaque string that should only be interpreted by the service that
53
+ * generated it. For example, it can be used to identify requests in the
54
+ * service's logs.
55
+ */
56
+ requestId: string;
57
+
58
+ /**
59
+ * Any data that was used to serve this request. For example, an encrypted
60
+ * stack trace that can be sent back to the service provider for debugging.
61
+ */
62
+ servingData: string;
63
+ }
64
+
65
+ /**
66
+ * RetryInfo describes when the clients can retry a failed request. Clients
67
+ * could ignore the recommendation here or retry when this information is
68
+ * missing from error responses.
69
+ *
70
+ * It's always recommended that clients should use exponential backoff when
71
+ * retrying.
72
+ *
73
+ * Clients should wait until `retryDelayMs` amount of time has passed since
74
+ * receiving the error response before retrying. If retrying requests also
75
+ * fail, clients should use an exponential backoff scheme to gradually increase
76
+ * the delay between retries based on `retryDelayMs`, until either a maximum
77
+ * number of retries have been reached or a maximum retry delay cap has been
78
+ * reached.
79
+ */
80
+ export interface RetryInfo {
81
+ /** Clients should wait at least this long between retrying the same request, in milliseconds. */
82
+ retryDelayMs: number;
83
+ }
84
+
85
+ /** Describes additional debugging info. */
86
+ export interface DebugInfo {
87
+ /** The stack trace entries indicating where the error occurred. */
88
+ stackEntries: string[];
89
+
90
+ /** Additional debugging information provided by the server. */
91
+ detail: string;
92
+ }
93
+
94
+ /**
95
+ * Describes how a quota check failed.
96
+ *
97
+ * For example if a daily limit was exceeded for the calling project,
98
+ * a service could respond with a QuotaFailure detail containing the project
99
+ * id and the description of the quota limit that was exceeded. If the
100
+ * calling project hasn't enabled the service in the developer console, then
101
+ * a service could respond with the project id and set `service_disabled`
102
+ * to true.
103
+ *
104
+ * Also see RetryInfo and Help types for other details about handling a
105
+ * quota failure.
106
+ */
107
+ export interface QuotaFailure {
108
+ /** Describes all quota violations. */
109
+ violations: QuotaFailureViolation[];
110
+ }
111
+
112
+ export interface QuotaFailureViolation {
113
+ /** The subject on which the quota check failed. */
114
+ subject: string;
115
+
116
+ /**
117
+ * A description of how the quota check failed. Clients can use this
118
+ * description to find more about the quota configuration in the service's
119
+ * public documentation, or find the relevant quota limit to adjust through
120
+ * developer console.
121
+ *
122
+ * For example: "Service disabled" or "Daily Limit for read operations
123
+ * exceeded".
124
+ */
125
+ description: string;
126
+ }
127
+
128
+ /** Describes what preconditions have failed. */
129
+ export interface PreconditionFailure {
130
+ /** Describes all precondition violations. */
131
+ violations: PreconditionFailureViolation[];
132
+ }
133
+
134
+ export interface PreconditionFailureViolation {
135
+ /** The type of PreconditionFailure. */
136
+ type: string;
137
+
138
+ /** The subject, relative to the type, that failed. */
139
+ subject: string;
140
+
141
+ /**
142
+ * A description of how the precondition failed. Developers can use this
143
+ * description to understand how to fix the failure.
144
+ *
145
+ * For example: "Terms of service not accepted".
146
+ */
147
+ description: string;
148
+ }
149
+
150
+ /**
151
+ * Describes violations in a client request. This error type focuses on the
152
+ * syntactic aspects of the request.
153
+ */
154
+ export interface BadRequest {
155
+ fieldViolations: BadRequestFieldViolation[];
156
+ }
157
+
158
+ export interface BadRequestFieldViolation {
159
+ /** A path leading to a field in the request body. */
160
+ field: string;
161
+
162
+ /** A description of why the request element is bad. */
163
+ description: string;
164
+ }
165
+
166
+ /** Describes the resource that is being accessed. */
167
+ export interface ResourceInfo {
168
+ /** A name for the type of resource being accessed. */
169
+ resourceType: string;
170
+
171
+ /** The name of the resource being accessed. */
172
+ resourceName: string;
173
+
174
+ /** The owner of the resource (optional). */
175
+ owner: string;
176
+
177
+ /** Describes what error is encountered when accessing this resource. */
178
+ description: string;
179
+ }
180
+
181
+ /**
182
+ * Provides links to documentation or for performing an out of band action.
183
+ *
184
+ * For example, if a quota check failed with an error indicating the calling
185
+ * project hasn't enabled the accessed service, this can contain a URL pointing
186
+ * directly to the right place in the developer console to flip the bit.
187
+ */
188
+ export interface Help {
189
+ /** URL(s) pointing to additional information on handling the current error. */
190
+ links: HelpLink[];
191
+ }
192
+
193
+ export interface HelpLink {
194
+ /** Describes what the link offers. */
195
+ description: string;
196
+
197
+ /** The URL of the link. */
198
+ url: string;
199
+ }
200
+
201
+ // ---------------------------------------------------------------------------
202
+ // Zod schemas for the wire format (snake_case JSON from the API).
203
+ //
204
+ // Each schema validates the snake_case wire format from the API and transforms
205
+ // it into the camelCase TypeScript interface. Nullish fields default to their
206
+ // zero values (empty string, empty array, empty record) when absent or null.
207
+ // ---------------------------------------------------------------------------
208
+
209
+ // Reusable schema fragments. Nullish fields default to their zero values
210
+ // (empty string, empty array, empty record) when absent or null.
211
+ const nullishString = z
212
+ .string()
213
+ .nullish()
214
+ .transform(v => v ?? '');
215
+
216
+ const errorInfoSchema = z.object({
217
+ reason: nullishString,
218
+ domain: nullishString,
219
+ metadata: z
220
+ .record(z.string(), z.string())
221
+ .nullish()
222
+ .transform(v => v ?? {}),
223
+ });
224
+
225
+ const requestInfoSchema = z
226
+ .object({
227
+ request_id: nullishString,
228
+ serving_data: nullishString,
229
+ })
230
+ .transform(d => ({
231
+ requestId: d.request_id,
232
+ servingData: d.serving_data,
233
+ }));
234
+
235
+ // Parses a protobuf Duration string (e.g. "42.000000001s") into milliseconds.
236
+ // Null/undefined defaults to 0. Invalid format causes a Zod validation failure.
237
+ const nullishProtoDuration = z
238
+ .string()
239
+ .nullish()
240
+ .transform((v, ctx) => {
241
+ if (v === null || v === undefined) {
242
+ return 0;
243
+ }
244
+ const ms = parseProtoDuration(v);
245
+ if (ms === undefined) {
246
+ ctx.addIssue({code: 'custom', message: 'Invalid protobuf duration.'});
247
+ return z.NEVER;
248
+ }
249
+ return ms;
250
+ });
251
+
252
+ const retryInfoSchema = z
253
+ .object({
254
+ retry_delay: nullishProtoDuration,
255
+ })
256
+ .transform(d => ({
257
+ retryDelayMs: d.retry_delay,
258
+ }));
259
+
260
+ const debugInfoSchema = z
261
+ .object({
262
+ stack_entries: z
263
+ .array(z.string())
264
+ .nullish()
265
+ .transform(v => v ?? []),
266
+ detail: nullishString,
267
+ })
268
+ .transform(d => ({
269
+ stackEntries: d.stack_entries,
270
+ detail: d.detail,
271
+ }));
272
+
273
+ const quotaViolationSchema = z.object({
274
+ subject: nullishString,
275
+ description: nullishString,
276
+ });
277
+
278
+ const quotaFailureSchema = z.object({
279
+ violations: z
280
+ .array(quotaViolationSchema)
281
+ .nullish()
282
+ .transform(v => v ?? []),
283
+ });
284
+
285
+ const preconditionViolationSchema = z.object({
286
+ type: nullishString,
287
+ subject: nullishString,
288
+ description: nullishString,
289
+ });
290
+
291
+ const preconditionFailureSchema = z.object({
292
+ violations: z
293
+ .array(preconditionViolationSchema)
294
+ .nullish()
295
+ .transform(v => v ?? []),
296
+ });
297
+
298
+ const fieldViolationSchema = z.object({
299
+ field: nullishString,
300
+ description: nullishString,
301
+ });
302
+
303
+ const badRequestSchema = z
304
+ .object({
305
+ field_violations: z
306
+ .array(fieldViolationSchema)
307
+ .nullish()
308
+ .transform(v => v ?? []),
309
+ })
310
+ .transform(d => ({
311
+ fieldViolations: d.field_violations,
312
+ }));
313
+
314
+ const resourceInfoSchema = z
315
+ .object({
316
+ resource_type: nullishString,
317
+ resource_name: nullishString,
318
+ owner: nullishString,
319
+ description: nullishString,
320
+ })
321
+ .transform(d => ({
322
+ resourceType: d.resource_type,
323
+ resourceName: d.resource_name,
324
+ owner: d.owner,
325
+ description: d.description,
326
+ }));
327
+
328
+ const helpLinkSchema = z.object({
329
+ description: nullishString,
330
+ url: nullishString,
331
+ });
332
+
333
+ const helpSchema = z.object({
334
+ links: z
335
+ .array(helpLinkSchema)
336
+ .nullish()
337
+ .transform(v => v ?? []),
338
+ });
339
+
340
+ // Google RPC type URLs used to identify error detail types in the wire format.
341
+ const ERROR_INFO_TYPE = 'type.googleapis.com/google.rpc.ErrorInfo';
342
+ const REQUEST_INFO_TYPE = 'type.googleapis.com/google.rpc.RequestInfo';
343
+ const RETRY_INFO_TYPE = 'type.googleapis.com/google.rpc.RetryInfo';
344
+ const DEBUG_INFO_TYPE = 'type.googleapis.com/google.rpc.DebugInfo';
345
+ const QUOTA_FAILURE_TYPE = 'type.googleapis.com/google.rpc.QuotaFailure';
346
+ const PRECONDITION_FAILURE_TYPE =
347
+ 'type.googleapis.com/google.rpc.PreconditionFailure';
348
+ const BAD_REQUEST_TYPE = 'type.googleapis.com/google.rpc.BadRequest';
349
+ const RESOURCE_INFO_TYPE = 'type.googleapis.com/google.rpc.ResourceInfo';
350
+ const HELP_TYPE = 'type.googleapis.com/google.rpc.Help';
351
+
352
+ // Checks whether a value is a plain object (not null, not an array).
353
+ function isRecord(value: unknown): value is Record<string, unknown> {
354
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
355
+ }
356
+
357
+ // Parses a protobuf Duration string (e.g. "42.000000001s") into milliseconds.
358
+ // Returns undefined if the format is invalid.
359
+ function parseProtoDuration(s: string): number | undefined {
360
+ if (!s.endsWith('s')) {
361
+ return undefined;
362
+ }
363
+ const seconds = Number(s.slice(0, -1));
364
+ if (Number.isNaN(seconds)) {
365
+ return undefined;
366
+ }
367
+ return seconds * 1000;
368
+ }
369
+
370
+ // Attempts to parse a single detail and assign it to the right ErrorDetails
371
+ // field. Returns true if the detail was recognized and valid, false otherwise.
372
+ function tryAssignDetail(
373
+ ed: ErrorDetails,
374
+ type: string,
375
+ raw: unknown
376
+ ): boolean {
377
+ switch (type) {
378
+ case ERROR_INFO_TYPE: {
379
+ const r = errorInfoSchema.safeParse(raw);
380
+ if (!r.success) return false;
381
+ ed.errorInfo = r.data;
382
+ return true;
383
+ }
384
+ case REQUEST_INFO_TYPE: {
385
+ const r = requestInfoSchema.safeParse(raw);
386
+ if (!r.success) return false;
387
+ ed.requestInfo = r.data;
388
+ return true;
389
+ }
390
+ case RETRY_INFO_TYPE: {
391
+ const r = retryInfoSchema.safeParse(raw);
392
+ if (!r.success) return false;
393
+ ed.retryInfo = r.data;
394
+ return true;
395
+ }
396
+ case DEBUG_INFO_TYPE: {
397
+ const r = debugInfoSchema.safeParse(raw);
398
+ if (!r.success) return false;
399
+ ed.debugInfo = r.data;
400
+ return true;
401
+ }
402
+ case QUOTA_FAILURE_TYPE: {
403
+ const r = quotaFailureSchema.safeParse(raw);
404
+ if (!r.success) return false;
405
+ ed.quotaFailure = r.data;
406
+ return true;
407
+ }
408
+ case PRECONDITION_FAILURE_TYPE: {
409
+ const r = preconditionFailureSchema.safeParse(raw);
410
+ if (!r.success) return false;
411
+ ed.preconditionFailure = r.data;
412
+ return true;
413
+ }
414
+ case BAD_REQUEST_TYPE: {
415
+ const r = badRequestSchema.safeParse(raw);
416
+ if (!r.success) return false;
417
+ ed.badRequest = r.data;
418
+ return true;
419
+ }
420
+ case RESOURCE_INFO_TYPE: {
421
+ const r = resourceInfoSchema.safeParse(raw);
422
+ if (!r.success) return false;
423
+ ed.resourceInfo = r.data;
424
+ return true;
425
+ }
426
+ case HELP_TYPE: {
427
+ const r = helpSchema.safeParse(raw);
428
+ if (!r.success) return false;
429
+ ed.help = r.data;
430
+ return true;
431
+ }
432
+ default:
433
+ return false;
434
+ }
435
+ }
436
+
437
+ /**
438
+ * Parses an array of raw error detail values into a structured ErrorDetails
439
+ * object. If multiple details of the same known type are present, the last
440
+ * one wins.
441
+ */
442
+ export function parseErrorDetails(rawDetails: unknown[]): ErrorDetails {
443
+ const ed: ErrorDetails = {unknownDetails: []};
444
+ for (const rd of rawDetails) {
445
+ if (!isRecord(rd)) {
446
+ ed.unknownDetails.push(rd);
447
+ continue;
448
+ }
449
+ const type = rd['@type'];
450
+ if (typeof type !== 'string') {
451
+ ed.unknownDetails.push(rd);
452
+ continue;
453
+ }
454
+ if (!tryAssignDetail(ed, type, rd)) {
455
+ ed.unknownDetails.push(rd);
456
+ }
457
+ }
458
+ return ed;
459
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Databricks API error types.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ export {ApiError} from './apierror';
8
+
9
+ export type {
10
+ ErrorDetails,
11
+ ErrorInfo,
12
+ RequestInfo,
13
+ RetryInfo,
14
+ DebugInfo,
15
+ QuotaFailure,
16
+ QuotaFailureViolation,
17
+ PreconditionFailure,
18
+ PreconditionFailureViolation,
19
+ BadRequest,
20
+ BadRequestFieldViolation,
21
+ ResourceInfo,
22
+ Help,
23
+ HelpLink,
24
+ } from './details';
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Detects the AI coding agent (e.g. Claude Code, Cursor, Gemini CLI) that
3
+ * is running the current Node.js process. The detected product name is
4
+ * appended to the user-agent header so that Databricks can understand
5
+ * which agents are invoking the SDK.
6
+ *
7
+ * The agent list and precedence rules are kept in sync across the Go,
8
+ * Java, Python, and TypeScript SDKs.
9
+ *
10
+ * @module
11
+ */
12
+
13
+ interface KnownAgent {
14
+ readonly envVar: string;
15
+ readonly product: string;
16
+ }
17
+
18
+ // Name of the agents.md standard env var. When set to a value that no
19
+ // known agent recognizes, detection falls back to "unknown".
20
+ const AGENT_ENV_VAR = 'AGENT';
21
+
22
+ // Canonical list of AI coding agents. Keep this list in sync with the
23
+ // Go, Java, and Python SDKs. Agents are listed alphabetically by product
24
+ // name.
25
+ const KNOWN_AGENTS: readonly KnownAgent[] = [
26
+ // The amp agent also sets AGENT=amp, handled by the central fallback.
27
+ {envVar: 'AMP_CURRENT_THREAD_ID', product: 'amp'},
28
+ {envVar: 'ANTIGRAVITY_AGENT', product: 'antigravity'},
29
+ {envVar: 'AUGMENT_AGENT', product: 'augment'},
30
+ {envVar: 'CLAUDECODE', product: 'claude-code'},
31
+ {envVar: 'CLINE_ACTIVE', product: 'cline'},
32
+ {envVar: 'CODEX_CI', product: 'codex'},
33
+ {envVar: 'COPILOT_CLI', product: 'copilot-cli'},
34
+ {envVar: 'CURSOR_AGENT', product: 'cursor'},
35
+ {envVar: 'GEMINI_CLI', product: 'gemini-cli'},
36
+ // The goose agent also sets AGENT=goose, handled by the central
37
+ // fallback.
38
+ {envVar: 'GOOSE_TERMINAL', product: 'goose'},
39
+ {envVar: 'KIRO', product: 'kiro'},
40
+ {envVar: 'OPENCLAW_SHELL', product: 'openclaw'},
41
+ {envVar: 'OPENCODE', product: 'opencode'},
42
+ // Set by VS Code 1.121+ for agent-initiated terminal commands
43
+ // (https://code.visualstudio.com/updates/v1_121).
44
+ {envVar: 'VSCODE_AGENT', product: 'vscode-agent'},
45
+ {envVar: 'WINDSURF_AGENT', product: 'windsurf'},
46
+ ];
47
+
48
+ function agentEnvFallback(): string {
49
+ const v = process.env[AGENT_ENV_VAR];
50
+ if (v === undefined || v === '') {
51
+ return '';
52
+ }
53
+ if (KNOWN_AGENTS.some(a => a.product === v)) {
54
+ return v;
55
+ }
56
+ return 'unknown';
57
+ }
58
+
59
+ /**
60
+ * Checks environment variables for known AI agents and returns the
61
+ * detected product name.
62
+ *
63
+ * Explicit product-specific env vars always take precedence over the
64
+ * generic agents.md `AGENT` env var. `AGENT` is consulted only as a
65
+ * fallback when no explicit matcher fires, so that an explicit signal
66
+ * (e.g. `CLAUDECODE=1`) always wins over a conflicting `AGENT=<name>`
67
+ * value.
68
+ *
69
+ * Returns:
70
+ *
71
+ * - The product name when exactly one known env var is set.
72
+ * - `"multiple"` when multiple known env vars are set. Agent env vars
73
+ * can be stacked when one agent invokes another as a subagent (e.g.
74
+ * Claude Code spawning a Cursor CLI subprocess), so the child process
75
+ * inherits env vars from multiple layers.
76
+ * - When no known env var is set and `AGENT` is a non-empty value: the
77
+ * value itself if it names a known product, otherwise `"unknown"`.
78
+ * - `""` when nothing is set.
79
+ */
80
+ export function lookupAgentProvider(): string {
81
+ const matches: string[] = [];
82
+ for (const a of KNOWN_AGENTS) {
83
+ if (a.envVar in process.env) {
84
+ matches.push(a.product);
85
+ }
86
+ }
87
+ if (matches.length === 1) {
88
+ return matches[0];
89
+ }
90
+ if (matches.length > 1) {
91
+ return 'multiple';
92
+ }
93
+ return agentEnvFallback();
94
+ }
95
+
96
+ let cached: string | undefined;
97
+
98
+ /**
99
+ * Returns the detected AI agent name, cached for the process lifetime.
100
+ *
101
+ * Returns one of:
102
+ *
103
+ * - The known product name when exactly one agent is detected via
104
+ * explicit env matchers, or when `AGENT` is set to a known product
105
+ * name and no explicit matcher fired.
106
+ * - `"multiple"` when multiple explicit matchers fire for different
107
+ * agents (typically nested agents, e.g. Cursor CLI running as a
108
+ * Claude Code subagent).
109
+ * - `"unknown"` when no explicit matcher fired and `AGENT` is set to a
110
+ * value that is not a known product name.
111
+ * - `""` when no agent is detected.
112
+ */
113
+ export function agentProvider(): string {
114
+ cached ??= lookupAgentProvider();
115
+ return cached;
116
+ }
117
+
118
+ /**
119
+ * Clears the cached agent detection result so that the next call to
120
+ * {@link agentProvider} re-evaluates the environment. Exported for
121
+ * testing only.
122
+ */
123
+ export function clearAgentCache(): void {
124
+ cached = undefined;
125
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Shared state management for {@link ClientInfo} defaults. This module
3
+ * is browser-safe and contains no Node.js-specific APIs.
4
+ *
5
+ * @module
6
+ */
7
+
8
+ import {ClientInfo, ClientInfoError, isSemVer} from './clientinfo';
9
+ import pkgJson from '../../package.json' with {type: 'json'};
10
+
11
+ export const MODULE_NAME = 'sdk-js-core';
12
+ export const VERSION: string = pkgJson.version;
13
+
14
+ // Holds segments added via addToDefault, setProduct, and setPartner.
15
+ // createDefault returns a copy of this with env detection appended.
16
+ let base = ClientInfo.EMPTY;
17
+
18
+ /**
19
+ * Sets the product name and version globally. The version must be a
20
+ * valid semver string.
21
+ *
22
+ * Must be called before any client is created. Not safe for concurrent
23
+ * use.
24
+ */
25
+ export function setProduct(name: string, version: string): void {
26
+ if (!isSemVer(version)) {
27
+ throw new ClientInfoError(
28
+ 'INVALID_VERSION',
29
+ `Invalid version: ${version}.`
30
+ );
31
+ }
32
+ addToDefault(name, version);
33
+ }
34
+
35
+ /**
36
+ * Adds a partner identifier globally. Partner attribution is a
37
+ * first-class concept used for support ticket routing.
38
+ *
39
+ * Must be called before any client is created. Not safe for concurrent
40
+ * use.
41
+ */
42
+ export function setPartner(partner: string): void {
43
+ addToDefault('partner', partner);
44
+ }
45
+
46
+ /**
47
+ * Adds a global key/value segment that will be included in every
48
+ * {@link createDefault} call. Same key with different values is allowed
49
+ * (e.g., multiple partners). Exact key+value duplicates are silently
50
+ * ignored.
51
+ *
52
+ * Must be called before any client is created. Not safe for concurrent
53
+ * use.
54
+ */
55
+ export function addToDefault(key: string, value: string): void {
56
+ base = base.with({key, value});
57
+ }
58
+
59
+ // Returns the base ClientInfo for use in createDefault.
60
+ export function getBase(): ClientInfo {
61
+ return base;
62
+ }
63
+
64
+ // Resets the base segments. Exported for testing only.
65
+ export function resetBase(): void {
66
+ base = ClientInfo.EMPTY;
67
+ }
68
+
69
+ // Returns the base segments as a formatted string. Exported for
70
+ // testing only.
71
+ export function baseToString(): string {
72
+ return base.toString();
73
+ }