@langchain/core 0.1.58 → 0.1.60
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/chat_history.cjs +49 -1
- package/dist/chat_history.d.ts +27 -0
- package/dist/chat_history.js +47 -0
- package/dist/output_parsers/structured.cjs +7 -1
- package/dist/output_parsers/structured.js +7 -1
- package/dist/runnables/base.cjs +6 -2
- package/dist/runnables/base.d.ts +2 -2
- package/dist/runnables/base.js +6 -2
- package/dist/stores.cjs +81 -1
- package/dist/stores.d.ts +48 -0
- package/dist/stores.js +79 -0
- package/dist/structured_query/base.cjs +139 -0
- package/dist/structured_query/base.d.ts +69 -0
- package/dist/structured_query/base.js +134 -0
- package/dist/structured_query/functional.cjs +198 -0
- package/dist/structured_query/functional.d.ts +87 -0
- package/dist/structured_query/functional.js +194 -0
- package/dist/structured_query/index.cjs +20 -0
- package/dist/structured_query/index.d.ts +4 -0
- package/dist/structured_query/index.js +4 -0
- package/dist/structured_query/ir.cjs +141 -0
- package/dist/structured_query/ir.d.ts +138 -0
- package/dist/structured_query/ir.js +132 -0
- package/dist/structured_query/utils.cjs +94 -0
- package/dist/structured_query/utils.d.ts +29 -0
- package/dist/structured_query/utils.js +85 -0
- package/dist/tools.cjs +12 -1
- package/dist/tools.d.ts +9 -0
- package/dist/tools.js +10 -0
- package/package.json +14 -1
- package/structured_query.cjs +1 -0
- package/structured_query.d.cts +1 -0
- package/structured_query.d.ts +1 -0
- package/structured_query.js +1 -0
package/dist/chat_history.cjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = void 0;
|
|
3
|
+
exports.InMemoryChatMessageHistory = exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = void 0;
|
|
4
4
|
const serializable_js_1 = require("./load/serializable.cjs");
|
|
5
5
|
const index_js_1 = require("./messages/index.cjs");
|
|
6
|
+
// TODO: Combine into one class for 0.2
|
|
6
7
|
/**
|
|
7
8
|
* Base class for all chat message histories. All chat message histories
|
|
8
9
|
* should extend this class.
|
|
@@ -60,3 +61,50 @@ class BaseListChatMessageHistory extends serializable_js_1.Serializable {
|
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
exports.BaseListChatMessageHistory = BaseListChatMessageHistory;
|
|
64
|
+
/**
|
|
65
|
+
* Class for storing chat message history in-memory. It extends the
|
|
66
|
+
* BaseListChatMessageHistory class and provides methods to get, add, and
|
|
67
|
+
* clear messages.
|
|
68
|
+
*/
|
|
69
|
+
class InMemoryChatMessageHistory extends BaseListChatMessageHistory {
|
|
70
|
+
constructor(messages) {
|
|
71
|
+
super(...arguments);
|
|
72
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
configurable: true,
|
|
75
|
+
writable: true,
|
|
76
|
+
value: ["langchain", "stores", "message", "in_memory"]
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(this, "messages", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
configurable: true,
|
|
81
|
+
writable: true,
|
|
82
|
+
value: []
|
|
83
|
+
});
|
|
84
|
+
this.messages = messages ?? [];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Method to get all the messages stored in the ChatMessageHistory
|
|
88
|
+
* instance.
|
|
89
|
+
* @returns Array of stored BaseMessage instances.
|
|
90
|
+
*/
|
|
91
|
+
async getMessages() {
|
|
92
|
+
return this.messages;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Method to add a new message to the ChatMessageHistory instance.
|
|
96
|
+
* @param message The BaseMessage instance to add.
|
|
97
|
+
* @returns A promise that resolves when the message has been added.
|
|
98
|
+
*/
|
|
99
|
+
async addMessage(message) {
|
|
100
|
+
this.messages.push(message);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Method to clear all the messages from the ChatMessageHistory instance.
|
|
104
|
+
* @returns A promise that resolves when all messages have been cleared.
|
|
105
|
+
*/
|
|
106
|
+
async clear() {
|
|
107
|
+
this.messages = [];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.InMemoryChatMessageHistory = InMemoryChatMessageHistory;
|
package/dist/chat_history.d.ts
CHANGED
|
@@ -54,3 +54,30 @@ export declare abstract class BaseListChatMessageHistory extends Serializable {
|
|
|
54
54
|
*/
|
|
55
55
|
clear(): Promise<void>;
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Class for storing chat message history in-memory. It extends the
|
|
59
|
+
* BaseListChatMessageHistory class and provides methods to get, add, and
|
|
60
|
+
* clear messages.
|
|
61
|
+
*/
|
|
62
|
+
export declare class InMemoryChatMessageHistory extends BaseListChatMessageHistory {
|
|
63
|
+
lc_namespace: string[];
|
|
64
|
+
private messages;
|
|
65
|
+
constructor(messages?: BaseMessage[]);
|
|
66
|
+
/**
|
|
67
|
+
* Method to get all the messages stored in the ChatMessageHistory
|
|
68
|
+
* instance.
|
|
69
|
+
* @returns Array of stored BaseMessage instances.
|
|
70
|
+
*/
|
|
71
|
+
getMessages(): Promise<BaseMessage[]>;
|
|
72
|
+
/**
|
|
73
|
+
* Method to add a new message to the ChatMessageHistory instance.
|
|
74
|
+
* @param message The BaseMessage instance to add.
|
|
75
|
+
* @returns A promise that resolves when the message has been added.
|
|
76
|
+
*/
|
|
77
|
+
addMessage(message: BaseMessage): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Method to clear all the messages from the ChatMessageHistory instance.
|
|
80
|
+
* @returns A promise that resolves when all messages have been cleared.
|
|
81
|
+
*/
|
|
82
|
+
clear(): Promise<void>;
|
|
83
|
+
}
|
package/dist/chat_history.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Serializable } from "./load/serializable.js";
|
|
2
2
|
import { HumanMessage, AIMessage } from "./messages/index.js";
|
|
3
|
+
// TODO: Combine into one class for 0.2
|
|
3
4
|
/**
|
|
4
5
|
* Base class for all chat message histories. All chat message histories
|
|
5
6
|
* should extend this class.
|
|
@@ -55,3 +56,49 @@ export class BaseListChatMessageHistory extends Serializable {
|
|
|
55
56
|
throw new Error("Not implemented.");
|
|
56
57
|
}
|
|
57
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Class for storing chat message history in-memory. It extends the
|
|
61
|
+
* BaseListChatMessageHistory class and provides methods to get, add, and
|
|
62
|
+
* clear messages.
|
|
63
|
+
*/
|
|
64
|
+
export class InMemoryChatMessageHistory extends BaseListChatMessageHistory {
|
|
65
|
+
constructor(messages) {
|
|
66
|
+
super(...arguments);
|
|
67
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: ["langchain", "stores", "message", "in_memory"]
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(this, "messages", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: []
|
|
78
|
+
});
|
|
79
|
+
this.messages = messages ?? [];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Method to get all the messages stored in the ChatMessageHistory
|
|
83
|
+
* instance.
|
|
84
|
+
* @returns Array of stored BaseMessage instances.
|
|
85
|
+
*/
|
|
86
|
+
async getMessages() {
|
|
87
|
+
return this.messages;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Method to add a new message to the ChatMessageHistory instance.
|
|
91
|
+
* @param message The BaseMessage instance to add.
|
|
92
|
+
* @returns A promise that resolves when the message has been added.
|
|
93
|
+
*/
|
|
94
|
+
async addMessage(message) {
|
|
95
|
+
this.messages.push(message);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Method to clear all the messages from the ChatMessageHistory instance.
|
|
99
|
+
* @returns A promise that resolves when all messages have been cleared.
|
|
100
|
+
*/
|
|
101
|
+
async clear() {
|
|
102
|
+
this.messages = [];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -77,7 +77,13 @@ ${JSON.stringify((0, zod_to_json_schema_1.zodToJsonSchema)(this.schema))}
|
|
|
77
77
|
const json = text.includes("```")
|
|
78
78
|
? text.trim().split(/```(?:json)?/)[1]
|
|
79
79
|
: text.trim();
|
|
80
|
-
|
|
80
|
+
const escapedJson = json
|
|
81
|
+
.replace(/"([^"\\]*(\\.[^"\\]*)*)"/g, (_match, capturedGroup) => {
|
|
82
|
+
const escapedInsideQuotes = capturedGroup.replace(/\n/g, "\\n");
|
|
83
|
+
return `"${escapedInsideQuotes}"`;
|
|
84
|
+
})
|
|
85
|
+
.replace(/\n/g, "");
|
|
86
|
+
return await this.schema.parseAsync(JSON.parse(escapedJson));
|
|
81
87
|
}
|
|
82
88
|
catch (e) {
|
|
83
89
|
throw new base_js_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
|
|
@@ -74,7 +74,13 @@ ${JSON.stringify(zodToJsonSchema(this.schema))}
|
|
|
74
74
|
const json = text.includes("```")
|
|
75
75
|
? text.trim().split(/```(?:json)?/)[1]
|
|
76
76
|
: text.trim();
|
|
77
|
-
|
|
77
|
+
const escapedJson = json
|
|
78
|
+
.replace(/"([^"\\]*(\\.[^"\\]*)*)"/g, (_match, capturedGroup) => {
|
|
79
|
+
const escapedInsideQuotes = capturedGroup.replace(/\n/g, "\\n");
|
|
80
|
+
return `"${escapedInsideQuotes}"`;
|
|
81
|
+
})
|
|
82
|
+
.replace(/\n/g, "");
|
|
83
|
+
return await this.schema.parseAsync(JSON.parse(escapedJson));
|
|
78
84
|
}
|
|
79
85
|
catch (e) {
|
|
80
86
|
throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
|
package/dist/runnables/base.cjs
CHANGED
|
@@ -927,7 +927,8 @@ class RunnableRetry extends RunnableBinding {
|
|
|
927
927
|
}
|
|
928
928
|
async _invoke(input, config, runManager) {
|
|
929
929
|
return (0, p_retry_1.default)((attemptNumber) => super.invoke(input, this._patchConfigForRetry(attemptNumber, config, runManager)), {
|
|
930
|
-
|
|
930
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
931
|
+
onFailedAttempt: (error) => this.onFailedAttempt(error, input),
|
|
931
932
|
retries: Math.max(this.maxAttemptNumber - 1, 0),
|
|
932
933
|
randomize: true,
|
|
933
934
|
});
|
|
@@ -968,6 +969,8 @@ class RunnableRetry extends RunnableBinding {
|
|
|
968
969
|
if (result instanceof Error) {
|
|
969
970
|
if (firstException === undefined) {
|
|
970
971
|
firstException = result;
|
|
972
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
973
|
+
firstException.input = remainingInputs[i];
|
|
971
974
|
}
|
|
972
975
|
}
|
|
973
976
|
resultsMap[resultMapIndex.toString()] = result;
|
|
@@ -977,7 +980,8 @@ class RunnableRetry extends RunnableBinding {
|
|
|
977
980
|
}
|
|
978
981
|
return results;
|
|
979
982
|
}, {
|
|
980
|
-
|
|
983
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
984
|
+
onFailedAttempt: (error) => this.onFailedAttempt(error, error.input),
|
|
981
985
|
retries: Math.max(this.maxAttemptNumber - 1, 0),
|
|
982
986
|
randomize: true,
|
|
983
987
|
});
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export type RunnableMapLike<RunInput, RunOutput> = {
|
|
|
16
16
|
[K in keyof RunOutput]: RunnableLike<RunInput, RunOutput[K]>;
|
|
17
17
|
};
|
|
18
18
|
export type RunnableLike<RunInput = any, RunOutput = any> = RunnableInterface<RunInput, RunOutput> | RunnableFunc<RunInput, RunOutput> | RunnableMapLike<RunInput, RunOutput>;
|
|
19
|
-
export type RunnableRetryFailedAttemptHandler = (error: any) => any;
|
|
19
|
+
export type RunnableRetryFailedAttemptHandler = (error: any, input: any) => any;
|
|
20
20
|
export declare function _coerceToDict(value: any, defaultKey: string): any;
|
|
21
21
|
/**
|
|
22
22
|
* A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
|
|
@@ -332,7 +332,7 @@ export declare class RunnableRetry<RunInput = any, RunOutput = any, CallOptions
|
|
|
332
332
|
static lc_name(): string;
|
|
333
333
|
lc_namespace: string[];
|
|
334
334
|
protected maxAttemptNumber: number;
|
|
335
|
-
onFailedAttempt
|
|
335
|
+
onFailedAttempt: RunnableRetryFailedAttemptHandler;
|
|
336
336
|
constructor(fields: RunnableBindingArgs<RunInput, RunOutput, CallOptions> & {
|
|
337
337
|
maxAttemptNumber?: number;
|
|
338
338
|
onFailedAttempt?: RunnableRetryFailedAttemptHandler;
|
package/dist/runnables/base.js
CHANGED
|
@@ -917,7 +917,8 @@ export class RunnableRetry extends RunnableBinding {
|
|
|
917
917
|
}
|
|
918
918
|
async _invoke(input, config, runManager) {
|
|
919
919
|
return pRetry((attemptNumber) => super.invoke(input, this._patchConfigForRetry(attemptNumber, config, runManager)), {
|
|
920
|
-
|
|
920
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
921
|
+
onFailedAttempt: (error) => this.onFailedAttempt(error, input),
|
|
921
922
|
retries: Math.max(this.maxAttemptNumber - 1, 0),
|
|
922
923
|
randomize: true,
|
|
923
924
|
});
|
|
@@ -958,6 +959,8 @@ export class RunnableRetry extends RunnableBinding {
|
|
|
958
959
|
if (result instanceof Error) {
|
|
959
960
|
if (firstException === undefined) {
|
|
960
961
|
firstException = result;
|
|
962
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
963
|
+
firstException.input = remainingInputs[i];
|
|
961
964
|
}
|
|
962
965
|
}
|
|
963
966
|
resultsMap[resultMapIndex.toString()] = result;
|
|
@@ -967,7 +970,8 @@ export class RunnableRetry extends RunnableBinding {
|
|
|
967
970
|
}
|
|
968
971
|
return results;
|
|
969
972
|
}, {
|
|
970
|
-
|
|
973
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
974
|
+
onFailedAttempt: (error) => this.onFailedAttempt(error, error.input),
|
|
971
975
|
retries: Math.max(this.maxAttemptNumber - 1, 0),
|
|
972
976
|
randomize: true,
|
|
973
977
|
});
|
package/dist/stores.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseStore = void 0;
|
|
3
|
+
exports.InMemoryStore = exports.BaseStore = void 0;
|
|
4
4
|
const serializable_js_1 = require("./load/serializable.cjs");
|
|
5
5
|
/**
|
|
6
6
|
* Abstract interface for a key-value store.
|
|
@@ -8,3 +8,83 @@ const serializable_js_1 = require("./load/serializable.cjs");
|
|
|
8
8
|
class BaseStore extends serializable_js_1.Serializable {
|
|
9
9
|
}
|
|
10
10
|
exports.BaseStore = BaseStore;
|
|
11
|
+
/**
|
|
12
|
+
* In-memory implementation of the BaseStore using a dictionary. Used for
|
|
13
|
+
* storing key-value pairs in memory.
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const store = new InMemoryStore<BaseMessage>();
|
|
17
|
+
* await store.mset(
|
|
18
|
+
* Array.from({ length: 5 }).map((_, index) => [
|
|
19
|
+
* `message:id:${index}`,
|
|
20
|
+
* index % 2 === 0
|
|
21
|
+
* ? new AIMessage("ai stuff...")
|
|
22
|
+
* : new HumanMessage("human stuff..."),
|
|
23
|
+
* ]),
|
|
24
|
+
* );
|
|
25
|
+
*
|
|
26
|
+
* const retrievedMessages = await store.mget(["message:id:0", "message:id:1"]);
|
|
27
|
+
* await store.mdelete(await store.yieldKeys("message:id:").toArray());
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
class InMemoryStore extends BaseStore {
|
|
32
|
+
constructor() {
|
|
33
|
+
super(...arguments);
|
|
34
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: ["langchain", "storage"]
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(this, "store", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: {}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves the values associated with the given keys from the store.
|
|
49
|
+
* @param keys Keys to retrieve values for.
|
|
50
|
+
* @returns Array of values associated with the given keys.
|
|
51
|
+
*/
|
|
52
|
+
async mget(keys) {
|
|
53
|
+
return keys.map((key) => this.store[key]);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Sets the values for the given keys in the store.
|
|
57
|
+
* @param keyValuePairs Array of key-value pairs to set in the store.
|
|
58
|
+
* @returns Promise that resolves when all key-value pairs have been set.
|
|
59
|
+
*/
|
|
60
|
+
async mset(keyValuePairs) {
|
|
61
|
+
for (const [key, value] of keyValuePairs) {
|
|
62
|
+
this.store[key] = value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Deletes the given keys and their associated values from the store.
|
|
67
|
+
* @param keys Keys to delete from the store.
|
|
68
|
+
* @returns Promise that resolves when all keys have been deleted.
|
|
69
|
+
*/
|
|
70
|
+
async mdelete(keys) {
|
|
71
|
+
for (const key of keys) {
|
|
72
|
+
delete this.store[key];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Asynchronous generator that yields keys from the store. If a prefix is
|
|
77
|
+
* provided, it only yields keys that start with the prefix.
|
|
78
|
+
* @param prefix Optional prefix to filter keys.
|
|
79
|
+
* @returns AsyncGenerator that yields keys from the store.
|
|
80
|
+
*/
|
|
81
|
+
async *yieldKeys(prefix) {
|
|
82
|
+
const keys = Object.keys(this.store);
|
|
83
|
+
for (const key of keys) {
|
|
84
|
+
if (prefix === undefined || key.startsWith(prefix)) {
|
|
85
|
+
yield key;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.InMemoryStore = InMemoryStore;
|
package/dist/stores.d.ts
CHANGED
|
@@ -55,3 +55,51 @@ export declare abstract class BaseStore<K, V> extends Serializable implements Ba
|
|
|
55
55
|
*/
|
|
56
56
|
abstract yieldKeys(prefix?: string): AsyncGenerator<K | string>;
|
|
57
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* In-memory implementation of the BaseStore using a dictionary. Used for
|
|
60
|
+
* storing key-value pairs in memory.
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const store = new InMemoryStore<BaseMessage>();
|
|
64
|
+
* await store.mset(
|
|
65
|
+
* Array.from({ length: 5 }).map((_, index) => [
|
|
66
|
+
* `message:id:${index}`,
|
|
67
|
+
* index % 2 === 0
|
|
68
|
+
* ? new AIMessage("ai stuff...")
|
|
69
|
+
* : new HumanMessage("human stuff..."),
|
|
70
|
+
* ]),
|
|
71
|
+
* );
|
|
72
|
+
*
|
|
73
|
+
* const retrievedMessages = await store.mget(["message:id:0", "message:id:1"]);
|
|
74
|
+
* await store.mdelete(await store.yieldKeys("message:id:").toArray());
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare class InMemoryStore<T = any> extends BaseStore<string, T> {
|
|
78
|
+
lc_namespace: string[];
|
|
79
|
+
protected store: Record<string, T>;
|
|
80
|
+
/**
|
|
81
|
+
* Retrieves the values associated with the given keys from the store.
|
|
82
|
+
* @param keys Keys to retrieve values for.
|
|
83
|
+
* @returns Array of values associated with the given keys.
|
|
84
|
+
*/
|
|
85
|
+
mget(keys: string[]): Promise<T[]>;
|
|
86
|
+
/**
|
|
87
|
+
* Sets the values for the given keys in the store.
|
|
88
|
+
* @param keyValuePairs Array of key-value pairs to set in the store.
|
|
89
|
+
* @returns Promise that resolves when all key-value pairs have been set.
|
|
90
|
+
*/
|
|
91
|
+
mset(keyValuePairs: [string, T][]): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Deletes the given keys and their associated values from the store.
|
|
94
|
+
* @param keys Keys to delete from the store.
|
|
95
|
+
* @returns Promise that resolves when all keys have been deleted.
|
|
96
|
+
*/
|
|
97
|
+
mdelete(keys: string[]): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Asynchronous generator that yields keys from the store. If a prefix is
|
|
100
|
+
* provided, it only yields keys that start with the prefix.
|
|
101
|
+
* @param prefix Optional prefix to filter keys.
|
|
102
|
+
* @returns AsyncGenerator that yields keys from the store.
|
|
103
|
+
*/
|
|
104
|
+
yieldKeys(prefix?: string | undefined): AsyncGenerator<string>;
|
|
105
|
+
}
|
package/dist/stores.js
CHANGED
|
@@ -4,3 +4,82 @@ import { Serializable } from "./load/serializable.js";
|
|
|
4
4
|
*/
|
|
5
5
|
export class BaseStore extends Serializable {
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* In-memory implementation of the BaseStore using a dictionary. Used for
|
|
9
|
+
* storing key-value pairs in memory.
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const store = new InMemoryStore<BaseMessage>();
|
|
13
|
+
* await store.mset(
|
|
14
|
+
* Array.from({ length: 5 }).map((_, index) => [
|
|
15
|
+
* `message:id:${index}`,
|
|
16
|
+
* index % 2 === 0
|
|
17
|
+
* ? new AIMessage("ai stuff...")
|
|
18
|
+
* : new HumanMessage("human stuff..."),
|
|
19
|
+
* ]),
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* const retrievedMessages = await store.mget(["message:id:0", "message:id:1"]);
|
|
23
|
+
* await store.mdelete(await store.yieldKeys("message:id:").toArray());
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
+
export class InMemoryStore extends BaseStore {
|
|
28
|
+
constructor() {
|
|
29
|
+
super(...arguments);
|
|
30
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: ["langchain", "storage"]
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(this, "store", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: {}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Retrieves the values associated with the given keys from the store.
|
|
45
|
+
* @param keys Keys to retrieve values for.
|
|
46
|
+
* @returns Array of values associated with the given keys.
|
|
47
|
+
*/
|
|
48
|
+
async mget(keys) {
|
|
49
|
+
return keys.map((key) => this.store[key]);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Sets the values for the given keys in the store.
|
|
53
|
+
* @param keyValuePairs Array of key-value pairs to set in the store.
|
|
54
|
+
* @returns Promise that resolves when all key-value pairs have been set.
|
|
55
|
+
*/
|
|
56
|
+
async mset(keyValuePairs) {
|
|
57
|
+
for (const [key, value] of keyValuePairs) {
|
|
58
|
+
this.store[key] = value;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Deletes the given keys and their associated values from the store.
|
|
63
|
+
* @param keys Keys to delete from the store.
|
|
64
|
+
* @returns Promise that resolves when all keys have been deleted.
|
|
65
|
+
*/
|
|
66
|
+
async mdelete(keys) {
|
|
67
|
+
for (const key of keys) {
|
|
68
|
+
delete this.store[key];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Asynchronous generator that yields keys from the store. If a prefix is
|
|
73
|
+
* provided, it only yields keys that start with the prefix.
|
|
74
|
+
* @param prefix Optional prefix to filter keys.
|
|
75
|
+
* @returns AsyncGenerator that yields keys from the store.
|
|
76
|
+
*/
|
|
77
|
+
async *yieldKeys(prefix) {
|
|
78
|
+
const keys = Object.keys(this.store);
|
|
79
|
+
for (const key of keys) {
|
|
80
|
+
if (prefix === undefined || key.startsWith(prefix)) {
|
|
81
|
+
yield key;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BasicTranslator = exports.BaseTranslator = void 0;
|
|
4
|
+
const ir_js_1 = require("./ir.cjs");
|
|
5
|
+
const utils_js_1 = require("./utils.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Abstract class that provides a blueprint for creating specific
|
|
8
|
+
* translator classes. Defines two abstract methods: formatFunction and
|
|
9
|
+
* mergeFilters.
|
|
10
|
+
*/
|
|
11
|
+
class BaseTranslator extends ir_js_1.Visitor {
|
|
12
|
+
}
|
|
13
|
+
exports.BaseTranslator = BaseTranslator;
|
|
14
|
+
/**
|
|
15
|
+
* Class that extends the BaseTranslator class and provides concrete
|
|
16
|
+
* implementations for the abstract methods. Also declares three types:
|
|
17
|
+
* VisitOperationOutput, VisitComparisonOutput, and
|
|
18
|
+
* VisitStructuredQueryOutput, which are used as the return types for the
|
|
19
|
+
* visitOperation, visitComparison, and visitStructuredQuery methods
|
|
20
|
+
* respectively.
|
|
21
|
+
*/
|
|
22
|
+
class BasicTranslator extends BaseTranslator {
|
|
23
|
+
constructor(opts) {
|
|
24
|
+
super();
|
|
25
|
+
Object.defineProperty(this, "allowedOperators", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: void 0
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "allowedComparators", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: void 0
|
|
36
|
+
});
|
|
37
|
+
this.allowedOperators = opts?.allowedOperators ?? [
|
|
38
|
+
ir_js_1.Operators.and,
|
|
39
|
+
ir_js_1.Operators.or,
|
|
40
|
+
];
|
|
41
|
+
this.allowedComparators = opts?.allowedComparators ?? [
|
|
42
|
+
ir_js_1.Comparators.eq,
|
|
43
|
+
ir_js_1.Comparators.ne,
|
|
44
|
+
ir_js_1.Comparators.gt,
|
|
45
|
+
ir_js_1.Comparators.gte,
|
|
46
|
+
ir_js_1.Comparators.lt,
|
|
47
|
+
ir_js_1.Comparators.lte,
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
formatFunction(func) {
|
|
51
|
+
if (func in ir_js_1.Comparators) {
|
|
52
|
+
if (this.allowedComparators.length > 0 &&
|
|
53
|
+
this.allowedComparators.indexOf(func) === -1) {
|
|
54
|
+
throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else if (func in ir_js_1.Operators) {
|
|
58
|
+
if (this.allowedOperators.length > 0 &&
|
|
59
|
+
this.allowedOperators.indexOf(func) === -1) {
|
|
60
|
+
throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
throw new Error("Unknown comparator or operator");
|
|
65
|
+
}
|
|
66
|
+
return `$${func}`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Visits an operation and returns a result.
|
|
70
|
+
* @param operation The operation to visit.
|
|
71
|
+
* @returns The result of visiting the operation.
|
|
72
|
+
*/
|
|
73
|
+
visitOperation(operation) {
|
|
74
|
+
const args = operation.args?.map((arg) => arg.accept(this));
|
|
75
|
+
return {
|
|
76
|
+
[this.formatFunction(operation.operator)]: args,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Visits a comparison and returns a result.
|
|
81
|
+
* @param comparison The comparison to visit.
|
|
82
|
+
* @returns The result of visiting the comparison.
|
|
83
|
+
*/
|
|
84
|
+
visitComparison(comparison) {
|
|
85
|
+
return {
|
|
86
|
+
[comparison.attribute]: {
|
|
87
|
+
[this.formatFunction(comparison.comparator)]: (0, utils_js_1.castValue)(comparison.value),
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Visits a structured query and returns a result.
|
|
93
|
+
* @param query The structured query to visit.
|
|
94
|
+
* @returns The result of visiting the structured query.
|
|
95
|
+
*/
|
|
96
|
+
visitStructuredQuery(query) {
|
|
97
|
+
let nextArg = {};
|
|
98
|
+
if (query.filter) {
|
|
99
|
+
nextArg = {
|
|
100
|
+
filter: query.filter.accept(this),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return nextArg;
|
|
104
|
+
}
|
|
105
|
+
mergeFilters(defaultFilter, generatedFilter, mergeType = "and", forceDefaultFilter = false) {
|
|
106
|
+
if ((0, utils_js_1.isFilterEmpty)(defaultFilter) && (0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
if ((0, utils_js_1.isFilterEmpty)(defaultFilter) || mergeType === "replace") {
|
|
110
|
+
if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
return generatedFilter;
|
|
114
|
+
}
|
|
115
|
+
if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
116
|
+
if (forceDefaultFilter) {
|
|
117
|
+
return defaultFilter;
|
|
118
|
+
}
|
|
119
|
+
if (mergeType === "and") {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
return defaultFilter;
|
|
123
|
+
}
|
|
124
|
+
if (mergeType === "and") {
|
|
125
|
+
return {
|
|
126
|
+
$and: [defaultFilter, generatedFilter],
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
else if (mergeType === "or") {
|
|
130
|
+
return {
|
|
131
|
+
$or: [defaultFilter, generatedFilter],
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
throw new Error("Unknown merge type");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
exports.BasicTranslator = BasicTranslator;
|