flexinference 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.0
4
+
5
+ Adds the **Messages** caller format (`client.messages.create`), the fourth format
6
+ alongside Responses, chat completions, and Interactions. It takes and returns the
7
+ Anthropic Messages shape and works for any model via the canonical Responses path,
8
+ posting to `/messages`. `max_tokens` is required (Anthropic requires it) and
9
+ `start_within` is required (same rule as the other formats); `service_tier` is a
10
+ compile-time error (the router owns the tier). New `messageText(obj)` helper pulls the
11
+ assistant text out of the response `content[]` blocks, where `outputText` and
12
+ `interactionText` return "". Also adds first-class Anthropic (`claude-*`) upstream
13
+ support behind the existing formats.
14
+
3
15
  ## 1.1.0
4
16
 
5
17
  Adds the **Interactions** caller format (`client.interactions.create`), the third format
package/dist/index.d.ts CHANGED
@@ -22,6 +22,11 @@ export type InteractionCreateParams = WithStartWithin<Schemas["InteractionCreate
22
22
  };
23
23
  export type InteractionObject = Schemas["InteractionObject"];
24
24
  export type InteractionStreamEvent = Schemas["InteractionStreamEvent"];
25
+ export type MessageCreateParams = WithStartWithin<Schemas["MessageCreateParams"]> & {
26
+ service_tier?: never;
27
+ };
28
+ export type MessageObject = Schemas["MessageObject"];
29
+ export type MessageStreamEvent = Schemas["MessageStreamEvent"];
25
30
  /**
26
31
  * Concatenate the assistant's text from a non-streamed response. Works on a Responses
27
32
  * object (walks `output[].content[]` for `output_text` parts) and on a chat completion
@@ -40,6 +45,15 @@ export declare function outputText(response: ResponseObject | ChatCompletion): s
40
45
  * reshaped. Streamed interactions are not objects; collect their `step.delta` text events.
41
46
  */
42
47
  export declare function interactionText(interaction: InteractionObject): string;
48
+ /**
49
+ * Concatenate the assistant's text from a non-streamed Messages response. The text lives
50
+ * in `content[]` as `{type:"text",text}` blocks (thinking and tool_use blocks are
51
+ * skipped) - the Anthropic Messages shape, a different layout from Responses/chat and
52
+ * Interactions, so `outputText` and `interactionText` return "" on it. Returns "" when
53
+ * there is no text (e.g. a tool_use-only response). Read-only: nothing is reshaped.
54
+ * Streamed messages are not objects; collect their `content_block_delta` text events.
55
+ */
56
+ export declare function messageText(message: MessageObject): string;
43
57
  export interface FlexErrorBody {
44
58
  error: {
45
59
  message: string;
@@ -79,6 +93,7 @@ export declare class FlexInference {
79
93
  readonly responses: Responses;
80
94
  readonly chat: Chat;
81
95
  readonly interactions: Interactions;
96
+ readonly messages: Messages;
82
97
  private readonly apiKey;
83
98
  private readonly baseURL;
84
99
  private readonly fetchImpl;
@@ -120,4 +135,14 @@ declare class Interactions {
120
135
  stream: true;
121
136
  }, options?: RequestOptions): Promise<AsyncIterable<InteractionStreamEvent>>;
122
137
  }
138
+ declare class Messages {
139
+ private readonly post;
140
+ constructor(post: Post);
141
+ create(body: MessageCreateParams & {
142
+ stream?: false | null;
143
+ }, options?: RequestOptions): Promise<MessageObject>;
144
+ create(body: MessageCreateParams & {
145
+ stream: true;
146
+ }, options?: RequestOptions): Promise<AsyncIterable<MessageStreamEvent>>;
147
+ }
123
148
  export {};
package/dist/index.js CHANGED
@@ -62,6 +62,29 @@ export function interactionText(interaction) {
62
62
  }
63
63
  return text;
64
64
  }
65
+ /**
66
+ * Concatenate the assistant's text from a non-streamed Messages response. The text lives
67
+ * in `content[]` as `{type:"text",text}` blocks (thinking and tool_use blocks are
68
+ * skipped) - the Anthropic Messages shape, a different layout from Responses/chat and
69
+ * Interactions, so `outputText` and `interactionText` return "" on it. Returns "" when
70
+ * there is no text (e.g. a tool_use-only response). Read-only: nothing is reshaped.
71
+ * Streamed messages are not objects; collect their `content_block_delta` text events.
72
+ */
73
+ export function messageText(message) {
74
+ const obj = message;
75
+ if (!isRecord(obj))
76
+ return "";
77
+ const content = obj.content;
78
+ if (!Array.isArray(content))
79
+ return "";
80
+ let text = "";
81
+ for (const block of content) {
82
+ if (isRecord(block) && block.type === "text" && typeof block.text === "string") {
83
+ text += block.text;
84
+ }
85
+ }
86
+ return text;
87
+ }
65
88
  export class FlexInferenceError extends Error {
66
89
  status;
67
90
  type;
@@ -114,6 +137,7 @@ export class FlexInference {
114
137
  responses;
115
138
  chat;
116
139
  interactions;
140
+ messages;
117
141
  apiKey;
118
142
  baseURL;
119
143
  fetchImpl;
@@ -133,6 +157,7 @@ export class FlexInference {
133
157
  this.responses = new Responses(post);
134
158
  this.chat = new Chat(post);
135
159
  this.interactions = new Interactions(post);
160
+ this.messages = new Messages(post);
136
161
  }
137
162
  async post(path, body, signal) {
138
163
  // One AbortController fed by three sources: the caller's signal, a 10s connect
@@ -261,6 +286,25 @@ class Interactions {
261
286
  }
262
287
  }
263
288
  }
289
+ class Messages {
290
+ post;
291
+ constructor(post) {
292
+ this.post = post;
293
+ }
294
+ async create(body, options) {
295
+ assertStartWithin(body);
296
+ const { res, cleanup } = await this.post("/messages", body, options?.signal);
297
+ if (body.stream) {
298
+ return streamSSE(res, cleanup);
299
+ }
300
+ try {
301
+ return (await res.json());
302
+ }
303
+ finally {
304
+ cleanup();
305
+ }
306
+ }
307
+ }
264
308
  async function* streamSSE(res, cleanup) {
265
309
  if (!res.body) {
266
310
  cleanup();
package/dist/types.d.ts CHANGED
@@ -81,6 +81,23 @@ export interface paths {
81
81
  patch?: never;
82
82
  trace?: never;
83
83
  };
84
+ "/messages": {
85
+ parameters: {
86
+ query?: never;
87
+ header?: never;
88
+ path?: never;
89
+ cookie?: never;
90
+ };
91
+ get?: never;
92
+ put?: never;
93
+ /** Create a message. The fourth FlexInference caller format (Anthropic Messages shape in and out); works for any model via the canonical Responses path. Send start_within and do not send service_tier. max_tokens is required. */
94
+ post: operations["createMessage"];
95
+ delete?: never;
96
+ options?: never;
97
+ head?: never;
98
+ patch?: never;
99
+ trace?: never;
100
+ };
84
101
  }
85
102
  export type webhooks = Record<string, never>;
86
103
  export interface components {
@@ -5675,6 +5692,287 @@ export interface components {
5675
5692
  * @description One server-sent event frame on the Interactions stream.
5676
5693
  */
5677
5694
  InteractionStreamEvent: components["schemas"]["InteractionCreatedEvent"] | components["schemas"]["InteractionStatusUpdateEvent"] | components["schemas"]["InteractionCompletedEvent"] | components["schemas"]["InteractionStepStartEvent"] | components["schemas"]["InteractionStepDeltaEvent"] | components["schemas"]["InteractionStepStopEvent"] | components["schemas"]["InteractionErrorEvent"];
5695
+ /** Message Text Block */
5696
+ MessageTextBlock: {
5697
+ /** @constant */
5698
+ type: "text";
5699
+ text: string;
5700
+ };
5701
+ /**
5702
+ * Message Image Source
5703
+ * @description An image source, either base64-encoded bytes or a URL.
5704
+ */
5705
+ MessageImageSource: {
5706
+ /** @enum {string} */
5707
+ type: "base64" | "url";
5708
+ media_type?: string;
5709
+ /** @description Base64-encoded image bytes (type base64). */
5710
+ data?: string;
5711
+ /** @description Image URL (type url). */
5712
+ url?: string;
5713
+ };
5714
+ /** Message Image Block */
5715
+ MessageImageBlock: {
5716
+ /** @constant */
5717
+ type: "image";
5718
+ source: components["schemas"]["MessageImageSource"];
5719
+ };
5720
+ /** Message Thinking Block */
5721
+ MessageThinkingBlock: {
5722
+ /** @constant */
5723
+ type: "thinking";
5724
+ thinking: string;
5725
+ signature?: string;
5726
+ };
5727
+ /** Message Tool Use Block */
5728
+ MessageToolUseBlock: {
5729
+ /** @constant */
5730
+ type: "tool_use";
5731
+ id: string;
5732
+ name: string;
5733
+ /** @description Parsed tool input object (not a JSON string). */
5734
+ input: {
5735
+ [key: string]: unknown;
5736
+ };
5737
+ };
5738
+ /**
5739
+ * Message Tool Result Content Block
5740
+ * @description A single content block inside a tool_result (text or image).
5741
+ */
5742
+ MessageToolResultContentBlock: components["schemas"]["MessageTextBlock"] | components["schemas"]["MessageImageBlock"];
5743
+ /** Message Tool Result Block */
5744
+ MessageToolResultBlock: {
5745
+ /** @constant */
5746
+ type: "tool_result";
5747
+ tool_use_id: string;
5748
+ /** @description Tool result payload; a string or an array of content blocks. */
5749
+ content?: string | components["schemas"]["MessageToolResultContentBlock"][];
5750
+ };
5751
+ /**
5752
+ * Message Content Block
5753
+ * @description A single piece of multimodal content in a Messages request.
5754
+ */
5755
+ MessageContentBlock: components["schemas"]["MessageTextBlock"] | components["schemas"]["MessageImageBlock"] | components["schemas"]["MessageThinkingBlock"] | components["schemas"]["MessageToolUseBlock"] | components["schemas"]["MessageToolResultBlock"];
5756
+ /**
5757
+ * Message
5758
+ * @description One turn in a Messages conversation.
5759
+ */
5760
+ Message: {
5761
+ /** @enum {string} */
5762
+ role: "user" | "assistant";
5763
+ /** @description The turn content as a string or an array of content blocks. */
5764
+ content: string | components["schemas"]["MessageContentBlock"][];
5765
+ };
5766
+ /** Message Tool */
5767
+ MessageTool: {
5768
+ name: string;
5769
+ description?: string;
5770
+ /** @description JSON Schema for the tool input. */
5771
+ input_schema: {
5772
+ [key: string]: unknown;
5773
+ };
5774
+ };
5775
+ /** Message Tool Choice */
5776
+ MessageToolChoice: {
5777
+ /** @enum {string} */
5778
+ type: "auto" | "any" | "tool";
5779
+ /** @description Required when type is tool. */
5780
+ name?: string;
5781
+ };
5782
+ /**
5783
+ * Message Thinking Config
5784
+ * @description Extended thinking controls. "adaptive" lets the model choose the budget; "enabled" requires budget_tokens. display "summarized" surfaces reasoning text.
5785
+ */
5786
+ MessageThinkingConfig: {
5787
+ /** @enum {string} */
5788
+ type: "adaptive" | "enabled";
5789
+ /** @description Required when type is enabled; must be >= 1024 and less than max_tokens. */
5790
+ budget_tokens?: number;
5791
+ display?: string;
5792
+ };
5793
+ /**
5794
+ * Message Create Params
5795
+ * @description Request body for POST /v1/messages (Anthropic Messages shape). Works for any model via the canonical Responses path. max_tokens is required (Anthropic requires it and the router rejects a claude-* request that reaches the Anthropic upstream without a canonical max_output_tokens). Does not accept service_tier (the router owns the tier). start_within is required at runtime but left optional here.
5796
+ */
5797
+ MessageCreateParams: {
5798
+ model: string;
5799
+ max_tokens: number;
5800
+ /** @description System prompt as a string or an array of text blocks. */
5801
+ system?: string | components["schemas"]["MessageTextBlock"][];
5802
+ messages: components["schemas"]["Message"][];
5803
+ temperature?: number;
5804
+ top_p?: number;
5805
+ tools?: components["schemas"]["MessageTool"][];
5806
+ tool_choice?: components["schemas"]["MessageToolChoice"];
5807
+ thinking?: components["schemas"]["MessageThinkingConfig"];
5808
+ stream?: boolean;
5809
+ /** @description FlexInference deadline control (see the Responses/Chat ops). Required on every request; enforced by the router at runtime, not by this schema. */
5810
+ start_within?: string;
5811
+ } & {
5812
+ [key: string]: unknown;
5813
+ };
5814
+ /** Message Cache Creation */
5815
+ MessageCacheCreation: {
5816
+ ephemeral_5m_input_tokens?: number;
5817
+ ephemeral_1h_input_tokens?: number;
5818
+ };
5819
+ /** Message Usage Output Tokens Details */
5820
+ MessageUsageOutputTokensDetails: {
5821
+ thinking_tokens?: number;
5822
+ };
5823
+ /**
5824
+ * Message Usage
5825
+ * @description Anthropic usage. input_tokens EXCLUDES cache; output_tokens INCLUDES thinking. The router folds cache_read/cache_creation into canonical input and keeps output_tokens verbatim. service_tier here is authoritative for the served tier.
5826
+ */
5827
+ MessageUsage: {
5828
+ input_tokens?: number;
5829
+ cache_creation_input_tokens?: number | null;
5830
+ cache_read_input_tokens?: number | null;
5831
+ cache_creation?: components["schemas"]["MessageCacheCreation"];
5832
+ output_tokens?: number;
5833
+ output_tokens_details?: components["schemas"]["MessageUsageOutputTokensDetails"];
5834
+ service_tier?: string;
5835
+ inference_geo?: string;
5836
+ } & {
5837
+ [key: string]: unknown;
5838
+ };
5839
+ /**
5840
+ * Message Response Content Block
5841
+ * @description A content block on a Messages response (text, thinking, or tool_use).
5842
+ */
5843
+ MessageResponseContentBlock: components["schemas"]["MessageTextBlock"] | components["schemas"]["MessageThinkingBlock"] | components["schemas"]["MessageToolUseBlock"];
5844
+ /**
5845
+ * Message Object
5846
+ * @description Non-stream response from POST /v1/messages (Anthropic Messages shape).
5847
+ */
5848
+ MessageObject: {
5849
+ id: string;
5850
+ /** @constant */
5851
+ type: "message";
5852
+ /** @constant */
5853
+ role: "assistant";
5854
+ model: string;
5855
+ content: components["schemas"]["MessageResponseContentBlock"][];
5856
+ /** @enum {string|null} */
5857
+ stop_reason?: "end_turn" | "max_tokens" | "tool_use" | "stop_sequence" | "refusal" | null;
5858
+ stop_sequence?: string | null;
5859
+ /** @description The served tier, authoritative from usage.service_tier. Anthropic serves "standard" even when sent "standard_only". */
5860
+ service_tier?: string;
5861
+ usage?: components["schemas"]["MessageUsage"];
5862
+ };
5863
+ /**
5864
+ * Message Stream Content Block
5865
+ * @description The content block descriptor on a content_block_start event.
5866
+ */
5867
+ MessageStreamContentBlock: {
5868
+ type: string;
5869
+ } & {
5870
+ [key: string]: unknown;
5871
+ };
5872
+ /**
5873
+ * Message Stream Delta
5874
+ * @description The incremental payload on a content_block_delta event.
5875
+ */
5876
+ MessageStreamDelta: {
5877
+ /** @enum {string} */
5878
+ type: "text_delta" | "thinking_delta" | "input_json_delta" | "signature_delta";
5879
+ text?: string;
5880
+ thinking?: string;
5881
+ partial_json?: string;
5882
+ signature?: string;
5883
+ };
5884
+ /**
5885
+ * Message Delta Stop Info
5886
+ * @description The stop info carried by a message_delta event.
5887
+ */
5888
+ MessageDeltaStopInfo: {
5889
+ stop_reason?: string | null;
5890
+ stop_sequence?: string | null;
5891
+ };
5892
+ /** Message Start Event */
5893
+ MessageStartEvent: {
5894
+ /**
5895
+ * @description discriminator enum property added by openapi-typescript
5896
+ * @enum {string}
5897
+ */
5898
+ type: "message_start";
5899
+ message: components["schemas"]["MessageObject"];
5900
+ };
5901
+ /** Message Content Block Start Event */
5902
+ MessageContentBlockStartEvent: {
5903
+ /**
5904
+ * @description discriminator enum property added by openapi-typescript
5905
+ * @enum {string}
5906
+ */
5907
+ type: "content_block_start";
5908
+ index: number;
5909
+ content_block: components["schemas"]["MessageStreamContentBlock"];
5910
+ };
5911
+ /** Message Content Block Delta Event */
5912
+ MessageContentBlockDeltaEvent: {
5913
+ /**
5914
+ * @description discriminator enum property added by openapi-typescript
5915
+ * @enum {string}
5916
+ */
5917
+ type: "content_block_delta";
5918
+ index: number;
5919
+ delta: components["schemas"]["MessageStreamDelta"];
5920
+ };
5921
+ /** Message Content Block Stop Event */
5922
+ MessageContentBlockStopEvent: {
5923
+ /**
5924
+ * @description discriminator enum property added by openapi-typescript
5925
+ * @enum {string}
5926
+ */
5927
+ type: "content_block_stop";
5928
+ index: number;
5929
+ };
5930
+ /** Message Delta Event */
5931
+ MessageDeltaEvent: {
5932
+ /**
5933
+ * @description discriminator enum property added by openapi-typescript
5934
+ * @enum {string}
5935
+ */
5936
+ type: "message_delta";
5937
+ delta: components["schemas"]["MessageDeltaStopInfo"];
5938
+ usage?: components["schemas"]["MessageUsage"];
5939
+ };
5940
+ /** Message Stop Event */
5941
+ MessageStopEvent: {
5942
+ /**
5943
+ * @description discriminator enum property added by openapi-typescript
5944
+ * @enum {string}
5945
+ */
5946
+ type: "message_stop";
5947
+ };
5948
+ /** Message Ping Event */
5949
+ MessagePingEvent: {
5950
+ /**
5951
+ * @description discriminator enum property added by openapi-typescript
5952
+ * @enum {string}
5953
+ */
5954
+ type: "ping";
5955
+ };
5956
+ /**
5957
+ * Message Error Event
5958
+ * @description Terminal error frame emitted when the upstream stream fails.
5959
+ */
5960
+ MessageErrorEvent: {
5961
+ /**
5962
+ * @description discriminator enum property added by openapi-typescript
5963
+ * @enum {string}
5964
+ */
5965
+ type: "error";
5966
+ error: {
5967
+ type?: string;
5968
+ message?: string;
5969
+ };
5970
+ };
5971
+ /**
5972
+ * Message Stream Event
5973
+ * @description One server-sent event frame on the Messages stream.
5974
+ */
5975
+ MessageStreamEvent: components["schemas"]["MessageStartEvent"] | components["schemas"]["MessageContentBlockStartEvent"] | components["schemas"]["MessageContentBlockDeltaEvent"] | components["schemas"]["MessageContentBlockStopEvent"] | components["schemas"]["MessageDeltaEvent"] | components["schemas"]["MessageStopEvent"] | components["schemas"]["MessagePingEvent"] | components["schemas"]["MessageErrorEvent"];
5678
5976
  };
5679
5977
  responses: never;
5680
5978
  parameters: never;
@@ -5813,4 +6111,47 @@ export interface operations {
5813
6111
  };
5814
6112
  };
5815
6113
  };
6114
+ createMessage: {
6115
+ parameters: {
6116
+ query?: never;
6117
+ header?: never;
6118
+ path?: never;
6119
+ cookie?: never;
6120
+ };
6121
+ requestBody: {
6122
+ content: {
6123
+ "application/json": components["schemas"]["MessageCreateParams"];
6124
+ };
6125
+ };
6126
+ responses: {
6127
+ /** @description OK */
6128
+ 200: {
6129
+ headers: {
6130
+ [name: string]: unknown;
6131
+ };
6132
+ content: {
6133
+ "application/json": components["schemas"]["MessageObject"];
6134
+ "text/event-stream": components["schemas"]["MessageStreamEvent"];
6135
+ };
6136
+ };
6137
+ /** @description Bad request. A FlexInference-layer rejection (type flexinference_error, e.g. missing_start_within, invalid_start_within, model_not_flex_capable, flex_unsupported_for_anthropic, missing_max_tokens, service_tier_not_allowed) or an upstream provider rejection passed through unchanged. */
6138
+ 400: {
6139
+ headers: {
6140
+ [name: string]: unknown;
6141
+ };
6142
+ content: {
6143
+ "application/json": components["schemas"]["FlexError"];
6144
+ };
6145
+ };
6146
+ /** @description Invalid or missing FlexInference API key (type flexinference_error, code invalid_api_key). */
6147
+ 401: {
6148
+ headers: {
6149
+ [name: string]: unknown;
6150
+ };
6151
+ content: {
6152
+ "application/json": components["schemas"]["FlexError"];
6153
+ };
6154
+ };
6155
+ };
6156
+ };
5816
6157
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexinference",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Official TypeScript SDK for FlexInference - a deadline-aware, OpenAI-compatible inference router.",
5
5
  "license": "MIT",
6
6
  "author": "Aditya Perswal <adityaperswal@gmail.com>",