Skip to content

Commit 56cdf27

Browse files
authored
Merge pull request #22 from plotday/change/add-id-to-agent
Add id to Agents and Tools
2 parents ea6dbd6 + 34e7e43 commit 56cdf27

9 files changed

Lines changed: 52 additions & 41 deletions

File tree

.changeset/bold-papers-stop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@plotday/sdk": minor
3+
---
4+
5+
Changed: BREAKING: Add agent id to Agent and Tool constructors

agents/chat/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export default class extends Agent {
1515
private ai: AI;
1616
private plot: Plot;
1717

18-
constructor(protected tools: Tools) {
19-
super(tools);
18+
constructor(id: string, protected tools: Tools) {
19+
super(id, tools);
2020
this.ai = tools.get(AI);
2121
this.plot = tools.get(Plot);
2222
}

agents/events/src/index.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import {
55
ActivityType,
66
Agent,
77
type Priority,
8-
Tools,
8+
type Tools,
99
} from "@plotday/sdk";
1010
import type {
1111
Calendar,
1212
CalendarAuth,
1313
CalendarTool,
1414
SyncOptions,
1515
} from "@plotday/sdk/common/calendar";
16+
import { Plot } from "@plotday/sdk/tools/plot";
1617
import { GoogleCalendar } from "@plotday/tool-google-calendar";
1718
import { OutlookCalendar } from "@plotday/tool-outlook-calendar";
18-
import { Plot } from "@plotday/sdk/tools/plot";
1919

2020
type CalendarProvider = "google" | "outlook";
2121

@@ -36,8 +36,8 @@ export default class extends Agent {
3636
private outlookCalendar: OutlookCalendar;
3737
private plot: Plot;
3838

39-
constructor(protected tools: Tools) {
40-
super(tools);
39+
constructor(id: string, protected tools: Tools) {
40+
super(id, tools);
4141
this.googleCalendar = tools.get(GoogleCalendar);
4242
this.outlookCalendar = tools.get(OutlookCalendar);
4343
this.plot = tools.get(Plot);
@@ -61,7 +61,7 @@ export default class extends Agent {
6161

6262
private async addStoredAuth(
6363
provider: CalendarProvider,
64-
authToken: string,
64+
authToken: string
6565
): Promise<void> {
6666
const auths = await this.getStoredAuths();
6767
const existingIndex = auths.findIndex((auth) => auth.provider === provider);
@@ -76,7 +76,7 @@ export default class extends Agent {
7676
}
7777

7878
private async getAuthToken(
79-
provider: CalendarProvider,
79+
provider: CalendarProvider
8080
): Promise<string | null> {
8181
const auths = await this.getStoredAuths();
8282
const auth = auths.find((auth) => auth.provider === provider);
@@ -98,10 +98,12 @@ export default class extends Agent {
9898
});
9999

100100
// Get auth links from both calendar tools
101-
const googleAuthLink =
102-
await this.googleCalendar.requestAuth(googleCallback);
103-
const outlookAuthLink =
104-
await this.outlookCalendar.requestAuth(outlookCallback);
101+
const googleAuthLink = await this.googleCalendar.requestAuth(
102+
googleCallback
103+
);
104+
const outlookAuthLink = await this.outlookCalendar.requestAuth(
105+
outlookCallback
106+
);
105107

106108
// Create activity with both auth links
107109
const connectActivity = await this.plot.createActivity({
@@ -129,7 +131,7 @@ export default class extends Agent {
129131
async startSync(
130132
provider: CalendarProvider,
131133
calendarId: string,
132-
options?: SyncOptions,
134+
options?: SyncOptions
133135
): Promise<void> {
134136
const authToken = await this.getAuthToken(provider);
135137
if (!authToken) {
@@ -149,7 +151,7 @@ export default class extends Agent {
149151

150152
async stopSync(
151153
provider: CalendarProvider,
152-
calendarId: string,
154+
calendarId: string
153155
): Promise<void> {
154156
const authToken = await this.getAuthToken(provider);
155157
if (!authToken) {
@@ -211,7 +213,7 @@ export default class extends Agent {
211213
await this.createCalendarSelectionActivity(
212214
provider,
213215
calendars,
214-
authResult.authToken,
216+
authResult.authToken
215217
);
216218
} catch (error) {
217219
console.error(`Failed to fetch calendars for ${provider}:`, error);
@@ -221,7 +223,7 @@ export default class extends Agent {
221223
private async createCalendarSelectionActivity(
222224
provider: CalendarProvider,
223225
calendars: Calendar[],
224-
authToken: string,
226+
authToken: string
225227
): Promise<void> {
226228
const links: ActivityLink[] = [];
227229

@@ -261,7 +263,7 @@ export default class extends Agent {
261263

262264
async onCalendarSelected(
263265
_link: ActivityLink,
264-
context: CalendarSelectionContext,
266+
context: CalendarSelectionContext
265267
): Promise<void> {
266268
console.log("Calendar selectedwith context:", context);
267269
if (!context) {
@@ -282,11 +284,11 @@ export default class extends Agent {
282284
await tool.startSync(
283285
context.authToken,
284286
context.calendarId,
285-
eventCallback,
287+
eventCallback
286288
);
287289

288290
console.log(
289-
`Started syncing ${context.provider} calendar: ${context.calendarName}`,
291+
`Started syncing ${context.provider} calendar: ${context.calendarName}`
290292
);
291293

292294
await this.plot.createActivity({
@@ -297,7 +299,7 @@ export default class extends Agent {
297299
} catch (error) {
298300
console.error(
299301
`Failed to start sync for calendar ${context.calendarName}:`,
300-
error,
302+
error
301303
);
302304
}
303305
}

sdk/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import { Plot } from "@plotday/sdk/tools/plot";
3232
export default class extends Agent {
3333
private plot: Plot;
3434

35-
constructor(tools: Tools) {
36-
super(tools);
35+
constructor(id: string, tools: Tools) {
36+
super(id, tools);
3737
this.plot = tools.get(Plot);
3838
}
3939

@@ -100,8 +100,8 @@ Tools provide functionality to agents. They can be:
100100
Access tools via the `tools.get()` method in your agent constructor. Store, Run, and Callback methods are available directly on the Agent class:
101101

102102
```typescript
103-
constructor(tools: Tools) {
104-
super(tools);
103+
constructor(id: string, tools: Tools) {
104+
super(id, tools);
105105
this.plot = tools.get(Plot);
106106
this.googleCalendar = tools.get(GoogleCalendar);
107107
// Store, Run, and Callback methods are available directly:

sdk/cli/templates/AGENTS.template.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import { Plot } from "@plotday/sdk/tools/plot";
2626
export default class MyAgent extends Agent {
2727
private plot: Plot;
2828

29-
constructor(protected tools: Tools) {
30-
super(tools);
29+
constructor(id: string, protected tools: Tools) {
30+
super(id, tools);
3131
this.plot = tools.get(Plot);
3232
// Store, Run, and Callback methods are available directly via this
3333
}
@@ -51,8 +51,8 @@ export default class MyAgent extends Agent {
5151
All tools are accessed through the `tools` parameter in the constructor:
5252

5353
```typescript
54-
constructor(protected tools: Tools) {
55-
super();
54+
constructor(id: string, protected tools: Tools) {
55+
super(id, tools);
5656
this.toolName = tools.get(ToolClass);
5757
}
5858
```

sdk/src/agent.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import type {
1818
* class FlatteringAgent extends Agent<FlatteringAgent> {
1919
* private plot: Plot;
2020
*
21-
* constructor(tools: Tools) {
22-
* super(tools);
21+
* constructor(id: string, tools: Tools) {
22+
* super(id, tools);
2323
* this.plot = tools.get(Plot);
2424
* }
2525
*
@@ -38,9 +38,11 @@ import type {
3838
* ```
3939
*/
4040
export abstract class Agent<TSelf = any> {
41+
protected id: string;
4142
protected tools: Tools;
4243

43-
constructor(tools: Tools) {
44+
constructor(id: string, tools: Tools) {
45+
this.id = id;
4446
this.tools = tools;
4547
}
4648

@@ -229,7 +231,7 @@ export abstract class Agent<TSelf = any> {
229231
*/
230232
export abstract class ITool {}
231233

232-
export type ToolConstructor<T extends ITool> = (abstract new (tools: Tools) => T) | (new (tools: Tools) => T);
234+
export type ToolConstructor<T extends ITool> = (abstract new (id: string, tools: Tools) => T) | (new (id: string, tools: Tools) => T);
233235

234236
/**
235237
* Base class for regular tools.
@@ -241,8 +243,8 @@ export type ToolConstructor<T extends ITool> = (abstract new (tools: Tools) => T
241243
* @example
242244
* ```typescript
243245
* class GoogleCalendarTool extends Tool<GoogleCalendarTool> {
244-
* constructor(tools: Tools) {
245-
* super(tools);
246+
* constructor(id: string, tools: Tools) {
247+
* super(id, tools);
246248
* this.auth = tools.get(Auth);
247249
* }
248250
*
@@ -253,9 +255,11 @@ export type ToolConstructor<T extends ITool> = (abstract new (tools: Tools) => T
253255
* ```
254256
*/
255257
export abstract class Tool<TSelf = any> implements ITool {
258+
protected id: string;
256259
protected tools: Tools;
257260

258-
constructor(tools: Tools) {
261+
constructor(id: string, tools: Tools) {
262+
this.id = id;
259263
this.tools = tools;
260264
}
261265

tools/google-calendar/src/google-calendar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export class GoogleCalendar extends Tool implements CalendarTool {
104104
private auth: Auth;
105105
private webhook: Webhook;
106106

107-
constructor(protected tools: Tools) {
108-
super(tools);
107+
constructor(id: string, protected tools: Tools) {
108+
super(id, tools);
109109
this.auth = tools.get(Auth);
110110
this.webhook = tools.get(Webhook);
111111
}

tools/google-contacts/src/google-contacts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ export default class extends Tool implements GoogleContacts {
248248

249249
private auth: Auth;
250250

251-
constructor(protected tools: Tools) {
252-
super(tools);
251+
constructor(id: string, protected tools: Tools) {
252+
super(id, tools);
253253
this.auth = tools.get(Auth);
254254
}
255255

tools/outlook-calendar/src/outlook-calendar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ export class OutlookCalendar extends Tool implements CalendarTool {
228228
private auth: Auth;
229229
private webhook: Webhook;
230230

231-
constructor(protected tools: Tools) {
232-
super(tools);
231+
constructor(id: string, protected tools: Tools) {
232+
super(id, tools);
233233
this.auth = tools.get(Auth);
234234
this.webhook = tools.get(Webhook);
235235
}

0 commit comments

Comments
 (0)