From 40045f8b1d4539c7cbfb84f31d0a1ec7eb956210 Mon Sep 17 00:00:00 2001 From: arvinxx Date: Mon, 1 Jan 2024 17:53:53 +0800 Subject: [PATCH] :memo: docs: add plugin en docs --- docs/api/plugin-channel.md | 2 +- docs/api/plugin-channel.zh-CN.md | 2 +- docs/guides/client-sdk.md | 45 +++++++ docs/guides/communication-mechanisms.md | 53 ++++++++ docs/guides/communication-mechanisms.zh-CN.md | 2 +- docs/guides/default-plugin.md | 33 +++++ docs/guides/js-server.md | 81 ++++++++++++ docs/guides/markdown-plugin.md | 60 +++++++++ docs/guides/openapi-schema.md | 51 ++++++++ docs/guides/openapi.md | 40 ++++++ docs/guides/openapi.zh-CN.md | 2 +- docs/guides/plugin-fontend.md | 79 ++++++++++++ docs/guides/plugin-invoke.md | 117 ++++++++++++++++++ docs/guides/plugin-manifest.md | 95 ++++++++++++++ docs/guides/plugin-server.md | 43 +++++++ docs/guides/plugin-type.md | 81 ++++++++++++ docs/guides/python-server.md | 28 +++++ docs/guides/server-auth.md | 59 +++++++++ docs/guides/server-gateway.md | 76 ++++++++++++ docs/guides/server-gateway.zh-CN.md | 2 +- docs/guides/standalone-plugin.md | 78 ++++++++++++ docs/quick-start/get-start.md | 1 + docs/quick-start/intro.md | 1 + docs/quick-start/plugin-settings.md | 2 +- docs/quick-start/plugin-settings.zh-CN.md | 2 +- docs/quick-start/template.md | 1 + 26 files changed, 1029 insertions(+), 7 deletions(-) create mode 100644 docs/guides/client-sdk.md create mode 100644 docs/guides/communication-mechanisms.md create mode 100644 docs/guides/default-plugin.md create mode 100644 docs/guides/js-server.md create mode 100644 docs/guides/markdown-plugin.md create mode 100644 docs/guides/openapi-schema.md create mode 100644 docs/guides/openapi.md create mode 100644 docs/guides/plugin-fontend.md create mode 100644 docs/guides/plugin-invoke.md create mode 100644 docs/guides/plugin-manifest.md create mode 100644 docs/guides/plugin-server.md create mode 100644 docs/guides/plugin-type.md create mode 100644 docs/guides/python-server.md create mode 100644 docs/guides/server-auth.md create mode 100644 docs/guides/server-gateway.md create mode 100644 docs/guides/standalone-plugin.md diff --git a/docs/api/plugin-channel.md b/docs/api/plugin-channel.md index 90cc541..59734b0 100644 --- a/docs/api/plugin-channel.md +++ b/docs/api/plugin-channel.md @@ -8,7 +8,7 @@ atomId: PluginChannel description: Provides detailed explanations of message types for plugin communication nav: title: API - order: 2 + order: 100 --- # PluginChannel Communication Messages diff --git a/docs/api/plugin-channel.zh-CN.md b/docs/api/plugin-channel.zh-CN.md index fb86b80..d88f6c5 100644 --- a/docs/api/plugin-channel.zh-CN.md +++ b/docs/api/plugin-channel.zh-CN.md @@ -8,7 +8,7 @@ atomId: PluginChannel description: 提供了关于插件通信的消息类型的详细说明 nav: title: API - order: 10 + order: 100 apiHeader: pkg: '@lobehub/chat-plugin-sdk' --- diff --git a/docs/guides/client-sdk.md b/docs/guides/client-sdk.md new file mode 100644 index 0000000..435fd6f --- /dev/null +++ b/docs/guides/client-sdk.md @@ -0,0 +1,45 @@ +--- +title: Client SDK +group: Plugin Frontend +order: 2 +--- + +# LobeChat Client SDK + +The LobeChat Client SDK is a frontend development toolkit provided to plugin developers, allowing plugins to communicate efficiently and securely with the LobeChat application. Through this SDK, developers can easily access data passed to the plugin by LobeChat, send messages, update plugin status, and manage plugin configuration information. + +The core functionality of the SDK is to encapsulate all the underlying communication logic required to interact with LobeChat, including using the browser's `postMessage` and `addEventListener` methods for cross-window communication. This means that developers do not need to delve into complex communication protocols and can focus on implementing plugin functionality. + +## Usage Example + +### Obtaining Plugin Initialization Information + +When the plugin is loaded, developers may need to obtain the initialization parameters and configuration passed by LobeChat. Using the LobeChat Client SDK, this can be easily accomplished with the following lines of code: + +```javascript +import { lobeChat } from '@lobehub/chat-plugin-sdk/client'; + +// Obtain initialization information +lobeChat.getPluginPayload().then((payload) => { + console.log('Plugin Name:', payload.name); + console.log('Plugin Arguments:', payload.arguments); + console.log('Plugin Settings:', payload.settings); +}); +``` + +### Updating Plugin Message Content + +If the plugin needs to send messages during interaction with the user, it can use the methods provided by the SDK to update the message content: + +```javascript +import { lobeChat } from '@lobehub/chat-plugin-sdk/client'; + +// Send message content +lobeChat.setPluginMessage('Welcome to using our plugin!'); +``` + +The LobeChat Client SDK is a powerful assistant for plugin developers, providing a complete, concise, and powerful set of tools to implement various interactive features of LobeChat plugins. With these tools, developers can focus more on innovation and enhancing user experience without worrying about the implementation details of communication mechanisms. + +## API + +For the complete usage API of the LobeChat Client SDK, please refer to: [LobeChat Client SDK API Documentation](/en-US/api/lobe-chat-client). diff --git a/docs/guides/communication-mechanisms.md b/docs/guides/communication-mechanisms.md new file mode 100644 index 0000000..bf5de4a --- /dev/null +++ b/docs/guides/communication-mechanisms.md @@ -0,0 +1,53 @@ +--- +title: Plugin Communication +group: Concepts +order: 4 +--- + +# Overview of Plugin Communication Mechanism + +## Server Communication + +For plugins of type `default` and `markdown`, you need to provide a backend service (standalone plugins can be pure frontend applications) to exchange data and process requests with the LobeChat core. + +The following will introduce the implementation principles and key details of server communication between the LobeChat core and plugins. + +### Plugin Server Communication Process + +The server communication between the LobeChat core and plugins is coordinated through a middleware layer, namely the [Plugin Gateway](https://github.com/lobehub/chat-plugins-gateway), to ensure the security and flexibility of communication. It also provides a standardized protocol to manage requests and responses. + +1. **Request Initialization**: The LobeChat core sends a request to the Gateway via HTTP POST, carrying a `PluginRequestPayload` containing the plugin identifier, API name, parameters, and other information. +2. **Gateway Processing**: Upon receiving the request, the Gateway parses the `PluginRequestPayload` in the request body and performs parameter validation. +3. **Request Handling and Response**: After successful validation, the Gateway calls the plugin's server based on the API name and parameters in the request, obtains the response, encapsulates the processing result as response data, and sends it back to the LobeChat core via HTTP response. +4. **Error Handling**: If an error occurs during request processing, the Gateway generates an error response, including the error type and message, and returns it to the LobeChat core. + +### Gateway Communication Implementation Details + +The following are key implementation details of the LobeChat plugin server: + +- **Request Payload Processing**: The Gateway determines the plugin's identity by parsing the `identifier` in the `PluginRequestPayload` and executes the corresponding API logic based on the `apiName`. +- **Plugin Manifest Retrieval**: If the request payload does not include the plugin manifest, the Gateway retrieves it from the [Plugin Store Index](https://github.com/lobehub/lobe-chat-plugins) to ensure correct identification and functionality of the plugin. +- **Parameter Validation**: The Gateway validates the parameters in the request based on the API parameter pattern defined in the plugin manifest to ensure their validity and security. +- **Setting Handling**: The Gateway adds the plugin's requested settings to the request header, allowing the plugin to retrieve the settings, such as API keys or other authentication information, using the `getPluginSettingsFromRequest` method. +- **OpenAPI Support**: If the plugin manifest specifies an [OpenAPI manifest](/zh-CN/guides/openapi), the Gateway will utilize `SwaggerClient` to interact with third-party services defined in the OpenAPI specification. + +### Error Handling + +Error handling in server communication is crucial. The Gateway defines various error types, such as `PluginErrorType.MethodNotAllowed` indicating an unsupported request method, and `PluginErrorType.PluginGatewayError` indicating a gateway error, ensuring clear error feedback to the LobeChat core in case of issues. For detailed error types, please refer to: [Server Error Types](/zh-CN/api/error) + +## Frontend Communication + +The frontend communication between the LobeChat core and plugins is based on the HTML5 `window.postMessage` API, which allows secure communication between pages from different origins. In this mechanism, the LobeChat core can securely exchange information with embedded plugins (usually through `