Skip to content

Commit

Permalink
📝 docs: update plugin documents
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Nov 6, 2023
1 parent 6756f67 commit f12d0f5
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 264 deletions.
49 changes: 0 additions & 49 deletions docs/api/fetch-plugin-settings.md

This file was deleted.

50 changes: 0 additions & 50 deletions docs/api/fetch-plugin-state.md

This file was deleted.

172 changes: 172 additions & 0 deletions docs/api/lobe-chat-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
---
title: lobeChat
description: LobeChat 插件客户端 SDK 实例
group:
title: 客户端
order: 10
apiHeader:
pkg: '@lobehub/chat-plugin-sdk/client'
nav: API
---

该实例包含插件侧与 LobeChat 交互的所有关键方法。

- 该实例的所有方法均使用了浏览器的 `postMessage``addEventListener` 方法,因此需要在浏览器环境中使用;
- 该函数的所有方法均通过 `postMessage` 向父窗口发送消息,因此插件必须嵌入在 LobeChat 中才能返回正确的消息;

## getPluginPayload

获取初始化插件的 Function Call 信息。

```ts
interface PluginPayload<T = any> {
arguments?: T;
name: string;
}

type GetPluginPayload = <T = any>() => Promise<PluginPayload<T>>;
```

#### 出参

- `name`: Function Call 的 api 名称
- `arguments`: Function Call 的参数对象

#### 示例

```ts
import { lobeChat } from '@lobehub/chat-plugin-sdk/client';

lobeChat.getPluginPayload().then((payload) => {
console.log(payload);
});

// payload:
// {
// name: 'showMJ',
// arguments: {
// rawInput: '一只小黄鸭摄影师,在湖边度假,在冬天的下雪天。湖面结冰了',
// },
// };
```

## getPluginMessage

用于获取插件消息的内容(`content` 字段)。

LobeChat 将插件返回的消息对象序列化后存储在 `content` 字段中,该方法将会获取该字段的内容,并反序列化为 JSON 对象。

```ts
type GetPluginMessage = <T = object>() => Promise<T>;
```

#### 示例

```ts | pure
import { lobeChat } from '@lobehub/chat-plugin-sdk/client';

lobeChat.getPluginMessage().then((message) => {
console.log(message);
});
```

## setPluginMessage

该方法用于向 LobeChat 发送消息以更新插件消息的内容。 内容将会序列化后,发送给 LobeChat,并继续会话流程。

```ts
type SetPluginMessage = <T = object>(content: T) => Promise<void>;
```

#### 参数

- `content`:要填充的插件内容。

#### 示例

```ts
import { lobeChat } from '@lobehub/chat-plugin-sdk/client';

lobeChat.setPluginMessage({ title: 'Hello', message: 'Welcome to my plugin' });
```

## getPluginState

用于获取存储在消息中的运行时状态

```ts
type GetPluginState = <T = any>(key: string) => Promise<T>;
```

#### 入参

- `key`:要获取的状态信息的键值。

#### 示例

```ts
import { lobeChat } from '@lobehub/chat-plugin-sdk/client';

lobeChat.getPluginState('counter').then((state) => {
console.log(state);
});
```

## setPluginState

该方法用于更新插件的指定状态信息。

```ts
type SetPluginState = (key: string, value: any) => Promise<void>;
```

#### 参数

- `key`:要更新的状态信息的键值。
- `value`:要更新的状态信息的值。

#### 示例

```ts
import { lobeChat } from '@lobehub/chat-plugin-sdk/client';

lobeChat.setPluginState('counter', 5);
```

## getPluginSettings

用于获取插件在 LobeChat 中存储的配置信息。

```ts
type GetPluginSettings = <T = any>() => Promise<T>;
```

#### 示例

```ts
import { lobeChat } from '@lobehub/chat-plugin-sdk/client';

lobeChat.getPluginSettings().then((state) => {
console.log(state);
});
```

## setPluginSettings

该方法用于更新插件的配置信息。

```ts
type SetPluginSettings<T> = (settings: Partial<T>) => Promise<void>;
```

#### 入参

- `settings`:要更新的插件配置信息,默认为局部更新。

#### 示例

```ts
import { lobeChat } from '@lobehub/chat-plugin-sdk/client';

lobeChat.setPluginSettings({ theme: 'dark', fontSize: 12 });
```
39 changes: 0 additions & 39 deletions docs/api/message.md

This file was deleted.

40 changes: 0 additions & 40 deletions docs/api/post-to-fill-content.md

This file was deleted.

42 changes: 0 additions & 42 deletions docs/api/post-to-update-plugin-state.md

This file was deleted.

Loading

0 comments on commit f12d0f5

Please sign in to comment.