@anthropic-ai/foundry-sdk 0.2.2 → 0.2.4

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 (145) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/internal/decoders/jsonl.js.map +1 -1
  3. package/internal/decoders/jsonl.mjs.map +1 -1
  4. package/internal/parse.d.mts.map +1 -1
  5. package/internal/parse.d.ts.map +1 -1
  6. package/internal/parse.js +5 -0
  7. package/internal/parse.js.map +1 -1
  8. package/internal/parse.mjs +5 -0
  9. package/internal/parse.mjs.map +1 -1
  10. package/internal/qs/formats.d.mts +7 -0
  11. package/internal/qs/formats.d.mts.map +1 -0
  12. package/internal/qs/formats.d.ts +7 -0
  13. package/internal/qs/formats.d.ts.map +1 -0
  14. package/internal/qs/formats.js +13 -0
  15. package/internal/qs/formats.js.map +1 -0
  16. package/internal/qs/formats.mjs +9 -0
  17. package/internal/qs/formats.mjs.map +1 -0
  18. package/internal/qs/index.d.mts +10 -0
  19. package/internal/qs/index.d.mts.map +1 -0
  20. package/internal/qs/index.d.ts +10 -0
  21. package/internal/qs/index.d.ts.map +1 -0
  22. package/internal/qs/index.js +14 -0
  23. package/internal/qs/index.js.map +1 -0
  24. package/internal/qs/index.mjs +10 -0
  25. package/internal/qs/index.mjs.map +1 -0
  26. package/internal/qs/stringify.d.mts +3 -0
  27. package/internal/qs/stringify.d.mts.map +1 -0
  28. package/internal/qs/stringify.d.ts +3 -0
  29. package/internal/qs/stringify.d.ts.map +1 -0
  30. package/internal/qs/stringify.js +277 -0
  31. package/internal/qs/stringify.js.map +1 -0
  32. package/internal/qs/stringify.mjs +274 -0
  33. package/internal/qs/stringify.mjs.map +1 -0
  34. package/internal/qs/types.d.mts +57 -0
  35. package/internal/qs/types.d.mts.map +1 -0
  36. package/internal/qs/types.d.ts +57 -0
  37. package/internal/qs/types.d.ts.map +1 -0
  38. package/internal/qs/types.js +3 -0
  39. package/internal/qs/types.js.map +1 -0
  40. package/internal/qs/types.mjs +2 -0
  41. package/internal/qs/types.mjs.map +1 -0
  42. package/internal/qs/utils.d.mts +15 -0
  43. package/internal/qs/utils.d.mts.map +1 -0
  44. package/internal/qs/utils.d.ts +15 -0
  45. package/internal/qs/utils.d.ts.map +1 -0
  46. package/internal/qs/utils.js +230 -0
  47. package/internal/qs/utils.js.map +1 -0
  48. package/internal/qs/utils.mjs +217 -0
  49. package/internal/qs/utils.mjs.map +1 -0
  50. package/internal/types.d.mts +6 -6
  51. package/internal/types.d.mts.map +1 -1
  52. package/internal/types.d.ts +6 -6
  53. package/internal/types.d.ts.map +1 -1
  54. package/internal/utils/abort.d.mts +13 -0
  55. package/internal/utils/abort.d.mts.map +1 -0
  56. package/internal/utils/abort.d.ts +13 -0
  57. package/internal/utils/abort.d.ts.map +1 -0
  58. package/internal/utils/abort.js +26 -0
  59. package/internal/utils/abort.js.map +1 -0
  60. package/internal/utils/abort.mjs +23 -0
  61. package/internal/utils/abort.mjs.map +1 -0
  62. package/internal/utils/async-queue.d.mts +31 -0
  63. package/internal/utils/async-queue.d.mts.map +1 -0
  64. package/internal/utils/async-queue.d.ts +31 -0
  65. package/internal/utils/async-queue.d.ts.map +1 -0
  66. package/internal/utils/async-queue.js +85 -0
  67. package/internal/utils/async-queue.js.map +1 -0
  68. package/internal/utils/async-queue.mjs +81 -0
  69. package/internal/utils/async-queue.mjs.map +1 -0
  70. package/internal/utils/backoff.d.mts +23 -0
  71. package/internal/utils/backoff.d.mts.map +1 -0
  72. package/internal/utils/backoff.d.ts +23 -0
  73. package/internal/utils/backoff.d.ts.map +1 -0
  74. package/internal/utils/backoff.js +44 -0
  75. package/internal/utils/backoff.js.map +1 -0
  76. package/internal/utils/backoff.mjs +36 -0
  77. package/internal/utils/backoff.mjs.map +1 -0
  78. package/internal/utils/env.js +2 -2
  79. package/internal/utils/env.js.map +1 -1
  80. package/internal/utils/env.mjs +2 -2
  81. package/internal/utils/env.mjs.map +1 -1
  82. package/internal/utils/log.d.mts.map +1 -1
  83. package/internal/utils/log.d.ts.map +1 -1
  84. package/internal/utils/log.js +3 -2
  85. package/internal/utils/log.js.map +1 -1
  86. package/internal/utils/log.mjs +3 -2
  87. package/internal/utils/log.mjs.map +1 -1
  88. package/internal/utils/promise.d.mts +11 -0
  89. package/internal/utils/promise.d.mts.map +1 -0
  90. package/internal/utils/promise.d.ts +11 -0
  91. package/internal/utils/promise.d.ts.map +1 -0
  92. package/internal/utils/promise.js +18 -0
  93. package/internal/utils/promise.js.map +1 -0
  94. package/internal/utils/promise.mjs +15 -0
  95. package/internal/utils/promise.mjs.map +1 -0
  96. package/internal/utils/query.d.mts +2 -0
  97. package/internal/utils/query.d.mts.map +1 -0
  98. package/internal/utils/query.d.ts +2 -0
  99. package/internal/utils/query.d.ts.map +1 -0
  100. package/internal/utils/query.js +42 -0
  101. package/internal/utils/query.js.map +1 -0
  102. package/internal/utils/query.mjs +6 -0
  103. package/internal/utils/query.mjs.map +1 -0
  104. package/internal/utils/sleep.d.mts +9 -1
  105. package/internal/utils/sleep.d.mts.map +1 -1
  106. package/internal/utils/sleep.d.ts +9 -1
  107. package/internal/utils/sleep.d.ts.map +1 -1
  108. package/internal/utils/sleep.js +23 -2
  109. package/internal/utils/sleep.js.map +1 -1
  110. package/internal/utils/sleep.mjs +23 -2
  111. package/internal/utils/sleep.mjs.map +1 -1
  112. package/internal/utils/time.d.mts +3 -0
  113. package/internal/utils/time.d.mts.map +1 -0
  114. package/internal/utils/time.d.ts +3 -0
  115. package/internal/utils/time.d.ts.map +1 -0
  116. package/internal/utils/time.js +8 -0
  117. package/internal/utils/time.js.map +1 -0
  118. package/internal/utils/time.mjs +5 -0
  119. package/internal/utils/time.mjs.map +1 -0
  120. package/internal/utils.d.mts +1 -0
  121. package/internal/utils.d.ts +1 -0
  122. package/internal/utils.js +1 -0
  123. package/internal/utils.js.map +1 -1
  124. package/internal/utils.mjs +1 -0
  125. package/package.json +1 -1
  126. package/src/internal/decoders/jsonl.ts +2 -2
  127. package/src/internal/parse.ts +6 -0
  128. package/src/internal/qs/LICENSE.md +13 -0
  129. package/src/internal/qs/README.md +3 -0
  130. package/src/internal/qs/formats.ts +10 -0
  131. package/src/internal/qs/index.ts +13 -0
  132. package/src/internal/qs/stringify.ts +385 -0
  133. package/src/internal/qs/types.ts +71 -0
  134. package/src/internal/qs/utils.ts +265 -0
  135. package/src/internal/types.ts +6 -8
  136. package/src/internal/utils/abort.ts +21 -0
  137. package/src/internal/utils/async-queue.ts +66 -0
  138. package/src/internal/utils/backoff.ts +41 -0
  139. package/src/internal/utils/env.ts +2 -2
  140. package/src/internal/utils/log.ts +2 -1
  141. package/src/internal/utils/promise.ts +18 -0
  142. package/src/internal/utils/query.ts +7 -0
  143. package/src/internal/utils/sleep.ts +25 -2
  144. package/src/internal/utils/time.ts +4 -0
  145. package/src/internal/utils.ts +1 -0
@@ -0,0 +1,265 @@
1
+ import { RFC1738 } from './formats';
2
+ import type { DefaultEncoder, Format } from './types';
3
+ import { isArray } from '../utils/values';
4
+
5
+ export let has = (obj: object, key: PropertyKey): boolean => (
6
+ (has = (Object as any).hasOwn ?? Function.prototype.call.bind(Object.prototype.hasOwnProperty)),
7
+ has(obj, key)
8
+ );
9
+
10
+ const hex_table = /* @__PURE__ */ (() => {
11
+ const array = [];
12
+ for (let i = 0; i < 256; ++i) {
13
+ array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
14
+ }
15
+
16
+ return array;
17
+ })();
18
+
19
+ function compact_queue<T extends Record<string, any>>(queue: Array<{ obj: T; prop: string }>) {
20
+ while (queue.length > 1) {
21
+ const item = queue.pop();
22
+ if (!item) continue;
23
+
24
+ const obj = item.obj[item.prop];
25
+
26
+ if (isArray(obj)) {
27
+ const compacted: unknown[] = [];
28
+
29
+ for (let j = 0; j < obj.length; ++j) {
30
+ if (typeof obj[j] !== 'undefined') {
31
+ compacted.push(obj[j]);
32
+ }
33
+ }
34
+
35
+ // @ts-ignore
36
+ item.obj[item.prop] = compacted;
37
+ }
38
+ }
39
+ }
40
+
41
+ function array_to_object(source: any[], options: { plainObjects: boolean }) {
42
+ const obj = options && options.plainObjects ? Object.create(null) : {};
43
+ for (let i = 0; i < source.length; ++i) {
44
+ if (typeof source[i] !== 'undefined') {
45
+ obj[i] = source[i];
46
+ }
47
+ }
48
+
49
+ return obj;
50
+ }
51
+
52
+ export function merge(
53
+ target: any,
54
+ source: any,
55
+ options: { plainObjects?: boolean; allowPrototypes?: boolean } = {},
56
+ ) {
57
+ if (!source) {
58
+ return target;
59
+ }
60
+
61
+ if (typeof source !== 'object') {
62
+ if (isArray(target)) {
63
+ target.push(source);
64
+ } else if (target && typeof target === 'object') {
65
+ if ((options && (options.plainObjects || options.allowPrototypes)) || !has(Object.prototype, source)) {
66
+ target[source] = true;
67
+ }
68
+ } else {
69
+ return [target, source];
70
+ }
71
+
72
+ return target;
73
+ }
74
+
75
+ if (!target || typeof target !== 'object') {
76
+ return [target].concat(source);
77
+ }
78
+
79
+ let mergeTarget = target;
80
+ if (isArray(target) && !isArray(source)) {
81
+ // @ts-ignore
82
+ mergeTarget = array_to_object(target, options);
83
+ }
84
+
85
+ if (isArray(target) && isArray(source)) {
86
+ source.forEach(function (item, i) {
87
+ if (has(target, i)) {
88
+ const targetItem = target[i];
89
+ if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
90
+ target[i] = merge(targetItem, item, options);
91
+ } else {
92
+ target.push(item);
93
+ }
94
+ } else {
95
+ target[i] = item;
96
+ }
97
+ });
98
+ return target;
99
+ }
100
+
101
+ return Object.keys(source).reduce(function (acc, key) {
102
+ const value = source[key];
103
+
104
+ if (has(acc, key)) {
105
+ acc[key] = merge(acc[key], value, options);
106
+ } else {
107
+ acc[key] = value;
108
+ }
109
+ return acc;
110
+ }, mergeTarget);
111
+ }
112
+
113
+ export function assign_single_source(target: any, source: any) {
114
+ return Object.keys(source).reduce(function (acc, key) {
115
+ acc[key] = source[key];
116
+ return acc;
117
+ }, target);
118
+ }
119
+
120
+ export function decode(str: string, _: any, charset: string) {
121
+ const strWithoutPlus = str.replace(/\+/g, ' ');
122
+ if (charset === 'iso-8859-1') {
123
+ // unescape never throws, no try...catch needed:
124
+ return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
125
+ }
126
+ // utf-8
127
+ try {
128
+ return decodeURIComponent(strWithoutPlus);
129
+ } catch (e) {
130
+ return strWithoutPlus;
131
+ }
132
+ }
133
+
134
+ const limit = 1024;
135
+
136
+ export const encode: (
137
+ str: any,
138
+ defaultEncoder: DefaultEncoder,
139
+ charset: string,
140
+ type: 'key' | 'value',
141
+ format: Format,
142
+ ) => string = (str, _defaultEncoder, charset, _kind, format: Format) => {
143
+ // This code was originally written by Brian White for the io.js core querystring library.
144
+ // It has been adapted here for stricter adherence to RFC 3986
145
+ if (str.length === 0) {
146
+ return str;
147
+ }
148
+
149
+ let string = str;
150
+ if (typeof str === 'symbol') {
151
+ string = Symbol.prototype.toString.call(str);
152
+ } else if (typeof str !== 'string') {
153
+ string = String(str);
154
+ }
155
+
156
+ if (charset === 'iso-8859-1') {
157
+ return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
158
+ return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
159
+ });
160
+ }
161
+
162
+ let out = '';
163
+ for (let j = 0; j < string.length; j += limit) {
164
+ const segment = string.length >= limit ? string.slice(j, j + limit) : string;
165
+ const arr = [];
166
+
167
+ for (let i = 0; i < segment.length; ++i) {
168
+ let c = segment.charCodeAt(i);
169
+ if (
170
+ c === 0x2d || // -
171
+ c === 0x2e || // .
172
+ c === 0x5f || // _
173
+ c === 0x7e || // ~
174
+ (c >= 0x30 && c <= 0x39) || // 0-9
175
+ (c >= 0x41 && c <= 0x5a) || // a-z
176
+ (c >= 0x61 && c <= 0x7a) || // A-Z
177
+ (format === RFC1738 && (c === 0x28 || c === 0x29)) // ( )
178
+ ) {
179
+ arr[arr.length] = segment.charAt(i);
180
+ continue;
181
+ }
182
+
183
+ if (c < 0x80) {
184
+ arr[arr.length] = hex_table[c];
185
+ continue;
186
+ }
187
+
188
+ if (c < 0x800) {
189
+ arr[arr.length] = hex_table[0xc0 | (c >> 6)]! + hex_table[0x80 | (c & 0x3f)];
190
+ continue;
191
+ }
192
+
193
+ if (c < 0xd800 || c >= 0xe000) {
194
+ arr[arr.length] =
195
+ hex_table[0xe0 | (c >> 12)]! + hex_table[0x80 | ((c >> 6) & 0x3f)] + hex_table[0x80 | (c & 0x3f)];
196
+ continue;
197
+ }
198
+
199
+ i += 1;
200
+ c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff));
201
+
202
+ arr[arr.length] =
203
+ hex_table[0xf0 | (c >> 18)]! +
204
+ hex_table[0x80 | ((c >> 12) & 0x3f)] +
205
+ hex_table[0x80 | ((c >> 6) & 0x3f)] +
206
+ hex_table[0x80 | (c & 0x3f)];
207
+ }
208
+
209
+ out += arr.join('');
210
+ }
211
+
212
+ return out;
213
+ };
214
+
215
+ export function compact(value: any) {
216
+ const queue = [{ obj: { o: value }, prop: 'o' }];
217
+ const refs = [];
218
+
219
+ for (let i = 0; i < queue.length; ++i) {
220
+ const item = queue[i];
221
+ // @ts-ignore
222
+ const obj = item.obj[item.prop];
223
+
224
+ const keys = Object.keys(obj);
225
+ for (let j = 0; j < keys.length; ++j) {
226
+ const key = keys[j]!;
227
+ const val = obj[key];
228
+ if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
229
+ queue.push({ obj: obj, prop: key });
230
+ refs.push(val);
231
+ }
232
+ }
233
+ }
234
+
235
+ compact_queue(queue);
236
+
237
+ return value;
238
+ }
239
+
240
+ export function is_regexp(obj: any) {
241
+ return Object.prototype.toString.call(obj) === '[object RegExp]';
242
+ }
243
+
244
+ export function is_buffer(obj: any) {
245
+ if (!obj || typeof obj !== 'object') {
246
+ return false;
247
+ }
248
+
249
+ return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
250
+ }
251
+
252
+ export function combine(a: any, b: any) {
253
+ return [].concat(a, b);
254
+ }
255
+
256
+ export function maybe_map<T>(val: T[], fn: (v: T) => T) {
257
+ if (isArray(val)) {
258
+ const mapped = [];
259
+ for (let i = 0; i < val.length; i += 1) {
260
+ mapped.push(fn(val[i]!));
261
+ }
262
+ return mapped;
263
+ }
264
+ return fn(val);
265
+ }
@@ -40,7 +40,6 @@ type OverloadedParameters<T> =
40
40
  : T extends (...args: infer A) => unknown ? A
41
41
  : never;
42
42
 
43
- /* eslint-disable */
44
43
  /**
45
44
  * These imports attempt to get types from a parent package's dependencies.
46
45
  * Unresolved bare specifiers can trigger [automatic type acquisition][1] in some projects, which
@@ -63,19 +62,18 @@ type OverloadedParameters<T> =
63
62
  *
64
63
  * [1]: https://www.typescriptlang.org/tsconfig/#typeAcquisition
65
64
  */
66
- /** @ts-ignore For users with \@types/node */
65
+ /** @ts-ignore For users with \@types/node */ /* prettier-ignore */
67
66
  type UndiciTypesRequestInit = NotAny<import('undici-types').RequestInit> | NotAny<import('undici-types').RequestInit> | NotAny<import('../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit>;
68
- /** @ts-ignore For users with undici */
67
+ /** @ts-ignore For users with undici */ /* prettier-ignore */
69
68
  type UndiciRequestInit = NotAny<import('undici').RequestInit> | NotAny<import('undici').RequestInit> | NotAny<import('../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici/index.d.ts').RequestInit>;
70
- /** @ts-ignore For users with \@types/bun */
69
+ /** @ts-ignore For users with \@types/bun */ /* prettier-ignore */
71
70
  type BunRequestInit = globalThis.FetchRequestInit;
72
- /** @ts-ignore For users with node-fetch@2 */
71
+ /** @ts-ignore For users with node-fetch@2 */ /* prettier-ignore */
73
72
  type NodeFetch2RequestInit = NotAny<import('node-fetch').RequestInit> | NotAny<import('node-fetch').RequestInit> | NotAny<import('../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit>;
74
- /** @ts-ignore For users with node-fetch@3, doesn't need file extension because types are at ./@types/index.d.ts */
73
+ /** @ts-ignore For users with node-fetch@3, doesn't need file extension because types are at ./@types/index.d.ts */ /* prettier-ignore */
75
74
  type NodeFetch3RequestInit = NotAny<import('node-fetch.js').RequestInit> | NotAny<import('node-fetch.js').RequestInit> | NotAny<import('../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/node-fetch').RequestInit>;
76
- /** @ts-ignore For users who use Deno */
75
+ /** @ts-ignore For users who use Deno */ /* prettier-ignore */
77
76
  type FetchRequestInit = NonNullable<OverloadedParameters<typeof fetch>[1]>;
78
- /* eslint-enable */
79
77
 
80
78
  type RequestInits =
81
79
  | NotAny<UndiciTypesRequestInit>
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Chain an external {@link AbortSignal} into a local {@link AbortController}:
3
+ * the controller aborts whenever `external` aborts (synchronously if it is
4
+ * already aborted).
5
+ *
6
+ * Returns a cleanup function that detaches the listener. Callers MUST invoke it
7
+ * on their normal teardown path — `{ once: true }` only removes the listener if
8
+ * abort actually fires, so a long-lived `external` signal (e.g. a daemon-wide
9
+ * signal reused across many short-lived controllers) would otherwise leak one
10
+ * listener per controller.
11
+ */
12
+ export function linkAbort(external: AbortSignal | null | undefined, controller: AbortController): () => void {
13
+ if (!external) return () => {};
14
+ if (external.aborted) {
15
+ controller.abort();
16
+ return () => {};
17
+ }
18
+ const onAbort = () => controller.abort();
19
+ external.addEventListener('abort', onAbort);
20
+ return () => external.removeEventListener('abort', onAbort);
21
+ }
@@ -0,0 +1,66 @@
1
+ export type AsyncQueueResult<T> = { done: false; value: T } | { done: true; value: undefined };
2
+
3
+ /**
4
+ * Single-consumer async queue that bridges background producers to an
5
+ * `AsyncIterator`-style reader. Producers `push()` items; the consumer awaits
6
+ * `next()`. `close()` is idempotent and wakes any pending `next()` with
7
+ * `done: true`. `tryShift()` synchronously drains remaining items after
8
+ * iteration has been signalled to stop.
9
+ */
10
+ export class AsyncQueue<T> {
11
+ #items: T[] = [];
12
+ #waiters: Array<(r: AsyncQueueResult<T>) => void> = [];
13
+ #closed = false;
14
+
15
+ /** Enqueue an item, or hand it directly to a waiting reader. Returns `false` once closed. */
16
+ push(item: T): boolean {
17
+ if (this.#closed) return false;
18
+ const w = this.#waiters.shift();
19
+ if (w) w({ done: false, value: item });
20
+ else this.#items.push(item);
21
+ return true;
22
+ }
23
+
24
+ /** Mark the queue done. Idempotent; wakes every pending reader with `done: true`. */
25
+ close(): void {
26
+ if (this.#closed) return;
27
+ this.#closed = true;
28
+ while (this.#waiters.length > 0) {
29
+ const w = this.#waiters.shift()!;
30
+ w({ done: true, value: undefined });
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Resolve with the next item, or `done: true` once the queue is closed and
36
+ * drained. When `signal` is supplied, aborting it resolves a pending read
37
+ * with `done: true` (cancellation is pushed down here rather than handled by
38
+ * an outer `Promise.race`).
39
+ */
40
+ next(signal?: AbortSignal): Promise<AsyncQueueResult<T>> {
41
+ if (this.#items.length > 0) {
42
+ return Promise.resolve({ done: false, value: this.#items.shift()! });
43
+ }
44
+ if (this.#closed || signal?.aborted) {
45
+ return Promise.resolve({ done: true, value: undefined });
46
+ }
47
+ return new Promise<AsyncQueueResult<T>>((resolve) => {
48
+ const waiter = (r: AsyncQueueResult<T>) => {
49
+ signal?.removeEventListener('abort', onAbort);
50
+ resolve(r);
51
+ };
52
+ const onAbort = () => {
53
+ const idx = this.#waiters.indexOf(waiter);
54
+ if (idx >= 0) this.#waiters.splice(idx, 1);
55
+ resolve({ done: true, value: undefined });
56
+ };
57
+ this.#waiters.push(waiter);
58
+ signal?.addEventListener('abort', onAbort, { once: true });
59
+ });
60
+ }
61
+
62
+ /** Synchronously remove and return the next buffered item, or `undefined` if empty. */
63
+ tryShift(): T | undefined {
64
+ return this.#items.shift();
65
+ }
66
+ }
@@ -0,0 +1,41 @@
1
+ import { APIError } from '../../core/error';
2
+
3
+ /** True when `e` is an {@link APIError} whose HTTP status equals `code`. */
4
+ export function isStatus(e: unknown, code: number): boolean {
5
+ return e instanceof APIError && e.status === code;
6
+ }
7
+
8
+ /** True when `e` is an {@link APIError} with a 4xx status. */
9
+ export function is4xx(e: unknown): boolean {
10
+ return e instanceof APIError && typeof e.status === 'number' && e.status >= 400 && e.status < 500;
11
+ }
12
+
13
+ /**
14
+ * True for a 4xx that the core client's retry policy would *not* retry, i.e. a
15
+ * permanent client error. 408 (request timeout), 409 (lock timeout) and 429
16
+ * (rate limit) are retryable for the base client (`Anthropic.shouldRetry`), so
17
+ * they are not treated as fatal here — keeping helper retry behaviour aligned
18
+ * with the rest of the SDK.
19
+ */
20
+ export function isFatal4xx(e: unknown): boolean {
21
+ return is4xx(e) && !isStatus(e, 408) && !isStatus(e, 409) && !isStatus(e, 429);
22
+ }
23
+
24
+ /** Exponential backoff: `baseMs * 2 ** attempt`, clamped to `capMs`. */
25
+ export function backoff(attempt: number, baseMs: number, capMs: number): number {
26
+ return Math.min(baseMs * 2 ** attempt, capMs);
27
+ }
28
+
29
+ /** Uniform random delay in the half-open interval `[lowMs, highMs)`. */
30
+ export function jitter(lowMs: number, highMs: number): number {
31
+ return lowMs + Math.random() * (highMs - lowMs);
32
+ }
33
+
34
+ /**
35
+ * Trim up to 25% off `ms` at random so a fleet of clients backing off after a
36
+ * shared outage does not retry in lockstep — mirrors the jitter the core client
37
+ * applies to its own retry timeout.
38
+ */
39
+ export function applyJitter(ms: number): number {
40
+ return ms * (1 - Math.random() * 0.25);
41
+ }
@@ -9,10 +9,10 @@
9
9
  */
10
10
  export const readEnv = (env: string): string | undefined => {
11
11
  if (typeof (globalThis as any).process !== 'undefined') {
12
- return (globalThis as any).process.env?.[env]?.trim() ?? undefined;
12
+ return (globalThis as any).process.env?.[env]?.trim() || undefined;
13
13
  }
14
14
  if (typeof (globalThis as any).Deno !== 'undefined') {
15
- return (globalThis as any).Deno.env?.get?.(env)?.trim();
15
+ return (globalThis as any).Deno.env?.get?.(env)?.trim() || undefined;
16
16
  }
17
17
  return undefined;
18
18
  };
@@ -106,8 +106,9 @@ export const formatRequestDetails = (details: {
106
106
  ([name, value]) => [
107
107
  name,
108
108
  (
109
- name.toLowerCase() === 'x-api-key' ||
110
109
  name.toLowerCase() === 'authorization' ||
110
+ name.toLowerCase() === 'api-key' ||
111
+ name.toLowerCase() === 'x-api-key' ||
111
112
  name.toLowerCase() === 'cookie' ||
112
113
  name.toLowerCase() === 'set-cookie'
113
114
  ) ?
@@ -0,0 +1,18 @@
1
+ /**
2
+ * A deferred: a `Promise` together with its `resolve` / `reject` functions.
3
+ * This is `Promise.withResolvers()`, which is not available in all supported
4
+ * runtimes.
5
+ */
6
+ export function promiseWithResolvers<T>(): {
7
+ promise: Promise<T>;
8
+ resolve: (value: T | PromiseLike<T>) => void;
9
+ reject: (reason?: unknown) => void;
10
+ } {
11
+ let resolve!: (value: T | PromiseLike<T>) => void;
12
+ let reject!: (reason?: unknown) => void;
13
+ const promise = new Promise<T>((res, rej) => {
14
+ resolve = res;
15
+ reject = rej;
16
+ });
17
+ return { promise, resolve, reject };
18
+ }
@@ -0,0 +1,7 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import * as qs from '../qs/stringify';
4
+
5
+ export function stringifyQuery(query: object | Record<string, unknown>) {
6
+ return qs.stringify(query, { arrayFormat: 'brackets' });
7
+ }
@@ -1,3 +1,26 @@
1
- // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
1
+ /**
2
+ * Resolve after `ms`, or immediately when `signal` aborts.
3
+ *
4
+ * When a `signal` is passed the abort listener is always removed so repeated
5
+ * calls do not accumulate listeners on a long-lived signal. Resolves (rather
6
+ * than rejects) on abort — callers treat abort as "wake up early," not as a
7
+ * failure; callers that want to unwind should check the signal themselves.
8
+ */
9
+ export const sleep = (ms: number, signal?: AbortSignal): Promise<void> =>
10
+ new Promise<void>((resolve) => {
11
+ if (signal?.aborted) return resolve();
2
12
 
3
- export const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
13
+ const onAbort = () => {
14
+ clearTimeout(timer);
15
+ resolve();
16
+ };
17
+
18
+ const timer = setTimeout(() => {
19
+ signal?.removeEventListener('abort', onAbort);
20
+ resolve();
21
+ }, ms);
22
+
23
+ // `{ once: true }` auto-removes the listener if abort fires first,
24
+ // so we only need an explicit remove on the timer-wins path above.
25
+ signal?.addEventListener('abort', onAbort, { once: true });
26
+ });
@@ -0,0 +1,4 @@
1
+ /** Current time as unix epoch seconds. */
2
+ export function nowAsSeconds(): number {
3
+ return Math.floor(Date.now() / 1000);
4
+ }
@@ -6,3 +6,4 @@ export * from './utils/env';
6
6
  export * from './utils/log';
7
7
  export * from './utils/uuid';
8
8
  export * from './utils/sleep';
9
+ export * from './utils/query';