ai-sdk-graph 0.2.0 → 0.3.1

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/graph.d.ts CHANGED
@@ -1,10 +1,8 @@
1
1
  import type { GraphSDK } from './types';
2
- import { createUIMessageStream } from 'ai';
3
2
  export declare class SuspenseError extends Error {
4
3
  readonly data?: unknown;
5
4
  constructor(data?: unknown);
6
5
  }
7
- type Writer = Parameters<Parameters<typeof createUIMessageStream>[0]['execute']>[0]['writer'];
8
6
  export declare class Graph<State extends Record<string, unknown>, NodeKeys extends string = 'START' | 'END'> {
9
7
  private readonly nodeRegistry;
10
8
  private readonly edgeRegistry;
@@ -17,7 +15,7 @@ export declare class Graph<State extends Record<string, unknown>, NodeKeys exten
17
15
  constructor(options?: GraphSDK.GraphOptions<State, NodeKeys>);
18
16
  node<NewKey extends string>(id: NewKey, execute: ({ state, writer, suspense, update }: {
19
17
  state: () => Readonly<State>;
20
- writer: Writer;
18
+ writer: GraphSDK.Writer;
21
19
  suspense: (data?: unknown) => never;
22
20
  update: (update: GraphSDK.StateUpdate<State>) => void;
23
21
  }) => Promise<void> | void): Graph<State, NodeKeys | NewKey>;
@@ -33,7 +31,7 @@ export declare class Graph<State extends Record<string, unknown>, NodeKeys exten
33
31
  direction?: 'TB' | 'LR';
34
32
  }): string;
35
33
  execute(runId: string, initialState: State | ((state: State | undefined) => State)): ReadableStream<import("ai").InferUIMessageChunk<import("ai").UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>>>;
36
- executeInternal(runId: string, initialState: State, writer: Writer): Promise<State>;
34
+ executeInternal(runId: string, initialState: State, writer: GraphSDK.Writer): Promise<State>;
37
35
  private registerBuiltInNodes;
38
36
  private addEdgeToRegistry;
39
37
  private createExecutionContext;
@@ -66,4 +64,3 @@ export declare class Graph<State extends Record<string, unknown>, NodeKeys exten
66
64
  private excludeTerminalNodes;
67
65
  }
68
66
  export declare function graph<State extends Record<string, unknown>, NodeKeys extends string = 'START' | 'END'>(options?: GraphSDK.GraphOptions<State, NodeKeys>): Graph<State, NodeKeys>;
69
- export {};
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './graph';
2
2
  export * from './storage';
3
3
  export * from './types';
4
+ export * from './utils';
package/dist/index.js CHANGED
@@ -407,8 +407,34 @@ class MermaidGenerator {
407
407
  });
408
408
  }
409
409
  }
410
+ // src/utils.ts
411
+ async function consumeAndMergeStream(stream, writer, options) {
412
+ let resolver;
413
+ let rejecter;
414
+ const messages = new Promise((resolve, reject) => {
415
+ resolver = resolve;
416
+ rejecter = reject;
417
+ });
418
+ try {
419
+ const uiMessageStream = stream.toUIMessageStream({
420
+ ...options,
421
+ onFinish: ({ messages: messages2 }) => {
422
+ resolver(messages2);
423
+ },
424
+ onError: (error) => {
425
+ rejecter(error);
426
+ return error instanceof Error ? error.message : "An error occurred.";
427
+ }
428
+ });
429
+ writer.merge(uiMessageStream);
430
+ } catch (error) {
431
+ rejecter(error);
432
+ }
433
+ return messages;
434
+ }
410
435
  export {
411
436
  graph,
437
+ consumeAndMergeStream,
412
438
  SuspenseError,
413
439
  RedisStorage,
414
440
  InMemoryStorage,
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { UIMessageStreamWriter } from 'ai';
1
+ import type { createUIMessageStream, UIMessageStreamWriter } from 'ai';
2
2
  export declare namespace GraphSDK {
3
3
  type StateUpdate<State> = Partial<State> | ((state: State) => Partial<State>);
4
4
  interface SubgraphOptions<ParentState extends Record<string, unknown>, ChildState extends Record<string, unknown>> {
@@ -49,4 +49,5 @@ export declare namespace GraphSDK {
49
49
  writer: UIMessageStreamWriter;
50
50
  }) => Promise<void> | void;
51
51
  }
52
+ type Writer = Parameters<Parameters<typeof createUIMessageStream>[0]['execute']>[0]['writer'];
52
53
  }
@@ -0,0 +1,3 @@
1
+ import type { StreamTextResult, ToolSet, UIMessage } from 'ai';
2
+ import type { GraphSDK } from './types';
3
+ export declare function consumeAndMergeStream<Stream extends StreamTextResult<ToolSet, any>>(stream: Stream, writer: GraphSDK.Writer, options?: Omit<Parameters<Stream['toUIMessageStream']>[0], 'onFinish' | 'onError'>): Promise<UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>[]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-sdk-graph",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Graph-based workflows for the AI SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",