Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bold-papers-stop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@plotday/sdk": minor
---

Changed: BREAKING: Add agent id to Agent and Tool constructors
4 changes: 2 additions & 2 deletions agents/chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default class extends Agent {
private ai: AI;
private plot: Plot;

constructor(protected tools: Tools) {
super(tools);
constructor(id: string, protected tools: Tools) {
super(id, tools);
this.ai = tools.get(AI);
this.plot = tools.get(Plot);
}
Expand Down
38 changes: 20 additions & 18 deletions agents/events/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import {
ActivityType,
Agent,
type Priority,
Tools,
type Tools,
} from "@plotday/sdk";
import type {
Calendar,
CalendarAuth,
CalendarTool,
SyncOptions,
} from "@plotday/sdk/common/calendar";
import { Plot } from "@plotday/sdk/tools/plot";
import { GoogleCalendar } from "@plotday/tool-google-calendar";
import { OutlookCalendar } from "@plotday/tool-outlook-calendar";
import { Plot } from "@plotday/sdk/tools/plot";

type CalendarProvider = "google" | "outlook";

Expand All @@ -36,8 +36,8 @@ export default class extends Agent {
private outlookCalendar: OutlookCalendar;
private plot: Plot;

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

private async addStoredAuth(
provider: CalendarProvider,
authToken: string,
authToken: string
): Promise<void> {
const auths = await this.getStoredAuths();
const existingIndex = auths.findIndex((auth) => auth.provider === provider);
Expand All @@ -76,7 +76,7 @@ export default class extends Agent {
}

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

// Get auth links from both calendar tools
const googleAuthLink =
await this.googleCalendar.requestAuth(googleCallback);
const outlookAuthLink =
await this.outlookCalendar.requestAuth(outlookCallback);
const googleAuthLink = await this.googleCalendar.requestAuth(
googleCallback
);
const outlookAuthLink = await this.outlookCalendar.requestAuth(
outlookCallback
);

// Create activity with both auth links
const connectActivity = await this.plot.createActivity({
Expand Down Expand Up @@ -129,7 +131,7 @@ export default class extends Agent {
async startSync(
provider: CalendarProvider,
calendarId: string,
options?: SyncOptions,
options?: SyncOptions
): Promise<void> {
const authToken = await this.getAuthToken(provider);
if (!authToken) {
Expand All @@ -149,7 +151,7 @@ export default class extends Agent {

async stopSync(
provider: CalendarProvider,
calendarId: string,
calendarId: string
): Promise<void> {
const authToken = await this.getAuthToken(provider);
if (!authToken) {
Expand Down Expand Up @@ -211,7 +213,7 @@ export default class extends Agent {
await this.createCalendarSelectionActivity(
provider,
calendars,
authResult.authToken,
authResult.authToken
);
} catch (error) {
console.error(`Failed to fetch calendars for ${provider}:`, error);
Expand All @@ -221,7 +223,7 @@ export default class extends Agent {
private async createCalendarSelectionActivity(
provider: CalendarProvider,
calendars: Calendar[],
authToken: string,
authToken: string
): Promise<void> {
const links: ActivityLink[] = [];

Expand Down Expand Up @@ -261,7 +263,7 @@ export default class extends Agent {

async onCalendarSelected(
_link: ActivityLink,
context: CalendarSelectionContext,
context: CalendarSelectionContext
): Promise<void> {
console.log("Calendar selectedwith context:", context);
if (!context) {
Expand All @@ -282,11 +284,11 @@ export default class extends Agent {
await tool.startSync(
context.authToken,
context.calendarId,
eventCallback,
eventCallback
);

console.log(
`Started syncing ${context.provider} calendar: ${context.calendarName}`,
`Started syncing ${context.provider} calendar: ${context.calendarName}`
);

await this.plot.createActivity({
Expand All @@ -297,7 +299,7 @@ export default class extends Agent {
} catch (error) {
console.error(
`Failed to start sync for calendar ${context.calendarName}:`,
error,
error
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import { Plot } from "@plotday/sdk/tools/plot";
export default class extends Agent {
private plot: Plot;

constructor(tools: Tools) {
super(tools);
constructor(id: string, tools: Tools) {
super(id, tools);
this.plot = tools.get(Plot);
}

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

```typescript
constructor(tools: Tools) {
super(tools);
constructor(id: string, tools: Tools) {
super(id, tools);
this.plot = tools.get(Plot);
this.googleCalendar = tools.get(GoogleCalendar);
// Store, Run, and Callback methods are available directly:
Expand Down
8 changes: 4 additions & 4 deletions sdk/cli/templates/AGENTS.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { Plot } from "@plotday/sdk/tools/plot";
export default class MyAgent extends Agent {
private plot: Plot;

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

```typescript
constructor(protected tools: Tools) {
super();
constructor(id: string, protected tools: Tools) {
super(id, tools);
this.toolName = tools.get(ToolClass);
}
```
Expand Down
18 changes: 11 additions & 7 deletions sdk/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type {
* class FlatteringAgent extends Agent<FlatteringAgent> {
* private plot: Plot;
*
* constructor(tools: Tools) {
* super(tools);
* constructor(id: string, tools: Tools) {
* super(id, tools);
* this.plot = tools.get(Plot);
* }
*
Expand All @@ -38,9 +38,11 @@ import type {
* ```
*/
export abstract class Agent<TSelf = any> {
protected id: string;
protected tools: Tools;

constructor(tools: Tools) {
constructor(id: string, tools: Tools) {
this.id = id;
this.tools = tools;
}

Expand Down Expand Up @@ -229,7 +231,7 @@ export abstract class Agent<TSelf = any> {
*/
export abstract class ITool {}

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

/**
* Base class for regular tools.
Expand All @@ -241,8 +243,8 @@ export type ToolConstructor<T extends ITool> = (abstract new (tools: Tools) => T
* @example
* ```typescript
* class GoogleCalendarTool extends Tool<GoogleCalendarTool> {
* constructor(tools: Tools) {
* super(tools);
* constructor(id: string, tools: Tools) {
* super(id, tools);
* this.auth = tools.get(Auth);
* }
*
Expand All @@ -253,9 +255,11 @@ export type ToolConstructor<T extends ITool> = (abstract new (tools: Tools) => T
* ```
*/
export abstract class Tool<TSelf = any> implements ITool {
protected id: string;
protected tools: Tools;

constructor(tools: Tools) {
constructor(id: string, tools: Tools) {
this.id = id;
this.tools = tools;
}

Expand Down
4 changes: 2 additions & 2 deletions tools/google-calendar/src/google-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export class GoogleCalendar extends Tool implements CalendarTool {
private auth: Auth;
private webhook: Webhook;

constructor(protected tools: Tools) {
super(tools);
constructor(id: string, protected tools: Tools) {
super(id, tools);
this.auth = tools.get(Auth);
this.webhook = tools.get(Webhook);
}
Expand Down
4 changes: 2 additions & 2 deletions tools/google-contacts/src/google-contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ export default class extends Tool implements GoogleContacts {

private auth: Auth;

constructor(protected tools: Tools) {
super(tools);
constructor(id: string, protected tools: Tools) {
super(id, tools);
this.auth = tools.get(Auth);
}

Expand Down
4 changes: 2 additions & 2 deletions tools/outlook-calendar/src/outlook-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ export class OutlookCalendar extends Tool implements CalendarTool {
private auth: Auth;
private webhook: Webhook;

constructor(protected tools: Tools) {
super(tools);
constructor(id: string, protected tools: Tools) {
super(id, tools);
this.auth = tools.get(Auth);
this.webhook = tools.get(Webhook);
}
Expand Down