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

chore: release main #431

Merged
merged 8 commits into from
May 30, 2024
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
6 changes: 3 additions & 3 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
".": "0.21.1",
"packages/vertex-sdk": "0.3.7",
"packages/bedrock-sdk": "0.9.8"
".": "0.22.0",
"packages/vertex-sdk": "0.4.0",
"packages/bedrock-sdk": "0.10.0"
}
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 3
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-11e9f674e1fe0eb85a713c2852de663254d4e9254dea701008dcd605a04987d5.yml
configured_endpoints: 2
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-4f6df1026ffeed840bbfada906ac51144508d1e1b099084c593aa9bac97a3362.yml
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## 0.22.0 (2024-05-30)

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

### Features

* **api/types:** add stream event type aliases with a Raw prefix ([#428](https://github.com/anthropics/anthropic-sdk-typescript/issues/428)) ([1e367e4](https://github.com/anthropics/anthropic-sdk-typescript/commit/1e367e4020fa4691c565c89bdfba40c2f6060871))
* **api:** tool use is GA and available on 3P ([#429](https://github.com/anthropics/anthropic-sdk-typescript/issues/429)) ([2decf85](https://github.com/anthropics/anthropic-sdk-typescript/commit/2decf85e7471932dad98c21d4ed2d476ab1588a6))
* **bedrock:** support tools ([91fc61a](https://github.com/anthropics/anthropic-sdk-typescript/commit/91fc61ae7246705d26e96a95dae38b46e9ad9290))
* **streaming:** add tools support ([4c83bb1](https://github.com/anthropics/anthropic-sdk-typescript/commit/4c83bb111735cd513c09d5ed57a5cb0888534afd))
* **vertex:** support tools ([acf0aa7](https://github.com/anthropics/anthropic-sdk-typescript/commit/acf0aa7571425c8582740616e24883c2ec65218b))


### Documentation

* **helpers:** mention inputJson event ([0ef0e39](https://github.com/anthropics/anthropic-sdk-typescript/commit/0ef0e39a870541bbe800b03c1bdcf88eb6e1350c))
* **readme:** add bundle size badge ([#426](https://github.com/anthropics/anthropic-sdk-typescript/issues/426)) ([bf7c1fd](https://github.com/anthropics/anthropic-sdk-typescript/commit/bf7c1fdaf3476d5c43079e8a0789ed0dd0c807a6))

## 0.21.1 (2024-05-21)

Full Changelog: [sdk-v0.21.0...sdk-v0.21.1](https://github.com/anthropics/anthropic-sdk-typescript/compare/sdk-v0.21.0...sdk-v0.21.1)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Anthropic TypeScript API Library

[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/sdk.svg)](https://npmjs.org/package/@anthropic-ai/sdk)
[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/sdk.svg)](https://npmjs.org/package/@anthropic-ai/sdk) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@anthropic-ai/sdk)

This library provides convenient access to the Anthropic REST API from server-side TypeScript or JavaScript.

Expand Down
37 changes: 12 additions & 25 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,31 @@ Types:
- <code><a href="./src/resources/messages.ts">ContentBlockStartEvent</a></code>
- <code><a href="./src/resources/messages.ts">ContentBlockStopEvent</a></code>
- <code><a href="./src/resources/messages.ts">ImageBlockParam</a></code>
- <code><a href="./src/resources/messages.ts">InputJsonDelta</a></code>
- <code><a href="./src/resources/messages.ts">Message</a></code>
- <code><a href="./src/resources/messages.ts">MessageDeltaEvent</a></code>
- <code><a href="./src/resources/messages.ts">MessageDeltaUsage</a></code>
- <code><a href="./src/resources/messages.ts">MessageParam</a></code>
- <code><a href="./src/resources/messages.ts">MessageStartEvent</a></code>
- <code><a href="./src/resources/messages.ts">MessageStopEvent</a></code>
- <code><a href="./src/resources/messages.ts">MessageStreamEvent</a></code>
- <code><a href="./src/resources/messages.ts">RawContentBlockDeltaEvent</a></code>
- <code><a href="./src/resources/messages.ts">RawContentBlockStartEvent</a></code>
- <code><a href="./src/resources/messages.ts">RawContentBlockStopEvent</a></code>
- <code><a href="./src/resources/messages.ts">RawMessageDeltaEvent</a></code>
- <code><a href="./src/resources/messages.ts">RawMessageStartEvent</a></code>
- <code><a href="./src/resources/messages.ts">RawMessageStopEvent</a></code>
- <code><a href="./src/resources/messages.ts">RawMessageStreamEvent</a></code>
- <code><a href="./src/resources/messages.ts">TextBlock</a></code>
- <code><a href="./src/resources/messages.ts">TextBlockParam</a></code>
- <code><a href="./src/resources/messages.ts">TextDelta</a></code>
- <code><a href="./src/resources/messages.ts">Tool</a></code>
- <code><a href="./src/resources/messages.ts">ToolResultBlockParam</a></code>
- <code><a href="./src/resources/messages.ts">ToolUseBlock</a></code>
- <code><a href="./src/resources/messages.ts">ToolUseBlockParam</a></code>
- <code><a href="./src/resources/messages.ts">Usage</a></code>

Methods:

- <code title="post /v1/messages">client.messages.<a href="./src/resources/messages.ts">create</a>({ ...params }) -> Message</code>
- <code>client.messages.<a href="./src/resources/messages.ts">stream</a>(body, options?) -> MessageStream</code>

# Beta

## Tools

### Messages

Types:

- <code><a href="./src/resources/beta/tools/messages.ts">InputJsonDelta</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">Tool</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">ToolResultBlockParam</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">ToolUseBlock</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">ToolUseBlockParam</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">ToolsBetaContentBlock</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">ToolsBetaContentBlockDeltaEvent</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">ToolsBetaContentBlockStartEvent</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">ToolsBetaMessage</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">ToolsBetaMessageParam</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">ToolsBetaMessageStreamEvent</a></code>

Methods:

- <code title="post /v1/messages?beta=tools">client.beta.tools.messages.<a href="./src/resources/beta/tools/messages.ts">create</a>({ ...params }) -> ToolsBetaMessage</code>
- <code>client.beta.tools.messages.<a href="./src/resources/beta/tools/messages.ts">stream</a>(body, options?) -> ToolsBetaMessageStream</code>
2 changes: 1 addition & 1 deletion examples/tools-streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { inspect } from 'util';
const client = new Anthropic();

async function main() {
const stream = client.beta.tools.messages
const stream = client.messages
.stream({
messages: [
{
Expand Down
10 changes: 5 additions & 5 deletions examples/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import assert from 'node:assert';
const client = new Anthropic(); // gets API Key from environment variable ANTHROPIC_API_KEY

async function main() {
const userMessage: Anthropic.Beta.Tools.ToolsBetaMessageParam = {
const userMessage: Anthropic.MessageParam = {
role: 'user',
content: 'What is the weather in SF?',
};
const tools: Anthropic.Beta.Tools.Tool[] = [
const tools: Anthropic.Tool[] = [
{
name: 'get_weather',
description: 'Get the weather for a specific location',
Expand All @@ -21,7 +21,7 @@ async function main() {
},
];

const message = await client.beta.tools.messages.create({
const message = await client.messages.create({
model: 'claude-3-opus-20240229',
max_tokens: 1024,
messages: [userMessage],
Expand All @@ -33,11 +33,11 @@ async function main() {
assert(message.stop_reason === 'tool_use');

const tool = message.content.find(
(content): content is Anthropic.Beta.Tools.ToolUseBlock => content.type === 'tool_use',
(content): content is Anthropic.ToolUseBlock => content.type === 'tool_use',
);
assert(tool);

const result = await client.beta.tools.messages.create({
const result = await client.messages.create({
model: 'claude-3-opus-20240229',
max_tokens: 1024,
messages: [
Expand Down
4 changes: 4 additions & 0 deletions helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ returns an accumulated `Message` which is progressively built-up over events.

The event fired when a text delta is sent by the API. The second parameter returns a `textSnapshot`.

#### `.on('inputJson', (patialJson: string, jsonSnapshot: unknown) => …)`

The event fired when a json delta is sent by the API. The second parameter returns a `jsonSnapshot`.

#### `.on('message', (message: Message) => …)`

The event fired when a message is done being streamed by the API. Corresponds to the `message_stop` SSE event.
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.21.1",
"version": "0.22.0",
"description": "The official TypeScript library for the Anthropic API",
"author": "Anthropic <[email protected]>",
"types": "dist/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions packages/bedrock-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.10.0 (2024-05-30)

Full Changelog: [bedrock-sdk-v0.9.8...bedrock-sdk-v0.10.0](https://github.com/anthropics/anthropic-sdk-typescript/compare/bedrock-sdk-v0.9.8...bedrock-sdk-v0.10.0)

### Features

* **bedrock:** support tools ([91fc61a](https://github.com/anthropics/anthropic-sdk-typescript/commit/91fc61ae7246705d26e96a95dae38b46e9ad9290))

## 0.9.8 (2024-05-16)

Full Changelog: [bedrock-sdk-v0.9.7...bedrock-sdk-v0.9.8](https://github.com/anthropics/anthropic-sdk-typescript/compare/bedrock-sdk-v0.9.7...bedrock-sdk-v0.9.8)
Expand Down
1 change: 1 addition & 0 deletions packages/bedrock-sdk/build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ npm exec tsc-multi
# No way to get that from index.ts because it would cause compile errors
# when building .mjs
DIST_PATH=./dist node ../../scripts/utils/fix-index-exports.cjs

# with "moduleResolution": "nodenext", if ESM resolves to index.d.ts,
# it'll have TS errors on the default import. But if it resolves to
# index.d.mts the default import will work (even though both files have
Expand Down
2 changes: 1 addition & 1 deletion packages/bedrock-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@anthropic-ai/bedrock-sdk",
"version": "0.9.8",
"version": "0.10.0",
"description": "The official TypeScript library for the Anthropic Bedrock API",
"author": "Anthropic <[email protected]>",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/bedrock-sdk/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

"@anthropic-ai/sdk@file:../../dist":
# x-release-please-start-version
version "0.21.1"
version "0.22.0"
# x-release-please-end-version
dependencies:
"@types/node" "^18.11.18"
Expand Down
8 changes: 8 additions & 0 deletions packages/vertex-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.4.0 (2024-05-30)

Full Changelog: [vertex-sdk-v0.3.7...vertex-sdk-v0.4.0](https://github.com/anthropics/anthropic-sdk-typescript/compare/vertex-sdk-v0.3.7...vertex-sdk-v0.4.0)

### Features

* **vertex:** support tools ([acf0aa7](https://github.com/anthropics/anthropic-sdk-typescript/commit/acf0aa7571425c8582740616e24883c2ec65218b))

## 0.3.7 (2024-05-16)

Full Changelog: [vertex-sdk-v0.3.6...vertex-sdk-v0.3.7](https://github.com/anthropics/anthropic-sdk-typescript/compare/vertex-sdk-v0.3.6...vertex-sdk-v0.3.7)
Expand Down
1 change: 1 addition & 0 deletions packages/vertex-sdk/build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ npm exec tsc-multi
# No way to get that from index.ts because it would cause compile errors
# when building .mjs
DIST_PATH=./dist node ../../scripts/utils/fix-index-exports.cjs

# with "moduleResolution": "nodenext", if ESM resolves to index.d.ts,
# it'll have TS errors on the default import. But if it resolves to
# index.d.mts the default import will work (even though both files have
Expand Down
2 changes: 1 addition & 1 deletion packages/vertex-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@anthropic-ai/vertex-sdk",
"version": "0.3.7",
"version": "0.4.0",
"description": "The official TypeScript library for the Anthropic Vertex API",
"author": "Anthropic <[email protected]>",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/vertex-sdk/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

"@anthropic-ai/sdk@file:../../dist":
# x-release-please-start-version
version "0.21.1"
version "0.22.0"
# x-release-please-end-version
dependencies:
"@types/node" "^18.11.18"
Expand Down
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export class Anthropic extends Core.APIClient {

completions: API.Completions = new API.Completions(this);
messages: API.Messages = new API.Messages(this);
beta: API.Beta = new API.Beta(this);

protected override defaultQuery(): Core.DefaultQuery | undefined {
return this._options.defaultQuery;
Expand Down Expand Up @@ -242,23 +241,33 @@ export namespace Anthropic {
export import ContentBlockStartEvent = API.ContentBlockStartEvent;
export import ContentBlockStopEvent = API.ContentBlockStopEvent;
export import ImageBlockParam = API.ImageBlockParam;
export import InputJsonDelta = API.InputJsonDelta;
export import Message = API.Message;
export import MessageDeltaEvent = API.MessageDeltaEvent;
export import MessageDeltaUsage = API.MessageDeltaUsage;
export import MessageParam = API.MessageParam;
export import MessageStartEvent = API.MessageStartEvent;
export import MessageStopEvent = API.MessageStopEvent;
export import MessageStreamEvent = API.MessageStreamEvent;
export import RawContentBlockDeltaEvent = API.RawContentBlockDeltaEvent;
export import RawContentBlockStartEvent = API.RawContentBlockStartEvent;
export import RawContentBlockStopEvent = API.RawContentBlockStopEvent;
export import RawMessageDeltaEvent = API.RawMessageDeltaEvent;
export import RawMessageStartEvent = API.RawMessageStartEvent;
export import RawMessageStopEvent = API.RawMessageStopEvent;
export import RawMessageStreamEvent = API.RawMessageStreamEvent;
export import TextBlock = API.TextBlock;
export import TextBlockParam = API.TextBlockParam;
export import TextDelta = API.TextDelta;
export import Tool = API.Tool;
export import ToolResultBlockParam = API.ToolResultBlockParam;
export import ToolUseBlock = API.ToolUseBlock;
export import ToolUseBlockParam = API.ToolUseBlockParam;
export import Usage = API.Usage;
export import MessageCreateParams = API.MessageCreateParams;
export import MessageCreateParamsNonStreaming = API.MessageCreateParamsNonStreaming;
export import MessageCreateParamsStreaming = API.MessageCreateParamsStreaming;
export import MessageStreamParams = API.MessageStreamParams;

export import Beta = API.Beta;
}

export default Anthropic;
36 changes: 31 additions & 5 deletions src/lib/MessageStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import {
MessageStreamEvent,
MessageParam,
MessageCreateParams,
MessageStreamParams,
MessageCreateParamsBase,
} from '@anthropic-ai/sdk/resources/messages';
import { type ReadableStream } from '@anthropic-ai/sdk/_shims/index';
import { Stream } from '@anthropic-ai/sdk/streaming';
import { TextBlock } from '@anthropic-ai/sdk/resources';
import { partialParse } from '../_vendor/partial-json-parser/parser';

export interface MessageStreamEvents {
connect: () => void;
streamEvent: (event: MessageStreamEvent, snapshot: Message) => void;
text: (textDelta: string, textSnapshot: string) => void;
inputJson: (partialJson: string, jsonSnapshot: unknown) => void;
message: (message: Message) => void;
contentBlock: (content: ContentBlock) => void;
finalMessage: (message: Message) => void;
Expand All @@ -29,6 +32,8 @@ type MessageStreamEventListeners<Event extends keyof MessageStreamEvents> = {
once?: boolean;
}[];

const JSON_BUF_PROPERTY = '__json_buf';

export class MessageStream implements AsyncIterable<MessageStreamEvent> {
messages: MessageParam[] = [];
receivedMessages: Message[] = [];
Expand Down Expand Up @@ -85,7 +90,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {

static createMessage(
messages: Messages,
params: MessageStreamParams,
params: MessageCreateParamsBase,
options?: Core.RequestOptions,
): MessageStream {
const runner = new MessageStream();
Expand Down Expand Up @@ -264,7 +269,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
}
const textBlocks = this.receivedMessages
.at(-1)!
.content.filter((block) => block.type === 'text')
.content.filter((block): block is TextBlock => block.type === 'text')
.map((block) => block.text);
if (textBlocks.length === 0) {
throw new AnthropicError('stream ended without producing a content block with type=text');
Expand Down Expand Up @@ -369,8 +374,13 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {

switch (event.type) {
case 'content_block_delta': {
if (event.delta.type === 'text_delta') {
this._emit('text', event.delta.text, messageSnapshot.content.at(-1)!.text || '');
const content = messageSnapshot.content.at(-1)!;
if (event.delta.type === 'text_delta' && content.type === 'text') {
this._emit('text', event.delta.text, content.text || '');
} else if (event.delta.type === 'input_json_delta' && content.type === 'tool_use') {
if (content.input) {
this._emit('inputJson', event.delta.partial_json, content.input);
}
}
break;
}
Expand Down Expand Up @@ -459,6 +469,22 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
const snapshotContent = snapshot.content.at(event.index);
if (snapshotContent?.type === 'text' && event.delta.type === 'text_delta') {
snapshotContent.text += event.delta.text;
} else if (snapshotContent?.type === 'tool_use' && event.delta.type === 'input_json_delta') {
// we need to keep track of the raw JSON string as well so that we can
// re-parse it for each delta, for now we just store it as an untyped
// non-enumerable property on the snapshot
let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';
jsonBuf += event.delta.partial_json;

Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {
value: jsonBuf,
enumerable: false,
writable: true,
});

if (jsonBuf) {
snapshotContent.input = partialParse(jsonBuf);
}
}
return snapshot;
}
Expand Down
Loading
Loading