@agent-smith/cli 0.0.35 → 0.0.37

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,4 +1,4 @@
1
- import { formatMode, isChatMode, promptfile } from "../../state/state.js";
1
+ import { formatMode, isChatMode, isDebug, promptfile } from "../../state/state.js";
2
2
  import { getFeatureSpec, readFeaturesDirs } from "../../state/features.js";
3
3
  import { readAliases, readFeatures } from "../../db/read.js";
4
4
  import { cleanupFeaturePaths, updateAliases, updateFeatures, updatePromptfilePath } from "../../db/write.js";
@@ -86,7 +86,7 @@ async function initCmds() {
86
86
  return cmds;
87
87
  }
88
88
  async function pingCmd(args = [], options) {
89
- const isUp = await initAgent(true);
89
+ const isUp = await initAgent(isDebug.value);
90
90
  return isUp;
91
91
  }
92
92
  async function _updateConfCmd(args = [], options) {
@@ -120,13 +120,13 @@ async function _executeTaskCmd(args = [], options) {
120
120
  console.warn("Provide a task name");
121
121
  return;
122
122
  }
123
- const { ok, data, conf, error } = await executeTaskCmd(args, options);
124
- if (!ok) {
123
+ const { data, error } = await executeTaskCmd(args, options);
124
+ if (error) {
125
125
  console.warn(error);
126
126
  }
127
127
  if (formatMode.value == "markdown") {
128
128
  console.log("\n------------------\n");
129
- console.log(marked.parse(data).trim());
129
+ console.log(marked.parse(data.text).trim());
130
130
  }
131
131
  else {
132
132
  console.log();
@@ -1,6 +1,6 @@
1
1
  import { AgentTask } from "@agent-smith/jobs";
2
- import { FeatureType } from "../../interfaces.js";
3
- declare function systemAction(path: string): AgentTask<FeatureType>;
4
- declare function pythonAction(path: string): AgentTask<FeatureType>;
5
- declare function executeActionCmd(args?: Array<string>, options?: any, quiet?: boolean): Promise<any>;
2
+ import { FeatureType, NodeReturnType } from "../../interfaces.js";
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>>;
6
6
  export { executeActionCmd, systemAction, pythonAction };
@@ -15,7 +15,7 @@ function systemAction(path) {
15
15
  actionSpec.data.args = [];
16
16
  }
17
17
  const out = await execute(actionSpec.data.cmd, [...actionSpec.data.args, ...args]);
18
- return { data: out.trim(), error: "", ok: true };
18
+ return { data: out.trim() };
19
19
  }
20
20
  });
21
21
  return action;
@@ -25,8 +25,17 @@ function pythonAction(path) {
25
25
  id: "python_action",
26
26
  title: "",
27
27
  run: async (args) => {
28
- const out = await runPyScript(pyShell, "python3", path, args);
29
- return { data: out.join("\n"), error: "", ok: true };
28
+ const { data, error } = await runPyScript(pyShell, "python3", path, args);
29
+ if (error) {
30
+ return { data: {}, error: error };
31
+ }
32
+ const txt = data.join("\n");
33
+ let final = txt;
34
+ if (txt.startsWith("{") || txt.startsWith("[")) {
35
+ final = JSON.parse(final);
36
+ }
37
+ const res = { data: final };
38
+ return res;
30
39
  }
31
40
  });
32
41
  return action;
@@ -35,7 +44,7 @@ async function executeActionCmd(args = [], options = {}, quiet = false) {
35
44
  const name = args.shift();
36
45
  const { found, path, ext } = getFeatureSpec(name, "action");
37
46
  if (!found) {
38
- return { ok: false, data: {}, error: "Action not found" };
47
+ throw new Error("Action not found");
39
48
  }
40
49
  let act;
41
50
  switch (ext) {
@@ -61,10 +70,13 @@ async function executeActionCmd(args = [], options = {}, quiet = false) {
61
70
  args.push(input);
62
71
  }
63
72
  const res = await act.run(args, {});
73
+ if (res?.error) {
74
+ throw res.error;
75
+ }
64
76
  if (!quiet) {
65
77
  console.log(res.data);
66
78
  }
67
79
  await processOutput(res);
68
- return { ok: true, data: res.data, error: "" };
80
+ return { data: res.data };
69
81
  }
70
82
  export { executeActionCmd, systemAction, pythonAction };
@@ -4,7 +4,7 @@ import { useAgentJob } from "@agent-smith/jobs";
4
4
  import { brain, marked, taskBuilder } from '../../agent.js';
5
5
  import { getFeatureSpec } from '../../state/features.js';
6
6
  import { formatMode, isDebug, isVerbose } from '../../state/state.js';
7
- import { createJsAction, initTaskConf, initTaskParams, initTaskVars, parseInputOptions, readTask } from './utils.js';
7
+ import { createJsAction, initActionVars, initTaskConf, initTaskParams, initTaskVars, parseInputOptions, readTask } from './utils.js';
8
8
  import { pythonAction, systemAction } from './execute_action.js';
9
9
  async function executeJobCmd(name, args = [], options = {}) {
10
10
  const { job, found } = await _dispatchReadJob(name);
@@ -12,8 +12,7 @@ async function executeJobCmd(name, args = [], options = {}) {
12
12
  console.log("Running job", name, Object.keys(job.tasks).length, "tasks");
13
13
  }
14
14
  if (!found) {
15
- console.log(`Job ${name} not found`);
16
- return { error: `Job ${name} not found` };
15
+ throw new Error(`Job ${name} not found`);
17
16
  }
18
17
  await job.start();
19
18
  let res = {};
@@ -26,7 +25,7 @@ async function executeJobCmd(name, args = [], options = {}) {
26
25
  const chain = task.properties?.chain;
27
26
  const { found, path } = getFeatureSpec(name, "task");
28
27
  if (!found) {
29
- return { ok: false, data: {}, error: `Task ${name} not found` };
28
+ throw new Error(`Task ${name} not found`);
30
29
  }
31
30
  const tres = readTask(path);
32
31
  if (!tres.found) {
@@ -54,7 +53,7 @@ async function executeJobCmd(name, args = [], options = {}) {
54
53
  params.prompt = params.text;
55
54
  }
56
55
  else {
57
- throw new Error(`No prompt provided for task ${name}.\Params: ${params}`);
56
+ throw new Error(`No prompt provided for task ${name}.\Params: ${JSON.stringify(params, null, 2)}`);
58
57
  }
59
58
  }
60
59
  const vs = initTaskParams(params, taskSpec?.inferParams ? taskSpec.inferParams : {});
@@ -87,6 +86,7 @@ async function executeJobCmd(name, args = [], options = {}) {
87
86
  ex.backend.setOnToken((t) => {
88
87
  process.stdout.write(t);
89
88
  });
89
+ brain.setDefaultExpert(ex);
90
90
  conf["expert"] = ex;
91
91
  try {
92
92
  if (isDebug.value || isVerbose.value) {
@@ -96,6 +96,7 @@ async function executeJobCmd(name, args = [], options = {}) {
96
96
  const invars = { ...params, ...vars };
97
97
  try {
98
98
  res = await job.runTask(name, invars, conf);
99
+ console.log("");
99
100
  }
100
101
  catch (e) {
101
102
  throw new Error(`Error running task ${name}: ${e}`);
@@ -139,6 +140,11 @@ async function executeJobCmd(name, args = [], options = {}) {
139
140
  else {
140
141
  try {
141
142
  const _p = i == 0 ? args : params;
143
+ let nextParams = {};
144
+ if (i == 0) {
145
+ const { vars } = initActionVars(args);
146
+ nextParams = vars;
147
+ }
142
148
  if (isDebug.value) {
143
149
  console.log(i + 1, "Running action", name, _p);
144
150
  }
@@ -147,6 +153,9 @@ async function executeJobCmd(name, args = [], options = {}) {
147
153
  }
148
154
  try {
149
155
  res = await job.runTask(name, _p, options);
156
+ if (res?.error) {
157
+ throw new Error(`Error executing job action ${res.error}`);
158
+ }
150
159
  }
151
160
  catch (e) {
152
161
  throw new Error(`Error executing job action ${e}`);
@@ -160,10 +169,10 @@ async function executeJobCmd(name, args = [], options = {}) {
160
169
  else {
161
170
  params = res.data;
162
171
  }
163
- params = res.data;
172
+ params = { ...res.data, ...nextParams };
164
173
  }
165
174
  catch (err) {
166
- return { error: `Error executing (${task.type}) task ${name}: ${err}` };
175
+ throw new Error(`Error executing (${task.type}) task ${name}: ${err}`);
167
176
  }
168
177
  }
169
178
  ++i;
@@ -241,7 +250,8 @@ async function _createJobFromSpec(spec) {
241
250
  if (t?.chain) {
242
251
  at.properties = { "chain": true };
243
252
  }
244
- tasks[t.name] = at;
253
+ const tsk = at;
254
+ tasks[t.name] = tsk;
245
255
  }
246
256
  }
247
257
  job.tasks = tasks;
@@ -1,2 +1,4 @@
1
- declare function executeTaskCmd(args?: Array<string> | Record<string, any>, options?: any): Promise<any>;
1
+ import { NodeReturnType } from "../../interfaces.js";
2
+ import { InferenceResult } from "@locallm/types/dist/interfaces.js";
3
+ declare function executeTaskCmd(args?: Array<string> | Record<string, any>, options?: any): Promise<NodeReturnType<InferenceResult>>;
2
4
  export { executeTaskCmd };
@@ -37,11 +37,10 @@ async function executeTaskCmd(args = [], options = {}) {
37
37
  name = args.name;
38
38
  delete args.name;
39
39
  pr = args.prompt;
40
- delete args.prompt;
41
40
  }
42
41
  const { found, path } = getFeatureSpec(name, "task");
43
42
  if (!found) {
44
- return { ok: false, data: "", conf: {}, error: `Task ${name} not found` };
43
+ return { data: {}, error: new Error(`Task ${name} not found`) };
45
44
  }
46
45
  const res = readTask(path);
47
46
  if (!res.found) {
@@ -85,17 +84,21 @@ async function executeTaskCmd(args = [], options = {}) {
85
84
  if (isDebug.value) {
86
85
  conf.debug = true;
87
86
  }
88
- const data = await task.run({ prompt: pr, ...vars }, conf);
89
- if (data?.error) {
90
- return { ok: false, data: "", conf: conf, error: `Error executing task: ${name} ${data.error}` };
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}`);
91
90
  }
92
91
  conf.prompt = pr;
93
92
  if (isChatMode.value) {
94
93
  if (brain.ex.name != ex.name) {
95
94
  brain.setDefaultExpert(ex);
96
95
  }
97
- brain.ex.template.pushToHistory({ user: pr, assistant: data.text });
96
+ brain.ex.template.pushToHistory({ user: pr, assistant: fres.text });
98
97
  }
99
- return { ok: true, data: data.text, conf: conf, error: "" };
98
+ const ir = fres;
99
+ if (isDebug.value) {
100
+ console.log("\n", ir.stats);
101
+ }
102
+ return { data: ir };
100
103
  }
101
104
  export { executeTaskCmd };
@@ -1,6 +1,6 @@
1
- import { AgentTask } from "@agent-smith/jobs/dist/jobsinterfaces.js";
1
+ import { AgentTask } from "@agent-smith/jobs";
2
2
  import { LmTask } from "@agent-smith/lmtask";
3
- import { FeatureType } from "../../interfaces.js";
3
+ import { FeatureType, NodeReturnType } from "../../interfaces.js";
4
4
  declare function setOptions(args: Array<string> | undefined, options: Record<string, any>): Promise<Array<string>>;
5
5
  declare function readPromptFile(): string;
6
6
  declare function processOutput(res: any): Promise<void>;
@@ -14,10 +14,11 @@ declare function initTaskParams(params: Record<string, any>, inferParams: Record
14
14
  conf: Record<string, any>;
15
15
  vars: Record<string, any>;
16
16
  };
17
+ declare function initActionVars(args: Array<any>): Record<string, any>;
17
18
  declare function initTaskVars(args: Array<any>, inferParams: Record<string, any>): {
18
19
  conf: Record<string, any>;
19
20
  vars: Record<string, any>;
20
21
  };
21
22
  declare function parseInputOptions(options: any): Promise<string | null>;
22
- declare function createJsAction(action: CallableFunction): AgentTask<FeatureType>;
23
- export { createJsAction, initTaskConf, initTaskParams, initTaskVars, parseInputOptions, processOutput, readPromptFile, readTask, readTasksDir, setOptions };
23
+ declare function createJsAction(action: CallableFunction): AgentTask<FeatureType, any, NodeReturnType<any>>;
24
+ export { createJsAction, initTaskConf, initTaskParams, initTaskVars, initActionVars, parseInputOptions, processOutput, readPromptFile, readTask, readTasksDir, setOptions };
@@ -44,7 +44,6 @@ async function processOutput(res) {
44
44
  let hasOutput = false;
45
45
  if (res?.data) {
46
46
  data = res.data;
47
- 0;
48
47
  hasOutput = true;
49
48
  }
50
49
  if (res?.text) {
@@ -129,15 +128,62 @@ function initTaskConf(conf, taskSpec) {
129
128
  function initTaskParams(params, inferParams) {
130
129
  const conf = { inferParams: inferParams };
131
130
  if (!params?.prompt) {
132
- throw new Error(`Error initializing task variable: provide a prompt`);
131
+ throw new Error(`Error initializing task params: provide a prompt`);
133
132
  }
134
133
  if (params?.images) {
135
134
  conf.inferParams.images = params.images;
136
135
  delete params.images;
137
136
  }
137
+ if (params?.size) {
138
+ conf.size = params.size;
139
+ delete params.size;
140
+ }
141
+ if (params?.model) {
142
+ conf.model = params.model;
143
+ delete params.model;
144
+ }
145
+ if (params?.template) {
146
+ conf.template = params.template;
147
+ delete params.template;
148
+ }
138
149
  const res = { conf: conf, vars: params };
139
150
  return res;
140
151
  }
152
+ function initActionVars(args) {
153
+ const vars = {};
154
+ args.forEach((a) => {
155
+ if (a.includes("=")) {
156
+ const t = a.split("=");
157
+ const k = t[0];
158
+ const v = t[1];
159
+ switch (k) {
160
+ case "m":
161
+ if (v.includes("/")) {
162
+ const _s = v.split("/");
163
+ vars.model = _s[0];
164
+ vars.template = _s[1];
165
+ }
166
+ else {
167
+ vars.model = v;
168
+ }
169
+ break;
170
+ case "ip":
171
+ v.split(",").forEach((p) => {
172
+ const s = p.split(":");
173
+ vars["inferParams"][s[0]] = parseFloat(s[1]);
174
+ });
175
+ break;
176
+ case "s":
177
+ vars.size = v;
178
+ break;
179
+ default:
180
+ vars[k] = v;
181
+ break;
182
+ }
183
+ }
184
+ });
185
+ return { vars };
186
+ }
141
187
  function initTaskVars(args, inferParams) {
142
188
  const conf = { inferParams: inferParams };
143
189
  const vars = {};
@@ -189,10 +235,16 @@ function createJsAction(action) {
189
235
  id: "",
190
236
  title: "",
191
237
  run: async (args) => {
192
- const res = await action(args);
193
- return { ok: true, data: res };
238
+ try {
239
+ const res = await action(args);
240
+ return { ok: true, data: res };
241
+ }
242
+ catch (e) {
243
+ const err = new Error(`Error executing action ${e}`);
244
+ return { ok: false, data: {}, error: err };
245
+ }
194
246
  }
195
247
  });
196
248
  return task;
197
249
  }
198
- export { createJsAction, initTaskConf, initTaskParams, initTaskVars, parseInputOptions, processOutput, readPromptFile, readTask, readTasksDir, setOptions };
250
+ export { createJsAction, initTaskConf, initTaskParams, initTaskVars, initActionVars, parseInputOptions, processOutput, readPromptFile, readTask, readTasksDir, setOptions };
@@ -1,3 +1,4 @@
1
+ import { NodeReturnType } from '../../interfaces.js';
1
2
  import { PythonShell } from 'python-shell';
2
- declare function runPyScript(rsShell: PythonShell, pythonPath: string, scriptPath: string, scriptArgs: Array<string>, handleOutputFrom?: "msg" | "stderr", onEmitLine?: CallableFunction): Promise<string[]>;
3
+ declare function runPyScript(rsShell: PythonShell, pythonPath: string, scriptPath: string, scriptArgs: Array<string>, onEmitLine?: CallableFunction): Promise<NodeReturnType<Array<string>>>;
3
4
  export { runPyScript };
@@ -1,5 +1,5 @@
1
1
  import { PythonShell } from 'python-shell';
2
- async function runPyScript(rsShell, pythonPath, scriptPath, scriptArgs, handleOutputFrom = "msg", onEmitLine) {
2
+ async function runPyScript(rsShell, pythonPath, scriptPath, scriptArgs, onEmitLine) {
3
3
  const _options = {
4
4
  mode: "text",
5
5
  pythonPath: pythonPath,
@@ -9,35 +9,24 @@ async function runPyScript(rsShell, pythonPath, scriptPath, scriptArgs, handleOu
9
9
  let promiseResolve;
10
10
  let promise = new Promise((resolve) => promiseResolve = resolve);
11
11
  rsShell = new PythonShell(scriptPath, _options);
12
- const msgs = new Array();
12
+ const res = { data: new Array() };
13
13
  function handleLine(msg) {
14
14
  if (onEmitLine) {
15
15
  onEmitLine(msg);
16
16
  }
17
- msgs.push(msg);
17
+ res.data.push(msg);
18
18
  }
19
19
  rsShell.on('message', function (message) {
20
- if (handleOutputFrom == "msg") {
21
- handleLine(message);
22
- }
23
- });
24
- rsShell.on('stderr', function (err) {
25
- console.log("STDERR", err);
26
- if (handleOutputFrom == "stderr") {
27
- handleLine(err);
28
- }
29
- else {
30
- promiseResolve(true);
31
- }
20
+ handleLine(message);
32
21
  });
33
22
  rsShell.on('pythonError', function (err) {
34
23
  console.log("PYERR", `${err.message}, ${err.traceback}`);
35
- promiseResolve(true);
24
+ res.error = new Error(err.traceback.toString());
36
25
  });
37
26
  rsShell.end(function (err, code, signal) {
38
27
  promiseResolve(true);
39
28
  });
40
29
  await promise;
41
- return msgs;
30
+ return res;
42
31
  }
43
32
  export { runPyScript };
@@ -35,6 +35,10 @@ interface ConfigFile {
35
35
  features?: Array<string>;
36
36
  plugins?: Array<string>;
37
37
  }
38
+ interface NodeReturnType<T = Record<string, any>> {
39
+ data: T;
40
+ error?: Error;
41
+ }
38
42
  type CmdExecutor = (args: Array<string>, options: any) => Promise<any>;
39
43
  type InputMode = "manual" | "promptfile" | "clipboard";
40
44
  type OutputMode = "txt" | "clipboard";
@@ -47,4 +51,4 @@ type JobExtension = "yml";
47
51
  type CmdExtension = "js";
48
52
  type FeatureExtension = TaskExtension | JobExtension | CmdExtension | ActionExtension;
49
53
  type AliasType = "task" | "action" | "job";
50
- export { Cmd, CmdExecutor, InputMode, OutputMode, RunMode, FormatMode, FeatureType, ActionExtension, TaskExtension, JobExtension, CmdExtension, FeatureSpec, Features, ConfigFile, FeatureExtension, AliasType, };
54
+ export { Cmd, CmdExecutor, NodeReturnType, InputMode, OutputMode, RunMode, FormatMode, FeatureType, ActionExtension, TaskExtension, JobExtension, CmdExtension, FeatureSpec, Features, ConfigFile, FeatureExtension, AliasType, };
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.35",
5
+ "version": "0.0.37",
6
6
  "scripts": {
7
7
  "buildrl": "rm -rf dist/* && rollup -c",
8
8
  "build": "rm -rf dist/* && tsc --noCheck",
@@ -11,8 +11,8 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@agent-smith/brain": "^0.0.38",
14
- "@agent-smith/jobs": "^0.0.12",
15
- "@agent-smith/lmtask": "^0.0.30",
14
+ "@agent-smith/jobs": "^0.0.13",
15
+ "@agent-smith/lmtask": "^0.0.31",
16
16
  "@agent-smith/tfm": "^0.1.1",
17
17
  "@inquirer/prompts": "^7.3.3",
18
18
  "@inquirer/select": "^4.0.10",
@@ -21,7 +21,7 @@
21
21
  "clipboardy": "^4.0.0",
22
22
  "commander": "^13.1.0",
23
23
  "marked-terminal": "^7.3.0",
24
- "modprompt": "^0.10.3",
24
+ "modprompt": "^0.10.4",
25
25
  "python-shell": "^5.0.0",
26
26
  "yaml": "^2.7.0"
27
27
  },