update: made import-example into import and it can import example workflows or user made workflows

This commit is contained in:
tusharmagar 2025-11-21 15:54:22 +05:30
parent 64a284ccc1
commit 58fa1b91c3
2 changed files with 58 additions and 17 deletions

View file

@ -37,15 +37,33 @@ yargs(hideBin(process.argv))
}
)
.command(
"import-example <example>",
"Import an example workflow by name",
(y) => y.positional("example", {
type: "string",
description: "The example to import",
}),
"import",
"Import an example workflow (--example) or custom workflow from file (--file)",
(y) => y
.option("example", {
type: "string",
description: "Name of built-in example to import",
})
.option("file", {
type: "string",
description: "Path to custom workflow JSON file",
})
.check((argv) => {
if (!argv.example && !argv.file) {
throw new Error("Either --example or --file must be provided");
}
if (argv.example && argv.file) {
throw new Error("Cannot use both --example and --file at the same time");
}
return true;
}),
async (argv) => {
try {
await importExample(String(argv.example).trim());
if (argv.example) {
await importExample(String(argv.example).trim());
} else if (argv.file) {
await importExample(undefined, String(argv.file).trim());
}
} catch (error) {
console.error("Error:", error?.message ?? error);
process.exit(1);

View file

@ -10,6 +10,7 @@ import { createInterface, Interface } from "node:readline/promises";
import { ToolCallPart } from "./application/entities/message.js";
import { Agent } from "./application/entities/agent.js";
import { McpServerConfig, McpServerDefinition } from "./application/entities/mcp.js";
import { Example } from "./application/entities/example.js";
import { z } from "zod";
import { Flavor } from "./application/entities/models.js";
import { examples } from "./examples/index.js";
@ -466,15 +467,37 @@ async function mergeMcpServers(servers: Record<string, z.infer<typeof McpServerD
return result;
}
export async function importExample(exampleName: string) {
// Validate example exists
const example = examples[exampleName];
if (!example) {
const availableExamples = Object.keys(examples);
const listMessage = availableExamples.length
? `Available examples: ${availableExamples.join(", ")}`
: "No packaged examples are available.";
throw new Error(`Unknown example '${exampleName}'. ${listMessage}`);
export async function importExample(exampleName?: string, filePath?: string) {
let example: z.infer<typeof Example>;
let sourceName: string;
if (exampleName) {
// Load from built-in examples
example = examples[exampleName];
if (!example) {
const availableExamples = Object.keys(examples);
const listMessage = availableExamples.length
? `Available examples: ${availableExamples.join(", ")}`
: "No packaged examples are available.";
throw new Error(`Unknown example '${exampleName}'. ${listMessage}`);
}
sourceName = exampleName;
} else if (filePath) {
// Load from file path
try {
const fileContent = await fsp.readFile(filePath, "utf8");
example = Example.parse(JSON.parse(fileContent));
sourceName = path.basename(filePath, ".json");
} catch (error: any) {
if (error?.code === "ENOENT") {
throw new Error(`File not found: ${filePath}`);
} else if (error?.name === "ZodError") {
throw new Error(`Invalid workflow file format: ${error.message}`);
}
throw new Error(`Failed to read workflow file: ${error.message ?? error}`);
}
} else {
throw new Error("Either exampleName or filePath must be provided");
}
// Import agents and MCP servers
@ -489,7 +512,7 @@ export async function importExample(exampleName: string) {
const entryAgent = example.entryAgent ?? importedAgents[0] ?? "";
const output = [
`✓ Imported example '${exampleName}'`,
`✓ Imported workflow '${sourceName}'`,
` Agents: ${importedAgents.join(", ")}`,
` Primary: ${entryAgent}`,
];