@aigne/cli 1.27.0 → 1.27.1-0

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/dist/cli.js CHANGED
@@ -2,6 +2,7 @@
2
2
  import { existsSync, realpathSync, statSync } from "node:fs";
3
3
  import chalk from "chalk";
4
4
  import { config } from "dotenv-flow";
5
+ import { hideBin } from "yargs/helpers";
5
6
  import { createAIGNECommand } from "./commands/aigne.js";
6
7
  config({ silent: true });
7
8
  function getAIGNEFilePath() {
@@ -16,7 +17,7 @@ function getAIGNEFilePath() {
16
17
  }
17
18
  const aigneFilePath = getAIGNEFilePath();
18
19
  createAIGNECommand({ aigneFilePath })
19
- .parseAsync(["", "", ...process.argv.slice(aigneFilePath ? 3 : 2)])
20
+ .parseAsync(hideBin([...process.argv.slice(0, 2), ...process.argv.slice(aigneFilePath ? 3 : 2)]))
20
21
  .catch((error) => {
21
22
  console.log(""); // Add an empty line for better readability
22
23
  console.error(`${chalk.red("Error:")} ${error.message}`);
@@ -1,4 +1,4 @@
1
- import { Command } from "commander";
1
+ import yargs from "yargs";
2
2
  export declare function createAIGNECommand(options?: {
3
3
  aigneFilePath?: string;
4
- }): Command;
4
+ }): yargs.Argv<{}>;
@@ -1,6 +1,7 @@
1
- import { Command } from "commander";
1
+ import yargs from "yargs";
2
2
  import { AIGNE_CLI_VERSION } from "../constants.js";
3
3
  import { asciiLogo } from "../utils/ascii-logo.js";
4
+ import { createAppCommands } from "./app.js";
4
5
  import { createConnectCommand } from "./connect.js";
5
6
  import { createCreateCommand } from "./create.js";
6
7
  import { createObservabilityCommand } from "./observe.js";
@@ -9,16 +10,20 @@ import { createServeMCPCommand } from "./serve-mcp.js";
9
10
  import { createTestCommand } from "./test.js";
10
11
  export function createAIGNECommand(options) {
11
12
  console.log(asciiLogo);
12
- return new Command()
13
- .name("aigne")
14
- .description("CLI for AIGNE framework")
13
+ return yargs()
14
+ .scriptName("aigne")
15
+ .usage("CLI for AIGNE framework")
15
16
  .version(AIGNE_CLI_VERSION)
16
- .addCommand(createRunCommand(options), { isDefault: true })
17
- .addCommand(createTestCommand(options))
18
- .addCommand(createCreateCommand())
19
- .addCommand(createServeMCPCommand(options))
20
- .addCommand(createObservabilityCommand())
21
- .addCommand(createConnectCommand())
22
- .showHelpAfterError(true)
23
- .showSuggestionAfterError(true);
17
+ .command(createRunCommand(options))
18
+ .command(createTestCommand(options))
19
+ .command(createCreateCommand())
20
+ .command(createServeMCPCommand(options))
21
+ .command(createObservabilityCommand())
22
+ .command(createConnectCommand())
23
+ .command(createAppCommands())
24
+ .help()
25
+ .alias("help", "h")
26
+ .alias("version", "v")
27
+ .showHelpOnFail(true)
28
+ .strict();
24
29
  }
@@ -0,0 +1,2 @@
1
+ import type { CommandModule } from "yargs";
2
+ export declare function createAppCommands(): CommandModule[];
@@ -0,0 +1,98 @@
1
+ import assert from "node:assert";
2
+ import { spawnSync } from "node:child_process";
3
+ import { mkdir, readFile, stat } from "node:fs/promises";
4
+ import { homedir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { AIGNE } from "@aigne/core";
7
+ import { joinURL } from "ufo";
8
+ import { ZodObject, ZodString } from "zod";
9
+ import { availableModels } from "../constants.js";
10
+ import { downloadAndExtract } from "../utils/download.js";
11
+ import { runAgentWithAIGNE } from "../utils/run-with-aigne.js";
12
+ const NPM_PACKAGE_CACHE_TIME_MS = 1000 * 60 * 60 * 24; // 1 day
13
+ export function createAppCommands() {
14
+ return [
15
+ {
16
+ command: "doc-smith",
17
+ describe: "Generate professional documents by doc-smith",
18
+ aliases: ["docsmith", "doc"],
19
+ builder: async (yargs) => {
20
+ const aigne = await loadApplication({ name: "doc-smith" });
21
+ for (const agent of aigne.agents) {
22
+ yargs.command(agent.name, agent.description || "", (yargs) => {
23
+ const options = Object.entries(agent.inputSchema instanceof ZodObject ? agent.inputSchema.shape : {});
24
+ for (const [option, config] of options) {
25
+ yargs.option(option, {
26
+ // TODO: support more types
27
+ type: config instanceof ZodString ? "string" : "string",
28
+ description: config.description,
29
+ });
30
+ if (!(config.isNullable() || config.isOptional())) {
31
+ yargs.demandOption(option);
32
+ }
33
+ }
34
+ }, async (argv) => {
35
+ try {
36
+ await runAgentWithAIGNE(aigne, agent, { input: argv });
37
+ }
38
+ finally {
39
+ await aigne.shutdown();
40
+ }
41
+ });
42
+ }
43
+ yargs.version('hello world');
44
+ return yargs.demandCommand();
45
+ },
46
+ handler: () => { },
47
+ },
48
+ ];
49
+ }
50
+ async function loadApplication({ name }) {
51
+ const info = await shouldDownloadNewVersion(name);
52
+ if (info.shouldDownload) {
53
+ assert(info.url, "Package URL should be defined when downloading");
54
+ // TODO: clean up old versions
55
+ await mkdir(info.dir, { recursive: true });
56
+ await downloadAndExtract(info.url.toString(), info.dir, { strip: 1 });
57
+ spawnSync("npm", ["install", "--omit", "dev"], { cwd: info.dir, stdio: "inherit" });
58
+ }
59
+ return AIGNE.load(info.dir, { models: availableModels() });
60
+ }
61
+ async function shouldDownloadNewVersion(name, { cacheTimeMs = NPM_PACKAGE_CACHE_TIME_MS } = {}) {
62
+ const nameWithOrg = `@aigne/${name}`;
63
+ const dir = join(homedir(), ".aigne", "registry.npmjs.org", nameWithOrg);
64
+ const s = await stat(join(dir, "package.json")).catch((error) => {
65
+ if (error.code === "ENOENT") {
66
+ return null;
67
+ }
68
+ throw error;
69
+ });
70
+ if (!s)
71
+ return {
72
+ shouldDownload: true,
73
+ dir,
74
+ url: await getNpmTgzInfo(nameWithOrg).then((info) => info.tarballUrl),
75
+ };
76
+ const lastModified = s.mtimeMs;
77
+ const now = Date.now();
78
+ if (now - lastModified > cacheTimeMs) {
79
+ const version = JSON.parse(await readFile(join(dir, "package.json"), "utf-8")).version;
80
+ const latest = await getNpmTgzInfo(`@aigne/${name}`);
81
+ if (version !== latest.version) {
82
+ return { shouldDownload: true, url: latest.tarballUrl, dir };
83
+ }
84
+ }
85
+ return { shouldDownload: false, dir };
86
+ }
87
+ async function getNpmTgzInfo(name) {
88
+ const res = await fetch(joinURL("https://registry.npmjs.org", name));
89
+ if (!res.ok)
90
+ throw new Error(`Failed to fetch package info for ${name}: ${res.statusText}`);
91
+ const data = await res.json();
92
+ const latestVersion = data["dist-tags"].latest;
93
+ const tarballUrl = data.versions[latestVersion].dist.tarball;
94
+ return {
95
+ version: latestVersion,
96
+ tarballUrl,
97
+ };
98
+ }
@@ -1,2 +1,6 @@
1
- import { Command } from "commander";
2
- export declare function createConnectCommand(): Command;
1
+ import type { CommandModule } from "yargs";
2
+ interface ConnectOptions {
3
+ url?: string;
4
+ }
5
+ export declare function createConnectCommand(): CommandModule<{}, ConnectOptions>;
6
+ export {};
@@ -1,7 +1,6 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import { readFile } from "node:fs/promises";
3
3
  import chalk from "chalk";
4
- import { Command } from "commander";
5
4
  import { parse } from "yaml";
6
5
  import { getUserInfo } from "../utils/aigne-hub-user.js";
7
6
  import { AIGNE_ENV_FILE, connectToAIGNEHub } from "../utils/load-aigne.js";
@@ -57,30 +56,36 @@ async function displayStatus(statusList) {
57
56
  }
58
57
  }
59
58
  export function createConnectCommand() {
60
- const connectCommand = new Command("connect")
61
- .description("Manage AIGNE Hub connections")
62
- .showHelpAfterError(true)
63
- .showSuggestionAfterError(true);
64
- connectCommand
65
- .command("status")
66
- .description("Show current connection status")
67
- .action(async () => {
68
- const statusList = await getConnectionStatus();
69
- await displayStatus(statusList);
70
- });
71
- connectCommand
72
- .option("--url <url>", "URL to the AIGNE Hub server")
73
- .action(async (options) => {
74
- const url = options.url || "https://hub.aigne.io/";
75
- console.log(chalk.blue(`Connecting to AIGNE Hub: ${url}`));
76
- try {
77
- await connectToAIGNEHub(url);
78
- console.log(chalk.green("✓ Successfully connected to AIGNE Hub"));
79
- }
80
- catch (error) {
81
- console.error(chalk.red("✗ Failed to connect to AIGNE Hub:"), error.message);
82
- process.exit(1);
83
- }
84
- });
85
- return connectCommand;
59
+ return {
60
+ command: "connect [url]",
61
+ describe: "Manage AIGNE Hub connections",
62
+ builder: (yargs) => {
63
+ return yargs
64
+ .positional("url", {
65
+ describe: "URL to the AIGNE Hub server",
66
+ type: "string",
67
+ default: "https://hub.aigne.io/",
68
+ })
69
+ .command({
70
+ command: "status",
71
+ describe: "Show current connection status",
72
+ handler: async () => {
73
+ const statusList = await getConnectionStatus();
74
+ await displayStatus(statusList);
75
+ },
76
+ });
77
+ },
78
+ handler: async (argv) => {
79
+ const url = argv.url || "https://hub.aigne.io/";
80
+ console.log(chalk.blue(`Connecting to AIGNE Hub: ${url}`));
81
+ try {
82
+ await connectToAIGNEHub(url);
83
+ console.log(chalk.green("✓ Successfully connected to AIGNE Hub"));
84
+ }
85
+ catch (error) {
86
+ console.error(chalk.red("✗ Failed to connect to AIGNE Hub:"), error.message);
87
+ process.exit(1);
88
+ }
89
+ },
90
+ };
86
91
  }
@@ -1,2 +1,6 @@
1
- import { Command } from "commander";
2
- export declare function createCreateCommand(): Command;
1
+ import type { CommandModule } from "yargs";
2
+ interface CreateOptions {
3
+ path: string;
4
+ }
5
+ export declare function createCreateCommand(): CommandModule<{}, CreateOptions>;
6
+ export {};
@@ -1,69 +1,73 @@
1
1
  import { existsSync, mkdirSync, readdirSync } from "node:fs";
2
2
  import { cp } from "node:fs/promises";
3
3
  import { isAbsolute, join, relative, resolve } from "node:path";
4
- import { Command } from "commander";
5
4
  import inquirer from "inquirer";
6
5
  export function createCreateCommand() {
7
- return new Command("create")
8
- .description("Create a new aigne project with agent config files")
9
- .argument("[path]", "Path to create the project directory", ".")
10
- .action(async (_path) => {
11
- let path = _path;
12
- if (path === ".") {
13
- const answers = await inquirer.prompt([
14
- {
15
- type: "input",
16
- name: "projectName",
17
- message: "Project name:",
18
- default: _path !== "." ? _path : "my-aigne-project",
19
- validate: (input) => {
20
- if (input.trim() === "")
21
- return "Project name cannot be empty.";
22
- return true;
6
+ return {
7
+ command: "create [path]",
8
+ describe: "Create a new aigne project with agent config files",
9
+ builder: (yargs) => {
10
+ return yargs.positional("path", {
11
+ describe: "Path to create the project directory",
12
+ type: "string",
13
+ default: ".",
14
+ });
15
+ },
16
+ handler: async ({ path }) => {
17
+ if (path === ".") {
18
+ const answers = await inquirer.prompt([
19
+ {
20
+ type: "input",
21
+ name: "projectName",
22
+ message: "Project name:",
23
+ default: path !== "." ? path : "my-aigne-project",
24
+ validate: (input) => {
25
+ if (input.trim() === "")
26
+ return "Project name cannot be empty.";
27
+ return true;
28
+ },
23
29
  },
24
- },
25
- ]);
26
- path = answers.projectName;
27
- }
28
- path = isAbsolute(path) ? path : resolve(process.cwd(), path);
29
- const isPathNotEmpty = existsSync(path) && readdirSync(path).length > 0;
30
- if (isPathNotEmpty) {
31
- const answers = await inquirer.prompt([
30
+ ]);
31
+ path = answers.projectName;
32
+ }
33
+ path = isAbsolute(path) ? path : resolve(process.cwd(), path);
34
+ const isPathNotEmpty = existsSync(path) && readdirSync(path).length > 0;
35
+ if (isPathNotEmpty) {
36
+ const answers = await inquirer.prompt([
37
+ {
38
+ type: "confirm",
39
+ name: "overwrite",
40
+ message: `The directory "${path}" is not empty. Do you want to remove its contents?`,
41
+ default: false,
42
+ },
43
+ ]);
44
+ if (!answers.overwrite) {
45
+ console.log("Operation cancelled.");
46
+ process.exit(0);
47
+ }
48
+ }
49
+ const templates = [{ name: "default" }];
50
+ const { template } = await inquirer.prompt([
32
51
  {
33
- type: "confirm",
34
- name: "overwrite",
35
- message: `The directory "${path}" is not empty. Do you want to remove its contents?`,
36
- default: false,
52
+ type: "list",
53
+ name: "template",
54
+ message: "Select a template:",
55
+ choices: templates.map((t) => t.name),
56
+ default: "default",
37
57
  },
38
58
  ]);
39
- if (!answers.overwrite) {
40
- console.log("Operation cancelled.");
41
- process.exit(0);
59
+ mkdirSync(path, { recursive: true });
60
+ const templatePath = join(import.meta.dirname, "../../templates", template);
61
+ if (!existsSync(templatePath))
62
+ throw new Error(`Template "${template}" not found.`);
63
+ const files = readdirSync(templatePath);
64
+ for (const file of files) {
65
+ const source = join(templatePath, file);
66
+ const destination = join(path, file);
67
+ await cp(source, destination, { recursive: true, force: true });
42
68
  }
43
- }
44
- const templates = [{ name: "default" }];
45
- const { template } = await inquirer.prompt([
46
- {
47
- type: "list",
48
- name: "template",
49
- message: "Select a template:",
50
- choices: templates.map((t) => t.name),
51
- default: "default",
52
- },
53
- ]);
54
- mkdirSync(path, { recursive: true });
55
- const templatePath = join(import.meta.dirname, "../../templates", template);
56
- if (!existsSync(templatePath))
57
- throw new Error(`Template "${template}" not found.`);
58
- const files = readdirSync(templatePath);
59
- for (const file of files) {
60
- const source = join(templatePath, file);
61
- const destination = join(path, file);
62
- await cp(source, destination, { recursive: true, force: true });
63
- }
64
- console.log("\n✅ AIGNE project created successfully!");
65
- console.log(`\nTo use your new agent, run:\n cd ${relative(process.cwd(), path)} && aigne run`);
66
- })
67
- .showHelpAfterError(true)
68
- .showSuggestionAfterError(true);
69
+ console.log("\n✅ AIGNE project created successfully!");
70
+ console.log(`\nTo use your new agent, run:\n cd ${relative(process.cwd(), path)} && aigne run`);
71
+ },
72
+ };
69
73
  }
@@ -1,2 +1,7 @@
1
- import { Command } from "commander";
2
- export declare function createObservabilityCommand(): Command;
1
+ import type { CommandModule } from "yargs";
2
+ interface ServeMCPOptions {
3
+ host: string;
4
+ port?: number;
5
+ }
6
+ export declare function createObservabilityCommand(): CommandModule<{}, ServeMCPOptions>;
7
+ export {};
@@ -2,7 +2,6 @@ import { tryOrThrow } from "@aigne/core/utils/type-utils.js";
2
2
  import { startObservabilityCLIServer } from "@aigne/observability-api/cli";
3
3
  import getObservabilityDbPath from "@aigne/observability-api/db-path";
4
4
  import chalk from "chalk";
5
- import { Command } from "commander";
6
5
  import detectPort from "detect-port";
7
6
  const DEFAULT_PORT = () => tryOrThrow(() => {
8
7
  const { PORT } = process.env;
@@ -14,16 +13,26 @@ const DEFAULT_PORT = () => tryOrThrow(() => {
14
13
  return port;
15
14
  }, (error) => new Error(`parse PORT error ${error.message}`));
16
15
  export function createObservabilityCommand() {
17
- return new Command("observe")
18
- .description("Start the observability server")
19
- .option("--host <host>", "Host to run the observability server on, use 0.0.0.0 to publicly expose the server", "localhost")
20
- .option("--port <port>", "Port to run the observability server on", (s) => Number.parseInt(s))
21
- .action(async (options) => {
22
- const port = await detectPort(options.port || DEFAULT_PORT());
23
- const dbUrl = getObservabilityDbPath();
24
- console.log("Observability database path:", chalk.greenBright(dbUrl));
25
- await startObservabilityCLIServer({ port, dbUrl });
26
- })
27
- .showHelpAfterError(true)
28
- .showSuggestionAfterError(true);
16
+ return {
17
+ command: "observe",
18
+ describe: "Start the observability server",
19
+ builder: (yargs) => {
20
+ return yargs
21
+ .option("host", {
22
+ type: "string",
23
+ describe: "Host to run the observability server on, use 0.0.0.0 to publicly expose the server",
24
+ default: "localhost",
25
+ })
26
+ .option("port", {
27
+ type: "number",
28
+ describe: "Port to run the observability server on",
29
+ });
30
+ },
31
+ handler: async (options) => {
32
+ const port = await detectPort(options.port || DEFAULT_PORT());
33
+ const dbUrl = getObservabilityDbPath();
34
+ console.log("Observability database path:", chalk.greenBright(dbUrl));
35
+ await startObservabilityCLIServer({ port, dbUrl });
36
+ },
37
+ };
29
38
  }
@@ -1,4 +1,4 @@
1
- import type { Command } from "commander";
2
- export declare function createRunCommand({ aigneFilePath }?: {
1
+ import type { CommandModule } from "yargs";
2
+ export declare function createRunCommand({ aigneFilePath, }?: {
3
3
  aigneFilePath?: string;
4
- }): Command;
4
+ }): CommandModule;
@@ -12,94 +12,109 @@ import { isV1Package, toAIGNEPackage } from "../utils/agent-v1.js";
12
12
  import { downloadAndExtract } from "../utils/download.js";
13
13
  import { loadAIGNE } from "../utils/load-aigne.js";
14
14
  import { createRunAIGNECommand, parseAgentInputByCommander, runAgentWithAIGNE, } from "../utils/run-with-aigne.js";
15
- export function createRunCommand({ aigneFilePath } = {}) {
16
- return createRunAIGNECommand()
17
- .description("Run AIGNE from the specified agent")
18
- .option("--url, --path <path_or_url>", "Path to the agents directory or URL to aigne project", ".")
19
- .option("--entry-agent <entry-agent>", "Name of the agent to run (defaults to the first agent found)")
20
- .option("--cache-dir <dir>", "Directory to download the package to (defaults to the ~/.aigne/xxx)")
21
- .action(async (options) => {
22
- const path = aigneFilePath || options.path;
23
- if (options.logLevel)
24
- logger.level = options.logLevel;
25
- const { cacheDir, dir } = prepareDirs(path, options);
26
- const { aigne, agent } = await new Listr([
27
- {
28
- title: "Prepare environment",
29
- task: (_, task) => {
30
- if (cacheDir) {
31
- return task.newListr([
32
- {
33
- title: "Download package",
34
- task: () => downloadPackage(path, cacheDir),
35
- },
36
- {
37
- title: "Extract package",
38
- task: () => extractPackage(cacheDir, dir),
39
- },
40
- ]);
41
- }
15
+ export function createRunCommand({ aigneFilePath, } = {}) {
16
+ return {
17
+ command: "run [path]",
18
+ describe: "Run AIGNE from the specified agent",
19
+ builder: (yargs) => {
20
+ return createRunAIGNECommand(yargs)
21
+ .positional("path", {
22
+ describe: "Path to the agents directory or URL to aigne project",
23
+ type: "string",
24
+ default: ".",
25
+ alias: ["url"],
26
+ })
27
+ .option("entry-agent", {
28
+ describe: "Name of the agent to run (defaults to the first agent found)",
29
+ type: "string",
30
+ })
31
+ .option("cache-dir", {
32
+ describe: "Directory to download the package to (defaults to the ~/.aigne/xxx)",
33
+ type: "string",
34
+ });
35
+ },
36
+ handler: async (argv) => {
37
+ const options = argv;
38
+ const path = aigneFilePath || options.path;
39
+ if (options.logLevel)
40
+ logger.level = options.logLevel;
41
+ const { cacheDir, dir } = prepareDirs(path, options);
42
+ const { aigne, agent } = await new Listr([
43
+ {
44
+ title: "Prepare environment",
45
+ task: (_, task) => {
46
+ if (cacheDir) {
47
+ return task.newListr([
48
+ {
49
+ title: "Download package",
50
+ task: () => downloadPackage(path, cacheDir),
51
+ },
52
+ {
53
+ title: "Extract package",
54
+ task: () => extractPackage(cacheDir, dir),
55
+ },
56
+ ]);
57
+ }
58
+ },
42
59
  },
43
- },
44
- {
45
- title: "Initialize AIGNE",
46
- task: async (ctx, task) => {
47
- // Load env files in the aigne directory
48
- config({ path: dir, silent: true });
49
- const aigne = await loadAIGNE(dir, { ...options, model: options.model || process.env.MODEL }, {
50
- inquirerPromptFn: (prompt) => {
51
- return task
52
- .prompt(ListrInquirerPromptAdapter)
53
- .run(select, prompt)
54
- .then((res) => ({ [prompt.name]: res }));
55
- },
56
- });
57
- ctx.aigne = aigne;
60
+ {
61
+ title: "Initialize AIGNE",
62
+ task: async (ctx, task) => {
63
+ // Load env files in the aigne directory
64
+ config({ path: dir, silent: true });
65
+ const aigne = await loadAIGNE(dir, { ...options, model: options.model || process.env.MODEL }, {
66
+ inquirerPromptFn: (prompt) => {
67
+ return task
68
+ .prompt(ListrInquirerPromptAdapter)
69
+ .run(select, prompt)
70
+ .then((res) => ({ [prompt.name]: res }));
71
+ },
72
+ });
73
+ ctx.aigne = aigne;
74
+ },
58
75
  },
59
- },
60
- {
61
- task: (ctx) => {
62
- const { aigne } = ctx;
63
- assert(aigne);
64
- let entryAgent;
65
- if (options.entryAgent) {
66
- entryAgent = aigne.agents[options.entryAgent];
67
- if (!entryAgent) {
68
- throw new Error(`\
76
+ {
77
+ task: (ctx) => {
78
+ const { aigne } = ctx;
79
+ assert(aigne);
80
+ let entryAgent;
81
+ if (options.entryAgent) {
82
+ entryAgent = aigne.agents[options.entryAgent];
83
+ if (!entryAgent) {
84
+ throw new Error(`\
69
85
  Agent "${options.entryAgent}" not found in ${aigne.rootDir}
70
86
 
71
87
  Available agents:
72
88
  ${aigne.agents.map((agent) => ` - ${agent.name}`).join("\n")}
73
89
  `);
90
+ }
91
+ }
92
+ else {
93
+ entryAgent = aigne.agents[0];
94
+ if (!entryAgent)
95
+ throw new Error(`No any agent found in ${aigne.rootDir}`);
74
96
  }
75
- }
76
- else {
77
- entryAgent = aigne.agents[0];
78
- if (!entryAgent)
79
- throw new Error(`No any agent found in ${aigne.rootDir}`);
80
- }
81
- ctx.agent = entryAgent;
97
+ ctx.agent = entryAgent;
98
+ },
99
+ },
100
+ ], {
101
+ rendererOptions: {
102
+ collapseSubtasks: false,
103
+ showErrorMessage: false,
104
+ timer: PRESET_TIMER,
82
105
  },
83
- },
84
- ], {
85
- rendererOptions: {
86
- collapseSubtasks: false,
87
- showErrorMessage: false,
88
- timer: PRESET_TIMER,
89
- },
90
- }).run();
91
- assert(aigne);
92
- assert(agent);
93
- const input = await parseAgentInputByCommander(agent, options);
94
- try {
95
- await runAgentWithAIGNE(aigne, agent, { ...options, input });
96
- }
97
- finally {
98
- await aigne.shutdown();
99
- }
100
- })
101
- .showHelpAfterError(true)
102
- .showSuggestionAfterError(true);
106
+ }).run();
107
+ assert(aigne);
108
+ assert(agent);
109
+ const input = await parseAgentInputByCommander(agent, options);
110
+ try {
111
+ await runAgentWithAIGNE(aigne, agent, { ...options, input });
112
+ }
113
+ finally {
114
+ await aigne.shutdown();
115
+ }
116
+ },
117
+ };
103
118
  }
104
119
  async function downloadPackage(url, cacheDir) {
105
120
  await rm(cacheDir, { recursive: true, force: true });
@@ -1,4 +1,11 @@
1
- import { Command } from "commander";
2
- export declare function createServeMCPCommand({ aigneFilePath }?: {
1
+ import type { CommandModule } from "yargs";
2
+ interface ServeMCPOptions {
3
+ path: string;
4
+ host: string;
5
+ port?: number;
6
+ pathname: string;
7
+ }
8
+ export declare function createServeMCPCommand({ aigneFilePath, }?: {
3
9
  aigneFilePath?: string;
4
- }): Command;
10
+ }): CommandModule<{}, ServeMCPOptions>;
11
+ export {};
@@ -1,6 +1,5 @@
1
1
  import { isAbsolute, resolve } from "node:path";
2
2
  import { tryOrThrow } from "@aigne/core/utils/type-utils.js";
3
- import { Command } from "commander";
4
3
  import { loadAIGNE } from "../utils/load-aigne.js";
5
4
  import { serveMCPServer } from "../utils/serve-mcp.js";
6
5
  const DEFAULT_PORT = () => tryOrThrow(() => {
@@ -12,26 +11,45 @@ const DEFAULT_PORT = () => tryOrThrow(() => {
12
11
  throw new Error(`Invalid PORT: ${PORT}`);
13
12
  return port;
14
13
  }, (error) => new Error(`parse PORT error ${error.message}`));
15
- export function createServeMCPCommand({ aigneFilePath } = {}) {
16
- return new Command("serve-mcp")
17
- .description("Serve the agents in the specified directory as a MCP server (streamable http)")
18
- .option("--url, --path <path_or_url>", "Path to the agents directory or URL to aigne project", ".")
19
- .option("--host <host>", "Host to run the MCP server on, use 0.0.0.0 to publicly expose the server", "localhost")
20
- .option("--port <port>", "Port to run the MCP server on", (s) => Number.parseInt(s))
21
- .option("--pathname <pathname>", "Pathname to the service", "/mcp")
22
- .action(async (options) => {
23
- const path = aigneFilePath || options.path;
24
- const absolutePath = isAbsolute(path) ? path : resolve(process.cwd(), path);
25
- const port = options.port || DEFAULT_PORT();
26
- const aigne = await loadAIGNE(absolutePath);
27
- await serveMCPServer({
28
- aigne,
29
- host: options.host,
30
- port,
31
- pathname: options.pathname,
32
- });
33
- console.log(`MCP server is running on http://${options.host}:${port}${options.pathname}`);
34
- })
35
- .showHelpAfterError(true)
36
- .showSuggestionAfterError(true);
14
+ export function createServeMCPCommand({ aigneFilePath, } = {}) {
15
+ return {
16
+ command: "serve-mcp",
17
+ describe: "Serve the agents in the specified directory as a MCP server (streamable http)",
18
+ builder: (yargs) => {
19
+ return yargs
20
+ .option("path", {
21
+ describe: "Path to the agents directory or URL to aigne project",
22
+ type: "string",
23
+ default: ".",
24
+ alias: ["url"],
25
+ })
26
+ .option("host", {
27
+ describe: "Host to run the MCP server on, use 0.0.0.0 to publicly expose the server",
28
+ type: "string",
29
+ default: "localhost",
30
+ })
31
+ .option("port", {
32
+ describe: "Port to run the MCP server on",
33
+ type: "number",
34
+ })
35
+ .option("pathname", {
36
+ describe: "Pathname to the service",
37
+ type: "string",
38
+ default: "/mcp",
39
+ });
40
+ },
41
+ handler: async (options) => {
42
+ const path = aigneFilePath || options.path;
43
+ const absolutePath = isAbsolute(path) ? path : resolve(process.cwd(), path);
44
+ const port = options.port || DEFAULT_PORT();
45
+ const aigne = await loadAIGNE(absolutePath);
46
+ await serveMCPServer({
47
+ aigne,
48
+ host: options.host,
49
+ port,
50
+ pathname: options.pathname,
51
+ });
52
+ console.log(`MCP server is running on http://${options.host}:${port}${options.pathname}`);
53
+ },
54
+ };
37
55
  }
@@ -1,4 +1,8 @@
1
- import { Command } from "commander";
2
- export declare function createTestCommand({ aigneFilePath }?: {
1
+ import type { CommandModule } from "yargs";
2
+ interface TestOptions {
3
+ path: string;
4
+ }
5
+ export declare function createTestCommand({ aigneFilePath, }?: {
3
6
  aigneFilePath?: string;
4
- }): Command;
7
+ }): CommandModule<{}, TestOptions>;
8
+ export {};
@@ -1,19 +1,25 @@
1
1
  import assert from "node:assert";
2
2
  import { spawnSync } from "node:child_process";
3
3
  import { isAbsolute, resolve } from "node:path";
4
- import { Command } from "commander";
5
4
  import { loadAIGNE } from "../utils/load-aigne.js";
6
- export function createTestCommand({ aigneFilePath } = {}) {
7
- return new Command("test")
8
- .description("Run tests in the specified agents directory")
9
- .option("--url, --path <path_or_url>", "Path to the agents directory or URL to aigne project", ".")
10
- .action(async (options) => {
11
- const path = aigneFilePath || options.path;
12
- const absolutePath = isAbsolute(path) ? path : resolve(process.cwd(), path);
13
- const aigne = await loadAIGNE(absolutePath);
14
- assert(aigne.rootDir);
15
- spawnSync("node", ["--test"], { cwd: aigne.rootDir, stdio: "inherit" });
16
- })
17
- .showHelpAfterError(true)
18
- .showSuggestionAfterError(true);
5
+ export function createTestCommand({ aigneFilePath, } = {}) {
6
+ return {
7
+ command: "test",
8
+ describe: "Run tests in the specified agents directory",
9
+ builder: (yargs) => {
10
+ return yargs.option("path", {
11
+ describe: "Path to the agents directory or URL to aigne project",
12
+ type: "string",
13
+ default: ".",
14
+ alias: ["url"],
15
+ });
16
+ },
17
+ handler: async (options) => {
18
+ const path = aigneFilePath || options.path;
19
+ const absolutePath = isAbsolute(path) ? path : resolve(process.cwd(), path);
20
+ const aigne = await loadAIGNE(absolutePath);
21
+ assert(aigne.rootDir);
22
+ spawnSync("node", ["--test"], { cwd: aigne.rootDir, stdio: "inherit" });
23
+ },
24
+ };
19
25
  }
@@ -1 +1,3 @@
1
- export declare function downloadAndExtract(url: string, dir: string): Promise<void>;
1
+ export declare function downloadAndExtract(url: string, dir: string, options?: {
2
+ strip?: number;
3
+ }): Promise<void>;
@@ -1,7 +1,7 @@
1
1
  import { Readable } from "node:stream";
2
2
  import { finished } from "node:stream/promises";
3
3
  import { x } from "tar";
4
- export async function downloadAndExtract(url, dir) {
4
+ export async function downloadAndExtract(url, dir, options = {}) {
5
5
  const response = await fetch(url).catch((error) => {
6
6
  throw new Error(`Failed to download package from ${url}: ${error.message}`);
7
7
  });
@@ -12,7 +12,7 @@ export async function downloadAndExtract(url, dir) {
12
12
  throw new Error(`Failed to download package from ${url}: Unexpected to get empty response`);
13
13
  }
14
14
  try {
15
- await finished(Readable.fromWeb(response.body).pipe(x({ C: dir })));
15
+ await finished(Readable.fromWeb(response.body).pipe(x({ C: dir, ...options })));
16
16
  }
17
17
  catch (error) {
18
18
  error.message = `Failed to extract package from ${url}: ${error.message}`;
@@ -1,7 +1,7 @@
1
1
  import { type Agent, AIGNE, type ChatModelOptions, type Message } from "@aigne/core";
2
2
  import { LogLevel } from "@aigne/core/utils/logger.js";
3
3
  import { type PromiseOrValue } from "@aigne/core/utils/type-utils.js";
4
- import { Command } from "commander";
4
+ import type { Argv } from "yargs";
5
5
  import { type ChatLoopOptions } from "./run-chat-loop.js";
6
6
  export interface RunAIGNECommandOptions {
7
7
  chat?: boolean;
@@ -17,7 +17,31 @@ export interface RunAIGNECommandOptions {
17
17
  logLevel?: LogLevel;
18
18
  force?: boolean;
19
19
  }
20
- export declare const createRunAIGNECommand: (name?: string) => Command;
20
+ export declare const createRunAIGNECommand: (yargs: Argv) => Argv<{
21
+ chat: boolean;
22
+ } & {
23
+ model: string | undefined;
24
+ } & {
25
+ temperature: number | undefined;
26
+ } & {
27
+ "top-p": number | undefined;
28
+ } & {
29
+ "presence-penalty": number | undefined;
30
+ } & {
31
+ "frequency-penalty": number | undefined;
32
+ } & {
33
+ input: (string | number)[] | undefined;
34
+ } & {
35
+ format: string | undefined;
36
+ } & {
37
+ output: string | undefined;
38
+ } & {
39
+ "output-key": string;
40
+ } & {
41
+ force: boolean;
42
+ } & {
43
+ "log-level": LogLevel;
44
+ }>;
21
45
  export declare function parseAgentInputByCommander(agent: Agent, options?: RunAIGNECommandOptions & {
22
46
  inputKey?: string;
23
47
  argv?: string[];
@@ -4,67 +4,90 @@ import { dirname, isAbsolute, join } from "node:path";
4
4
  import { isatty } from "node:tty";
5
5
  import { promisify } from "node:util";
6
6
  import { exists } from "@aigne/agent-library/utils/fs.js";
7
- import { AIGNE, DEFAULT_OUTPUT_KEY, readAllString, UserAgent, } from "@aigne/core";
7
+ import { AIAgent, AIGNE, DEFAULT_OUTPUT_KEY, readAllString, UserAgent, } from "@aigne/core";
8
8
  import { loadModel } from "@aigne/core/loader/index.js";
9
9
  import { getLevelFromEnv, LogLevel, logger } from "@aigne/core/utils/logger.js";
10
- import { isEmpty, isNonNullable, tryOrThrow, } from "@aigne/core/utils/type-utils.js";
10
+ import { flat, isEmpty, tryOrThrow, } from "@aigne/core/utils/type-utils.js";
11
11
  import chalk from "chalk";
12
- import { Command } from "commander";
13
12
  import { parse } from "yaml";
13
+ import yargs from "yargs";
14
14
  import { ZodError, ZodObject, z } from "zod";
15
15
  import { availableModels } from "../constants.js";
16
16
  import { TerminalTracer } from "../tracer/terminal.js";
17
17
  import { DEFAULT_CHAT_INPUT_KEY, runChatLoopInTerminal, } from "./run-chat-loop.js";
18
- export const createRunAIGNECommand = (name = "run") => new Command(name)
19
- .allowUnknownOption(true)
20
- .allowExcessArguments(true)
21
- .description("Run agent with AIGNE in terminal")
22
- .option("--chat", "Run chat loop in terminal", false)
23
- .option("--model <provider[:model]>", `AI model to use in format 'provider[:model]' where model is optional. Examples: 'openai' or 'openai:gpt-4o-mini'. Available providers: ${availableModels()
24
- .map((i) => i.name.toLowerCase().replace(/ChatModel$/i, ""))
25
- .join(", ")} (default: openai)`)
26
- .option("--temperature <temperature>", "Temperature for the model (controls randomness, higher values produce more random outputs). Range: 0.0-2.0", customZodError("--temperature", (s) => z.coerce.number().min(0).max(2).parse(s)))
27
- .option("--top-p <top-p>", "Top P (nucleus sampling) parameter for the model (controls diversity). Range: 0.0-1.0", customZodError("--top-p", (s) => z.coerce.number().min(0).max(1).parse(s)))
28
- .option("--presence-penalty <presence-penalty>", "Presence penalty for the model (penalizes repeating the same tokens). Range: -2.0 to 2.0", customZodError("--presence-penalty", (s) => z.coerce.number().min(-2).max(2).parse(s)))
29
- .option("--frequency-penalty <frequency-penalty>", "Frequency penalty for the model (penalizes frequency of token usage). Range: -2.0 to 2.0", customZodError("--frequency-penalty", (s) => z.coerce.number().min(-2).max(2).parse(s)))
30
- .option("--input -i <input...>", "Input to the agent, use @<file> to read from a file")
31
- .option("--format <format>", "Input format for the agent (available: text, json, yaml default: text)")
32
- .option("--output -o <output>", "Output file to save the result (default: stdout)")
33
- .option("--output-key <output-key>", "Key in the result to save to the output file", DEFAULT_OUTPUT_KEY)
34
- .option("--force", "Truncate the output file if it exists, and create directory if the output path is not exists", false)
35
- .option("--log-level <level>", `Log level for detailed debugging information. Values: ${Object.values(LogLevel).join(", ")}`, customZodError("--log-level", (s) => z.nativeEnum(LogLevel).parse(s)), getLevelFromEnv(logger.options.ns) || LogLevel.INFO);
18
+ export const createRunAIGNECommand = (yargs) => yargs
19
+ .option("chat", {
20
+ describe: "Run chat loop in terminal",
21
+ type: "boolean",
22
+ default: false,
23
+ })
24
+ .option("model", {
25
+ describe: `AI model to use in format 'provider[:model]' where model is optional. Examples: 'openai' or 'openai:gpt-4o-mini'. Available providers: ${availableModels()
26
+ .map((i) => i.name.toLowerCase().replace(/ChatModel$/i, ""))
27
+ .join(", ")} (default: openai)`,
28
+ type: "string",
29
+ })
30
+ .option("temperature", {
31
+ describe: "Temperature for the model (controls randomness, higher values produce more random outputs). Range: 0.0-2.0",
32
+ type: "number",
33
+ coerce: customZodError("--temperature", (s) => z.coerce.number().min(0).max(2).parse(s)),
34
+ })
35
+ .option("top-p", {
36
+ describe: "Top P (nucleus sampling) parameter for the model (controls diversity). Range: 0.0-1.0",
37
+ type: "number",
38
+ coerce: customZodError("--top-p", (s) => z.coerce.number().min(0).max(1).parse(s)),
39
+ })
40
+ .option("presence-penalty", {
41
+ describe: "Presence penalty for the model (penalizes repeating the same tokens). Range: -2.0 to 2.0",
42
+ type: "number",
43
+ coerce: customZodError("--presence-penalty", (s) => z.coerce.number().min(-2).max(2).parse(s)),
44
+ })
45
+ .option("frequency-penalty", {
46
+ describe: "Frequency penalty for the model (penalizes frequency of token usage). Range: -2.0 to 2.0",
47
+ type: "number",
48
+ coerce: customZodError("--frequency-penalty", (s) => z.coerce.number().min(-2).max(2).parse(s)),
49
+ })
50
+ .option("input", {
51
+ describe: "Input to the agent, use @<file> to read from a file",
52
+ type: "array",
53
+ alias: "i",
54
+ })
55
+ .option("format", {
56
+ describe: "Input format for the agent (available: text, json, yaml default: text)",
57
+ type: "string",
58
+ })
59
+ .option("output", {
60
+ describe: "Output file to save the result (default: stdout)",
61
+ type: "string",
62
+ alias: "o",
63
+ })
64
+ .option("output-key", {
65
+ describe: "Key in the result to save to the output file",
66
+ type: "string",
67
+ default: DEFAULT_OUTPUT_KEY,
68
+ })
69
+ .option("force", {
70
+ describe: "Truncate the output file if it exists, and create directory if the output path is not exists",
71
+ type: "boolean",
72
+ default: false,
73
+ })
74
+ .option("log-level", {
75
+ describe: `Log level for detailed debugging information. Values: ${Object.values(LogLevel).join(", ")}`,
76
+ type: "string",
77
+ default: getLevelFromEnv(logger.options.ns) || LogLevel.INFO,
78
+ coerce: customZodError("--log-level", (s) => z.nativeEnum(LogLevel).parse(s)),
79
+ });
36
80
  export async function parseAgentInputByCommander(agent, options = {}) {
37
- const cmd = new Command()
38
- .description(`Run agent ${agent.name} with AIGNE`)
39
- .allowUnknownOption(true)
40
- .allowExcessArguments(true);
41
- const inputSchemaShape = agent.inputSchema instanceof ZodObject ? Object.keys(agent.inputSchema.shape) : [];
42
- for (const option of inputSchemaShape) {
43
- cmd.option(`--input-${option} <${option}>`);
44
- }
45
- const input = await new Promise((resolve, reject) => {
46
- cmd
47
- .action(async (agentInputOptions) => {
48
- try {
49
- const input = Object.fromEntries((await Promise.all(Object.entries(agentInputOptions).map(async ([key, value]) => {
50
- let k = key.replace(/^input/, "");
51
- k = k.charAt(0).toLowerCase() + k.slice(1);
52
- if (!k)
53
- return null;
54
- if (typeof value === "string" && value.startsWith("@")) {
55
- value = await readFile(value.slice(1), "utf8");
56
- }
57
- return [k, value];
58
- }))).filter(isNonNullable));
59
- resolve(input);
60
- }
61
- catch (error) {
62
- reject(error);
63
- }
64
- })
65
- .parseAsync(options.argv ?? process.argv)
66
- .catch((error) => reject(error));
67
- });
81
+ const inputSchemaShape = flat(agent instanceof AIAgent ? agent.inputKey : undefined, agent.inputSchema instanceof ZodObject ? Object.keys(agent.inputSchema.shape) : []);
82
+ const parsedInput = await yargs().parseAsync(options.argv ?? process.argv);
83
+ const input = Object.fromEntries(await Promise.all(inputSchemaShape.map(async (key) => {
84
+ const k = `input${key.charAt(0).toUpperCase()}${key.slice(1)}`;
85
+ let value = parsedInput[k];
86
+ if (typeof value === "string" && value.startsWith("@")) {
87
+ value = await readFile(value.slice(1), "utf8");
88
+ }
89
+ return [key, value];
90
+ })));
68
91
  const rawInput = options.input ||
69
92
  (isatty(process.stdin.fd) || !(await stdinHasData())
70
93
  ? null
@@ -100,10 +123,8 @@ export const parseModelOption = (model) => {
100
123
  return { provider, name };
101
124
  };
102
125
  export async function runWithAIGNE(agentCreator, { argv = process.argv, chatLoopOptions, modelOptions, outputKey, } = {}) {
103
- await createRunAIGNECommand()
104
- .showHelpAfterError(true)
105
- .showSuggestionAfterError(true)
106
- .action(async (options) => {
126
+ await yargs()
127
+ .command("$0", "Run an agent with AIGNE", (yargs) => createRunAIGNECommand(yargs), async (options) => {
107
128
  if (options.logLevel) {
108
129
  logger.level = options.logLevel;
109
130
  }
@@ -134,6 +155,8 @@ export async function runWithAIGNE(agentCreator, { argv = process.argv, chatLoop
134
155
  await aigne.shutdown();
135
156
  }
136
157
  })
158
+ .alias("h", "help")
159
+ .alias("v", "version")
137
160
  .parseAsync(argv)
138
161
  .catch((error) => {
139
162
  console.error(`${chalk.red("Error:")} ${error.message}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/cli",
3
- "version": "1.27.0",
3
+ "version": "1.27.1-0",
4
4
  "description": "cli for AIGNE framework",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -51,7 +51,6 @@
51
51
  "@ocap/mcrypto": "^1.21.0",
52
52
  "@smithy/node-http-handler": "^4.1.0",
53
53
  "chalk": "^5.4.1",
54
- "commander": "^14.0.0",
55
54
  "crypto": "^1.0.1",
56
55
  "detect-port": "^2.1.0",
57
56
  "dotenv-flow": "^4.1.0",
@@ -69,21 +68,22 @@
69
68
  "tar": "^7.4.3",
70
69
  "wrap-ansi": "^9.0.0",
71
70
  "yaml": "^2.8.0",
71
+ "yargs": "^18.0.0",
72
72
  "zod": "^3.25.67",
73
73
  "@aigne/agent-library": "^1.21.6",
74
- "@aigne/aigne-hub": "^0.3.0",
75
74
  "@aigne/agentic-memory": "^1.0.6",
75
+ "@aigne/aigne-hub": "^0.3.0",
76
76
  "@aigne/anthropic": "^0.10.2",
77
77
  "@aigne/bedrock": "^0.8.6",
78
+ "@aigne/core": "^1.39.0",
78
79
  "@aigne/deepseek": "^0.7.6",
79
80
  "@aigne/default-memory": "^1.0.6",
80
81
  "@aigne/gemini": "^0.8.6",
81
- "@aigne/observability-api": "^0.8.2",
82
- "@aigne/open-router": "^0.7.6",
83
82
  "@aigne/ollama": "^0.7.6",
84
- "@aigne/core": "^1.39.0",
83
+ "@aigne/open-router": "^0.7.6",
85
84
  "@aigne/openai": "^0.10.6",
86
- "@aigne/xai": "^0.7.6"
85
+ "@aigne/xai": "^0.7.6",
86
+ "@aigne/observability-api": "^0.8.2"
87
87
  },
88
88
  "devDependencies": {
89
89
  "@types/archiver": "^6.0.3",
@@ -92,6 +92,7 @@
92
92
  "@types/glob": "^9.0.0",
93
93
  "@types/gradient-string": "^1.1.6",
94
94
  "@types/node": "^24.0.12",
95
+ "@types/yargs": "^17.0.33",
95
96
  "archiver": "^7.0.1",
96
97
  "hono": "4.8.4",
97
98
  "npm-run-all": "^4.1.5",