Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Outlook integration toolkit #3465

Open
wants to merge 74 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
12dd2a9
outlook integration
hahahafafa Nov 14, 2023
3388d62
Update outlookIntegration.ts
SimonLi1020 Nov 15, 2023
2694daf
Merge pull request #1 from hahahafafa/develop
hahahafafa Nov 21, 2023
884bab7
get message tips integrated
hahahafafa Nov 22, 2023
929f9f8
added some tests and added getToken for getting Access token
oscarchen178 Nov 27, 2023
44928c1
move our integration to outlook folder, extract authentication to oth…
oscarchen178 Nov 29, 2023
ba52666
add get credentials from env, remove unused package
oscarchen178 Nov 29, 2023
106868d
create entrypoints for outlook, create read and send tool extending b…
oscarchen178 Nov 29, 2023
3b54b6f
Update index.ts
oscarchen178 Nov 29, 2023
5c259c7
reformat the read email
Qi123123Li Nov 30, 2023
3d123c5
now our outlook tools depend on abstract authFlowBase not concrete au…
oscarchen178 Nov 30, 2023
df52718
added two authentication flow, make outlook tool construct more simpl…
oscarchen178 Nov 30, 2023
5fc9972
change the code to fit standard lint rules
Qi123123Li Nov 30, 2023
43388c7
add test case for send email
Qi123123Li Nov 30, 2023
497e486
fix some small bugs
Qi123123Li Nov 30, 2023
e29c32d
Merge branch 'langchain-ai:main' into main
hahahafafa Nov 30, 2023
11d91fc
add invalid token test
oscarchen178 Nov 30, 2023
d19b446
Merge branch 'dev2'
hahahafafa Nov 30, 2023
7091118
Update .env.example
SimonLi1020 Nov 30, 2023
d48ab61
Merge branch 'main' into main
SimonLi1020 Nov 30, 2023
6234417
Merge branch 'main' into main
SimonLi1020 Nov 30, 2023
d38f9f6
use typedoc
Qi123123Li Dec 1, 2023
1bc7761
Merge branch 'main' into run-typedoc
Qi123123Li Dec 1, 2023
0ff7f6d
Merge pull request #4 from hahahafafa/run-typedoc
Qi123123Li Dec 1, 2023
12d160f
add http and url in package
Qi123123Li Dec 1, 2023
cf52d46
Merge branch 'run-typedoc'
Qi123123Li Dec 1, 2023
7c03901
Merge branch 'main' into main
Qi123123Li Dec 1, 2023
083a8ae
Merge branch 'main' into main
oscarchen178 Dec 4, 2023
d70da41
remove optional deps, remove modules using opt depd from index entryp…
oscarchen178 Dec 4, 2023
918ad47
Merge branch 'main' into main
oscarchen178 Dec 4, 2023
9a6ef37
Merge branch 'main' into main
oscarchen178 Dec 5, 2023
43ee152
add to create-entrypoints
oscarchen178 Dec 5, 2023
d4bf536
change from openurl to open package
oscarchen178 Dec 5, 2023
d21fc40
Merge branch 'main' into main
oscarchen178 Dec 5, 2023
960d082
remove url from dependencies
oscarchen178 Dec 6, 2023
f4861fe
log the url for manually open instead of using open package
oscarchen178 Dec 6, 2023
bdb51ab
Merge branch 'main' into main
oscarchen178 Dec 6, 2023
c518e9f
Update langchain/src/tools/outlook/authFlowREST.ts
Qi123123Li Dec 6, 2023
3212a3c
Update langchain/src/tools/outlook/descriptions.ts
Qi123123Li Dec 6, 2023
767acbc
Update langchain/src/tools/outlook/descriptions.ts
Qi123123Li Dec 6, 2023
216c3cc
Update langchain/src/tools/tests/outlookIntegration.test.ts
Qi123123Li Dec 6, 2023
c94886e
fix the format
Qi123123Li Dec 7, 2023
c3f6c74
add the jsdoc
Qi123123Li Dec 7, 2023
3cec11e
add jsdoc
Qi123123Li Dec 7, 2023
9b1d3f4
change OutlookBase to abstract; remove `state` in rest; apply camel c…
oscarchen178 Dec 10, 2023
d92e771
Merge branch 'main' into main
oscarchen178 Dec 10, 2023
6ebb241
Merge branch 'main' of https://github.com/hwchase17/langchainjs into …
jacoblee93 Dec 12, 2023
da61a07
Merge
jacoblee93 Dec 12, 2023
ed41abe
Skip test
jacoblee93 Dec 12, 2023
ba2ec31
Merge branch 'main' into main
oscarchen178 Dec 20, 2023
d9ca68d
Merge branch 'main' into main
oscarchen178 Dec 21, 2023
04a7221
Merge branch 'main' into main
oscarchen178 Dec 22, 2023
23271fd
Merge branch 'main' of https://github.com/hwchase17/langchainjs into …
jacoblee93 Dec 22, 2023
562df12
Move/rename files around
jacoblee93 Dec 22, 2023
a9ba549
add a docs page for outlook toolkit
oscarchen178 Dec 22, 2023
7fa8d85
Update outlook.mdx
jacoblee93 Dec 23, 2023
c2a9eba
Update outlook.mdx
oscarchen178 Dec 23, 2023
c7eb5c6
Update outlook.mdx
oscarchen178 Dec 23, 2023
6718669
add getRefreshToken for AuthFlowREST
oscarchen178 Dec 23, 2023
4cbdeb9
Merge branch 'main' into main
Qi123123Li Dec 26, 2023
b300654
Merge branch 'main' into main
oscarchen178 Dec 28, 2023
1fb53e0
move getAuth from outlook tool to authFLow
oscarchen178 Dec 29, 2023
4719a52
format
oscarchen178 Dec 29, 2023
cdde1d4
update docs
oscarchen178 Dec 29, 2023
ab49c28
Merge branch 'main' into main
oscarchen178 Dec 29, 2023
175324c
Merge branch 'main' into main
oscarchen178 Jan 4, 2024
8751341
Merge branch 'main' into main
oscarchen178 Jan 6, 2024
42189f8
format outlook.mdx
oscarchen178 Jan 6, 2024
f94225a
Merge branch 'main' into main
oscarchen178 Jan 6, 2024
d1e961e
putting example in the examples/
oscarchen178 Jan 18, 2024
569b34e
Update outlook.mdx
oscarchen178 Jan 19, 2024
981242e
Update outlook.ts
oscarchen178 Jan 19, 2024
1aa5ee3
Merge remote-tracking branch 'upstream/main'
oscarchen178 Jan 20, 2024
d03bd77
Merge branch 'main' into main
oscarchen178 Jan 23, 2024
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
1 change: 1 addition & 0 deletions docs/api_refs/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"./langchain/src/tools/webbrowser.ts",
"./langchain/src/tools/gmail/index.ts",
"./langchain/src/tools/google_calendar/index.ts",
"./langchain/src/tools/outlook/index.ts",
"./langchain/src/tools/google_places.ts",
"./langchain/src/chains/index.ts",
"./langchain/src/chains/combine_documents/reduce.ts",
Expand Down
5 changes: 5 additions & 0 deletions langchain/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ NEO4J_USERNAME=ADD_YOURS_HERE
NEO4J_PASSWORD=ADD_YOURS_HERE
CLOSEVECTOR_API_KEY=ADD_YOURS_HERE
CLOSEVECTOR_API_SECRET=ADD_YOURS_HERE
OUTLOOK_CLIENT_ID=ADD_YOURS_HERE
OUTLOOK_CLIENT_SECRET=ADD_YOURS_HERE
OUTLOOK_REDIRECT_URI=ADD_YOURS_HERE
OUTLOOK_REFRESH_TOKEN=ADD_YOURS_HERE
OUTLOOK_ACCESS_TOKEN=ADD_YOURS_HERE
GPLACES_API_KEY=ADD_YOURS_HERE
CONFLUENCE_USERNAME=ADD_YOURS_HERE
CONFLUENCE_PASSWORD=ADD_YOURS_HERE
Expand Down
3 changes: 3 additions & 0 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ tools/gmail.d.ts
tools/google_calendar.cjs
tools/google_calendar.js
tools/google_calendar.d.ts
tools/outlook.cjs
tools/outlook.js
tools/outlook.d.ts
tools/google_places.cjs
tools/google_places.js
tools/google_places.d.ts
Expand Down
8 changes: 8 additions & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
"tools/google_calendar.cjs",
"tools/google_calendar.js",
"tools/google_calendar.d.ts",
"tools/outlook.cjs",
"tools/outlook.js",
"tools/outlook.d.ts",
"tools/google_places.cjs",
"tools/google_places.js",
"tools/google_places.d.ts",
Expand Down Expand Up @@ -1603,6 +1606,11 @@
"import": "./tools/google_calendar.js",
"require": "./tools/google_calendar.cjs"
},
"./tools/outlook": {
"types": "./tools/outlook.d.ts",
"import": "./tools/outlook.js",
"require": "./tools/outlook.cjs"
},
"./tools/google_places": {
"types": "./tools/google_places.d.ts",
"import": "./tools/google_places.js",
Expand Down
2 changes: 2 additions & 0 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const entrypoints = {
"tools/webbrowser": "tools/webbrowser",
"tools/gmail": "tools/gmail/index",
"tools/google_calendar": "tools/google_calendar/index",
"tools/outlook": "tools/outlook/index",
"tools/google_places": "tools/google_places",
// chains
chains: "chains/index",
Expand Down Expand Up @@ -358,6 +359,7 @@ const requiresOptionalDependency = [
"tools/webbrowser",
"tools/google_calendar",
"tools/gmail",
"tools/outlook",
"callbacks/handlers/llmonitor",
"chains/load",
"chains/sql_db",
Expand Down
1 change: 1 addition & 0 deletions langchain/src/load/import_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const optionalImportEntrypoints = [
"langchain/tools/webbrowser",
"langchain/tools/gmail",
"langchain/tools/google_calendar",
"langchain/tools/outlook",
"langchain/chains/load",
"langchain/chains/query_constructor",
"langchain/chains/query_constructor/ir",
Expand Down
3 changes: 3 additions & 0 deletions langchain/src/load/import_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export interface OptionalImportMap {
"langchain/tools/google_calendar"?:
| typeof import("../tools/google_calendar/index.js")
| Promise<typeof import("../tools/google_calendar/index.js")>;
"langchain/tools/outlook"?:
| typeof import("../tools/outlook/index.js")
| Promise<typeof import("../tools/outlook/index.js")>;
"langchain/chains/load"?:
| typeof import("../chains/load.js")
| Promise<typeof import("../chains/load.js")>;
Expand Down
13 changes: 13 additions & 0 deletions langchain/src/tools/outlook/authFlowBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export abstract class AuthFlowBase {
protected clientId: string;

protected accessToken = "";

constructor(clientId: string) {
this.clientId = clientId;
}

public abstract getAccessToken(): Promise<string>;

public abstract refreshAccessToken(): Promise<string>;
}
205 changes: 205 additions & 0 deletions langchain/src/tools/outlook/authFlowREST.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import * as http from "http";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds a new HTTP request using the fetch function in the getAccessToken and refreshAccessToken methods. This comment is flagging the change for maintainers to review.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds code that explicitly accesses and reads environment variables via the getEnvironmentVariable function, which should be reviewed by maintainers to ensure proper handling and usage of environment variables.

import * as url from "url";
import { AuthFlowBase } from "./authFlowBase.js";
import { getEnvironmentVariable } from "../../util/env.js";

interface AccessTokenResponse {
access_token: string;
refresh_token: string;
}

// this class uses your clientId, clientSecret and redirectUri
// to get access token and refresh token, if you already have them,
// you can use AuthFlowToken or AuthFlowRefresh instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a JSDoc so devs & our api refs can get the description properly

export class AuthFlowREST extends AuthFlowBase {
private clientSecret: string;

private redirectUri: string;

private port: number;

private pathname: string;

private refreshToken = "";

constructor({
clientId,
clientSecret,
redirectUri,
}: { clientId?: string; clientSecret?: string; redirectUri?: string } = {}) {
let id = clientId;
let secret = clientSecret;
let uri = redirectUri;
if (!id || !secret || !uri) {
id = id ?? getEnvironmentVariable("OUTLOOK_CLIENT_ID");
secret = secret ?? getEnvironmentVariable("OUTLOOK_CLIENT_SECRET");
uri = uri ?? getEnvironmentVariable("OUTLOOK_REDIRECT_URI");
}
if (!id || !secret || !uri) {
throw new Error("Missing clientId, clientSecret or redirectUri.");
}
super(id);
this.clientSecret = secret;
this.redirectUri = uri;
const parsedUrl = new URL(this.redirectUri);
this.port = parsedUrl.port ? parseInt(parsedUrl.port, 10) : 3000;
this.pathname = parsedUrl.pathname || "";
}

// Function to open or print the login url for user to login
Copy link
Member

@bracesproul bracesproul Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, JSDoc
Please update the rest of the method comments too

private openAuthUrl(): string {
const loginEndpoint =
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
const { clientId } = this; // client ID regestered in Azure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do object destructuring here, since it's only used in one place it's clearer to just call this.clientId

const response_type = "code";
const response_mode = "query";
const redirectUri = encodeURIComponent(this.redirectUri); // redirect URI regestered in Azure
const scope = encodeURIComponent(
"openid offline_access https://graph.microsoft.com/.default"
);
const state = "12345";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checking this is the correct value?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found that this is optional so I will remove this


const url = [
`${loginEndpoint}?client_id=${clientId}`,
`&response_type=${response_type}`,
`&response_mode=${response_mode}`,
`&redirect_uri=${redirectUri}`,
`&scope=${scope}`,
`&state=${state}`,
].join("");

console.log("Please open the following url to login:");
console.log(url);
return url;
}

// Function to start the server
private startServer(): Promise<http.Server> {
return new Promise((resolve, reject) => {
const server = http.createServer();

server.listen(this.port, () => {
console.log(`Server listening at http://localhost:${this.port}`);
resolve(server);
});

server.on("error", (err) => {
reject(err);
});
});
}

// Function to listen for the authorization code
private async listenForCode(server: http.Server): Promise<string> {
return new Promise((resolve, reject) => {
// Set timeout in case the user fails to login
const timeout = setTimeout(() => {
server.close();
reject(new Error("Timeout"));
}, 180000);

server.on("request", (req, res) => {
try {
const reqUrl = url.parse(req.url || "", true);

if (reqUrl.pathname === this.pathname) {
const authCode = reqUrl.query.code as string;

res.writeHead(200, { "Content-Type": "text/html" });
res.end("Authorization code received. You can close this window.");

clearTimeout(timeout);
server.close();
console.log("Server closed");
resolve(authCode); // Resolve the Promise with the authorization code
} else {
res.writeHead(404);
res.end("404 Not Found");
}
} catch (err) {
clearTimeout(timeout);
res.writeHead(500);
res.end("Server error");
reject(err);
}
});
});
}

// get authentication code from user login
private async getCode(): Promise<string> {
// check credentials
if (!this.clientId || !this.redirectUri) {
throw new Error("Missing clientId or redirectUri.");
}

const server = await this.startServer();
this.openAuthUrl();
const code = await this.listenForCode(server);
return code;
}

// Function to get the token using the code and client credentials
public async getAccessToken(): Promise<string> {
// fetch auth code from user login
const code = await this.getCode();
// fetch access token using auth code
const req_body =
`client_id=${encodeURIComponent(this.clientId)}&` +
`client_secret=${encodeURIComponent(this.clientSecret)}&` +
`scope=${encodeURIComponent("https://graph.microsoft.com/.default")}&` +
`redirect_uri=${encodeURIComponent(this.redirectUri)}&` +
`grant_type=authorization_code&` +
`code=${encodeURIComponent(code)}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be cleaner to use something like URLSearchParams


const response = await fetch(
"https://login.microsoftonline.com/common/oauth2/v2.0/token",
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: req_body,
}
);

if (!response.ok) {
throw new Error(`fetch token error! response: ${response.status}`);
}
// save access token and refresh token
const json = (await response.json()) as AccessTokenResponse;
this.accessToken = json.access_token;
this.refreshToken = json.refresh_token;
return this.accessToken;
}

public async refreshAccessToken(): Promise<string> {
// fetch new access token using refresh token
const req_body =
`client_id=${encodeURIComponent(this.clientId)}&` +
`client_secret=${encodeURIComponent(this.clientSecret)}&` +
`scope=${encodeURIComponent("https://graph.microsoft.com/.default")}&` +
`redirect_uri=${encodeURIComponent(this.redirectUri)}&` +
`grant_type=refresh_token&` +
`refresh_token=${encodeURIComponent(this.refreshToken)}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same search params comment here


const response = await fetch(
"https://login.microsoftonline.com/common/oauth2/v2.0/token",
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: req_body,
}
);

if (!response.ok) {
throw new Error(`fetch token error! response: ${response.status}`);
}
// save new access token
const json = (await response.json()) as AccessTokenResponse;
this.accessToken = json.access_token;
return this.accessToken;
}
}
Loading
Loading