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
113 lines (99 loc) · 3.07 KB
/
Copy pathconfig.ts
File metadata and controls
113 lines (99 loc) · 3.07 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
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";
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[];
frontendTools?: FrontendToolConfig[];
humanInTheLoop?: HumanInTheLoopConfig[];
}
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,
},
};
}