@ag-ui/client 0.0.31 → 0.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,83 +1,55 @@
1
- import { ApplyEvents, BaseEvent, Message, State, RunAgentInput, RunAgent } from '@ag-ui/core';
1
+ import { Message, State, RunAgentInput, BaseEvent, RunStartedEvent, RunFinishedEvent, RunErrorEvent, StepStartedEvent, StepFinishedEvent, TextMessageStartEvent, TextMessageContentEvent, TextMessageEndEvent, ToolCallStartEvent, ToolCallArgsEvent, ToolCallEndEvent, ToolCallResultEvent, StateSnapshotEvent, StateDeltaEvent, MessagesSnapshotEvent, RawEvent, CustomEvent, ToolCall } from '@ag-ui/core';
2
2
  export * from '@ag-ui/core';
3
3
  import { Observable } from 'rxjs';
4
4
  import { z } from 'zod';
5
5
 
6
- declare const defaultApplyEvents: (...args: Parameters<ApplyEvents>) => ReturnType<ApplyEvents>;
7
-
8
- declare const verifyEvents: (debug: boolean) => (source$: Observable<BaseEvent>) => Observable<BaseEvent>;
9
-
10
- declare enum HttpEventType {
11
- HEADERS = "headers",
12
- DATA = "data"
13
- }
14
- interface HttpDataEvent {
15
- type: HttpEventType.DATA;
16
- data?: Uint8Array;
6
+ interface AgentConfig {
7
+ agentId?: string;
8
+ description?: string;
9
+ threadId?: string;
10
+ initialMessages?: Message[];
11
+ initialState?: State;
12
+ debug?: boolean;
17
13
  }
18
- interface HttpHeadersEvent {
19
- type: HttpEventType.HEADERS;
20
- status: number;
21
- headers: Headers;
14
+ interface HttpAgentConfig extends AgentConfig {
15
+ url: string;
16
+ headers?: Record<string, string>;
22
17
  }
23
- type HttpEvent = HttpDataEvent | HttpHeadersEvent;
24
- declare const runHttpRequest: (url: string, requestInit: RequestInit) => Observable<HttpEvent>;
25
-
26
- /**
27
- * Transforms HTTP events into BaseEvents using the appropriate format parser based on content type.
28
- */
29
- declare const transformHttpEventStream: (source$: Observable<HttpEvent>) => Observable<BaseEvent>;
30
-
31
- /**
32
- * Parses a stream of HTTP events into a stream of JSON objects using Server-Sent Events (SSE) format.
33
- * Strictly follows the SSE standard where:
34
- * - Events are separated by double newlines ('\n\n')
35
- * - Only 'data:' prefixed lines are processed
36
- * - Multi-line data events are supported and joined
37
- * - Non-data fields (event, id, retry) are ignored
38
- */
39
- declare const parseSSEStream: (source$: Observable<HttpEvent>) => Observable<any>;
40
-
41
- /**
42
- * Parses a stream of HTTP events into a stream of BaseEvent objects using Protocol Buffer format.
43
- * Each message is prefixed with a 4-byte length header (uint32 in big-endian format)
44
- * followed by the protocol buffer encoded message.
45
- */
46
- declare const parseProtoStream: (source$: Observable<HttpEvent>) => Observable<BaseEvent>;
18
+ type RunAgentParameters = Partial<Pick<RunAgentInput, "runId" | "tools" | "context" | "forwardedProps">>;
47
19
 
48
20
  declare const LegacyRuntimeProtocolEvent: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
49
21
  type: z.ZodLiteral<"TextMessageStart">;
50
22
  messageId: z.ZodString;
51
23
  parentMessageId: z.ZodOptional<z.ZodString>;
52
24
  }, "strip", z.ZodTypeAny, {
53
- messageId: string;
54
25
  type: "TextMessageStart";
26
+ messageId: string;
55
27
  parentMessageId?: string | undefined;
56
28
  }, {
57
- messageId: string;
58
29
  type: "TextMessageStart";
30
+ messageId: string;
59
31
  parentMessageId?: string | undefined;
60
32
  }>, z.ZodObject<{
61
33
  type: z.ZodLiteral<"TextMessageContent">;
62
34
  messageId: z.ZodString;
63
35
  content: z.ZodString;
64
36
  }, "strip", z.ZodTypeAny, {
37
+ type: "TextMessageContent";
65
38
  messageId: string;
66
39
  content: string;
67
- type: "TextMessageContent";
68
40
  }, {
41
+ type: "TextMessageContent";
69
42
  messageId: string;
70
43
  content: string;
71
- type: "TextMessageContent";
72
44
  }>, z.ZodObject<{
73
45
  type: z.ZodLiteral<"TextMessageEnd">;
74
46
  messageId: z.ZodString;
75
47
  }, "strip", z.ZodTypeAny, {
76
- messageId: string;
77
48
  type: "TextMessageEnd";
78
- }, {
79
49
  messageId: string;
50
+ }, {
80
51
  type: "TextMessageEnd";
52
+ messageId: string;
81
53
  }>, z.ZodObject<{
82
54
  type: z.ZodLiteral<"ActionExecutionStart">;
83
55
  actionExecutionId: z.ZodString;
@@ -141,55 +113,43 @@ declare const LegacyRuntimeProtocolEvent: z.ZodDiscriminatedUnion<"type", [z.Zod
141
113
  running: z.ZodBoolean;
142
114
  }, "strip", z.ZodTypeAny, {
143
115
  role: string;
144
- type: "AgentStateMessage";
116
+ runId: string;
145
117
  threadId: string;
118
+ state: string;
119
+ type: "AgentStateMessage";
146
120
  agentName: string;
147
121
  nodeName: string;
148
- runId: string;
149
122
  active: boolean;
150
- state: string;
151
123
  running: boolean;
152
124
  }, {
153
125
  role: string;
154
- type: "AgentStateMessage";
126
+ runId: string;
155
127
  threadId: string;
128
+ state: string;
129
+ type: "AgentStateMessage";
156
130
  agentName: string;
157
131
  nodeName: string;
158
- runId: string;
159
132
  active: boolean;
160
- state: string;
161
133
  running: boolean;
162
134
  }>, z.ZodObject<{
163
135
  type: z.ZodLiteral<"MetaEvent">;
164
136
  name: z.ZodEnum<["LangGraphInterruptEvent", "PredictState", "Exit"]>;
165
137
  value: z.ZodAny;
166
138
  }, "strip", z.ZodTypeAny, {
167
- name: "PredictState" | "LangGraphInterruptEvent" | "Exit";
168
139
  type: "MetaEvent";
140
+ name: "LangGraphInterruptEvent" | "PredictState" | "Exit";
169
141
  value?: any;
170
142
  }, {
171
- name: "PredictState" | "LangGraphInterruptEvent" | "Exit";
172
143
  type: "MetaEvent";
144
+ name: "LangGraphInterruptEvent" | "PredictState" | "Exit";
173
145
  value?: any;
174
146
  }>]>;
175
147
  type LegacyRuntimeProtocolEvent = z.infer<typeof LegacyRuntimeProtocolEvent>;
176
148
 
177
- declare const convertToLegacyEvents: (threadId: string, runId: string, agentName: string) => (events$: Observable<BaseEvent>) => Observable<LegacyRuntimeProtocolEvent>;
178
-
179
- interface AgentConfig {
180
- agentId?: string;
181
- description?: string;
182
- threadId?: string;
183
- initialMessages?: Message[];
184
- initialState?: State;
185
- debug?: boolean;
186
- }
187
- interface HttpAgentConfig extends AgentConfig {
188
- url: string;
189
- headers?: Record<string, string>;
149
+ interface RunAgentResult {
150
+ result: any;
151
+ newMessages: Message[];
190
152
  }
191
- type RunAgentParameters = Partial<Pick<RunAgentInput, "runId" | "tools" | "context" | "forwardedProps">>;
192
-
193
153
  declare abstract class AbstractAgent {
194
154
  agentId?: string;
195
155
  description: string;
@@ -197,19 +157,170 @@ declare abstract class AbstractAgent {
197
157
  messages: Message[];
198
158
  state: State;
199
159
  debug: boolean;
160
+ subscribers: AgentSubscriber[];
200
161
  constructor({ agentId, description, threadId, initialMessages, initialState, debug, }?: AgentConfig);
201
- protected abstract run(...args: Parameters<RunAgent>): ReturnType<RunAgent>;
202
- runAgent(parameters?: RunAgentParameters): Promise<void>;
162
+ subscribe(subscriber: AgentSubscriber): {
163
+ unsubscribe: () => void;
164
+ };
165
+ protected abstract run(input: RunAgentInput): Observable<BaseEvent>;
166
+ runAgent(parameters?: RunAgentParameters, subscriber?: AgentSubscriber): Promise<RunAgentResult>;
203
167
  abortRun(): void;
204
- protected apply(...args: Parameters<ApplyEvents>): ReturnType<ApplyEvents>;
205
- protected processApplyEvents(input: RunAgentInput, events$: ReturnType<ApplyEvents>): ReturnType<ApplyEvents>;
168
+ protected apply(input: RunAgentInput, events$: Observable<BaseEvent>, subscribers: AgentSubscriber[]): Observable<AgentStateMutation>;
169
+ protected processApplyEvents(input: RunAgentInput, events$: Observable<AgentStateMutation>, subscribers: AgentSubscriber[]): Observable<AgentStateMutation>;
206
170
  protected prepareRunAgentInput(parameters?: RunAgentParameters): RunAgentInput;
207
- protected onError(error: Error): void;
208
- protected onFinalize(): void;
171
+ protected onInitialize(input: RunAgentInput, subscribers: AgentSubscriber[]): Promise<void>;
172
+ protected onError(input: RunAgentInput, error: Error, subscribers: AgentSubscriber[]): Observable<AgentStateMutation>;
173
+ protected onFinalize(input: RunAgentInput, subscribers: AgentSubscriber[]): Promise<void>;
209
174
  clone(): any;
175
+ addMessage(message: Message): void;
176
+ addMessages(messages: Message[]): void;
177
+ setMessages(messages: Message[]): void;
178
+ setState(state: State): void;
210
179
  legacy_to_be_removed_runAgentBridged(config?: RunAgentParameters): Observable<LegacyRuntimeProtocolEvent>;
211
180
  }
212
181
 
182
+ interface AgentStateMutation {
183
+ messages?: Message[];
184
+ state?: State;
185
+ stopPropagation?: boolean;
186
+ }
187
+ interface AgentSubscriberParams {
188
+ messages: Message[];
189
+ state: State;
190
+ agent: AbstractAgent;
191
+ input: RunAgentInput;
192
+ }
193
+ type MaybePromise<T> = T | Promise<T>;
194
+ interface AgentSubscriber {
195
+ onRunInitialized?(params: AgentSubscriberParams): MaybePromise<Omit<AgentStateMutation, "stopPropagation"> | void>;
196
+ onRunFailed?(params: {
197
+ error: Error;
198
+ } & AgentSubscriberParams): MaybePromise<Omit<AgentStateMutation, "stopPropagation"> | void>;
199
+ onRunFinalized?(params: AgentSubscriberParams): MaybePromise<Omit<AgentStateMutation, "stopPropagation"> | void>;
200
+ onEvent?(params: {
201
+ event: BaseEvent;
202
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
203
+ onRunStartedEvent?(params: {
204
+ event: RunStartedEvent;
205
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
206
+ onRunFinishedEvent?(params: {
207
+ event: RunFinishedEvent;
208
+ result?: any;
209
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
210
+ onRunErrorEvent?(params: {
211
+ event: RunErrorEvent;
212
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
213
+ onStepStartedEvent?(params: {
214
+ event: StepStartedEvent;
215
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
216
+ onStepFinishedEvent?(params: {
217
+ event: StepFinishedEvent;
218
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
219
+ onTextMessageStartEvent?(params: {
220
+ event: TextMessageStartEvent;
221
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
222
+ onTextMessageContentEvent?(params: {
223
+ event: TextMessageContentEvent;
224
+ textMessageBuffer: string;
225
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
226
+ onTextMessageEndEvent?(params: {
227
+ event: TextMessageEndEvent;
228
+ textMessageBuffer: string;
229
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
230
+ onToolCallStartEvent?(params: {
231
+ event: ToolCallStartEvent;
232
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
233
+ onToolCallArgsEvent?(params: {
234
+ event: ToolCallArgsEvent;
235
+ toolCallBuffer: string;
236
+ toolCallName: string;
237
+ partialToolCallArgs: Record<string, any>;
238
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
239
+ onToolCallEndEvent?(params: {
240
+ event: ToolCallEndEvent;
241
+ toolCallName: string;
242
+ toolCallArgs: Record<string, any>;
243
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
244
+ onToolCallResultEvent?(params: {
245
+ event: ToolCallResultEvent;
246
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
247
+ onStateSnapshotEvent?(params: {
248
+ event: StateSnapshotEvent;
249
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
250
+ onStateDeltaEvent?(params: {
251
+ event: StateDeltaEvent;
252
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
253
+ onMessagesSnapshotEvent?(params: {
254
+ event: MessagesSnapshotEvent;
255
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
256
+ onRawEvent?(params: {
257
+ event: RawEvent;
258
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
259
+ onCustomEvent?(params: {
260
+ event: CustomEvent;
261
+ } & AgentSubscriberParams): MaybePromise<AgentStateMutation | void>;
262
+ onMessagesChanged?(params: Omit<AgentSubscriberParams, "input"> & {
263
+ input?: RunAgentInput;
264
+ }): MaybePromise<void>;
265
+ onStateChanged?(params: Omit<AgentSubscriberParams, "input"> & {
266
+ input?: RunAgentInput;
267
+ }): MaybePromise<void>;
268
+ onNewMessage?(params: {
269
+ message: Message;
270
+ } & Omit<AgentSubscriberParams, "input"> & {
271
+ input?: RunAgentInput;
272
+ }): MaybePromise<void>;
273
+ onNewToolCall?(params: {
274
+ toolCall: ToolCall;
275
+ } & Omit<AgentSubscriberParams, "input"> & {
276
+ input?: RunAgentInput;
277
+ }): MaybePromise<void>;
278
+ }
279
+
280
+ declare const defaultApplyEvents: (input: RunAgentInput, events$: Observable<BaseEvent>, agent: AbstractAgent, subscribers: AgentSubscriber[]) => Observable<AgentStateMutation>;
281
+
282
+ declare const verifyEvents: (debug: boolean) => (source$: Observable<BaseEvent>) => Observable<BaseEvent>;
283
+
284
+ declare enum HttpEventType {
285
+ HEADERS = "headers",
286
+ DATA = "data"
287
+ }
288
+ interface HttpDataEvent {
289
+ type: HttpEventType.DATA;
290
+ data?: Uint8Array;
291
+ }
292
+ interface HttpHeadersEvent {
293
+ type: HttpEventType.HEADERS;
294
+ status: number;
295
+ headers: Headers;
296
+ }
297
+ type HttpEvent = HttpDataEvent | HttpHeadersEvent;
298
+ declare const runHttpRequest: (url: string, requestInit: RequestInit) => Observable<HttpEvent>;
299
+
300
+ /**
301
+ * Transforms HTTP events into BaseEvents using the appropriate format parser based on content type.
302
+ */
303
+ declare const transformHttpEventStream: (source$: Observable<HttpEvent>) => Observable<BaseEvent>;
304
+
305
+ /**
306
+ * Parses a stream of HTTP events into a stream of JSON objects using Server-Sent Events (SSE) format.
307
+ * Strictly follows the SSE standard where:
308
+ * - Events are separated by double newlines ('\n\n')
309
+ * - Only 'data:' prefixed lines are processed
310
+ * - Multi-line data events are supported and joined
311
+ * - Non-data fields (event, id, retry) are ignored
312
+ */
313
+ declare const parseSSEStream: (source$: Observable<HttpEvent>) => Observable<any>;
314
+
315
+ /**
316
+ * Parses a stream of HTTP events into a stream of BaseEvent objects using Protocol Buffer format.
317
+ * Each message is prefixed with a 4-byte length header (uint32 in big-endian format)
318
+ * followed by the protocol buffer encoded message.
319
+ */
320
+ declare const parseProtoStream: (source$: Observable<HttpEvent>) => Observable<BaseEvent>;
321
+
322
+ declare const convertToLegacyEvents: (threadId: string, runId: string, agentName: string) => (events$: Observable<BaseEvent>) => Observable<LegacyRuntimeProtocolEvent>;
323
+
213
324
  interface RunHttpAgentConfig extends RunAgentParameters {
214
325
  abortController?: AbortController;
215
326
  }
@@ -224,7 +335,7 @@ declare class HttpAgent extends AbstractAgent {
224
335
  * @returns The fetch config for the http request.
225
336
  */
226
337
  protected requestInit(input: RunAgentInput): RequestInit;
227
- runAgent(parameters?: RunHttpAgentConfig): Promise<void>;
338
+ runAgent(parameters?: RunHttpAgentConfig): Promise<RunAgentResult>;
228
339
  abortRun(): void;
229
340
  constructor(config: HttpAgentConfig);
230
341
  run(input: RunAgentInput): Observable<BaseEvent>;