From d01e7f32650bbba08dd4c804535441d66155fe55 Mon Sep 17 00:00:00 2001 From: YuShifan <894402575bt@gmail.com> Date: Wed, 27 Dec 2023 14:47:13 +0800 Subject: [PATCH 1/2] feat(settings): add enable copilot switch --- src/background.ts | 1 + src/database/database.config.ts | 2 + .../migration/1703659148195-enableCopilot.ts | 122 ++++++++++++++++++ src/database/models/SettingEntity.ts | 3 + src/lang/settings.ts | 7 + src/store/getter.ts | 1 + src/store/modules/app.ts | 11 ++ src/types/global.d.ts | 1 + src/views/connections/ConnectionsDetail.vue | 11 +- src/views/settings/index.vue | 24 ++++ 10 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 src/database/migration/1703659148195-enableCopilot.ts diff --git a/src/background.ts b/src/background.ts index 5aa239295..53832c0e6 100644 --- a/src/background.ts +++ b/src/background.ts @@ -126,6 +126,7 @@ async function createWindow() { syncOsTheme: setting.syncOsTheme, multiTopics: setting.multiTopics, jsonHighlight: setting.jsonHighlight, + enableCopilot: setting.enableCopilot, openAIAPIKey: setting.openAIAPIKey, model: setting.model, } diff --git a/src/database/database.config.ts b/src/database/database.config.ts index 74e318474..4ec8d6df5 100644 --- a/src/database/database.config.ts +++ b/src/database/database.config.ts @@ -41,6 +41,7 @@ import { jsonHighlight1691071794840 } from './migration/1691071794840-jsonHighli import { ALPNProtocols1691817588169 } from './migration/1691817588169-ALPNProtocols' import { aiSettings1701761407723 } from './migration/1701761407723-aiSettings' import { aiTables1701936842016 } from './migration/1701936842016-aiTables' +import { enableCopilot1703659148195 } from './migration/1703659148195-enableCopilot' const STORE_PATH = getAppDataPath('MQTTX') try { @@ -88,6 +89,7 @@ const ORMConfig = { ALPNProtocols1691817588169, aiSettings1701761407723, aiTables1701936842016, + enableCopilot1703659148195, ], migrationsTableName: 'temp_migration_table', entities: [ diff --git a/src/database/migration/1703659148195-enableCopilot.ts b/src/database/migration/1703659148195-enableCopilot.ts new file mode 100644 index 000000000..946dd904d --- /dev/null +++ b/src/database/migration/1703659148195-enableCopilot.ts @@ -0,0 +1,122 @@ +import { MigrationInterface, QueryRunner } from 'typeorm' + +export class enableCopilot1703659148195 implements MigrationInterface { + name = 'enableCopilot1703659148195' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE TABLE "temporary_SettingEntity" ( + "id" varchar PRIMARY KEY NOT NULL, + "width" integer NOT NULL DEFAULT (1025), + "height" integer NOT NULL DEFAULT (749), + "autoCheck" boolean NOT NULL DEFAULT (1), + "currentLang" varchar CHECK(currentLang IN ('zh', 'en', 'ja', 'tr', 'hu')) NOT NULL DEFAULT ('en'), + "currentTheme" varchar CHECK(currentTheme IN ('light', 'dark', 'night')) NOT NULL DEFAULT ('light'), + "maxReconnectTimes" integer NOT NULL DEFAULT (10), + "autoResub" boolean NOT NULL DEFAULT (1), + "syncOsTheme" boolean NOT NULL DEFAULT (0), + "multiTopics" boolean NOT NULL DEFAULT (1), + "jsonHighlight" boolean NOT NULL DEFAULT (1), + "openAIAPIKey" varchar NOT NULL DEFAULT (''), + "model" varchar NOT NULL DEFAULT ('gpt-3.5-turbo'), + "enableCopilot" boolean NOT NULL DEFAULT (1) + ) + `) + await queryRunner.query(` + INSERT INTO "temporary_SettingEntity"( + "id", + "width", + "height", + "autoCheck", + "currentLang", + "currentTheme", + "maxReconnectTimes", + "autoResub", + "syncOsTheme", + "multiTopics", + "jsonHighlight", + "openAIAPIKey", + "model" + ) + SELECT "id", + "width", + "height", + "autoCheck", + "currentLang", + "currentTheme", + "maxReconnectTimes", + "autoResub", + "syncOsTheme", + "multiTopics", + "jsonHighlight", + "openAIAPIKey", + "model" + FROM "SettingEntity" + `) + await queryRunner.query(` + DROP TABLE "SettingEntity" + `) + await queryRunner.query(` + ALTER TABLE "temporary_SettingEntity" + RENAME TO "SettingEntity" + `) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + ALTER TABLE "SettingEntity" + RENAME TO "temporary_SettingEntity" + `) + await queryRunner.query(` + CREATE TABLE "SettingEntity" ( + "id" varchar PRIMARY KEY NOT NULL, + "width" integer NOT NULL DEFAULT (1025), + "height" integer NOT NULL DEFAULT (749), + "autoCheck" boolean NOT NULL DEFAULT (1), + "currentLang" varchar CHECK(currentLang IN ('zh', 'en', 'ja', 'tr', 'hu')) NOT NULL DEFAULT ('en'), + "currentTheme" varchar CHECK(currentTheme IN ('light', 'dark', 'night')) NOT NULL DEFAULT ('light'), + "maxReconnectTimes" integer NOT NULL DEFAULT (10), + "autoResub" boolean NOT NULL DEFAULT (1), + "syncOsTheme" boolean NOT NULL DEFAULT (0), + "multiTopics" boolean NOT NULL DEFAULT (1), + "jsonHighlight" boolean NOT NULL DEFAULT (1), + "openAIAPIKey" varchar NOT NULL DEFAULT (''), + "model" varchar NOT NULL DEFAULT ('gpt-3.5-turbo') + ) + `) + await queryRunner.query(` + INSERT INTO "SettingEntity"( + "id", + "width", + "height", + "autoCheck", + "currentLang", + "currentTheme", + "maxReconnectTimes", + "autoResub", + "syncOsTheme", + "multiTopics", + "jsonHighlight", + "openAIAPIKey", + "model" + ) + SELECT "id", + "width", + "height", + "autoCheck", + "currentLang", + "currentTheme", + "maxReconnectTimes", + "autoResub", + "syncOsTheme", + "multiTopics", + "jsonHighlight", + "openAIAPIKey", + "model" + FROM "temporary_SettingEntity" + `) + await queryRunner.query(` + DROP TABLE "temporary_SettingEntity" + `) + } +} diff --git a/src/database/models/SettingEntity.ts b/src/database/models/SettingEntity.ts index ddfac7b42..7e8ffcde5 100644 --- a/src/database/models/SettingEntity.ts +++ b/src/database/models/SettingEntity.ts @@ -35,6 +35,9 @@ export default class SettingEntity { @Column({ type: 'boolean', default: true }) jsonHighlight!: boolean + @Column({ type: 'boolean', default: true }) + enableCopilot!: boolean + @Column({ type: 'varchar', default: '' }) openAIAPIKey!: string diff --git a/src/lang/settings.ts b/src/lang/settings.ts index 34a41bbbb..7bd7dbf27 100644 --- a/src/lang/settings.ts +++ b/src/lang/settings.ts @@ -174,4 +174,11 @@ export default { ja: 'モデル', hu: 'Modell', }, + enableCopilot: { + zh: '启用 Copilot', + en: 'Enable Copilot', + tr: "Copilot'u Etkinleştir", + ja: 'Copilotを有効にする', + hu: 'Copilot engedélyezése', + }, } diff --git a/src/store/getter.ts b/src/store/getter.ts index 821974b6d..c2ee4fba3 100644 --- a/src/store/getter.ts +++ b/src/store/getter.ts @@ -16,6 +16,7 @@ const getters = { currentScript: (state: State) => state.app.currentScript, multiTopics: (state: State) => state.app.multiTopics, jsonHighlight: (state: State) => state.app.jsonHighlight, + enableCopilot: (state: State) => state.app.enableCopilot, openAIAPIKey: (state: State) => state.app.openAIAPIKey, model: (state: State) => state.app.model, isPrismButtonAdded: (state: State) => state.app.isPrismButtonAdded, diff --git a/src/store/modules/app.ts b/src/store/modules/app.ts index 032c75fa4..259243b68 100644 --- a/src/store/modules/app.ts +++ b/src/store/modules/app.ts @@ -24,6 +24,7 @@ const TOGGLE_JSON_HIGHLIGHT = 'TOGGLE_JSON_HIGHLIGHT' const SET_OPEN_AI_API_KEY = 'SET_OPEN_AI_API_KEY' const SET_MODEL = 'SET_MODEL' const SET_INSERT_BUTTON_ADDED = 'SET_INSERT_BUTTON_ADDED' +const TOGGLE_ENABLE_COPILOT = 'TOGGLE_ENABLE_COPILOT' const getShowSubscriptions = (): boolean => { const $showSubscriptions: string | null = localStorage.getItem('showSubscriptions') @@ -54,6 +55,7 @@ const app = { willMessageVisible: true, currentScript: null, currentConnectionId: null, + enableCopilot: settingData.enableCopilot, openAIAPIKey: settingData.openAIAPIKey || '', model: settingData.model || 'gpt-3.5-turbo', isPrismButtonAdded: false, @@ -149,6 +151,9 @@ const app = { [SET_INSERT_BUTTON_ADDED](state: App, isPrismButtonAdded: boolean) { state.isPrismButtonAdded = isPrismButtonAdded }, + [TOGGLE_ENABLE_COPILOT](state: App, enableCopilot: boolean) { + state.enableCopilot = enableCopilot + }, }, actions: { async TOGGLE_THEME({ commit }: any, payload: App) { @@ -232,6 +237,12 @@ const app = { async SET_CURRENT_CONNECTION_ID({ commit }: any, currentConnectionId: string) { commit(SET_CURRENT_CONNECTION_ID, currentConnectionId) }, + async TOGGLE_ENABLE_COPILOT({ commit }: any, payload: App) { + const { settingService } = useServices() + commit(TOGGLE_ENABLE_COPILOT, payload.enableCopilot) + settingData.enableCopilot = payload.enableCopilot + await settingService.update(payload) + }, async SET_OPEN_AI_API_KEY({ commit }: any, payload: App) { const { settingService } = useServices() commit(SET_OPEN_AI_API_KEY, payload.openAIAPIKey) diff --git a/src/types/global.d.ts b/src/types/global.d.ts index 2ae47426b..65123f51c 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -105,6 +105,7 @@ declare global { currentConnectionId: string | null connectionTreeState: ConnectionTreeStateMap jsonHighlight: boolean + enableCopilot: boolean openAIAPIKey: string model: AIModel isPrismButtonAdded: boolean diff --git a/src/views/connections/ConnectionsDetail.vue b/src/views/connections/ConnectionsDetail.vue index 3a02fa756..072f6daa3 100644 --- a/src/views/connections/ConnectionsDetail.vue +++ b/src/views/connections/ConnectionsDetail.vue @@ -1,6 +1,6 @@