Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 32 additions & 38 deletions packages/cli/src/commands/agent/agent.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -87,7 +86,7 @@ export default class AgentCmd extends Command {

async run(): Promise<void> {
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...');
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
21 changes: 0 additions & 21 deletions packages/cli/src/commands/agent/chat.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 1 addition & 22 deletions packages/cli/src/commands/agent/mcp.cmd.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,27 +9,6 @@ import { Agent } from '@smythos/sdk';
const clientTransports = new Map<string, { transport: SSEServerTransport; server: Server }>();
const defaultPort = 3388;
export const startMcpServer = async (agentData, serverType, port, flags): Promise<void> => {
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;
Expand Down
22 changes: 1 addition & 21 deletions packages/cli/src/commands/agent/prompt.cmd.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
21 changes: 0 additions & 21 deletions packages/cli/src/commands/agent/skill.cmd.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
44 changes: 44 additions & 0 deletions packages/cli/src/utils/validations.utils.ts
Original file line number Diff line number Diff line change
@@ -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
};
}