@agent-smith/cli 0.0.71 → 0.0.81
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/cmd/clicmds/aliases.js +12 -6
- package/dist/cmd/clicmds/base.js +13 -7
- package/dist/cmd/cmds.js +0 -20
- package/dist/cmd/lib/actions/cmd.d.ts +3 -4
- package/dist/cmd/lib/actions/cmd.js +48 -64
- package/dist/cmd/lib/actions/read.d.ts +2 -3
- package/dist/cmd/lib/actions/read.js +9 -14
- package/dist/cmd/lib/adaptaters/cmd.d.ts +1 -1
- package/dist/cmd/lib/adaptaters/cmd.js +7 -4
- package/dist/cmd/lib/mcp.d.ts +3 -3
- package/dist/cmd/lib/options_parsers.js +1 -1
- package/dist/cmd/lib/tasks/cmd.d.ts +3 -4
- package/dist/cmd/lib/tasks/cmd.js +98 -72
- package/dist/cmd/lib/tasks/read.d.ts +5 -6
- package/dist/cmd/lib/tasks/read.js +19 -16
- package/dist/cmd/lib/utils.js +3 -1
- package/dist/cmd/lib/workflows/cmd.js +9 -6
- package/dist/cmd/lib/workflows/read.d.ts +3 -4
- package/dist/cmd/lib/workflows/read.js +37 -15
- package/dist/conf.js +45 -9
- package/dist/const.d.ts +3 -0
- package/dist/const.js +24 -0
- package/dist/db/read.d.ts +3 -3
- package/dist/db/read.js +4 -4
- package/dist/db/schemas.js +3 -2
- package/dist/db/write.d.ts +4 -3
- package/dist/db/write.js +36 -12
- package/dist/index.js +2 -4
- package/dist/interfaces.d.ts +21 -16
- package/dist/main.d.ts +1 -2
- package/dist/main.js +1 -2
- package/dist/state/backends.d.ts +7 -0
- package/dist/state/backends.js +128 -0
- package/dist/utils/perf.js +1 -1
- package/dist/utils/user_msgs.js +5 -5
- package/package.json +20 -21
- package/dist/agent.d.ts +0 -8
- package/dist/agent.js +0 -24
- package/dist/cmd/backends.d.ts +0 -3
- package/dist/cmd/backends.js +0 -20
|
@@ -13,12 +13,18 @@ function initCommandsFromAliases(program, aliases, features) {
|
|
|
13
13
|
});
|
|
14
14
|
allOptions.forEach(o => tcmd.addOption(o));
|
|
15
15
|
if (features.task[alias.name]?.variables) {
|
|
16
|
-
features.task[alias.name].variables?.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
16
|
+
const rtv = features.task[alias.name].variables?.required;
|
|
17
|
+
if (rtv) {
|
|
18
|
+
for (const name of Object.keys(rtv)) {
|
|
19
|
+
tcmd.option(`--${name} <value>`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const otv = features.task[alias.name].variables?.optional;
|
|
23
|
+
if (otv) {
|
|
24
|
+
for (const name of Object.keys(otv)) {
|
|
25
|
+
tcmd.option(`--${name} <value>`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
22
28
|
}
|
|
23
29
|
break;
|
|
24
30
|
case "action":
|
package/dist/cmd/clicmds/base.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import YAML from 'yaml';
|
|
2
|
-
import { initAgent, taskBuilder } from "../../agent.js";
|
|
3
2
|
import { dbPath } from "../../conf.js";
|
|
4
3
|
import { readFeaturePaths, readFeaturesType } from "../../db/read.js";
|
|
5
4
|
import { cleanupFeaturePaths, updateAliases, updateFeatures } from "../../db/write.js";
|
|
@@ -11,11 +10,8 @@ import { parseCommandArgs } from "../lib/options_parsers.js";
|
|
|
11
10
|
import { deleteFileIfExists } from "../sys/delete_file.js";
|
|
12
11
|
import { readTask } from "../sys/read_task.js";
|
|
13
12
|
import { updateConfCmd } from "./update.js";
|
|
14
|
-
import {
|
|
13
|
+
import { listBackends, setBackend } from "../../state/backends.js";
|
|
15
14
|
function initBaseCommands(program) {
|
|
16
|
-
program.command("ping")
|
|
17
|
-
.description("ping inference servers")
|
|
18
|
-
.action(async (...args) => { console.log("Found working inference server(s):", await initAgent(initRemoteBackends())); });
|
|
19
15
|
program.command("exit")
|
|
20
16
|
.description("exit the cli")
|
|
21
17
|
.action(() => process.exit(0));
|
|
@@ -37,6 +33,17 @@ function initBaseCommands(program) {
|
|
|
37
33
|
const ca = parseCommandArgs(args);
|
|
38
34
|
await showModelsCmd(ca.args);
|
|
39
35
|
});
|
|
36
|
+
program.command("backend <name>")
|
|
37
|
+
.description("set the default backend")
|
|
38
|
+
.action(async (...args) => {
|
|
39
|
+
const ca = parseCommandArgs(args);
|
|
40
|
+
await setBackend(ca.args[0]);
|
|
41
|
+
});
|
|
42
|
+
program.command("backends")
|
|
43
|
+
.description("list the available backends")
|
|
44
|
+
.action(async (...args) => {
|
|
45
|
+
await listBackends();
|
|
46
|
+
});
|
|
40
47
|
program.command("update")
|
|
41
48
|
.description("update the available features: run this after adding a new feature")
|
|
42
49
|
.action(async (...args) => {
|
|
@@ -89,7 +96,6 @@ async function _readTaskCmd(args) {
|
|
|
89
96
|
if (!res.found) {
|
|
90
97
|
throw new Error(`Task ${args[0]}, ${path} not found`);
|
|
91
98
|
}
|
|
92
|
-
|
|
93
|
-
console.log(YAML.stringify(ts));
|
|
99
|
+
console.log(YAML.stringify(res.ymlTask));
|
|
94
100
|
}
|
|
95
101
|
export { initBaseCommands };
|
package/dist/cmd/cmds.js
CHANGED
|
@@ -1,30 +1,10 @@
|
|
|
1
|
-
import { input } from "@inquirer/prompts";
|
|
2
|
-
import { toRaw } from "@vue/reactivity";
|
|
3
1
|
import { Command } from "commander";
|
|
4
|
-
import { brain } from "../agent.js";
|
|
5
|
-
import { query } from "../cli.js";
|
|
6
2
|
import { readAliases, readFeatures } from "../db/read.js";
|
|
7
|
-
import { chatInferenceParams } from "../state/chat.js";
|
|
8
|
-
import { isChatMode, runMode } from "../state/state.js";
|
|
9
3
|
import { initCommandsFromAliases } from "./clicmds/aliases.js";
|
|
10
4
|
import { initBaseCommands } from "./clicmds/base.js";
|
|
11
5
|
import { initUserCmds } from "./clicmds/cmds.js";
|
|
12
6
|
const program = new Command();
|
|
13
7
|
async function chat(program) {
|
|
14
|
-
const data = { message: '>', default: "" };
|
|
15
|
-
const prompt = await input(data);
|
|
16
|
-
if (prompt == "/q") {
|
|
17
|
-
isChatMode.value = false;
|
|
18
|
-
if (runMode.value == "cmd") {
|
|
19
|
-
process.exit(0);
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
await query(program);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
await brain.ex.think(prompt, toRaw(chatInferenceParams));
|
|
26
|
-
console.log();
|
|
27
|
-
await chat(program);
|
|
28
8
|
}
|
|
29
9
|
async function buildCmds() {
|
|
30
10
|
initBaseCommands(program);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { FeatureType } from "../../../interfaces.js";
|
|
1
|
+
import { FeatureExecutor } from "../../../interfaces.js";
|
|
3
2
|
declare function executeAction(name: string, payload: Record<string, any>, options: Record<string, any>, quiet?: boolean): Promise<any>;
|
|
4
3
|
declare function executeActionCmd(name: string, aargs: Array<any>, quiet?: boolean): Promise<any>;
|
|
5
|
-
declare function systemAction(path: string):
|
|
6
|
-
declare function pythonAction(path: string):
|
|
4
|
+
declare function systemAction(path: string): FeatureExecutor<Array<string>, any>;
|
|
5
|
+
declare function pythonAction(path: string): FeatureExecutor<Array<string>>;
|
|
7
6
|
export { executeAction, executeActionCmd, systemAction, pythonAction, };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { useAgentTask } from "@agent-smith/jobs";
|
|
2
1
|
import { getFeatureSpec } from '../../../state/features.js';
|
|
3
2
|
import { readYmlFile } from "../../sys/read_yml_file.js";
|
|
4
3
|
import { execute } from "../../sys/execute.js";
|
|
@@ -10,7 +9,7 @@ import { readClipboard } from "../../sys/clipboard.js";
|
|
|
10
9
|
import { processOutput, readPromptFile } from "../utils.js";
|
|
11
10
|
import { parseCommandArgs } from "../options_parsers.js";
|
|
12
11
|
async function executeAction(name, payload, options, quiet = false) {
|
|
13
|
-
let
|
|
12
|
+
let run;
|
|
14
13
|
const { found, path, ext } = getFeatureSpec(name, "action");
|
|
15
14
|
if (!found) {
|
|
16
15
|
throw new Error(`Action ${name} not found at ${path}`);
|
|
@@ -18,18 +17,22 @@ async function executeAction(name, payload, options, quiet = false) {
|
|
|
18
17
|
switch (ext) {
|
|
19
18
|
case "js":
|
|
20
19
|
const mjsa = await import(path);
|
|
21
|
-
|
|
20
|
+
run = createJsAction(mjsa.action);
|
|
22
21
|
break;
|
|
23
22
|
case "yml":
|
|
24
|
-
|
|
23
|
+
run = systemAction(path);
|
|
25
24
|
break;
|
|
26
25
|
case "py":
|
|
27
|
-
|
|
26
|
+
run = pythonAction(path);
|
|
28
27
|
break;
|
|
29
28
|
default:
|
|
30
29
|
throw new Error(`Action ext ${ext} not implemented`);
|
|
31
30
|
}
|
|
32
|
-
|
|
31
|
+
let _pl = payload;
|
|
32
|
+
if (payload?.args) {
|
|
33
|
+
_pl = payload.args;
|
|
34
|
+
}
|
|
35
|
+
const res = await run(_pl, options);
|
|
33
36
|
if (!quiet) {
|
|
34
37
|
if (res) {
|
|
35
38
|
console.log(res);
|
|
@@ -53,71 +56,52 @@ async function executeActionCmd(name, aargs, quiet = false) {
|
|
|
53
56
|
return await executeAction(name, params, options, quiet);
|
|
54
57
|
}
|
|
55
58
|
function systemAction(path) {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
else {
|
|
65
|
-
try {
|
|
66
|
-
runArgs = Object.values(params);
|
|
67
|
-
}
|
|
68
|
-
catch (e) {
|
|
69
|
-
throw new Error(`wrong system action args: ${e}`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
const actionSpec = readYmlFile(path);
|
|
73
|
-
if (!actionSpec.found) {
|
|
74
|
-
runtimeError("System action yml file", path, "not found");
|
|
75
|
-
}
|
|
76
|
-
if (!actionSpec.data?.args) {
|
|
77
|
-
actionSpec.data.args = [];
|
|
78
|
-
}
|
|
79
|
-
const out = await execute(actionSpec.data.cmd, [...actionSpec.data.args, ...runArgs]);
|
|
80
|
-
return out.trim();
|
|
59
|
+
const run = async (params) => {
|
|
60
|
+
let runArgs = params;
|
|
61
|
+
const actionSpec = readYmlFile(path);
|
|
62
|
+
if (!actionSpec.found) {
|
|
63
|
+
runtimeError("System action yml file", path, "not found");
|
|
64
|
+
}
|
|
65
|
+
if (!actionSpec.data?.args) {
|
|
66
|
+
actionSpec.data.args = [];
|
|
81
67
|
}
|
|
82
|
-
|
|
83
|
-
|
|
68
|
+
const out = await execute(actionSpec.data.cmd, [...actionSpec.data.args, ...runArgs]);
|
|
69
|
+
return out.trim();
|
|
70
|
+
};
|
|
71
|
+
return run;
|
|
84
72
|
}
|
|
85
73
|
function pythonAction(path) {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
else {
|
|
95
|
-
try {
|
|
96
|
-
runArgs = Object.values(params);
|
|
97
|
-
}
|
|
98
|
-
catch (e) {
|
|
99
|
-
throw new Error(`wrong python action args: ${e}`);
|
|
100
|
-
}
|
|
74
|
+
const run = async (params) => {
|
|
75
|
+
let runArgs = new Array();
|
|
76
|
+
if (params?.args) {
|
|
77
|
+
runArgs = params.args;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
try {
|
|
81
|
+
runArgs = Object.values(params);
|
|
101
82
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
throw new Error(`python error: ${error}`);
|
|
83
|
+
catch (e) {
|
|
84
|
+
throw new Error(`wrong python action args: ${e}`);
|
|
105
85
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
86
|
+
}
|
|
87
|
+
const { data, error } = await runPyScript(pyShell, "python3", path, runArgs);
|
|
88
|
+
if (error) {
|
|
89
|
+
throw new Error(`python error: ${error}`);
|
|
90
|
+
}
|
|
91
|
+
let txt = data[0];
|
|
92
|
+
if (data.length > 1) {
|
|
93
|
+
txt = data.join("\n");
|
|
94
|
+
}
|
|
95
|
+
let final = txt;
|
|
96
|
+
if (txt.startsWith("{") || txt.startsWith("[")) {
|
|
97
|
+
try {
|
|
98
|
+
final = JSON.parse(txt);
|
|
109
99
|
}
|
|
110
|
-
|
|
111
|
-
if (txt.startsWith("{") || txt.startsWith("[")) {
|
|
112
|
-
try {
|
|
113
|
-
final = JSON.parse(txt);
|
|
114
|
-
}
|
|
115
|
-
catch (e) {
|
|
116
|
-
}
|
|
100
|
+
catch (e) {
|
|
117
101
|
}
|
|
118
|
-
return final;
|
|
119
102
|
}
|
|
120
|
-
|
|
121
|
-
|
|
103
|
+
return final;
|
|
104
|
+
};
|
|
105
|
+
return run;
|
|
122
106
|
}
|
|
123
107
|
export { executeAction, executeActionCmd, systemAction, pythonAction, };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
declare function createJsAction(action: CallableFunction): AgentTask<FeatureType, any, any>;
|
|
1
|
+
import { FeatureExecutor } from "../../../interfaces.js";
|
|
2
|
+
declare function createJsAction(action: CallableFunction): FeatureExecutor;
|
|
4
3
|
export { createJsAction };
|
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
import { useAgentTask } from "@agent-smith/jobs";
|
|
2
1
|
function createJsAction(action) {
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
try {
|
|
8
|
-
const res = await action(args, options);
|
|
9
|
-
return res;
|
|
10
|
-
}
|
|
11
|
-
catch (e) {
|
|
12
|
-
throw new Error(`executing action:${e}`);
|
|
13
|
-
}
|
|
2
|
+
const run = async (args, options) => {
|
|
3
|
+
try {
|
|
4
|
+
const res = await action(args, options);
|
|
5
|
+
return res;
|
|
14
6
|
}
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
catch (e) {
|
|
8
|
+
throw new Error(`executing action:${e}`);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
return run;
|
|
17
12
|
}
|
|
18
13
|
export { createJsAction };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare function executeAdaptater(name: string,
|
|
1
|
+
declare function executeAdaptater(name: string, params: any, options: Record<string, any>): Promise<any>;
|
|
2
2
|
export { executeAdaptater, };
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { getFeatureSpec } from "../../../state/features.js";
|
|
2
2
|
import { createJsAction } from "../actions/read.js";
|
|
3
|
-
async function executeAdaptater(name,
|
|
3
|
+
async function executeAdaptater(name, params, options) {
|
|
4
|
+
if (params?.args) {
|
|
5
|
+
params = params.args;
|
|
6
|
+
}
|
|
4
7
|
const { found, path } = getFeatureSpec(name, "adaptater");
|
|
5
8
|
if (!found) {
|
|
6
9
|
throw new Error(`adaptater ${name} not found`);
|
|
7
10
|
}
|
|
8
|
-
let
|
|
11
|
+
let run;
|
|
9
12
|
const jsa = await import(path);
|
|
10
|
-
|
|
13
|
+
run = createJsAction(jsa.action);
|
|
11
14
|
let res;
|
|
12
15
|
try {
|
|
13
|
-
res = await
|
|
16
|
+
res = await run(params, options);
|
|
14
17
|
}
|
|
15
18
|
catch (e) {
|
|
16
19
|
throw new Error(`adaptater ${name}: ${e}`);
|
package/dist/cmd/lib/mcp.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
2
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
3
|
-
import {
|
|
3
|
+
import { ToolSpec } from "@locallm/types/dist/tools";
|
|
4
4
|
declare class McpClient {
|
|
5
5
|
name: string;
|
|
6
6
|
transport: StdioClientTransport;
|
|
7
7
|
client: Client;
|
|
8
8
|
authorizedTools: Array<string> | null;
|
|
9
|
-
tools: Record<string,
|
|
9
|
+
tools: Record<string, ToolSpec>;
|
|
10
10
|
constructor(servername: string, command: string, args: Array<string>, authorizedTools?: Array<string> | null);
|
|
11
11
|
start(): Promise<void>;
|
|
12
12
|
stop(): Promise<void>;
|
|
13
|
-
extractTools(): Promise<Array<
|
|
13
|
+
extractTools(): Promise<Array<ToolSpec>>;
|
|
14
14
|
}
|
|
15
15
|
export { McpClient, };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
declare function
|
|
4
|
-
declare function executeTaskCmd(name: string, targs?: Array<any>): Promise<LmTaskOutput>;
|
|
1
|
+
import { TaskOutput } from "@agent-smith/task";
|
|
2
|
+
declare function executeTask(name: string, payload: Record<string, any>, options: Record<string, any>, quiet?: boolean): Promise<TaskOutput>;
|
|
3
|
+
declare function executeTaskCmd(name: string, targs?: Array<any>): Promise<TaskOutput>;
|
|
5
4
|
export { executeTask, executeTaskCmd };
|
|
@@ -1,19 +1,27 @@
|
|
|
1
|
+
import { Agent } from "@agent-smith/agent";
|
|
2
|
+
import { useTemplateForModel } from "@agent-smith/tfm";
|
|
1
3
|
import { input } from "@inquirer/prompts";
|
|
2
4
|
import { compile, serializeGrammar } from "@intrinsicai/gbnfgen";
|
|
3
|
-
import color from "ansi-colors";
|
|
5
|
+
import { default as color, default as colors } from "ansi-colors";
|
|
6
|
+
import { PromptTemplate } from "modprompt";
|
|
4
7
|
import ora from 'ora';
|
|
5
|
-
import { brain } from "../../../agent.js";
|
|
6
8
|
import { query } from "../../../cli.js";
|
|
7
9
|
import { readClipboard } from "../../../cmd/sys/clipboard.js";
|
|
8
10
|
import { usePerfTimer } from "../../../main.js";
|
|
11
|
+
import { backend } from "../../../state/backends.js";
|
|
9
12
|
import { isChatMode, runMode } from "../../../state/state.js";
|
|
10
13
|
import { program } from "../../cmds.js";
|
|
11
14
|
import { parseCommandArgs } from "../options_parsers.js";
|
|
12
15
|
import { runtimeDataError, runtimeWarning } from "../user_msgs.js";
|
|
13
16
|
import { formatStats, processOutput, readPromptFile } from "../utils.js";
|
|
14
17
|
import { readTask } from "./read.js";
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
const tfm = useTemplateForModel();
|
|
19
|
+
async function executeTask(name, payload, options, quiet) {
|
|
20
|
+
const agent = new Agent(backend.value);
|
|
21
|
+
if (options?.debug) {
|
|
22
|
+
console.log("Agent:", colors.bold(agent.lm.name), "( " + agent.lm.providerType + " backend type)");
|
|
23
|
+
}
|
|
24
|
+
const { task, model, conf, vars, mcpServers } = await readTask(name, payload, options, agent);
|
|
17
25
|
if (model?.inferParams?.tsGrammar) {
|
|
18
26
|
model.inferParams.grammar = serializeGrammar(await compile(model.inferParams.tsGrammar, "Grammar"));
|
|
19
27
|
delete model.inferParams.tsGrammar;
|
|
@@ -21,15 +29,21 @@ async function executeTask(name, payload, options, quiet, expert) {
|
|
|
21
29
|
if (options?.debug) {
|
|
22
30
|
console.log("Task model:", model);
|
|
23
31
|
}
|
|
24
|
-
const ex = expert ?? brain.getOrCreateExpertForModel(model.name, model.template);
|
|
25
|
-
if (!ex) {
|
|
26
|
-
throw new Error("No expert found for model " + model.name);
|
|
27
|
-
}
|
|
28
|
-
ex.checkStatus();
|
|
29
|
-
let i = 0;
|
|
30
32
|
let c = false;
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
+
const useTemplates = agent.lm.providerType !== "openai";
|
|
34
|
+
let hasThink = false;
|
|
35
|
+
let tpl = null;
|
|
36
|
+
if (useTemplates) {
|
|
37
|
+
if ((!task.def.model?.template)) {
|
|
38
|
+
const gt = tfm.guess(task.def.model.name);
|
|
39
|
+
if (gt == "none") {
|
|
40
|
+
throw new Error(`Unable to guess the template for ${task.def.model}: please provide a template in the taskDef definition`);
|
|
41
|
+
}
|
|
42
|
+
task.def.model.template = gt;
|
|
43
|
+
}
|
|
44
|
+
tpl = new PromptTemplate(task.def.model.template);
|
|
45
|
+
hasThink = tpl.tags?.think ? true : false;
|
|
46
|
+
}
|
|
33
47
|
const printToken = (t) => {
|
|
34
48
|
if (options?.tokens === true) {
|
|
35
49
|
let txt = t;
|
|
@@ -42,90 +56,99 @@ async function executeTask(name, payload, options, quiet, expert) {
|
|
|
42
56
|
process.stdout.write(t);
|
|
43
57
|
}
|
|
44
58
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
59
|
+
const hasTools = options?.tools;
|
|
60
|
+
let continueWrite = true;
|
|
61
|
+
let skipNextEmptyLinesToken = false;
|
|
62
|
+
const spinner = ora("Thinking ...");
|
|
63
|
+
const ts = "Thinking";
|
|
64
|
+
const te = color.dim("tokens");
|
|
65
|
+
const formatTokenCount = (i) => {
|
|
66
|
+
return `${ts} ${color.bold(i.toString())} ${te}`;
|
|
67
|
+
};
|
|
68
|
+
const perfTimer = usePerfTimer(false);
|
|
69
|
+
let i = 0;
|
|
70
|
+
const processToken = (t) => {
|
|
71
|
+
if (i == 0) {
|
|
72
|
+
perfTimer.start();
|
|
73
|
+
}
|
|
74
|
+
spinner.text = formatTokenCount(i);
|
|
75
|
+
if (!options?.verbose && !options?.debug) {
|
|
76
|
+
if (hasThink && tpl) {
|
|
77
|
+
if (t == tpl.tags.think?.start) {
|
|
78
|
+
spinner.start();
|
|
79
|
+
continueWrite = false;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
else if (t == tpl.tags.think?.end) {
|
|
83
|
+
continueWrite = true;
|
|
84
|
+
skipNextEmptyLinesToken = true;
|
|
85
|
+
let msg = color.dim("Thinking:") + ` ${i} ${color.dim("tokens")}`;
|
|
86
|
+
msg = msg + " " + color.dim(perfTimer.time());
|
|
87
|
+
spinner.info(msg);
|
|
88
|
+
return;
|
|
77
89
|
}
|
|
78
90
|
}
|
|
79
|
-
|
|
80
|
-
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
if (tpl) {
|
|
94
|
+
if (t == tpl.tags.think?.end) {
|
|
81
95
|
let msg = color.dim("Thinking:") + ` ${i} ${color.dim("tokens")}`;
|
|
82
96
|
msg = msg + " " + color.dim(perfTimer.time());
|
|
83
97
|
console.log(msg);
|
|
84
98
|
}
|
|
85
99
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return;
|
|
100
|
+
}
|
|
101
|
+
if (hasTools && tpl) {
|
|
102
|
+
if (t == tpl.tags.toolCall?.start) {
|
|
103
|
+
continueWrite = false;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
else if (t == tpl.tags.toolCall?.end) {
|
|
107
|
+
if (options?.verbose === true) {
|
|
108
|
+
skipNextEmptyLinesToken = true;
|
|
109
|
+
continueWrite = true;
|
|
97
110
|
}
|
|
111
|
+
return;
|
|
98
112
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
113
|
+
}
|
|
114
|
+
if (continueWrite) {
|
|
115
|
+
if (skipNextEmptyLinesToken) {
|
|
116
|
+
if (t == "\n\n") {
|
|
117
|
+
skipNextEmptyLinesToken = false;
|
|
118
|
+
return;
|
|
105
119
|
}
|
|
106
|
-
printToken(t);
|
|
107
120
|
}
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
121
|
+
printToken(t);
|
|
122
|
+
}
|
|
123
|
+
++i;
|
|
124
|
+
};
|
|
125
|
+
const spinnerInit = (name) => ora(`Executing ${name} tool ...`);
|
|
126
|
+
let tcspinner;
|
|
111
127
|
const onToolCall = (tc) => {
|
|
112
128
|
console.log("⚒️ ", color.bold(name), "=>", `${color.yellowBright(tc.name)}`, tc.arguments);
|
|
129
|
+
tcspinner = spinnerInit(tc.name);
|
|
130
|
+
tcspinner.start();
|
|
131
|
+
};
|
|
132
|
+
const onToolCallEnd = (tr) => {
|
|
133
|
+
tcspinner.stop();
|
|
113
134
|
};
|
|
114
135
|
if (options?.onToken) {
|
|
115
|
-
|
|
136
|
+
task.agent.lm.onToken = options.onToken;
|
|
116
137
|
}
|
|
117
138
|
else {
|
|
118
|
-
|
|
139
|
+
task.agent.lm.onToken = processToken;
|
|
140
|
+
}
|
|
141
|
+
if (!conf?.inferParams) {
|
|
142
|
+
conf.inferParams = {};
|
|
119
143
|
}
|
|
120
144
|
conf.inferParams.stream = true;
|
|
121
145
|
const tconf = {
|
|
122
|
-
expert: ex,
|
|
123
146
|
model: model,
|
|
124
147
|
debug: options?.debug ?? false,
|
|
125
148
|
onToolCall: onToolCall,
|
|
149
|
+
onToolCallEnd: onToolCallEnd,
|
|
126
150
|
...conf,
|
|
127
151
|
};
|
|
128
|
-
tconf.expert = ex;
|
|
129
152
|
let out;
|
|
130
153
|
try {
|
|
131
154
|
out = await task.run({ prompt: payload.prompt, ...vars }, tconf);
|
|
@@ -151,12 +174,15 @@ async function executeTask(name, payload, options, quiet, expert) {
|
|
|
151
174
|
}
|
|
152
175
|
}
|
|
153
176
|
else {
|
|
154
|
-
await executeTask(name, { ...vars, prompt: prompt }, options, quiet
|
|
177
|
+
await executeTask(name, { ...vars, prompt: prompt }, options, quiet);
|
|
155
178
|
}
|
|
156
179
|
}
|
|
157
180
|
if (options?.debug === true || options?.verbose === true) {
|
|
158
181
|
try {
|
|
159
182
|
console.log("\n", formatStats(out.answer.stats));
|
|
183
|
+
if (options?.debug === true) {
|
|
184
|
+
console.log(out.answer.stats);
|
|
185
|
+
}
|
|
160
186
|
}
|
|
161
187
|
catch (e) {
|
|
162
188
|
runtimeWarning("Error formating stats:", `${e}`);
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Agent } from "@agent-smith/agent";
|
|
2
|
+
import { ModelSpec, Task, TaskConf } from "@agent-smith/task";
|
|
2
3
|
import { McpClient } from "../mcp.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
declare function readTask(name: string, payload: Record<string, any>, options: Record<string, any>): Promise<{
|
|
6
|
-
task: AgentTask<FeatureType, LmTaskInput, LmTaskOutput, Record<string, any>>;
|
|
4
|
+
declare function readTask(name: string, payload: Record<string, any>, options: Record<string, any>, agent: Agent): Promise<{
|
|
5
|
+
task: Task;
|
|
7
6
|
model: ModelSpec;
|
|
8
|
-
conf:
|
|
7
|
+
conf: TaskConf;
|
|
9
8
|
vars: Record<string, any>;
|
|
10
9
|
mcpServers: Array<McpClient>;
|
|
11
10
|
}>;
|