@@ -37,116 +37,124 @@ export interface RunAgentSubscriberParams {
37
37
input : RunAgentInput ;
38
38
}
39
39
40
- // TODO make shit async
40
+ // Utility type to allow callbacks to be implemented either synchronously or asynchronously.
41
+ export type MaybePromise < T > = T | Promise < T > ;
42
+
41
43
export interface RunAgentSubscriber {
42
44
// Request lifecycle
43
45
onRunInitialized ?(
44
46
params : RunAgentSubscriberParams ,
45
- ) : Omit < AgentStateMutation , "stopPropagation" > | undefined ;
47
+ ) : MaybePromise < Omit < AgentStateMutation , "stopPropagation" > | undefined > ;
46
48
onRunFailed ?(
47
49
params : { error : Error } & RunAgentSubscriberParams ,
48
- ) : Omit < AgentStateMutation , "stopPropagation" > | undefined ;
50
+ ) : MaybePromise < Omit < AgentStateMutation , "stopPropagation" > | undefined > ;
49
51
onRunFinalized ?(
50
52
params : RunAgentSubscriberParams ,
51
- ) : Omit < AgentStateMutation , "stopPropagation" > | undefined ;
53
+ ) : MaybePromise < Omit < AgentStateMutation , "stopPropagation" > | undefined > ;
52
54
53
55
// Events
54
- onEvent ?( params : { event : BaseEvent } & RunAgentSubscriberParams ) : AgentStateMutation | undefined ;
56
+ onEvent ?(
57
+ params : { event : BaseEvent } & RunAgentSubscriberParams ,
58
+ ) : MaybePromise < AgentStateMutation | undefined > ;
55
59
56
60
onRunStartedEvent ?(
57
61
params : { event : RunStartedEvent } & RunAgentSubscriberParams ,
58
- ) : AgentStateMutation | undefined ;
62
+ ) : MaybePromise < AgentStateMutation | undefined > ;
59
63
onRunFinishedEvent ?(
60
64
params : { event : RunFinishedEvent } & RunAgentSubscriberParams ,
61
- ) : AgentStateMutation | undefined ;
65
+ ) : MaybePromise < AgentStateMutation | undefined > ;
62
66
onRunErrorEvent ?(
63
67
params : { event : RunErrorEvent } & RunAgentSubscriberParams ,
64
- ) : AgentStateMutation | undefined ;
68
+ ) : MaybePromise < AgentStateMutation | undefined > ;
65
69
66
70
onStepStartedEvent ?(
67
71
params : { event : StepStartedEvent } & RunAgentSubscriberParams ,
68
- ) : AgentStateMutation | undefined ;
72
+ ) : MaybePromise < AgentStateMutation | undefined > ;
69
73
onStepFinishedEvent ?(
70
74
params : { event : StepFinishedEvent } & RunAgentSubscriberParams ,
71
- ) : AgentStateMutation | undefined ;
75
+ ) : MaybePromise < AgentStateMutation | undefined > ;
72
76
73
77
onTextMessageStartEvent ?(
74
78
params : { event : TextMessageStartEvent } & RunAgentSubscriberParams ,
75
- ) : AgentStateMutation | undefined ;
79
+ ) : MaybePromise < AgentStateMutation | undefined > ;
76
80
onTextMessageContentEvent ?(
77
81
params : {
78
82
event : TextMessageContentEvent ;
79
83
textMessageBuffer : string ;
80
84
} & RunAgentSubscriberParams ,
81
- ) : AgentStateMutation | undefined ;
85
+ ) : MaybePromise < AgentStateMutation | undefined > ;
82
86
onTextMessageEndEvent ?(
83
87
params : { event : TextMessageEndEvent ; textMessageBuffer : string } & RunAgentSubscriberParams ,
84
- ) : AgentStateMutation | undefined ;
88
+ ) : MaybePromise < AgentStateMutation | undefined > ;
85
89
86
90
onToolCallStartEvent ?(
87
91
params : { event : ToolCallStartEvent } & RunAgentSubscriberParams ,
88
- ) : AgentStateMutation | undefined ;
92
+ ) : MaybePromise < AgentStateMutation | undefined > ;
89
93
onToolCallArgsEvent ?(
90
94
params : {
91
95
event : ToolCallArgsEvent ;
92
96
toolCallBuffer : string ;
93
97
toolCallName : string ;
94
98
} & RunAgentSubscriberParams ,
95
- ) : AgentStateMutation | undefined ;
99
+ ) : MaybePromise < AgentStateMutation | undefined > ;
96
100
onToolCallEndEvent ?(
97
101
params : {
98
102
event : ToolCallEndEvent ;
99
103
toolCallBuffer : string ;
100
104
toolCallName : string ;
101
105
} & RunAgentSubscriberParams ,
102
- ) : AgentStateMutation | undefined ;
106
+ ) : MaybePromise < AgentStateMutation | undefined > ;
103
107
104
108
onToolCallResultEvent ?(
105
109
params : { event : ToolCallResultEvent } & RunAgentSubscriberParams ,
106
- ) : AgentStateMutation | undefined ;
110
+ ) : MaybePromise < AgentStateMutation | undefined > ;
107
111
108
112
onStateSnapshotEvent ?(
109
113
params : { event : StateSnapshotEvent } & RunAgentSubscriberParams ,
110
- ) : AgentStateMutation | undefined ;
114
+ ) : MaybePromise < AgentStateMutation | undefined > ;
111
115
112
116
onStateDeltaEvent ?(
113
117
params : { event : StateDeltaEvent } & RunAgentSubscriberParams ,
114
- ) : AgentStateMutation | undefined ;
118
+ ) : MaybePromise < AgentStateMutation | undefined > ;
115
119
116
120
onMessagesSnapshotEvent ?(
117
121
params : { event : MessagesSnapshotEvent } & RunAgentSubscriberParams ,
118
- ) : AgentStateMutation | undefined ;
122
+ ) : MaybePromise < AgentStateMutation | undefined > ;
119
123
120
124
onRawEvent ?(
121
125
params : { event : RawEvent } & RunAgentSubscriberParams ,
122
- ) : AgentStateMutation | undefined ;
126
+ ) : MaybePromise < AgentStateMutation | undefined > ;
123
127
124
128
onCustomEvent ?(
125
129
params : { event : CustomEvent } & RunAgentSubscriberParams ,
126
- ) : AgentStateMutation | undefined ;
130
+ ) : MaybePromise < AgentStateMutation | undefined > ;
127
131
128
132
// State changes
129
- onMessagesChanged ?( params : Omit < RunAgentSubscriberParams , "state" > ) : void ;
130
- onStateChanged ?( params : Omit < RunAgentSubscriberParams , "messages" > ) : void ;
133
+ onMessagesChanged ?( params : Omit < RunAgentSubscriberParams , "state" > ) : MaybePromise < void > ;
134
+ onStateChanged ?( params : Omit < RunAgentSubscriberParams , "messages" > ) : MaybePromise < void > ;
131
135
}
132
136
133
- export function runSubscribersWithMutation (
137
+ export async function runSubscribersWithMutation (
134
138
subscribers : RunAgentSubscriber [ ] ,
135
139
initialMessages : Message [ ] ,
136
140
initialState : State ,
137
141
executor : (
138
142
subscriber : RunAgentSubscriber ,
139
143
messages : Message [ ] ,
140
144
state : State ,
141
- ) => AgentStateMutation | undefined ,
142
- ) : AgentStateMutation {
145
+ ) => MaybePromise < AgentStateMutation | undefined > ,
146
+ ) : Promise < AgentStateMutation > {
143
147
let messages : Message [ ] = initialMessages ;
144
148
let state : State = initialState ;
145
149
146
- let stopPropagation = undefined ;
150
+ let stopPropagation : boolean | undefined = undefined ;
147
151
148
152
for ( const subscriber of subscribers ) {
149
- const mutation = executor ( subscriber , structuredClone_ ( messages ) , structuredClone_ ( state ) ) ;
153
+ const mutation = await executor (
154
+ subscriber ,
155
+ structuredClone_ ( messages ) ,
156
+ structuredClone_ ( state ) ,
157
+ ) ;
150
158
151
159
if ( mutation === undefined ) {
152
160
// Nothing returned – keep going
0 commit comments