langchain 0.0.146 → 0.0.148

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 (168) hide show
  1. package/chat_models/googlevertexai/web.cjs +1 -0
  2. package/chat_models/googlevertexai/web.d.ts +1 -0
  3. package/chat_models/googlevertexai/web.js +1 -0
  4. package/chat_models/googlevertexai.cjs +1 -1
  5. package/chat_models/googlevertexai.d.ts +1 -1
  6. package/chat_models/googlevertexai.js +1 -1
  7. package/dist/base_language/index.cjs +2 -2
  8. package/dist/base_language/index.d.ts +2 -1
  9. package/dist/base_language/index.js +1 -1
  10. package/dist/chains/base.d.ts +1 -1
  11. package/dist/chains/constitutional_ai/constitutional_principle.cjs +272 -1
  12. package/dist/chains/constitutional_ai/constitutional_principle.js +272 -1
  13. package/dist/chains/openai_functions/openapi.cjs +32 -27
  14. package/dist/chains/openai_functions/openapi.d.ts +9 -0
  15. package/dist/chains/openai_functions/openapi.js +31 -27
  16. package/dist/chat_models/base.d.ts +1 -1
  17. package/dist/chat_models/{googlevertexai.cjs → googlevertexai/common.cjs} +14 -26
  18. package/dist/chat_models/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -22
  19. package/dist/chat_models/{googlevertexai.js → googlevertexai/common.js} +12 -24
  20. package/dist/chat_models/googlevertexai/index.cjs +36 -0
  21. package/dist/chat_models/googlevertexai/index.d.ts +21 -0
  22. package/dist/chat_models/googlevertexai/index.js +31 -0
  23. package/dist/chat_models/googlevertexai/web.cjs +33 -0
  24. package/dist/chat_models/googlevertexai/web.d.ts +19 -0
  25. package/dist/chat_models/googlevertexai/web.js +28 -0
  26. package/dist/chat_models/openai.cjs +1 -1
  27. package/dist/chat_models/openai.js +1 -1
  28. package/dist/document_loaders/web/notionapi.cjs +93 -70
  29. package/dist/document_loaders/web/notionapi.d.ts +33 -1
  30. package/dist/document_loaders/web/notionapi.js +89 -71
  31. package/dist/embeddings/googlevertexai.cjs +5 -1
  32. package/dist/embeddings/googlevertexai.d.ts +2 -1
  33. package/dist/embeddings/googlevertexai.js +5 -1
  34. package/dist/evaluation/agents/index.cjs +17 -0
  35. package/dist/evaluation/agents/index.d.ts +1 -0
  36. package/dist/evaluation/agents/index.js +1 -0
  37. package/dist/evaluation/agents/prompt.cjs +132 -0
  38. package/dist/evaluation/agents/prompt.d.ts +6 -0
  39. package/dist/evaluation/agents/prompt.js +129 -0
  40. package/dist/evaluation/agents/trajectory.cjs +189 -0
  41. package/dist/evaluation/agents/trajectory.d.ts +54 -0
  42. package/dist/evaluation/agents/trajectory.js +184 -0
  43. package/dist/evaluation/base.cjs +274 -0
  44. package/dist/evaluation/base.d.ts +232 -0
  45. package/dist/evaluation/base.js +263 -0
  46. package/dist/evaluation/comparison/index.cjs +17 -0
  47. package/dist/evaluation/comparison/index.d.ts +1 -0
  48. package/dist/evaluation/comparison/index.js +1 -0
  49. package/dist/evaluation/comparison/pairwise.cjs +244 -0
  50. package/dist/evaluation/comparison/pairwise.d.ts +50 -0
  51. package/dist/evaluation/comparison/pairwise.js +238 -0
  52. package/dist/evaluation/comparison/prompt.cjs +74 -0
  53. package/dist/evaluation/comparison/prompt.d.ts +21 -0
  54. package/dist/evaluation/comparison/prompt.js +71 -0
  55. package/dist/evaluation/criteria/criteria.cjs +259 -0
  56. package/dist/evaluation/criteria/criteria.d.ts +73 -0
  57. package/dist/evaluation/criteria/criteria.js +253 -0
  58. package/dist/evaluation/criteria/index.cjs +17 -0
  59. package/dist/evaluation/criteria/index.d.ts +1 -0
  60. package/dist/evaluation/criteria/index.js +1 -0
  61. package/dist/evaluation/criteria/prompt.cjs +36 -0
  62. package/dist/evaluation/criteria/prompt.d.ts +12 -0
  63. package/dist/evaluation/criteria/prompt.js +33 -0
  64. package/dist/evaluation/embedding_distance/base.cjs +163 -0
  65. package/dist/evaluation/embedding_distance/base.d.ts +78 -0
  66. package/dist/evaluation/embedding_distance/base.js +156 -0
  67. package/dist/evaluation/embedding_distance/index.cjs +17 -0
  68. package/dist/evaluation/embedding_distance/index.d.ts +1 -0
  69. package/dist/evaluation/embedding_distance/index.js +1 -0
  70. package/dist/evaluation/index.cjs +6 -0
  71. package/dist/evaluation/index.d.ts +6 -0
  72. package/dist/evaluation/index.js +6 -0
  73. package/dist/evaluation/loader.cjs +60 -0
  74. package/dist/evaluation/loader.d.ts +27 -0
  75. package/dist/evaluation/loader.js +56 -0
  76. package/dist/evaluation/types.cjs +2 -0
  77. package/dist/evaluation/types.d.ts +35 -0
  78. package/dist/evaluation/types.js +1 -0
  79. package/dist/experimental/llms/bittensor.cjs +141 -0
  80. package/dist/experimental/llms/bittensor.d.ts +33 -0
  81. package/dist/experimental/llms/bittensor.js +137 -0
  82. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +5 -1
  83. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
  84. package/dist/experimental/multimodal_embeddings/googlevertexai.js +5 -1
  85. package/dist/hub.d.ts +1 -1
  86. package/dist/llms/base.d.ts +1 -1
  87. package/dist/llms/{googlevertexai.js → googlevertexai/common.cjs} +21 -17
  88. package/dist/llms/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -23
  89. package/dist/llms/{googlevertexai.cjs → googlevertexai/common.js} +17 -21
  90. package/dist/llms/googlevertexai/index.cjs +34 -0
  91. package/dist/llms/googlevertexai/index.d.ts +26 -0
  92. package/dist/llms/googlevertexai/index.js +30 -0
  93. package/dist/llms/googlevertexai/web.cjs +31 -0
  94. package/dist/llms/googlevertexai/web.d.ts +24 -0
  95. package/dist/llms/googlevertexai/web.js +27 -0
  96. package/dist/llms/openai-chat.cjs +1 -1
  97. package/dist/llms/openai-chat.js +1 -1
  98. package/dist/llms/openai.cjs +1 -1
  99. package/dist/llms/openai.js +1 -1
  100. package/dist/load/import_constants.cjs +3 -0
  101. package/dist/load/import_constants.js +3 -0
  102. package/dist/load/import_map.cjs +3 -2
  103. package/dist/load/import_map.d.ts +2 -1
  104. package/dist/load/import_map.js +2 -1
  105. package/dist/load/index.cjs +2 -1
  106. package/dist/load/index.js +2 -1
  107. package/dist/load/serializable.cjs +23 -4
  108. package/dist/load/serializable.js +23 -4
  109. package/dist/prompts/base.cjs +2 -2
  110. package/dist/prompts/base.d.ts +1 -1
  111. package/dist/prompts/base.js +1 -1
  112. package/dist/prompts/chat.cjs +2 -2
  113. package/dist/prompts/chat.d.ts +1 -1
  114. package/dist/prompts/chat.js +1 -1
  115. package/dist/retrievers/multi_query.cjs +140 -0
  116. package/dist/retrievers/multi_query.d.ts +33 -0
  117. package/dist/retrievers/multi_query.js +136 -0
  118. package/dist/schema/document.cjs +2 -2
  119. package/dist/schema/document.d.ts +1 -1
  120. package/dist/schema/document.js +1 -1
  121. package/dist/schema/output_parser.cjs +2 -2
  122. package/dist/schema/output_parser.d.ts +2 -1
  123. package/dist/schema/output_parser.js +1 -1
  124. package/dist/schema/retriever.cjs +2 -2
  125. package/dist/schema/retriever.d.ts +2 -1
  126. package/dist/schema/retriever.js +1 -1
  127. package/dist/schema/runnable/config.cjs +8 -0
  128. package/dist/schema/runnable/config.d.ts +3 -0
  129. package/dist/schema/runnable/config.js +4 -0
  130. package/dist/schema/{runnable.cjs → runnable/index.cjs} +290 -101
  131. package/dist/schema/{runnable.d.ts → runnable/index.d.ts} +127 -41
  132. package/dist/schema/{runnable.js → runnable/index.js} +284 -99
  133. package/dist/tools/base.d.ts +1 -1
  134. package/dist/types/googlevertexai-types.d.ts +11 -4
  135. package/dist/util/async_caller.cjs +35 -25
  136. package/dist/util/async_caller.d.ts +8 -0
  137. package/dist/util/async_caller.js +35 -25
  138. package/dist/util/googlevertexai-connection.cjs +14 -15
  139. package/dist/util/googlevertexai-connection.d.ts +7 -7
  140. package/dist/util/googlevertexai-connection.js +14 -15
  141. package/dist/util/googlevertexai-webauth.cjs +56 -0
  142. package/dist/util/googlevertexai-webauth.d.ts +25 -0
  143. package/dist/util/googlevertexai-webauth.js +52 -0
  144. package/dist/vectorstores/googlevertexai.cjs +9 -8
  145. package/dist/vectorstores/googlevertexai.d.ts +8 -7
  146. package/dist/vectorstores/googlevertexai.js +9 -8
  147. package/dist/vectorstores/pinecone.cjs +30 -22
  148. package/dist/vectorstores/pinecone.d.ts +3 -1
  149. package/dist/vectorstores/pinecone.js +30 -22
  150. package/dist/vectorstores/vectara.cjs +20 -23
  151. package/dist/vectorstores/vectara.d.ts +9 -2
  152. package/dist/vectorstores/vectara.js +20 -23
  153. package/experimental/llms/bittensor.cjs +1 -0
  154. package/experimental/llms/bittensor.d.ts +1 -0
  155. package/experimental/llms/bittensor.js +1 -0
  156. package/llms/googlevertexai/web.cjs +1 -0
  157. package/llms/googlevertexai/web.d.ts +1 -0
  158. package/llms/googlevertexai/web.js +1 -0
  159. package/llms/googlevertexai.cjs +1 -1
  160. package/llms/googlevertexai.d.ts +1 -1
  161. package/llms/googlevertexai.js +1 -1
  162. package/package.json +40 -3
  163. package/retrievers/multi_query.cjs +1 -0
  164. package/retrievers/multi_query.d.ts +1 -0
  165. package/retrievers/multi_query.js +1 -0
  166. package/schema/runnable.cjs +1 -1
  167. package/schema/runnable.d.ts +1 -1
  168. package/schema/runnable.js +1 -1
@@ -1,6 +1,5 @@
1
- import { BaseChatModel } from "./base.js";
2
- import { AIMessage, ChatMessage, } from "../schema/index.js";
3
- import { GoogleVertexAILLMConnection } from "../util/googlevertexai-connection.js";
1
+ import { BaseChatModel } from "../base.js";
2
+ import { AIMessage, ChatMessage, } from "../../schema/index.js";
4
3
  /**
5
4
  * Represents a chat message in the Google Vertex AI chat model.
6
5
  */
@@ -81,22 +80,15 @@ export class GoogleVertexAIChatMessage {
81
80
  }
82
81
  }
83
82
  /**
84
- * Enables calls to the Google Cloud's Vertex AI API to access
85
- * Large Language Models in a chat-like fashion.
86
- *
87
- * To use, you will need to have one of the following authentication
88
- * methods in place:
89
- * - You are logged into an account permitted to the Google Cloud project
90
- * using Vertex AI.
91
- * - You are running this on a machine using a service account permitted to
92
- * the Google Cloud project using Vertex AI.
93
- * - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
94
- * path of a credentials file for a service account permitted to the
95
- * Google Cloud project using Vertex AI.
83
+ * Base class for Google Vertex AI chat models.
84
+ * Implemented subclasses must provide a GoogleVertexAILLMConnection
85
+ * with appropriate auth client.
96
86
  */
97
- export class ChatGoogleVertexAI extends BaseChatModel {
98
- static lc_name() {
99
- return "ChatGoogleVertexAI";
87
+ export class BaseChatGoogleVertexAI extends BaseChatModel {
88
+ get lc_aliases() {
89
+ return {
90
+ model: "model_name",
91
+ };
100
92
  }
101
93
  constructor(fields) {
102
94
  super(fields ?? {});
@@ -154,10 +146,6 @@ export class ChatGoogleVertexAI extends BaseChatModel {
154
146
  this.topP = fields?.topP ?? this.topP;
155
147
  this.topK = fields?.topK ?? this.topK;
156
148
  this.examples = fields?.examples ?? this.examples;
157
- this.connection = new GoogleVertexAILLMConnection({
158
- ...fields,
159
- ...this,
160
- }, this.caller);
161
149
  }
162
150
  _combineLLMOutput() {
163
151
  // TODO: Combine the safetyAttributes
@@ -173,13 +161,13 @@ export class ChatGoogleVertexAI extends BaseChatModel {
173
161
  maxOutputTokens: this.maxOutputTokens,
174
162
  };
175
163
  const result = await this.connection.request([instance], parameters, options);
176
- const generations = result?.data?.predictions?.map((prediction) => ChatGoogleVertexAI.convertPrediction(prediction)) ?? [];
164
+ const generations = result?.data?.predictions?.map((prediction) => BaseChatGoogleVertexAI.convertPrediction(prediction)) ?? [];
177
165
  return {
178
166
  generations,
179
167
  };
180
168
  }
181
169
  _llmType() {
182
- return "googlevertexai";
170
+ return "vertexai";
183
171
  }
184
172
  /**
185
173
  * Creates an instance of the Google Vertex AI chat model.
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GoogleVertexAIChatMessage = exports.ChatGoogleVertexAI = void 0;
4
+ const google_auth_library_1 = require("google-auth-library");
5
+ const common_js_1 = require("./common.cjs");
6
+ const googlevertexai_connection_js_1 = require("../../util/googlevertexai-connection.cjs");
7
+ /**
8
+ * Enables calls to the Google Cloud's Vertex AI API to access
9
+ * Large Language Models in a chat-like fashion.
10
+ *
11
+ * To use, you will need to have one of the following authentication
12
+ * methods in place:
13
+ * - You are logged into an account permitted to the Google Cloud project
14
+ * using Vertex AI.
15
+ * - You are running this on a machine using a service account permitted to
16
+ * the Google Cloud project using Vertex AI.
17
+ * - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
18
+ * path of a credentials file for a service account permitted to the
19
+ * Google Cloud project using Vertex AI.
20
+ */
21
+ class ChatGoogleVertexAI extends common_js_1.BaseChatGoogleVertexAI {
22
+ static lc_name() {
23
+ return "ChatVertexAI";
24
+ }
25
+ constructor(fields) {
26
+ super(fields);
27
+ const client = new google_auth_library_1.GoogleAuth({
28
+ scopes: "https://www.googleapis.com/auth/cloud-platform",
29
+ ...fields?.authOptions,
30
+ });
31
+ this.connection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
32
+ }
33
+ }
34
+ exports.ChatGoogleVertexAI = ChatGoogleVertexAI;
35
+ var common_js_2 = require("./common.cjs");
36
+ Object.defineProperty(exports, "GoogleVertexAIChatMessage", { enumerable: true, get: function () { return common_js_2.GoogleVertexAIChatMessage; } });
@@ -0,0 +1,21 @@
1
+ import { GoogleAuthOptions } from "google-auth-library";
2
+ import { BaseChatGoogleVertexAI, GoogleVertexAIChatInput } from "./common.js";
3
+ /**
4
+ * Enables calls to the Google Cloud's Vertex AI API to access
5
+ * Large Language Models in a chat-like fashion.
6
+ *
7
+ * To use, you will need to have one of the following authentication
8
+ * methods in place:
9
+ * - You are logged into an account permitted to the Google Cloud project
10
+ * using Vertex AI.
11
+ * - You are running this on a machine using a service account permitted to
12
+ * the Google Cloud project using Vertex AI.
13
+ * - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
14
+ * path of a credentials file for a service account permitted to the
15
+ * Google Cloud project using Vertex AI.
16
+ */
17
+ export declare class ChatGoogleVertexAI extends BaseChatGoogleVertexAI<GoogleAuthOptions> {
18
+ static lc_name(): string;
19
+ constructor(fields?: GoogleVertexAIChatInput<GoogleAuthOptions>);
20
+ }
21
+ export { ChatExample, GoogleVertexAIChatAuthor, GoogleVertexAIChatInput, GoogleVertexAIChatInstance, GoogleVertexAIChatMessage, GoogleVertexAIChatMessageFields, GoogleVertexAIChatPrediction, } from "./common.js";
@@ -0,0 +1,31 @@
1
+ import { GoogleAuth } from "google-auth-library";
2
+ import { BaseChatGoogleVertexAI } from "./common.js";
3
+ import { GoogleVertexAILLMConnection } from "../../util/googlevertexai-connection.js";
4
+ /**
5
+ * Enables calls to the Google Cloud's Vertex AI API to access
6
+ * Large Language Models in a chat-like fashion.
7
+ *
8
+ * To use, you will need to have one of the following authentication
9
+ * methods in place:
10
+ * - You are logged into an account permitted to the Google Cloud project
11
+ * using Vertex AI.
12
+ * - You are running this on a machine using a service account permitted to
13
+ * the Google Cloud project using Vertex AI.
14
+ * - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
15
+ * path of a credentials file for a service account permitted to the
16
+ * Google Cloud project using Vertex AI.
17
+ */
18
+ export class ChatGoogleVertexAI extends BaseChatGoogleVertexAI {
19
+ static lc_name() {
20
+ return "ChatVertexAI";
21
+ }
22
+ constructor(fields) {
23
+ super(fields);
24
+ const client = new GoogleAuth({
25
+ scopes: "https://www.googleapis.com/auth/cloud-platform",
26
+ ...fields?.authOptions,
27
+ });
28
+ this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
29
+ }
30
+ }
31
+ export { GoogleVertexAIChatMessage, } from "./common.js";
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GoogleVertexAIChatMessage = exports.ChatGoogleVertexAI = void 0;
4
+ const googlevertexai_connection_js_1 = require("../../util/googlevertexai-connection.cjs");
5
+ const googlevertexai_webauth_js_1 = require("../../util/googlevertexai-webauth.cjs");
6
+ const common_js_1 = require("./common.cjs");
7
+ /**
8
+ * Enables calls to the Google Cloud's Vertex AI API to access
9
+ * Large Language Models in a chat-like fashion.
10
+ *
11
+ * This entrypoint and class are intended to be used in web environments like Edge
12
+ * functions where you do not have access to the file system. It supports passing
13
+ * service account credentials directly as a "GOOGLE_VERTEX_AI_WEB_CREDENTIALS"
14
+ * environment variable or directly as "authOptions.credentials".
15
+ */
16
+ class ChatGoogleVertexAI extends common_js_1.BaseChatGoogleVertexAI {
17
+ static lc_name() {
18
+ return "ChatVertexAI";
19
+ }
20
+ get lc_secrets() {
21
+ return {
22
+ "authOptions.credentials": "GOOGLE_VERTEX_AI_WEB_CREDENTIALS",
23
+ };
24
+ }
25
+ constructor(fields) {
26
+ super(fields);
27
+ const client = new googlevertexai_webauth_js_1.WebGoogleAuth(fields?.authOptions);
28
+ this.connection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
29
+ }
30
+ }
31
+ exports.ChatGoogleVertexAI = ChatGoogleVertexAI;
32
+ var common_js_2 = require("./common.cjs");
33
+ Object.defineProperty(exports, "GoogleVertexAIChatMessage", { enumerable: true, get: function () { return common_js_2.GoogleVertexAIChatMessage; } });
@@ -0,0 +1,19 @@
1
+ import { WebGoogleAuthOptions } from "../../util/googlevertexai-webauth.js";
2
+ import { BaseChatGoogleVertexAI, GoogleVertexAIChatInput } from "./common.js";
3
+ /**
4
+ * Enables calls to the Google Cloud's Vertex AI API to access
5
+ * Large Language Models in a chat-like fashion.
6
+ *
7
+ * This entrypoint and class are intended to be used in web environments like Edge
8
+ * functions where you do not have access to the file system. It supports passing
9
+ * service account credentials directly as a "GOOGLE_VERTEX_AI_WEB_CREDENTIALS"
10
+ * environment variable or directly as "authOptions.credentials".
11
+ */
12
+ export declare class ChatGoogleVertexAI extends BaseChatGoogleVertexAI<WebGoogleAuthOptions> {
13
+ static lc_name(): string;
14
+ get lc_secrets(): {
15
+ [key: string]: string;
16
+ };
17
+ constructor(fields?: GoogleVertexAIChatInput<WebGoogleAuthOptions>);
18
+ }
19
+ export { ChatExample, GoogleVertexAIChatAuthor, GoogleVertexAIChatInput, GoogleVertexAIChatInstance, GoogleVertexAIChatMessage, GoogleVertexAIChatMessageFields, GoogleVertexAIChatPrediction, } from "./common.js";
@@ -0,0 +1,28 @@
1
+ import { GoogleVertexAILLMConnection } from "../../util/googlevertexai-connection.js";
2
+ import { WebGoogleAuth, } from "../../util/googlevertexai-webauth.js";
3
+ import { BaseChatGoogleVertexAI } from "./common.js";
4
+ /**
5
+ * Enables calls to the Google Cloud's Vertex AI API to access
6
+ * Large Language Models in a chat-like fashion.
7
+ *
8
+ * This entrypoint and class are intended to be used in web environments like Edge
9
+ * functions where you do not have access to the file system. It supports passing
10
+ * service account credentials directly as a "GOOGLE_VERTEX_AI_WEB_CREDENTIALS"
11
+ * environment variable or directly as "authOptions.credentials".
12
+ */
13
+ export class ChatGoogleVertexAI extends BaseChatGoogleVertexAI {
14
+ static lc_name() {
15
+ return "ChatVertexAI";
16
+ }
17
+ get lc_secrets() {
18
+ return {
19
+ "authOptions.credentials": "GOOGLE_VERTEX_AI_WEB_CREDENTIALS",
20
+ };
21
+ }
22
+ constructor(fields) {
23
+ super(fields);
24
+ const client = new WebGoogleAuth(fields?.authOptions);
25
+ this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
26
+ }
27
+ }
28
+ export { GoogleVertexAIChatMessage, } from "./common.js";
@@ -388,7 +388,7 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
388
388
  let defaultRole;
389
389
  const streamIterable = await this.completionWithRetry(params, options);
390
390
  for await (const data of streamIterable) {
391
- const choice = data.choices[0];
391
+ const choice = data?.choices[0];
392
392
  if (!choice) {
393
393
  continue;
394
394
  }
@@ -385,7 +385,7 @@ export class ChatOpenAI extends BaseChatModel {
385
385
  let defaultRole;
386
386
  const streamIterable = await this.completionWithRetry(params, options);
387
387
  for await (const data of streamIterable) {
388
- const choice = data.choices[0];
388
+ const choice = data?.choices[0];
389
389
  if (!choice) {
390
390
  continue;
391
391
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotionAPILoader = void 0;
3
+ exports.NotionAPILoader = exports.isDatabase = exports.isPage = exports.isErrorResponse = exports.isDatabaseResponse = exports.isPageResponse = 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");
@@ -8,18 +8,15 @@ const document_js_1 = require("../../document.cjs");
8
8
  const base_js_1 = require("../base.cjs");
9
9
  const async_caller_js_1 = require("../../util/async_caller.cjs");
10
10
  const isPageResponse = (res) => !(0, client_1.isNotionClientError)(res) && res.object === "page";
11
+ exports.isPageResponse = isPageResponse;
11
12
  const isDatabaseResponse = (res) => !(0, client_1.isNotionClientError)(res) && res.object === "database";
13
+ exports.isDatabaseResponse = isDatabaseResponse;
12
14
  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
- };
15
+ exports.isErrorResponse = isErrorResponse;
16
+ const isPage = (res) => (0, exports.isPageResponse)(res) && (0, client_1.isFullPage)(res);
17
+ exports.isPage = isPage;
18
+ const isDatabase = (res) => (0, exports.isDatabaseResponse)(res) && (0, client_1.isFullDatabase)(res);
19
+ exports.isDatabase = isDatabase;
23
20
  /**
24
21
  * A class that extends the BaseDocumentLoader class. It represents a
25
22
  * document loader for loading documents from Notion using the Notion API.
@@ -107,11 +104,85 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
107
104
  this.rootTitle = "";
108
105
  this.onDocumentLoaded = options.onDocumentLoaded ?? ((_ti, _cu) => { });
109
106
  }
107
+ /**
108
+ * Adds a selection of page ids to the pageQueue and removes duplicates.
109
+ * @param items An array of string ids
110
+ */
110
111
  addToQueue(...items) {
111
112
  const deDuped = items.filter((item) => !this.pageCompleted.concat(this.pageQueue).includes(item));
112
113
  this.pageQueue.push(...deDuped);
113
114
  this.pageQueueTotal += deDuped.length;
114
115
  }
116
+ /**
117
+ * Parses a Notion GetResponse object (page or database) and returns a string of the title.
118
+ * @param obj The Notion GetResponse object to parse.
119
+ * @returns The string of the title.
120
+ */
121
+ getTitle(obj) {
122
+ if ((0, exports.isPage)(obj) && obj.properties.title.type === "title") {
123
+ return obj.properties.title.title[0]?.plain_text;
124
+ }
125
+ if ((0, exports.isDatabase)(obj))
126
+ return obj.title[0]?.plain_text;
127
+ return null;
128
+ }
129
+ /**
130
+ * Parses the property type and returns a string
131
+ * @param page The Notion page property to parse.
132
+ * @returns A string of parsed property.
133
+ */
134
+ getPropValue(prop) {
135
+ switch (prop.type) {
136
+ case "number": {
137
+ const propNumber = prop[prop.type];
138
+ return propNumber !== null ? propNumber.toString() : "";
139
+ }
140
+ case "url":
141
+ return prop[prop.type] || "";
142
+ case "select":
143
+ return prop[prop.type]?.name ?? "";
144
+ case "multi_select":
145
+ return `[${prop[prop.type].map((v) => `"${v.name}"`).join(", ")}]`;
146
+ case "status":
147
+ return prop[prop.type]?.name ?? "";
148
+ case "date":
149
+ return `${prop[prop.type]?.start ?? ""}${prop[prop.type]?.end ? ` - ${prop[prop.type]?.end}` : ""}`;
150
+ case "email":
151
+ return prop[prop.type] || "";
152
+ case "phone_number":
153
+ return prop[prop.type] || "";
154
+ case "checkbox":
155
+ return prop[prop.type].toString();
156
+ case "files":
157
+ return `[${prop[prop.type].map((v) => `"${v.name}"`).join(", ")}]`;
158
+ case "created_by":
159
+ return `["${prop[prop.type].object}", "${prop[prop.type].id}"]`;
160
+ case "created_time":
161
+ return prop[prop.type];
162
+ case "last_edited_by":
163
+ return `["${prop[prop.type].object}", "${prop[prop.type].id}"]`;
164
+ case "last_edited_time":
165
+ return prop[prop.type];
166
+ case "title":
167
+ return prop[prop.type]
168
+ .map((v) => this.n2mClient.annotatePlainText(v.plain_text, v.annotations))
169
+ .join("");
170
+ case "rich_text":
171
+ return prop[prop.type]
172
+ .map((v) => this.n2mClient.annotatePlainText(v.plain_text, v.annotations))
173
+ .join("");
174
+ case "people":
175
+ return `[${prop[prop.type]
176
+ .map((v) => `["${v.object}", "${v.id}"]`)
177
+ .join(", ")}]`;
178
+ case "unique_id":
179
+ return `${prop[prop.type].prefix || ""}${prop[prop.type].number}`;
180
+ case "relation":
181
+ return `[${prop[prop.type].map((v) => `"${v.id}"`).join(", ")}]`;
182
+ default:
183
+ return `Unsupported type: ${prop.type}`;
184
+ }
185
+ }
115
186
  /**
116
187
  * Parses the properties of a Notion page and returns them as key-value
117
188
  * pairs.
@@ -119,60 +190,11 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
119
190
  * @returns An object containing the parsed properties as key-value pairs.
120
191
  */
121
192
  parsePageProperties(page) {
122
- return Object.fromEntries(Object.entries(page.properties).map(([_, prop]) => {
123
- switch (prop.type) {
124
- case "number":
125
- return [prop.type, prop[prop.type]];
126
- case "url":
127
- return [prop.type, prop[prop.type]];
128
- case "select":
129
- return [prop.type, prop[prop.type]?.name ?? ""];
130
- case "multi_select":
131
- return [
132
- prop.type,
133
- prop[prop.type].map((select) => select.name).join(", "),
134
- ];
135
- case "status":
136
- return [prop.type, prop[prop.type]?.name ?? ""];
137
- case "date":
138
- return [
139
- prop.type,
140
- `${prop[prop.type]?.start ?? ""}${prop[prop.type]?.end ? `- ${prop[prop.type]?.end}` : ""}`,
141
- ];
142
- case "email":
143
- return [prop.type, prop[prop.type]];
144
- case "phone_number":
145
- return [prop.type, prop[prop.type]];
146
- case "checkbox":
147
- return [prop.type, prop[prop.type].toString()];
148
- // case "files":
149
- case "created_by":
150
- return [prop.type, prop[prop.type]];
151
- case "created_time":
152
- return [prop.type, prop[prop.type]];
153
- case "last_edited_by":
154
- return [prop.type, prop[prop.type]];
155
- case "last_edited_time":
156
- return [prop.type, prop[prop.type]];
157
- // case "formula":
158
- case "title":
159
- return [
160
- prop.type,
161
- prop[prop.type].map((v) => v.plain_text).join(""),
162
- ];
163
- case "rich_text":
164
- return [
165
- prop.type,
166
- prop[prop.type].map((v) => v.plain_text).join(""),
167
- ];
168
- case "people":
169
- return [prop.type, prop[prop.type]];
170
- // case "relation":
171
- // case "rollup":
172
- default:
173
- return [prop.type, "Unsupported type"];
174
- }
175
- }));
193
+ return Object.entries(page.properties).reduce((accum, [propName, prop]) => {
194
+ const value = this.getPropValue(prop);
195
+ const props = { ...accum, [propName]: value };
196
+ return prop.type === "title" ? { ...props, _title: value } : props;
197
+ }, {});
176
198
  }
177
199
  /**
178
200
  * Parses the details of a Notion page and returns them as an object.
@@ -180,10 +202,10 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
180
202
  * @returns An object containing the parsed details of the page.
181
203
  */
182
204
  parsePageDetails(page) {
183
- const metadata = Object.fromEntries(Object.entries(page).filter(([key, _]) => key !== "id"));
205
+ const { id, ...rest } = page;
184
206
  return {
185
- ...metadata,
186
- notionId: page.id,
207
+ ...rest,
208
+ notionId: id,
187
209
  properties: this.parsePageProperties(page),
188
210
  };
189
211
  }
@@ -306,7 +328,7 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
306
328
  resDatabasePromise,
307
329
  ]);
308
330
  // Check if both resPage and resDatabase resulted in error responses
309
- const errors = [resPage, resDatabase].filter(isErrorResponse);
331
+ const errors = [resPage, resDatabase].filter(exports.isErrorResponse);
310
332
  if (errors.length === 2) {
311
333
  if (errors.every((e) => e.code === client_1.APIErrorCode.ObjectNotFound)) {
312
334
  throw new AggregateError([
@@ -316,7 +338,8 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
316
338
  }
317
339
  throw new AggregateError(errors);
318
340
  }
319
- this.rootTitle = getTitle(resPage) || getTitle(resDatabase) || this.id;
341
+ this.rootTitle =
342
+ this.getTitle(resPage) || this.getTitle(resDatabase) || this.id;
320
343
  let pageId = this.pageQueue.shift();
321
344
  while (pageId) {
322
345
  await this.loadPage(pageId);
@@ -1,7 +1,22 @@
1
- import { Client } from "@notionhq/client";
1
+ import { APIResponseError, Client, isFullBlock, isFullPage, isFullDatabase } from "@notionhq/client";
2
2
  import { Document } from "../../document.js";
3
3
  import { BaseDocumentLoader } from "../base.js";
4
4
  import { AsyncCaller } from "../../util/async_caller.js";
5
+ type GuardType<T> = T extends (x: any, ...rest: any) => x is infer U ? U : never;
6
+ export type GetBlockResponse = Parameters<typeof isFullBlock>[0];
7
+ export type GetPageResponse = Parameters<typeof isFullPage>[0];
8
+ export type GetDatabaseResponse = Parameters<typeof isFullDatabase>[0];
9
+ export type BlockObjectResponse = GuardType<typeof isFullBlock>;
10
+ export type PageObjectResponse = GuardType<typeof isFullPage>;
11
+ export type DatabaseObjectResponse = GuardType<typeof isFullDatabase>;
12
+ export type GetResponse = GetBlockResponse | GetPageResponse | GetDatabaseResponse | APIResponseError;
13
+ export type PagePropertiesType = PageObjectResponse["properties"];
14
+ export type PagePropertiesValue = PagePropertiesType[keyof PagePropertiesType];
15
+ export declare const isPageResponse: (res: GetResponse) => res is import("@notionhq/client/build/src/api-endpoints.js").PageObjectResponse | import("@notionhq/client/build/src/api-endpoints.js").PartialPageObjectResponse;
16
+ export declare const isDatabaseResponse: (res: GetResponse) => res is import("@notionhq/client/build/src/api-endpoints.js").DatabaseObjectResponse | import("@notionhq/client/build/src/api-endpoints.js").PartialDatabaseObjectResponse;
17
+ export declare const isErrorResponse: (res: GetResponse) => res is APIResponseError;
18
+ export declare const isPage: (res: GetResponse) => res is import("@notionhq/client/build/src/api-endpoints.js").PageObjectResponse;
19
+ export declare const isDatabase: (res: GetResponse) => res is import("@notionhq/client/build/src/api-endpoints.js").DatabaseObjectResponse;
5
20
  /**
6
21
  * Represents the type of Notion API to load documents from. The options
7
22
  * are "database" or "page".
@@ -31,7 +46,23 @@ export declare class NotionAPILoader extends BaseDocumentLoader {
31
46
  private rootTitle;
32
47
  private onDocumentLoaded;
33
48
  constructor(options: NotionAPILoaderOptions);
49
+ /**
50
+ * Adds a selection of page ids to the pageQueue and removes duplicates.
51
+ * @param items An array of string ids
52
+ */
34
53
  private addToQueue;
54
+ /**
55
+ * Parses a Notion GetResponse object (page or database) and returns a string of the title.
56
+ * @param obj The Notion GetResponse object to parse.
57
+ * @returns The string of the title.
58
+ */
59
+ private getTitle;
60
+ /**
61
+ * Parses the property type and returns a string
62
+ * @param page The Notion page property to parse.
63
+ * @returns A string of parsed property.
64
+ */
65
+ private getPropValue;
35
66
  /**
36
67
  * Parses the properties of a Notion page and returns them as key-value
37
68
  * pairs.
@@ -73,3 +104,4 @@ export declare class NotionAPILoader extends BaseDocumentLoader {
73
104
  */
74
105
  load(): Promise<Document[]>;
75
106
  }
107
+ export {};