Skip to content

Commit

Permalink
feat(api/types): add stream event type aliases with a Raw prefix (#428)
Browse files Browse the repository at this point in the history
this change is mainly for consistency with the Python SDK as there is a
distinct difference between `Raw` and non-`Raw` types, see this PR for
more details: anthropics/anthropic-sdk-python#528
  • Loading branch information
stainless-bot committed May 30, 2024
1 parent bf7c1fd commit 1e367e4
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 73 deletions.
10 changes: 10 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ Types:
- <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>
Expand All @@ -35,6 +42,9 @@ Methods:
Types:

- <code><a href="./src/resources/beta/tools/messages.ts">InputJsonDelta</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">RawToolsBetaContentBlockDeltaEvent</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">RawToolsBetaContentBlockStartEvent</a></code>
- <code><a href="./src/resources/beta/tools/messages.ts">RawToolsBetaMessageStreamEvent</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>
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ export namespace Anthropic {
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;
Expand Down
3 changes: 3 additions & 0 deletions src/resources/beta/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

export {
InputJsonDelta,
RawToolsBetaContentBlockDeltaEvent,
RawToolsBetaContentBlockStartEvent,
RawToolsBetaMessageStreamEvent,
Tool,
ToolResultBlockParam,
ToolUseBlock,
Expand Down
59 changes: 34 additions & 25 deletions src/resources/beta/tools/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ export class Messages extends APIResource {
create(
body: MessageCreateParamsStreaming,
options?: Core.RequestOptions,
): APIPromise<Stream<ToolsBetaMessageStreamEvent>>;
): APIPromise<Stream<RawToolsBetaMessageStreamEvent>>;
create(
body: MessageCreateParamsBase,
options?: Core.RequestOptions,
): APIPromise<Stream<ToolsBetaMessageStreamEvent> | ToolsBetaMessage>;
): APIPromise<Stream<RawToolsBetaMessageStreamEvent> | ToolsBetaMessage>;
create(
body: MessageCreateParams,
options?: Core.RequestOptions,
): APIPromise<ToolsBetaMessage> | APIPromise<Stream<ToolsBetaMessageStreamEvent>> {
): APIPromise<ToolsBetaMessage> | APIPromise<Stream<RawToolsBetaMessageStreamEvent>> {
return this._client.post('/v1/messages?beta=tools', {
body,
timeout: 600000,
...options,
headers: { 'anthropic-beta': 'tools-2024-05-16', ...options?.headers },
stream: body.stream ?? false,
}) as APIPromise<ToolsBetaMessage> | APIPromise<Stream<ToolsBetaMessageStreamEvent>>;
}) as APIPromise<ToolsBetaMessage> | APIPromise<Stream<RawToolsBetaMessageStreamEvent>>;
}

/**
Expand All @@ -55,6 +55,30 @@ export interface InputJsonDelta {
type: 'input_json_delta';
}

export interface RawToolsBetaContentBlockDeltaEvent {
delta: MessagesAPI.TextDelta | InputJsonDelta;

index: number;

type: 'content_block_delta';
}

export interface RawToolsBetaContentBlockStartEvent {
content_block: MessagesAPI.TextBlock | ToolUseBlock;

index: number;

type: 'content_block_start';
}

export type RawToolsBetaMessageStreamEvent =
| MessagesAPI.RawMessageStartEvent
| MessagesAPI.RawMessageDeltaEvent
| MessagesAPI.RawMessageStopEvent
| RawToolsBetaContentBlockStartEvent
| RawToolsBetaContentBlockDeltaEvent
| MessagesAPI.RawContentBlockStopEvent;

export interface Tool {
/**
* [JSON schema](https://json-schema.org/) for this tool's input.
Expand Down Expand Up @@ -124,21 +148,9 @@ export interface ToolUseBlockParam {

export type ToolsBetaContentBlock = MessagesAPI.TextBlock | ToolUseBlock;

export interface ToolsBetaContentBlockDeltaEvent {
delta: MessagesAPI.TextDelta | InputJsonDelta;

index: number;

type: 'content_block_delta';
}
export type ToolsBetaContentBlockDeltaEvent = RawToolsBetaContentBlockDeltaEvent;

export interface ToolsBetaContentBlockStartEvent {
content_block: MessagesAPI.TextBlock | ToolUseBlock;

index: number;

type: 'content_block_start';
}
export type ToolsBetaContentBlockStartEvent = RawToolsBetaContentBlockStartEvent;

export interface ToolsBetaMessage {
/**
Expand Down Expand Up @@ -253,13 +265,7 @@ export interface ToolsBetaMessageParam {
role: 'user' | 'assistant';
}

export type ToolsBetaMessageStreamEvent =
| MessagesAPI.MessageStartEvent
| MessagesAPI.MessageDeltaEvent
| MessagesAPI.MessageStopEvent
| ToolsBetaContentBlockStartEvent
| ToolsBetaContentBlockDeltaEvent
| MessagesAPI.ContentBlockStopEvent;
export type ToolsBetaMessageStreamEvent = RawToolsBetaMessageStreamEvent;

export type MessageCreateParams = MessageCreateParamsNonStreaming | MessageCreateParamsStreaming;

Expand Down Expand Up @@ -890,6 +896,9 @@ export namespace MessageStreamParams {

export namespace Messages {
export import InputJsonDelta = ToolsMessagesAPI.InputJsonDelta;
export import RawToolsBetaContentBlockDeltaEvent = ToolsMessagesAPI.RawToolsBetaContentBlockDeltaEvent;
export import RawToolsBetaContentBlockStartEvent = ToolsMessagesAPI.RawToolsBetaContentBlockStartEvent;
export import RawToolsBetaMessageStreamEvent = ToolsMessagesAPI.RawToolsBetaMessageStreamEvent;
export import Tool = ToolsMessagesAPI.Tool;
export import ToolResultBlockParam = ToolsMessagesAPI.ToolResultBlockParam;
export import ToolUseBlock = ToolsMessagesAPI.ToolUseBlock;
Expand Down
3 changes: 3 additions & 0 deletions src/resources/beta/tools/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export class Tools extends APIResource {
export namespace Tools {
export import Messages = MessagesAPI.Messages;
export import InputJsonDelta = MessagesAPI.InputJsonDelta;
export import RawToolsBetaContentBlockDeltaEvent = MessagesAPI.RawToolsBetaContentBlockDeltaEvent;
export import RawToolsBetaContentBlockStartEvent = MessagesAPI.RawToolsBetaContentBlockStartEvent;
export import RawToolsBetaMessageStreamEvent = MessagesAPI.RawToolsBetaMessageStreamEvent;
export import Tool = MessagesAPI.Tool;
export import ToolResultBlockParam = MessagesAPI.ToolResultBlockParam;
export import ToolUseBlock = MessagesAPI.ToolUseBlock;
Expand Down
7 changes: 7 additions & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export {
MessageStartEvent,
MessageStopEvent,
MessageStreamEvent,
RawContentBlockDeltaEvent,
RawContentBlockStartEvent,
RawContentBlockStopEvent,
RawMessageDeltaEvent,
RawMessageStartEvent,
RawMessageStopEvent,
RawMessageStreamEvent,
TextBlock,
TextBlockParam,
TextDelta,
Expand Down
117 changes: 69 additions & 48 deletions src/resources/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ export class Messages extends APIResource {
create(
body: MessageCreateParamsStreaming,
options?: Core.RequestOptions,
): APIPromise<Stream<MessageStreamEvent>>;
): APIPromise<Stream<RawMessageStreamEvent>>;
create(
body: MessageCreateParamsBase,
options?: Core.RequestOptions,
): APIPromise<Stream<MessageStreamEvent> | Message>;
): APIPromise<Stream<RawMessageStreamEvent> | Message>;
create(
body: MessageCreateParams,
options?: Core.RequestOptions,
): APIPromise<Message> | APIPromise<Stream<MessageStreamEvent>> {
): APIPromise<Message> | APIPromise<Stream<RawMessageStreamEvent>> {
return this._client.post('/v1/messages', {
body,
timeout: 600000,
...options,
stream: body.stream ?? false,
}) as APIPromise<Message> | APIPromise<Stream<MessageStreamEvent>>;
}) as APIPromise<Message> | APIPromise<Stream<RawMessageStreamEvent>>;
}

/**
Expand All @@ -49,27 +49,11 @@ export class Messages extends APIResource {

export type ContentBlock = TextBlock;

export interface ContentBlockDeltaEvent {
delta: TextDelta;
export type ContentBlockDeltaEvent = RawContentBlockDeltaEvent;

index: number;
export type ContentBlockStartEvent = RawContentBlockStartEvent;

type: 'content_block_delta';
}

export interface ContentBlockStartEvent {
content_block: TextBlock;

index: number;

type: 'content_block_start';
}

export interface ContentBlockStopEvent {
index: number;

type: 'content_block_stop';
}
export type ContentBlockStopEvent = RawContentBlockStopEvent;

export interface ImageBlockParam {
source: ImageBlockParam.Source;
Expand Down Expand Up @@ -189,8 +173,51 @@ export interface Message {
usage: Usage;
}

export interface MessageDeltaEvent {
delta: MessageDeltaEvent.Delta;
export type MessageDeltaEvent = RawMessageDeltaEvent;

export interface MessageDeltaUsage {
/**
* The cumulative number of output tokens which were used.
*/
output_tokens: number;
}

export interface MessageParam {
content: string | Array<TextBlockParam | ImageBlockParam>;

role: 'user' | 'assistant';
}

export type MessageStartEvent = RawMessageStartEvent;

export type MessageStopEvent = RawMessageStopEvent;

export type MessageStreamEvent = RawMessageStreamEvent;

export interface RawContentBlockDeltaEvent {
delta: TextDelta;

index: number;

type: 'content_block_delta';
}

export interface RawContentBlockStartEvent {
content_block: TextBlock;

index: number;

type: 'content_block_start';
}

export interface RawContentBlockStopEvent {
index: number;

type: 'content_block_stop';
}

export interface RawMessageDeltaEvent {
delta: RawMessageDeltaEvent.Delta;

type: 'message_delta';

Expand All @@ -211,44 +238,31 @@ export interface MessageDeltaEvent {
usage: MessageDeltaUsage;
}

export namespace MessageDeltaEvent {
export namespace RawMessageDeltaEvent {
export interface Delta {
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | null;

stop_sequence: string | null;
}
}

export interface MessageDeltaUsage {
/**
* The cumulative number of output tokens which were used.
*/
output_tokens: number;
}

export interface MessageParam {
content: string | Array<TextBlockParam | ImageBlockParam>;

role: 'user' | 'assistant';
}

export interface MessageStartEvent {
export interface RawMessageStartEvent {
message: Message;

type: 'message_start';
}

export interface MessageStopEvent {
export interface RawMessageStopEvent {
type: 'message_stop';
}

export type MessageStreamEvent =
| MessageStartEvent
| MessageDeltaEvent
| MessageStopEvent
| ContentBlockStartEvent
| ContentBlockDeltaEvent
| ContentBlockStopEvent;
export type RawMessageStreamEvent =
| RawMessageStartEvent
| RawMessageDeltaEvent
| RawMessageStopEvent
| RawContentBlockStartEvent
| RawContentBlockDeltaEvent
| RawContentBlockStopEvent;

export interface TextBlock {
text: string;
Expand Down Expand Up @@ -718,6 +732,13 @@ export namespace Messages {
export import MessageStartEvent = MessagesAPI.MessageStartEvent;
export import MessageStopEvent = MessagesAPI.MessageStopEvent;
export import MessageStreamEvent = MessagesAPI.MessageStreamEvent;
export import RawContentBlockDeltaEvent = MessagesAPI.RawContentBlockDeltaEvent;
export import RawContentBlockStartEvent = MessagesAPI.RawContentBlockStartEvent;
export import RawContentBlockStopEvent = MessagesAPI.RawContentBlockStopEvent;
export import RawMessageDeltaEvent = MessagesAPI.RawMessageDeltaEvent;
export import RawMessageStartEvent = MessagesAPI.RawMessageStartEvent;
export import RawMessageStopEvent = MessagesAPI.RawMessageStopEvent;
export import RawMessageStreamEvent = MessagesAPI.RawMessageStreamEvent;
export import TextBlock = MessagesAPI.TextBlock;
export import TextBlockParam = MessagesAPI.TextBlockParam;
export import TextDelta = MessagesAPI.TextDelta;
Expand Down

0 comments on commit 1e367e4

Please sign in to comment.