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