mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
feat: init new plugin system (#3323)
This commit is contained in:
@@ -1,28 +1,27 @@
|
||||
{
|
||||
"name": "@affine/copilot",
|
||||
"private": true,
|
||||
"main": "./src/index.ts",
|
||||
"module": "./src/index.ts",
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
"affinePlugin": {
|
||||
"release": false,
|
||||
"entry": {
|
||||
"core": "./src/index.ts"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@affine/component": "workspace:*",
|
||||
"@toeverything/plugin-infra": "workspace:*",
|
||||
"idb": "^7.1.1",
|
||||
"langchain": "^0.0.107",
|
||||
"marked": "^5.1.0",
|
||||
"marked-gfm-heading-id": "^3.0.4",
|
||||
"marked-mangle": "^1.1.0"
|
||||
"marked-mangle": "^1.1.0",
|
||||
"zod": "^3.21.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/marked": "^5.0.0",
|
||||
"@types/react": "^18.2.14",
|
||||
"@types/react-dom": "^18.2.6",
|
||||
"idb": "^7.1.1",
|
||||
"jotai": "^2.2.2",
|
||||
"react": "18.2.0",
|
||||
"react-dom": "18.2.0",
|
||||
"zod": "^3.21.4"
|
||||
"react-dom": "18.2.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": "*",
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { IconButton } from '@affine/component';
|
||||
import { SendIcon } from '@blocksuite/icons';
|
||||
import { rootStore } from '@toeverything/plugin-infra/manager';
|
||||
import type { PluginUIAdapter } from '@toeverything/plugin-infra/type';
|
||||
import { Provider, useAtomValue, useSetAtom } from 'jotai';
|
||||
import { contentLayoutAtom } from '@toeverything/plugin-infra/manager';
|
||||
import { useAtomValue, useSetAtom } from 'jotai';
|
||||
import type { ReactElement } from 'react';
|
||||
import { StrictMode, Suspense, useCallback, useState } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import { Suspense, useCallback, useState } from 'react';
|
||||
|
||||
import { ConversationList } from '../core/components/conversation-list';
|
||||
import { FollowingUp } from '../core/components/following-up';
|
||||
@@ -17,51 +15,6 @@ import {
|
||||
textareaStyle,
|
||||
} from './index.css';
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
import('@blocksuite/blocks')
|
||||
.then(({ FormatQuickBar }) => {
|
||||
FormatQuickBar.customElements.push((_page, getSelection) => {
|
||||
const div = document.createElement('div');
|
||||
const root = createRoot(div);
|
||||
|
||||
const AskAI = (): ReactElement => {
|
||||
const { conversationAtom } = useChatAtoms();
|
||||
const call = useSetAtom(conversationAtom);
|
||||
const onClickAskAI = useCallback(() => {
|
||||
const selection = getSelection();
|
||||
if (selection != null) {
|
||||
const text = selection.models
|
||||
.map(model => {
|
||||
return model.text?.toString();
|
||||
})
|
||||
.filter((v): v is string => Boolean(v))
|
||||
.join('\n');
|
||||
console.log('selected text:', text);
|
||||
call(
|
||||
`I selected some text from the document: \n"${text}."`
|
||||
).catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
}, [call]);
|
||||
|
||||
return <div onClick={onClickAskAI}>Ask AI</div>;
|
||||
};
|
||||
root.render(
|
||||
<StrictMode>
|
||||
<Provider store={rootStore}>
|
||||
<AskAI />
|
||||
</Provider>
|
||||
</StrictMode>
|
||||
);
|
||||
return div;
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
|
||||
const Actions = () => {
|
||||
const { conversationAtom, followingUpAtoms } = useChatAtoms();
|
||||
const call = useSetAtom(conversationAtom);
|
||||
@@ -108,12 +61,10 @@ const DetailContentImpl = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export const DetailContent: PluginUIAdapter['detailContent'] = ({
|
||||
contentLayoutAtom,
|
||||
}): ReactElement => {
|
||||
export const DetailContent = (): ReactElement => {
|
||||
const layout = useAtomValue(contentLayoutAtom);
|
||||
const key = useAtomValue(openAIApiKeyAtom);
|
||||
if (layout === 'editor' || layout.second !== 'com.affine.copilot') {
|
||||
if (layout === 'editor' || layout.second !== 'copilot') {
|
||||
return <></>;
|
||||
}
|
||||
if (!key) {
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { IconButton, Tooltip } from '@affine/component';
|
||||
import type { PluginUIAdapter } from '@toeverything/plugin-infra/type';
|
||||
import { contentLayoutAtom } from '@toeverything/plugin-infra/manager';
|
||||
import { useSetAtom } from 'jotai';
|
||||
import type { ReactElement } from 'react';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
export const HeaderItem: PluginUIAdapter['headerItem'] = ({
|
||||
contentLayoutAtom,
|
||||
}): ReactElement => {
|
||||
export const HeaderItem = (): ReactElement => {
|
||||
const setLayout = useSetAtom(contentLayoutAtom);
|
||||
return (
|
||||
<Tooltip content="Chat with AI" placement="bottom-end">
|
||||
@@ -18,7 +16,7 @@ export const HeaderItem: PluginUIAdapter['headerItem'] = ({
|
||||
return {
|
||||
direction: 'horizontal',
|
||||
first: 'editor',
|
||||
second: 'com.affine.copilot',
|
||||
second: 'copilot',
|
||||
splitPercentage: 70,
|
||||
};
|
||||
} else {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Button } from '@affine/component';
|
||||
import { WorkspaceAvatar } from '@affine/component/workspace-avatar';
|
||||
import { PlusIcon, ResetIcon } from '@blocksuite/icons';
|
||||
import { clsx } from 'clsx';
|
||||
import type { MessageType } from 'langchain/schema';
|
||||
@@ -31,7 +30,6 @@ export const Conversation = (props: ConversationProps): ReactElement => {
|
||||
[styles.avatarRightStyle]: props.type === 'human',
|
||||
})}
|
||||
>
|
||||
<WorkspaceAvatar workspace={null} />
|
||||
<div className={styles.conversationContainerStyle}>
|
||||
<div
|
||||
className={clsx(styles.conversationStyle, {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { IndexedDBChatMessageHistory } from '@affine/copilot/core/langchain/message-history';
|
||||
import { atom, useAtomValue } from 'jotai';
|
||||
import { atomWithDefault } from 'jotai/utils';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
import { atomWithDefault, atomWithStorage } from 'jotai/utils';
|
||||
import type { WritableAtom } from 'jotai/vanilla';
|
||||
import type { LLMChain } from 'langchain/chains';
|
||||
import { type ConversationChain } from 'langchain/chains';
|
||||
|
||||
@@ -1,31 +1,33 @@
|
||||
import { definePlugin } from '@toeverything/plugin-infra/manager';
|
||||
import { ReleaseStage } from '@toeverything/plugin-infra/type';
|
||||
import type { PluginContext } from '@toeverything/plugin-infra/entry';
|
||||
import { createElement } from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
|
||||
definePlugin(
|
||||
{
|
||||
id: 'com.affine.copilot',
|
||||
name: {
|
||||
fallback: 'AFFiNE Copilot',
|
||||
i18nKey: 'com.affine.copilot.name',
|
||||
},
|
||||
description: {
|
||||
fallback:
|
||||
'AFFiNE Copilot will help you with best writing experience on the World.',
|
||||
},
|
||||
publisher: {
|
||||
name: {
|
||||
fallback: 'AFFiNE',
|
||||
},
|
||||
link: 'https://affine.pro',
|
||||
},
|
||||
stage: ReleaseStage.NIGHTLY,
|
||||
version: '0.0.1',
|
||||
commands: [],
|
||||
},
|
||||
{
|
||||
load: () => import('./UI/index'),
|
||||
hotModuleReload: onHot =>
|
||||
import.meta.webpackHot &&
|
||||
import.meta.webpackHot.accept('./UI', () => onHot(import('./UI/index'))),
|
||||
}
|
||||
);
|
||||
import { DetailContent } from './UI/detail-content';
|
||||
import { HeaderItem } from './UI/header-item';
|
||||
|
||||
export const entry = (context: PluginContext) => {
|
||||
context.register('headerItem', div => {
|
||||
const root = createRoot(div);
|
||||
root.render(
|
||||
createElement(context.utils.PluginProvider, {}, createElement(HeaderItem))
|
||||
);
|
||||
return () => {
|
||||
root.unmount();
|
||||
};
|
||||
});
|
||||
|
||||
context.register('window', div => {
|
||||
const root = createRoot(div);
|
||||
root.render(
|
||||
createElement(
|
||||
context.utils.PluginProvider,
|
||||
{},
|
||||
createElement(DetailContent)
|
||||
)
|
||||
);
|
||||
return () => {
|
||||
root.unmount();
|
||||
};
|
||||
});
|
||||
return () => {};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user