@tencent-ai/agent-sdk 0.0.1
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 +236 -0
- package/dist/agent-sdk.js +1 -0
- package/lib/common/application-utils.d.ts +5 -0
- package/lib/common/application-utils.d.ts.map +1 -0
- package/lib/common/application-utils.js +23 -0
- package/lib/common/application-utils.js.map +1 -0
- package/lib/common/component-registry.d.ts +10 -0
- package/lib/common/component-registry.d.ts.map +1 -0
- package/lib/common/component-registry.js +50 -0
- package/lib/common/component-registry.js.map +1 -0
- package/lib/common/index.d.ts +4 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +20 -0
- package/lib/common/index.js.map +1 -0
- package/lib/common/services.d.ts +19 -0
- package/lib/common/services.d.ts.map +1 -0
- package/lib/common/services.js +54 -0
- package/lib/common/services.js.map +1 -0
- package/lib/package.spec.d.ts +1 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +6 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +22 -0
package/README.md
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# Genie - Agent SDK
|
|
2
|
+
|
|
3
|
+
负责与 Agent 交互,提供 Agent SDK 接口。
|
|
4
|
+
|
|
5
|
+
## 使用
|
|
6
|
+
|
|
7
|
+
需要注意的是:
|
|
8
|
+
|
|
9
|
+
- **所有的组件注册都必须在获取组件(容器中的任何一个组件)之前完成,否则注册无效。**
|
|
10
|
+
- **此模块的所有功能都依赖于配置模块 `Product`, 因此在使用时,必须先通过 `registerProductProvider` 去提供配置,才可以正常使用。**
|
|
11
|
+
|
|
12
|
+
### Product
|
|
13
|
+
|
|
14
|
+
#### 注册 `ProductProvider`
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
18
|
+
|
|
19
|
+
class Component {
|
|
20
|
+
// ...
|
|
21
|
+
|
|
22
|
+
async init() {
|
|
23
|
+
await Services.registerProductProvider(CustomProductProvider);
|
|
24
|
+
logger.info('hello world');
|
|
25
|
+
}
|
|
26
|
+
// ...
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class CustomProductProvider {
|
|
30
|
+
async provide(){
|
|
31
|
+
return {
|
|
32
|
+
endpoint: 'your endpoint'
|
|
33
|
+
// ...
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### 获取配置管理组件
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
43
|
+
|
|
44
|
+
class Component {
|
|
45
|
+
// ...
|
|
46
|
+
|
|
47
|
+
async init() {
|
|
48
|
+
const productManager = await Services.getProductManager();
|
|
49
|
+
const productConfiguration = await productManager.waitConfiguration();
|
|
50
|
+
const logger = await Services.getLogger();
|
|
51
|
+
logger.info(productConfiguration)
|
|
52
|
+
}
|
|
53
|
+
// ...
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Http Request
|
|
58
|
+
|
|
59
|
+
在整个模块中会共用同一个 `axios` 实例用于 http 请求。
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
63
|
+
|
|
64
|
+
class Component {
|
|
65
|
+
// ...
|
|
66
|
+
|
|
67
|
+
async init() {
|
|
68
|
+
const restOperations = await Services.getRestOperations();
|
|
69
|
+
restOperations.get('path', {});
|
|
70
|
+
}
|
|
71
|
+
// ...
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
如果需要注册拦截器,方法与 `axios` 一致。
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
79
|
+
|
|
80
|
+
class Component {
|
|
81
|
+
// ...
|
|
82
|
+
|
|
83
|
+
async init() {
|
|
84
|
+
const restOperations = await Services.getRestOperations();
|
|
85
|
+
restOperations.interceptors.request.use(async config => {
|
|
86
|
+
if (!config.headers) {
|
|
87
|
+
config.headers = {};
|
|
88
|
+
}
|
|
89
|
+
config.headers['X-User-ID'] = 'user-id'
|
|
90
|
+
return config;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
// ...
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Logger
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
101
|
+
|
|
102
|
+
class Component {
|
|
103
|
+
// ...
|
|
104
|
+
|
|
105
|
+
async init() {
|
|
106
|
+
const logger = await Services.getLogger();
|
|
107
|
+
logger.info('hello world');
|
|
108
|
+
}
|
|
109
|
+
// ...
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
这里默认输出到 `console` 控制台,如果需要自定义输出,可以通过 `registerLogger` 注册自定义的 logger。
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
117
|
+
|
|
118
|
+
class Component {
|
|
119
|
+
// ...
|
|
120
|
+
async init() {
|
|
121
|
+
await Services.registerLogger(CustomLogger);
|
|
122
|
+
const logger = await Services.getLogger();
|
|
123
|
+
logger.info('hello world');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ...
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
class CustomLogger {
|
|
130
|
+
// ...
|
|
131
|
+
info(){
|
|
132
|
+
// do something
|
|
133
|
+
}
|
|
134
|
+
// ...
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Tracing
|
|
139
|
+
|
|
140
|
+
#### 初始化和使用
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
144
|
+
|
|
145
|
+
class Component {
|
|
146
|
+
// ...
|
|
147
|
+
|
|
148
|
+
async init() {
|
|
149
|
+
const traceService = await Services.getTraceService();
|
|
150
|
+
await traceService.init({serviceName: 'Your-Service-Name'});
|
|
151
|
+
const trace = traceService.startTrace('login');
|
|
152
|
+
trace.markAsSuccessful();
|
|
153
|
+
}
|
|
154
|
+
// ...
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### 注册公共信息到 sources
|
|
159
|
+
|
|
160
|
+
sourcesProvider 提供的信息会作为公共数据绑定到每一个 trace 链路中
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
164
|
+
|
|
165
|
+
class Component {
|
|
166
|
+
// ...
|
|
167
|
+
|
|
168
|
+
async init() {
|
|
169
|
+
await Services.registerResourcesProvider(CustomSourcesProvider);
|
|
170
|
+
const traceService = await Services.getTraceService();
|
|
171
|
+
await traceService.init({serviceName: 'Your-Service-Name'});
|
|
172
|
+
const trace = traceService.startTrace('login');
|
|
173
|
+
trace.markAsSuccessful();
|
|
174
|
+
}
|
|
175
|
+
// ...
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
class CustomSourcesProvider {
|
|
179
|
+
async provide() {
|
|
180
|
+
return {
|
|
181
|
+
username: '123',
|
|
182
|
+
userId: '456'
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### 事件上报
|
|
189
|
+
|
|
190
|
+
#### 使用
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
194
|
+
|
|
195
|
+
class Component {
|
|
196
|
+
// ...
|
|
197
|
+
|
|
198
|
+
async init() {
|
|
199
|
+
const eventService = await Services.getEventService();
|
|
200
|
+
eventService.Event('your-event-name', { payload: 1 })
|
|
201
|
+
}
|
|
202
|
+
// ...
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
#### 处理事件公共字段
|
|
207
|
+
|
|
208
|
+
模块内部会自动收集操作系统相关的信息,作为公共字段。
|
|
209
|
+
如果还需要携带如用户信息,编辑器信息等,需要通过注册处理器实现。
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
import { Services } from '@tencent-ai/agent-sdk';
|
|
213
|
+
|
|
214
|
+
class Component {
|
|
215
|
+
// ...
|
|
216
|
+
|
|
217
|
+
async init() {
|
|
218
|
+
await Services.registerEventProcessor(UserInfoEventProcessor);
|
|
219
|
+
const eventService = await Services.getEventService();
|
|
220
|
+
eventService.Event('your-event-name', { payload: 1 })
|
|
221
|
+
}
|
|
222
|
+
// ...
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
class UserInfoEventProcessor {
|
|
226
|
+
async process({
|
|
227
|
+
code: string;
|
|
228
|
+
event: EventPayload;
|
|
229
|
+
}) {
|
|
230
|
+
// 这样子所有的事件上报的时候都会带上以下用户信息字段
|
|
231
|
+
event.username = '张三';
|
|
232
|
+
event.userId = '123';
|
|
233
|
+
event.userNickname = '李四'
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
```
|