tune-sdk 0.1.13 → 0.2.2
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 +1 -1
- package/bin/cli.js +3 -132
- package/{tools → config}/default.ctx.js +9 -10
- package/config/package.json +6 -0
- package/config/package.prod.json +7 -0
- package/dist/cli.js +401 -0
- package/dist/rpc.js +94 -47
- package/dist/tune.js +103 -36
- package/package.json +8 -2
- package/tools/json_format.proc.mjs +3 -0
- package/tools/log.proc.js +9 -6
- package/tools/message.schema.json +3 -3
- package/tools/nu.schema.json +13 -0
- package/tools/nu.tool.mjs +14 -0
- package/tools/yandex_tts.schema.json +41 -0
- package/tools/yandex_tts.tool.mjs +31 -0
- package/tools/antrophic.ctx.js +0 -34
- package/tools/gemini.ctx.js +0 -49
- package/tools/groq.ctx.js +0 -33
- package/tools/mistral.ctx.js +0 -64
- package/tools/openai.ctx.js +0 -33
- package/tools/openrouter.ctx.js +0 -38
package/README.md
CHANGED
|
@@ -54,7 +54,7 @@ npx tune-sdk --user "hi how are you?" --filename newchat.chat --save
|
|
|
54
54
|
npx tune-sdk --system "You are Groot" --user "Hi how are you?"
|
|
55
55
|
|
|
56
56
|
#set context variable
|
|
57
|
-
npx tune-sdk --set "
|
|
57
|
+
npx tune-sdk --set-test "hello" --user "@test" --system "You are echo you print everythting back"
|
|
58
58
|
```
|
|
59
59
|
|
|
60
60
|
|
package/bin/cli.js
CHANGED
|
@@ -1,133 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var curKey;
|
|
6
|
-
curKey = null;
|
|
7
|
-
var res;
|
|
8
|
-
res = args.reduce((function(memo, arg) {
|
|
9
|
-
var key, value, _ref, _i;
|
|
10
|
-
if (arg.startsWith("--")) {
|
|
11
|
-
_ref = arg.substring(2)
|
|
12
|
-
.split("=");
|
|
13
|
-
key = _ref[0];
|
|
14
|
-
value = _ref[1];
|
|
15
|
-
if (!!value) {
|
|
16
|
-
memo[key] = value;
|
|
17
|
-
curKey = null;
|
|
18
|
-
} else {
|
|
19
|
-
curKey = key;
|
|
20
|
-
memo[key] = true;
|
|
21
|
-
}
|
|
22
|
-
} else if (curKey) {
|
|
23
|
-
memo[curKey] = arg;
|
|
24
|
-
curKey = null;
|
|
25
|
-
}
|
|
26
|
-
return memo;
|
|
27
|
-
}), {});
|
|
28
|
-
var res1;
|
|
29
|
-
res1 = {};
|
|
30
|
-
_ref = res;
|
|
31
|
-
for (key in _ref) {
|
|
32
|
-
value = _ref[key];
|
|
33
|
-
if (key.startsWith("set-")) {
|
|
34
|
-
res1.params = res1.params || {}
|
|
35
|
-
res1.params[key.substr(4)] = value;
|
|
36
|
-
} else {
|
|
37
|
-
res1[key] = value;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return res1;
|
|
41
|
-
}
|
|
42
|
-
parseArgs;
|
|
43
|
-
tune = require("../dist/tune.js");
|
|
44
|
-
rpc = require("../dist/rpc.js");
|
|
45
|
-
async function rpc(args, ctx) {
|
|
46
|
-
var server;
|
|
47
|
-
var server;
|
|
48
|
-
return (server = jsonrpc());
|
|
49
|
-
}
|
|
50
|
-
rpc;
|
|
51
|
-
async function run(args, ctx) {
|
|
52
|
-
var stopVal, params;
|
|
53
|
-
var stopVal;
|
|
54
|
-
stopVal = args.stop || "assistant";
|
|
55
|
-
if ((stopVal !== "step" && stopVal !== "assistant")) stopVal = (function(msgs) {
|
|
56
|
-
var lastMsg;
|
|
57
|
-
if (!msgs.length) return false;
|
|
58
|
-
var lastMsg;
|
|
59
|
-
lastMsg = msgs["slice"](-1)[0];
|
|
60
|
-
if (!lastMsg.content) return false;
|
|
61
|
-
return (-1 !== lastMsg.content.inexOf(args.stop || "assistant"));
|
|
62
|
-
});
|
|
63
|
-
var params;
|
|
64
|
-
params = args.params;
|
|
65
|
-
delete args.params;
|
|
66
|
-
return ctx.file2run(args, params);
|
|
67
|
-
}
|
|
68
|
-
run;
|
|
69
|
-
async function main() {
|
|
70
|
-
var dirs, args, ctx, dir, ctxName, ext, module, m, _i, _res, _ref, _len, _ref0, _i0, _res0, _ref1, _len0, _ref2;
|
|
71
|
-
var dirs;
|
|
72
|
-
var args;
|
|
73
|
-
dirs = [];
|
|
74
|
-
args = parseArgs(process.argv.slice(2));
|
|
75
|
-
if (args.path) {
|
|
76
|
-
dirs = args.path.split(path.delimiter);
|
|
77
|
-
} else {
|
|
78
|
-
if (process.env.TUNE_PATH) dirs = process.env.TUNE_PATH.split(path.delimiter);
|
|
79
|
-
dirs.push(process.cwd());
|
|
80
|
-
}
|
|
81
|
-
ctx = tune.makeContext(process.env);
|
|
82
|
-
_res = [];
|
|
83
|
-
_ref = dirs;
|
|
84
|
-
for (_i = 0, _len = _ref.length; _i < _len; ++_i) {
|
|
85
|
-
dir = _ref[_i];
|
|
86
|
-
var ctxName;
|
|
87
|
-
ctxName = ["default.ctx.js", "default.ctx.cjs", "default.ctx.mjs"]
|
|
88
|
-
.map((function(name) {
|
|
89
|
-
return path.join(dir, name);
|
|
90
|
-
}))
|
|
91
|
-
.find((function(name) {
|
|
92
|
-
return fs.existsSync(name);
|
|
93
|
-
}));
|
|
94
|
-
if (!ctxName) continue;
|
|
95
|
-
var ext;
|
|
96
|
-
var module;
|
|
97
|
-
ext = path.extname(filename);
|
|
98
|
-
module = null;
|
|
99
|
-
if ((ext === ".js" || ext === ".cjs")) {
|
|
100
|
-
module = require(ctxName);
|
|
101
|
-
} else {
|
|
102
|
-
module = await import(ctxName);
|
|
103
|
-
module = module.default;
|
|
104
|
-
}
|
|
105
|
-
if ((typeof module === "function")) {
|
|
106
|
-
_ref0 = ctx.use(module);
|
|
107
|
-
} else if (Array.isArray(module)) {
|
|
108
|
-
_res0 = [];
|
|
109
|
-
_ref1 = module;
|
|
110
|
-
for (_i0 = 0, _len0 = _ref1.length; _i0 < _len0; ++_i0) {
|
|
111
|
-
m = _ref1[_i0];
|
|
112
|
-
if ((typeof m === "function")) {
|
|
113
|
-
_ref2 = ctx.use(module);
|
|
114
|
-
} else {
|
|
115
|
-
_ref2 = undefined;
|
|
116
|
-
throw Error(tpl("err: Context file export is not an array of functions or function {}", ctxName));
|
|
117
|
-
}
|
|
118
|
-
if (typeof _ref2 !== 'undefined') _res0.push(_ref2);
|
|
119
|
-
}
|
|
120
|
-
_ref0 = _res0;
|
|
121
|
-
} else {
|
|
122
|
-
_ref0 = undefined;
|
|
123
|
-
throw Error(tpl("err: Context file export is not an array of functions or function {}", ctxName));
|
|
124
|
-
}
|
|
125
|
-
if (typeof _ref0 !== 'undefined') _res.push(_ref0);
|
|
126
|
-
}
|
|
127
|
-
return _res;
|
|
128
|
-
}
|
|
129
|
-
main;
|
|
130
|
-
exports.parseArgs = parseArgs;
|
|
131
|
-
exports.rpc = rpc;
|
|
132
|
-
exports.main = main;
|
|
133
|
-
exports.run = run;
|
|
3
|
+
const cli = require("../dist/cli.js")
|
|
4
|
+
cli.main()
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const fs = require('fs')
|
|
2
2
|
const path = require('path')
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
const {
|
|
4
|
+
const models = require("tune-models")
|
|
5
|
+
const { curFile, fsMix, defaultWrite } = require("tune-sdk/fsctx");
|
|
6
|
+
const { file2run, makeContext } = require("tune-sdk");
|
|
6
7
|
|
|
7
8
|
async function makeSchema(params, ctx) {
|
|
8
9
|
return file2run({ filename: path.join(__dirname, "schema.tool.chat")}, params, ctx);
|
|
@@ -11,16 +12,14 @@ let dirs = [];
|
|
|
11
12
|
if (process.env.TUNE_PATH) {
|
|
12
13
|
dirs = process.env.TUNE_PATH.split(path.delimiter);
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
+
|
|
15
16
|
|
|
16
17
|
module.exports = [
|
|
17
|
-
curFile,
|
|
18
|
+
// curFile,
|
|
18
19
|
fsMix(dirs, { makeSchema }),
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
require("./mistral.ctx.js"),
|
|
24
|
-
require("./groq.ctx.js"),
|
|
20
|
+
models({
|
|
21
|
+
default: "gpt-4.1-mini",
|
|
22
|
+
alias: { "sonnet": "claude-sonnet-4-20250514"}
|
|
23
|
+
}),
|
|
25
24
|
defaultWrite()
|
|
26
25
|
]
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
var assert, tune, rpc, path, fs, os, cp, stream;
|
|
2
|
+
assert = require("assert");
|
|
3
|
+
|
|
4
|
+
function showHelp() {
|
|
5
|
+
console.log("TUNE-CLI - Command Line Interface for Tune SDK");
|
|
6
|
+
console.log("");
|
|
7
|
+
console.log("USAGE:");
|
|
8
|
+
console.log(" npx tune-sdk [OPTIONS]");
|
|
9
|
+
console.log("");
|
|
10
|
+
console.log("TLDR EXAMPLES:");
|
|
11
|
+
console.log(" # Quick chat with system prompt");
|
|
12
|
+
console.log(" npx tune-sdk --system \"You are Groot\" --user \"Hi how are you?\"");
|
|
13
|
+
console.log("");
|
|
14
|
+
console.log(" # Continue existing chat");
|
|
15
|
+
console.log(" npx tune-sdk --user \"continue the conversation\" --filename chat.chat --save");
|
|
16
|
+
console.log("");
|
|
17
|
+
console.log(" # Set context variables");
|
|
18
|
+
console.log(" npx tune-sdk --set-test=hello --user \"@test\" --system \"Echo assistant\"");
|
|
19
|
+
console.log("");
|
|
20
|
+
console.log(" # RPC mode for editor integration");
|
|
21
|
+
console.log(" npx tune-sdk --rpc");
|
|
22
|
+
console.log("");
|
|
23
|
+
console.log("OPTIONS:");
|
|
24
|
+
console.log(" --user <text> User message to send");
|
|
25
|
+
console.log(" --system <text> System prompt to use");
|
|
26
|
+
console.log(" --filename <file> Chat file to load/save");
|
|
27
|
+
console.log(" --save Save conversation to file");
|
|
28
|
+
console.log(" --stop <mode> Stop condition: assistant|step|<custom>");
|
|
29
|
+
console.log(" --text <content> chat content");
|
|
30
|
+
console.log(" --response <type> Response format: content|json|messages|chat (default: content)");
|
|
31
|
+
console.log(" --set-<name>=<value> Set context parameter");
|
|
32
|
+
console.log(" --rpc Start RPC server mode");
|
|
33
|
+
console.log(" --path <paths> Additional search paths (colon-separated)");
|
|
34
|
+
console.log(" --home <dir> Tune config directory (default: ~/.tune)");
|
|
35
|
+
console.log(" --debug Enable debug output");
|
|
36
|
+
console.log(" --silent Suppress output");
|
|
37
|
+
console.log(" --force-init Force config initialization");
|
|
38
|
+
console.log(" --help Show this help");
|
|
39
|
+
console.log("");
|
|
40
|
+
console.log("EXAMPLES:");
|
|
41
|
+
console.log(" # Start new chat");
|
|
42
|
+
console.log(" npx tune-sdk --system \"You are Groot\" --user \"Hi how are you?\"");
|
|
43
|
+
console.log("");
|
|
44
|
+
console.log(" # Append to existing chat and save");
|
|
45
|
+
console.log(" npx tune-sdk --user \"hi how are you?\" --filename newchat.chat --save");
|
|
46
|
+
console.log("");
|
|
47
|
+
console.log(" # Stop at specific word");
|
|
48
|
+
console.log(" npx tune-sdk --user \"continue\" --filename chat.chat --stop \"END\"");
|
|
49
|
+
console.log("");
|
|
50
|
+
console.log(" #Set context variable");
|
|
51
|
+
console.log(" npx tune-sdk --set-test=\"hello\" --user \"@test\" --system \"You are echo you print everythting back\"");
|
|
52
|
+
return console.log("");
|
|
53
|
+
}
|
|
54
|
+
showHelp;
|
|
55
|
+
|
|
56
|
+
function validateArgs(args) {
|
|
57
|
+
assert(!!args && (typeof args === "object"), "Arguments must be an object");
|
|
58
|
+
if (args.user) assert(typeof args.user === "string", "--user must be a string");
|
|
59
|
+
if (args.system) assert(typeof args.system === "string", "--system must be a string");
|
|
60
|
+
if (args.filename) assert(typeof args.filename === "string", "--filename must be a string");
|
|
61
|
+
if (args.text) assert(typeof args.text === "string", "--text must be a string");
|
|
62
|
+
if (args.response) assert(typeof args.response === "string", "--response must be a string");
|
|
63
|
+
if (args.stop) assert(typeof args.stop === "string", "--stop must be a string");
|
|
64
|
+
if (args.path) assert(typeof args.path === "string", "--path must be a string");
|
|
65
|
+
if (args.home) assert(typeof args.home === "string", "--home must be a string");
|
|
66
|
+
if (!!args.save) assert(typeof args.save === "boolean", "--save must be a boolean");
|
|
67
|
+
if (!!args.rpc) assert(typeof args.rpc === "boolean", "--rpc must be a boolean");
|
|
68
|
+
if (!!args.debug) assert(typeof args.debug === "boolean" || typeof args.debug === "string", "--debug must be a boolean");
|
|
69
|
+
if (!!args.silent) assert(typeof args.silent === "boolean", "--silent must be a boolean");
|
|
70
|
+
if (!!args.forceInit) assert(typeof args.forceInit === "boolean", "--force-init must be a boolean");
|
|
71
|
+
if (args.params) assert(!!args.params && (typeof args.params === "object"), "--set-* parameters must form a valid object");
|
|
72
|
+
if ((args.stop && (typeof args.stop === "string"))) assert((args.stop === "assistant") || (args.stop === "step") || (args.stop.length > 0), "--stop must be 'assistant', 'step', or a non-empty custom string");
|
|
73
|
+
if ((!args.rpc && !args.help && !args.user && !args.filename)) assert(false, "Must specify --user, --filename, --rpc, or --help");
|
|
74
|
+
return args;
|
|
75
|
+
}
|
|
76
|
+
validateArgs;
|
|
77
|
+
|
|
78
|
+
function parseArgs(args) {
|
|
79
|
+
var curKey, res, res1, key, value, stop, _ref, _len;
|
|
80
|
+
assert(Array.isArray(args), "parseArgs expects an array of arguments");
|
|
81
|
+
var curKey;
|
|
82
|
+
curKey = null;
|
|
83
|
+
var res;
|
|
84
|
+
res = args.reduce((function(memo, arg) {
|
|
85
|
+
var key, value, _ref, _i;
|
|
86
|
+
assert(typeof arg === "string", "Each argument must be a string");
|
|
87
|
+
if (arg.startsWith("--")) {
|
|
88
|
+
_ref = arg.substring(2)
|
|
89
|
+
.split("=");
|
|
90
|
+
key = _ref[0];
|
|
91
|
+
value = _ref[1];
|
|
92
|
+
assert((typeof key === "string") && (key.length > 0), "Argument key must be a non-empty string");
|
|
93
|
+
if (!!value) {
|
|
94
|
+
memo[key] = value;
|
|
95
|
+
curKey = null;
|
|
96
|
+
} else {
|
|
97
|
+
curKey = key;
|
|
98
|
+
memo[key] = true;
|
|
99
|
+
}
|
|
100
|
+
} else if (curKey) {
|
|
101
|
+
memo[curKey] = arg;
|
|
102
|
+
curKey = null;
|
|
103
|
+
}
|
|
104
|
+
return memo;
|
|
105
|
+
}), {});
|
|
106
|
+
assert(!!res && (typeof res === "object"), "Parsed arguments must form an object");
|
|
107
|
+
var res1;
|
|
108
|
+
res1 = {};
|
|
109
|
+
_ref = res;
|
|
110
|
+
for (key in _ref) {
|
|
111
|
+
value = _ref[key];
|
|
112
|
+
assert(typeof key === "string", "Argument keys must be strings");
|
|
113
|
+
if (key.startsWith("set-")) {
|
|
114
|
+
res1.params = res1.params || {}
|
|
115
|
+
assert(key.substr(4).length > 0, "Set parameter name cannot be empty");
|
|
116
|
+
res1.params[key.substr(4)] = value;
|
|
117
|
+
} else {
|
|
118
|
+
res1[key] = value;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if ((res1.h || res1.help)) res1.help = true;
|
|
122
|
+
stop = res1.stop;
|
|
123
|
+
if ((!!stop && (stop !== "step" && stop !== "assistant"))) {
|
|
124
|
+
assert(typeof stop === "string", "Custom stop condition must be a string");
|
|
125
|
+
assert(stop.length > 0, "Custom stop condition cannot be empty");
|
|
126
|
+
res1.stop = (function(msgs) {
|
|
127
|
+
var lastMsg;
|
|
128
|
+
assert(Array.isArray(msgs), "Messages must be an array");
|
|
129
|
+
if (!msgs.length) return false;
|
|
130
|
+
var lastMsg;
|
|
131
|
+
lastMsg = msgs["slice"](-1)[0];
|
|
132
|
+
assert(!!lastMsg && (typeof lastMsg === "object"), "Last message must be an object");
|
|
133
|
+
if (!lastMsg.content) return false;
|
|
134
|
+
assert(typeof lastMsg.content === "string", "Message content must be a string");
|
|
135
|
+
return (-1 !== lastMsg.content.indexOf(stop));
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return res1;
|
|
139
|
+
}
|
|
140
|
+
parseArgs;
|
|
141
|
+
tune = require("../dist/tune.js");
|
|
142
|
+
rpc = require("../dist/rpc.js");
|
|
143
|
+
path = require("path");
|
|
144
|
+
fs = require("fs");
|
|
145
|
+
os = require("os");
|
|
146
|
+
cp = require("child_process");
|
|
147
|
+
stream = require("stream");
|
|
148
|
+
|
|
149
|
+
function getHomedir(args) {
|
|
150
|
+
assert(!!args && (typeof args === "object"), "getHomedir expects args to be an object");
|
|
151
|
+
if (args.home) assert(typeof args.home === "string", "args.home must be a string");
|
|
152
|
+
return path.resolve(path.normalize((args.home || process.env.TUNE_HOME || "~/.tune")
|
|
153
|
+
.replace("~", os.homedir())));
|
|
154
|
+
}
|
|
155
|
+
getHomedir;
|
|
156
|
+
async function initConfig(args) {
|
|
157
|
+
var homedir, stdout, stderr, _ref, _i;
|
|
158
|
+
assert(!!args && (typeof args === "object"), "initConfig expects args to be an object");
|
|
159
|
+
var homedir;
|
|
160
|
+
homedir = getHomedir(args);
|
|
161
|
+
assert(typeof homedir === "string", "Home directory must be a string");
|
|
162
|
+
if ((!args.forceInit && fs.existsSync(homedir))) return;
|
|
163
|
+
console.error("[tune-sdk] initialize " + homedir);
|
|
164
|
+
fs.mkdirSync(homedir, {
|
|
165
|
+
recursive: true
|
|
166
|
+
});
|
|
167
|
+
console.error("[tune-sdk] copying files");
|
|
168
|
+
fs.copyFileSync(path.resolve(__dirname, "../config/default.ctx.js"), path.resolve(homedir, "default.ctx.js"));
|
|
169
|
+
fs.copyFileSync(path.resolve(__dirname, "../config/package.json"), path.resolve(homedir, "package.json"));
|
|
170
|
+
console.error("[tune-sdk] installing npm");
|
|
171
|
+
try {
|
|
172
|
+
_ref = cp.execSync("npm i", {
|
|
173
|
+
cwd: homedir,
|
|
174
|
+
encoding: "utf8"
|
|
175
|
+
});
|
|
176
|
+
stdout = _ref[0];
|
|
177
|
+
stderr = _ref[1];
|
|
178
|
+
if (stdout.trim()) console.error("[tune-sdk]", stdout.trim());
|
|
179
|
+
stderr.trim() ? console.error("[tune-sdk]", stderr.trim()) : undefined;
|
|
180
|
+
} catch (err) {}
|
|
181
|
+
return console.error("[tune-sdk] done");
|
|
182
|
+
}
|
|
183
|
+
initConfig;
|
|
184
|
+
async function suggest(params, ctx) {
|
|
185
|
+
var node, _ref;
|
|
186
|
+
var node;
|
|
187
|
+
node = await ctx.resolve(params.query, {
|
|
188
|
+
output: "all",
|
|
189
|
+
match: "regex"
|
|
190
|
+
});
|
|
191
|
+
if (!node) {
|
|
192
|
+
_ref = [];
|
|
193
|
+
} else if (!Array.isArray(node)) {
|
|
194
|
+
_ref = Array(node);
|
|
195
|
+
} else {
|
|
196
|
+
_ref = node;
|
|
197
|
+
}
|
|
198
|
+
node = _ref;
|
|
199
|
+
return node.map((function(item) {
|
|
200
|
+
return {
|
|
201
|
+
name: item.name,
|
|
202
|
+
dirname: item.dirname,
|
|
203
|
+
source: item.source || item.fullname,
|
|
204
|
+
fullname: item.fullname,
|
|
205
|
+
basename: (item.fullname ? path.basename(item.fullname) : undefined),
|
|
206
|
+
type: item.type
|
|
207
|
+
}
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
suggest;
|
|
211
|
+
async function remoteContext(name, params) {
|
|
212
|
+
var server, node;
|
|
213
|
+
var server;
|
|
214
|
+
server = this;
|
|
215
|
+
var node;
|
|
216
|
+
node = await server.resolve({
|
|
217
|
+
name: name,
|
|
218
|
+
params: params
|
|
219
|
+
});
|
|
220
|
+
if ((((typeof node !== "undefined") && (node !== null) && !Number.isNaN(node) && (typeof node.error !== "undefined") && (node.error !== null) && !Number.isNaN(node.error)) ? node.error : undefined)) return;
|
|
221
|
+
node.read = (function() {
|
|
222
|
+
return server.read({
|
|
223
|
+
name: name,
|
|
224
|
+
params: params
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
node.exec = (function() {
|
|
228
|
+
return server.exec({
|
|
229
|
+
name: name,
|
|
230
|
+
params: params
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
return node;
|
|
234
|
+
}
|
|
235
|
+
remoteContext;
|
|
236
|
+
async function runRpc(args) {
|
|
237
|
+
var inStream, outStream, debugStream, ctx, server;
|
|
238
|
+
var inStream;
|
|
239
|
+
var outStream;
|
|
240
|
+
var debugStream;
|
|
241
|
+
var ctx;
|
|
242
|
+
var server;
|
|
243
|
+
inStream = stream.Readable.toWeb(process.stdin);
|
|
244
|
+
outStream = stream.Writable.toWeb(process.stdout);
|
|
245
|
+
debugStream = ((typeof args.debug === "string") ? fs.createWriteStream(args.debug, {
|
|
246
|
+
flags: "a"
|
|
247
|
+
}) : undefined);
|
|
248
|
+
ctx = await initContext(args);
|
|
249
|
+
server = rpc.jsonrpc({
|
|
250
|
+
inStream: inStream,
|
|
251
|
+
outStream: outStream,
|
|
252
|
+
debug: ((typeof args.debug === "string") ? (function() {
|
|
253
|
+
var _i;
|
|
254
|
+
var args = 1 <= arguments.length ? [].slice.call(arguments, 0, _i = arguments.length - 0) : (_i = 0, []);
|
|
255
|
+
return debugStream.write(args.join(" ") + "\n", "utf8");
|
|
256
|
+
}) : true),
|
|
257
|
+
name: "server"
|
|
258
|
+
}, {
|
|
259
|
+
init: (async function(methods) {
|
|
260
|
+
if ((-1 !== methods.indexOf("resolve"))) ctx.use(remoteContext.bind(server));
|
|
261
|
+
return "done";
|
|
262
|
+
}),
|
|
263
|
+
file2run: (async function(params, stream) {
|
|
264
|
+
return tune.file2run({
|
|
265
|
+
filename: params.filename,
|
|
266
|
+
response: params.response,
|
|
267
|
+
stream: stream,
|
|
268
|
+
stop: params.stop,
|
|
269
|
+
text: params.text,
|
|
270
|
+
system: params.system,
|
|
271
|
+
user: params.user,
|
|
272
|
+
save: params.save
|
|
273
|
+
}, {}, ctx);
|
|
274
|
+
}),
|
|
275
|
+
suggest: (async function(params) {
|
|
276
|
+
return suggest(params, ctx);
|
|
277
|
+
})
|
|
278
|
+
});
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
runRpc;
|
|
282
|
+
async function run(args) {
|
|
283
|
+
var ctx, stop, params, res;
|
|
284
|
+
ctx = await initContext(args);
|
|
285
|
+
var stop;
|
|
286
|
+
stop = args.stop || "assistant";
|
|
287
|
+
var params;
|
|
288
|
+
params = args.params || {}
|
|
289
|
+
delete args.params;
|
|
290
|
+
var res;
|
|
291
|
+
res = await ctx.file2run(args, params);
|
|
292
|
+
return (!args.silient ? console.log(res) : undefined);
|
|
293
|
+
}
|
|
294
|
+
run;
|
|
295
|
+
async function initContext(args) {
|
|
296
|
+
var dirs, pwd, ctx, dir, ctxName, ext, module, m, _i, _ref, _len, _i0, _ref0, _len0;
|
|
297
|
+
var dirs;
|
|
298
|
+
var pwd;
|
|
299
|
+
dirs = [];
|
|
300
|
+
pwd = process.cwd();
|
|
301
|
+
if (args.path) dirs = args.path.split(path.delimiter)
|
|
302
|
+
.map((function(dir) {
|
|
303
|
+
return path.resolve(pwd, dir);
|
|
304
|
+
}));
|
|
305
|
+
dirs.push(getHomedir(args));
|
|
306
|
+
dirs.unshift(pwd);
|
|
307
|
+
process.env.TUNE_PATH = dirs.join(path.delimiter);
|
|
308
|
+
ctx = tune.makeContext({
|
|
309
|
+
TUNE_PATH: process.env.TUNE_PATH,
|
|
310
|
+
TUNE_HOME: getHomedir(args)
|
|
311
|
+
});
|
|
312
|
+
_ref = dirs;
|
|
313
|
+
for (_i = 0, _len = _ref.length; _i < _len; ++_i) {
|
|
314
|
+
dir = _ref[_i];
|
|
315
|
+
var ctxName;
|
|
316
|
+
ctxName = ["default.ctx.js", "default.ctx.cjs", "default.ctx.mjs"]
|
|
317
|
+
.map((function(name) {
|
|
318
|
+
return path.join(dir, name);
|
|
319
|
+
}))
|
|
320
|
+
.find((function(name) {
|
|
321
|
+
return fs.existsSync(name);
|
|
322
|
+
}));
|
|
323
|
+
if (!ctxName) continue;
|
|
324
|
+
var ext;
|
|
325
|
+
var module;
|
|
326
|
+
ext = path.extname(ctxName);
|
|
327
|
+
module = null;
|
|
328
|
+
if ((ext === ".js" || ext === ".cjs")) {
|
|
329
|
+
module = require(ctxName);
|
|
330
|
+
} else {
|
|
331
|
+
module = await import(ctxName);
|
|
332
|
+
module = module.default;
|
|
333
|
+
}
|
|
334
|
+
if ((typeof module === "function")) {
|
|
335
|
+
ctx.use(module);
|
|
336
|
+
} else if (Array.isArray(module)) {
|
|
337
|
+
_ref0 = module;
|
|
338
|
+
for (_i0 = 0, _len0 = _ref0.length; _i0 < _len0; ++_i0) {
|
|
339
|
+
m = _ref0[_i0];
|
|
340
|
+
if ((typeof m === "function")) {
|
|
341
|
+
ctx.use(m);
|
|
342
|
+
} else {
|
|
343
|
+
throw Error(tpl("err: Context file export is not an array of functions or function {name}: {module}", {
|
|
344
|
+
name: ctxName,
|
|
345
|
+
module: m
|
|
346
|
+
}));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
} else {
|
|
350
|
+
throw Error(tpl("err: Context file export is not an array of functions or function {name}: {module}", {
|
|
351
|
+
name: ctxName,
|
|
352
|
+
module: module
|
|
353
|
+
}));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return ctx;
|
|
357
|
+
}
|
|
358
|
+
initContext;
|
|
359
|
+
async function main() {
|
|
360
|
+
var args, _ref;
|
|
361
|
+
try {
|
|
362
|
+
var args;
|
|
363
|
+
args = parseArgs(process.argv.slice(2));
|
|
364
|
+
if (args.help) {
|
|
365
|
+
showHelp();
|
|
366
|
+
process.exit(0);
|
|
367
|
+
}
|
|
368
|
+
validateArgs(args);
|
|
369
|
+
await initConfig(args);
|
|
370
|
+
_ref = args.rpc ? await runRpc(args) : await run(args);
|
|
371
|
+
} catch (e) {
|
|
372
|
+
console.error(e);
|
|
373
|
+
_ref = process.exit(1);
|
|
374
|
+
}
|
|
375
|
+
return _ref;
|
|
376
|
+
}
|
|
377
|
+
main;
|
|
378
|
+
|
|
379
|
+
function tpl(str) {
|
|
380
|
+
var _i;
|
|
381
|
+
var params = 2 <= arguments.length ? [].slice.call(arguments, 1, _i = arguments.length - 0) : (_i = 1, []);
|
|
382
|
+
return (function(paramIndex, params) {
|
|
383
|
+
var _ref;
|
|
384
|
+
try {
|
|
385
|
+
_ref = str.replace(/{(\W*)(\w*)(\W*)}/gm, (function(_, pre, name, post) {
|
|
386
|
+
return (function(res) {
|
|
387
|
+
paramIndex += 1;
|
|
388
|
+
return ((typeof res !== 'undefined') ? ((pre || "") + res + (post || "")) : "");
|
|
389
|
+
})(params[name || paramIndex]);
|
|
390
|
+
}));
|
|
391
|
+
} catch (e) {
|
|
392
|
+
_ref = console.log.apply(console, [].concat([e, str]).concat(params));
|
|
393
|
+
}
|
|
394
|
+
return _ref;
|
|
395
|
+
})(0, (((typeof params[0] === "object") && (params.length === 1)) ? params[0] : params));
|
|
396
|
+
}
|
|
397
|
+
tpl;
|
|
398
|
+
exports.parseArgs = parseArgs;
|
|
399
|
+
exports.rpc = rpc;
|
|
400
|
+
exports.main = main;
|
|
401
|
+
exports.run = run;
|