-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0053311
commit 60024ea
Showing
34 changed files
with
566 additions
and
761 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
src/shared/plugins/base/client/client-plugin-context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
143
src/shared/plugins/base/client/client-plugin-registry.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.