-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathplugin.ts
More file actions
66 lines (57 loc) · 1.37 KB
/
plugin.ts
File metadata and controls
66 lines (57 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {
InferWorkflowEventData,
Workflow,
workflowEvent,
WorkflowEventData
} from '@llama-flow/core'
import { AsyncLocalStorage } from 'node:async_hooks'
export type Plugin = {
setup (): void
}
/**
* @internal
*/
export type PluginState = {
description: string
workflow: Workflow
}
/**
* @internal
*/
function usePluginState () {
const workflow = workflowAsyncContext.getStore()
if (!workflow) {
throw new Error('No workflow is available in the current context')
}
return workflow
}
/**
* @internal
*/
export const workflowAsyncContext = new AsyncLocalStorage<PluginState>()
/**
* @internal
*/
export const pluginRegisterEvent = workflowEvent()
/**
* @internal
*/
export const pluginUnregisterEvent = workflowEvent()
export function useDescription (
description: string
) {
const pluginState = usePluginState()
pluginState.description = description
}
export function onRegister (
handler: (pluginRegister: WorkflowEventData<InferWorkflowEventData<typeof pluginRegisterEvent>>) => ReturnType<Workflow['handle']>
) {
const { workflow } = usePluginState()
workflow.handle([pluginRegisterEvent], handler)
}
export function onUnregister (
handler: (pluginUnregister: WorkflowEventData<InferWorkflowEventData<typeof pluginUnregisterEvent>>) => void
) {
const { workflow } = usePluginState()
workflow.handle([pluginUnregisterEvent], handler)
}