@anthropic-ai/sdk 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{dist/esm/_shims → _shims}/agent.d.ts +2 -2
- package/_shims/agent.d.ts.map +1 -0
- package/_shims/agent.js.map +1 -0
- package/{dist/esm/_shims/agent.js → _shims/agent.mjs} +1 -1
- package/_shims/agent.mjs.map +1 -0
- package/{dist/esm/_shims → _shims}/agent.node.d.ts +1 -0
- package/_shims/agent.node.d.ts.map +1 -0
- package/_shims/agent.node.js.map +1 -0
- package/{dist/esm/_shims/agent.node.js → _shims/agent.node.mjs} +1 -1
- package/_shims/agent.node.mjs.map +1 -0
- package/_shims/fetch.d.ts +52 -0
- package/_shims/fetch.js +13 -0
- package/_shims/fetch.mjs +15 -0
- package/_shims/fetch.node.d.ts +53 -0
- package/_shims/fetch.node.js +12 -0
- package/_shims/fetch.node.mjs +14 -0
- package/{dist/cjs/_shims → _shims}/fileFromPath.d.ts +1 -2
- package/_shims/fileFromPath.d.ts.map +1 -0
- package/_shims/fileFromPath.js +16 -0
- package/_shims/fileFromPath.js.map +1 -0
- package/{dist/esm/_shims/fileFromPath.js → _shims/fileFromPath.mjs} +1 -1
- package/_shims/fileFromPath.mjs.map +1 -0
- package/{dist/cjs/_shims → _shims}/fileFromPath.node.d.ts +1 -2
- package/_shims/fileFromPath.node.d.ts.map +1 -0
- package/_shims/fileFromPath.node.js +17 -0
- package/_shims/fileFromPath.node.js.map +1 -0
- package/{dist/esm/_shims/fileFromPath.node.js → _shims/fileFromPath.node.mjs} +1 -1
- package/_shims/fileFromPath.node.mjs.map +1 -0
- package/_shims/formdata.d.ts +43 -0
- package/_shims/formdata.js +9 -0
- package/{dist/esm/_shims/formdata.js → _shims/formdata.mjs} +7 -11
- package/_shims/formdata.node.d.ts +44 -0
- package/_shims/formdata.node.js +11 -0
- package/_shims/getMultipartRequestOptions.d.ts.map +1 -0
- package/_shims/getMultipartRequestOptions.js +12 -0
- package/_shims/getMultipartRequestOptions.js.map +1 -0
- package/_shims/getMultipartRequestOptions.mjs +8 -0
- package/_shims/getMultipartRequestOptions.mjs.map +1 -0
- package/_shims/getMultipartRequestOptions.node.d.ts.map +1 -0
- package/_shims/getMultipartRequestOptions.node.js +22 -0
- package/_shims/getMultipartRequestOptions.node.js.map +1 -0
- package/{dist/esm/_shims/getMultipartRequestOptions.node.js → _shims/getMultipartRequestOptions.node.mjs} +3 -2
- package/_shims/getMultipartRequestOptions.node.mjs.map +1 -0
- package/_shims/node-readable.d.ts +23 -0
- package/_shims/node-readable.d.ts.map +1 -0
- package/_shims/node-readable.js +11 -0
- package/_shims/node-readable.js.map +1 -0
- package/_shims/node-readable.mjs +7 -0
- package/_shims/node-readable.mjs.map +1 -0
- package/_shims/node-readable.node.d.ts +8 -0
- package/_shims/node-readable.node.d.ts.map +1 -0
- package/_shims/node-readable.node.js +9 -0
- package/_shims/node-readable.node.js.map +1 -0
- package/_shims/node-readable.node.mjs +5 -0
- package/_shims/node-readable.node.mjs.map +1 -0
- package/{dist/esm/core.d.ts → core.d.ts} +15 -10
- package/core.d.ts.map +1 -0
- package/{dist/cjs/core.js → core.js} +191 -333
- package/core.js.map +1 -0
- package/{dist/esm/core.js → core.mjs} +56 -25
- package/core.mjs.map +1 -0
- package/error.d.ts.map +1 -0
- package/{dist/cjs/error.js → error.js} +1 -1
- package/error.js.map +1 -0
- package/{dist/esm/error.js → error.mjs} +5 -3
- package/error.mjs.map +1 -0
- package/{dist/cjs/index.d.ts → index.d.mts} +42 -7
- package/{dist/esm/index.d.ts → index.d.ts} +38 -3
- package/index.d.ts.map +1 -0
- package/{dist/cjs/index.js → index.js} +17 -15
- package/index.js.map +1 -0
- package/{dist/esm/index.js → index.mjs} +12 -6
- package/index.mjs.map +1 -0
- package/package.json +34 -77
- package/resource.d.ts.map +1 -0
- package/resource.js.map +1 -0
- package/{dist/esm/resource.js → resource.mjs} +1 -1
- package/resource.mjs.map +1 -0
- package/{dist/esm/resources → resources}/completions.d.ts +3 -3
- package/resources/completions.d.ts.map +1 -0
- package/{dist/cjs/resources → resources}/completions.js +7 -7
- package/resources/completions.js.map +1 -0
- package/resources/completions.mjs +15 -0
- package/resources/completions.mjs.map +1 -0
- package/resources/index.d.ts.map +1 -0
- package/{dist/cjs/resources → resources}/index.js +1 -1
- package/resources/index.js.map +1 -0
- package/resources/index.mjs +3 -0
- package/resources/index.mjs.map +1 -0
- package/resources/top-level.d.ts.map +1 -0
- package/resources/top-level.js.map +1 -0
- package/{dist/esm/resources/top-level.js → resources/top-level.mjs} +1 -1
- package/resources/top-level.mjs.map +1 -0
- package/{_shims → src/_shims}/agent.node.ts +2 -0
- package/{_shims → src/_shims}/agent.ts +2 -2
- package/src/_shims/fetch.d.ts +52 -0
- package/src/_shims/fetch.js +13 -0
- package/src/_shims/fetch.mjs +15 -0
- package/src/_shims/fetch.node.d.ts +53 -0
- package/src/_shims/fetch.node.js +12 -0
- package/src/_shims/fetch.node.mjs +14 -0
- package/{_shims → src/_shims}/fileFromPath.node.ts +1 -2
- package/{_shims → src/_shims}/fileFromPath.ts +1 -2
- package/src/_shims/formdata.d.ts +43 -0
- package/src/_shims/formdata.js +9 -0
- package/src/_shims/formdata.mjs +11 -0
- package/src/_shims/formdata.node.d.ts +44 -0
- package/src/_shims/formdata.node.js +11 -0
- package/{dist/esm/_shims/formdata.node.js → src/_shims/formdata.node.mjs} +3 -1
- package/{_shims → src/_shims}/getMultipartRequestOptions.node.ts +2 -1
- package/{_shims → src/_shims}/getMultipartRequestOptions.ts +2 -1
- package/src/_shims/node-readable.node.ts +10 -0
- package/src/_shims/node-readable.ts +30 -0
- package/{core.ts → src/core.ts} +42 -20
- package/{index.ts → src/index.ts} +50 -3
- package/{resources → src/resources}/completions.ts +4 -4
- package/{streaming.ts → src/streaming.ts} +76 -13
- package/src/uploads.ts +248 -0
- package/src/version.ts +1 -0
- package/{dist/esm/streaming.d.ts → streaming.d.ts} +2 -0
- package/streaming.d.ts.map +1 -0
- package/streaming.js +216 -0
- package/streaming.js.map +1 -0
- package/{dist/esm/streaming.js → streaming.mjs} +62 -13
- package/streaming.mjs.map +1 -0
- package/uploads.d.ts +90 -0
- package/uploads.d.ts.map +1 -0
- package/uploads.js +207 -0
- package/uploads.js.map +1 -0
- package/uploads.mjs +174 -0
- package/uploads.mjs.map +1 -0
- package/version.d.ts +2 -0
- package/version.d.ts.map +1 -0
- package/{dist/cjs/version.js → version.js} +1 -1
- package/version.js.map +1 -0
- package/version.mjs +2 -0
- package/version.mjs.map +1 -0
- package/_shims/fetch.node.ts +0 -11
- package/_shims/fetch.ts +0 -32
- package/_shims/formdata.ts +0 -22
- package/_shims/newFileArgs.ts +0 -94
- package/_shims/toFile.node.ts +0 -25
- package/_shims/toFile.ts +0 -26
- package/_shims/uploadable.node.ts +0 -21
- package/_shims/uploadable.ts +0 -82
- package/check-version.ts +0 -18
- package/dist/cjs/_shims/agent.d.ts +0 -9
- package/dist/cjs/_shims/agent.d.ts.map +0 -1
- package/dist/cjs/_shims/agent.js.map +0 -1
- package/dist/cjs/_shims/agent.node.d.ts +0 -6
- package/dist/cjs/_shims/agent.node.d.ts.map +0 -1
- package/dist/cjs/_shims/agent.node.js.map +0 -1
- package/dist/cjs/_shims/fetch.d.ts +0 -19
- package/dist/cjs/_shims/fetch.d.ts.map +0 -1
- package/dist/cjs/_shims/fetch.js +0 -24
- package/dist/cjs/_shims/fetch.js.map +0 -1
- package/dist/cjs/_shims/fetch.node.d.ts +0 -9
- package/dist/cjs/_shims/fetch.node.d.ts.map +0 -1
- package/dist/cjs/_shims/fetch.node.js +0 -68
- package/dist/cjs/_shims/fetch.node.js.map +0 -1
- package/dist/cjs/_shims/fileFromPath.d.ts.map +0 -1
- package/dist/cjs/_shims/fileFromPath.js +0 -49
- package/dist/cjs/_shims/fileFromPath.js.map +0 -1
- package/dist/cjs/_shims/fileFromPath.node.d.ts.map +0 -1
- package/dist/cjs/_shims/fileFromPath.node.js +0 -50
- package/dist/cjs/_shims/fileFromPath.node.js.map +0 -1
- package/dist/cjs/_shims/formdata.d.ts +0 -13
- package/dist/cjs/_shims/formdata.d.ts.map +0 -1
- package/dist/cjs/_shims/formdata.js +0 -20
- package/dist/cjs/_shims/formdata.js.map +0 -1
- package/dist/cjs/_shims/formdata.node.d.ts +0 -7
- package/dist/cjs/_shims/formdata.node.d.ts.map +0 -1
- package/dist/cjs/_shims/formdata.node.js +0 -27
- package/dist/cjs/_shims/formdata.node.js.map +0 -1
- package/dist/cjs/_shims/getMultipartRequestOptions.d.ts +0 -10
- package/dist/cjs/_shims/getMultipartRequestOptions.d.ts.map +0 -1
- package/dist/cjs/_shims/getMultipartRequestOptions.js +0 -44
- package/dist/cjs/_shims/getMultipartRequestOptions.js.map +0 -1
- package/dist/cjs/_shims/getMultipartRequestOptions.node.d.ts +0 -10
- package/dist/cjs/_shims/getMultipartRequestOptions.node.d.ts.map +0 -1
- package/dist/cjs/_shims/getMultipartRequestOptions.node.js +0 -52
- package/dist/cjs/_shims/getMultipartRequestOptions.node.js.map +0 -1
- package/dist/cjs/_shims/newFileArgs.d.ts +0 -19
- package/dist/cjs/_shims/newFileArgs.d.ts.map +0 -1
- package/dist/cjs/_shims/newFileArgs.js +0 -169
- package/dist/cjs/_shims/newFileArgs.js.map +0 -1
- package/dist/cjs/_shims/toFile.d.ts +0 -21
- package/dist/cjs/_shims/toFile.d.ts.map +0 -1
- package/dist/cjs/_shims/toFile.js +0 -56
- package/dist/cjs/_shims/toFile.js.map +0 -1
- package/dist/cjs/_shims/toFile.node.d.ts +0 -20
- package/dist/cjs/_shims/toFile.node.d.ts.map +0 -1
- package/dist/cjs/_shims/toFile.node.js +0 -56
- package/dist/cjs/_shims/toFile.node.js.map +0 -1
- package/dist/cjs/_shims/uploadable.d.ts +0 -56
- package/dist/cjs/_shims/uploadable.d.ts.map +0 -1
- package/dist/cjs/_shims/uploadable.js +0 -37
- package/dist/cjs/_shims/uploadable.js.map +0 -1
- package/dist/cjs/_shims/uploadable.node.d.ts +0 -7
- package/dist/cjs/_shims/uploadable.node.d.ts.map +0 -1
- package/dist/cjs/_shims/uploadable.node.js +0 -23
- package/dist/cjs/_shims/uploadable.node.js.map +0 -1
- package/dist/cjs/check-version.d.ts +0 -2
- package/dist/cjs/check-version.d.ts.map +0 -1
- package/dist/cjs/check-version.js +0 -23
- package/dist/cjs/check-version.js.map +0 -1
- package/dist/cjs/core.d.ts +0 -221
- package/dist/cjs/core.d.ts.map +0 -1
- package/dist/cjs/core.js.map +0 -1
- package/dist/cjs/error.d.ts +0 -48
- package/dist/cjs/error.d.ts.map +0 -1
- package/dist/cjs/error.js.map +0 -1
- package/dist/cjs/index.d.ts.map +0 -1
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/resource.d.ts +0 -12
- package/dist/cjs/resource.d.ts.map +0 -1
- package/dist/cjs/resource.js.map +0 -1
- package/dist/cjs/resources/completions.d.ts +0 -311
- package/dist/cjs/resources/completions.d.ts.map +0 -1
- package/dist/cjs/resources/completions.js.map +0 -1
- package/dist/cjs/resources/index.d.ts +0 -2
- package/dist/cjs/resources/index.d.ts.map +0 -1
- package/dist/cjs/resources/index.js.map +0 -1
- package/dist/cjs/resources/top-level.d.ts.map +0 -1
- package/dist/cjs/resources/top-level.js.map +0 -1
- package/dist/cjs/streaming.d.ts +0 -12
- package/dist/cjs/streaming.d.ts.map +0 -1
- package/dist/cjs/streaming.js +0 -297
- package/dist/cjs/streaming.js.map +0 -1
- package/dist/cjs/uploads.d.ts +0 -24
- package/dist/cjs/uploads.d.ts.map +0 -1
- package/dist/cjs/uploads.js +0 -121
- package/dist/cjs/uploads.js.map +0 -1
- package/dist/cjs/version.d.ts +0 -2
- package/dist/cjs/version.d.ts.map +0 -1
- package/dist/cjs/version.js.map +0 -1
- package/dist/esm/_shims/agent.d.ts.map +0 -1
- package/dist/esm/_shims/agent.js.map +0 -1
- package/dist/esm/_shims/agent.node.d.ts.map +0 -1
- package/dist/esm/_shims/agent.node.js.map +0 -1
- package/dist/esm/_shims/fetch.d.ts +0 -19
- package/dist/esm/_shims/fetch.d.ts.map +0 -1
- package/dist/esm/_shims/fetch.js +0 -18
- package/dist/esm/_shims/fetch.js.map +0 -1
- package/dist/esm/_shims/fetch.node.d.ts +0 -9
- package/dist/esm/_shims/fetch.node.d.ts.map +0 -1
- package/dist/esm/_shims/fetch.node.js +0 -7
- package/dist/esm/_shims/fetch.node.js.map +0 -1
- package/dist/esm/_shims/fileFromPath.d.ts +0 -23
- package/dist/esm/_shims/fileFromPath.d.ts.map +0 -1
- package/dist/esm/_shims/fileFromPath.js.map +0 -1
- package/dist/esm/_shims/fileFromPath.node.d.ts +0 -18
- package/dist/esm/_shims/fileFromPath.node.d.ts.map +0 -1
- package/dist/esm/_shims/fileFromPath.node.js.map +0 -1
- package/dist/esm/_shims/formdata.d.ts +0 -13
- package/dist/esm/_shims/formdata.d.ts.map +0 -1
- package/dist/esm/_shims/formdata.js.map +0 -1
- package/dist/esm/_shims/formdata.node.d.ts +0 -7
- package/dist/esm/_shims/formdata.node.d.ts.map +0 -1
- package/dist/esm/_shims/formdata.node.js.map +0 -1
- package/dist/esm/_shims/getMultipartRequestOptions.d.ts.map +0 -1
- package/dist/esm/_shims/getMultipartRequestOptions.js +0 -7
- package/dist/esm/_shims/getMultipartRequestOptions.js.map +0 -1
- package/dist/esm/_shims/getMultipartRequestOptions.node.d.ts.map +0 -1
- package/dist/esm/_shims/getMultipartRequestOptions.node.js.map +0 -1
- package/dist/esm/_shims/newFileArgs.d.ts +0 -19
- package/dist/esm/_shims/newFileArgs.d.ts.map +0 -1
- package/dist/esm/_shims/newFileArgs.js +0 -66
- package/dist/esm/_shims/newFileArgs.js.map +0 -1
- package/dist/esm/_shims/toFile.d.ts +0 -21
- package/dist/esm/_shims/toFile.d.ts.map +0 -1
- package/dist/esm/_shims/toFile.js +0 -19
- package/dist/esm/_shims/toFile.js.map +0 -1
- package/dist/esm/_shims/toFile.node.d.ts +0 -20
- package/dist/esm/_shims/toFile.node.d.ts.map +0 -1
- package/dist/esm/_shims/toFile.node.js +0 -19
- package/dist/esm/_shims/toFile.node.js.map +0 -1
- package/dist/esm/_shims/uploadable.d.ts +0 -56
- package/dist/esm/_shims/uploadable.d.ts.map +0 -1
- package/dist/esm/_shims/uploadable.js +0 -30
- package/dist/esm/_shims/uploadable.js.map +0 -1
- package/dist/esm/_shims/uploadable.node.d.ts +0 -7
- package/dist/esm/_shims/uploadable.node.d.ts.map +0 -1
- package/dist/esm/_shims/uploadable.node.js +0 -10
- package/dist/esm/_shims/uploadable.node.js.map +0 -1
- package/dist/esm/check-version.d.ts +0 -2
- package/dist/esm/check-version.d.ts.map +0 -1
- package/dist/esm/check-version.js +0 -16
- package/dist/esm/check-version.js.map +0 -1
- package/dist/esm/core.d.ts.map +0 -1
- package/dist/esm/core.js.map +0 -1
- package/dist/esm/error.d.ts.map +0 -1
- package/dist/esm/error.js.map +0 -1
- package/dist/esm/index.d.ts.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/package.json +0 -3
- package/dist/esm/resource.d.ts.map +0 -1
- package/dist/esm/resource.js.map +0 -1
- package/dist/esm/resources/completions.d.ts.map +0 -1
- package/dist/esm/resources/completions.js +0 -9
- package/dist/esm/resources/completions.js.map +0 -1
- package/dist/esm/resources/index.d.ts.map +0 -1
- package/dist/esm/resources/index.js +0 -3
- package/dist/esm/resources/index.js.map +0 -1
- package/dist/esm/resources/top-level.d.ts +0 -2
- package/dist/esm/resources/top-level.d.ts.map +0 -1
- package/dist/esm/resources/top-level.js.map +0 -1
- package/dist/esm/streaming.d.ts.map +0 -1
- package/dist/esm/streaming.js.map +0 -1
- package/dist/esm/uploads.d.ts +0 -24
- package/dist/esm/uploads.d.ts.map +0 -1
- package/dist/esm/uploads.js +0 -61
- package/dist/esm/uploads.js.map +0 -1
- package/dist/esm/version.d.ts +0 -2
- package/dist/esm/version.d.ts.map +0 -1
- package/dist/esm/version.js +0 -2
- package/dist/esm/version.js.map +0 -1
- package/uploads.ts +0 -81
- package/version.ts +0 -1
- /package/{dist/cjs/_shims → _shims}/agent.js +0 -0
- /package/{dist/cjs/_shims → _shims}/agent.node.js +0 -0
- /package/_shims/{formdata.node.ts → formdata.node.mjs} +0 -0
- /package/{dist/esm/_shims → _shims}/getMultipartRequestOptions.d.ts +0 -0
- /package/{dist/esm/_shims → _shims}/getMultipartRequestOptions.node.d.ts +0 -0
- /package/{dist/esm/error.d.ts → error.d.ts} +0 -0
- /package/{dist/esm/resource.d.ts → resource.d.ts} +0 -0
- /package/{dist/cjs/resource.js → resource.js} +0 -0
- /package/{dist/esm/resources → resources}/index.d.ts +0 -0
- /package/{dist/cjs/resources → resources}/top-level.d.ts +0 -0
- /package/{dist/cjs/resources → resources}/top-level.js +0 -0
- /package/{error.ts → src/error.ts} +0 -0
- /package/{resource.ts → src/resource.ts} +0 -0
- /package/{resources → src/resources}/index.ts +0 -0
- /package/{resources → src/resources}/top-level.ts +0 -0
|
@@ -6,6 +6,7 @@ import { FormData } from './formdata.node';
|
|
|
6
6
|
import type { RequestOptions } from '../core';
|
|
7
7
|
import { Readable } from 'node:stream';
|
|
8
8
|
import { FormDataEncoder } from 'form-data-encoder';
|
|
9
|
+
import { MultipartBody } from '../uploads';
|
|
9
10
|
|
|
10
11
|
export async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
|
|
11
12
|
form: FormData,
|
|
@@ -13,7 +14,7 @@ export async function getMultipartRequestOptions<T extends {} = Record<string, u
|
|
|
13
14
|
): Promise<RequestOptions<T>> {
|
|
14
15
|
const encoder = new FormDataEncoder(form);
|
|
15
16
|
const readable = Readable.from(encoder);
|
|
16
|
-
const body =
|
|
17
|
+
const body = new MultipartBody(readable);
|
|
17
18
|
const headers = {
|
|
18
19
|
...opts.headers,
|
|
19
20
|
...encoder.headers,
|
|
@@ -4,10 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
import { FormData } from './formdata';
|
|
6
6
|
import type { RequestOptions } from '../core';
|
|
7
|
+
import { MultipartBody } from '../uploads';
|
|
7
8
|
|
|
8
9
|
export async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
|
|
9
10
|
form: FormData,
|
|
10
11
|
opts: RequestOptions<T>,
|
|
11
12
|
): Promise<RequestOptions<T>> {
|
|
12
|
-
return { ...opts, body:
|
|
13
|
+
return { ...opts, body: new MultipartBody(form) as any };
|
|
13
14
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
export type { Readable } from 'node:stream';
|
|
5
|
+
import { ReadStream as FsReadStream } from 'node:fs';
|
|
6
|
+
export type { FsReadStream };
|
|
7
|
+
|
|
8
|
+
export function isFsReadStream(value: any): value is FsReadStream {
|
|
9
|
+
return value instanceof FsReadStream;
|
|
10
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// shim these Node types to avoid importing @types/node and polluting the user's
|
|
6
|
+
// type environment in non-node projects
|
|
7
|
+
|
|
8
|
+
export declare class Readable {
|
|
9
|
+
readable: boolean;
|
|
10
|
+
readonly readableEnded: boolean;
|
|
11
|
+
readonly readableFlowing: boolean | null;
|
|
12
|
+
readonly readableHighWaterMark: number;
|
|
13
|
+
readonly readableLength: number;
|
|
14
|
+
readonly readableObjectMode: boolean;
|
|
15
|
+
destroyed: boolean;
|
|
16
|
+
read(size?: number): any;
|
|
17
|
+
pause(): this;
|
|
18
|
+
resume(): this;
|
|
19
|
+
isPaused(): boolean;
|
|
20
|
+
destroy(error?: Error): this;
|
|
21
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<any>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export declare class FsReadStream extends Readable {
|
|
25
|
+
path: {}; // node type is string | Buffer
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function isFsReadStream(value: any): value is FsReadStream {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
package/{core.ts → src/core.ts}
RENAMED
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { Agent } from 'http';
|
|
3
|
-
|
|
4
|
-
import qs from 'qs';
|
|
5
|
-
|
|
1
|
+
import * as qs from 'qs';
|
|
6
2
|
import { VERSION } from './version';
|
|
7
3
|
import { Stream } from './streaming';
|
|
8
4
|
import { APIError, APIConnectionError, APIConnectionTimeoutError } from './error';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
5
|
+
import type { Readable } from '@anthropic-ai/sdk/_shims/node-readable';
|
|
6
|
+
import { getDefaultAgent, type Agent } from '@anthropic-ai/sdk/_shims/agent';
|
|
7
|
+
import {
|
|
8
|
+
fetch,
|
|
9
|
+
isPolyfilled as fetchIsPolyfilled,
|
|
10
|
+
type RequestInfo,
|
|
11
|
+
type RequestInit,
|
|
12
|
+
type Response,
|
|
13
|
+
} from '@anthropic-ai/sdk/_shims/fetch';
|
|
12
14
|
import { isMultipartBody } from './uploads';
|
|
13
|
-
export {
|
|
14
|
-
|
|
15
|
+
export {
|
|
16
|
+
maybeMultipartFormRequestOptions,
|
|
17
|
+
multipartFormRequestOptions,
|
|
18
|
+
createForm,
|
|
19
|
+
type Uploadable,
|
|
20
|
+
} from './uploads';
|
|
15
21
|
|
|
16
22
|
const MAX_RETRIES = 2;
|
|
17
23
|
|
|
@@ -67,6 +73,8 @@ export abstract class APIClient {
|
|
|
67
73
|
};
|
|
68
74
|
}
|
|
69
75
|
|
|
76
|
+
protected abstract defaultQuery(): DefaultQuery | undefined;
|
|
77
|
+
|
|
70
78
|
/**
|
|
71
79
|
* Override this to add your own headers validation:
|
|
72
80
|
*/
|
|
@@ -118,7 +126,7 @@ export abstract class APIClient {
|
|
|
118
126
|
const { method, path, query, headers: headers = {} } = options;
|
|
119
127
|
|
|
120
128
|
const body =
|
|
121
|
-
isMultipartBody(options.body) ? options.body.
|
|
129
|
+
isMultipartBody(options.body) ? options.body.body
|
|
122
130
|
: options.body ? JSON.stringify(options.body, null, 2)
|
|
123
131
|
: null;
|
|
124
132
|
const contentLength = typeof body === 'string' ? body.length.toString() : null;
|
|
@@ -148,7 +156,7 @@ export abstract class APIClient {
|
|
|
148
156
|
|
|
149
157
|
const req: RequestInit = {
|
|
150
158
|
method,
|
|
151
|
-
...(body && { body }),
|
|
159
|
+
...(body && { body: body as any }),
|
|
152
160
|
headers: reqHeaders,
|
|
153
161
|
...(httpAgent && { agent: httpAgent }),
|
|
154
162
|
};
|
|
@@ -254,6 +262,11 @@ export abstract class APIClient {
|
|
|
254
262
|
new URL(path)
|
|
255
263
|
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
|
|
256
264
|
|
|
265
|
+
const defaultQuery = this.defaultQuery();
|
|
266
|
+
if (!isEmptyObj(defaultQuery)) {
|
|
267
|
+
query = { ...defaultQuery, ...query } as Req;
|
|
268
|
+
}
|
|
269
|
+
|
|
257
270
|
if (query) {
|
|
258
271
|
url.search = qs.stringify(query, this.qsOptions());
|
|
259
272
|
}
|
|
@@ -263,11 +276,12 @@ export abstract class APIClient {
|
|
|
263
276
|
|
|
264
277
|
async fetchWithTimeout(
|
|
265
278
|
url: RequestInfo,
|
|
266
|
-
|
|
279
|
+
init: RequestInit | undefined,
|
|
267
280
|
ms: number,
|
|
268
281
|
controller: AbortController,
|
|
269
|
-
) {
|
|
270
|
-
|
|
282
|
+
): Promise<Response> {
|
|
283
|
+
const { signal, ...options } = init || {};
|
|
284
|
+
if (signal) signal.addEventListener('abort', () => controller.abort());
|
|
271
285
|
|
|
272
286
|
const timeout = setTimeout(() => controller.abort(), ms);
|
|
273
287
|
|
|
@@ -491,18 +505,25 @@ export class PagePromise<
|
|
|
491
505
|
export const createResponseHeaders = (
|
|
492
506
|
headers: Awaited<ReturnType<Fetch>>['headers'],
|
|
493
507
|
): Record<string, string> => {
|
|
494
|
-
return new Proxy(
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
508
|
+
return new Proxy(
|
|
509
|
+
Object.fromEntries(
|
|
510
|
+
// @ts-ignore
|
|
511
|
+
headers.entries(),
|
|
512
|
+
),
|
|
513
|
+
{
|
|
514
|
+
get(target, name) {
|
|
515
|
+
const key = name.toString();
|
|
516
|
+
return target[key.toLowerCase()] || target[key];
|
|
517
|
+
},
|
|
498
518
|
},
|
|
499
|
-
|
|
519
|
+
);
|
|
500
520
|
};
|
|
501
521
|
|
|
502
522
|
type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
503
523
|
|
|
504
524
|
export type RequestClient = { fetch: Fetch };
|
|
505
525
|
export type Headers = Record<string, string | null | undefined>;
|
|
526
|
+
export type DefaultQuery = Record<string, string | undefined>;
|
|
506
527
|
export type KeysEnum<T> = { [P in keyof Required<T>]: true };
|
|
507
528
|
|
|
508
529
|
export type RequestOptions<Req extends {} = Record<string, unknown> | Readable> = {
|
|
@@ -551,6 +572,7 @@ export type FinalRequestOptions<Req extends {} = Record<string, unknown> | Reada
|
|
|
551
572
|
};
|
|
552
573
|
|
|
553
574
|
export type APIResponse<T> = T & {
|
|
575
|
+
/** @deprecated - we plan to add a different way to access raw response information shortly. */
|
|
554
576
|
responseHeaders: Headers;
|
|
555
577
|
};
|
|
556
578
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless.
|
|
2
2
|
|
|
3
|
-
import qs from 'qs';
|
|
3
|
+
import * as qs from 'qs';
|
|
4
4
|
import * as Core from './core';
|
|
5
|
-
import * as API from './resources';
|
|
5
|
+
import * as API from './resources/index';
|
|
6
6
|
import * as Errors from './error';
|
|
7
|
-
import type { Agent } from '
|
|
7
|
+
import type { Agent } from '@anthropic-ai/sdk/_shims/agent';
|
|
8
8
|
import * as Uploads from './uploads';
|
|
9
9
|
|
|
10
10
|
type Config = {
|
|
@@ -12,11 +12,53 @@ type Config = {
|
|
|
12
12
|
* Defaults to process.env["ANTHROPIC_API_KEY"]. Set it to null if you want to send unauthenticated requests.
|
|
13
13
|
*/
|
|
14
14
|
apiKey?: string | null;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
|
|
18
|
+
*/
|
|
15
19
|
baseURL?: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The maximum amount of time (in milliseconds) that the client should wait for a response
|
|
23
|
+
* from the server before timing out a single request.
|
|
24
|
+
*
|
|
25
|
+
* Note that request timeouts are retried by default, so in a worst-case scenario you may wait
|
|
26
|
+
* much longer than this timeout before the promise succeeds or fails.
|
|
27
|
+
*/
|
|
16
28
|
timeout?: number;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An HTTP agent used to manage HTTP(S) connections.
|
|
32
|
+
*
|
|
33
|
+
* If not provided, an agent will be constructed by default in the Node.js environment,
|
|
34
|
+
* otherwise no agent is used.
|
|
35
|
+
*/
|
|
17
36
|
httpAgent?: Agent;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The maximum number of times that the client will retry a request in case of a
|
|
40
|
+
* temporary failure, like a network error or a 5XX error from the server.
|
|
41
|
+
*
|
|
42
|
+
* @default 2
|
|
43
|
+
*/
|
|
18
44
|
maxRetries?: number;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Default headers to include with every request to the API.
|
|
48
|
+
*
|
|
49
|
+
* These can be removed in individual requests by explicitly setting the
|
|
50
|
+
* header to `undefined` or `null` in request options.
|
|
51
|
+
*/
|
|
19
52
|
defaultHeaders?: Core.Headers;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Default query parameters to include with every request to the API.
|
|
56
|
+
*
|
|
57
|
+
* These can be removed in individual requests by explicitly setting the
|
|
58
|
+
* param to `undefined` in request options.
|
|
59
|
+
*/
|
|
60
|
+
defaultQuery?: Core.DefaultQuery;
|
|
61
|
+
|
|
20
62
|
authToken?: string | null;
|
|
21
63
|
};
|
|
22
64
|
|
|
@@ -48,6 +90,10 @@ export class Anthropic extends Core.APIClient {
|
|
|
48
90
|
|
|
49
91
|
completions: API.Completions = new API.Completions(this);
|
|
50
92
|
|
|
93
|
+
protected override defaultQuery(): Core.DefaultQuery | undefined {
|
|
94
|
+
return this._options.defaultQuery;
|
|
95
|
+
}
|
|
96
|
+
|
|
51
97
|
protected override defaultHeaders(): Core.Headers {
|
|
52
98
|
return {
|
|
53
99
|
...super.defaultHeaders(),
|
|
@@ -150,4 +196,5 @@ export namespace Anthropic {
|
|
|
150
196
|
export import Completion = API.Completion;
|
|
151
197
|
export import CompletionCreateParams = API.CompletionCreateParams;
|
|
152
198
|
}
|
|
199
|
+
|
|
153
200
|
export default Anthropic;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless.
|
|
2
2
|
|
|
3
|
-
import * as Core from '
|
|
4
|
-
import { APIResource } from '
|
|
3
|
+
import * as Core from '@anthropic-ai/sdk/core';
|
|
4
|
+
import { APIResource } from '@anthropic-ai/sdk/resource';
|
|
5
5
|
import * as API from './';
|
|
6
|
-
import { Stream } from '
|
|
6
|
+
import { Stream } from '@anthropic-ai/sdk/streaming';
|
|
7
7
|
|
|
8
8
|
export class Completions extends APIResource {
|
|
9
9
|
/**
|
|
@@ -21,7 +21,7 @@ export class Completions extends APIResource {
|
|
|
21
21
|
body: CompletionCreateParams,
|
|
22
22
|
options?: Core.RequestOptions,
|
|
23
23
|
): Promise<Core.APIResponse<Completion | Stream<Completion>>> {
|
|
24
|
-
return this.post('/v1/complete', { body, ...options, stream: body.stream ?? false });
|
|
24
|
+
return this.post('/v1/complete', { body, timeout: 600000, ...options, stream: body.stream ?? false });
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import type { Response } from '@anthropic-ai/sdk/_shims/fetch';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
2
|
+
|
|
3
|
+
import { APIResponse, Headers, createResponseHeaders } from './core';
|
|
4
|
+
|
|
5
|
+
import { safeJSON } from '@anthropic-ai/sdk/core';
|
|
6
|
+
import { APIError } from '@anthropic-ai/sdk/error';
|
|
7
|
+
|
|
8
|
+
type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
|
|
5
9
|
|
|
6
10
|
type ServerSentEvent = {
|
|
7
11
|
event: string | null;
|
|
@@ -65,7 +69,9 @@ class SSEDecoder {
|
|
|
65
69
|
}
|
|
66
70
|
|
|
67
71
|
export class Stream<Item> implements AsyncIterable<Item>, APIResponse<Stream<Item>> {
|
|
72
|
+
/** @deprecated - please use the async iterator instead. We plan to add additional helper methods shortly. */
|
|
68
73
|
response: Response;
|
|
74
|
+
/** @deprecated - we plan to add a different way to access raw response information shortly. */
|
|
69
75
|
responseHeaders: Headers;
|
|
70
76
|
controller: AbortController;
|
|
71
77
|
|
|
@@ -83,18 +89,11 @@ export class Stream<Item> implements AsyncIterable<Item>, APIResponse<Stream<Ite
|
|
|
83
89
|
this.controller.abort();
|
|
84
90
|
throw new Error(`Attempted to iterate over a response with no body`);
|
|
85
91
|
}
|
|
86
|
-
|
|
87
92
|
const lineDecoder = new LineDecoder();
|
|
88
93
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
text = chunk.toString();
|
|
93
|
-
} else if ((chunk as any) instanceof Uint8Array) {
|
|
94
|
-
text = Buffer.from(chunk).toString();
|
|
95
|
-
} else {
|
|
96
|
-
text = chunk;
|
|
97
|
-
}
|
|
94
|
+
const iter = readableStreamAsyncIterable<Bytes>(this.response.body);
|
|
95
|
+
for await (const chunk of iter) {
|
|
96
|
+
const text = decodeText(chunk);
|
|
98
97
|
|
|
99
98
|
for (const line of lineDecoder.decode(text)) {
|
|
100
99
|
const sse = this.decoder.decode(line);
|
|
@@ -215,3 +214,67 @@ function partition(str: string, delimiter: string): [string, string, string] {
|
|
|
215
214
|
|
|
216
215
|
return [str, '', ''];
|
|
217
216
|
}
|
|
217
|
+
|
|
218
|
+
let _textDecoder;
|
|
219
|
+
function decodeText(bytes: Bytes): string {
|
|
220
|
+
if (bytes == null) return '';
|
|
221
|
+
if (typeof bytes === 'string') return bytes;
|
|
222
|
+
|
|
223
|
+
// Node:
|
|
224
|
+
if (typeof Buffer !== 'undefined') {
|
|
225
|
+
if (bytes instanceof Buffer) {
|
|
226
|
+
return bytes.toString();
|
|
227
|
+
}
|
|
228
|
+
if (bytes instanceof Uint8Array) {
|
|
229
|
+
return Buffer.from(bytes).toString();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
throw new Error(`Unexpected: received non-Uint8Array (${bytes.constructor.name}) in Node.`);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Browser
|
|
236
|
+
if (typeof TextDecoder !== 'undefined') {
|
|
237
|
+
if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
|
|
238
|
+
_textDecoder ??= new TextDecoder('utf8');
|
|
239
|
+
return _textDecoder.decode(bytes);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
throw new Error(
|
|
243
|
+
`Unexpected: received non-Uint8Array/ArrayBuffer (${
|
|
244
|
+
(bytes as any).constructor.name
|
|
245
|
+
}) in a web platform.`,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
throw new Error(`Unexpected: neither Buffer nor TextDecoder are available as globals.`);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Most browsers don't yet have async iterable support for ReadableStream,
|
|
254
|
+
* and Node has a very different way of reading bytes from its "ReadableStream".
|
|
255
|
+
*
|
|
256
|
+
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1624185965
|
|
257
|
+
*
|
|
258
|
+
* We make extensive use of "any" here to avoid pulling in either "node" or "dom" types
|
|
259
|
+
* to library users' type scopes.
|
|
260
|
+
*/
|
|
261
|
+
function readableStreamAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
|
|
262
|
+
if (stream[Symbol.asyncIterator]) {
|
|
263
|
+
return stream[Symbol.asyncIterator];
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const reader = stream.getReader();
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
next() {
|
|
270
|
+
return reader.read();
|
|
271
|
+
},
|
|
272
|
+
async return() {
|
|
273
|
+
reader.releaseLock();
|
|
274
|
+
return { done: true, value: undefined };
|
|
275
|
+
},
|
|
276
|
+
[Symbol.asyncIterator]() {
|
|
277
|
+
return this;
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
}
|
package/src/uploads.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { type RequestOptions } from './core';
|
|
2
|
+
import { type Readable } from '@anthropic-ai/sdk/_shims/node-readable';
|
|
3
|
+
import { type BodyInit } from '@anthropic-ai/sdk/_shims/fetch';
|
|
4
|
+
import { FormData, File, type FilePropertyBag } from '@anthropic-ai/sdk/_shims/formdata';
|
|
5
|
+
import { getMultipartRequestOptions } from '@anthropic-ai/sdk/_shims/getMultipartRequestOptions';
|
|
6
|
+
import { fileFromPath } from '@anthropic-ai/sdk/_shims/fileFromPath';
|
|
7
|
+
import { type FsReadStream, isFsReadStream } from '@anthropic-ai/sdk/_shims/node-readable';
|
|
8
|
+
|
|
9
|
+
export { fileFromPath };
|
|
10
|
+
|
|
11
|
+
export type BlobPart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Typically, this is a native "File" class.
|
|
15
|
+
*
|
|
16
|
+
* We provide the {@link toFile} utility to convert a variety of objects
|
|
17
|
+
* into the File class.
|
|
18
|
+
*
|
|
19
|
+
* For convenience, you can also pass a fetch Response, or in Node,
|
|
20
|
+
* the result of fs.createReadStream().
|
|
21
|
+
*/
|
|
22
|
+
export type Uploadable = FileLike | ResponseLike | FsReadStream;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Intended to match web.Blob, node.Blob, node-fetch.Blob, etc.
|
|
26
|
+
*/
|
|
27
|
+
export interface BlobLike {
|
|
28
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
|
|
29
|
+
readonly size: number;
|
|
30
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
|
|
31
|
+
readonly type: string;
|
|
32
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
|
|
33
|
+
text(): Promise<string>;
|
|
34
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
|
|
35
|
+
slice(start?: number, end?: number): BlobLike;
|
|
36
|
+
// unfortunately @types/node-fetch@^2.6.4 doesn't type the arrayBuffer method
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Intended to match web.File, node.File, node-fetch.File, etc.
|
|
41
|
+
*/
|
|
42
|
+
export interface FileLike extends BlobLike {
|
|
43
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
|
|
44
|
+
readonly lastModified: number;
|
|
45
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
|
|
46
|
+
readonly name: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Intended to match web.Response, node.Response, node-fetch.Response, etc.
|
|
51
|
+
*/
|
|
52
|
+
export interface ResponseLike {
|
|
53
|
+
url: string;
|
|
54
|
+
blob(): Promise<BlobLike>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const isResponseLike = (value: any): value is ResponseLike =>
|
|
58
|
+
value != null &&
|
|
59
|
+
typeof value === 'object' &&
|
|
60
|
+
typeof value.url === 'string' &&
|
|
61
|
+
typeof value.blob === 'function';
|
|
62
|
+
|
|
63
|
+
export const isFileLike = (value: any): value is FileLike =>
|
|
64
|
+
value != null &&
|
|
65
|
+
typeof value === 'object' &&
|
|
66
|
+
typeof value.name === 'string' &&
|
|
67
|
+
typeof value.lastModified === 'number' &&
|
|
68
|
+
isBlobLike(value);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
|
|
72
|
+
* adds the arrayBuffer() method type because it is available and used at runtime
|
|
73
|
+
*/
|
|
74
|
+
export const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
|
|
75
|
+
value != null &&
|
|
76
|
+
typeof value === 'object' &&
|
|
77
|
+
typeof value.size === 'number' &&
|
|
78
|
+
typeof value.type === 'string' &&
|
|
79
|
+
typeof value.text === 'function' &&
|
|
80
|
+
typeof value.slice === 'function' &&
|
|
81
|
+
typeof value.arrayBuffer === 'function';
|
|
82
|
+
|
|
83
|
+
export const isUploadable = (value: any): value is Uploadable => {
|
|
84
|
+
return isFileLike(value) || isResponseLike(value) || isFsReadStream(value);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export type ToFileInput = Uploadable | Exclude<BlobPart, string> | AsyncIterable<BlobPart>;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
|
|
91
|
+
* @param bits the raw content of the file. Can be an {@link Uploadable}, {@link BlobPart}, or {@link AsyncIterable} of {@link BlobPart}s
|
|
92
|
+
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
|
|
93
|
+
* @param {Object=} options additional properties
|
|
94
|
+
* @param {string=} options.type the MIME type of the content
|
|
95
|
+
* @param {number=} options.lastModified the last modified timestamp
|
|
96
|
+
* @returns a {@link File} with the given properties
|
|
97
|
+
*/
|
|
98
|
+
export async function toFile(
|
|
99
|
+
value: ToFileInput,
|
|
100
|
+
name?: string | null | undefined,
|
|
101
|
+
options: FilePropertyBag | undefined = {},
|
|
102
|
+
): Promise<FileLike> {
|
|
103
|
+
if (isResponseLike(value)) {
|
|
104
|
+
const blob = await value.blob();
|
|
105
|
+
name ||= new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file';
|
|
106
|
+
|
|
107
|
+
return new File([blob as any], name, options);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const bits = await getBytes(value);
|
|
111
|
+
|
|
112
|
+
name ||= getName(value) ?? 'unknown_file';
|
|
113
|
+
|
|
114
|
+
if (!options.type) {
|
|
115
|
+
const type = (bits[0] as any)?.type;
|
|
116
|
+
if (typeof type === 'string') {
|
|
117
|
+
options = { ...options, type };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return new File(bits, name, options);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function getBytes(value: ToFileInput): Promise<Array<BlobPart>> {
|
|
125
|
+
if (value instanceof Promise) return getBytes(await (value as any));
|
|
126
|
+
|
|
127
|
+
let parts: Array<BlobPart> = [];
|
|
128
|
+
if (
|
|
129
|
+
typeof value === 'string' ||
|
|
130
|
+
ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
|
|
131
|
+
value instanceof ArrayBuffer
|
|
132
|
+
) {
|
|
133
|
+
parts.push(value);
|
|
134
|
+
} else if (isBlobLike(value)) {
|
|
135
|
+
parts.push(await value.arrayBuffer());
|
|
136
|
+
} else if (
|
|
137
|
+
isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
|
|
138
|
+
) {
|
|
139
|
+
for await (const chunk of value) {
|
|
140
|
+
parts.push(chunk as BlobPart); // TODO, consider validating?
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Unexpected data type: ${typeof value}; constructor: ${
|
|
145
|
+
value?.constructor?.name
|
|
146
|
+
}; props: ${propsForError(value)}`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return parts;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function propsForError(value: any): string {
|
|
154
|
+
const props = Object.getOwnPropertyNames(value);
|
|
155
|
+
return `[${props.map((p) => `"${p}"`).join(', ')}]`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function getName(value: any): string | undefined {
|
|
159
|
+
return (
|
|
160
|
+
getStringFromMaybeBuffer(value.name) ||
|
|
161
|
+
getStringFromMaybeBuffer(value.filename) ||
|
|
162
|
+
// For fs.ReadStream
|
|
163
|
+
getStringFromMaybeBuffer(value.path)?.split(/[\\/]/).pop()
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const getStringFromMaybeBuffer = (x: string | Buffer | unknown): string | undefined => {
|
|
168
|
+
if (typeof x === 'string') return x;
|
|
169
|
+
if (typeof Buffer !== 'undefined' && x instanceof Buffer) return String(x);
|
|
170
|
+
return undefined;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const isAsyncIterableIterator = (value: any): value is AsyncIterableIterator<unknown> =>
|
|
174
|
+
value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
|
|
175
|
+
|
|
176
|
+
export class MultipartBody {
|
|
177
|
+
constructor(public body: Readable | BodyInit) {}
|
|
178
|
+
get [Symbol.toStringTag](): string {
|
|
179
|
+
return 'MultipartBody';
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export const isMultipartBody = (body: any): body is MultipartBody =>
|
|
184
|
+
body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
|
|
188
|
+
* Otherwise returns the request as is.
|
|
189
|
+
*/
|
|
190
|
+
export const maybeMultipartFormRequestOptions = async <T extends {} = Record<string, unknown>>(
|
|
191
|
+
opts: RequestOptions<T>,
|
|
192
|
+
): Promise<RequestOptions<T | MultipartBody>> => {
|
|
193
|
+
if (!hasUploadableValue(opts.body)) return opts;
|
|
194
|
+
|
|
195
|
+
const form = await createForm(opts.body);
|
|
196
|
+
return getMultipartRequestOptions(form, opts);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
export const multipartFormRequestOptions = async <T extends {} = Record<string, unknown>>(
|
|
200
|
+
opts: RequestOptions<T>,
|
|
201
|
+
): Promise<RequestOptions<T | MultipartBody>> => {
|
|
202
|
+
const form = await createForm(opts.body);
|
|
203
|
+
return getMultipartRequestOptions(form, opts);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export const createForm = async <T = Record<string, unknown>>(body: T | undefined): Promise<FormData> => {
|
|
207
|
+
const form = new FormData();
|
|
208
|
+
await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
|
|
209
|
+
return form;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const hasUploadableValue = (value: unknown): boolean => {
|
|
213
|
+
if (isUploadable(value)) return true;
|
|
214
|
+
if (Array.isArray(value)) return value.some(hasUploadableValue);
|
|
215
|
+
if (value && typeof value === 'object') {
|
|
216
|
+
for (const k in value) {
|
|
217
|
+
if (hasUploadableValue((value as any)[k])) return true;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return false;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const addFormValue = async (form: FormData, key: string, value: unknown): Promise<void> => {
|
|
224
|
+
if (value === undefined) return;
|
|
225
|
+
if (value == null) {
|
|
226
|
+
throw new TypeError(
|
|
227
|
+
`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// TODO: make nested formats configurable
|
|
232
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
233
|
+
form.append(key, value);
|
|
234
|
+
} else if (isUploadable(value)) {
|
|
235
|
+
const file = await toFile(value);
|
|
236
|
+
form.append(key, file);
|
|
237
|
+
} else if (Array.isArray(value)) {
|
|
238
|
+
await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
|
|
239
|
+
} else if (typeof value === 'object') {
|
|
240
|
+
await Promise.all(
|
|
241
|
+
Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)),
|
|
242
|
+
);
|
|
243
|
+
} else {
|
|
244
|
+
throw new TypeError(
|
|
245
|
+
`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
};
|
package/src/version.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const VERSION = '0.5.2';
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { Response } from '@anthropic-ai/sdk/_shims/fetch';
|
|
2
2
|
import { APIResponse, Headers } from './core.js';
|
|
3
3
|
export declare class Stream<Item> implements AsyncIterable<Item>, APIResponse<Stream<Item>> {
|
|
4
|
+
/** @deprecated - please use the async iterator instead. We plan to add additional helper methods shortly. */
|
|
4
5
|
response: Response;
|
|
6
|
+
/** @deprecated - we plan to add a different way to access raw response information shortly. */
|
|
5
7
|
responseHeaders: Headers;
|
|
6
8
|
controller: AbortController;
|
|
7
9
|
private decoder;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAE/D,OAAO,EAAE,WAAW,EAAE,OAAO,EAAyB,MAAM,QAAQ,CAAC;AAoErE,qBAAa,MAAM,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACjF,6GAA6G;IAC7G,QAAQ,EAAE,QAAQ,CAAC;IACnB,+FAA+F;IAC/F,eAAe,EAAE,OAAO,CAAC;IACzB,UAAU,EAAE,eAAe,CAAC;IAE5B,OAAO,CAAC,OAAO,CAAa;gBAEhB,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe;YAO5C,YAAY;IAuBpB,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC;CAkCrE"}
|