-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: support triggerAIMessage and createAssistantMessage
- Loading branch information
Showing
15 changed files
with
230 additions
and
202 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,16 +1,21 @@ | ||
export enum PluginChannel { | ||
createAssistantMessage = 'lobe-chat:create-assistant-message', | ||
|
||
fetchPluginMessage = 'lobe-chat:fetch-plugin-message', | ||
fetchPluginSettings = 'lobe-chat:fetch-plugin-settings', | ||
fetchPluginState = 'lobe-chat:fetch-plugin-state', | ||
|
||
fetchPluginState = 'lobe-chat:fetch-plugin-state', | ||
fillStandalonePluginContent = 'lobe-chat:fill-plugin-content', | ||
initStandalonePlugin = 'lobe-chat:init-standalone-plugin', | ||
|
||
pluginReadyForRender = 'lobe-chat:plugin-ready-for-render', | ||
|
||
renderPlugin = 'lobe-chat:render-plugin', | ||
renderPluginSettings = 'lobe-chat:render-plugin-settings', | ||
renderPluginState = 'lobe-chat:render-plugin-state', | ||
|
||
triggerAIMessage = 'lobe-chat:trigger-ai-message', | ||
|
||
updatePluginSettings = 'lobe-chat:update-plugin-settings', | ||
updatePluginState = 'lobe-chat:update-plugin-state', | ||
} |
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,33 @@ | ||
import { lobeChat } from './lobeChat'; | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
export const postToFillPluginContent = lobeChat.setPluginMessage; | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
export const postToUpdatePluginState = lobeChat.setPluginState; | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
export const postToUpdatePluginSettings = lobeChat.setPluginSettings; | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
export const fetchPluginState = lobeChat.getPluginState; | ||
/** | ||
* @deprecated | ||
*/ | ||
export const fetchPluginMessage = lobeChat.getPluginMessage; | ||
/** | ||
* @deprecated | ||
*/ | ||
export const fetchPluginPayload = lobeChat.getPluginPayload; | ||
/** | ||
* @deprecated | ||
*/ | ||
export const fetchPluginSettings = lobeChat.getPluginSettings; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
export * from './const'; | ||
export * from './fetch'; | ||
export * from './deprecatedOnV2'; | ||
export * from './hooks'; | ||
export * from './lobeChat'; | ||
export * from './postMessage'; | ||
export * from './type'; |
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 |
---|---|---|
@@ -1,26 +1,130 @@ | ||
import { | ||
fetchPluginMessage, | ||
fetchPluginPayload, | ||
fetchPluginSettings, | ||
fetchPluginState, | ||
} from './fetch'; | ||
import { | ||
postToFillPluginContent, | ||
postToUpdatePluginSettings, | ||
postToUpdatePluginState, | ||
} from './postMessage'; | ||
import { PluginChannel } from '@/client/const'; | ||
import { PluginRenderProps } from '@/client/type'; | ||
import { PluginRequestPayload } from '@/schema/market'; | ||
|
||
/** | ||
* Represents a plugin payload. | ||
* | ||
* @template T - Type of the arguments. | ||
* @property {T} [arguments] - The arguments for the plugin. | ||
* @property {string} name - The name of the api payload | ||
* @property {any} settings - The settings for the plugin. | ||
* @property {any} [state] - The state of the current plugin message | ||
*/ | ||
export interface PluginPayload<T = any> { | ||
arguments?: T; | ||
name: string; | ||
settings?: any; | ||
state?: any; | ||
} | ||
|
||
class LobeChat { | ||
getPluginPayload = fetchPluginPayload; | ||
getPluginPayload = <T = any>() => | ||
new Promise<PluginPayload<T>>((resolve) => { | ||
if (typeof window === 'undefined') { | ||
resolve(undefined as any); | ||
return; | ||
} | ||
const receiverData = (e: MessageEvent) => { | ||
if (e.data.type === PluginChannel.initStandalonePlugin) { | ||
// TODO: drop e.data.props in v2 | ||
const payload: PluginRequestPayload = e.data.payload || e.data.props; | ||
const func = payload.apiName; | ||
const args = JSON.parse(payload.arguments || '{}'); | ||
resolve({ | ||
arguments: args, | ||
name: func, | ||
settings: e.data.settings, | ||
state: e.data.state, | ||
}); | ||
|
||
window.removeEventListener('message', receiverData); | ||
} | ||
}; | ||
|
||
window.addEventListener('message', receiverData); | ||
|
||
top?.postMessage({ type: PluginChannel.pluginReadyForRender }, '*'); | ||
}); | ||
|
||
getPluginSettings = <T = any>() => | ||
new Promise<T>((resolve) => { | ||
if (typeof window === 'undefined') { | ||
resolve(undefined as any); | ||
return; | ||
} | ||
|
||
const receiverData = (e: MessageEvent) => { | ||
if (e.data.type === PluginChannel.renderPluginSettings) { | ||
resolve(e.data.value); | ||
|
||
window.removeEventListener('message', receiverData); | ||
} | ||
}; | ||
|
||
window.addEventListener('message', receiverData); | ||
|
||
top?.postMessage({ type: PluginChannel.fetchPluginSettings }, '*'); | ||
}); | ||
|
||
setPluginSettings = (settings: any) => { | ||
top?.postMessage({ type: PluginChannel.updatePluginSettings, value: settings }, '*'); | ||
}; | ||
|
||
getPluginMessage = <T = any>() => | ||
new Promise<T>((resolve) => { | ||
if (typeof window === 'undefined') { | ||
resolve(undefined as any); | ||
return; | ||
} | ||
const receiverData = (e: MessageEvent) => { | ||
if (e.data.type === PluginChannel.renderPlugin) { | ||
const props = e.data.props as PluginRenderProps<T>; | ||
resolve(props.content as T); | ||
|
||
window.removeEventListener('message', receiverData); | ||
} | ||
}; | ||
|
||
window.addEventListener('message', receiverData); | ||
|
||
top?.postMessage({ type: PluginChannel.fetchPluginMessage }, '*'); | ||
}); | ||
|
||
setPluginMessage = (content: any) => { | ||
top?.postMessage({ content, type: PluginChannel.fillStandalonePluginContent }, '*'); | ||
}; | ||
|
||
getPluginState = <T = any>(key: string) => | ||
new Promise<T>((resolve) => { | ||
if (typeof window === 'undefined') { | ||
resolve(undefined as any); | ||
return; | ||
} | ||
|
||
const receiverData = (e: MessageEvent) => { | ||
if (e.data.type === PluginChannel.renderPluginState && e.data.key === key) { | ||
resolve(e.data.value); | ||
|
||
window.removeEventListener('message', receiverData); | ||
} | ||
}; | ||
|
||
window.addEventListener('message', receiverData); | ||
|
||
getPluginSettings = fetchPluginSettings; | ||
setPluginSettings = postToUpdatePluginSettings; | ||
top?.postMessage({ key, type: PluginChannel.fetchPluginState }, '*'); | ||
}); | ||
setPluginState = (key: string, value: any) => { | ||
top?.postMessage({ key, type: PluginChannel.updatePluginState, value }, '*'); | ||
}; | ||
|
||
getPluginMessage = fetchPluginMessage; | ||
setPluginMessage = postToFillPluginContent; | ||
triggerAIMessage = (id: string) => { | ||
top?.postMessage({ id, type: PluginChannel.triggerAIMessage }, '*'); | ||
}; | ||
|
||
getPluginState = fetchPluginState; | ||
setPluginState = postToUpdatePluginState; | ||
createAssistantMessage = (content: string) => { | ||
top?.postMessage({ content, type: PluginChannel.createAssistantMessage }, '*'); | ||
}; | ||
} | ||
|
||
export const lobeChat = new LobeChat(); |
Oops, something went wrong.