Skip to content

Commit 54a14b4

Browse files
committed
✨ feat: support triggerAIMessage and createAssistantMessage
1 parent 149dd68 commit 54a14b4

15 files changed

+230
-202
lines changed

src/client/const.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
export enum PluginChannel {
2+
createAssistantMessage = 'lobe-chat:create-assistant-message',
3+
24
fetchPluginMessage = 'lobe-chat:fetch-plugin-message',
35
fetchPluginSettings = 'lobe-chat:fetch-plugin-settings',
4-
fetchPluginState = 'lobe-chat:fetch-plugin-state',
56

7+
fetchPluginState = 'lobe-chat:fetch-plugin-state',
68
fillStandalonePluginContent = 'lobe-chat:fill-plugin-content',
79
initStandalonePlugin = 'lobe-chat:init-standalone-plugin',
10+
811
pluginReadyForRender = 'lobe-chat:plugin-ready-for-render',
912

1013
renderPlugin = 'lobe-chat:render-plugin',
1114
renderPluginSettings = 'lobe-chat:render-plugin-settings',
1215
renderPluginState = 'lobe-chat:render-plugin-state',
1316

17+
triggerAIMessage = 'lobe-chat:trigger-ai-message',
18+
1419
updatePluginSettings = 'lobe-chat:update-plugin-settings',
1520
updatePluginState = 'lobe-chat:update-plugin-state',
1621
}

src/client/deprecatedOnV2.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { lobeChat } from './lobeChat';
2+
3+
/**
4+
* @deprecated
5+
*/
6+
export const postToFillPluginContent = lobeChat.setPluginMessage;
7+
8+
/**
9+
* @deprecated
10+
*/
11+
export const postToUpdatePluginState = lobeChat.setPluginState;
12+
13+
/**
14+
* @deprecated
15+
*/
16+
export const postToUpdatePluginSettings = lobeChat.setPluginSettings;
17+
18+
/**
19+
* @deprecated
20+
*/
21+
export const fetchPluginState = lobeChat.getPluginState;
22+
/**
23+
* @deprecated
24+
*/
25+
export const fetchPluginMessage = lobeChat.getPluginMessage;
26+
/**
27+
* @deprecated
28+
*/
29+
export const fetchPluginPayload = lobeChat.getPluginPayload;
30+
/**
31+
* @deprecated
32+
*/
33+
export const fetchPluginSettings = lobeChat.getPluginSettings;

src/client/fetch/index.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/client/fetch/message.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/client/fetch/pluginPayload.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/client/fetch/pluginSettings.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/client/fetch/pluginState.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/client/hooks/useOnStandalonePluginInit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useEffect } from 'react';
22

3-
import { PluginPayload, fetchPluginPayload } from '../fetch/pluginPayload';
3+
import { PluginPayload, lobeChat } from '@/client';
44

55
export const useOnStandalonePluginInit = <T = any>(
66
callback: (payload: PluginPayload<T>) => void,
77
) => {
88
useEffect(() => {
9-
fetchPluginPayload().then((e) => {
9+
lobeChat.getPluginPayload().then((e) => {
1010
if (!e) return;
1111

1212
callback(e);

src/client/hooks/usePluginSettings.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { useCallback, useEffect, useState } from 'react';
22

3-
import { fetchPluginSettings } from '@/client/fetch';
4-
import { postToUpdatePluginSettings } from '@/client/postMessage';
3+
import { lobeChat } from '@/client';
54

65
export const usePluginSettings = <T>(initialValue: T) => {
76
const [value, setValue] = useState(initialValue);
87

98
useEffect(() => {
10-
fetchPluginSettings().then((e) => {
9+
lobeChat.getPluginSettings().then((e) => {
1110
if (!e) return;
1211

1312
setValue(e);
@@ -16,7 +15,7 @@ export const usePluginSettings = <T>(initialValue: T) => {
1615

1716
const updateValue = useCallback((value: T) => {
1817
setValue(value);
19-
postToUpdatePluginSettings(value);
18+
lobeChat.setPluginSettings(value);
2019
}, []);
2120

2221
return [value, updateValue] as const;

src/client/hooks/usePluginState.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { useCallback, useEffect, useState } from 'react';
22

3-
import { fetchPluginState } from '@/client/fetch';
4-
import { postToUpdatePluginState } from '@/client/postMessage';
3+
import { lobeChat } from '@/client';
54

65
export const usePluginState = <T>(key: string, initialValue: T) => {
76
const [value, setValue] = useState(initialValue);
87

98
useEffect(() => {
10-
fetchPluginState(key).then((e) => {
9+
lobeChat.getPluginState(key).then((e) => {
1110
if (!e) return;
1211

1312
setValue(e);
@@ -17,7 +16,7 @@ export const usePluginState = <T>(key: string, initialValue: T) => {
1716
const updateValue = useCallback(
1817
(value: T) => {
1918
setValue(value);
20-
postToUpdatePluginState(key, value);
19+
lobeChat.setPluginState(key, value);
2120
},
2221
[key],
2322
);

src/client/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from './const';
2-
export * from './fetch';
2+
export * from './deprecatedOnV2';
33
export * from './hooks';
44
export * from './lobeChat';
5-
export * from './postMessage';
65
export * from './type';

src/client/lobeChat.ts

Lines changed: 122 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,130 @@
1-
import {
2-
fetchPluginMessage,
3-
fetchPluginPayload,
4-
fetchPluginSettings,
5-
fetchPluginState,
6-
} from './fetch';
7-
import {
8-
postToFillPluginContent,
9-
postToUpdatePluginSettings,
10-
postToUpdatePluginState,
11-
} from './postMessage';
1+
import { PluginChannel } from '@/client/const';
2+
import { PluginRenderProps } from '@/client/type';
3+
import { PluginRequestPayload } from '@/schema/market';
4+
5+
/**
6+
* Represents a plugin payload.
7+
*
8+
* @template T - Type of the arguments.
9+
* @property {T} [arguments] - The arguments for the plugin.
10+
* @property {string} name - The name of the api payload
11+
* @property {any} settings - The settings for the plugin.
12+
* @property {any} [state] - The state of the current plugin message
13+
*/
14+
export interface PluginPayload<T = any> {
15+
arguments?: T;
16+
name: string;
17+
settings?: any;
18+
state?: any;
19+
}
1220

1321
class LobeChat {
14-
getPluginPayload = fetchPluginPayload;
22+
getPluginPayload = <T = any>() =>
23+
new Promise<PluginPayload<T>>((resolve) => {
24+
if (typeof window === 'undefined') {
25+
resolve(undefined as any);
26+
return;
27+
}
28+
const receiverData = (e: MessageEvent) => {
29+
if (e.data.type === PluginChannel.initStandalonePlugin) {
30+
// TODO: drop e.data.props in v2
31+
const payload: PluginRequestPayload = e.data.payload || e.data.props;
32+
const func = payload.apiName;
33+
const args = JSON.parse(payload.arguments || '{}');
34+
resolve({
35+
arguments: args,
36+
name: func,
37+
settings: e.data.settings,
38+
state: e.data.state,
39+
});
40+
41+
window.removeEventListener('message', receiverData);
42+
}
43+
};
44+
45+
window.addEventListener('message', receiverData);
46+
47+
top?.postMessage({ type: PluginChannel.pluginReadyForRender }, '*');
48+
});
49+
50+
getPluginSettings = <T = any>() =>
51+
new Promise<T>((resolve) => {
52+
if (typeof window === 'undefined') {
53+
resolve(undefined as any);
54+
return;
55+
}
56+
57+
const receiverData = (e: MessageEvent) => {
58+
if (e.data.type === PluginChannel.renderPluginSettings) {
59+
resolve(e.data.value);
60+
61+
window.removeEventListener('message', receiverData);
62+
}
63+
};
64+
65+
window.addEventListener('message', receiverData);
66+
67+
top?.postMessage({ type: PluginChannel.fetchPluginSettings }, '*');
68+
});
69+
70+
setPluginSettings = (settings: any) => {
71+
top?.postMessage({ type: PluginChannel.updatePluginSettings, value: settings }, '*');
72+
};
73+
74+
getPluginMessage = <T = any>() =>
75+
new Promise<T>((resolve) => {
76+
if (typeof window === 'undefined') {
77+
resolve(undefined as any);
78+
return;
79+
}
80+
const receiverData = (e: MessageEvent) => {
81+
if (e.data.type === PluginChannel.renderPlugin) {
82+
const props = e.data.props as PluginRenderProps<T>;
83+
resolve(props.content as T);
84+
85+
window.removeEventListener('message', receiverData);
86+
}
87+
};
88+
89+
window.addEventListener('message', receiverData);
90+
91+
top?.postMessage({ type: PluginChannel.fetchPluginMessage }, '*');
92+
});
93+
94+
setPluginMessage = (content: any) => {
95+
top?.postMessage({ content, type: PluginChannel.fillStandalonePluginContent }, '*');
96+
};
97+
98+
getPluginState = <T = any>(key: string) =>
99+
new Promise<T>((resolve) => {
100+
if (typeof window === 'undefined') {
101+
resolve(undefined as any);
102+
return;
103+
}
104+
105+
const receiverData = (e: MessageEvent) => {
106+
if (e.data.type === PluginChannel.renderPluginState && e.data.key === key) {
107+
resolve(e.data.value);
108+
109+
window.removeEventListener('message', receiverData);
110+
}
111+
};
112+
113+
window.addEventListener('message', receiverData);
15114

16-
getPluginSettings = fetchPluginSettings;
17-
setPluginSettings = postToUpdatePluginSettings;
115+
top?.postMessage({ key, type: PluginChannel.fetchPluginState }, '*');
116+
});
117+
setPluginState = (key: string, value: any) => {
118+
top?.postMessage({ key, type: PluginChannel.updatePluginState, value }, '*');
119+
};
18120

19-
getPluginMessage = fetchPluginMessage;
20-
setPluginMessage = postToFillPluginContent;
121+
triggerAIMessage = (id: string) => {
122+
top?.postMessage({ id, type: PluginChannel.triggerAIMessage }, '*');
123+
};
21124

22-
getPluginState = fetchPluginState;
23-
setPluginState = postToUpdatePluginState;
125+
createAssistantMessage = (content: string) => {
126+
top?.postMessage({ content, type: PluginChannel.createAssistantMessage }, '*');
127+
};
24128
}
25129

26130
export const lobeChat = new LobeChat();

0 commit comments

Comments
 (0)