import { BaseMessage } from "../messages/base.js"; import { RunnableConfig } from "./types.js"; import { Run } from "../tracers/base.js"; import { Runnable, RunnableBinding, RunnableBindingArgs } from "./base.js"; import { BaseChatMessageHistory, BaseListChatMessageHistory } from "../chat_history.js"; //#region src/runnables/history.d.ts type GetSessionHistoryCallable = (...args: Array) => Promise | BaseChatMessageHistory | BaseListChatMessageHistory; interface RunnableWithMessageHistoryInputs extends Omit, "bound" | "config"> { runnable: Runnable; getMessageHistory: GetSessionHistoryCallable; inputMessagesKey?: string; outputMessagesKey?: string; historyMessagesKey?: string; config?: RunnableConfig; } /** * Wraps a LCEL chain and manages history. It appends input messages * and chain outputs as history, and adds the current history messages to * the chain input. * @example * ```typescript * // pnpm install @langchain/anthropic @langchain/classic * * import { * ChatPromptTemplate, * MessagesPlaceholder, * } from "@langchain/core/prompts"; * import { ChatAnthropic } from "@langchain/anthropic"; * import { ChatMessageHistory } from "@langchain/classic/stores/message/in_memory"; * * const prompt = ChatPromptTemplate.fromMessages([ * ["system", "You're an assistant who's good at {ability}"], * new MessagesPlaceholder("history"), * ["human", "{question}"], * ]); * * const chain = prompt.pipe(new ChatAnthropic({})); * * const chainWithHistory = new RunnableWithMessageHistory({ * runnable: chain, * getMessageHistory: (sessionId) => * new UpstashRedisChatMessageHistory({ * sessionId, * config: { * url: process.env.UPSTASH_REDIS_REST_URL!, * token: process.env.UPSTASH_REDIS_REST_TOKEN!, * }, * }), * inputMessagesKey: "question", * historyMessagesKey: "history", * }); * * const result = await chainWithHistory.invoke( * { * ability: "math", * question: "What does cosine mean?", * }, * { * configurable: { * sessionId: "some_string_identifying_a_user", * }, * } * ); * * const result2 = await chainWithHistory.invoke( * { * ability: "math", * question: "What's its inverse?", * }, * { * configurable: { * sessionId: "some_string_identifying_a_user", * }, * } * ); * ``` */ declare class RunnableWithMessageHistory extends RunnableBinding { runnable: Runnable; inputMessagesKey?: string; outputMessagesKey?: string; historyMessagesKey?: string; getMessageHistory: GetSessionHistoryCallable; constructor(fields: RunnableWithMessageHistoryInputs); _getInputMessages(inputValue: string | BaseMessage | Array | Record): Array; _getOutputMessages(outputValue: string | BaseMessage | Array | Record): Array; _enterHistory(input: any, kwargs?: RunnableConfig): Promise; _exitHistory(run: Run, config: RunnableConfig): Promise; _mergeConfig(...configs: Array): Promise>>>; } //#endregion export { RunnableWithMessageHistory, RunnableWithMessageHistoryInputs }; //# sourceMappingURL=history.d.ts.map