@providerprotocol/ai 0.0.31 → 0.0.33

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.
@@ -1407,6 +1407,8 @@ declare const StreamEventType: {
1407
1407
  readonly VideoDelta: "video_delta";
1408
1408
  /** Incremental tool call data (arguments being streamed) */
1409
1409
  readonly ToolCallDelta: "tool_call_delta";
1410
+ /** Incremental structured object data (for structured output responses) */
1411
+ readonly ObjectDelta: "object_delta";
1410
1412
  /** Tool execution has started (may be emitted after completion in some implementations) */
1411
1413
  readonly ToolExecutionStart: "tool_execution_start";
1412
1414
  /** Tool execution has completed */
@@ -1431,24 +1433,40 @@ type StreamEventType = (typeof StreamEventType)[keyof typeof StreamEventType];
1431
1433
  * Event delta data payload.
1432
1434
  *
1433
1435
  * Contains the type-specific data for a streaming event.
1434
- * Different fields are populated depending on the event type.
1436
+ * Different fields are populated depending on the event type:
1437
+ *
1438
+ * | Event Type | Fields |
1439
+ * |------------|--------|
1440
+ * | `text_delta` | `text` |
1441
+ * | `reasoning_delta` | `text` |
1442
+ * | `object_delta` | `text` |
1443
+ * | `image_delta` | `data` |
1444
+ * | `audio_delta` | `data` |
1445
+ * | `video_delta` | `data` |
1446
+ * | `tool_call_delta` | `toolCallId`, `toolName`, `argumentsJson` |
1447
+ * | `tool_execution_start` | `toolCallId`, `toolName`, `timestamp` |
1448
+ * | `tool_execution_end` | `toolCallId`, `toolName`, `result`, `isError`, `timestamp` |
1449
+ * | `message_start` | (none) |
1450
+ * | `message_stop` | (none) |
1451
+ * | `content_block_start` | (none) |
1452
+ * | `content_block_stop` | (none) |
1435
1453
  */
1436
1454
  interface EventDelta {
1437
- /** Incremental text content (for text_delta, reasoning_delta) */
1455
+ /** Incremental text content (text_delta, reasoning_delta, object_delta) */
1438
1456
  text?: string;
1439
- /** Incremental binary data (for image_delta, audio_delta, video_delta) */
1457
+ /** Incremental binary data (image_delta, audio_delta, video_delta) */
1440
1458
  data?: Uint8Array;
1441
- /** Tool call identifier (for tool_call_delta, tool_execution_start/end) */
1459
+ /** Tool call identifier (tool_call_delta, tool_execution_start/end) */
1442
1460
  toolCallId?: string;
1443
- /** Tool name (for tool_call_delta, tool_execution_start/end) */
1461
+ /** Tool name (tool_call_delta, tool_execution_start/end) */
1444
1462
  toolName?: string;
1445
- /** Incremental JSON arguments string (for tool_call_delta) */
1463
+ /** Incremental JSON arguments string (tool_call_delta) */
1446
1464
  argumentsJson?: string;
1447
- /** Tool execution result (for tool_execution_end) */
1465
+ /** Tool execution result (tool_execution_end) */
1448
1466
  result?: unknown;
1449
- /** Whether tool execution resulted in an error (for tool_execution_end) */
1467
+ /** Whether tool execution resulted in an error (tool_execution_end) */
1450
1468
  isError?: boolean;
1451
- /** Timestamp in milliseconds (for tool_execution_start/end) */
1469
+ /** Timestamp in milliseconds (tool_execution_start/end) */
1452
1470
  timestamp?: number;
1453
1471
  }
1454
1472
  /**
@@ -1554,6 +1572,14 @@ declare function textDelta(text: string, index?: number): StreamEvent;
1554
1572
  * @returns A tool_call_delta StreamEvent
1555
1573
  */
1556
1574
  declare function toolCallDelta(toolCallId: string, toolName: string, argumentsJson: string, index?: number): StreamEvent;
1575
+ /**
1576
+ * Creates an object delta stream event for structured output responses.
1577
+ *
1578
+ * @param text - The incremental text content
1579
+ * @param index - Content block index (default: 0)
1580
+ * @returns An object_delta StreamEvent
1581
+ */
1582
+ declare function objectDelta(text: string, index?: number): StreamEvent;
1557
1583
  /**
1558
1584
  * Creates a message start stream event.
1559
1585
  *
@@ -1580,6 +1606,28 @@ declare function contentBlockStart(index: number): StreamEvent;
1580
1606
  * @returns A content_block_stop StreamEvent
1581
1607
  */
1582
1608
  declare function contentBlockStop(index: number): StreamEvent;
1609
+ /**
1610
+ * Creates a tool execution start stream event.
1611
+ *
1612
+ * @param toolCallId - Unique identifier for the tool call
1613
+ * @param toolName - Name of the tool being executed
1614
+ * @param timestamp - Start timestamp in milliseconds
1615
+ * @param index - Content block index (default: 0)
1616
+ * @returns A tool_execution_start StreamEvent
1617
+ */
1618
+ declare function toolExecutionStart(toolCallId: string, toolName: string, timestamp: number, index?: number): StreamEvent;
1619
+ /**
1620
+ * Creates a tool execution end stream event.
1621
+ *
1622
+ * @param toolCallId - Unique identifier for the tool call
1623
+ * @param toolName - Name of the tool that was executed
1624
+ * @param result - The result from the tool execution
1625
+ * @param isError - Whether the execution resulted in an error
1626
+ * @param timestamp - End timestamp in milliseconds
1627
+ * @param index - Content block index (default: 0)
1628
+ * @returns A tool_execution_end StreamEvent
1629
+ */
1630
+ declare function toolExecutionEnd(toolCallId: string, toolName: string, result: unknown, isError: boolean, timestamp: number, index?: number): StreamEvent;
1583
1631
 
1584
1632
  /**
1585
1633
  * @fileoverview Error types for the Unified Provider Protocol.
@@ -1913,6 +1961,198 @@ declare class Image {
1913
1961
  static fromBlock(block: ImageBlock): Image;
1914
1962
  }
1915
1963
 
1964
+ /**
1965
+ * @fileoverview Middleware types for the Universal Provider Protocol.
1966
+ *
1967
+ * Defines the interfaces for composable middleware that can intercept and
1968
+ * transform requests, responses, and stream events across all modalities.
1969
+ *
1970
+ * @module types/middleware
1971
+ */
1972
+
1973
+ /**
1974
+ * Modality discriminator for middleware context.
1975
+ */
1976
+ type MiddlewareModality = 'llm' | 'embedding' | 'image';
1977
+ /**
1978
+ * Union type for all request types across modalities.
1979
+ */
1980
+ type AnyRequest = LLMRequest | EmbeddingRequest | ImageRequest;
1981
+ /**
1982
+ * Union type for all response types across modalities.
1983
+ */
1984
+ type AnyResponse = LLMResponse | EmbeddingResponse | ImageResponse;
1985
+ /**
1986
+ * Shared context passed to all middleware hooks.
1987
+ *
1988
+ * Provides access to request/response data, timing information,
1989
+ * and a shared state map for passing data between middleware.
1990
+ *
1991
+ * @example
1992
+ * ```typescript
1993
+ * const loggingMiddleware: Middleware = {
1994
+ * name: 'logging',
1995
+ * onStart(ctx) {
1996
+ * ctx.state.set('requestId', crypto.randomUUID());
1997
+ * },
1998
+ * onEnd(ctx) {
1999
+ * const duration = ctx.endTime! - ctx.startTime;
2000
+ * console.log(`[${ctx.provider}] ${ctx.state.get('requestId')} completed in ${duration}ms`);
2001
+ * },
2002
+ * };
2003
+ * ```
2004
+ */
2005
+ interface MiddlewareContext {
2006
+ /** The modality being used */
2007
+ readonly modality: MiddlewareModality;
2008
+ /** Model ID */
2009
+ readonly modelId: string;
2010
+ /** Provider name */
2011
+ readonly provider: string;
2012
+ /** Whether this is a streaming request */
2013
+ readonly streaming: boolean;
2014
+ /** Request object (modality-specific, mutable for onRequest hook) */
2015
+ request: AnyRequest;
2016
+ /** Response object (populated after execution, mutable for onResponse hook) */
2017
+ response?: AnyResponse;
2018
+ /** Shared state across middleware - use for passing data between hooks */
2019
+ readonly state: Map<string, unknown>;
2020
+ /** Request start timestamp in milliseconds */
2021
+ readonly startTime: number;
2022
+ /** Request end timestamp in milliseconds (set after completion) */
2023
+ endTime?: number;
2024
+ }
2025
+ /**
2026
+ * Context for stream event hooks.
2027
+ *
2028
+ * Provides a shared state map for middleware to store and retrieve data.
2029
+ * Middleware that need to accumulate text or other data should manage
2030
+ * their own state using the provided state map.
2031
+ *
2032
+ * @example
2033
+ * ```typescript
2034
+ * const filterMiddleware: Middleware = {
2035
+ * name: 'reasoning-filter',
2036
+ * onStreamEvent(event, ctx) {
2037
+ * // Filter out reasoning events
2038
+ * if (event.type === StreamEventType.ReasoningDelta) {
2039
+ * return null;
2040
+ * }
2041
+ * return event;
2042
+ * },
2043
+ * };
2044
+ * ```
2045
+ */
2046
+ interface StreamContext {
2047
+ /** Shared state (same reference as MiddlewareContext.state) */
2048
+ readonly state: Map<string, unknown>;
2049
+ }
2050
+ /**
2051
+ * Middleware interface with optional hooks.
2052
+ *
2053
+ * Implement only the hooks you need. Middleware are executed in array order
2054
+ * for request/start hooks, and reverse order for response/end hooks.
2055
+ *
2056
+ * @example
2057
+ * ```typescript
2058
+ * import { type Middleware, StreamEventType } from '@providerprotocol/ai';
2059
+ *
2060
+ * const customMiddleware: Middleware = {
2061
+ * name: 'request-id',
2062
+ *
2063
+ * onRequest(ctx) {
2064
+ * ctx.state.set('requestId', crypto.randomUUID());
2065
+ * },
2066
+ *
2067
+ * onStreamEvent(event, ctx) {
2068
+ * // Filter out reasoning events
2069
+ * if (event.type === StreamEventType.ReasoningDelta) {
2070
+ * return null;
2071
+ * }
2072
+ * return event;
2073
+ * },
2074
+ *
2075
+ * onEnd(ctx) {
2076
+ * const duration = ctx.endTime! - ctx.startTime;
2077
+ * console.log(`Request ${ctx.state.get('requestId')} took ${duration}ms`);
2078
+ * },
2079
+ * };
2080
+ * ```
2081
+ */
2082
+ interface Middleware {
2083
+ /** Middleware name for debugging and logging */
2084
+ readonly name: string;
2085
+ /**
2086
+ * Called when generate/stream starts, before any provider execution.
2087
+ *
2088
+ * @param ctx - The middleware context
2089
+ */
2090
+ onStart?(ctx: MiddlewareContext): void | Promise<void>;
2091
+ /**
2092
+ * Called when generate/stream completes successfully.
2093
+ * Called in reverse middleware order.
2094
+ *
2095
+ * @param ctx - The middleware context with response populated
2096
+ */
2097
+ onEnd?(ctx: MiddlewareContext): void | Promise<void>;
2098
+ /**
2099
+ * Called on any error during execution.
2100
+ * Called for all middleware that have this hook, regardless of order.
2101
+ *
2102
+ * @param error - The error that occurred
2103
+ * @param ctx - The middleware context
2104
+ */
2105
+ onError?(error: Error, ctx: MiddlewareContext): void | Promise<void>;
2106
+ /**
2107
+ * Called before provider execution. Can modify the request.
2108
+ *
2109
+ * @param ctx - The middleware context with mutable request
2110
+ */
2111
+ onRequest?(ctx: MiddlewareContext): void | Promise<void>;
2112
+ /**
2113
+ * Called after provider execution. Can modify the response.
2114
+ * Called in reverse middleware order.
2115
+ *
2116
+ * @param ctx - The middleware context with mutable response
2117
+ */
2118
+ onResponse?(ctx: MiddlewareContext): void | Promise<void>;
2119
+ /**
2120
+ * Called for each stream event. Can transform, filter, or expand events.
2121
+ *
2122
+ * Return values:
2123
+ * - `StreamEvent` - Pass through (potentially modified)
2124
+ * - `StreamEvent[]` - Expand into multiple events
2125
+ * - `null` - Filter out this event
2126
+ *
2127
+ * @param event - The stream event to process
2128
+ * @param ctx - The stream context
2129
+ * @returns Transformed event(s) or null to filter
2130
+ */
2131
+ onStreamEvent?(event: StreamEvent, ctx: StreamContext): StreamEvent | StreamEvent[] | null;
2132
+ /**
2133
+ * Called when stream completes, after all events have been processed.
2134
+ *
2135
+ * @param ctx - The stream context
2136
+ */
2137
+ onStreamEnd?(ctx: StreamContext): void | Promise<void>;
2138
+ /**
2139
+ * Called when a tool is about to be executed.
2140
+ *
2141
+ * @param tool - The tool being called
2142
+ * @param params - The parameters for the tool call
2143
+ * @param ctx - The middleware context
2144
+ */
2145
+ onToolCall?(tool: Tool, params: unknown, ctx: MiddlewareContext): void | Promise<void>;
2146
+ /**
2147
+ * Called after tool execution completes.
2148
+ *
2149
+ * @param tool - The tool that was executed
2150
+ * @param result - The result from the tool
2151
+ * @param ctx - The middleware context
2152
+ */
2153
+ onToolResult?(tool: Tool, result: unknown, ctx: MiddlewareContext): void | Promise<void>;
2154
+ }
2155
+
1916
2156
  /**
1917
2157
  * @fileoverview Image generation types for the Universal Provider Protocol.
1918
2158
  *
@@ -1959,6 +2199,13 @@ interface ImageOptions<TParams = unknown> {
1959
2199
  config?: ProviderConfig;
1960
2200
  /** Provider-specific parameters (passed through unchanged) */
1961
2201
  params?: TParams;
2202
+ /**
2203
+ * Middleware for intercepting and transforming requests and responses.
2204
+ *
2205
+ * Middleware are executed in array order for request/start hooks,
2206
+ * and reverse order for response/end hooks.
2207
+ */
2208
+ middleware?: Middleware[];
1962
2209
  }
1963
2210
  /**
1964
2211
  * Options for image generation.
@@ -2506,6 +2753,8 @@ interface EmbeddingRequest<TParams = unknown> {
2506
2753
  config: ProviderConfig;
2507
2754
  /** Abort signal for cancellation */
2508
2755
  signal?: AbortSignal;
2756
+ /** Input type hint for provider-specific optimization */
2757
+ inputType?: 'document' | 'query';
2509
2758
  }
2510
2759
  /**
2511
2760
  * Response from provider's embed method.
@@ -2959,6 +3208,24 @@ interface LLMOptions<TParams = unknown> {
2959
3208
  toolStrategy?: ToolUseStrategy;
2960
3209
  /** Structured output schema (JSON Schema) */
2961
3210
  structure?: JSONSchema;
3211
+ /**
3212
+ * Middleware for intercepting and transforming requests, responses, and streams.
3213
+ *
3214
+ * Middleware are executed in array order for request/start hooks,
3215
+ * and reverse order for response/end hooks.
3216
+ *
3217
+ * @example
3218
+ * ```typescript
3219
+ * const model = llm({
3220
+ * model: anthropic('claude-sonnet-4-20250514'),
3221
+ * middleware: [
3222
+ * loggingMiddleware(),
3223
+ * parsedObjectMiddleware(),
3224
+ * ],
3225
+ * });
3226
+ * ```
3227
+ */
3228
+ middleware?: Middleware[];
2962
3229
  }
2963
3230
  /**
2964
3231
  * LLM instance returned by the llm() function.
@@ -3115,4 +3382,4 @@ interface BoundLLMModel<TParams = unknown> {
3115
3382
  stream(request: LLMRequest<TParams>): LLMStreamResult;
3116
3383
  }
3117
3384
 
3118
- export { MessageRole as $, type AudioBlock as A, type BinaryBlock as B, type ContentBlock as C, type DocumentSource as D, type EmbeddingHandler as E, isBinaryBlock as F, type Tool as G, type ToolCall as H, type ImageOptions as I, type JSONSchema as J, type ToolResult as K, type LLMOptions as L, type ModelReference as M, type ToolMetadata as N, type ToolUseStrategy as O, type Provider as P, type BeforeCallResult as Q, type ReasoningBlock as R, type AfterCallResult as S, type TextBlock as T, UPPError as U, type VideoBlock as V, type ToolExecution as W, Message as X, UserMessage as Y, AssistantMessage as Z, ToolResultMessage as _, type LLMInstance as a, isUserMessage as a0, isAssistantMessage as a1, isToolResultMessage as a2, type MessageType as a3, type MessageMetadata as a4, type MessageOptions as a5, type Turn as a6, type TokenUsage as a7, createTurn as a8, emptyUsage as a9, type EmbeddingUsage as aA, type EmbeddingInput as aB, type LLMCapabilities as aC, type LLMRequest as aD, type LLMResponse as aE, type LLMStreamResult as aF, type BoundLLMModel as aG, type InferenceInput as aH, type ImageInput as aI, type ImageEditInput as aJ, type ImageGenerateOptions as aK, type GeneratedImage as aL, type ImageUsage as aM, type ImageResult as aN, type ImageStreamEvent as aO, type ImageStreamResult as aP, type ImageCapabilities as aQ, type ImageRequest as aR, type ImageEditRequest as aS, type ImageResponse as aT, type ImageProviderStreamResult as aU, type BoundImageModel as aV, type ImageModelInput as aW, type TurnJSON as aX, aggregateUsage as aa, Thread as ab, type ThreadJSON as ac, type MessageJSON as ad, type StreamEvent as ae, type EventDelta as af, type StreamResult as ag, StreamEventType as ah, createStreamResult as ai, textDelta as aj, toolCallDelta as ak, messageStart as al, messageStop as am, contentBlockStart as an, contentBlockStop as ao, type ProviderIdentity as ap, type ProviderConfig as aq, type KeyStrategy as ar, type RetryStrategy as as, type LLMProvider as at, type EmbeddingProvider as au, type ImageProvider as av, type BoundEmbeddingModel as aw, type EmbeddingRequest as ax, type EmbeddingResponse as ay, type EmbeddingVector as az, type ImageInstance as b, type LLMHandler as c, type ImageHandler as d, type DocumentBlock as e, Image as f, ErrorCode as g, ModalityType as h, type Modality as i, type JSONSchemaProperty as j, type JSONSchemaPropertyType as k, type ImageBlock as l, type ImageSource as m, type UserContent as n, type AssistantContent as o, ContentBlockType as p, ImageSourceType as q, DocumentSourceType as r, reasoning as s, text as t, isTextBlock as u, isReasoningBlock as v, isImageBlock as w, isDocumentBlock as x, isAudioBlock as y, isVideoBlock as z };
3385
+ export { UserMessage as $, type AudioBlock as A, type BinaryBlock as B, type ContentBlock as C, type DocumentSource as D, type EmbeddingHandler as E, isAudioBlock as F, isVideoBlock as G, isBinaryBlock as H, type ImageOptions as I, type JSONSchema as J, type Tool as K, type LLMOptions as L, type ModelReference as M, type ToolCall as N, type ToolResult as O, type Provider as P, type ToolMetadata as Q, type ReasoningBlock as R, type StreamEvent as S, type TextBlock as T, UPPError as U, type VideoBlock as V, type ToolUseStrategy as W, type BeforeCallResult as X, type AfterCallResult as Y, type ToolExecution as Z, Message as _, type LLMInstance as a, type MiddlewareContext as a$, AssistantMessage as a0, ToolResultMessage as a1, MessageRole as a2, isUserMessage as a3, isAssistantMessage as a4, isToolResultMessage as a5, type MessageType as a6, type MessageMetadata as a7, type MessageOptions as a8, type Turn as a9, type BoundEmbeddingModel as aA, type EmbeddingRequest as aB, type EmbeddingResponse as aC, type EmbeddingVector as aD, type EmbeddingUsage as aE, type EmbeddingInput as aF, type LLMCapabilities as aG, type LLMRequest as aH, type LLMResponse as aI, type LLMStreamResult as aJ, type BoundLLMModel as aK, type InferenceInput as aL, type ImageInput as aM, type ImageEditInput as aN, type ImageGenerateOptions as aO, type GeneratedImage as aP, type ImageUsage as aQ, type ImageResult as aR, type ImageStreamEvent as aS, type ImageStreamResult as aT, type ImageCapabilities as aU, type ImageRequest as aV, type ImageEditRequest as aW, type ImageResponse as aX, type ImageProviderStreamResult as aY, type BoundImageModel as aZ, type ImageModelInput as a_, type TokenUsage as aa, createTurn as ab, emptyUsage as ac, aggregateUsage as ad, Thread as ae, type ThreadJSON as af, type MessageJSON as ag, type StreamResult as ah, StreamEventType as ai, createStreamResult as aj, textDelta as ak, toolCallDelta as al, objectDelta as am, messageStart as an, messageStop as ao, contentBlockStart as ap, contentBlockStop as aq, toolExecutionStart as ar, toolExecutionEnd as as, type ProviderIdentity as at, type ProviderConfig as au, type KeyStrategy as av, type RetryStrategy as aw, type LLMProvider as ax, type EmbeddingProvider as ay, type ImageProvider as az, type ImageInstance as b, type StreamContext as b0, type MiddlewareModality as b1, type AnyRequest as b2, type AnyResponse as b3, type TurnJSON as b4, type LLMHandler as c, type ImageHandler as d, type DocumentBlock as e, type Middleware as f, type EventDelta as g, Image as h, ErrorCode as i, ModalityType as j, type Modality as k, type JSONSchemaProperty as l, type JSONSchemaPropertyType as m, type ImageBlock as n, type ImageSource as o, type UserContent as p, type AssistantContent as q, ContentBlockType as r, ImageSourceType as s, DocumentSourceType as t, text as u, reasoning as v, isTextBlock as w, isReasoningBlock as x, isImageBlock as y, isDocumentBlock as z };
@@ -1,4 +1,4 @@
1
- import { P as Provider } from '../llm-DgDEy9il.js';
1
+ import { P as Provider } from '../llm-BQJZj3cD.js';
2
2
 
3
3
  /**
4
4
  * @fileoverview Type definitions for the Ollama provider.
@@ -2,8 +2,9 @@ import {
2
2
  parseJsonResponse
3
3
  } from "../chunk-3C7O2RNO.js";
4
4
  import {
5
- StreamEventType
6
- } from "../chunk-73IIE3QT.js";
5
+ StreamEventType,
6
+ objectDelta
7
+ } from "../chunk-6S222DHN.js";
7
8
  import {
8
9
  AssistantMessage,
9
10
  createProvider,
@@ -562,7 +563,11 @@ function createLLMHandler() {
562
563
  }
563
564
  const events = transformStreamChunk(chunk, state);
564
565
  for (const event of events) {
565
- yield event;
566
+ if (request.structure && event.type === StreamEventType.TextDelta) {
567
+ yield objectDelta(event.delta.text ?? "", event.index);
568
+ } else {
569
+ yield event;
570
+ }
566
571
  }
567
572
  }
568
573
  responseResolve(buildResponseFromState(state));