@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.
- package/CHANGELOG.md +117 -0
- package/README.md +16 -6
- package/_shims/MultipartBody.d.ts +9 -0
- package/_shims/MultipartBody.d.ts.map +1 -0
- package/_shims/MultipartBody.js +16 -0
- package/_shims/MultipartBody.js.map +1 -0
- package/_shims/MultipartBody.mjs +12 -0
- package/_shims/MultipartBody.mjs.map +1 -0
- package/_shims/README.md +46 -0
- package/{src/_shims/ReadableStream.node.ts → _shims/auto/runtime-bun.d.ts} +2 -3
- package/_shims/auto/runtime-bun.d.ts.map +1 -0
- package/_shims/auto/runtime-bun.js +21 -0
- package/_shims/auto/runtime-bun.js.map +1 -0
- package/_shims/auto/runtime-bun.mjs +2 -0
- package/_shims/auto/runtime-bun.mjs.map +1 -0
- package/_shims/auto/runtime-node.d.ts +5 -0
- package/_shims/auto/runtime-node.d.ts.map +1 -0
- package/_shims/auto/runtime-node.js +21 -0
- package/_shims/auto/runtime-node.js.map +1 -0
- package/_shims/auto/runtime-node.mjs +2 -0
- package/_shims/auto/runtime-node.mjs.map +1 -0
- package/_shims/auto/runtime.d.ts +5 -0
- package/_shims/auto/runtime.d.ts.map +1 -0
- package/_shims/auto/runtime.js +21 -0
- package/_shims/auto/runtime.js.map +1 -0
- package/_shims/auto/runtime.mjs +2 -0
- package/_shims/auto/runtime.mjs.map +1 -0
- package/_shims/auto/types-node.d.ts +5 -0
- package/_shims/auto/types-node.d.ts.map +1 -0
- package/_shims/auto/types-node.js +21 -0
- package/_shims/auto/types-node.js.map +1 -0
- package/_shims/auto/types-node.mjs +2 -0
- package/_shims/auto/types-node.mjs.map +1 -0
- package/_shims/auto/types.d.ts +101 -0
- package/{src/_shims/ReadableStream.js → _shims/auto/types.js} +0 -2
- package/_shims/{ReadableStream.js → auto/types.mjs} +0 -2
- package/_shims/bun-runtime.d.ts +6 -0
- package/_shims/bun-runtime.d.ts.map +1 -0
- package/_shims/bun-runtime.js +14 -0
- package/_shims/bun-runtime.js.map +1 -0
- package/_shims/bun-runtime.mjs +10 -0
- package/_shims/bun-runtime.mjs.map +1 -0
- package/_shims/index.d.ts +81 -0
- package/_shims/index.js +13 -0
- package/_shims/index.mjs +7 -0
- package/_shims/manual-types.d.ts +12 -0
- package/_shims/manual-types.js +3 -0
- package/_shims/manual-types.mjs +3 -0
- package/_shims/node-runtime.d.ts +3 -0
- package/_shims/node-runtime.d.ts.map +1 -0
- package/_shims/node-runtime.js +90 -0
- package/_shims/node-runtime.js.map +1 -0
- package/_shims/node-runtime.mjs +56 -0
- package/_shims/node-runtime.mjs.map +1 -0
- package/_shims/node-types.d.ts +42 -0
- package/_shims/node-types.js +3 -0
- package/_shims/node-types.mjs +3 -0
- package/_shims/registry.d.ts +37 -0
- package/_shims/registry.d.ts.map +1 -0
- package/_shims/registry.js +41 -0
- package/_shims/registry.js.map +1 -0
- package/_shims/registry.mjs +37 -0
- package/_shims/registry.mjs.map +1 -0
- package/_shims/web-runtime.d.ts +5 -0
- package/_shims/web-runtime.d.ts.map +1 -0
- package/_shims/web-runtime.js +78 -0
- package/_shims/web-runtime.js.map +1 -0
- package/_shims/web-runtime.mjs +71 -0
- package/_shims/web-runtime.mjs.map +1 -0
- package/_shims/web-types.d.ts +83 -0
- package/_shims/web-types.js +3 -0
- package/_shims/web-types.mjs +3 -0
- package/core.d.ts +172 -224
- package/core.d.ts.map +1 -1
- package/core.js +701 -736
- package/core.js.map +1 -1
- package/core.mjs +694 -688
- package/core.mjs.map +1 -1
- package/error.d.ts +32 -32
- package/error.d.ts.map +1 -1
- package/error.js +105 -106
- package/error.js.map +1 -1
- package/error.mjs +101 -91
- package/error.mjs.map +1 -1
- package/index.d.mts +110 -117
- package/index.d.ts +110 -117
- package/index.d.ts.map +1 -1
- package/index.js +107 -159
- package/index.js.map +1 -1
- package/index.mjs +84 -104
- package/index.mjs.map +1 -1
- package/package.json +36 -21
- package/resource.d.ts +10 -10
- package/resource.js +12 -12
- package/resource.mjs +10 -10
- package/resources/completions.d.ts +136 -142
- package/resources/completions.d.ts.map +1 -1
- package/resources/completions.js +9 -14
- package/resources/completions.js.map +1 -1
- package/resources/completions.mjs +6 -11
- package/resources/completions.mjs.map +1 -1
- package/resources/index.d.ts +2 -9
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +5 -10
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +2 -2
- package/resources/index.mjs.map +1 -1
- package/resources/top-level.d.ts +1 -1
- package/resources/top-level.js +3 -3
- package/resources/top-level.mjs +1 -1
- package/shims/node.d.ts +29 -0
- package/shims/node.d.ts.map +1 -0
- package/shims/node.js +31 -0
- package/shims/node.js.map +1 -0
- package/shims/node.mjs +5 -0
- package/shims/node.mjs.map +1 -0
- package/shims/web.d.ts +26 -0
- package/shims/web.d.ts.map +1 -0
- package/shims/web.js +31 -0
- package/shims/web.js.map +1 -0
- package/shims/web.mjs +5 -0
- package/shims/web.mjs.map +1 -0
- package/src/_shims/MultipartBody.ts +9 -0
- package/src/_shims/README.md +46 -0
- package/src/_shims/auto/runtime-bun.ts +4 -0
- package/src/_shims/auto/runtime-node.ts +4 -0
- package/src/_shims/auto/runtime.ts +4 -0
- package/src/_shims/auto/types-node.ts +4 -0
- package/src/_shims/auto/types.d.ts +101 -0
- package/src/_shims/auto/types.js +3 -0
- package/src/_shims/auto/types.mjs +3 -0
- package/src/_shims/bun-runtime.ts +14 -0
- package/src/_shims/index.d.ts +81 -0
- package/src/_shims/index.js +13 -0
- package/src/_shims/index.mjs +7 -0
- package/src/_shims/manual-types.d.ts +12 -0
- package/src/_shims/manual-types.js +3 -0
- package/src/_shims/manual-types.mjs +3 -0
- package/src/_shims/node-runtime.ts +83 -0
- package/src/_shims/node-types.d.ts +42 -0
- package/src/_shims/node-types.js +3 -0
- package/src/_shims/node-types.mjs +3 -0
- package/src/_shims/registry.ts +67 -0
- package/src/_shims/web-runtime.ts +103 -0
- package/src/_shims/web-types.d.ts +83 -0
- package/src/_shims/web-types.js +3 -0
- package/src/_shims/web-types.mjs +3 -0
- package/src/core.ts +107 -56
- package/src/error.ts +20 -8
- package/src/index.ts +24 -17
- package/src/resources/completions.ts +11 -11
- package/src/resources/index.ts +0 -1
- package/src/shims/node.ts +50 -0
- package/src/shims/web.ts +50 -0
- package/src/streaming.ts +164 -59
- package/src/tsconfig.json +11 -0
- package/src/uploads.ts +11 -15
- package/src/version.ts +1 -1
- package/streaming.d.ts +11 -8
- package/streaming.d.ts.map +1 -1
- package/streaming.js +300 -192
- package/streaming.js.map +1 -1
- package/streaming.mjs +296 -188
- package/streaming.mjs.map +1 -1
- package/uploads.d.ts +23 -39
- package/uploads.d.ts.map +1 -1
- package/uploads.js +111 -157
- package/uploads.js.map +1 -1
- package/uploads.mjs +107 -133
- package/uploads.mjs.map +1 -1
- package/version.d.ts +2 -2
- package/version.js +4 -4
- package/version.mjs +2 -2
- package/_shims/ReadableStream.d.ts +0 -38
- package/_shims/ReadableStream.mjs +0 -7
- package/_shims/ReadableStream.node.d.ts +0 -6
- package/_shims/ReadableStream.node.d.ts.map +0 -1
- package/_shims/ReadableStream.node.js +0 -14
- package/_shims/ReadableStream.node.js.map +0 -1
- package/_shims/ReadableStream.node.mjs +0 -3
- package/_shims/ReadableStream.node.mjs.map +0 -1
- package/_shims/agent.d.ts +0 -9
- package/_shims/agent.d.ts.map +0 -1
- package/_shims/agent.js +0 -14
- package/_shims/agent.js.map +0 -1
- package/_shims/agent.mjs +0 -10
- package/_shims/agent.mjs.map +0 -1
- package/_shims/agent.node.d.ts +0 -7
- package/_shims/agent.node.d.ts.map +0 -1
- package/_shims/agent.node.js +0 -28
- package/_shims/agent.node.js.map +0 -1
- package/_shims/agent.node.mjs +0 -16
- package/_shims/agent.node.mjs.map +0 -1
- package/_shims/fetch.d.ts +0 -60
- package/_shims/fetch.js +0 -13
- package/_shims/fetch.mjs +0 -15
- package/_shims/fetch.node.d.ts +0 -64
- package/_shims/fetch.node.js +0 -12
- package/_shims/fetch.node.mjs +0 -14
- package/_shims/fileFromPath.d.ts +0 -22
- package/_shims/fileFromPath.d.ts.map +0 -1
- package/_shims/fileFromPath.js +0 -16
- package/_shims/fileFromPath.js.map +0 -1
- package/_shims/fileFromPath.mjs +0 -12
- package/_shims/fileFromPath.mjs.map +0 -1
- package/_shims/fileFromPath.node.d.ts +0 -17
- package/_shims/fileFromPath.node.d.ts.map +0 -1
- package/_shims/fileFromPath.node.js +0 -17
- package/_shims/fileFromPath.node.js.map +0 -1
- package/_shims/fileFromPath.node.mjs +0 -13
- package/_shims/fileFromPath.node.mjs.map +0 -1
- package/_shims/formdata.d.ts +0 -43
- package/_shims/formdata.js +0 -17
- package/_shims/formdata.mjs +0 -20
- package/_shims/formdata.node.d.ts +0 -45
- package/_shims/formdata.node.js +0 -11
- package/_shims/formdata.node.mjs +0 -9
- package/_shims/getMultipartRequestOptions.d.ts +0 -10
- package/_shims/getMultipartRequestOptions.d.ts.map +0 -1
- package/_shims/getMultipartRequestOptions.js +0 -12
- package/_shims/getMultipartRequestOptions.js.map +0 -1
- package/_shims/getMultipartRequestOptions.mjs +0 -8
- package/_shims/getMultipartRequestOptions.mjs.map +0 -1
- package/_shims/getMultipartRequestOptions.node.d.ts +0 -10
- package/_shims/getMultipartRequestOptions.node.d.ts.map +0 -1
- package/_shims/getMultipartRequestOptions.node.js +0 -22
- package/_shims/getMultipartRequestOptions.node.js.map +0 -1
- package/_shims/getMultipartRequestOptions.node.mjs +0 -18
- package/_shims/getMultipartRequestOptions.node.mjs.map +0 -1
- package/_shims/node-readable.d.ts +0 -23
- package/_shims/node-readable.d.ts.map +0 -1
- package/_shims/node-readable.js +0 -11
- package/_shims/node-readable.js.map +0 -1
- package/_shims/node-readable.mjs +0 -7
- package/_shims/node-readable.mjs.map +0 -1
- package/_shims/node-readable.node.d.ts +0 -8
- package/_shims/node-readable.node.d.ts.map +0 -1
- package/_shims/node-readable.node.js +0 -9
- package/_shims/node-readable.node.js.map +0 -1
- package/_shims/node-readable.node.mjs +0 -5
- package/_shims/node-readable.node.mjs.map +0 -1
- package/src/_shims/ReadableStream.d.ts +0 -38
- package/src/_shims/ReadableStream.mjs +0 -7
- package/src/_shims/agent.node.ts +0 -22
- package/src/_shims/agent.ts +0 -12
- package/src/_shims/fetch.d.ts +0 -60
- package/src/_shims/fetch.deno.ts +0 -23
- package/src/_shims/fetch.js +0 -13
- package/src/_shims/fetch.mjs +0 -15
- package/src/_shims/fetch.node.d.ts +0 -64
- package/src/_shims/fetch.node.js +0 -12
- package/src/_shims/fetch.node.mjs +0 -14
- package/src/_shims/fileFromPath.node.ts +0 -29
- package/src/_shims/fileFromPath.ts +0 -29
- package/src/_shims/formdata.d.ts +0 -43
- package/src/_shims/formdata.deno.ts +0 -16
- package/src/_shims/formdata.js +0 -17
- package/src/_shims/formdata.mjs +0 -20
- package/src/_shims/formdata.node.d.ts +0 -45
- package/src/_shims/formdata.node.js +0 -11
- package/src/_shims/formdata.node.mjs +0 -9
- package/src/_shims/getMultipartRequestOptions.node.ts +0 -25
- package/src/_shims/getMultipartRequestOptions.ts +0 -14
- package/src/_shims/node-readable.node.ts +0 -10
- 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
|
+
}
|
package/src/shims/web.ts
ADDED
|
@@ -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
|
|
1
|
+
import { ReadableStream, type Response } from './_shims/index';
|
|
2
|
+
import { AnthropicError } from './error';
|
|
2
3
|
|
|
3
|
-
import { safeJSON, createResponseHeaders } from
|
|
4
|
-
import { APIError } from
|
|
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
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
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
|
-
|
|
62
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
|
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
|
|
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
|
|
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 {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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.
|
|
1
|
+
export const VERSION = '0.6.8'; // x-release-please-version
|
package/streaming.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
import { ReadableStream, type Response } from "./_shims/index.js";
|
|
2
3
|
export declare class Stream<Item> implements AsyncIterable<Item> {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
package/streaming.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";AAAA,OAAO,
|
|
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"}
|