Skip to content

Commit 2dc7b5f

Browse files
committed
Add unit test
1 parent a9b4ca7 commit 2dc7b5f

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

sdks/typescript/packages/client/src/agent/agent.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export abstract class AbstractAgent {
3737
public subscribers: AgentSubscriber[] = [];
3838
public isRunning: boolean = false;
3939
private middlewares: Middleware[] = [];
40-
public maxVersion = packageJson.version;
40+
41+
get maxVersion() {
42+
return packageJson.version;
43+
}
4144

4245
constructor({
4346
agentId,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { AbstractAgent } from "@/agent";
2+
import { BaseEvent, EventType, Message, RunAgentInput } from "@ag-ui/core";
3+
import { Observable, of } from "rxjs";
4+
5+
class LegacyAgent extends AbstractAgent {
6+
public receivedInput?: RunAgentInput;
7+
8+
constructor(initialMessages: Message[]) {
9+
super({ initialMessages });
10+
}
11+
12+
override get maxVersion(): string {
13+
return "0.0.39";
14+
}
15+
16+
override run(input: RunAgentInput): Observable<BaseEvent> {
17+
this.receivedInput = input;
18+
return of({
19+
type: EventType.RUN_STARTED,
20+
threadId: input.threadId,
21+
runId: input.runId,
22+
} as BaseEvent);
23+
}
24+
25+
protected override prepareRunAgentInput(
26+
parameters?: Parameters<AbstractAgent["prepareRunAgentInput"]>[0],
27+
): RunAgentInput {
28+
const prepared = super.prepareRunAgentInput(parameters);
29+
return { ...prepared, parentRunId: "legacy-parent" };
30+
}
31+
}
32+
33+
describe("BackwardCompatibility_0_0_39 middleware (auto insertion)", () => {
34+
it("automatically strips parentRunId and flattens array message content when maxVersion <= 0.0.39", async () => {
35+
const initialMessages: Message[] = [
36+
{
37+
id: "msg-1",
38+
role: "user",
39+
content: [
40+
{ type: "text", text: "Hello " },
41+
{ type: "text", text: "world!" },
42+
{ type: "binary", mimeType: "text/plain", data: "ignored" },
43+
] as unknown as Message["content"],
44+
} as Message,
45+
{
46+
id: "msg-2",
47+
role: "assistant",
48+
content: undefined,
49+
} as Message,
50+
];
51+
52+
const agent = new LegacyAgent(initialMessages);
53+
54+
await agent.runAgent({
55+
runId: "run-1",
56+
tools: [],
57+
context: [],
58+
forwardedProps: {},
59+
});
60+
61+
expect(agent.receivedInput).toBeDefined();
62+
expect(agent.receivedInput?.parentRunId).toBeUndefined();
63+
expect(agent.receivedInput?.messages[0].content).toBe("Hello world!");
64+
expect(agent.receivedInput?.messages[1].content).toBe("");
65+
});
66+
});

0 commit comments

Comments
 (0)