Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: 0.12.4 #262

Closed
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
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.12.3"
".": "0.12.4"
}
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 0.12.4 (2024-01-23)

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

### Chores

* **internal:** add internal helpers & improve build scripts ([#261](https://github.com/anthropics/anthropic-sdk-typescript/issues/261)) ([4c1504a](https://github.com/anthropics/anthropic-sdk-typescript/commit/4c1504abc7eb8685a8409c4a19dc46d83ea26392))
* **internal:** minor streaming updates ([#264](https://github.com/anthropics/anthropic-sdk-typescript/issues/264)) ([d4414ff](https://github.com/anthropics/anthropic-sdk-typescript/commit/d4414ffeafbc47769b91c4b2681f130b46d1a7c1))
* **internal:** update resource client type ([#263](https://github.com/anthropics/anthropic-sdk-typescript/issues/263)) ([bc4f115](https://github.com/anthropics/anthropic-sdk-typescript/commit/bc4f115900cbeba1ff09d6f3cec79e639a8fda5e))

## 0.12.3 (2024-01-19)

Full Changelog: [v0.12.2...v0.12.3](https://github.com/anthropics/anthropic-sdk-typescript/compare/v0.12.2...v0.12.3)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@anthropic-ai/sdk",
"version": "0.12.3",
"version": "0.12.4",
"description": "The official TypeScript library for the Anthropic API",
"author": "Anthropic <support@anthropic.com>",
"types": "dist/index.d.ts",
Expand Down
6 changes: 5 additions & 1 deletion scripts/fix-index-exports.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const fs = require('fs');
const path = require('path');

const indexJs = path.resolve(__dirname, '..', 'dist', 'index.js');
const indexJs =
process.env['DIST_PATH'] ?
path.resolve(process.env['DIST_PATH'], 'index.js')
: path.resolve(__dirname, '..', 'dist', 'index.js');

let before = fs.readFileSync(indexJs, 'utf8');
let after = before.replace(
/^\s*exports\.default\s*=\s*(\w+)/m,
Expand Down
2 changes: 1 addition & 1 deletion scripts/make-dist-package-json.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pkgJson = require('../package.json');
const pkgJson = require(process.env['PKG_JSON_PATH'] || '../package.json');

function processExportMap(m) {
for (const key in m) {
Expand Down
11 changes: 8 additions & 3 deletions scripts/postprocess-files.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ const fs = require('fs');
const path = require('path');
const { parse } = require('@typescript-eslint/parser');

const distDir = path.resolve(__dirname, '..', 'dist');
const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? '@anthropic-ai/sdk/'

const distDir =
process.env['DIST_PATH'] ?
path.resolve(process.env['DIST_PATH'])
: path.resolve(__dirname, '..', 'dist');
const distSrcDir = path.join(distDir, 'src');

/**
Expand Down Expand Up @@ -105,11 +110,11 @@ async function postprocess() {

let transformed = mapModulePaths(code, (importPath) => {
if (file.startsWith(distSrcDir)) {
if (importPath.startsWith('@anthropic-ai/sdk/')) {
if (importPath.startsWith(pkgImportPath)) {
// convert self-references in dist/src to relative paths
let relativePath = path.relative(
path.dirname(file),
path.join(distSrcDir, importPath.substring('@anthropic-ai/sdk/'.length)),
path.join(distSrcDir, importPath.substring(pkgImportPath.length)),
);
if (!relativePath.startsWith('.')) relativePath = `./${relativePath}`;
return relativePath;
Expand Down
18 changes: 18 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {

// Note: there is an invariant here that isn't represented in the type system
// that if you set `stream: true` the response type must also be `Stream<T>`

if (props.options.__streamClass) {
return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;
}

return Stream.fromSSEResponse(response, props.controller) as any;
}

Expand Down Expand Up @@ -342,6 +347,11 @@ export abstract class APIClient {
return reqHeaders;
}

/**
* Used as a callback for mutating the given `FinalRequestOptions` object.
*/
protected async prepareOptions(options: FinalRequestOptions): Promise<void> {}

/**
* Used as a callback for mutating the given `RequestInit` object.
*
Expand Down Expand Up @@ -387,6 +397,8 @@ export abstract class APIClient {
retriesRemaining = options.maxRetries ?? this.maxRetries;
}

await this.prepareOptions(options);

const { req, url, timeout } = this.buildRequest(options);

await this.prepareRequest(req, { url, options });
Expand Down Expand Up @@ -736,6 +748,7 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
idempotencyKey?: string;

__binaryResponse?: boolean | undefined;
__streamClass?: typeof Stream;
};

// This is required so that we can determine if a given object matches the RequestOptions
Expand All @@ -756,6 +769,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
idempotencyKey: true,

__binaryResponse: true,
__streamClass: true,
};

export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
Expand Down Expand Up @@ -1139,3 +1153,7 @@ export const toBase64 = (str: string | null | undefined): string => {

throw new AnthropicError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');
};

export function isObj(obj: unknown): obj is Record<string, unknown> {
return obj != null && typeof obj === 'object' && !Array.isArray(obj);
}
6 changes: 3 additions & 3 deletions src/resource.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// File generated from our OpenAPI spec by Stainless.

import type { Anthropic } from './index';
import * as Core from '@anthropic-ai/sdk/core';

export class APIResource {
protected _client: Anthropic;
protected _client: Core.APIClient;

constructor(client: Anthropic) {
constructor(client: Core.APIClient) {
this._client = client;
}
}
2 changes: 1 addition & 1 deletion src/resources/beta/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Messages extends APIResource {
* Create a Message stream
*/
stream(body: MessageStreamParams, options?: Core.RequestOptions): MessageStream {
return MessageStream.createMessage(this._client.beta.messages, body, options);
return MessageStream.createMessage(this, body, options);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { AnthropicError } from './error';
import { safeJSON, createResponseHeaders } from '@anthropic-ai/sdk/core';
import { APIError } from '@anthropic-ai/sdk/error';

type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
export type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;

type ServerSentEvent = {
export type ServerSentEvent = {
event: string | null;
data: string;
raw: string[];
Expand Down Expand Up @@ -396,7 +396,7 @@ function partition(str: string, delimiter: string): [string, string, string] {
*
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
*/
function readableStreamAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
export function readableStreamAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
if (stream[Symbol.asyncIterator]) return stream;

const reader = stream.getReader();
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.12.3'; // x-release-please-version
export const VERSION = '0.12.4'; // x-release-please-version
Loading