-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
base.ts
98 lines (79 loc) · 2.38 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { google } from "googleapis";
import { Tool } from "@langchain/core/tools";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { BaseLLM } from "@langchain/core/language_models/llms";
export interface GoogleCalendarAgentParams {
credentials?: {
clientEmail?: string;
privateKey?: string;
calendarId?: string;
};
scopes?: string[];
model?: BaseLLM;
}
export class GoogleCalendarBase extends Tool {
name = "Google Calendar";
description =
"A tool to lookup Google Calendar events and create events in Google Calendar";
protected clientEmail: string;
protected privateKey: string;
protected calendarId: string;
protected scopes: string[];
protected llm: BaseLLM;
constructor(
fields: GoogleCalendarAgentParams = {
credentials: {
clientEmail: getEnvironmentVariable("GOOGLE_CALENDAR_CLIENT_EMAIL"),
privateKey: getEnvironmentVariable("GOOGLE_CALENDAR_PRIVATE_KEY"),
calendarId: getEnvironmentVariable("GOOGLE_CALENDAR_CALENDAR_ID"),
},
scopes: [
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/calendar.events",
],
}
) {
super(...arguments);
if (!fields.model) {
throw new Error("Missing llm instance to interact with Google Calendar");
}
if (!fields.credentials) {
throw new Error("Missing credentials to authenticate to Google Calendar");
}
if (!fields.credentials.clientEmail) {
throw new Error(
"Missing GOOGLE_CALENDAR_CLIENT_EMAIL to interact with Google Calendar"
);
}
if (!fields.credentials.privateKey) {
throw new Error(
"Missing GOOGLE_CALENDAR_PRIVATE_KEY to interact with Google Calendar"
);
}
if (!fields.credentials.calendarId) {
throw new Error(
"Missing GOOGLE_CALENDAR_CALENDAR_ID to interact with Google Calendar"
);
}
this.clientEmail = fields.credentials.clientEmail;
this.privateKey = fields.credentials.privateKey;
this.calendarId = fields.credentials.calendarId;
this.scopes = fields.scopes || [];
this.llm = fields.model;
}
getModel() {
return this.llm;
}
async getAuth() {
const auth = new google.auth.JWT(
this.clientEmail,
undefined,
this.privateKey,
this.scopes
);
return auth;
}
async _call(input: string) {
return input;
}
}