lua-cli 2.5.8 → 3.0.0-alpha.4
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/api/job.api.service.d.ts +219 -0
- package/dist/api/job.api.service.js +216 -0
- package/dist/api/lazy-instances.d.ts +24 -0
- package/dist/api/lazy-instances.js +48 -0
- package/dist/api/postprocessor.api.service.d.ts +158 -0
- package/dist/api/postprocessor.api.service.js +111 -0
- package/dist/api/preprocessor.api.service.d.ts +158 -0
- package/dist/api/preprocessor.api.service.js +111 -0
- package/dist/api/user.data.api.service.d.ts +13 -0
- package/dist/api/user.data.api.service.js +20 -0
- package/dist/api/webhook.api.service.d.ts +151 -0
- package/dist/api/webhook.api.service.js +134 -0
- package/dist/api-exports.d.ts +176 -41
- package/dist/api-exports.js +195 -21
- package/dist/cli/command-definitions.js +75 -5
- package/dist/commands/chat.js +32 -5
- package/dist/commands/compile.js +140 -7
- package/dist/commands/dev.js +23 -2
- package/dist/commands/index.d.ts +4 -0
- package/dist/commands/index.js +4 -0
- package/dist/commands/init.js +53 -7
- package/dist/commands/jobs.d.ts +20 -0
- package/dist/commands/jobs.js +533 -0
- package/dist/commands/logs.js +2 -5
- package/dist/commands/postprocessors.d.ts +8 -0
- package/dist/commands/postprocessors.js +431 -0
- package/dist/commands/preprocessors.d.ts +8 -0
- package/dist/commands/preprocessors.js +431 -0
- package/dist/commands/push.js +686 -5
- package/dist/commands/test.d.ts +9 -18
- package/dist/commands/test.js +574 -82
- package/dist/commands/webhooks.d.ts +18 -0
- package/dist/commands/webhooks.js +424 -0
- package/dist/common/job.instance.d.ts +80 -0
- package/dist/common/job.instance.js +116 -0
- package/dist/common/user.instance.d.ts +1 -0
- package/dist/common/user.instance.js +9 -0
- package/dist/config/constants.d.ts +4 -3
- package/dist/config/constants.js +10 -8
- package/dist/interfaces/agent.d.ts +2 -1
- package/dist/interfaces/chat.d.ts +52 -1
- package/dist/interfaces/index.d.ts +10 -0
- package/dist/interfaces/index.js +7 -0
- package/dist/interfaces/jobs.d.ts +193 -0
- package/dist/interfaces/jobs.js +5 -0
- package/dist/interfaces/postprocessors.d.ts +35 -0
- package/dist/interfaces/postprocessors.js +4 -0
- package/dist/interfaces/preprocessors.d.ts +35 -0
- package/dist/interfaces/preprocessors.js +4 -0
- package/dist/interfaces/webhooks.d.ts +104 -0
- package/dist/interfaces/webhooks.js +5 -0
- package/dist/types/api-contracts.d.ts +5 -0
- package/dist/types/compile.types.d.ts +49 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.js +1 -1
- package/dist/types/skill.d.ts +521 -0
- package/dist/types/skill.js +471 -0
- package/dist/utils/agent-management.d.ts +25 -0
- package/dist/utils/agent-management.js +67 -0
- package/dist/utils/bundling.d.ts +44 -5
- package/dist/utils/bundling.js +723 -23
- package/dist/utils/compile.d.ts +63 -0
- package/dist/utils/compile.js +712 -36
- package/dist/utils/deployment.d.ts +2 -1
- package/dist/utils/deployment.js +16 -2
- package/dist/utils/dev-api.d.ts +42 -2
- package/dist/utils/dev-api.js +177 -4
- package/dist/utils/dev-server.d.ts +1 -1
- package/dist/utils/dev-server.js +4 -4
- package/dist/utils/dynamic-job-bundler.d.ts +17 -0
- package/dist/utils/dynamic-job-bundler.js +143 -0
- package/dist/utils/init-agent.d.ts +3 -1
- package/dist/utils/init-agent.js +6 -4
- package/dist/utils/init-prompts.d.ts +2 -1
- package/dist/utils/init-prompts.js +14 -9
- package/dist/utils/job-management.d.ts +24 -0
- package/dist/utils/job-management.js +264 -0
- package/dist/utils/postprocessor-management.d.ts +9 -0
- package/dist/utils/postprocessor-management.js +118 -0
- package/dist/utils/pre-bundle-jobs.d.ts +26 -0
- package/dist/utils/pre-bundle-jobs.js +176 -0
- package/dist/utils/preprocessor-management.d.ts +9 -0
- package/dist/utils/preprocessor-management.js +118 -0
- package/dist/utils/sandbox-storage.d.ts +48 -0
- package/dist/utils/sandbox-storage.js +114 -0
- package/dist/utils/sandbox.d.ts +61 -1
- package/dist/utils/sandbox.js +299 -72
- package/dist/utils/tool-detection.d.ts +3 -2
- package/dist/utils/tool-detection.js +18 -4
- package/dist/utils/webhook-management.d.ts +24 -0
- package/dist/utils/webhook-management.js +256 -0
- package/package.json +1 -1
- package/template/README.md +30 -2
- package/template/lua.skill.yaml +47 -0
- package/template/package-lock.json +10505 -0
- package/template/package.json +2 -1
- package/template/src/index.ts +103 -2
- package/template/src/jobs/AbandonedBasketProcessorJob.ts +139 -0
- package/template/src/jobs/DailyCleanupJob.ts +100 -0
- package/template/src/jobs/DataMigrationJob.ts +133 -0
- package/template/src/jobs/HealthCheckJob.ts +87 -0
- package/template/src/tools/CreateInlineJob.ts +42 -0
- package/template/src/tools/GameScoreTrackerTool.ts +356 -0
- package/template/src/tools/SmartBasketTool.ts +188 -0
- package/template/src/webhooks/PaymentWebhook.ts +113 -0
- package/template/src/webhooks/UserEventWebhook.ts +77 -0
- package/API_REFERENCE.md +0 -1408
- package/CHANGELOG.md +0 -236
- package/CLI_REFERENCE.md +0 -908
- package/GETTING_STARTED.md +0 -1040
- package/INSTANCE_TYPES.md +0 -1158
- package/README.md +0 -865
- package/TEMPLATE_GUIDE.md +0 -1398
- package/USER_DATA_INSTANCE.md +0 -621
- package/template/TOOL_EXAMPLES.md +0 -655
package/dist/utils/sandbox.js
CHANGED
|
@@ -9,6 +9,11 @@ import ProductApiService from "../api/products.api.service.js";
|
|
|
9
9
|
import BasketApiService from "../api/basket.api.service.js";
|
|
10
10
|
import OrderApiService from "../api/order.api.service.js";
|
|
11
11
|
import CustomDataApiService from "../api/custom.data.api.service.js";
|
|
12
|
+
import WebhookApi from "../api/webhook.api.service.js";
|
|
13
|
+
import JobApi from "../api/job.api.service.js";
|
|
14
|
+
import { BasketStatus } from "../interfaces/baskets.js";
|
|
15
|
+
import { OrderStatus } from "../interfaces/orders.js";
|
|
16
|
+
import { compressCode } from "./compile.js";
|
|
12
17
|
/**
|
|
13
18
|
* Loads environment variables from multiple sources in priority order:
|
|
14
19
|
* 1. process.env (lowest priority)
|
|
@@ -54,7 +59,18 @@ export function loadEnvironmentVariables() {
|
|
|
54
59
|
return envVars;
|
|
55
60
|
}
|
|
56
61
|
/**
|
|
57
|
-
* Creates a VM sandbox context with all necessary globals and utilities
|
|
62
|
+
* Creates a VM sandbox context with all necessary globals and utilities.
|
|
63
|
+
*
|
|
64
|
+
* Available globals in the sandbox:
|
|
65
|
+
* - User: User data API
|
|
66
|
+
* - Products: Product catalog API
|
|
67
|
+
* - Data: Custom data collections API
|
|
68
|
+
* - Baskets: Shopping basket API
|
|
69
|
+
* - Orders: Order management API
|
|
70
|
+
* - Webhooks: Webhook management API (new)
|
|
71
|
+
* - Jobs: Job scheduling API (new)
|
|
72
|
+
* - env(): Environment variable access function
|
|
73
|
+
* - console, fetch, and other standard globals
|
|
58
74
|
*/
|
|
59
75
|
export function createSandbox(options) {
|
|
60
76
|
const { apiKey, agentId, customConsole, broadcastLog } = options;
|
|
@@ -62,73 +78,6 @@ export function createSandbox(options) {
|
|
|
62
78
|
const envVars = loadEnvironmentVariables();
|
|
63
79
|
// Create a CommonJS context for execution
|
|
64
80
|
const require = createRequire(process.cwd() + '/package.json');
|
|
65
|
-
// const updateUserData = async (data: any) => {
|
|
66
|
-
// return await UserDataApi.updateUserData(apiKey, agentId, data);
|
|
67
|
-
// };
|
|
68
|
-
// const getUserData = async () => {
|
|
69
|
-
// return await UserDataApi.getUserData(apiKey, agentId);
|
|
70
|
-
// };
|
|
71
|
-
// const linkUserData = async (data: any) => {
|
|
72
|
-
// return await UserDataApi.createUserData(apiKey, agentId, data);
|
|
73
|
-
// };
|
|
74
|
-
// const createProduct = async (data: any) => {
|
|
75
|
-
// return await ProductApi.createProduct(apiKey, agentId, data);
|
|
76
|
-
// };
|
|
77
|
-
// const getProducts = async (page: number = 1, limit: number = 10) => {
|
|
78
|
-
// return await ProductApi.getProducts(apiKey, agentId, page, limit);
|
|
79
|
-
// };
|
|
80
|
-
// const updateProduct = async (data: any, productId: string) => {
|
|
81
|
-
// return await ProductApi.updateProduct(apiKey, agentId, { ...data, id: productId });
|
|
82
|
-
// };
|
|
83
|
-
// const deleteProduct = async (productId: string) => {
|
|
84
|
-
// return await ProductApi.deleteProduct(apiKey, agentId, productId);
|
|
85
|
-
// };
|
|
86
|
-
// const createBasket = async (data: any) => {
|
|
87
|
-
// return await ProductApi.createBasket(apiKey, agentId, data);
|
|
88
|
-
// };
|
|
89
|
-
// const getBaskets = async (status?: BasketStatus) => {
|
|
90
|
-
// return await ProductApi.getUserBaskets(apiKey, agentId, status);
|
|
91
|
-
// };
|
|
92
|
-
// const addItemToBasket = async (basketId: string, data: any) => {
|
|
93
|
-
// return await ProductApi.addItemToBasket(apiKey, agentId, basketId, data);
|
|
94
|
-
// };
|
|
95
|
-
// const removeItemFromBasket = async (basketId: string, itemId: string) => {
|
|
96
|
-
// return await ProductApi.removeItemFromBasket(apiKey, agentId, basketId, itemId);
|
|
97
|
-
// };
|
|
98
|
-
// const clearBasket = async (basketId: string) => {
|
|
99
|
-
// return await ProductApi.clearBasket(apiKey, agentId, basketId);
|
|
100
|
-
// };
|
|
101
|
-
// const updateBasketStatus = async (basketId: string, status: BasketStatus) => {
|
|
102
|
-
// return await ProductApi.updateBasketStatus(apiKey, agentId, basketId, status);
|
|
103
|
-
// };
|
|
104
|
-
// const createOrder = async (data: any) => {
|
|
105
|
-
// return await ProductApi.createOrder(apiKey, agentId, data);
|
|
106
|
-
// };
|
|
107
|
-
// const updateOrderStatus = async (orderId: string, status: OrderStatus) => {
|
|
108
|
-
// return await ProductApi.updateOrderStatus(apiKey, agentId, orderId, status);
|
|
109
|
-
// };
|
|
110
|
-
// const getUserOrders = async (userId: string) => {
|
|
111
|
-
// return await ProductApi.getUserOrders(apiKey, agentId, userId);
|
|
112
|
-
// };
|
|
113
|
-
// // Custom Data API functions
|
|
114
|
-
// const createCustomData = async (collectionName: string, data: any) => {
|
|
115
|
-
// return await CustomDataApi.createCustomData(apiKey, agentId, collectionName, data);
|
|
116
|
-
// };
|
|
117
|
-
// const getCustomData = async (collectionName: string, filter?: any, page: number = 1, limit: number = 10) => {
|
|
118
|
-
// return await CustomDataApi.getCustomData(apiKey, agentId, collectionName, filter, page, limit);
|
|
119
|
-
// };
|
|
120
|
-
// const getCustomDataEntry = async (collectionName: string, entryId: string) => {
|
|
121
|
-
// return await CustomDataApi.getCustomDataEntry(apiKey, agentId, collectionName, entryId);
|
|
122
|
-
// };
|
|
123
|
-
// const updateCustomData = async (collectionName: string, entryId: string, data: any) => {
|
|
124
|
-
// return await CustomDataApi.updateCustomData(apiKey, agentId, collectionName, entryId, data);
|
|
125
|
-
// };
|
|
126
|
-
// const searchCustomData = async (collectionName: string, searchText: string, limit: number = 10, scoreThreshold: number = 0.6) => {
|
|
127
|
-
// return await CustomDataApi.searchCustomData(apiKey, agentId, collectionName, searchText, limit, scoreThreshold);
|
|
128
|
-
// };
|
|
129
|
-
// const deleteCustomData = async (collectionName: string, entryId: string) => {
|
|
130
|
-
// return await CustomDataApi.deleteCustomData(apiKey, agentId, collectionName, entryId);
|
|
131
|
-
// };
|
|
132
81
|
// Create console object (use custom console if provided, otherwise default)
|
|
133
82
|
const consoleObj = customConsole || console;
|
|
134
83
|
// Create API service instances
|
|
@@ -137,6 +86,8 @@ export function createSandbox(options) {
|
|
|
137
86
|
const dataService = new CustomDataApiService(BASE_URLS.API, apiKey, agentId);
|
|
138
87
|
const basketsService = new BasketApiService(BASE_URLS.API, apiKey, agentId);
|
|
139
88
|
const orderService = new OrderApiService(BASE_URLS.API, apiKey, agentId);
|
|
89
|
+
const webhookService = new WebhookApi(BASE_URLS.API, apiKey, agentId);
|
|
90
|
+
const jobService = new JobApi(BASE_URLS.API, apiKey, agentId);
|
|
140
91
|
// Override User service methods if needed
|
|
141
92
|
// Example: Override the update method
|
|
142
93
|
// const originalUpdate = userService.update.bind(userService);
|
|
@@ -232,9 +183,6 @@ export function createSandbox(options) {
|
|
|
232
183
|
setInterval,
|
|
233
184
|
clearTimeout,
|
|
234
185
|
clearInterval,
|
|
235
|
-
process: {
|
|
236
|
-
env: envVars
|
|
237
|
-
},
|
|
238
186
|
global: globalThis,
|
|
239
187
|
__dirname: process.cwd(),
|
|
240
188
|
__filename: path.join(process.cwd(), 'index.ts'),
|
|
@@ -274,9 +222,68 @@ export function createSandbox(options) {
|
|
|
274
222
|
Products: productService,
|
|
275
223
|
Data: dataService,
|
|
276
224
|
Baskets: basketsService,
|
|
225
|
+
compressCode: compressCode,
|
|
277
226
|
Orders: orderService,
|
|
227
|
+
// Jobs API
|
|
228
|
+
Jobs: {
|
|
229
|
+
create: async (config) => {
|
|
230
|
+
// Convert execute function to string
|
|
231
|
+
const executeString = typeof config.execute === 'function'
|
|
232
|
+
? config.execute.toString()
|
|
233
|
+
: config.execute;
|
|
234
|
+
console.log('IsDynamicJob', config.dynamic);
|
|
235
|
+
// Create job with version and activation
|
|
236
|
+
return await jobService.createJobInstance({
|
|
237
|
+
name: config.name,
|
|
238
|
+
description: config.description,
|
|
239
|
+
context: config.description || '',
|
|
240
|
+
schedule: config.schedule,
|
|
241
|
+
timeout: config.timeout,
|
|
242
|
+
retry: config.retry,
|
|
243
|
+
metadata: config.metadata,
|
|
244
|
+
dynamic: true,
|
|
245
|
+
version: {
|
|
246
|
+
version: '1.0.0',
|
|
247
|
+
description: config.description,
|
|
248
|
+
context: config.description || '',
|
|
249
|
+
code: '',
|
|
250
|
+
executeFunction: executeString,
|
|
251
|
+
timeout: config.timeout,
|
|
252
|
+
retry: config.retry,
|
|
253
|
+
metadata: config.metadata
|
|
254
|
+
},
|
|
255
|
+
activate: config.activate ?? true
|
|
256
|
+
});
|
|
257
|
+
},
|
|
258
|
+
get: async (jobId) => jobService.getJob(jobId)
|
|
259
|
+
},
|
|
278
260
|
// Environment variables function
|
|
279
|
-
env: (key) => envVars[key]
|
|
261
|
+
env: (key) => envVars[key],
|
|
262
|
+
// Enums for status values
|
|
263
|
+
BasketStatus: BasketStatus,
|
|
264
|
+
OrderStatus: OrderStatus,
|
|
265
|
+
// AI Generation API
|
|
266
|
+
AI: {
|
|
267
|
+
generate: async (context, messages, targetAgentId) => {
|
|
268
|
+
const ChatApi = (await import('../api/chat.api.service.js')).default;
|
|
269
|
+
const chatService = new ChatApi(BASE_URLS.API, apiKey);
|
|
270
|
+
// Use provided agentId or default to current agent
|
|
271
|
+
const useAgentId = targetAgentId || 'luaAgent';
|
|
272
|
+
const result = await chatService.sendMessage(useAgentId, {
|
|
273
|
+
messages,
|
|
274
|
+
navigate: false,
|
|
275
|
+
skillOverride: [],
|
|
276
|
+
runtimeContext: context
|
|
277
|
+
});
|
|
278
|
+
if (!result.success) {
|
|
279
|
+
throw new Error(result.error?.message || 'AI generation failed');
|
|
280
|
+
}
|
|
281
|
+
return result.data?.text || '';
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
process: {
|
|
285
|
+
env: envVars
|
|
286
|
+
}
|
|
280
287
|
};
|
|
281
288
|
return sandbox;
|
|
282
289
|
}
|
|
@@ -324,6 +331,226 @@ try{
|
|
|
324
331
|
const executeFunction = context.module.exports;
|
|
325
332
|
return await executeFunction(inputs);
|
|
326
333
|
}
|
|
334
|
+
/**
|
|
335
|
+
* Executes a webhook in a sandboxed environment.
|
|
336
|
+
*
|
|
337
|
+
* @param options - Webhook execution options including code and request data
|
|
338
|
+
* @returns Result from webhook execution
|
|
339
|
+
*/
|
|
340
|
+
export async function executeWebhook(options) {
|
|
341
|
+
const { webhookCode, query, headers, body } = options;
|
|
342
|
+
// Create sandbox
|
|
343
|
+
const sandbox = createSandbox(options);
|
|
344
|
+
// Create the CommonJS wrapper code
|
|
345
|
+
const commonJsWrapper = `
|
|
346
|
+
const executeFunction = ${webhookCode};
|
|
347
|
+
|
|
348
|
+
// Export the function for testing
|
|
349
|
+
module.exports = async (query, headers, body) => {
|
|
350
|
+
try{
|
|
351
|
+
return await executeFunction(query, headers, body);
|
|
352
|
+
}catch(e){
|
|
353
|
+
console.error(e);
|
|
354
|
+
return { status: 'error', error: e.message };
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
`;
|
|
358
|
+
// Execute the code in the sandbox
|
|
359
|
+
const context = vm.createContext(sandbox);
|
|
360
|
+
// Add polyfills
|
|
361
|
+
vm.runInContext(`
|
|
362
|
+
if (typeof global !== 'undefined') {
|
|
363
|
+
global.AbortController = AbortController;
|
|
364
|
+
global.FormData = FormData;
|
|
365
|
+
global.TextEncoder = TextEncoder;
|
|
366
|
+
global.TextDecoder = TextDecoder;
|
|
367
|
+
global.crypto = crypto;
|
|
368
|
+
global.fetch = fetch;
|
|
369
|
+
global.Headers = Headers;
|
|
370
|
+
global.Request = Request;
|
|
371
|
+
global.Response = Response;
|
|
372
|
+
global.URL = URL;
|
|
373
|
+
global.URLSearchParams = URLSearchParams;
|
|
374
|
+
}
|
|
375
|
+
`, context);
|
|
376
|
+
vm.runInContext(commonJsWrapper, context);
|
|
377
|
+
// Get the exported function and execute with webhook input structure
|
|
378
|
+
const executeFunction = context.module.exports;
|
|
379
|
+
return await executeFunction({ query, headers, body });
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Executes a job in a sandboxed environment.
|
|
383
|
+
*
|
|
384
|
+
* @param options - Job execution options including code and job data
|
|
385
|
+
* @returns Result from job execution
|
|
386
|
+
*/
|
|
387
|
+
export async function executeJob(options) {
|
|
388
|
+
const { jobCode, jobData, apiKey, agentId } = options;
|
|
389
|
+
// Create sandbox
|
|
390
|
+
const sandbox = createSandbox(options);
|
|
391
|
+
// Create the CommonJS wrapper code that creates JobInstance from jobData
|
|
392
|
+
const commonJsWrapper = `
|
|
393
|
+
const executeFunction = ${jobCode};
|
|
394
|
+
|
|
395
|
+
// Export the function for testing
|
|
396
|
+
module.exports = async (jobData) => {
|
|
397
|
+
try{
|
|
398
|
+
// Create job instance from jobData if provided
|
|
399
|
+
let jobInstance = undefined;
|
|
400
|
+
if (jobData) {
|
|
401
|
+
jobInstance = {
|
|
402
|
+
id: jobData.id || jobData.jobId,
|
|
403
|
+
jobId: jobData.jobId || jobData.id,
|
|
404
|
+
name: jobData.name,
|
|
405
|
+
schedule: jobData.schedule,
|
|
406
|
+
metadata: jobData.metadata || {},
|
|
407
|
+
data: jobData,
|
|
408
|
+
updateMetadata: async (newMetadata) => {
|
|
409
|
+
jobInstance.metadata = { ...jobInstance.metadata, ...newMetadata };
|
|
410
|
+
console.log('✅ Metadata updated:', jobInstance.metadata);
|
|
411
|
+
},
|
|
412
|
+
delete: async () => {
|
|
413
|
+
console.log('🗑️ Job.delete() called for:', jobInstance.jobId);
|
|
414
|
+
// Note: In production, this would call Jobs.deactivate(jobId)
|
|
415
|
+
},
|
|
416
|
+
toJSON: () => jobData
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return await executeFunction(jobInstance);
|
|
421
|
+
}catch(e){
|
|
422
|
+
console.error(e);
|
|
423
|
+
return { status: 'error', error: e.message };
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
`;
|
|
427
|
+
// Execute the code in the sandbox
|
|
428
|
+
const context = vm.createContext(sandbox);
|
|
429
|
+
// Add polyfills
|
|
430
|
+
vm.runInContext(`
|
|
431
|
+
if (typeof global !== 'undefined') {
|
|
432
|
+
global.AbortController = AbortController;
|
|
433
|
+
global.FormData = FormData;
|
|
434
|
+
global.TextEncoder = TextEncoder;
|
|
435
|
+
global.TextDecoder = TextDecoder;
|
|
436
|
+
global.crypto = crypto;
|
|
437
|
+
global.fetch = fetch;
|
|
438
|
+
global.Headers = Headers;
|
|
439
|
+
global.Request = Request;
|
|
440
|
+
global.Response = Response;
|
|
441
|
+
global.URL = URL;
|
|
442
|
+
global.URLSearchParams = URLSearchParams;
|
|
443
|
+
}
|
|
444
|
+
`, context);
|
|
445
|
+
vm.runInContext(commonJsWrapper, context);
|
|
446
|
+
// Get the exported function and execute with jobData
|
|
447
|
+
const executeFunction = context.module.exports;
|
|
448
|
+
return await executeFunction(jobData);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Executes a preprocessor in a sandboxed environment.
|
|
452
|
+
*
|
|
453
|
+
* @param options - PreProcessor execution options
|
|
454
|
+
* @returns Processed message string
|
|
455
|
+
*/
|
|
456
|
+
export async function executePreProcessor(options) {
|
|
457
|
+
const { processorCode, messages, channel, apiKey, agentId } = options;
|
|
458
|
+
// Create sandbox
|
|
459
|
+
const sandbox = createSandbox(options);
|
|
460
|
+
// Get user instance first (outside VM)
|
|
461
|
+
const UserDataApi = (await import('../api/user.data.api.service.js')).default;
|
|
462
|
+
const userService = new UserDataApi(BASE_URLS.API, apiKey, agentId);
|
|
463
|
+
const userInstance = await userService.get();
|
|
464
|
+
// Create the CommonJS wrapper code
|
|
465
|
+
const commonJsWrapper = `
|
|
466
|
+
const executeFunction = ${processorCode};
|
|
467
|
+
|
|
468
|
+
// Export the function for testing
|
|
469
|
+
module.exports = async (input) => {
|
|
470
|
+
try{
|
|
471
|
+
// userInstance is passed as parameter
|
|
472
|
+
return await executeFunction(input.user, input.messages, input.channel);
|
|
473
|
+
}catch(e){
|
|
474
|
+
console.error(e);
|
|
475
|
+
return { status: 'error', error: e.message };
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
`;
|
|
479
|
+
// Execute the code in the sandbox
|
|
480
|
+
const context = vm.createContext(sandbox);
|
|
481
|
+
// Add polyfills
|
|
482
|
+
vm.runInContext(`
|
|
483
|
+
if (typeof global !== 'undefined') {
|
|
484
|
+
global.AbortController = AbortController;
|
|
485
|
+
global.FormData = FormData;
|
|
486
|
+
global.TextEncoder = TextEncoder;
|
|
487
|
+
global.TextDecoder = TextDecoder;
|
|
488
|
+
global.crypto = crypto;
|
|
489
|
+
global.fetch = fetch;
|
|
490
|
+
global.Headers = Headers;
|
|
491
|
+
global.Request = Request;
|
|
492
|
+
global.Response = Response;
|
|
493
|
+
global.URL = URL;
|
|
494
|
+
global.URLSearchParams = URLSearchParams;
|
|
495
|
+
}
|
|
496
|
+
`, context);
|
|
497
|
+
vm.runInContext(commonJsWrapper, context);
|
|
498
|
+
// Get the exported function and execute with user instance
|
|
499
|
+
const executeFunction = context.module.exports;
|
|
500
|
+
return await executeFunction({ user: userInstance, messages, channel });
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Executes a postprocessor in a sandboxed environment.
|
|
504
|
+
*
|
|
505
|
+
* @param options - PostProcessor execution options
|
|
506
|
+
* @returns Processed response string
|
|
507
|
+
*/
|
|
508
|
+
export async function executePostProcessor(options) {
|
|
509
|
+
const { processorCode, message, response, channel, apiKey, agentId } = options;
|
|
510
|
+
// Create sandbox
|
|
511
|
+
const sandbox = createSandbox(options);
|
|
512
|
+
// Get user instance first (outside VM)
|
|
513
|
+
const UserDataApi = (await import('../api/user.data.api.service.js')).default;
|
|
514
|
+
const userService = new UserDataApi(BASE_URLS.API, apiKey, agentId);
|
|
515
|
+
const userInstance = await userService.get();
|
|
516
|
+
// Create the CommonJS wrapper code
|
|
517
|
+
const commonJsWrapper = `
|
|
518
|
+
const executeFunction = ${processorCode};
|
|
519
|
+
|
|
520
|
+
// Export the function for testing
|
|
521
|
+
module.exports = async (input) => {
|
|
522
|
+
try{
|
|
523
|
+
// userInstance is passed as parameter
|
|
524
|
+
return await executeFunction(input.user, input.message, input.response, input.channel);
|
|
525
|
+
}catch(e){
|
|
526
|
+
console.error(e);
|
|
527
|
+
return { status: 'error', error: e.message };
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
`;
|
|
531
|
+
// Execute the code in the sandbox
|
|
532
|
+
const context = vm.createContext(sandbox);
|
|
533
|
+
// Add polyfills
|
|
534
|
+
vm.runInContext(`
|
|
535
|
+
if (typeof global !== 'undefined') {
|
|
536
|
+
global.AbortController = AbortController;
|
|
537
|
+
global.FormData = FormData;
|
|
538
|
+
global.TextEncoder = TextEncoder;
|
|
539
|
+
global.TextDecoder = TextDecoder;
|
|
540
|
+
global.crypto = crypto;
|
|
541
|
+
global.fetch = fetch;
|
|
542
|
+
global.Headers = Headers;
|
|
543
|
+
global.Request = Request;
|
|
544
|
+
global.Response = Response;
|
|
545
|
+
global.URL = URL;
|
|
546
|
+
global.URLSearchParams = URLSearchParams;
|
|
547
|
+
}
|
|
548
|
+
`, context);
|
|
549
|
+
vm.runInContext(commonJsWrapper, context);
|
|
550
|
+
// Get the exported function and execute with user instance
|
|
551
|
+
const executeFunction = context.module.exports;
|
|
552
|
+
return await executeFunction({ user: userInstance, message, response, channel });
|
|
553
|
+
}
|
|
327
554
|
/**
|
|
328
555
|
* Creates a custom console that broadcasts logs via WebSocket
|
|
329
556
|
*/
|
|
@@ -6,13 +6,14 @@ import { Project } from "ts-morph";
|
|
|
6
6
|
import { ToolInfo } from '../interfaces/compile.js';
|
|
7
7
|
import { TypeScriptSourceFile } from '../types/compile.types.js';
|
|
8
8
|
/**
|
|
9
|
-
* Detects all tools in the project by analyzing the index file.
|
|
9
|
+
* Detects all tools in the project by analyzing the index file and skill files.
|
|
10
10
|
* Searches for tools in two places:
|
|
11
11
|
* 1. LuaSkill constructor's tools array
|
|
12
12
|
* 2. skill.addTools() or skill.addTool() method calls
|
|
13
13
|
*
|
|
14
14
|
* @param indexFile - The main index TypeScript source file
|
|
15
15
|
* @param project - The ts-morph Project instance
|
|
16
|
+
* @param skillFilePaths - Optional array of skill file paths to scan for tools (for LuaAgent)
|
|
16
17
|
* @returns Promise resolving to array of detected tool information
|
|
17
18
|
*/
|
|
18
|
-
export declare function detectTools(indexFile: TypeScriptSourceFile, project: Project): Promise<ToolInfo[]>;
|
|
19
|
+
export declare function detectTools(indexFile: TypeScriptSourceFile, project: Project, skillFilePaths?: string[]): Promise<ToolInfo[]>;
|
|
@@ -5,21 +5,35 @@
|
|
|
5
5
|
import { Node } from "ts-morph";
|
|
6
6
|
import { extractToolFromNewExpressionSync, } from './compile.js';
|
|
7
7
|
/**
|
|
8
|
-
* Detects all tools in the project by analyzing the index file.
|
|
8
|
+
* Detects all tools in the project by analyzing the index file and skill files.
|
|
9
9
|
* Searches for tools in two places:
|
|
10
10
|
* 1. LuaSkill constructor's tools array
|
|
11
11
|
* 2. skill.addTools() or skill.addTool() method calls
|
|
12
12
|
*
|
|
13
13
|
* @param indexFile - The main index TypeScript source file
|
|
14
14
|
* @param project - The ts-morph Project instance
|
|
15
|
+
* @param skillFilePaths - Optional array of skill file paths to scan for tools (for LuaAgent)
|
|
15
16
|
* @returns Promise resolving to array of detected tool information
|
|
16
17
|
*/
|
|
17
|
-
export async function detectTools(indexFile, project) {
|
|
18
|
+
export async function detectTools(indexFile, project, skillFilePaths) {
|
|
18
19
|
const tools = [];
|
|
19
|
-
// Search strategy 1: Find tools in LuaSkill constructor's tools array
|
|
20
|
+
// Search strategy 1: Find tools in LuaSkill constructor's tools array in index file
|
|
20
21
|
detectToolsFromConstructor(indexFile, project, tools);
|
|
21
|
-
// Search strategy 2: Find tools from skill.addTools() or skill.addTool() method calls
|
|
22
|
+
// Search strategy 2: Find tools from skill.addTools() or skill.addTool() method calls in index file
|
|
22
23
|
detectToolsFromMethodCalls(indexFile, project, tools);
|
|
24
|
+
// Search strategy 3: If skill files are provided (from LuaAgent), scan those too
|
|
25
|
+
if (skillFilePaths && skillFilePaths.length > 0) {
|
|
26
|
+
for (const skillFilePath of skillFilePaths) {
|
|
27
|
+
try {
|
|
28
|
+
const skillFile = project.addSourceFileAtPath(skillFilePath);
|
|
29
|
+
detectToolsFromConstructor(skillFile, project, tools);
|
|
30
|
+
detectToolsFromMethodCalls(skillFile, project, tools);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
console.warn(`Warning: Could not scan skill file ${skillFilePath} for tools:`, error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
23
37
|
return tools;
|
|
24
38
|
}
|
|
25
39
|
/**
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webhook Management Utilities
|
|
3
|
+
* Handles webhook creation via API and YAML configuration management
|
|
4
|
+
*/
|
|
5
|
+
import { SkillConfig } from '../types/compile.types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Ensures all detected webhooks exist in the YAML config with valid webhook IDs.
|
|
8
|
+
* If a webhook doesn't exist or has no ID, creates it via the API.
|
|
9
|
+
*
|
|
10
|
+
* @param webhooksArray - Array of webhooks detected from source code
|
|
11
|
+
* @param config - The skill configuration from lua.skill.yaml
|
|
12
|
+
* @returns Updated webhooks array with valid webhook IDs
|
|
13
|
+
*/
|
|
14
|
+
export declare function ensureWebhooksExistInYaml(webhooksArray: any[], config: SkillConfig): Promise<any[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Syncs the server webhooks with the YAML configuration.
|
|
17
|
+
* Performs a two-way sync:
|
|
18
|
+
* 1. Deletes/deactivates webhooks from server that aren't in YAML
|
|
19
|
+
* 2. Updates YAML with active version numbers from server
|
|
20
|
+
*
|
|
21
|
+
* @param config - The skill configuration from lua.skill.yaml
|
|
22
|
+
* @returns Array of messages about sync operations
|
|
23
|
+
*/
|
|
24
|
+
export declare function syncServerWebhooksWithYaml(config: SkillConfig): Promise<string[]>;
|