lua-cli 1.3.2-alpha.3 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import * as fs from "fs";
2
2
  import * as path from "path";
3
3
  import pkg from 'js-yaml';
4
- const { load } = pkg;
4
+ const { load, dump } = pkg;
5
5
  export function copyTemplateFiles(templateDir, targetDir) {
6
6
  const files = fs.readdirSync(templateDir);
7
7
  for (const file of files) {
@@ -59,7 +59,6 @@ function updatePackageJson(srcPath, destPath) {
59
59
  fs.writeFileSync(destPath, JSON.stringify(templatePackageJson, null, 2) + '\n');
60
60
  }
61
61
  export function createSkillYaml(agentId, orgId, skillName, skillId, persona, welcomeMessage) {
62
- const skillIdSection = skillId ? ` skillId: "${skillId}"\n` : '';
63
62
  // Handle multiline strings properly for YAML
64
63
  const personaSection = persona ? ` persona: |\n${persona.split('\n').map(line => ` ${line}`).join('\n')}\n` : '';
65
64
  const welcomeMessageSection = welcomeMessage ? ` welcomeMessage: "${welcomeMessage}"\n` : '';
@@ -67,16 +66,9 @@ export function createSkillYaml(agentId, orgId, skillName, skillId, persona, wel
67
66
  agentId: "${agentId}"
68
67
  orgId: "${orgId}"
69
68
  ${personaSection}${welcomeMessageSection}
70
- skill:
71
- name: "${skillName}"
72
- version: "0.0.1"
73
- ${skillIdSection} env:
74
- # Example environment variables - customize these for your skill
75
- PINECONE_API_KEY: "pcsk_5iFtQD_xxxxx"
76
- OPENAI_API_KEY: "sk-proj--xxxx"
77
- STRIPE_SECRET_KEY: "sk_test_xxxx"
78
- # Add your own environment variables below
79
- # CUSTOM_VAR: "custom-value"`;
69
+ skills:
70
+ # Skills will be auto-generated during compilation
71
+ `;
80
72
  fs.writeFileSync("lua.skill.yaml", yamlContent);
81
73
  }
82
74
  export function readSkillYaml() {
@@ -108,18 +100,13 @@ export function updateSkillYamlPersona(persona, welcomeMessage) {
108
100
  if (welcomeMessage) {
109
101
  data.agent.welcomeMessage = welcomeMessage;
110
102
  }
111
- // Convert back to YAML with proper formatting
112
- const updatedYamlContent = `agent:
113
- agentId: "${data.agent.agentId}"
114
- orgId: "${data.agent.orgId}"
115
- persona: |
116
- ${persona.split('\n').map(line => ` ${line}`).join('\n')}
117
- welcomeMessage: "${data.agent.welcomeMessage || ''}"
118
-
119
- skill:
120
- name: "${data.skill.name}"
121
- version: "${data.skill.version}"
122
- skillId: "${data.skill.skillId || ''}"
123
- `;
103
+ // Use yaml.dump to properly handle both old and new formats
104
+ const updatedYamlContent = dump(data, {
105
+ indent: 2,
106
+ lineWidth: -1,
107
+ noRefs: true,
108
+ quotingType: '"',
109
+ forceQuotes: false
110
+ });
124
111
  fs.writeFileSync(yamlPath, updatedYamlContent);
125
112
  }
@@ -8,6 +8,13 @@ export interface ExecuteToolOptions extends SandboxOptions {
8
8
  toolCode: string;
9
9
  inputs: any;
10
10
  }
11
+ /**
12
+ * Loads environment variables from multiple sources in priority order:
13
+ * 1. process.env (lowest priority)
14
+ * 2. .env file (medium priority)
15
+ * 3. lua.skill.yaml env section (highest priority)
16
+ */
17
+ export declare function loadEnvironmentVariables(): Record<string, string>;
11
18
  /**
12
19
  * Creates a VM sandbox context with all necessary globals and utilities
13
20
  */
@@ -1,28 +1,60 @@
1
1
  import { createRequire } from "module";
2
2
  import vm from "vm";
3
3
  import path from "path";
4
+ import fs from "fs";
4
5
  import { UserDataApi } from "../services/api.js";
5
6
  import { readSkillConfig } from "./files.js";
6
7
  /**
7
- * Creates a VM sandbox context with all necessary globals and utilities
8
+ * Loads environment variables from multiple sources in priority order:
9
+ * 1. process.env (lowest priority)
10
+ * 2. .env file (medium priority)
11
+ * 3. lua.skill.yaml env section (highest priority)
8
12
  */
9
- export function createSandbox(options) {
10
- const { apiKey, agentId, customConsole, broadcastLog } = options;
11
- // Extract environment variables from YAML config and merge with process.env
12
- const config = readSkillConfig();
13
+ export function loadEnvironmentVariables() {
13
14
  const envVars = {};
14
- // Copy process.env, filtering out undefined values
15
+ // 1. Start with process.env
15
16
  for (const [key, value] of Object.entries(process.env)) {
16
17
  if (value !== undefined) {
17
18
  envVars[key] = value;
18
19
  }
19
20
  }
20
- // Override with config values
21
+ // 2. Load from .env file if it exists
22
+ const envFilePath = path.join(process.cwd(), '.env');
23
+ if (fs.existsSync(envFilePath)) {
24
+ try {
25
+ const envFileContent = fs.readFileSync(envFilePath, 'utf8');
26
+ const envLines = envFileContent.split('\n');
27
+ for (const line of envLines) {
28
+ const trimmedLine = line.trim();
29
+ if (trimmedLine && !trimmedLine.startsWith('#')) {
30
+ const [key, ...valueParts] = trimmedLine.split('=');
31
+ if (key && valueParts.length > 0) {
32
+ const value = valueParts.join('=').replace(/^["']|["']$/g, ''); // Remove quotes
33
+ envVars[key.trim()] = value;
34
+ }
35
+ }
36
+ }
37
+ }
38
+ catch (error) {
39
+ console.warn('Warning: Could not read .env file:', error);
40
+ }
41
+ }
42
+ // 3. Override with YAML config values (highest priority)
43
+ const config = readSkillConfig();
21
44
  if (config?.skill?.env) {
22
45
  for (const [key, value] of Object.entries(config.skill.env)) {
23
46
  envVars[key] = value;
24
47
  }
25
48
  }
49
+ return envVars;
50
+ }
51
+ /**
52
+ * Creates a VM sandbox context with all necessary globals and utilities
53
+ */
54
+ export function createSandbox(options) {
55
+ const { apiKey, agentId, customConsole, broadcastLog } = options;
56
+ // Load environment variables from multiple sources (same logic as test command)
57
+ const envVars = loadEnvironmentVariables();
26
58
  // Create a CommonJS context for execution
27
59
  const require = createRequire(process.cwd() + '/package.json');
28
60
  const updateUserData = async (data) => {
@@ -125,7 +157,6 @@ export function createSandbox(options) {
125
157
  clearTimeout,
126
158
  clearInterval,
127
159
  process: {
128
- ...process,
129
160
  env: envVars
130
161
  },
131
162
  global: globalThis,
@@ -188,7 +219,12 @@ const executeFunction = ${toolCode};
188
219
 
189
220
  // Export the function for testing
190
221
  module.exports = async (input) => {
222
+ try{
191
223
  return await executeFunction(input);
224
+ }catch(e){
225
+ console.error(e);
226
+ return { status: 'error', error: e.message };
227
+ }
192
228
  };
193
229
  `;
194
230
  // Execute the code in the sandbox