@@ -3822,30 +3822,25 @@ async def agent_openai(
3822
3822
"running" ,
3823
3823
None
3824
3824
) is None :
3825
- snoop .pp (threads [result .action_data .thread_id ]["messages" ])
3825
+ action_data_run_thread = result .action_data
3826
+ # snoop.pp(threads[action_data_run_thread.thread_id]["messages"])
3826
3827
def make_run_it (result , user_context ):
3827
3828
async def run_it ():
3828
3829
nonlocal result
3829
3830
nonlocal user_context
3830
3831
result = Runner .run_streamed (
3831
- starting_agent = agents [result . action_data .agent_id ],
3832
- input = threads [result . action_data .thread_id ]["messages" ],
3832
+ starting_agent = agents [action_data_run_thread .agent_id ],
3833
+ input = threads [action_data_run_thread .thread_id ]["messages" ],
3833
3834
context = user_context ,
3834
3835
)
3835
3836
more_events = True
3836
- events = threads [result .action_data .thread_id ]["events" ]
3837
- try :
3838
- async for event in result .stream_events ():
3839
- events .append (event )
3840
- events .append (result )
3841
- finally :
3842
- more_events = False
3843
-
3837
+ event_queue = asyncio .Queue ()
3838
+ events = threads [action_data_run_thread .thread_id ]["events" ]
3844
3839
async def stream_get_thread_messages ():
3845
3840
nonlocal more_events
3846
- nonlocal events
3841
+ nonlocal event_queue
3847
3842
while more_events :
3848
- yield events . pop ()
3843
+ yield await event_queue . get ()
3849
3844
3850
3845
thread_messages = stream_get_thread_messages ()
3851
3846
thread_messages_iter = thread_messages .__aiter__ ()
@@ -3856,10 +3851,20 @@ async def stream_get_thread_messages():
3856
3851
)
3857
3852
)
3858
3853
] = (
3859
- f"thread.messages.{ result . action_data .thread_id } " ,
3860
- (action_new_thread_run , thread_messages_iter ),
3854
+ f"thread.messages.{ action_data_run_thread .thread_id } " ,
3855
+ (action_data_run_thread , thread_messages_iter ),
3861
3856
)
3862
3857
3858
+ try :
3859
+ async for event in result .stream_events ():
3860
+ events .append (event )
3861
+ await event_queue .put (event )
3862
+ events .append (result )
3863
+ await event_queue .put (result )
3864
+ finally :
3865
+ await event_queue .join ()
3866
+ more_events = False
3867
+
3863
3868
return result
3864
3869
return run_it
3865
3870
run_it = make_run_it (result , user_context )
@@ -3909,7 +3914,6 @@ async def stream_get_thread_messages():
3909
3914
)
3910
3915
if thread is None :
3911
3916
raise AGIThreadNotFoundError (result .action_data .thread_id )
3912
- # result.to_input_list() + [{"role": "user", "content": }]
3913
3917
thread ["messages" ].append (
3914
3918
{
3915
3919
"role" : result .action_data .message_role ,
@@ -3928,7 +3932,7 @@ async def stream_get_thread_messages():
3928
3932
),
3929
3933
)
3930
3934
elif work_name .startswith ("thread.runs." ):
3931
- snoop .pp ("Run completed" , result , work_ctx )
3935
+ snoop .pp ("Run completed" ) # , result, work_ctx)
3932
3936
# NOTE XXX WARNING _old_run is inconsistent right now
3933
3937
action_new_thread_run , _old_run = work_ctx
3934
3938
# TODO Support streaming of results
@@ -3944,38 +3948,6 @@ async def stream_get_thread_messages():
3944
3948
run_status = result .status ,
3945
3949
),
3946
3950
)
3947
- # TODO Send this similar to seed back to a feedback queue to
3948
- # process as an action for get thread messages
3949
- async def get_thread_messages (action_new_thread_run ):
3950
- with snoop ():
3951
- thread = threads [
3952
- action_new_thread_run .action_data .thread_id
3953
- ]
3954
- thread_task = thread ["running" ]["task" ]
3955
- if not thread_task .done ():
3956
- await thread ["running" ]["event" ].wait ()
3957
- thread_result = thread_task .result ()
3958
- thread ["messages" ].extend (
3959
- thread_result .to_input_list ()
3960
- )
3961
- # TODO XXX DEBUG XXX TODO REMOVE
3962
- snoop .pp (thread_result .final_output )
3963
- for event in threads [
3964
- action_new_thread_run .action_data .thread_id
3965
- ]["events" ]:
3966
- yield event
3967
- thread_messages = get_thread_messages (action_new_thread_run )
3968
- thread_messages_iter = thread_messages .__aiter__ ()
3969
- work [
3970
- tg .create_task (
3971
- ignore_stopasynciteration (
3972
- thread_messages_iter .__anext__ ()
3973
- )
3974
- )
3975
- ] = (
3976
- f"thread.messages.{ action_new_thread_run .action_data .thread_id } " ,
3977
- (action_new_thread_run , thread_messages_iter ),
3978
- )
3979
3951
elif result .status in ("queued" , "in_progress" ):
3980
3952
yield AGIEvent (
3981
3953
event_type = AGIEventType .THREAD_RUN_IN_PROGRESS ,
@@ -4028,25 +4000,39 @@ async def get_thread_messages(action_new_thread_run):
4028
4000
),
4029
4001
)
4030
4002
elif work_name .startswith ("thread.messages." ):
4031
- action_new_thread_run , thread_messages_iter = work_ctx
4003
+ action_data_run_thread , thread_messages_iter = work_ctx
4032
4004
_ , _ , thread_id = work_name .split ("." , maxsplit = 3 )
4033
4005
# The first time we iterate is the most recent response
4034
4006
# TODO Keep track of what the last response received was so that
4035
4007
# we can create_task as many times as there might be responses
4036
4008
# in case there are multiple within one run.
4037
4009
thread = threads .get (
4038
- result . action_data .thread_id ,
4010
+ action_data_run_thread .thread_id ,
4039
4011
None ,
4040
4012
)
4041
4013
if thread is None :
4042
- raise AGIThreadNotFoundError (result . action_data .thread_id )
4014
+ raise AGIThreadNotFoundError (action_data_run_thread .thread_id )
4043
4015
work [
4044
4016
tg .create_task (
4045
4017
ignore_stopasynciteration (
4046
4018
thread_messages_iter .__anext__ ()
4047
4019
)
4048
4020
)
4049
4021
] = (work_name , work_ctx )
4022
+ if getattr (result , "is_complete" , False ):
4023
+ snoop .pp ("complete" , result .final_output )
4024
+ yield AGIEvent (
4025
+ event_type = AGIEventType .NEW_THREAD_MESSAGE ,
4026
+ event_data = AGIEventNewThreadMessage (
4027
+ agent_id = action_data_run_thread .agent_id ,
4028
+ thread_id = action_data_run_thread .thread_id ,
4029
+ message_role = "agent" ,
4030
+ message_content_type = f"agi.class/{ result .final_output .__class__ .__qualname__ } " ,
4031
+ message_content = result .final_output ,
4032
+ ),
4033
+ )
4034
+ """
4035
+ snoop.pp("message iter", result)
4050
4036
if not result["id"]:
4051
4037
result["id"] = str(uuid.uuid4())
4052
4038
if result["id"] not in thread["messages_received"]:
@@ -4055,15 +4041,16 @@ async def get_thread_messages(action_new_thread_run):
4055
4041
yield AGIEvent(
4056
4042
event_type=AGIEventType.NEW_THREAD_MESSAGE,
4057
4043
event_data=AGIEventNewThreadMessage(
4058
- agent_id = action_new_thread_run . action_data .agent_id ,
4059
- thread_id = result .thread_id ,
4044
+ agent_id=action_data_run_thread .agent_id,
4045
+ thread_id=action_data_run_thread .thread_id,
4060
4046
message_role="agent"
4061
4047
if result.role == "assistant"
4062
4048
else "user",
4063
4049
message_content_type=content.type,
4064
4050
message_content=content.text.value,
4065
4051
),
4066
4052
)
4053
+ """
4067
4054
except Exception as error :
4068
4055
traceback .print_exc ()
4069
4056
yield AGIEvent (
@@ -4192,8 +4179,9 @@ async def DEBUG_TEMP_message_handler(user_name,
4192
4179
agent_event ,
4193
4180
pane = None ):
4194
4181
# TODO https://rich.readthedocs.io/en/stable/markdown.html
4182
+ # TODO Output non-workflow responses
4195
4183
if (
4196
- agent_event .event_data .message_content_type == "text "
4184
+ agent_event .event_data .message_content_type == f"agi.class/ { PolicyEngineWorkflow . __qualname__ } "
4197
4185
and agent_event .event_data .message_role == "agent"
4198
4186
):
4199
4187
# TODOTODOTODO
@@ -4202,7 +4190,7 @@ async def DEBUG_TEMP_message_handler(user_name,
4202
4190
# pane.send_keys(f"{agent_event.event_data.message_content}")
4203
4191
# pane.send_keys(f"{agent_event.event_data.message_content}")
4204
4192
# print()
4205
- snoop .pp (json .loads (agent_event .event_data .message_content ))
4193
+ # snoop.pp(json.loads(agent_event.event_data.message_content))
4206
4194
session = pane .window .session
4207
4195
tempdir_lookup_env_var = f'TEMPDIR_ENV_VAR_TMUX_WINDOW_{ session .active_window .id .replace ("@" , "" )} '
4208
4196
tempdir_env_var = pane .window .session .show_environment ()[tempdir_lookup_env_var ]
@@ -4213,11 +4201,8 @@ async def DEBUG_TEMP_message_handler(user_name,
4213
4201
# for take off (aka workload id and exec in phase 0). Executing the
4214
4202
# policy aka the workflow (would be the one we insert to once
4215
4203
# paths can be mapped to poliy engine workflows easily
4216
- response = AGIOpenAIAssistantResponse .model_validate_json (
4217
- agent_event .event_data .message_content
4218
- )
4219
4204
proposed_workflow_contents = yaml .dump (
4220
- json .loads (response . workflow .model_dump_json ()),
4205
+ json .loads (agent_event . event_data . message_content .model_dump_json ()),
4221
4206
default_flow_style = False ,
4222
4207
sort_keys = True ,
4223
4208
)
@@ -4227,7 +4212,7 @@ async def DEBUG_TEMP_message_handler(user_name,
4227
4212
inputs = {},
4228
4213
context = {},
4229
4214
stack = {},
4230
- workflow = response . workflow ,
4215
+ workflow = agent_event . event_data . message_content ,
4231
4216
).model_dump_json (),
4232
4217
)
4233
4218
)
@@ -4511,7 +4496,7 @@ async def user_input_action_stream_queue_iterator(queue):
4511
4496
pane .send_keys (
4512
4497
textwrap .dedent (
4513
4498
f"""
4514
- echo "Hello Alice. Shall we play a game? My name is $USER. Please run nmap against all machines on all conntected networks . Here are some details about the system we are on: $(echo $(echo $(cat /usr/lib/os-release || cat /etc/os-release)))" | tee -a ${ agi_name .upper ()} _INPUT
4499
+ echo "Hello Alice. Shall we play a game? My name is $USER. Please run nmap against localhost . Here are some details about the system we are on: $(echo $(echo $(cat /usr/lib/os-release || cat /etc/os-release)))" | tee -a ${ agi_name .upper ()} _INPUT
4515
4500
""" .strip (),
4516
4501
),
4517
4502
enter = False ,
@@ -4635,57 +4620,36 @@ async def user_input_action_stream_queue_iterator(queue):
4635
4620
)
4636
4621
)
4637
4622
)
4638
- if (
4639
- isinstance (user_input , str )
4640
- and user_input .startswith ("AGI_ACTION_TYPE.INGEST_FILE:" )
4641
- ):
4642
- file_path = user_input .split ("AGI_ACTION_TYPE.INGEST_FILE:" , maxsplit = 1 )[1 ]
4643
- if pathlib .Path (file_path ).is_file ():
4644
- waiting .append (
4645
- (
4646
- AGIEventType .NEW_THREAD_CREATED ,
4647
- async_lambda (
4648
- lambda : AGIAction (
4649
- action_type = AGIActionType .INGEST_FILE ,
4650
- action_data = AGIActionIngestFile (
4651
- agent_id = agents .currently .state_data .agent_id ,
4652
- file_path = file_path ,
4653
- ),
4654
- )
4655
- )
4656
- )
4657
- )
4658
- else :
4659
- waiting .append (
4660
- (
4661
- AGIEventType .NEW_THREAD_CREATED ,
4662
- async_lambda (
4663
- lambda : AGIAction (
4664
- action_type = AGIActionType .ADD_MESSAGE ,
4665
- action_data = AGIActionAddMessage (
4666
- agent_id = agents .currently .state_data .agent_id ,
4667
- thread_id = threads .currently .state_data .thread_id ,
4668
- message_role = "user" ,
4669
- message_content = user_input ,
4670
- ),
4671
- )
4623
+ waiting .append (
4624
+ (
4625
+ AGIEventType .NEW_THREAD_CREATED ,
4626
+ async_lambda (
4627
+ lambda : AGIAction (
4628
+ action_type = AGIActionType .ADD_MESSAGE ,
4629
+ action_data = AGIActionAddMessage (
4630
+ agent_id = agents .currently .state_data .agent_id ,
4631
+ thread_id = threads .currently .state_data .thread_id ,
4632
+ message_role = "user" ,
4633
+ message_content = user_input ,
4634
+ ),
4672
4635
)
4673
4636
)
4674
4637
)
4675
- waiting . append (
4676
- (
4677
- AGIEventType . THREAD_MESSAGE_ADDED ,
4678
- async_lambda (
4679
- lambda : AGIAction (
4680
- action_type = AGIActionType . RUN_THREAD ,
4681
- action_data = AGIActionRunThread (
4682
- agent_id = threads . currently . state_data . agent_id ,
4683
- thread_id = threads .currently .state_data .thread_id ,
4684
- ) ,
4685
- )
4686
- ),
4638
+ )
4639
+ waiting . append (
4640
+ (
4641
+ AGIEventType . THREAD_MESSAGE_ADDED ,
4642
+ async_lambda (
4643
+ lambda : AGIAction (
4644
+ action_type = AGIActionType . RUN_THREAD ,
4645
+ action_data = AGIActionRunThread (
4646
+ agent_id = threads .currently .state_data .agent_id ,
4647
+ thread_id = threads . currently . state_data . thread_id ,
4648
+ ),
4649
+ )
4687
4650
),
4688
- )
4651
+ ),
4652
+ )
4689
4653
# Run actions which have are waiting for an event which was seen
4690
4654
still_waiting = []
4691
4655
while waiting :
0 commit comments