@aigne/cli 1.3.0 → 1.3.1-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.
@@ -1,9 +1,11 @@
1
+ import assert from "node:assert";
1
2
  import { cp, mkdir, rm } from "node:fs/promises";
2
3
  import { homedir } from "node:os";
3
4
  import { isAbsolute, join, resolve } from "node:path";
4
5
  import { ExecutionEngine } from "@aigne/core";
5
6
  import { loadModel } from "@aigne/core/loader/index.js";
6
7
  import { isNonNullable } from "@aigne/core/utils/type-utils.js";
8
+ import { Listr, PRESET_TIMER } from "@aigne/listr2";
7
9
  import { Command } from "commander";
8
10
  import { isV1Package, toAIGNEPackage } from "../utils/agent-v1.js";
9
11
  import { downloadAndExtract } from "../utils/download.js";
@@ -17,69 +19,111 @@ export function createRunCommand() {
17
19
  .option("--model-provider <provider>", "Model provider to use, available providers: openai, claude, xai (defaults to the aigne.yaml definition or openai)")
18
20
  .option("--model-name <model>", "Model name to use, available models depend on the provider (defaults to the aigne.yaml definition or gpt-4o-mini)")
19
21
  .action(async (path, options) => {
20
- if (path.startsWith("http")) {
21
- await downloadAndRunPackage(path, options);
22
- return;
23
- }
24
- const absolutePath = isAbsolute(path) ? path : resolve(process.cwd(), path);
25
- await runEngine(path, absolutePath, options);
22
+ const { downloadDir, dir } = prepareDirs(path, options);
23
+ const { engine, agent } = await new Listr([
24
+ {
25
+ title: "Prepare environment",
26
+ task: (_, task) => {
27
+ if (downloadDir) {
28
+ return task.newListr([
29
+ {
30
+ title: "Download package",
31
+ task: () => downloadPackage(path, downloadDir),
32
+ },
33
+ {
34
+ title: "Extract package",
35
+ task: () => extractPackage(downloadDir, dir),
36
+ },
37
+ ]);
38
+ }
39
+ },
40
+ },
41
+ {
42
+ title: "Initialize execution engine",
43
+ task: async (ctx) => {
44
+ const engine = await runEngine(dir, options);
45
+ ctx.engine = engine;
46
+ },
47
+ },
48
+ {
49
+ task: (ctx) => {
50
+ const { engine } = ctx;
51
+ assert(engine);
52
+ let agent;
53
+ if (options.agent) {
54
+ agent = engine.agents[options.agent];
55
+ if (!agent) {
56
+ console.error(`Agent "${options.agent}" not found in ${path}`);
57
+ console.log("Available agents:");
58
+ for (const agent of engine.agents) {
59
+ console.log(`- ${agent.name}`);
60
+ }
61
+ throw new Error(`Agent "${options.agent}" not found in ${path}`);
62
+ }
63
+ }
64
+ else {
65
+ agent = engine.agents[0];
66
+ if (!agent)
67
+ throw new Error(`No agents found in ${path}`);
68
+ }
69
+ ctx.agent = agent;
70
+ },
71
+ },
72
+ ], {
73
+ rendererOptions: {
74
+ collapseSubtasks: false,
75
+ timer: PRESET_TIMER,
76
+ },
77
+ }).run();
78
+ assert(engine);
79
+ assert(agent);
80
+ const user = engine.call(agent);
81
+ await runChatLoopInTerminal(user, {});
82
+ await engine.shutdown();
26
83
  })
27
84
  .showHelpAfterError(true)
28
85
  .showSuggestionAfterError(true);
29
86
  }
30
- async function runEngine(originalPath, path, options) {
87
+ async function runEngine(path, options) {
31
88
  if (options.modelName && !options.modelProvider) {
32
89
  throw new Error("please specify --model-provider when using the --model-name option");
33
90
  }
34
91
  const model = options.modelProvider
35
92
  ? await loadModel({ provider: options.modelProvider, name: options.modelName })
36
93
  : undefined;
37
- const engine = await ExecutionEngine.load({ path, model });
38
- let agent;
39
- if (options.agent) {
40
- agent = engine.agents[options.agent];
41
- if (!agent) {
42
- console.error(`Agent "${options.agent}" not found in ${originalPath}`);
43
- console.log("Available agents:");
44
- for (const agent of engine.agents) {
45
- console.log(`- ${agent.name}`);
46
- }
47
- throw new Error(`Agent "${options.agent}" not found in ${originalPath}`);
48
- }
94
+ return await ExecutionEngine.load({ path, model });
95
+ }
96
+ async function downloadPackage(url, downloadDir) {
97
+ await rm(downloadDir, { recursive: true, force: true });
98
+ await mkdir(downloadDir, { recursive: true });
99
+ await downloadAndExtract(url, downloadDir);
100
+ }
101
+ async function extractPackage(downloadDir, dir) {
102
+ await mkdir(dir, { recursive: true });
103
+ if (await isV1Package(downloadDir)) {
104
+ await toAIGNEPackage(downloadDir, dir);
49
105
  }
50
106
  else {
51
- agent = engine.agents[0];
52
- if (!agent)
53
- throw new Error(`No agents found in ${originalPath}`);
107
+ await cp(downloadDir, dir, { recursive: true, force: true });
54
108
  }
55
- const user = engine.call(agent);
56
- await runChatLoopInTerminal(user, {});
57
109
  }
58
- async function downloadAndRunPackage(url, options) {
110
+ function prepareDirs(path, options) {
59
111
  let dir;
60
112
  let downloadDir;
61
- if (options.downloadDir) {
113
+ if (!path.startsWith("http")) {
114
+ dir = isAbsolute(path) ? path : resolve(process.cwd(), path);
115
+ }
116
+ else if (options.downloadDir) {
62
117
  dir = isAbsolute(options.downloadDir)
63
118
  ? options.downloadDir
64
119
  : resolve(process.cwd(), options.downloadDir);
65
120
  downloadDir = join(dir, ".download");
66
121
  }
67
122
  else {
68
- dir = getLocalPackagePathFromUrl(url);
69
- downloadDir = getLocalPackagePathFromUrl(url, { subdir: ".download" });
70
- }
71
- // clean up the download directory
72
- await rm(downloadDir, { recursive: true, force: true });
73
- await mkdir(dir, { recursive: true });
74
- await mkdir(downloadDir, { recursive: true });
75
- await downloadAndExtract(url, downloadDir);
76
- if (await isV1Package(downloadDir)) {
77
- await toAIGNEPackage(downloadDir, dir);
78
- }
79
- else {
80
- await cp(downloadDir, dir, { recursive: true, force: true });
123
+ dir = getLocalPackagePathFromUrl(path);
124
+ downloadDir = getLocalPackagePathFromUrl(path, { subdir: ".download" });
81
125
  }
82
- await runEngine(url, dir, options);
126
+ return { downloadDir, dir };
83
127
  }
84
128
  function getLocalPackagePathFromUrl(url, { subdir } = {}) {
85
129
  const root = [homedir(), ".aigne", subdir].filter(isNonNullable);
@@ -54,7 +54,6 @@ async function callAgent(userAgent, input, options) {
54
54
  ? { [options.inputKey]: input }
55
55
  : createMessage(input));
56
56
  console.log(`
57
- ${chalk.grey(figures.tick)} 💬 ${inspect(input, { colors: true })}
58
57
  ${chalk.grey(figures.tick)} 🤖 ${tracer.formatTokenUsage(context.usage)}
59
58
  ${formatAIResponse(result)}
60
59
  `);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/cli",
3
- "version": "1.3.0",
3
+ "version": "1.3.1-1",
4
4
  "description": "cli for AIGNE framework",
5
5
  "publishConfig": {
6
6
  "access": "public"