@dainprotocol/service-sdk 2.0.49 → 2.0.51
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/client/client.js.map +1 -1
- package/dist/lib/schemaStructure.d.ts +4 -0
- package/dist/lib/schemaStructure.js +135 -2
- package/dist/lib/schemaStructure.js.map +1 -1
- package/dist/service/hitl.d.ts +81 -0
- package/dist/service/hitl.js +219 -0
- package/dist/service/hitl.js.map +1 -0
- package/dist/service/index.d.ts +2 -1
- package/dist/service/index.js +4 -1
- package/dist/service/index.js.map +1 -1
- package/dist/service/nodeService.d.ts +3 -1
- package/dist/service/nodeService.js +8 -1
- package/dist/service/nodeService.js.map +1 -1
- package/dist/service/server.js +185 -40
- package/dist/service/server.js.map +1 -1
- package/dist/service/types.d.ts +29 -0
- package/dist/service/webhooks.d.ts +20 -0
- package/dist/service/webhooks.js +39 -7
- package/dist/service/webhooks.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Human-in-the-Loop (HITL) Action Handler
|
|
4
|
+
*
|
|
5
|
+
* Provides automatic routing of action callbacks to automation-core using the
|
|
6
|
+
* Standardized Action Protocol (SAP): scope:id:action[:payload]
|
|
7
|
+
*
|
|
8
|
+
* Example actions:
|
|
9
|
+
* - run:123:approve - Approve automation run
|
|
10
|
+
* - run:123:reject - Reject automation run
|
|
11
|
+
* - user:456:ban - Ban user
|
|
12
|
+
* - payment:789:confirm - Confirm payment
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.HITLHandler = void 0;
|
|
16
|
+
exports.routeActionToService = routeActionToService;
|
|
17
|
+
/**
|
|
18
|
+
* HITL Action Handler
|
|
19
|
+
* Routes actions to appropriate handlers based on scope
|
|
20
|
+
*/
|
|
21
|
+
class HITLHandler {
|
|
22
|
+
automationApiUrl;
|
|
23
|
+
automationApiKey;
|
|
24
|
+
constructor(config) {
|
|
25
|
+
this.automationApiUrl = config.automationApiUrl;
|
|
26
|
+
this.automationApiKey = config.automationApiKey;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Parse action data into components
|
|
30
|
+
* Format: scope:id:action[:payload]
|
|
31
|
+
*/
|
|
32
|
+
parseAction(actionData) {
|
|
33
|
+
const parts = actionData.split(':');
|
|
34
|
+
if (parts.length < 3) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
scope: parts[0],
|
|
39
|
+
id: parts[1],
|
|
40
|
+
action: parts[2],
|
|
41
|
+
payload: parts.length > 3 ? parts.slice(3).join(':') : undefined,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Check if action data is valid
|
|
46
|
+
*/
|
|
47
|
+
isValidAction(data) {
|
|
48
|
+
return this.parseAction(data) !== null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Route action to appropriate handler
|
|
52
|
+
*/
|
|
53
|
+
async handleAction(actionData, context) {
|
|
54
|
+
const parsed = this.parseAction(actionData);
|
|
55
|
+
if (!parsed) {
|
|
56
|
+
return {
|
|
57
|
+
success: false,
|
|
58
|
+
message: 'Invalid action format. Expected: scope:id:action[:payload]',
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const { scope, id, action, payload } = parsed;
|
|
62
|
+
console.log(`[HITL] Routing action: ${actionData}`, {
|
|
63
|
+
scope,
|
|
64
|
+
id,
|
|
65
|
+
action,
|
|
66
|
+
payload,
|
|
67
|
+
platform: context.platform,
|
|
68
|
+
user: context.user?.username || context.user?.id,
|
|
69
|
+
});
|
|
70
|
+
// Route based on scope
|
|
71
|
+
switch (scope) {
|
|
72
|
+
case 'run':
|
|
73
|
+
return await this.handleRunAction(id, action, payload, context);
|
|
74
|
+
case 'user':
|
|
75
|
+
return await this.handleUserAction(id, action, payload, context);
|
|
76
|
+
case 'payment':
|
|
77
|
+
return await this.handlePaymentAction(id, action, payload, context);
|
|
78
|
+
default:
|
|
79
|
+
return {
|
|
80
|
+
success: false,
|
|
81
|
+
message: `Unknown action scope: ${scope}`,
|
|
82
|
+
scope,
|
|
83
|
+
id,
|
|
84
|
+
action,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Handle run actions (automation approvals)
|
|
90
|
+
* Scope: run:{runId}:{action}
|
|
91
|
+
*/
|
|
92
|
+
async handleRunAction(runId, action, payload, context) {
|
|
93
|
+
try {
|
|
94
|
+
// Call automation-core API to submit input
|
|
95
|
+
const url = `${this.automationApiUrl}/api/runs/${runId}/input`;
|
|
96
|
+
const headers = {
|
|
97
|
+
'Content-Type': 'application/json',
|
|
98
|
+
};
|
|
99
|
+
if (this.automationApiKey) {
|
|
100
|
+
headers['Authorization'] = `Bearer ${this.automationApiKey}`;
|
|
101
|
+
}
|
|
102
|
+
const response = await fetch(url, {
|
|
103
|
+
method: 'POST',
|
|
104
|
+
headers,
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
decision: action,
|
|
107
|
+
user: context.user,
|
|
108
|
+
platform: context.platform,
|
|
109
|
+
metadata: context.metadata,
|
|
110
|
+
payload: payload ? JSON.parse(payload) : undefined,
|
|
111
|
+
}),
|
|
112
|
+
});
|
|
113
|
+
if (!response.ok) {
|
|
114
|
+
const error = await response.text();
|
|
115
|
+
console.error(`[HITL] Run action failed:`, error);
|
|
116
|
+
return {
|
|
117
|
+
success: false,
|
|
118
|
+
message: `Failed to ${action} run: ${error}`,
|
|
119
|
+
scope: 'run',
|
|
120
|
+
id: runId,
|
|
121
|
+
action,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const result = await response.json();
|
|
125
|
+
// Format action verb for message (approve -> approved, reject -> rejected)
|
|
126
|
+
const actionVerb = action.endsWith('e') ? `${action}d` : `${action}ed`;
|
|
127
|
+
return {
|
|
128
|
+
success: true,
|
|
129
|
+
message: `Run ${actionVerb} successfully`,
|
|
130
|
+
scope: 'run',
|
|
131
|
+
id: runId,
|
|
132
|
+
action,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
console.error(`[HITL] Run action error:`, error);
|
|
137
|
+
return {
|
|
138
|
+
success: false,
|
|
139
|
+
message: `Error processing run action: ${error.message}`,
|
|
140
|
+
scope: 'run',
|
|
141
|
+
id: runId,
|
|
142
|
+
action,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Handle user actions (moderation, etc.)
|
|
148
|
+
* Scope: user:{userId}:{action}
|
|
149
|
+
*/
|
|
150
|
+
async handleUserAction(userId, action, payload, context) {
|
|
151
|
+
// Placeholder for user action handling
|
|
152
|
+
// This would integrate with a user management API
|
|
153
|
+
console.log(`[HITL] User action not implemented:`, {
|
|
154
|
+
userId,
|
|
155
|
+
action,
|
|
156
|
+
payload,
|
|
157
|
+
});
|
|
158
|
+
return {
|
|
159
|
+
success: false,
|
|
160
|
+
message: 'User actions not yet implemented',
|
|
161
|
+
scope: 'user',
|
|
162
|
+
id: userId,
|
|
163
|
+
action,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Handle payment actions
|
|
168
|
+
* Scope: payment:{paymentId}:{action}
|
|
169
|
+
*/
|
|
170
|
+
async handlePaymentAction(paymentId, action, payload, context) {
|
|
171
|
+
// Placeholder for payment action handling
|
|
172
|
+
// This would integrate with a payment processing API
|
|
173
|
+
console.log(`[HITL] Payment action not implemented:`, {
|
|
174
|
+
paymentId,
|
|
175
|
+
action,
|
|
176
|
+
payload,
|
|
177
|
+
});
|
|
178
|
+
return {
|
|
179
|
+
success: false,
|
|
180
|
+
message: 'Payment actions not yet implemented',
|
|
181
|
+
scope: 'payment',
|
|
182
|
+
id: paymentId,
|
|
183
|
+
action,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.HITLHandler = HITLHandler;
|
|
188
|
+
/**
|
|
189
|
+
* Helper: Route action to HITL endpoint
|
|
190
|
+
*
|
|
191
|
+
* Use this in notification services to forward actions to the SDK's HITL webhook
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const result = await routeActionToService('run:123:approve', {
|
|
196
|
+
* user: { id: '456', username: 'john' },
|
|
197
|
+
* platform: 'telegram',
|
|
198
|
+
* });
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
async function routeActionToService(actionData, context, serviceUrl = 'http://localhost:3000') {
|
|
202
|
+
console.log(`[HITL] Routing action to service: ${actionData}`, {
|
|
203
|
+
context,
|
|
204
|
+
serviceUrl,
|
|
205
|
+
});
|
|
206
|
+
const response = await fetch(`${serviceUrl}/actions`, {
|
|
207
|
+
method: 'POST',
|
|
208
|
+
headers: { 'Content-Type': 'application/json' },
|
|
209
|
+
body: JSON.stringify({
|
|
210
|
+
action: actionData,
|
|
211
|
+
context,
|
|
212
|
+
}),
|
|
213
|
+
});
|
|
214
|
+
if (!response.ok) {
|
|
215
|
+
throw new Error(`HITL request failed: ${response.statusText}`);
|
|
216
|
+
}
|
|
217
|
+
return response.json();
|
|
218
|
+
}
|
|
219
|
+
//# sourceMappingURL=hitl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hitl.js","sourceRoot":"","sources":["../../src/service/hitl.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;AA+PH,oDAyBC;AApQD;;;GAGG;AACH,MAAa,WAAW;IACd,gBAAgB,CAAS;IACzB,gBAAgB,CAAU;IAElC,YAAY,MAAkB;QAC5B,IAAI,CAAC,gBAAgB,GAAG,MAAM,CAAC,gBAAgB,CAAC;QAChD,IAAI,CAAC,gBAAgB,GAAG,MAAM,CAAC,gBAAgB,CAAC;IAClD,CAAC;IAED;;;OAGG;IACH,WAAW,CAAC,UAA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
@@ -8,7 +8,8 @@ import { ProcessHandler, RedisProcessStore, MemoryProcessStore } from "./process
|
|
|
8
8
|
import { requireScope } from "./server";
|
|
9
9
|
import { hasScope, hasAllScopes, hasAnyScope } from "./auth";
|
|
10
10
|
import { WebhookTriggerRegistry, createWebhookTriggerRegistry, createSimpleTrigger, type WebhookTrigger, type WebhookTriggerDefinition, type WebhookProcessResult } from "./webhooks";
|
|
11
|
+
import { HITLHandler, routeActionToService, type HITLConfig, type HITLResult } from "./hitl";
|
|
11
12
|
export declare const defineDAINService: (config: DAINServiceConfig) => DAINService;
|
|
12
|
-
export { defineNodeService, defineDenoService, defineCloudflareService, createNextDainService, createTool, createService, createToolbox, CoreUtils, createOAuth2Tool, createAgent, ProcessHandler, RedisProcessStore, MemoryProcessStore, requireScope, hasScope, hasAllScopes, hasAnyScope, WebhookTriggerRegistry, createWebhookTriggerRegistry, createSimpleTrigger, type WebhookTrigger, type WebhookTriggerDefinition, type WebhookProcessResult, };
|
|
13
|
+
export { defineNodeService, defineDenoService, defineCloudflareService, createNextDainService, createTool, createService, createToolbox, CoreUtils, createOAuth2Tool, createAgent, ProcessHandler, RedisProcessStore, MemoryProcessStore, requireScope, hasScope, hasAllScopes, hasAnyScope, WebhookTriggerRegistry, createWebhookTriggerRegistry, createSimpleTrigger, type WebhookTrigger, type WebhookTriggerDefinition, type WebhookProcessResult, HITLHandler, routeActionToService, type HITLConfig, type HITLResult, };
|
|
13
14
|
export * from './types';
|
|
14
15
|
export * from './oauth2Store';
|
package/dist/service/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// File: src/service/index.ts
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.createSimpleTrigger = exports.createWebhookTriggerRegistry = exports.WebhookTriggerRegistry = exports.hasAnyScope = exports.hasAllScopes = exports.hasScope = exports.requireScope = exports.MemoryProcessStore = exports.RedisProcessStore = exports.ProcessHandler = exports.createAgent = exports.createOAuth2Tool = exports.CoreUtils = exports.createToolbox = exports.createService = exports.createTool = exports.createNextDainService = exports.defineCloudflareService = exports.defineDenoService = exports.defineNodeService = exports.defineDAINService = void 0;
|
|
4
|
+
exports.routeActionToService = exports.HITLHandler = exports.createSimpleTrigger = exports.createWebhookTriggerRegistry = exports.WebhookTriggerRegistry = exports.hasAnyScope = exports.hasAllScopes = exports.hasScope = exports.requireScope = exports.MemoryProcessStore = exports.RedisProcessStore = exports.ProcessHandler = exports.createAgent = exports.createOAuth2Tool = exports.CoreUtils = exports.createToolbox = exports.createService = exports.createTool = exports.createNextDainService = exports.defineCloudflareService = exports.defineDenoService = exports.defineNodeService = exports.defineDAINService = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const nodeService_1 = require("./nodeService");
|
|
7
7
|
Object.defineProperty(exports, "defineNodeService", { enumerable: true, get: function () { return nodeService_1.defineDAINService; } });
|
|
@@ -32,6 +32,9 @@ const webhooks_1 = require("./webhooks");
|
|
|
32
32
|
Object.defineProperty(exports, "WebhookTriggerRegistry", { enumerable: true, get: function () { return webhooks_1.WebhookTriggerRegistry; } });
|
|
33
33
|
Object.defineProperty(exports, "createWebhookTriggerRegistry", { enumerable: true, get: function () { return webhooks_1.createWebhookTriggerRegistry; } });
|
|
34
34
|
Object.defineProperty(exports, "createSimpleTrigger", { enumerable: true, get: function () { return webhooks_1.createSimpleTrigger; } });
|
|
35
|
+
const hitl_1 = require("./hitl");
|
|
36
|
+
Object.defineProperty(exports, "HITLHandler", { enumerable: true, get: function () { return hitl_1.HITLHandler; } });
|
|
37
|
+
Object.defineProperty(exports, "routeActionToService", { enumerable: true, get: function () { return hitl_1.routeActionToService; } });
|
|
35
38
|
const defineDAINService = (config) => {
|
|
36
39
|
throw new Error("This is a fallback implementation. Use the appropriate runtime-specific import.");
|
|
37
40
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/service/index.ts"],"names":[],"mappings":";AAAA,6BAA6B;;;;AAG7B,+CAAuE;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/service/index.ts"],"names":[],"mappings":";AAAA,6BAA6B;;;;AAG7B,+CAAuE;AA4BrE,kGA5B4B,+BAAiB,OA4B5B;AA3BnB,+CAAuE;AA4BrE,kGA5B4B,+BAAiB,OA4B5B;AA3BnB,2DAAmF;AA4BjF,wGA5B4B,qCAAuB,OA4B5B;AA3BzB,+CAAsD;AA4BpD,sGA5BO,mCAAqB,OA4BP;AA3BvB,iCAA4G;AA4B1G,2FA5BO,iBAAU,OA4BP;AACV,8FA7BmB,oBAAa,OA6BnB;AACb,8FA9BkC,oBAAa,OA8BlC;AACb,0FA/BiD,gBAAS,OA+BjD;AACT,iGAhC4D,uBAAgB,OAgC5D;AAChB,4FAjC8E,kBAAW,OAiC9E;AAhCb,2CAAoF;AAiClF,+FAjCO,0BAAc,OAiCP;AACd,kGAlCuB,6BAAiB,OAkCvB;AACjB,mGAnC0C,8BAAkB,OAmC1C;AAlCpB,qCAAwC;AAmCtC,6FAnCO,qBAAY,OAmCP;AAlCd,iCAA6D;AAmC3D,yFAnCO,eAAQ,OAmCP;AACR,6FApCiB,mBAAY,OAoCjB;AACZ,4FArC+B,kBAAW,OAqC/B;AApCb,yCAOoB;AA+BlB,uGArCA,iCAAsB,OAqCA;AACtB,6GArCA,uCAA4B,OAqCA;AAC5B,oGArCA,8BAAmB,OAqCA;AAhCrB,iCAKgB;AAgCd,4FApCA,kBAAW,OAoCA;AACX,qGApCA,2BAAoB,OAoCA;AA/Bf,MAAM,iBAAiB,GAAG,CAAC,MAAyB,EAAe,EAAE;IAC1E,MAAM,IAAI,KAAK,CAAC,iFAAiF,CAAC,CAAC;AACrG,CAAC,CAAC;AAFW,QAAA,iBAAiB,qBAE5B;AAkCF,sBAAsB;AACtB,kDAAwB;AAExB,wDAA8B"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { DAINServiceConfig, DAINService } from "./types";
|
|
2
2
|
import { createTool, createService, createToolbox, CoreUtils, createOAuth2Tool, createAgent } from "./core";
|
|
3
3
|
import { ProcessHandler, RedisProcessStore, MemoryProcessStore } from "./processes";
|
|
4
|
+
import { WebhookTriggerRegistry, createWebhookTriggerRegistry, createSimpleTrigger } from "./webhooks";
|
|
5
|
+
import { HITLHandler, routeActionToService } from "./hitl";
|
|
4
6
|
export declare function defineDAINService(config: DAINServiceConfig): DAINService;
|
|
5
|
-
export { createTool, createService, createToolbox, CoreUtils, createOAuth2Tool, createAgent, ProcessHandler, RedisProcessStore, MemoryProcessStore, };
|
|
7
|
+
export { createTool, createService, createToolbox, CoreUtils, createOAuth2Tool, createAgent, ProcessHandler, RedisProcessStore, MemoryProcessStore, WebhookTriggerRegistry, createWebhookTriggerRegistry, createSimpleTrigger, HITLHandler, routeActionToService, };
|
|
6
8
|
export * from "./types";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MemoryProcessStore = exports.RedisProcessStore = exports.ProcessHandler = exports.createAgent = exports.createOAuth2Tool = exports.CoreUtils = exports.createToolbox = exports.createService = exports.createTool = void 0;
|
|
3
|
+
exports.routeActionToService = exports.HITLHandler = exports.createSimpleTrigger = exports.createWebhookTriggerRegistry = exports.WebhookTriggerRegistry = exports.MemoryProcessStore = exports.RedisProcessStore = exports.ProcessHandler = exports.createAgent = exports.createOAuth2Tool = exports.CoreUtils = exports.createToolbox = exports.createService = exports.createTool = void 0;
|
|
4
4
|
exports.defineDAINService = defineDAINService;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const service_1 = require("./service");
|
|
@@ -15,6 +15,13 @@ const processes_1 = require("./processes");
|
|
|
15
15
|
Object.defineProperty(exports, "ProcessHandler", { enumerable: true, get: function () { return processes_1.ProcessHandler; } });
|
|
16
16
|
Object.defineProperty(exports, "RedisProcessStore", { enumerable: true, get: function () { return processes_1.RedisProcessStore; } });
|
|
17
17
|
Object.defineProperty(exports, "MemoryProcessStore", { enumerable: true, get: function () { return processes_1.MemoryProcessStore; } });
|
|
18
|
+
const webhooks_1 = require("./webhooks");
|
|
19
|
+
Object.defineProperty(exports, "WebhookTriggerRegistry", { enumerable: true, get: function () { return webhooks_1.WebhookTriggerRegistry; } });
|
|
20
|
+
Object.defineProperty(exports, "createWebhookTriggerRegistry", { enumerable: true, get: function () { return webhooks_1.createWebhookTriggerRegistry; } });
|
|
21
|
+
Object.defineProperty(exports, "createSimpleTrigger", { enumerable: true, get: function () { return webhooks_1.createSimpleTrigger; } });
|
|
22
|
+
const hitl_1 = require("./hitl");
|
|
23
|
+
Object.defineProperty(exports, "HITLHandler", { enumerable: true, get: function () { return hitl_1.HITLHandler; } });
|
|
24
|
+
Object.defineProperty(exports, "routeActionToService", { enumerable: true, get: function () { return hitl_1.routeActionToService; } });
|
|
18
25
|
function defineDAINService(config) {
|
|
19
26
|
const baseService = (0, service_1.createBaseDAINService)(config);
|
|
20
27
|
const startNode = async (options = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodeService.js","sourceRoot":"","sources":["../../src/service/nodeService.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"nodeService.js","sourceRoot":"","sources":["../../src/service/nodeService.ts"],"names":[],"mappings":";;;AAeA,8CAqDC;;AAnED,uCAAkD;AAClD,iCAOgB;AA8Dd,2FApEA,iBAAU,OAoEA;AACV,8FApEA,oBAAa,OAoEA;AACb,8FApEA,oBAAa,OAoEA;AACb,0FApEA,gBAAS,OAoEA;AACT,iGApEA,uBAAgB,OAoEA;AAChB,4FApEA,kBAAW,OAoEA;AAjEb,2CAAoF;AAkElF,+FAlEO,0BAAc,OAkEP;AACd,kGAnEuB,6BAAiB,OAmEvB;AACjB,mGApE0C,8BAAkB,OAoE1C;AAnEpB,yCAAuG;AAoErG,uGApEO,iCAAsB,OAoEP;AACtB,6GArE+B,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G,CAAC,+BAA+B,CAAC,CAAC;oBAC7C,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjB,CAAC;gBACD,GAAG,EAAE,OAAO;gBACZ,YAAY,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,SAAS;gBACrE,MAAM,EAAE,MAAM;aACf,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;gBACnE,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ;oBACrC,CAAC,CAAC,2GAA2G,IAAI,GAAG,CAAC,IAAI;oBACzH,CAAC,CAAC,EAAE,CAAC;gBACP,MAAM,IAAI,KAAK,CAAC,sBAAsB,IAAI,yGAAyG,UAAU,EAAE,CAAC,CAAC;YACnK,CAAC;YACD,MAAM,KAAK,CAAC,CAAC,wBAAwB;QACvC,CAAC;IACH,CAAC,CAAC;IACF,OAAO;QACL,GAAG,WAAW;QACd,SAAS;KACK,CAAC;AACnB,CAAC;AAmBD,sBAAsB;AACtB,kDAAwB"}
|
package/dist/service/server.js
CHANGED
|
@@ -20,6 +20,7 @@ const sha256_1 = require("@noble/hashes/sha256");
|
|
|
20
20
|
const utils_1 = require("@noble/hashes/utils");
|
|
21
21
|
const auth_2 = require("./auth");
|
|
22
22
|
const core_1 = require("./core");
|
|
23
|
+
const hitl_1 = require("./hitl");
|
|
23
24
|
const VERBOSE_SERVICE_LOGS = process.env.DAIN_VERBOSE_SERVICE_LOGS === "true";
|
|
24
25
|
const debugLog = (...args) => {
|
|
25
26
|
if (VERBOSE_SERVICE_LOGS) {
|
|
@@ -185,14 +186,22 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
185
186
|
c.res.headers.set("X-DAIN-ADDRESS", config.identity.publicKey);
|
|
186
187
|
c.res.headers.set("X-DAIN-TIMESTAMP", timestamp);
|
|
187
188
|
});
|
|
189
|
+
// Get all webhook paths for auth bypass
|
|
190
|
+
const webhookPaths = config.webhookTriggers?.getWebhookPaths() || [];
|
|
191
|
+
const hitlPath = config.hitl?.enabled ? (config.hitl.webhookPath || '/actions') : null;
|
|
188
192
|
// Dual Authentication: JWT (users) or API Key (services)
|
|
189
193
|
app.use("*", async (c, next) => {
|
|
190
194
|
debugLog(`[Auth Middleware] Path: ${c.req.path}, Method: ${c.req.method}`);
|
|
195
|
+
// Check if path is a webhook path (auth bypass)
|
|
196
|
+
const isWebhookPath = webhookPaths.some(path => c.req.path === path);
|
|
197
|
+
const isHITLPath = hitlPath && c.req.path === hitlPath;
|
|
191
198
|
if (c.req.path.startsWith("/oauth2/callback/") ||
|
|
192
199
|
c.req.path.startsWith("/addons") ||
|
|
193
200
|
c.req.path.startsWith("/getAllToolsAsJsonSchema") ||
|
|
194
201
|
c.req.path.startsWith("/metadata") ||
|
|
195
|
-
c.req.path.startsWith("/ping")
|
|
202
|
+
c.req.path.startsWith("/ping") ||
|
|
203
|
+
isWebhookPath ||
|
|
204
|
+
isHITLPath) {
|
|
196
205
|
debugLog(`[Auth Middleware] Path ${c.req.path} - SKIPPING auth`);
|
|
197
206
|
await next();
|
|
198
207
|
return;
|
|
@@ -305,16 +314,23 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
305
314
|
app.get("/metadata", (c) => c.json(metadata));
|
|
306
315
|
// Tools list endpoint
|
|
307
316
|
app.get("/tools", (c) => {
|
|
308
|
-
const toolInfo = tools.map((tool) =>
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
317
|
+
const toolInfo = tools.map((tool) => {
|
|
318
|
+
const inputSchema = (0, schemaStructure_1.zodToJsonSchema)(tool.input);
|
|
319
|
+
const outputSchema = (0, schemaStructure_1.zodToJsonSchema)(tool.output);
|
|
320
|
+
console.log(`[Service SDK Server] Tool: ${tool.id}`);
|
|
321
|
+
console.log(`[Service SDK Server] Input Schema:`, JSON.stringify(inputSchema, null, 2));
|
|
322
|
+
return {
|
|
323
|
+
id: tool.id,
|
|
324
|
+
name: tool.name,
|
|
325
|
+
description: tool.description,
|
|
326
|
+
pricing: tool.pricing,
|
|
327
|
+
inputSchema,
|
|
328
|
+
outputSchema,
|
|
329
|
+
interface: tool.interface,
|
|
330
|
+
suggestConfirmation: tool.suggestConfirmation,
|
|
331
|
+
};
|
|
332
|
+
});
|
|
333
|
+
console.log(`[Service SDK Server] Returning ${toolInfo.length} tools`);
|
|
318
334
|
return c.json(toolInfo);
|
|
319
335
|
});
|
|
320
336
|
// POST version of the basic contexts listing
|
|
@@ -694,8 +710,8 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
694
710
|
prompt: agent.prompt,
|
|
695
711
|
resolveCondition: agent.resolveCondition,
|
|
696
712
|
serviceConnections: agent.serviceConnections || [process.env.BASE_URL || 'http://localhost:3000'],
|
|
697
|
-
inputSchema: (0, schemaStructure_1.
|
|
698
|
-
outputSchema: (0, schemaStructure_1.
|
|
713
|
+
inputSchema: (0, schemaStructure_1.zodToJsonSchema)(agent.input),
|
|
714
|
+
outputSchema: (0, schemaStructure_1.zodToJsonSchema)(agent.output),
|
|
699
715
|
}));
|
|
700
716
|
return c.json(agentInfo);
|
|
701
717
|
});
|
|
@@ -719,8 +735,8 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
719
735
|
prompt: agent.prompt,
|
|
720
736
|
resolveCondition: agent.resolveCondition,
|
|
721
737
|
serviceConnections: agent.serviceConnections || [process.env.BASE_URL || 'http://localhost:3000'],
|
|
722
|
-
inputSchema: (0, schemaStructure_1.
|
|
723
|
-
outputSchema: (0, schemaStructure_1.
|
|
738
|
+
inputSchema: (0, schemaStructure_1.zodToJsonSchema)(agent.input),
|
|
739
|
+
outputSchema: (0, schemaStructure_1.zodToJsonSchema)(agent.output),
|
|
724
740
|
}));
|
|
725
741
|
// Process plugins for the response
|
|
726
742
|
const processedResponse = await processPluginsForResponse(agentsInfo, body, { extraData: { plugins: processedPluginData.plugins } });
|
|
@@ -737,8 +753,8 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
737
753
|
prompt: agent.prompt,
|
|
738
754
|
resolveCondition: agent.resolveCondition,
|
|
739
755
|
serviceConnections: agent.serviceConnections || [process.env.BASE_URL || 'http://localhost:3000'],
|
|
740
|
-
inputSchema: (0, schemaStructure_1.
|
|
741
|
-
outputSchema: (0, schemaStructure_1.
|
|
756
|
+
inputSchema: (0, schemaStructure_1.zodToJsonSchema)(agent.input),
|
|
757
|
+
outputSchema: (0, schemaStructure_1.zodToJsonSchema)(agent.output),
|
|
742
758
|
};
|
|
743
759
|
return c.json(agentDetails);
|
|
744
760
|
}
|
|
@@ -768,8 +784,8 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
768
784
|
prompt: agent.prompt,
|
|
769
785
|
resolveCondition: agent.resolveCondition,
|
|
770
786
|
serviceConnections: agent.serviceConnections || [process.env.BASE_URL || 'http://localhost:3000'],
|
|
771
|
-
inputSchema: (0, schemaStructure_1.
|
|
772
|
-
outputSchema: (0, schemaStructure_1.
|
|
787
|
+
inputSchema: (0, schemaStructure_1.zodToJsonSchema)(agent.input),
|
|
788
|
+
outputSchema: (0, schemaStructure_1.zodToJsonSchema)(agent.output),
|
|
773
789
|
};
|
|
774
790
|
// Process plugins for the response
|
|
775
791
|
const processedResponse = await processPluginsForResponse(agentDetails, body, { extraData: { plugins: processedPluginData.plugins } });
|
|
@@ -781,24 +797,21 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
781
797
|
});
|
|
782
798
|
app.get("/getAllToolsAsJsonSchema", (c) => {
|
|
783
799
|
const toolInfo = tools.map((tool) => {
|
|
784
|
-
const inputSchema = (0, schemaStructure_1.
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
fs.appendFileSync('/tmp/dain-sdk-schemas.log', `\n\n=== Tool: ${tool.id} ===\n` +
|
|
789
|
-
JSON.stringify(inputSchema, null, 2) + '\n');
|
|
790
|
-
}
|
|
791
|
-
catch (e) { }
|
|
800
|
+
const inputSchema = (0, schemaStructure_1.zodToJsonSchema)(tool.input);
|
|
801
|
+
const outputSchema = (0, schemaStructure_1.zodToJsonSchema)(tool.output);
|
|
802
|
+
console.log(`[getAllToolsAsJsonSchema GET] Tool: ${tool.id}`);
|
|
803
|
+
console.log(`[getAllToolsAsJsonSchema GET] Input Schema:`, JSON.stringify(inputSchema, null, 2));
|
|
792
804
|
return {
|
|
793
805
|
id: tool.id,
|
|
794
806
|
name: tool.name,
|
|
795
807
|
description: tool.description,
|
|
796
|
-
inputSchema
|
|
797
|
-
outputSchema
|
|
808
|
+
inputSchema,
|
|
809
|
+
outputSchema,
|
|
798
810
|
interface: tool.interface,
|
|
799
811
|
suggestConfirmation: tool.suggestConfirmation,
|
|
800
812
|
};
|
|
801
813
|
});
|
|
814
|
+
console.log(`[getAllToolsAsJsonSchema GET] Returning ${toolInfo.length} tools`);
|
|
802
815
|
return c.json({
|
|
803
816
|
tools: toolInfo,
|
|
804
817
|
reccomendedPrompts: toolboxes.map((toolbox) => toolbox.recommendedPrompt),
|
|
@@ -817,19 +830,52 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
817
830
|
}
|
|
818
831
|
// Process plugins for the request
|
|
819
832
|
const processedPluginData = await processPluginsForRequest(body, agentInfo);
|
|
820
|
-
const toolInfo = tools.map((tool) =>
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
833
|
+
const toolInfo = tools.map((tool) => {
|
|
834
|
+
let inputSchema = (0, schemaStructure_1.zodToJsonSchema)(tool.input);
|
|
835
|
+
let outputSchema = (0, schemaStructure_1.zodToJsonSchema)(tool.output);
|
|
836
|
+
// Ensure inputSchema has type: 'object' at root level (butterfly-web compatibility)
|
|
837
|
+
if (!inputSchema.type) {
|
|
838
|
+
inputSchema = { ...inputSchema, type: 'object' };
|
|
839
|
+
}
|
|
840
|
+
if (!outputSchema.type) {
|
|
841
|
+
outputSchema = { ...outputSchema, type: 'object' };
|
|
842
|
+
}
|
|
843
|
+
// Fix empty object schemas (from z.any()) to have type
|
|
844
|
+
const fixEmptySchemas = (schema) => {
|
|
845
|
+
if (schema && typeof schema === 'object') {
|
|
846
|
+
if (schema.properties) {
|
|
847
|
+
Object.keys(schema.properties).forEach(key => {
|
|
848
|
+
const prop = schema.properties[key];
|
|
849
|
+
if (prop && typeof prop === 'object' && Object.keys(prop).length === 0) {
|
|
850
|
+
schema.properties[key] = { type: 'object' };
|
|
851
|
+
}
|
|
852
|
+
fixEmptySchemas(prop);
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
if (schema.items) {
|
|
856
|
+
fixEmptySchemas(schema.items);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
fixEmptySchemas(inputSchema);
|
|
861
|
+
fixEmptySchemas(outputSchema);
|
|
862
|
+
console.log(`[getAllToolsAsJsonSchema POST] Tool: ${tool.id}`);
|
|
863
|
+
console.log(`[getAllToolsAsJsonSchema POST] Input Schema:`, JSON.stringify(inputSchema, null, 2));
|
|
864
|
+
return {
|
|
865
|
+
id: tool.id,
|
|
866
|
+
name: tool.name,
|
|
867
|
+
description: tool.description,
|
|
868
|
+
inputSchema,
|
|
869
|
+
outputSchema,
|
|
870
|
+
interface: tool.interface,
|
|
871
|
+
suggestConfirmation: tool.suggestConfirmation,
|
|
872
|
+
};
|
|
873
|
+
});
|
|
829
874
|
const response = {
|
|
830
875
|
tools: toolInfo,
|
|
831
876
|
reccomendedPrompts: toolboxes.map((toolbox) => toolbox.recommendedPrompt),
|
|
832
877
|
};
|
|
878
|
+
console.log(`[getAllToolsAsJsonSchema POST] Returning ${toolInfo.length} tools`);
|
|
833
879
|
// Process plugins for the response
|
|
834
880
|
const processedResponse = await processPluginsForResponse(response, body, { extraData: { plugins: processedPluginData.plugins } });
|
|
835
881
|
return c.json(processedResponse);
|
|
@@ -839,6 +885,100 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
839
885
|
const triggers = config.webhookTriggers?.getTriggerDefinitions() || [];
|
|
840
886
|
return c.json({ triggers });
|
|
841
887
|
});
|
|
888
|
+
// Auto-register webhook endpoints from webhookTriggers
|
|
889
|
+
if (config.webhookTriggers) {
|
|
890
|
+
const webhookPaths = config.webhookTriggers.getWebhookPaths();
|
|
891
|
+
for (const path of webhookPaths) {
|
|
892
|
+
const triggersForPath = config.webhookTriggers.getTriggersForPath(path);
|
|
893
|
+
debugLog(`[Webhook] Registering endpoint: POST ${path} (${triggersForPath.length} trigger(s))`);
|
|
894
|
+
app.post(path, async (c) => {
|
|
895
|
+
try {
|
|
896
|
+
// Get request body
|
|
897
|
+
const payload = await c.req.json();
|
|
898
|
+
console.log(`[Webhook] Received POST ${path}`);
|
|
899
|
+
debugLog(`[Webhook] Received request at ${path}`, payload);
|
|
900
|
+
// Process webhook through all triggers for this path
|
|
901
|
+
const result = await config.webhookTriggers.processWebhook(payload);
|
|
902
|
+
if (result) {
|
|
903
|
+
debugLog(`[Webhook] Trigger matched: ${result.triggerId}`);
|
|
904
|
+
// Return success with trigger info
|
|
905
|
+
return c.json({
|
|
906
|
+
success: true,
|
|
907
|
+
triggerId: result.triggerId,
|
|
908
|
+
triggerName: result.triggerName,
|
|
909
|
+
data: result.data
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
else {
|
|
913
|
+
debugLog(`[Webhook] No trigger matched for payload`);
|
|
914
|
+
// No trigger matched - return 200 OK but with success: false
|
|
915
|
+
return c.json({
|
|
916
|
+
success: false,
|
|
917
|
+
message: 'No trigger matched'
|
|
918
|
+
}, 200);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
catch (error) {
|
|
922
|
+
console.error(`[Webhook] Error processing webhook at ${path}:`, error);
|
|
923
|
+
// Return error response
|
|
924
|
+
throw new http_exception_1.HTTPException(500, {
|
|
925
|
+
message: `Webhook processing failed: ${error.message}`
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
console.log(`[Webhook] Registered ${webhookPaths.length} webhook endpoint(s): ${webhookPaths.join(', ')}`);
|
|
931
|
+
}
|
|
932
|
+
// Auto-register HITL action webhook
|
|
933
|
+
if (config.hitl?.enabled) {
|
|
934
|
+
const hitlPath = config.hitl.webhookPath || '/actions';
|
|
935
|
+
const automationApiUrl = config.hitl.automationApiUrl ||
|
|
936
|
+
process.env.AUTOMATION_API_URL ||
|
|
937
|
+
'http://localhost:3000';
|
|
938
|
+
const automationApiKey = config.hitl.automationApiKey || process.env.AUTOMATION_API_KEY;
|
|
939
|
+
if (!automationApiUrl) {
|
|
940
|
+
console.warn('[HITL] Enabled but missing automationApiUrl - HITL webhook will not work correctly');
|
|
941
|
+
}
|
|
942
|
+
const hitlHandler = new hitl_1.HITLHandler({
|
|
943
|
+
automationApiUrl,
|
|
944
|
+
automationApiKey,
|
|
945
|
+
});
|
|
946
|
+
debugLog(`[HITL] Registering action webhook: POST ${hitlPath}`);
|
|
947
|
+
app.post(hitlPath, async (c) => {
|
|
948
|
+
try {
|
|
949
|
+
const body = await c.req.json();
|
|
950
|
+
const { action, context } = body;
|
|
951
|
+
if (!action || typeof action !== 'string') {
|
|
952
|
+
throw new http_exception_1.HTTPException(400, {
|
|
953
|
+
message: 'Missing or invalid "action" field in request body',
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
if (!context || typeof context !== 'object') {
|
|
957
|
+
throw new http_exception_1.HTTPException(400, {
|
|
958
|
+
message: 'Missing or invalid "context" field in request body',
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
if (!hitlHandler.isValidAction(action)) {
|
|
962
|
+
throw new http_exception_1.HTTPException(400, {
|
|
963
|
+
message: 'Invalid action format. Expected: scope:id:action[:payload]',
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
const result = await hitlHandler.handleAction(action, context);
|
|
967
|
+
return c.json(result);
|
|
968
|
+
}
|
|
969
|
+
catch (error) {
|
|
970
|
+
if (error instanceof http_exception_1.HTTPException) {
|
|
971
|
+
throw error;
|
|
972
|
+
}
|
|
973
|
+
console.error('[HITL] Error processing action:', error);
|
|
974
|
+
throw new http_exception_1.HTTPException(500, {
|
|
975
|
+
message: `Action processing failed: ${error.message}`,
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
});
|
|
979
|
+
console.log(`[HITL] Action webhook registered at POST ${hitlPath}`);
|
|
980
|
+
console.log(`[HITL] Routing actions to: ${automationApiUrl}`);
|
|
981
|
+
}
|
|
842
982
|
// Detailed info for a specific tool
|
|
843
983
|
app.get("/tools/:toolId", (c) => {
|
|
844
984
|
const tool = tools.find((t) => t.id === c.req.param("toolId"));
|
|
@@ -1540,8 +1680,13 @@ function setupHttpServer(config, tools, services, toolboxes, metadata, privateKe
|
|
|
1540
1680
|
catch (error) {
|
|
1541
1681
|
console.error(`[Tool ${tool.id} +Context] ========== ERROR ==========`);
|
|
1542
1682
|
console.error(`[Tool ${tool.id} +Context] Error:`, error);
|
|
1543
|
-
console.error(`[Tool ${tool.id} +Context]
|
|
1544
|
-
|
|
1683
|
+
console.error(`[Tool ${tool.id} +Context] Error message:`, error?.message);
|
|
1684
|
+
console.error(`[Tool ${tool.id} +Context] Error response:`, error?.response?.data);
|
|
1685
|
+
console.error(`[Tool ${tool.id} +Context] Stack:`, error?.stack);
|
|
1686
|
+
throw new http_exception_1.HTTPException(500, {
|
|
1687
|
+
message: error?.message || "Internal server error",
|
|
1688
|
+
cause: error
|
|
1689
|
+
});
|
|
1545
1690
|
}
|
|
1546
1691
|
});
|
|
1547
1692
|
});
|