langchain 0.0.174 → 0.0.176

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 (69) hide show
  1. package/dist/chat_models/googlevertexai/common.cjs +46 -7
  2. package/dist/chat_models/googlevertexai/common.d.ts +7 -2
  3. package/dist/chat_models/googlevertexai/common.js +47 -8
  4. package/dist/chat_models/googlevertexai/index.cjs +4 -3
  5. package/dist/chat_models/googlevertexai/index.js +4 -3
  6. package/dist/chat_models/googlevertexai/web.cjs +2 -1
  7. package/dist/chat_models/googlevertexai/web.js +2 -1
  8. package/dist/embeddings/googlevertexai.cjs +1 -1
  9. package/dist/embeddings/googlevertexai.js +1 -1
  10. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +2 -2
  11. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +1 -1
  12. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
  13. package/dist/experimental/multimodal_embeddings/googlevertexai.js +2 -2
  14. package/dist/experimental/plan_and_execute/agent_executor.cjs +7 -4
  15. package/dist/experimental/plan_and_execute/agent_executor.d.ts +4 -3
  16. package/dist/experimental/plan_and_execute/agent_executor.js +8 -5
  17. package/dist/experimental/plan_and_execute/prompt.cjs +25 -9
  18. package/dist/experimental/plan_and_execute/prompt.d.ts +9 -1
  19. package/dist/experimental/plan_and_execute/prompt.js +23 -8
  20. package/dist/llms/googlevertexai/common.cjs +46 -13
  21. package/dist/llms/googlevertexai/common.d.ts +8 -3
  22. package/dist/llms/googlevertexai/common.js +46 -13
  23. package/dist/llms/googlevertexai/index.cjs +4 -3
  24. package/dist/llms/googlevertexai/index.js +4 -3
  25. package/dist/llms/googlevertexai/web.cjs +2 -1
  26. package/dist/llms/googlevertexai/web.js +2 -1
  27. package/dist/load/import_constants.cjs +4 -0
  28. package/dist/load/import_constants.js +4 -0
  29. package/dist/storage/convex.cjs +145 -0
  30. package/dist/storage/convex.d.ts +85 -0
  31. package/dist/storage/convex.js +141 -0
  32. package/dist/stores/message/convex.cjs +120 -0
  33. package/dist/stores/message/convex.d.ts +60 -0
  34. package/dist/stores/message/convex.js +116 -0
  35. package/dist/types/googlevertexai-types.d.ts +12 -10
  36. package/dist/util/convex.cjs +77 -0
  37. package/dist/util/convex.d.ts +26 -0
  38. package/dist/util/convex.js +74 -0
  39. package/dist/util/googlevertexai-connection.cjs +298 -10
  40. package/dist/util/googlevertexai-connection.d.ts +76 -7
  41. package/dist/util/googlevertexai-connection.js +294 -9
  42. package/dist/util/googlevertexai-gauth.cjs +36 -0
  43. package/dist/util/googlevertexai-gauth.d.ts +8 -0
  44. package/dist/util/googlevertexai-gauth.js +32 -0
  45. package/dist/util/googlevertexai-webauth.cjs +38 -2
  46. package/dist/util/googlevertexai-webauth.d.ts +2 -6
  47. package/dist/util/googlevertexai-webauth.js +38 -2
  48. package/dist/vectorstores/convex.cjs +177 -0
  49. package/dist/vectorstores/convex.d.ts +113 -0
  50. package/dist/vectorstores/convex.js +173 -0
  51. package/dist/vectorstores/googlevertexai.d.ts +4 -4
  52. package/dist/vectorstores/milvus.cjs +4 -2
  53. package/dist/vectorstores/milvus.js +4 -2
  54. package/dist/vectorstores/vercel_postgres.cjs +29 -7
  55. package/dist/vectorstores/vercel_postgres.d.ts +1 -1
  56. package/dist/vectorstores/vercel_postgres.js +29 -7
  57. package/package.json +38 -1
  58. package/storage/convex.cjs +1 -0
  59. package/storage/convex.d.ts +1 -0
  60. package/storage/convex.js +1 -0
  61. package/stores/message/convex.cjs +1 -0
  62. package/stores/message/convex.d.ts +1 -0
  63. package/stores/message/convex.js +1 -0
  64. package/util/convex.cjs +1 -0
  65. package/util/convex.d.ts +1 -0
  66. package/util/convex.js +1 -0
  67. package/vectorstores/convex.cjs +1 -0
  68. package/vectorstores/convex.d.ts +1 -0
  69. package/vectorstores/convex.js +1 -0
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseGoogleVertexAI = void 0;
4
4
  const base_js_1 = require("../base.cjs");
5
+ const index_js_1 = require("../../schema/index.cjs");
5
6
  /**
6
7
  * Base class for Google Vertex AI LLMs.
7
8
  * Implemented subclasses must provide a GoogleVertexAILLMConnection
@@ -57,6 +58,12 @@ class BaseGoogleVertexAI extends base_js_1.BaseLLM {
57
58
  writable: true,
58
59
  value: void 0
59
60
  });
61
+ Object.defineProperty(this, "streamedConnection", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: void 0
66
+ });
60
67
  this.model = fields?.model ?? this.model;
61
68
  // Change the defaults for code models
62
69
  if (this.model.startsWith("code-gecko")) {
@@ -73,26 +80,37 @@ class BaseGoogleVertexAI extends base_js_1.BaseLLM {
73
80
  _llmType() {
74
81
  return "vertexai";
75
82
  }
83
+ async *_streamResponseChunks(_input, _options, _runManager) {
84
+ // Make the call as a streaming request
85
+ const instance = this.formatInstance(_input);
86
+ const parameters = this.formatParameters();
87
+ const result = await this.streamedConnection.request([instance], parameters, _options);
88
+ // Get the streaming parser of the response
89
+ const stream = result.data;
90
+ // Loop until the end of the stream
91
+ // During the loop, yield each time we get a chunk from the streaming parser
92
+ // that is either available or added to the queue
93
+ while (!stream.streamDone) {
94
+ const output = await stream.nextChunk();
95
+ const chunk = output !== null
96
+ ? new index_js_1.GenerationChunk(this.extractGenerationFromPrediction(output.outputs[0]))
97
+ : new index_js_1.GenerationChunk({
98
+ text: "",
99
+ generationInfo: { finishReason: "stop" },
100
+ });
101
+ yield chunk;
102
+ }
103
+ }
76
104
  async _generate(prompts, options) {
77
105
  const generations = await Promise.all(prompts.map((prompt) => this._generatePrompt(prompt, options)));
78
106
  return { generations };
79
107
  }
80
108
  async _generatePrompt(prompt, options) {
81
109
  const instance = this.formatInstance(prompt);
82
- const parameters = {
83
- temperature: this.temperature,
84
- topK: this.topK,
85
- topP: this.topP,
86
- maxOutputTokens: this.maxOutputTokens,
87
- };
110
+ const parameters = this.formatParameters();
88
111
  const result = await this.connection.request([instance], parameters, options);
89
112
  const prediction = this.extractPredictionFromResponse(result);
90
- return [
91
- {
92
- text: prediction.content,
93
- generationInfo: prediction,
94
- },
95
- ];
113
+ return [this.extractGenerationFromPrediction(prediction)];
96
114
  }
97
115
  /**
98
116
  * Formats the input instance as a text instance for the Google Vertex AI
@@ -123,13 +141,28 @@ class BaseGoogleVertexAI extends base_js_1.BaseLLM {
123
141
  ? this.formatInstanceCode(prompt)
124
142
  : this.formatInstanceText(prompt);
125
143
  }
144
+ formatParameters() {
145
+ return {
146
+ temperature: this.temperature,
147
+ topK: this.topK,
148
+ topP: this.topP,
149
+ maxOutputTokens: this.maxOutputTokens,
150
+ };
151
+ }
126
152
  /**
127
153
  * Extracts the prediction from the API response.
128
154
  * @param result The API response from which to extract the prediction.
129
155
  * @returns A TextPrediction object representing the extracted prediction.
130
156
  */
131
157
  extractPredictionFromResponse(result) {
132
- return result?.data?.predictions[0];
158
+ return result?.data
159
+ ?.predictions[0];
160
+ }
161
+ extractGenerationFromPrediction(prediction) {
162
+ return {
163
+ text: prediction.content,
164
+ generationInfo: prediction,
165
+ };
133
166
  }
134
167
  }
135
168
  exports.BaseGoogleVertexAI = BaseGoogleVertexAI;
@@ -1,8 +1,9 @@
1
1
  import { BaseLLM } from "../base.js";
2
- import { Generation, LLMResult } from "../../schema/index.js";
3
- import { GoogleVertexAILLMConnection } from "../../util/googlevertexai-connection.js";
4
- import { GoogleVertexAIBaseLLMInput, GoogleVertexAIBasePrediction, GoogleVertexAILLMResponse } from "../../types/googlevertexai-types.js";
2
+ import { Generation, GenerationChunk, LLMResult } from "../../schema/index.js";
3
+ import { GoogleVertexAILLMConnection, GoogleVertexAILLMResponse } from "../../util/googlevertexai-connection.js";
4
+ import { GoogleVertexAIBaseLLMInput, GoogleVertexAIBasePrediction, GoogleVertexAIModelParams } from "../../types/googlevertexai-types.js";
5
5
  import { BaseLanguageModelCallOptions } from "../../base_language/index.js";
6
+ import { CallbackManagerForLLMRun } from "../../callbacks/index.js";
6
7
  /**
7
8
  * Interface representing the instance of text input to the Google Vertex
8
9
  * AI model.
@@ -41,9 +42,11 @@ export declare class BaseGoogleVertexAI<AuthOptions> extends BaseLLM implements
41
42
  topP: number;
42
43
  topK: number;
43
44
  protected connection: GoogleVertexAILLMConnection<BaseLanguageModelCallOptions, GoogleVertexAILLMInstance, TextPrediction, AuthOptions>;
45
+ protected streamedConnection: GoogleVertexAILLMConnection<BaseLanguageModelCallOptions, GoogleVertexAILLMInstance, TextPrediction, AuthOptions>;
44
46
  get lc_aliases(): Record<string, string>;
45
47
  constructor(fields?: GoogleVertexAIBaseLLMInput<AuthOptions>);
46
48
  _llmType(): string;
49
+ _streamResponseChunks(_input: string, _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): AsyncGenerator<GenerationChunk>;
47
50
  _generate(prompts: string[], options: this["ParsedCallOptions"]): Promise<LLMResult>;
48
51
  _generatePrompt(prompt: string, options: this["ParsedCallOptions"]): Promise<Generation[]>;
49
52
  /**
@@ -67,11 +70,13 @@ export declare class BaseGoogleVertexAI<AuthOptions> extends BaseLLM implements
67
70
  * @returns A GoogleVertexAILLMInstance object representing the formatted instance.
68
71
  */
69
72
  formatInstance(prompt: string): GoogleVertexAILLMInstance;
73
+ formatParameters(): GoogleVertexAIModelParams;
70
74
  /**
71
75
  * Extracts the prediction from the API response.
72
76
  * @param result The API response from which to extract the prediction.
73
77
  * @returns A TextPrediction object representing the extracted prediction.
74
78
  */
75
79
  extractPredictionFromResponse(result: GoogleVertexAILLMResponse<TextPrediction>): TextPrediction;
80
+ extractGenerationFromPrediction(prediction: TextPrediction): Generation;
76
81
  }
77
82
  export {};
@@ -1,4 +1,5 @@
1
1
  import { BaseLLM } from "../base.js";
2
+ import { GenerationChunk } from "../../schema/index.js";
2
3
  /**
3
4
  * Base class for Google Vertex AI LLMs.
4
5
  * Implemented subclasses must provide a GoogleVertexAILLMConnection
@@ -54,6 +55,12 @@ export class BaseGoogleVertexAI extends BaseLLM {
54
55
  writable: true,
55
56
  value: void 0
56
57
  });
58
+ Object.defineProperty(this, "streamedConnection", {
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true,
62
+ value: void 0
63
+ });
57
64
  this.model = fields?.model ?? this.model;
58
65
  // Change the defaults for code models
59
66
  if (this.model.startsWith("code-gecko")) {
@@ -70,26 +77,37 @@ export class BaseGoogleVertexAI extends BaseLLM {
70
77
  _llmType() {
71
78
  return "vertexai";
72
79
  }
80
+ async *_streamResponseChunks(_input, _options, _runManager) {
81
+ // Make the call as a streaming request
82
+ const instance = this.formatInstance(_input);
83
+ const parameters = this.formatParameters();
84
+ const result = await this.streamedConnection.request([instance], parameters, _options);
85
+ // Get the streaming parser of the response
86
+ const stream = result.data;
87
+ // Loop until the end of the stream
88
+ // During the loop, yield each time we get a chunk from the streaming parser
89
+ // that is either available or added to the queue
90
+ while (!stream.streamDone) {
91
+ const output = await stream.nextChunk();
92
+ const chunk = output !== null
93
+ ? new GenerationChunk(this.extractGenerationFromPrediction(output.outputs[0]))
94
+ : new GenerationChunk({
95
+ text: "",
96
+ generationInfo: { finishReason: "stop" },
97
+ });
98
+ yield chunk;
99
+ }
100
+ }
73
101
  async _generate(prompts, options) {
74
102
  const generations = await Promise.all(prompts.map((prompt) => this._generatePrompt(prompt, options)));
75
103
  return { generations };
76
104
  }
77
105
  async _generatePrompt(prompt, options) {
78
106
  const instance = this.formatInstance(prompt);
79
- const parameters = {
80
- temperature: this.temperature,
81
- topK: this.topK,
82
- topP: this.topP,
83
- maxOutputTokens: this.maxOutputTokens,
84
- };
107
+ const parameters = this.formatParameters();
85
108
  const result = await this.connection.request([instance], parameters, options);
86
109
  const prediction = this.extractPredictionFromResponse(result);
87
- return [
88
- {
89
- text: prediction.content,
90
- generationInfo: prediction,
91
- },
92
- ];
110
+ return [this.extractGenerationFromPrediction(prediction)];
93
111
  }
94
112
  /**
95
113
  * Formats the input instance as a text instance for the Google Vertex AI
@@ -120,12 +138,27 @@ export class BaseGoogleVertexAI extends BaseLLM {
120
138
  ? this.formatInstanceCode(prompt)
121
139
  : this.formatInstanceText(prompt);
122
140
  }
141
+ formatParameters() {
142
+ return {
143
+ temperature: this.temperature,
144
+ topK: this.topK,
145
+ topP: this.topP,
146
+ maxOutputTokens: this.maxOutputTokens,
147
+ };
148
+ }
123
149
  /**
124
150
  * Extracts the prediction from the API response.
125
151
  * @param result The API response from which to extract the prediction.
126
152
  * @returns A TextPrediction object representing the extracted prediction.
127
153
  */
128
154
  extractPredictionFromResponse(result) {
129
- return result?.data?.predictions[0];
155
+ return result?.data
156
+ ?.predictions[0];
157
+ }
158
+ extractGenerationFromPrediction(prediction) {
159
+ return {
160
+ text: prediction.content,
161
+ generationInfo: prediction,
162
+ };
130
163
  }
131
164
  }
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GoogleVertexAI = void 0;
4
- const google_auth_library_1 = require("google-auth-library");
5
4
  const googlevertexai_connection_js_1 = require("../../util/googlevertexai-connection.cjs");
6
5
  const common_js_1 = require("./common.cjs");
6
+ const googlevertexai_gauth_js_1 = require("../../util/googlevertexai-gauth.cjs");
7
7
  /**
8
8
  * Enables calls to the Google Cloud's Vertex AI API to access
9
9
  * Large Language Models.
@@ -24,11 +24,12 @@ class GoogleVertexAI extends common_js_1.BaseGoogleVertexAI {
24
24
  }
25
25
  constructor(fields) {
26
26
  super(fields);
27
- const client = new google_auth_library_1.GoogleAuth({
27
+ const client = new googlevertexai_gauth_js_1.GAuthClient({
28
28
  scopes: "https://www.googleapis.com/auth/cloud-platform",
29
29
  ...fields?.authOptions,
30
30
  });
31
- this.connection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
31
+ this.connection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, false);
32
+ this.streamedConnection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, true);
32
33
  }
33
34
  }
34
35
  exports.GoogleVertexAI = GoogleVertexAI;
@@ -1,6 +1,6 @@
1
- import { GoogleAuth } from "google-auth-library";
2
1
  import { GoogleVertexAILLMConnection } from "../../util/googlevertexai-connection.js";
3
2
  import { BaseGoogleVertexAI } from "./common.js";
3
+ import { GAuthClient } from "../../util/googlevertexai-gauth.js";
4
4
  /**
5
5
  * Enables calls to the Google Cloud's Vertex AI API to access
6
6
  * Large Language Models.
@@ -21,10 +21,11 @@ export class GoogleVertexAI extends BaseGoogleVertexAI {
21
21
  }
22
22
  constructor(fields) {
23
23
  super(fields);
24
- const client = new GoogleAuth({
24
+ const client = new GAuthClient({
25
25
  scopes: "https://www.googleapis.com/auth/cloud-platform",
26
26
  ...fields?.authOptions,
27
27
  });
28
- this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
28
+ this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, false);
29
+ this.streamedConnection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, true);
29
30
  }
30
31
  }
@@ -25,7 +25,8 @@ class GoogleVertexAI extends common_js_1.BaseGoogleVertexAI {
25
25
  constructor(fields) {
26
26
  super(fields);
27
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);
28
+ this.connection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, false);
29
+ this.streamedConnection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, true);
29
30
  }
30
31
  }
31
32
  exports.GoogleVertexAI = GoogleVertexAI;
@@ -22,6 +22,7 @@ export class GoogleVertexAI extends BaseGoogleVertexAI {
22
22
  constructor(fields) {
23
23
  super(fields);
24
24
  const client = new WebGoogleAuth(fields?.authOptions);
25
- this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
25
+ this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, false);
26
+ this.streamedConnection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, true);
26
27
  }
27
28
  }
@@ -41,6 +41,7 @@ exports.optionalImportEntrypoints = [
41
41
  "langchain/prompts/load",
42
42
  "langchain/vectorstores/analyticdb",
43
43
  "langchain/vectorstores/cassandra",
44
+ "langchain/vectorstores/convex",
44
45
  "langchain/vectorstores/elasticsearch",
45
46
  "langchain/vectorstores/cloudflare_vectorize",
46
47
  "langchain/vectorstores/closevector/web",
@@ -133,6 +134,7 @@ exports.optionalImportEntrypoints = [
133
134
  "langchain/cache/upstash_redis",
134
135
  "langchain/stores/doc/gcs",
135
136
  "langchain/stores/file/node",
137
+ "langchain/stores/message/convex",
136
138
  "langchain/stores/message/cloudflare_d1",
137
139
  "langchain/stores/message/dynamodb",
138
140
  "langchain/stores/message/firestore",
@@ -143,12 +145,14 @@ exports.optionalImportEntrypoints = [
143
145
  "langchain/stores/message/upstash_redis",
144
146
  "langchain/stores/message/planetscale",
145
147
  "langchain/stores/message/xata",
148
+ "langchain/storage/convex",
146
149
  "langchain/storage/ioredis",
147
150
  "langchain/storage/vercel_kv",
148
151
  "langchain/storage/upstash_redis",
149
152
  "langchain/storage/file_system",
150
153
  "langchain/graphs/neo4j_graph",
151
154
  "langchain/hub",
155
+ "langchain/util/convex",
152
156
  "langchain/experimental/multimodal_embeddings/googlevertexai",
153
157
  "langchain/experimental/chat_models/anthropic_functions",
154
158
  "langchain/experimental/llms/bittensor",
@@ -38,6 +38,7 @@ export const optionalImportEntrypoints = [
38
38
  "langchain/prompts/load",
39
39
  "langchain/vectorstores/analyticdb",
40
40
  "langchain/vectorstores/cassandra",
41
+ "langchain/vectorstores/convex",
41
42
  "langchain/vectorstores/elasticsearch",
42
43
  "langchain/vectorstores/cloudflare_vectorize",
43
44
  "langchain/vectorstores/closevector/web",
@@ -130,6 +131,7 @@ export const optionalImportEntrypoints = [
130
131
  "langchain/cache/upstash_redis",
131
132
  "langchain/stores/doc/gcs",
132
133
  "langchain/stores/file/node",
134
+ "langchain/stores/message/convex",
133
135
  "langchain/stores/message/cloudflare_d1",
134
136
  "langchain/stores/message/dynamodb",
135
137
  "langchain/stores/message/firestore",
@@ -140,12 +142,14 @@ export const optionalImportEntrypoints = [
140
142
  "langchain/stores/message/upstash_redis",
141
143
  "langchain/stores/message/planetscale",
142
144
  "langchain/stores/message/xata",
145
+ "langchain/storage/convex",
143
146
  "langchain/storage/ioredis",
144
147
  "langchain/storage/vercel_kv",
145
148
  "langchain/storage/upstash_redis",
146
149
  "langchain/storage/file_system",
147
150
  "langchain/graphs/neo4j_graph",
148
151
  "langchain/hub",
152
+ "langchain/util/convex",
149
153
  "langchain/experimental/multimodal_embeddings/googlevertexai",
150
154
  "langchain/experimental/chat_models/anthropic_functions",
151
155
  "langchain/experimental/llms/bittensor",
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConvexKVStore = void 0;
4
+ // eslint-disable-next-line import/no-extraneous-dependencies
5
+ const server_1 = require("convex/server");
6
+ const storage_js_1 = require("../schema/storage.cjs");
7
+ /**
8
+ * Class that extends the BaseStore class to interact with a Convex
9
+ * database. It provides methods for getting, setting, and deleting key value pairs,
10
+ * as well as yielding keys from the database.
11
+ */
12
+ class ConvexKVStore extends storage_js_1.BaseStore {
13
+ constructor(config) {
14
+ super(config);
15
+ Object.defineProperty(this, "lc_namespace", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: ["langchain", "storage", "convex"]
20
+ });
21
+ Object.defineProperty(this, "ctx", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "table", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "index", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "keyField", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "valueField", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "upsert", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "lookup", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ Object.defineProperty(this, "deleteMany", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: void 0
68
+ });
69
+ this.ctx = config.ctx;
70
+ this.table = config.table ?? "cache";
71
+ this.index = config.index ?? "byKey";
72
+ this.keyField = config.keyField ?? "key";
73
+ this.valueField = config.valueField ?? "value";
74
+ this.upsert =
75
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
+ config.upsert ?? (0, server_1.makeFunctionReference)("langchain/db:upsert");
77
+ this.lookup =
78
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
+ config.lookup ?? (0, server_1.makeFunctionReference)("langchain/db:lookup");
80
+ this.deleteMany =
81
+ config.deleteMany ??
82
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
+ (0, server_1.makeFunctionReference)("langchain/db:deleteMany");
84
+ }
85
+ /**
86
+ * Gets multiple keys from the Convex database.
87
+ * @param keys Array of keys to be retrieved.
88
+ * @returns An array of retrieved values.
89
+ */
90
+ async mget(keys) {
91
+ return (await Promise.all(keys.map(async (key) => {
92
+ const found = (await this.ctx.runQuery(this.lookup, {
93
+ table: this.table,
94
+ index: this.index,
95
+ keyField: this.keyField,
96
+ key,
97
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
+ }));
99
+ return found.length > 0 ? found[0][this.valueField] : undefined;
100
+ })));
101
+ }
102
+ /**
103
+ * Sets multiple keys in the Convex database.
104
+ * @param keyValuePairs Array of key-value pairs to be set.
105
+ * @returns Promise that resolves when all keys have been set.
106
+ */
107
+ async mset(keyValuePairs) {
108
+ // TODO: Remove chunking when Convex handles the concurrent requests correctly
109
+ const PAGE_SIZE = 16;
110
+ for (let i = 0; i < keyValuePairs.length; i += PAGE_SIZE) {
111
+ await Promise.all(keyValuePairs.slice(i, i + PAGE_SIZE).map(([key, value]) => this.ctx.runMutation(this.upsert, {
112
+ table: this.table,
113
+ index: this.index,
114
+ keyField: this.keyField,
115
+ key,
116
+ document: { [this.keyField]: key, [this.valueField]: value },
117
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
118
+ })));
119
+ }
120
+ }
121
+ /**
122
+ * Deletes multiple keys from the Convex database.
123
+ * @param keys Array of keys to be deleted.
124
+ * @returns Promise that resolves when all keys have been deleted.
125
+ */
126
+ async mdelete(keys) {
127
+ await Promise.all(keys.map((key) => this.ctx.runMutation(this.deleteMany, {
128
+ table: this.table,
129
+ index: this.index,
130
+ keyField: this.keyField,
131
+ key,
132
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
133
+ })));
134
+ }
135
+ /**
136
+ * Yields keys from the Convex database.
137
+ * @param prefix Optional prefix to filter the keys.
138
+ * @returns An AsyncGenerator that yields keys from the Convex database.
139
+ */
140
+ // eslint-disable-next-line require-yield
141
+ async *yieldKeys(_prefix) {
142
+ throw new Error("yieldKeys not implemented yet for ConvexKVStore");
143
+ }
144
+ }
145
+ exports.ConvexKVStore = ConvexKVStore;
@@ -0,0 +1,85 @@
1
+ import { FieldPaths, FunctionReference, GenericActionCtx, GenericDataModel, NamedTableInfo, TableNamesInDataModel, VectorIndexNames } from "convex/server";
2
+ import { Value } from "convex/values";
3
+ import { BaseStore } from "../schema/storage.js";
4
+ /**
5
+ * Type that defines the config required to initialize the
6
+ * ConvexKVStore class. It includes the table name,
7
+ * index name, field name.
8
+ */
9
+ export type ConvexKVStoreConfig<DataModel extends GenericDataModel, TableName extends TableNamesInDataModel<DataModel>, IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>>, KeyFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, ValueFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, UpsertMutation extends FunctionReference<"mutation", "internal", {
10
+ table: string;
11
+ document: object;
12
+ }>, LookupQuery extends FunctionReference<"query", "internal", {
13
+ table: string;
14
+ index: string;
15
+ keyField: string;
16
+ key: string;
17
+ }, object[]>, DeleteManyMutation extends FunctionReference<"mutation", "internal", {
18
+ table: string;
19
+ index: string;
20
+ keyField: string;
21
+ key: string;
22
+ }>> = {
23
+ readonly ctx: GenericActionCtx<DataModel>;
24
+ readonly table?: TableName;
25
+ readonly index?: IndexName;
26
+ readonly keyField?: KeyFieldName;
27
+ readonly valueField?: ValueFieldName;
28
+ readonly upsert?: UpsertMutation;
29
+ readonly lookup?: LookupQuery;
30
+ readonly deleteMany?: DeleteManyMutation;
31
+ };
32
+ /**
33
+ * Class that extends the BaseStore class to interact with a Convex
34
+ * database. It provides methods for getting, setting, and deleting key value pairs,
35
+ * as well as yielding keys from the database.
36
+ */
37
+ export declare class ConvexKVStore<T extends Value, DataModel extends GenericDataModel, TableName extends TableNamesInDataModel<DataModel>, IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>>, KeyFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, ValueFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, UpsertMutation extends FunctionReference<"mutation", "internal", {
38
+ table: string;
39
+ document: object;
40
+ }>, LookupQuery extends FunctionReference<"query", "internal", {
41
+ table: string;
42
+ index: string;
43
+ keyField: string;
44
+ key: string;
45
+ }, object[]>, DeleteManyMutation extends FunctionReference<"mutation", "internal", {
46
+ table: string;
47
+ index: string;
48
+ keyField: string;
49
+ key: string;
50
+ }>> extends BaseStore<string, T> {
51
+ lc_namespace: string[];
52
+ private readonly ctx;
53
+ private readonly table;
54
+ private readonly index;
55
+ private readonly keyField;
56
+ private readonly valueField;
57
+ private readonly upsert;
58
+ private readonly lookup;
59
+ private readonly deleteMany;
60
+ constructor(config: ConvexKVStoreConfig<DataModel, TableName, IndexName, KeyFieldName, ValueFieldName, UpsertMutation, LookupQuery, DeleteManyMutation>);
61
+ /**
62
+ * Gets multiple keys from the Convex database.
63
+ * @param keys Array of keys to be retrieved.
64
+ * @returns An array of retrieved values.
65
+ */
66
+ mget(keys: string[]): Promise<(T | undefined)[]>;
67
+ /**
68
+ * Sets multiple keys in the Convex database.
69
+ * @param keyValuePairs Array of key-value pairs to be set.
70
+ * @returns Promise that resolves when all keys have been set.
71
+ */
72
+ mset(keyValuePairs: [string, T][]): Promise<void>;
73
+ /**
74
+ * Deletes multiple keys from the Convex database.
75
+ * @param keys Array of keys to be deleted.
76
+ * @returns Promise that resolves when all keys have been deleted.
77
+ */
78
+ mdelete(keys: string[]): Promise<void>;
79
+ /**
80
+ * Yields keys from the Convex database.
81
+ * @param prefix Optional prefix to filter the keys.
82
+ * @returns An AsyncGenerator that yields keys from the Convex database.
83
+ */
84
+ yieldKeys(_prefix?: string): AsyncGenerator<string>;
85
+ }