Elffuss BraidHub

kikocis/continue public

960dd3a6efa1 · 3.9 KiBRawPermalink
1import { MessageModes, ModelDescription } from "core";
2import { ProfileDescription } from "core/config/ProfileLifecycleManager";
3import {
4 DEFAULT_AGENT_SYSTEM_MESSAGE,
5 DEFAULT_CHAT_SYSTEM_MESSAGE,
6} from "core/llm/constructMessages";
7import _ from "lodash";
8import { KeyboardEvent as ReactKeyboardEvent } from "react";
9import { getLocalStorage } from "./localStorage";
10
11export type Platform = "mac" | "linux" | "windows" | "unknown";
12
13export function getPlatform(): Platform {
14 const platform = window.navigator.platform.toUpperCase();
15 if (platform.indexOf("MAC") >= 0) {
16 return "mac";
17 } else if (platform.indexOf("LINUX") >= 0) {
18 return "linux";
19 } else if (platform.indexOf("WIN") >= 0) {
20 return "windows";
21 } else {
22 return "unknown";
23 }
24}
25
26export function isMetaEquivalentKeyPressed({
27 metaKey,
28 ctrlKey,
29}: KeyboardEvent | ReactKeyboardEvent): boolean {
30 const platform = getPlatform();
31 switch (platform) {
32 case "mac":
33 return metaKey;
34 case "linux":
35 case "windows":
36 return ctrlKey;
37 default:
38 return metaKey;
39 }
40}
41
42export function getMetaKeyLabel(): string {
43 return getPlatform() === "mac" ? "⌘" : "Ctrl";
44}
45
46export function getAltKeyLabel(): string {
47 const platform = getPlatform();
48 switch (platform) {
49 case "mac":
50 return "⌥";
51 default:
52 return "Alt";
53 }
54}
55
56export function getFontSize(): number {
57 return getLocalStorage("fontSize") ?? (isJetBrains() ? 15 : 14);
58}
59
60export function fontSize(n: number): string {
61 return `${getFontSize() + n}px`;
62}
63
64export function isJetBrains() {
65 return getLocalStorage("ide") === "jetbrains";
66}
67
68export function isWebEnvironment(): boolean {
69 return (
70 typeof window !== "undefined" &&
71 window.navigator &&
72 window.navigator.userAgent.indexOf("Electron") === -1
73 );
74}
75
76export function isPrerelease() {
77 const extensionVersion = getLocalStorage("extensionVersion");
78 if (!extensionVersion) {
79 console.warn(
80 `Could not find extension version in local storage, assuming it's a prerelease`,
81 );
82 return true;
83 }
84 const minor = parseInt(extensionVersion.split(".")[1], 10);
85 if (minor % 2 !== 0) {
86 return true;
87 }
88 return false;
89}
90
91/**
92 * Updates the values of an object's properties based on the specified paths.
93 *
94 * This function creates a deep clone of the provided object and updates its properties
95 * based on the given path-to-value mappings. If a value in the mapping is a function,
96 * it will be used to determine the new value for the property. Otherwise, the value itself
97 * will be set for the property.
98 *
99 * @param {Object} old - The original object to be cloned and updated.
100 * @param {Object} pathToValue - An object where the keys represent the paths to the properties
101 * to be updated and the values represent the new values or functions
102 * to determine the new values.
103 * @returns {Object} A new object with the updated values.
104 */
105export function updatedObj(old: any, pathToValue: { [key: string]: any }) {
106 const newObject = _.cloneDeep(old);
107
108 for (const key in pathToValue) {
109 if (typeof pathToValue[key] === "function") {
110 _.updateWith(newObject, key, pathToValue[key]);
111 } else {
112 _.updateWith(newObject, key, (__) => pathToValue[key]);
113 }
114 }
115
116 return newObject;
117}
118
119export function isLocalProfile(profile: ProfileDescription): boolean {
120 return profile.profileType === "local";
121}
122
123/**
124 * Get the base system message for the agent or chat mode from the model description.
125 */
126export function getBaseSystemMessage(
127 modelDetails: ModelDescription | null,
128 mode: MessageModes,
129) {
130 let baseChatOrAgentSystemMessage: string | undefined;
131 if (mode === "agent") {
132 baseChatOrAgentSystemMessage =
133 modelDetails?.baseAgentSystemMessage ?? DEFAULT_AGENT_SYSTEM_MESSAGE;
134 } else {
135 baseChatOrAgentSystemMessage =
136 modelDetails?.baseChatSystemMessage ?? DEFAULT_CHAT_SYSTEM_MESSAGE;
137 }
138 return baseChatOrAgentSystemMessage;
139}