-
Notifications
You must be signed in to change notification settings - Fork 188
feat: support human in the loop for TS #686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,653
−91
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
e4c07f9
feat: support human in the loop for TS
thucpn eba44a3
add example for custom workflow
thucpn 7875178
fix: need to request humanResponseEvent to save missing step to snapshot
thucpn ac5a1ef
refactor: human response data should be any
thucpn 832f5b6
refactor runWorkflow function to support resume stream
thucpn 23fdd51
refactor: hitl
thucpn 7b21682
fix: workflow
thucpn ddccbcf
add summary event
thucpn 45af254
send tool event
thucpn be894df
use requestId from Vercel
thucpn 99ff5b4
Merge branch 'main' into tp/hitl-for-ts
thucpn 2d31294
update chat route.ts
thucpn baf16fc
fix copy utils/*
thucpn 98913ed
refactor: workflow and stream
thucpn d93ee94
Create eight-moons-perform.md
thucpn 38cd475
update typo
thucpn 0e67d8a
make schema simple
thucpn 6851960
fix typo
thucpn 537489a
use messages in startAgentEvent
thucpn a440a34
save to snapshots folder
thucpn 0896824
fix lint
thucpn 7e4c68b
feat: workflowBaseEvent
thucpn 6a5db05
include response event in input event
thucpn 8f107f5
simplify type
thucpn 2c062c9
update readme
thucpn af47dbb
update document
thucpn c5c72f5
fix typecheck
thucpn 9b3c1ad
bump: "@llamaindex/workflow": "~1.1.8"
thucpn 66b8db6
remove any
thucpn 22cd865
use fixed tsx version to fix e2e
thucpn 7ee59c5
fix wrong copy
thucpn 5a16f10
add cli hitl examples as a use case for both Python and TS
thucpn 159d15d
update changeset to release create-llama also
thucpn 10fcf50
fix e2e
thucpn 9175ad9
fix e2e
thucpn d4c822d
hitl frontend chat
thucpn 5b06106
try disable hitl test
thucpn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
|
||
| import { | ||
| withSnapshot, | ||
| workflowEvent, | ||
| type Workflow, | ||
| type WorkflowContext, | ||
| } from "@llamaindex/workflow"; | ||
| import type { Message } from "ai"; | ||
| import { z } from "zod"; | ||
|
|
||
| // @llama-flow doesn't export snapshot types, we need to infer them from the functions | ||
| export type SnapshotWorkflow = ReturnType<typeof withSnapshot<Workflow>>; | ||
| export type SnapshotWorkflowContext = ReturnType< | ||
| SnapshotWorkflow["createContext"] | ||
| >; | ||
|
|
||
| export const serializableMemoryMap = new Map<string, any>(); | ||
|
|
||
| export const saveSnapshot = async (requestId: string, snapshot: any) => { | ||
| // TODO: save to file | ||
| serializableMemoryMap.set(requestId, snapshot); | ||
| }; | ||
|
|
||
| export const loadSnapshot = async ( | ||
| requestId: string, | ||
| ): Promise<any | undefined> => { | ||
| return serializableMemoryMap.get(requestId); | ||
| }; | ||
|
thucpn marked this conversation as resolved.
Outdated
|
||
|
|
||
| export type HumanInputEventData = { | ||
| event_type: string; // An identifier for the input component in UI | ||
| data: unknown; // The data to be sent to the input component in UI | ||
| }; | ||
|
|
||
| // humanInputEvent should be triggered when workflow need to request input from user | ||
| // when it is emitted, workflow snapshot will be saved and stream will be paused | ||
| // then send HumanInputEventData as annotation to UI to render the input form | ||
| export const humanInputEvent = workflowEvent<HumanInputEventData>(); | ||
|
|
||
| // TODO: we can consider sending JSON Schema for the requested information from user | ||
| // Then render it as a form in UI with https://github.com/rjsf-team/react-jsonschema-form | ||
| // we can call it formInputEvent (same logic as humanInputEvent but useful when requesting multiple inputs) | ||
|
|
||
| export type HumanResponseEventData = { | ||
| data: unknown; // The response data from user | ||
| }; | ||
|
|
||
| // When user make a response to the input request, workflow will be re-created from the last snapshot | ||
| // and then trigger humanResponseEvent to resume the workflow | ||
| export const humanResponseEvent = workflowEvent<HumanResponseEventData>(); | ||
|
|
||
| export const humanResponseAnnotationSchema = z.object({ | ||
| type: z.literal("human_response"), | ||
| data: z.any(), | ||
| }); | ||
|
|
||
| export const getHumanResponseFromMessage = (message: Message) => { | ||
| if (message.annotations) { | ||
| for (const annotation of message.annotations) { | ||
| if (humanResponseAnnotationSchema.safeParse(annotation).success) { | ||
| return (annotation as z.infer<typeof humanResponseAnnotationSchema>) | ||
| .data; | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| }; | ||
|
thucpn marked this conversation as resolved.
Outdated
|
||
|
|
||
| export const createWorkflowContextFromHumanResponse = async ( | ||
| workflow: Workflow, | ||
| requestId: string, | ||
| humanResponse: any, | ||
| ): Promise<SnapshotWorkflowContext> => { | ||
| // check workflow is snapshotable | ||
| if (!("resume" in workflow)) { | ||
| // TODO: ensure AgentWorkflow is snapshotable | ||
| throw new Error( | ||
| "Workflow is not a snapshot workflow. Please use withSnapshot() to make it snapshotable.", | ||
| ); | ||
| } | ||
|
|
||
| // if there is no snapshot, we can't resume the workflow | ||
| const snapshot = await loadSnapshot(requestId); | ||
| if (!snapshot) { | ||
| throw new Error("No snapshot found for request id: " + requestId); | ||
| } | ||
|
|
||
| // resume the workflow from the snapshot with human response | ||
| const context = (workflow as SnapshotWorkflow).resume( | ||
| humanResponse, | ||
| snapshot, | ||
| ); | ||
|
|
||
| return context; | ||
| }; | ||
|
thucpn marked this conversation as resolved.
Outdated
|
||
|
|
||
| export const pauseForHumanInput = async ( | ||
| context: WorkflowContext, | ||
| requestId: string, | ||
| ) => { | ||
| if (!("snapshot" in context)) { | ||
| // check workflow is snapshotable | ||
| throw new Error( | ||
| "Cannot get snapshot of the workflow. Please use withSnapshot() to make it snapshotable.", | ||
| ); | ||
| } | ||
|
|
||
| // save snapshot with the key is requestId | ||
| const snapshotContext = context as SnapshotWorkflowContext; | ||
| const snapshot = await snapshotContext.snapshot(); | ||
| await saveSnapshot(requestId, snapshot); | ||
| }; | ||
|
thucpn marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.