@agent-smith/cli 0.0.115 → 0.0.118

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.
Files changed (114) hide show
  1. package/README.md +274 -34
  2. package/dist/cli.js +4 -8
  3. package/dist/cmd/{clicmds/aliases.d.ts → aliases.d.ts} +1 -1
  4. package/dist/cmd/{clicmds/aliases.js → aliases.js} +1 -26
  5. package/dist/cmd/{clicmds/base.js → base.js} +21 -17
  6. package/dist/cmd/build.d.ts +9 -0
  7. package/dist/cmd/build.js +59 -0
  8. package/dist/cmd/callbacks.d.ts +11 -0
  9. package/dist/cmd/callbacks.js +90 -0
  10. package/dist/cmd/cmds.d.ts +8 -9
  11. package/dist/cmd/cmds.js +134 -103
  12. package/dist/cmd/features.d.ts +6 -0
  13. package/dist/cmd/features.js +57 -0
  14. package/dist/cmd/{sys/read_cmds.d.ts → read_cmds.d.ts} +1 -1
  15. package/dist/cmd/{sys/read_cmds.js → read_cmds.js} +1 -1
  16. package/dist/cmd/user_cmds.d.ts +3 -0
  17. package/dist/cmd/user_cmds.js +23 -0
  18. package/dist/index.js +11 -11
  19. package/dist/main.d.ts +3 -38
  20. package/dist/main.js +3 -37
  21. package/dist/state.d.ts +6 -0
  22. package/dist/state.js +8 -0
  23. package/dist/utils.d.ts +8 -0
  24. package/dist/utils.js +50 -0
  25. package/package.json +10 -20
  26. package/dist/cmd/clicmds/cmds.d.ts +0 -7
  27. package/dist/cmd/clicmds/cmds.js +0 -135
  28. package/dist/cmd/clicmds/updateconf.d.ts +0 -3
  29. package/dist/cmd/clicmds/updateconf.js +0 -74
  30. package/dist/cmd/lib/actions/cmd.d.ts +0 -6
  31. package/dist/cmd/lib/actions/cmd.js +0 -154
  32. package/dist/cmd/lib/actions/read.d.ts +0 -3
  33. package/dist/cmd/lib/actions/read.js +0 -17
  34. package/dist/cmd/lib/adaptaters/cmd.d.ts +0 -2
  35. package/dist/cmd/lib/adaptaters/cmd.js +0 -39
  36. package/dist/cmd/lib/agents/cmd.d.ts +0 -3
  37. package/dist/cmd/lib/agents/cmd.js +0 -11
  38. package/dist/cmd/lib/mcp.d.ts +0 -16
  39. package/dist/cmd/lib/mcp.js +0 -105
  40. package/dist/cmd/lib/options_parsers.d.ts +0 -7
  41. package/dist/cmd/lib/options_parsers.js +0 -52
  42. package/dist/cmd/lib/tasks/cmd.d.ts +0 -4
  43. package/dist/cmd/lib/tasks/cmd.js +0 -419
  44. package/dist/cmd/lib/tasks/conf.d.ts +0 -6
  45. package/dist/cmd/lib/tasks/conf.js +0 -131
  46. package/dist/cmd/lib/tasks/read.d.ts +0 -14
  47. package/dist/cmd/lib/tasks/read.js +0 -186
  48. package/dist/cmd/lib/tasks/utils.d.ts +0 -8
  49. package/dist/cmd/lib/tasks/utils.js +0 -48
  50. package/dist/cmd/lib/tools.d.ts +0 -15
  51. package/dist/cmd/lib/tools.js +0 -147
  52. package/dist/cmd/lib/utils.d.ts +0 -3
  53. package/dist/cmd/lib/utils.js +0 -54
  54. package/dist/cmd/lib/workflows/cmd.d.ts +0 -3
  55. package/dist/cmd/lib/workflows/cmd.js +0 -230
  56. package/dist/cmd/lib/workflows/read.d.ts +0 -6
  57. package/dist/cmd/lib/workflows/read.js +0 -61
  58. package/dist/cmd/sys/clipboard.d.ts +0 -3
  59. package/dist/cmd/sys/clipboard.js +0 -33
  60. package/dist/cmd/sys/delete_file.d.ts +0 -2
  61. package/dist/cmd/sys/delete_file.js +0 -10
  62. package/dist/cmd/sys/dirs.d.ts +0 -2
  63. package/dist/cmd/sys/dirs.js +0 -9
  64. package/dist/cmd/sys/execute.d.ts +0 -13
  65. package/dist/cmd/sys/execute.js +0 -48
  66. package/dist/cmd/sys/read.d.ts +0 -3
  67. package/dist/cmd/sys/read.js +0 -21
  68. package/dist/cmd/sys/read_conf.d.ts +0 -6
  69. package/dist/cmd/sys/read_conf.js +0 -12
  70. package/dist/cmd/sys/read_features.d.ts +0 -3
  71. package/dist/cmd/sys/read_features.js +0 -112
  72. package/dist/cmd/sys/read_task.d.ts +0 -6
  73. package/dist/cmd/sys/read_task.js +0 -23
  74. package/dist/cmd/sys/read_yml_file.d.ts +0 -5
  75. package/dist/cmd/sys/read_yml_file.js +0 -11
  76. package/dist/cmd/sys/run_python.d.ts +0 -6
  77. package/dist/cmd/sys/run_python.js +0 -39
  78. package/dist/conf.d.ts +0 -14
  79. package/dist/conf.js +0 -166
  80. package/dist/const.d.ts +0 -3
  81. package/dist/const.js +0 -24
  82. package/dist/db/db.d.ts +0 -4
  83. package/dist/db/db.js +0 -23
  84. package/dist/db/read.d.ts +0 -34
  85. package/dist/db/read.js +0 -128
  86. package/dist/db/schemas.d.ts +0 -2
  87. package/dist/db/schemas.js +0 -105
  88. package/dist/db/write.d.ts +0 -15
  89. package/dist/db/write.js +0 -376
  90. package/dist/interfaces.d.ts +0 -165
  91. package/dist/interfaces.js +0 -1
  92. package/dist/state/backends.d.ts +0 -7
  93. package/dist/state/backends.js +0 -159
  94. package/dist/state/chat.d.ts +0 -7
  95. package/dist/state/chat.js +0 -10
  96. package/dist/state/features.d.ts +0 -8
  97. package/dist/state/features.js +0 -35
  98. package/dist/state/plugins.d.ts +0 -6
  99. package/dist/state/plugins.js +0 -27
  100. package/dist/state/state.d.ts +0 -20
  101. package/dist/state/state.js +0 -80
  102. package/dist/state/tasks.d.ts +0 -6
  103. package/dist/state/tasks.js +0 -28
  104. package/dist/utils/perf.d.ts +0 -9
  105. package/dist/utils/perf.js +0 -63
  106. package/dist/utils/text.d.ts +0 -6
  107. package/dist/utils/text.js +0 -41
  108. package/dist/utils/user_msgs.d.ts +0 -5
  109. package/dist/utils/user_msgs.js +0 -19
  110. /package/dist/cmd/{clicmds/base.d.ts → base.d.ts} +0 -0
  111. /package/dist/{cmd/options.d.ts → options.d.ts} +0 -0
  112. /package/dist/{cmd/options.js → options.js} +0 -0
  113. /package/dist/{cmd/lib/user_msgs.d.ts → user_msgs.d.ts} +0 -0
  114. /package/dist/{cmd/lib/user_msgs.js → user_msgs.js} +0 -0
@@ -1,131 +0,0 @@
1
- import { useTemplateForModel } from "@agent-smith/tfm";
2
- const tfm = useTemplateForModel();
3
- function guessTemplate(modelname) {
4
- const gt = tfm.guess(modelname);
5
- if (gt == "none") {
6
- throw new Error(`Unable to guess the template for ${modelname}: please provide a template name: --tpl templatename`);
7
- }
8
- return gt;
9
- }
10
- function configureTaskModel(itConf, taskSpec) {
11
- //console.log("IT CONF", itConf);
12
- //console.log("TASK SPEC", taskSpec);
13
- //let modelName: string = "";
14
- //let templateName: string = "";
15
- let ip = itConf.inferParams;
16
- let isModelFromTaskFile = false;
17
- let model = { template: "", name: "" };
18
- let foundModel = false;
19
- let foundTemplate = false;
20
- if (itConf?.templateName) {
21
- model.template = itConf.templateName;
22
- foundTemplate = true;
23
- }
24
- if (!foundTemplate) {
25
- if (itConf?.model?.name) {
26
- const gt = guessTemplate(itConf.model.name);
27
- model.template = gt;
28
- foundTemplate = true;
29
- }
30
- else if (taskSpec?.model?.template) {
31
- model.template = taskSpec.model.template;
32
- foundTemplate = true;
33
- }
34
- else {
35
- const gt = guessTemplate(taskSpec.model.name);
36
- model.template = gt;
37
- foundTemplate = true;
38
- }
39
- }
40
- if (itConf?.model?.name) {
41
- if (taskSpec?.models && Object.keys(taskSpec.models).includes(itConf.model.name)) {
42
- // try to find the model from the task's models
43
- for (const [k, v] of Object.entries(taskSpec.models)) {
44
- //console.log("TSM", modelName, "/", k);
45
- if (k == itConf.model.name) {
46
- model = v;
47
- if (v?.inferParams) {
48
- const tip = v.inferParams;
49
- for (const [k, v] of Object.entries(tip)) {
50
- // @ts-ignore
51
- ip[k] = v;
52
- }
53
- }
54
- if (v?.system) {
55
- model.system = v.system;
56
- }
57
- if (v.assistant) {
58
- model.assistant = v.assistant;
59
- }
60
- isModelFromTaskFile = true;
61
- foundModel = true;
62
- break;
63
- }
64
- }
65
- }
66
- else {
67
- model.name = itConf.model.name;
68
- foundModel = true;
69
- }
70
- }
71
- else {
72
- if (!taskSpec?.model) {
73
- throw new Error(`No model found: either specify a model in the task or use the -m flag`);
74
- }
75
- model = taskSpec.model;
76
- foundModel = true;
77
- }
78
- // try to find the models from db models
79
- /*if (!found) {
80
- const m = readModel(modelName);
81
- //console.log("FM", m.found, m)
82
- //console.log("DBM", templateName, "/", m.modelData.template);
83
- if (m.found) {
84
- model = m.modelData as ModelSpec;
85
- model.template = templateName ?? m.modelData.template;
86
- found = true
87
- }
88
- }*/
89
- // fallback to use the model name directly
90
- if (!foundModel) {
91
- throw new Error(`No model found in task`);
92
- }
93
- /*if (!foundTemplate) {
94
- // try to guess the template
95
- const gt = tfm.guess(model.name);
96
- if (gt == "none") {
97
- throw new Error(`Unable to guess the template for ${model.name}: please provide a template name: --tpl templatename`)
98
- }
99
- model.template = gt;
100
- foundTemplate = true;
101
- }
102
- if (!model?.template) {
103
- throw new Error(`No template found`)
104
- }*/
105
- //model.inferParams = ip;
106
- // use default ctx if the model is not from defined in the task file
107
- if (!model?.ctx || !isModelFromTaskFile) {
108
- model.ctx = taskSpec.ctx;
109
- }
110
- //model.inferParams = ip;
111
- return model;
112
- }
113
- function mergeConfOptions(conf, options) {
114
- const res = conf;
115
- for (const [k, v] of Object.entries(options)) {
116
- if (k == "inferParams") {
117
- continue;
118
- }
119
- res[k] = v;
120
- }
121
- return res;
122
- }
123
- function mergeInferParams(userInferParams, taskInferParams) {
124
- const ip = taskInferParams;
125
- //console.log("IP", ip);
126
- for (const [k, v] of Object.entries(userInferParams)) {
127
- ip[k] = v;
128
- }
129
- return ip;
130
- }
131
- export { mergeConfOptions, mergeInferParams, configureTaskModel, };
@@ -1,14 +0,0 @@
1
- import { Agent } from "@agent-smith/agent";
2
- import { ModelSpec } from "@agent-smith/task";
3
- import { NodeTask } from "@agent-smith/nodetask";
4
- import { McpClient } from "../mcp.js";
5
- import type { LmTaskConfig } from "../../../interfaces.js";
6
- declare function readTask(name: string, payload: Record<string, any>, options: Record<string, any>, agent: Agent): Promise<{
7
- task: NodeTask;
8
- model: ModelSpec;
9
- conf: LmTaskConfig;
10
- vars: Record<string, any>;
11
- mcpServers: Array<McpClient>;
12
- taskDir: string;
13
- }>;
14
- export { readTask };
@@ -1,186 +0,0 @@
1
- import path from "path";
2
- import { NodeTask } from "@agent-smith/nodetask";
3
- import { compile, serializeGrammar } from "@intrinsicai/gbnfgen";
4
- import { readTool } from "../../../db/read.js";
5
- import { executeAction } from "../actions/cmd.js";
6
- import { McpClient } from "../mcp.js";
7
- import { parseTaskConfigOptions } from "../options_parsers.js";
8
- import { executeWorkflow } from "../workflows/cmd.js";
9
- import { executeTask } from "./cmd.js";
10
- import { configureTaskModel, mergeInferParams } from "./conf.js";
11
- import { openTaskSpec } from "./utils.js";
12
- import { confirmToolUsage } from "../tools.js";
13
- async function readTask(name, payload, options, agent) {
14
- if (options?.debug) {
15
- console.log("Task", name);
16
- console.log("Payload:", payload);
17
- console.log("Task options:", options);
18
- }
19
- const { taskFileSpec, taskPath } = openTaskSpec(name, options?.isAgent);
20
- const taskDir = path.dirname(taskPath);
21
- // merge passed options from payload
22
- const opts = payload?.inferParams ? { ...options, ...payload.inferParams } : options;
23
- const conf = parseTaskConfigOptions(opts);
24
- if (options?.debug) {
25
- console.log("Task conf:", conf);
26
- conf.debug = true;
27
- }
28
- conf.inferParams = mergeInferParams(conf.inferParams, taskFileSpec.inferParams ?? {});
29
- const model = configureTaskModel(conf, taskFileSpec);
30
- if (options?.ctx) {
31
- model.ctx = options.ctx;
32
- }
33
- // vars
34
- const taskSpec = taskFileSpec;
35
- let vars = {};
36
- if (taskSpec?.variables?.optional) {
37
- for (const k of Object.keys(taskSpec.variables.optional)) {
38
- if (k in options) {
39
- vars[k] = options[k];
40
- }
41
- else if (k in payload) {
42
- vars[k] = payload[k];
43
- }
44
- }
45
- }
46
- if (taskSpec?.variables?.required) {
47
- for (const k of Object.keys(taskSpec.variables.required)) {
48
- //console.log("TASK V required:", Object.keys(taskSpec.variables.required), "/", k in options, "/", k in payload);
49
- if (k in options) {
50
- vars[k] = options[k];
51
- }
52
- else if (k in payload) {
53
- vars[k] = payload[k];
54
- }
55
- }
56
- }
57
- const mcpServers = new Array();
58
- if (!taskSpec?.tools) {
59
- taskSpec.tools = [];
60
- }
61
- const mcpServersArgs = {};
62
- if (options?.mcp) {
63
- options.mcp.forEach(v => {
64
- const s = v.split(":");
65
- if (s.length < 2) {
66
- throw new Error(`Malformed mcp option ${v}: use --mcp servername:arg1,arg2`);
67
- }
68
- const sn = s[0];
69
- const sa = s[1];
70
- const _margs = sa.split(",");
71
- mcpServersArgs[sn] = _margs;
72
- });
73
- if (options?.debug) {
74
- console.log("Opening", options.mcp.length, "server(s)");
75
- }
76
- }
77
- // mcp tools
78
- if (taskFileSpec?.mcp) {
79
- for (const [servername, tool] of Object.entries(taskFileSpec.mcp)) {
80
- //console.log("MCP TOOL:", tool)
81
- const authorizedTools = new Array();
82
- const askUserTools = new Array();
83
- if (tool?.tools) {
84
- tool.tools.forEach(t => {
85
- let tn = t;
86
- if (t.endsWith("?")) {
87
- tn = t.slice(0, -1);
88
- askUserTools.push(tn);
89
- }
90
- authorizedTools.push(tn);
91
- });
92
- }
93
- const margs = tool.arguments;
94
- if (servername in mcpServersArgs) {
95
- margs.push(...mcpServersArgs[servername]);
96
- }
97
- const mcp = new McpClient(servername, tool.command, tool.arguments, authorizedTools.length > 0 ? authorizedTools : null, askUserTools.length > 0 ? askUserTools : null);
98
- //console.log("MCP", mcp);
99
- mcpServers.push(mcp);
100
- /*await mcp.start();
101
- const tools = await mcp.extractTools();
102
- tools.forEach(t => taskSpec.tools?.push(t))*/
103
- }
104
- }
105
- // tools
106
- //console.log("Task tools list:", taskSpec.toolsList);
107
- if (taskSpec.toolsList) {
108
- for (const rawToolName of taskSpec.toolsList) {
109
- let toolName = rawToolName;
110
- let autoRunTool = true;
111
- if (rawToolName.endsWith("?")) {
112
- autoRunTool = false;
113
- toolName = rawToolName.slice(0, -1);
114
- }
115
- const { found, tool, type } = readTool(toolName);
116
- if (!found) {
117
- throw new Error(`tool ${toolName} not found for task ${taskSpec.name}`);
118
- }
119
- //console.log("Tool found:", toolName, tool);
120
- const quiet = !options?.debug;
121
- const lmTool = {
122
- ...tool,
123
- execute: async (params) => {
124
- //console.log("EXEC TOOL:", type, toolName, params);
125
- switch (type) {
126
- case "action":
127
- const res = await executeAction(toolName, params, options, quiet);
128
- return res;
129
- case "task":
130
- options.isToolCall = true;
131
- options.isAgent = false;
132
- const tres = await executeTask(toolName, params, options);
133
- options.isToolCall = false;
134
- //console.log("WFTRESP", tres.answer.text);
135
- return tres.answer.text;
136
- case "agent":
137
- options.isToolCall = true;
138
- options.isAgent = true;
139
- const agres = await executeTask(toolName, params, options);
140
- options.isAgent = false;
141
- options.isToolCall = false;
142
- //console.log("WFTRESP", tres.answer.text);
143
- if (agres?.answer?.text) {
144
- return agres.answer.text;
145
- }
146
- return agres;
147
- case "workflow":
148
- options.isToolCall = true;
149
- const wres = await executeWorkflow(toolName, params, options);
150
- options.isToolCall = false;
151
- return wres;
152
- default:
153
- throw new Error(`unknown tool execution function type: ${type} for ${toolName}`);
154
- }
155
- }
156
- };
157
- if (!autoRunTool) {
158
- lmTool.canRun = options?.confirmToolUsage ?
159
- options.confirmToolUsage :
160
- confirmToolUsage;
161
- }
162
- taskSpec.tools.push(lmTool);
163
- }
164
- delete taskSpec.toolsList;
165
- }
166
- ;
167
- if (options?.chatMode) {
168
- taskSpec.prompt = "{prompt}";
169
- }
170
- //console.log("TASK SPEC:", JSON.stringify(taskSpec, null, " "));
171
- const task = new NodeTask(agent, taskSpec);
172
- //task.addTools(taskSpec.tools);
173
- //console.log("TASK TOOLS", task.agent.tools);
174
- // check for grammars
175
- if (model?.inferParams?.tsGrammar) {
176
- //console.log("TSG");
177
- model.inferParams.grammar = serializeGrammar(await compile(model.inferParams.tsGrammar, "Grammar"));
178
- delete model.inferParams.tsGrammar;
179
- }
180
- /*if (options?.debug) {
181
- console.log("Task model:", model);
182
- //console.log("Task vars:", vars);
183
- }*/
184
- return { task, model, conf, vars, mcpServers, taskDir };
185
- }
186
- export { readTask };
@@ -1,8 +0,0 @@
1
- import { LmTaskFileSpec } from "../../../interfaces.js";
2
- declare function openTaskSpec(name: string, isAgent?: boolean): {
3
- taskFileSpec: LmTaskFileSpec;
4
- taskPath: string;
5
- };
6
- declare function getInputFromOptions(options: Record<string, any>): Promise<string | null>;
7
- declare function getTaskPrompt(name: string, args: Array<string>, options: Record<string, any>): Promise<string>;
8
- export { getTaskPrompt, getInputFromOptions, openTaskSpec, };
@@ -1,48 +0,0 @@
1
- import YAML from 'yaml';
2
- import { readClipboard } from '../../../cmd/sys/clipboard.js';
3
- import { readTask } from "../../../cmd/sys/read_task.js";
4
- import { getFeatureSpec } from "../../../state/features.js";
5
- import { runtimeDataError } from '../user_msgs.js';
6
- import { readPromptFile } from '../utils.js';
7
- function openTaskSpec(name, isAgent = false) {
8
- const ft = isAgent ? "agent" : "task";
9
- const { found, path } = getFeatureSpec(name, ft);
10
- if (!found) {
11
- throw new Error(`${ft} ${name} not found`);
12
- }
13
- const res = readTask(path);
14
- if (!res.found) {
15
- throw new Error(`${ft} ${name}, ${path} not found`);
16
- }
17
- const taskFileSpec = YAML.parse(res.ymlTask);
18
- taskFileSpec.name = name;
19
- return { taskFileSpec: taskFileSpec, taskPath: path };
20
- }
21
- async function getInputFromOptions(options) {
22
- let out = null;
23
- if (options?.clipboardInput === true) {
24
- out = await readClipboard();
25
- options.clipboardInput = false;
26
- }
27
- else if (options?.inputFile === true) {
28
- out = readPromptFile();
29
- options.inputFile = false;
30
- }
31
- return out;
32
- }
33
- async function getTaskPrompt(name, args, options) {
34
- const ic = await getInputFromOptions(options);
35
- if (ic) {
36
- return ic;
37
- }
38
- let pr;
39
- if (args[0] !== undefined) {
40
- pr = args[0];
41
- }
42
- else {
43
- runtimeDataError(options?.isAgent ? "agent" : "task", name, "provide a prompt or use input options");
44
- throw new Error();
45
- }
46
- return pr;
47
- }
48
- export { getTaskPrompt, getInputFromOptions, openTaskSpec, };
@@ -1,15 +0,0 @@
1
- import { FeatureExtension } from '../../interfaces.js';
2
- import { type ToolCallSpec } from '@locallm/types';
3
- declare function confirmToolUsage(toolCall: ToolCallSpec): Promise<boolean>;
4
- declare function extractTaskToolDocAndVariables(name: string, ext: FeatureExtension, dirPath: string): {
5
- toolDoc: string;
6
- variables: {
7
- required: Array<string>;
8
- optional: Array<string>;
9
- };
10
- };
11
- declare function extractToolDoc(name: string, ext: FeatureExtension, dirPath: string): {
12
- found: boolean;
13
- toolDoc: string;
14
- };
15
- export { extractToolDoc, extractTaskToolDocAndVariables, confirmToolUsage, };
@@ -1,147 +0,0 @@
1
- import YAML from 'yaml';
2
- import * as fs from 'fs';
3
- import { readYmlFile } from '../sys/read_yml_file.js';
4
- import { confirm } from '@inquirer/prompts';
5
- import colors from "ansi-colors";
6
- async function confirmToolUsage(toolCall) {
7
- console.log("Tool call:", colors.bold(toolCall.name));
8
- if (toolCall?.arguments) {
9
- console.log("Arguments:", toolCall.arguments);
10
- }
11
- else {
12
- console.log("No arguments");
13
- }
14
- return await confirm({ message: `Execute tool ${toolCall.name}?` });
15
- }
16
- function _extractToolDoc(filePath, startComment, endComment) {
17
- try {
18
- const fileContent = fs.readFileSync(filePath, 'utf-8');
19
- const startMarker = startComment + '\n# tool';
20
- if (!fileContent.startsWith(startMarker)) {
21
- return { found: false, doc: "" };
22
- }
23
- const endMarker = endComment;
24
- const startIndex = fileContent.indexOf(startMarker) + startMarker.length;
25
- const endIndex = fileContent.indexOf(endMarker, startIndex);
26
- if (endIndex === -1) {
27
- throw new Error(`Markers not found in the file: ${filePath}`);
28
- }
29
- const extractedContent = fileContent.substring(startIndex, endIndex).trim();
30
- return { found: true, doc: extractedContent };
31
- }
32
- catch (error) {
33
- if (error instanceof Error) {
34
- if (error.message.includes('ENOENT')) {
35
- throw new Error(`File not found: ${filePath}`);
36
- }
37
- else {
38
- throw new Error(`Error processing the file: ${filePath}. ${error.message}`);
39
- }
40
- }
41
- else {
42
- throw new Error(`An unexpected error occurred: ${error}`);
43
- }
44
- }
45
- }
46
- function _extractPyToolDoc(filePath) {
47
- return _extractToolDoc(filePath, '"""', '"""');
48
- }
49
- function _extractJsToolDoc(filePath) {
50
- return _extractToolDoc(filePath, '/*', '*/');
51
- }
52
- function _extractYamlToolDoc(filePath, name) {
53
- const { data, found } = readYmlFile(filePath);
54
- //console.log("_extractYamlToolDoc from", name, data?.tool);
55
- if (!found) {
56
- return { found: false, tspec: {} };
57
- }
58
- if (!data?.tool) {
59
- return { found: false, tspec: {} };
60
- }
61
- data.tool.name = name;
62
- return { found: true, tspec: data.tool };
63
- }
64
- function _parseToolDoc(rawTxt, name) {
65
- try {
66
- const res = YAML.parse(rawTxt);
67
- res["name"] = name;
68
- //console.log("PARSE TOOL DOC", res);
69
- return res;
70
- }
71
- catch (e) {
72
- throw new Error(`Error parsing tool ${name}: ${e} \nData:\n${rawTxt}\n`);
73
- }
74
- }
75
- function _parseTaskVariables(data) {
76
- const res = { required: new Array(), optional: new Array() };
77
- if (data?.variables) {
78
- if (data.variables?.required) {
79
- res.required = data.variables.required;
80
- }
81
- if (data.variables?.optional) {
82
- res.optional = data.variables.optional;
83
- }
84
- }
85
- return res;
86
- }
87
- function extractTaskToolDocAndVariables(name, ext, dirPath) {
88
- const fp = dirPath + "/" + name + "." + ext;
89
- const { data, found } = readYmlFile(fp);
90
- const res = { variables: { required: new Array(), optional: new Array() }, toolDoc: "" };
91
- // tools
92
- let tspec;
93
- if (!found) {
94
- throw new Error(`extractTaskToolDocAndVariables: file ${fp} not found`);
95
- }
96
- if (data?.tool) {
97
- data.tool.name = name;
98
- tspec = data.tool;
99
- res.toolDoc = JSON.stringify(tspec, null, " ");
100
- }
101
- // variables
102
- const { required, optional } = _parseTaskVariables(data);
103
- res.variables.required = required;
104
- res.variables.optional = optional;
105
- return res;
106
- }
107
- function extractToolDoc(name, ext, dirPath) {
108
- let spec;
109
- let found = false;
110
- let doc = "";
111
- let docts = null;
112
- switch (ext) {
113
- case "py":
114
- let res = _extractPyToolDoc(dirPath + "/" + name + "." + ext);
115
- found = res.found;
116
- doc = res.doc;
117
- break;
118
- case "js":
119
- let res2 = _extractJsToolDoc(dirPath + "/" + name + "." + ext);
120
- found = res2.found;
121
- doc = res2.doc;
122
- break;
123
- case "yml":
124
- let res3 = _extractYamlToolDoc(dirPath + "/" + name + "." + ext, name);
125
- found = res3.found;
126
- docts = res3.tspec;
127
- break;
128
- default:
129
- return { found: false, toolDoc: "" };
130
- //throw new Error(`Unknown tool doc feature type`)
131
- }
132
- if (found) {
133
- let ts;
134
- if (docts) {
135
- ts = docts;
136
- }
137
- else {
138
- ts = _parseToolDoc(doc, name);
139
- }
140
- spec = JSON.stringify(ts, null, " ");
141
- }
142
- else {
143
- return { found: false, toolDoc: "" };
144
- }
145
- return { found: true, toolDoc: spec };
146
- }
147
- export { extractToolDoc, extractTaskToolDocAndVariables, confirmToolUsage, };
@@ -1,3 +0,0 @@
1
- declare function readPromptFile(): string;
2
- declare function processOutput(res: any): Promise<void>;
3
- export { processOutput, readPromptFile, };
@@ -1,54 +0,0 @@
1
- import { formatMode, initFilepaths, outputMode, promptfilePath } from "../../state/state.js";
2
- import { writeToClipboard } from "../sys/clipboard.js";
3
- import { readFile } from "../sys/read.js";
4
- import { splitThinking } from "../../utils/text.js";
5
- import { runtimeError } from "./user_msgs.js";
6
- import { marked } from 'marked';
7
- import { markedTerminal } from "marked-terminal";
8
- marked.use(markedTerminal());
9
- function readPromptFile() {
10
- initFilepaths();
11
- return readFile(promptfilePath.value);
12
- }
13
- async function processOutput(res) {
14
- //if (!(outputMode.value == "clipboard")) { return }
15
- let data = "";
16
- //console.log("Process OUTPUT", typeof res);
17
- let hasTextData = false;
18
- if (typeof res == "object") {
19
- if (res?.answer?.text) {
20
- //console.log("****************** TPL", res?.answer?.template);
21
- if (res?.template?.tags?.think) {
22
- const { finalAnswer } = splitThinking(res.answer.text, res.template.tags.think.start, res.template.tags.think.end);
23
- data = finalAnswer;
24
- }
25
- else {
26
- data = res.answer.text;
27
- }
28
- hasTextData = true;
29
- }
30
- else {
31
- try {
32
- data = JSON.stringify(res);
33
- }
34
- catch (e) {
35
- runtimeError("Unable to parse json result");
36
- }
37
- }
38
- }
39
- else {
40
- data = res;
41
- }
42
- //onsole.log("OUTPUT", typeof res, data);
43
- if (outputMode.value == "clipboard") {
44
- //console.log("Writing to kb", data)
45
- await writeToClipboard(data);
46
- }
47
- if (hasTextData) {
48
- if (formatMode.value == "markdown") {
49
- console.log("\n------------------\n");
50
- console.log(marked.parse(data).trim());
51
- }
52
- }
53
- }
54
- export { processOutput, readPromptFile, };
@@ -1,3 +0,0 @@
1
- declare function executeWorkflow(wname: string, args: any, options?: Record<string, any>): Promise<any>;
2
- declare function executeWorkflowCmd(name: string, wargs: Array<any>): Promise<any>;
3
- export { executeWorkflow, executeWorkflowCmd };