import { Parameter, Action, AnnotatedFunction, ToolDefinition } from '@copilotkit/shared'; import { CopilotKitServiceAdapter } from '../types/service-adapter.js'; import { RemoteChain } from '../types/langserve.js'; import { CopilotCloud } from './copilot-cloud.js'; /** * Handles requests from frontend, provides function calling and various LLM backends. * * * * * ```jsx CopilotRuntime Example * import { * CopilotRuntime, * OpenAIAdapter * } from "@copilotkit/backend"; * * export async function POST(req: Request) { * const copilotKit = new CopilotRuntime(); * return copilotKit.response(req, new OpenAIAdapter()); * } * * ``` * * * This class is the main entry point for the runtime. It handles requests from the frontend, provides function calling and various LLM backends. * * For example, to use OpenAI as a backend (check the [OpenAI Adapter](./OpenAIAdapter) docs for more info): * ```typescript * const copilotKit = new CopilotRuntime(); * return copilotKit.response(req, new OpenAIAdapter()); * ``` * * Currently we support: * * - [OpenAI](./OpenAIAdapter) * - [LangChain](./LangChainAdapter) * - [OpenAI Assistant API](./OpenAIAssistantAdapter) * - [Google Gemini](./GoogleGenerativeAIAdapter) * * ## Server Side Actions * * CopilotKit supports actions that can be executed on the server side. You can define server side actions by passing the `actions` parameter: * * ```typescript * const copilotKit = new CopilotRuntime({ * actions: [ * { * name: "sayHello", * description: "Says hello to someone.", * argumentAnnotations: [ * { * name: "arg", * type: "string", * description: "The name of the person to say hello to.", * required: true, * }, * ], * implementation: async (arg) => { * console.log("Hello from the server", arg, "!"); * }, * }, * ], * }); * ``` * * Server side actions can also return a result which becomes part of the message history. * * This is useful because it gives the LLM context about what happened on the server side. In addition, * it can be used to look up information from a vector or relational database and other sources. * * In addition to that, server side actions can also come from LangChain, including support for streaming responses. * * Returned results can be of the following type: * * - anything serializable to JSON * - `string` * - LangChain types: * - `IterableReadableStream` * - `BaseMessageChunk` * - `AIMessage` * * ## LangServe * * The backend also supports LangServe, enabling you to connect to existing chains, for example python based chains. * Use the `langserve` parameter to specify URLs for LangServe. * * ```typescript * const copilotKit = new CopilotRuntime({ * langserve: [ * { * chainUrl: "http://my-langserve.chain", * name: "performResearch", * description: "Performs research on a given topic.", * }, * ], * }); * ``` * * When left out, arguments are automatically inferred from the schema provided by LangServe. */ interface CopilotRuntimeConstructorParams { actions?: Action[]; langserve?: RemoteChain[]; debug?: boolean; copilotCloud?: CopilotCloud; } interface CopilotDeprecatedRuntimeConstructorParams { actions?: AnnotatedFunction[]; langserve?: RemoteChain[]; debug?: boolean; copilotCloud?: CopilotCloud; } declare class CopilotRuntime { private actions; private langserve; private debug; private copilotCloud; constructor(params?: CopilotRuntimeConstructorParams); constructor(params?: CopilotDeprecatedRuntimeConstructorParams); addAction(action: Action): void; /** @deprecated Use addAction with Action instead. */ addAction(action: AnnotatedFunction): void; removeAction(actionName: string): void; removeBackendOnlyProps(forwardedProps: any): void; private getResponse; /** * Returns a `Response` object for streaming back the result to the client * * @param req The HTTP request * @param serviceAdapter The adapter to use for the response. */ response(req: Request, serviceAdapter: CopilotKitServiceAdapter): Promise; /** * Streams messages back to the client using the HTTP response object. Use with express, or Node.js HTTP server. * * @param req The HTTP request * @param res The HTTP response * @param serviceAdapter The adapter to use for the response. * @param headers Additional headers to send with the response. */ streamHttpServerResponse(req: any, res: any, serviceAdapter: CopilotKitServiceAdapter, headers?: Record): Promise; } declare function flattenToolCallsNoDuplicates(toolsByPriority: ToolDefinition[]): ToolDefinition[]; /** * @deprecated use CopilotRuntime instead */ declare class CopilotBackend extends CopilotRuntime { } export { CopilotBackend, CopilotRuntime, flattenToolCallsNoDuplicates };