lua-cli 2.5.7 → 3.0.0-alpha.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.
Files changed (124) hide show
  1. package/dist/api/agent.api.service.d.ts +45 -0
  2. package/dist/api/agent.api.service.js +54 -0
  3. package/dist/api/job.api.service.d.ts +210 -0
  4. package/dist/api/job.api.service.js +200 -0
  5. package/dist/api/lazy-instances.d.ts +24 -0
  6. package/dist/api/lazy-instances.js +48 -0
  7. package/dist/api/postprocessor.api.service.d.ts +98 -0
  8. package/dist/api/postprocessor.api.service.js +76 -0
  9. package/dist/api/preprocessor.api.service.d.ts +98 -0
  10. package/dist/api/preprocessor.api.service.js +76 -0
  11. package/dist/api/user.data.api.service.d.ts +28 -0
  12. package/dist/api/user.data.api.service.js +51 -0
  13. package/dist/api/webhook.api.service.d.ts +151 -0
  14. package/dist/api/webhook.api.service.js +134 -0
  15. package/dist/api-exports.d.ts +156 -41
  16. package/dist/api-exports.js +182 -21
  17. package/dist/cli/command-definitions.js +149 -7
  18. package/dist/commands/compile.js +124 -5
  19. package/dist/commands/completion.d.ts +11 -0
  20. package/dist/commands/completion.js +209 -0
  21. package/dist/commands/env.d.ts +3 -2
  22. package/dist/commands/env.js +42 -17
  23. package/dist/commands/features.d.ts +16 -0
  24. package/dist/commands/features.js +352 -0
  25. package/dist/commands/index.d.ts +7 -0
  26. package/dist/commands/index.js +7 -0
  27. package/dist/commands/init.js +53 -7
  28. package/dist/commands/jobs.d.ts +20 -0
  29. package/dist/commands/jobs.js +533 -0
  30. package/dist/commands/logs.js +2 -5
  31. package/dist/commands/persona.d.ts +3 -2
  32. package/dist/commands/persona.js +43 -18
  33. package/dist/commands/postprocessors.d.ts +8 -0
  34. package/dist/commands/postprocessors.js +431 -0
  35. package/dist/commands/preprocessors.d.ts +8 -0
  36. package/dist/commands/preprocessors.js +431 -0
  37. package/dist/commands/push.d.ts +9 -13
  38. package/dist/commands/push.js +937 -69
  39. package/dist/commands/skills.d.ts +16 -0
  40. package/dist/commands/skills.js +438 -0
  41. package/dist/commands/test.d.ts +9 -18
  42. package/dist/commands/test.js +558 -82
  43. package/dist/commands/webhooks.d.ts +18 -0
  44. package/dist/commands/webhooks.js +424 -0
  45. package/dist/common/data.entry.instance.d.ts +7 -0
  46. package/dist/common/data.entry.instance.js +15 -0
  47. package/dist/common/job.instance.d.ts +77 -0
  48. package/dist/common/job.instance.js +108 -0
  49. package/dist/common/order.instance.d.ts +6 -0
  50. package/dist/common/order.instance.js +14 -0
  51. package/dist/common/product.instance.d.ts +6 -0
  52. package/dist/common/product.instance.js +14 -0
  53. package/dist/common/user.instance.d.ts +15 -0
  54. package/dist/common/user.instance.js +38 -0
  55. package/dist/config/constants.d.ts +2 -2
  56. package/dist/config/constants.js +4 -4
  57. package/dist/index.js +14 -3
  58. package/dist/interfaces/agent.d.ts +33 -1
  59. package/dist/interfaces/chat.d.ts +22 -0
  60. package/dist/interfaces/index.d.ts +10 -0
  61. package/dist/interfaces/index.js +7 -0
  62. package/dist/interfaces/jobs.d.ts +172 -0
  63. package/dist/interfaces/jobs.js +5 -0
  64. package/dist/interfaces/message.d.ts +18 -0
  65. package/dist/interfaces/message.js +1 -0
  66. package/dist/interfaces/postprocessors.d.ts +35 -0
  67. package/dist/interfaces/postprocessors.js +4 -0
  68. package/dist/interfaces/preprocessors.d.ts +35 -0
  69. package/dist/interfaces/preprocessors.js +4 -0
  70. package/dist/interfaces/webhooks.d.ts +104 -0
  71. package/dist/interfaces/webhooks.js +5 -0
  72. package/dist/types/api-contracts.d.ts +14 -0
  73. package/dist/types/api-contracts.js +0 -7
  74. package/dist/types/compile.types.d.ts +49 -0
  75. package/dist/types/index.d.ts +1 -1
  76. package/dist/types/index.js +1 -1
  77. package/dist/types/skill.d.ts +502 -0
  78. package/dist/types/skill.js +477 -0
  79. package/dist/utils/agent-management.d.ts +25 -0
  80. package/dist/utils/agent-management.js +67 -0
  81. package/dist/utils/bundling.d.ts +31 -1
  82. package/dist/utils/bundling.js +653 -10
  83. package/dist/utils/compile.d.ts +63 -0
  84. package/dist/utils/compile.js +691 -36
  85. package/dist/utils/deployment.d.ts +2 -1
  86. package/dist/utils/deployment.js +16 -2
  87. package/dist/utils/init-agent.d.ts +3 -1
  88. package/dist/utils/init-agent.js +6 -4
  89. package/dist/utils/init-prompts.d.ts +2 -1
  90. package/dist/utils/init-prompts.js +14 -9
  91. package/dist/utils/job-management.d.ts +24 -0
  92. package/dist/utils/job-management.js +264 -0
  93. package/dist/utils/postprocessor-management.d.ts +9 -0
  94. package/dist/utils/postprocessor-management.js +118 -0
  95. package/dist/utils/preprocessor-management.d.ts +9 -0
  96. package/dist/utils/preprocessor-management.js +118 -0
  97. package/dist/utils/sandbox.d.ts +61 -1
  98. package/dist/utils/sandbox.js +283 -72
  99. package/dist/utils/tool-detection.d.ts +3 -2
  100. package/dist/utils/tool-detection.js +18 -4
  101. package/dist/utils/webhook-management.d.ts +24 -0
  102. package/dist/utils/webhook-management.js +256 -0
  103. package/dist/web/app.css +152 -736
  104. package/dist/web/app.js +45 -45
  105. package/package.json +2 -2
  106. package/template/AGENT_CONFIGURATION.md +251 -0
  107. package/template/COMPLEX_JOB_EXAMPLES.md +795 -0
  108. package/template/DYNAMIC_JOB_CREATION.md +371 -0
  109. package/template/README.md +30 -2
  110. package/template/WEBHOOKS_JOBS_QUICKSTART.md +318 -0
  111. package/template/WEBHOOK_JOB_EXAMPLES.md +817 -0
  112. package/template/package.json +1 -1
  113. package/template/src/index-agent-example.ts +201 -0
  114. package/template/src/index.ts +39 -0
  115. package/template/src/jobs/AbandonedBasketProcessorJob.ts +139 -0
  116. package/template/src/jobs/DailyCleanupJob.ts +100 -0
  117. package/template/src/jobs/DataMigrationJob.ts +133 -0
  118. package/template/src/jobs/HealthCheckJob.ts +87 -0
  119. package/template/src/postprocessors/ResponseFormatter.ts +151 -0
  120. package/template/src/preprocessors/MessageFilter.ts +91 -0
  121. package/template/src/tools/GameScoreTrackerTool.ts +356 -0
  122. package/template/src/tools/SmartBasketTool.ts +188 -0
  123. package/template/src/webhooks/PaymentWebhook.ts +113 -0
  124. package/template/src/webhooks/UserEventWebhook.ts +77 -0
@@ -0,0 +1,256 @@
1
+ /**
2
+ * Webhook Management Utilities
3
+ * Handles webhook creation via API and YAML configuration management
4
+ */
5
+ import fs from "fs";
6
+ import path from "path";
7
+ import yaml from "js-yaml";
8
+ import WebhookApi from '../api/webhook.api.service.js';
9
+ import { BASE_URLS } from '../config/constants.js';
10
+ import { loadApiKey } from '../services/auth.js';
11
+ import { COMPILE_FILES, SKILL_DEFAULTS, YAML_FORMAT, } from '../config/compile.constants.js';
12
+ /**
13
+ * Ensures all detected webhooks exist in the YAML config with valid webhook IDs.
14
+ * If a webhook doesn't exist or has no ID, creates it via the API.
15
+ *
16
+ * @param webhooksArray - Array of webhooks detected from source code
17
+ * @param config - The skill configuration from lua.skill.yaml
18
+ * @returns Updated webhooks array with valid webhook IDs
19
+ */
20
+ export async function ensureWebhooksExistInYaml(webhooksArray, config) {
21
+ const updatedWebhooksArray = [];
22
+ let yamlUpdated = false;
23
+ const existingWebhooks = config?.webhooks || [];
24
+ const existingWebhooksMap = createWebhooksMap(existingWebhooks);
25
+ // Process each detected webhook
26
+ for (const webhook of webhooksArray) {
27
+ const existingWebhook = existingWebhooksMap.get(webhook.name);
28
+ if (existingWebhook && existingWebhook.webhookId && existingWebhook.webhookId !== '') {
29
+ // Webhook exists with valid webhookId - reuse it
30
+ updatedWebhooksArray.push({
31
+ ...webhook,
32
+ webhookId: existingWebhook.webhookId
33
+ });
34
+ }
35
+ else {
36
+ // Webhook doesn't exist or missing webhookId - create via API
37
+ const createdWebhook = await createWebhookViaApi(webhook, config, existingWebhooks, existingWebhook);
38
+ updatedWebhooksArray.push(createdWebhook);
39
+ yamlUpdated = true;
40
+ }
41
+ }
42
+ // Update YAML file if any changes were made
43
+ if (yamlUpdated) {
44
+ await updateYamlWithWebhooks(config);
45
+ }
46
+ return updatedWebhooksArray;
47
+ }
48
+ /**
49
+ * Creates a map of existing webhooks for quick lookup by name.
50
+ *
51
+ * @param existingWebhooks - Array of webhooks from YAML config
52
+ * @returns Map of webhook names to webhook objects
53
+ */
54
+ function createWebhooksMap(existingWebhooks) {
55
+ const map = new Map();
56
+ existingWebhooks.forEach((webhook) => {
57
+ map.set(webhook.name, webhook);
58
+ });
59
+ return map;
60
+ }
61
+ /**
62
+ * Creates a new webhook via the Lua API and updates the config.
63
+ *
64
+ * @param webhook - The webhook to create
65
+ * @param config - The skill configuration
66
+ * @param existingWebhooks - Array of existing webhooks (mutated)
67
+ * @param existingWebhook - The existing webhook entry if one exists (may be without ID)
68
+ * @returns The webhook with its new ID
69
+ */
70
+ async function createWebhookViaApi(webhook, config, existingWebhooks, existingWebhook) {
71
+ try {
72
+ // Validate prerequisites
73
+ const apiKey = await loadApiKey();
74
+ if (!apiKey) {
75
+ throw new Error("No API key found. Run 'lua auth configure' first.");
76
+ }
77
+ const agentId = config?.agent?.agentId;
78
+ if (!agentId) {
79
+ throw new Error("No agent ID found in lua.skill.yaml. Run 'lua init' first.");
80
+ }
81
+ // Create webhook via API
82
+ const webhookPayload = {
83
+ name: webhook.name,
84
+ description: webhook.description || `A Lua webhook for ${webhook.name}`,
85
+ context: webhook.context || ''
86
+ };
87
+ const webhookApi = new WebhookApi(BASE_URLS.API, apiKey, agentId);
88
+ const result = await webhookApi.createWebhook(webhookPayload);
89
+ if (result.success && result.data && result.data.id) {
90
+ const newWebhookId = result.data.id;
91
+ // Update YAML config with new webhook ID
92
+ if (!existingWebhook) {
93
+ existingWebhooks.push({
94
+ name: webhook.name || '',
95
+ version: webhook.version || SKILL_DEFAULTS.VERSION,
96
+ webhookId: newWebhookId
97
+ });
98
+ }
99
+ else {
100
+ existingWebhook.webhookId = newWebhookId;
101
+ }
102
+ // Add webhooks to config if not already present
103
+ if (!config.webhooks) {
104
+ config.webhooks = existingWebhooks;
105
+ }
106
+ return {
107
+ ...webhook,
108
+ webhookId: newWebhookId
109
+ };
110
+ }
111
+ else {
112
+ console.error(`❌ Failed to create webhook ${webhook.name}:`, result.error);
113
+ throw new Error(result.error?.message || 'Failed to create webhook - no ID returned from API');
114
+ }
115
+ }
116
+ catch (error) {
117
+ console.error(`❌ Failed to create webhook ${webhook.name}:`, error);
118
+ throw error;
119
+ }
120
+ }
121
+ /**
122
+ * Updates the lua.skill.yaml file with the current webhooks array.
123
+ * Ensures no undefined values are written to the YAML file.
124
+ *
125
+ * @param config - Current skill configuration to merge with
126
+ */
127
+ async function updateYamlWithWebhooks(config) {
128
+ const webhooks = config?.webhooks || [];
129
+ // Clean webhooks array to ensure no undefined values
130
+ const cleanedWebhooks = webhooks.map(webhook => ({
131
+ name: webhook.name || '',
132
+ version: webhook.version || SKILL_DEFAULTS.VERSION,
133
+ webhookId: webhook.webhookId || ''
134
+ }));
135
+ // Update config with cleaned webhooks array
136
+ const updatedConfig = {
137
+ ...config,
138
+ webhooks: cleanedWebhooks
139
+ };
140
+ // Write updated YAML with consistent formatting
141
+ const yamlPath = path.join(process.cwd(), COMPILE_FILES.LUA_SKILL_YAML);
142
+ const yamlContent = yaml.dump(updatedConfig, {
143
+ indent: YAML_FORMAT.INDENT,
144
+ lineWidth: YAML_FORMAT.LINE_WIDTH,
145
+ noRefs: YAML_FORMAT.NO_REFS,
146
+ replacer: (key, value) => {
147
+ // Replace undefined values with empty strings
148
+ return value === undefined ? '' : value;
149
+ }
150
+ });
151
+ fs.writeFileSync(yamlPath, yamlContent);
152
+ }
153
+ /**
154
+ * Syncs the server webhooks with the YAML configuration.
155
+ * Performs a two-way sync:
156
+ * 1. Deletes/deactivates webhooks from server that aren't in YAML
157
+ * 2. Updates YAML with active version numbers from server
158
+ *
159
+ * @param config - The skill configuration from lua.skill.yaml
160
+ * @returns Array of messages about sync operations
161
+ */
162
+ export async function syncServerWebhooksWithYaml(config) {
163
+ const messages = [];
164
+ let yamlNeedsUpdate = false;
165
+ try {
166
+ // Validate prerequisites
167
+ const apiKey = await loadApiKey();
168
+ if (!apiKey) {
169
+ console.warn("⚠️ No API key found. Skipping server webhook sync.");
170
+ return messages;
171
+ }
172
+ const agentId = config?.agent?.agentId;
173
+ if (!agentId) {
174
+ console.warn("⚠️ No agent ID found in lua.skill.yaml. Skipping server webhook sync.");
175
+ return messages;
176
+ }
177
+ // Get webhooks from server
178
+ const webhookApi = new WebhookApi(BASE_URLS.API, apiKey, agentId);
179
+ const serverWebhooksResponse = await webhookApi.getWebhooks();
180
+ if (!serverWebhooksResponse.success || !serverWebhooksResponse.data?.webhooks) {
181
+ console.warn("⚠️ Could not retrieve server webhooks. Skipping server webhook sync.");
182
+ return messages;
183
+ }
184
+ const serverWebhooks = serverWebhooksResponse.data.webhooks;
185
+ const yamlWebhooks = config?.webhooks || [];
186
+ // Create maps for efficient lookup
187
+ const yamlWebhooksMap = new Map(yamlWebhooks
188
+ .filter((webhook) => webhook.webhookId)
189
+ .map((webhook) => [webhook.webhookId, webhook]));
190
+ const serverWebhooksMap = new Map(serverWebhooks.map(webhook => [webhook.id, webhook]));
191
+ // Part 1: Delete webhooks from server that aren't in YAML
192
+ const webhooksToDelete = serverWebhooks.filter(serverWebhook => !yamlWebhooksMap.has(serverWebhook.id));
193
+ for (const webhook of webhooksToDelete) {
194
+ try {
195
+ const deleteResponse = await webhookApi.deleteWebhook(webhook.id);
196
+ if (deleteResponse.success && deleteResponse.data) {
197
+ if (deleteResponse.data.deleted) {
198
+ const msg = `✅ Deleted webhook "${webhook.name}" from server`;
199
+ messages.push(msg);
200
+ console.log(msg);
201
+ }
202
+ else if (deleteResponse.data.deactivated) {
203
+ const msg = `⚠️ Webhook "${webhook.name}" has versions and cannot be deleted. It has been deactivated instead.`;
204
+ messages.push(msg);
205
+ console.warn(msg);
206
+ }
207
+ }
208
+ else {
209
+ const msg = `❌ Failed to delete webhook "${webhook.name}": ${deleteResponse.error?.message || 'Unknown error'}`;
210
+ messages.push(msg);
211
+ console.error(msg);
212
+ }
213
+ }
214
+ catch (error) {
215
+ const msg = `❌ Error deleting webhook "${webhook.name}": ${error}`;
216
+ messages.push(msg);
217
+ console.error(msg);
218
+ }
219
+ }
220
+ // Part 2: Sync version numbers from server to YAML
221
+ const updatedYamlWebhooks = yamlWebhooks.map((yamlWebhook) => {
222
+ const serverWebhook = serverWebhooksMap.get(yamlWebhook.webhookId);
223
+ if (serverWebhook && serverWebhook.versions && serverWebhook.versions.length > 0) {
224
+ // Find the active version on the server
225
+ const activeVersion = serverWebhook.versions.find((v) => v.isActive);
226
+ if (activeVersion && activeVersion.version !== yamlWebhook.version) {
227
+ const msg = `📝 Updated "${yamlWebhook.name}" webhook version in YAML: ${yamlWebhook.version} → ${activeVersion.version}`;
228
+ messages.push(msg);
229
+ console.log(msg);
230
+ yamlNeedsUpdate = true;
231
+ return {
232
+ ...yamlWebhook,
233
+ version: activeVersion.version
234
+ };
235
+ }
236
+ }
237
+ return yamlWebhook;
238
+ });
239
+ // Update YAML file if versions changed
240
+ if (yamlNeedsUpdate) {
241
+ const updatedConfig = {
242
+ ...config,
243
+ webhooks: updatedYamlWebhooks
244
+ };
245
+ await updateYamlWithWebhooks(updatedConfig);
246
+ console.log("✅ YAML webhook versions synced with server");
247
+ }
248
+ if (webhooksToDelete.length === 0 && !yamlNeedsUpdate) {
249
+ console.log("✅ Server webhooks and YAML are fully in sync");
250
+ }
251
+ }
252
+ catch (error) {
253
+ console.error("❌ Error syncing server webhooks:", error);
254
+ }
255
+ return messages;
256
+ }