Skip to content

Commit

Permalink
feat: add doc setting for mention
Browse files Browse the repository at this point in the history
  • Loading branch information
2214962083 committed Dec 10, 2024
1 parent 0053311 commit 60024ea
Show file tree
Hide file tree
Showing 34 changed files with 566 additions and 761 deletions.
75 changes: 0 additions & 75 deletions src/shared/plugins/base/client/client-plugin-context.ts

This file was deleted.

117 changes: 117 additions & 0 deletions src/shared/plugins/base/client/client-plugin-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import React, { createContext, useContext, useRef } from 'react'
import { useCallbackRef } from '@webview/hooks/use-callback-ref'
import { useImmer } from 'use-immer'

import { ProviderUtils } from '../provider-manager'
import { PluginId, PluginState } from '../types'
import type { ClientPluginProviderMap } from './client-plugin-types'

type ProviderKey = keyof ClientPluginProviderMap

type IdProviderMap = Record<
PluginId,
() => ClientPluginProviderMap[ProviderKey]
>

export interface ClientPluginContextValue {
state: Record<PluginId, PluginState>
getState: () => Record<PluginId, PluginState>
setState: (
pluginId: PluginId,
updater: PluginState | ((draft: PluginState) => void)
) => void
registerProvider: <K extends ProviderKey>(
pluginId: PluginId,
providerKey: K,
provider: () => ClientPluginProviderMap[K]
) => void
getProviders: <K extends ProviderKey>(
providerKey: K
) => ClientPluginProviderMap[K][]
mergeProviders: <K extends ProviderKey>(
providerKey: K
) => ClientPluginProviderMap[K] | undefined
}

const ClientPluginContext = createContext<ClientPluginContextValue | null>(null)

export const ClientPluginProvider: React.FC<React.PropsWithChildren> = ({
children
}) => {
const [state, setState] = useImmer<Record<PluginId, PluginState>>(
{} as Record<PluginId, PluginState>
)
const providerKeyInfoMapRef = useRef({} as Record<ProviderKey, IdProviderMap>)

const setPluginState = (
pluginId: PluginId,
updater: PluginState | ((draft: PluginState) => void)
) => {
setState(draft => {
if (!draft[pluginId]) {
draft[pluginId] = {}
}
if (typeof updater === 'function') {
updater(draft[pluginId])
} else {
draft[pluginId] = updater
}
})
}

const registerProvider = <K extends keyof ClientPluginProviderMap>(
pluginId: PluginId,
providerKey: K,
provider: () => ClientPluginProviderMap[K]
) => {
if (!providerKeyInfoMapRef.current[providerKey]) {
providerKeyInfoMapRef.current[providerKey] = {} as IdProviderMap
}
providerKeyInfoMapRef.current[providerKey]![pluginId] = provider
}

const getProviders = <K extends keyof ClientPluginProviderMap>(
providerKey: K
): ClientPluginProviderMap[K][] => {
const idProviderMap = (providerKeyInfoMapRef.current[providerKey] ||
{}) as Record<PluginId, () => ClientPluginProviderMap[K]>

return ProviderUtils.getValues(idProviderMap)
}

const mergeProviders = <K extends keyof ClientPluginProviderMap>(
providerKey: K
): ClientPluginProviderMap[K] | undefined => {
const idProviderMap = (providerKeyInfoMapRef.current[providerKey] ||
{}) as Record<PluginId, () => ClientPluginProviderMap[K]>

const result = ProviderUtils.mergeAll(idProviderMap)

return result
}

const getState = useCallbackRef(() => state)

return (
<ClientPluginContext.Provider
value={{
state,
getState,
setState: setPluginState,
registerProvider,
getProviders,
mergeProviders
}}
>
{children}
</ClientPluginContext.Provider>
)
}

export const usePlugin = () => {
const context = useContext(ClientPluginContext)
if (!context) {
throw new Error('usePlugin must be used within ClientPluginProvider')
}
return context
}
143 changes: 0 additions & 143 deletions src/shared/plugins/base/client/client-plugin-registry.ts

This file was deleted.

30 changes: 30 additions & 0 deletions src/shared/plugins/base/client/client-plugin-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { FC } from 'react'
import type { BaseConversationLog, ConversationLog } from '@shared/entities'
import type { ImageInfo } from '@shared/plugins/fs-plugin/types'
import type { FileInfo, MentionOption } from '@webview/types/chat'

export type UseMentionOptionsReturns = MentionOption[]

export type UseSelectedFilesReturns = {
selectedFiles: FileInfo[]
setSelectedFiles: (files: FileInfo[]) => void
}

export type UseSelectedImagesReturns = {
selectedImages: ImageInfo[]
addSelectedImage: (image: ImageInfo) => void
removeSelectedImage: (image: ImageInfo) => void
}

export type CustomRenderLogPreviewProps<
T extends BaseConversationLog = ConversationLog
> = {
log: T
}

export type ClientPluginProviderMap = {
useMentionOptions: () => UseMentionOptionsReturns
useSelectedFiles: () => UseSelectedFilesReturns
useSelectedImages: () => UseSelectedImagesReturns
CustomRenderLogPreview: FC<CustomRenderLogPreviewProps>
}
12 changes: 6 additions & 6 deletions src/shared/plugins/base/client/create-client-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { GitClientPlugin } from '@shared/plugins/git-plugin/client/git-client-pl
import { TerminalClientPlugin } from '@shared/plugins/terminal-plugin/client/terminal-client-plugin'
import { WebClientPlugin } from '@shared/plugins/web-plugin/client/web-client-plugin'

import type { ClientPlugin } from './client-plugin-context'
import type { ClientPlugin } from './use-client-plugin'

export const createClientPlugins = (): ClientPlugin[] => {
const plugins: ClientPlugin[] = [
new FsClientPlugin(),
new DocClientPlugin(),
new WebClientPlugin(),
new GitClientPlugin(),
new TerminalClientPlugin()
FsClientPlugin,
DocClientPlugin,
WebClientPlugin,
GitClientPlugin,
TerminalClientPlugin
]

return plugins
Expand Down
Loading

0 comments on commit 60024ea

Please sign in to comment.