Compare commits

..

4 Commits

Author SHA1 Message Date
github-actions[bot] b39f40dbd8 Release (#1477)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: himself65 <himself65@users.noreply.github.com>
2024-11-13 01:25:40 -08:00
Alex Yang fadc8b8ea0 feat: recoverable data with error handling (#1476) 2024-11-13 01:15:50 -08:00
Alex Yang ea92b6986d chore: update changeset 2024-11-13 01:15:28 -08:00
Alex Yang 17f9022d22 fix: output event check (#1475) 2024-11-13 00:46:35 -08:00
13 changed files with 147 additions and 11 deletions
+6
View File
@@ -1,5 +1,11 @@
# docs
## 0.0.117
### Patch Changes
- @llamaindex/examples@0.0.15
## 0.0.116
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "docs",
"version": "0.0.116",
"version": "0.0.117",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
+8
View File
@@ -1,5 +1,13 @@
# @llamaindex/doc
## 0.0.15
### Patch Changes
- Updated dependencies [ea92b69]
- Updated dependencies [fadc8b8]
- @llamaindex/workflow@0.0.5
## 0.0.14
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/doc",
"version": "0.0.14",
"version": "0.0.15",
"private": true,
"scripts": {
"build": "pnpm run build:docs && next build",
+8
View File
@@ -1,5 +1,13 @@
# examples
## 0.0.15
### Patch Changes
- Updated dependencies [ea92b69]
- Updated dependencies [fadc8b8]
- @llamaindex/workflow@0.0.5
## 0.0.14
### Patch Changes
+2 -2
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/examples",
"private": true,
"version": "0.0.14",
"version": "0.0.15",
"dependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@azure/cosmos": "^4.1.1",
@@ -9,7 +9,7 @@
"@datastax/astra-db-ts": "^1.4.1",
"@llamaindex/core": "^0.4.7",
"@llamaindex/readers": "^1.0.8",
"@llamaindex/workflow": "^0.0.4",
"@llamaindex/workflow": "^0.0.5",
"@notionhq/client": "^2.2.15",
"@pinecone-database/pinecone": "^3.0.2",
"@vercel/postgres": "^0.10.0",
+7
View File
@@ -1,5 +1,12 @@
# @llamaindex/workflow
## 0.0.5
### Patch Changes
- ea92b69: fix: output event check
- fadc8b8: feat: recoverable context with error handling
## 0.0.4
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/workflow",
"description": "Workflow API",
"version": "0.0.4",
"version": "0.0.5",
"type": "module",
"types": "dist/index.d.ts",
"module": "dist/index.js",
+8 -4
View File
@@ -397,10 +397,11 @@ export class WorkflowContext<Start = string, Stop = string, Data = unknown>
);
}
const outputs = outputsMap.get(step) ?? [];
const outputEvents = flattenEvents(outputs, [
nextEvent,
]);
if (outputEvents.length !== outputs.length) {
if (
!outputs.some(
(output) => nextEvent.constructor === output,
)
) {
if (this.#strict) {
const error = Error(
`Step ${step.name} returned an unexpected output event ${nextEvent}`,
@@ -452,6 +453,9 @@ export class WorkflowContext<Start = string, Stop = string, Data = unknown>
}),
)
.catch((err) => {
// when the step raise an error, should go back to the previous step
this.#sendEvent(event);
isPendingEvents.add(event);
controller.error(err);
});
}
+1 -1
View File
@@ -590,7 +590,7 @@ importers:
specifier: ^1.0.8
version: link:../packages/readers
'@llamaindex/workflow':
specifier: ^0.0.4
specifier: ^0.0.5
version: link:../packages/workflow
'@notionhq/client':
specifier: ^2.2.15
+8
View File
@@ -1,5 +1,13 @@
# @llamaindex/unit-test
## 0.0.22
### Patch Changes
- Updated dependencies [ea92b69]
- Updated dependencies [fadc8b8]
- @llamaindex/workflow@0.0.5
## 0.0.21
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/unit-test",
"private": true,
"version": "0.0.21",
"version": "0.0.22",
"type": "module",
"scripts": {
"test": "vitest run"
+95
View File
@@ -794,6 +794,21 @@ describe("workflow event loop", () => {
}
`);
});
test("workflow multiple output", async () => {
const myFlow = new Workflow<unknown, string, string>({ verbose: true });
myFlow.addStep(
{
inputs: [StartEvent<string>],
outputs: [StopEvent<string>, StopEvent<string>],
},
async (_context, ev) => {
return new StopEvent(`Hello ${ev.data}!`);
},
);
const result = await myFlow.run("world").strict();
expect(result.data).toBe("Hello world!");
});
});
describe("snapshot", async () => {
@@ -869,3 +884,83 @@ describe("snapshot", async () => {
expect(fn).toHaveBeenCalledTimes(1);
});
});
describe("error", () => {
test("error in handler", async () => {
const myFlow = new Workflow<boolean, string, string>({ verbose: true });
myFlow.addStep(
{
inputs: [StartEvent<string>],
outputs: [StopEvent<string>],
},
async ({ data }) => {
if (!data) {
throw new Error("Something went wrong");
} else {
return new StopEvent(`Hello ${data}!`);
}
},
);
await expect(myFlow.run("world")).rejects.toThrow("Something went wrong");
{
const context = myFlow.run("world");
try {
for await (const _ of context) {
// do nothing
}
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toBe("Something went wrong");
const snapshot = context.snapshot();
const newContext = myFlow.recover(snapshot).with(true);
expect((await newContext).data).toBe("Hello true!");
}
}
});
test("recover in the middle of workflow", async () => {
const myFlow = new Workflow<string | undefined, string, string>({
verbose: true,
});
class AEvent extends WorkflowEvent<string> {}
myFlow.addStep(
{
inputs: [StartEvent<string>],
outputs: [AEvent],
},
async ({ data }) => {
if (data !== undefined) {
throw new Error("Something went wrong");
}
return new AEvent("world");
},
);
myFlow.addStep(
{
inputs: [AEvent],
outputs: [StopEvent],
},
async ({ data }, ev) => {
if (data === undefined) {
throw new Error("Something went wrong");
}
return new StopEvent(`Hello, ${data}!`);
},
);
// no context, so will throw error
const context = myFlow.run("world");
try {
for await (const _ of context) {
// do nothing
}
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toBe("Something went wrong");
const snapshot = context.snapshot();
const newContext = myFlow.recover(snapshot).with("Recovered Data");
expect((await newContext).data).toBe("Hello, Recovered Data!");
}
});
});