@lark-apaas/client-capability 0.0.1-alpha.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/LICENSE +13 -0
- package/README.md +246 -0
- package/dist/index.cjs +334 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +124 -0
- package/dist/index.d.ts +124 -0
- package/dist/index.js +313 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Lark Technologies Pte. Ltd. and/or its affiliates
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,provided that the above copyright notice and this permission notice appear in all copies.
|
|
6
|
+
|
|
7
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
8
|
+
IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
|
|
9
|
+
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
|
|
10
|
+
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
|
|
11
|
+
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
12
|
+
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
13
|
+
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# @lark-apaas/client-capability
|
|
2
|
+
|
|
3
|
+
前端 SDK,用于调用后端能力(Capability)接口。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lark-apaas/client-capability
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 概述
|
|
12
|
+
|
|
13
|
+
本 SDK 提供以下功能:
|
|
14
|
+
|
|
15
|
+
- 封装 `/api/capability/:id` HTTP 调用
|
|
16
|
+
- 流式调用支持(SSE)
|
|
17
|
+
- 运行时能力发现和验证
|
|
18
|
+
- 统一的错误处理
|
|
19
|
+
|
|
20
|
+
## 快速开始
|
|
21
|
+
|
|
22
|
+
### 基础使用
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { client } from '@lark-apaas/client-capability';
|
|
26
|
+
|
|
27
|
+
// 调用能力(自动初始化)
|
|
28
|
+
const result = await client.call('create_feishu_group', 'run', {
|
|
29
|
+
group_name: '项目讨论群',
|
|
30
|
+
members: ['user_001', 'user_002'],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
console.log(result);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 手动初始化
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { client } from '@lark-apaas/client-capability';
|
|
40
|
+
|
|
41
|
+
// 手动初始化
|
|
42
|
+
await client.init();
|
|
43
|
+
|
|
44
|
+
// 检查能力是否存在
|
|
45
|
+
if (client.hasCapability('create_feishu_group')) {
|
|
46
|
+
const result = await client.call('create_feishu_group', 'run', params);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 获取能力列表
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { client } from '@lark-apaas/client-capability';
|
|
54
|
+
|
|
55
|
+
await client.init();
|
|
56
|
+
|
|
57
|
+
// 获取所有能力
|
|
58
|
+
const capabilities = await client.listCapabilities();
|
|
59
|
+
console.log(capabilities);
|
|
60
|
+
// [
|
|
61
|
+
// { id: 'create_feishu_group', name: '创建飞书群', ... },
|
|
62
|
+
// { id: 'send_message', name: '发送消息', ... },
|
|
63
|
+
// ]
|
|
64
|
+
|
|
65
|
+
// 获取单个能力信息
|
|
66
|
+
const info = await client.getCapability('create_feishu_group');
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 自定义配置
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { createClient } from '@lark-apaas/client-capability';
|
|
73
|
+
|
|
74
|
+
const client = createClient({
|
|
75
|
+
baseURL: 'https://api.example.com',
|
|
76
|
+
fetchOptions: {
|
|
77
|
+
credentials: 'include',
|
|
78
|
+
headers: {
|
|
79
|
+
'X-Custom-Header': 'value',
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
autoInit: false,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
await client.init();
|
|
86
|
+
const result = await client.call('xxx', 'run', params);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 错误处理
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import {
|
|
93
|
+
client,
|
|
94
|
+
CapabilityNotFoundError,
|
|
95
|
+
ExecutionError
|
|
96
|
+
} from '@lark-apaas/client-capability';
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
const result = await client.call('create_feishu_group', 'run', params);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error instanceof CapabilityNotFoundError) {
|
|
102
|
+
console.error('能力不存在:', error.message);
|
|
103
|
+
} else if (error instanceof ExecutionError) {
|
|
104
|
+
console.error('执行失败:', error.message);
|
|
105
|
+
} else {
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 流式调用
|
|
112
|
+
|
|
113
|
+
用于 LLM 对话等流式输出场景:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { client } from '@lark-apaas/client-capability';
|
|
117
|
+
|
|
118
|
+
const stream = client.callStream<{ content: string }>('ai_chat', 'chat', {
|
|
119
|
+
message: 'hello',
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
for await (const chunk of stream) {
|
|
123
|
+
console.log(chunk.content);
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
流式调用错误处理:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { client, NetworkError, ExecutionError } from '@lark-apaas/client-capability';
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
const stream = client.callStream('ai_chat', 'chat', { message: 'hello' });
|
|
134
|
+
for await (const chunk of stream) {
|
|
135
|
+
process(chunk);
|
|
136
|
+
}
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (error instanceof NetworkError) {
|
|
139
|
+
console.error('网络中断:', error.message);
|
|
140
|
+
} else if (error instanceof ExecutionError) {
|
|
141
|
+
console.error('执行错误:', error.message);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## API
|
|
147
|
+
|
|
148
|
+
### CapabilityClient
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
class CapabilityClient {
|
|
152
|
+
// 是否已初始化
|
|
153
|
+
readonly initialized: boolean;
|
|
154
|
+
|
|
155
|
+
// 初始化客户端,获取能力列表
|
|
156
|
+
init(): Promise<void>;
|
|
157
|
+
|
|
158
|
+
// 调用能力
|
|
159
|
+
call<T = unknown>(
|
|
160
|
+
capabilityId: string,
|
|
161
|
+
action: string,
|
|
162
|
+
params?: Record<string, unknown>
|
|
163
|
+
): Promise<T>;
|
|
164
|
+
|
|
165
|
+
// 流式调用能力
|
|
166
|
+
callStream<T = unknown>(
|
|
167
|
+
capabilityId: string,
|
|
168
|
+
action: string,
|
|
169
|
+
params?: Record<string, unknown>
|
|
170
|
+
): AsyncIterable<T>;
|
|
171
|
+
|
|
172
|
+
// 检查能力是否存在
|
|
173
|
+
hasCapability(capabilityId: string): boolean;
|
|
174
|
+
|
|
175
|
+
// 获取所有可用能力
|
|
176
|
+
listCapabilities(): Promise<CapabilityInfo[]>;
|
|
177
|
+
|
|
178
|
+
// 获取单个能力信息
|
|
179
|
+
getCapability(capabilityId: string): Promise<CapabilityInfo | null>;
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### 配置选项
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
interface CapabilityClientOptions {
|
|
187
|
+
// API 基础路径,默认 ''(相对路径)
|
|
188
|
+
baseURL?: string;
|
|
189
|
+
|
|
190
|
+
// 自定义 fetch 配置
|
|
191
|
+
fetchOptions?: RequestInit;
|
|
192
|
+
|
|
193
|
+
// 是否在创建时自动初始化,默认 true
|
|
194
|
+
autoInit?: boolean;
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### CapabilityInfo
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
interface CapabilityInfo {
|
|
202
|
+
id: string; // 能力 ID
|
|
203
|
+
name: string; // 能力名称
|
|
204
|
+
description: string; // 能力描述
|
|
205
|
+
pluginID: string; // 关联的插件 ID
|
|
206
|
+
pluginVersion: string; // 关联的插件版本
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## 错误类型
|
|
211
|
+
|
|
212
|
+
| 错误类型 | 错误码 | 描述 |
|
|
213
|
+
|---------|--------|------|
|
|
214
|
+
| `CapabilityError` | - | 错误基类 |
|
|
215
|
+
| `ClientNotInitializedError` | CLIENT_NOT_INITIALIZED | 客户端未初始化 |
|
|
216
|
+
| `CapabilityNotFoundError` | CAPABILITY_NOT_FOUND | 能力不存在 |
|
|
217
|
+
| `ActionNotFoundError` | ACTION_NOT_FOUND | Action 不存在 |
|
|
218
|
+
| `NetworkError` | NETWORK_ERROR | 网络错误 |
|
|
219
|
+
| `ExecutionError` | EXECUTION_ERROR | 执行错误 |
|
|
220
|
+
|
|
221
|
+
## 导出
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
// 默认客户端实例
|
|
225
|
+
export { client } from '@lark-apaas/client-capability';
|
|
226
|
+
|
|
227
|
+
// 创建自定义客户端
|
|
228
|
+
export { createClient, CapabilityClient } from '@lark-apaas/client-capability';
|
|
229
|
+
|
|
230
|
+
// 类型
|
|
231
|
+
export type { CapabilityClientOptions, CapabilityInfo } from '@lark-apaas/client-capability';
|
|
232
|
+
|
|
233
|
+
// 错误类型
|
|
234
|
+
export {
|
|
235
|
+
CapabilityError,
|
|
236
|
+
ClientNotInitializedError,
|
|
237
|
+
CapabilityNotFoundError,
|
|
238
|
+
ActionNotFoundError,
|
|
239
|
+
NetworkError,
|
|
240
|
+
ExecutionError,
|
|
241
|
+
} from '@lark-apaas/client-capability';
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## 许可证
|
|
245
|
+
|
|
246
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
+
|
|
23
|
+
// src/index.ts
|
|
24
|
+
var index_exports = {};
|
|
25
|
+
__export(index_exports, {
|
|
26
|
+
ActionNotFoundError: () => ActionNotFoundError,
|
|
27
|
+
CapabilityClient: () => CapabilityClient,
|
|
28
|
+
CapabilityError: () => CapabilityError,
|
|
29
|
+
CapabilityNotFoundError: () => CapabilityNotFoundError,
|
|
30
|
+
ClientNotInitializedError: () => ClientNotInitializedError,
|
|
31
|
+
ExecutionError: () => ExecutionError,
|
|
32
|
+
NetworkError: () => NetworkError,
|
|
33
|
+
client: () => client,
|
|
34
|
+
createClient: () => createClient
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/errors.ts
|
|
39
|
+
var _CapabilityError = class _CapabilityError extends Error {
|
|
40
|
+
constructor(message, code, statusCode) {
|
|
41
|
+
super(message);
|
|
42
|
+
/** 错误码 */
|
|
43
|
+
__publicField(this, "code");
|
|
44
|
+
/** HTTP 状态码 */
|
|
45
|
+
__publicField(this, "statusCode");
|
|
46
|
+
this.name = "CapabilityError";
|
|
47
|
+
this.code = code;
|
|
48
|
+
this.statusCode = statusCode;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
__name(_CapabilityError, "CapabilityError");
|
|
52
|
+
var CapabilityError = _CapabilityError;
|
|
53
|
+
var _ClientNotInitializedError = class _ClientNotInitializedError extends CapabilityError {
|
|
54
|
+
constructor() {
|
|
55
|
+
super("Client not initialized. Call init() first or enable autoInit.", "CLIENT_NOT_INITIALIZED");
|
|
56
|
+
this.name = "ClientNotInitializedError";
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
__name(_ClientNotInitializedError, "ClientNotInitializedError");
|
|
60
|
+
var ClientNotInitializedError = _ClientNotInitializedError;
|
|
61
|
+
var _CapabilityNotFoundError = class _CapabilityNotFoundError extends CapabilityError {
|
|
62
|
+
constructor(capabilityId, statusCode) {
|
|
63
|
+
super(`Capability not found: ${capabilityId}`, "CAPABILITY_NOT_FOUND", statusCode);
|
|
64
|
+
this.name = "CapabilityNotFoundError";
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
__name(_CapabilityNotFoundError, "CapabilityNotFoundError");
|
|
68
|
+
var CapabilityNotFoundError = _CapabilityNotFoundError;
|
|
69
|
+
var _ActionNotFoundError = class _ActionNotFoundError extends CapabilityError {
|
|
70
|
+
constructor(message, statusCode) {
|
|
71
|
+
super(message, "ACTION_NOT_FOUND", statusCode);
|
|
72
|
+
this.name = "ActionNotFoundError";
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
__name(_ActionNotFoundError, "ActionNotFoundError");
|
|
76
|
+
var ActionNotFoundError = _ActionNotFoundError;
|
|
77
|
+
var _NetworkError = class _NetworkError extends CapabilityError {
|
|
78
|
+
constructor(message) {
|
|
79
|
+
super(message, "NETWORK_ERROR");
|
|
80
|
+
this.name = "NetworkError";
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
__name(_NetworkError, "NetworkError");
|
|
84
|
+
var NetworkError = _NetworkError;
|
|
85
|
+
var _ExecutionError = class _ExecutionError extends CapabilityError {
|
|
86
|
+
constructor(message, statusCode) {
|
|
87
|
+
super(message, "EXECUTION_ERROR", statusCode);
|
|
88
|
+
this.name = "ExecutionError";
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
__name(_ExecutionError, "ExecutionError");
|
|
92
|
+
var ExecutionError = _ExecutionError;
|
|
93
|
+
|
|
94
|
+
// src/client.ts
|
|
95
|
+
var DEFAULT_OPTIONS = {
|
|
96
|
+
baseURL: "",
|
|
97
|
+
fetchOptions: {},
|
|
98
|
+
autoInit: true
|
|
99
|
+
};
|
|
100
|
+
var _CapabilityClient = class _CapabilityClient {
|
|
101
|
+
constructor(options) {
|
|
102
|
+
__publicField(this, "options");
|
|
103
|
+
__publicField(this, "capabilities", /* @__PURE__ */ new Map());
|
|
104
|
+
__publicField(this, "_initialized", false);
|
|
105
|
+
__publicField(this, "initPromise", null);
|
|
106
|
+
this.options = {
|
|
107
|
+
...DEFAULT_OPTIONS,
|
|
108
|
+
...options
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 是否已初始化
|
|
113
|
+
*/
|
|
114
|
+
get initialized() {
|
|
115
|
+
return this._initialized;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 初始化客户端,获取能力列表
|
|
119
|
+
*/
|
|
120
|
+
async init() {
|
|
121
|
+
if (this._initialized) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (this.initPromise) {
|
|
125
|
+
return this.initPromise;
|
|
126
|
+
}
|
|
127
|
+
this.initPromise = this.doInit();
|
|
128
|
+
try {
|
|
129
|
+
await this.initPromise;
|
|
130
|
+
} finally {
|
|
131
|
+
this.initPromise = null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async doInit() {
|
|
135
|
+
const url = `${this.options.baseURL}/api/capability/list`;
|
|
136
|
+
try {
|
|
137
|
+
const response = await fetch(url, {
|
|
138
|
+
method: "GET",
|
|
139
|
+
...this.options.fetchOptions,
|
|
140
|
+
headers: {
|
|
141
|
+
"Content-Type": "application/json",
|
|
142
|
+
...this.options.fetchOptions.headers
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
if (!response.ok) {
|
|
146
|
+
throw new NetworkError(`Failed to fetch capabilities: ${response.status} ${response.statusText}`);
|
|
147
|
+
}
|
|
148
|
+
const data = await response.json();
|
|
149
|
+
if (data.code !== 0) {
|
|
150
|
+
throw new ExecutionError(data.message);
|
|
151
|
+
}
|
|
152
|
+
this.capabilities.clear();
|
|
153
|
+
for (const capability of data.data) {
|
|
154
|
+
this.capabilities.set(capability.id, capability);
|
|
155
|
+
}
|
|
156
|
+
this._initialized = true;
|
|
157
|
+
} catch (error) {
|
|
158
|
+
if (error instanceof CapabilityError) {
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
throw new NetworkError(error instanceof Error ? error.message : String(error));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* 确保已初始化
|
|
166
|
+
*/
|
|
167
|
+
async ensureInitialized() {
|
|
168
|
+
if (!this._initialized && this.options.autoInit) {
|
|
169
|
+
await this.init();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 调用能力
|
|
174
|
+
* @param capabilityId - 能力 ID
|
|
175
|
+
* @param action - Action 名称
|
|
176
|
+
* @param params - 输入参数
|
|
177
|
+
* @returns Action 执行结果
|
|
178
|
+
*/
|
|
179
|
+
async call(capabilityId, action, params) {
|
|
180
|
+
await this.ensureInitialized();
|
|
181
|
+
if (this._initialized && !this.capabilities.has(capabilityId)) {
|
|
182
|
+
throw new CapabilityNotFoundError(capabilityId);
|
|
183
|
+
}
|
|
184
|
+
const url = `${this.options.baseURL}/api/capability/${capabilityId}`;
|
|
185
|
+
try {
|
|
186
|
+
const response = await fetch(url, {
|
|
187
|
+
method: "POST",
|
|
188
|
+
...this.options.fetchOptions,
|
|
189
|
+
headers: {
|
|
190
|
+
"Content-Type": "application/json",
|
|
191
|
+
...this.options.fetchOptions.headers
|
|
192
|
+
},
|
|
193
|
+
body: JSON.stringify({
|
|
194
|
+
action,
|
|
195
|
+
params: params ?? {}
|
|
196
|
+
})
|
|
197
|
+
});
|
|
198
|
+
const data = await response.json();
|
|
199
|
+
if (!response.ok || data.code !== 0) {
|
|
200
|
+
const errorResponse = data;
|
|
201
|
+
const errorCode = errorResponse.error ?? "UNKNOWN_ERROR";
|
|
202
|
+
switch (errorCode) {
|
|
203
|
+
case "CAPABILITY_NOT_FOUND":
|
|
204
|
+
throw new CapabilityNotFoundError(capabilityId, response.status);
|
|
205
|
+
case "ACTION_NOT_FOUND":
|
|
206
|
+
throw new ActionNotFoundError(errorResponse.message, response.status);
|
|
207
|
+
case "PLUGIN_NOT_FOUND":
|
|
208
|
+
case "EXECUTION_ERROR":
|
|
209
|
+
default:
|
|
210
|
+
throw new ExecutionError(errorResponse.message, response.status);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return data.data;
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (error instanceof CapabilityError) {
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
throw new NetworkError(error instanceof Error ? error.message : String(error));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* 检查能力是否存在
|
|
223
|
+
*/
|
|
224
|
+
hasCapability(capabilityId) {
|
|
225
|
+
return this.capabilities.has(capabilityId);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* 获取所有可用能力
|
|
229
|
+
*/
|
|
230
|
+
async listCapabilities() {
|
|
231
|
+
await this.ensureInitialized();
|
|
232
|
+
return Array.from(this.capabilities.values());
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* 获取单个能力信息
|
|
236
|
+
*/
|
|
237
|
+
async getCapability(capabilityId) {
|
|
238
|
+
await this.ensureInitialized();
|
|
239
|
+
return this.capabilities.get(capabilityId) ?? null;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* 流式调用能力
|
|
243
|
+
* @param capabilityId - 能力 ID
|
|
244
|
+
* @param action - Action 名称
|
|
245
|
+
* @param params - 输入参数
|
|
246
|
+
* @returns AsyncIterable,逐个 yield chunk
|
|
247
|
+
*/
|
|
248
|
+
async *callStream(capabilityId, action, params) {
|
|
249
|
+
await this.ensureInitialized();
|
|
250
|
+
if (this._initialized && !this.capabilities.has(capabilityId)) {
|
|
251
|
+
throw new CapabilityNotFoundError(capabilityId);
|
|
252
|
+
}
|
|
253
|
+
const url = `${this.options.baseURL}/api/capability/${capabilityId}/stream`;
|
|
254
|
+
let response;
|
|
255
|
+
try {
|
|
256
|
+
response = await fetch(url, {
|
|
257
|
+
method: "POST",
|
|
258
|
+
...this.options.fetchOptions,
|
|
259
|
+
headers: {
|
|
260
|
+
"Content-Type": "application/json",
|
|
261
|
+
...this.options.fetchOptions.headers
|
|
262
|
+
},
|
|
263
|
+
body: JSON.stringify({
|
|
264
|
+
action,
|
|
265
|
+
params: params ?? {}
|
|
266
|
+
})
|
|
267
|
+
});
|
|
268
|
+
} catch (error) {
|
|
269
|
+
throw new NetworkError(error instanceof Error ? error.message : String(error));
|
|
270
|
+
}
|
|
271
|
+
if (!response.ok) {
|
|
272
|
+
throw new NetworkError(`HTTP ${response.status} ${response.statusText}`);
|
|
273
|
+
}
|
|
274
|
+
if (!response.body) {
|
|
275
|
+
throw new NetworkError("Response body is null");
|
|
276
|
+
}
|
|
277
|
+
const reader = response.body.getReader();
|
|
278
|
+
const decoder = new TextDecoder();
|
|
279
|
+
let buffer = "";
|
|
280
|
+
try {
|
|
281
|
+
while (true) {
|
|
282
|
+
const { done, value } = await reader.read();
|
|
283
|
+
if (done) break;
|
|
284
|
+
buffer += decoder.decode(value, {
|
|
285
|
+
stream: true
|
|
286
|
+
});
|
|
287
|
+
const lines = buffer.split("\n");
|
|
288
|
+
buffer = lines.pop() ?? "";
|
|
289
|
+
for (const line of lines) {
|
|
290
|
+
if (line.startsWith("data: ")) {
|
|
291
|
+
const data = line.slice(6);
|
|
292
|
+
if (data === "[DONE]") {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
try {
|
|
296
|
+
const parsed = JSON.parse(data);
|
|
297
|
+
if (parsed.error) {
|
|
298
|
+
const errorCode = parsed.code ?? "EXECUTION_ERROR";
|
|
299
|
+
switch (errorCode) {
|
|
300
|
+
case "CAPABILITY_NOT_FOUND":
|
|
301
|
+
throw new CapabilityNotFoundError(capabilityId);
|
|
302
|
+
case "ACTION_NOT_FOUND":
|
|
303
|
+
throw new ActionNotFoundError(parsed.error);
|
|
304
|
+
default:
|
|
305
|
+
throw new ExecutionError(parsed.error);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
yield parsed;
|
|
309
|
+
} catch (parseError) {
|
|
310
|
+
if (parseError instanceof CapabilityError) {
|
|
311
|
+
throw parseError;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
} catch (error) {
|
|
318
|
+
if (error instanceof CapabilityError) {
|
|
319
|
+
throw error;
|
|
320
|
+
}
|
|
321
|
+
throw new NetworkError(error instanceof Error ? error.message : String(error));
|
|
322
|
+
} finally {
|
|
323
|
+
reader.releaseLock();
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
__name(_CapabilityClient, "CapabilityClient");
|
|
328
|
+
var CapabilityClient = _CapabilityClient;
|
|
329
|
+
function createClient(options) {
|
|
330
|
+
return new CapabilityClient(options);
|
|
331
|
+
}
|
|
332
|
+
__name(createClient, "createClient");
|
|
333
|
+
var client = new CapabilityClient();
|
|
334
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/errors.ts","../src/client.ts"],"sourcesContent":["// 默认客户端实例\nexport { client, createClient, CapabilityClient } from './client';\n\n// 类型\nexport type { CapabilityClientOptions, CapabilityInfo } from './types';\n\n// 错误类型\nexport {\n CapabilityError,\n ClientNotInitializedError,\n CapabilityNotFoundError,\n ActionNotFoundError,\n NetworkError,\n ExecutionError,\n} from './errors';\n","/**\n * 能力调用错误基类\n */\nexport class CapabilityError extends Error {\n /** 错误码 */\n code: string;\n /** HTTP 状态码 */\n statusCode?: number;\n\n constructor(message: string, code: string, statusCode?: number) {\n super(message);\n this.name = 'CapabilityError';\n this.code = code;\n this.statusCode = statusCode;\n }\n}\n\n/**\n * 客户端未初始化错误\n */\nexport class ClientNotInitializedError extends CapabilityError {\n constructor() {\n super('Client not initialized. Call init() first or enable autoInit.', 'CLIENT_NOT_INITIALIZED');\n this.name = 'ClientNotInitializedError';\n }\n}\n\n/**\n * 能力不存在错误\n */\nexport class CapabilityNotFoundError extends CapabilityError {\n constructor(capabilityId: string, statusCode?: number) {\n super(`Capability not found: ${capabilityId}`, 'CAPABILITY_NOT_FOUND', statusCode);\n this.name = 'CapabilityNotFoundError';\n }\n}\n\n/**\n * Action 不存在错误\n */\nexport class ActionNotFoundError extends CapabilityError {\n constructor(message: string, statusCode?: number) {\n super(message, 'ACTION_NOT_FOUND', statusCode);\n this.name = 'ActionNotFoundError';\n }\n}\n\n/**\n * 网络错误\n */\nexport class NetworkError extends CapabilityError {\n constructor(message: string) {\n super(message, 'NETWORK_ERROR');\n this.name = 'NetworkError';\n }\n}\n\n/**\n * 执行错误\n */\nexport class ExecutionError extends CapabilityError {\n constructor(message: string, statusCode?: number) {\n super(message, 'EXECUTION_ERROR', statusCode);\n this.name = 'ExecutionError';\n }\n}\n","import type { CapabilityClientOptions, CapabilityInfo, ApiResponse } from './types';\nimport {\n CapabilityError,\n CapabilityNotFoundError,\n ActionNotFoundError,\n NetworkError,\n ExecutionError,\n} from './errors';\n\nconst DEFAULT_OPTIONS: Required<CapabilityClientOptions> = {\n baseURL: '',\n fetchOptions: {},\n autoInit: true,\n};\n\n/**\n * 能力客户端\n */\nexport class CapabilityClient {\n private options: Required<CapabilityClientOptions>;\n private capabilities: Map<string, CapabilityInfo> = new Map();\n private _initialized = false;\n private initPromise: Promise<void> | null = null;\n\n /**\n * 是否已初始化\n */\n get initialized(): boolean {\n return this._initialized;\n }\n\n constructor(options?: CapabilityClientOptions) {\n this.options = { ...DEFAULT_OPTIONS, ...options };\n }\n\n /**\n * 初始化客户端,获取能力列表\n */\n async init(): Promise<void> {\n if (this._initialized) {\n return;\n }\n\n // 防止并发初始化\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = this.doInit();\n try {\n await this.initPromise;\n } finally {\n this.initPromise = null;\n }\n }\n\n private async doInit(): Promise<void> {\n const url = `${this.options.baseURL}/api/capability/list`;\n\n try {\n const response = await fetch(url, {\n method: 'GET',\n ...this.options.fetchOptions,\n headers: {\n 'Content-Type': 'application/json',\n ...this.options.fetchOptions.headers,\n },\n });\n\n if (!response.ok) {\n throw new NetworkError(`Failed to fetch capabilities: ${response.status} ${response.statusText}`);\n }\n\n const data = (await response.json()) as ApiResponse<CapabilityInfo[]>;\n\n if (data.code !== 0) {\n throw new ExecutionError((data as { message: string }).message);\n }\n\n this.capabilities.clear();\n for (const capability of (data as { data: CapabilityInfo[] }).data) {\n this.capabilities.set(capability.id, capability);\n }\n\n this._initialized = true;\n } catch (error) {\n if (error instanceof CapabilityError) {\n throw error;\n }\n throw new NetworkError(error instanceof Error ? error.message : String(error));\n }\n }\n\n /**\n * 确保已初始化\n */\n private async ensureInitialized(): Promise<void> {\n if (!this._initialized && this.options.autoInit) {\n await this.init();\n }\n }\n\n /**\n * 调用能力\n * @param capabilityId - 能力 ID\n * @param action - Action 名称\n * @param params - 输入参数\n * @returns Action 执行结果\n */\n async call<T = unknown>(\n capabilityId: string,\n action: string,\n params?: Record<string, unknown>\n ): Promise<T> {\n await this.ensureInitialized();\n\n // 检查能力是否存在(如果已初始化)\n if (this._initialized && !this.capabilities.has(capabilityId)) {\n throw new CapabilityNotFoundError(capabilityId);\n }\n\n const url = `${this.options.baseURL}/api/capability/${capabilityId}`;\n\n try {\n const response = await fetch(url, {\n method: 'POST',\n ...this.options.fetchOptions,\n headers: {\n 'Content-Type': 'application/json',\n ...this.options.fetchOptions.headers,\n },\n body: JSON.stringify({\n action,\n params: params ?? {},\n }),\n });\n\n const data = (await response.json()) as ApiResponse<T>;\n\n if (!response.ok || data.code !== 0) {\n const errorResponse = data as { code: number; message: string; error?: string };\n const errorCode = errorResponse.error ?? 'UNKNOWN_ERROR';\n\n switch (errorCode) {\n case 'CAPABILITY_NOT_FOUND':\n throw new CapabilityNotFoundError(capabilityId, response.status);\n case 'ACTION_NOT_FOUND':\n throw new ActionNotFoundError(errorResponse.message, response.status);\n case 'PLUGIN_NOT_FOUND':\n case 'EXECUTION_ERROR':\n default:\n throw new ExecutionError(errorResponse.message, response.status);\n }\n }\n\n return (data as { data: T }).data;\n } catch (error) {\n if (error instanceof CapabilityError) {\n throw error;\n }\n throw new NetworkError(error instanceof Error ? error.message : String(error));\n }\n }\n\n /**\n * 检查能力是否存在\n */\n hasCapability(capabilityId: string): boolean {\n return this.capabilities.has(capabilityId);\n }\n\n /**\n * 获取所有可用能力\n */\n async listCapabilities(): Promise<CapabilityInfo[]> {\n await this.ensureInitialized();\n return Array.from(this.capabilities.values());\n }\n\n /**\n * 获取单个能力信息\n */\n async getCapability(capabilityId: string): Promise<CapabilityInfo | null> {\n await this.ensureInitialized();\n return this.capabilities.get(capabilityId) ?? null;\n }\n\n /**\n * 流式调用能力\n * @param capabilityId - 能力 ID\n * @param action - Action 名称\n * @param params - 输入参数\n * @returns AsyncIterable,逐个 yield chunk\n */\n async *callStream<T = unknown>(\n capabilityId: string,\n action: string,\n params?: Record<string, unknown>\n ): AsyncIterable<T> {\n await this.ensureInitialized();\n\n // 检查能力是否存在(如果已初始化)\n if (this._initialized && !this.capabilities.has(capabilityId)) {\n throw new CapabilityNotFoundError(capabilityId);\n }\n\n const url = `${this.options.baseURL}/api/capability/${capabilityId}/stream`;\n\n let response: Response;\n try {\n response = await fetch(url, {\n method: 'POST',\n ...this.options.fetchOptions,\n headers: {\n 'Content-Type': 'application/json',\n ...this.options.fetchOptions.headers,\n },\n body: JSON.stringify({\n action,\n params: params ?? {},\n }),\n });\n } catch (error) {\n throw new NetworkError(error instanceof Error ? error.message : String(error));\n }\n\n if (!response.ok) {\n throw new NetworkError(`HTTP ${response.status} ${response.statusText}`);\n }\n\n if (!response.body) {\n throw new NetworkError('Response body is null');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n const lines = buffer.split('\\n');\n buffer = lines.pop() ?? '';\n\n for (const line of lines) {\n if (line.startsWith('data: ')) {\n const data = line.slice(6);\n\n // 流结束标记\n if (data === '[DONE]') {\n return;\n }\n\n try {\n const parsed = JSON.parse(data) as { error?: string; code?: string } & T;\n\n // 服务端错误\n if (parsed.error) {\n const errorCode = parsed.code ?? 'EXECUTION_ERROR';\n switch (errorCode) {\n case 'CAPABILITY_NOT_FOUND':\n throw new CapabilityNotFoundError(capabilityId);\n case 'ACTION_NOT_FOUND':\n throw new ActionNotFoundError(parsed.error);\n default:\n throw new ExecutionError(parsed.error);\n }\n }\n\n yield parsed as T;\n } catch (parseError) {\n // JSON 解析错误,跳过此行\n if (parseError instanceof CapabilityError) {\n throw parseError;\n }\n // 忽略非 JSON 行\n }\n }\n }\n }\n } catch (error) {\n if (error instanceof CapabilityError) {\n throw error;\n }\n throw new NetworkError(error instanceof Error ? error.message : String(error));\n } finally {\n reader.releaseLock();\n }\n }\n}\n\n/**\n * 创建自定义客户端\n */\nexport function createClient(options?: CapabilityClientOptions): CapabilityClient {\n return new CapabilityClient(options);\n}\n\n/**\n * 默认客户端实例\n */\nexport const client = new CapabilityClient();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;ACGO,IAAMA,mBAAN,MAAMA,yBAAwBC,MAAAA;EAMnC,YAAYC,SAAiBC,MAAcC,YAAqB;AAC9D,UAAMF,OAAAA;AALRC;;AAEAC;;AAIE,SAAKC,OAAO;AACZ,SAAKF,OAAOA;AACZ,SAAKC,aAAaA;EACpB;AACF;AAZqCH;AAA9B,IAAMD,kBAAN;AAiBA,IAAMM,6BAAN,MAAMA,mCAAkCN,gBAAAA;EAC7C,cAAc;AACZ,UAAM,iEAAiE,wBAAA;AACvE,SAAKK,OAAO;EACd;AACF;AAL+CL;AAAxC,IAAMM,4BAAN;AAUA,IAAMC,2BAAN,MAAMA,iCAAgCP,gBAAAA;EAC3C,YAAYQ,cAAsBJ,YAAqB;AACrD,UAAM,yBAAyBI,YAAAA,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eAAe+C,OAAOzC,KAAK;gBACzC;cACF;AAEA,oBAAMyC;YACR,SAASE,YAAY;AAEnB,kBAAIA,sBAAsB1C,iBAAiB;AACzC,sBAAM0C;cACR;YAEF;UACF;QACF;MACF;IACF,SAAS3C,OAAO;AACd,UAAIA,iBAAiBC,iBAAiB;AACpC,cAAMD;MACR;AACA,YAAM,IAAIZ,aAAaY,iBAAiBE,QAAQF,MAAML,UAAUQ,OAAOH,KAAAA,CAAAA;IACzE,UAAA;AACEyB,aAAOmB,YAAW;IACpB;EACF;AACF;AAlRavE;AAAN,IAAMA,mBAAN;AAuRA,SAASwE,aAAavE,SAAiC;AAC5D,SAAO,IAAID,iBAAiBC,OAAAA;AAC9B;AAFgBuE;AAOT,IAAMC,SAAS,IAAIzE,iBAAAA;","names":["CapabilityError","Error","message","code","statusCode","name","ClientNotInitializedError","CapabilityNotFoundError","capabilityId","ActionNotFoundError","NetworkError","ExecutionError","DEFAULT_OPTIONS","baseURL","fetchOptions","autoInit","CapabilityClient","options","capabilities","Map","_initialized","initPromise","initialized","init","doInit","url","response","fetch","method","headers","ok","NetworkError","status","statusText","data","json","code","ExecutionError","message","clear","capability","set","id","error","CapabilityError","Error","String","ensureInitialized","call","capabilityId","action","params","has","CapabilityNotFoundError","body","JSON","stringify","errorResponse","errorCode","ActionNotFoundError","hasCapability","listCapabilities","Array","from","values","getCapability","get","callStream","reader","getReader","decoder","TextDecoder","buffer","done","value","read","decode","stream","lines","split","pop","line","startsWith","slice","parsed","parse","parseError","releaseLock","createClient","client"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 客户端配置选项
|
|
3
|
+
*/
|
|
4
|
+
interface CapabilityClientOptions {
|
|
5
|
+
/** API 基础路径,默认 '' (相对路径) */
|
|
6
|
+
baseURL?: string;
|
|
7
|
+
/** 自定义 fetch 配置 */
|
|
8
|
+
fetchOptions?: RequestInit;
|
|
9
|
+
/** 是否在创建时自动初始化,默认 true */
|
|
10
|
+
autoInit?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 能力信息
|
|
14
|
+
*/
|
|
15
|
+
interface CapabilityInfo {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
pluginID: string;
|
|
20
|
+
pluginVersion: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 能力客户端
|
|
25
|
+
*/
|
|
26
|
+
declare class CapabilityClient {
|
|
27
|
+
private options;
|
|
28
|
+
private capabilities;
|
|
29
|
+
private _initialized;
|
|
30
|
+
private initPromise;
|
|
31
|
+
/**
|
|
32
|
+
* 是否已初始化
|
|
33
|
+
*/
|
|
34
|
+
get initialized(): boolean;
|
|
35
|
+
constructor(options?: CapabilityClientOptions);
|
|
36
|
+
/**
|
|
37
|
+
* 初始化客户端,获取能力列表
|
|
38
|
+
*/
|
|
39
|
+
init(): Promise<void>;
|
|
40
|
+
private doInit;
|
|
41
|
+
/**
|
|
42
|
+
* 确保已初始化
|
|
43
|
+
*/
|
|
44
|
+
private ensureInitialized;
|
|
45
|
+
/**
|
|
46
|
+
* 调用能力
|
|
47
|
+
* @param capabilityId - 能力 ID
|
|
48
|
+
* @param action - Action 名称
|
|
49
|
+
* @param params - 输入参数
|
|
50
|
+
* @returns Action 执行结果
|
|
51
|
+
*/
|
|
52
|
+
call<T = unknown>(capabilityId: string, action: string, params?: Record<string, unknown>): Promise<T>;
|
|
53
|
+
/**
|
|
54
|
+
* 检查能力是否存在
|
|
55
|
+
*/
|
|
56
|
+
hasCapability(capabilityId: string): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* 获取所有可用能力
|
|
59
|
+
*/
|
|
60
|
+
listCapabilities(): Promise<CapabilityInfo[]>;
|
|
61
|
+
/**
|
|
62
|
+
* 获取单个能力信息
|
|
63
|
+
*/
|
|
64
|
+
getCapability(capabilityId: string): Promise<CapabilityInfo | null>;
|
|
65
|
+
/**
|
|
66
|
+
* 流式调用能力
|
|
67
|
+
* @param capabilityId - 能力 ID
|
|
68
|
+
* @param action - Action 名称
|
|
69
|
+
* @param params - 输入参数
|
|
70
|
+
* @returns AsyncIterable,逐个 yield chunk
|
|
71
|
+
*/
|
|
72
|
+
callStream<T = unknown>(capabilityId: string, action: string, params?: Record<string, unknown>): AsyncIterable<T>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 创建自定义客户端
|
|
76
|
+
*/
|
|
77
|
+
declare function createClient(options?: CapabilityClientOptions): CapabilityClient;
|
|
78
|
+
/**
|
|
79
|
+
* 默认客户端实例
|
|
80
|
+
*/
|
|
81
|
+
declare const client: CapabilityClient;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 能力调用错误基类
|
|
85
|
+
*/
|
|
86
|
+
declare class CapabilityError extends Error {
|
|
87
|
+
/** 错误码 */
|
|
88
|
+
code: string;
|
|
89
|
+
/** HTTP 状态码 */
|
|
90
|
+
statusCode?: number;
|
|
91
|
+
constructor(message: string, code: string, statusCode?: number);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 客户端未初始化错误
|
|
95
|
+
*/
|
|
96
|
+
declare class ClientNotInitializedError extends CapabilityError {
|
|
97
|
+
constructor();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 能力不存在错误
|
|
101
|
+
*/
|
|
102
|
+
declare class CapabilityNotFoundError extends CapabilityError {
|
|
103
|
+
constructor(capabilityId: string, statusCode?: number);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Action 不存在错误
|
|
107
|
+
*/
|
|
108
|
+
declare class ActionNotFoundError extends CapabilityError {
|
|
109
|
+
constructor(message: string, statusCode?: number);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 网络错误
|
|
113
|
+
*/
|
|
114
|
+
declare class NetworkError extends CapabilityError {
|
|
115
|
+
constructor(message: string);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 执行错误
|
|
119
|
+
*/
|
|
120
|
+
declare class ExecutionError extends CapabilityError {
|
|
121
|
+
constructor(message: string, statusCode?: number);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { ActionNotFoundError, CapabilityClient, type CapabilityClientOptions, CapabilityError, type CapabilityInfo, CapabilityNotFoundError, ClientNotInitializedError, ExecutionError, NetworkError, client, createClient };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 客户端配置选项
|
|
3
|
+
*/
|
|
4
|
+
interface CapabilityClientOptions {
|
|
5
|
+
/** API 基础路径,默认 '' (相对路径) */
|
|
6
|
+
baseURL?: string;
|
|
7
|
+
/** 自定义 fetch 配置 */
|
|
8
|
+
fetchOptions?: RequestInit;
|
|
9
|
+
/** 是否在创建时自动初始化,默认 true */
|
|
10
|
+
autoInit?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 能力信息
|
|
14
|
+
*/
|
|
15
|
+
interface CapabilityInfo {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
pluginID: string;
|
|
20
|
+
pluginVersion: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 能力客户端
|
|
25
|
+
*/
|
|
26
|
+
declare class CapabilityClient {
|
|
27
|
+
private options;
|
|
28
|
+
private capabilities;
|
|
29
|
+
private _initialized;
|
|
30
|
+
private initPromise;
|
|
31
|
+
/**
|
|
32
|
+
* 是否已初始化
|
|
33
|
+
*/
|
|
34
|
+
get initialized(): boolean;
|
|
35
|
+
constructor(options?: CapabilityClientOptions);
|
|
36
|
+
/**
|
|
37
|
+
* 初始化客户端,获取能力列表
|
|
38
|
+
*/
|
|
39
|
+
init(): Promise<void>;
|
|
40
|
+
private doInit;
|
|
41
|
+
/**
|
|
42
|
+
* 确保已初始化
|
|
43
|
+
*/
|
|
44
|
+
private ensureInitialized;
|
|
45
|
+
/**
|
|
46
|
+
* 调用能力
|
|
47
|
+
* @param capabilityId - 能力 ID
|
|
48
|
+
* @param action - Action 名称
|
|
49
|
+
* @param params - 输入参数
|
|
50
|
+
* @returns Action 执行结果
|
|
51
|
+
*/
|
|
52
|
+
call<T = unknown>(capabilityId: string, action: string, params?: Record<string, unknown>): Promise<T>;
|
|
53
|
+
/**
|
|
54
|
+
* 检查能力是否存在
|
|
55
|
+
*/
|
|
56
|
+
hasCapability(capabilityId: string): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* 获取所有可用能力
|
|
59
|
+
*/
|
|
60
|
+
listCapabilities(): Promise<CapabilityInfo[]>;
|
|
61
|
+
/**
|
|
62
|
+
* 获取单个能力信息
|
|
63
|
+
*/
|
|
64
|
+
getCapability(capabilityId: string): Promise<CapabilityInfo | null>;
|
|
65
|
+
/**
|
|
66
|
+
* 流式调用能力
|
|
67
|
+
* @param capabilityId - 能力 ID
|
|
68
|
+
* @param action - Action 名称
|
|
69
|
+
* @param params - 输入参数
|
|
70
|
+
* @returns AsyncIterable,逐个 yield chunk
|
|
71
|
+
*/
|
|
72
|
+
callStream<T = unknown>(capabilityId: string, action: string, params?: Record<string, unknown>): AsyncIterable<T>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 创建自定义客户端
|
|
76
|
+
*/
|
|
77
|
+
declare function createClient(options?: CapabilityClientOptions): CapabilityClient;
|
|
78
|
+
/**
|
|
79
|
+
* 默认客户端实例
|
|
80
|
+
*/
|
|
81
|
+
declare const client: CapabilityClient;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 能力调用错误基类
|
|
85
|
+
*/
|
|
86
|
+
declare class CapabilityError extends Error {
|
|
87
|
+
/** 错误码 */
|
|
88
|
+
code: string;
|
|
89
|
+
/** HTTP 状态码 */
|
|
90
|
+
statusCode?: number;
|
|
91
|
+
constructor(message: string, code: string, statusCode?: number);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 客户端未初始化错误
|
|
95
|
+
*/
|
|
96
|
+
declare class ClientNotInitializedError extends CapabilityError {
|
|
97
|
+
constructor();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 能力不存在错误
|
|
101
|
+
*/
|
|
102
|
+
declare class CapabilityNotFoundError extends CapabilityError {
|
|
103
|
+
constructor(capabilityId: string, statusCode?: number);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Action 不存在错误
|
|
107
|
+
*/
|
|
108
|
+
declare class ActionNotFoundError extends CapabilityError {
|
|
109
|
+
constructor(message: string, statusCode?: number);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 网络错误
|
|
113
|
+
*/
|
|
114
|
+
declare class NetworkError extends CapabilityError {
|
|
115
|
+
constructor(message: string);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 执行错误
|
|
119
|
+
*/
|
|
120
|
+
declare class ExecutionError extends CapabilityError {
|
|
121
|
+
constructor(message: string, statusCode?: number);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { ActionNotFoundError, CapabilityClient, type CapabilityClientOptions, CapabilityError, type CapabilityInfo, CapabilityNotFoundError, ClientNotInitializedError, ExecutionError, NetworkError, client, createClient };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
|
|
6
|
+
// src/errors.ts
|
|
7
|
+
var _CapabilityError = class _CapabilityError extends Error {
|
|
8
|
+
constructor(message, code, statusCode) {
|
|
9
|
+
super(message);
|
|
10
|
+
/** 错误码 */
|
|
11
|
+
__publicField(this, "code");
|
|
12
|
+
/** HTTP 状态码 */
|
|
13
|
+
__publicField(this, "statusCode");
|
|
14
|
+
this.name = "CapabilityError";
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.statusCode = statusCode;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
__name(_CapabilityError, "CapabilityError");
|
|
20
|
+
var CapabilityError = _CapabilityError;
|
|
21
|
+
var _ClientNotInitializedError = class _ClientNotInitializedError extends CapabilityError {
|
|
22
|
+
constructor() {
|
|
23
|
+
super("Client not initialized. Call init() first or enable autoInit.", "CLIENT_NOT_INITIALIZED");
|
|
24
|
+
this.name = "ClientNotInitializedError";
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
__name(_ClientNotInitializedError, "ClientNotInitializedError");
|
|
28
|
+
var ClientNotInitializedError = _ClientNotInitializedError;
|
|
29
|
+
var _CapabilityNotFoundError = class _CapabilityNotFoundError extends CapabilityError {
|
|
30
|
+
constructor(capabilityId, statusCode) {
|
|
31
|
+
super(`Capability not found: ${capabilityId}`, "CAPABILITY_NOT_FOUND", statusCode);
|
|
32
|
+
this.name = "CapabilityNotFoundError";
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
__name(_CapabilityNotFoundError, "CapabilityNotFoundError");
|
|
36
|
+
var CapabilityNotFoundError = _CapabilityNotFoundError;
|
|
37
|
+
var _ActionNotFoundError = class _ActionNotFoundError extends CapabilityError {
|
|
38
|
+
constructor(message, statusCode) {
|
|
39
|
+
super(message, "ACTION_NOT_FOUND", statusCode);
|
|
40
|
+
this.name = "ActionNotFoundError";
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
__name(_ActionNotFoundError, "ActionNotFoundError");
|
|
44
|
+
var ActionNotFoundError = _ActionNotFoundError;
|
|
45
|
+
var _NetworkError = class _NetworkError extends CapabilityError {
|
|
46
|
+
constructor(message) {
|
|
47
|
+
super(message, "NETWORK_ERROR");
|
|
48
|
+
this.name = "NetworkError";
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
__name(_NetworkError, "NetworkError");
|
|
52
|
+
var NetworkError = _NetworkError;
|
|
53
|
+
var _ExecutionError = class _ExecutionError extends CapabilityError {
|
|
54
|
+
constructor(message, statusCode) {
|
|
55
|
+
super(message, "EXECUTION_ERROR", statusCode);
|
|
56
|
+
this.name = "ExecutionError";
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
__name(_ExecutionError, "ExecutionError");
|
|
60
|
+
var ExecutionError = _ExecutionError;
|
|
61
|
+
|
|
62
|
+
// src/client.ts
|
|
63
|
+
var DEFAULT_OPTIONS = {
|
|
64
|
+
baseURL: "",
|
|
65
|
+
fetchOptions: {},
|
|
66
|
+
autoInit: true
|
|
67
|
+
};
|
|
68
|
+
var _CapabilityClient = class _CapabilityClient {
|
|
69
|
+
constructor(options) {
|
|
70
|
+
__publicField(this, "options");
|
|
71
|
+
__publicField(this, "capabilities", /* @__PURE__ */ new Map());
|
|
72
|
+
__publicField(this, "_initialized", false);
|
|
73
|
+
__publicField(this, "initPromise", null);
|
|
74
|
+
this.options = {
|
|
75
|
+
...DEFAULT_OPTIONS,
|
|
76
|
+
...options
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 是否已初始化
|
|
81
|
+
*/
|
|
82
|
+
get initialized() {
|
|
83
|
+
return this._initialized;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 初始化客户端,获取能力列表
|
|
87
|
+
*/
|
|
88
|
+
async init() {
|
|
89
|
+
if (this._initialized) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (this.initPromise) {
|
|
93
|
+
return this.initPromise;
|
|
94
|
+
}
|
|
95
|
+
this.initPromise = this.doInit();
|
|
96
|
+
try {
|
|
97
|
+
await this.initPromise;
|
|
98
|
+
} finally {
|
|
99
|
+
this.initPromise = null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async doInit() {
|
|
103
|
+
const url = `${this.options.baseURL}/api/capability/list`;
|
|
104
|
+
try {
|
|
105
|
+
const response = await fetch(url, {
|
|
106
|
+
method: "GET",
|
|
107
|
+
...this.options.fetchOptions,
|
|
108
|
+
headers: {
|
|
109
|
+
"Content-Type": "application/json",
|
|
110
|
+
...this.options.fetchOptions.headers
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
if (!response.ok) {
|
|
114
|
+
throw new NetworkError(`Failed to fetch capabilities: ${response.status} ${response.statusText}`);
|
|
115
|
+
}
|
|
116
|
+
const data = await response.json();
|
|
117
|
+
if (data.code !== 0) {
|
|
118
|
+
throw new ExecutionError(data.message);
|
|
119
|
+
}
|
|
120
|
+
this.capabilities.clear();
|
|
121
|
+
for (const capability of data.data) {
|
|
122
|
+
this.capabilities.set(capability.id, capability);
|
|
123
|
+
}
|
|
124
|
+
this._initialized = true;
|
|
125
|
+
} catch (error) {
|
|
126
|
+
if (error instanceof CapabilityError) {
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
throw new NetworkError(error instanceof Error ? error.message : String(error));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 确保已初始化
|
|
134
|
+
*/
|
|
135
|
+
async ensureInitialized() {
|
|
136
|
+
if (!this._initialized && this.options.autoInit) {
|
|
137
|
+
await this.init();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 调用能力
|
|
142
|
+
* @param capabilityId - 能力 ID
|
|
143
|
+
* @param action - Action 名称
|
|
144
|
+
* @param params - 输入参数
|
|
145
|
+
* @returns Action 执行结果
|
|
146
|
+
*/
|
|
147
|
+
async call(capabilityId, action, params) {
|
|
148
|
+
await this.ensureInitialized();
|
|
149
|
+
if (this._initialized && !this.capabilities.has(capabilityId)) {
|
|
150
|
+
throw new CapabilityNotFoundError(capabilityId);
|
|
151
|
+
}
|
|
152
|
+
const url = `${this.options.baseURL}/api/capability/${capabilityId}`;
|
|
153
|
+
try {
|
|
154
|
+
const response = await fetch(url, {
|
|
155
|
+
method: "POST",
|
|
156
|
+
...this.options.fetchOptions,
|
|
157
|
+
headers: {
|
|
158
|
+
"Content-Type": "application/json",
|
|
159
|
+
...this.options.fetchOptions.headers
|
|
160
|
+
},
|
|
161
|
+
body: JSON.stringify({
|
|
162
|
+
action,
|
|
163
|
+
params: params ?? {}
|
|
164
|
+
})
|
|
165
|
+
});
|
|
166
|
+
const data = await response.json();
|
|
167
|
+
if (!response.ok || data.code !== 0) {
|
|
168
|
+
const errorResponse = data;
|
|
169
|
+
const errorCode = errorResponse.error ?? "UNKNOWN_ERROR";
|
|
170
|
+
switch (errorCode) {
|
|
171
|
+
case "CAPABILITY_NOT_FOUND":
|
|
172
|
+
throw new CapabilityNotFoundError(capabilityId, response.status);
|
|
173
|
+
case "ACTION_NOT_FOUND":
|
|
174
|
+
throw new ActionNotFoundError(errorResponse.message, response.status);
|
|
175
|
+
case "PLUGIN_NOT_FOUND":
|
|
176
|
+
case "EXECUTION_ERROR":
|
|
177
|
+
default:
|
|
178
|
+
throw new ExecutionError(errorResponse.message, response.status);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return data.data;
|
|
182
|
+
} catch (error) {
|
|
183
|
+
if (error instanceof CapabilityError) {
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
throw new NetworkError(error instanceof Error ? error.message : String(error));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* 检查能力是否存在
|
|
191
|
+
*/
|
|
192
|
+
hasCapability(capabilityId) {
|
|
193
|
+
return this.capabilities.has(capabilityId);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* 获取所有可用能力
|
|
197
|
+
*/
|
|
198
|
+
async listCapabilities() {
|
|
199
|
+
await this.ensureInitialized();
|
|
200
|
+
return Array.from(this.capabilities.values());
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 获取单个能力信息
|
|
204
|
+
*/
|
|
205
|
+
async getCapability(capabilityId) {
|
|
206
|
+
await this.ensureInitialized();
|
|
207
|
+
return this.capabilities.get(capabilityId) ?? null;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* 流式调用能力
|
|
211
|
+
* @param capabilityId - 能力 ID
|
|
212
|
+
* @param action - Action 名称
|
|
213
|
+
* @param params - 输入参数
|
|
214
|
+
* @returns AsyncIterable,逐个 yield chunk
|
|
215
|
+
*/
|
|
216
|
+
async *callStream(capabilityId, action, params) {
|
|
217
|
+
await this.ensureInitialized();
|
|
218
|
+
if (this._initialized && !this.capabilities.has(capabilityId)) {
|
|
219
|
+
throw new CapabilityNotFoundError(capabilityId);
|
|
220
|
+
}
|
|
221
|
+
const url = `${this.options.baseURL}/api/capability/${capabilityId}/stream`;
|
|
222
|
+
let response;
|
|
223
|
+
try {
|
|
224
|
+
response = await fetch(url, {
|
|
225
|
+
method: "POST",
|
|
226
|
+
...this.options.fetchOptions,
|
|
227
|
+
headers: {
|
|
228
|
+
"Content-Type": "application/json",
|
|
229
|
+
...this.options.fetchOptions.headers
|
|
230
|
+
},
|
|
231
|
+
body: JSON.stringify({
|
|
232
|
+
action,
|
|
233
|
+
params: params ?? {}
|
|
234
|
+
})
|
|
235
|
+
});
|
|
236
|
+
} catch (error) {
|
|
237
|
+
throw new NetworkError(error instanceof Error ? error.message : String(error));
|
|
238
|
+
}
|
|
239
|
+
if (!response.ok) {
|
|
240
|
+
throw new NetworkError(`HTTP ${response.status} ${response.statusText}`);
|
|
241
|
+
}
|
|
242
|
+
if (!response.body) {
|
|
243
|
+
throw new NetworkError("Response body is null");
|
|
244
|
+
}
|
|
245
|
+
const reader = response.body.getReader();
|
|
246
|
+
const decoder = new TextDecoder();
|
|
247
|
+
let buffer = "";
|
|
248
|
+
try {
|
|
249
|
+
while (true) {
|
|
250
|
+
const { done, value } = await reader.read();
|
|
251
|
+
if (done) break;
|
|
252
|
+
buffer += decoder.decode(value, {
|
|
253
|
+
stream: true
|
|
254
|
+
});
|
|
255
|
+
const lines = buffer.split("\n");
|
|
256
|
+
buffer = lines.pop() ?? "";
|
|
257
|
+
for (const line of lines) {
|
|
258
|
+
if (line.startsWith("data: ")) {
|
|
259
|
+
const data = line.slice(6);
|
|
260
|
+
if (data === "[DONE]") {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
const parsed = JSON.parse(data);
|
|
265
|
+
if (parsed.error) {
|
|
266
|
+
const errorCode = parsed.code ?? "EXECUTION_ERROR";
|
|
267
|
+
switch (errorCode) {
|
|
268
|
+
case "CAPABILITY_NOT_FOUND":
|
|
269
|
+
throw new CapabilityNotFoundError(capabilityId);
|
|
270
|
+
case "ACTION_NOT_FOUND":
|
|
271
|
+
throw new ActionNotFoundError(parsed.error);
|
|
272
|
+
default:
|
|
273
|
+
throw new ExecutionError(parsed.error);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
yield parsed;
|
|
277
|
+
} catch (parseError) {
|
|
278
|
+
if (parseError instanceof CapabilityError) {
|
|
279
|
+
throw parseError;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
} catch (error) {
|
|
286
|
+
if (error instanceof CapabilityError) {
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
throw new NetworkError(error instanceof Error ? error.message : String(error));
|
|
290
|
+
} finally {
|
|
291
|
+
reader.releaseLock();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
__name(_CapabilityClient, "CapabilityClient");
|
|
296
|
+
var CapabilityClient = _CapabilityClient;
|
|
297
|
+
function createClient(options) {
|
|
298
|
+
return new CapabilityClient(options);
|
|
299
|
+
}
|
|
300
|
+
__name(createClient, "createClient");
|
|
301
|
+
var client = new CapabilityClient();
|
|
302
|
+
export {
|
|
303
|
+
ActionNotFoundError,
|
|
304
|
+
CapabilityClient,
|
|
305
|
+
CapabilityError,
|
|
306
|
+
CapabilityNotFoundError,
|
|
307
|
+
ClientNotInitializedError,
|
|
308
|
+
ExecutionError,
|
|
309
|
+
NetworkError,
|
|
310
|
+
client,
|
|
311
|
+
createClient
|
|
312
|
+
};
|
|
313
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/client.ts"],"sourcesContent":["/**\n * 能力调用错误基类\n */\nexport class CapabilityError extends Error {\n /** 错误码 */\n code: string;\n /** HTTP 状态码 */\n statusCode?: number;\n\n constructor(message: string, code: string, statusCode?: number) {\n super(message);\n this.name = 'CapabilityError';\n this.code = code;\n this.statusCode = statusCode;\n }\n}\n\n/**\n * 客户端未初始化错误\n */\nexport class ClientNotInitializedError extends CapabilityError {\n constructor() {\n super('Client not initialized. Call init() first or enable autoInit.', 'CLIENT_NOT_INITIALIZED');\n this.name = 'ClientNotInitializedError';\n }\n}\n\n/**\n * 能力不存在错误\n */\nexport class CapabilityNotFoundError extends CapabilityError {\n constructor(capabilityId: string, statusCode?: number) {\n super(`Capability not found: ${capabilityId}`, 'CAPABILITY_NOT_FOUND', statusCode);\n this.name = 'CapabilityNotFoundError';\n }\n}\n\n/**\n * Action 不存在错误\n */\nexport class ActionNotFoundError extends CapabilityError {\n constructor(message: string, statusCode?: number) {\n super(message, 'ACTION_NOT_FOUND', statusCode);\n this.name = 'ActionNotFoundError';\n }\n}\n\n/**\n * 网络错误\n */\nexport class NetworkError extends CapabilityError {\n constructor(message: string) {\n super(message, 'NETWORK_ERROR');\n this.name = 'NetworkError';\n }\n}\n\n/**\n * 执行错误\n */\nexport class ExecutionError extends CapabilityError {\n constructor(message: string, statusCode?: number) {\n super(message, 'EXECUTION_ERROR', statusCode);\n this.name = 'ExecutionError';\n }\n}\n","import type { CapabilityClientOptions, CapabilityInfo, ApiResponse } from './types';\nimport {\n CapabilityError,\n CapabilityNotFoundError,\n ActionNotFoundError,\n NetworkError,\n ExecutionError,\n} from './errors';\n\nconst DEFAULT_OPTIONS: Required<CapabilityClientOptions> = {\n baseURL: '',\n fetchOptions: {},\n autoInit: true,\n};\n\n/**\n * 能力客户端\n */\nexport class CapabilityClient {\n private options: Required<CapabilityClientOptions>;\n private capabilities: Map<string, CapabilityInfo> = new Map();\n private _initialized = false;\n private initPromise: Promise<void> | null = null;\n\n /**\n * 是否已初始化\n */\n get initialized(): boolean {\n return this._initialized;\n }\n\n constructor(options?: CapabilityClientOptions) {\n this.options = { ...DEFAULT_OPTIONS, ...options };\n }\n\n /**\n * 初始化客户端,获取能力列表\n */\n async init(): Promise<void> {\n if (this._initialized) {\n return;\n }\n\n // 防止并发初始化\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = this.doInit();\n try {\n await this.initPromise;\n } finally {\n this.initPromise = null;\n }\n }\n\n private async doInit(): Promise<void> {\n const url = `${this.options.baseURL}/api/capability/list`;\n\n try {\n const response = await fetch(url, {\n method: 'GET',\n ...this.options.fetchOptions,\n headers: {\n 'Content-Type': 'application/json',\n ...this.options.fetchOptions.headers,\n },\n });\n\n if (!response.ok) {\n throw new NetworkError(`Failed to fetch capabilities: ${response.status} ${response.statusText}`);\n }\n\n const data = (await response.json()) as ApiResponse<CapabilityInfo[]>;\n\n if (data.code !== 0) {\n throw new ExecutionError((data as { message: string }).message);\n }\n\n this.capabilities.clear();\n for (const capability of (data as { data: CapabilityInfo[] }).data) {\n this.capabilities.set(capability.id, capability);\n }\n\n this._initialized = true;\n } catch (error) {\n if (error instanceof CapabilityError) {\n throw error;\n }\n throw new NetworkError(error instanceof Error ? error.message : String(error));\n }\n }\n\n /**\n * 确保已初始化\n */\n private async ensureInitialized(): Promise<void> {\n if (!this._initialized && this.options.autoInit) {\n await this.init();\n }\n }\n\n /**\n * 调用能力\n * @param capabilityId - 能力 ID\n * @param action - Action 名称\n * @param params - 输入参数\n * @returns Action 执行结果\n */\n async call<T = unknown>(\n capabilityId: string,\n action: string,\n params?: Record<string, unknown>\n ): Promise<T> {\n await this.ensureInitialized();\n\n // 检查能力是否存在(如果已初始化)\n if (this._initialized && !this.capabilities.has(capabilityId)) {\n throw new CapabilityNotFoundError(capabilityId);\n }\n\n const url = `${this.options.baseURL}/api/capability/${capabilityId}`;\n\n try {\n const response = await fetch(url, {\n method: 'POST',\n ...this.options.fetchOptions,\n headers: {\n 'Content-Type': 'application/json',\n ...this.options.fetchOptions.headers,\n },\n body: JSON.stringify({\n action,\n params: params ?? {},\n }),\n });\n\n const data = (await response.json()) as ApiResponse<T>;\n\n if (!response.ok || data.code !== 0) {\n const errorResponse = data as { code: number; message: string; error?: string };\n const errorCode = errorResponse.error ?? 'UNKNOWN_ERROR';\n\n switch (errorCode) {\n case 'CAPABILITY_NOT_FOUND':\n throw new CapabilityNotFoundError(capabilityId, response.status);\n case 'ACTION_NOT_FOUND':\n throw new ActionNotFoundError(errorResponse.message, response.status);\n case 'PLUGIN_NOT_FOUND':\n case 'EXECUTION_ERROR':\n default:\n throw new ExecutionError(errorResponse.message, response.status);\n }\n }\n\n return (data as { data: T }).data;\n } catch (error) {\n if (error instanceof CapabilityError) {\n throw error;\n }\n throw new NetworkError(error instanceof Error ? error.message : String(error));\n }\n }\n\n /**\n * 检查能力是否存在\n */\n hasCapability(capabilityId: string): boolean {\n return this.capabilities.has(capabilityId);\n }\n\n /**\n * 获取所有可用能力\n */\n async listCapabilities(): Promise<CapabilityInfo[]> {\n await this.ensureInitialized();\n return Array.from(this.capabilities.values());\n }\n\n /**\n * 获取单个能力信息\n */\n async getCapability(capabilityId: string): Promise<CapabilityInfo | null> {\n await this.ensureInitialized();\n return this.capabilities.get(capabilityId) ?? null;\n }\n\n /**\n * 流式调用能力\n * @param capabilityId - 能力 ID\n * @param action - Action 名称\n * @param params - 输入参数\n * @returns AsyncIterable,逐个 yield chunk\n */\n async *callStream<T = unknown>(\n capabilityId: string,\n action: string,\n params?: Record<string, unknown>\n ): AsyncIterable<T> {\n await this.ensureInitialized();\n\n // 检查能力是否存在(如果已初始化)\n if (this._initialized && !this.capabilities.has(capabilityId)) {\n throw new CapabilityNotFoundError(capabilityId);\n }\n\n const url = `${this.options.baseURL}/api/capability/${capabilityId}/stream`;\n\n let response: Response;\n try {\n response = await fetch(url, {\n method: 'POST',\n ...this.options.fetchOptions,\n headers: {\n 'Content-Type': 'application/json',\n ...this.options.fetchOptions.headers,\n },\n body: JSON.stringify({\n action,\n params: params ?? {},\n }),\n });\n } catch (error) {\n throw new NetworkError(error instanceof Error ? error.message : String(error));\n }\n\n if (!response.ok) {\n throw new NetworkError(`HTTP ${response.status} ${response.statusText}`);\n }\n\n if (!response.body) {\n throw new NetworkError('Response body is null');\n }\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n const lines = buffer.split('\\n');\n buffer = lines.pop() ?? '';\n\n for (const line of lines) {\n if (line.startsWith('data: ')) {\n const data = line.slice(6);\n\n // 流结束标记\n if (data === '[DONE]') {\n return;\n }\n\n try {\n const parsed = JSON.parse(data) as { error?: string; code?: string } & T;\n\n // 服务端错误\n if (parsed.error) {\n const errorCode = parsed.code ?? 'EXECUTION_ERROR';\n switch (errorCode) {\n case 'CAPABILITY_NOT_FOUND':\n throw new CapabilityNotFoundError(capabilityId);\n case 'ACTION_NOT_FOUND':\n throw new ActionNotFoundError(parsed.error);\n default:\n throw new ExecutionError(parsed.error);\n }\n }\n\n yield parsed as T;\n } catch (parseError) {\n // JSON 解析错误,跳过此行\n if (parseError instanceof CapabilityError) {\n throw parseError;\n }\n // 忽略非 JSON 行\n }\n }\n }\n }\n } catch (error) {\n if (error instanceof CapabilityError) {\n throw error;\n }\n throw new NetworkError(error instanceof Error ? error.message : String(error));\n } finally {\n reader.releaseLock();\n }\n }\n}\n\n/**\n * 创建自定义客户端\n */\nexport function createClient(options?: CapabilityClientOptions): CapabilityClient {\n return new CapabilityClient(options);\n}\n\n/**\n * 默认客户端实例\n */\nexport const client = new CapabilityClient();\n"],"mappings":";;;;;;AAGO,IAAMA,mBAAN,MAAMA,yBAAwBC,MAAAA;EAMnC,YAAYC,SAAiBC,MAAcC,YAAqB;AAC9D,UAAMF,OAAAA;AALRC;;AAEAC;;AAIE,SAAKC,OAAO;AACZ,SAAKF,OAAOA;AACZ,SAAKC,aAAaA;EACpB;AACF;AAZqCH;AAA9B,IAAMD,kBAAN;AAiBA,IAAMM,6BAAN,MAAMA,mCAAkCN,gBAAAA;EAC7C,cAAc;AACZ,UAAM,iEAAiE,wBAAA;AACvE,SAAKK,OAAO;EACd;AACF;AAL+CL;AAAxC,IAAMM,4BAAN;AAUA,IAAMC,2BAAN,MAAMA,iCAAgCP,gBAAAA;EAC3C,YAAYQ,cAAsBJ,YAAqB;AACrD,UAAM,yBAAyBI,YAAAA,IAAgB,wBAAwBJ,UAAAA;AACvE,SAAKC,OAAO;EACd;AACF;AAL6CL;AAAtC,IAAMO,0BAAN;AAUA,IAAME,uBAAN,MAAMA,6BAA4BT,gBAAAA;EACvC,YAAYE,SAAiBE,YAAqB;AAChD,UAAMF,SAAS,oBAAoBE,UAAAA;AACnC,SAAKC,OAAO;EACd;AACF;AALyCL;AAAlC,IAAMS,sBAAN;AAUA,IAAMC,gBAAN,MAAMA,sBAAqBV,gBAAAA;EAChC,YAAYE,SAAiB;AAC3B,UAAMA,SAAS,eAAA;AACf,SAAKG,OAAO;EACd;AACF;AALkCL;AAA3B,IAAMU,eAAN;AAUA,IAAMC,kBAAN,MAAMA,wBAAuBX,gBAAAA;EAClC,YAAYE,SAAiBE,YAAqB;AAChD,UAAMF,SAAS,mBAAmBE,UAAAA;AAClC,SAAKC,OAAO;EACd;AACF;AALoCL;AAA7B,IAAMW,iBAAN;;;ACnDP,IAAMC,kBAAqD;EACzDC,SAAS;EACTC,cAAc,CAAC;EACfC,UAAU;AACZ;AAKO,IAAMC,oBAAN,MAAMA,kBAA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AAQM,OAAOF,OAAO;UAAEG,QAAQ;QAAK,CAAA;AAC/C,cAAMC,QAAQN,OAAOO,MAAM,IAAA;AAC3BP,iBAASM,MAAME,IAAG,KAAM;AAExB,mBAAWC,QAAQH,OAAO;AACxB,cAAIG,KAAKC,WAAW,QAAA,GAAW;AAC7B,kBAAMhD,OAAO+C,KAAKE,MAAM,CAAA;AAGxB,gBAAIjD,SAAS,UAAU;AACrB;YACF;AAEA,gBAAI;AACF,oBAAMkD,SAAS7B,KAAK8B,MAAMnD,IAAAA;AAG1B,kBAAIkD,OAAOzC,OAAO;AAChB,sBAAMe,YAAY0B,OAAOhD,QAAQ;AACjC,wBAAQsB,WAAAA;kBACN,KAAK;AACH,0BAAM,IAAIL,wBAAwBJ,YAAAA;kBACpC,KAAK;AACH,0BAAM,IAAIU,oBAAoByB,OAAOzC,KAAK;kBAC5C;AACE,0BAAM,IAAIN,eAAe+C,OAAOzC,KAAK;gBACzC;cACF;AAEA,oBAAMyC;YACR,SAASE,YAAY;AAEnB,kBAAIA,sBAAsB1C,iBAAiB;AACzC,sBAAM0C;cACR;YAEF;UACF;QACF;MACF;IACF,SAAS3C,OAAO;AACd,UAAIA,iBAAiBC,iBAAiB;AACpC,cAAMD;MACR;AACA,YAAM,IAAIZ,aAAaY,iBAAiBE,QAAQF,MAAML,UAAUQ,OAAOH,KAAAA,CAAAA;IACzE,UAAA;AACEyB,aAAOmB,YAAW;IACpB;EACF;AACF;AAlRavE;AAAN,IAAMA,mBAAN;AAuRA,SAASwE,aAAavE,SAAiC;AAC5D,SAAO,IAAID,iBAAiBC,OAAAA;AAC9B;AAFgBuE;AAOT,IAAMC,SAAS,IAAIzE,iBAAAA;","names":["CapabilityError","Error","message","code","statusCode","name","ClientNotInitializedError","CapabilityNotFoundError","capabilityId","ActionNotFoundError","NetworkError","ExecutionError","DEFAULT_OPTIONS","baseURL","fetchOptions","autoInit","CapabilityClient","options","capabilities","Map","_initialized","initPromise","initialized","init","doInit","url","response","fetch","method","headers","ok","NetworkError","status","statusText","data","json","code","ExecutionError","message","clear","capability","set","id","error","CapabilityError","Error","String","ensureInitialized","call","capabilityId","action","params","has","CapabilityNotFoundError","body","JSON","stringify","errorResponse","errorCode","ActionNotFoundError","hasCapability","listCapabilities","Array","from","values","getCapability","get","callStream","reader","getReader","decoder","TextDecoder","buffer","done","value","read","decode","stream","lines","split","pop","line","startsWith","slice","parsed","parse","parseError","releaseLock","createClient","client"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lark-apaas/client-capability",
|
|
3
|
+
"version": "0.0.1-alpha.0",
|
|
4
|
+
"description": "Client SDK for calling capabilities",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"capability",
|
|
15
|
+
"client",
|
|
16
|
+
"sdk",
|
|
17
|
+
"typescript"
|
|
18
|
+
],
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"require": "./dist/index.cjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsup",
|
|
31
|
+
"dev": "tsup --watch",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"test": "vitest run",
|
|
34
|
+
"test:watch": "vitest",
|
|
35
|
+
"lint": "echo 'ESLint skipped'",
|
|
36
|
+
"prepublishOnly": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"tsup": "^8.0.0",
|
|
40
|
+
"typescript": "^5.0.0",
|
|
41
|
+
"vitest": "^1.6.0"
|
|
42
|
+
}
|
|
43
|
+
}
|