@broberg/ai-sdk 0.2.0 → 0.3.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/dist/index.d.ts CHANGED
@@ -107,12 +107,39 @@ interface ChatRequest {
107
107
  tools?: Tool[];
108
108
  maxTokens?: number;
109
109
  temperature?: number;
110
+ /** "json" → request JSON-object output where the provider supports it (F009). */
111
+ responseFormat?: "json" | "text";
110
112
  }
111
113
  interface ChatResult {
112
114
  text: string;
113
115
  toolCalls?: ToolCall[];
114
116
  usage: Usage;
115
117
  }
118
+ /** A single event from `ai.chatStream` / `adapter.chatStream` (F8). A streamed
119
+ * turn yields `text`/`tool_call` events as they arrive, one `usage` event when
120
+ * the provider reports totals, and a terminal `finish` (or `error`). Tool calls
121
+ * are emitted COMPLETE — accumulated across wire fragments, never partial. */
122
+ type ChatStreamEvent = {
123
+ type: "text";
124
+ delta: string;
125
+ } | {
126
+ type: "tool_call";
127
+ id: string;
128
+ name: string;
129
+ args: Record<string, unknown>;
130
+ } | {
131
+ type: "usage";
132
+ costUsd: number;
133
+ model: string;
134
+ usage: Usage;
135
+ } | {
136
+ type: "finish";
137
+ reason: "end_turn" | "tool_calls" | "length" | "stop";
138
+ } | {
139
+ type: "error";
140
+ message: string;
141
+ status?: number;
142
+ };
116
143
  interface ImageRequest {
117
144
  prompt: string;
118
145
  spec: TierSpec;
@@ -152,6 +179,9 @@ interface ProviderAdapter {
152
179
  * (e.g. fal does image only). The client guards each call and throws a clear
153
180
  * "provider X does not support Y" when a capability is absent. */
154
181
  chat?(req: ChatRequest): Promise<ChatResult>;
182
+ /** Streaming chat (F8). Optional — absence is a typed "no streaming support".
183
+ * Same request shape as chat; yields ChatStreamEvents as the turn unfolds. */
184
+ chatStream?(req: ChatRequest): AsyncIterable<ChatStreamEvent>;
155
185
  vision?(req: ChatRequest): Promise<ChatResult>;
156
186
  image?(req: ImageRequest): Promise<ImageResult>;
157
187
  embedding?(req: EmbeddingRequest): Promise<EmbeddingResult>;
@@ -441,6 +471,8 @@ declare const chatInputSchema: z.ZodObject<{
441
471
  }>, "many">>;
442
472
  maxTokens: z.ZodOptional<z.ZodNumber>;
443
473
  temperature: z.ZodOptional<z.ZodNumber>;
474
+ /** "json" requests JSON-object output (OpenAI-compatible response_format). */
475
+ responseFormat: z.ZodOptional<z.ZodEnum<["json", "text"]>>;
444
476
  }, "strip", z.ZodTypeAny, {
445
477
  system?: string | undefined;
446
478
  prompt?: string | undefined;
@@ -468,6 +500,7 @@ declare const chatInputSchema: z.ZodObject<{
468
500
  }[] | undefined;
469
501
  temperature?: number | undefined;
470
502
  maxTokens?: number | undefined;
503
+ responseFormat?: "text" | "json" | undefined;
471
504
  tier?: "fast" | "smart" | "powerful" | "cheap" | "vision" | "embedding" | undefined;
472
505
  override?: {
473
506
  provider?: string | undefined;
@@ -507,6 +540,7 @@ declare const chatInputSchema: z.ZodObject<{
507
540
  }[] | undefined;
508
541
  temperature?: number | undefined;
509
542
  maxTokens?: number | undefined;
543
+ responseFormat?: "text" | "json" | undefined;
510
544
  tier?: "fast" | "smart" | "powerful" | "cheap" | "vision" | "embedding" | undefined;
511
545
  override?: {
512
546
  provider?: string | undefined;
@@ -902,6 +936,9 @@ type AiConfig = z.infer<typeof aiConfigSchema>;
902
936
  /** The public facade. Defined here because it depends on the derived inputs. */
903
937
  interface AiClient {
904
938
  chat(input: ChatInput): Promise<ChatResult>;
939
+ /** Streaming chat (F8) — same input as chat; yields ChatStreamEvents. The
940
+ * caller owns the tool-loop (per-turn engine, not an agent runtime). */
941
+ chatStream(input: ChatInput): AsyncIterable<ChatStreamEvent>;
905
942
  vision(input: VisionInput): Promise<ChatResult>;
906
943
  translate(input: TranslateInput): Promise<TranslateResult>;
907
944
  image(input: ImageInput): Promise<ImageResult>;
@@ -927,6 +964,7 @@ declare function anthropicAdapter(config?: {
927
964
  apiKey?: string;
928
965
  baseUrl?: string;
929
966
  anthropicVersion?: string;
967
+ fetch?: typeof fetch;
930
968
  }): ProviderAdapter;
931
969
 
932
970
  declare function openaiAdapter(config?: {
@@ -938,6 +976,7 @@ declare function openaiAdapter(config?: {
938
976
  declare function geminiAdapter(config?: {
939
977
  apiKey?: string;
940
978
  baseUrl?: string;
979
+ fetch?: typeof fetch;
941
980
  }): ProviderAdapter;
942
981
 
943
982
  declare function deepinfraAdapter(config?: {
@@ -975,6 +1014,12 @@ interface OpenAICompatibleConfig {
975
1014
  apiKey?: string;
976
1015
  /** Extra headers (e.g. OpenRouter's HTTP-Referer / X-Title). */
977
1016
  extraHeaders?: Record<string, string>;
1017
+ /** Injectable fetch for the streaming path (tests). */
1018
+ fetch?: typeof fetch;
1019
+ /** OpenRouter ground-truth cost (F010): send `usage:{include:true}` and use the
1020
+ * response's `usage.cost` (USD) as costUsd, falling back to the pricing table.
1021
+ * Only OpenRouter returns this field — openai/deepinfra leave it false. */
1022
+ costFromResponseField?: boolean;
978
1023
  }
979
1024
  declare function makeOpenAICompatibleAdapter(config: OpenAICompatibleConfig): ProviderAdapter;
980
1025
 
@@ -993,8 +1038,8 @@ declare const falStubAdapter: ProviderAdapter;
993
1038
  * wires the live adapters. */
994
1039
  declare const stubProviders: Record<string, ProviderAdapter>;
995
1040
 
996
- declare const VERSION: "0.2.0";
997
- declare const SDK_TAG: "@broberg/ai-sdk@0.2.0";
1041
+ declare const VERSION: "0.3.0";
1042
+ declare const SDK_TAG: "@broberg/ai-sdk@0.3.0";
998
1043
 
999
1044
  /** Built-in defaults. Every entry is overridable via AiConfig.defaults or a
1000
1045
  * per-call override. Model IDs are current at scaffold time; callers pin their
@@ -1170,4 +1215,21 @@ declare function httpTransport(req: TransportRequest): Promise<HttpResponse>;
1170
1215
  declare function parseClaudeCliJson(raw: string): SubprocessResponse;
1171
1216
  declare function subprocessTransport(req: TransportRequest): Promise<SubprocessResponse>;
1172
1217
 
1173
- export { type AiClient, type AiConfig, type BudgetConfig, BudgetExceededError, BudgetGuard, type BudgetStore, type CallOptions, type Capability, type ChatInput, type ChatRequest, type ChatResult, type ClassifyInput, type ClassifyResult, type ContentPart, type Contracts, type CostSink, type CostSummary, DEFAULT_TIER_MAP, type DesignInput, type DesignResult, type DiscordSinkConfig, type EmbeddingInput, type EmbeddingRequest, type EmbeddingResult, type ExtractInput, type ExtractResult, type FalAdapterConfig, type HttpResponse, type ImageInput, type ImageRequest, type ImageResult, type Message, type MockupInput, type MockupResult, type OpenAICompatibleConfig, type PricingEntry, type ProviderAdapter, type RerankInput, type RerankResult, type Role, SDK_TAG, type SqliteBudgetStoreConfig, type SqliteSinkConfig, type SubprocessResponse, type Tier, type TierSpec, type Tool, type ToolCall, type TranscribeInput, type TranscribeRequest, type TranscribeResult, type TranslateInput, type TranslateResult, type Transport, type TransportRequest, type TransportResponse, type UpmetricsSinkConfig, type Usage, VERSION, type VisionInput, aiConfigSchema, anthropicAdapter, anthropicApiAdapter, anthropicSubprocessAdapter, chatInputSchema, computeCost, createAI, deepinfraAdapter, defaultProviders, discordSink, embeddingInputSchema, falAdapter, falStubAdapter, freshUsage, fromProviderToolCall, geminiAdapter, getCostSummary, getPrice, httpTransport, imageInputSchema, makeContracts, makeOpenAICompatibleAdapter, messageSchema, multiSink, noopSink, openaiAdapter, openaiStubAdapter, openrouterAdapter, parseClaudeCliJson, parseJsonLoose, resolveTier, sqliteBudgetStore, sqliteSink, stubProviders, subprocessTransport, tierSpecSchema, toProviderTools, toolSchema, translateInputSchema, upmetricsSink, visionInputSchema };
1218
+ /** HTTP error from a streaming connect carries `status` so the client's
1219
+ * pre-stream fallback can tell an eligible 429/5xx from a hard 4xx. */
1220
+ declare class StreamHttpError extends Error {
1221
+ readonly status: number;
1222
+ constructor(message: string, status: number);
1223
+ }
1224
+ interface StreamTransportRequest extends TransportRequest {
1225
+ /** Injectable fetch for tests. */
1226
+ fetch?: typeof fetch;
1227
+ }
1228
+ /**
1229
+ * Open an SSE stream and yield each event's `data:` payload as a raw string
1230
+ * (the JSON after `data: `), skipping the `[DONE]` terminator and non-data
1231
+ * lines. Throws StreamHttpError before the first yield on a non-2xx connect.
1232
+ */
1233
+ declare function streamTransport(req: StreamTransportRequest): AsyncIterable<string>;
1234
+
1235
+ export { type AiClient, type AiConfig, type BudgetConfig, BudgetExceededError, BudgetGuard, type BudgetStore, type CallOptions, type Capability, type ChatInput, type ChatRequest, type ChatResult, type ChatStreamEvent, type ClassifyInput, type ClassifyResult, type ContentPart, type Contracts, type CostSink, type CostSummary, DEFAULT_TIER_MAP, type DesignInput, type DesignResult, type DiscordSinkConfig, type EmbeddingInput, type EmbeddingRequest, type EmbeddingResult, type ExtractInput, type ExtractResult, type FalAdapterConfig, type HttpResponse, type ImageInput, type ImageRequest, type ImageResult, type Message, type MockupInput, type MockupResult, type OpenAICompatibleConfig, type PricingEntry, type ProviderAdapter, type RerankInput, type RerankResult, type Role, SDK_TAG, type SqliteBudgetStoreConfig, type SqliteSinkConfig, StreamHttpError, type SubprocessResponse, type Tier, type TierSpec, type Tool, type ToolCall, type TranscribeInput, type TranscribeRequest, type TranscribeResult, type TranslateInput, type TranslateResult, type Transport, type TransportRequest, type TransportResponse, type UpmetricsSinkConfig, type Usage, VERSION, type VisionInput, aiConfigSchema, anthropicAdapter, anthropicApiAdapter, anthropicSubprocessAdapter, chatInputSchema, computeCost, createAI, deepinfraAdapter, defaultProviders, discordSink, embeddingInputSchema, falAdapter, falStubAdapter, freshUsage, fromProviderToolCall, geminiAdapter, getCostSummary, getPrice, httpTransport, imageInputSchema, makeContracts, makeOpenAICompatibleAdapter, messageSchema, multiSink, noopSink, openaiAdapter, openaiStubAdapter, openrouterAdapter, parseClaudeCliJson, parseJsonLoose, resolveTier, sqliteBudgetStore, sqliteSink, streamTransport, stubProviders, subprocessTransport, tierSpecSchema, toProviderTools, toolSchema, translateInputSchema, upmetricsSink, visionInputSchema };