Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions .github/workflows/open-release-prs.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.6.3"
".": "0.6.4"
}
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Changelog

## 0.6.4 (2023-09-08)

Full Changelog: [v0.6.3...v0.6.4](https://github.com/anthropics/anthropic-sdk-typescript/compare/v0.6.3...v0.6.4)

### Features

* **package:** add Bun export map ([#139](https://github.com/anthropics/anthropic-sdk-typescript/issues/139)) ([ba3310d](https://github.com/anthropics/anthropic-sdk-typescript/commit/ba3310d903cd5fda91168266335f4e445e60cad4))


### Bug Fixes

* **client:** fix TS errors that appear when users Go to Source in VSCode ([#142](https://github.com/anthropics/anthropic-sdk-typescript/issues/142)) ([f7bfbea](https://github.com/anthropics/anthropic-sdk-typescript/commit/f7bfbeaa54d364201bbe5cddf3132875ae2a3ccf))
* **client:** handle case where the client is instantiated with a undefined baseURL ([#143](https://github.com/anthropics/anthropic-sdk-typescript/issues/143)) ([10e5203](https://github.com/anthropics/anthropic-sdk-typescript/commit/10e52034990d90dcdaf26672ea384545b88ddf35))
* **client:** use explicit file extensions in _shims imports ([#141](https://github.com/anthropics/anthropic-sdk-typescript/issues/141)) ([10fd687](https://github.com/anthropics/anthropic-sdk-typescript/commit/10fd68742a202c5c0a8b520db190c239dce9b676))
* fix module not found errors in Vercel edge ([#148](https://github.com/anthropics/anthropic-sdk-typescript/issues/148)) ([72e51a1](https://github.com/anthropics/anthropic-sdk-typescript/commit/72e51a170855281a8d099b00c6fb1e9ccb276212))
* **readme:** update link to api.md to use the correct branch ([#145](https://github.com/anthropics/anthropic-sdk-typescript/issues/145)) ([5db78ed](https://github.com/anthropics/anthropic-sdk-typescript/commit/5db78edec4826f86b2fc21ee3f470b49a4987029))


### Chores

* **internal:** export helper from core ([#147](https://github.com/anthropics/anthropic-sdk-typescript/issues/147)) ([7e79de1](https://github.com/anthropics/anthropic-sdk-typescript/commit/7e79de14edeab8110d740e996653e9f9cc2299a4))


### Documentation

* **readme:** add link to api.md ([#144](https://github.com/anthropics/anthropic-sdk-typescript/issues/144)) ([716c9f0](https://github.com/anthropics/anthropic-sdk-typescript/commit/716c9f0714c0e9c26cb6cdcb007457aff1284cf4))

## 0.6.3 (2023-08-28)

Full Changelog: [v0.6.2...v0.6.3](https://github.com/anthropics/anthropic-sdk-typescript/compare/v0.6.2...v0.6.3)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ yarn add @anthropic-ai/sdk

## Usage

The full API of this library can be found in [api.md](https://www.github.com/anthropics/anthropic-sdk-typescript/blob/main/api.md).

```js
import Anthropic from '@anthropic-ai/sdk';

Expand Down Expand Up @@ -330,6 +332,8 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ant

## Requirements

TypeScript >= 4.5 is supported.

The following runtimes are supported:

- Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
Expand Down
11 changes: 6 additions & 5 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rm -rf dist; mkdir dist
# Copy src to dist/src and build from dist/src into dist, so that
# the source map for index.js.map will refer to ./src/index.ts etc
cp -rp src README.md dist
rm dist/src/_shims/*-deno.*
for file in LICENSE CHANGELOG.md; do
if [ -e "${file}" ]; then cp "${file}" dist; fi
done
Expand All @@ -37,14 +38,14 @@ node scripts/fix-index-exports.cjs
# index.d.mts the default import will work (even though both files have
# the same export default statement)
cp dist/index.d.ts dist/index.d.mts

SED=(sed -i)
if [[ "$OSTYPE" == "darwin"* ]]; then SED=(sed -i ''); fi
cp tsconfig.dist-src.json dist/src/tsconfig.json

# strip out lib="dom" and types="node" references; these are needed at build time,
# but would pollute the user's TS environment
REFERENCE_SUBS='s/^ *\/\/\/ *<reference *lib="dom".*//g;s/^ *\/\/\/ *<reference *types="node".*//g'
find dist -type f -exec "${SED[@]}" "${REFERENCE_SUBS}" {} +
find dist -type f -exec node scripts/remove-triple-slash-references.js {} +
# strip out `unknown extends RequestInit ? never :` from dist/src/_shims;
# these cause problems when viewing the .ts source files in go to definition
find dist/src/_shims -type f -exec node scripts/replace-shim-guards.js {} +

npm exec prettier -- --loglevel=warn --write .

Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
testEnvironment: 'node',
moduleNameMapper: {
'^@anthropic-ai/sdk$': '<rootDir>/src/index.ts',
'^@anthropic-ai/sdk/_shims/(.*)$': '<rootDir>/src/_shims/$1.node',
'^@anthropic-ai/sdk/_shims/(.*)$': '<rootDir>/src/_shims/$1-node',
'^@anthropic-ai/sdk/(.*)$': '<rootDir>/src/$1',
},
modulePathIgnorePatterns: ['<rootDir>/ecosystem-tests/', '<rootDir>/dist/', '<rootDir>/deno_tests/'],
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@anthropic-ai/sdk",
"version": "0.6.3",
"version": "0.6.4",
"description": "Client library for the Anthropic API",
"author": "Anthropic <[email protected]>",
"types": "dist/index.d.ts",
Expand All @@ -16,6 +16,11 @@
"require": "./dist/_shims/*.js",
"default": "./dist/_shims/*.mjs"
},
"bun": {
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
"default": "./dist/_shims/*.mjs"
},
"browser": {
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
Expand All @@ -32,9 +37,9 @@
"default": "./dist/_shims/*.mjs"
},
"node": {
"types": "./dist/_shims/*.node.d.ts",
"require": "./dist/_shims/*.node.js",
"default": "./dist/_shims/*.node.mjs"
"types": "./dist/_shims/*-node.d.ts",
"require": "./dist/_shims/*-node.js",
"default": "./dist/_shims/*-node.mjs"
},
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
Expand Down
11 changes: 11 additions & 0 deletions scripts/remove-triple-slash-references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// strip out lib="dom" and types="node" references; these are needed at build time,
// but would pollute the user's TS environment
const fs = require('fs');
for (const file of process.argv.slice(2)) {
const before = fs.readFileSync(file, 'utf8');
const after = before.replace(/^ *\/\/\/ *<reference +(lib="dom"|types="node").*?\n/gm, '');
if (after !== before) {
fs.writeFileSync(file, after, 'utf8');
console.error('wrote', file);
}
}
7 changes: 5 additions & 2 deletions scripts/replace-self-referencing-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
Object.defineProperty(exports, '__esModule', { value: true });

const path = require('path');
const distSrcDir = path.resolve(__dirname, '..', 'dist', 'src');
const distDir = path.resolve(__dirname, '..', 'dist');
const distSrcDir = path.join(distDir, 'src');

function replaceSelfReferencingImports({ orig, file, config }) {
// replace self-referencing imports in source files to reduce errors users will
// see if they go to definition
if (!file.startsWith(distSrcDir)) return orig;
if (!file.startsWith(distDir)) return orig;

return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => {
if (!importPath.startsWith('@anthropic-ai/sdk/')) return match;
if (!file.startsWith(distSrcDir)) return match;
let relativePath = path.relative(
path.dirname(file),
path.join(distSrcDir, importPath.substring('@anthropic-ai/sdk/'.length)),
Expand Down
14 changes: 14 additions & 0 deletions scripts/replace-shim-guards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// strip out `unknown extends RequestInit ? never :` from dist/src/_shims;
// these cause problems when viewing the .ts source files in go to definition
const fs = require('fs');
for (const file of process.argv.slice(2)) {
const before = fs.readFileSync(file, 'utf8');
const after = before.replace(
new RegExp('unknown extends (typeof )?\\S+ \\? \\S+ :\\s*'.replace(/\s+/, '\\s+'), 'gm'),
'',
);
if (after !== before) {
fs.writeFileSync(file, after, 'utf8');
console.error('wrote', file);
}
}
16 changes: 16 additions & 0 deletions scripts/resolve-full-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });

const path = require('path');

// tsc-alias --resolveFullPaths is buggy, it replaces 'formdata-node'
// with 'formdata-node.js' because we have a file with that name
function resolveFullPaths({ orig, file, config }) {
return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => {
if (!importPath.startsWith('.')) return match;
const { dir, name } = path.parse(importPath);
const ext = /\.mjs$/.test(file) ? '.mjs' : '.js';
return JSON.stringify(`${dir}/${name}${ext}`);
});
}
exports.default = resolveFullPaths;
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/_shims/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*
* This is a stub for non-node environments.
* In node environments, it gets replaced agent.node.ts by the package export map
* In node environments, it gets replaced agent-node.ts by the package export map
*/

export type Agent = any;
Expand Down
4 changes: 4 additions & 0 deletions src/_shims/fetch.deno.ts → src/_shims/fetch-deno.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const _fetch = fetch;
type _fetch = typeof fetch;
const _Request = Request;
type _Request = Request;
type _RequestInfo = RequestInfo;
type _RequestInit = RequestInit;
const _Response = Response;
type _Response = Response;
type _ResponseInit = ResponseInit;
type _BodyInit = BodyInit;
const _Headers = Headers;
type _Headers = Headers;
type _HeadersInit = HeadersInit;

export const isPolyfilled = false;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { fileFromPath as _fileFromPath } from 'formdata-node/file-from-path';
import type { File, FilePropertyBag } from './formdata.node';
import type { File, FilePropertyBag } from './form-data-node';

export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;

Expand Down
4 changes: 2 additions & 2 deletions src/_shims/fileFromPath.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*
* This is a stub that gets replaced by fileFromPath.node.js for node environments
* This is a stub that gets replaced by fileFromPath-node.js for node environments
* in the package export map
*/

import type { FilePropertyBag, File } from './formdata';
import type { FilePropertyBag, File } from './form-data';

export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ type _BlobPropertyBag = BlobPropertyBag;
type _FilePropertyBag = FilePropertyBag;

const _FormData = FormData;
type _FormData = FormData;
const _File = File;
type _File = File;
const _Blob = Blob;
type _Blob = Blob;

export const isPolyfilled = false;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/

import { FormData } from './formdata.node';
import { FormData } from './form-data-node';
import type { RequestOptions } from '../core';
import { Readable } from 'node:stream';
import { FormDataEncoder } from 'form-data-encoder';
Expand Down
2 changes: 1 addition & 1 deletion src/_shims/getMultipartRequestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/

import { FormData } from './formdata';
import { FormData } from './form-data';
import type { RequestOptions } from '../core';
import { MultipartBody } from '../uploads';

Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
// TODO handle blob, arraybuffer, other content types, etc.
const text = await response.text();
debug('response', response.status, response.url, response.headers, text);
return text as T;
return text as any as T;
}

/**
Expand Down Expand Up @@ -313,7 +313,8 @@ export abstract class APIClient {
protected parseHeaders(headers: HeadersInit | null | undefined): Record<string, string> {
return (
!headers ? {}
: Symbol.iterator in headers ? Object.fromEntries(Array.from(headers).map((header) => [...header]))
: Symbol.iterator in headers ?
Object.fromEntries(Array.from(headers as Iterable<string[]>).map((header) => [...header]))
: { ...headers }
);
}
Expand Down Expand Up @@ -397,7 +398,7 @@ export abstract class APIClient {
return new PagePromise<PageClass, Item>(this, request, Page);
}

buildURL<Req>(path: string, query: Req | undefined): string {
buildURL<Req extends Record<string, unknown>>(path: string, query: Req | null | undefined): string {
const url =
isAbsoluteURL(path) ?
new URL(path)
Expand Down Expand Up @@ -900,7 +901,7 @@ const isAbsoluteURL = (url: string): boolean => {
return startsWithSchemeRegexp.test(url);
};

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

const validatePositiveInteger = (name: string, n: unknown): number => {
if (typeof n !== 'number' || !Number.isInteger(n)) {
Expand Down
4 changes: 2 additions & 2 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ export class APIConnectionError extends APIError {
}

export class APIConnectionTimeoutError extends APIConnectionError {
constructor() {
super({ message: 'Request timed out.' });
constructor({ message }: { message?: string } = {}) {
super({ message: message ?? 'Request timed out.' });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ export class Anthropic extends Core.APIClient {
const options: ClientOptions = {
apiKey,
authToken,
baseURL: `https://api.anthropic.com`,
...opts,
baseURL: opts.baseURL ?? `https://api.anthropic.com`,
};

super({
Expand Down
2 changes: 1 addition & 1 deletion src/uploads.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type RequestOptions } from './core';
import { type Readable } from '@anthropic-ai/sdk/_shims/node-readable';
import { type BodyInit } from '@anthropic-ai/sdk/_shims/fetch';
import { FormData, File, type Blob, type FilePropertyBag } from '@anthropic-ai/sdk/_shims/formdata';
import { FormData, File, type Blob, type FilePropertyBag } from '@anthropic-ai/sdk/_shims/form-data';
import { getMultipartRequestOptions } from '@anthropic-ai/sdk/_shims/getMultipartRequestOptions';
import { fileFromPath } from '@anthropic-ai/sdk/_shims/fileFromPath';
import { type FsReadStream, isFsReadStream } from '@anthropic-ai/sdk/_shims/node-readable';
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = '0.6.3'; // x-release-please-version
export const VERSION = '0.6.4'; // x-release-please-version
2 changes: 1 addition & 1 deletion tests/form.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { multipartFormRequestOptions, createForm } from '@anthropic-ai/sdk/core';
import { Blob } from '@anthropic-ai/sdk/_shims/formdata';
import { Blob } from '@anthropic-ai/sdk/_shims/form-data';
import { toFile } from '@anthropic-ai/sdk';

describe('form data validation', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/uploads.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import { toFile, type ResponseLike } from '@anthropic-ai/sdk/uploads';
import { File } from '@anthropic-ai/sdk/_shims/formdata';
import { File } from '@anthropic-ai/sdk/_shims/form-data';

class MyClass {
name: string = 'foo';
Expand Down
Loading