@copilotkit/react-core
Version:
<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />
148 lines (147 loc) • 5.68 kB
text/typescript
import * as react from "react";
import React$1 from "react";
import { LicenseContextValue, StandardSchemaV1 } from "@copilotkit/shared";
import { CopilotKitCore, CopilotKitCoreConfig, CopilotKitCoreSubscriber, CopilotKitCoreSubscription, ToolCallStatus } from "@copilotkit/core";
import { ActivityMessage, Message } from "@ag-ui/core";
import { AbstractAgent } from "@ag-ui/client";
//#region src/v2/types/react-tool-call-renderer.d.ts
interface ReactToolCallRenderer<T = unknown> {
name: string;
args: StandardSchemaV1<any, T>;
/**
* Optional agent ID to constrain this tool renderer to a specific agent.
* If specified, this renderer will only be used for the specified agent.
*/
agentId?: string;
render: React.ComponentType<{
name: string;
toolCallId: string;
args: Partial<T>;
status: ToolCallStatus.InProgress;
result: undefined;
} | {
name: string;
toolCallId: string;
args: T;
status: ToolCallStatus.Executing;
result: undefined;
} | {
name: string;
toolCallId: string;
args: T;
status: ToolCallStatus.Complete;
result: string;
}>;
}
//#endregion
//#region src/v2/types/react-activity-message-renderer.d.ts
interface ReactActivityMessageRenderer<TActivityContent> {
/**
* Activity type to match when rendering. Use "*" as a wildcard renderer.
*/
activityType: string;
/**
* Optional agent ID to scope the renderer to a particular agent.
*/
agentId?: string;
/**
* Schema describing the activity content payload.
*/
content: StandardSchemaV1<any, TActivityContent>;
/**
* React component invoked to render the activity message.
*/
render: React.ComponentType<{
activityType: string;
content: TActivityContent;
message: ActivityMessage;
agent: AbstractAgent | undefined;
}>;
}
//#endregion
//#region src/v2/types/react-custom-message-renderer.d.ts
type ReactCustomMessageRendererPosition = "before" | "after";
interface ReactCustomMessageRenderer {
agentId?: string;
render: React.ComponentType<{
message: Message;
position: ReactCustomMessageRendererPosition;
runId: string;
messageIndex: number;
messageIndexInRun: number;
numberOfMessagesInRun: number;
agentId: string;
stateSnapshot: any;
}> | null;
}
//#endregion
//#region src/v2/lib/react-core.d.ts
interface CopilotKitCoreReactConfig extends CopilotKitCoreConfig {
renderToolCalls?: ReactToolCallRenderer<any>[];
renderActivityMessages?: ReactActivityMessageRenderer<any>[];
renderCustomMessages?: ReactCustomMessageRenderer[];
}
interface CopilotKitCoreReactSubscriber extends CopilotKitCoreSubscriber {
onRenderToolCallsChanged?: (event: {
copilotkit: CopilotKitCore;
renderToolCalls: ReactToolCallRenderer<any>[];
}) => void | Promise<void>;
onInterruptElementChanged?: (event: {
copilotkit: CopilotKitCore;
interruptElement: React$1.ReactElement | null;
}) => void | Promise<void>;
}
declare class CopilotKitCoreReact extends CopilotKitCore {
private _renderToolCalls;
private _hookRenderToolCalls;
private _cachedMergedRenderToolCalls;
private _renderCustomMessages;
private _renderActivityMessages;
private _interruptElement;
constructor(config: CopilotKitCoreReactConfig);
get renderCustomMessages(): Readonly<ReactCustomMessageRenderer[]>;
get renderActivityMessages(): Readonly<ReactActivityMessageRenderer<any>>[];
get renderToolCalls(): Readonly<ReactToolCallRenderer<any>>[];
setRenderActivityMessages(renderers: ReactActivityMessageRenderer<any>[]): void;
setRenderCustomMessages(renderers: ReactCustomMessageRenderer[]): void;
setRenderToolCalls(renderToolCalls: ReactToolCallRenderer<any>[]): void;
addHookRenderToolCall(entry: ReactToolCallRenderer<any>): void;
removeHookRenderToolCall(name: string, agentId?: string): void;
private _notifyRenderToolCallsChanged;
get interruptElement(): React$1.ReactElement | null;
setInterruptElement(element: React$1.ReactElement | null): void;
subscribe(subscriber: CopilotKitCoreReactSubscriber): CopilotKitCoreSubscription;
/**
* Wait for pending React state updates before the follow-up agent run.
*
* When a frontend tool handler calls setState(), React 18 batches the update
* and schedules a commit via its internal scheduler (MessageChannel). The
* useAgentContext hook registers context via useLayoutEffect, which runs
* synchronously after React commits that batch.
*
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
* MessageChannel task runs first, committing the pending state and running
* useLayoutEffect (which updates the context store). The follow-up runAgent
* call then reads fresh context.
*/
waitForPendingFrameworkUpdates(): Promise<void>;
}
//#endregion
//#region src/v2/context.d.ts
interface CopilotKitContextValue {
copilotkit: CopilotKitCoreReact;
/**
* Set of tool call IDs currently being executed.
* This is tracked at the provider level to ensure tool execution events
* are captured even before child components mount.
*/
executingToolCallIds: ReadonlySet<string>;
}
declare const EMPTY_SET: ReadonlySet<string>;
declare const CopilotKitContext: react.Context<CopilotKitContextValue | null>;
declare const useCopilotKit: () => CopilotKitContextValue;
declare const LicenseContext: react.Context<LicenseContextValue>;
declare const useLicenseContext: () => LicenseContextValue;
//#endregion
export { CopilotKitContext, CopilotKitContextValue, CopilotKitCoreReact, type CopilotKitCoreReactConfig, EMPTY_SET, LicenseContext, useCopilotKit, useLicenseContext };
//# sourceMappingURL=context.d.cts.map