Skip to content

Commit 823f3a9

Browse files
authored
Merge pull request #154 from run-llama/terry/handler_refactor
refactor workflow/handler store
2 parents e9a512e + 5ef7965 commit 823f3a9

33 files changed

+962
-1194
lines changed

.changeset/cold-buttons-lie.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@llamaindex/workflow-debugger": patch
3+
"@llamaindex/ui": patch
4+
---
5+
6+
refactor workflow/handler store

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@llamaindex/ui-monorepo",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"private": true,
55
"type": "module",
66
"scripts": {
@@ -42,4 +42,4 @@
4242
"@vitejs/plugin-react-swc": "^4.1.0"
4343
},
4444
"packageManager": "pnpm@10.11.1+sha512.e519b9f7639869dc8d5c3c5dfef73b3f091094b0a006d7317353c72b124e80e1afd429732e28705ad6bfa1ee879c1fce46c128ccebd3192101f43dd67c667912"
45-
}
45+
}

packages/server-py/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio
22
import logging
33
from workflows.server import WorkflowServer
4-
from test_workflows.basic_worklfow import CalculatorWorkflow
4+
from test_workflows.sum import SumWorkflow
5+
from test_workflows.subtract import SubtractWorkflow
56

67
# Configure logging to output to stdout
78
logging.basicConfig(
@@ -15,7 +16,8 @@ async def main() -> None:
1516
server = WorkflowServer()
1617

1718
# Register workflows
18-
server.add_workflow("calculator", CalculatorWorkflow())
19+
server.add_workflow("sum", SumWorkflow())
20+
server.add_workflow("subtract", SubtractWorkflow())
1921

2022
# Enable auto-reload for development
2123
await server.serve(

packages/server-py/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ readme = "README.md"
66
requires-python = ">=3.11"
77
dependencies = [
88
"llama-index>=0.14.5",
9-
"llama-index-workflows[server]>=2.8.0",
9+
"llama-index-workflows[server]>=2.10.0",
1010
]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import asyncio
2+
from workflows.context import Context
3+
from workflows import Workflow, step
4+
from workflows.events import Event, StartEvent, StopEvent
5+
from logging import getLogger
6+
7+
logger = getLogger(__name__)
8+
9+
class SubtractInput(StartEvent):
10+
a: int
11+
b: int
12+
13+
class ProgressEvent(Event):
14+
step: str
15+
progress: int
16+
message: str
17+
18+
class CalculateRequestEvent(Event):
19+
a: int
20+
b: int
21+
22+
class CalculateResponseEvent(Event):
23+
result: int
24+
25+
class SubtractOutput(StopEvent):
26+
results: int
27+
28+
class SubtractWorkflow(Workflow):
29+
@step
30+
async def initialize(self, ctx: Context, ev: SubtractInput) -> CalculateRequestEvent:
31+
logger.info("Starting Subtract workflow")
32+
ctx.write_event_to_stream(
33+
ProgressEvent(
34+
step="start",
35+
progress=10,
36+
message="Starting Subtract workflow"
37+
)
38+
)
39+
await asyncio.sleep(1.0)
40+
return CalculateRequestEvent(a=ev.a, b=ev.b)
41+
42+
@step
43+
async def Subtract(self, ctx: Context, ev: CalculateRequestEvent) -> CalculateResponseEvent:
44+
logger.info("Calculating result")
45+
await asyncio.sleep(1.0)
46+
ctx.write_event_to_stream(
47+
ProgressEvent(
48+
step="calculate",
49+
progress=50,
50+
message="Calculating result"
51+
)
52+
)
53+
result = ev.a - ev.b
54+
return CalculateResponseEvent(result=result)
55+
56+
@step
57+
async def finalize(self, ctx: Context, ev: CalculateResponseEvent) -> SubtractOutput:
58+
logger.info("Finalizing result")
59+
await asyncio.sleep(1.0)
60+
return SubtractOutput(results=ev.result)

packages/server-py/test_workflows/basic_worklfow.py renamed to packages/server-py/test_workflows/sum.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22
from workflows.context import Context
33
from workflows import Workflow, step
44
from workflows.events import Event, StartEvent, StopEvent
5-
from enum import Enum
65
from logging import getLogger
76

87
logger = getLogger(__name__)
98

10-
class Operation(Enum):
11-
SUM = "sum"
12-
SUBTRACT = "subtract"
13-
MULTIPLY = "multiply"
14-
DIVIDE = "divide"
15-
16-
class CalculatorInput(StartEvent):
9+
class SumInput(StartEvent):
1710
a: int
1811
b: int
19-
operation: Operation
2012

2113
class ProgressEvent(Event):
2214
step: str
@@ -26,17 +18,16 @@ class ProgressEvent(Event):
2618
class CalculateRequestEvent(Event):
2719
a: int
2820
b: int
29-
operation: Operation
3021

3122
class CalculateResponseEvent(Event):
3223
result: int
3324

34-
class CalculatorOutput(StopEvent):
25+
class SumOutput(StopEvent):
3526
results: int
3627

37-
class CalculatorWorkflow(Workflow):
28+
class SumWorkflow(Workflow):
3829
@step
39-
async def initialize(self, ctx: Context, ev: CalculatorInput) -> CalculateRequestEvent:
30+
async def initialize(self, ctx: Context, ev: SumInput) -> CalculateRequestEvent:
4031
logger.info("Starting sum workflow")
4132
ctx.write_event_to_stream(
4233
ProgressEvent(
@@ -46,7 +37,7 @@ async def initialize(self, ctx: Context, ev: CalculatorInput) -> CalculateReques
4637
)
4738
)
4839
await asyncio.sleep(1.0)
49-
return CalculateRequestEvent(a=ev.a, b=ev.b, operation=ev.operation)
40+
return CalculateRequestEvent(a=ev.a, b=ev.b)
5041

5142
@step
5243
async def sum(self, ctx: Context, ev: CalculateRequestEvent) -> CalculateResponseEvent:
@@ -59,18 +50,11 @@ async def sum(self, ctx: Context, ev: CalculateRequestEvent) -> CalculateRespons
5950
message="Calculating result"
6051
)
6152
)
62-
if ev.operation == Operation.SUM:
63-
result = ev.a + ev.b
64-
elif ev.operation == Operation.SUBTRACT:
65-
result = ev.a - ev.b
66-
elif ev.operation == Operation.MULTIPLY:
67-
result = ev.a * ev.b
68-
elif ev.operation == Operation.DIVIDE:
69-
result = ev.a / ev.b
53+
result = ev.a + ev.b
7054
return CalculateResponseEvent(result=result)
7155

7256
@step
73-
async def finalize(self, ctx: Context, ev: CalculateResponseEvent) -> CalculatorOutput:
57+
async def finalize(self, ctx: Context, ev: CalculateResponseEvent) -> SumOutput:
7458
logger.info("Finalizing result")
7559
await asyncio.sleep(1.0)
76-
return CalculatorOutput(results=ev.result)
60+
return SumOutput(results=ev.result)

packages/server-py/uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ui/eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const languageOptions = {
3232
ecmaVersion: 2017,
3333
sourceType: "module",
3434
projectService: true,
35+
tsconfigRootDir: import.meta.dirname,
3536
},
3637
};
3738

packages/ui/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,16 @@
338338
"sonner": "^2.0.5",
339339
"tailwind-merge": "^3.3.1",
340340
"uuid": "^13.0.0",
341+
"valtio": "^2.1.8",
341342
"vaul": "^1.1.2",
342343
"zod": "^3.25.76",
343344
"zustand": "^5.0.6"
344345
},
345346
"peerDependencies": {
346347
"@llamaindex/workflows-client": "^1.2.0",
347348
"llama-cloud-services": "^0.3.6",
348-
"react": ">=18 <20",
349-
"react-dom": ">=18 <20"
349+
"react": "catalog:",
350+
"react-dom": "catalog:"
350351
},
351352
"devDependencies": {
352353
"@chromatic-com/storybook": "^4.0.1",
@@ -364,8 +365,8 @@
364365
"@testing-library/user-event": "^14.5.2",
365366
"@types/glob": "^9.0.0",
366367
"@types/node": "^20.0.0",
367-
"@types/react": "19.2.2",
368-
"@types/react-dom": "18.3.7",
368+
"@types/react": "catalog:",
369+
"@types/react-dom": "catalog:",
369370
"@vitejs/plugin-react-swc": "^4.1.0",
370371
"@vitest/browser": "^3.2.4",
371372
"@vitest/coverage-v8": "^3.2.3",
@@ -379,8 +380,8 @@
379380
"msw-storybook-addon": "^2.0.5",
380381
"playwright": "^1.56.1",
381382
"postcss-cli": "^11.0.0",
382-
"react": "^19.0.0",
383-
"react-dom": "^19.0.0",
383+
"react": "catalog:",
384+
"react-dom": "catalog:",
384385
"storybook": "^9.1.3",
385386
"tailwind-classname-prefix-loader": "^1.0.6",
386387
"tailwindcss": "^4",

packages/ui/src/lib/shared-streaming.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface StreamSubscriber<TEvent> {
1212
onStart?: () => void;
1313
onData?: (event: TEvent) => void;
1414
onError?: (error: Error) => void;
15+
onCancel?: () => void;
1516
onSuccess?: (allEvents: TEvent[]) => void;
1617
onComplete?: () => void; // Called when stream ends (success or error)
1718
}

0 commit comments

Comments
 (0)