forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
305 lines (225 loc) · 7.39 KB
/
Copy pathprotocol.py
File metadata and controls
305 lines (225 loc) · 7.39 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
CopilotKit Protocol
"""
import json
from enum import Enum
from typing import Union, Optional
from typing_extensions import TypedDict, Literal, Any, Dict
class RuntimeEventTypes(Enum):
"""CopilotKit Runtime Event Types"""
TEXT_MESSAGE_START = "TextMessageStart"
TEXT_MESSAGE_CONTENT = "TextMessageContent"
TEXT_MESSAGE_END = "TextMessageEnd"
ACTION_EXECUTION_START = "ActionExecutionStart"
ACTION_EXECUTION_ARGS = "ActionExecutionArgs"
ACTION_EXECUTION_END = "ActionExecutionEnd"
ACTION_EXECUTION_RESULT = "ActionExecutionResult"
AGENT_STATE_MESSAGE = "AgentStateMessage"
META_EVENT = "MetaEvent"
RUN_STARTED = "RunStarted"
RUN_FINISHED = "RunFinished"
RUN_ERROR = "RunError"
NODE_STARTED = "NodeStarted"
NODE_FINISHED = "NodeFinished"
class RuntimeMetaEventName(Enum):
"""Runtime Meta Event Name"""
LANG_GRAPH_INTERRUPT_EVENT = "LangGraphInterruptEvent"
PREDICT_STATE = "PredictState"
EXIT = "Exit"
class TextMessageStart(TypedDict):
"""Text Message Start Event"""
type: Literal[RuntimeEventTypes.TEXT_MESSAGE_START]
messageId: str
parentMessageId: Optional[str]
class TextMessageContent(TypedDict):
"""Text Message Content Event"""
type: Literal[RuntimeEventTypes.TEXT_MESSAGE_CONTENT]
messageId: str
content: str
class TextMessageEnd(TypedDict):
"""Text Message End Event"""
type: Literal[RuntimeEventTypes.TEXT_MESSAGE_END]
messageId: str
class ActionExecutionStart(TypedDict):
"""Action Execution Start Event"""
type: Literal[RuntimeEventTypes.ACTION_EXECUTION_START]
actionExecutionId: str
actionName: str
parentMessageId: Optional[str]
class ActionExecutionArgs(TypedDict):
"""Action Execution Args Event"""
type: Literal[RuntimeEventTypes.ACTION_EXECUTION_ARGS]
actionExecutionId: str
args: str
class ActionExecutionEnd(TypedDict):
"""Action Execution End Event"""
type: Literal[RuntimeEventTypes.ACTION_EXECUTION_END]
actionExecutionId: str
class ActionExecutionResult(TypedDict):
"""Action Execution Result Event"""
type: Literal[RuntimeEventTypes.ACTION_EXECUTION_RESULT]
actionName: str
actionExecutionId: str
result: str
class AgentStateMessage(TypedDict):
"""Agent State Message Event"""
type: Literal[RuntimeEventTypes.AGENT_STATE_MESSAGE]
threadId: str
agentName: str
nodeName: str
runId: str
active: bool
role: str
state: str
running: bool
class MetaEvent(TypedDict):
"""Meta Event"""
type: Literal[RuntimeEventTypes.META_EVENT]
name: RuntimeMetaEventName
value: Any
class RunStarted(TypedDict):
"""Run Started Event"""
type: Literal[RuntimeEventTypes.RUN_STARTED]
state: Dict[str, Any]
class RunFinished(TypedDict):
"""Run Finished Event"""
type: Literal[RuntimeEventTypes.RUN_FINISHED]
state: Dict[str, Any]
class RunError(TypedDict):
"""Run Error Event"""
type: Literal[RuntimeEventTypes.RUN_ERROR]
error: Any
class NodeStarted(TypedDict):
"""Node Started Event"""
type: Literal[RuntimeEventTypes.NODE_STARTED]
node_name: str
state: Dict[str, Any]
class NodeFinished(TypedDict):
"""Node Finished Event"""
type: Literal[RuntimeEventTypes.NODE_FINISHED]
node_name: str
state: Dict[str, Any]
RuntimeProtocolEvent = Union[
TextMessageStart,
TextMessageContent,
TextMessageEnd,
ActionExecutionStart,
ActionExecutionArgs,
ActionExecutionEnd,
ActionExecutionResult,
AgentStateMessage,
MetaEvent,
]
RuntimeLifecycleEvent = Union[
RunStarted,
RunFinished,
RunError,
NodeStarted,
NodeFinished,
]
RuntimeEvent = Union[
RuntimeProtocolEvent,
RuntimeLifecycleEvent,
]
class PredictStateConfig(TypedDict):
"""
Predict State Config
"""
tool_name: str
tool_argument: Optional[str]
def text_message_start(
*, message_id: str, parent_message_id: Optional[str] = None
) -> TextMessageStart:
"""Utility function to create a text message start event"""
return {
"type": RuntimeEventTypes.TEXT_MESSAGE_START,
"messageId": message_id,
"parentMessageId": parent_message_id,
}
def text_message_content(*, message_id: str, content: str) -> TextMessageContent:
"""Utility function to create a text message content event"""
return {
"type": RuntimeEventTypes.TEXT_MESSAGE_CONTENT,
"messageId": message_id,
"content": content,
}
def text_message_end(*, message_id: str) -> TextMessageEnd:
"""Utility function to create a text message end event"""
return {"type": RuntimeEventTypes.TEXT_MESSAGE_END, "messageId": message_id}
def action_execution_start(
*,
action_execution_id: str,
action_name: str,
parent_message_id: Optional[str] = None,
) -> ActionExecutionStart:
"""Utility function to create an action execution start event"""
return {
"type": RuntimeEventTypes.ACTION_EXECUTION_START,
"actionExecutionId": action_execution_id,
"actionName": action_name,
"parentMessageId": parent_message_id,
}
def action_execution_args(
*, action_execution_id: str, args: str
) -> ActionExecutionArgs:
"""Utility function to create an action execution args event"""
return {
"type": RuntimeEventTypes.ACTION_EXECUTION_ARGS,
"actionExecutionId": action_execution_id,
"args": args,
}
def action_execution_end(*, action_execution_id: str) -> ActionExecutionEnd:
"""Utility function to create an action execution end event"""
return {
"type": RuntimeEventTypes.ACTION_EXECUTION_END,
"actionExecutionId": action_execution_id,
}
def action_execution_result(
*, action_name: str, action_execution_id: str, result: str
) -> ActionExecutionResult:
"""Utility function to create an action execution result event"""
return {
"type": RuntimeEventTypes.ACTION_EXECUTION_RESULT,
"actionName": action_name,
"actionExecutionId": action_execution_id,
"result": result,
}
def agent_state_message( # pylint: disable=too-many-arguments
*,
thread_id: str,
agent_name: str,
node_name: str,
run_id: str,
active: bool,
role: str,
state: str,
running: bool,
) -> AgentStateMessage:
"""Utility function to create an agent state message event"""
return {
"type": RuntimeEventTypes.AGENT_STATE_MESSAGE,
"threadId": thread_id,
"agentName": agent_name,
"nodeName": node_name,
"runId": run_id,
"active": active,
"role": role,
"state": state,
"running": running,
}
def meta_event(*, name: RuntimeMetaEventName, value: Any) -> MetaEvent:
"""Utility function to create a meta event"""
return {"type": RuntimeEventTypes.META_EVENT, "name": name, "value": value}
def emit_runtime_events(*events: RuntimeProtocolEvent) -> str:
"""Emit a list of runtime events"""
def serialize_event(event):
# Convert enum values to their string representation
if isinstance(event, dict):
return {
k: (v.value if isinstance(v, Enum) else v) for k, v in event.items()
}
return event
return "\n".join(json.dumps(serialize_event(event)) for event in events) + "\n"
def emit_runtime_event(event: RuntimeProtocolEvent) -> str:
"""Emit a single runtime event"""
return emit_runtime_events(event)