@agent-smith/core 0.0.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.
- package/README.md +378 -0
- package/dist/actions/cmd.d.ts +5 -0
- package/dist/actions/cmd.js +135 -0
- package/dist/actions/read.d.ts +3 -0
- package/dist/actions/read.js +17 -0
- package/dist/adaptaters/cmd.d.ts +2 -0
- package/dist/adaptaters/cmd.js +39 -0
- package/dist/agents/cmd.d.ts +3 -0
- package/dist/agents/cmd.js +10 -0
- package/dist/agents/conf.d.ts +3 -0
- package/dist/agents/conf.js +9 -0
- package/dist/agents/files.d.ts +3 -0
- package/dist/agents/files.js +40 -0
- package/dist/agents/read.d.ts +11 -0
- package/dist/agents/read.js +241 -0
- package/dist/agents/useagent.d.ts +13 -0
- package/dist/agents/useagent.js +395 -0
- package/dist/conf.d.ts +14 -0
- package/dist/conf.js +160 -0
- package/dist/const.d.ts +3 -0
- package/dist/const.js +10 -0
- package/dist/db/db.d.ts +3 -0
- package/dist/db/db.js +24 -0
- package/dist/db/read.d.ts +45 -0
- package/dist/db/read.js +280 -0
- package/dist/db/schemas.d.ts +2 -0
- package/dist/db/schemas.js +141 -0
- package/dist/db/write.d.ts +23 -0
- package/dist/db/write.js +570 -0
- package/dist/features/actions/load-skill.d.ts +2 -0
- package/dist/features/actions/load-skill.js +37 -0
- package/dist/features/actions/notify-user.d.ts +2 -0
- package/dist/features/actions/notify-user.js +25 -0
- package/dist/features/actions/read-feature.d.ts +2 -0
- package/dist/features/actions/read-feature.js +25 -0
- package/dist/features/actions/run-agent.d.ts +2 -0
- package/dist/features/actions/run-agent.js +34 -0
- package/dist/features/actions/run-collaborator.d.ts +1 -0
- package/dist/features/actions/run-collaborator.js +42 -0
- package/dist/features/actions/run-worker.d.ts +2 -0
- package/dist/features/actions/run-worker.js +34 -0
- package/dist/features/adaptaters/agent-smith-db-getschema.d.ts +4 -0
- package/dist/features/adaptaters/agent-smith-db-getschema.js +62 -0
- package/dist/features/adaptaters/imgs2base64.d.ts +1 -0
- package/dist/features/adaptaters/imgs2base64.js +13 -0
- package/dist/features/adaptaters/prequery.d.ts +4 -0
- package/dist/features/adaptaters/prequery.js +5 -0
- package/dist/features/agents/agent-smith-colab.yml +37 -0
- package/dist/features/agents/agent-smith-doc.yml +50 -0
- package/dist/features/agents/agent-smith-help.yml +35 -0
- package/dist/features/agents/agent-smith-search.yml +41 -0
- package/dist/features/agents/agent-smith-sql.yml +31 -0
- package/dist/features/agents/agent-smith.yml +37 -0
- package/dist/features/agents/collaborator.yml +15 -0
- package/dist/features/agents/infer.yml +12 -0
- package/dist/features/fragments/ctx-helper-files.md +4 -0
- package/dist/features/fragments/workspace.txt +6 -0
- package/dist/features/skills/create-package-readme/SKILL.md +66 -0
- package/dist/features/skills/document-package/SKILL.md +83 -0
- package/dist/features/skills/t/SKILL.md +26 -0
- package/dist/features/skills/update-codebase-summary/SKILL.md +45 -0
- package/dist/features/skills/update-doc-map/SKILL.md +8 -0
- package/dist/features/skills/update-doc-map/scripts/generate-doc-map.mjs +196 -0
- package/dist/features/workflows/agent-smith-db.yml +9 -0
- package/dist/features/workflows/q.yml +3 -0
- package/dist/features/workflows/vision.yml +3 -0
- package/dist/main.d.ts +127 -0
- package/dist/main.js +80 -0
- package/dist/mcp.d.ts +16 -0
- package/dist/mcp.js +110 -0
- package/dist/state/backends.d.ts +7 -0
- package/dist/state/backends.js +96 -0
- package/dist/state/features.d.ts +10 -0
- package/dist/state/features.js +42 -0
- package/dist/state/plugins.d.ts +6 -0
- package/dist/state/plugins.js +27 -0
- package/dist/state/state.d.ts +18 -0
- package/dist/state/state.js +79 -0
- package/dist/state/tasks.d.ts +6 -0
- package/dist/state/tasks.js +33 -0
- package/dist/tools.d.ts +15 -0
- package/dist/tools.js +149 -0
- package/dist/updateconf.d.ts +5 -0
- package/dist/updateconf.js +124 -0
- package/dist/utils/io.d.ts +10 -0
- package/dist/utils/io.js +98 -0
- package/dist/utils/perf.d.ts +9 -0
- package/dist/utils/perf.js +63 -0
- package/dist/utils/sys/clipboard.d.ts +3 -0
- package/dist/utils/sys/clipboard.js +33 -0
- package/dist/utils/sys/delete_file.d.ts +2 -0
- package/dist/utils/sys/delete_file.js +10 -0
- package/dist/utils/sys/dirs.d.ts +2 -0
- package/dist/utils/sys/dirs.js +9 -0
- package/dist/utils/sys/execute.d.ts +13 -0
- package/dist/utils/sys/execute.js +48 -0
- package/dist/utils/sys/read.d.ts +3 -0
- package/dist/utils/sys/read.js +21 -0
- package/dist/utils/sys/read_agent.d.ts +6 -0
- package/dist/utils/sys/read_agent.js +23 -0
- package/dist/utils/sys/read_cmds.d.ts +7 -0
- package/dist/utils/sys/read_cmds.js +37 -0
- package/dist/utils/sys/read_conf.d.ts +6 -0
- package/dist/utils/sys/read_conf.js +12 -0
- package/dist/utils/sys/read_features.d.ts +3 -0
- package/dist/utils/sys/read_features.js +134 -0
- package/dist/utils/sys/read_yml_file.d.ts +5 -0
- package/dist/utils/sys/read_yml_file.js +11 -0
- package/dist/utils/sys/run_python.d.ts +6 -0
- package/dist/utils/sys/run_python.js +39 -0
- package/dist/utils/text.d.ts +2 -0
- package/dist/utils/text.js +28 -0
- package/dist/utils/user_msgs.d.ts +5 -0
- package/dist/utils/user_msgs.js +19 -0
- package/dist/workflows/cmd.d.ts +3 -0
- package/dist/workflows/cmd.js +189 -0
- package/dist/workflows/read.d.ts +6 -0
- package/dist/workflows/read.js +61 -0
- package/package.json +45 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { compile, serializeGrammar } from "@intrinsicai/gbnfgen";
|
|
3
|
+
import { readSkillsFromList, readTool } from "../db/read.js";
|
|
4
|
+
import { executeAction } from "../actions/cmd.js";
|
|
5
|
+
import { McpClient } from "../mcp.js";
|
|
6
|
+
import { executeWorkflow } from "../workflows/cmd.js";
|
|
7
|
+
import { executeAgent } from "./cmd.js";
|
|
8
|
+
import { openAgentSpec } from "../utils/io.js";
|
|
9
|
+
import { applyFilePlaceholders } from "./files.js";
|
|
10
|
+
//import { confirmToolUsage } from "../tools.js";
|
|
11
|
+
async function readAgent(name, payload, options) {
|
|
12
|
+
/*console.log("Read Agent", name);
|
|
13
|
+
console.log("Payload:", payload);
|
|
14
|
+
console.log("Options:", options);*/
|
|
15
|
+
const { agentSpec, agentPath } = openAgentSpec(name);
|
|
16
|
+
//console.log("Agent vars:", agentSpec?.variables);
|
|
17
|
+
const agentDir = path.dirname(agentPath);
|
|
18
|
+
if (!options?.backend && agentSpec?.backend) {
|
|
19
|
+
options.backend = agentSpec.backend;
|
|
20
|
+
}
|
|
21
|
+
let ip = {};
|
|
22
|
+
if (!options?.isToolCall) {
|
|
23
|
+
if (options?.params) {
|
|
24
|
+
ip = options.params;
|
|
25
|
+
}
|
|
26
|
+
else if (agentSpec?.inferParams) {
|
|
27
|
+
ip = agentSpec.inferParams;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
if (options?.propagateInferParams) {
|
|
32
|
+
if (options?.params) {
|
|
33
|
+
ip = options.params;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (agentSpec?.inferParams) {
|
|
37
|
+
ip = agentSpec.inferParams;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// vars
|
|
41
|
+
let vars = {};
|
|
42
|
+
if (agentSpec?.variables?.optional) {
|
|
43
|
+
for (const k of Object.keys(agentSpec.variables.optional)) {
|
|
44
|
+
if (k in payload) {
|
|
45
|
+
vars[k] = payload[k];
|
|
46
|
+
vars[k] = payload[k];
|
|
47
|
+
delete payload[k];
|
|
48
|
+
}
|
|
49
|
+
else if (options?.variables) {
|
|
50
|
+
if (k in options.variables) {
|
|
51
|
+
vars[k] = options.variables[k];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// workflow takes variables in options
|
|
56
|
+
if (k in options) {
|
|
57
|
+
vars[k] = options[k];
|
|
58
|
+
delete options[k];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (agentSpec?.variables?.required) {
|
|
64
|
+
for (const k of Object.keys(agentSpec.variables.required)) {
|
|
65
|
+
//console.log("TASK V required:", Object.keys(agentSpec.variables.required), "/", k in options, "/", k in payload);
|
|
66
|
+
if (k in payload) {
|
|
67
|
+
vars[k] = payload[k];
|
|
68
|
+
vars[k] = payload[k];
|
|
69
|
+
delete payload[k];
|
|
70
|
+
}
|
|
71
|
+
else if (options?.variables) {
|
|
72
|
+
if (k in options.variables) {
|
|
73
|
+
vars[k] = options.variables[k];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// workflow takes variables in options
|
|
78
|
+
if (k in options) {
|
|
79
|
+
vars[k] = options[k];
|
|
80
|
+
delete options[k];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//console.log("END VARS", vars);
|
|
86
|
+
const mcpServers = new Array();
|
|
87
|
+
if (!agentSpec?.tools) {
|
|
88
|
+
agentSpec.tools = [];
|
|
89
|
+
}
|
|
90
|
+
const mcpServersArgs = {};
|
|
91
|
+
if (options?.mcpArgs) {
|
|
92
|
+
(options.mcpArgs).forEach(v => {
|
|
93
|
+
const s = v.split(":");
|
|
94
|
+
if (s.length < 2) {
|
|
95
|
+
throw new Error(`Malformed mcp option ${v}: use --mcp servername:arg1,arg2`);
|
|
96
|
+
}
|
|
97
|
+
const sn = s[0];
|
|
98
|
+
const sa = s[1];
|
|
99
|
+
const _margs = sa.split(",");
|
|
100
|
+
mcpServersArgs[sn] = _margs;
|
|
101
|
+
});
|
|
102
|
+
if (options?.debug) {
|
|
103
|
+
console.log("Opening", options.mcpArgs.length, "server(s)");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// mcp tools
|
|
107
|
+
if (agentSpec?.mcp) {
|
|
108
|
+
for (const [servername, tool] of Object.entries(agentSpec.mcp)) {
|
|
109
|
+
//console.log("MCP TOOL:", tool)
|
|
110
|
+
const authorizedTools = new Array();
|
|
111
|
+
const askUserTools = new Array();
|
|
112
|
+
if (tool?.tools) {
|
|
113
|
+
tool.tools.forEach(t => {
|
|
114
|
+
let tn = t;
|
|
115
|
+
if (t.endsWith("?")) {
|
|
116
|
+
tn = t.slice(0, -1);
|
|
117
|
+
askUserTools.push(tn);
|
|
118
|
+
}
|
|
119
|
+
authorizedTools.push(tn);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
const margs = tool.arguments;
|
|
123
|
+
if (servername in mcpServersArgs) {
|
|
124
|
+
margs.push(...mcpServersArgs[servername]);
|
|
125
|
+
}
|
|
126
|
+
const mcp = new McpClient(servername, tool.command, tool.arguments, authorizedTools.length > 0 ? authorizedTools : null, askUserTools.length > 0 ? askUserTools : null);
|
|
127
|
+
//console.log("MCP", mcp);
|
|
128
|
+
mcpServers.push(mcp);
|
|
129
|
+
/*await mcp.start();
|
|
130
|
+
const tools = await mcp.extractTools();
|
|
131
|
+
tools.forEach(t => agentSpec.tools?.push(t))*/
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// tools
|
|
135
|
+
//console.log("AGENT SPECS", agent.name, agentSpec);
|
|
136
|
+
if (agentSpec?.skills) {
|
|
137
|
+
// skills text
|
|
138
|
+
const sks = readSkillsFromList(agentSpec.skills);
|
|
139
|
+
const skLines = new Array();
|
|
140
|
+
for (const s of Object.values(sks)) {
|
|
141
|
+
if (!s?.variables) {
|
|
142
|
+
throw new Error(`no variables in skill feature ${s.name}`);
|
|
143
|
+
}
|
|
144
|
+
const vars = s.variables;
|
|
145
|
+
skLines.push("- **" + vars.name + "**: " + vars.description);
|
|
146
|
+
}
|
|
147
|
+
const skt = skLines.join("\n");
|
|
148
|
+
if (agentSpec.prompt.includes("{skills}")) {
|
|
149
|
+
agentSpec.prompt = agentSpec.prompt.replace("{skills}", skt);
|
|
150
|
+
}
|
|
151
|
+
if (agentSpec?.template?.system) {
|
|
152
|
+
if (agentSpec.template.system.includes("{skills}")) {
|
|
153
|
+
agentSpec.template.system = agentSpec.template.system.replace("{skills}", skt);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// load skill
|
|
157
|
+
//if (agentSpec?.skills) {
|
|
158
|
+
if (!agentSpec?.toolsList) {
|
|
159
|
+
agentSpec.toolsList = [];
|
|
160
|
+
}
|
|
161
|
+
agentSpec.toolsList.push("load-skill");
|
|
162
|
+
/*} else {
|
|
163
|
+
throw new Error(`loading skill: no skills defined in agent`)
|
|
164
|
+
}*/
|
|
165
|
+
}
|
|
166
|
+
//console.log("ATL", agentSpec.toolsList);
|
|
167
|
+
if (agentSpec?.toolsList) {
|
|
168
|
+
for (const rawToolName of agentSpec.toolsList) {
|
|
169
|
+
let toolName = rawToolName;
|
|
170
|
+
let autoRunTool = true;
|
|
171
|
+
if (rawToolName.endsWith("?")) {
|
|
172
|
+
autoRunTool = false;
|
|
173
|
+
toolName = rawToolName.slice(0, -1);
|
|
174
|
+
}
|
|
175
|
+
const { found, tool } = readTool(toolName);
|
|
176
|
+
if (!found) {
|
|
177
|
+
throw new Error(`tool ${toolName} not found for agent ${agentSpec.name}`);
|
|
178
|
+
}
|
|
179
|
+
//console.log("Tool found:", toolName, tool);
|
|
180
|
+
const quiet = !options?.debug;
|
|
181
|
+
const lmTool = {
|
|
182
|
+
...tool,
|
|
183
|
+
execute: async (params) => {
|
|
184
|
+
//console.log("EXEC TOOL:", type, toolName, params);
|
|
185
|
+
let toolOpts = { ...options };
|
|
186
|
+
if (params?.toolOptions) {
|
|
187
|
+
toolOpts = params.toolOptions;
|
|
188
|
+
//console.log("EXECTOOL CALLER", toolOpts?.caller)
|
|
189
|
+
delete params.toolOptions;
|
|
190
|
+
}
|
|
191
|
+
toolOpts.isToolCall = true;
|
|
192
|
+
switch (tool.type) {
|
|
193
|
+
case "action":
|
|
194
|
+
const res = await executeAction(toolName, params, toolOpts, quiet);
|
|
195
|
+
return res;
|
|
196
|
+
case "agent":
|
|
197
|
+
const agres = await executeAgent(toolName, params, toolOpts);
|
|
198
|
+
//console.log("WFTRESP", tres.answer.text);
|
|
199
|
+
if (agres?.text) {
|
|
200
|
+
return agres.text;
|
|
201
|
+
}
|
|
202
|
+
return agres;
|
|
203
|
+
case "workflow":
|
|
204
|
+
const wres = await executeWorkflow(toolName, params, toolOpts);
|
|
205
|
+
return wres;
|
|
206
|
+
default:
|
|
207
|
+
throw new Error(`unknown tool execution function type: ${tool.type} for ${toolName}`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
if (!autoRunTool) {
|
|
212
|
+
if (!options?.confirmToolUsage) {
|
|
213
|
+
throw new Error("provide a tool usage confirm function");
|
|
214
|
+
}
|
|
215
|
+
lmTool.canRun = options.confirmToolUsage;
|
|
216
|
+
}
|
|
217
|
+
agentSpec.tools.push(lmTool);
|
|
218
|
+
}
|
|
219
|
+
delete agentSpec.toolsList;
|
|
220
|
+
}
|
|
221
|
+
;
|
|
222
|
+
//console.log("AS TOOLS", agentSpec.name, agentSpec.tools);
|
|
223
|
+
if (options?.isChatMode) {
|
|
224
|
+
agentSpec.prompt = "{prompt}";
|
|
225
|
+
}
|
|
226
|
+
//console.log("TASK SPEC:", JSON.stringify(agentSpec, null, " "));
|
|
227
|
+
if (!options?.baseDir) {
|
|
228
|
+
options.baseDir = agentDir;
|
|
229
|
+
}
|
|
230
|
+
applyFilePlaceholders(agentSpec, options.baseDir);
|
|
231
|
+
//task.addTools(agentSpec.tools);
|
|
232
|
+
//console.log("TASK TOOLS", task.agent.tools);
|
|
233
|
+
// check for grammars
|
|
234
|
+
if (agentSpec?.inferParams?.tsGrammar) {
|
|
235
|
+
//console.log("TSG");
|
|
236
|
+
agentSpec.inferParams.grammar = serializeGrammar(await compile(agentSpec.inferParams.tsGrammar, "Grammar"));
|
|
237
|
+
}
|
|
238
|
+
//console.log("AS END", agentSpec);
|
|
239
|
+
return { agentSpec, vars, mcpServers, agentDir };
|
|
240
|
+
}
|
|
241
|
+
export { readAgent };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Agent } from "@agent-smith/agent";
|
|
2
|
+
import type { AgentInferenceOptions, AgentSettings, InferenceResult } from "@agent-smith/types";
|
|
3
|
+
declare const useAgentExecutor: (name: string, payload: {
|
|
4
|
+
prompt: string;
|
|
5
|
+
} & Record<string, any>, options: AgentInferenceOptions) => Promise<{
|
|
6
|
+
agent: Agent;
|
|
7
|
+
vars: Record<string, any>;
|
|
8
|
+
mcpServers: import("../mcp.js").McpClient[];
|
|
9
|
+
agentDir: string;
|
|
10
|
+
settings: AgentSettings;
|
|
11
|
+
execute: () => Promise<InferenceResult>;
|
|
12
|
+
}>;
|
|
13
|
+
export { useAgentExecutor };
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { Agent } from "@agent-smith/agent";
|
|
2
|
+
import { compile, serializeGrammar } from "@intrinsicai/gbnfgen";
|
|
3
|
+
import { backend, backends, listBackends } from "../state/backends.js";
|
|
4
|
+
import { initAgentSettings, isAgentSettingsInitialized, agentSettings } from "../state/tasks.js";
|
|
5
|
+
import { processOutput } from "../utils/io.js";
|
|
6
|
+
import { usePerfTimer } from "../utils/perf.js";
|
|
7
|
+
import { runtimeDataError, runtimeError } from "../utils/user_msgs.js";
|
|
8
|
+
import { readAgent } from "./read.js";
|
|
9
|
+
import { toRaw } from "@vue/reactivity";
|
|
10
|
+
import { readAllSkills } from "../db/read.js";
|
|
11
|
+
import { default as fm } from "front-matter";
|
|
12
|
+
import { readFile } from "../utils/sys/read.js";
|
|
13
|
+
const useAgentExecutor = async (name, payload, options) => {
|
|
14
|
+
const localOptions = Object.assign({}, options);
|
|
15
|
+
// skill loader
|
|
16
|
+
if (payload.prompt.includes("%")) {
|
|
17
|
+
const skills = readAllSkills();
|
|
18
|
+
for (const [k, v] of Object.entries(skills)) {
|
|
19
|
+
//console.log("S", k, payload.prompt.includes("%" + k));
|
|
20
|
+
if (payload.prompt.includes("%" + k)) {
|
|
21
|
+
const fc = readFile(v.path);
|
|
22
|
+
const data = fm(fc);
|
|
23
|
+
payload.prompt = payload.prompt.replace("%" + k, data.body);
|
|
24
|
+
if (localOptions?.debug) {
|
|
25
|
+
console.log(`loading skill ${k} in prompt`);
|
|
26
|
+
}
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const { agentSpec, vars, mcpServers, agentDir } = await readAgent(name, payload, localOptions);
|
|
32
|
+
if (!isAgentSettingsInitialized.value) {
|
|
33
|
+
initAgentSettings();
|
|
34
|
+
}
|
|
35
|
+
const hasSettings = Object.keys(agentSettings).includes(name);
|
|
36
|
+
let settings = {};
|
|
37
|
+
// backend
|
|
38
|
+
let backendName = "";
|
|
39
|
+
if (hasSettings) {
|
|
40
|
+
settings = agentSettings[name];
|
|
41
|
+
}
|
|
42
|
+
//console.log("EA OPTS", localOptions);
|
|
43
|
+
if (!localOptions?.isToolCall) {
|
|
44
|
+
if (localOptions?.backend) {
|
|
45
|
+
//console.log("BK from options", localOptions.backend);
|
|
46
|
+
backendName = localOptions.backend;
|
|
47
|
+
}
|
|
48
|
+
else if (settings?.backend) {
|
|
49
|
+
//console.log("BK from settings", settings.backend);
|
|
50
|
+
backendName = settings.backend;
|
|
51
|
+
}
|
|
52
|
+
else if (agentSpec?.backend) {
|
|
53
|
+
//console.log("BK from spec", agentSpec.backend);
|
|
54
|
+
backendName = agentSpec.backend;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
// fallback to default backend
|
|
58
|
+
if (!backend.value?.name) {
|
|
59
|
+
const m = `${name} agent executor: no default backend set`;
|
|
60
|
+
console.error(m);
|
|
61
|
+
throw new Error(m);
|
|
62
|
+
}
|
|
63
|
+
//console.log("BK from default", backend.value.name);
|
|
64
|
+
// use default backend
|
|
65
|
+
backendName = backend.value.name;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
if (localOptions?.propagateModel) {
|
|
70
|
+
if (!localOptions?.backend) {
|
|
71
|
+
const m = `${name} agent executor: set a backend in options if propagateModel is false`;
|
|
72
|
+
console.error(m);
|
|
73
|
+
throw new Error(m);
|
|
74
|
+
}
|
|
75
|
+
backendName = localOptions.backend;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
if (agentSpec?.backend) {
|
|
79
|
+
backendName = agentSpec.backend;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
// if not specified use the default backend
|
|
83
|
+
if (!backend.value) {
|
|
84
|
+
const m = `${name} agent executor: no default backend or agent spec backend specified for propagateModel false.`;
|
|
85
|
+
console.error(m, "Default backend:", toRaw(backend), "Backends:", toRaw(backends));
|
|
86
|
+
throw new Error(m);
|
|
87
|
+
}
|
|
88
|
+
backendName = backend.value.name;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (localOptions?.system) {
|
|
92
|
+
// in context agent
|
|
93
|
+
if (!agentSpec?.template) {
|
|
94
|
+
agentSpec.template = { system: localOptions.system };
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
agentSpec.template.system = localOptions.system;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (agentSpec?.template?.system) {
|
|
102
|
+
localOptions.system = agentSpec.template.system;
|
|
103
|
+
}
|
|
104
|
+
if (!(backendName in backends)) {
|
|
105
|
+
const bks = await listBackends(false);
|
|
106
|
+
runtimeDataError(`The backend ${backendName} is not registered in config. Available backends:\n`, bks);
|
|
107
|
+
throw new Error();
|
|
108
|
+
}
|
|
109
|
+
/*if (localOptions?.debug) {
|
|
110
|
+
console.log("Agent executor:", name, "backend:", backendName);
|
|
111
|
+
}*/
|
|
112
|
+
if (backendName.length == 0) {
|
|
113
|
+
throw new Error(`${name} agent executor: no backend set in options`);
|
|
114
|
+
}
|
|
115
|
+
// check setting for backend
|
|
116
|
+
const agent = new Agent({
|
|
117
|
+
name: name,
|
|
118
|
+
lm: backends[backendName],
|
|
119
|
+
}, agentSpec);
|
|
120
|
+
//console.log("AGENT BK", backends[backendName], "\nagb:", agent.lm.name)
|
|
121
|
+
if (!localOptions?.model) {
|
|
122
|
+
if (hasSettings) {
|
|
123
|
+
if (settings?.model) {
|
|
124
|
+
localOptions.model = settings.model;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const execute = async () => {
|
|
129
|
+
//console.log("EXEC AGENT OPTS", localOptions);
|
|
130
|
+
if (localOptions?.verbosity?.mcp && mcpServers.length > 0) {
|
|
131
|
+
console.log("Starting", mcpServers.length, "mcp servers");
|
|
132
|
+
}
|
|
133
|
+
for (const mcp of mcpServers) {
|
|
134
|
+
await mcp.start();
|
|
135
|
+
const _tools = await mcp.extractTools(localOptions);
|
|
136
|
+
_tools.forEach(t => agentSpec.tools?.push(t));
|
|
137
|
+
if (localOptions?.verbosity?.mcp) {
|
|
138
|
+
console.log("MCP start", mcp.name);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
//console.log("TASKCONF IP", conf.inferParams);
|
|
142
|
+
if (!localOptions?.params) {
|
|
143
|
+
localOptions.params = {};
|
|
144
|
+
}
|
|
145
|
+
let applySettings = hasSettings;
|
|
146
|
+
if (localOptions?.isToolCall) {
|
|
147
|
+
if (!localOptions?.propagateInferParams) {
|
|
148
|
+
applySettings = false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (applySettings) {
|
|
152
|
+
if (settings?.max_tokens && !localOptions?.params?.max_tokens) {
|
|
153
|
+
localOptions.params.max_tokens = settings.max_tokens;
|
|
154
|
+
}
|
|
155
|
+
if (settings?.top_k && !localOptions?.params?.top_k) {
|
|
156
|
+
localOptions.params.top_k = settings.top_k;
|
|
157
|
+
}
|
|
158
|
+
if (settings?.top_p && !localOptions?.params?.top_p) {
|
|
159
|
+
localOptions.params.top_p = settings.top_p;
|
|
160
|
+
}
|
|
161
|
+
if (settings?.min_p && !localOptions?.params?.min_p) {
|
|
162
|
+
localOptions.params.min_p = settings.min_p;
|
|
163
|
+
}
|
|
164
|
+
if (settings?.temperature && !localOptions?.params?.temperature) {
|
|
165
|
+
localOptions.params.temperature = settings.temperature;
|
|
166
|
+
}
|
|
167
|
+
if (settings?.repeat_penalty && !localOptions?.params?.repeat_penalty) {
|
|
168
|
+
localOptions.params.repeat_penalty = settings.repeat_penalty;
|
|
169
|
+
}
|
|
170
|
+
if (settings?.presence_penalty && !localOptions?.params?.presence_penalty) {
|
|
171
|
+
localOptions.params.presence_penalty = settings.presence_penalty;
|
|
172
|
+
}
|
|
173
|
+
if (settings?.frequency_penalty && !localOptions?.params?.frequency_penalty) {
|
|
174
|
+
localOptions.params.frequency_penalty = settings.frequency_penalty;
|
|
175
|
+
}
|
|
176
|
+
if (settings?.chat_template_kwargs && !localOptions?.params?.chat_template_kwargs) {
|
|
177
|
+
localOptions.params.chat_template_kwargs = settings.chat_template_kwargs;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
//console.log("TASK MODEL", model);
|
|
181
|
+
// check for grammars
|
|
182
|
+
if (localOptions.params?.tsGrammar) {
|
|
183
|
+
//console.log("TSG");
|
|
184
|
+
localOptions.params.grammar = serializeGrammar(await compile(localOptions.params.tsGrammar, "Grammar"));
|
|
185
|
+
delete localOptions.params.tsGrammar;
|
|
186
|
+
}
|
|
187
|
+
let c = false;
|
|
188
|
+
/*if (localOptions?.verbosity?.task) {
|
|
189
|
+
console.log("Task model:", localOptions.model);
|
|
190
|
+
console.log("Task vars:", vars);
|
|
191
|
+
}*/
|
|
192
|
+
let emittedTokens = 0;
|
|
193
|
+
let emittedThinkingTokens = 0;
|
|
194
|
+
const printToken = (t, dim = false) => {
|
|
195
|
+
if (dim === true) {
|
|
196
|
+
process.stdout.write(`\x1b[2m${t}\x1b[0m`);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
if (localOptions?.showTokens === true) {
|
|
200
|
+
let txt = t;
|
|
201
|
+
txt = c ? t : `\x1b[100m${t}\x1b[0m`;
|
|
202
|
+
process.stdout.write(txt);
|
|
203
|
+
c = !c;
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
/*if (formatMode.value == "markdown") {
|
|
207
|
+
fullTxt += t;
|
|
208
|
+
process.stdout.write('\u001Bc\u001B[3J');
|
|
209
|
+
process.stdout.write(marked.parse(fullTxt) as string);*/
|
|
210
|
+
//} else {
|
|
211
|
+
process.stdout.write(t);
|
|
212
|
+
//}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
const perfTimer = usePerfTimer(false);
|
|
217
|
+
//const spinner = ora({ text: "Thinking ...", discardStdin: false });
|
|
218
|
+
let abort = localOptions?.abort ? localOptions.abort : new AbortController();
|
|
219
|
+
const abortTicker = setInterval(() => {
|
|
220
|
+
//console.log("ABS", abort.signal.aborted);
|
|
221
|
+
if (abort.signal.aborted) {
|
|
222
|
+
agent.lm.abort();
|
|
223
|
+
abort = new AbortController();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
}, 200);
|
|
227
|
+
const processToken = (t) => {
|
|
228
|
+
if (emittedTokens == 0) {
|
|
229
|
+
perfTimer.start();
|
|
230
|
+
}
|
|
231
|
+
printToken(t);
|
|
232
|
+
++emittedTokens;
|
|
233
|
+
};
|
|
234
|
+
localOptions.params.stream = true;
|
|
235
|
+
if (!localOptions?.onToken) {
|
|
236
|
+
localOptions.onToken = processToken;
|
|
237
|
+
}
|
|
238
|
+
localOptions.baseDir = agentDir;
|
|
239
|
+
localOptions.variables = vars;
|
|
240
|
+
if (!localOptions?.tools) {
|
|
241
|
+
localOptions.tools = agentSpec.tools;
|
|
242
|
+
}
|
|
243
|
+
let out;
|
|
244
|
+
//console.log("CLI EXEC AGENT", payload.prompt, "\nOPTS", localOptions)
|
|
245
|
+
try {
|
|
246
|
+
out = await agent.run(payload.prompt, localOptions);
|
|
247
|
+
}
|
|
248
|
+
catch (e) {
|
|
249
|
+
//console.log("ERR CATCH", e);
|
|
250
|
+
const errMsg = `${e}`;
|
|
251
|
+
if (errMsg.includes("502 Bad Gateway")) {
|
|
252
|
+
clearInterval(abortTicker);
|
|
253
|
+
const msg = "The server answered with a 502 Bad Gateway error. It might be down or misconfigured. Check your inference server.";
|
|
254
|
+
const err = msg + "\n" + errMsg;
|
|
255
|
+
if ((localOptions?.onError)) {
|
|
256
|
+
console.error(err, name);
|
|
257
|
+
localOptions.onError(err, name);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
runtimeError(msg);
|
|
261
|
+
if (localOptions?.nocli) {
|
|
262
|
+
throw new Error(err);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return {};
|
|
266
|
+
}
|
|
267
|
+
else if (errMsg.includes("404 Not Found")) {
|
|
268
|
+
clearInterval(abortTicker);
|
|
269
|
+
const msg = "The server answered with a 404 Not Found error. That might mean that the model you are requesting does not exist on the server.";
|
|
270
|
+
const err = msg + "\n" + errMsg;
|
|
271
|
+
if ((localOptions?.onError)) {
|
|
272
|
+
console.error(err, name);
|
|
273
|
+
localOptions.onError(err, name);
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
runtimeError(msg);
|
|
277
|
+
if (localOptions?.nocli) {
|
|
278
|
+
throw new Error(err);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return {};
|
|
282
|
+
}
|
|
283
|
+
else if (errMsg.includes("400 Bad Request")) {
|
|
284
|
+
clearInterval(abortTicker);
|
|
285
|
+
const msg = "The server answered with a 400 Bad Request error. That might mean that:\n- The model you are requesting does not exist on the server\n- A parameter is wrong or missing in your request\n- The request size exceeds the available context window size";
|
|
286
|
+
const err = msg + "\n" + errMsg;
|
|
287
|
+
if ((localOptions?.onError)) {
|
|
288
|
+
console.error(err, name);
|
|
289
|
+
localOptions.onError(err, name);
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
runtimeError();
|
|
293
|
+
if (localOptions?.nocli) {
|
|
294
|
+
throw new Error(err);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return {};
|
|
298
|
+
}
|
|
299
|
+
else if (errMsg.includes("fetch failed")) {
|
|
300
|
+
clearInterval(abortTicker);
|
|
301
|
+
const msg = "The server is not responding. Check if your inference backend is running.";
|
|
302
|
+
const err = msg + "\n" + errMsg;
|
|
303
|
+
if ((localOptions?.onError)) {
|
|
304
|
+
console.error(err, name);
|
|
305
|
+
localOptions.onError(err, name);
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
runtimeError(msg);
|
|
309
|
+
if (localOptions?.nocli) {
|
|
310
|
+
throw new Error(err);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
//@ts-ignore
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
else if (e instanceof DOMException && e.name === 'AbortError') {
|
|
317
|
+
if (localOptions?.debug || localOptions?.verbose) {
|
|
318
|
+
console.warn("\n*** The request was canceled by the user ***");
|
|
319
|
+
}
|
|
320
|
+
clearInterval(abortTicker);
|
|
321
|
+
/*if ((localOptions?.onError)) {
|
|
322
|
+
localOptions.onError(errMsg, name);
|
|
323
|
+
} else {*/
|
|
324
|
+
return {};
|
|
325
|
+
// }
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
throw new Error(errMsg);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
clearInterval(abortTicker);
|
|
332
|
+
//console.log("END TASK", out);
|
|
333
|
+
if (!localOptions?.isToolCall) {
|
|
334
|
+
if (!out.text.endsWith("\n")) {
|
|
335
|
+
console.log();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
//console.log("END", name, "ISCM", isChatMode.value, "isTC", localOptions?.isToolCall)
|
|
339
|
+
/*if (!isChatMode.value || localOptions?.isToolCall) {
|
|
340
|
+
// close mcp connections
|
|
341
|
+
if (localOptions?.debug && mcpServers.length > 0) {
|
|
342
|
+
console.log("Closing", mcpServers.length, "mcp server(s)")
|
|
343
|
+
}
|
|
344
|
+
mcpServers.forEach((s) => {
|
|
345
|
+
s.stop();
|
|
346
|
+
if (localOptions?.debug) {
|
|
347
|
+
console.log("MCP stop", s.name);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}*/
|
|
351
|
+
await processOutput(out);
|
|
352
|
+
// chat mode
|
|
353
|
+
//console.log("CLI CONF IP", initialInferParams);
|
|
354
|
+
/*if (!localOptions?.isToolCall && isChatMode.value) {
|
|
355
|
+
if (task.def.tools) {
|
|
356
|
+
localOptions.tools = task.def.tools
|
|
357
|
+
}
|
|
358
|
+
if (task.def.shots) {
|
|
359
|
+
localOptions.history = localOptions?.history ? [...localOptions.history, ...task.def.shots] : task.def.shots;
|
|
360
|
+
}
|
|
361
|
+
if (task.def.template?.system) {
|
|
362
|
+
localOptions.system = task.def.template.system
|
|
363
|
+
}
|
|
364
|
+
if (task.def.template?.assistant) {
|
|
365
|
+
localOptions.assistant = task.def.template.assistant
|
|
366
|
+
}
|
|
367
|
+
setChatInferenceParams(initialInferParams);
|
|
368
|
+
//await chat(program, localOptions, agent, mcpServers);
|
|
369
|
+
}*/
|
|
370
|
+
/*if (localOptions?.verbosity?.stats) {
|
|
371
|
+
try {
|
|
372
|
+
console.log(emittedTokens.toString(), color.dim("tokens"), out.stats.tokensPerSecond, color.dim("tps"));
|
|
373
|
+
} catch (e) {
|
|
374
|
+
runtimeWarning("Error formating stats:", `${e}`)
|
|
375
|
+
}
|
|
376
|
+
}*/
|
|
377
|
+
/*if (localOptions?.backend || settings?.backend) {
|
|
378
|
+
//console.log("SET BACK AGENT BACKEND TO", backend.value);
|
|
379
|
+
// set back the default backend
|
|
380
|
+
agent.lm = backend.value!;
|
|
381
|
+
}*/
|
|
382
|
+
//console.log("TASK OUT", out);
|
|
383
|
+
//console.log("TASK A", agent);
|
|
384
|
+
return out;
|
|
385
|
+
};
|
|
386
|
+
return {
|
|
387
|
+
agent,
|
|
388
|
+
vars,
|
|
389
|
+
mcpServers,
|
|
390
|
+
agentDir,
|
|
391
|
+
settings,
|
|
392
|
+
execute,
|
|
393
|
+
};
|
|
394
|
+
};
|
|
395
|
+
export { useAgentExecutor };
|
package/dist/conf.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ConfigFile } from "@agent-smith/types";
|
|
2
|
+
declare function getConfigPath(appName: string, filename: string): {
|
|
3
|
+
confDir: string;
|
|
4
|
+
dbPath: string;
|
|
5
|
+
};
|
|
6
|
+
declare const confDir: string, dbPath: string;
|
|
7
|
+
declare function updateConfigFile(conf: ConfigFile, cfp?: string): string;
|
|
8
|
+
declare function createConfigFileIfNotExists(cfp?: string): string;
|
|
9
|
+
declare function processConfPath(confPath: string): Promise<{
|
|
10
|
+
paths: Array<string>;
|
|
11
|
+
pf: string;
|
|
12
|
+
dd: string;
|
|
13
|
+
}>;
|
|
14
|
+
export { confDir, createConfigFileIfNotExists, dbPath, getConfigPath, processConfPath, updateConfigFile };
|