|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from enum import Enum, auto |
| 3 | +from typing import Any, Awaitable, Callable, Sequence |
| 4 | + |
| 5 | +from parlant.core.emissions import EmittedEvent, EventEmitter |
| 6 | +from parlant.core.engines.types import Context |
| 7 | +from parlant.core.guidelines import Guideline |
| 8 | + |
| 9 | + |
| 10 | +class LifecycleHookResult(Enum): |
| 11 | + CALL_NEXT = auto() |
| 12 | + """Runs the next hook in the chain, if any""" |
| 13 | + |
| 14 | + RESOLVE = auto() |
| 15 | + """Returns without running the next hooks in the chain""" |
| 16 | + |
| 17 | + BAIL = auto() |
| 18 | + """Returns without running the next hooks in the chain, and quietly discards the current execution. |
| 19 | +
|
| 20 | + For most hooks, this completely bails out of the processing execution, dropping the response to the customer. |
| 21 | + Specifically for preparation iterations, this immediately signals that preparation is complete. |
| 22 | + """ |
| 23 | + |
| 24 | + |
| 25 | +@dataclass(frozen=False) |
| 26 | +class LifecycleHooks: |
| 27 | + on_error: list[Callable[[Context, EventEmitter, Exception], Awaitable[LifecycleHookResult]]] = ( |
| 28 | + field(default_factory=list) |
| 29 | + ) |
| 30 | + """Called when the engine has encountered a runtime error""" |
| 31 | + |
| 32 | + on_acknowledging: list[Callable[[Context, EventEmitter], Awaitable[LifecycleHookResult]]] = ( |
| 33 | + field(default_factory=list) |
| 34 | + ) |
| 35 | + """Called just before emitting an acknowledgement status event""" |
| 36 | + |
| 37 | + on_acknowledged: list[Callable[[Context, EventEmitter], Awaitable[LifecycleHookResult]]] = ( |
| 38 | + field(default_factory=list) |
| 39 | + ) |
| 40 | + """Called right after emitting an acknowledgement status event""" |
| 41 | + |
| 42 | + on_preparing: list[Callable[[Context, EventEmitter], Awaitable[LifecycleHookResult]]] = field( |
| 43 | + default_factory=list |
| 44 | + ) |
| 45 | + """Called just before beginning the preparation iterations""" |
| 46 | + |
| 47 | + on_preparation_iteration_start: list[ |
| 48 | + Callable[ |
| 49 | + [Context, EventEmitter, list[EmittedEvent]], |
| 50 | + Awaitable[LifecycleHookResult], |
| 51 | + ] |
| 52 | + ] = field(default_factory=list) |
| 53 | + """Called just before beginning a preparation iteration""" |
| 54 | + |
| 55 | + on_preparation_iteration_end: list[ |
| 56 | + Callable[ |
| 57 | + [Context, EventEmitter, list[EmittedEvent], Sequence[Guideline]], |
| 58 | + Awaitable[LifecycleHookResult], |
| 59 | + ] |
| 60 | + ] = field(default_factory=list) |
| 61 | + """Called right after finishing a preparation iteration""" |
| 62 | + |
| 63 | + on_generating_messages: list[ |
| 64 | + Callable[ |
| 65 | + [Context, EventEmitter, list[EmittedEvent], Sequence[Guideline]], |
| 66 | + Awaitable[LifecycleHookResult], |
| 67 | + ] |
| 68 | + ] = field(default_factory=list) |
| 69 | + """Called just before generating messages""" |
| 70 | + |
| 71 | + on_generated_messages: list[ |
| 72 | + Callable[ |
| 73 | + [Context, EventEmitter, Sequence[EmittedEvent], Sequence[Guideline]], |
| 74 | + Awaitable[LifecycleHookResult], |
| 75 | + ] |
| 76 | + ] = field(default_factory=list) |
| 77 | + """Called right after generating messages""" |
| 78 | + |
| 79 | + async def call_on_error( |
| 80 | + self, context: Context, emitter: EventEmitter, exception: Exception |
| 81 | + ) -> bool: |
| 82 | + return await self._call_hook(self.on_error, context, emitter, exception) |
| 83 | + |
| 84 | + async def call_on_acknowledging(self, context: Context, emitter: EventEmitter) -> bool: |
| 85 | + return await self._call_hook(self.on_acknowledging, context, emitter) |
| 86 | + |
| 87 | + async def call_on_acknowledged(self, context: Context, emitter: EventEmitter) -> bool: |
| 88 | + return await self._call_hook(self.on_acknowledged, context, emitter) |
| 89 | + |
| 90 | + async def call_on_preparing(self, context: Context, emitter: EventEmitter) -> bool: |
| 91 | + return await self._call_hook(self.on_preparing, context, emitter) |
| 92 | + |
| 93 | + async def call_on_preparation_iteration_start( |
| 94 | + self, |
| 95 | + context: Context, |
| 96 | + emitter: EventEmitter, |
| 97 | + events: list[EmittedEvent], |
| 98 | + ) -> bool: |
| 99 | + return await self._call_hook(self.on_preparation_iteration_start, context, emitter, events) |
| 100 | + |
| 101 | + async def call_on_preparation_iteration_end( |
| 102 | + self, |
| 103 | + context: Context, |
| 104 | + emitter: EventEmitter, |
| 105 | + events: list[EmittedEvent], |
| 106 | + guidelines: Sequence[Guideline], |
| 107 | + ) -> bool: |
| 108 | + return await self._call_hook( |
| 109 | + self.on_preparation_iteration_end, context, emitter, events, guidelines |
| 110 | + ) |
| 111 | + |
| 112 | + async def call_on_generating_messages( |
| 113 | + self, |
| 114 | + context: Context, |
| 115 | + emitter: EventEmitter, |
| 116 | + events: list[EmittedEvent], |
| 117 | + guidelines: Sequence[Guideline], |
| 118 | + ) -> bool: |
| 119 | + return await self._call_hook( |
| 120 | + self.on_generating_messages, context, emitter, events, guidelines |
| 121 | + ) |
| 122 | + |
| 123 | + async def call_on_generated_messages( |
| 124 | + self, |
| 125 | + context: Context, |
| 126 | + emitter: EventEmitter, |
| 127 | + events: Sequence[EmittedEvent], |
| 128 | + guidelines: Sequence[Guideline], |
| 129 | + ) -> bool: |
| 130 | + return await self._call_hook( |
| 131 | + self.on_generated_messages, context, emitter, events, guidelines |
| 132 | + ) |
| 133 | + |
| 134 | + async def _call_hook(self, hook: Any, *args: Any) -> bool: |
| 135 | + for callable in hook: |
| 136 | + match await callable(*args): |
| 137 | + case LifecycleHookResult.CALL_NEXT: |
| 138 | + continue |
| 139 | + case LifecycleHookResult.RESOLVE: |
| 140 | + return True |
| 141 | + case LifecycleHookResult.BAIL: |
| 142 | + return False |
| 143 | + return True |
| 144 | + |
| 145 | + |
| 146 | +lifecycle_hooks = LifecycleHooks() |
0 commit comments