Skip to content

Commit be5d44b

Browse files
committed
fix unit test compile errors
1 parent 82bac56 commit be5d44b

File tree

3 files changed

+116
-78
lines changed

3 files changed

+116
-78
lines changed

typescript-sdk/packages/client/src/apply/__tests__/default.state.test.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1+
import { AbstractAgent } from "@/agent";
12
import { defaultApplyEvents } from "../default";
2-
import { EventType, StateDeltaEvent, AgentState } from "@ag-ui/core";
3+
import { EventType, StateDeltaEvent } from "@ag-ui/core";
34
import { of } from "rxjs";
5+
import { AgentStateMutation } from "@/agent/subscriber";
46

5-
// Define the exact type expected by defaultApplyEvents
6-
interface RunAgentInput {
7-
threadId: string;
8-
runId: string;
9-
messages: {
10-
id: string;
11-
role: "developer" | "system" | "assistant" | "user";
12-
content?: string;
13-
name?: string;
14-
toolCalls?: any[];
15-
}[];
16-
tools: any[];
17-
context: any[];
18-
state?: any;
19-
forwardedProps?: any;
20-
}
7+
const FAKE_AGENT = null as unknown as AbstractAgent;
218

229
describe("defaultApplyEvents - State Patching", () => {
2310
it("should apply state delta patch correctly", (done) => {
@@ -43,9 +30,9 @@ describe("defaultApplyEvents - State Patching", () => {
4330

4431
const events$ = of(stateDelta);
4532

46-
const result$ = defaultApplyEvents(initialState, events$);
33+
const result$ = defaultApplyEvents(initialState, events$, FAKE_AGENT, []);
4734

48-
result$.subscribe((update: AgentState) => {
35+
result$.subscribe((update: AgentStateMutation) => {
4936
expect(update.state).toEqual({
5037
count: 1,
5138
text: "world",
@@ -78,9 +65,9 @@ describe("defaultApplyEvents - State Patching", () => {
7865

7966
const events$ = of(stateDelta);
8067
// Cast to any to bypass strict type checking
81-
const result$ = defaultApplyEvents(initialState as any, events$);
68+
const result$ = defaultApplyEvents(initialState as any, events$, FAKE_AGENT, []);
8269

83-
result$.subscribe((update: AgentState) => {
70+
result$.subscribe((update: AgentStateMutation) => {
8471
expect(update.state).toEqual({
8572
user: {
8673
name: "John",
@@ -115,9 +102,9 @@ describe("defaultApplyEvents - State Patching", () => {
115102

116103
const events$ = of(stateDelta);
117104
// Cast to any to bypass strict type checking
118-
const result$ = defaultApplyEvents(initialState as any, events$);
105+
const result$ = defaultApplyEvents(initialState as any, events$, FAKE_AGENT, []);
119106

120-
result$.subscribe((update: AgentState) => {
107+
result$.subscribe((update: AgentStateMutation) => {
121108
expect(update.state).toEqual({
122109
items: ["x", "b", "c", "d"],
123110
});
@@ -150,10 +137,10 @@ describe("defaultApplyEvents - State Patching", () => {
150137

151138
const events$ = of(...stateDeltas);
152139
// Cast to any to bypass strict type checking
153-
const result$ = defaultApplyEvents(initialState as any, events$);
140+
const result$ = defaultApplyEvents(initialState as any, events$, FAKE_AGENT, []);
154141

155142
let updateCount = 0;
156-
result$.subscribe((update: AgentState) => {
143+
result$.subscribe((update: AgentStateMutation) => {
157144
updateCount++;
158145
if (updateCount === 2) {
159146
expect(update.state).toEqual({
@@ -189,11 +176,11 @@ describe("defaultApplyEvents - State Patching", () => {
189176

190177
const events$ = of(stateDelta);
191178
// Cast to any to bypass strict type checking
192-
const result$ = defaultApplyEvents(initialState as any, events$);
179+
const result$ = defaultApplyEvents(initialState as any, events$, FAKE_AGENT, []);
193180

194181
let updateCount = 0;
195182
result$.subscribe({
196-
next: (update: AgentState) => {
183+
next: (update: AgentStateMutation) => {
197184
updateCount++;
198185
},
199186
complete: () => {

typescript-sdk/packages/client/src/apply/__tests__/default.text-message.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import { firstValueFrom } from "rxjs";
44
import {
55
BaseEvent,
66
EventType,
7-
AgentState,
87
RunStartedEvent,
98
TextMessageStartEvent,
109
TextMessageContentEvent,
1110
TextMessageEndEvent,
1211
RunAgentInput,
1312
} from "@ag-ui/core";
1413
import { defaultApplyEvents } from "../default";
14+
import { AbstractAgent } from "@/agent";
15+
16+
const FAKE_AGENT = null as unknown as AbstractAgent;
1517

1618
describe("defaultApplyEvents with text messages", () => {
1719
it("should handle text message events correctly", async () => {
@@ -27,7 +29,7 @@ describe("defaultApplyEvents with text messages", () => {
2729
};
2830

2931
// Create the observable stream
30-
const result$ = defaultApplyEvents(initialState, events$);
32+
const result$ = defaultApplyEvents(initialState, events$, FAKE_AGENT, []);
3133

3234
// Collect all emitted state updates in an array
3335
const stateUpdatesPromise = firstValueFrom(result$.pipe(toArray()));
@@ -100,7 +102,7 @@ describe("defaultApplyEvents with text messages", () => {
100102
};
101103

102104
// Create the observable stream
103-
const result$ = defaultApplyEvents(initialState, events$);
105+
const result$ = defaultApplyEvents(initialState, events$, FAKE_AGENT, []);
104106

105107
// Collect all emitted state updates in an array
106108
const stateUpdatesPromise = firstValueFrom(result$.pipe(toArray()));

typescript-sdk/packages/client/src/apply/__tests__/default.tool-calls.test.ts

Lines changed: 97 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,36 @@ import { firstValueFrom } from "rxjs";
44
import {
55
BaseEvent,
66
EventType,
7-
AgentState,
87
RunStartedEvent,
98
ToolCallStartEvent,
109
ToolCallArgsEvent,
1110
ToolCallEndEvent,
11+
RunAgentInput,
12+
AssistantMessage,
1213
} from "@ag-ui/core";
1314
import { defaultApplyEvents } from "../default";
15+
import { AbstractAgent } from "@/agent";
16+
17+
const FAKE_AGENT = null as unknown as AbstractAgent;
1418

1519
describe("defaultApplyEvents with tool calls", () => {
1620
it("should handle a single tool call correctly", async () => {
1721
// Create a subject and state for events
1822
const events$ = new Subject<BaseEvent>();
19-
const initialState: AgentState = {
23+
const initialState = {
2024
messages: [],
21-
state: {},
25+
state: {
26+
count: 0,
27+
text: "hello",
28+
},
29+
threadId: "test-thread",
30+
runId: "test-run",
31+
tools: [],
32+
context: [],
2233
};
2334

2435
// Create the observable stream
25-
const result$ = defaultApplyEvents(initialState, events$);
36+
const result$ = defaultApplyEvents(initialState, events$, FAKE_AGENT, []);
2637

2738
// Collect all emitted state updates in an array
2839
const stateUpdatesPromise = firstValueFrom(result$.pipe(toArray()));
@@ -70,36 +81,46 @@ describe("defaultApplyEvents with tool calls", () => {
7081
expect(stateUpdates.length).toBe(4);
7182

7283
// First update: tool call created
73-
expect(stateUpdates[0].messages.length).toBe(1);
74-
expect(stateUpdates[0].messages[0]?.toolCalls?.length).toBe(1);
75-
expect(stateUpdates[0].messages[0]?.toolCalls?.[0]?.id).toBe("tool1");
76-
expect(stateUpdates[0].messages[0]?.toolCalls?.[0]?.function?.name).toBe("search");
77-
expect(stateUpdates[0].messages[0]?.toolCalls?.[0]?.function?.arguments).toBe("");
84+
expect(stateUpdates[0].messages?.length).toBe(1);
85+
expect((stateUpdates[0].messages?.[0] as AssistantMessage).toolCalls?.length).toBe(1);
86+
expect((stateUpdates[0].messages?.[0] as AssistantMessage).toolCalls?.[0]?.id).toBe("tool1");
87+
expect((stateUpdates[0].messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.name).toBe(
88+
"search",
89+
);
90+
expect(
91+
(stateUpdates[0].messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.arguments,
92+
).toBe("");
7893

7994
// Second update: first args chunk added
80-
expect(stateUpdates[1].messages[0]?.toolCalls?.[0]?.function?.arguments).toBe('{"query": "');
95+
expect(
96+
(stateUpdates[1].messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.arguments,
97+
).toBe('{"query": "');
8198

8299
// Third update: second args chunk appended
83-
expect(stateUpdates[2].messages[0]?.toolCalls?.[0]?.function?.arguments).toBe(
84-
'{"query": "test search',
85-
);
100+
expect(
101+
(stateUpdates[2].messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.arguments,
102+
).toBe('{"query": "test search');
86103

87104
// Fourth update: third args chunk appended
88-
expect(stateUpdates[3].messages[0]?.toolCalls?.[0]?.function?.arguments).toBe(
89-
'{"query": "test search"}',
90-
);
105+
expect(
106+
(stateUpdates[3].messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.arguments,
107+
).toBe('{"query": "test search"}');
91108
});
92109

93110
it("should handle multiple tool calls correctly", async () => {
94111
// Create a subject and state for events
95112
const events$ = new Subject<BaseEvent>();
96-
const initialState: AgentState = {
113+
const initialState: RunAgentInput = {
97114
messages: [],
98115
state: {},
116+
threadId: "test-thread",
117+
runId: "test-run",
118+
tools: [],
119+
context: [],
99120
};
100121

101122
// Create the observable stream
102-
const result$ = defaultApplyEvents(initialState, events$);
123+
const result$ = defaultApplyEvents(initialState, events$, FAKE_AGENT, []);
103124

104125
// Collect all emitted state updates in an array
105126
const stateUpdatesPromise = firstValueFrom(result$.pipe(toArray()));
@@ -157,19 +178,25 @@ describe("defaultApplyEvents with tool calls", () => {
157178

158179
// Check last state update for the correct tool calls
159180
const finalState = stateUpdates[stateUpdates.length - 1];
160-
expect(finalState.messages.length).toBe(2);
181+
expect(finalState.messages?.length).toBe(2);
161182

162183
// First message should have first tool call
163-
expect(finalState.messages[0]?.toolCalls?.length).toBe(1);
164-
expect(finalState.messages[0]?.toolCalls?.[0]?.id).toBe("tool1");
165-
expect(finalState.messages[0]?.toolCalls?.[0]?.function?.name).toBe("search");
166-
expect(finalState.messages[0]?.toolCalls?.[0]?.function?.arguments).toBe('{"query":"test"}');
184+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.length).toBe(1);
185+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.[0]?.id).toBe("tool1");
186+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.name).toBe(
187+
"search",
188+
);
189+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.arguments).toBe(
190+
'{"query":"test"}',
191+
);
167192

168193
// Second message should have second tool call
169-
expect(finalState.messages[1]?.toolCalls?.length).toBe(1);
170-
expect(finalState.messages[1]?.toolCalls?.[0]?.id).toBe("tool2");
171-
expect(finalState.messages[1]?.toolCalls?.[0]?.function?.name).toBe("calculate");
172-
expect(finalState.messages[1]?.toolCalls?.[0]?.function?.arguments).toBe(
194+
expect((finalState.messages?.[1] as AssistantMessage).toolCalls?.length).toBe(1);
195+
expect((finalState.messages?.[1] as AssistantMessage).toolCalls?.[0]?.id).toBe("tool2");
196+
expect((finalState.messages?.[1] as AssistantMessage).toolCalls?.[0]?.function?.name).toBe(
197+
"calculate",
198+
);
199+
expect((finalState.messages?.[1] as AssistantMessage).toolCalls?.[0]?.function?.arguments).toBe(
173200
'{"expression":"1+1"}',
174201
);
175202
});
@@ -180,7 +207,7 @@ describe("defaultApplyEvents with tool calls", () => {
180207

181208
// Create initial state with an existing message
182209
const parentMessageId = "existing_message";
183-
const initialState: AgentState = {
210+
const initialState: RunAgentInput = {
184211
messages: [
185212
{
186213
id: parentMessageId,
@@ -190,10 +217,14 @@ describe("defaultApplyEvents with tool calls", () => {
190217
},
191218
],
192219
state: {},
220+
threadId: "test-thread",
221+
runId: "test-run",
222+
tools: [],
223+
context: [],
193224
};
194225

195226
// Create the observable stream
196-
const result$ = defaultApplyEvents(initialState, events$);
227+
const result$ = defaultApplyEvents(initialState, events$, FAKE_AGENT, []);
197228

198229
// Collect all emitted state updates in an array
199230
const stateUpdatesPromise = firstValueFrom(result$.pipe(toArray()));
@@ -230,25 +261,33 @@ describe("defaultApplyEvents with tool calls", () => {
230261

231262
// Check that the tool call was added to the existing message
232263
const finalState = stateUpdates[stateUpdates.length - 1];
233-
expect(finalState.messages.length).toBe(1);
234-
expect(finalState.messages[0].id).toBe(parentMessageId);
235-
expect(finalState.messages[0].content).toBe("I'll help you with that.");
236-
expect(finalState.messages[0]?.toolCalls?.length).toBe(1);
237-
expect(finalState.messages[0]?.toolCalls?.[0]?.id).toBe("tool1");
238-
expect(finalState.messages[0]?.toolCalls?.[0]?.function?.name).toBe("search");
239-
expect(finalState.messages[0]?.toolCalls?.[0]?.function?.arguments).toBe('{"query":"test"}');
264+
expect(finalState.messages?.length).toBe(1);
265+
expect(finalState.messages?.[0]?.id).toBe(parentMessageId);
266+
expect(finalState.messages?.[0]?.content).toBe("I'll help you with that.");
267+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.length).toBe(1);
268+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.[0]?.id).toBe("tool1");
269+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.name).toBe(
270+
"search",
271+
);
272+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.arguments).toBe(
273+
'{"query":"test"}',
274+
);
240275
});
241276

242277
it("should handle errors and partial updates correctly", async () => {
243278
// Create a subject and state for events
244279
const events$ = new Subject<BaseEvent>();
245-
const initialState: AgentState = {
280+
const initialState: RunAgentInput = {
246281
messages: [],
247282
state: {},
283+
threadId: "test-thread",
284+
runId: "test-run",
285+
tools: [],
286+
context: [],
248287
};
249288

250289
// Create the observable stream
251-
const result$ = defaultApplyEvents(initialState, events$);
290+
const result$ = defaultApplyEvents(initialState, events$, FAKE_AGENT, []);
252291

253292
// Collect all emitted state updates in an array
254293
const stateUpdatesPromise = firstValueFrom(result$.pipe(toArray()));
@@ -289,19 +328,25 @@ describe("defaultApplyEvents with tool calls", () => {
289328

290329
// Check the final JSON (should be valid now)
291330
const finalState = stateUpdates[stateUpdates.length - 1];
292-
expect(finalState.messages[0]?.toolCalls?.[0]?.function?.arguments).toBe('{"query:"test"}');
331+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.arguments).toBe(
332+
'{"query:"test"}',
333+
);
293334
});
294335

295336
it("should handle advanced scenarios with multiple tools and text messages", async () => {
296337
// Create a subject and state for events
297338
const events$ = new Subject<BaseEvent>();
298-
const initialState: AgentState = {
339+
const initialState: RunAgentInput = {
299340
messages: [],
300341
state: {},
342+
threadId: "test-thread",
343+
runId: "test-run",
344+
tools: [],
345+
context: [],
301346
};
302347

303348
// Create the observable stream
304-
const result$ = defaultApplyEvents(initialState, events$);
349+
const result$ = defaultApplyEvents(initialState, events$, FAKE_AGENT, []);
305350

306351
// Collect all emitted state updates in an array
307352
const stateUpdatesPromise = firstValueFrom(result$.pipe(toArray()));
@@ -355,16 +400,20 @@ describe("defaultApplyEvents with tool calls", () => {
355400

356401
// Check the final state for both tool calls
357402
const finalState = stateUpdates[stateUpdates.length - 1];
358-
expect(finalState.messages.length).toBe(2);
403+
expect(finalState.messages?.length).toBe(2);
359404

360405
// Verify first tool call
361-
expect(finalState.messages[0]?.toolCalls?.length).toBe(1);
362-
expect(finalState.messages[0]?.toolCalls?.[0]?.id).toBe("tool1");
363-
expect(finalState.messages[0]?.toolCalls?.[0]?.function?.name).toBe("search");
406+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.length).toBe(1);
407+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.[0]?.id).toBe("tool1");
408+
expect((finalState.messages?.[0] as AssistantMessage).toolCalls?.[0]?.function?.name).toBe(
409+
"search",
410+
);
364411

365412
// Verify second tool call
366-
expect(finalState.messages[1]?.toolCalls?.length).toBe(1);
367-
expect(finalState.messages[1]?.toolCalls?.[0]?.id).toBe("tool2");
368-
expect(finalState.messages[1]?.toolCalls?.[0]?.function?.name).toBe("calculate");
413+
expect((finalState.messages?.[1] as AssistantMessage).toolCalls?.length).toBe(1);
414+
expect((finalState.messages?.[1] as AssistantMessage).toolCalls?.[0]?.id).toBe("tool2");
415+
expect((finalState.messages?.[1] as AssistantMessage).toolCalls?.[0]?.function?.name).toBe(
416+
"calculate",
417+
);
369418
});
370419
});

0 commit comments

Comments
 (0)