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

feat(api/types): add stream event type aliases with a Raw prefix #428

Merged
merged 1 commit 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
10 changes: 10 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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 @@ -32,6 +39,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 @@ -21,22 +21,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 @@ -46,6 +46,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 @@ -115,21 +139,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 @@ -244,13 +256,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 @@ -584,6 +590,9 @@ export interface MessageCreateParamsStreaming extends MessageCreateParamsBase {

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 @@ -20,47 +20,31 @@ 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>>;
}
}

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 @@ -180,8 +164,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 @@ -202,44 +229,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 @@ -513,6 +527,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