@tusi-ai/plugins 0.0.0-stage → 0.0.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -2
- package/dist/calendar/index.cjs +119 -0
- package/dist/calendar/index.d.mts +30 -0
- package/dist/calendar/index.d.ts +30 -0
- package/dist/calendar/index.js +117 -0
- package/dist/camera/index.cjs +351 -0
- package/dist/camera/index.d.mts +98 -0
- package/dist/camera/index.d.ts +98 -0
- package/dist/camera/index.js +349 -0
- package/dist/clipboard/index.cjs +139 -0
- package/dist/clipboard/index.d.mts +41 -0
- package/dist/clipboard/index.d.ts +41 -0
- package/dist/clipboard/index.js +137 -0
- package/dist/contact-picker/index.cjs +100 -0
- package/dist/contact-picker/index.d.mts +37 -0
- package/dist/contact-picker/index.d.ts +37 -0
- package/dist/contact-picker/index.js +98 -0
- package/dist/filesystem/index.cjs +447 -0
- package/dist/filesystem/index.d.mts +202 -0
- package/dist/filesystem/index.d.ts +202 -0
- package/dist/filesystem/index.js +445 -0
- package/dist/geolocation/index.cjs +114 -0
- package/dist/geolocation/index.d.mts +49 -0
- package/dist/geolocation/index.d.ts +49 -0
- package/dist/geolocation/index.js +112 -0
- package/dist/index.cjs +40 -0
- package/dist/index.d.mts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +38 -0
- package/dist/local-notifications/index.cjs +152 -0
- package/dist/local-notifications/index.d.mts +84 -0
- package/dist/local-notifications/index.d.ts +84 -0
- package/dist/local-notifications/index.js +150 -0
- package/dist/share/index.cjs +145 -0
- package/dist/share/index.d.mts +45 -0
- package/dist/share/index.d.ts +45 -0
- package/dist/share/index.js +143 -0
- package/package.json +123 -3
package/README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tusi-ai/plugins
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> 吐司端能力插件。页面按子路径引入,SDK 按运行环境选择 Web 实现或 JSB。
|
|
4
|
+
|
|
5
|
+
依赖 `@tusi-ai/core`。通信协议见 [docs/native-jsb-api.md](../../docs/native-jsb-api.md)。
|
|
6
|
+
|
|
7
|
+
## 用法
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Camera } from '@tusi-ai/plugins/camera';
|
|
11
|
+
import { Filesystem } from '@tusi-ai/plugins/filesystem';
|
|
12
|
+
|
|
13
|
+
const photo = await Camera.getPhoto({
|
|
14
|
+
resultType: 'uri',
|
|
15
|
+
source: 'CAMERA',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const picked = await Filesystem.pickMedia({ mediaType: 'image', maxCount: 1 });
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
子路径:`camera`、`filesystem`、`geolocation`、`contact-picker`、`local-notifications`、`share`、`clipboard`、`calendar`。
|
|
22
|
+
|
|
23
|
+
包入口只导出 `BaseAdapter`。页面从对应子路径拿插件。
|
|
24
|
+
|
|
25
|
+
运行环境由 `@tusi-ai/core` 判断:
|
|
26
|
+
|
|
27
|
+
| 环境 | 插件走哪 |
|
|
28
|
+
|---|---|
|
|
29
|
+
| 浏览器 | `browser.ts`。没有对应 Web API 时抛 `ErrorCode.Unsupported`(`9002`) |
|
|
30
|
+
| 预览 iframe | `native.ts`,经 Penpal 交给预览壳,再发 `jsb://` |
|
|
31
|
+
| 包壳 WebView | `native.ts`,页面直接发 `jsb://` |
|
|
32
|
+
|
|
33
|
+
预览 iframe 要求父页面是 `http` 或 `https` 的 `qq.com` 子域。页面不用声明父页面 origin。
|
|
34
|
+
|
|
35
|
+
失败抛 `TusiError`。参数在进入适配器之前校验,不合法为 `1001`。
|
|
36
|
+
|
|
37
|
+
## 插件
|
|
38
|
+
|
|
39
|
+
| 引入 | 方法 |
|
|
40
|
+
|---|---|
|
|
41
|
+
| `@tusi-ai/plugins/camera` | `getPhoto`、`checkPermissions`、`requestPermissions` |
|
|
42
|
+
| `@tusi-ai/plugins/filesystem` | `pickMedia`、`saveFile`、`readFile`、`writeFile`、`appendFile`、`deleteFile`、`mkdir`、`rmdir`、`readdir`、`getUri`、`stat`、`rename`、`copy` |
|
|
43
|
+
| `@tusi-ai/plugins/geolocation` | `getCurrentPos` |
|
|
44
|
+
| `@tusi-ai/plugins/contact-picker` | `pickContact` |
|
|
45
|
+
| `@tusi-ai/plugins/local-notifications` | `schedule`、`cancel`、`getPending` |
|
|
46
|
+
| `@tusi-ai/plugins/share` | `canShare`、`share` |
|
|
47
|
+
| `@tusi-ai/plugins/clipboard` | `write`、`read` |
|
|
48
|
+
| `@tusi-ai/plugins/calendar` | `createEventWithPrompt` |
|
|
49
|
+
|
|
50
|
+
目录读写使用 `path` + `directory`。`directory` 为 `DOCUMENTS`、`DATA`、`CACHE`、`EXTERNAL`。路径拒绝 `..`。
|
|
51
|
+
|
|
52
|
+
每个插件目录有 `SKILL.md`,写方法参数和返回值。
|
|
53
|
+
|
|
54
|
+
## 目录结构
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
src/
|
|
58
|
+
├── base-adapter.ts
|
|
59
|
+
├── index.ts
|
|
60
|
+
├── camera/
|
|
61
|
+
├── filesystem/
|
|
62
|
+
├── geolocation/
|
|
63
|
+
├── contact-picker/
|
|
64
|
+
├── local-notifications/
|
|
65
|
+
├── share/
|
|
66
|
+
├── clipboard/
|
|
67
|
+
└── calendar/
|
|
68
|
+
playground/ # Vite 测试页,不进入 npm 包
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
每个插件目录:`index.ts`、`types.ts`、`adapter.ts`、`native.ts`、`browser.ts`、`test/`、`SKILL.md`、`meta.json`。
|
|
72
|
+
|
|
73
|
+
## 脚本
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pnpm --filter @tusi-ai/plugins playground
|
|
77
|
+
pnpm --filter @tusi-ai/plugins playground:build
|
|
78
|
+
pnpm --filter @tusi-ai/plugins test
|
|
79
|
+
pnpm --filter @tusi-ai/plugins build
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`playground` 地址是 `http://localhost:5173/`。`playground:build` 产出 `playground/dist`,资源使用相对路径。
|
|
83
|
+
|
|
84
|
+
## 硬约束
|
|
85
|
+
|
|
86
|
+
- 8 个插件、26 个一次性方法。没有 `addListener`、`removeListener`、`removeAllListeners`
|
|
87
|
+
- 浏览器实现在进入 `bridge` 之前选定
|
|
88
|
+
- 系统差异留在同一个 Native 适配器里,通过 `getNativeHost()` 读取后再分支
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@tusi-ai/core');
|
|
4
|
+
|
|
5
|
+
var __async = (__this, __arguments, generator) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
var fulfilled = (value) => {
|
|
8
|
+
try {
|
|
9
|
+
step(generator.next(value));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
reject(e);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var rejected = (value) => {
|
|
15
|
+
try {
|
|
16
|
+
step(generator.throw(value));
|
|
17
|
+
} catch (e) {
|
|
18
|
+
reject(e);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
22
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
var BaseAdapter = class {
|
|
26
|
+
/**
|
|
27
|
+
* 绑定插件名,后续原生调用会拼成 `pluginName.method`。
|
|
28
|
+
*
|
|
29
|
+
* @param pluginName - JSB 插件名,例如 `Camera`
|
|
30
|
+
*/
|
|
31
|
+
constructor(pluginName) {
|
|
32
|
+
this.pluginName = pluginName;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 调用 `pluginName.method`。只给原生适配器使用。
|
|
36
|
+
*
|
|
37
|
+
* @param method - 插件内的方法名,例如 `getPhoto`
|
|
38
|
+
* @param params - 原样交给 {@link bridge}
|
|
39
|
+
* @returns 客户端响应的 `data`
|
|
40
|
+
*/
|
|
41
|
+
callNative(method, params) {
|
|
42
|
+
return core.bridge.call(`${this.pluginName}.${method}`, params);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 当前环境没有这个能力。在进入 bridge 之前抛出。
|
|
46
|
+
*
|
|
47
|
+
* @param method - 插件内的方法名
|
|
48
|
+
* @throws {@link TusiError} {@link ErrorCode.Unsupported}
|
|
49
|
+
*/
|
|
50
|
+
unsupported(method) {
|
|
51
|
+
throw new core.TusiError(
|
|
52
|
+
core.ErrorCode.Unsupported,
|
|
53
|
+
`${this.pluginName}.${method} is not supported`,
|
|
54
|
+
`${this.pluginName}.${method}`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/calendar/adapter.ts
|
|
60
|
+
var CalendarAdapter = class extends BaseAdapter {
|
|
61
|
+
constructor() {
|
|
62
|
+
super("Calendar");
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/calendar/browser.ts
|
|
67
|
+
var BrowserCalendarAdapter = class extends CalendarAdapter {
|
|
68
|
+
createEventWithPrompt(_options) {
|
|
69
|
+
this.unsupported("createEventWithPrompt");
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/calendar/native.ts
|
|
74
|
+
var NativeCalendarAdapter = class extends CalendarAdapter {
|
|
75
|
+
createEventWithPrompt(options) {
|
|
76
|
+
return this.callNative("createEventWithPrompt", options);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/calendar/index.ts
|
|
81
|
+
var adapter;
|
|
82
|
+
function getAdapter() {
|
|
83
|
+
adapter != null ? adapter : adapter = core.getRuntimeEnv() === core.RuntimeEnv.Browser ? new BrowserCalendarAdapter() : new NativeCalendarAdapter();
|
|
84
|
+
return adapter;
|
|
85
|
+
}
|
|
86
|
+
function assertEvent(options) {
|
|
87
|
+
if (!(options == null ? void 0 : options.title) || options.startDate === void 0 || options.endDate === void 0) {
|
|
88
|
+
throw new core.TusiError(
|
|
89
|
+
core.ErrorCode.InvalidParams,
|
|
90
|
+
"title, startDate and endDate are required",
|
|
91
|
+
"Calendar.createEventWithPrompt"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
if (options.endDate < options.startDate) {
|
|
95
|
+
throw new core.TusiError(
|
|
96
|
+
core.ErrorCode.InvalidParams,
|
|
97
|
+
"endDate must not be earlier than startDate",
|
|
98
|
+
"Calendar.createEventWithPrompt"
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
var Calendar = {
|
|
103
|
+
/**
|
|
104
|
+
* 预填标题和时间并打开系统新建日程界面。
|
|
105
|
+
*
|
|
106
|
+
* @param options - 标题、开始和结束时间。地点和全天有值才带上
|
|
107
|
+
* @throws {@link TusiError}
|
|
108
|
+
* {@link ErrorCode.InvalidParams} 时间不合法,{@link ErrorCode.PermissionDenied} 拒绝写权限,
|
|
109
|
+
* {@link ErrorCode.Cancelled} 用户取消,{@link ErrorCode.Unsupported} 浏览器没有该界面
|
|
110
|
+
*/
|
|
111
|
+
createEventWithPrompt(options) {
|
|
112
|
+
return __async(this, null, function* () {
|
|
113
|
+
assertEvent(options);
|
|
114
|
+
return getAdapter().createEventWithPrompt(options);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
exports.Calendar = Calendar;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** {@link Calendar.createEventWithPrompt} 的入参。 */
|
|
2
|
+
interface CreateEventOptions {
|
|
3
|
+
/** 日程标题。 */
|
|
4
|
+
title: string;
|
|
5
|
+
/** 开始时间,Unix 毫秒。 */
|
|
6
|
+
startDate: number;
|
|
7
|
+
/** 结束时间,Unix 毫秒。 */
|
|
8
|
+
endDate: number;
|
|
9
|
+
/** 地点。有值才传给系统界面。 */
|
|
10
|
+
location?: string;
|
|
11
|
+
/** 是否全天。有值才传给系统界面。 */
|
|
12
|
+
isAllDay?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 日历插件。只打开系统新建日程界面,不读、不改、不删已有日程。
|
|
17
|
+
*/
|
|
18
|
+
declare const Calendar: {
|
|
19
|
+
/**
|
|
20
|
+
* 预填标题和时间并打开系统新建日程界面。
|
|
21
|
+
*
|
|
22
|
+
* @param options - 标题、开始和结束时间。地点和全天有值才带上
|
|
23
|
+
* @throws {@link TusiError}
|
|
24
|
+
* {@link ErrorCode.InvalidParams} 时间不合法,{@link ErrorCode.PermissionDenied} 拒绝写权限,
|
|
25
|
+
* {@link ErrorCode.Cancelled} 用户取消,{@link ErrorCode.Unsupported} 浏览器没有该界面
|
|
26
|
+
*/
|
|
27
|
+
createEventWithPrompt(options: CreateEventOptions): Promise<void>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { Calendar, type CreateEventOptions };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** {@link Calendar.createEventWithPrompt} 的入参。 */
|
|
2
|
+
interface CreateEventOptions {
|
|
3
|
+
/** 日程标题。 */
|
|
4
|
+
title: string;
|
|
5
|
+
/** 开始时间,Unix 毫秒。 */
|
|
6
|
+
startDate: number;
|
|
7
|
+
/** 结束时间,Unix 毫秒。 */
|
|
8
|
+
endDate: number;
|
|
9
|
+
/** 地点。有值才传给系统界面。 */
|
|
10
|
+
location?: string;
|
|
11
|
+
/** 是否全天。有值才传给系统界面。 */
|
|
12
|
+
isAllDay?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 日历插件。只打开系统新建日程界面,不读、不改、不删已有日程。
|
|
17
|
+
*/
|
|
18
|
+
declare const Calendar: {
|
|
19
|
+
/**
|
|
20
|
+
* 预填标题和时间并打开系统新建日程界面。
|
|
21
|
+
*
|
|
22
|
+
* @param options - 标题、开始和结束时间。地点和全天有值才带上
|
|
23
|
+
* @throws {@link TusiError}
|
|
24
|
+
* {@link ErrorCode.InvalidParams} 时间不合法,{@link ErrorCode.PermissionDenied} 拒绝写权限,
|
|
25
|
+
* {@link ErrorCode.Cancelled} 用户取消,{@link ErrorCode.Unsupported} 浏览器没有该界面
|
|
26
|
+
*/
|
|
27
|
+
createEventWithPrompt(options: CreateEventOptions): Promise<void>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { Calendar, type CreateEventOptions };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { TusiError, ErrorCode, getRuntimeEnv, RuntimeEnv, bridge } from '@tusi-ai/core';
|
|
2
|
+
|
|
3
|
+
var __async = (__this, __arguments, generator) => {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
var fulfilled = (value) => {
|
|
6
|
+
try {
|
|
7
|
+
step(generator.next(value));
|
|
8
|
+
} catch (e) {
|
|
9
|
+
reject(e);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var rejected = (value) => {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.throw(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
20
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var BaseAdapter = class {
|
|
24
|
+
/**
|
|
25
|
+
* 绑定插件名,后续原生调用会拼成 `pluginName.method`。
|
|
26
|
+
*
|
|
27
|
+
* @param pluginName - JSB 插件名,例如 `Camera`
|
|
28
|
+
*/
|
|
29
|
+
constructor(pluginName) {
|
|
30
|
+
this.pluginName = pluginName;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 调用 `pluginName.method`。只给原生适配器使用。
|
|
34
|
+
*
|
|
35
|
+
* @param method - 插件内的方法名,例如 `getPhoto`
|
|
36
|
+
* @param params - 原样交给 {@link bridge}
|
|
37
|
+
* @returns 客户端响应的 `data`
|
|
38
|
+
*/
|
|
39
|
+
callNative(method, params) {
|
|
40
|
+
return bridge.call(`${this.pluginName}.${method}`, params);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 当前环境没有这个能力。在进入 bridge 之前抛出。
|
|
44
|
+
*
|
|
45
|
+
* @param method - 插件内的方法名
|
|
46
|
+
* @throws {@link TusiError} {@link ErrorCode.Unsupported}
|
|
47
|
+
*/
|
|
48
|
+
unsupported(method) {
|
|
49
|
+
throw new TusiError(
|
|
50
|
+
ErrorCode.Unsupported,
|
|
51
|
+
`${this.pluginName}.${method} is not supported`,
|
|
52
|
+
`${this.pluginName}.${method}`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/calendar/adapter.ts
|
|
58
|
+
var CalendarAdapter = class extends BaseAdapter {
|
|
59
|
+
constructor() {
|
|
60
|
+
super("Calendar");
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/calendar/browser.ts
|
|
65
|
+
var BrowserCalendarAdapter = class extends CalendarAdapter {
|
|
66
|
+
createEventWithPrompt(_options) {
|
|
67
|
+
this.unsupported("createEventWithPrompt");
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// src/calendar/native.ts
|
|
72
|
+
var NativeCalendarAdapter = class extends CalendarAdapter {
|
|
73
|
+
createEventWithPrompt(options) {
|
|
74
|
+
return this.callNative("createEventWithPrompt", options);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/calendar/index.ts
|
|
79
|
+
var adapter;
|
|
80
|
+
function getAdapter() {
|
|
81
|
+
adapter != null ? adapter : adapter = getRuntimeEnv() === RuntimeEnv.Browser ? new BrowserCalendarAdapter() : new NativeCalendarAdapter();
|
|
82
|
+
return adapter;
|
|
83
|
+
}
|
|
84
|
+
function assertEvent(options) {
|
|
85
|
+
if (!(options == null ? void 0 : options.title) || options.startDate === void 0 || options.endDate === void 0) {
|
|
86
|
+
throw new TusiError(
|
|
87
|
+
ErrorCode.InvalidParams,
|
|
88
|
+
"title, startDate and endDate are required",
|
|
89
|
+
"Calendar.createEventWithPrompt"
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
if (options.endDate < options.startDate) {
|
|
93
|
+
throw new TusiError(
|
|
94
|
+
ErrorCode.InvalidParams,
|
|
95
|
+
"endDate must not be earlier than startDate",
|
|
96
|
+
"Calendar.createEventWithPrompt"
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
var Calendar = {
|
|
101
|
+
/**
|
|
102
|
+
* 预填标题和时间并打开系统新建日程界面。
|
|
103
|
+
*
|
|
104
|
+
* @param options - 标题、开始和结束时间。地点和全天有值才带上
|
|
105
|
+
* @throws {@link TusiError}
|
|
106
|
+
* {@link ErrorCode.InvalidParams} 时间不合法,{@link ErrorCode.PermissionDenied} 拒绝写权限,
|
|
107
|
+
* {@link ErrorCode.Cancelled} 用户取消,{@link ErrorCode.Unsupported} 浏览器没有该界面
|
|
108
|
+
*/
|
|
109
|
+
createEventWithPrompt(options) {
|
|
110
|
+
return __async(this, null, function* () {
|
|
111
|
+
assertEvent(options);
|
|
112
|
+
return getAdapter().createEventWithPrompt(options);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export { Calendar };
|