forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
93 lines (82 loc) · 2.63 KB
/
Copy pathutils.ts
File metadata and controls
93 lines (82 loc) · 2.63 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
import { CopilotContextParams, defaultCopilotContextCategories } from "@copilotkit/react-core";
import { CopilotKitVersion } from "./types";
export function shouldShowDevConsole(showDevConsole: boolean | "auto"): boolean {
if (typeof showDevConsole === "boolean") {
return showDevConsole;
}
return (
getHostname() === "localhost" ||
getHostname() === "127.0.0.1" ||
getHostname() === "0.0.0.0" ||
getHostname() === "::1"
);
}
function getHostname(): string {
if (typeof window !== "undefined" && window.location) {
return window.location.hostname;
}
return "";
}
export async function getPublishedCopilotKitVersion(
current: string,
forceCheck: boolean = false,
): Promise<CopilotKitVersion> {
const LOCAL_STORAGE_KEY = "__copilotkit_version_check__";
const serializedVersion = localStorage.getItem(LOCAL_STORAGE_KEY);
if (serializedVersion && !forceCheck) {
try {
const parsedVersion: CopilotKitVersion = JSON.parse(serializedVersion);
const oneHour = 60 * 60 * 1000;
const now = new Date().getTime();
if (
parsedVersion.current === current &&
now - new Date(parsedVersion.lastChecked).getTime() < oneHour
) {
return parsedVersion;
}
} catch (error) {
console.error("Failed to parse CopilotKitVersion from localStorage", error);
}
}
try {
const response = await fetch("https://api.cloud.stagingcopilotkit.ai/check-for-updates", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
packages: [
{
packageName: "@copilotkit/shared",
packageVersion: current,
},
],
}),
});
const data = await response.json();
const version: CopilotKitVersion = {
current,
lastChecked: new Date().getTime(),
latest: data.packages[0].latestVersion,
severity: data.packages[0].severity,
advisory: data.packages[0].advisory || null,
};
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(version));
return version;
} catch (error) {
console.error("Failed to check for updates", error);
throw error;
}
}
export function logReadables(context: CopilotContextParams) {
console.log(context.getContextString([], defaultCopilotContextCategories));
}
export function logActions(context: CopilotContextParams) {
for (const action of Object.values(context.actions)) {
console.group(action.name);
console.log("name", action.name);
console.log("description", action.description);
console.log("parameters", action.parameters);
console.groupEnd();
}
}