| 1 | import { MessageModes, ModelDescription } from "core"; |
| 2 | import { ProfileDescription } from "core/config/ProfileLifecycleManager"; |
| 3 | import _ from "lodash"; |
| 4 | import { KeyboardEvent } from "react"; |
| 5 | import { getLocalStorage } from "./localStorage"; |
| 6 | import { |
| 7 | DEFAULT_CHAT_SYSTEM_MESSAGE, |
| 8 | DEFAULT_AGENT_SYSTEM_MESSAGE, |
| 9 | } from "core/llm/constructMessages"; |
| 10 | |
| 11 | export type Platform = "mac" | "linux" | "windows" | "unknown"; |
| 12 | |
| 13 | export 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 | |
| 26 | export function isMetaEquivalentKeyPressed({ |
| 27 | metaKey, |
| 28 | ctrlKey, |
| 29 | }: KeyboardEvent): 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 | |
| 42 | export function getMetaKeyLabel(): string { |
| 43 | return getPlatform() === "mac" ? "⌘" : "Ctrl"; |
| 44 | } |
| 45 | |
| 46 | export function getAltKeyLabel(): string { |
| 47 | const platform = getPlatform(); |
| 48 | switch (platform) { |
| 49 | case "mac": |
| 50 | return "⌥"; |
| 51 | default: |
| 52 | return "Alt"; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | export function getFontSize(): number { |
| 57 | return getLocalStorage("fontSize") ?? (isJetBrains() ? 15 : 14); |
| 58 | } |
| 59 | |
| 60 | export function fontSize(n: number): string { |
| 61 | return `${getFontSize() + n}px`; |
| 62 | } |
| 63 | |
| 64 | export function isJetBrains() { |
| 65 | return getLocalStorage("ide") === "jetbrains"; |
| 66 | } |
| 67 | |
| 68 | export function isWebEnvironment(): boolean { |
| 69 | return ( |
| 70 | typeof window !== "undefined" && |
| 71 | window.navigator && |
| 72 | window.navigator.userAgent.indexOf("Electron") === -1 |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | export 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 | */ |
| 105 | export 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 | |
| 119 | export 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 | */ |
| 126 | export 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 | } |