@agent-smith/cli 0.0.63 → 0.0.65
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.d.ts +2 -1
- package/dist/cli.js +7 -48
- package/dist/cmd/clicmds/aliases.d.ts +7 -0
- package/dist/cmd/clicmds/aliases.js +43 -0
- package/dist/cmd/clicmds/base.d.ts +3 -0
- package/dist/cmd/clicmds/base.js +94 -0
- package/dist/cmd/clicmds/cmds.d.ts +4 -7
- package/dist/cmd/clicmds/cmds.js +6 -184
- package/dist/cmd/clicmds/update.d.ts +2 -0
- package/dist/cmd/clicmds/update.js +46 -0
- package/dist/cmd/cmds.d.ts +3 -5
- package/dist/cmd/cmds.js +19 -53
- package/dist/cmd/lib/actions/cmd.d.ts +3 -2
- package/dist/cmd/lib/actions/cmd.js +67 -50
- package/dist/cmd/lib/actions/read.js +2 -2
- package/dist/cmd/lib/adaptaters/cmd.d.ts +2 -2
- package/dist/cmd/lib/adaptaters/cmd.js +3 -15
- package/dist/cmd/lib/models.d.ts +1 -1
- package/dist/cmd/lib/models.js +8 -6
- package/dist/cmd/lib/options_parsers.d.ts +7 -0
- package/dist/cmd/lib/options_parsers.js +41 -0
- package/dist/cmd/lib/tasks/cmd.d.ts +3 -2
- package/dist/cmd/lib/tasks/cmd.js +105 -63
- package/dist/cmd/lib/tasks/mcp.d.ts +13 -0
- package/dist/cmd/lib/tasks/mcp.js +57 -0
- package/dist/cmd/lib/tools.d.ts +8 -1
- package/dist/cmd/lib/tools.js +31 -1
- package/dist/cmd/lib/utils.d.ts +1 -2
- package/dist/cmd/lib/utils.js +1 -23
- package/dist/cmd/lib/workflows/cmd.d.ts +3 -2
- package/dist/cmd/lib/workflows/cmd.js +21 -27
- package/dist/cmd/options.d.ts +8 -0
- package/dist/cmd/options.js +62 -0
- package/dist/cmd/sys/read_cmds.d.ts +2 -2
- package/dist/cmd/sys/read_cmds.js +2 -2
- package/dist/db/db.d.ts +1 -1
- package/dist/db/db.js +3 -2
- package/dist/db/read.d.ts +3 -2
- package/dist/db/read.js +18 -12
- package/dist/db/schemas.js +12 -6
- package/dist/db/write.js +30 -13
- package/dist/index.js +14 -8
- package/dist/interfaces.d.ts +23 -6
- package/dist/main.d.ts +6 -6
- package/dist/main.js +6 -6
- package/dist/state/state.d.ts +2 -7
- package/dist/state/state.js +3 -11
- package/dist/utils/perf.d.ts +1 -1
- package/dist/utils/perf.js +6 -6
- package/package.json +10 -11
- package/dist/cmd/clicmds/modes.d.ts +0 -3
- package/dist/cmd/clicmds/modes.js +0 -95
- package/dist/utils/args.d.ts +0 -7
- package/dist/utils/args.js +0 -85
|
@@ -6,28 +6,73 @@ import { runPyScript } from "../../sys/run_python.js";
|
|
|
6
6
|
import { pyShell } from "../../../state/state.js";
|
|
7
7
|
import { createJsAction } from "./read.js";
|
|
8
8
|
import { runtimeError } from "../../../utils/user_msgs.js";
|
|
9
|
+
import { readClipboard } from "../../sys/clipboard.js";
|
|
10
|
+
import { processOutput, readPromptFile } from "../utils.js";
|
|
11
|
+
import { parseCommandArgs } from "../options_parsers.js";
|
|
12
|
+
async function executeAction(name, payload, options, quiet = false) {
|
|
13
|
+
let act;
|
|
14
|
+
const { found, path, ext } = getFeatureSpec(name, "action");
|
|
15
|
+
if (!found) {
|
|
16
|
+
throw new Error(`Action ${name} not found at ${path}`);
|
|
17
|
+
}
|
|
18
|
+
switch (ext) {
|
|
19
|
+
case "js":
|
|
20
|
+
const mjsa = await import(path);
|
|
21
|
+
act = createJsAction(mjsa.action);
|
|
22
|
+
break;
|
|
23
|
+
case "yml":
|
|
24
|
+
act = systemAction(path);
|
|
25
|
+
break;
|
|
26
|
+
case "py":
|
|
27
|
+
act = pythonAction(path);
|
|
28
|
+
break;
|
|
29
|
+
default:
|
|
30
|
+
throw new Error(`Action ext ${ext} not implemented`);
|
|
31
|
+
}
|
|
32
|
+
const res = await act.run(payload, options);
|
|
33
|
+
if (!quiet) {
|
|
34
|
+
if (res) {
|
|
35
|
+
console.log(res);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
await processOutput(res);
|
|
39
|
+
return res;
|
|
40
|
+
}
|
|
41
|
+
async function executeActionCmd(name, aargs, quiet = false) {
|
|
42
|
+
const { args, options } = parseCommandArgs(aargs);
|
|
43
|
+
const params = { args: args };
|
|
44
|
+
if (options?.clipBoardInput) {
|
|
45
|
+
params.args.push(await readClipboard());
|
|
46
|
+
}
|
|
47
|
+
else if (options?.inputFile) {
|
|
48
|
+
params.args.push(readPromptFile());
|
|
49
|
+
}
|
|
50
|
+
if (options?.debug) {
|
|
51
|
+
console.log("Action", name, "params", params);
|
|
52
|
+
}
|
|
53
|
+
return await executeAction(name, params, options, quiet);
|
|
54
|
+
}
|
|
9
55
|
function systemAction(path) {
|
|
10
56
|
const action = useAgentTask({
|
|
11
57
|
id: "system_action",
|
|
12
58
|
title: "",
|
|
13
|
-
run: async (
|
|
59
|
+
run: async (params) => {
|
|
14
60
|
let runArgs = new Array();
|
|
15
|
-
if (
|
|
61
|
+
if (params?.args) {
|
|
62
|
+
runArgs = params.args;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
16
65
|
try {
|
|
17
|
-
runArgs = Object.values(
|
|
66
|
+
runArgs = Object.values(params);
|
|
18
67
|
}
|
|
19
68
|
catch (e) {
|
|
20
69
|
throw new Error(`wrong system action args: ${e}`);
|
|
21
70
|
}
|
|
22
71
|
}
|
|
23
|
-
else {
|
|
24
|
-
runArgs = args;
|
|
25
|
-
}
|
|
26
72
|
const actionSpec = readYmlFile(path);
|
|
27
73
|
if (!actionSpec.found) {
|
|
28
74
|
runtimeError("System action yml file", path, "not found");
|
|
29
75
|
}
|
|
30
|
-
console.log("Yml action", JSON.stringify(actionSpec.data, null, " "));
|
|
31
76
|
if (!actionSpec.data?.args) {
|
|
32
77
|
actionSpec.data.args = [];
|
|
33
78
|
}
|
|
@@ -41,8 +86,20 @@ function pythonAction(path) {
|
|
|
41
86
|
const action = useAgentTask({
|
|
42
87
|
id: "python_action",
|
|
43
88
|
title: "",
|
|
44
|
-
run: async (
|
|
45
|
-
|
|
89
|
+
run: async (params) => {
|
|
90
|
+
let runArgs = new Array();
|
|
91
|
+
if (params?.args) {
|
|
92
|
+
runArgs = params.args;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
try {
|
|
96
|
+
runArgs = Object.values(params);
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
throw new Error(`wrong python action args: ${e}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const { data, error } = await runPyScript(pyShell, "python3", path, runArgs);
|
|
46
103
|
if (error) {
|
|
47
104
|
throw new Error(`python error: ${error}`);
|
|
48
105
|
}
|
|
@@ -63,44 +120,4 @@ function pythonAction(path) {
|
|
|
63
120
|
});
|
|
64
121
|
return action;
|
|
65
122
|
}
|
|
66
|
-
|
|
67
|
-
const isWorkflow = !Array.isArray(args);
|
|
68
|
-
let name;
|
|
69
|
-
if (!isWorkflow) {
|
|
70
|
-
name = args.shift();
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
if (!args.name) {
|
|
74
|
-
throw new Error("Provide an action name param");
|
|
75
|
-
}
|
|
76
|
-
name = args.name;
|
|
77
|
-
delete args.name;
|
|
78
|
-
}
|
|
79
|
-
const { found, path, ext } = getFeatureSpec(name, "action");
|
|
80
|
-
if (!found) {
|
|
81
|
-
throw new Error(`Action ${name} not found at ${path}`);
|
|
82
|
-
}
|
|
83
|
-
let act;
|
|
84
|
-
switch (ext) {
|
|
85
|
-
case "js":
|
|
86
|
-
const mjsa = await import(path);
|
|
87
|
-
act = createJsAction(mjsa.action);
|
|
88
|
-
break;
|
|
89
|
-
case "yml":
|
|
90
|
-
act = systemAction(path);
|
|
91
|
-
break;
|
|
92
|
-
case "py":
|
|
93
|
-
act = pythonAction(path);
|
|
94
|
-
break;
|
|
95
|
-
default:
|
|
96
|
-
throw new Error(`Action ext ${ext} not implemented`);
|
|
97
|
-
}
|
|
98
|
-
const res = await act.run(args, options);
|
|
99
|
-
if (!quiet) {
|
|
100
|
-
if (res) {
|
|
101
|
-
console.log(res);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return res;
|
|
105
|
-
}
|
|
106
|
-
export { executeActionCmd, systemAction, pythonAction };
|
|
123
|
+
export { executeAction, executeActionCmd, systemAction, pythonAction, };
|
|
@@ -3,9 +3,9 @@ function createJsAction(action) {
|
|
|
3
3
|
const task = useAgentTask({
|
|
4
4
|
id: "",
|
|
5
5
|
title: "",
|
|
6
|
-
run: async (args) => {
|
|
6
|
+
run: async (args, options) => {
|
|
7
7
|
try {
|
|
8
|
-
const res = await action(args);
|
|
8
|
+
const res = await action(args, options);
|
|
9
9
|
return res;
|
|
10
10
|
}
|
|
11
11
|
catch (e) {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare function
|
|
2
|
-
export {
|
|
1
|
+
declare function executeAdaptater(name: string, argsOrParams: Record<string, any> | Array<any>, options: Record<string, any>): Promise<any>;
|
|
2
|
+
export { executeAdaptater, };
|
|
@@ -1,18 +1,6 @@
|
|
|
1
1
|
import { getFeatureSpec } from "../../../state/features.js";
|
|
2
2
|
import { createJsAction } from "../actions/read.js";
|
|
3
|
-
async function
|
|
4
|
-
const isWorkflow = !Array.isArray(args);
|
|
5
|
-
let name;
|
|
6
|
-
if (!isWorkflow) {
|
|
7
|
-
name = args.shift();
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
if (!args.name) {
|
|
11
|
-
throw new Error("provide an adaptater name param");
|
|
12
|
-
}
|
|
13
|
-
name = args.name;
|
|
14
|
-
delete args.name;
|
|
15
|
-
}
|
|
3
|
+
async function executeAdaptater(name, argsOrParams, options) {
|
|
16
4
|
const { found, path } = getFeatureSpec(name, "adaptater");
|
|
17
5
|
if (!found) {
|
|
18
6
|
throw new Error(`adaptater ${name} not found`);
|
|
@@ -22,7 +10,7 @@ async function executeAdaptaterCmd(args = [], options = {}) {
|
|
|
22
10
|
act = createJsAction(jsa.action);
|
|
23
11
|
let res;
|
|
24
12
|
try {
|
|
25
|
-
res = await act.run(
|
|
13
|
+
res = await act.run(argsOrParams, options);
|
|
26
14
|
}
|
|
27
15
|
catch (e) {
|
|
28
16
|
throw new Error(`adaptater ${name}: ${e}`);
|
|
@@ -32,4 +20,4 @@ async function executeAdaptaterCmd(args = [], options = {}) {
|
|
|
32
20
|
}
|
|
33
21
|
return res;
|
|
34
22
|
}
|
|
35
|
-
export {
|
|
23
|
+
export { executeAdaptater, };
|
package/dist/cmd/lib/models.d.ts
CHANGED
package/dist/cmd/lib/models.js
CHANGED
|
@@ -3,19 +3,21 @@ import { readModelfiles, readModels } from "../../db/read.js";
|
|
|
3
3
|
import { readModelsFile } from "../sys/read_modelfile.js";
|
|
4
4
|
import { updateModels as dbUpdateModels } from "../../db/write.js";
|
|
5
5
|
import color from "ansi-colors";
|
|
6
|
-
async function showModelsCmd(args
|
|
6
|
+
async function showModelsCmd(args) {
|
|
7
7
|
let models = readModels();
|
|
8
8
|
models.sort((a, b) => a.name.localeCompare(b.name));
|
|
9
|
+
let foundModels = new Array();
|
|
9
10
|
if (args.length > 0) {
|
|
10
11
|
args.forEach((a) => {
|
|
11
|
-
|
|
12
|
+
const fm = models.filter((m) => m.shortname.includes(a) || m.name.includes(a));
|
|
13
|
+
foundModels.push(...fm);
|
|
12
14
|
});
|
|
13
15
|
}
|
|
14
|
-
|
|
16
|
+
else {
|
|
17
|
+
foundModels = models;
|
|
18
|
+
}
|
|
19
|
+
foundModels.forEach((model) => {
|
|
15
20
|
const ips = model.data.inferParams;
|
|
16
|
-
if (!ips?.max_tokens) {
|
|
17
|
-
throw new Error(`no max tokens in ${model.shortname}`);
|
|
18
|
-
}
|
|
19
21
|
const mt = ips.max_tokens;
|
|
20
22
|
delete ips.max_tokens;
|
|
21
23
|
const vip = Object.keys(ips).length > 0 ? JSON.stringify(ips) : "";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { LmTaskConfig } from "../../interfaces.js";
|
|
2
|
+
declare function parseCommandArgs(args: Array<any>): {
|
|
3
|
+
args: Array<any>;
|
|
4
|
+
options: Record<string, any>;
|
|
5
|
+
};
|
|
6
|
+
declare function parseTaskConfigOptions(options: Record<string, any>): LmTaskConfig;
|
|
7
|
+
export { parseCommandArgs, parseTaskConfigOptions, };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
function parseCommandArgs(args) {
|
|
2
|
+
const res = {
|
|
3
|
+
args: new Array(),
|
|
4
|
+
options: {},
|
|
5
|
+
};
|
|
6
|
+
args.pop();
|
|
7
|
+
res.options = args.pop();
|
|
8
|
+
res.args = Array.isArray(args[0]) ? args[0] : args;
|
|
9
|
+
return res;
|
|
10
|
+
}
|
|
11
|
+
function parseTaskConfigOptions(options) {
|
|
12
|
+
const conf = { inferParams: {}, modelname: "", templateName: "" };
|
|
13
|
+
const optionsInferParams = {};
|
|
14
|
+
if (options?.temperature) {
|
|
15
|
+
optionsInferParams.temperature = options.temperature;
|
|
16
|
+
}
|
|
17
|
+
if (options?.top_k !== undefined) {
|
|
18
|
+
optionsInferParams.top_k = options.top_k;
|
|
19
|
+
}
|
|
20
|
+
if (options?.top_p !== undefined) {
|
|
21
|
+
optionsInferParams.top_p = options.top_p;
|
|
22
|
+
}
|
|
23
|
+
if (options?.min_p !== undefined) {
|
|
24
|
+
optionsInferParams.min_p = options.min_p;
|
|
25
|
+
}
|
|
26
|
+
if (options?.max_tokens !== undefined) {
|
|
27
|
+
optionsInferParams.max_tokens = options.max_tokens;
|
|
28
|
+
}
|
|
29
|
+
if (options?.repeat_penalty !== undefined) {
|
|
30
|
+
optionsInferParams.repeat_penalty = options.repeat_penalty;
|
|
31
|
+
}
|
|
32
|
+
if (options?.model !== undefined) {
|
|
33
|
+
conf.modelname = options.model;
|
|
34
|
+
}
|
|
35
|
+
if (options?.template !== undefined) {
|
|
36
|
+
conf.templateName = options.template;
|
|
37
|
+
}
|
|
38
|
+
conf.inferParams = optionsInferParams;
|
|
39
|
+
return conf;
|
|
40
|
+
}
|
|
41
|
+
export { parseCommandArgs, parseTaskConfigOptions, };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { LmTaskOutput } from "@agent-smith/lmtask";
|
|
2
|
-
declare function
|
|
3
|
-
|
|
2
|
+
declare function executeTask(name: string, payload: Record<string, any>, options: Record<string, any>, quiet?: boolean): Promise<LmTaskOutput>;
|
|
3
|
+
declare function executeTaskCmd(name: string, targs?: Array<any>): Promise<LmTaskOutput>;
|
|
4
|
+
export { executeTask, executeTaskCmd };
|
|
@@ -1,57 +1,62 @@
|
|
|
1
1
|
import { compile, serializeGrammar } from "@intrinsicai/gbnfgen";
|
|
2
|
+
import color from "ansi-colors";
|
|
2
3
|
import ora from 'ora';
|
|
3
4
|
import { brain, initAgent, taskBuilder } from "../../../agent.js";
|
|
5
|
+
import { readClipboard } from "../../../cmd/sys/clipboard.js";
|
|
4
6
|
import { readTool } from "../../../db/read.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
7
|
+
import { usePerfTimer } from "../../../main.js";
|
|
8
|
+
import { isChatMode } from "../../../state/state.js";
|
|
9
|
+
import { executeAction } from "../actions/cmd.js";
|
|
10
|
+
import { parseCommandArgs, parseTaskConfigOptions } from "../options_parsers.js";
|
|
11
|
+
import { runtimeDataError } from "../user_msgs.js";
|
|
12
|
+
import { formatStats, readPromptFile } from "../utils.js";
|
|
13
|
+
import { executeWorkflow } from "../workflows/cmd.js";
|
|
14
|
+
import { configureTaskModel, mergeInferParams } from "./conf.js";
|
|
11
15
|
import { openTaskSpec } from "./utils.js";
|
|
12
|
-
|
|
16
|
+
import { McpServer } from "./mcp.js";
|
|
17
|
+
async function executeTask(name, payload, options, quiet = false) {
|
|
13
18
|
await initAgent();
|
|
14
|
-
if (
|
|
15
|
-
console.log("
|
|
19
|
+
if (options.debug) {
|
|
20
|
+
console.log("Payload:", payload);
|
|
16
21
|
console.log("Task options:", options);
|
|
17
22
|
}
|
|
18
|
-
const isWorkflow = !Array.isArray(args);
|
|
19
|
-
let name;
|
|
20
|
-
let pr;
|
|
21
|
-
if (!isWorkflow) {
|
|
22
|
-
name = args.shift();
|
|
23
|
-
const _pr = await parseInputOptions(options);
|
|
24
|
-
if (!_pr) {
|
|
25
|
-
const p = args.shift();
|
|
26
|
-
if (p) {
|
|
27
|
-
pr = p;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
throw new Error("Please provide a prompt");
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
pr = _pr;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
if (!(args?.name)) {
|
|
39
|
-
throw new Error("Provide a task name param");
|
|
40
|
-
}
|
|
41
|
-
if (!(args?.prompt)) {
|
|
42
|
-
throw new Error("Provide a task prompt param");
|
|
43
|
-
}
|
|
44
|
-
name = args.name;
|
|
45
|
-
pr = args.prompt;
|
|
46
|
-
}
|
|
47
23
|
const taskFileSpec = openTaskSpec(name);
|
|
48
|
-
|
|
49
|
-
|
|
24
|
+
const conf = parseTaskConfigOptions(options);
|
|
25
|
+
if (options.debug) {
|
|
26
|
+
console.log("conf:", conf);
|
|
27
|
+
}
|
|
50
28
|
conf.inferParams = mergeInferParams(conf.inferParams, taskFileSpec.inferParams ?? {});
|
|
51
29
|
const model = configureTaskModel(conf, taskFileSpec);
|
|
30
|
+
if (options?.ctx) {
|
|
31
|
+
model.ctx = options.ctx;
|
|
32
|
+
}
|
|
52
33
|
const taskSpec = taskFileSpec;
|
|
53
|
-
|
|
34
|
+
let vars = {};
|
|
35
|
+
taskSpec.variables?.optional?.forEach(k => {
|
|
36
|
+
if (k in options) {
|
|
37
|
+
vars[k] = options[k];
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
taskSpec.variables?.required?.forEach(k => {
|
|
41
|
+
if (k in options) {
|
|
42
|
+
vars[k] = options[k];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const mcpServers = new Array();
|
|
46
|
+
if (taskFileSpec?.mcp) {
|
|
54
47
|
taskSpec.tools = [];
|
|
48
|
+
for (const tool of Object.values(taskFileSpec.mcp)) {
|
|
49
|
+
const mcp = new McpServer(tool.command, tool.arguments, tool?.tools ?? null);
|
|
50
|
+
mcpServers.push(mcp);
|
|
51
|
+
await mcp.start();
|
|
52
|
+
const tools = await mcp.extractTools();
|
|
53
|
+
tools.forEach(t => taskSpec.tools?.push(t));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (taskSpec.toolsList) {
|
|
57
|
+
if (!taskSpec?.tools) {
|
|
58
|
+
taskSpec.tools = [];
|
|
59
|
+
}
|
|
55
60
|
for (const toolName of taskSpec.toolsList) {
|
|
56
61
|
const { found, tool, type } = readTool(toolName);
|
|
57
62
|
if (!found) {
|
|
@@ -59,21 +64,17 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
59
64
|
}
|
|
60
65
|
const lmTool = {
|
|
61
66
|
...tool,
|
|
62
|
-
execute: async (
|
|
63
|
-
const normalizedArgs = Array.isArray(args) ? [toolName, ...args] : {
|
|
64
|
-
name: toolName,
|
|
65
|
-
...args,
|
|
66
|
-
};
|
|
67
|
+
execute: async (params) => {
|
|
67
68
|
switch (type) {
|
|
68
69
|
case "action":
|
|
69
|
-
const res = await
|
|
70
|
+
const res = await executeAction(toolName, params, options, true);
|
|
70
71
|
return res;
|
|
71
72
|
case "task":
|
|
72
|
-
conf.quiet = !
|
|
73
|
-
const tres = await
|
|
73
|
+
conf.quiet = !options?.debug;
|
|
74
|
+
const tres = await executeTask(name, params, options, true);
|
|
74
75
|
return tres.answer.text;
|
|
75
76
|
case "workflow":
|
|
76
|
-
const wres = await
|
|
77
|
+
const wres = await executeWorkflow(toolName, params, options);
|
|
77
78
|
return wres;
|
|
78
79
|
default:
|
|
79
80
|
throw new Error(`unknown tool execution function type: ${type} for ${toolName}`);
|
|
@@ -90,9 +91,8 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
90
91
|
model.inferParams.grammar = serializeGrammar(await compile(model.inferParams.tsGrammar, "Grammar"));
|
|
91
92
|
delete model.inferParams.tsGrammar;
|
|
92
93
|
}
|
|
93
|
-
if (
|
|
94
|
+
if (options.debug) {
|
|
94
95
|
console.log("Task model:", model);
|
|
95
|
-
console.log("Task vars:", vars);
|
|
96
96
|
}
|
|
97
97
|
const ex = brain.getOrCreateExpertForModel(model.name, model.template);
|
|
98
98
|
if (!ex) {
|
|
@@ -104,7 +104,7 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
104
104
|
const hasThink = ex.template?.tags?.think;
|
|
105
105
|
const hasTools = ex.template?.tags?.toolCall;
|
|
106
106
|
const printToken = (t) => {
|
|
107
|
-
if (
|
|
107
|
+
if (options?.tokens === true) {
|
|
108
108
|
let txt = t;
|
|
109
109
|
txt = c ? t : `\x1b[100m${t}\x1b[0m`;
|
|
110
110
|
process.stdout.write(txt);
|
|
@@ -116,12 +116,23 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
116
116
|
}
|
|
117
117
|
};
|
|
118
118
|
let processToken = printToken;
|
|
119
|
-
if ((hasThink || hasTools) && !
|
|
119
|
+
if ((hasThink || hasTools) && !options?.debug) {
|
|
120
120
|
let continueWrite = true;
|
|
121
121
|
let skipNextEmptyLinesToken = false;
|
|
122
122
|
const spinner = ora("Thinking ...");
|
|
123
|
+
const ts = "Thinking";
|
|
124
|
+
const te = color.dim("tokens");
|
|
125
|
+
const formatTokenCount = (i) => {
|
|
126
|
+
return `${ts} ${color.bold(i.toString())} ${te}`;
|
|
127
|
+
};
|
|
128
|
+
const perfTimer = usePerfTimer(false);
|
|
129
|
+
let i = 0;
|
|
123
130
|
processToken = (t) => {
|
|
124
|
-
if (
|
|
131
|
+
if (i == 0) {
|
|
132
|
+
perfTimer.start();
|
|
133
|
+
}
|
|
134
|
+
spinner.text = formatTokenCount(i);
|
|
135
|
+
if (!options?.verbose) {
|
|
125
136
|
if (hasThink) {
|
|
126
137
|
if (t == ex.template.tags.think?.start) {
|
|
127
138
|
spinner.start();
|
|
@@ -131,18 +142,27 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
131
142
|
else if (t == ex.template.tags.think?.end) {
|
|
132
143
|
continueWrite = true;
|
|
133
144
|
skipNextEmptyLinesToken = true;
|
|
134
|
-
|
|
145
|
+
let msg = color.dim("Thinking:") + ` ${i} ${color.dim("tokens")}`;
|
|
146
|
+
msg = msg + " " + color.dim(perfTimer.time());
|
|
147
|
+
spinner.info(msg);
|
|
135
148
|
return;
|
|
136
149
|
}
|
|
137
150
|
}
|
|
138
151
|
}
|
|
152
|
+
else {
|
|
153
|
+
if (t == ex.template.tags.think?.end) {
|
|
154
|
+
let msg = color.dim("Thinking:") + ` ${i} ${color.dim("tokens")}`;
|
|
155
|
+
msg = msg + " " + color.dim(perfTimer.time());
|
|
156
|
+
console.log(msg);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
139
159
|
if (hasTools) {
|
|
140
160
|
if (t == ex.template.tags.toolCall?.start) {
|
|
141
161
|
continueWrite = false;
|
|
142
162
|
return;
|
|
143
163
|
}
|
|
144
164
|
else if (t == ex.template.tags.toolCall?.end) {
|
|
145
|
-
if (
|
|
165
|
+
if (options?.verbose === true) {
|
|
146
166
|
skipNextEmptyLinesToken = true;
|
|
147
167
|
continueWrite = true;
|
|
148
168
|
}
|
|
@@ -162,7 +182,7 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
162
182
|
};
|
|
163
183
|
}
|
|
164
184
|
const onToolCall = (tc) => {
|
|
165
|
-
console.log("⚒️ ",
|
|
185
|
+
console.log("⚒️ ", color.bold(name), "=>", `${color.yellowBright(tc.name)}`, tc.arguments);
|
|
166
186
|
};
|
|
167
187
|
if (options?.onToken) {
|
|
168
188
|
ex.backend.setOnToken(options.onToken);
|
|
@@ -173,14 +193,14 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
173
193
|
const tconf = {
|
|
174
194
|
expert: ex,
|
|
175
195
|
model: model,
|
|
176
|
-
debug:
|
|
196
|
+
debug: options.debug,
|
|
177
197
|
onToolCall: onToolCall,
|
|
178
198
|
...conf,
|
|
179
199
|
};
|
|
180
200
|
tconf.expert = ex;
|
|
181
201
|
let out;
|
|
182
202
|
try {
|
|
183
|
-
out = await task.run({ prompt:
|
|
203
|
+
out = await task.run({ prompt: payload.prompt, ...vars }, tconf);
|
|
184
204
|
if (!out.answer.text.endsWith("\n")) {
|
|
185
205
|
console.log();
|
|
186
206
|
}
|
|
@@ -188,15 +208,37 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
188
208
|
catch (err) {
|
|
189
209
|
throw new Error(`executing task: ${name} (${err})`);
|
|
190
210
|
}
|
|
211
|
+
mcpServers.forEach(async (s) => await s.stop());
|
|
191
212
|
if (isChatMode.value) {
|
|
192
213
|
if (brain.ex.name != ex.name) {
|
|
193
214
|
brain.setDefaultExpert(ex);
|
|
194
215
|
}
|
|
195
|
-
brain.ex.template.pushToHistory({ user:
|
|
216
|
+
brain.ex.template.pushToHistory({ user: payload.prompt, assistant: out.answer.text });
|
|
196
217
|
}
|
|
197
|
-
if (
|
|
218
|
+
if (options?.debug === true || options?.verbose === true) {
|
|
198
219
|
console.log("\n", formatStats(out.answer.stats));
|
|
199
220
|
}
|
|
200
221
|
return out;
|
|
201
222
|
}
|
|
202
|
-
|
|
223
|
+
async function executeTaskCmd(name, targs = []) {
|
|
224
|
+
const { args, options } = parseCommandArgs(targs);
|
|
225
|
+
let pr;
|
|
226
|
+
if (options?.clipboardInput === true) {
|
|
227
|
+
pr = await readClipboard();
|
|
228
|
+
}
|
|
229
|
+
else if (options?.inputFile === true) {
|
|
230
|
+
pr = readPromptFile();
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
if (args[0] !== undefined) {
|
|
234
|
+
pr = args[0];
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
runtimeDataError("task", name, "provide a prompt or use input options");
|
|
238
|
+
throw new Error();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const params = { args: args, prompt: pr };
|
|
242
|
+
return await executeTask(name, params, options);
|
|
243
|
+
}
|
|
244
|
+
export { executeTask, executeTaskCmd };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
3
|
+
import { LmTaskToolSpec } from "@agent-smith/lmtask/dist/interfaces.js";
|
|
4
|
+
declare class McpServer {
|
|
5
|
+
transport: StdioClientTransport;
|
|
6
|
+
client: Client;
|
|
7
|
+
authorizedTools: Array<string> | null;
|
|
8
|
+
constructor(command: string, args: Array<string>, authorizedTools?: string[]);
|
|
9
|
+
start(): Promise<void>;
|
|
10
|
+
stop(): Promise<void>;
|
|
11
|
+
extractTools(): Promise<Array<LmTaskToolSpec>>;
|
|
12
|
+
}
|
|
13
|
+
export { McpServer, };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
3
|
+
class McpServer {
|
|
4
|
+
transport;
|
|
5
|
+
client;
|
|
6
|
+
authorizedTools = null;
|
|
7
|
+
constructor(command, args, authorizedTools = new Array) {
|
|
8
|
+
this.transport = new StdioClientTransport({
|
|
9
|
+
command: command,
|
|
10
|
+
args: args
|
|
11
|
+
});
|
|
12
|
+
this.client = new Client({ name: "AgentSmith", version: "1.0.0" });
|
|
13
|
+
if (authorizedTools) {
|
|
14
|
+
this.authorizedTools = authorizedTools;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async start() {
|
|
18
|
+
await this.client.connect(this.transport);
|
|
19
|
+
}
|
|
20
|
+
async stop() {
|
|
21
|
+
await this.client.close();
|
|
22
|
+
}
|
|
23
|
+
async extractTools() {
|
|
24
|
+
const toolSpecs = new Array();
|
|
25
|
+
const serverToolsList = await this.client.listTools();
|
|
26
|
+
for (const tool of serverToolsList.tools) {
|
|
27
|
+
if (this.authorizedTools) {
|
|
28
|
+
if (!this.authorizedTools.includes(tool.name)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const args = {};
|
|
33
|
+
if (tool.inputSchema.properties) {
|
|
34
|
+
for (const [k, v] of Object.entries(tool.inputSchema.properties)) {
|
|
35
|
+
const vv = v;
|
|
36
|
+
args[k] = { description: vv.description + " (" + vv.type + ")" };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const exec = async (args) => {
|
|
40
|
+
const res = await this.client.callTool({
|
|
41
|
+
name: tool.name,
|
|
42
|
+
arguments: args,
|
|
43
|
+
});
|
|
44
|
+
return res;
|
|
45
|
+
};
|
|
46
|
+
const t = {
|
|
47
|
+
name: tool.name,
|
|
48
|
+
description: tool.description ?? "",
|
|
49
|
+
arguments: args,
|
|
50
|
+
execute: exec
|
|
51
|
+
};
|
|
52
|
+
toolSpecs.push(t);
|
|
53
|
+
}
|
|
54
|
+
return toolSpecs;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export { McpServer, };
|
package/dist/cmd/lib/tools.d.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { FeatureExtension } from '../../interfaces.js';
|
|
2
|
+
declare function extractTaskToolDocAndVariables(name: string, ext: FeatureExtension, dirPath: string): {
|
|
3
|
+
toolDoc: string;
|
|
4
|
+
variables: {
|
|
5
|
+
required: Array<string>;
|
|
6
|
+
optional: Array<string>;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
2
9
|
declare function extractToolDoc(name: string, ext: FeatureExtension, dirPath: string): {
|
|
3
10
|
found: boolean;
|
|
4
11
|
toolDoc: string;
|
|
5
12
|
};
|
|
6
|
-
export { extractToolDoc, };
|
|
13
|
+
export { extractToolDoc, extractTaskToolDocAndVariables, };
|