import { BaseMessageChunk, AIMessage } from '@langchain/core/messages'; import { IterableReadableStream } from '@langchain/core/utils/stream'; import { CopilotKitServiceAdapter, CopilotKitResponse } from '../types/service-adapter.js'; /** * CopilotKit Adapter for LangChain * * Use this adapter to use LangChain as a backend. * * ```typescript * return copilotKit.response( * req, * new LangChainAdapter(async (forwardedProps) => { * const model = new ChatOpenAI({ modelName: "gpt-4o" }); * return model.stream(forwardedProps.messages, { * tools: forwardedProps.tools, * }); * }) * ); * ``` * The async handler function can return: * * - a simple `string` response * - a LangChain stream `IterableReadableStream` * - a LangChain `BaseMessageChunk` object * - a LangChain `AIMessage` object */ type LangChainMessageStream = IterableReadableStream; type LangChainReturnType = LangChainMessageStream | BaseMessageChunk | string | AIMessage; declare class LangChainAdapter implements CopilotKitServiceAdapter { private chainFn; /** * To use LangChain as a backend, provide a handler function to the adapter with your custom LangChain logic. */ constructor(chainFn: (forwardedProps: any) => Promise); getResponse(forwardedProps: any): Promise; /** * Transforms the props that are forwarded to the LangChain function. * Currently this just transforms the messages to the format that LangChain expects. * * @param forwardedProps * @returns {any} */ private transformProps; /** * Reads from the LangChainMessageStream and converts the output to a ReadableStream. * * @param streamedChain * @returns ReadableStream */ streamResult(streamedChain: LangChainMessageStream): ReadableStream; } export { LangChainAdapter, LangChainMessageStream, LangChainReturnType };