@anthropic-ai/sdk 0.5.0 → 0.5.1
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/cjs/_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} +6 -3
- 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 +168 -0
- package/streaming.js.map +1 -0
- package/{dist/esm/streaming.js → streaming.mjs} +5 -4
- 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/LICENSE +0 -8
- package/README.md +0 -305
- 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.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 +0 -6
- 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
package/uploads.mjs
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { FormData, File } from '@anthropic-ai/sdk/_shims/formdata';
|
|
2
|
+
import { getMultipartRequestOptions } from '@anthropic-ai/sdk/_shims/getMultipartRequestOptions';
|
|
3
|
+
import { fileFromPath } from '@anthropic-ai/sdk/_shims/fileFromPath';
|
|
4
|
+
import { isFsReadStream } from '@anthropic-ai/sdk/_shims/node-readable';
|
|
5
|
+
export { fileFromPath };
|
|
6
|
+
export const isResponseLike = (value) =>
|
|
7
|
+
value != null &&
|
|
8
|
+
typeof value === 'object' &&
|
|
9
|
+
typeof value.url === 'string' &&
|
|
10
|
+
typeof value.blob === 'function';
|
|
11
|
+
export const isFileLike = (value) =>
|
|
12
|
+
value != null &&
|
|
13
|
+
typeof value === 'object' &&
|
|
14
|
+
typeof value.name === 'string' &&
|
|
15
|
+
typeof value.lastModified === 'number' &&
|
|
16
|
+
isBlobLike(value);
|
|
17
|
+
/**
|
|
18
|
+
* The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
|
|
19
|
+
* adds the arrayBuffer() method type because it is available and used at runtime
|
|
20
|
+
*/
|
|
21
|
+
export const isBlobLike = (value) =>
|
|
22
|
+
value != null &&
|
|
23
|
+
typeof value === 'object' &&
|
|
24
|
+
typeof value.size === 'number' &&
|
|
25
|
+
typeof value.type === 'string' &&
|
|
26
|
+
typeof value.text === 'function' &&
|
|
27
|
+
typeof value.slice === 'function' &&
|
|
28
|
+
typeof value.arrayBuffer === 'function';
|
|
29
|
+
export const isUploadable = (value) => {
|
|
30
|
+
return isFileLike(value) || isResponseLike(value) || isFsReadStream(value);
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
|
|
34
|
+
* @param bits the raw content of the file. Can be an {@link Uploadable}, {@link BlobPart}, or {@link AsyncIterable} of {@link BlobPart}s
|
|
35
|
+
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
|
|
36
|
+
* @param {Object=} options additional properties
|
|
37
|
+
* @param {string=} options.type the MIME type of the content
|
|
38
|
+
* @param {number=} options.lastModified the last modified timestamp
|
|
39
|
+
* @returns a {@link File} with the given properties
|
|
40
|
+
*/
|
|
41
|
+
export async function toFile(value, name, options = {}) {
|
|
42
|
+
var _a, _b, _c;
|
|
43
|
+
if (isResponseLike(value)) {
|
|
44
|
+
const blob = await value.blob();
|
|
45
|
+
name ||
|
|
46
|
+
(name =
|
|
47
|
+
(_a = new URL(value.url).pathname.split(/[\\/]/).pop()) !== null && _a !== void 0 ?
|
|
48
|
+
_a
|
|
49
|
+
: 'unknown_file');
|
|
50
|
+
return new File([blob], name, options);
|
|
51
|
+
}
|
|
52
|
+
const bits = await getBytes(value);
|
|
53
|
+
name || (name = (_b = getName(value)) !== null && _b !== void 0 ? _b : 'unknown_file');
|
|
54
|
+
if (!options.type) {
|
|
55
|
+
const type = (_c = bits[0]) === null || _c === void 0 ? void 0 : _c.type;
|
|
56
|
+
if (typeof type === 'string') {
|
|
57
|
+
options = { ...options, type };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return new File(bits, name, options);
|
|
61
|
+
}
|
|
62
|
+
async function getBytes(value) {
|
|
63
|
+
var _a;
|
|
64
|
+
if (value instanceof Promise) return getBytes(await value);
|
|
65
|
+
let parts = [];
|
|
66
|
+
if (
|
|
67
|
+
typeof value === 'string' ||
|
|
68
|
+
ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
|
|
69
|
+
value instanceof ArrayBuffer
|
|
70
|
+
) {
|
|
71
|
+
parts.push(value);
|
|
72
|
+
} else if (isBlobLike(value)) {
|
|
73
|
+
parts.push(await value.arrayBuffer());
|
|
74
|
+
} else if (
|
|
75
|
+
isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
|
|
76
|
+
) {
|
|
77
|
+
for await (const chunk of value) {
|
|
78
|
+
parts.push(chunk); // TODO, consider validating?
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Unexpected data type: ${typeof value}; constructor: ${
|
|
83
|
+
(_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ?
|
|
84
|
+
void 0
|
|
85
|
+
: _a.name
|
|
86
|
+
}; props: ${propsForError(value)}`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return parts;
|
|
90
|
+
}
|
|
91
|
+
function propsForError(value) {
|
|
92
|
+
const props = Object.getOwnPropertyNames(value);
|
|
93
|
+
return `[${props.map((p) => `"${p}"`).join(', ')}]`;
|
|
94
|
+
}
|
|
95
|
+
function getName(value) {
|
|
96
|
+
var _a;
|
|
97
|
+
return (
|
|
98
|
+
getStringFromMaybeBuffer(value.name) ||
|
|
99
|
+
getStringFromMaybeBuffer(value.filename) ||
|
|
100
|
+
// For fs.ReadStream
|
|
101
|
+
((_a = getStringFromMaybeBuffer(value.path)) === null || _a === void 0 ? void 0 : _a.split(/[\\/]/).pop())
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
const getStringFromMaybeBuffer = (x) => {
|
|
105
|
+
if (typeof x === 'string') return x;
|
|
106
|
+
if (typeof Buffer !== 'undefined' && x instanceof Buffer) return String(x);
|
|
107
|
+
return undefined;
|
|
108
|
+
};
|
|
109
|
+
const isAsyncIterableIterator = (value) =>
|
|
110
|
+
value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
|
|
111
|
+
export class MultipartBody {
|
|
112
|
+
constructor(body) {
|
|
113
|
+
this.body = body;
|
|
114
|
+
}
|
|
115
|
+
get [Symbol.toStringTag]() {
|
|
116
|
+
return 'MultipartBody';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
export const isMultipartBody = (body) =>
|
|
120
|
+
body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
|
|
121
|
+
/**
|
|
122
|
+
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
|
|
123
|
+
* Otherwise returns the request as is.
|
|
124
|
+
*/
|
|
125
|
+
export const maybeMultipartFormRequestOptions = async (opts) => {
|
|
126
|
+
if (!hasUploadableValue(opts.body)) return opts;
|
|
127
|
+
const form = await createForm(opts.body);
|
|
128
|
+
return getMultipartRequestOptions(form, opts);
|
|
129
|
+
};
|
|
130
|
+
export const multipartFormRequestOptions = async (opts) => {
|
|
131
|
+
const form = await createForm(opts.body);
|
|
132
|
+
return getMultipartRequestOptions(form, opts);
|
|
133
|
+
};
|
|
134
|
+
export const createForm = async (body) => {
|
|
135
|
+
const form = new FormData();
|
|
136
|
+
await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
|
|
137
|
+
return form;
|
|
138
|
+
};
|
|
139
|
+
const hasUploadableValue = (value) => {
|
|
140
|
+
if (isUploadable(value)) return true;
|
|
141
|
+
if (Array.isArray(value)) return value.some(hasUploadableValue);
|
|
142
|
+
if (value && typeof value === 'object') {
|
|
143
|
+
for (const k in value) {
|
|
144
|
+
if (hasUploadableValue(value[k])) return true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
};
|
|
149
|
+
const addFormValue = async (form, key, value) => {
|
|
150
|
+
if (value === undefined) return;
|
|
151
|
+
if (value == null) {
|
|
152
|
+
throw new TypeError(
|
|
153
|
+
`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
// TODO: make nested formats configurable
|
|
157
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
158
|
+
form.append(key, value);
|
|
159
|
+
} else if (isUploadable(value)) {
|
|
160
|
+
const file = await toFile(value);
|
|
161
|
+
form.append(key, file);
|
|
162
|
+
} else if (Array.isArray(value)) {
|
|
163
|
+
await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
|
|
164
|
+
} else if (typeof value === 'object') {
|
|
165
|
+
await Promise.all(
|
|
166
|
+
Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)),
|
|
167
|
+
);
|
|
168
|
+
} else {
|
|
169
|
+
throw new TypeError(
|
|
170
|
+
`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
//# sourceMappingURL=uploads.mjs.map
|
package/uploads.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uploads.mjs","sourceRoot":"","sources":["src/uploads.ts"],"names":[],"mappings":"OAGO,EAAE,QAAQ,EAAE,IAAI,EAAwB,MAAM,mCAAmC;OACjF,EAAE,0BAA0B,EAAE,MAAM,qDAAqD;OACzF,EAAE,YAAY,EAAE,MAAM,uCAAuC;OAC7D,EAAqB,cAAc,EAAE,MAAM,wCAAwC;AAE1F,OAAO,EAAE,YAAY,EAAE,CAAC;AAgDxB,MAAM,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AAI,EAAE,GAAG,GAAG,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;KAChF;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QACpC,MAAM,OAAO,CAAC,GAAG,CACf,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,IAAI,GAAG,EAAE,IAAI,CAAC,CAAC,CACzF,CAAC;KACH;SAAM;QACL,MAAM,IAAI,SAAS,CACjB,wGAAwG,KAAK,UAAU,CACxH,CAAC;KACH;AACH,CAAC,CAAC"}
|
package/version.d.ts
ADDED
package/version.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,UAAU,CAAC"}
|
package/version.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,OAAO,GAAG,OAAO,CAAC"}
|
package/version.mjs
ADDED
package/version.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.mjs","sourceRoot":"","sources":["src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC"}
|
package/LICENSE
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
Copyright 2023 Anthropic, PBC.
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
-
|
|
5
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
-
|
package/README.md
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
# Anthropic TypeScript API Library
|
|
2
|
-
|
|
3
|
-
[](https://npmjs.org/package/@anthropic-ai/sdk)
|
|
4
|
-
|
|
5
|
-
The Anthropic TypeScript library provides convenient access to the Anthropic REST API from applications written in server-side JavaScript.
|
|
6
|
-
It includes TypeScript definitions for all request params and response fields.
|
|
7
|
-
|
|
8
|
-
## Migration from v0.4.x and below
|
|
9
|
-
|
|
10
|
-
In `v0.5.0`, we introduced a fully rewritten SDK. The new version offers better error handling, a more robust and intuitive streaming implementation, and more.
|
|
11
|
-
|
|
12
|
-
Key interface changes:
|
|
13
|
-
|
|
14
|
-
1. `new Client(apiKey)` → `new Anthropic({ apiKey })`
|
|
15
|
-
2. `client.complete()` → `client.completions.create()`
|
|
16
|
-
3. `client.completeStream()` → `client.completions.create({ stream: true })`
|
|
17
|
-
1. `onUpdate` callback → `for await (const x of stream)`
|
|
18
|
-
2. full message in stream → delta of message in stream
|
|
19
|
-
|
|
20
|
-
<details>
|
|
21
|
-
<summary>Example diff</summary>
|
|
22
|
-
|
|
23
|
-
```diff ts
|
|
24
|
-
// Import "Anthropic" instead of "Client":
|
|
25
|
-
- import { Client, HUMAN_PROMPT, AI_PROMPT } from '@anthropic-ai/sdk';
|
|
26
|
-
+ import Anthropic, { HUMAN_PROMPT, AI_PROMPT } from '@anthropic-ai/sdk';
|
|
27
|
-
|
|
28
|
-
// Instantiate with "apiKey" as an object property:
|
|
29
|
-
- const client = new Client(apiKey);
|
|
30
|
-
+ const client = new Anthropic({ apiKey });
|
|
31
|
-
// or, simply provide an ANTHROPIC_API_KEY environment variable:
|
|
32
|
-
+ const client = new Anthropic();
|
|
33
|
-
|
|
34
|
-
async function main() {
|
|
35
|
-
// Request & response types are the same as before, but better-typed.
|
|
36
|
-
const params = {
|
|
37
|
-
prompt: `${HUMAN_PROMPT} How many toes do dogs have?${AI_PROMPT}`,
|
|
38
|
-
max_tokens_to_sample: 200,
|
|
39
|
-
model: "claude-1",
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// Instead of "client.complete()", you now call "client.completions.create()":
|
|
43
|
-
- await client.complete(params);
|
|
44
|
-
+ await client.completions.create(params);
|
|
45
|
-
|
|
46
|
-
// Streaming requests now use async iterators instead of callbacks:
|
|
47
|
-
- client.completeStream(params, {
|
|
48
|
-
- onUpdate: (completion) => {
|
|
49
|
-
- console.log(completion.completion); // full text
|
|
50
|
-
- },
|
|
51
|
-
- });
|
|
52
|
-
+ const stream = await client.completions.create({ ...params, stream: true });
|
|
53
|
-
+ for await (const completion of stream) {
|
|
54
|
-
+ process.stdout.write(completion.completion); // incremental text
|
|
55
|
-
+ }
|
|
56
|
-
|
|
57
|
-
// And, since this library uses `Anthropic-Version: 2023-06-01`,
|
|
58
|
-
// completion streams are now incremental diffs of text
|
|
59
|
-
// rather than sending the whole message every time:
|
|
60
|
-
let text = '';
|
|
61
|
-
- await client.completeStream(params, {
|
|
62
|
-
- onUpdate: (completion) => {
|
|
63
|
-
- const diff = completion.completion.replace(text, "");
|
|
64
|
-
- text = completion.completion;
|
|
65
|
-
- process.stdout.write(diff);
|
|
66
|
-
- },
|
|
67
|
-
- });
|
|
68
|
-
+ const stream = await client.completions.create({ ...params, stream: true });
|
|
69
|
-
+ for await (const completion of stream) {
|
|
70
|
-
+ const diff = completion.completion;
|
|
71
|
-
+ text += diff;
|
|
72
|
-
+ process.stdout.write(diff);
|
|
73
|
-
+ }
|
|
74
|
-
console.log('Done; final text is:')
|
|
75
|
-
console.log(text)
|
|
76
|
-
}
|
|
77
|
-
main();
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
</details>
|
|
81
|
-
|
|
82
|
-
## Documentation
|
|
83
|
-
|
|
84
|
-
The API documentation can be found [here](https://docs.anthropic.com/claude/reference/).
|
|
85
|
-
|
|
86
|
-
## Installation
|
|
87
|
-
|
|
88
|
-
```sh
|
|
89
|
-
npm install --save @anthropic-ai/sdk
|
|
90
|
-
# or
|
|
91
|
-
yarn add @anthropic-ai/sdk
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
## Usage
|
|
95
|
-
|
|
96
|
-
```js
|
|
97
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
98
|
-
|
|
99
|
-
const anthropic = new Anthropic({
|
|
100
|
-
apiKey: 'my api key', // defaults to process.env["ANTHROPIC_API_KEY"]
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
async function main() {
|
|
104
|
-
const completion = await anthropic.completions.create({
|
|
105
|
-
model: 'claude-1',
|
|
106
|
-
max_tokens_to_sample: 300,
|
|
107
|
-
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
main().catch(console.error);
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
## Streaming Responses
|
|
114
|
-
|
|
115
|
-
We provide support for streaming responses using Server Side Events (SSE).
|
|
116
|
-
|
|
117
|
-
```ts
|
|
118
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
119
|
-
|
|
120
|
-
const anthropic = new Anthropic();
|
|
121
|
-
|
|
122
|
-
const stream = await anthropic.completions.create({
|
|
123
|
-
prompt: `${Anthropic.HUMAN_PROMPT} Your prompt here ${Anthropic.AI_PROMPT}`,
|
|
124
|
-
model: 'claude-1',
|
|
125
|
-
stream: true,
|
|
126
|
-
max_tokens_to_sample: 300,
|
|
127
|
-
});
|
|
128
|
-
for await (const completion of stream) {
|
|
129
|
-
console.log(completion.completion);
|
|
130
|
-
}
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
If you need to cancel a stream, you can `break` from the loop
|
|
134
|
-
or call `stream.controller.abort()`.
|
|
135
|
-
|
|
136
|
-
### Usage with TypeScript
|
|
137
|
-
|
|
138
|
-
Importing, instantiating, and interacting with the library are the same as above.
|
|
139
|
-
If you like, you may reference our types directly:
|
|
140
|
-
|
|
141
|
-
```ts
|
|
142
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
143
|
-
|
|
144
|
-
const anthropic = new Anthropic({
|
|
145
|
-
apiKey: 'my api key', // defaults to process.env["ANTHROPIC_API_KEY"]
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
async function main() {
|
|
149
|
-
const params: Anthropic.CompletionCreateParams = {
|
|
150
|
-
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
|
|
151
|
-
max_tokens_to_sample: 300,
|
|
152
|
-
model: 'claude-1',
|
|
153
|
-
};
|
|
154
|
-
const completion: Anthropic.Completion = await anthropic.completions.create(params);
|
|
155
|
-
}
|
|
156
|
-
main().catch(console.error);
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
|
|
160
|
-
|
|
161
|
-
## Handling errors
|
|
162
|
-
|
|
163
|
-
When the library is unable to connect to the API,
|
|
164
|
-
or if the API returns a non-success status code (i.e., 4xx or 5xx response),
|
|
165
|
-
a subclass of `APIError` will be thrown:
|
|
166
|
-
|
|
167
|
-
```ts
|
|
168
|
-
async function main() {
|
|
169
|
-
const completion = await anthropic.completions
|
|
170
|
-
.create({
|
|
171
|
-
prompt: `${Anthropic.HUMAN_PROMPT} Your prompt here ${Anthropic.AI_PROMPT}`,
|
|
172
|
-
max_tokens_to_sample: 300,
|
|
173
|
-
model: 'claude-1',
|
|
174
|
-
})
|
|
175
|
-
.catch((err) => {
|
|
176
|
-
if (err instanceof Anthropic.APIError) {
|
|
177
|
-
console.log(err.status); // 400
|
|
178
|
-
console.log(err.name); // BadRequestError
|
|
179
|
-
console.log(err.headers); // {server: 'nginx', ...}
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
main().catch(console.error);
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
Error codes are as followed:
|
|
187
|
-
|
|
188
|
-
| Status Code | Error Type |
|
|
189
|
-
| ----------- | -------------------------- |
|
|
190
|
-
| 400 | `BadRequestError` |
|
|
191
|
-
| 401 | `AuthenticationError` |
|
|
192
|
-
| 403 | `PermissionDeniedError` |
|
|
193
|
-
| 404 | `NotFoundError` |
|
|
194
|
-
| 422 | `UnprocessableEntityError` |
|
|
195
|
-
| 429 | `RateLimitError` |
|
|
196
|
-
| >=500 | `InternalServerError` |
|
|
197
|
-
| N/A | `APIConnectionError` |
|
|
198
|
-
|
|
199
|
-
### Retries
|
|
200
|
-
|
|
201
|
-
Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
|
|
202
|
-
Connection errors (for example, due to a network connectivity problem), 409 Conflict, 429 Rate Limit,
|
|
203
|
-
and >=500 Internal errors will all be retried by default.
|
|
204
|
-
|
|
205
|
-
You can use the `maxRetries` option to configure or disable this:
|
|
206
|
-
|
|
207
|
-
<!-- prettier-ignore -->
|
|
208
|
-
```js
|
|
209
|
-
// Configure the default for all requests:
|
|
210
|
-
const anthropic = new Anthropic({
|
|
211
|
-
maxRetries: 0, // default is 2
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
// Or, configure per-request:
|
|
215
|
-
await anthropic.completions.create(
|
|
216
|
-
{
|
|
217
|
-
prompt: `${Anthropic.HUMAN_PROMPT} Can you help me effectively ask for a raise at work? ${Anthropic.AI_PROMPT}`,
|
|
218
|
-
max_tokens_to_sample: 300,
|
|
219
|
-
model: 'claude-1',
|
|
220
|
-
},
|
|
221
|
-
{
|
|
222
|
-
maxRetries: 5,
|
|
223
|
-
},
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
### Timeouts
|
|
229
|
-
|
|
230
|
-
Requests time out after 60 seconds by default. You can configure this with a `timeout` option:
|
|
231
|
-
|
|
232
|
-
<!-- prettier-ignore -->
|
|
233
|
-
```ts
|
|
234
|
-
// Configure the default for all requests:
|
|
235
|
-
const anthropic = new Anthropic({
|
|
236
|
-
timeout: 20 * 1000, // 20 seconds (default is 60s)
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
// Override per-request:
|
|
240
|
-
await anthropic.completions.create(
|
|
241
|
-
{
|
|
242
|
-
prompt: `${Anthropic.HUMAN_PROMPT} Where can I get a good coffee in my neighbourhood? ${Anthropic.AI_PROMPT}`,
|
|
243
|
-
max_tokens_to_sample: 300,
|
|
244
|
-
model: 'claude-1',
|
|
245
|
-
},
|
|
246
|
-
{
|
|
247
|
-
timeout: 5 * 1000,
|
|
248
|
-
},
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
On timeout, an `APIConnectionTimeoutError` is thrown.
|
|
254
|
-
|
|
255
|
-
Note that requests which time out will be [retried twice by default](#retries).
|
|
256
|
-
|
|
257
|
-
## Configuring an HTTP(S) Agent (e.g., for proxies)
|
|
258
|
-
|
|
259
|
-
By default, this library uses a stable agent for all http/https requests to reuse TCP connections, eliminating many TCP & TLS handshakes and shaving around 100ms off most requests.
|
|
260
|
-
|
|
261
|
-
If you would like to disable or customize this behavior, for example to use the API behind a proxy, you can pass an `httpAgent` which is used for all requests (be they http or https), for example:
|
|
262
|
-
|
|
263
|
-
<!-- prettier-ignore -->
|
|
264
|
-
```ts
|
|
265
|
-
import http from 'http';
|
|
266
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
267
|
-
import HttpsProxyAgent from 'https-proxy-agent';
|
|
268
|
-
|
|
269
|
-
// Configure the default for all requests:
|
|
270
|
-
const anthropic = new Anthropic({
|
|
271
|
-
httpAgent: new HttpsProxyAgent(process.env.PROXY_URL),
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
// Override per-request:
|
|
275
|
-
await anthropic.completions.create(
|
|
276
|
-
{
|
|
277
|
-
prompt: `${Anthropic.HUMAN_PROMPT} How does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
|
|
278
|
-
max_tokens_to_sample: 300,
|
|
279
|
-
model: 'claude-1',
|
|
280
|
-
},
|
|
281
|
-
{
|
|
282
|
-
baseURL: 'http://localhost:8080/test-api',
|
|
283
|
-
httpAgent: new http.Agent({ keepAlive: false }),
|
|
284
|
-
},
|
|
285
|
-
);
|
|
286
|
-
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
## Status
|
|
290
|
-
|
|
291
|
-
This package is in beta. Its internals and interfaces are not stable
|
|
292
|
-
and subject to change without a major semver bump;
|
|
293
|
-
please reach out if you rely on any undocumented behavior.
|
|
294
|
-
|
|
295
|
-
We are keen for your feedback; please open an [issue](https://www.github.com/anthropics/anthropic-sdk-typescript/issues) with questions, bugs, or suggestions.
|
|
296
|
-
|
|
297
|
-
## Requirements
|
|
298
|
-
|
|
299
|
-
The following runtimes are supported:
|
|
300
|
-
|
|
301
|
-
- Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
|
|
302
|
-
- Deno v1.28.0 or higher (experimental).
|
|
303
|
-
Use `import Anthropic from "npm:@anthropic-ai/sdk"`.
|
|
304
|
-
|
|
305
|
-
If you are interested in other runtime environments, please open or upvote an issue on GitHub.
|
package/_shims/fetch.node.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import fetch, { Request, Response, Headers } from 'node-fetch';
|
|
6
|
-
import type { RequestInfo, RequestInit, BodyInit } from 'node-fetch';
|
|
7
|
-
|
|
8
|
-
export { fetch, Request, Response, Headers };
|
|
9
|
-
export type { RequestInfo, RequestInit, BodyInit };
|
|
10
|
-
|
|
11
|
-
export const isPolyfilled = true;
|
package/_shims/fetch.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type * as nf from 'node-fetch';
|
|
6
|
-
|
|
7
|
-
const _fetch: typeof nf.default =
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
fetch as any;
|
|
10
|
-
type _fetch = typeof nf.default;
|
|
11
|
-
const _Request: typeof nf.Request =
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
Request as any;
|
|
14
|
-
type _Request = nf.Request;
|
|
15
|
-
const _Response: typeof nf.Response =
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
Response as any;
|
|
18
|
-
type _Response = nf.Response;
|
|
19
|
-
const _Headers: typeof nf.Headers =
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
Headers as any;
|
|
22
|
-
type _Headers = nf.Headers;
|
|
23
|
-
|
|
24
|
-
export const isPolyfilled = false;
|
|
25
|
-
|
|
26
|
-
export { _fetch as fetch, _Request as Request, _Response as Response, _Headers as Headers };
|
|
27
|
-
|
|
28
|
-
type _RequestInfo = nf.RequestInfo;
|
|
29
|
-
type _RequestInit = nf.RequestInit;
|
|
30
|
-
type _BodyInit = nf.BodyInit;
|
|
31
|
-
|
|
32
|
-
export type { _RequestInit as RequestInit, _RequestInfo as RequestInfo, _BodyInit as BodyInit };
|
package/_shims/formdata.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type * as fd from 'formdata-node';
|
|
6
|
-
|
|
7
|
-
const _FormData: typeof fd.FormData =
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
FormData as any;
|
|
10
|
-
type _FormData = fd.FormData;
|
|
11
|
-
const _File: typeof fd.File =
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
File as any;
|
|
14
|
-
type _File = fd.File;
|
|
15
|
-
const _Blob: typeof fd.Blob =
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
Blob as any;
|
|
18
|
-
type _Blob = fd.Blob;
|
|
19
|
-
|
|
20
|
-
export const isPolyfilled = false;
|
|
21
|
-
|
|
22
|
-
export { _FormData as FormData, _File as File, _Blob as Blob };
|