@agent-smith/cli 0.0.44 → 0.0.46
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 +10 -5
- 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 +41 -68
- package/dist/cmd/lib/tasks/conf.d.ts +7 -0
- package/dist/cmd/lib/tasks/conf.js +150 -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 -10
- 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: {
|
|
@@ -82,11 +82,15 @@ async function _updateConfCmd(args = [], options) {
|
|
|
82
82
|
console.warn("Provide a config.yml file path");
|
|
83
83
|
return;
|
|
84
84
|
}
|
|
85
|
-
const { paths, pf } = await processConfPath(args[0]);
|
|
85
|
+
const { paths, pf, dd } = await processConfPath(args[0]);
|
|
86
86
|
if (pf.length > 0) {
|
|
87
87
|
updatePromptfilePath(pf);
|
|
88
88
|
promptfilePath.value = pf;
|
|
89
89
|
}
|
|
90
|
+
if (dd.length > 0) {
|
|
91
|
+
updateDataDirPath(dd);
|
|
92
|
+
dataDirPath.value = dd;
|
|
93
|
+
}
|
|
90
94
|
const feats = readFeaturesDirs(paths);
|
|
91
95
|
updateFeatures(feats);
|
|
92
96
|
updateAliases(feats);
|
|
@@ -147,6 +151,7 @@ async function _readTaskCmd(args = [], options) {
|
|
|
147
151
|
console.log(YAML.stringify(ts));
|
|
148
152
|
}
|
|
149
153
|
async function _listTasksCmd(args = [], options) {
|
|
150
|
-
Object.keys(readFeatures().task).
|
|
154
|
+
const ts = Object.keys(readFeatures().task).sort();
|
|
155
|
+
console.table(ts);
|
|
151
156
|
}
|
|
152
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 { configureTaskModel, 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 model;
|
|
59
|
+
let vars = {};
|
|
60
|
+
if (!isWorkflow) {
|
|
61
|
+
const tv = parseTaskVars(args, taskFileSpec?.inferParams ? taskFileSpec.inferParams : {});
|
|
62
|
+
vars = tv.vars;
|
|
63
|
+
model = configureTaskModel(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
|
+
model = configureTaskModel(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,39 +100,40 @@ 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 (model?.inferParams?.tsGrammar) {
|
|
104
|
+
model.inferParams.grammar = serializeGrammar(await compile(model.inferParams.tsGrammar, "Grammar"));
|
|
105
|
+
delete model.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
|
-
console.log("Task
|
|
108
|
+
console.log("Task model:", model);
|
|
145
109
|
console.log("Task vars:", vars);
|
|
146
110
|
}
|
|
147
|
-
const ex = brain.getOrCreateExpertForModel(
|
|
111
|
+
const ex = brain.getOrCreateExpertForModel(model.name, model.template);
|
|
148
112
|
if (!ex) {
|
|
149
|
-
throw new Error("No expert found for model " +
|
|
113
|
+
throw new Error("No expert found for model " + model.name);
|
|
150
114
|
}
|
|
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
|
+
}
|
|
132
|
+
const conf = {
|
|
133
|
+
expert: ex,
|
|
134
|
+
model: model,
|
|
135
|
+
debug: isDebug.value,
|
|
136
|
+
};
|
|
163
137
|
conf.expert = ex;
|
|
164
138
|
if (isDebug.value || isVerbose.value) {
|
|
165
139
|
conf.debug = true;
|
|
@@ -172,7 +146,6 @@ async function executeTaskCmd(args = [], options = {}) {
|
|
|
172
146
|
catch (err) {
|
|
173
147
|
throw new Error(`Error executing task: ${name} ${err}`);
|
|
174
148
|
}
|
|
175
|
-
conf.prompt = pr;
|
|
176
149
|
if (isChatMode.value) {
|
|
177
150
|
if (brain.ex.name != ex.name) {
|
|
178
151
|
brain.setDefaultExpert(ex);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { LmTaskConfig, LmTaskFileSpec, ModelSpec } from "../../../interfaces.js";
|
|
2
|
+
declare function configureTaskModel(itConf: LmTaskConfig, taskSpec: LmTaskFileSpec): ModelSpec;
|
|
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, configureTaskModel, };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { useTemplateForModel } from "@agent-smith/tfm";
|
|
2
|
+
import { readModel } from "../../../db/read.js";
|
|
3
|
+
const tfm = useTemplateForModel();
|
|
4
|
+
function configureTaskModel(itConf, taskSpec) {
|
|
5
|
+
let modelName = "";
|
|
6
|
+
let templateName = "";
|
|
7
|
+
let ip = itConf.inferParams;
|
|
8
|
+
let isModelFromTaskFile = false;
|
|
9
|
+
let model = {};
|
|
10
|
+
let found = false;
|
|
11
|
+
if (itConf?.templateName) {
|
|
12
|
+
templateName = itConf.templateName;
|
|
13
|
+
}
|
|
14
|
+
if (!itConf?.modelname) {
|
|
15
|
+
if (taskSpec?.model?.name) {
|
|
16
|
+
model = taskSpec.model;
|
|
17
|
+
isModelFromTaskFile = true;
|
|
18
|
+
found = true;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
if (!taskSpec?.modelpack) {
|
|
22
|
+
throw new Error("provide a default model or a use a modelpack in the task definition");
|
|
23
|
+
}
|
|
24
|
+
modelName = taskSpec.modelpack.default;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
modelName = itConf.modelname;
|
|
29
|
+
}
|
|
30
|
+
if (!found) {
|
|
31
|
+
if (modelName.length == 0) {
|
|
32
|
+
throw new Error("no model name defined");
|
|
33
|
+
}
|
|
34
|
+
if (taskSpec?.models) {
|
|
35
|
+
for (const [k, v] of Object.entries(taskSpec.models)) {
|
|
36
|
+
if (modelName == k) {
|
|
37
|
+
model = v;
|
|
38
|
+
if (v?.inferParams) {
|
|
39
|
+
const tip = v.inferParams;
|
|
40
|
+
for (const [k, v] of Object.entries(tip)) {
|
|
41
|
+
ip[k] = v;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (v?.system) {
|
|
45
|
+
model.system = v.system;
|
|
46
|
+
}
|
|
47
|
+
if (v.assistant) {
|
|
48
|
+
model.assistant = v.assistant;
|
|
49
|
+
}
|
|
50
|
+
isModelFromTaskFile = true;
|
|
51
|
+
found = true;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (!found) {
|
|
57
|
+
const m = readModel(modelName);
|
|
58
|
+
if (m.found) {
|
|
59
|
+
model = m.modelData;
|
|
60
|
+
found = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (!found) {
|
|
65
|
+
const gt = tfm.guess(modelName);
|
|
66
|
+
if (gt == "none") {
|
|
67
|
+
throw new Error(`Unable to guess the template for ${modelName}: please provide a template name: m="modelname/templatename"`);
|
|
68
|
+
}
|
|
69
|
+
const m = {
|
|
70
|
+
name: modelName,
|
|
71
|
+
template: gt
|
|
72
|
+
};
|
|
73
|
+
model = m;
|
|
74
|
+
}
|
|
75
|
+
model.inferParams = ip;
|
|
76
|
+
if (!model?.ctx || !isModelFromTaskFile) {
|
|
77
|
+
model.ctx = taskSpec.ctx;
|
|
78
|
+
}
|
|
79
|
+
model.inferParams = ip;
|
|
80
|
+
if (templateName.length > 0) {
|
|
81
|
+
model.template = templateName;
|
|
82
|
+
}
|
|
83
|
+
return model;
|
|
84
|
+
}
|
|
85
|
+
function parseTaskVars(params, inferParams) {
|
|
86
|
+
switch (Array.isArray(params)) {
|
|
87
|
+
case true:
|
|
88
|
+
return _initTaskVars(params, inferParams);
|
|
89
|
+
default:
|
|
90
|
+
return _initTaskParams(params, inferParams);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function _initTaskParams(params, inferParams) {
|
|
94
|
+
const conf = { inferParams: inferParams, modelname: "", templateName: "" };
|
|
95
|
+
if (!params?.prompt) {
|
|
96
|
+
throw new Error(`Error initializing task params: provide a prompt`);
|
|
97
|
+
}
|
|
98
|
+
if (params?.images) {
|
|
99
|
+
conf.inferParams.images = params.images;
|
|
100
|
+
delete params.images;
|
|
101
|
+
}
|
|
102
|
+
if (params?.model) {
|
|
103
|
+
conf.modelname = params.model;
|
|
104
|
+
delete params.model;
|
|
105
|
+
}
|
|
106
|
+
if (params?.template) {
|
|
107
|
+
conf.templateName = params.template;
|
|
108
|
+
delete params.template;
|
|
109
|
+
}
|
|
110
|
+
const res = { conf: conf, vars: params };
|
|
111
|
+
return res;
|
|
112
|
+
}
|
|
113
|
+
function _initTaskVars(args, inferParams) {
|
|
114
|
+
const conf = { inferParams: inferParams, modelname: "", templateName: "" };
|
|
115
|
+
const vars = {};
|
|
116
|
+
args.forEach((a) => {
|
|
117
|
+
if (a.includes("=")) {
|
|
118
|
+
const delimiter = "=";
|
|
119
|
+
const [k, v] = a.split(delimiter, 2);
|
|
120
|
+
if (v === undefined) {
|
|
121
|
+
throw new Error(`invalid parameter ${a}`);
|
|
122
|
+
}
|
|
123
|
+
switch (k) {
|
|
124
|
+
case "m":
|
|
125
|
+
if (v.includes("/")) {
|
|
126
|
+
const _s = v.split("/");
|
|
127
|
+
conf.modelname = _s[0];
|
|
128
|
+
conf.templateName = _s[1];
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
conf.modelname = v;
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
case "ip":
|
|
135
|
+
v.split(",").forEach((p) => {
|
|
136
|
+
const s = p.split(":");
|
|
137
|
+
const cip = conf.inferParams;
|
|
138
|
+
cip[s[0]] = parseFloat(s[1]);
|
|
139
|
+
conf.inferParams = cip;
|
|
140
|
+
});
|
|
141
|
+
break;
|
|
142
|
+
default:
|
|
143
|
+
vars[k] = v;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
return { conf, vars };
|
|
149
|
+
}
|
|
150
|
+
export { parseTaskVars, configureTaskModel, };
|
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,
|