@agent-smith/cli 0.0.43 → 0.0.45
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/cmds.js +12 -21
- package/dist/cmd/lib/actions/cmd.js +3 -1
- package/dist/cmd/lib/models.d.ts +2 -0
- package/dist/cmd/lib/models.js +35 -0
- package/dist/cmd/lib/tasks/cmd.d.ts +2 -2
- package/dist/cmd/lib/tasks/cmd.js +33 -64
- package/dist/cmd/lib/tasks/conf.d.ts +7 -0
- package/dist/cmd/lib/tasks/conf.js +144 -0
- package/dist/cmd/lib/utils.d.ts +1 -12
- package/dist/cmd/lib/utils.js +5 -167
- package/dist/cmd/sys/dirs.d.ts +2 -0
- package/dist/cmd/sys/dirs.js +9 -0
- package/dist/cmd/sys/read_cmds.js +3 -0
- package/dist/cmd/sys/read_features.js +2 -2
- package/dist/cmd/sys/read_modelfile.d.ts +8 -0
- package/dist/cmd/sys/{read_models.js → read_modelfile.js} +3 -3
- package/dist/conf.d.ts +1 -0
- package/dist/conf.js +6 -2
- package/dist/db/read.d.ts +12 -3
- package/dist/db/read.js +39 -9
- package/dist/db/schemas.js +8 -1
- package/dist/db/write.d.ts +4 -2
- package/dist/db/write.js +31 -2
- package/dist/interfaces.d.ts +41 -6
- package/dist/main.d.ts +6 -2
- package/dist/main.js +5 -2
- package/dist/primitives/args.d.ts +5 -0
- package/dist/primitives/args.js +40 -0
- package/dist/primitives/perf.d.ts +7 -0
- package/dist/primitives/perf.js +38 -0
- package/dist/state/chat.d.ts +2 -0
- package/dist/state/features.js +2 -2
- package/dist/state/state.d.ts +3 -1
- package/dist/state/state.js +29 -3
- package/package.json +10 -11
- package/dist/cmd/sys/read_models.d.ts +0 -7
- /package/dist/cmd/sys/{reset.d.ts → delete_file.d.ts} +0 -0
- /package/dist/cmd/sys/{reset.js → delete_file.js} +0 -0
package/dist/cmd/clicmds/cmds.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import YAML from 'yaml';
|
|
2
|
-
import { formatMode, isChatMode, promptfilePath, runMode } from "../../state/state.js";
|
|
2
|
+
import { dataDirPath, formatMode, isChatMode, promptfilePath, runMode } from "../../state/state.js";
|
|
3
3
|
import { getFeatureSpec, readFeaturesDirs } from "../../state/features.js";
|
|
4
4
|
import { readAliases, readFeatures } from "../../db/read.js";
|
|
5
|
-
import { cleanupFeaturePaths, updateAliases, updateFeatures, updatePromptfilePath } from "../../db/write.js";
|
|
5
|
+
import { cleanupFeaturePaths, updateAliases, updateDataDirPath, updateFeatures, updatePromptfilePath } from "../../db/write.js";
|
|
6
6
|
import { processConfPath } from "../../conf.js";
|
|
7
7
|
import { executeActionCmd } from "../lib/actions/cmd.js";
|
|
8
8
|
import { initAgent, marked, taskBuilder } from "../../agent.js";
|
|
@@ -10,7 +10,7 @@ import { executeTaskCmd } from "../lib/tasks/cmd.js";
|
|
|
10
10
|
import { readCmds } from "../sys/read_cmds.js";
|
|
11
11
|
import { executeWorkflowCmd } from "../lib/workflows/cmd.js";
|
|
12
12
|
import { readTask } from "../sys/read_task.js";
|
|
13
|
-
import { deleteFileIfExists } from "../sys/
|
|
13
|
+
import { deleteFileIfExists } from "../sys/delete_file.js";
|
|
14
14
|
import { dbPath } from "../../db/db.js";
|
|
15
15
|
let cmds = {
|
|
16
16
|
exit: {
|
|
@@ -21,29 +21,15 @@ let cmds = {
|
|
|
21
21
|
cmd: async () => pingCmd(["verbose"], undefined),
|
|
22
22
|
description: "ping inference servers",
|
|
23
23
|
},
|
|
24
|
-
|
|
24
|
+
tasks: {
|
|
25
25
|
cmd: _listTasksCmd,
|
|
26
26
|
description: "list all the tasks"
|
|
27
27
|
},
|
|
28
|
-
|
|
28
|
+
task: {
|
|
29
29
|
cmd: _readTaskCmd,
|
|
30
30
|
description: "read a task",
|
|
31
31
|
args: "arguments: \n-task (required): the task name"
|
|
32
32
|
},
|
|
33
|
-
t: {
|
|
34
|
-
cmd: _executeTaskCmd,
|
|
35
|
-
description: "execute a task",
|
|
36
|
-
},
|
|
37
|
-
w: {
|
|
38
|
-
cmd: _executeWorkflowCmd,
|
|
39
|
-
description: "execute a workflow",
|
|
40
|
-
args: "arguments: \n-workflow (required): the workflow name\n-args: arguments if any for the workflow"
|
|
41
|
-
},
|
|
42
|
-
a: {
|
|
43
|
-
cmd: executeActionCmd,
|
|
44
|
-
description: "execute an action",
|
|
45
|
-
args: "arguments: \n-action (required): the task name\n-args: other arguments if any for the action"
|
|
46
|
-
},
|
|
47
33
|
conf: {
|
|
48
34
|
cmd: _updateConfCmd,
|
|
49
35
|
description: "process config file",
|
|
@@ -96,11 +82,15 @@ async function _updateConfCmd(args = [], options) {
|
|
|
96
82
|
console.warn("Provide a config.yml file path");
|
|
97
83
|
return;
|
|
98
84
|
}
|
|
99
|
-
const { paths, pf } = await processConfPath(args[0]);
|
|
85
|
+
const { paths, pf, dd } = await processConfPath(args[0]);
|
|
100
86
|
if (pf.length > 0) {
|
|
101
87
|
updatePromptfilePath(pf);
|
|
102
88
|
promptfilePath.value = pf;
|
|
103
89
|
}
|
|
90
|
+
if (dd.length > 0) {
|
|
91
|
+
updateDataDirPath(dd);
|
|
92
|
+
dataDirPath.value = dd;
|
|
93
|
+
}
|
|
104
94
|
const feats = readFeaturesDirs(paths);
|
|
105
95
|
updateFeatures(feats);
|
|
106
96
|
updateAliases(feats);
|
|
@@ -161,6 +151,7 @@ async function _readTaskCmd(args = [], options) {
|
|
|
161
151
|
console.log(YAML.stringify(ts));
|
|
162
152
|
}
|
|
163
153
|
async function _listTasksCmd(args = [], options) {
|
|
164
|
-
Object.keys(readFeatures().task).
|
|
154
|
+
const ts = Object.keys(readFeatures().task).sort();
|
|
155
|
+
console.table(ts);
|
|
165
156
|
}
|
|
166
157
|
export { cmds, initCmds, pingCmd, initAliases };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { readModelfiles } from "../../db/read.js";
|
|
3
|
+
import { readModelsFile } from "../sys/read_modelfile.js";
|
|
4
|
+
import { upsertModels } from "../../db/write.js";
|
|
5
|
+
function updateModels() {
|
|
6
|
+
const mfs = readModelfiles();
|
|
7
|
+
const modelNames = new Array();
|
|
8
|
+
const modelDefs = new Array();
|
|
9
|
+
mfs.forEach((mf) => {
|
|
10
|
+
const filePath = path.join(mf.path + "/" + mf.name + "." + mf.ext);
|
|
11
|
+
const { models, ctx, max_tokens, found } = readModelsFile(filePath);
|
|
12
|
+
if (!found) {
|
|
13
|
+
throw new Error(`model file ${filePath} not found`);
|
|
14
|
+
}
|
|
15
|
+
for (const [name, m] of (Object.entries(models))) {
|
|
16
|
+
if (modelNames.includes(m.name)) {
|
|
17
|
+
console.log("🔴 [models] error: duplicate model name", m.name, "found in", filePath);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (!m?.ctx) {
|
|
21
|
+
m.ctx = ctx;
|
|
22
|
+
}
|
|
23
|
+
if (!m?.inferParams) {
|
|
24
|
+
m.inferParams = {};
|
|
25
|
+
}
|
|
26
|
+
if (!m?.inferParams?.max_tokens) {
|
|
27
|
+
m.inferParams.max_tokens = max_tokens;
|
|
28
|
+
}
|
|
29
|
+
const md = { name: m.name, shortname: name, data: m };
|
|
30
|
+
modelDefs.push(md);
|
|
31
|
+
}
|
|
32
|
+
upsertModels(modelDefs);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
export { updateModels, };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { LmTaskOutput } from "
|
|
2
|
-
declare function executeTaskCmd(args?: Array<string> | Record<string, any>, options?: any): Promise<LmTaskOutput>;
|
|
1
|
+
import { LmTaskOutput } from "@agent-smith/lmtask";
|
|
2
|
+
declare function executeTaskCmd(args?: Array<string> | Record<string, any>, options?: Record<string, any>): Promise<LmTaskOutput>;
|
|
3
3
|
export { executeTaskCmd };
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import { compile, serializeGrammar } from "@intrinsicai/gbnfgen";
|
|
2
|
+
import YAML from 'yaml';
|
|
1
3
|
import { brain, initAgent, taskBuilder } from "../../../agent.js";
|
|
2
4
|
import { getFeatureSpec } from "../../../state/features.js";
|
|
3
5
|
import { isChatMode, isDebug, isShowTokens, isVerbose } from "../../../state/state.js";
|
|
4
|
-
import { formatStats,
|
|
6
|
+
import { formatStats, parseInputOptions } from "../utils.js";
|
|
5
7
|
import { readTask } from "../../sys/read_task.js";
|
|
6
|
-
import {
|
|
7
|
-
import { LmTaskBuilder } from "../../../../../lmtask/dist/main.js";
|
|
8
|
+
import { readTool } from "../../../db/read.js";
|
|
8
9
|
import { executeActionCmd, } from "../actions/cmd.js";
|
|
9
10
|
import { executeWorkflowCmd } from "../workflows/cmd.js";
|
|
10
|
-
import {
|
|
11
|
+
import { configureTask, parseTaskVars } from "./conf.js";
|
|
11
12
|
async function executeTaskCmd(args = [], options = {}) {
|
|
12
13
|
await initAgent();
|
|
13
14
|
if (isDebug.value) {
|
|
@@ -53,48 +54,20 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
53
54
|
if (!res.found) {
|
|
54
55
|
throw new Error(`Task ${name}, ${path} not found`);
|
|
55
56
|
}
|
|
56
|
-
const
|
|
57
|
-
let
|
|
58
|
-
let
|
|
59
|
-
if (
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
const { found, ctx, max_tokens, models } = readModelsFile(modelsFeat.feature.path + "/" + modelsFeat.feature.name + "." + modelsFeat.feature.ext);
|
|
65
|
-
if (!found) {
|
|
66
|
-
throw new Error(`modelset ${taskRawSpec.modelset.name} not found`);
|
|
67
|
-
}
|
|
68
|
-
defaultCtx = ctx;
|
|
69
|
-
for (const [k, v] of Object.entries(models)) {
|
|
70
|
-
if (!v?.ctx) {
|
|
71
|
-
v.ctx = ctx;
|
|
72
|
-
models[k] = v;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
taskRawSpec.model = models[taskRawSpec.modelset.default];
|
|
76
|
-
if (!taskRawSpec.model) {
|
|
77
|
-
throw new Error(`model ${taskRawSpec.modelset.default} not found`);
|
|
78
|
-
}
|
|
79
|
-
if (!taskRawSpec.model?.ctx) {
|
|
80
|
-
taskRawSpec.model.ctx = ctx;
|
|
81
|
-
}
|
|
82
|
-
if (!taskRawSpec?.inferParams?.max_tokens) {
|
|
83
|
-
if (!taskRawSpec?.inferParams) {
|
|
84
|
-
taskRawSpec.inferParams = {};
|
|
85
|
-
}
|
|
86
|
-
taskRawSpec.inferParams.max_tokens = max_tokens;
|
|
87
|
-
}
|
|
88
|
-
taskRawSpec.models = models;
|
|
89
|
-
taskSpec = LmTaskBuilder.fromRawSpec(taskRawSpec);
|
|
57
|
+
const taskFileSpec = YAML.parse(res.ymlTask);
|
|
58
|
+
let conf = {};
|
|
59
|
+
let vars = {};
|
|
60
|
+
if (!isWorkflow) {
|
|
61
|
+
const tv = parseTaskVars(args, taskFileSpec?.inferParams ? taskFileSpec.inferParams : {});
|
|
62
|
+
vars = tv.vars;
|
|
63
|
+
conf = configureTask(tv.conf, taskFileSpec);
|
|
90
64
|
}
|
|
91
65
|
else {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
defaultCtx = taskSpec.model.ctx;
|
|
66
|
+
const tv = parseTaskVars({ name: name, prompt: pr, ...args }, taskFileSpec?.inferParams ? taskFileSpec.inferParams : {});
|
|
67
|
+
vars = tv.vars;
|
|
68
|
+
conf = configureTask(tv.conf, taskFileSpec);
|
|
97
69
|
}
|
|
70
|
+
const taskSpec = taskFileSpec;
|
|
98
71
|
if (taskSpec.toolsList) {
|
|
99
72
|
taskSpec.tools = [];
|
|
100
73
|
for (const toolName of taskSpec.toolsList) {
|
|
@@ -127,19 +100,10 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
127
100
|
}
|
|
128
101
|
;
|
|
129
102
|
const task = taskBuilder.init(taskSpec);
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const tv = initTaskVars(args, taskSpec?.inferParams ? taskSpec.inferParams : {});
|
|
134
|
-
conf = tv.conf;
|
|
135
|
-
vars = tv.vars;
|
|
103
|
+
if (conf?.inferParams?.tsGrammar) {
|
|
104
|
+
conf.inferParams.grammar = serializeGrammar(await compile(conf.inferParams.tsGrammar, "Grammar"));
|
|
105
|
+
delete conf.inferParams.tsGrammar;
|
|
136
106
|
}
|
|
137
|
-
else {
|
|
138
|
-
const tv = initTaskParams({ name: name, prompt: pr, ...args }, taskSpec?.inferParams ? taskSpec.inferParams : {});
|
|
139
|
-
conf = tv.conf;
|
|
140
|
-
vars = tv.vars;
|
|
141
|
-
}
|
|
142
|
-
conf = initTaskConf(conf, taskSpec, defaultCtx);
|
|
143
107
|
if (isDebug.value) {
|
|
144
108
|
console.log("Task conf:", conf);
|
|
145
109
|
console.log("Task vars:", vars);
|
|
@@ -151,15 +115,20 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
151
115
|
ex.checkStatus();
|
|
152
116
|
let i = 0;
|
|
153
117
|
let c = false;
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
118
|
+
if (options?.onToken) {
|
|
119
|
+
ex.backend.setOnToken(options.onToken);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
ex.backend.setOnToken((t) => {
|
|
123
|
+
let txt = t;
|
|
124
|
+
if (isShowTokens.value) {
|
|
125
|
+
txt = c ? t : `\x1b[100m${t}\x1b[0m`;
|
|
126
|
+
}
|
|
127
|
+
process.stdout.write(txt);
|
|
128
|
+
++i;
|
|
129
|
+
c = !c;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
163
132
|
conf.expert = ex;
|
|
164
133
|
if (isDebug.value || isVerbose.value) {
|
|
165
134
|
conf.debug = true;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { LmTaskConfig, FinalLmTaskConfig, LmTaskFileSpec } from "../../../interfaces.js";
|
|
2
|
+
declare function configureTask(itConf: LmTaskConfig, taskSpec: LmTaskFileSpec): FinalLmTaskConfig;
|
|
3
|
+
declare function parseTaskVars(params: Array<any> | Record<string, any>, inferParams: Record<string, any>): {
|
|
4
|
+
conf: LmTaskConfig;
|
|
5
|
+
vars: Record<string, any>;
|
|
6
|
+
};
|
|
7
|
+
export { parseTaskVars, configureTask, };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { readModel } from "../../../db/read.js";
|
|
2
|
+
function configureTask(itConf, taskSpec) {
|
|
3
|
+
const _conf = {};
|
|
4
|
+
let modelName = "";
|
|
5
|
+
let templateName = "";
|
|
6
|
+
let ip = itConf.inferParams;
|
|
7
|
+
let isModelFromTaskFile = false;
|
|
8
|
+
let model = {};
|
|
9
|
+
let found = false;
|
|
10
|
+
if (itConf?.templateName) {
|
|
11
|
+
templateName = itConf.templateName;
|
|
12
|
+
}
|
|
13
|
+
if (!itConf?.modelname) {
|
|
14
|
+
if (taskSpec?.model?.name) {
|
|
15
|
+
model = taskSpec.model;
|
|
16
|
+
isModelFromTaskFile = true;
|
|
17
|
+
found = true;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
if (!taskSpec?.modelpack) {
|
|
21
|
+
throw new Error("provide a default model or a use a modelpack in the task definition");
|
|
22
|
+
}
|
|
23
|
+
modelName = taskSpec.modelpack.default;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
modelName = itConf.modelname;
|
|
28
|
+
}
|
|
29
|
+
if (!found) {
|
|
30
|
+
if (modelName.length == 0) {
|
|
31
|
+
throw new Error("no model name defined");
|
|
32
|
+
}
|
|
33
|
+
if (taskSpec?.models) {
|
|
34
|
+
for (const [k, v] of Object.entries(taskSpec.models)) {
|
|
35
|
+
if (modelName == k) {
|
|
36
|
+
model = v;
|
|
37
|
+
if (v?.inferParams) {
|
|
38
|
+
const tip = v.inferParams;
|
|
39
|
+
for (const [k, v] of Object.entries(tip)) {
|
|
40
|
+
ip[k] = v;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (v?.system) {
|
|
44
|
+
model.system = v.system;
|
|
45
|
+
}
|
|
46
|
+
if (v.assistant) {
|
|
47
|
+
model.assistant = v.assistant;
|
|
48
|
+
}
|
|
49
|
+
isModelFromTaskFile = true;
|
|
50
|
+
found = true;
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!found) {
|
|
56
|
+
const m = readModel(modelName);
|
|
57
|
+
if (m.found) {
|
|
58
|
+
model = m.modelData;
|
|
59
|
+
found = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (found) {
|
|
64
|
+
model.inferParams = ip;
|
|
65
|
+
if (!model?.ctx || !isModelFromTaskFile) {
|
|
66
|
+
model.ctx = taskSpec.ctx;
|
|
67
|
+
}
|
|
68
|
+
_conf.model = model;
|
|
69
|
+
_conf.model.inferParams = ip;
|
|
70
|
+
if (templateName.length > 0) {
|
|
71
|
+
_conf.model.template = templateName;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
_conf.modelname = modelName;
|
|
76
|
+
}
|
|
77
|
+
return _conf;
|
|
78
|
+
}
|
|
79
|
+
function parseTaskVars(params, inferParams) {
|
|
80
|
+
switch (Array.isArray(params)) {
|
|
81
|
+
case true:
|
|
82
|
+
return _initTaskVars(params, inferParams);
|
|
83
|
+
default:
|
|
84
|
+
return _initTaskParams(params, inferParams);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function _initTaskParams(params, inferParams) {
|
|
88
|
+
const conf = { inferParams: inferParams, modelname: "", templateName: "" };
|
|
89
|
+
if (!params?.prompt) {
|
|
90
|
+
throw new Error(`Error initializing task params: provide a prompt`);
|
|
91
|
+
}
|
|
92
|
+
if (params?.images) {
|
|
93
|
+
conf.inferParams.images = params.images;
|
|
94
|
+
delete params.images;
|
|
95
|
+
}
|
|
96
|
+
if (params?.model) {
|
|
97
|
+
conf.modelname = params.model;
|
|
98
|
+
delete params.model;
|
|
99
|
+
}
|
|
100
|
+
if (params?.template) {
|
|
101
|
+
conf.templateName = params.template;
|
|
102
|
+
delete params.template;
|
|
103
|
+
}
|
|
104
|
+
const res = { conf: conf, vars: params };
|
|
105
|
+
return res;
|
|
106
|
+
}
|
|
107
|
+
function _initTaskVars(args, inferParams) {
|
|
108
|
+
const conf = { inferParams: inferParams, modelname: "", templateName: "" };
|
|
109
|
+
const vars = {};
|
|
110
|
+
args.forEach((a) => {
|
|
111
|
+
if (a.includes("=")) {
|
|
112
|
+
const delimiter = "=";
|
|
113
|
+
const [k, v] = a.split(delimiter, 2);
|
|
114
|
+
if (v === undefined) {
|
|
115
|
+
throw new Error(`invalid parameter ${a}`);
|
|
116
|
+
}
|
|
117
|
+
switch (k) {
|
|
118
|
+
case "m":
|
|
119
|
+
if (v.includes("/")) {
|
|
120
|
+
const _s = v.split("/");
|
|
121
|
+
conf.modelname = _s[0];
|
|
122
|
+
conf.templateName = _s[1];
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
conf.modelname = v;
|
|
126
|
+
}
|
|
127
|
+
break;
|
|
128
|
+
case "ip":
|
|
129
|
+
v.split(",").forEach((p) => {
|
|
130
|
+
const s = p.split(":");
|
|
131
|
+
const cip = conf.inferParams;
|
|
132
|
+
cip[s[0]] = parseFloat(s[1]);
|
|
133
|
+
conf.inferParams = cip;
|
|
134
|
+
});
|
|
135
|
+
break;
|
|
136
|
+
default:
|
|
137
|
+
vars[k] = v;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
return { conf, vars };
|
|
143
|
+
}
|
|
144
|
+
export { parseTaskVars, configureTask, };
|
package/dist/cmd/lib/utils.d.ts
CHANGED
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
import { LmTask } from "@agent-smith/lmtask";
|
|
2
1
|
import { InferenceStats } from "@locallm/types/dist/interfaces.js";
|
|
3
2
|
declare function setOptions(args: Array<string> | undefined, options: Record<string, any>): Promise<Array<string>>;
|
|
4
3
|
declare function readPromptFile(): string;
|
|
5
4
|
declare function processOutput(res: any): Promise<void>;
|
|
6
|
-
declare function initTaskConf(conf: Record<string, any>, taskSpec: LmTask, defaultCtx: number): Record<string, any>;
|
|
7
|
-
declare function initTaskParams(params: Record<string, any>, inferParams: Record<string, any>): {
|
|
8
|
-
conf: Record<string, any>;
|
|
9
|
-
vars: Record<string, any>;
|
|
10
|
-
};
|
|
11
|
-
declare function initActionVars(args: Array<any>): Record<string, any>;
|
|
12
|
-
declare function initTaskVars(args: Array<any>, inferParams: Record<string, any>): {
|
|
13
|
-
conf: Record<string, any>;
|
|
14
|
-
vars: Record<string, any>;
|
|
15
|
-
};
|
|
16
5
|
declare function parseInputOptions(options: any): Promise<string | null>;
|
|
17
6
|
declare function formatStats(stats: InferenceStats): string;
|
|
18
|
-
export {
|
|
7
|
+
export { parseInputOptions, processOutput, readPromptFile, setOptions, formatStats, };
|
package/dist/cmd/lib/utils.js
CHANGED
|
@@ -34,9 +34,12 @@ async function processOutput(res) {
|
|
|
34
34
|
}
|
|
35
35
|
let data = "";
|
|
36
36
|
if (typeof res == "object") {
|
|
37
|
-
if (res?.answer
|
|
37
|
+
if (res?.answer?.text) {
|
|
38
38
|
data = res.answer.text;
|
|
39
39
|
}
|
|
40
|
+
else {
|
|
41
|
+
data = JSON.stringify(res);
|
|
42
|
+
}
|
|
40
43
|
}
|
|
41
44
|
else {
|
|
42
45
|
data = res;
|
|
@@ -48,171 +51,6 @@ async function processOutput(res) {
|
|
|
48
51
|
await writeToClipboard(data);
|
|
49
52
|
}
|
|
50
53
|
}
|
|
51
|
-
function initTaskConf(conf, taskSpec, defaultCtx) {
|
|
52
|
-
const _conf = conf;
|
|
53
|
-
let m = taskSpec.model.name;
|
|
54
|
-
let t = taskSpec.model.template;
|
|
55
|
-
let c = taskSpec.model?.ctx ?? defaultCtx;
|
|
56
|
-
let ip = conf.inferParams;
|
|
57
|
-
let system = taskSpec.model?.system;
|
|
58
|
-
let assistant = taskSpec.model?.assistant;
|
|
59
|
-
if (conf?.model) {
|
|
60
|
-
m = conf.model;
|
|
61
|
-
if (conf?.template) {
|
|
62
|
-
t = conf.template;
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
const gt = tfm.guess(m);
|
|
66
|
-
if (gt == "none") {
|
|
67
|
-
throw new Error(`Unable to guess the template for ${m}: please provide a template name"`);
|
|
68
|
-
}
|
|
69
|
-
t = gt;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
if (conf?.size) {
|
|
74
|
-
if (!taskSpec?.models) {
|
|
75
|
-
throw new Error(`Model ${conf.size} not found in task`);
|
|
76
|
-
}
|
|
77
|
-
if (!Object.keys(taskSpec.models).includes(conf.size)) {
|
|
78
|
-
throw new Error(`Model ${conf.size} not found in task`);
|
|
79
|
-
}
|
|
80
|
-
m = taskSpec.models[conf.size].name;
|
|
81
|
-
t = taskSpec.models[conf.size].template;
|
|
82
|
-
c = taskSpec.models[conf.size]?.ctx ?? defaultCtx;
|
|
83
|
-
if (taskSpec.models[conf.size]?.inferParams) {
|
|
84
|
-
const tip = taskSpec.models[conf.size].inferParams;
|
|
85
|
-
for (const [k, v] of Object.entries(tip)) {
|
|
86
|
-
ip[k] = v;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
system = taskSpec.models[conf.size]?.system;
|
|
90
|
-
assistant = taskSpec.models[conf.size]?.assistant;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
_conf.model = {
|
|
94
|
-
name: m,
|
|
95
|
-
template: t,
|
|
96
|
-
ctx: c,
|
|
97
|
-
};
|
|
98
|
-
if (system) {
|
|
99
|
-
_conf.model.system = system;
|
|
100
|
-
}
|
|
101
|
-
if (assistant) {
|
|
102
|
-
_conf.model.assistant = assistant;
|
|
103
|
-
}
|
|
104
|
-
_conf.inferParams = ip;
|
|
105
|
-
if (_conf?.template) {
|
|
106
|
-
delete conf.template;
|
|
107
|
-
}
|
|
108
|
-
return _conf;
|
|
109
|
-
}
|
|
110
|
-
function initTaskParams(params, inferParams) {
|
|
111
|
-
const conf = { inferParams: inferParams };
|
|
112
|
-
if (!params?.prompt) {
|
|
113
|
-
throw new Error(`Error initializing task params: provide a prompt`);
|
|
114
|
-
}
|
|
115
|
-
if (params?.images) {
|
|
116
|
-
conf.inferParams.images = params.images;
|
|
117
|
-
delete params.images;
|
|
118
|
-
}
|
|
119
|
-
if (params?.size) {
|
|
120
|
-
conf.size = params.size;
|
|
121
|
-
delete params.size;
|
|
122
|
-
}
|
|
123
|
-
if (params?.model) {
|
|
124
|
-
conf.model = params.model;
|
|
125
|
-
delete params.model;
|
|
126
|
-
}
|
|
127
|
-
if (params?.template) {
|
|
128
|
-
conf.template = params.template;
|
|
129
|
-
delete params.template;
|
|
130
|
-
}
|
|
131
|
-
const res = { conf: conf, vars: params };
|
|
132
|
-
return res;
|
|
133
|
-
}
|
|
134
|
-
function initActionVars(args) {
|
|
135
|
-
const vars = {};
|
|
136
|
-
args.forEach((a) => {
|
|
137
|
-
if (a.includes("=")) {
|
|
138
|
-
const t = a.split("=");
|
|
139
|
-
const k = t[0];
|
|
140
|
-
const v = t[1];
|
|
141
|
-
switch (k) {
|
|
142
|
-
case "m":
|
|
143
|
-
if (v.includes("/")) {
|
|
144
|
-
const _s = v.split("/");
|
|
145
|
-
vars.model = _s[0];
|
|
146
|
-
vars.template = _s[1];
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
vars.model = v;
|
|
150
|
-
}
|
|
151
|
-
break;
|
|
152
|
-
case "ip":
|
|
153
|
-
v.split(",").forEach((p) => {
|
|
154
|
-
const s = p.split(":");
|
|
155
|
-
vars["inferParams"][s[0]] = parseFloat(s[1]);
|
|
156
|
-
});
|
|
157
|
-
break;
|
|
158
|
-
case "s":
|
|
159
|
-
vars.size = v;
|
|
160
|
-
break;
|
|
161
|
-
default:
|
|
162
|
-
vars[k] = v;
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
return { vars };
|
|
168
|
-
}
|
|
169
|
-
function initTaskVars(args, inferParams) {
|
|
170
|
-
const conf = { inferParams: inferParams };
|
|
171
|
-
const vars = {};
|
|
172
|
-
args.forEach((a) => {
|
|
173
|
-
if (a.includes("=")) {
|
|
174
|
-
const delimiter = "=";
|
|
175
|
-
const firstDelimiterIndex = a.indexOf(delimiter);
|
|
176
|
-
let t = new Array();
|
|
177
|
-
if (firstDelimiterIndex !== -1) {
|
|
178
|
-
t = [
|
|
179
|
-
a.slice(0, firstDelimiterIndex),
|
|
180
|
-
a.slice(firstDelimiterIndex + 1)
|
|
181
|
-
];
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
t = [a];
|
|
185
|
-
}
|
|
186
|
-
const k = t[0];
|
|
187
|
-
const v = t[1];
|
|
188
|
-
switch (k) {
|
|
189
|
-
case "m":
|
|
190
|
-
if (v.includes("/")) {
|
|
191
|
-
const _s = v.split("/");
|
|
192
|
-
conf.model = _s[0];
|
|
193
|
-
conf.template = _s[1];
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
conf.model = v;
|
|
197
|
-
}
|
|
198
|
-
break;
|
|
199
|
-
case "ip":
|
|
200
|
-
v.split(",").forEach((p) => {
|
|
201
|
-
const s = p.split(":");
|
|
202
|
-
conf["inferParams"][s[0]] = parseFloat(s[1]);
|
|
203
|
-
});
|
|
204
|
-
break;
|
|
205
|
-
case "s":
|
|
206
|
-
conf.size = v;
|
|
207
|
-
break;
|
|
208
|
-
default:
|
|
209
|
-
vars[k] = v;
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
return { conf, vars };
|
|
215
|
-
}
|
|
216
54
|
async function parseInputOptions(options) {
|
|
217
55
|
let out = null;
|
|
218
56
|
if (options?.Ic == true || inputMode.value == "clipboard") {
|
|
@@ -231,4 +69,4 @@ function formatStats(stats) {
|
|
|
231
69
|
buf.push(`${stats.inferenceTimeSeconds}s inference)`);
|
|
232
70
|
return buf.join(" ");
|
|
233
71
|
}
|
|
234
|
-
export {
|
|
72
|
+
export { parseInputOptions, processOutput, readPromptFile, setOptions, formatStats, };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
function createDirectoryIfNotExists(dirPath, recursive = false) {
|
|
4
|
+
const resolvedDirPath = path.resolve(dirPath);
|
|
5
|
+
if (!fs.existsSync(resolvedDirPath)) {
|
|
6
|
+
fs.mkdirSync(resolvedDirPath, { recursive: recursive });
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export { createDirectoryIfNotExists };
|
|
@@ -19,6 +19,9 @@ async function readCmds(dir) {
|
|
|
19
19
|
const fileNames = _readCmdsDir(dir);
|
|
20
20
|
for (const name of fileNames) {
|
|
21
21
|
const { cmd } = await import(path.join(dir, name + ".js"));
|
|
22
|
+
if (!cmd) {
|
|
23
|
+
throw new Error(`command ${name} not found in ${dir}`);
|
|
24
|
+
}
|
|
22
25
|
cmds[name] = cmd;
|
|
23
26
|
}
|
|
24
27
|
return cmds;
|
|
@@ -22,7 +22,7 @@ function readFeaturesDir(dir) {
|
|
|
22
22
|
cmd: [],
|
|
23
23
|
workflow: [],
|
|
24
24
|
adaptater: [],
|
|
25
|
-
|
|
25
|
+
modelfile: []
|
|
26
26
|
};
|
|
27
27
|
let dirpath = path.join(dir, "tasks");
|
|
28
28
|
if (fs.existsSync(dirpath)) {
|
|
@@ -101,7 +101,7 @@ function readFeaturesDir(dir) {
|
|
|
101
101
|
const parts = filename.split(".");
|
|
102
102
|
const ext = parts.pop();
|
|
103
103
|
const name = parts.join("");
|
|
104
|
-
feats.
|
|
104
|
+
feats.modelfile.push({
|
|
105
105
|
name: name,
|
|
106
106
|
path: path.join(dirpath),
|
|
107
107
|
ext: ext,
|