langchain 0.0.132 → 0.0.134

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.
Files changed (70) hide show
  1. package/dist/agents/chat/outputParser.cjs +2 -1
  2. package/dist/agents/chat/outputParser.js +2 -1
  3. package/dist/agents/executor.cjs +106 -7
  4. package/dist/agents/executor.d.ts +23 -0
  5. package/dist/agents/executor.js +104 -6
  6. package/dist/agents/mrkl/outputParser.cjs +2 -1
  7. package/dist/agents/mrkl/outputParser.js +2 -1
  8. package/dist/callbacks/index.cjs +2 -1
  9. package/dist/callbacks/index.d.ts +1 -1
  10. package/dist/callbacks/index.js +1 -1
  11. package/dist/chains/sql_db/sql_db_chain.d.ts +1 -1
  12. package/dist/chains/sql_db/sql_db_prompt.d.ts +6 -6
  13. package/dist/chat_models/googlevertexai.cjs +1 -1
  14. package/dist/chat_models/googlevertexai.d.ts +2 -2
  15. package/dist/chat_models/googlevertexai.js +2 -2
  16. package/dist/chat_models/ollama.cjs +8 -8
  17. package/dist/chat_models/ollama.js +8 -8
  18. package/dist/document_loaders/web/notionapi.cjs +153 -74
  19. package/dist/document_loaders/web/notionapi.d.ts +19 -10
  20. package/dist/document_loaders/web/notionapi.js +154 -75
  21. package/dist/document_loaders/web/recursive_url.cjs +177 -0
  22. package/dist/document_loaders/web/recursive_url.d.ts +27 -0
  23. package/dist/document_loaders/web/recursive_url.js +173 -0
  24. package/dist/embeddings/googlevertexai.cjs +1 -1
  25. package/dist/embeddings/googlevertexai.d.ts +2 -2
  26. package/dist/embeddings/googlevertexai.js +2 -2
  27. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +1 -1
  28. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -2
  29. package/dist/experimental/multimodal_embeddings/googlevertexai.js +2 -2
  30. package/dist/hub.cjs +16 -0
  31. package/dist/hub.d.ts +4 -0
  32. package/dist/hub.js +11 -0
  33. package/dist/llms/bedrock.cjs +63 -19
  34. package/dist/llms/bedrock.d.ts +9 -1
  35. package/dist/llms/bedrock.js +63 -19
  36. package/dist/llms/googlevertexai.cjs +1 -1
  37. package/dist/llms/googlevertexai.js +2 -2
  38. package/dist/load/import_constants.cjs +3 -0
  39. package/dist/load/import_constants.js +3 -0
  40. package/dist/schema/output_parser.cjs +2 -2
  41. package/dist/schema/output_parser.js +2 -2
  42. package/dist/tools/base.cjs +26 -2
  43. package/dist/tools/base.d.ts +9 -0
  44. package/dist/tools/base.js +24 -1
  45. package/dist/tools/sql.cjs +9 -3
  46. package/dist/tools/sql.d.ts +0 -1
  47. package/dist/tools/sql.js +9 -3
  48. package/dist/types/googlevertexai-types.d.ts +8 -3
  49. package/dist/util/googlevertexai-connection.cjs +49 -15
  50. package/dist/util/googlevertexai-connection.d.ts +12 -4
  51. package/dist/util/googlevertexai-connection.js +46 -13
  52. package/dist/vectorstores/googlevertexai.cjs +551 -0
  53. package/dist/vectorstores/googlevertexai.d.ts +180 -0
  54. package/dist/vectorstores/googlevertexai.js +520 -0
  55. package/dist/vectorstores/myscale.cjs +2 -2
  56. package/dist/vectorstores/myscale.d.ts +1 -1
  57. package/dist/vectorstores/myscale.js +2 -2
  58. package/dist/vectorstores/vectara.cjs +11 -2
  59. package/dist/vectorstores/vectara.d.ts +10 -1
  60. package/dist/vectorstores/vectara.js +11 -2
  61. package/document_loaders/web/recursive_url.cjs +1 -0
  62. package/document_loaders/web/recursive_url.d.ts +1 -0
  63. package/document_loaders/web/recursive_url.js +1 -0
  64. package/hub.cjs +1 -0
  65. package/hub.d.ts +1 -0
  66. package/hub.js +1 -0
  67. package/package.json +41 -2
  68. package/vectorstores/googlevertexai.cjs +1 -0
  69. package/vectorstores/googlevertexai.d.ts +1 -0
  70. 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, "type", {
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.notionClient = new client_1.Client(options.clientOptions);
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.type = options.type;
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
- return {
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 child documents recursively.
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 object containing the loaded MdBlocks and child Documents.
215
+ * @returns A Promise that resolves to an array containing the loaded MdBlocks.
140
216
  */
141
- async loadBlocksAndDocs(blocksResponse) {
217
+ async loadBlocks(blocksResponse) {
142
218
  const blocks = blocksResponse.filter(client_1.isFullBlock);
143
- const [childPageDocuments, childDatabaseDocuments, blocksDocsArray] = await Promise.all([
144
- Promise.all(blocks
145
- .filter((block) => block.type.includes("child_page"))
146
- .map((block) => this.loadPage(block.id))),
147
- Promise.all(blocks
148
- .filter((block) => block.type.includes("child_database"))
149
- .map((block) => this.loadDatabase(block.id))),
150
- Promise.all(blocks
151
- .filter((block) => !["child_page", "child_database"].includes(block.type))
152
- .map(async (block) => {
153
- const mdBlock = await this.loadBlock(block);
154
- let childDocuments = [];
155
- if (block.has_children) {
156
- const block_id = block.type === "synced_block" &&
157
- block.synced_block?.synced_from?.block_id
158
- ? block.synced_block.synced_from.block_id
159
- : block.id;
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
- const allMdBlocks = blocksDocsArray
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 GetPageResponse
244
+ // Check page is a page ID or a PageObjectResponse
192
245
  const [pageData, pageId] = typeof page === "string"
193
- ? [this.notionClient.pages.retrieve({ page_id: page }), page]
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 { mdBlocks, childDocuments } = await this.loadBlocksAndDocs(pageBlocks);
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
- return [pageDocument, ...childDocuments];
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 its documents.
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
- if (!(0, client_1.isFullPage)(page))
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 documents = [];
237
- switch (this.type) {
238
- case "page":
239
- documents.push(...(await this.loadPage(this.id)));
240
- break;
241
- case "database":
242
- documents.push(...(await this.loadDatabase(this.id)));
243
- break;
244
- default:
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: NotionAPIType;
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 type;
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 child documents recursively.
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 object containing the loaded MdBlocks and child Documents.
57
+ * @returns A Promise that resolves to an array containing the loaded MdBlocks.
47
58
  */
48
- private loadBlocksAndDocs;
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 its documents.
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
  /**