Skip to content

Commit

Permalink
feat: add setting preview for global search
Browse files Browse the repository at this point in the history
  • Loading branch information
2214962083 committed Dec 5, 2024
1 parent a2ad9d0 commit 207e2aa
Show file tree
Hide file tree
Showing 33 changed files with 1,020 additions and 759 deletions.
34 changes: 33 additions & 1 deletion src/extension/webview-api/controllers/settings-controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { SettingKey, SettingValue } from '@shared/entities'
import { settingKeyItemConfigMap } from '@shared/entities'
import type {
SettingKey,
SettingsSaveType,
SettingValue
} from '@shared/entities'

import { globalSettingsDB, workspaceSettingsDB } from '../lowdb/settings-db'
import { Controller } from '../types'
Expand Down Expand Up @@ -58,6 +63,33 @@ export class SettingsController extends Controller {
}
}

private async getSaveType(key: SettingKey): Promise<SettingsSaveType> {
return settingKeyItemConfigMap[key].saveType
}

async getSetting(req: {
key: SettingKey
}): Promise<SettingValue<SettingKey> | null> {
const saveType = await this.getSaveType(req.key)
return saveType === 'global'
? await globalSettingsDB.getSetting(req.key)
: await workspaceSettingsDB.getSetting(req.key)
}

async setSettings(req: {
settings: Partial<Record<SettingKey, SettingValue<SettingKey>>>
}): Promise<void> {
const { settings } = req
for (const [key, value] of Object.entries(settings)) {
const saveType = await this.getSaveType(key as SettingKey)
if (saveType === 'global') {
await globalSettingsDB.setSetting(key as SettingKey, value)
} else {
await workspaceSettingsDB.setSetting(key as SettingKey, value)
}
}
}

async getMergedSettings(): Promise<Record<string, any>> {
const globalSettings = await globalSettingsDB.getAllSettings()
const workspaceSettings = await workspaceSettingsDB.getAllSettings()
Expand Down
15 changes: 4 additions & 11 deletions src/extension/webview-api/lowdb/settings-db.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from 'path'
import { aidePaths } from '@extension/file-utils/paths'
import {
settingKeyItemConfigMap,
settingsConfig,
SettingsEntity,
type SettingCategory,
type SettingKey,
type Settings,
type SettingValue
} from '@shared/entities/settings-entity'
} from '@shared/entities'

import { BaseDB } from './base-db'

Expand All @@ -28,7 +28,6 @@ class SettingsDB extends BaseDB<Settings> {
): Promise<Settings> {
const existingSettings = await this.getAll()
const existing = existingSettings.find(s => s.key === key)
const config = settingsConfig[key]

if (existing) {
return this.update(existing.id, {
Expand All @@ -40,7 +39,6 @@ class SettingsDB extends BaseDB<Settings> {
const setting = new SettingsEntity({
key,
value,
category: config.category,
updatedAt: Date.now()
}).entity

Expand All @@ -54,7 +52,7 @@ class SettingsDB extends BaseDB<Settings> {
const setting = settings.find(s => s.key === key)
return setting
? (setting.value as SettingValue<K>)
: (settingsConfig[key].defaultValue as SettingValue<K>)
: settingKeyItemConfigMap[key].renderOptions.defaultValue
}

async getAllSettings(): Promise<Record<string, any>> {
Expand All @@ -78,13 +76,8 @@ class SettingsDB extends BaseDB<Settings> {
return { ...defaults, ...userSettings }
}

async getSettingsByCategory(category: SettingCategory): Promise<Settings[]> {
const settings = await this.getAll()
return settings.filter(setting => setting.category === category)
}

getSettingConfig<K extends SettingKey>(key: K) {
return settingsConfig[key]
return settingKeyItemConfigMap[key]
}

getAllSettingConfigs() {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export * from './chat-context-entity'
export * from './chat-session-entity'
export * from './conversation-entity'
export * from './doc-site-entity'
export * from './settings-entity'
export * from './setting-entity'
22 changes: 22 additions & 0 deletions src/shared/entities/setting-entity/entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { v4 as uuidv4 } from 'uuid'

import { BaseEntity, type IBaseEntity } from '../base-entity'
import type { SettingKey, SettingValue } from './setting-config'

export interface Settings extends IBaseEntity {
key: SettingKey
value: SettingValue<SettingKey>
updatedAt: number
}

export class SettingsEntity extends BaseEntity<Settings> {
protected getDefaults(data?: Partial<Settings>): Settings {
return {
id: uuidv4(),
key: 'unknown' as SettingKey,
value: 'unknown',
updatedAt: Date.now(),
...data
}
}
}
5 changes: 5 additions & 0 deletions src/shared/entities/setting-entity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './entity'
export * from './render-options'
export * from './setting-config'
export * from './setting-items-config'
export * from './types'
51 changes: 51 additions & 0 deletions src/shared/entities/setting-entity/render-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export type SettingsSaveType = 'global' | 'workspace'

interface BaseRenderOptions<FormType, ValueType> {
type: FormType
label: string
description: string
placeholder?: string
defaultValue: ValueType
}

export type InputRenderOptions = BaseRenderOptions<'input', string>
export type TextareaRenderOptions = BaseRenderOptions<'textarea', string>
export type SwitchRenderOptions = BaseRenderOptions<'switch', boolean>
export type NumberInputRenderOptions = BaseRenderOptions<'numberInput', number>
export type SelectInputRenderOptions = BaseRenderOptions<
'selectInput',
string
> & {
options: Array<string | { label: string; value: string }>
}
export type ArrayInputRenderOptions = BaseRenderOptions<'arrayInput', any[]>
export type ObjectInputRenderOptions = BaseRenderOptions<
'objectInput',
Record<string, any>
>
export type ModelManagementRenderOptions = BaseRenderOptions<
'modelManagement',
any
>
export type DocIndexingRenderOptions = BaseRenderOptions<'docManagement', any>
export type CodebaseIndexingRenderOptions = BaseRenderOptions<
'codebaseIndexing',
any
>

export type RenderOptions =
| InputRenderOptions
| TextareaRenderOptions
| SwitchRenderOptions
| NumberInputRenderOptions
| SelectInputRenderOptions
| ArrayInputRenderOptions
| ObjectInputRenderOptions
| ModelManagementRenderOptions
| DocIndexingRenderOptions
| CodebaseIndexingRenderOptions

export type RenderOptionsType = RenderOptions['type']
export type RenderOptionsMap = {
[T in RenderOptionsType]: Extract<RenderOptions, { type: T }>
}
126 changes: 126 additions & 0 deletions src/shared/entities/setting-entity/setting-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import type { ValueUnion } from '@shared/types/common'

import * as settingItemsConfig from './setting-items-config'
import {
aiCommandAutoRunConfig,
aiCommandConfig,
aiCommandCopyBeforeRunConfig,
aiPromptConfig,
apiConcurrencyConfig,
autoRememberConvertLanguagePairsConfig,
codeViewerHelperPromptConfig,
convertLanguagePairsConfig,
docManagementConfig,
expertCodeEnhancerPromptListConfig,
ignorePatternsConfig,
modelsConfig,
openaiBaseUrlConfig,
openaiKeyConfig,
openaiModelConfig,
readClipboardImageConfig,
respectGitIgnoreConfig,
useSystemProxyConfig
} from './setting-items-config'
import type { SettingConfig, SettingConfigItem } from './types'

// Setting groups and pages configuration
export const settingsConfig: SettingConfig = {
pages: [
{
id: 'general',
label: 'General',
settings: [
openaiKeyConfig,
openaiModelConfig,
openaiBaseUrlConfig,
apiConcurrencyConfig,
useSystemProxyConfig,
ignorePatternsConfig,
respectGitIgnoreConfig
]
}
],
groups: [
{
id: 'chat',
label: 'Chat',
pages: [
{
id: 'chatModel',
label: 'AI Models',
settings: [modelsConfig]
},
{
id: 'chatDoc',
label: 'Doc Sites Indexing',
settings: [docManagementConfig]
}
]
},
{
id: 'tools',
label: 'Tools',
pages: [
{
id: 'copyAsPrompt',
label: 'Copy As Prompt',
settings: [aiPromptConfig]
},
{
id: 'codeConvert',
label: 'Code Convert',
settings: [
convertLanguagePairsConfig,
autoRememberConvertLanguagePairsConfig
]
},
{
id: 'codeViewerHelper',
label: 'Code Viewer Helper',
settings: [codeViewerHelperPromptConfig]
},
{
id: 'expertCodeEnhancer',
label: 'Expert Code Enhancer',
settings: [expertCodeEnhancerPromptListConfig]
},
{
id: 'smartPaste',
label: 'Smart Paste',
settings: [readClipboardImageConfig]
},
{
id: 'askAI',
label: 'Ask AI',
settings: [
aiCommandConfig,
aiCommandCopyBeforeRunConfig,
aiCommandAutoRunConfig
]
}
]
}
]
}

type SettingItemsConfig = typeof settingItemsConfig
export type SettingKey = ValueUnion<SettingItemsConfig>['key']

type SettingItemConfigFromKey<K extends SettingKey> = SettingConfigItem<
Extract<ValueUnion<SettingItemsConfig>, { key: K }>['renderOptions']['type']
>

export type SettingValue<K extends SettingKey> =
SettingItemConfigFromKey<K>['renderOptions']['defaultValue']

export const settingKeyItemConfigMap = Object.values(settingItemsConfig).reduce(
(acc, item) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
acc[item.key] = item
return acc
},
{} as {
[K in SettingKey]: SettingItemConfigFromKey<K>
}
)
Loading

0 comments on commit 207e2aa

Please sign in to comment.