forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
133 lines (118 loc) · 3.73 KB
/
Copy pathconfig.ts
File metadata and controls
133 lines (118 loc) · 3.73 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { inject, InjectionToken, Provider } from "@angular/core";
import { AbstractAgent } from "@ag-ui/client";
import {
ClientTool,
FrontendToolConfig,
HumanInTheLoopConfig,
RenderToolCallConfig,
} from "./tools";
import { LICENSE_WATERMARK_ENABLED } from "./license-watermark";
import type { RenderActivityMessageConfig } from "./activity-renderer";
import type { SuggestionsConfig } from "@copilotkit/core";
import type { OpenGenerativeUIConfig } from "./open-generative-ui";
import type {
Catalog,
LitComponentImplementation,
LitRenderable,
Theme as A2UITheme,
} from "@copilotkit/a2ui-renderer/web-components";
export interface A2UIConfig {
theme?: A2UITheme;
catalog?: Catalog<LitComponentImplementation>;
loadingComponent?: () => LitRenderable;
includeSchema?: boolean;
}
export interface CopilotKitConfig {
runtimeUrl?: string;
headers?: Record<string, string>;
licenseKey?: string;
properties?: Record<string, unknown>;
agents?: Record<string, AbstractAgent>;
selfManagedAgents?: Record<string, AbstractAgent>;
tools?: ClientTool[];
renderToolCalls?: RenderToolCallConfig[];
renderActivityMessages?: RenderActivityMessageConfig[];
suggestionsConfig?: SuggestionsConfig[];
frontendTools?: FrontendToolConfig[];
humanInTheLoop?: HumanInTheLoopConfig[];
a2ui?: A2UIConfig;
openGenerativeUI?: OpenGenerativeUIConfig;
}
const COPILOT_CLOUD_PUBLIC_API_KEY_HEADER = "X-CopilotCloud-Public-Api-Key";
const COPILOT_CLOUD_PUBLIC_API_KEY_REGEX = /^ck_pub_[0-9a-f]{32}$/i;
const LICENSE_WATERMARK_LOG_FLAG = "__copilotkitAngularLicenseWatermarkLogged";
type ResolvedLicense = {
key?: string;
valid: boolean;
warning?: string;
};
function logLicenseWatermarkWarning(message: string): void {
const globalWindow = globalThis as typeof globalThis & {
[LICENSE_WATERMARK_LOG_FLAG]?: boolean;
};
if (globalWindow[LICENSE_WATERMARK_LOG_FLAG]) {
return;
}
globalWindow[LICENSE_WATERMARK_LOG_FLAG] = true;
console.warn(
[
"========================================",
"[CopilotKit] License Required",
message,
"Get your CopilotCloud license key and add it as `licenseKey` to remove this watermark.",
"========================================",
].join("\n"),
);
}
function resolveLicense(config: CopilotKitConfig): ResolvedLicense {
const headerKey = config.headers?.[COPILOT_CLOUD_PUBLIC_API_KEY_HEADER];
const key = config.licenseKey ?? headerKey;
if (!key) {
return {
valid: false,
warning:
"No CopilotCloud license key was found. A watermark will be shown until one is added.",
};
}
if (!COPILOT_CLOUD_PUBLIC_API_KEY_REGEX.test(key)) {
return {
key,
valid: false,
warning:
"Your CopilotCloud license key appears invalid. A watermark will be shown until a valid key is added.",
};
}
return { key, valid: true };
}
export const COPILOT_KIT_CONFIG = new InjectionToken<CopilotKitConfig>(
"COPILOT_KIT_CONFIG",
);
export function injectCopilotKitConfig(): CopilotKitConfig {
return inject(COPILOT_KIT_CONFIG);
}
export function provideCopilotKit(config: CopilotKitConfig): Provider {
const resolvedLicense = resolveLicense(config);
const headers = config.headers ?? {};
if (
LICENSE_WATERMARK_ENABLED &&
!resolvedLicense.valid &&
resolvedLicense.warning
) {
logLicenseWatermarkWarning(resolvedLicense.warning);
}
const mergedHeaders = headers[COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]
? headers
: !resolvedLicense.valid || !resolvedLicense.key
? headers
: {
...headers,
[COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: resolvedLicense.key,
};
return {
provide: COPILOT_KIT_CONFIG,
useValue: {
...config,
headers: mergedHeaders,
},
};
}