pipeai 0.8.0 → 0.8.2

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.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { Tool, ToolExecutionOptions, UIMessageStreamWriter, FlexibleSchema, streamText, generateText, Output, LanguageModel, ModelMessage, ToolChoice, ToolSet, StopCondition, OnStepFinishEvent, OnFinishEvent, GenerateTextResult as GenerateTextResult$1, StreamTextResult as StreamTextResult$1 } from 'ai';
1
+ import { Tool, ToolExecutionOptions, UIMessageStreamWriter, FlexibleSchema, streamText, generateText, Output, LanguageModel, ModelMessage, ToolChoice, ToolSet, StopCondition, OnStepFinishEvent, OnFinishEvent, GenerateTextResult as GenerateTextResult$1, StreamTextResult as StreamTextResult$1, UIMessage, UIMessageStreamOnFinishCallback, IdGenerator } from 'ai';
2
2
  import { ZodType } from 'zod';
3
3
 
4
4
  declare const TOOL_PROVIDER_BRAND: unique symbol;
@@ -443,6 +443,7 @@ interface AgentStepHooks<TContext, TOutput, TNextOutput> {
443
443
  result: StreamTextResult<ToolSet, OutputType<TNextOutput>>;
444
444
  writer: UIMessageStreamWriter;
445
445
  ctx: Readonly<TContext>;
446
+ input: TOutput;
446
447
  }) => MaybePromise<void>;
447
448
  }
448
449
  type StepOptions<TContext, TOutput, TNextOutput> = AgentStepHooks<TContext, TOutput, TNextOutput> & {
@@ -490,9 +491,50 @@ interface WorkflowStreamResult<TOutput> {
490
491
  stream: ReadableStream;
491
492
  output: Promise<WorkflowResult<TOutput>>;
492
493
  }
493
- interface WorkflowStreamOptions {
494
+ /**
495
+ * Options for `Workflow.stream` / `ResumedWorkflow.stream` /
496
+ * `CheckpointResumedWorkflow.stream`. Forwarded verbatim to the AI SDK's
497
+ * `createUIMessageStream`.
498
+ *
499
+ * Generic over the UI message shape so consumers with a custom
500
+ * `UIMessage<METADATA, DATA_PARTS, TOOLS>` get their narrowed type in
501
+ * `onFinish` / `originalMessages` instead of the unparameterized default.
502
+ *
503
+ * Note: AI SDK's `createUIMessageStream` ALSO accepts an `onStepFinish`
504
+ * (per-token-step) callback. We intentionally do NOT expose it here — there
505
+ * are already two clearer step-finish callbacks at different granularities:
506
+ * - `Agent.onStepFinish` for per-model-call observation, and
507
+ * - `WorkflowObservability.onStepFinish` for per-workflow-step observation.
508
+ * Adding a third one named the same thing on `WorkflowStreamOptions` would
509
+ * be confusing. Reach for one of the two above instead.
510
+ */
511
+ interface WorkflowStreamOptions<UI_MESSAGE extends UIMessage = UIMessage> {
512
+ /**
513
+ * Map an unknown error into a user-visible string. Forwarded as-is to
514
+ * `createUIMessageStream`'s `onError`. Returning `string` is required by
515
+ * the AI SDK — the string is what the stream emits to clients.
516
+ */
494
517
  onError?: (error: unknown) => string;
495
- onFinish?: () => MaybePromise<void>;
518
+ /**
519
+ * Prior `UIMessage`s the stream should continue from. When provided, the
520
+ * AI SDK assumes persistence mode and assigns a response-message id.
521
+ * Used for chat resumption / continuation flows.
522
+ */
523
+ originalMessages?: UI_MESSAGE[];
524
+ /**
525
+ * Fires once the stream finishes, with the full payload the AI SDK
526
+ * delivers: the updated `messages` array, the freshly-emitted
527
+ * `responseMessage`, `isAborted` / `isContinuation` flags, and the
528
+ * `finishReason`. Use this for persistence, analytics, or downstream
529
+ * notification.
530
+ */
531
+ onFinish?: UIMessageStreamOnFinishCallback<UI_MESSAGE>;
532
+ /**
533
+ * Override the response message-id generator. Forwarded to
534
+ * `createUIMessageStream`'s `generateId` option. Useful for deterministic
535
+ * IDs in tests or coordinating with a server-side ID space.
536
+ */
537
+ generateId?: IdGenerator;
496
538
  }
497
539
  type LoopPredicate<TContext, TOutput> = (params: {
498
540
  output: TOutput;
@@ -652,7 +694,7 @@ declare class SealedWorkflow<TContext, TInput = void, TOutput = void, TGates ext
652
694
  protected fireHook<K extends keyof WorkflowObservability, E extends Parameters<NonNullable<WorkflowObservability[K]>>[0]>(state: RuntimeState, name: K, event: E): MaybePromise<unknown>;
653
695
  private fireHookSlow;
654
696
  generate(ctx: TContext, ...args: TInput extends void ? [input?: TInput, opts?: RunOptions] : [input: TInput, opts?: RunOptions]): Promise<WorkflowResult<TOutput>>;
655
- stream(ctx: TContext, ...args: TInput extends void ? [input?: TInput, options?: WorkflowStreamOptions, opts?: RunOptions] : [input: TInput, options?: WorkflowStreamOptions, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
697
+ stream<UI_MESSAGE extends UIMessage = UIMessage>(ctx: TContext, ...args: TInput extends void ? [input?: TInput, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions] : [input: TInput, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
656
698
  protected buildResult(state: RuntimeState): WorkflowResult<TOutput>;
657
699
  protected execute(state: RuntimeState, startIndex?: number, opts?: RunOptions, initialError?: PendingError | null): Promise<void>;
658
700
  protected executeNestedWorkflow(state: RuntimeState, workflow: SealedWorkflow<TContext, unknown, unknown, any>): Promise<void>;
@@ -704,7 +746,7 @@ declare class ResumedWorkflow<TContext, TResponse = unknown, TOutput = void> ext
704
746
  constructor(steps: ReadonlyArray<StepNode>, startIndex: number, config: ResumedWorkflowConfig);
705
747
  private validateResponse;
706
748
  generate(ctx: TContext, ...args: TResponse extends void ? [response?: TResponse, opts?: RunOptions] : [response: TResponse, opts?: RunOptions]): Promise<WorkflowResult<TOutput>>;
707
- stream(ctx: TContext, ...args: TResponse extends void ? [response?: TResponse, options?: WorkflowStreamOptions, opts?: RunOptions] : [response: TResponse, options?: WorkflowStreamOptions, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
749
+ stream<UI_MESSAGE extends UIMessage = UIMessage>(ctx: TContext, ...args: TResponse extends void ? [response?: TResponse, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions] : [response: TResponse, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
708
750
  }
709
751
  declare class CheckpointResumedWorkflow<TContext, TOutput = void> extends SealedWorkflow<TContext, void, TOutput> {
710
752
  private readonly startIndex;
@@ -712,7 +754,7 @@ declare class CheckpointResumedWorkflow<TContext, TOutput = void> extends Sealed
712
754
  /** @internal */
713
755
  constructor(steps: ReadonlyArray<StepNode>, startIndex: number, config: ResumedWorkflowConfig);
714
756
  generate(ctx: TContext, ...args: [input?: void, opts?: RunOptions]): Promise<WorkflowResult<TOutput>>;
715
- stream(ctx: TContext, ...args: [input?: void, options?: WorkflowStreamOptions, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
757
+ stream<UI_MESSAGE extends UIMessage = UIMessage>(ctx: TContext, ...args: [input?: void, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
716
758
  }
717
759
  declare class Workflow<TContext, TInput = void, TOutput = void, TGates extends Record<string, unknown> = {}> extends SealedWorkflow<TContext, TInput, TOutput, TGates> {
718
760
  /**
@@ -733,6 +775,7 @@ declare class Workflow<TContext, TInput = void, TOutput = void, TGates extends R
733
775
  step<TNextOutput>(id: string, fn: (params: {
734
776
  ctx: Readonly<TContext>;
735
777
  input: TOutput;
778
+ writer?: UIMessageStreamWriter;
736
779
  }) => MaybePromise<TNextOutput>): Workflow<TContext, TInput, TNextOutput, TGates>;
737
780
  gate<TResponse = TOutput, Id extends string = string>(id: Id & (Id extends keyof TGates ? never : Id), options?: {
738
781
  payload?: (params: {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Tool, ToolExecutionOptions, UIMessageStreamWriter, FlexibleSchema, streamText, generateText, Output, LanguageModel, ModelMessage, ToolChoice, ToolSet, StopCondition, OnStepFinishEvent, OnFinishEvent, GenerateTextResult as GenerateTextResult$1, StreamTextResult as StreamTextResult$1 } from 'ai';
1
+ import { Tool, ToolExecutionOptions, UIMessageStreamWriter, FlexibleSchema, streamText, generateText, Output, LanguageModel, ModelMessage, ToolChoice, ToolSet, StopCondition, OnStepFinishEvent, OnFinishEvent, GenerateTextResult as GenerateTextResult$1, StreamTextResult as StreamTextResult$1, UIMessage, UIMessageStreamOnFinishCallback, IdGenerator } from 'ai';
2
2
  import { ZodType } from 'zod';
3
3
 
4
4
  declare const TOOL_PROVIDER_BRAND: unique symbol;
@@ -443,6 +443,7 @@ interface AgentStepHooks<TContext, TOutput, TNextOutput> {
443
443
  result: StreamTextResult<ToolSet, OutputType<TNextOutput>>;
444
444
  writer: UIMessageStreamWriter;
445
445
  ctx: Readonly<TContext>;
446
+ input: TOutput;
446
447
  }) => MaybePromise<void>;
447
448
  }
448
449
  type StepOptions<TContext, TOutput, TNextOutput> = AgentStepHooks<TContext, TOutput, TNextOutput> & {
@@ -490,9 +491,50 @@ interface WorkflowStreamResult<TOutput> {
490
491
  stream: ReadableStream;
491
492
  output: Promise<WorkflowResult<TOutput>>;
492
493
  }
493
- interface WorkflowStreamOptions {
494
+ /**
495
+ * Options for `Workflow.stream` / `ResumedWorkflow.stream` /
496
+ * `CheckpointResumedWorkflow.stream`. Forwarded verbatim to the AI SDK's
497
+ * `createUIMessageStream`.
498
+ *
499
+ * Generic over the UI message shape so consumers with a custom
500
+ * `UIMessage<METADATA, DATA_PARTS, TOOLS>` get their narrowed type in
501
+ * `onFinish` / `originalMessages` instead of the unparameterized default.
502
+ *
503
+ * Note: AI SDK's `createUIMessageStream` ALSO accepts an `onStepFinish`
504
+ * (per-token-step) callback. We intentionally do NOT expose it here — there
505
+ * are already two clearer step-finish callbacks at different granularities:
506
+ * - `Agent.onStepFinish` for per-model-call observation, and
507
+ * - `WorkflowObservability.onStepFinish` for per-workflow-step observation.
508
+ * Adding a third one named the same thing on `WorkflowStreamOptions` would
509
+ * be confusing. Reach for one of the two above instead.
510
+ */
511
+ interface WorkflowStreamOptions<UI_MESSAGE extends UIMessage = UIMessage> {
512
+ /**
513
+ * Map an unknown error into a user-visible string. Forwarded as-is to
514
+ * `createUIMessageStream`'s `onError`. Returning `string` is required by
515
+ * the AI SDK — the string is what the stream emits to clients.
516
+ */
494
517
  onError?: (error: unknown) => string;
495
- onFinish?: () => MaybePromise<void>;
518
+ /**
519
+ * Prior `UIMessage`s the stream should continue from. When provided, the
520
+ * AI SDK assumes persistence mode and assigns a response-message id.
521
+ * Used for chat resumption / continuation flows.
522
+ */
523
+ originalMessages?: UI_MESSAGE[];
524
+ /**
525
+ * Fires once the stream finishes, with the full payload the AI SDK
526
+ * delivers: the updated `messages` array, the freshly-emitted
527
+ * `responseMessage`, `isAborted` / `isContinuation` flags, and the
528
+ * `finishReason`. Use this for persistence, analytics, or downstream
529
+ * notification.
530
+ */
531
+ onFinish?: UIMessageStreamOnFinishCallback<UI_MESSAGE>;
532
+ /**
533
+ * Override the response message-id generator. Forwarded to
534
+ * `createUIMessageStream`'s `generateId` option. Useful for deterministic
535
+ * IDs in tests or coordinating with a server-side ID space.
536
+ */
537
+ generateId?: IdGenerator;
496
538
  }
497
539
  type LoopPredicate<TContext, TOutput> = (params: {
498
540
  output: TOutput;
@@ -652,7 +694,7 @@ declare class SealedWorkflow<TContext, TInput = void, TOutput = void, TGates ext
652
694
  protected fireHook<K extends keyof WorkflowObservability, E extends Parameters<NonNullable<WorkflowObservability[K]>>[0]>(state: RuntimeState, name: K, event: E): MaybePromise<unknown>;
653
695
  private fireHookSlow;
654
696
  generate(ctx: TContext, ...args: TInput extends void ? [input?: TInput, opts?: RunOptions] : [input: TInput, opts?: RunOptions]): Promise<WorkflowResult<TOutput>>;
655
- stream(ctx: TContext, ...args: TInput extends void ? [input?: TInput, options?: WorkflowStreamOptions, opts?: RunOptions] : [input: TInput, options?: WorkflowStreamOptions, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
697
+ stream<UI_MESSAGE extends UIMessage = UIMessage>(ctx: TContext, ...args: TInput extends void ? [input?: TInput, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions] : [input: TInput, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
656
698
  protected buildResult(state: RuntimeState): WorkflowResult<TOutput>;
657
699
  protected execute(state: RuntimeState, startIndex?: number, opts?: RunOptions, initialError?: PendingError | null): Promise<void>;
658
700
  protected executeNestedWorkflow(state: RuntimeState, workflow: SealedWorkflow<TContext, unknown, unknown, any>): Promise<void>;
@@ -704,7 +746,7 @@ declare class ResumedWorkflow<TContext, TResponse = unknown, TOutput = void> ext
704
746
  constructor(steps: ReadonlyArray<StepNode>, startIndex: number, config: ResumedWorkflowConfig);
705
747
  private validateResponse;
706
748
  generate(ctx: TContext, ...args: TResponse extends void ? [response?: TResponse, opts?: RunOptions] : [response: TResponse, opts?: RunOptions]): Promise<WorkflowResult<TOutput>>;
707
- stream(ctx: TContext, ...args: TResponse extends void ? [response?: TResponse, options?: WorkflowStreamOptions, opts?: RunOptions] : [response: TResponse, options?: WorkflowStreamOptions, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
749
+ stream<UI_MESSAGE extends UIMessage = UIMessage>(ctx: TContext, ...args: TResponse extends void ? [response?: TResponse, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions] : [response: TResponse, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
708
750
  }
709
751
  declare class CheckpointResumedWorkflow<TContext, TOutput = void> extends SealedWorkflow<TContext, void, TOutput> {
710
752
  private readonly startIndex;
@@ -712,7 +754,7 @@ declare class CheckpointResumedWorkflow<TContext, TOutput = void> extends Sealed
712
754
  /** @internal */
713
755
  constructor(steps: ReadonlyArray<StepNode>, startIndex: number, config: ResumedWorkflowConfig);
714
756
  generate(ctx: TContext, ...args: [input?: void, opts?: RunOptions]): Promise<WorkflowResult<TOutput>>;
715
- stream(ctx: TContext, ...args: [input?: void, options?: WorkflowStreamOptions, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
757
+ stream<UI_MESSAGE extends UIMessage = UIMessage>(ctx: TContext, ...args: [input?: void, options?: WorkflowStreamOptions<UI_MESSAGE>, opts?: RunOptions]): WorkflowStreamResult<TOutput>;
716
758
  }
717
759
  declare class Workflow<TContext, TInput = void, TOutput = void, TGates extends Record<string, unknown> = {}> extends SealedWorkflow<TContext, TInput, TOutput, TGates> {
718
760
  /**
@@ -733,6 +775,7 @@ declare class Workflow<TContext, TInput = void, TOutput = void, TGates extends R
733
775
  step<TNextOutput>(id: string, fn: (params: {
734
776
  ctx: Readonly<TContext>;
735
777
  input: TOutput;
778
+ writer?: UIMessageStreamWriter;
736
779
  }) => MaybePromise<TNextOutput>): Workflow<TContext, TInput, TNextOutput, TGates>;
737
780
  gate<TResponse = TOutput, Id extends string = string>(id: Id & (Id extends keyof TGates ? never : Id), options?: {
738
781
  payload?: (params: {
package/dist/index.js CHANGED
@@ -677,7 +677,9 @@ var SealedWorkflow = class _SealedWorkflow {
677
677
  }
678
678
  },
679
679
  ...options?.onError ? { onError: options.onError } : {},
680
- ...options?.onFinish ? { onFinish: options.onFinish } : {}
680
+ ...options?.onFinish ? { onFinish: options.onFinish } : {},
681
+ ...options?.originalMessages ? { originalMessages: options.originalMessages } : {},
682
+ ...options?.generateId ? { generateId: options.generateId } : {}
681
683
  });
682
684
  return {
683
685
  stream,
@@ -957,7 +959,7 @@ var SealedWorkflow = class _SealedWorkflow {
957
959
  await runWithWriter(writer, async () => {
958
960
  const result = await agent.stream(ctx, state.output, agentCallOpts);
959
961
  if (options?.handleStream) {
960
- await options.handleStream({ result, writer, ctx });
962
+ await options.handleStream({ result, writer, ctx, input });
961
963
  } else {
962
964
  writer.merge(result.toUIMessageStream());
963
965
  }
@@ -1186,7 +1188,9 @@ var ResumedWorkflow = class extends SealedWorkflow {
1186
1188
  }
1187
1189
  },
1188
1190
  ...options?.onError ? { onError: options.onError } : {},
1189
- ...options?.onFinish ? { onFinish: options.onFinish } : {}
1191
+ ...options?.onFinish ? { onFinish: options.onFinish } : {},
1192
+ ...options?.originalMessages ? { originalMessages: options.originalMessages } : {},
1193
+ ...options?.generateId ? { generateId: options.generateId } : {}
1190
1194
  });
1191
1195
  return { stream, output: outputPromise };
1192
1196
  }
@@ -1248,7 +1252,9 @@ var CheckpointResumedWorkflow = class extends SealedWorkflow {
1248
1252
  }
1249
1253
  },
1250
1254
  ...options?.onError ? { onError: options.onError } : {},
1251
- ...options?.onFinish ? { onFinish: options.onFinish } : {}
1255
+ ...options?.onFinish ? { onFinish: options.onFinish } : {},
1256
+ ...options?.originalMessages ? { originalMessages: options.originalMessages } : {},
1257
+ ...options?.generateId ? { generateId: options.generateId } : {}
1252
1258
  });
1253
1259
  return { stream, output: outputPromise };
1254
1260
  }
@@ -1303,7 +1309,10 @@ var Workflow = class _Workflow extends SealedWorkflow {
1303
1309
  execute: async (state) => {
1304
1310
  state.output = await fn({
1305
1311
  ctx: state.ctx,
1306
- input: state.output
1312
+ input: state.output,
1313
+ // Present in stream mode (undefined in generate mode), letting the
1314
+ // inline step emit UIMessageChunk parts onto the workflow's stream.
1315
+ writer: state.writer
1307
1316
  });
1308
1317
  }
1309
1318
  };