tune-sdk 0.1.13 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/bin/cli.js +3 -132
- package/config/default.ctx.js +22 -0
- package/config/package.json +6 -0
- package/config/package.prod.json +7 -0
- package/dist/cli.js +288 -0
- package/dist/rpc.js +77 -45
- package/dist/tune.js +96 -32
- package/package.json +8 -2
- package/tools/default.ctx.js +0 -1
- package/tools/yandex_tts.schema.json +41 -0
- package/tools/yandex_tts.tool.mjs +31 -0
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()
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
|
|
4
|
+
const models = require("tune-models")
|
|
5
|
+
const { curFile, fsMix, defaultWrite } = require("tune-sdk/fsctx");
|
|
6
|
+
const { file2run, makeContext } = require("tune-sdk");
|
|
7
|
+
|
|
8
|
+
async function makeSchema(params, ctx) {
|
|
9
|
+
return file2run({ filename: path.join(__dirname, "schema.tool.chat")}, params, ctx);
|
|
10
|
+
}
|
|
11
|
+
let dirs = [];
|
|
12
|
+
if (process.env.TUNE_PATH) {
|
|
13
|
+
dirs = process.env.TUNE_PATH.split(path.delimiter);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
module.exports = [
|
|
18
|
+
curFile,
|
|
19
|
+
fsMix(dirs, { makeSchema }),
|
|
20
|
+
models(),
|
|
21
|
+
defaultWrite()
|
|
22
|
+
]
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
var tune, rpc, path, fs, os, cp, stream;
|
|
2
|
+
|
|
3
|
+
function parseArgs(args) {
|
|
4
|
+
var curKey, res, res1, key, value, stop, _ref, _len;
|
|
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
|
+
stop = res1.stop;
|
|
41
|
+
if ((!!stop && (stop !== "step" && stop !== "assistant"))) res1.stop = (function(msgs) {
|
|
42
|
+
var lastMsg;
|
|
43
|
+
if (!msgs.length) return false;
|
|
44
|
+
var lastMsg;
|
|
45
|
+
lastMsg = msgs["slice"](-1)[0];
|
|
46
|
+
if (!lastMsg.content) return false;
|
|
47
|
+
return (-1 !== lastMsg.content.inexOf(stop));
|
|
48
|
+
});
|
|
49
|
+
return res1;
|
|
50
|
+
}
|
|
51
|
+
parseArgs;
|
|
52
|
+
tune = require("../dist/tune.js");
|
|
53
|
+
rpc = require("../dist/rpc.js");
|
|
54
|
+
path = require("path");
|
|
55
|
+
fs = require("fs");
|
|
56
|
+
os = require("os");
|
|
57
|
+
cp = require("child_process");
|
|
58
|
+
stream = require("stream");
|
|
59
|
+
|
|
60
|
+
function getHomedir(args) {
|
|
61
|
+
return path.resolve(path.normalize((args.home || process.env.TUNE_HOME || "~/.tune")
|
|
62
|
+
.replace("~", os.homedir())));
|
|
63
|
+
}
|
|
64
|
+
getHomedir;
|
|
65
|
+
async function initConfig(args) {
|
|
66
|
+
var homedir, stdout, stderr, _ref, _i;
|
|
67
|
+
var homedir;
|
|
68
|
+
homedir = getHomedir(args);
|
|
69
|
+
if ((!args.forceInit && fs.existsSync(homedir))) return;
|
|
70
|
+
console.error("[tune-sdk] initialize " + homedir);
|
|
71
|
+
fs.mkdirSync(homedir, {
|
|
72
|
+
recursive: true
|
|
73
|
+
});
|
|
74
|
+
console.error("[tune-sdk] copying files");
|
|
75
|
+
fs.copyFileSync(path.resolve(__dirname, "../config/default.ctx.js"), path.resolve(homedir, "default.ctx.js"));
|
|
76
|
+
fs.copyFileSync(path.resolve(__dirname, "../config/package.json"), path.resolve(homedir, "package.json"));
|
|
77
|
+
console.error("[tune-sdk] installing npm");
|
|
78
|
+
try {
|
|
79
|
+
_ref = cp.execSync("npm i", {
|
|
80
|
+
cwd: homedir,
|
|
81
|
+
encoding: "utf8"
|
|
82
|
+
});
|
|
83
|
+
stdout = _ref[0];
|
|
84
|
+
stderr = _ref[1];
|
|
85
|
+
if (stdout.trim()) console.error("[tune-sdk]", stdout.trim());
|
|
86
|
+
stderr.trim() ? console.error("[tune-sdk]", stderr.trim()) : undefined;
|
|
87
|
+
} catch (err) {}
|
|
88
|
+
return console.error("[tune-sdk] done");
|
|
89
|
+
}
|
|
90
|
+
initConfig;
|
|
91
|
+
async function suggest(params, ctx) {
|
|
92
|
+
var node, _ref;
|
|
93
|
+
var node;
|
|
94
|
+
node = await ctx.resolve(params.query, {
|
|
95
|
+
output: "all",
|
|
96
|
+
match: "regex"
|
|
97
|
+
});
|
|
98
|
+
if (!node) {
|
|
99
|
+
_ref = [];
|
|
100
|
+
} else if (!Array.isArray(node)) {
|
|
101
|
+
_ref = Array(node);
|
|
102
|
+
} else {
|
|
103
|
+
_ref = node;
|
|
104
|
+
}
|
|
105
|
+
node = _ref;
|
|
106
|
+
return node.map((function(item) {
|
|
107
|
+
return {
|
|
108
|
+
name: item.name,
|
|
109
|
+
dirname: item.dirname,
|
|
110
|
+
fullname: item.fullname,
|
|
111
|
+
basename: (item.fullname ? path.basename(item.fullname) : undefined),
|
|
112
|
+
type: item.type
|
|
113
|
+
}
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
suggest;
|
|
117
|
+
async function remoteContext(name, params) {
|
|
118
|
+
var server, node;
|
|
119
|
+
var server;
|
|
120
|
+
server = this;
|
|
121
|
+
var node;
|
|
122
|
+
node = await server.resolve({
|
|
123
|
+
name: name,
|
|
124
|
+
params: params
|
|
125
|
+
});
|
|
126
|
+
if ((((typeof node !== "undefined") && (node !== null) && !Number.isNaN(node) && (typeof node.error !== "undefined") && (node.error !== null) && !Number.isNaN(node.error)) ? node.error : undefined)) return;
|
|
127
|
+
node.read = (function() {
|
|
128
|
+
return server.read({
|
|
129
|
+
name: name,
|
|
130
|
+
params: params
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
node.exec = (function() {
|
|
134
|
+
return server.exec({
|
|
135
|
+
name: name,
|
|
136
|
+
params: params
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
return node;
|
|
140
|
+
}
|
|
141
|
+
remoteContext;
|
|
142
|
+
async function runRpc(args) {
|
|
143
|
+
var inStream, outStream, ctx, server;
|
|
144
|
+
var inStream;
|
|
145
|
+
var outStream;
|
|
146
|
+
var ctx;
|
|
147
|
+
var server;
|
|
148
|
+
inStream = stream.Readable.toWeb(process.stdin);
|
|
149
|
+
outStream = stream.Writable.toWeb(process.stdout);
|
|
150
|
+
ctx = await initContext(args);
|
|
151
|
+
server = rpc.jsonrpc({
|
|
152
|
+
inStream: inStream,
|
|
153
|
+
outStream: outStream,
|
|
154
|
+
debug: args.debug,
|
|
155
|
+
name: "server"
|
|
156
|
+
}, {
|
|
157
|
+
init: (async function(methods) {
|
|
158
|
+
return ((-1 !== methods.indexOf("resolve")) ? ctx.use(remoteContext.bind(server)) : undefined);
|
|
159
|
+
}),
|
|
160
|
+
file2run: (async function(params, stream) {
|
|
161
|
+
return tune.file2run({
|
|
162
|
+
filename: params.filename,
|
|
163
|
+
response: params.response,
|
|
164
|
+
stream: stream,
|
|
165
|
+
stop: params.stop,
|
|
166
|
+
text: params.text,
|
|
167
|
+
system: params.system,
|
|
168
|
+
user: params.user,
|
|
169
|
+
save: params.save
|
|
170
|
+
}, {}, ctx);
|
|
171
|
+
}),
|
|
172
|
+
suggest: (async function(params) {
|
|
173
|
+
return suggest(params, ctx);
|
|
174
|
+
})
|
|
175
|
+
});
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
runRpc;
|
|
179
|
+
async function run(args) {
|
|
180
|
+
var ctx, stop, params, res;
|
|
181
|
+
ctx = await initContext(args);
|
|
182
|
+
var stop;
|
|
183
|
+
stop = args.stop || "assistant";
|
|
184
|
+
var params;
|
|
185
|
+
params = args.params || {}
|
|
186
|
+
delete args.params;
|
|
187
|
+
var res;
|
|
188
|
+
res = await ctx.file2run(args, params);
|
|
189
|
+
return (!args.filename ? console.log(res) : undefined);
|
|
190
|
+
}
|
|
191
|
+
run;
|
|
192
|
+
async function initContext(args) {
|
|
193
|
+
var dirs, pwd, ctx, dir, ctxName, ext, module, m, _i, _ref, _len, _i0, _ref0, _len0;
|
|
194
|
+
var dirs;
|
|
195
|
+
var pwd;
|
|
196
|
+
dirs = [];
|
|
197
|
+
pwd = process.cwd();
|
|
198
|
+
args.path ? dirs = args.path.split(path.delimiter)
|
|
199
|
+
.map((function(dir) {
|
|
200
|
+
return path.resolve(pwd, dir);
|
|
201
|
+
})) : dirs.push(getHomedir(args));
|
|
202
|
+
dirs.push(pwd);
|
|
203
|
+
process.env.TUNE_PATH = dirs.join(path.delimiter);
|
|
204
|
+
ctx = tune.makeContext(process.env);
|
|
205
|
+
_ref = dirs;
|
|
206
|
+
for (_i = 0, _len = _ref.length; _i < _len; ++_i) {
|
|
207
|
+
dir = _ref[_i];
|
|
208
|
+
var ctxName;
|
|
209
|
+
ctxName = ["default.ctx.js", "default.ctx.cjs", "default.ctx.mjs"]
|
|
210
|
+
.map((function(name) {
|
|
211
|
+
return path.join(dir, name);
|
|
212
|
+
}))
|
|
213
|
+
.find((function(name) {
|
|
214
|
+
return fs.existsSync(name);
|
|
215
|
+
}));
|
|
216
|
+
if (!ctxName) continue;
|
|
217
|
+
var ext;
|
|
218
|
+
var module;
|
|
219
|
+
ext = path.extname(ctxName);
|
|
220
|
+
module = null;
|
|
221
|
+
if ((ext === ".js" || ext === ".cjs")) {
|
|
222
|
+
module = require(ctxName);
|
|
223
|
+
} else {
|
|
224
|
+
module = await import(ctxName);
|
|
225
|
+
module = module.default;
|
|
226
|
+
}
|
|
227
|
+
if ((typeof module === "function")) {
|
|
228
|
+
ctx.use(module);
|
|
229
|
+
} else if (Array.isArray(module)) {
|
|
230
|
+
_ref0 = module;
|
|
231
|
+
for (_i0 = 0, _len0 = _ref0.length; _i0 < _len0; ++_i0) {
|
|
232
|
+
m = _ref0[_i0];
|
|
233
|
+
if ((typeof m === "function")) {
|
|
234
|
+
ctx.use(m);
|
|
235
|
+
} else {
|
|
236
|
+
throw Error(tpl("err: Context file export is not an array of functions or function {name}: {module}", {
|
|
237
|
+
name: ctxName,
|
|
238
|
+
module: m
|
|
239
|
+
}));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
throw Error(tpl("err: Context file export is not an array of functions or function {name}: {module}", {
|
|
244
|
+
name: ctxName,
|
|
245
|
+
module: module
|
|
246
|
+
}));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return ctx;
|
|
250
|
+
}
|
|
251
|
+
initContext;
|
|
252
|
+
async function main() {
|
|
253
|
+
var args, _ref;
|
|
254
|
+
try {
|
|
255
|
+
var args;
|
|
256
|
+
args = parseArgs(process.argv.slice(2));
|
|
257
|
+
await initConfig(args);
|
|
258
|
+
_ref = args.rpc ? await runRpc(args) : await run(args);
|
|
259
|
+
} catch (e) {
|
|
260
|
+
_ref = console.error(e);
|
|
261
|
+
}
|
|
262
|
+
return _ref;
|
|
263
|
+
}
|
|
264
|
+
main;
|
|
265
|
+
|
|
266
|
+
function tpl(str) {
|
|
267
|
+
var _i;
|
|
268
|
+
var params = 2 <= arguments.length ? [].slice.call(arguments, 1, _i = arguments.length - 0) : (_i = 1, []);
|
|
269
|
+
return (function(paramIndex, params) {
|
|
270
|
+
var _ref;
|
|
271
|
+
try {
|
|
272
|
+
_ref = str.replace(/{(\W*)(\w*)(\W*)}/gm, (function(_, pre, name, post) {
|
|
273
|
+
return (function(res) {
|
|
274
|
+
paramIndex += 1;
|
|
275
|
+
return ((typeof res !== 'undefined') ? ((pre || "") + res + (post || "")) : "");
|
|
276
|
+
})(params[name || paramIndex]);
|
|
277
|
+
}));
|
|
278
|
+
} catch (e) {
|
|
279
|
+
_ref = console.log.apply(console, [].concat([e, str]).concat(params));
|
|
280
|
+
}
|
|
281
|
+
return _ref;
|
|
282
|
+
})(0, (((typeof params[0] === "object") && (params.length === 1)) ? params[0] : params));
|
|
283
|
+
}
|
|
284
|
+
tpl;
|
|
285
|
+
exports.parseArgs = parseArgs;
|
|
286
|
+
exports.rpc = rpc;
|
|
287
|
+
exports.main = main;
|
|
288
|
+
exports.run = run;
|
package/dist/rpc.js
CHANGED
|
@@ -35,8 +35,9 @@ function jsonrpc(params, exports) {
|
|
|
35
35
|
}
|
|
36
36
|
jsonrpc;
|
|
37
37
|
|
|
38
|
-
function concatU8A(
|
|
39
|
-
var totalLen, res, offset, arr, _i, _ref, _len;
|
|
38
|
+
function concatU8A() {
|
|
39
|
+
var totalLen, res, offset, arr, _i, _i0, _ref, _len;
|
|
40
|
+
var u8arrays = 1 <= arguments.length ? [].slice.call(arguments, 0, _i = arguments.length - 0) : (_i = 0, []);
|
|
40
41
|
var totalLen;
|
|
41
42
|
var res;
|
|
42
43
|
var offset;
|
|
@@ -46,8 +47,8 @@ function concatU8A(u8arrays) {
|
|
|
46
47
|
res = new Uint8Array(totalLen);
|
|
47
48
|
offset = 0;
|
|
48
49
|
_ref = u8arrays;
|
|
49
|
-
for (
|
|
50
|
-
arr = _ref[
|
|
50
|
+
for (_i0 = 0, _len = _ref.length; _i0 < _len; ++_i0) {
|
|
51
|
+
arr = _ref[_i0];
|
|
51
52
|
res.set(arr, offset);
|
|
52
53
|
offset = offset + arr.length;
|
|
53
54
|
}
|
|
@@ -55,10 +56,21 @@ function concatU8A(u8arrays) {
|
|
|
55
56
|
}
|
|
56
57
|
concatU8A;
|
|
57
58
|
|
|
59
|
+
function assert(cond, msg) {
|
|
60
|
+
var _ref;
|
|
61
|
+
if (!cond) {
|
|
62
|
+
_ref = undefined;
|
|
63
|
+
throw Error, msg;
|
|
64
|
+
} else {
|
|
65
|
+
_ref = undefined;
|
|
66
|
+
}
|
|
67
|
+
return _ref;
|
|
68
|
+
}
|
|
69
|
+
assert;
|
|
70
|
+
|
|
58
71
|
function JSONRPC(params, exports) {
|
|
59
72
|
var self;
|
|
60
73
|
this.msgId = 1;
|
|
61
|
-
this.chunks = [];
|
|
62
74
|
this.debug = params.debug;
|
|
63
75
|
this.name = params.name;
|
|
64
76
|
this.exports = exports || params.exports || {}
|
|
@@ -67,35 +79,34 @@ function JSONRPC(params, exports) {
|
|
|
67
79
|
this.reader = params.inStream.getReader();
|
|
68
80
|
this.writer = params.outStream.getWriter();
|
|
69
81
|
self = this;
|
|
70
|
-
|
|
71
|
-
assert
|
|
72
|
-
assert.ok(params.outStream, "outStream has to be defined and be a WritableStream");
|
|
82
|
+
assert(params.inStream, "inStream has to be defined and be a ReadableStream");
|
|
83
|
+
assert(params.outStream, "outStream has to be defined and be a WritableStream");
|
|
73
84
|
async function pump() {
|
|
74
|
-
var res, value, done,
|
|
85
|
+
var res, value, done, newlineIdx, line, msg, cb, iter, err, _ref, _err;
|
|
75
86
|
var res;
|
|
76
|
-
res = await self.reader.read();
|
|
77
87
|
var value;
|
|
78
88
|
var done;
|
|
89
|
+
res = await self.reader.read();
|
|
79
90
|
value = res.value;
|
|
80
91
|
done = res.done;
|
|
81
92
|
if (done) return;
|
|
82
|
-
self.chunks.push(value);
|
|
83
|
-
var buf;
|
|
84
93
|
var newlineIdx;
|
|
85
|
-
buf = concatU8A(self.chunks);
|
|
86
94
|
newlineIdx = 0;
|
|
87
|
-
|
|
95
|
+
self.buf = (self.buf ? concatU8A(self.buf, value) : value);
|
|
96
|
+
while (-1 !== (newlineIdx = self.buf.indexOf(10))) {
|
|
88
97
|
var line;
|
|
89
|
-
line =
|
|
90
|
-
buf = buf.subarray(newlineIdx + 1);
|
|
91
|
-
self.
|
|
92
|
-
if (self.debug) console.log(self.name, line);
|
|
98
|
+
line = new TextDecoder().decode(self.buf.subarray(0, newlineIdx));
|
|
99
|
+
self.buf = self.buf.subarray(newlineIdx + 1);
|
|
100
|
+
if (self.debug) console.error(self.name, "<==", line);
|
|
93
101
|
var msg;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
102
|
+
try {
|
|
103
|
+
_ref = JSON.parse(line);
|
|
104
|
+
} catch (_err) {
|
|
105
|
+
console.error(self.name, ":", line);
|
|
106
|
+
_ref = {}
|
|
98
107
|
}
|
|
108
|
+
msg = _ref;
|
|
109
|
+
if (!msg.id) continue;
|
|
99
110
|
var cb;
|
|
100
111
|
var iter;
|
|
101
112
|
cb = self.callbacks[msg.id];
|
|
@@ -125,30 +136,33 @@ function JSONRPC(params, exports) {
|
|
|
125
136
|
if (!self.exports[msg.method]) {
|
|
126
137
|
self._error(msg.id, "method not found: " + msg.method);
|
|
127
138
|
continue;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
139
|
+
}(function(m, r, chunk) {
|
|
140
|
+
var _ref0;
|
|
141
|
+
try {
|
|
142
|
+
_ref0 = self.exports[m.method](msg.params, msg.stream)
|
|
143
|
+
.then((async function(r) {
|
|
144
|
+
var _res, _ref0, _ref1;
|
|
145
|
+
if (((typeof r !== 'undefined') && !!r[Symbol.asyncIterator])) {
|
|
146
|
+
_res = [];
|
|
147
|
+
while (!chunk.done) {
|
|
148
|
+
chunk = await r.next();
|
|
149
|
+
if (chunk.err) throw chunk.err;
|
|
150
|
+
if (typeof(_ref0 = (!!chunk.value ? self._result(m.id, chunk.value, chunk.done) : undefined)) !== 'undefined') _res.push(_ref0);
|
|
151
|
+
}
|
|
152
|
+
_ref1 = _res;
|
|
153
|
+
} else {
|
|
154
|
+
_ref1 = self._result(m.id, r, true);
|
|
155
|
+
}
|
|
156
|
+
return _ref1;
|
|
157
|
+
}))
|
|
158
|
+
.catch((function(e) {
|
|
159
|
+
return self._error(m.id, e.message, e.stack);
|
|
160
|
+
}));
|
|
161
|
+
} catch (e) {
|
|
162
|
+
_ref0 = self._error(m.id, e.message, e.stack);
|
|
147
163
|
}
|
|
148
|
-
_ref0;
|
|
149
|
-
}
|
|
150
|
-
self._error(m.id, e.message, e.stack);
|
|
151
|
-
}
|
|
164
|
+
return _ref0;
|
|
165
|
+
})(msg, undefined, {});
|
|
152
166
|
}
|
|
153
167
|
}
|
|
154
168
|
return pump();
|
|
@@ -164,6 +178,7 @@ JSONRPC.prototype._write = (async function(payload) {
|
|
|
164
178
|
var data;
|
|
165
179
|
encoder = new TextEncoder();
|
|
166
180
|
data = encoder.encode(JSON.stringify(payload) + "\n");
|
|
181
|
+
if (this.debug) console.error(this.name, "==>", JSON.stringify(payload));
|
|
167
182
|
return await this.writer.write(data);
|
|
168
183
|
});
|
|
169
184
|
JSONRPC.prototype._error = (function(msgId, message, stack) {
|
|
@@ -208,4 +223,21 @@ JSONRPC.prototype._call = (async function(method, params, stream) {
|
|
|
208
223
|
}));
|
|
209
224
|
return (self.iterators[msgId] = new AsyncIter());
|
|
210
225
|
});
|
|
226
|
+
|
|
227
|
+
function _once(cond, body) {
|
|
228
|
+
return new Promise((function(resolve, reject) {
|
|
229
|
+
function handler() {
|
|
230
|
+
var _ref;
|
|
231
|
+
try {
|
|
232
|
+
_ref = cond() ? resolve(body()) : setTimeout(handler, 10);
|
|
233
|
+
} catch (err) {
|
|
234
|
+
_ref = reject(err);
|
|
235
|
+
}
|
|
236
|
+
return _ref;
|
|
237
|
+
}
|
|
238
|
+
setTimeout(handler, 10);
|
|
239
|
+
return "";
|
|
240
|
+
}));
|
|
241
|
+
}
|
|
242
|
+
_once;
|
|
211
243
|
exports.jsonrpc = jsonrpc;
|
package/dist/tune.js
CHANGED
|
@@ -1057,11 +1057,19 @@ Context.prototype.clone = (function() {
|
|
|
1057
1057
|
return res;
|
|
1058
1058
|
});
|
|
1059
1059
|
Context.prototype.use = (function(middleware) {
|
|
1060
|
-
|
|
1060
|
+
var _ref;
|
|
1061
|
+
if ((typeof middleware !== "function")) {
|
|
1062
|
+
_ref = undefined;
|
|
1063
|
+
throw Error(tpl("middleware {} is not a function", util.inspect(middleware)));
|
|
1064
|
+
} else {
|
|
1065
|
+
_ref = ((middleware.name === "write") ? this.ws.push(middleware) : this.ms.push(middleware));
|
|
1066
|
+
}
|
|
1067
|
+
return _ref;
|
|
1061
1068
|
});
|
|
1062
|
-
|
|
1069
|
+
async function resolve(ctx, name, args, middlewares) {
|
|
1063
1070
|
var i, output, match, type, result, res, md;
|
|
1064
|
-
|
|
1071
|
+
middlewares = middlewares || ctx.ms;
|
|
1072
|
+
if (!middlewares.length) return;
|
|
1065
1073
|
var i;
|
|
1066
1074
|
var output;
|
|
1067
1075
|
var match;
|
|
@@ -1074,10 +1082,10 @@ Context.prototype.resolve = (async function(name, args) {
|
|
|
1074
1082
|
type = (((typeof args !== "undefined") && (args !== null) && !Number.isNaN(args) && (typeof args.type !== "undefined") && (args.type !== null) && !Number.isNaN(args.type)) ? args.type : (((typeof "any" !== "undefined") && ("any" !== null) && !Number.isNaN("any")) ? "any" : undefined));
|
|
1075
1083
|
result = ((output === "all") ? [] : undefined);
|
|
1076
1084
|
res = undefined;
|
|
1077
|
-
while (i <
|
|
1085
|
+
while (i < middlewares.length) {
|
|
1078
1086
|
var md;
|
|
1079
|
-
md =
|
|
1080
|
-
res = await md.call(
|
|
1087
|
+
md = middlewares[i];
|
|
1088
|
+
res = await md.call(ctx, name, {
|
|
1081
1089
|
output: output,
|
|
1082
1090
|
match: match,
|
|
1083
1091
|
type: type
|
|
@@ -1106,6 +1114,10 @@ Context.prototype.resolve = (async function(name, args) {
|
|
|
1106
1114
|
i++;
|
|
1107
1115
|
}
|
|
1108
1116
|
return result;
|
|
1117
|
+
}
|
|
1118
|
+
resolve;
|
|
1119
|
+
Context.prototype.resolve = (async function(name, args) {
|
|
1120
|
+
return resolve(this, name, args);
|
|
1109
1121
|
});
|
|
1110
1122
|
Context.prototype.text2run = (function(text, opts) {
|
|
1111
1123
|
return text2run(text, this, opts);
|
|
@@ -2099,45 +2111,96 @@ function text2run(text, ctx, opts) {
|
|
|
2099
2111
|
}
|
|
2100
2112
|
text2run;
|
|
2101
2113
|
async function file2run(args, params, ctx) {
|
|
2102
|
-
var lctx,
|
|
2114
|
+
var lctx, text, stop, node, response, res, r, chunk, itergQageqc, _ref;
|
|
2103
2115
|
var lctx;
|
|
2104
2116
|
lctx = ctx.clone();
|
|
2105
2117
|
lctx.ms.unshift(envmd(params));
|
|
2106
|
-
var
|
|
2107
|
-
|
|
2118
|
+
var text;
|
|
2119
|
+
var stop;
|
|
2120
|
+
text = args.text;
|
|
2121
|
+
stop = (((typeof args !== "undefined") && (args !== null) && !Number.isNaN(args) && (typeof args.stop !== "undefined") && (args.stop !== null) && !Number.isNaN(args.stop)) ? args.stop : (((typeof "assistant" !== "undefined") && ("assistant" !== null) && !Number.isNaN("assistant")) ? "assistant" : undefined));
|
|
2108
2122
|
if (args.filename) {
|
|
2109
2123
|
node = await ctx.resolve(args.filename);
|
|
2110
2124
|
if (node) {
|
|
2111
2125
|
lctx.stack.push(node);
|
|
2112
|
-
text = await node.read();
|
|
2126
|
+
if (!text) text = await node.read();
|
|
2113
2127
|
}
|
|
2114
2128
|
}
|
|
2115
2129
|
if ((!text && args.system)) text = tpl("system:\n{system}", args);
|
|
2116
2130
|
if (args.user) text += tpl("\nuser:\n{user}", args);
|
|
2117
|
-
var res;
|
|
2118
|
-
res = await lctx.text2run(text, {
|
|
2119
|
-
stop: (((typeof args !== "undefined") && (args !== null) && !Number.isNaN(args) && (typeof args.stop !== "undefined") && (args.stop !== null) && !Number.isNaN(args.stop)) ? args.stop : (((typeof "assistant" !== "undefined") && ("assistant" !== null) && !Number.isNaN("assistant")) ? "assistant" : undefined))
|
|
2120
|
-
});
|
|
2121
|
-
if ((args.filename && (((typeof args !== "undefined") && (args !== null) && !Number.isNaN(args) && (typeof args.save !== "undefined") && (args.save !== null) && !Number.isNaN(args.save)) ? args.save : undefined))) {
|
|
2122
|
-
var longFormatRegex;
|
|
2123
|
-
longFormatRegex = /^(system|user|tool_call|tool_result|assistant|error):/;
|
|
2124
|
-
await ctx.write(args.filename, text + "\n" + msg2text(res, longFormatRegex.test(text)));
|
|
2125
|
-
}
|
|
2126
2131
|
var response;
|
|
2127
2132
|
response = (((typeof args !== "undefined") && (args !== null) && !Number.isNaN(args) && (typeof args.response !== "undefined") && (args.response !== null) && !Number.isNaN(args.response)) ? args.response : (((typeof "content" !== "undefined") && ("content" !== null) && !Number.isNaN("content")) ? "content" : undefined));
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2133
|
+
|
|
2134
|
+
function transformOutput(res) {
|
|
2135
|
+
var _ref;
|
|
2136
|
+
switch (response) {
|
|
2137
|
+
case "content":
|
|
2138
|
+
_ref = res["slice"](-1)[0].content;
|
|
2139
|
+
break;
|
|
2140
|
+
case "json":
|
|
2141
|
+
_ref = JSON.parse(res["slice"](-1)[0].content);
|
|
2142
|
+
break;
|
|
2143
|
+
case "messages":
|
|
2144
|
+
_ref = res;
|
|
2145
|
+
break;
|
|
2146
|
+
case "chat":
|
|
2147
|
+
_ref = msg2text(res, true);
|
|
2148
|
+
break;
|
|
2149
|
+
default:
|
|
2150
|
+
_ref = undefined;
|
|
2151
|
+
throw new TuneError(tpl("Invalid response type '{}'", response));
|
|
2152
|
+
}
|
|
2153
|
+
return _ref;
|
|
2154
|
+
}
|
|
2155
|
+
transformOutput;
|
|
2156
|
+
async function save() {
|
|
2157
|
+
var longFormatRegex, _ref;
|
|
2158
|
+
if ((args.filename && (((typeof args !== "undefined") && (args !== null) && !Number.isNaN(args) && (typeof args.save !== "undefined") && (args.save !== null) && !Number.isNaN(args.save)) ? args.save : undefined))) {
|
|
2159
|
+
var longFormatRegex;
|
|
2160
|
+
longFormatRegex = /^(system|user|tool_call|tool_result|assistant|error):/;
|
|
2161
|
+
_ref = await ctx.write(args.filename, text + "\n" + msg2text(res, longFormatRegex.test(text)));
|
|
2162
|
+
} else {
|
|
2139
2163
|
_ref = undefined;
|
|
2140
|
-
|
|
2164
|
+
}
|
|
2165
|
+
return _ref;
|
|
2166
|
+
}
|
|
2167
|
+
save;
|
|
2168
|
+
if (!args.stream) {
|
|
2169
|
+
var res;
|
|
2170
|
+
res = await lctx.text2run(text, {
|
|
2171
|
+
stop: stop
|
|
2172
|
+
});
|
|
2173
|
+
await save();
|
|
2174
|
+
_ref = transformOutput(res);
|
|
2175
|
+
} else {
|
|
2176
|
+
r = await lctx.text2run(text, {
|
|
2177
|
+
stop: stop,
|
|
2178
|
+
stream: true
|
|
2179
|
+
});
|
|
2180
|
+
chunk = {};
|
|
2181
|
+
itergQageqc = new AsyncIter();
|
|
2182
|
+
(async function($lastRes) {
|
|
2183
|
+
var _ref;
|
|
2184
|
+
try {
|
|
2185
|
+
while (!chunk.done) {
|
|
2186
|
+
chunk = await r.next();
|
|
2187
|
+
res = (chunk.value || "");
|
|
2188
|
+
if (chunk.done) await save();
|
|
2189
|
+
$lastRes = transformOutput(res) || $lastRes;
|
|
2190
|
+
itergQageqc.result = {
|
|
2191
|
+
value: $lastRes
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
_ref = itergQageqc.result = {
|
|
2195
|
+
value: $lastRes,
|
|
2196
|
+
done: true
|
|
2197
|
+
}
|
|
2198
|
+
} catch (e) {
|
|
2199
|
+
_ref = (itergQageqc.err = e);
|
|
2200
|
+
}
|
|
2201
|
+
return _ref;
|
|
2202
|
+
})();
|
|
2203
|
+
_ref = itergQageqc;
|
|
2141
2204
|
}
|
|
2142
2205
|
return _ref;
|
|
2143
2206
|
}
|
|
@@ -2414,4 +2477,5 @@ exports.text2payload = text2payload;
|
|
|
2414
2477
|
exports.payload2http = payload2http;
|
|
2415
2478
|
exports.envmd = envmd;
|
|
2416
2479
|
exports.unescape = unescape;
|
|
2417
|
-
exports.escape = escape;
|
|
2480
|
+
exports.escape = escape;
|
|
2481
|
+
exports.resolve = resolve;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tune-sdk",
|
|
3
|
-
"version": "0.1
|
|
4
|
-
"description": "tune -
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "tune - LLM chat in text file",
|
|
5
5
|
"main": "dist/tune.js",
|
|
6
6
|
"module": "dist/tune.mjs",
|
|
7
7
|
"bin": "bin/cli.js",
|
|
@@ -12,6 +12,12 @@
|
|
|
12
12
|
},
|
|
13
13
|
"./fsctx": {
|
|
14
14
|
"require": "./dist/fsctx.js"
|
|
15
|
+
},
|
|
16
|
+
"./cli": {
|
|
17
|
+
"require": "./dist/cli.js"
|
|
18
|
+
},
|
|
19
|
+
"./rpc": {
|
|
20
|
+
"require": "./dist/rpc.js"
|
|
15
21
|
}
|
|
16
22
|
},
|
|
17
23
|
"keywords": [
|
package/tools/default.ctx.js
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "Fetch audio from Yandex's TTS",
|
|
3
|
+
"parameters": {
|
|
4
|
+
"type": "object",
|
|
5
|
+
"properties": {
|
|
6
|
+
"text": {
|
|
7
|
+
"type": "string",
|
|
8
|
+
"description": "Text input to convert to speech. + Set stress/accent e.g. 'h+ello'\nSpecify context-dependent pause: <[small]>, allowed: tiny, small, medium, large, huge\nHighlight emcented]> or **emphasized word**"
|
|
9
|
+
},
|
|
10
|
+
"voice": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"enum": [ "alena-neutral", "alena-good",
|
|
13
|
+
"filipp",
|
|
14
|
+
"ermil-neutral", "ermil-good",
|
|
15
|
+
"jane-neutral", "jane-good", "jane-evil",
|
|
16
|
+
"omazh-neutral", "omazh-evil",
|
|
17
|
+
"zahar-neutral", "zahar-good",
|
|
18
|
+
"dasha-neutral", "dasha-good", "dasha-friendly",
|
|
19
|
+
"julia-neutral", "julia-strict",
|
|
20
|
+
"lera-neutral", "lera-friendly",
|
|
21
|
+
"masha-good", "masha-strict", "masha-friendly",
|
|
22
|
+
"marina-neutral", "marina-whisper", "marina-friendly",
|
|
23
|
+
"alexander-neutral", "alexander-good",
|
|
24
|
+
"kirill-neutral", "kirill-strict", "kirill-good",
|
|
25
|
+
"anton-neutral", "anton-good",
|
|
26
|
+
"madi_ru",
|
|
27
|
+
"saule_ru-neutral", "saule_ru-strict", "saule_ru-whisper",
|
|
28
|
+
"zamira_ru-neutral", "zamira_ru-strict", "zamira_ru-friendly",
|
|
29
|
+
"zhanar_ru-neutral", "zhanar_ru-strict", "zhanar_ru-friendly",
|
|
30
|
+
"yulduz_ru-neutral", "yulduz_ru-strict", "yulduz_ru-friendly", "yulduz_ru-whisper"
|
|
31
|
+
],
|
|
32
|
+
"description": "Voice type to use for TTS"
|
|
33
|
+
},
|
|
34
|
+
"filename": {
|
|
35
|
+
"type": "string",
|
|
36
|
+
"description": "wav filename to save the audio to"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"required": ["text", "voice", "filename"]
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
export default async function yandexTTS({ text, voice, filename }, ctx) {
|
|
3
|
+
const key = await ctx.read("YC_TOKEN");
|
|
4
|
+
const folderId = await ctx.read("YC_FOLDER_ID")
|
|
5
|
+
const [v, r] = voice.split("-")
|
|
6
|
+
const hints = [{ voice: v}]
|
|
7
|
+
if (r) {
|
|
8
|
+
hints.push({ role: r})
|
|
9
|
+
}
|
|
10
|
+
const response = await fetch("https://tts.api.cloud.yandex.net/tts/v3/utteranceSynthesis", {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: {
|
|
13
|
+
"Authorization": `Bearer ${key}`,
|
|
14
|
+
"x-folder-id": folderId,
|
|
15
|
+
"Content-Type": "application/json",
|
|
16
|
+
},
|
|
17
|
+
body: JSON.stringify({
|
|
18
|
+
text,
|
|
19
|
+
hints
|
|
20
|
+
}),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
if (!response.ok) {
|
|
25
|
+
const { error } = await response.json()
|
|
26
|
+
throw new Error(`Error: ${response.status} ${response.statusText}\n${error.message}`);
|
|
27
|
+
}
|
|
28
|
+
const result = await response.json()
|
|
29
|
+
await ctx.write(filename, Buffer.from(result.result.audioChunk.data, 'base64'));
|
|
30
|
+
return "speech generated"
|
|
31
|
+
}
|