Skip to content

Commit 6fc58a6

Browse files
committed
addMessage
1 parent eaea76a commit 6fc58a6

File tree

3 files changed

+80
-13
lines changed

3 files changed

+80
-13
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { AbstractAgent } from "../agent";
2-
import { RunAgentSubscriber, AgentStateMutation, runSubscribersWithMutation } from "../subscriber";
2+
import { RunAgentSubscriber } from "../subscriber";
33
import {
44
BaseEvent,
55
EventType,
66
Message,
7-
State,
87
RunAgentInput,
98
TextMessageStartEvent,
109
TextMessageContentEvent,
@@ -17,14 +16,11 @@ import {
1716
ToolCallEndEvent,
1817
ToolCallResultEvent,
1918
CustomEvent,
20-
ToolCall,
2119
StepStartedEvent,
2220
StepFinishedEvent,
2321
} from "@ag-ui/core";
2422
import { Observable, of, throwError, from } from "rxjs";
25-
import { mergeMap, defaultIfEmpty, startWith } from "rxjs/operators";
26-
import { v4 as uuidv4 } from "uuid";
27-
import { defaultApplyEvents } from "@/apply/default";
23+
import { mergeMap } from "rxjs/operators";
2824

2925
// Mock uuid module
3026
jest.mock("uuid", () => ({

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defaultApplyEvents } from "@/apply/default";
2-
import { Message, State, RunAgentInput, BaseEvent } from "@ag-ui/core";
2+
import { Message, State, RunAgentInput, BaseEvent, ToolCall, AssistantMessage } from "@ag-ui/core";
33

44
import { AgentConfig, RunAgentParameters } from "./types";
55
import { v4 as uuidv4 } from "uuid";
@@ -283,6 +283,58 @@ export abstract class AbstractAgent {
283283
return cloned;
284284
}
285285

286+
public addMessage(message: Message) {
287+
// Add message to the messages array
288+
this.messages.push(message);
289+
290+
// Create minimal input for subscriber context
291+
const input: RunAgentInput = {
292+
threadId: this.threadId,
293+
runId: uuidv4(),
294+
tools: [],
295+
context: [],
296+
forwardedProps: {},
297+
state: structuredClone_(this.state),
298+
messages: structuredClone_(this.messages),
299+
};
300+
301+
// Fire onMessagesChanged
302+
this.subscribers.forEach((subscriber) => {
303+
subscriber.onMessagesChanged?.({
304+
messages: this.messages,
305+
state: this.state,
306+
agent: this,
307+
input,
308+
});
309+
});
310+
311+
// Fire onNewMessage
312+
this.subscribers.forEach((subscriber) => {
313+
subscriber.onNewMessage?.({
314+
message,
315+
messages: this.messages,
316+
state: this.state,
317+
agent: this,
318+
input,
319+
});
320+
});
321+
322+
// Fire onNewToolCall if the message is from assistant and contains tool calls
323+
if (message.role === "assistant" && message.toolCalls) {
324+
message.toolCalls.forEach((toolCall: ToolCall) => {
325+
this.subscribers.forEach((subscriber) => {
326+
subscriber.onNewToolCall?.({
327+
toolCall,
328+
messages: this.messages,
329+
state: this.state,
330+
agent: this,
331+
input,
332+
});
333+
});
334+
});
335+
}
336+
}
337+
286338
public legacy_to_be_removed_runAgentBridged(
287339
config?: RunAgentParameters,
288340
): Observable<LegacyRuntimeProtocolEvent> {

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,22 @@ export interface RunAgentSubscriber {
132132
): MaybePromise<AgentStateMutation | void>;
133133

134134
// State changes
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>;
135+
onMessagesChanged?(
136+
params: Omit<RunAgentSubscriberParams, "input"> & { input?: RunAgentInput },
137+
): MaybePromise<void>;
138+
onStateChanged?(
139+
params: Omit<RunAgentSubscriberParams, "input"> & { input?: RunAgentInput },
140+
): MaybePromise<void>;
141+
onNewMessage?(
142+
params: { message: Message } & Omit<RunAgentSubscriberParams, "input"> & {
143+
input?: RunAgentInput;
144+
},
145+
): MaybePromise<void>;
146+
onNewToolCall?(
147+
params: { toolCall: ToolCall } & Omit<RunAgentSubscriberParams, "input"> & {
148+
input?: RunAgentInput;
149+
},
150+
): MaybePromise<void>;
139151
}
140152

141153
export async function runSubscribersWithMutation(
@@ -181,8 +193,15 @@ export async function runSubscribersWithMutation(
181193
break;
182194
}
183195
} catch (error) {
184-
// Log subscriber errors but continue processing
185-
console.error("Subscriber error:", error);
196+
// Log subscriber errors but continue processing (silence during tests)
197+
const isTestEnvironment =
198+
process.env.NODE_ENV === "test" ||
199+
process.env.JEST_WORKER_ID !== undefined ||
200+
typeof jest !== "undefined";
201+
202+
if (!isTestEnvironment) {
203+
console.error("Subscriber error:", error);
204+
}
186205
// Continue to next subscriber unless we want to stop propagation
187206
continue;
188207
}

0 commit comments

Comments
 (0)