forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
62 lines (52 loc) · 2.02 KB
/
Copy pathcontext.ts
File metadata and controls
62 lines (52 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"use client";
import { createContext, useContext, useEffect, useReducer } from "react";
import { CopilotKitCoreReact } from "./lib/react-core";
import type { CopilotKitCoreReactConfig } from "./lib/react-core";
import type { LicenseContextValue } from "@copilotkit/shared";
// Re-export so headless.ts (and consumers) reference the same type declaration.
export { CopilotKitCoreReact };
export type { CopilotKitCoreReactConfig };
export 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>;
}
export const EMPTY_SET: ReadonlySet<string> = new Set();
export const CopilotKitContext = createContext<CopilotKitContextValue | null>(
null,
);
export const useCopilotKit = (): CopilotKitContextValue => {
const context = useContext(CopilotKitContext);
const [, forceUpdate] = useReducer((x: number) => x + 1, 0);
if (!context) {
throw new Error("useCopilotKit must be used within CopilotKitProvider");
}
useEffect(() => {
const subscription = context.copilotkit.subscribe({
onRuntimeConnectionStatusChanged: () => {
forceUpdate();
},
});
return () => {
subscription.unsubscribe();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return context;
};
// License context — shared between web and RN providers.
// Default is permissive (all features allowed) — providers override via createLicenseContextValue.
// Inlined here to avoid a runtime import from @copilotkit/shared, which pulls in
// Node-only deps (jose) that break React Native's Metro bundler.
export const LicenseContext = createContext<LicenseContextValue>({
status: null,
license: null,
checkFeature: () => true,
getLimit: () => null,
} as LicenseContextValue);
export const useLicenseContext = (): LicenseContextValue =>
useContext(LicenseContext);