ai-sdk-graph 0.1.3 → 0.1.4

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.
Files changed (2) hide show
  1. package/dist/index.js +11 -7
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -98,8 +98,12 @@ class Graph {
98
98
  let context;
99
99
  return createUIMessageStream({
100
100
  execute: async ({ writer }) => {
101
- context = await this.createExecutionContext(runId, initialState, writer);
102
- await this.onStart({ state: context.state, writer });
101
+ const result = await this.createExecutionContext(runId, initialState, writer);
102
+ context = result.context;
103
+ const firstTime = result.firstTime;
104
+ if (firstTime) {
105
+ await this.onStart({ state: context.state, writer });
106
+ }
103
107
  await this.runExecutionLoop(context);
104
108
  },
105
109
  onFinish: async () => {
@@ -110,7 +114,7 @@ class Graph {
110
114
  });
111
115
  }
112
116
  async executeInternal(runId, initialState, writer) {
113
- const context = await this.createExecutionContext(runId, initialState, writer);
117
+ const { context } = await this.createExecutionContext(runId, initialState, writer);
114
118
  await this.runExecutionLoopInternal(context);
115
119
  return context.state;
116
120
  }
@@ -124,8 +128,8 @@ class Graph {
124
128
  this.edgeRegistry.set(edge.from, existingEdges);
125
129
  }
126
130
  async createExecutionContext(runId, initialState, writer) {
127
- const restored = await this.restoreCheckpoint(runId, initialState);
128
- return { runId, writer, ...restored };
131
+ const { context, firstTime } = await this.restoreCheckpoint(runId, initialState);
132
+ return { context: { ...context, runId, writer }, firstTime };
129
133
  }
130
134
  async runExecutionLoop(context) {
131
135
  await this.executeWithStrategy(context, "return");
@@ -177,9 +181,9 @@ class Graph {
177
181
  async restoreCheckpoint(runId, initialState) {
178
182
  const checkpoint = await this.storage.load(runId);
179
183
  if (this.isValidCheckpoint(checkpoint)) {
180
- return this.restoreFromCheckpoint(checkpoint, initialState);
184
+ return { context: this.restoreFromCheckpoint(checkpoint, initialState), firstTime: false };
181
185
  }
182
- return this.createFreshExecution(initialState);
186
+ return { context: this.createFreshExecution(initialState), firstTime: true };
183
187
  }
184
188
  isValidCheckpoint(checkpoint) {
185
189
  return this.hasNodeIds(checkpoint) && this.hasAtLeastOneNode(checkpoint);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-sdk-graph",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Graph-based workflows for the AI SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",