Skip to content

Commit 214bf13

Browse files
jagnani73rogarcia
andauthored
Yj-feat/vercel-migration (#80)
Migrate to Vercel AI SDK for ModelProviders (breaking change!) --------- Co-authored-by: Rodrigo Garcia <[email protected]>
1 parent 1ec4d6b commit 214bf13

31 files changed

+1731
-1985
lines changed

packages/ai-agent-sdk/.env.example

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# LLM API Keys
22
OPENAI_API_KEY
3-
GEMINI_API_KEY
4-
DEEPSEEK_API_KEY
5-
GROK_API_KEY
3+
GOOGLE_GENERATIVE_AI_API_KEY
64

75
# Tool API Keys
86
GOLDRUSH_API_KEY

packages/ai-agent-sdk/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"test": "vitest",
3939
"test:agent": "vitest src/core/agent/agent.test.ts",
4040
"test:llm": "vitest src/core/llm/llm.test.ts",
41-
"test:tools:goldrush": "vitest src/core/tools/goldrush/index.test.ts",
41+
"test:tools:goldrush": "vitest src/core/tools/goldrush/goldrush.test.ts",
4242
"test:zee": "vitest src/core/zee/zee.test.ts",
4343
"build": "tsc",
4444
"clean": "rm -rf dist",
@@ -48,7 +48,11 @@
4848
"pretty": "prettier . --write"
4949
},
5050
"dependencies": {
51+
"@ai-sdk/anthropic": "^1.1.6",
52+
"@ai-sdk/google": "^1.1.11",
53+
"@ai-sdk/openai": "^1.1.9",
5154
"@covalenthq/client-sdk": "^2.2.3",
55+
"ai": "^4.1.34",
5256
"commander": "^13.1.0",
5357
"dotenv": "^16.4.7",
5458
"openai": "^4.79.1",

packages/ai-agent-sdk/src/core/agent/agent.test.ts

+42-87
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,87 @@
11
import { Agent } from ".";
2-
import { user } from "../base";
3-
import type { ModelConfig } from "../llm";
4-
import { StateFn } from "../state";
5-
import { createTool, runToolCalls } from "../tools";
2+
import { userMessage } from "../../functions";
3+
import { type ModelProvider } from "../llm";
4+
import { Tool, type ToolSet } from "../tools";
65
import fetch from "node-fetch";
7-
import type { ChatCompletionAssistantMessageParam } from "openai/resources";
86
import { describe, expect, test } from "vitest";
97
import { z } from "zod";
108

11-
describe("agent", () => {
12-
const providers: ModelConfig[] = [
9+
describe("@ai-agent-sdk/agent", () => {
10+
const providers: ModelProvider[] = [
1311
{
14-
provider: "OPEN_AI",
15-
name: "gpt-4o-mini",
12+
provider: "openai",
13+
id: "gpt-4o-mini",
1614
},
1715
{
18-
provider: "GEMINI",
19-
name: "gemini-1.5-flash",
16+
provider: "google",
17+
id: "gemini-1.5-flash",
2018
},
21-
] as const;
19+
];
2220

23-
providers.forEach((config) => {
24-
describe(config.provider, () => {
21+
providers.forEach((model) => {
22+
describe(`${model.provider}::${model.id}`, () => {
2523
test("default agent flow", async () => {
2624
const agent = new Agent({
2725
name: "research agent",
28-
model: config,
26+
model: model,
2927
description:
30-
"You are a senior NYT researcher writing an article on a topic.",
28+
"You are a senior New York Times researcher writing an article on a topic.",
3129
instructions: [
3230
"For a given topic, search for the top 5 links.",
3331
"Then read each URL and extract the article text, if a URL isn't available, ignore it.",
34-
"Analyse and prepare an NYT worthy article based on the information.",
32+
"Analyze and prepare an New York Times worthy article based on the information.",
3533
],
3634
});
3735

38-
const schema = {
39-
article: z.object({
40-
title: z.string(),
41-
text: z.string(),
42-
}),
43-
};
44-
45-
const result = await agent.generate(
46-
[user("The future of AI")],
47-
schema
48-
);
36+
const result = await agent.generate({
37+
messages: [userMessage("Future of AI")],
38+
});
4939

5040
console.log(result);
5141

52-
if (result.type !== "article") {
53-
throw new Error(
54-
`Expected article response, got ${result.type}`
55-
);
56-
}
57-
58-
expect(result.value["title"]).toBeDefined();
59-
expect(result.value["text"]).toBeDefined();
42+
expect(result.type).toBe("assistant");
43+
expect(result.value).toBeDefined();
6044
});
6145

6246
test("agent with custom tool", async () => {
63-
const tools = {
64-
weather: createTool({
65-
id: "weather-tool",
66-
description:
67-
"Fetch the current weather in Vancouver, BC",
68-
schema: z.object({
69-
temperature: z.number(),
47+
const tools: ToolSet = {
48+
weather: new Tool({
49+
provider: model.provider,
50+
name: "weather",
51+
description: "Fetch the current weather in a location",
52+
parameters: z.object({
53+
location: z.string(),
7054
}),
71-
execute: async (_args) => {
72-
const lat = 49.2827,
73-
lon = -123.1207;
74-
75-
const url = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}&current_weather=true`;
76-
77-
const r = await fetch(url);
78-
const data = await r.json();
79-
80-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
81-
// @ts-expect-error
82-
return `Current temperature in Vancouver, BC is ${data.current_weather.temperature}°C`;
55+
execute: async ({ location }) => {
56+
const response = await fetch(
57+
`https://api.weatherapi.com/v1/current.json?q=${location}&key=88f97127772c41a991095603230604`
58+
);
59+
const data = await response.json();
60+
return data;
8361
},
8462
}),
8563
};
8664

8765
const agent = new Agent({
88-
name: "research agent",
89-
model: config,
66+
name: "weather agent",
67+
model,
9068
description:
91-
"You are a senior NYT researcher writing an article on the current weather in Vancouver, BC.",
69+
"You are a senior weather analyst writing a summary on the current weather for the provided location.",
9270
instructions: [
9371
"Use the weather tool to get the current weather in Celsius.",
9472
"Elaborate on the weather.",
9573
],
9674
tools,
9775
});
9876

99-
const state = StateFn.root(agent.description);
100-
state.messages.push(
101-
user("What is the weather in Vancouver, BC?")
102-
);
103-
104-
const result = await agent.run(state);
105-
expect(result.status).toEqual("paused");
106-
expect(result.messages.length).toBeGreaterThan(0);
107-
108-
const toolCall = result.messages[
109-
result.messages.length - 1
110-
] as ChatCompletionAssistantMessageParam;
111-
expect(toolCall?.tool_calls?.length).toBeGreaterThanOrEqual(1);
112-
113-
const toolResponses = await runToolCalls(
114-
tools,
115-
toolCall?.tool_calls ?? []
116-
);
117-
118-
const updatedState = {
119-
...result,
120-
status: "running" as const,
121-
messages: [...result.messages, ...toolResponses],
122-
};
123-
124-
const finalResult = await agent.run(updatedState);
77+
const result = await agent.generate({
78+
messages: [userMessage("What is the weather in Delhi?")],
79+
});
12580

126-
console.log(finalResult);
81+
console.log(result);
12782

128-
expect(finalResult.messages.length).toBeGreaterThan(1);
129-
expect(finalResult.status).toEqual("finished");
83+
expect(result.type).toBe("assistant");
84+
expect(result.value).toBeDefined();
13085
});
13186
});
13287
});

0 commit comments

Comments
 (0)