agsamantha/node_modules/@langchain/community/dist/tools/gmail/base.js
2024-10-02 15:15:21 -05:00

68 lines
2.6 KiB
JavaScript

import { google } from "googleapis";
import { z } from "zod";
import { StructuredTool } from "@langchain/core/tools";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
export class GmailBaseTool extends StructuredTool {
constructor(fields) {
super(...arguments);
Object.defineProperty(this, "CredentialsSchema", {
enumerable: true,
configurable: true,
writable: true,
value: 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") ?? ""),
subject: z
.string()
.default(getEnvironmentVariable("GMAIL_SUBJECT") ?? ""),
})
.refine((credentials) => credentials.privateKey !== "" || credentials.keyfile !== "", {
message: "Missing GMAIL_PRIVATE_KEY or GMAIL_KEYFILE to interact with Gmail",
})
});
Object.defineProperty(this, "GmailBaseToolParamsSchema", {
enumerable: true,
configurable: true,
writable: true,
value: z
.object({
credentials: this.CredentialsSchema.default({}),
scopes: z.array(z.string()).default(["https://mail.google.com/"]),
})
.default({})
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: "Gmail"
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: "A tool to send and view emails through Gmail"
});
Object.defineProperty(this, "gmail", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
const { credentials, scopes } = this.GmailBaseToolParamsSchema.parse(fields);
this.gmail = this.getGmail(scopes, credentials.clientEmail, credentials.privateKey, credentials.keyfile, credentials.subject);
}
getGmail(scopes, email, key, keyfile, subject) {
const auth = new google.auth.JWT(email, keyfile, key, scopes, subject);
return google.gmail({ version: "v1", auth });
}
}