@oracle/langchain-oracledb 1.0.0 → 1.1.0

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.
@@ -0,0 +1,345 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.OracleChatMessageHistory = void 0;
7
+ const chat_history_1 = require("@langchain/core/chat_history");
8
+ const vectorstores_js_1 = require("./vectorstores.cjs");
9
+ const node_crypto_1 = require("node:crypto");
10
+ const oracledb_1 = __importDefault(require("oracledb"));
11
+ const errors_js_1 = require("./errors.cjs");
12
+ const messages_1 = require("@langchain/core/messages");
13
+ const utils_js_1 = require("./utils.cjs");
14
+ const DEFAULT_TABLE_NAME = "langchain_message_store";
15
+ const DEFAULT_SESSION_ID_KEY = "session_id";
16
+ const DEFAULT_HISTORY_KEY = "message";
17
+ const DEFAULT_ID_KEY = (0, utils_js_1.quoteIdentifier)("id");
18
+ function unqualifiedIdentifier(name) {
19
+ return name.replaceAll('"', "").split(".").pop();
20
+ }
21
+ function defaultIndexName(tableName, sessionIdKey) {
22
+ const baseName = `idx_${unqualifiedIdentifier(tableName)}_` +
23
+ unqualifiedIdentifier(sessionIdKey);
24
+ const encoded = Buffer.from(baseName, "utf-8");
25
+ if (encoded.length <= 128)
26
+ return baseName;
27
+ const digest = (0, node_crypto_1.createHash)("sha1").update(encoded).digest("hex").slice(0, 8);
28
+ const prefix = new TextDecoder("utf-8").decode(encoded.subarray(0, 119), {
29
+ stream: true,
30
+ });
31
+ return `${prefix}_${digest}`;
32
+ }
33
+ /**
34
+ * Stores conversation messages in Oracle Database, with one row per message
35
+ * and a session ID identifying each conversation. Extends BaseListChatMessageHistory.
36
+ *
37
+ * Call `createTables()` before using the history unless the table already exists.
38
+ * A single table can hold many sessions separated by the configured session ID column.
39
+ * @example
40
+ * ```typescript
41
+ * // `pool` is an Oracle connection pool.
42
+ * await OracleChatMessageHistory.createTables({
43
+ * client: pool,
44
+ * tableName: "langchain_chat_histories"
45
+ * });
46
+ *
47
+ * const chatHistory = new OracleChatMessageHistory({
48
+ * client: pool,
49
+ * tableName: "langchain_chat_histories",
50
+ * sessionId: "lc-example",
51
+ * historySize: 20,
52
+ * });
53
+ *
54
+ * ```
55
+ */
56
+ class OracleChatMessageHistory extends chat_history_1.BaseListChatMessageHistory {
57
+ /**
58
+ * Creates an OracleChatMessageHistory instance without performing database setup.
59
+ * @param {OracleChatMessageHistoryInput} options The history configuration.
60
+ * @param {OracleDBClient | OracleDBClientProvider} options.client The connection, pool, or async provider to use.
61
+ * @param {string} options.sessionId The session ID used to store and retrieve messages.
62
+ * @param {string} options.tableName Table used to store chat history rows. Defaults to `langchain_message_store`.
63
+ * @param {string} options.sessionIdKey Column containing the session identifier. Defaults to `session_id`.
64
+ * @param {string} options.historyKey Column containing the serialized message payload. Defaults to `message`.
65
+ * @param {number} options.historySize Optional maximum number of most recent messages to return.
66
+ * @throws If the client is missing, the session ID is empty or not a string,
67
+ * an identifier is invalid, or the history size is not a positive safe integer.
68
+ */
69
+ constructor({ client, sessionId, tableName = DEFAULT_TABLE_NAME, sessionIdKey = DEFAULT_SESSION_ID_KEY, historyKey = DEFAULT_HISTORY_KEY, historySize, }) {
70
+ super();
71
+ Object.defineProperty(this, "lc_namespace", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: ["langchain", "stores", "message", "oracle"]
76
+ });
77
+ Object.defineProperty(this, "client", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ Object.defineProperty(this, "sessionId", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ Object.defineProperty(this, "quotedTableName", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: void 0
94
+ });
95
+ Object.defineProperty(this, "quotedSessionIdKey", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: void 0
100
+ });
101
+ Object.defineProperty(this, "quotedHistoryKey", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: void 0
106
+ });
107
+ Object.defineProperty(this, "historySize", {
108
+ enumerable: true,
109
+ configurable: true,
110
+ writable: true,
111
+ value: void 0
112
+ });
113
+ if (typeof sessionId !== "string" || !sessionId) {
114
+ (0, errors_js_1.throwError)(errors_js_1.ErrorCode.VALIDATION_INVALID_INPUT, "sessionId must be a non-empty string");
115
+ }
116
+ if (client == null) {
117
+ (0, errors_js_1.throwError)(errors_js_1.ErrorCode.VALIDATION_MISSING_REQUIRED_PARAMETER, "client");
118
+ }
119
+ if (historySize !== undefined &&
120
+ (!Number.isSafeInteger(historySize) || historySize < 1)) {
121
+ (0, errors_js_1.throwError)(errors_js_1.ErrorCode.VALIDATION_INVALID_INPUT, "historySize must be greater than 0");
122
+ }
123
+ this.client = client;
124
+ this.sessionId = sessionId;
125
+ this.historySize = historySize;
126
+ this.quotedTableName = (0, utils_js_1.quoteIdentifier)(tableName);
127
+ this.quotedSessionIdKey = (0, utils_js_1.quoteIdentifier)(sessionIdKey);
128
+ this.quotedHistoryKey = (0, utils_js_1.quoteIdentifier)(historyKey);
129
+ }
130
+ /**
131
+ * Creates the history table and optional session index if they do not exist.
132
+ * @param {OracleChatMessageHistorySetupInput} options The database setup configuration.
133
+ * @param {boolean} options.createIndex Whether to create the session index. Defaults to `true`.
134
+ * @returns Promise that resolves when database setup completes.
135
+ */
136
+ static async createTables({ client, tableName = DEFAULT_TABLE_NAME, createIndex = true, sessionIdKey = DEFAULT_SESSION_ID_KEY, historyKey = DEFAULT_HISTORY_KEY, }) {
137
+ try {
138
+ const quotedTableName = (0, utils_js_1.quoteIdentifier)(tableName);
139
+ const quotedSessionIdKey = (0, utils_js_1.quoteIdentifier)(sessionIdKey);
140
+ const quotedHistoryKey = (0, utils_js_1.quoteIdentifier)(historyKey);
141
+ const createTableSql = `
142
+ CREATE TABLE IF NOT EXISTS ${quotedTableName} (
143
+ ${DEFAULT_ID_KEY} NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
144
+ ${quotedSessionIdKey} VARCHAR2(255) NOT NULL,
145
+ ${quotedHistoryKey} CLOB NOT NULL,
146
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL
147
+ )`;
148
+ const indexName = defaultIndexName(tableName, sessionIdKey);
149
+ const createIndexSql = `
150
+ CREATE INDEX IF NOT EXISTS ${(0, utils_js_1.quoteIdentifier)(indexName)}
151
+ ON ${quotedTableName} (${quotedSessionIdKey})
152
+ `;
153
+ await (0, utils_js_1.withConnection)(client, async (connection) => {
154
+ await connection.execute(createTableSql);
155
+ if (createIndex) {
156
+ await connection.execute(createIndexSql);
157
+ }
158
+ });
159
+ }
160
+ catch (error) {
161
+ (0, errors_js_1.handleError)(error);
162
+ }
163
+ }
164
+ /**
165
+ * Drops and purges the history table if it exists, removing every session's messages.
166
+ * @param {OracleDBClient | OracleDBClientProvider} client The connection, pool, or async provider to use.
167
+ * @param {string} tableName The table to drop. Defaults to `langchain_message_store`.
168
+ * @returns Promise that resolves when the table has been dropped or does not exist.
169
+ */
170
+ static async dropTable(client, tableName = DEFAULT_TABLE_NAME) {
171
+ try {
172
+ await (0, utils_js_1.withConnection)(client, async (connection) => {
173
+ await (0, vectorstores_js_1.dropTablePurge)(connection, tableName);
174
+ });
175
+ }
176
+ catch (error) {
177
+ (0, errors_js_1.handleError)(error);
178
+ }
179
+ }
180
+ /**
181
+ * Adds one message to the session.
182
+ * @param {BaseMessage} message The message to store.
183
+ * @returns Promise that resolves when the message has been stored.
184
+ */
185
+ async addMessage(message) {
186
+ try {
187
+ const binds = [this.sessionId, JSON.stringify(message.toDict())];
188
+ await (0, utils_js_1.withConnection)(this.client, async (connection) => {
189
+ await connection.execute(this.insertQuery, binds);
190
+ await connection.commit();
191
+ });
192
+ }
193
+ catch (error) {
194
+ (0, errors_js_1.handleError)(error);
195
+ }
196
+ }
197
+ /**
198
+ * Adds a batch of messages in insertion order.
199
+ * @param {BaseMessage[]} messages The messages to store.
200
+ * @returns Promise that resolves when the messages have been stored.
201
+ */
202
+ async addMessages(messages) {
203
+ try {
204
+ if (messages.length === 0)
205
+ return;
206
+ const rows = messages.map((message) => [
207
+ this.sessionId,
208
+ JSON.stringify(message.toDict()),
209
+ ]);
210
+ await (0, utils_js_1.withConnection)(this.client, async (connection) => {
211
+ await connection.executeMany(this.insertQuery, rows, {
212
+ autoCommit: false,
213
+ });
214
+ await connection.commit();
215
+ });
216
+ }
217
+ catch (error) {
218
+ (0, errors_js_1.handleError)(error);
219
+ }
220
+ }
221
+ /**
222
+ * Retrieves messages for the configured session in ascending ID order.
223
+ * When `historySize` is set, returns only the most recent messages in that order.
224
+ * @returns Promise resolving to the stored messages, or an empty array for an empty session.
225
+ */
226
+ async getMessages() {
227
+ try {
228
+ let query;
229
+ let params;
230
+ if (this.historySize === undefined) {
231
+ query = `
232
+ SELECT ${this.quotedHistoryKey} AS "payload", ${DEFAULT_ID_KEY}
233
+ FROM ${this.quotedTableName}
234
+ WHERE ${this.quotedSessionIdKey} = :session_id
235
+ ORDER BY ${DEFAULT_ID_KEY}
236
+ `;
237
+ params = { session_id: this.sessionId };
238
+ }
239
+ else {
240
+ query = `
241
+ SELECT payload AS "payload", ${DEFAULT_ID_KEY}
242
+ FROM (
243
+ SELECT ${this.quotedHistoryKey} AS payload, ${DEFAULT_ID_KEY}
244
+ FROM ${this.quotedTableName}
245
+ WHERE ${this.quotedSessionIdKey} = :session_id
246
+ ORDER BY ${DEFAULT_ID_KEY} DESC
247
+ )
248
+ WHERE ROWNUM <= :history_size
249
+ ORDER BY ${DEFAULT_ID_KEY}
250
+ `;
251
+ params = {
252
+ session_id: this.sessionId,
253
+ history_size: this.historySize,
254
+ };
255
+ }
256
+ const result = await (0, utils_js_1.withConnection)(this.client, (connection) => connection.execute(query, params, {
257
+ outFormat: oracledb_1.default.OUT_FORMAT_OBJECT,
258
+ fetchInfo: {
259
+ payload: { type: oracledb_1.default.STRING },
260
+ },
261
+ }));
262
+ return (result.rows ?? []).map((row) => {
263
+ try {
264
+ return (0, messages_1.mapStoredMessageToChatMessage)(JSON.parse(row.payload));
265
+ }
266
+ catch (error) {
267
+ throw (0, errors_js_1.createErrorFromCodeWithCause)(errors_js_1.ErrorCode.HISTORY_DESERIALIZATION_FAILED, error);
268
+ }
269
+ });
270
+ }
271
+ catch (error) {
272
+ (0, errors_js_1.handleError)(error);
273
+ }
274
+ }
275
+ /**
276
+ * Deletes all messages belonging to the configured session.
277
+ * @returns Promise that resolves when this session's messages have been cleared.
278
+ */
279
+ async clear() {
280
+ try {
281
+ await (0, utils_js_1.withConnection)(this.client, async (connection) => {
282
+ await connection.execute(this.deleteQuery, { session_id: this.sessionId }, { autoCommit: true });
283
+ });
284
+ }
285
+ catch (error) {
286
+ (0, errors_js_1.handleError)(error);
287
+ }
288
+ }
289
+ /**
290
+ * Replaces the configured session's stored messages. An empty array clears the session.
291
+ * @param {BaseMessage[]} messages The replacement messages in insertion order.
292
+ * @returns Promise that resolves when the replacement has been committed.
293
+ */
294
+ async replaceMessages(messages) {
295
+ try {
296
+ const rows = messages.map((message) => [
297
+ this.sessionId,
298
+ JSON.stringify(message.toDict()),
299
+ ]);
300
+ await (0, utils_js_1.withConnection)(this.client, async (connection) => {
301
+ try {
302
+ await connection.execute(this.deleteQuery, { session_id: this.sessionId }, { autoCommit: false });
303
+ if (rows.length > 0) {
304
+ await connection.executeMany(this.insertQuery, rows, {
305
+ autoCommit: false,
306
+ batchErrors: false,
307
+ });
308
+ }
309
+ await connection.commit();
310
+ }
311
+ catch (error) {
312
+ let cause = error;
313
+ try {
314
+ await connection.rollback();
315
+ }
316
+ catch (rollbackError) {
317
+ cause = new AggregateError([error, rollbackError], "Replacing messages failed, and rollback also failed.");
318
+ }
319
+ throw (0, errors_js_1.createErrorFromCodeWithCause)(errors_js_1.ErrorCode.HISTORY_REPLACEMENT_FAILED, cause);
320
+ }
321
+ });
322
+ }
323
+ catch (error) {
324
+ (0, errors_js_1.handleError)(error);
325
+ }
326
+ }
327
+ get insertQuery() {
328
+ return `
329
+ INSERT INTO ${this.quotedTableName}
330
+ (
331
+ ${this.quotedSessionIdKey},
332
+ ${this.quotedHistoryKey}
333
+ )
334
+ VALUES (:1, :2)
335
+ `;
336
+ }
337
+ get deleteQuery() {
338
+ return `
339
+ DELETE FROM ${this.quotedTableName}
340
+ WHERE ${this.quotedSessionIdKey} = :session_id
341
+ `;
342
+ }
343
+ }
344
+ exports.OracleChatMessageHistory = OracleChatMessageHistory;
345
+ //# sourceMappingURL=chatMessageHistories.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chatMessageHistories.cjs","sourceRoot":"","sources":["../src/chatMessageHistories.ts"],"names":[],"mappings":";;;;;;AAAA,+DAA0E;AAC1E,uDAAmD;AACnD,6CAAyC;AACzC,wDAAgC;AAChC,2CAKqB;AACrB,uDAIkC;AAClC,yCAKoB;AAEpB,MAAM,kBAAkB,GAAG,yBAAyB,CAAC;AACrD,MAAM,sBAAsB,GAAG,YAAY,CAAC;AAC5C,MAAM,mBAAmB,GAAG,SAAS,CAAC;AACtC,MAAM,cAAc,GAAG,IAAA,0BAAe,EAAC,IAAI,CAAC,CAAC;AAoB7C,SAAS,qBAAqB,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAG,CAAC;AACpD,CAAC;AAED,SAAS,gBAAgB,CAAC,SAAiB,EAAE,YAAoB;IAC/D,MAAM,QAAQ,GACZ,OAAO,qBAAqB,CAAC,SAAS,CAAC,GAAG;QAC1C,qBAAqB,CAAC,YAAY,CAAC,CAAC;IAEtC,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,O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AAkB;KAChC,CAAC;IACJ,CAAC;CACF;AA3TD,4DA2TC"}
@@ -0,0 +1,107 @@
1
+ import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
2
+ import { BaseMessage } from "@langchain/core/messages";
3
+ import { OracleDBClient, OracleDBClientProvider } from "./utils.js";
4
+ interface OracleChatMessageHistoryBaseInput {
5
+ client: OracleDBClient | OracleDBClientProvider;
6
+ tableName?: string;
7
+ sessionIdKey?: string;
8
+ historyKey?: string;
9
+ }
10
+ interface OracleChatMessageHistoryInput extends OracleChatMessageHistoryBaseInput {
11
+ sessionId: string;
12
+ historySize?: number;
13
+ }
14
+ interface OracleChatMessageHistorySetupInput extends OracleChatMessageHistoryBaseInput {
15
+ createIndex?: boolean;
16
+ }
17
+ /**
18
+ * Stores conversation messages in Oracle Database, with one row per message
19
+ * and a session ID identifying each conversation. Extends BaseListChatMessageHistory.
20
+ *
21
+ * Call `createTables()` before using the history unless the table already exists.
22
+ * A single table can hold many sessions separated by the configured session ID column.
23
+ * @example
24
+ * ```typescript
25
+ * // `pool` is an Oracle connection pool.
26
+ * await OracleChatMessageHistory.createTables({
27
+ * client: pool,
28
+ * tableName: "langchain_chat_histories"
29
+ * });
30
+ *
31
+ * const chatHistory = new OracleChatMessageHistory({
32
+ * client: pool,
33
+ * tableName: "langchain_chat_histories",
34
+ * sessionId: "lc-example",
35
+ * historySize: 20,
36
+ * });
37
+ *
38
+ * ```
39
+ */
40
+ export declare class OracleChatMessageHistory extends BaseListChatMessageHistory {
41
+ lc_namespace: string[];
42
+ readonly client: OracleDBClient | OracleDBClientProvider;
43
+ readonly sessionId: string;
44
+ private readonly quotedTableName;
45
+ private readonly quotedSessionIdKey;
46
+ private readonly quotedHistoryKey;
47
+ readonly historySize?: number | undefined;
48
+ /**
49
+ * Creates an OracleChatMessageHistory instance without performing database setup.
50
+ * @param {OracleChatMessageHistoryInput} options The history configuration.
51
+ * @param {OracleDBClient | OracleDBClientProvider} options.client The connection, pool, or async provider to use.
52
+ * @param {string} options.sessionId The session ID used to store and retrieve messages.
53
+ * @param {string} options.tableName Table used to store chat history rows. Defaults to `langchain_message_store`.
54
+ * @param {string} options.sessionIdKey Column containing the session identifier. Defaults to `session_id`.
55
+ * @param {string} options.historyKey Column containing the serialized message payload. Defaults to `message`.
56
+ * @param {number} options.historySize Optional maximum number of most recent messages to return.
57
+ * @throws If the client is missing, the session ID is empty or not a string,
58
+ * an identifier is invalid, or the history size is not a positive safe integer.
59
+ */
60
+ constructor({ client, sessionId, tableName, sessionIdKey, historyKey, historySize, }: OracleChatMessageHistoryInput);
61
+ /**
62
+ * Creates the history table and optional session index if they do not exist.
63
+ * @param {OracleChatMessageHistorySetupInput} options The database setup configuration.
64
+ * @param {boolean} options.createIndex Whether to create the session index. Defaults to `true`.
65
+ * @returns Promise that resolves when database setup completes.
66
+ */
67
+ static createTables({ client, tableName, createIndex, sessionIdKey, historyKey, }: OracleChatMessageHistorySetupInput): Promise<void>;
68
+ /**
69
+ * Drops and purges the history table if it exists, removing every session's messages.
70
+ * @param {OracleDBClient | OracleDBClientProvider} client The connection, pool, or async provider to use.
71
+ * @param {string} tableName The table to drop. Defaults to `langchain_message_store`.
72
+ * @returns Promise that resolves when the table has been dropped or does not exist.
73
+ */
74
+ static dropTable(client: OracleDBClient | OracleDBClientProvider, tableName?: string): Promise<void>;
75
+ /**
76
+ * Adds one message to the session.
77
+ * @param {BaseMessage} message The message to store.
78
+ * @returns Promise that resolves when the message has been stored.
79
+ */
80
+ addMessage(message: BaseMessage): Promise<void>;
81
+ /**
82
+ * Adds a batch of messages in insertion order.
83
+ * @param {BaseMessage[]} messages The messages to store.
84
+ * @returns Promise that resolves when the messages have been stored.
85
+ */
86
+ addMessages(messages: BaseMessage[]): Promise<void>;
87
+ /**
88
+ * Retrieves messages for the configured session in ascending ID order.
89
+ * When `historySize` is set, returns only the most recent messages in that order.
90
+ * @returns Promise resolving to the stored messages, or an empty array for an empty session.
91
+ */
92
+ getMessages(): Promise<BaseMessage[]>;
93
+ /**
94
+ * Deletes all messages belonging to the configured session.
95
+ * @returns Promise that resolves when this session's messages have been cleared.
96
+ */
97
+ clear(): Promise<void>;
98
+ /**
99
+ * Replaces the configured session's stored messages. An empty array clears the session.
100
+ * @param {BaseMessage[]} messages The replacement messages in insertion order.
101
+ * @returns Promise that resolves when the replacement has been committed.
102
+ */
103
+ replaceMessages(messages: BaseMessage[]): Promise<void>;
104
+ private get insertQuery();
105
+ private get deleteQuery();
106
+ }
107
+ export {};
@@ -0,0 +1,107 @@
1
+ import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
2
+ import { BaseMessage } from "@langchain/core/messages";
3
+ import { OracleDBClient, OracleDBClientProvider } from "./utils.js";
4
+ interface OracleChatMessageHistoryBaseInput {
5
+ client: OracleDBClient | OracleDBClientProvider;
6
+ tableName?: string;
7
+ sessionIdKey?: string;
8
+ historyKey?: string;
9
+ }
10
+ interface OracleChatMessageHistoryInput extends OracleChatMessageHistoryBaseInput {
11
+ sessionId: string;
12
+ historySize?: number;
13
+ }
14
+ interface OracleChatMessageHistorySetupInput extends OracleChatMessageHistoryBaseInput {
15
+ createIndex?: boolean;
16
+ }
17
+ /**
18
+ * Stores conversation messages in Oracle Database, with one row per message
19
+ * and a session ID identifying each conversation. Extends BaseListChatMessageHistory.
20
+ *
21
+ * Call `createTables()` before using the history unless the table already exists.
22
+ * A single table can hold many sessions separated by the configured session ID column.
23
+ * @example
24
+ * ```typescript
25
+ * // `pool` is an Oracle connection pool.
26
+ * await OracleChatMessageHistory.createTables({
27
+ * client: pool,
28
+ * tableName: "langchain_chat_histories"
29
+ * });
30
+ *
31
+ * const chatHistory = new OracleChatMessageHistory({
32
+ * client: pool,
33
+ * tableName: "langchain_chat_histories",
34
+ * sessionId: "lc-example",
35
+ * historySize: 20,
36
+ * });
37
+ *
38
+ * ```
39
+ */
40
+ export declare class OracleChatMessageHistory extends BaseListChatMessageHistory {
41
+ lc_namespace: string[];
42
+ readonly client: OracleDBClient | OracleDBClientProvider;
43
+ readonly sessionId: string;
44
+ private readonly quotedTableName;
45
+ private readonly quotedSessionIdKey;
46
+ private readonly quotedHistoryKey;
47
+ readonly historySize?: number | undefined;
48
+ /**
49
+ * Creates an OracleChatMessageHistory instance without performing database setup.
50
+ * @param {OracleChatMessageHistoryInput} options The history configuration.
51
+ * @param {OracleDBClient | OracleDBClientProvider} options.client The connection, pool, or async provider to use.
52
+ * @param {string} options.sessionId The session ID used to store and retrieve messages.
53
+ * @param {string} options.tableName Table used to store chat history rows. Defaults to `langchain_message_store`.
54
+ * @param {string} options.sessionIdKey Column containing the session identifier. Defaults to `session_id`.
55
+ * @param {string} options.historyKey Column containing the serialized message payload. Defaults to `message`.
56
+ * @param {number} options.historySize Optional maximum number of most recent messages to return.
57
+ * @throws If the client is missing, the session ID is empty or not a string,
58
+ * an identifier is invalid, or the history size is not a positive safe integer.
59
+ */
60
+ constructor({ client, sessionId, tableName, sessionIdKey, historyKey, historySize, }: OracleChatMessageHistoryInput);
61
+ /**
62
+ * Creates the history table and optional session index if they do not exist.
63
+ * @param {OracleChatMessageHistorySetupInput} options The database setup configuration.
64
+ * @param {boolean} options.createIndex Whether to create the session index. Defaults to `true`.
65
+ * @returns Promise that resolves when database setup completes.
66
+ */
67
+ static createTables({ client, tableName, createIndex, sessionIdKey, historyKey, }: OracleChatMessageHistorySetupInput): Promise<void>;
68
+ /**
69
+ * Drops and purges the history table if it exists, removing every session's messages.
70
+ * @param {OracleDBClient | OracleDBClientProvider} client The connection, pool, or async provider to use.
71
+ * @param {string} tableName The table to drop. Defaults to `langchain_message_store`.
72
+ * @returns Promise that resolves when the table has been dropped or does not exist.
73
+ */
74
+ static dropTable(client: OracleDBClient | OracleDBClientProvider, tableName?: string): Promise<void>;
75
+ /**
76
+ * Adds one message to the session.
77
+ * @param {BaseMessage} message The message to store.
78
+ * @returns Promise that resolves when the message has been stored.
79
+ */
80
+ addMessage(message: BaseMessage): Promise<void>;
81
+ /**
82
+ * Adds a batch of messages in insertion order.
83
+ * @param {BaseMessage[]} messages The messages to store.
84
+ * @returns Promise that resolves when the messages have been stored.
85
+ */
86
+ addMessages(messages: BaseMessage[]): Promise<void>;
87
+ /**
88
+ * Retrieves messages for the configured session in ascending ID order.
89
+ * When `historySize` is set, returns only the most recent messages in that order.
90
+ * @returns Promise resolving to the stored messages, or an empty array for an empty session.
91
+ */
92
+ getMessages(): Promise<BaseMessage[]>;
93
+ /**
94
+ * Deletes all messages belonging to the configured session.
95
+ * @returns Promise that resolves when this session's messages have been cleared.
96
+ */
97
+ clear(): Promise<void>;
98
+ /**
99
+ * Replaces the configured session's stored messages. An empty array clears the session.
100
+ * @param {BaseMessage[]} messages The replacement messages in insertion order.
101
+ * @returns Promise that resolves when the replacement has been committed.
102
+ */
103
+ replaceMessages(messages: BaseMessage[]): Promise<void>;
104
+ private get insertQuery();
105
+ private get deleteQuery();
106
+ }
107
+ export {};