lua-cli 2.2.8-alpha.1 → 2.3.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.
- package/API_REFERENCE.md +1408 -0
- package/CLI_REFERENCE.md +818 -0
- package/GETTING_STARTED.md +1040 -0
- package/README.md +738 -424
- package/TEMPLATE_GUIDE.md +1398 -0
- package/dist/api/agent.api.service.d.ts +45 -0
- package/dist/api/agent.api.service.js +57 -0
- package/dist/api/auth.api.service.d.ts +48 -0
- package/dist/api/auth.api.service.js +54 -0
- package/dist/api/basket.api.service.d.ts +85 -0
- package/dist/api/basket.api.service.js +164 -0
- package/dist/api/chat.api.service.d.ts +21 -0
- package/dist/api/chat.api.service.js +24 -0
- package/dist/api/credentials.d.ts +24 -0
- package/dist/api/credentials.js +46 -0
- package/dist/api/custom.data.api.service.d.ts +69 -0
- package/dist/api/custom.data.api.service.js +125 -0
- package/dist/api/lazy-instances.d.ts +49 -0
- package/dist/api/lazy-instances.js +95 -0
- package/dist/api/order.api.service.d.ts +53 -0
- package/dist/api/order.api.service.js +95 -0
- package/dist/api/products.api.service.d.ts +66 -0
- package/dist/api/products.api.service.js +112 -0
- package/dist/api/skills.api.service.d.ts +77 -0
- package/dist/api/skills.api.service.js +88 -0
- package/dist/api/tool.api.service.d.ts +52 -0
- package/dist/api/tool.api.service.js +73 -0
- package/dist/api/user.data.api.service.d.ts +33 -0
- package/dist/api/user.data.api.service.js +59 -0
- package/dist/api-exports.d.ts +271 -0
- package/dist/api-exports.js +372 -0
- package/dist/cli/command-definitions.d.ts +30 -0
- package/dist/cli/command-definitions.js +71 -0
- package/dist/commands/agents.d.ts +20 -0
- package/dist/commands/agents.js +24 -2
- package/dist/commands/apiKey.d.ts +23 -0
- package/dist/commands/apiKey.js +23 -0
- package/dist/commands/compile.d.ts +24 -0
- package/dist/commands/compile.js +67 -759
- package/dist/commands/configure.d.ts +24 -0
- package/dist/commands/configure.js +31 -96
- package/dist/commands/deploy.d.ts +31 -19
- package/dist/commands/deploy.js +45 -74
- package/dist/commands/destroy.d.ts +27 -0
- package/dist/commands/destroy.js +27 -1
- package/dist/commands/dev.d.ts +25 -62
- package/dist/commands/dev.js +58 -873
- package/dist/commands/init.d.ts +27 -0
- package/dist/commands/init.js +98 -260
- package/dist/commands/push.d.ts +24 -21
- package/dist/commands/push.js +39 -92
- package/dist/commands/test.d.ts +26 -0
- package/dist/commands/test.js +41 -188
- package/dist/common/basket.instance.d.ts +78 -0
- package/dist/common/basket.instance.js +132 -0
- package/dist/common/data.entry.instance.d.ts +39 -0
- package/dist/common/data.entry.instance.js +76 -0
- package/dist/common/http.client.d.ts +64 -0
- package/dist/common/http.client.js +133 -0
- package/dist/common/order.instance.d.ts +40 -0
- package/dist/common/order.instance.js +79 -0
- package/dist/common/product.instance.d.ts +33 -0
- package/dist/common/product.instance.js +63 -0
- package/dist/common/product.pagination.instance.d.ts +43 -0
- package/dist/common/product.pagination.instance.js +74 -0
- package/dist/common/product.search.instance.d.ts +22 -0
- package/dist/common/product.search.instance.js +40 -0
- package/dist/common/user.instance.d.ts +41 -0
- package/dist/common/user.instance.js +84 -0
- package/dist/config/auth.constants.d.ts +11 -0
- package/dist/config/auth.constants.js +11 -0
- package/dist/config/compile.constants.d.ts +67 -0
- package/dist/config/compile.constants.js +99 -0
- package/dist/config/constants.d.ts +5 -0
- package/dist/config/constants.js +5 -0
- package/dist/config/dev.constants.d.ts +65 -0
- package/dist/config/dev.constants.js +79 -0
- package/dist/config/init.constants.d.ts +23 -0
- package/dist/config/init.constants.js +41 -0
- package/dist/index.d.ts +19 -3
- package/dist/index.js +28 -44
- package/dist/interfaces/admin.d.ts +101 -0
- package/dist/interfaces/admin.js +5 -0
- package/dist/interfaces/agent.d.ts +107 -0
- package/dist/interfaces/agent.js +5 -0
- package/dist/interfaces/baskets.d.ts +135 -0
- package/dist/interfaces/baskets.js +19 -0
- package/dist/interfaces/chat.d.ts +61 -0
- package/dist/interfaces/chat.js +5 -0
- package/dist/interfaces/common.d.ts +62 -0
- package/dist/interfaces/common.js +8 -0
- package/dist/interfaces/compile.d.ts +11 -0
- package/dist/interfaces/compile.js +4 -0
- package/dist/interfaces/custom.data.d.ts +82 -0
- package/dist/interfaces/custom.data.js +5 -0
- package/dist/interfaces/deploy.d.ts +29 -0
- package/dist/interfaces/deploy.js +4 -0
- package/dist/interfaces/dev.d.ts +53 -0
- package/dist/interfaces/dev.js +5 -0
- package/dist/interfaces/init.d.ts +60 -0
- package/dist/interfaces/init.js +4 -0
- package/dist/interfaces/orders.d.ts +91 -0
- package/dist/interfaces/orders.js +19 -0
- package/dist/interfaces/product.d.ts +65 -0
- package/dist/interfaces/product.js +5 -0
- package/dist/interfaces/push.d.ts +26 -0
- package/dist/interfaces/push.js +4 -0
- package/dist/interfaces/test.d.ts +36 -0
- package/dist/interfaces/test.js +4 -0
- package/dist/services/auth.d.ts +54 -99
- package/dist/services/auth.js +76 -12
- package/dist/types/api-contracts.d.ts +211 -0
- package/dist/types/api-contracts.js +8 -0
- package/dist/types/compile.types.d.ts +76 -0
- package/dist/types/compile.types.js +4 -0
- package/dist/types/index.d.ts +23 -85
- package/dist/types/index.js +25 -14
- package/dist/types/skill.d.ts +142 -0
- package/dist/{skill.js → types/skill.js} +66 -19
- package/dist/types/tool-validation.d.ts +34 -0
- package/dist/types/tool-validation.js +42 -0
- package/dist/utils/auth-flows.d.ts +26 -0
- package/dist/utils/auth-flows.js +141 -0
- package/dist/utils/bundling.d.ts +36 -0
- package/dist/utils/bundling.js +137 -0
- package/dist/utils/compile.d.ts +37 -0
- package/dist/utils/compile.js +242 -0
- package/dist/utils/deploy-api.d.ts +26 -0
- package/dist/utils/deploy-api.js +53 -0
- package/dist/utils/deploy-helpers.d.ts +46 -0
- package/dist/utils/deploy-helpers.js +86 -0
- package/dist/utils/deployment.d.ts +25 -0
- package/dist/utils/deployment.js +161 -0
- package/dist/utils/dev-api.d.ts +61 -0
- package/dist/utils/dev-api.js +262 -0
- package/dist/utils/dev-helpers.d.ts +46 -0
- package/dist/utils/dev-helpers.js +83 -0
- package/dist/utils/dev-server.d.ts +24 -0
- package/dist/utils/dev-server.js +555 -0
- package/dist/utils/dev-watcher.d.ts +31 -0
- package/dist/utils/dev-watcher.js +110 -0
- package/dist/utils/files.js +0 -5
- package/dist/utils/init-agent.d.ts +34 -0
- package/dist/utils/init-agent.js +129 -0
- package/dist/utils/init-helpers.d.ts +41 -0
- package/dist/utils/init-helpers.js +73 -0
- package/dist/utils/init-prompts.d.ts +47 -0
- package/dist/utils/init-prompts.js +168 -0
- package/dist/utils/push-api.d.ts +15 -0
- package/dist/utils/push-api.js +48 -0
- package/dist/utils/push-helpers.d.ts +38 -0
- package/dist/utils/push-helpers.js +84 -0
- package/dist/utils/sandbox-storage.d.ts +27 -0
- package/dist/utils/sandbox-storage.js +71 -0
- package/dist/utils/sandbox.js +78 -118
- package/dist/utils/skill-management.d.ts +14 -0
- package/dist/utils/skill-management.js +148 -0
- package/dist/utils/test-helpers.d.ts +40 -0
- package/dist/utils/test-helpers.js +92 -0
- package/dist/utils/test-prompts.d.ts +23 -0
- package/dist/utils/test-prompts.js +186 -0
- package/dist/utils/tool-detection.d.ts +18 -0
- package/dist/utils/tool-detection.js +110 -0
- package/dist/web/app.css +14 -9
- package/package.json +11 -12
- package/template/QUICKSTART.md +299 -144
- package/template/README.md +928 -349
- package/template/TOOL_EXAMPLES.md +655 -0
- package/template/package-lock.json +3781 -0
- package/template/package.json +1 -1
- package/template/src/index.ts +81 -40
- package/template/src/tools/BasketTool.ts +128 -0
- package/template/src/tools/CustomDataTool.ts +7 -13
- package/template/src/tools/OrderTool.ts +54 -0
- package/template/src/tools/PaymentTool.ts +1 -1
- package/template/src/tools/ProductsTool.ts +56 -118
- package/template/src/tools/UserDataTool.ts +4 -27
- package/dist/custom-data-api.d.ts +0 -72
- package/dist/custom-data-api.js +0 -174
- package/dist/product-api.d.ts +0 -197
- package/dist/product-api.js +0 -152
- package/dist/services/api.d.ts +0 -569
- package/dist/services/api.js +0 -625
- package/dist/skill.d.ts +0 -50
- package/dist/types.d.ts +0 -1
- package/dist/types.js +0 -2
- package/dist/user-data-api.d.ts +0 -39
- package/dist/user-data-api.js +0 -50
- package/template/API.md +0 -604
- package/template/DEVELOPER.md +0 -771
- package/template/lua.skill.yaml +0 -16
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Lua Skill System
|
|
3
|
+
* Core types and classes for building Lua AI skills
|
|
4
|
+
*/
|
|
5
|
+
import { assertValidToolName } from "./tool-validation.js";
|
|
2
6
|
import fs from "fs";
|
|
3
7
|
import path from "path";
|
|
4
8
|
import yaml from "js-yaml";
|
|
5
|
-
import dotenv from "dotenv";
|
|
6
|
-
dotenv.config();
|
|
7
9
|
/**
|
|
8
|
-
* Safe environment variable access function
|
|
9
|
-
* Gets injected at runtime with skill-specific environment variables
|
|
10
|
+
* Safe environment variable access function.
|
|
11
|
+
* Gets injected at runtime with skill-specific environment variables.
|
|
12
|
+
*
|
|
13
|
+
* Checks in order:
|
|
14
|
+
* 1. Process environment variables (.env file)
|
|
15
|
+
* 2. lua.skill.yaml configuration
|
|
10
16
|
*
|
|
11
17
|
* @param key - The environment variable key to retrieve
|
|
12
18
|
* @returns The environment variable value or undefined if not found
|
|
@@ -20,13 +26,13 @@ dotenv.config();
|
|
|
20
26
|
export const env = (key) => {
|
|
21
27
|
// Try to load environment variables from lua.skill.yaml
|
|
22
28
|
try {
|
|
29
|
+
// Check process.env first (.env file)
|
|
30
|
+
if (process.env[key]) {
|
|
31
|
+
return process.env[key];
|
|
32
|
+
}
|
|
23
33
|
// Look for lua.skill.yaml in current directory and parent directories
|
|
24
34
|
let currentDir = process.cwd();
|
|
25
35
|
let yamlPath = null;
|
|
26
|
-
//check if in .env file
|
|
27
|
-
if (dotenv.config().parsed?.[key]) {
|
|
28
|
-
return dotenv.config().parsed?.[key];
|
|
29
|
-
}
|
|
30
36
|
while (currentDir !== path.dirname(currentDir)) {
|
|
31
37
|
const potentialPath = path.join(currentDir, 'lua.skill.yaml');
|
|
32
38
|
if (fs.existsSync(potentialPath)) {
|
|
@@ -47,21 +53,40 @@ export const env = (key) => {
|
|
|
47
53
|
}
|
|
48
54
|
return undefined;
|
|
49
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Lua Skill class.
|
|
58
|
+
* Main class for building AI skills with tools.
|
|
59
|
+
*
|
|
60
|
+
* A skill is a collection of tools that the AI agent can use to accomplish tasks.
|
|
61
|
+
* Tools are functions with validated inputs and well-defined outputs.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { LuaSkill } from 'lua-cli';
|
|
66
|
+
*
|
|
67
|
+
* const skill = new LuaSkill({
|
|
68
|
+
* name: 'weather-skill',
|
|
69
|
+
* version: '1.0.0',
|
|
70
|
+
* description: "Weather and calculator utilities",
|
|
71
|
+
* context: "This skill provides weather information and math operations. " +
|
|
72
|
+
* "Use get_weather for current conditions and calculator for arithmetic.",
|
|
73
|
+
* tools: [weatherTool, calculatorTool]
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* // Or add tools after construction
|
|
77
|
+
* skill.addTool(anotherTool);
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
50
80
|
export class LuaSkill {
|
|
51
81
|
/**
|
|
52
|
-
* Creates a new LuaSkill instance
|
|
82
|
+
* Creates a new LuaSkill instance.
|
|
53
83
|
*
|
|
54
84
|
* @param config - Configuration object containing skill metadata
|
|
85
|
+
* @param config.name - Skill name (optional, defaults to 'unnamed-skill')
|
|
86
|
+
* @param config.version - Skill version (optional, defaults to '1.0.0')
|
|
55
87
|
* @param config.description - Short description of what the skill does (1-2 sentences)
|
|
56
88
|
* @param config.context - Detailed explanation of how the agent should use the tools
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* const skill = new LuaSkill({
|
|
61
|
-
* description: "Weather and calculator utilities",
|
|
62
|
-
* context: "This skill provides weather information for any city and basic mathematical operations. Use get_weather for current conditions and calculator for arithmetic operations."
|
|
63
|
-
* });
|
|
64
|
-
* ```
|
|
89
|
+
* @param config.tools - Optional array of tools to add immediately
|
|
65
90
|
*/
|
|
66
91
|
constructor(config) {
|
|
67
92
|
this.tools = [];
|
|
@@ -74,11 +99,24 @@ export class LuaSkill {
|
|
|
74
99
|
this.addTools(config.tools);
|
|
75
100
|
}
|
|
76
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Adds a single tool to the skill.
|
|
104
|
+
* Tool name is validated before being added.
|
|
105
|
+
*
|
|
106
|
+
* @param tool - Tool to add
|
|
107
|
+
* @throws Error if tool name is invalid
|
|
108
|
+
*/
|
|
77
109
|
addTool(tool) {
|
|
78
|
-
// Validate the tool name before adding it
|
|
79
110
|
assertValidToolName(tool.name);
|
|
80
111
|
this.tools.push(tool);
|
|
81
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Adds multiple tools to the skill.
|
|
115
|
+
* All tool names are validated before being added.
|
|
116
|
+
*
|
|
117
|
+
* @param tools - Array of tools to add
|
|
118
|
+
* @throws Error if any tool name is invalid
|
|
119
|
+
*/
|
|
82
120
|
addTools(tools) {
|
|
83
121
|
// Validate all tool names before adding them
|
|
84
122
|
for (const tool of tools) {
|
|
@@ -86,6 +124,15 @@ export class LuaSkill {
|
|
|
86
124
|
}
|
|
87
125
|
this.tools.push(...tools);
|
|
88
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Executes a tool by name with provided input.
|
|
129
|
+
* Input is validated against the tool's Zod schema.
|
|
130
|
+
*
|
|
131
|
+
* @param input - Input object containing tool name and parameters
|
|
132
|
+
* @param input.tool - Name of the tool to execute
|
|
133
|
+
* @returns Promise resolving to tool execution result
|
|
134
|
+
* @throws Error if tool not found or input validation fails
|
|
135
|
+
*/
|
|
89
136
|
async run(input) {
|
|
90
137
|
const tool = this.tools.find(tool => tool.name === input.tool);
|
|
91
138
|
if (!tool) {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Validation Utilities
|
|
3
|
+
* Validates tool names and other tool-related constraints
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Validates that a tool name contains only alphanumeric characters, hyphens, and underscores.
|
|
7
|
+
* No spaces or other special characters are allowed.
|
|
8
|
+
*
|
|
9
|
+
* This restriction ensures tool names:
|
|
10
|
+
* - Are compatible with all systems
|
|
11
|
+
* - Can be used in URLs and file paths
|
|
12
|
+
* - Are easy to reference in code
|
|
13
|
+
*
|
|
14
|
+
* Valid examples: "my-tool", "calculate_sum", "getTodo123"
|
|
15
|
+
* Invalid examples: "my tool", "calc@sum", "get.todo"
|
|
16
|
+
*
|
|
17
|
+
* @param name - Tool name to validate
|
|
18
|
+
* @returns True if name is valid, false otherwise
|
|
19
|
+
*/
|
|
20
|
+
export declare function validateToolName(name: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Asserts that a tool name is valid, throwing an error if not.
|
|
23
|
+
* Use this when adding tools to ensure names are valid.
|
|
24
|
+
*
|
|
25
|
+
* @param name - Tool name to validate
|
|
26
|
+
* @throws Error if tool name is invalid
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* assertValidToolName('my-tool'); // OK
|
|
31
|
+
* assertValidToolName('my tool'); // Throws error
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function assertValidToolName(name: string): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Validation Utilities
|
|
3
|
+
* Validates tool names and other tool-related constraints
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Validates that a tool name contains only alphanumeric characters, hyphens, and underscores.
|
|
7
|
+
* No spaces or other special characters are allowed.
|
|
8
|
+
*
|
|
9
|
+
* This restriction ensures tool names:
|
|
10
|
+
* - Are compatible with all systems
|
|
11
|
+
* - Can be used in URLs and file paths
|
|
12
|
+
* - Are easy to reference in code
|
|
13
|
+
*
|
|
14
|
+
* Valid examples: "my-tool", "calculate_sum", "getTodo123"
|
|
15
|
+
* Invalid examples: "my tool", "calc@sum", "get.todo"
|
|
16
|
+
*
|
|
17
|
+
* @param name - Tool name to validate
|
|
18
|
+
* @returns True if name is valid, false otherwise
|
|
19
|
+
*/
|
|
20
|
+
export function validateToolName(name) {
|
|
21
|
+
const validNameRegex = /^[a-zA-Z0-9_-]+$/;
|
|
22
|
+
return validNameRegex.test(name);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Asserts that a tool name is valid, throwing an error if not.
|
|
26
|
+
* Use this when adding tools to ensure names are valid.
|
|
27
|
+
*
|
|
28
|
+
* @param name - Tool name to validate
|
|
29
|
+
* @throws Error if tool name is invalid
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* assertValidToolName('my-tool'); // OK
|
|
34
|
+
* assertValidToolName('my tool'); // Throws error
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function assertValidToolName(name) {
|
|
38
|
+
if (!validateToolName(name)) {
|
|
39
|
+
throw new Error(`Invalid tool name "${name}". Tool names can only contain alphanumeric characters, ` +
|
|
40
|
+
`hyphens (-), and underscores (_). No spaces or other special characters are allowed.`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication Flow Utilities
|
|
3
|
+
* Handles different authentication methods (API key and email OTP)
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Handles API key authentication flow.
|
|
7
|
+
* Prompts for API key, validates it, and saves it securely.
|
|
8
|
+
*
|
|
9
|
+
* @returns Promise that resolves when authentication is complete
|
|
10
|
+
* @throws Error if API key is invalid
|
|
11
|
+
*/
|
|
12
|
+
export declare function handleApiKeyAuth(): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Handles email authentication flow with OTP verification.
|
|
15
|
+
* Sends OTP to email, verifies it, generates API key, and saves it.
|
|
16
|
+
*
|
|
17
|
+
* @returns Promise that resolves when authentication is complete
|
|
18
|
+
* @throws Error if OTP sending fails, verification fails, or API key generation fails
|
|
19
|
+
*/
|
|
20
|
+
export declare function handleEmailAuth(): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Prompts user to choose authentication method.
|
|
23
|
+
*
|
|
24
|
+
* @returns Selected authentication method ('api-key' or 'email')
|
|
25
|
+
*/
|
|
26
|
+
export declare function promptAuthMethod(): Promise<'api-key' | 'email'>;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication Flow Utilities
|
|
3
|
+
* Handles different authentication methods (API key and email OTP)
|
|
4
|
+
*/
|
|
5
|
+
import inquirer from "inquirer";
|
|
6
|
+
import { saveApiKey, checkApiKey, requestEmailOTP, verifyOTPAndGetToken, generateApiKey } from "../services/auth.js";
|
|
7
|
+
import { clearPromptLines, writeProgress, writeSuccess } from "./cli.js";
|
|
8
|
+
/**
|
|
9
|
+
* Configuration for OTP verification
|
|
10
|
+
*/
|
|
11
|
+
const OTP_CONFIG = {
|
|
12
|
+
MAX_ATTEMPTS: 3,
|
|
13
|
+
OTP_LENGTH: 6,
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Handles API key authentication flow.
|
|
17
|
+
* Prompts for API key, validates it, and saves it securely.
|
|
18
|
+
*
|
|
19
|
+
* @returns Promise that resolves when authentication is complete
|
|
20
|
+
* @throws Error if API key is invalid
|
|
21
|
+
*/
|
|
22
|
+
export async function handleApiKeyAuth() {
|
|
23
|
+
const answers = await inquirer.prompt([
|
|
24
|
+
{
|
|
25
|
+
type: "password",
|
|
26
|
+
name: "apiKey",
|
|
27
|
+
message: "Enter your API key",
|
|
28
|
+
mask: "*",
|
|
29
|
+
},
|
|
30
|
+
]);
|
|
31
|
+
const data = await checkApiKey(answers.apiKey);
|
|
32
|
+
if (!data) {
|
|
33
|
+
throw new Error("Invalid API key");
|
|
34
|
+
}
|
|
35
|
+
await saveApiKey(answers.apiKey);
|
|
36
|
+
writeSuccess("✅ API key saved securely.");
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Handles email authentication flow with OTP verification.
|
|
40
|
+
* Sends OTP to email, verifies it, generates API key, and saves it.
|
|
41
|
+
*
|
|
42
|
+
* @returns Promise that resolves when authentication is complete
|
|
43
|
+
* @throws Error if OTP sending fails, verification fails, or API key generation fails
|
|
44
|
+
*/
|
|
45
|
+
export async function handleEmailAuth() {
|
|
46
|
+
// Step 1: Collect email
|
|
47
|
+
const { email } = await inquirer.prompt([
|
|
48
|
+
{
|
|
49
|
+
type: "input",
|
|
50
|
+
name: "email",
|
|
51
|
+
message: "Enter your email address:",
|
|
52
|
+
validate: (input) => {
|
|
53
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
54
|
+
return emailRegex.test(input) || "Please enter a valid email address";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
]);
|
|
58
|
+
clearPromptLines(2);
|
|
59
|
+
// Step 2: Send OTP
|
|
60
|
+
writeProgress("📧 Sending OTP to your email...");
|
|
61
|
+
const otpSent = await requestEmailOTP(email);
|
|
62
|
+
if (!otpSent) {
|
|
63
|
+
throw new Error("Failed to send OTP. Please try again.");
|
|
64
|
+
}
|
|
65
|
+
writeProgress("✅ OTP sent successfully!");
|
|
66
|
+
// Step 3: Verify OTP with retry logic
|
|
67
|
+
const signInToken = await verifyOTPWithRetry(email);
|
|
68
|
+
// Step 4: Generate API key
|
|
69
|
+
writeProgress("🔑 Generating API key...");
|
|
70
|
+
const apiKey = await generateApiKey(signInToken);
|
|
71
|
+
if (!apiKey) {
|
|
72
|
+
throw new Error("Failed to generate API key. Please try again.");
|
|
73
|
+
}
|
|
74
|
+
// Step 5: Save API key
|
|
75
|
+
await saveApiKey(apiKey);
|
|
76
|
+
writeSuccess("✅ API key generated and saved securely.");
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Verifies OTP with retry logic.
|
|
80
|
+
* Allows multiple attempts to enter the correct OTP.
|
|
81
|
+
*
|
|
82
|
+
* @param email - Email address for OTP verification
|
|
83
|
+
* @returns Sign-in token from successful verification
|
|
84
|
+
* @throws Error if maximum attempts are exceeded
|
|
85
|
+
*/
|
|
86
|
+
async function verifyOTPWithRetry(email) {
|
|
87
|
+
let otpVerified = false;
|
|
88
|
+
let attempts = 0;
|
|
89
|
+
let signInToken = null;
|
|
90
|
+
while (!otpVerified && attempts < OTP_CONFIG.MAX_ATTEMPTS) {
|
|
91
|
+
const { pin } = await inquirer.prompt([
|
|
92
|
+
{
|
|
93
|
+
type: "input",
|
|
94
|
+
name: "pin",
|
|
95
|
+
message: "Enter the OTP code:",
|
|
96
|
+
validate: (input) => {
|
|
97
|
+
return input.length === OTP_CONFIG.OTP_LENGTH || `OTP must be ${OTP_CONFIG.OTP_LENGTH} digits`;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
]);
|
|
101
|
+
clearPromptLines(2);
|
|
102
|
+
writeProgress("🔐 Verifying OTP...");
|
|
103
|
+
signInToken = await verifyOTPAndGetToken(email, pin);
|
|
104
|
+
if (signInToken) {
|
|
105
|
+
otpVerified = true;
|
|
106
|
+
writeProgress("✅ OTP verified successfully!");
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
attempts++;
|
|
110
|
+
if (attempts < OTP_CONFIG.MAX_ATTEMPTS) {
|
|
111
|
+
console.log(`❌ Invalid OTP. ${OTP_CONFIG.MAX_ATTEMPTS - attempts} attempts remaining.`);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
throw new Error("Invalid OTP. Maximum attempts reached. Please try again later.");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (!signInToken) {
|
|
119
|
+
throw new Error("OTP verification failed");
|
|
120
|
+
}
|
|
121
|
+
return signInToken;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Prompts user to choose authentication method.
|
|
125
|
+
*
|
|
126
|
+
* @returns Selected authentication method ('api-key' or 'email')
|
|
127
|
+
*/
|
|
128
|
+
export async function promptAuthMethod() {
|
|
129
|
+
const { authMethod } = await inquirer.prompt([
|
|
130
|
+
{
|
|
131
|
+
type: "list",
|
|
132
|
+
name: "authMethod",
|
|
133
|
+
message: "Choose authentication method:",
|
|
134
|
+
choices: [
|
|
135
|
+
{ name: "API Key", value: "api-key" },
|
|
136
|
+
{ name: "Email", value: "email" }
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
]);
|
|
140
|
+
return authMethod;
|
|
141
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bundling Utilities
|
|
3
|
+
* Handles esbuild bundling of tools and main index
|
|
4
|
+
*/
|
|
5
|
+
import { ToolInfo } from '../interfaces/compile.js';
|
|
6
|
+
import { Project } from "ts-morph";
|
|
7
|
+
/**
|
|
8
|
+
* Bundles a tool's TypeScript code into a standalone JavaScript file.
|
|
9
|
+
* Uses esbuild to:
|
|
10
|
+
* - Bundle all dependencies (except external packages)
|
|
11
|
+
* - Minify code for production
|
|
12
|
+
* - Wrap for VM execution
|
|
13
|
+
*
|
|
14
|
+
* @param tool - The tool to bundle
|
|
15
|
+
* @param distDir - The distribution directory for output
|
|
16
|
+
*/
|
|
17
|
+
export declare function bundleTool(tool: ToolInfo, distDir: string): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Bundles the main index.ts file into a standalone JavaScript file.
|
|
20
|
+
* This creates the entry point for the skill.
|
|
21
|
+
*
|
|
22
|
+
* @param indexPath - Path to the index.ts file
|
|
23
|
+
* @param distDir - The distribution directory for output
|
|
24
|
+
*/
|
|
25
|
+
export declare function bundleMainIndex(indexPath: string, distDir: string): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Extracts execute code and input schema from a tool.
|
|
28
|
+
* This function:
|
|
29
|
+
* 1. Reads the bundled tool code
|
|
30
|
+
* 2. Extracts the execute function
|
|
31
|
+
* 3. Converts Zod schema to JSON Schema format
|
|
32
|
+
*
|
|
33
|
+
* @param tool - The tool to extract metadata from (mutated with executeCode and inputSchema)
|
|
34
|
+
* @param project - The ts-morph Project instance for AST analysis
|
|
35
|
+
*/
|
|
36
|
+
export declare function extractExecuteCode(tool: ToolInfo, project: Project): Promise<void>;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bundling Utilities
|
|
3
|
+
* Handles esbuild bundling of tools and main index
|
|
4
|
+
*/
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import { build } from "esbuild";
|
|
8
|
+
import { writeProgress } from "./cli.js";
|
|
9
|
+
import { COMPILE_DIRS, COMPILE_FILES, ESBUILD_TOOL_CONFIG, ESBUILD_INDEX_CONFIG, DEFAULT_INPUT_SCHEMA, } from '../config/compile.constants.js';
|
|
10
|
+
import { wrapToolForVM, createExecuteFunction, evaluateZodSchemaToJsonSchema, } from './compile.js';
|
|
11
|
+
/**
|
|
12
|
+
* Bundles a tool's TypeScript code into a standalone JavaScript file.
|
|
13
|
+
* Uses esbuild to:
|
|
14
|
+
* - Bundle all dependencies (except external packages)
|
|
15
|
+
* - Minify code for production
|
|
16
|
+
* - Wrap for VM execution
|
|
17
|
+
*
|
|
18
|
+
* @param tool - The tool to bundle
|
|
19
|
+
* @param distDir - The distribution directory for output
|
|
20
|
+
*/
|
|
21
|
+
export async function bundleTool(tool, distDir) {
|
|
22
|
+
writeProgress(`📦 Bundling ${tool.className}...`);
|
|
23
|
+
try {
|
|
24
|
+
const outputPath = path.join(distDir, COMPILE_DIRS.TOOLS, `${tool.className}.js`);
|
|
25
|
+
await build({
|
|
26
|
+
...ESBUILD_TOOL_CONFIG,
|
|
27
|
+
entryPoints: [tool.filePath],
|
|
28
|
+
outfile: outputPath,
|
|
29
|
+
});
|
|
30
|
+
// Wrap the bundled code for VM execution environment
|
|
31
|
+
await wrapToolForVM(outputPath, tool);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
console.warn(`Warning: Failed to bundle ${tool.className}:`, error);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Bundles the main index.ts file into a standalone JavaScript file.
|
|
39
|
+
* This creates the entry point for the skill.
|
|
40
|
+
*
|
|
41
|
+
* @param indexPath - Path to the index.ts file
|
|
42
|
+
* @param distDir - The distribution directory for output
|
|
43
|
+
*/
|
|
44
|
+
export async function bundleMainIndex(indexPath, distDir) {
|
|
45
|
+
writeProgress("📦 Bundling main index...");
|
|
46
|
+
try {
|
|
47
|
+
await build({
|
|
48
|
+
...ESBUILD_INDEX_CONFIG,
|
|
49
|
+
entryPoints: [indexPath],
|
|
50
|
+
outfile: path.join(distDir, COMPILE_FILES.INDEX_JS),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.warn("Warning: Failed to bundle main index:", error);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Extracts execute code and input schema from a tool.
|
|
59
|
+
* This function:
|
|
60
|
+
* 1. Reads the bundled tool code
|
|
61
|
+
* 2. Extracts the execute function
|
|
62
|
+
* 3. Converts Zod schema to JSON Schema format
|
|
63
|
+
*
|
|
64
|
+
* @param tool - The tool to extract metadata from (mutated with executeCode and inputSchema)
|
|
65
|
+
* @param project - The ts-morph Project instance for AST analysis
|
|
66
|
+
*/
|
|
67
|
+
export async function extractExecuteCode(tool, project) {
|
|
68
|
+
try {
|
|
69
|
+
const toolSourceFile = project.getSourceFile(tool.filePath);
|
|
70
|
+
if (!toolSourceFile) {
|
|
71
|
+
console.warn(`Warning: Could not find source file for ${tool.className}`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const classDecl = toolSourceFile.getClass(tool.className);
|
|
75
|
+
if (!classDecl) {
|
|
76
|
+
console.warn(`Warning: Could not find class ${tool.className} in ${tool.filePath}`);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// Verify execute method exists
|
|
80
|
+
const executeMethod = classDecl.getMethod('execute');
|
|
81
|
+
if (!executeMethod) {
|
|
82
|
+
console.warn(`Warning: Could not find execute method in ${tool.className}`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const executeBody = executeMethod.getBodyText();
|
|
86
|
+
if (!executeBody) {
|
|
87
|
+
console.warn(`Warning: Execute method has no body in ${tool.className}`);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
// Extract execute code from bundled file
|
|
91
|
+
tool.executeCode = extractExecuteCodeFromBundledFile(tool);
|
|
92
|
+
// Extract and convert input schema
|
|
93
|
+
tool.inputSchema = await extractInputSchema(classDecl, tool);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
console.warn(`Warning: Could not extract execute code for ${tool.className}:`, error);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Extracts execute code from the bundled tool file.
|
|
101
|
+
*
|
|
102
|
+
* @param tool - The tool whose bundled code to read
|
|
103
|
+
* @returns The execute function code or undefined if not found
|
|
104
|
+
*/
|
|
105
|
+
function extractExecuteCodeFromBundledFile(tool) {
|
|
106
|
+
const bundledPath = path.join(process.cwd(), COMPILE_DIRS.DIST, COMPILE_DIRS.TOOLS, `${tool.className}.js`);
|
|
107
|
+
if (fs.existsSync(bundledPath)) {
|
|
108
|
+
const bundledCode = fs.readFileSync(bundledPath, 'utf8');
|
|
109
|
+
return createExecuteFunction(bundledCode, tool);
|
|
110
|
+
}
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Extracts and converts the input schema from a tool class.
|
|
115
|
+
* Converts Zod schemas to JSON Schema format.
|
|
116
|
+
*
|
|
117
|
+
* @param classDecl - The tool class declaration from ts-morph
|
|
118
|
+
* @param tool - The tool being analyzed (for logging)
|
|
119
|
+
* @returns The JSON Schema object or default schema
|
|
120
|
+
*/
|
|
121
|
+
async function extractInputSchema(classDecl, tool) {
|
|
122
|
+
const inputSchemaProperty = classDecl.getProperty('inputSchema');
|
|
123
|
+
if (inputSchemaProperty && inputSchemaProperty.getInitializer()) {
|
|
124
|
+
const initializer = inputSchemaProperty.getInitializer();
|
|
125
|
+
if (initializer) {
|
|
126
|
+
try {
|
|
127
|
+
const zodSchemaCode = initializer.getText();
|
|
128
|
+
return await evaluateZodSchemaToJsonSchema(zodSchemaCode);
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
console.warn(`Warning: Could not parse Zod schema for ${tool.className}:`, error);
|
|
132
|
+
return DEFAULT_INPUT_SCHEMA;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return DEFAULT_INPUT_SCHEMA;
|
|
137
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for compile command
|
|
3
|
+
*/
|
|
4
|
+
import { Project } from "ts-morph";
|
|
5
|
+
import { ToolInfo } from "../interfaces/compile.js";
|
|
6
|
+
/**
|
|
7
|
+
* Compresses code using gzip and returns base64 encoded string
|
|
8
|
+
*/
|
|
9
|
+
export declare function compressCode(code: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Finds the index.ts file in the project
|
|
12
|
+
*/
|
|
13
|
+
export declare function findIndexFile(): string;
|
|
14
|
+
/**
|
|
15
|
+
* Resolves import path from module specifier
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveImportPath(moduleSpecifier: string, currentFilePath: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* Creates execute function wrapper from bundled code
|
|
20
|
+
*/
|
|
21
|
+
export declare function createExecuteFunction(bundledCode: string, tool: ToolInfo): string;
|
|
22
|
+
/**
|
|
23
|
+
* Evaluates Zod schema code and converts it to JSON Schema
|
|
24
|
+
*/
|
|
25
|
+
export declare function evaluateZodSchemaToJsonSchema(zodSchemaCode: string): Promise<any>;
|
|
26
|
+
/**
|
|
27
|
+
* Wraps bundled tool code for VM execution
|
|
28
|
+
*/
|
|
29
|
+
export declare function wrapToolForVM(outputPath: string, tool: ToolInfo): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Extracts tool information from a new expression in the AST
|
|
32
|
+
*/
|
|
33
|
+
export declare function extractToolFromNewExpressionSync(newExpr: any, project: Project): ToolInfo | null;
|
|
34
|
+
/**
|
|
35
|
+
* Extracts skills metadata from index file
|
|
36
|
+
*/
|
|
37
|
+
export declare function extractSkillsMetadata(indexFile: any): any[];
|