langchain 0.0.132 → 0.0.133
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/chat/outputParser.cjs +2 -1
- package/dist/agents/chat/outputParser.js +2 -1
- package/dist/agents/executor.cjs +106 -7
- package/dist/agents/executor.d.ts +23 -0
- package/dist/agents/executor.js +104 -6
- package/dist/agents/mrkl/outputParser.cjs +2 -1
- package/dist/agents/mrkl/outputParser.js +2 -1
- package/dist/chat_models/googlevertexai.cjs +1 -1
- package/dist/chat_models/googlevertexai.d.ts +2 -2
- package/dist/chat_models/googlevertexai.js +2 -2
- package/dist/chat_models/ollama.cjs +8 -8
- package/dist/chat_models/ollama.js +8 -8
- package/dist/document_loaders/web/notionapi.cjs +153 -74
- package/dist/document_loaders/web/notionapi.d.ts +19 -10
- package/dist/document_loaders/web/notionapi.js +154 -75
- package/dist/embeddings/googlevertexai.cjs +1 -1
- package/dist/embeddings/googlevertexai.d.ts +2 -2
- package/dist/embeddings/googlevertexai.js +2 -2
- package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +1 -1
- package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -2
- package/dist/experimental/multimodal_embeddings/googlevertexai.js +2 -2
- package/dist/llms/googlevertexai.cjs +1 -1
- package/dist/llms/googlevertexai.js +2 -2
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/schema/output_parser.cjs +2 -2
- package/dist/schema/output_parser.js +2 -2
- package/dist/tools/base.cjs +26 -2
- package/dist/tools/base.d.ts +9 -0
- package/dist/tools/base.js +24 -1
- package/dist/types/googlevertexai-types.d.ts +8 -3
- package/dist/util/googlevertexai-connection.cjs +49 -15
- package/dist/util/googlevertexai-connection.d.ts +12 -4
- package/dist/util/googlevertexai-connection.js +46 -13
- package/dist/vectorstores/googlevertexai.cjs +550 -0
- package/dist/vectorstores/googlevertexai.d.ts +180 -0
- package/dist/vectorstores/googlevertexai.js +519 -0
- package/dist/vectorstores/vectara.cjs +11 -2
- package/dist/vectorstores/vectara.d.ts +10 -1
- package/dist/vectorstores/vectara.js +11 -2
- package/package.json +10 -2
- package/vectorstores/googlevertexai.cjs +1 -0
- package/vectorstores/googlevertexai.d.ts +1 -0
- package/vectorstores/googlevertexai.js +1 -0
|
@@ -4,8 +4,22 @@ exports.NotionAPILoader = void 0;
|
|
|
4
4
|
const client_1 = require("@notionhq/client");
|
|
5
5
|
const notion_to_md_1 = require("notion-to-md");
|
|
6
6
|
const notion_js_1 = require("notion-to-md/build/utils/notion.js");
|
|
7
|
-
const base_js_1 = require("../base.cjs");
|
|
8
7
|
const document_js_1 = require("../../document.cjs");
|
|
8
|
+
const base_js_1 = require("../base.cjs");
|
|
9
|
+
const async_caller_js_1 = require("../../util/async_caller.cjs");
|
|
10
|
+
const isPageResponse = (res) => !(0, client_1.isNotionClientError)(res) && res.object === "page";
|
|
11
|
+
const isDatabaseResponse = (res) => !(0, client_1.isNotionClientError)(res) && res.object === "database";
|
|
12
|
+
const isErrorResponse = (res) => (0, client_1.isNotionClientError)(res);
|
|
13
|
+
const isPage = (res) => isPageResponse(res) && (0, client_1.isFullPage)(res);
|
|
14
|
+
const isDatabase = (res) => isDatabaseResponse(res) && (0, client_1.isFullDatabase)(res);
|
|
15
|
+
const getTitle = (obj) => {
|
|
16
|
+
if (isPage(obj) && obj.properties.title.type === "title") {
|
|
17
|
+
return obj.properties.title.title[0]?.plain_text;
|
|
18
|
+
}
|
|
19
|
+
if (isDatabase(obj))
|
|
20
|
+
return obj.title[0]?.plain_text;
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
9
23
|
/**
|
|
10
24
|
* A class that extends the BaseDocumentLoader class. It represents a
|
|
11
25
|
* document loader for loading documents from Notion using the Notion API.
|
|
@@ -13,6 +27,12 @@ const document_js_1 = require("../../document.cjs");
|
|
|
13
27
|
class NotionAPILoader extends base_js_1.BaseDocumentLoader {
|
|
14
28
|
constructor(options) {
|
|
15
29
|
super();
|
|
30
|
+
Object.defineProperty(this, "caller", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: void 0
|
|
35
|
+
});
|
|
16
36
|
Object.defineProperty(this, "notionClient", {
|
|
17
37
|
enumerable: true,
|
|
18
38
|
configurable: true,
|
|
@@ -31,19 +51,66 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
|
|
|
31
51
|
writable: true,
|
|
32
52
|
value: void 0
|
|
33
53
|
});
|
|
34
|
-
Object.defineProperty(this, "
|
|
54
|
+
Object.defineProperty(this, "pageQueue", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
value: void 0
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(this, "pageCompleted", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
configurable: true,
|
|
63
|
+
writable: true,
|
|
64
|
+
value: void 0
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(this, "pageQueueTotal", {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
configurable: true,
|
|
69
|
+
writable: true,
|
|
70
|
+
value: void 0
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(this, "documents", {
|
|
35
73
|
enumerable: true,
|
|
36
74
|
configurable: true,
|
|
37
75
|
writable: true,
|
|
38
76
|
value: void 0
|
|
39
77
|
});
|
|
40
|
-
this
|
|
78
|
+
Object.defineProperty(this, "rootTitle", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
configurable: true,
|
|
81
|
+
writable: true,
|
|
82
|
+
value: void 0
|
|
83
|
+
});
|
|
84
|
+
Object.defineProperty(this, "onDocumentLoaded", {
|
|
85
|
+
enumerable: true,
|
|
86
|
+
configurable: true,
|
|
87
|
+
writable: true,
|
|
88
|
+
value: void 0
|
|
89
|
+
});
|
|
90
|
+
this.caller = new async_caller_js_1.AsyncCaller({
|
|
91
|
+
maxConcurrency: 64,
|
|
92
|
+
...options.callerOptions,
|
|
93
|
+
});
|
|
94
|
+
this.notionClient = new client_1.Client({
|
|
95
|
+
logger: () => { },
|
|
96
|
+
...options.clientOptions,
|
|
97
|
+
});
|
|
41
98
|
this.n2mClient = new notion_to_md_1.NotionToMarkdown({
|
|
42
99
|
notionClient: this.notionClient,
|
|
43
100
|
config: { parseChildPages: false, convertImagesToBase64: false },
|
|
44
101
|
});
|
|
45
102
|
this.id = options.id;
|
|
46
|
-
this.
|
|
103
|
+
this.pageQueue = [];
|
|
104
|
+
this.pageCompleted = [];
|
|
105
|
+
this.pageQueueTotal = 0;
|
|
106
|
+
this.documents = [];
|
|
107
|
+
this.rootTitle = "";
|
|
108
|
+
this.onDocumentLoaded = options.onDocumentLoaded ?? ((_ti, _cu) => { });
|
|
109
|
+
}
|
|
110
|
+
addToQueue(...items) {
|
|
111
|
+
const deDuped = items.filter((item) => !this.pageCompleted.concat(this.pageQueue).includes(item));
|
|
112
|
+
this.pageQueue.push(...deDuped);
|
|
113
|
+
this.pageQueueTotal += deDuped.length;
|
|
47
114
|
}
|
|
48
115
|
/**
|
|
49
116
|
* Parses the properties of a Notion page and returns them as key-value
|
|
@@ -126,124 +193,136 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
|
|
|
126
193
|
* @returns A Promise that resolves to an MdBlock object.
|
|
127
194
|
*/
|
|
128
195
|
async loadBlock(block) {
|
|
129
|
-
|
|
196
|
+
const mdBlock = {
|
|
130
197
|
type: block.type,
|
|
131
198
|
blockId: block.id,
|
|
132
|
-
parent: await this.n2mClient.blockToMarkdown(block),
|
|
199
|
+
parent: await this.caller.call(() => this.n2mClient.blockToMarkdown(block)),
|
|
133
200
|
children: [],
|
|
134
201
|
};
|
|
202
|
+
if (block.has_children) {
|
|
203
|
+
const block_id = block.type === "synced_block" &&
|
|
204
|
+
block.synced_block?.synced_from?.block_id
|
|
205
|
+
? block.synced_block.synced_from.block_id
|
|
206
|
+
: block.id;
|
|
207
|
+
const childBlocks = await this.loadBlocks(await this.caller.call(() => (0, notion_js_1.getBlockChildren)(this.notionClient, block_id, null)));
|
|
208
|
+
mdBlock.children = childBlocks;
|
|
209
|
+
}
|
|
210
|
+
return mdBlock;
|
|
135
211
|
}
|
|
136
212
|
/**
|
|
137
|
-
* Loads Notion blocks and their
|
|
213
|
+
* Loads Notion blocks and their children recursively.
|
|
138
214
|
* @param blocksResponse The response from the Notion API containing the blocks to load.
|
|
139
|
-
* @returns A Promise that resolves to an
|
|
215
|
+
* @returns A Promise that resolves to an array containing the loaded MdBlocks.
|
|
140
216
|
*/
|
|
141
|
-
async
|
|
217
|
+
async loadBlocks(blocksResponse) {
|
|
142
218
|
const blocks = blocksResponse.filter(client_1.isFullBlock);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const childBlocksDocs = await this.loadBlocksAndDocs(await (0, notion_js_1.getBlockChildren)(this.notionClient, block_id, null));
|
|
161
|
-
mdBlock.children = childBlocksDocs.mdBlocks;
|
|
162
|
-
childDocuments = childBlocksDocs.childDocuments;
|
|
163
|
-
}
|
|
164
|
-
return {
|
|
165
|
-
mdBlocks: [mdBlock],
|
|
166
|
-
childDocuments,
|
|
167
|
-
};
|
|
168
|
-
})),
|
|
219
|
+
// Add child pages to queue
|
|
220
|
+
const childPages = blocks
|
|
221
|
+
.filter((block) => block.type.includes("child_page"))
|
|
222
|
+
.map((block) => block.id);
|
|
223
|
+
if (childPages.length > 0)
|
|
224
|
+
this.addToQueue(...childPages);
|
|
225
|
+
// Add child database pages to queue
|
|
226
|
+
const childDatabases = blocks
|
|
227
|
+
.filter((block) => block.type.includes("child_database"))
|
|
228
|
+
.map((block) => this.caller.call(() => this.loadDatabase(block.id)));
|
|
229
|
+
// Load this block and child blocks
|
|
230
|
+
const loadingMdBlocks = blocks
|
|
231
|
+
.filter((block) => !["child_page", "child_database"].includes(block.type))
|
|
232
|
+
.map((block) => this.loadBlock(block));
|
|
233
|
+
const [mdBlocks] = await Promise.all([
|
|
234
|
+
Promise.all(loadingMdBlocks),
|
|
235
|
+
Promise.all(childDatabases),
|
|
169
236
|
]);
|
|
170
|
-
|
|
171
|
-
.flat()
|
|
172
|
-
.map((blockDoc) => blockDoc.mdBlocks);
|
|
173
|
-
const childDocuments = blocksDocsArray
|
|
174
|
-
.flat()
|
|
175
|
-
.map((blockDoc) => blockDoc.childDocuments);
|
|
176
|
-
return {
|
|
177
|
-
mdBlocks: [...allMdBlocks.flat()],
|
|
178
|
-
childDocuments: [
|
|
179
|
-
...childPageDocuments.flat(),
|
|
180
|
-
...childDatabaseDocuments.flat(),
|
|
181
|
-
...childDocuments.flat(),
|
|
182
|
-
],
|
|
183
|
-
};
|
|
237
|
+
return mdBlocks;
|
|
184
238
|
}
|
|
185
239
|
/**
|
|
186
|
-
* Loads a Notion page and its child documents.
|
|
240
|
+
* Loads a Notion page and its child documents, then adds it to the completed documents array.
|
|
187
241
|
* @param page The Notion page or page ID to load.
|
|
188
|
-
* @returns A Promise that resolves to an array of Documents.
|
|
189
242
|
*/
|
|
190
243
|
async loadPage(page) {
|
|
191
|
-
// Check page is a page ID or a
|
|
244
|
+
// Check page is a page ID or a PageObjectResponse
|
|
192
245
|
const [pageData, pageId] = typeof page === "string"
|
|
193
|
-
? [
|
|
246
|
+
? [
|
|
247
|
+
this.caller.call(() => this.notionClient.pages.retrieve({ page_id: page })),
|
|
248
|
+
page,
|
|
249
|
+
]
|
|
194
250
|
: [page, page.id];
|
|
195
251
|
const [pageDetails, pageBlocks] = await Promise.all([
|
|
196
252
|
pageData,
|
|
197
|
-
(0, notion_js_1.getBlockChildren)(this.notionClient, pageId, null),
|
|
253
|
+
this.caller.call(() => (0, notion_js_1.getBlockChildren)(this.notionClient, pageId, null)),
|
|
198
254
|
]);
|
|
199
255
|
if (!(0, client_1.isFullPage)(pageDetails))
|
|
200
|
-
return
|
|
201
|
-
const
|
|
256
|
+
return;
|
|
257
|
+
const mdBlocks = await this.loadBlocks(pageBlocks);
|
|
202
258
|
const mdStringObject = this.n2mClient.toMarkdownString(mdBlocks);
|
|
203
259
|
const pageDocument = new document_js_1.Document({
|
|
204
260
|
pageContent: mdStringObject.parent,
|
|
205
261
|
metadata: this.parsePageDetails(pageDetails),
|
|
206
262
|
});
|
|
207
|
-
|
|
263
|
+
this.documents.push(pageDocument);
|
|
264
|
+
this.pageCompleted.push(pageId);
|
|
265
|
+
this.onDocumentLoaded(this.documents.length, this.pageQueueTotal, pageDocument.metadata.properties.title, this.rootTitle);
|
|
208
266
|
}
|
|
209
267
|
/**
|
|
210
|
-
* Loads a Notion database and
|
|
268
|
+
* Loads a Notion database and adds it's pages to the queue.
|
|
211
269
|
* @param id The ID of the Notion database to load.
|
|
212
|
-
* @returns A Promise that resolves to an array of Documents.
|
|
213
270
|
*/
|
|
214
271
|
async loadDatabase(id) {
|
|
215
|
-
const documents = [];
|
|
216
272
|
try {
|
|
217
273
|
for await (const page of (0, client_1.iteratePaginatedAPI)(this.notionClient.databases.query, {
|
|
218
274
|
database_id: id,
|
|
275
|
+
page_size: 50,
|
|
219
276
|
})) {
|
|
220
|
-
|
|
221
|
-
continue;
|
|
222
|
-
documents.push(...(await this.loadPage(page)));
|
|
277
|
+
this.addToQueue(page.id);
|
|
223
278
|
}
|
|
224
279
|
}
|
|
225
280
|
catch (e) {
|
|
226
281
|
console.log(e);
|
|
227
282
|
// TODO: Catch and report api request errors
|
|
228
283
|
}
|
|
229
|
-
return documents;
|
|
230
284
|
}
|
|
231
285
|
/**
|
|
232
286
|
* Loads the documents from Notion based on the specified options.
|
|
233
287
|
* @returns A Promise that resolves to an array of Documents.
|
|
234
288
|
*/
|
|
235
289
|
async load() {
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
290
|
+
const resPagePromise = this.notionClient.pages
|
|
291
|
+
.retrieve({ page_id: this.id })
|
|
292
|
+
.then((res) => {
|
|
293
|
+
this.addToQueue(this.id);
|
|
294
|
+
return res;
|
|
295
|
+
})
|
|
296
|
+
.catch((error) => error);
|
|
297
|
+
const resDatabasePromise = this.notionClient.databases
|
|
298
|
+
.retrieve({ database_id: this.id })
|
|
299
|
+
.then(async (res) => {
|
|
300
|
+
await this.loadDatabase(this.id);
|
|
301
|
+
return res;
|
|
302
|
+
})
|
|
303
|
+
.catch((error) => error);
|
|
304
|
+
const [resPage, resDatabase] = await Promise.all([
|
|
305
|
+
resPagePromise,
|
|
306
|
+
resDatabasePromise,
|
|
307
|
+
]);
|
|
308
|
+
// Check if both resPage and resDatabase resulted in error responses
|
|
309
|
+
const errors = [resPage, resDatabase].filter(isErrorResponse);
|
|
310
|
+
if (errors.length === 2) {
|
|
311
|
+
if (errors.every((e) => e.code === client_1.APIErrorCode.ObjectNotFound)) {
|
|
312
|
+
throw new AggregateError([
|
|
313
|
+
Error(`Could not find object with ID: ${this.id}. Make sure the relevant pages and databases are shared with your integration.`),
|
|
314
|
+
...errors,
|
|
315
|
+
]);
|
|
316
|
+
}
|
|
317
|
+
throw new AggregateError(errors);
|
|
318
|
+
}
|
|
319
|
+
this.rootTitle = getTitle(resPage) || getTitle(resDatabase) || this.id;
|
|
320
|
+
let pageId = this.pageQueue.shift();
|
|
321
|
+
while (pageId) {
|
|
322
|
+
await this.loadPage(pageId);
|
|
323
|
+
pageId = this.pageQueue.shift();
|
|
245
324
|
}
|
|
246
|
-
return documents;
|
|
325
|
+
return this.documents;
|
|
247
326
|
}
|
|
248
327
|
}
|
|
249
328
|
exports.NotionAPILoader = NotionAPILoader;
|
|
@@ -1,26 +1,37 @@
|
|
|
1
1
|
import { Client } from "@notionhq/client";
|
|
2
|
-
import { BaseDocumentLoader } from "../base.js";
|
|
3
2
|
import { Document } from "../../document.js";
|
|
3
|
+
import { BaseDocumentLoader } from "../base.js";
|
|
4
|
+
import { AsyncCaller } from "../../util/async_caller.js";
|
|
4
5
|
/**
|
|
5
6
|
* Represents the type of Notion API to load documents from. The options
|
|
6
7
|
* are "database" or "page".
|
|
7
8
|
*/
|
|
8
9
|
export type NotionAPIType = "database" | "page";
|
|
10
|
+
export type OnDocumentLoadedCallback = (current: number, total: number, currentTitle?: string, rootTitle?: string) => void;
|
|
9
11
|
export type NotionAPILoaderOptions = {
|
|
10
12
|
clientOptions: ConstructorParameters<typeof Client>[0];
|
|
11
13
|
id: string;
|
|
12
|
-
type
|
|
14
|
+
type?: NotionAPIType;
|
|
15
|
+
callerOptions?: ConstructorParameters<typeof AsyncCaller>[0];
|
|
16
|
+
onDocumentLoaded?: OnDocumentLoadedCallback;
|
|
13
17
|
};
|
|
14
18
|
/**
|
|
15
19
|
* A class that extends the BaseDocumentLoader class. It represents a
|
|
16
20
|
* document loader for loading documents from Notion using the Notion API.
|
|
17
21
|
*/
|
|
18
22
|
export declare class NotionAPILoader extends BaseDocumentLoader {
|
|
23
|
+
private caller;
|
|
19
24
|
private notionClient;
|
|
20
25
|
private n2mClient;
|
|
21
26
|
private id;
|
|
22
|
-
private
|
|
27
|
+
private pageQueue;
|
|
28
|
+
private pageCompleted;
|
|
29
|
+
pageQueueTotal: number;
|
|
30
|
+
private documents;
|
|
31
|
+
private rootTitle;
|
|
32
|
+
private onDocumentLoaded;
|
|
23
33
|
constructor(options: NotionAPILoaderOptions);
|
|
34
|
+
private addToQueue;
|
|
24
35
|
/**
|
|
25
36
|
* Parses the properties of a Notion page and returns them as key-value
|
|
26
37
|
* pairs.
|
|
@@ -41,21 +52,19 @@ export declare class NotionAPILoader extends BaseDocumentLoader {
|
|
|
41
52
|
*/
|
|
42
53
|
private loadBlock;
|
|
43
54
|
/**
|
|
44
|
-
* Loads Notion blocks and their
|
|
55
|
+
* Loads Notion blocks and their children recursively.
|
|
45
56
|
* @param blocksResponse The response from the Notion API containing the blocks to load.
|
|
46
|
-
* @returns A Promise that resolves to an
|
|
57
|
+
* @returns A Promise that resolves to an array containing the loaded MdBlocks.
|
|
47
58
|
*/
|
|
48
|
-
private
|
|
59
|
+
private loadBlocks;
|
|
49
60
|
/**
|
|
50
|
-
* Loads a Notion page and its child documents.
|
|
61
|
+
* Loads a Notion page and its child documents, then adds it to the completed documents array.
|
|
51
62
|
* @param page The Notion page or page ID to load.
|
|
52
|
-
* @returns A Promise that resolves to an array of Documents.
|
|
53
63
|
*/
|
|
54
64
|
private loadPage;
|
|
55
65
|
/**
|
|
56
|
-
* Loads a Notion database and
|
|
66
|
+
* Loads a Notion database and adds it's pages to the queue.
|
|
57
67
|
* @param id The ID of the Notion database to load.
|
|
58
|
-
* @returns A Promise that resolves to an array of Documents.
|
|
59
68
|
*/
|
|
60
69
|
private loadDatabase;
|
|
61
70
|
/**
|