Skip to content

Commit 64063c3

Browse files
committed
update subscriber interface
1 parent 56942f8 commit 64063c3

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

typescript-sdk/packages/client/src/agent/agent.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { lastValueFrom } from "rxjs";
1414
import { transformChunks } from "@/chunks";
1515
import { AgentStateMutation, RunAgentSubscriber, runSubscribersWithMutation } from "./subscriber";
1616

17+
export interface RunAgentResult {
18+
result: any;
19+
}
20+
1721
export abstract class AbstractAgent {
1822
public agentId?: string;
1923
public description: string;
@@ -53,7 +57,7 @@ export abstract class AbstractAgent {
5357
public async runAgent(
5458
parameters?: RunAgentParameters,
5559
subscriber?: RunAgentSubscriber,
56-
): Promise<any> {
60+
): Promise<RunAgentResult> {
5761
this.agentId = this.agentId ?? uuidv4();
5862
const input = this.prepareRunAgentInput(parameters);
5963
let result: any = undefined;
@@ -83,7 +87,7 @@ export abstract class AbstractAgent {
8387
}),
8488
);
8589

86-
return lastValueFrom(pipeline(of(null))).then(() => result);
90+
return lastValueFrom(pipeline(of(null))).then(() => ({ result }));
8791
}
8892

8993
public abortRun() {}
@@ -108,6 +112,7 @@ export abstract class AbstractAgent {
108112
subscribers.forEach((subscriber) => {
109113
subscriber.onMessagesChanged?.({
110114
messages: this.messages,
115+
state: this.state,
111116
agent: this,
112117
input,
113118
});
@@ -118,6 +123,7 @@ export abstract class AbstractAgent {
118123
subscribers.forEach((subscriber) => {
119124
subscriber.onStateChanged?.({
120125
state: this.state,
126+
messages: this.messages,
121127
agent: this,
122128
input,
123129
});
@@ -157,6 +163,7 @@ export abstract class AbstractAgent {
157163
subscribers.forEach((subscriber) => {
158164
subscriber.onMessagesChanged?.({
159165
messages: this.messages,
166+
state: this.state,
160167
agent: this,
161168
input,
162169
});
@@ -168,6 +175,7 @@ export abstract class AbstractAgent {
168175
subscribers.forEach((subscriber) => {
169176
subscriber.onStateChanged?.({
170177
state: this.state,
178+
messages: this.messages,
171179
agent: this,
172180
input,
173181
});
@@ -194,6 +202,7 @@ export abstract class AbstractAgent {
194202
subscribers.forEach((subscriber) => {
195203
subscriber.onMessagesChanged?.({
196204
messages: this.messages,
205+
state: this.state,
197206
agent: this,
198207
input,
199208
});
@@ -204,6 +213,7 @@ export abstract class AbstractAgent {
204213
subscribers.forEach((subscriber) => {
205214
subscriber.onStateChanged?.({
206215
state: this.state,
216+
messages: this.messages,
207217
agent: this,
208218
input,
209219
});
@@ -240,6 +250,7 @@ export abstract class AbstractAgent {
240250
subscribers.forEach((subscriber) => {
241251
subscriber.onMessagesChanged?.({
242252
messages: this.messages,
253+
state: this.state,
243254
agent: this,
244255
input,
245256
});
@@ -250,6 +261,7 @@ export abstract class AbstractAgent {
250261
subscribers.forEach((subscriber) => {
251262
subscriber.onStateChanged?.({
252263
state: this.state,
264+
messages: this.messages,
253265
agent: this,
254266
input,
255267
});

typescript-sdk/packages/client/src/agent/subscriber.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
MessagesSnapshotEvent,
2121
RawEvent,
2222
CustomEvent,
23+
ToolCall,
2324
} from "@ag-ui/core";
2425
import { AbstractAgent } from "./agent";
2526
import { structuredClone_ } from "@/utils";
@@ -131,8 +132,10 @@ export interface RunAgentSubscriber {
131132
): MaybePromise<AgentStateMutation | void>;
132133

133134
// State changes
134-
onMessagesChanged?(params: Omit<RunAgentSubscriberParams, "state">): MaybePromise<void>;
135-
onStateChanged?(params: Omit<RunAgentSubscriberParams, "messages">): MaybePromise<void>;
135+
onMessagesChanged?(params: RunAgentSubscriberParams): MaybePromise<void>;
136+
onStateChanged?(params: RunAgentSubscriberParams): MaybePromise<void>;
137+
onNewMessage?(params: { message: Message } & RunAgentSubscriberParams): MaybePromise<void>;
138+
onNewToolCall?(params: { toolCall: ToolCall } & RunAgentSubscriberParams): MaybePromise<void>;
136139
}
137140

138141
export async function runSubscribersWithMutation(

typescript-sdk/packages/client/src/apply/default.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ export const defaultApplyEvents = (
148148
);
149149
applyMutation(mutation);
150150

151+
await Promise.all(
152+
subscribers.map((subscriber) => {
153+
subscriber.onNewMessage?.({
154+
message: messages[messages.length - 1],
155+
messages,
156+
state,
157+
agent,
158+
input,
159+
});
160+
}),
161+
);
162+
151163
return emitUpdates();
152164
}
153165

@@ -287,6 +299,21 @@ export const defaultApplyEvents = (
287299
);
288300
applyMutation(mutation);
289301

302+
await Promise.all(
303+
subscribers.map((subscriber) => {
304+
subscriber.onNewToolCall?.({
305+
toolCall:
306+
messages[messages.length - 1].toolCalls[
307+
messages[messages.length - 1].toolCalls.length - 1
308+
],
309+
messages,
310+
state,
311+
agent,
312+
input,
313+
});
314+
}),
315+
);
316+
290317
return emitUpdates();
291318
}
292319

@@ -319,6 +346,18 @@ export const defaultApplyEvents = (
319346

320347
messages.push(toolMessage);
321348

349+
await Promise.all(
350+
subscribers.map((subscriber) => {
351+
subscriber.onNewMessage?.({
352+
message: toolMessage,
353+
messages,
354+
state,
355+
agent,
356+
input,
357+
});
358+
}),
359+
);
360+
322361
applyMutation({ messages });
323362
}
324363

0 commit comments

Comments
 (0)