diff --git a/packages/cli/src/commands/agent/agent.index.ts b/packages/cli/src/commands/agent/agent.index.ts index f2584862..64e5d166 100644 --- a/packages/cli/src/commands/agent/agent.index.ts +++ b/packages/cli/src/commands/agent/agent.index.ts @@ -11,9 +11,8 @@ import runChat from './chat.cmd'; import runSkill from './skill.cmd'; import runPrompt from './prompt.cmd'; import { startMcpServer } from './mcp.cmd'; -import fs from 'fs'; -import path from 'path'; - +import { validateJSONFile } from '../../utils/validations.utils'; +import { SRE } from '@smythos/sre'; export default class AgentCmd extends Command { static override description = 'Run .smyth agent with various execution modes'; @@ -87,7 +86,7 @@ export default class AgentCmd extends Command { async run(): Promise { const { args, flags } = await this.parse(AgentCmd); - + const sreConfigs: any = {}; // If no arguments and no flags are provided, show help if (!args.path && Object.keys(flags).length === 0) { this.log('No agent path provided, showing help...'); @@ -156,17 +155,38 @@ export default class AgentCmd extends Command { }); } } - let vaultPath; + if (flags.vault) { this.log(chalk.cyan(` • Vault mode: ${flags.vault}`)); - const formattedVaultPath = validateVaultPath(flags.vault, this); - vaultPath = formattedVaultPath; + const vaultPathValidation = validateJSONFile(flags.vault); + if (vaultPathValidation.error) { + this.log(chalk.red(` • ${vaultPathValidation.error}`)); + } else { + sreConfigs.Vault = { + Connector: 'JSONFileVault', + Settings: { + file: vaultPathValidation.path, + }, + }; + } } let modelsPath; if (flags.models) { this.log(chalk.cyan(` • Models mode: ${flags.models}`)); - modelsPath = flags.models; + const modelsPathValidation = validateJSONFile(flags.models); + if (modelsPathValidation.error) { + this.log(chalk.red(` • ${modelsPathValidation.error}`)); + } else { + modelsPath = modelsPathValidation.path; + sreConfigs.ModelsProvider = { + Connector: 'JSONModelsProvider', + Settings: { + models: modelsPath, + mode: 'merge', + }, + }; + } } if (!flags.chat && !flags.skill && !flags.endpoint && !flags.prompt) { @@ -199,11 +219,14 @@ export default class AgentCmd extends Command { mcp: flags.mcp ? parseFlagsarams(flags.mcp) : null, prompt, promptModel, - vault: vaultPath || null, models: modelsPath || null, }; this.log(chalk.gray(` Flags: ${JSON.stringify(allFlags, null, 2).replace(/\n/g, '\n ')}`)); + // Initialize SRE with the configs + SRE.init(sreConfigs); + await SRE.ready(); + if (flags.skill) { await runSkill(args, allFlags); return; @@ -239,32 +262,3 @@ function parseFlagsarams(flags: string[]) { return parsed; } -function validateVaultPath(vaultFlag: any, context: any) { - // Check if the provided path is relative or absolute - let vaultPath; - if (path.isAbsolute(vaultFlag)) { - vaultPath = vaultFlag; - } else { - // Convert relative path to absolute path - vaultPath = path.resolve(vaultFlag); - } - - // Check if the file exists - if (!fs.existsSync(vaultPath)) { - context.log(chalk.red(`Vault file not found: ${vaultPath}`)); - return; - } - - // Check if it's a valid JSON file - try { - const vaultContent = fs.readFileSync(vaultPath, 'utf8'); - JSON.parse(vaultContent); // Validate JSON - context.log(chalk.gray(` Vault path: ${vaultPath}`)); - context.log(chalk.gray(` Vault file: Valid JSON`)); - } catch (error) { - context.log(chalk.red(`Invalid JSON in vault file: ${vaultPath}`)); - return; - } - - return vaultPath; -} diff --git a/packages/cli/src/commands/agent/chat.cmd.ts b/packages/cli/src/commands/agent/chat.cmd.ts index 7849812d..1beb71d3 100644 --- a/packages/cli/src/commands/agent/chat.cmd.ts +++ b/packages/cli/src/commands/agent/chat.cmd.ts @@ -2,29 +2,8 @@ import { Agent, Chat, TLLMEvent } from '@smythos/sdk'; import chalk from 'chalk'; import readline from 'readline'; import logUpdate from 'log-update'; -import { SRE } from '@smythos/sre'; export default async function runChat(args: any, flags: any) { - const sreConfigs: any = {}; - if (flags.vault) { - sreConfigs.Vault = { - Connector: 'JSONFileVault', - Settings: { - file: flags.vault, - }, - }; - } - if (flags.models) { - sreConfigs.ModelsProvider = { - Connector: 'JSONModelsProvider', - Settings: { - models: flags.models, - mode: 'merge', - }, - }; - } - SRE.init(sreConfigs); - await SRE.ready(); const agentPath = args.path; const model = flags.chat === 'DEFAULT_MODEL' ? 'gpt-4o' : flags.chat; diff --git a/packages/cli/src/commands/agent/mcp.cmd.ts b/packages/cli/src/commands/agent/mcp.cmd.ts index 4491be8d..c9178883 100644 --- a/packages/cli/src/commands/agent/mcp.cmd.ts +++ b/packages/cli/src/commands/agent/mcp.cmd.ts @@ -1,6 +1,6 @@ import express from 'express'; import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js'; -import { ConnectorService, AgentProcess, SRE } from '@smythos/sre'; +import { ConnectorService } from '@smythos/sre'; import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import fs from 'fs'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; @@ -9,27 +9,6 @@ import { Agent } from '@smythos/sdk'; const clientTransports = new Map(); const defaultPort = 3388; export const startMcpServer = async (agentData, serverType, port, flags): Promise => { - const sreConfigs: any = {}; - if (flags.vault) { - sreConfigs.Vault = { - Connector: 'JSONFileVault', - Settings: { - file: flags.vault, - }, - }; - } - if (flags.models) { - sreConfigs.ModelsProvider = { - Connector: 'JSONModelsProvider', - Settings: { - models: flags.models, - mode: 'merge', - }, - }; - } - SRE.init(sreConfigs); - await SRE.ready(); - if (serverType.toLowerCase() === 'stdio') { await getMCPServer(agentData, serverType, null); return; diff --git a/packages/cli/src/commands/agent/prompt.cmd.ts b/packages/cli/src/commands/agent/prompt.cmd.ts index 3fda8846..85bec8a6 100644 --- a/packages/cli/src/commands/agent/prompt.cmd.ts +++ b/packages/cli/src/commands/agent/prompt.cmd.ts @@ -1,28 +1,8 @@ import { Agent } from '@smythos/sdk'; -import { SRE } from '@smythos/sre'; import ora from 'ora'; export default async function runPrompt(args: any, flags: any) { - const sreConfigs: any = {}; - if (flags.vault) { - sreConfigs.Vault = { - Connector: 'JSONFileVault', - Settings: { - file: flags.vault, - }, - }; - } - if (flags.models) { - sreConfigs.ModelsProvider = { - Connector: 'JSONModelsProvider', - Settings: { - models: flags.models, - mode: 'merge', - }, - }; - } - SRE.init(sreConfigs); - await SRE.ready(); + const agentPath = args.path; const prompt = flags.prompt; const model = flags.promptModel || 'gpt-4o'; diff --git a/packages/cli/src/commands/agent/skill.cmd.ts b/packages/cli/src/commands/agent/skill.cmd.ts index 35a59284..b38c98f0 100644 --- a/packages/cli/src/commands/agent/skill.cmd.ts +++ b/packages/cli/src/commands/agent/skill.cmd.ts @@ -1,28 +1,7 @@ import { Agent } from '@smythos/sdk'; -import { SRE } from '@smythos/sre'; import util from 'util'; export default async function runSkill(args: any, flags: any) { - const sreConfigs: any = {}; - if (flags.vault) { - sreConfigs.Vault = { - Connector: 'JSONFileVault', - Settings: { - file: flags.vault, - }, - }; - } - if (flags.models) { - sreConfigs.ModelsProvider = { - Connector: 'JSONModelsProvider', - Settings: { - models: flags.models, - mode: 'merge', - }, - }; - } - SRE.init(sreConfigs); - await SRE.ready(); const agentPath = args.path; //Importing the agent workflow diff --git a/packages/cli/src/utils/validations.utils.ts b/packages/cli/src/utils/validations.utils.ts new file mode 100644 index 00000000..99c59cd0 --- /dev/null +++ b/packages/cli/src/utils/validations.utils.ts @@ -0,0 +1,44 @@ +import path from "path"; +import fs from "fs"; + +type PathValidationResponse = { + path: string | null; + error?: string; +} + +export function validateJSONFile(filePath: string): PathValidationResponse { + + // Check if the provided path is relative or absolute + let absolutePath = null; + if (path.isAbsolute(filePath)) { + absolutePath = filePath; + } else { + // Convert relative path to absolute path + absolutePath = path.resolve(filePath); + } + + // Check if the file exists + if (!fs.existsSync(absolutePath)) { + + return { + path: null, + error: `File not found: ${absolutePath}` + }; + } + + // Check if it's a valid JSON file + try { + const fileContent = fs.readFileSync(absolutePath, 'utf8'); + JSON.parse(fileContent); // Validate JSON + } catch (error) { + return { + path: null, + error: `Invalid JSON in file: ${absolutePath}` + }; + } + + return { + path: absolutePath, + error: null + }; +} \ No newline at end of file