Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: support standalone plugin mode #356

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/app/chat/(desktop)/features/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const ChatInputDesktopLayout = memo(() => {

return (
<DraggablePanel
expandable={false}
fullscreen={expand}
headerHeight={HEADER_HEIGHT}
minHeight={CHAT_TEXTAREA_HEIGHT}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const PluginSettings: RenderErrorMessage = memo(({ id, plugin }) => {
<Flexbox className={styles.desc}>{t('pluginSettings.desc')}</Flexbox>
<Divider style={{ margin: '0 16px' }} />
{manifest.settings && (
<PluginSettingsConfig id={manifest.identifier} settings={manifest.settings} />
<PluginSettingsConfig id={manifest.identifier} schema={manifest.settings} />
)}
<Button
block
Expand Down
27 changes: 12 additions & 15 deletions src/app/chat/features/Conversation/ChatList/Messages/Assistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ import { memo } from 'react';

import { useSessionStore } from '@/store/session';
import { chatSelectors } from '@/store/session/selectors';
import { isFunctionMessage } from '@/utils/message';
import { isFunctionMessageAtStart } from '@/utils/message';

import FunctionCall from '../Plugins/FunctionCall';
import Inspector from '../Plugins/Inspector';
import { DefaultMessage } from './Default';

export const AssistantMessage: RenderMessage = memo(
({ id, plugin, function_call, content, ...props }) => {
const genFunctionCallProps = useSessionStore(chatSelectors.getFunctionMessageParams);
export const AssistantMessage: RenderMessage = memo(({ id, plugin, content, ...props }) => {
const fcProps = useSessionStore(chatSelectors.getFunctionMessageProps({ content, id, plugin }));

if (!isFunctionMessage(content)) return <DefaultMessage content={content} id={id} {...props} />;
if (!isFunctionMessageAtStart(content))
return <DefaultMessage content={content} id={id} {...props} />;

const fcProps = genFunctionCallProps({ content, function_call, id, plugin });

return (
<div id={id}>
<FunctionCall {...fcProps} />
</div>
);
},
);
return (
<div id={id}>
<Inspector {...fcProps} />
</div>
);
});
41 changes: 26 additions & 15 deletions src/app/chat/features/Conversation/ChatList/Messages/Function.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { RenderMessage } from '@lobehub/ui';
import { memo } from 'react';
import isEqual from 'fast-deep-equal';
import { memo, useState } from 'react';
import { Flexbox } from 'react-layout-kit';

import { useSessionStore } from '@/store/session';
import { chatSelectors } from '@/store/session/selectors';

import FunctionCall from '../Plugins/FunctionCall';
import PluginMessage from '../Plugins/PluginMessage';
import Inspector from '../Plugins/Inspector';
import PluginRender from '../Plugins/Render';

export const FunctionMessage: RenderMessage = memo(
({ id, content, plugin, function_call, name }) => {
const genFunctionCallProps = useSessionStore(chatSelectors.getFunctionMessageParams);
const fcProps = genFunctionCallProps({ content, function_call, id, plugin });
export const FunctionMessage: RenderMessage = memo(({ id, content, plugin, name }) => {
const fcProps = useSessionStore(
chatSelectors.getFunctionMessageProps({ content, id, plugin }),
isEqual,
);
const [showRender, setShow] = useState(true);

return (
<Flexbox gap={12} id={id}>
<FunctionCall {...fcProps} />
<PluginMessage content={content} loading={fcProps.loading} name={name} />
</Flexbox>
);
},
);
return (
<Flexbox gap={12} id={id}>
<Inspector showRender={showRender} {...fcProps} setShow={setShow} />
{showRender && (
<PluginRender
content={content}
id={id}
loading={fcProps.loading}
name={name}
payload={fcProps.command}
type={fcProps.type}
/>
)}
</Flexbox>
);
});

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ActionIcon } from '@lobehub/ui';
import { LucideSettings } from 'lucide-react';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import PluginSettingsModal from '@/features/PluginSettingsModal';
import { pluginSelectors, usePluginStore } from '@/store/plugin';

const Settings = memo<{ id: string }>(({ id }) => {
const item = usePluginStore(pluginSelectors.getPluginManifestById(id));
const [open, setOpen] = useState(false);
const { t } = useTranslation('plugin');
return (
item?.settings && (
<>
<ActionIcon
icon={LucideSettings}
onClick={() => {
setOpen(true);
}}
title={t('setting')}
/>
<PluginSettingsModal
id={id}
onClose={() => {
setOpen(false);
}}
open={open}
schema={item.settings}
/>
</>
)
);
});

export default Settings;
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
import { LoadingOutlined } from '@ant-design/icons';
import { Avatar, Highlighter, Icon } from '@lobehub/ui';
import { LobePluginType } from '@lobehub/chat-plugin-sdk';
import { ActionIcon, Avatar, Highlighter, Icon } from '@lobehub/ui';
import { Tabs } from 'antd';
import { LucideChevronDown, LucideChevronUp, LucideToyBrick } from 'lucide-react';
import { LucideChevronDown, LucideChevronUp, LucideOrbit, LucideToyBrick } from 'lucide-react';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';

import { pluginHelpers, pluginSelectors, usePluginStore } from '@/store/plugin';

import PluginResult from './PluginResultRender';
import PluginResult from './PluginResultJSON';
import Settings from './Settings';
import { useStyles } from './style';

export interface FunctionCallProps {
export interface InspectorProps {
arguments?: string;
command?: any;
content: string;
id?: string;
loading?: boolean;
setShow?: (showRender: boolean) => void;
showRender?: boolean;
type?: LobePluginType;
}

const FunctionCall = memo<FunctionCallProps>(
({ arguments: requestArgs = '{}', command, loading, content, id = 'unknown' }) => {
const Inspector = memo<InspectorProps>(
({
arguments: requestArgs = '{}',
command,
showRender,
loading,
setShow,
content,
id = 'unknown',
type,
}) => {
const { t } = useTranslation('plugin');
const { styles } = useStyles();
const [open, setOpen] = useState(false);
Expand All @@ -41,24 +55,38 @@ const FunctionCall = memo<FunctionCallProps>(

return (
<Flexbox gap={8}>
<Flexbox
align={'center'}
className={styles.container}
gap={8}
horizontal
onClick={() => {
setOpen(!open);
}}
>
{loading ? (
<div>
<LoadingOutlined />
</div>
) : (
avatar
)}
{pluginTitle ?? t('plugins.unknown')}
<Icon icon={open ? LucideChevronUp : LucideChevronDown} />
<Flexbox align={'center'} distribution={'space-between'} gap={24} horizontal>
<Flexbox
align={'center'}
className={styles.container}
gap={8}
horizontal
onClick={() => {
setOpen(!open);
}}
>
{loading ? (
<div>
<LoadingOutlined />
</div>
) : (
avatar
)}
{pluginTitle ?? t('plugins.unknown')}
<Icon icon={open ? LucideChevronUp : LucideChevronDown} />
</Flexbox>
<Flexbox horizontal>
{type === 'standalone' && <ActionIcon icon={LucideOrbit} />}
<Settings id={id} />
{setShow && (
<ActionIcon
icon={showRender ? LucideChevronUp : LucideChevronDown}
onClick={() => {
setShow(!showRender);
}}
/>
)}
</Flexbox>
</Flexbox>
{open && (
<Tabs
Expand Down Expand Up @@ -87,4 +115,4 @@ const FunctionCall = memo<FunctionCallProps>(
},
);

export default FunctionCall;
export default Inspector;
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PluginRenderProps } from '@lobehub/chat-plugin-sdk/client';
import { Skeleton } from 'antd';
import { memo, useEffect, useRef, useState } from 'react';
import { memo, useRef, useState } from 'react';

import { useOnPluginFetchMessage, useOnPluginReady } from './hooks';
import { sendMessageToPlugin } from './utils';
import { useOnPluginReadyForInteraction } from '../../utils/iframeOnReady';
import { sendMessageToPlugin } from '../../utils/postMessage';

interface IFrameRenderProps extends PluginRenderProps {
height?: number;
Expand All @@ -12,24 +12,13 @@ interface IFrameRenderProps extends PluginRenderProps {
}

const IFrameRender = memo<IFrameRenderProps>(({ url, width = 800, height = 300, ...props }) => {
const [loading, setLoading] = useState(true);
const [readyForRender, setReady] = useState(false);
const iframeRef = useRef<HTMLIFrameElement>(null);

useOnPluginReady(() => setReady(true));
const [loading, setLoading] = useState(true);

// 当 props 发生变化时,主动向 iframe 发送数据
useEffect(() => {
useOnPluginReadyForInteraction(() => {
const iframeWin = iframeRef.current?.contentWindow;

if (iframeWin && readyForRender) {
sendMessageToPlugin(iframeWin, props);
}
}, [readyForRender, props]);

// 当接收到来自 iframe 的请求时,触发发送数据
useOnPluginFetchMessage(() => {
const iframeWin = iframeRef.current?.contentWindow;
if (iframeWin) {
sendMessageToPlugin(iframeWin, props);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import IFrameRender from './IFrameRender';

const SystemJsRender = dynamic(() => import('./SystemJsRender'), { ssr: false });

export interface FunctionMessageProps {
export interface PluginDefaultTypeProps {
content: string;
loading?: boolean;
name?: string;
}

const PluginMessage = memo<FunctionMessageProps>(({ content, name }) => {
const PluginDefaultType = memo<PluginDefaultTypeProps>(({ content, name }) => {
const { t } = useTranslation('plugin');

const manifest = usePluginStore((s) => s.pluginManifestMap[name || '']);
Expand Down Expand Up @@ -65,4 +65,4 @@ const PluginMessage = memo<FunctionMessageProps>(({ content, name }) => {
);
});

export default PluginMessage;
export default PluginDefaultType;
Loading