@anthropic-ai/sdk 0.6.2 → 0.6.8

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 (265) hide show
  1. package/CHANGELOG.md +117 -0
  2. package/README.md +16 -6
  3. package/_shims/MultipartBody.d.ts +9 -0
  4. package/_shims/MultipartBody.d.ts.map +1 -0
  5. package/_shims/MultipartBody.js +16 -0
  6. package/_shims/MultipartBody.js.map +1 -0
  7. package/_shims/MultipartBody.mjs +12 -0
  8. package/_shims/MultipartBody.mjs.map +1 -0
  9. package/_shims/README.md +46 -0
  10. package/{src/_shims/ReadableStream.node.ts → _shims/auto/runtime-bun.d.ts} +2 -3
  11. package/_shims/auto/runtime-bun.d.ts.map +1 -0
  12. package/_shims/auto/runtime-bun.js +21 -0
  13. package/_shims/auto/runtime-bun.js.map +1 -0
  14. package/_shims/auto/runtime-bun.mjs +2 -0
  15. package/_shims/auto/runtime-bun.mjs.map +1 -0
  16. package/_shims/auto/runtime-node.d.ts +5 -0
  17. package/_shims/auto/runtime-node.d.ts.map +1 -0
  18. package/_shims/auto/runtime-node.js +21 -0
  19. package/_shims/auto/runtime-node.js.map +1 -0
  20. package/_shims/auto/runtime-node.mjs +2 -0
  21. package/_shims/auto/runtime-node.mjs.map +1 -0
  22. package/_shims/auto/runtime.d.ts +5 -0
  23. package/_shims/auto/runtime.d.ts.map +1 -0
  24. package/_shims/auto/runtime.js +21 -0
  25. package/_shims/auto/runtime.js.map +1 -0
  26. package/_shims/auto/runtime.mjs +2 -0
  27. package/_shims/auto/runtime.mjs.map +1 -0
  28. package/_shims/auto/types-node.d.ts +5 -0
  29. package/_shims/auto/types-node.d.ts.map +1 -0
  30. package/_shims/auto/types-node.js +21 -0
  31. package/_shims/auto/types-node.js.map +1 -0
  32. package/_shims/auto/types-node.mjs +2 -0
  33. package/_shims/auto/types-node.mjs.map +1 -0
  34. package/_shims/auto/types.d.ts +101 -0
  35. package/{src/_shims/ReadableStream.js → _shims/auto/types.js} +0 -2
  36. package/_shims/{ReadableStream.js → auto/types.mjs} +0 -2
  37. package/_shims/bun-runtime.d.ts +6 -0
  38. package/_shims/bun-runtime.d.ts.map +1 -0
  39. package/_shims/bun-runtime.js +14 -0
  40. package/_shims/bun-runtime.js.map +1 -0
  41. package/_shims/bun-runtime.mjs +10 -0
  42. package/_shims/bun-runtime.mjs.map +1 -0
  43. package/_shims/index.d.ts +81 -0
  44. package/_shims/index.js +13 -0
  45. package/_shims/index.mjs +7 -0
  46. package/_shims/manual-types.d.ts +12 -0
  47. package/_shims/manual-types.js +3 -0
  48. package/_shims/manual-types.mjs +3 -0
  49. package/_shims/node-runtime.d.ts +3 -0
  50. package/_shims/node-runtime.d.ts.map +1 -0
  51. package/_shims/node-runtime.js +90 -0
  52. package/_shims/node-runtime.js.map +1 -0
  53. package/_shims/node-runtime.mjs +56 -0
  54. package/_shims/node-runtime.mjs.map +1 -0
  55. package/_shims/node-types.d.ts +42 -0
  56. package/_shims/node-types.js +3 -0
  57. package/_shims/node-types.mjs +3 -0
  58. package/_shims/registry.d.ts +37 -0
  59. package/_shims/registry.d.ts.map +1 -0
  60. package/_shims/registry.js +41 -0
  61. package/_shims/registry.js.map +1 -0
  62. package/_shims/registry.mjs +37 -0
  63. package/_shims/registry.mjs.map +1 -0
  64. package/_shims/web-runtime.d.ts +5 -0
  65. package/_shims/web-runtime.d.ts.map +1 -0
  66. package/_shims/web-runtime.js +78 -0
  67. package/_shims/web-runtime.js.map +1 -0
  68. package/_shims/web-runtime.mjs +71 -0
  69. package/_shims/web-runtime.mjs.map +1 -0
  70. package/_shims/web-types.d.ts +83 -0
  71. package/_shims/web-types.js +3 -0
  72. package/_shims/web-types.mjs +3 -0
  73. package/core.d.ts +172 -224
  74. package/core.d.ts.map +1 -1
  75. package/core.js +701 -736
  76. package/core.js.map +1 -1
  77. package/core.mjs +694 -688
  78. package/core.mjs.map +1 -1
  79. package/error.d.ts +32 -32
  80. package/error.d.ts.map +1 -1
  81. package/error.js +105 -106
  82. package/error.js.map +1 -1
  83. package/error.mjs +101 -91
  84. package/error.mjs.map +1 -1
  85. package/index.d.mts +110 -117
  86. package/index.d.ts +110 -117
  87. package/index.d.ts.map +1 -1
  88. package/index.js +107 -159
  89. package/index.js.map +1 -1
  90. package/index.mjs +84 -104
  91. package/index.mjs.map +1 -1
  92. package/package.json +36 -21
  93. package/resource.d.ts +10 -10
  94. package/resource.js +12 -12
  95. package/resource.mjs +10 -10
  96. package/resources/completions.d.ts +136 -142
  97. package/resources/completions.d.ts.map +1 -1
  98. package/resources/completions.js +9 -14
  99. package/resources/completions.js.map +1 -1
  100. package/resources/completions.mjs +6 -11
  101. package/resources/completions.mjs.map +1 -1
  102. package/resources/index.d.ts +2 -9
  103. package/resources/index.d.ts.map +1 -1
  104. package/resources/index.js +5 -10
  105. package/resources/index.js.map +1 -1
  106. package/resources/index.mjs +2 -2
  107. package/resources/index.mjs.map +1 -1
  108. package/resources/top-level.d.ts +1 -1
  109. package/resources/top-level.js +3 -3
  110. package/resources/top-level.mjs +1 -1
  111. package/shims/node.d.ts +29 -0
  112. package/shims/node.d.ts.map +1 -0
  113. package/shims/node.js +31 -0
  114. package/shims/node.js.map +1 -0
  115. package/shims/node.mjs +5 -0
  116. package/shims/node.mjs.map +1 -0
  117. package/shims/web.d.ts +26 -0
  118. package/shims/web.d.ts.map +1 -0
  119. package/shims/web.js +31 -0
  120. package/shims/web.js.map +1 -0
  121. package/shims/web.mjs +5 -0
  122. package/shims/web.mjs.map +1 -0
  123. package/src/_shims/MultipartBody.ts +9 -0
  124. package/src/_shims/README.md +46 -0
  125. package/src/_shims/auto/runtime-bun.ts +4 -0
  126. package/src/_shims/auto/runtime-node.ts +4 -0
  127. package/src/_shims/auto/runtime.ts +4 -0
  128. package/src/_shims/auto/types-node.ts +4 -0
  129. package/src/_shims/auto/types.d.ts +101 -0
  130. package/src/_shims/auto/types.js +3 -0
  131. package/src/_shims/auto/types.mjs +3 -0
  132. package/src/_shims/bun-runtime.ts +14 -0
  133. package/src/_shims/index.d.ts +81 -0
  134. package/src/_shims/index.js +13 -0
  135. package/src/_shims/index.mjs +7 -0
  136. package/src/_shims/manual-types.d.ts +12 -0
  137. package/src/_shims/manual-types.js +3 -0
  138. package/src/_shims/manual-types.mjs +3 -0
  139. package/src/_shims/node-runtime.ts +83 -0
  140. package/src/_shims/node-types.d.ts +42 -0
  141. package/src/_shims/node-types.js +3 -0
  142. package/src/_shims/node-types.mjs +3 -0
  143. package/src/_shims/registry.ts +67 -0
  144. package/src/_shims/web-runtime.ts +103 -0
  145. package/src/_shims/web-types.d.ts +83 -0
  146. package/src/_shims/web-types.js +3 -0
  147. package/src/_shims/web-types.mjs +3 -0
  148. package/src/core.ts +107 -56
  149. package/src/error.ts +20 -8
  150. package/src/index.ts +24 -17
  151. package/src/resources/completions.ts +11 -11
  152. package/src/resources/index.ts +0 -1
  153. package/src/shims/node.ts +50 -0
  154. package/src/shims/web.ts +50 -0
  155. package/src/streaming.ts +164 -59
  156. package/src/tsconfig.json +11 -0
  157. package/src/uploads.ts +11 -15
  158. package/src/version.ts +1 -1
  159. package/streaming.d.ts +11 -8
  160. package/streaming.d.ts.map +1 -1
  161. package/streaming.js +300 -192
  162. package/streaming.js.map +1 -1
  163. package/streaming.mjs +296 -188
  164. package/streaming.mjs.map +1 -1
  165. package/uploads.d.ts +23 -39
  166. package/uploads.d.ts.map +1 -1
  167. package/uploads.js +111 -157
  168. package/uploads.js.map +1 -1
  169. package/uploads.mjs +107 -133
  170. package/uploads.mjs.map +1 -1
  171. package/version.d.ts +2 -2
  172. package/version.js +4 -4
  173. package/version.mjs +2 -2
  174. package/_shims/ReadableStream.d.ts +0 -38
  175. package/_shims/ReadableStream.mjs +0 -7
  176. package/_shims/ReadableStream.node.d.ts +0 -6
  177. package/_shims/ReadableStream.node.d.ts.map +0 -1
  178. package/_shims/ReadableStream.node.js +0 -14
  179. package/_shims/ReadableStream.node.js.map +0 -1
  180. package/_shims/ReadableStream.node.mjs +0 -3
  181. package/_shims/ReadableStream.node.mjs.map +0 -1
  182. package/_shims/agent.d.ts +0 -9
  183. package/_shims/agent.d.ts.map +0 -1
  184. package/_shims/agent.js +0 -14
  185. package/_shims/agent.js.map +0 -1
  186. package/_shims/agent.mjs +0 -10
  187. package/_shims/agent.mjs.map +0 -1
  188. package/_shims/agent.node.d.ts +0 -7
  189. package/_shims/agent.node.d.ts.map +0 -1
  190. package/_shims/agent.node.js +0 -28
  191. package/_shims/agent.node.js.map +0 -1
  192. package/_shims/agent.node.mjs +0 -16
  193. package/_shims/agent.node.mjs.map +0 -1
  194. package/_shims/fetch.d.ts +0 -60
  195. package/_shims/fetch.js +0 -13
  196. package/_shims/fetch.mjs +0 -15
  197. package/_shims/fetch.node.d.ts +0 -64
  198. package/_shims/fetch.node.js +0 -12
  199. package/_shims/fetch.node.mjs +0 -14
  200. package/_shims/fileFromPath.d.ts +0 -22
  201. package/_shims/fileFromPath.d.ts.map +0 -1
  202. package/_shims/fileFromPath.js +0 -16
  203. package/_shims/fileFromPath.js.map +0 -1
  204. package/_shims/fileFromPath.mjs +0 -12
  205. package/_shims/fileFromPath.mjs.map +0 -1
  206. package/_shims/fileFromPath.node.d.ts +0 -17
  207. package/_shims/fileFromPath.node.d.ts.map +0 -1
  208. package/_shims/fileFromPath.node.js +0 -17
  209. package/_shims/fileFromPath.node.js.map +0 -1
  210. package/_shims/fileFromPath.node.mjs +0 -13
  211. package/_shims/fileFromPath.node.mjs.map +0 -1
  212. package/_shims/formdata.d.ts +0 -43
  213. package/_shims/formdata.js +0 -17
  214. package/_shims/formdata.mjs +0 -20
  215. package/_shims/formdata.node.d.ts +0 -45
  216. package/_shims/formdata.node.js +0 -11
  217. package/_shims/formdata.node.mjs +0 -9
  218. package/_shims/getMultipartRequestOptions.d.ts +0 -10
  219. package/_shims/getMultipartRequestOptions.d.ts.map +0 -1
  220. package/_shims/getMultipartRequestOptions.js +0 -12
  221. package/_shims/getMultipartRequestOptions.js.map +0 -1
  222. package/_shims/getMultipartRequestOptions.mjs +0 -8
  223. package/_shims/getMultipartRequestOptions.mjs.map +0 -1
  224. package/_shims/getMultipartRequestOptions.node.d.ts +0 -10
  225. package/_shims/getMultipartRequestOptions.node.d.ts.map +0 -1
  226. package/_shims/getMultipartRequestOptions.node.js +0 -22
  227. package/_shims/getMultipartRequestOptions.node.js.map +0 -1
  228. package/_shims/getMultipartRequestOptions.node.mjs +0 -18
  229. package/_shims/getMultipartRequestOptions.node.mjs.map +0 -1
  230. package/_shims/node-readable.d.ts +0 -23
  231. package/_shims/node-readable.d.ts.map +0 -1
  232. package/_shims/node-readable.js +0 -11
  233. package/_shims/node-readable.js.map +0 -1
  234. package/_shims/node-readable.mjs +0 -7
  235. package/_shims/node-readable.mjs.map +0 -1
  236. package/_shims/node-readable.node.d.ts +0 -8
  237. package/_shims/node-readable.node.d.ts.map +0 -1
  238. package/_shims/node-readable.node.js +0 -9
  239. package/_shims/node-readable.node.js.map +0 -1
  240. package/_shims/node-readable.node.mjs +0 -5
  241. package/_shims/node-readable.node.mjs.map +0 -1
  242. package/src/_shims/ReadableStream.d.ts +0 -38
  243. package/src/_shims/ReadableStream.mjs +0 -7
  244. package/src/_shims/agent.node.ts +0 -22
  245. package/src/_shims/agent.ts +0 -12
  246. package/src/_shims/fetch.d.ts +0 -60
  247. package/src/_shims/fetch.deno.ts +0 -23
  248. package/src/_shims/fetch.js +0 -13
  249. package/src/_shims/fetch.mjs +0 -15
  250. package/src/_shims/fetch.node.d.ts +0 -64
  251. package/src/_shims/fetch.node.js +0 -12
  252. package/src/_shims/fetch.node.mjs +0 -14
  253. package/src/_shims/fileFromPath.node.ts +0 -29
  254. package/src/_shims/fileFromPath.ts +0 -29
  255. package/src/_shims/formdata.d.ts +0 -43
  256. package/src/_shims/formdata.deno.ts +0 -16
  257. package/src/_shims/formdata.js +0 -17
  258. package/src/_shims/formdata.mjs +0 -20
  259. package/src/_shims/formdata.node.d.ts +0 -45
  260. package/src/_shims/formdata.node.js +0 -11
  261. package/src/_shims/formdata.node.mjs +0 -9
  262. package/src/_shims/getMultipartRequestOptions.node.ts +0 -25
  263. package/src/_shims/getMultipartRequestOptions.ts +0 -14
  264. package/src/_shims/node-readable.node.ts +0 -10
  265. package/src/_shims/node-readable.ts +0 -30
@@ -0,0 +1,50 @@
1
+ // @ts-ignore
2
+ import * as types from '../_shims/node-types';
3
+ import { setShims } from '../_shims/registry';
4
+ import { getRuntime } from '../_shims/node-runtime';
5
+ setShims(getRuntime());
6
+
7
+ declare module '../_shims/manual-types' {
8
+ export namespace manual {
9
+ // @ts-ignore
10
+ export type Agent = types.Agent;
11
+ // @ts-ignore
12
+ export import fetch = types.fetch;
13
+ // @ts-ignore
14
+ export type Request = types.Request;
15
+ // @ts-ignore
16
+ export type RequestInfo = types.RequestInfo;
17
+ // @ts-ignore
18
+ export type RequestInit = types.RequestInit;
19
+ // @ts-ignore
20
+ export type Response = types.Response;
21
+ // @ts-ignore
22
+ export type ResponseInit = types.ResponseInit;
23
+ // @ts-ignore
24
+ export type ResponseType = types.ResponseType;
25
+ // @ts-ignore
26
+ export type BodyInit = types.BodyInit;
27
+ // @ts-ignore
28
+ export type Headers = types.Headers;
29
+ // @ts-ignore
30
+ export type HeadersInit = types.HeadersInit;
31
+ // @ts-ignore
32
+ export type BlobPropertyBag = types.BlobPropertyBag;
33
+ // @ts-ignore
34
+ export type FilePropertyBag = types.FilePropertyBag;
35
+ // @ts-ignore
36
+ export type FileFromPathOptions = types.FileFromPathOptions;
37
+ // @ts-ignore
38
+ export import FormData = types.FormData;
39
+ // @ts-ignore
40
+ export import File = types.File;
41
+ // @ts-ignore
42
+ export import Blob = types.Blob;
43
+ // @ts-ignore
44
+ export type Readable = types.Readable;
45
+ // @ts-ignore
46
+ export type FsReadStream = types.FsReadStream;
47
+ // @ts-ignore
48
+ export import ReadableStream = types.ReadableStream;
49
+ }
50
+ }
@@ -0,0 +1,50 @@
1
+ // @ts-ignore
2
+ import * as types from '../_shims/web-types';
3
+ import { setShims } from '../_shims/registry';
4
+ import { getRuntime } from '../_shims/web-runtime';
5
+ setShims(getRuntime({ manuallyImported: true }));
6
+
7
+ declare module '../_shims/manual-types' {
8
+ export namespace manual {
9
+ // @ts-ignore
10
+ export type Agent = types.Agent;
11
+ // @ts-ignore
12
+ export import fetch = types.fetch;
13
+ // @ts-ignore
14
+ export type Request = types.Request;
15
+ // @ts-ignore
16
+ export type RequestInfo = types.RequestInfo;
17
+ // @ts-ignore
18
+ export type RequestInit = types.RequestInit;
19
+ // @ts-ignore
20
+ export type Response = types.Response;
21
+ // @ts-ignore
22
+ export type ResponseInit = types.ResponseInit;
23
+ // @ts-ignore
24
+ export type ResponseType = types.ResponseType;
25
+ // @ts-ignore
26
+ export type BodyInit = types.BodyInit;
27
+ // @ts-ignore
28
+ export type Headers = types.Headers;
29
+ // @ts-ignore
30
+ export type HeadersInit = types.HeadersInit;
31
+ // @ts-ignore
32
+ export type BlobPropertyBag = types.BlobPropertyBag;
33
+ // @ts-ignore
34
+ export type FilePropertyBag = types.FilePropertyBag;
35
+ // @ts-ignore
36
+ export type FileFromPathOptions = types.FileFromPathOptions;
37
+ // @ts-ignore
38
+ export import FormData = types.FormData;
39
+ // @ts-ignore
40
+ export import File = types.File;
41
+ // @ts-ignore
42
+ export import Blob = types.Blob;
43
+ // @ts-ignore
44
+ export type Readable = types.Readable;
45
+ // @ts-ignore
46
+ export type FsReadStream = types.FsReadStream;
47
+ // @ts-ignore
48
+ export import ReadableStream = types.ReadableStream;
49
+ }
50
+ }
package/src/streaming.ts CHANGED
@@ -1,7 +1,8 @@
1
- import type { Response } from './_shims/fetch.js';
1
+ import { ReadableStream, type Response } from './_shims/index';
2
+ import { AnthropicError } from './error';
2
3
 
3
- import { safeJSON, createResponseHeaders } from './core';
4
- import { APIError } from './error';
4
+ import { safeJSON, createResponseHeaders } from "./pic-ai/sdk/core";
5
+ import { APIError } from "./pic-ai/sdk/error";
5
6
 
6
7
  type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
7
8
 
@@ -14,76 +15,180 @@ type ServerSentEvent = {
14
15
  export class Stream<Item> implements AsyncIterable<Item> {
15
16
  controller: AbortController;
16
17
 
17
- private response: Response;
18
- private decoder: SSEDecoder;
19
-
20
- constructor(response: Response, controller: AbortController) {
21
- this.response = response;
18
+ constructor(private iterator: () => AsyncIterator<Item>, controller: AbortController) {
22
19
  this.controller = controller;
23
- this.decoder = new SSEDecoder();
24
20
  }
25
21
 
26
- private async *iterMessages(): AsyncGenerator<ServerSentEvent, void, unknown> {
27
- if (!this.response.body) {
28
- this.controller.abort();
29
- throw new Error(`Attempted to iterate over a response with no body`);
30
- }
31
- const lineDecoder = new LineDecoder();
22
+ static fromSSEResponse<Item>(response: Response, controller: AbortController) {
23
+ let consumed = false;
24
+ const decoder = new SSEDecoder();
25
+
26
+ async function* iterMessages(): AsyncGenerator<ServerSentEvent, void, unknown> {
27
+ if (!response.body) {
28
+ controller.abort();
29
+ throw new AnthropicError(`Attempted to iterate over a response with no body`);
30
+ }
31
+
32
+ const lineDecoder = new LineDecoder();
32
33
 
33
- const iter = readableStreamAsyncIterable<Bytes>(this.response.body);
34
- for await (const chunk of iter) {
35
- for (const line of lineDecoder.decode(chunk)) {
36
- const sse = this.decoder.decode(line);
34
+ const iter = readableStreamAsyncIterable<Bytes>(response.body);
35
+ for await (const chunk of iter) {
36
+ for (const line of lineDecoder.decode(chunk)) {
37
+ const sse = decoder.decode(line);
38
+ if (sse) yield sse;
39
+ }
40
+ }
41
+
42
+ for (const line of lineDecoder.flush()) {
43
+ const sse = decoder.decode(line);
37
44
  if (sse) yield sse;
38
45
  }
39
46
  }
40
47
 
41
- for (const line of lineDecoder.flush()) {
42
- const sse = this.decoder.decode(line);
43
- if (sse) yield sse;
44
- }
45
- }
48
+ async function* iterator(): AsyncIterator<Item, any, undefined> {
49
+ if (consumed) {
50
+ throw new Error('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
51
+ }
52
+ consumed = true;
53
+ let done = false;
54
+ try {
55
+ for await (const sse of iterMessages()) {
56
+ if (sse.event === 'completion') {
57
+ try {
58
+ yield JSON.parse(sse.data);
59
+ } catch (e) {
60
+ console.error(`Could not parse message into JSON:`, sse.data);
61
+ console.error(`From chunk:`, sse.raw);
62
+ throw e;
63
+ }
64
+ }
65
+
66
+ if (sse.event === 'ping') {
67
+ continue;
68
+ }
69
+
70
+ if (sse.event === 'error') {
71
+ const errText = sse.data;
72
+ const errJSON = safeJSON(errText);
73
+ const errMessage = errJSON ? undefined : errText;
46
74
 
47
- async *[Symbol.asyncIterator](): AsyncIterator<Item, any, undefined> {
48
- let done = false;
49
- try {
50
- for await (const sse of this.iterMessages()) {
51
- if (sse.event === 'completion') {
52
- try {
53
- yield JSON.parse(sse.data);
54
- } catch (e) {
55
- console.error(`Could not parse message into JSON:`, sse.data);
56
- console.error(`From chunk:`, sse.raw);
57
- throw e;
75
+ throw APIError.generate(undefined, errJSON, errMessage, createResponseHeaders(response.headers));
58
76
  }
59
77
  }
78
+ done = true;
79
+ } catch (e) {
80
+ // If the user calls `stream.controller.abort()`, we should exit without throwing.
81
+ if (e instanceof Error && e.name === 'AbortError') return;
82
+ throw e;
83
+ } finally {
84
+ // If the user `break`s, abort the ongoing request.
85
+ if (!done) controller.abort();
86
+ }
87
+ }
88
+
89
+ return new Stream(iterator, controller);
90
+ }
60
91
 
61
- if (sse.event === 'ping') {
62
- continue;
92
+ // Generates a Stream from a newline-separated ReadableStream where each item
93
+ // is a JSON Value.
94
+ static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController) {
95
+ let consumed = false;
96
+
97
+ async function* iterLines(): AsyncGenerator<string, void, unknown> {
98
+ const lineDecoder = new LineDecoder();
99
+
100
+ const iter = readableStreamAsyncIterable<Bytes>(readableStream);
101
+ for await (const chunk of iter) {
102
+ for (const line of lineDecoder.decode(chunk)) {
103
+ yield line;
63
104
  }
105
+ }
106
+
107
+ for (const line of lineDecoder.flush()) {
108
+ yield line;
109
+ }
110
+ }
64
111
 
65
- if (sse.event === 'error') {
66
- const errText = sse.data;
67
- const errJSON = safeJSON(errText);
68
- const errMessage = errJSON ? undefined : errText;
69
-
70
- throw APIError.generate(
71
- undefined,
72
- errJSON,
73
- errMessage,
74
- createResponseHeaders(this.response.headers),
75
- );
112
+ async function* iterator(): AsyncIterator<Item, any, undefined> {
113
+ if (consumed) {
114
+ throw new Error('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
115
+ }
116
+ consumed = true;
117
+ let done = false;
118
+ try {
119
+ for await (const line of iterLines()) {
120
+ if (done) continue;
121
+ if (line) yield JSON.parse(line);
76
122
  }
123
+ done = true;
124
+ } catch (e) {
125
+ // If the user calls `stream.controller.abort()`, we should exit without throwing.
126
+ if (e instanceof Error && e.name === 'AbortError') return;
127
+ throw e;
128
+ } finally {
129
+ // If the user `break`s, abort the ongoing request.
130
+ if (!done) controller.abort();
77
131
  }
78
- done = true;
79
- } catch (e) {
80
- // If the user calls `stream.controller.abort()`, we should exit without throwing.
81
- if (e instanceof Error && e.name === 'AbortError') return;
82
- throw e;
83
- } finally {
84
- // If the user `break`s, abort the ongoing request.
85
- if (!done) this.controller.abort();
86
132
  }
133
+
134
+ return new Stream(iterator, controller);
135
+ }
136
+
137
+ [Symbol.asyncIterator](): AsyncIterator<Item> {
138
+ return this.iterator();
139
+ }
140
+
141
+ tee(): [Stream<Item>, Stream<Item>] {
142
+ const left: Array<Promise<IteratorResult<Item>>> = [];
143
+ const right: Array<Promise<IteratorResult<Item>>> = [];
144
+ const iterator = this.iterator();
145
+
146
+ const teeIterator = (queue: Array<Promise<IteratorResult<Item>>>): AsyncIterator<Item> => {
147
+ return {
148
+ next: () => {
149
+ if (queue.length === 0) {
150
+ const result = iterator.next();
151
+ left.push(result);
152
+ right.push(result);
153
+ }
154
+ return queue.shift()!;
155
+ },
156
+ };
157
+ };
158
+
159
+ return [
160
+ new Stream(() => teeIterator(left), this.controller),
161
+ new Stream(() => teeIterator(right), this.controller),
162
+ ];
163
+ }
164
+
165
+ // Converts this stream to a newline-separated ReadableStream of JSON Stringified values in the stream
166
+ // which can be turned back into a Stream with Stream.fromReadableStream.
167
+ toReadableStream(): ReadableStream {
168
+ const self = this;
169
+ let iter: AsyncIterator<Item>;
170
+ const encoder = new TextEncoder();
171
+
172
+ return new ReadableStream({
173
+ async start() {
174
+ iter = self[Symbol.asyncIterator]();
175
+ },
176
+ async pull(ctrl) {
177
+ try {
178
+ const { value, done } = await iter.next();
179
+ if (done) return ctrl.close();
180
+
181
+ const bytes = encoder.encode(JSON.stringify(value) + '\n');
182
+
183
+ ctrl.enqueue(bytes);
184
+ } catch (err) {
185
+ ctrl.error(err);
186
+ }
187
+ },
188
+ async cancel() {
189
+ await iter.return?.();
190
+ },
191
+ });
87
192
  }
88
193
  }
89
194
 
@@ -211,7 +316,7 @@ class LineDecoder {
211
316
  return Buffer.from(bytes).toString();
212
317
  }
213
318
 
214
- throw new Error(
319
+ throw new AnthropicError(
215
320
  `Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`,
216
321
  );
217
322
  }
@@ -223,14 +328,14 @@ class LineDecoder {
223
328
  return this.textDecoder.decode(bytes);
224
329
  }
225
330
 
226
- throw new Error(
331
+ throw new AnthropicError(
227
332
  `Unexpected: received non-Uint8Array/ArrayBuffer (${
228
333
  (bytes as any).constructor.name
229
334
  }) in a web platform. Please report this error.`,
230
335
  );
231
336
  }
232
337
 
233
- throw new Error(
338
+ throw new AnthropicError(
234
339
  `Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.`,
235
340
  );
236
341
  }
@@ -0,0 +1,11 @@
1
+ {
2
+ // this config is included in the published src directory to prevent TS errors
3
+ // from appearing when users go to source, and VSCode opens the source .ts file
4
+ // via declaration maps
5
+ "include": ["index.ts"],
6
+ "compilerOptions": {
7
+ "target": "es2015",
8
+ "lib": ["DOM"],
9
+ "moduleResolution": "node"
10
+ }
11
+ }
package/src/uploads.ts CHANGED
@@ -1,12 +1,15 @@
1
1
  import { type RequestOptions } from './core';
2
- import { type Readable } from './_shims/node-readable';
3
- import { type BodyInit } from './_shims/fetch.js';
4
- import { FormData, File, type Blob, type FilePropertyBag } from './_shims/formdata.js';
5
- import { getMultipartRequestOptions } from './_shims/getMultipartRequestOptions';
6
- import { fileFromPath } from './_shims/fileFromPath';
7
- import { type FsReadStream, isFsReadStream } from './_shims/node-readable';
8
-
9
- export { fileFromPath };
2
+ import {
3
+ FormData,
4
+ File,
5
+ type Blob,
6
+ type FilePropertyBag,
7
+ getMultipartRequestOptions,
8
+ type FsReadStream,
9
+ isFsReadStream,
10
+ } from './_shims/index';
11
+ import { MultipartBody } from './_shims/MultipartBody';
12
+ export { fileFromPath } from './_shims/index';
10
13
 
11
14
  type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;
12
15
  export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | Uint8Array | DataView;
@@ -175,13 +178,6 @@ const getStringFromMaybeBuffer = (x: string | Buffer | unknown): string | undefi
175
178
  const isAsyncIterableIterator = (value: any): value is AsyncIterableIterator<unknown> =>
176
179
  value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
177
180
 
178
- export class MultipartBody {
179
- constructor(public body: Readable | BodyInit) {}
180
- get [Symbol.toStringTag](): string {
181
- return 'MultipartBody';
182
- }
183
- }
184
-
185
181
  export const isMultipartBody = (body: any): body is MultipartBody =>
186
182
  body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
187
183
 
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.6.2'; // x-release-please-version
1
+ export const VERSION = '0.6.8'; // x-release-please-version
package/streaming.d.ts CHANGED
@@ -1,10 +1,13 @@
1
- import type { Response } from '@anthropic-ai/sdk/_shims/fetch';
1
+
2
+ import { ReadableStream, type Response } from "./_shims/index.js";
2
3
  export declare class Stream<Item> implements AsyncIterable<Item> {
3
- controller: AbortController;
4
- private response;
5
- private decoder;
6
- constructor(response: Response, controller: AbortController);
7
- private iterMessages;
8
- [Symbol.asyncIterator](): AsyncIterator<Item, any, undefined>;
4
+ private iterator;
5
+ controller: AbortController;
6
+ constructor(iterator: () => AsyncIterator<Item>, controller: AbortController);
7
+ static fromSSEResponse<Item>(response: Response, controller: AbortController): Stream<Item>;
8
+ static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController): Stream<Item>;
9
+ [Symbol.asyncIterator](): AsyncIterator<Item>;
10
+ tee(): [Stream<Item>, Stream<Item>];
11
+ toReadableStream(): ReadableStream;
9
12
  }
10
- //# sourceMappingURL=streaming.d.ts.map
13
+ //# sourceMappingURL=streaming.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAa/D,qBAAa,MAAM,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC;IACtD,UAAU,EAAE,eAAe,CAAC;IAE5B,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,OAAO,CAAa;gBAEhB,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe;YAM5C,YAAY;IAqBpB,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;CAyCrE"}
1
+ {"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAc/D,qBAAa,MAAM,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC;IAG1C,OAAO,CAAC,QAAQ;IAF5B,UAAU,EAAE,eAAe,CAAC;gBAER,QAAQ,EAAE,MAAM,aAAa,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,eAAe;IAIpF,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe;IAwE5E,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,cAAc,EAAE,cAAc,EAAE,UAAU,EAAE,eAAe;IA2C3F,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC;IAI7C,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IA0BnC,gBAAgB,IAAI,cAAc;CA0BnC"}