@agent-smith/cli 0.0.37 → 0.0.39

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.
@@ -9,6 +9,7 @@ import { executeJobCmd, readJob } from "../lib/execute_job.js";
9
9
  import { executeTaskCmd } from "../lib/execute_task.js";
10
10
  import { readCmds } from "../sys/read_cmds.js";
11
11
  import { readTask } from "../lib/utils.js";
12
+ import { executeWorkflowCmd } from "../lib/workflows/cmd.js";
12
13
  let cmds = {
13
14
  q: {
14
15
  cmd: async () => process.exit(0),
@@ -41,6 +42,11 @@ let cmds = {
41
42
  description: "execute a job",
42
43
  args: "arguments: \n-job (required): the job name\n-args: arguments if any for the job"
43
44
  },
45
+ w: {
46
+ cmd: _executeWorkflowCmd,
47
+ description: "execute a workflow",
48
+ args: "arguments: \n-workflow (required): the workflow name\n-args: arguments if any for the workflow"
49
+ },
44
50
  a: {
45
51
  cmd: executeActionCmd,
46
52
  description: "execute an action",
@@ -74,6 +80,11 @@ function initAliases() {
74
80
  cmd: (args = [], options) => _executeJobCmd([alias.name, ...args], options),
75
81
  description: "job: " + alias.name,
76
82
  };
83
+ case "workflow":
84
+ _cmds[alias.name] = {
85
+ cmd: (args = [], options) => _executeWorkflowCmd([alias.name, ...args], options),
86
+ description: "wokflow: " + alias.name,
87
+ };
77
88
  }
78
89
  });
79
90
  return _cmds;
@@ -144,6 +155,15 @@ async function _executeJobCmd(args = [], options) {
144
155
  const res = await executeJobCmd(name, args, options);
145
156
  return res;
146
157
  }
158
+ async function _executeWorkflowCmd(args = [], options) {
159
+ if (args.length == 0) {
160
+ console.warn("Provide a workflow name");
161
+ return;
162
+ }
163
+ const name = args.shift();
164
+ const res = await executeWorkflowCmd(name, args, options);
165
+ return res;
166
+ }
147
167
  async function _readTaskCmd(args = [], options) {
148
168
  if (args.length == 0) {
149
169
  console.warn("Provide a task name");
@@ -1,6 +1,6 @@
1
1
  import { AgentTask } from "@agent-smith/jobs";
2
2
  import { FeatureType, NodeReturnType } from "../../interfaces.js";
3
3
  declare function systemAction(path: string): AgentTask<FeatureType, Array<string>, NodeReturnType<string>>;
4
- declare function pythonAction(path: string): AgentTask<FeatureType, any, NodeReturnType<string | Record<string, any> | Array<any>>>;
5
- declare function executeActionCmd(args?: Array<string>, options?: any, quiet?: boolean): Promise<NodeReturnType<any>>;
4
+ declare function pythonAction(path: string): AgentTask<FeatureType, Array<string>, NodeReturnType<string | Record<string, any> | Array<any>>>;
5
+ declare function executeActionCmd(args?: Array<string> | Record<string, any>, options?: any, quiet?: boolean): Promise<NodeReturnType<any>>;
6
6
  export { executeActionCmd, systemAction, pythonAction };
@@ -41,12 +41,28 @@ function pythonAction(path) {
41
41
  return action;
42
42
  }
43
43
  async function executeActionCmd(args = [], options = {}, quiet = false) {
44
- const name = args.shift();
44
+ const isWorkflow = !Array.isArray(args);
45
+ let name;
46
+ if (!isWorkflow) {
47
+ name = args.shift();
48
+ }
49
+ else {
50
+ if (!args.name) {
51
+ throw new Error("Provide an action name param");
52
+ }
53
+ name = args.name;
54
+ delete args.name;
55
+ }
45
56
  const { found, path, ext } = getFeatureSpec(name, "action");
46
57
  if (!found) {
47
58
  throw new Error("Action not found");
48
59
  }
49
60
  let act;
61
+ if (!["js", "mjs"].includes(ext)) {
62
+ if (isWorkflow) {
63
+ throw new Error(`Action ${name} param error: ${typeof args}, ${args}`);
64
+ }
65
+ }
50
66
  switch (ext) {
51
67
  case "js":
52
68
  const { action } = await import(path);
@@ -69,7 +85,7 @@ async function executeActionCmd(args = [], options = {}, quiet = false) {
69
85
  if (input) {
70
86
  args.push(input);
71
87
  }
72
- const res = await act.run(args, {});
88
+ const res = await act.run(args, options);
73
89
  if (res?.error) {
74
90
  throw res.error;
75
91
  }
@@ -1,6 +1,6 @@
1
1
  import { brain, initAgent, taskBuilder } from "../../agent.js";
2
2
  import { getFeatureSpec } from "../../state/features.js";
3
- import { isChatMode, isDebug } from "../../state/state.js";
3
+ import { isChatMode, isDebug, isVerbose } from "../../state/state.js";
4
4
  import { initTaskConf, initTaskParams, initTaskVars, parseInputOptions, readTask } from "./utils.js";
5
5
  async function executeTaskCmd(args = [], options = {}) {
6
6
  await initAgent();
@@ -8,10 +8,10 @@ async function executeTaskCmd(args = [], options = {}) {
8
8
  console.log("Task args:", args);
9
9
  console.log("Task options:", options);
10
10
  }
11
- const isJob = !Array.isArray(args);
11
+ const isWorkflow = !Array.isArray(args);
12
12
  let name;
13
13
  let pr;
14
- if (!isJob) {
14
+ if (!isWorkflow) {
15
15
  name = args.shift();
16
16
  const _pr = await parseInputOptions(options);
17
17
  if (!_pr) {
@@ -50,7 +50,7 @@ async function executeTaskCmd(args = [], options = {}) {
50
50
  const task = taskBuilder.fromYaml(res.ymlTask);
51
51
  let conf = {};
52
52
  let vars = {};
53
- if (!isJob) {
53
+ if (!isWorkflow) {
54
54
  const tv = initTaskVars(args, taskSpec?.inferParams ? taskSpec.inferParams : {});
55
55
  conf = tv.conf;
56
56
  vars = tv.vars;
@@ -81,21 +81,23 @@ async function executeTaskCmd(args = [], options = {}) {
81
81
  process.stdout.write(t);
82
82
  });
83
83
  conf.expert = ex;
84
- if (isDebug.value) {
84
+ if (isDebug.value || isVerbose.value) {
85
85
  conf.debug = true;
86
86
  }
87
- const fres = await task.run({ prompt: pr, ...vars }, conf);
88
- if (fres.error) {
89
- throw new Error(`Error executing task: ${name} ${fres.data.error}`);
87
+ let ir;
88
+ try {
89
+ ir = await task.run({ prompt: pr, ...vars }, conf);
90
+ }
91
+ catch (err) {
92
+ throw new Error(`Error executing task: ${name} ${err}`);
90
93
  }
91
94
  conf.prompt = pr;
92
95
  if (isChatMode.value) {
93
96
  if (brain.ex.name != ex.name) {
94
97
  brain.setDefaultExpert(ex);
95
98
  }
96
- brain.ex.template.pushToHistory({ user: pr, assistant: fres.text });
99
+ brain.ex.template.pushToHistory({ user: pr, assistant: ir.text });
97
100
  }
98
- const ir = fres;
99
101
  if (isDebug.value) {
100
102
  console.log("\n", ir.stats);
101
103
  }
@@ -0,0 +1,3 @@
1
+ import { NodeReturnType } from "../../../interfaces.js";
2
+ declare function executeWorkflowCmd(name: string, args?: Array<any>, options?: any): Promise<NodeReturnType<any>>;
3
+ export { executeWorkflowCmd, };
@@ -0,0 +1,52 @@
1
+ import { isDebug, isVerbose } from "../../../state/state.js";
2
+ import { readWorkflow } from "./read.js";
3
+ import { executeTaskCmd } from "../execute_task.js";
4
+ import { executeActionCmd } from "../execute_action.js";
5
+ async function executeWorkflowCmd(name, args = [], options = {}) {
6
+ const { workflow, found } = await readWorkflow(name);
7
+ if (!found) {
8
+ throw new Error(`Workflow ${name} not found`);
9
+ }
10
+ const stepNames = Object.keys(workflow);
11
+ if (isDebug.value || isVerbose.value) {
12
+ console.log("Running workflow", name, stepNames.length, "steps");
13
+ }
14
+ let params = {};
15
+ let i = 0;
16
+ const finalTaskIndex = stepNames.length + 1;
17
+ let taskRes = { data: {}, error: new Error("Empty task res") };
18
+ for (const [name, step] of Object.entries(workflow)) {
19
+ if (isDebug.value || isVerbose.value) {
20
+ console.log(`${i + 1}: ${step.type} ${name}`);
21
+ }
22
+ const p = i == 0 ? [name, ...args] : { name: name, ...params };
23
+ switch (step.type) {
24
+ case "task":
25
+ try {
26
+ taskRes = await executeTaskCmd(p, options);
27
+ }
28
+ catch (e) {
29
+ throw new Error(`Wokflow task ${i + 1} error: ${e}`);
30
+ }
31
+ break;
32
+ case "action":
33
+ try {
34
+ const ares = await executeActionCmd(p, options, true);
35
+ taskRes = ares.data;
36
+ if (i == finalTaskIndex) {
37
+ console.log(taskRes.data);
38
+ }
39
+ }
40
+ catch (e) {
41
+ throw new Error(`Workflow action ${i + 1} error: ${e}`);
42
+ }
43
+ break;
44
+ default:
45
+ throw new Error(`Unknown task type ${step.type} in workflow ${name}`);
46
+ }
47
+ params = taskRes;
48
+ ++i;
49
+ }
50
+ return taskRes;
51
+ }
52
+ export { executeWorkflowCmd, };
@@ -0,0 +1,7 @@
1
+ import { AgentTask } from "@agent-smith/jobs";
2
+ import { FeatureType, NodeReturnType } from '../../../interfaces.js';
3
+ declare function readWorkflow(name: string): Promise<{
4
+ found: boolean;
5
+ workflow: Record<string, AgentTask<FeatureType, any, NodeReturnType<any>, Record<string, any>>>;
6
+ }>;
7
+ export { readWorkflow, };
@@ -0,0 +1,104 @@
1
+ import YAML from 'yaml';
2
+ import { default as fs } from "fs";
3
+ import { taskBuilder } from '../../../agent.js';
4
+ import { getFeatureSpec } from '../../../state/features.js';
5
+ import { createJsAction, readTask } from '../utils.js';
6
+ import { pythonAction, systemAction } from './../execute_action.js';
7
+ async function _createWorkflowFromSpec(spec) {
8
+ const steps = {};
9
+ for (const step of spec.steps) {
10
+ const type = Object.keys(step)[0];
11
+ const sval = step[type];
12
+ let name;
13
+ if (typeof sval == "string") {
14
+ name = sval;
15
+ }
16
+ else {
17
+ name = step[type].name;
18
+ }
19
+ if (type == "action") {
20
+ const { found, path, ext } = getFeatureSpec(name, "action");
21
+ if (!found) {
22
+ throw new Error(`Action ${name} not found`);
23
+ }
24
+ switch (ext) {
25
+ case "js":
26
+ const { action } = await import(path);
27
+ const at = action;
28
+ at.type = "action";
29
+ steps[name] = at;
30
+ break;
31
+ case "mjs":
32
+ const mjsa = await import(path);
33
+ const act = createJsAction(mjsa.action);
34
+ act.type = "action";
35
+ steps[name] = act;
36
+ break;
37
+ case "yml":
38
+ const _t1 = systemAction(path);
39
+ _t1.type = "action";
40
+ steps[name] = _t1;
41
+ break;
42
+ case "py":
43
+ const _t = pythonAction(path);
44
+ _t.type = "action";
45
+ steps[name] = _t;
46
+ break;
47
+ default:
48
+ throw new Error(`Unknown feature extension ${ext}`);
49
+ }
50
+ }
51
+ else {
52
+ const { found, path } = getFeatureSpec(name, "task");
53
+ if (!found) {
54
+ throw new Error(`Task ${name} not found`);
55
+ }
56
+ const res = readTask(path);
57
+ if (!res.found) {
58
+ throw new Error(`Unable to read task ${name} ${path}`);
59
+ }
60
+ const tsk = taskBuilder.fromYaml(res.ymlTask, "task");
61
+ steps[name] = tsk;
62
+ }
63
+ }
64
+ return steps;
65
+ }
66
+ async function _readWorkflowFromDisk(name) {
67
+ const { found, path } = getFeatureSpec(name, "workflow");
68
+ if (!found) {
69
+ return { found: false, data: {} };
70
+ }
71
+ if (!fs.existsSync(path)) {
72
+ return { data: {}, found: false };
73
+ }
74
+ const file = fs.readFileSync(path, 'utf8');
75
+ const data = YAML.parse(file);
76
+ data.name = name;
77
+ return { data: data, found: true };
78
+ }
79
+ async function readWorkflow(name) {
80
+ const { found, ext } = getFeatureSpec(name, "workflow");
81
+ if (!found) {
82
+ return { found: false, workflow: {} };
83
+ }
84
+ let wf = {};
85
+ switch (ext) {
86
+ case "yml":
87
+ const { data } = await _readWorkflowFromDisk(name);
88
+ try {
89
+ const workflow = await _createWorkflowFromSpec(data);
90
+ if (!found) {
91
+ return { found: false, workflow: {} };
92
+ }
93
+ wf = workflow;
94
+ }
95
+ catch (e) {
96
+ throw new Error(`Workflow create error: ${e}`);
97
+ }
98
+ break;
99
+ default:
100
+ throw new Error(`Workflow extension ${ext} not implemented`);
101
+ }
102
+ return { found: true, workflow: wf };
103
+ }
104
+ export { readWorkflow, };
@@ -21,6 +21,7 @@ function readFeaturesDir(dir) {
21
21
  job: [],
22
22
  action: [],
23
23
  cmd: [],
24
+ workflow: []
24
25
  };
25
26
  let dirpath = path.join(dir, "tasks");
26
27
  if (fs.existsSync(dirpath)) {
@@ -50,6 +51,20 @@ function readFeaturesDir(dir) {
50
51
  });
51
52
  });
52
53
  }
54
+ dirpath = path.join(dir, "workflows");
55
+ if (fs.existsSync(dirpath)) {
56
+ const data = _readDir(dirpath, [".yml"]);
57
+ data.forEach((filename) => {
58
+ const parts = filename.split(".");
59
+ const ext = parts.pop();
60
+ const name = parts.join("");
61
+ feats.workflow.push({
62
+ name: name,
63
+ path: path.join(dirpath),
64
+ ext: ext,
65
+ });
66
+ });
67
+ }
53
68
  dirpath = path.join(dir, "actions");
54
69
  if (fs.existsSync(dirpath)) {
55
70
  const data = _readDir(dirpath, ["yml", ".js", "mjs", ".py"]);
package/dist/db/read.js CHANGED
@@ -27,11 +27,12 @@ function _readFeaturesType(type) {
27
27
  return f;
28
28
  }
29
29
  function readFeatures() {
30
- const feats = { task: {}, job: {}, action: {}, cmd: {} };
30
+ const feats = { task: {}, job: {}, action: {}, cmd: {}, workflow: {} };
31
31
  feats.task = _readFeaturesType("task");
32
32
  feats.job = _readFeaturesType("job");
33
33
  feats.action = _readFeaturesType("action");
34
34
  feats.cmd = _readFeaturesType("cmd");
35
+ feats.workflow = _readFeaturesType("workflow");
35
36
  return feats;
36
37
  }
37
38
  function readAliases() {
@@ -24,6 +24,12 @@ const jobs = `CREATE TABLE IF NOT EXISTS job (
24
24
  path TEXT NOT NULL,
25
25
  ext TEXT NOT NULL CHECK ( ext IN ('yml') )
26
26
  );`;
27
+ const workflow = `CREATE TABLE IF NOT EXISTS workflow (
28
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
29
+ name TEXT UNIQUE NOT NULL,
30
+ path TEXT NOT NULL,
31
+ ext TEXT NOT NULL CHECK ( ext IN ('yml') )
32
+ );`;
27
33
  const actions = `CREATE TABLE IF NOT EXISTS action (
28
34
  id INTEGER PRIMARY KEY AUTOINCREMENT,
29
35
  name TEXT UNIQUE NOT NULL,
@@ -39,9 +45,9 @@ const cmds = `CREATE TABLE IF NOT EXISTS cmd (
39
45
  const aliases = `CREATE TABLE IF NOT EXISTS aliases (
40
46
  id INTEGER PRIMARY KEY AUTOINCREMENT,
41
47
  name TEXT UNIQUE NOT NULL,
42
- type TEXT NOT NULL CHECK ( type IN ('task', 'action', 'job') )
48
+ type TEXT NOT NULL CHECK ( type IN ('task', 'action', 'job', 'workflow') )
43
49
  );`;
44
50
  const schemas = [
45
- filepaths, featurespaths, tasks, jobs, actions, cmds, plugins, aliases
51
+ filepaths, featurespaths, tasks, jobs, workflow, actions, cmds, plugins, aliases
46
52
  ];
47
53
  export { schemas };
package/dist/db/write.js CHANGED
@@ -62,6 +62,9 @@ function updateAliases(feats) {
62
62
  feats.job.forEach((feat) => {
63
63
  existingAliases = _updateAlias(existingAliases, feat.name, "job");
64
64
  });
65
+ feats.workflow.forEach((feat) => {
66
+ existingAliases = _updateAlias(existingAliases, feat.name, "workflow");
67
+ });
65
68
  }
66
69
  function upsertAndCleanFeatures(feats, type) {
67
70
  const stmt = db.prepare(`SELECT name FROM ${type}`);
@@ -88,5 +91,6 @@ function updateFeatures(feats) {
88
91
  upsertAndCleanFeatures(feats.job, "job");
89
92
  upsertAndCleanFeatures(feats.action, "action");
90
93
  upsertAndCleanFeatures(feats.cmd, "cmd");
94
+ upsertAndCleanFeatures(feats.workflow, "workflow");
91
95
  }
92
96
  export { updatePromptfilePath, insertFeaturesPathIfNotExists, insertPluginIfNotExists, updateFeatures, updateAliases, cleanupFeaturePaths, };
package/dist/index.js CHANGED
File without changes
@@ -29,6 +29,11 @@ interface Features {
29
29
  path: string;
30
30
  ext: ActionExtension;
31
31
  }>;
32
+ workflow: Array<{
33
+ name: string;
34
+ path: string;
35
+ ext: WorkflowExtension;
36
+ }>;
32
37
  }
33
38
  interface ConfigFile {
34
39
  promptfile?: string;
@@ -44,11 +49,12 @@ type InputMode = "manual" | "promptfile" | "clipboard";
44
49
  type OutputMode = "txt" | "clipboard";
45
50
  type RunMode = "cli" | "cmd";
46
51
  type FormatMode = "text" | "markdown";
47
- type FeatureType = "task" | "job" | "action" | "cmd";
52
+ type FeatureType = "task" | "job" | "action" | "cmd" | "workflow";
48
53
  type ActionExtension = "js" | "mjs" | "py" | "yml";
49
54
  type TaskExtension = "yml";
55
+ type WorkflowExtension = "yml";
50
56
  type JobExtension = "yml";
51
57
  type CmdExtension = "js";
52
- type FeatureExtension = TaskExtension | JobExtension | CmdExtension | ActionExtension;
53
- type AliasType = "task" | "action" | "job";
54
- export { Cmd, CmdExecutor, NodeReturnType, InputMode, OutputMode, RunMode, FormatMode, FeatureType, ActionExtension, TaskExtension, JobExtension, CmdExtension, FeatureSpec, Features, ConfigFile, FeatureExtension, AliasType, };
58
+ type FeatureExtension = TaskExtension | JobExtension | CmdExtension | ActionExtension | WorkflowExtension;
59
+ type AliasType = "task" | "action" | "job" | "workflow";
60
+ export { Cmd, CmdExecutor, NodeReturnType, InputMode, OutputMode, RunMode, FormatMode, FeatureType, ActionExtension, TaskExtension, JobExtension, WorkflowExtension, CmdExtension, FeatureSpec, Features, ConfigFile, FeatureExtension, AliasType, };
package/dist/main.d.ts CHANGED
@@ -2,7 +2,8 @@ import { execute, run } from "./cmd/sys/execute.js";
2
2
  import { executeJobCmd } from "./cmd/lib/execute_job.js";
3
3
  import { executeActionCmd } from "./cmd/lib/execute_action.js";
4
4
  import { executeTaskCmd } from "./cmd/lib/execute_task.js";
5
+ import { executeWorkflowCmd } from "./cmd/lib/workflows/cmd.js";
5
6
  import { writeToClipboard } from "./cmd/sys/clipboard.js";
6
7
  import { pingCmd } from "./cmd/clicmds/cmds.js";
7
8
  import { initAgent } from "./agent.js";
8
- export { execute, run, pingCmd, executeJobCmd, executeActionCmd, executeTaskCmd, writeToClipboard, initAgent, };
9
+ export { execute, run, pingCmd, executeJobCmd, executeWorkflowCmd, executeActionCmd, executeTaskCmd, writeToClipboard, initAgent, };
package/dist/main.js CHANGED
@@ -2,7 +2,8 @@ import { execute, run } from "./cmd/sys/execute.js";
2
2
  import { executeJobCmd } from "./cmd/lib/execute_job.js";
3
3
  import { executeActionCmd } from "./cmd/lib/execute_action.js";
4
4
  import { executeTaskCmd } from "./cmd/lib/execute_task.js";
5
+ import { executeWorkflowCmd } from "./cmd/lib/workflows/cmd.js";
5
6
  import { writeToClipboard } from "./cmd/sys/clipboard.js";
6
7
  import { pingCmd } from "./cmd/clicmds/cmds.js";
7
8
  import { initAgent } from "./agent.js";
8
- export { execute, run, pingCmd, executeJobCmd, executeActionCmd, executeTaskCmd, writeToClipboard, initAgent, };
9
+ export { execute, run, pingCmd, executeJobCmd, executeWorkflowCmd, executeActionCmd, executeTaskCmd, writeToClipboard, initAgent, };
@@ -7,6 +7,7 @@ function readFeaturesDirs(featuresPaths) {
7
7
  job: [],
8
8
  action: [],
9
9
  cmd: [],
10
+ workflow: [],
10
11
  };
11
12
  featuresPaths.forEach((dir) => {
12
13
  const _f = readFeaturesDir(dir);
@@ -14,6 +15,7 @@ function readFeaturesDirs(featuresPaths) {
14
15
  _f.job.forEach((item) => feats.job.push(item));
15
16
  _f.action.forEach((item) => feats.action.push(item));
16
17
  _f.cmd.forEach((item) => feats.cmd.push(item));
18
+ _f.workflow.forEach((item) => feats.workflow.push(item));
17
19
  });
18
20
  return feats;
19
21
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@agent-smith/cli",
3
3
  "description": "Agent Smith: terminal client for language model agents",
4
4
  "repository": "https://github.com/synw/agent-smith",
5
- "version": "0.0.37",
5
+ "version": "0.0.39",
6
6
  "scripts": {
7
7
  "buildrl": "rm -rf dist/* && rollup -c",
8
8
  "build": "rm -rf dist/* && tsc --noCheck",
@@ -17,7 +17,7 @@
17
17
  "@inquirer/prompts": "^7.3.3",
18
18
  "@inquirer/select": "^4.0.10",
19
19
  "@vue/reactivity": "^3.5.13",
20
- "better-sqlite3": "^11.8.1",
20
+ "better-sqlite3": "^11.9.0",
21
21
  "clipboardy": "^4.0.0",
22
22
  "commander": "^13.1.0",
23
23
  "marked-terminal": "^7.3.0",
@@ -29,7 +29,7 @@
29
29
  "@agent-smith/tmem-jobs": "^0.0.4",
30
30
  "@commander-js/extra-typings": "^13.1.0",
31
31
  "@locallm/types": "^0.1.5",
32
- "@rollup/plugin-node-resolve": "^16.0.0",
32
+ "@rollup/plugin-node-resolve": "^16.0.1",
33
33
  "@rollup/plugin-typescript": "^12.1.2",
34
34
  "@types/better-sqlite3": "^7.6.12",
35
35
  "@types/marked-terminal": "^6.1.1",