-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
base.ts
75 lines (64 loc) · 1.86 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
import { gmail_v1, google } from "googleapis";
import { z } from "zod";
import { StructuredTool } from "@langchain/core/tools";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
export interface GmailBaseToolParams {
credentials?: {
clientEmail?: string;
privateKey?: string;
keyfile?: string;
};
scopes?: string[];
}
export abstract class GmailBaseTool extends StructuredTool {
private CredentialsSchema = z
.object({
clientEmail: z
.string()
.min(1)
.default(getEnvironmentVariable("GMAIL_CLIENT_EMAIL") ?? ""),
privateKey: z
.string()
.default(getEnvironmentVariable("GMAIL_PRIVATE_KEY") ?? ""),
keyfile: z
.string()
.default(getEnvironmentVariable("GMAIL_KEYFILE") ?? ""),
})
.refine(
(credentials) =>
credentials.privateKey !== "" || credentials.keyfile !== "",
{
message:
"Missing GMAIL_PRIVATE_KEY or GMAIL_KEYFILE to interact with Gmail",
}
);
private GmailBaseToolParamsSchema = z
.object({
credentials: this.CredentialsSchema.default({}),
scopes: z.array(z.string()).default(["https://mail.google.com/"]),
})
.default({});
name = "Gmail";
description = "A tool to send and view emails through Gmail";
protected gmail: gmail_v1.Gmail;
constructor(fields?: Partial<GmailBaseToolParams>) {
super(...arguments);
const { credentials, scopes } =
this.GmailBaseToolParamsSchema.parse(fields);
this.gmail = this.getGmail(
scopes,
credentials.clientEmail,
credentials.privateKey,
credentials.keyfile
);
}
private getGmail(
scopes: string[],
email: string,
key?: string,
keyfile?: string
) {
const auth = new google.auth.JWT(email, keyfile, key, scopes);
return google.gmail({ version: "v1", auth });
}
}