@lark-apaas/nestjs-capability 0.0.1-alpha.3 → 0.0.1-alpha.6
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/dist/index.cjs +307 -149
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +183 -70
- package/dist/index.d.ts +183 -70
- package/dist/index.js +306 -149
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,6 +7,22 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
7
7
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
+
// src/interfaces/error-codes.ts
|
|
11
|
+
var ErrorCodes = {
|
|
12
|
+
/** 成功 */
|
|
13
|
+
SUCCESS: "0",
|
|
14
|
+
/** 能力不存在 */
|
|
15
|
+
CAPABILITY_NOT_FOUND: "k_ec_cap_001",
|
|
16
|
+
/** 插件不存在 */
|
|
17
|
+
PLUGIN_NOT_FOUND: "k_ec_cap_002",
|
|
18
|
+
/** Action 不存在 */
|
|
19
|
+
ACTION_NOT_FOUND: "k_ec_cap_003",
|
|
20
|
+
/** 参数验证失败 */
|
|
21
|
+
PARAMS_VALIDATION_ERROR: "k_ec_cap_004",
|
|
22
|
+
/** 执行失败 */
|
|
23
|
+
EXECUTION_ERROR: "k_ec_cap_005"
|
|
24
|
+
};
|
|
25
|
+
|
|
10
26
|
// src/services/template-engine.service.ts
|
|
11
27
|
import { Injectable } from "@nestjs/common";
|
|
12
28
|
function _ts_decorate(decorators, target, key, desc) {
|
|
@@ -93,8 +109,8 @@ var PluginNotFoundError = class extends Error {
|
|
|
93
109
|
static {
|
|
94
110
|
__name(this, "PluginNotFoundError");
|
|
95
111
|
}
|
|
96
|
-
constructor(
|
|
97
|
-
super(`Plugin not found: ${
|
|
112
|
+
constructor(pluginKey) {
|
|
113
|
+
super(`Plugin not found: ${pluginKey}`);
|
|
98
114
|
this.name = "PluginNotFoundError";
|
|
99
115
|
}
|
|
100
116
|
};
|
|
@@ -102,8 +118,8 @@ var PluginLoadError = class extends Error {
|
|
|
102
118
|
static {
|
|
103
119
|
__name(this, "PluginLoadError");
|
|
104
120
|
}
|
|
105
|
-
constructor(
|
|
106
|
-
super(`Failed to load plugin ${
|
|
121
|
+
constructor(pluginKey, reason) {
|
|
122
|
+
super(`Failed to load plugin ${pluginKey}: ${reason}`);
|
|
107
123
|
this.name = "PluginLoadError";
|
|
108
124
|
}
|
|
109
125
|
};
|
|
@@ -113,41 +129,41 @@ var PluginLoaderService = class _PluginLoaderService {
|
|
|
113
129
|
}
|
|
114
130
|
logger = new Logger(_PluginLoaderService.name);
|
|
115
131
|
pluginInstances = /* @__PURE__ */ new Map();
|
|
116
|
-
async loadPlugin(
|
|
117
|
-
const cached = this.pluginInstances.get(
|
|
132
|
+
async loadPlugin(pluginKey) {
|
|
133
|
+
const cached = this.pluginInstances.get(pluginKey);
|
|
118
134
|
if (cached) {
|
|
119
|
-
this.logger.debug(`Using cached plugin instance: ${
|
|
135
|
+
this.logger.debug(`Using cached plugin instance: ${pluginKey}`);
|
|
120
136
|
return cached;
|
|
121
137
|
}
|
|
122
|
-
this.logger.log(`Loading plugin: ${
|
|
138
|
+
this.logger.log(`Loading plugin: ${pluginKey}`);
|
|
123
139
|
try {
|
|
124
|
-
const pluginPackage = (await import(
|
|
140
|
+
const pluginPackage = (await import(pluginKey)).default;
|
|
125
141
|
if (typeof pluginPackage.create !== "function") {
|
|
126
|
-
throw new PluginLoadError(
|
|
142
|
+
throw new PluginLoadError(pluginKey, "Plugin does not export create() function");
|
|
127
143
|
}
|
|
128
144
|
const instance = pluginPackage.create();
|
|
129
|
-
this.pluginInstances.set(
|
|
130
|
-
this.logger.log(`Plugin loaded successfully: ${
|
|
145
|
+
this.pluginInstances.set(pluginKey, instance);
|
|
146
|
+
this.logger.log(`Plugin loaded successfully: ${pluginKey}`);
|
|
131
147
|
return instance;
|
|
132
148
|
} catch (error) {
|
|
133
149
|
if (error.code === "MODULE_NOT_FOUND") {
|
|
134
|
-
throw new PluginNotFoundError(
|
|
150
|
+
throw new PluginNotFoundError(pluginKey);
|
|
135
151
|
}
|
|
136
|
-
throw new PluginLoadError(
|
|
152
|
+
throw new PluginLoadError(pluginKey, error instanceof Error ? error.message : String(error));
|
|
137
153
|
}
|
|
138
154
|
}
|
|
139
|
-
isPluginInstalled(
|
|
155
|
+
isPluginInstalled(pluginKey) {
|
|
140
156
|
try {
|
|
141
|
-
__require.resolve(
|
|
157
|
+
__require.resolve(pluginKey);
|
|
142
158
|
return true;
|
|
143
159
|
} catch {
|
|
144
160
|
return false;
|
|
145
161
|
}
|
|
146
162
|
}
|
|
147
|
-
clearCache(
|
|
148
|
-
if (
|
|
149
|
-
this.pluginInstances.delete(
|
|
150
|
-
this.logger.log(`Cleared cache for plugin: ${
|
|
163
|
+
clearCache(pluginKey) {
|
|
164
|
+
if (pluginKey) {
|
|
165
|
+
this.pluginInstances.delete(pluginKey);
|
|
166
|
+
this.logger.log(`Cleared cache for plugin: ${pluginKey}`);
|
|
151
167
|
} else {
|
|
152
168
|
this.pluginInstances.clear();
|
|
153
169
|
this.logger.log("Cleared all plugin caches");
|
|
@@ -193,8 +209,8 @@ var ActionNotFoundError = class extends Error {
|
|
|
193
209
|
static {
|
|
194
210
|
__name(this, "ActionNotFoundError");
|
|
195
211
|
}
|
|
196
|
-
constructor(
|
|
197
|
-
super(`Action '${actionName}' not found in plugin ${
|
|
212
|
+
constructor(pluginKey, actionName) {
|
|
213
|
+
super(`Action '${actionName}' not found in plugin ${pluginKey}`);
|
|
198
214
|
this.name = "ActionNotFoundError";
|
|
199
215
|
}
|
|
200
216
|
};
|
|
@@ -274,6 +290,9 @@ var CapabilityService = class _CapabilityService {
|
|
|
274
290
|
callStream: /* @__PURE__ */ __name((actionName, input, contextOverride) => {
|
|
275
291
|
return this.executeCallStream(config, actionName, input, contextOverride);
|
|
276
292
|
}, "callStream"),
|
|
293
|
+
callStreamWithEvents: /* @__PURE__ */ __name((actionName, input, contextOverride) => {
|
|
294
|
+
return this.executeCallStreamWithEvents(config, actionName, input, contextOverride);
|
|
295
|
+
}, "callStreamWithEvents"),
|
|
277
296
|
isStream: /* @__PURE__ */ __name(async (actionName) => {
|
|
278
297
|
return this.checkIsStream(config, actionName);
|
|
279
298
|
}, "isStream")
|
|
@@ -283,9 +302,9 @@ var CapabilityService = class _CapabilityService {
|
|
|
283
302
|
* 检查 action 是否为流式
|
|
284
303
|
*/
|
|
285
304
|
async checkIsStream(config, actionName) {
|
|
286
|
-
const pluginInstance = await this.pluginLoaderService.loadPlugin(config.
|
|
305
|
+
const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginKey);
|
|
287
306
|
if (!pluginInstance.hasAction(actionName)) {
|
|
288
|
-
throw new ActionNotFoundError(config.
|
|
307
|
+
throw new ActionNotFoundError(config.pluginKey, actionName);
|
|
289
308
|
}
|
|
290
309
|
return pluginInstance.isStreamAction?.(actionName) ?? false;
|
|
291
310
|
}
|
|
@@ -297,9 +316,9 @@ var CapabilityService = class _CapabilityService {
|
|
|
297
316
|
async executeCall(config, actionName, input, contextOverride) {
|
|
298
317
|
const startTime = Date.now();
|
|
299
318
|
try {
|
|
300
|
-
const pluginInstance = await this.pluginLoaderService.loadPlugin(config.
|
|
319
|
+
const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginKey);
|
|
301
320
|
if (!pluginInstance.hasAction(actionName)) {
|
|
302
|
-
throw new ActionNotFoundError(config.
|
|
321
|
+
throw new ActionNotFoundError(config.pluginKey, actionName);
|
|
303
322
|
}
|
|
304
323
|
const resolvedParams = this.templateEngineService.resolve(config.formValue, input);
|
|
305
324
|
const context = this.buildActionContext(contextOverride);
|
|
@@ -308,7 +327,7 @@ var CapabilityService = class _CapabilityService {
|
|
|
308
327
|
message: "Executing capability",
|
|
309
328
|
capabilityId: config.id,
|
|
310
329
|
action: actionName,
|
|
311
|
-
|
|
330
|
+
pluginKey: config.pluginKey,
|
|
312
331
|
isStream
|
|
313
332
|
});
|
|
314
333
|
let result;
|
|
@@ -347,9 +366,9 @@ var CapabilityService = class _CapabilityService {
|
|
|
347
366
|
async *executeCallStream(config, actionName, input, contextOverride) {
|
|
348
367
|
const startTime = Date.now();
|
|
349
368
|
try {
|
|
350
|
-
const pluginInstance = await this.pluginLoaderService.loadPlugin(config.
|
|
369
|
+
const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginKey);
|
|
351
370
|
if (!pluginInstance.hasAction(actionName)) {
|
|
352
|
-
throw new ActionNotFoundError(config.
|
|
371
|
+
throw new ActionNotFoundError(config.pluginKey, actionName);
|
|
353
372
|
}
|
|
354
373
|
const resolvedParams = this.templateEngineService.resolve(config.formValue, input);
|
|
355
374
|
const context = this.buildActionContext(contextOverride);
|
|
@@ -358,7 +377,7 @@ var CapabilityService = class _CapabilityService {
|
|
|
358
377
|
message: "Executing capability (stream)",
|
|
359
378
|
capabilityId: config.id,
|
|
360
379
|
action: actionName,
|
|
361
|
-
|
|
380
|
+
pluginKey: config.pluginKey,
|
|
362
381
|
isStream
|
|
363
382
|
});
|
|
364
383
|
if (isStream && pluginInstance.runStream) {
|
|
@@ -384,6 +403,79 @@ var CapabilityService = class _CapabilityService {
|
|
|
384
403
|
throw error;
|
|
385
404
|
}
|
|
386
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* 流式执行 capability,返回带事件协议的流
|
|
408
|
+
* - 优先使用 pluginInstance.runStreamWithEvents
|
|
409
|
+
* - 如果插件不支持,则包装 runStream/run 为 StreamEvent
|
|
410
|
+
*/
|
|
411
|
+
async *executeCallStreamWithEvents(config, actionName, input, contextOverride) {
|
|
412
|
+
const startTime = Date.now();
|
|
413
|
+
let chunkCount = 0;
|
|
414
|
+
try {
|
|
415
|
+
const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginKey);
|
|
416
|
+
if (!pluginInstance.hasAction(actionName)) {
|
|
417
|
+
throw new ActionNotFoundError(config.pluginKey, actionName);
|
|
418
|
+
}
|
|
419
|
+
const resolvedParams = this.templateEngineService.resolve(config.formValue, input);
|
|
420
|
+
const context = this.buildActionContext(contextOverride);
|
|
421
|
+
this.logger.log({
|
|
422
|
+
message: "Executing capability (streamWithEvents)",
|
|
423
|
+
capabilityId: config.id,
|
|
424
|
+
action: actionName,
|
|
425
|
+
pluginKey: config.pluginKey
|
|
426
|
+
});
|
|
427
|
+
if (pluginInstance.runStreamWithEvents) {
|
|
428
|
+
yield* pluginInstance.runStreamWithEvents(actionName, context, resolvedParams);
|
|
429
|
+
} else {
|
|
430
|
+
const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
|
|
431
|
+
if (isStream && pluginInstance.runStream) {
|
|
432
|
+
for await (const chunk of pluginInstance.runStream(actionName, context, resolvedParams)) {
|
|
433
|
+
chunkCount++;
|
|
434
|
+
yield {
|
|
435
|
+
type: "data",
|
|
436
|
+
data: chunk
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
} else {
|
|
440
|
+
const result = await pluginInstance.run(actionName, context, resolvedParams);
|
|
441
|
+
chunkCount = 1;
|
|
442
|
+
yield {
|
|
443
|
+
type: "data",
|
|
444
|
+
data: result
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
yield {
|
|
448
|
+
type: "done",
|
|
449
|
+
metadata: {
|
|
450
|
+
chunks: chunkCount,
|
|
451
|
+
duration: Date.now() - startTime
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
this.logger.log({
|
|
456
|
+
message: "Capability streamWithEvents completed",
|
|
457
|
+
capabilityId: config.id,
|
|
458
|
+
action: actionName,
|
|
459
|
+
duration: Date.now() - startTime,
|
|
460
|
+
chunks: chunkCount
|
|
461
|
+
});
|
|
462
|
+
} catch (error) {
|
|
463
|
+
this.logger.error({
|
|
464
|
+
message: "Capability streamWithEvents execution failed",
|
|
465
|
+
capabilityId: config.id,
|
|
466
|
+
action: actionName,
|
|
467
|
+
error: error instanceof Error ? error.message : String(error),
|
|
468
|
+
duration: Date.now() - startTime
|
|
469
|
+
});
|
|
470
|
+
yield {
|
|
471
|
+
type: "error",
|
|
472
|
+
error: {
|
|
473
|
+
code: "EXECUTION_ERROR",
|
|
474
|
+
message: error instanceof Error ? error.message : String(error)
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
}
|
|
387
479
|
buildActionContext(override) {
|
|
388
480
|
return {
|
|
389
481
|
logger: this.logger,
|
|
@@ -445,14 +537,15 @@ var DebugController = class {
|
|
|
445
537
|
list() {
|
|
446
538
|
const capabilities = this.capabilityService.listCapabilities();
|
|
447
539
|
return {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
540
|
+
status_code: ErrorCodes.SUCCESS,
|
|
541
|
+
data: {
|
|
542
|
+
capabilities: capabilities.map((c) => ({
|
|
543
|
+
id: c.id,
|
|
544
|
+
name: c.name,
|
|
545
|
+
pluginID: c.pluginKey,
|
|
546
|
+
pluginVersion: c.pluginVersion
|
|
547
|
+
}))
|
|
548
|
+
}
|
|
456
549
|
};
|
|
457
550
|
}
|
|
458
551
|
/**
|
|
@@ -477,16 +570,16 @@ var DebugController = class {
|
|
|
477
570
|
* 获取 action 名称
|
|
478
571
|
* 优先使用传入的 action,否则使用插件第一个 action
|
|
479
572
|
*/
|
|
480
|
-
async getActionName(
|
|
573
|
+
async getActionName(pluginKey, bodyAction) {
|
|
481
574
|
if (bodyAction) {
|
|
482
575
|
return bodyAction;
|
|
483
576
|
}
|
|
484
|
-
const pluginInstance = await this.pluginLoaderService.loadPlugin(
|
|
577
|
+
const pluginInstance = await this.pluginLoaderService.loadPlugin(pluginKey);
|
|
485
578
|
const actions = pluginInstance.listActions();
|
|
486
579
|
if (actions.length === 0) {
|
|
487
580
|
throw new HttpException({
|
|
488
581
|
code: 1,
|
|
489
|
-
message: `Plugin ${
|
|
582
|
+
message: `Plugin ${pluginKey} has no actions`,
|
|
490
583
|
error: "NO_ACTIONS"
|
|
491
584
|
}, HttpStatus.BAD_REQUEST);
|
|
492
585
|
}
|
|
@@ -496,68 +589,45 @@ var DebugController = class {
|
|
|
496
589
|
const startTime = Date.now();
|
|
497
590
|
const params = body.params ?? {};
|
|
498
591
|
const config = this.getCapabilityConfig(capabilityId, body.capability);
|
|
499
|
-
const action = await this.getActionName(config.
|
|
592
|
+
const action = await this.getActionName(config.pluginKey, body.action);
|
|
500
593
|
const resolvedParams = this.templateEngineService.resolve(config.formValue, params);
|
|
501
594
|
try {
|
|
502
595
|
const result = await this.capabilityService.loadWithConfig(config).call(action, params);
|
|
503
596
|
return {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
597
|
+
status_code: ErrorCodes.SUCCESS,
|
|
598
|
+
data: {
|
|
599
|
+
output: result,
|
|
600
|
+
debug: {
|
|
601
|
+
capabilityConfig: config,
|
|
602
|
+
resolvedParams,
|
|
603
|
+
duration: Date.now() - startTime,
|
|
604
|
+
pluginID: config.pluginKey,
|
|
605
|
+
action
|
|
606
|
+
}
|
|
513
607
|
}
|
|
514
608
|
};
|
|
515
609
|
} catch (error) {
|
|
516
|
-
const duration = Date.now() - startTime;
|
|
517
610
|
if (error instanceof CapabilityNotFoundError) {
|
|
518
611
|
throw new HttpException({
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
error: "CAPABILITY_NOT_FOUND",
|
|
522
|
-
debug: {
|
|
523
|
-
duration
|
|
524
|
-
}
|
|
612
|
+
status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
|
|
613
|
+
error_msg: `Capability not found: ${capabilityId}`
|
|
525
614
|
}, HttpStatus.NOT_FOUND);
|
|
526
615
|
}
|
|
527
616
|
if (error instanceof PluginNotFoundError) {
|
|
528
617
|
throw new HttpException({
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
error: "PLUGIN_NOT_FOUND",
|
|
532
|
-
debug: {
|
|
533
|
-
duration,
|
|
534
|
-
pluginID: config.pluginID,
|
|
535
|
-
action
|
|
536
|
-
}
|
|
618
|
+
status_code: ErrorCodes.PLUGIN_NOT_FOUND,
|
|
619
|
+
error_msg: `Plugin not found: ${config.pluginKey}`
|
|
537
620
|
}, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
538
621
|
}
|
|
539
622
|
if (error instanceof ActionNotFoundError) {
|
|
540
623
|
throw new HttpException({
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
error: "ACTION_NOT_FOUND",
|
|
544
|
-
debug: {
|
|
545
|
-
duration,
|
|
546
|
-
pluginID: config.pluginID,
|
|
547
|
-
action
|
|
548
|
-
}
|
|
624
|
+
status_code: ErrorCodes.ACTION_NOT_FOUND,
|
|
625
|
+
error_msg: `Action '${action}' not found in plugin ${config.pluginKey}`
|
|
549
626
|
}, HttpStatus.BAD_REQUEST);
|
|
550
627
|
}
|
|
551
628
|
throw new HttpException({
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
error: "EXECUTION_ERROR",
|
|
555
|
-
debug: {
|
|
556
|
-
duration,
|
|
557
|
-
pluginID: config.pluginID,
|
|
558
|
-
action,
|
|
559
|
-
resolvedParams
|
|
560
|
-
}
|
|
629
|
+
status_code: ErrorCodes.EXECUTION_ERROR,
|
|
630
|
+
error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
|
|
561
631
|
}, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
562
632
|
}
|
|
563
633
|
}
|
|
@@ -568,35 +638,78 @@ var DebugController = class {
|
|
|
568
638
|
res.setHeader("Connection", "keep-alive");
|
|
569
639
|
try {
|
|
570
640
|
const config = this.getCapabilityConfig(capabilityId, body.capability);
|
|
571
|
-
const action = await this.getActionName(config.
|
|
641
|
+
const action = await this.getActionName(config.pluginKey, body.action);
|
|
572
642
|
const capability = this.capabilityService.loadWithConfig(config);
|
|
573
|
-
const
|
|
574
|
-
for await (const
|
|
575
|
-
|
|
643
|
+
const eventStream = capability.callStreamWithEvents(action, params);
|
|
644
|
+
for await (const event of eventStream) {
|
|
645
|
+
switch (event.type) {
|
|
646
|
+
case "data": {
|
|
647
|
+
const response = {
|
|
648
|
+
status_code: ErrorCodes.SUCCESS,
|
|
649
|
+
data: {
|
|
650
|
+
type: "content",
|
|
651
|
+
delta: {
|
|
652
|
+
content: event.data
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
res.write(`data: ${JSON.stringify(response)}
|
|
657
|
+
|
|
658
|
+
`);
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
case "done": {
|
|
662
|
+
const response = {
|
|
663
|
+
status_code: ErrorCodes.SUCCESS,
|
|
664
|
+
data: {
|
|
665
|
+
type: "content",
|
|
666
|
+
delta: {
|
|
667
|
+
content: null
|
|
668
|
+
},
|
|
669
|
+
finished: true
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
res.write(`data: ${JSON.stringify(response)}
|
|
576
673
|
|
|
577
674
|
`);
|
|
675
|
+
res.end();
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
case "error": {
|
|
679
|
+
const response = {
|
|
680
|
+
status_code: ErrorCodes.SUCCESS,
|
|
681
|
+
data: {
|
|
682
|
+
type: "error",
|
|
683
|
+
error: {
|
|
684
|
+
code: 0,
|
|
685
|
+
message: event.error.message
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
res.write(`data: ${JSON.stringify(response)}
|
|
690
|
+
|
|
691
|
+
`);
|
|
692
|
+
res.end();
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
578
696
|
}
|
|
579
|
-
res.
|
|
697
|
+
res.end();
|
|
580
698
|
} catch (error) {
|
|
581
|
-
const
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
}
|
|
593
|
-
res.write(`data: ${JSON.stringify({
|
|
594
|
-
error: message,
|
|
595
|
-
code: errorCode
|
|
596
|
-
})}
|
|
699
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
700
|
+
const response = {
|
|
701
|
+
status_code: ErrorCodes.SUCCESS,
|
|
702
|
+
data: {
|
|
703
|
+
type: "error",
|
|
704
|
+
error: {
|
|
705
|
+
code: 0,
|
|
706
|
+
message: errorMsg
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
res.write(`data: ${JSON.stringify(response)}
|
|
597
711
|
|
|
598
712
|
`);
|
|
599
|
-
} finally {
|
|
600
713
|
res.end();
|
|
601
714
|
}
|
|
602
715
|
}
|
|
@@ -605,7 +718,7 @@ _ts_decorate4([
|
|
|
605
718
|
Get("list"),
|
|
606
719
|
_ts_metadata2("design:type", Function),
|
|
607
720
|
_ts_metadata2("design:paramtypes", []),
|
|
608
|
-
_ts_metadata2("design:returntype", typeof
|
|
721
|
+
_ts_metadata2("design:returntype", typeof SuccessResponse === "undefined" ? Object : SuccessResponse)
|
|
609
722
|
], DebugController.prototype, "list", null);
|
|
610
723
|
_ts_decorate4([
|
|
611
724
|
Post("debug/:capability_id"),
|
|
@@ -671,51 +784,48 @@ var WebhookController = class {
|
|
|
671
784
|
list() {
|
|
672
785
|
const capabilities = this.capabilityService.listCapabilities();
|
|
673
786
|
return {
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
}
|
|
787
|
+
status_code: ErrorCodes.SUCCESS,
|
|
788
|
+
data: {
|
|
789
|
+
capabilities: capabilities.map((c) => ({
|
|
790
|
+
id: c.id,
|
|
791
|
+
name: c.name,
|
|
792
|
+
pluginID: c.pluginKey,
|
|
793
|
+
pluginVersion: c.pluginVersion
|
|
794
|
+
}))
|
|
795
|
+
}
|
|
683
796
|
};
|
|
684
797
|
}
|
|
685
798
|
async execute(capabilityId, body) {
|
|
686
799
|
try {
|
|
687
800
|
const result = await this.capabilityService.load(capabilityId).call(body.action, body.params);
|
|
688
801
|
return {
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
802
|
+
status_code: ErrorCodes.SUCCESS,
|
|
803
|
+
data: {
|
|
804
|
+
output: result
|
|
805
|
+
}
|
|
692
806
|
};
|
|
693
807
|
} catch (error) {
|
|
694
808
|
if (error instanceof CapabilityNotFoundError) {
|
|
695
809
|
throw new HttpException2({
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
error: "CAPABILITY_NOT_FOUND"
|
|
810
|
+
status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
|
|
811
|
+
error_msg: `Capability not found: ${capabilityId}`
|
|
699
812
|
}, HttpStatus2.NOT_FOUND);
|
|
700
813
|
}
|
|
701
814
|
if (error instanceof PluginNotFoundError) {
|
|
702
815
|
throw new HttpException2({
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
error: "PLUGIN_NOT_FOUND"
|
|
816
|
+
status_code: ErrorCodes.PLUGIN_NOT_FOUND,
|
|
817
|
+
error_msg: `Plugin not found`
|
|
706
818
|
}, HttpStatus2.INTERNAL_SERVER_ERROR);
|
|
707
819
|
}
|
|
708
820
|
if (error instanceof ActionNotFoundError) {
|
|
709
821
|
throw new HttpException2({
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
error: "ACTION_NOT_FOUND"
|
|
822
|
+
status_code: ErrorCodes.ACTION_NOT_FOUND,
|
|
823
|
+
error_msg: `Action '${body.action}' not found`
|
|
713
824
|
}, HttpStatus2.BAD_REQUEST);
|
|
714
825
|
}
|
|
715
826
|
throw new HttpException2({
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
error: "EXECUTION_ERROR"
|
|
827
|
+
status_code: ErrorCodes.EXECUTION_ERROR,
|
|
828
|
+
error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
|
|
719
829
|
}, HttpStatus2.INTERNAL_SERVER_ERROR);
|
|
720
830
|
}
|
|
721
831
|
}
|
|
@@ -725,30 +835,76 @@ var WebhookController = class {
|
|
|
725
835
|
res.setHeader("Connection", "keep-alive");
|
|
726
836
|
try {
|
|
727
837
|
const capability = this.capabilityService.load(capabilityId);
|
|
728
|
-
const
|
|
729
|
-
for await (const
|
|
730
|
-
|
|
838
|
+
const eventStream = capability.callStreamWithEvents(body.action, body.params);
|
|
839
|
+
for await (const event of eventStream) {
|
|
840
|
+
switch (event.type) {
|
|
841
|
+
case "data": {
|
|
842
|
+
const response = {
|
|
843
|
+
status_code: ErrorCodes.SUCCESS,
|
|
844
|
+
data: {
|
|
845
|
+
type: "content",
|
|
846
|
+
delta: {
|
|
847
|
+
content: event.data
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
res.write(`data: ${JSON.stringify(response)}
|
|
731
852
|
|
|
732
853
|
`);
|
|
854
|
+
break;
|
|
855
|
+
}
|
|
856
|
+
case "done": {
|
|
857
|
+
const response = {
|
|
858
|
+
status_code: ErrorCodes.SUCCESS,
|
|
859
|
+
data: {
|
|
860
|
+
type: "content",
|
|
861
|
+
delta: {
|
|
862
|
+
content: null
|
|
863
|
+
},
|
|
864
|
+
finished: true
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
res.write(`data: ${JSON.stringify(response)}
|
|
868
|
+
|
|
869
|
+
`);
|
|
870
|
+
res.end();
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
case "error": {
|
|
874
|
+
const response = {
|
|
875
|
+
status_code: ErrorCodes.SUCCESS,
|
|
876
|
+
data: {
|
|
877
|
+
type: "error",
|
|
878
|
+
error: {
|
|
879
|
+
code: 0,
|
|
880
|
+
message: event.error.message
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
res.write(`data: ${JSON.stringify(response)}
|
|
885
|
+
|
|
886
|
+
`);
|
|
887
|
+
res.end();
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
733
891
|
}
|
|
734
|
-
res.
|
|
892
|
+
res.end();
|
|
735
893
|
} catch (error) {
|
|
736
|
-
const
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
})}
|
|
894
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
895
|
+
const response = {
|
|
896
|
+
status_code: ErrorCodes.SUCCESS,
|
|
897
|
+
data: {
|
|
898
|
+
type: "error",
|
|
899
|
+
error: {
|
|
900
|
+
code: 0,
|
|
901
|
+
message: errorMsg
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
res.write(`data: ${JSON.stringify(response)}
|
|
749
906
|
|
|
750
907
|
`);
|
|
751
|
-
} finally {
|
|
752
908
|
res.end();
|
|
753
909
|
}
|
|
754
910
|
}
|
|
@@ -757,7 +913,7 @@ _ts_decorate5([
|
|
|
757
913
|
Get2("list"),
|
|
758
914
|
_ts_metadata3("design:type", Function),
|
|
759
915
|
_ts_metadata3("design:paramtypes", []),
|
|
760
|
-
_ts_metadata3("design:returntype", typeof
|
|
916
|
+
_ts_metadata3("design:returntype", typeof SuccessResponse === "undefined" ? Object : SuccessResponse)
|
|
761
917
|
], WebhookController.prototype, "list", null);
|
|
762
918
|
_ts_decorate5([
|
|
763
919
|
Post2(":capability_id"),
|
|
@@ -871,6 +1027,7 @@ export {
|
|
|
871
1027
|
CapabilityNotFoundError,
|
|
872
1028
|
CapabilityService,
|
|
873
1029
|
DebugController,
|
|
1030
|
+
ErrorCodes,
|
|
874
1031
|
PluginLoadError,
|
|
875
1032
|
PluginLoaderService,
|
|
876
1033
|
PluginNotFoundError,
|