@copilotkit/runtime
Version:
<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />
233 lines (232 loc) • 8.62 kB
text/typescript
import "reflect-metadata";
import { MessageInput } from "../../graphql/inputs/message.input.mjs";
import { Message as Message$1 } from "../../graphql/types/converted/index.mjs";
import { CopilotServiceAdapter } from "../../service-adapters/service-adapter.mjs";
import { RemoteChainParameters } from "../../service-adapters/langchain/langserve.mjs";
import "../../service-adapters/index.mjs";
import { AgentRunner } from "../../v2/runtime/runner/agent-runner.mjs";
import { AgentFactoryContext, AgentsConfig, AgentsFactory, CopilotRuntime as CopilotRuntime$1, CopilotRuntimeOptions } from "../../v2/runtime/core/runtime.mjs";
import "../../v2/runtime/index.mjs";
import { CopilotKitEndpoint, EndpointDefinition, EndpointType, LangGraphPlatformEndpoint } from "./types.mjs";
import { CopilotObservabilityConfig } from "../observability.mjs";
import { MCPClient, MCPEndpointConfig } from "./mcp-tools-utils.mjs";
import { Action, CopilotErrorHandler, DebugConfig, Parameter, PartialBy } from "@copilotkit/shared";
import { AbstractAgent } from "@ag-ui/client";
//#region src/lib/runtime/copilot-runtime.d.ts
type CreateMCPClientFunction = (config: MCPEndpointConfig) => Promise<MCPClient>;
type ActionsConfiguration<T extends Parameter[] | [] = []> = Action<T>[] | ((ctx: {
properties: any;
url?: string;
}) => Action<T>[]);
interface OnBeforeRequestOptions {
threadId?: string;
runId?: string;
inputMessages: Message$1[];
properties: any;
url?: string;
}
type OnBeforeRequestHandler = (options: OnBeforeRequestOptions) => void | Promise<void>;
interface OnAfterRequestOptions {
threadId: string;
runId?: string;
inputMessages: Message$1[];
outputMessages: Message$1[];
properties: any;
url?: string;
}
type OnAfterRequestHandler = (options: OnAfterRequestOptions) => void | Promise<void>;
interface OnStopGenerationOptions {
threadId: string;
runId?: string;
url?: string;
agentName?: string;
lastMessage: MessageInput;
}
type OnStopGenerationHandler = (options: OnStopGenerationOptions) => void | Promise<void>;
interface Middleware$1 {
/**
* A function that is called before the request is processed.
*/
/**
* @deprecated This middleware hook is deprecated and will be removed in a future version.
* Use updated middleware integration methods in CopilotRuntimeVNext instead.
*/
onBeforeRequest?: OnBeforeRequestHandler;
/**
* A function that is called after the request is processed.
*/
/**
* @deprecated This middleware hook is deprecated and will be removed in a future version.
* Use updated middleware integration methods in CopilotRuntimeVNext instead.
*/
onAfterRequest?: OnAfterRequestHandler;
}
interface CopilotRuntimeConstructorParams_BASE<T extends Parameter[] | [] = []> {
/**
* Middleware to be used by the runtime.
*
* ```ts
* onBeforeRequest: (options: {
* threadId?: string;
* runId?: string;
* inputMessages: Message[];
* properties: any;
* }) => void | Promise<void>;
* ```
*
* ```ts
* onAfterRequest: (options: {
* threadId?: string;
* runId?: string;
* inputMessages: Message[];
* outputMessages: Message[];
* properties: any;
* }) => void | Promise<void>;
* ```
*/
/**
* @deprecated This middleware hook is deprecated and will be removed in a future version.
* Use updated middleware integration methods in CopilotRuntimeVNext instead.
*/
middleware?: Middleware$1;
actions?: ActionsConfiguration<T>;
remoteActions?: CopilotKitEndpoint[];
remoteEndpoints?: EndpointDefinition[];
langserve?: RemoteChainParameters[];
/**
* Optional agent runner to use for SSE runtime.
*/
runner?: AgentRunner;
agents?: Record<string, AbstractAgent>;
delegateAgentProcessingToServiceAdapter?: boolean;
/**
* Configuration for LLM request/response logging.
* Requires publicApiKey from CopilotKit component to be set:
*
* ```tsx
* <CopilotKit publicApiKey="ck_pub_..." />
* ```
*
* Example logging config:
* ```ts
* logging: {
* enabled: true, // Enable or disable logging
* progressive: true, // Set to false for buffered logging
* logger: {
* logRequest: (data) => langfuse.trace({ name: "LLM Request", input: data }),
* logResponse: (data) => langfuse.trace({ name: "LLM Response", output: data }),
* logError: (errorData) => langfuse.trace({ name: "LLM Error", metadata: errorData }),
* },
* }
* ```
*/
observability_c?: CopilotObservabilityConfig;
/**
* Configuration for connecting to Model Context Protocol (MCP) servers.
* Allows fetching and using tools defined on external MCP-compliant servers.
* Requires providing the `createMCPClient` function during instantiation.
* @experimental
*/
mcpServers?: MCPEndpointConfig[];
/**
* A function that creates an MCP client instance for a given endpoint configuration.
* This function is responsible for using the appropriate MCP client library
* (e.g., `@copilotkit/runtime`, `ai`) to establish a connection.
* Required if `mcpServers` is provided.
*
* ```typescript
* import { experimental_createMCPClient } from "ai"; // Import from vercel ai library
* // ...
* const runtime = new CopilotRuntime({
* mcpServers: [{ endpoint: "..." }],
* async createMCPClient(config) {
* return await experimental_createMCPClient({
* transport: {
* type: "sse",
* url: config.endpoint,
* headers: config.apiKey
* ? { Authorization: `Bearer ${config.apiKey}` }
* : undefined,
* },
* });
* }
* });
* ```
*/
createMCPClient?: CreateMCPClientFunction;
/**
* Optional error handler for comprehensive debugging and observability.
*
* **Requires publicApiKey**: Error handling only works when requests include a valid publicApiKey.
* This is a premium Copilot Cloud feature.
*
* @param errorEvent - Structured error event with rich debugging context
*
* @example
* ```typescript
* const runtime = new CopilotRuntime({
* onError: (errorEvent) => {
* debugDashboard.capture(errorEvent);
* }
* });
* ```
*/
onError?: CopilotErrorHandler;
onStopGeneration?: OnStopGenerationHandler;
/**
* Enable debug logging for the runtime event pipeline.
* Pass `true` for full output, or an object for granular control:
*
* ```ts
* const runtime = new CopilotRuntime({
* debug: true,
* // or: debug: { events: true, lifecycle: true, verbose: false }
* });
* ```
*/
debug?: DebugConfig;
}
interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> extends Omit<CopilotRuntimeConstructorParams_BASE<T>, "agents">, Omit<CopilotRuntimeOptions, "agents" | "transcriptionService"> {
/**
* TODO: un-omit `transcriptionService` above once it's supported
*
* This satisfies...
* – the optional constraint in `CopilotRuntimeConstructorParams_BASE`
* – the `MaybePromise<NonEmptyRecord<T>>` constraint in `CopilotRuntimeOptionsVNext`
* – the `Record<string, AbstractAgent>` constraint in `both
*/
agents?: AgentsConfig;
}
/**
* Central runtime object passed to all request handlers.
*/
declare class CopilotRuntime<const T extends Parameter[] | [] = []> {
params?: CopilotRuntimeConstructorParams<T>;
private observability?;
private mcpToolsCache;
private runtimeArgs;
private _instance;
constructor(params?: CopilotRuntimeConstructorParams<T> & PartialBy<CopilotRuntimeOptions, "agents">);
get instance(): CopilotRuntime$1;
private assignEndpointsToAgents;
handleServiceAdapter(serviceAdapter: CopilotServiceAdapter): void;
private getToolsFromActions;
private assignToolsToAgents;
private createOnBeforeRequestHandler;
private createOnAfterRequestHandler;
/**
* Log LLM request if observability is enabled
*/
private logObservabilityBeforeRequest;
/**
* Log final LLM response after request completes
*/
private logObservabilityAfterRequest;
private getToolsFromMCP;
}
declare function copilotKitEndpoint(config: Omit<CopilotKitEndpoint, "type">): CopilotKitEndpoint;
declare function langGraphPlatformEndpoint(config: Omit<LangGraphPlatformEndpoint, "type">): LangGraphPlatformEndpoint;
declare function resolveEndpointType(endpoint: EndpointDefinition): EndpointType;
//#endregion
export { CopilotRuntime, CopilotRuntimeConstructorParams_BASE, copilotKitEndpoint, langGraphPlatformEndpoint, resolveEndpointType };
//# sourceMappingURL=copilot-runtime.d.mts.map