tune-sdk 0.2.17 → 0.2.18
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 +2 -2
- package/dist/cli.js +16 -18
- package/dist/rpc.js +9 -1
- package/dist/tune.js +25 -16
- package/package.json +8 -4
- package/src/cli.js +442 -0
- package/src/rpc.js +313 -0
- package/dist/run.php +0 -78
- package/dist/run.py +0 -22
- package/dist/umsgpack.py +0 -1248
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Tune - AI
|
|
1
|
+
# Tune - the last AI tool you’ll need to learn
|
|
2
2
|
[](https://www.reddit.com/r/tuneai/)
|
|
3
3
|
[](https://discord.gg/hu32FNYPYD)
|
|
4
4
|
|
package/bin/cli.js
CHANGED
package/dist/cli.js
CHANGED
|
@@ -265,11 +265,6 @@ async function remoteContext(name, params) {
|
|
|
265
265
|
remoteContext;
|
|
266
266
|
async function runRpc(args) {
|
|
267
267
|
var inStream, outStream, debugStream, ctx, server;
|
|
268
|
-
var inStream;
|
|
269
|
-
var outStream;
|
|
270
|
-
var debugStream;
|
|
271
|
-
var ctx;
|
|
272
|
-
var server;
|
|
273
268
|
inStream = stream.Readable.toWeb(process.stdin);
|
|
274
269
|
outStream = stream.Writable.toWeb(process.stdout);
|
|
275
270
|
debugStream = ((typeof args.debug === "string") ? fs.createWriteStream(args.debug, {
|
|
@@ -288,24 +283,27 @@ async function runRpc(args) {
|
|
|
288
283
|
}, {
|
|
289
284
|
init: (async function(methods) {
|
|
290
285
|
if ((-1 !== methods.indexOf("resolve"))) ctx.use(remoteContext.bind(server));
|
|
291
|
-
return
|
|
292
|
-
}),
|
|
293
|
-
file2run: (async function(params, stream) {
|
|
294
|
-
return tune.file2run({
|
|
295
|
-
filename: params.filename,
|
|
296
|
-
response: params.response,
|
|
297
|
-
stream: stream,
|
|
298
|
-
stop: params.stop,
|
|
299
|
-
text: params.text,
|
|
300
|
-
system: params.system,
|
|
301
|
-
user: params.user,
|
|
302
|
-
save: params.save
|
|
303
|
-
}, {}, ctx);
|
|
286
|
+
return;
|
|
304
287
|
}),
|
|
288
|
+
file2run: (async function(params, stream) {
|
|
289
|
+
// backward compatibility
|
|
290
|
+
let payload = params
|
|
291
|
+
let args = {}
|
|
292
|
+
if (params.payload) {
|
|
293
|
+
payload = params.payload
|
|
294
|
+
args = params.params
|
|
295
|
+
}
|
|
296
|
+
return ctx.file2run({
|
|
297
|
+
...payload,
|
|
298
|
+
stream
|
|
299
|
+
}, args);
|
|
300
|
+
}),
|
|
305
301
|
suggest: (async function(params) {
|
|
306
302
|
return suggest(params, ctx);
|
|
307
303
|
})
|
|
308
304
|
});
|
|
305
|
+
// const node = await server.resolve("OLLAMA_URL")
|
|
306
|
+
// console.log("node", node)
|
|
309
307
|
return null;
|
|
310
308
|
}
|
|
311
309
|
runRpc;
|
package/dist/rpc.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
function AsyncIter() {
|
|
2
|
-
return
|
|
2
|
+
return this;
|
|
3
3
|
}
|
|
4
4
|
AsyncIter;
|
|
5
|
+
AsyncIter.prototype[Symbol.asyncIterator] = (function() {
|
|
6
|
+
return this;
|
|
7
|
+
});
|
|
5
8
|
AsyncIter.prototype.next = (async function() {
|
|
6
9
|
var self, result;
|
|
7
10
|
var self;
|
|
8
11
|
self = this;
|
|
12
|
+
if ((self.lastReturn && self.lastReturn.done)) return self.lastReturn;
|
|
9
13
|
await _once((function() {
|
|
10
14
|
return (!!self.err || !!self.result);
|
|
11
15
|
}), (function() {
|
|
@@ -14,6 +18,10 @@ AsyncIter.prototype.next = (async function() {
|
|
|
14
18
|
var result;
|
|
15
19
|
result = self.result;
|
|
16
20
|
self.result = undefined;
|
|
21
|
+
self.lastReturn = result;
|
|
22
|
+
if ((result && result.value && result.done)) result = {
|
|
23
|
+
value: result.value
|
|
24
|
+
};
|
|
17
25
|
if (self.err) throw self.err;
|
|
18
26
|
return result;
|
|
19
27
|
});
|
package/dist/tune.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var util, AsyncLocalStorage,
|
|
1
|
+
var als, util, AsyncLocalStorage, $roles;
|
|
2
2
|
|
|
3
3
|
function extend() {
|
|
4
4
|
var _i;
|
|
@@ -20,13 +20,17 @@ function extend() {
|
|
|
20
20
|
extend;
|
|
21
21
|
|
|
22
22
|
function AsyncIter() {
|
|
23
|
-
return
|
|
23
|
+
return this;
|
|
24
24
|
}
|
|
25
25
|
AsyncIter;
|
|
26
|
+
AsyncIter.prototype[Symbol.asyncIterator] = (function() {
|
|
27
|
+
return this;
|
|
28
|
+
});
|
|
26
29
|
AsyncIter.prototype.next = (async function() {
|
|
27
30
|
var self, result;
|
|
28
31
|
var self;
|
|
29
32
|
self = this;
|
|
33
|
+
if ((self.lastReturn && self.lastReturn.done)) return self.lastReturn;
|
|
30
34
|
await _once((function() {
|
|
31
35
|
return (!!self.err || !!self.result);
|
|
32
36
|
}), (function() {
|
|
@@ -35,20 +39,24 @@ AsyncIter.prototype.next = (async function() {
|
|
|
35
39
|
var result;
|
|
36
40
|
result = self.result;
|
|
37
41
|
self.result = undefined;
|
|
42
|
+
self.lastReturn = result;
|
|
43
|
+
if ((result && result.value && result.done)) result = {
|
|
44
|
+
value: result.value
|
|
45
|
+
};
|
|
38
46
|
if (self.err) throw self.err;
|
|
39
47
|
return result;
|
|
40
48
|
});
|
|
41
|
-
if ((typeof
|
|
42
|
-
util = require("util");
|
|
43
|
-
AsyncLocalStorage = require("node:async_hooks").AsyncLocalStorage;
|
|
44
|
-
als = new AsyncLocalStorage();
|
|
45
|
-
} else {
|
|
49
|
+
if ((typeof window !== 'undefined')) {
|
|
46
50
|
als = {
|
|
47
51
|
getStore: (function() {}),
|
|
48
52
|
run: (async function(store, func) {
|
|
49
53
|
return func();
|
|
50
54
|
})
|
|
51
55
|
};
|
|
56
|
+
} else {
|
|
57
|
+
util = require("util");
|
|
58
|
+
AsyncLocalStorage = require("node:async_hooks").AsyncLocalStorage;
|
|
59
|
+
als = new AsyncLocalStorage();
|
|
52
60
|
}
|
|
53
61
|
|
|
54
62
|
function TuneError(message, filename, row, col, stack, originalError) {
|
|
@@ -1151,8 +1159,9 @@ async function resolve(ctx, name, args, middlewares) {
|
|
|
1151
1159
|
}));
|
|
1152
1160
|
if (!item.type) {
|
|
1153
1161
|
_ref = undefined;
|
|
1154
|
-
throw Error(tpl("resolved '@{name}' node must have a 'type' property", {
|
|
1155
|
-
name: name
|
|
1162
|
+
throw Error(tpl("resolved '@{name}' node must have a 'type' property {item}", {
|
|
1163
|
+
name: name,
|
|
1164
|
+
item: JSON.stringify(item)
|
|
1156
1165
|
}));
|
|
1157
1166
|
} else {
|
|
1158
1167
|
_ref = undefined;
|
|
@@ -2231,7 +2240,7 @@ function text2run(text, ctx, opts) {
|
|
|
2231
2240
|
}
|
|
2232
2241
|
text2run;
|
|
2233
2242
|
async function file2run(args, params, ctx) {
|
|
2234
|
-
var lctx, text, stop, node, response, res, r, chunk,
|
|
2243
|
+
var lctx, text, stop, node, response, res, r, chunk, itergEXej98, _ref;
|
|
2235
2244
|
var lctx;
|
|
2236
2245
|
lctx = ctx.clone();
|
|
2237
2246
|
if (params) lctx.ms.unshift(envmd(params));
|
|
@@ -2298,7 +2307,7 @@ async function file2run(args, params, ctx) {
|
|
|
2298
2307
|
stream: true
|
|
2299
2308
|
});
|
|
2300
2309
|
chunk = {};
|
|
2301
|
-
|
|
2310
|
+
itergEXej98 = new AsyncIter();
|
|
2302
2311
|
(async function($lastRes) {
|
|
2303
2312
|
var _ref;
|
|
2304
2313
|
try {
|
|
@@ -2307,20 +2316,20 @@ async function file2run(args, params, ctx) {
|
|
|
2307
2316
|
res = (chunk.value || "");
|
|
2308
2317
|
if (chunk.done) await save();
|
|
2309
2318
|
$lastRes = transformOutput(res) || $lastRes;
|
|
2310
|
-
|
|
2319
|
+
itergEXej98.result = {
|
|
2311
2320
|
value: $lastRes
|
|
2312
2321
|
}
|
|
2313
2322
|
}
|
|
2314
|
-
_ref =
|
|
2323
|
+
_ref = itergEXej98.result = {
|
|
2315
2324
|
value: $lastRes,
|
|
2316
2325
|
done: true
|
|
2317
2326
|
}
|
|
2318
2327
|
} catch (e) {
|
|
2319
|
-
_ref = (
|
|
2328
|
+
_ref = (itergEXej98.err = e);
|
|
2320
2329
|
}
|
|
2321
2330
|
return _ref;
|
|
2322
2331
|
})();
|
|
2323
|
-
_ref =
|
|
2332
|
+
_ref = itergEXej98;
|
|
2324
2333
|
}
|
|
2325
2334
|
return _ref;
|
|
2326
2335
|
}
|
|
@@ -2595,4 +2604,4 @@ exports.payload2http = payload2http;
|
|
|
2595
2604
|
exports.envmd = envmd;
|
|
2596
2605
|
exports.unescape = unescape;
|
|
2597
2606
|
exports.escape = escape;
|
|
2598
|
-
exports.resolve = resolve;
|
|
2607
|
+
exports.resolve = resolve;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tune-sdk",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.18",
|
|
4
4
|
"description": "tune - LLM chat in text file",
|
|
5
5
|
"main": "dist/tune.js",
|
|
6
6
|
"module": "dist/tune.mjs",
|
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
"import": "./dist/tune.mjs"
|
|
12
12
|
},
|
|
13
13
|
"./cli": {
|
|
14
|
-
"require": "./
|
|
14
|
+
"require": "./src/cli.js"
|
|
15
15
|
},
|
|
16
16
|
"./rpc": {
|
|
17
|
-
"require": "./
|
|
17
|
+
"require": "./src/rpc.js"
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"keywords": [
|
|
@@ -27,5 +27,9 @@
|
|
|
27
27
|
"type": "git",
|
|
28
28
|
"url": "https://github.com/iovdin/tune"
|
|
29
29
|
},
|
|
30
|
-
"license": "MIT"
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "node test/index.js",
|
|
33
|
+
"test:watch": "node --watch test/index.js"
|
|
34
|
+
}
|
|
31
35
|
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
var assert, tune, rpc, path, fs, os, cp, stream;
|
|
2
|
+
assert = require("assert");
|
|
3
|
+
|
|
4
|
+
function tpl(str) {
|
|
5
|
+
var _i;
|
|
6
|
+
var params = 2 <= arguments.length ? [].slice.call(arguments, 1, _i = arguments.length - 0) : (_i = 1, []);
|
|
7
|
+
return (function(paramIndex, params) {
|
|
8
|
+
var _ref;
|
|
9
|
+
try {
|
|
10
|
+
_ref = str.replace(/{(\W*)(\w*)(\W*)}/gm, (function(_, pre, name, post) {
|
|
11
|
+
return (function(res) {
|
|
12
|
+
paramIndex += 1;
|
|
13
|
+
return ((typeof res !== 'undefined') ? ((pre || "") + res + (post || "")) : "");
|
|
14
|
+
})(params[name || paramIndex]);
|
|
15
|
+
}));
|
|
16
|
+
} catch (e) {
|
|
17
|
+
_ref = console.log.apply(console, [].concat([e, str]).concat(params));
|
|
18
|
+
}
|
|
19
|
+
return _ref;
|
|
20
|
+
})(0, (((typeof params[0] === "object") && (params.length === 1)) ? params[0] : params));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function showHelp() {
|
|
24
|
+
console.log("TUNE-CLI - Command Line Interface for Tune SDK");
|
|
25
|
+
console.log("");
|
|
26
|
+
console.log("USAGE:");
|
|
27
|
+
console.log(" tune-sdk [cmd] [OPTIONS]");
|
|
28
|
+
console.log("");
|
|
29
|
+
console.log("COMMANDS:");
|
|
30
|
+
console.log(" rpc Start RPC server mode");
|
|
31
|
+
console.log(" init Initialize Tune config directory");
|
|
32
|
+
console.log("");
|
|
33
|
+
console.log("EXAMPLES:");
|
|
34
|
+
console.log(" # Quick chat with system prompt");
|
|
35
|
+
console.log(" tune-sdk --system \"You are Groot\" --user \"Hi how are you?\"");
|
|
36
|
+
console.log("");
|
|
37
|
+
console.log(" # Continue existing chat");
|
|
38
|
+
console.log(" tune-sdk --user \"continue the conversation\" --filename chat.chat --save");
|
|
39
|
+
console.log("");
|
|
40
|
+
console.log(" # Set context variables");
|
|
41
|
+
console.log(" tune-sdk --set-test=hello --user \"@test\" --system \"Echo assistant\"");
|
|
42
|
+
console.log("");
|
|
43
|
+
console.log(" # RPC mode for editor integration");
|
|
44
|
+
console.log(" tune-sdk rpc");
|
|
45
|
+
console.log("");
|
|
46
|
+
console.log(" # Initialize or reinitialize config directory");
|
|
47
|
+
console.log(" tune-sdk init --force");
|
|
48
|
+
console.log("");
|
|
49
|
+
console.log("OPTIONS:");
|
|
50
|
+
console.log(" --user <text> User message to send");
|
|
51
|
+
console.log(" --system <text> System prompt to use");
|
|
52
|
+
console.log(" --filename <file> Chat file to load/save");
|
|
53
|
+
console.log(" --save Save conversation to file");
|
|
54
|
+
console.log(" --stop <mode> Stop condition: assistant|step|<custom>");
|
|
55
|
+
console.log(" --text <content> chat content");
|
|
56
|
+
console.log(" --response <type> Response format: content|json|messages|chat (default: content)");
|
|
57
|
+
console.log(" --set-<name>=<value> Set context parameter");
|
|
58
|
+
console.log(" --path <paths> Additional search paths (colon-separated)");
|
|
59
|
+
console.log(" --home <dir> Tune config directory (default: ~/.tune)");
|
|
60
|
+
console.log(" --debug Enable debug output");
|
|
61
|
+
console.log(" --silent Suppress output");
|
|
62
|
+
console.log(" --force Force config initialization (with 'init')");
|
|
63
|
+
console.log(" --help Show this help");
|
|
64
|
+
console.log(" --version Show CLI version");
|
|
65
|
+
return console.log("");
|
|
66
|
+
}
|
|
67
|
+
showHelp;
|
|
68
|
+
|
|
69
|
+
function validateArgs(args) {
|
|
70
|
+
assert(!!args && (typeof args === "object"), "Arguments must be an object");
|
|
71
|
+
if (args.user) assert(typeof args.user === "string", "--user must be a string");
|
|
72
|
+
if (args.system) assert(typeof args.system === "string", "--system must be a string");
|
|
73
|
+
if (args.filename) assert(typeof args.filename === "string", "--filename must be a string");
|
|
74
|
+
if (args.text) assert(typeof args.text === "string", "--text must be a string");
|
|
75
|
+
if (args.response) assert(typeof args.response === "string", "--response must be a string");
|
|
76
|
+
if (args.stop) assert(typeof args.stop === "string", "--stop must be a string");
|
|
77
|
+
if (args.path) assert(typeof args.path === "string", "--path must be a string");
|
|
78
|
+
if (args.home) assert(typeof args.home === "string", "--home must be a string");
|
|
79
|
+
if (!!args.save) assert(typeof args.save === "boolean", "--save must be a boolean");
|
|
80
|
+
if (!!args.debug) assert(typeof args.debug === "boolean" || typeof args.debug === "string", "--debug must be a boolean");
|
|
81
|
+
if (!!args.silent) assert(typeof args.silent === "boolean", "--silent must be a boolean");
|
|
82
|
+
if (!!args.force) assert(typeof args.force === "boolean", "--force must be a boolean");
|
|
83
|
+
if (typeof args.rpc !== "undefined") assert(false, "Use 'tune-sdk rpc' instead of --rpc");
|
|
84
|
+
if (typeof args.forceInit !== "undefined") assert(false, "Use 'tune-sdk init --force' instead of --force-init");
|
|
85
|
+
if (args.params) assert(!!args.params && (typeof args.params === "object"), "--set-* parameters must form a valid object");
|
|
86
|
+
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");
|
|
87
|
+
if (args.cmd) {
|
|
88
|
+
assert(typeof args.cmd === "string", "Command must be a string");
|
|
89
|
+
assert((args.cmd === "rpc") || (args.cmd === "init"), "Unknown command: " + args.cmd);
|
|
90
|
+
}
|
|
91
|
+
if ((!args.help && !args.version && !args.cmd && !args.user && !args.filename)) assert(false, "Must specify --user, --filename, a command (rpc|init), --version, or --help");
|
|
92
|
+
return args;
|
|
93
|
+
}
|
|
94
|
+
validateArgs;
|
|
95
|
+
|
|
96
|
+
function parseArgs(args) {
|
|
97
|
+
var curKey, res, res1, key, value, stop, _ref, _len;
|
|
98
|
+
assert(Array.isArray(args), "parseArgs expects an array of arguments");
|
|
99
|
+
var curKey;
|
|
100
|
+
curKey = null;
|
|
101
|
+
var res;
|
|
102
|
+
res = args.reduce((function(memo, arg) {
|
|
103
|
+
var key, value, _ref, _i;
|
|
104
|
+
assert(typeof arg === "string", "Each argument must be a string");
|
|
105
|
+
if (arg.startsWith("--")) {
|
|
106
|
+
_ref = arg.substring(2)
|
|
107
|
+
.split("=");
|
|
108
|
+
key = _ref[0];
|
|
109
|
+
value = _ref[1];
|
|
110
|
+
assert((typeof key === "string") && (key.length > 0), "Argument key must be a non-empty string");
|
|
111
|
+
if (!!value) {
|
|
112
|
+
memo[key] = value;
|
|
113
|
+
curKey = null;
|
|
114
|
+
} else {
|
|
115
|
+
curKey = key;
|
|
116
|
+
memo[key] = true;
|
|
117
|
+
}
|
|
118
|
+
} else if (curKey) {
|
|
119
|
+
memo[curKey] = arg;
|
|
120
|
+
curKey = null;
|
|
121
|
+
} else {
|
|
122
|
+
if (!memo.__cmd) {
|
|
123
|
+
memo.__cmd = arg;
|
|
124
|
+
} else {
|
|
125
|
+
assert(false, "Only a single positional command is allowed");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return memo;
|
|
129
|
+
}), {});
|
|
130
|
+
assert(!!res && (typeof res === "object"), "Parsed arguments must form an object");
|
|
131
|
+
var res1;
|
|
132
|
+
res1 = {};
|
|
133
|
+
_ref = res;
|
|
134
|
+
for (key in _ref) {
|
|
135
|
+
value = _ref[key];
|
|
136
|
+
assert(typeof key === "string", "Argument keys must be strings");
|
|
137
|
+
if (key === "__cmd") {
|
|
138
|
+
res1.cmd = value;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (key.startsWith("set-")) {
|
|
142
|
+
res1.params = res1.params || {}
|
|
143
|
+
assert(key.substr(4).length > 0, "Set parameter name cannot be empty");
|
|
144
|
+
res1.params[key.substr(4)] = value;
|
|
145
|
+
} else {
|
|
146
|
+
res1[key] = value;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if ((res1.h || res1.help)) res1.help = true;
|
|
150
|
+
if ((res1.v || res1.version)) res1.version = true;
|
|
151
|
+
stop = res1.stop;
|
|
152
|
+
if ((!!stop && (stop !== "step" && stop !== "assistant"))) {
|
|
153
|
+
assert(typeof stop === "string", "Custom stop condition must be a string");
|
|
154
|
+
assert(stop.length > 0, "Custom stop condition cannot be empty");
|
|
155
|
+
res1.stop = (function(msgs) {
|
|
156
|
+
var lastMsg;
|
|
157
|
+
assert(Array.isArray(msgs), "Messages must be an array");
|
|
158
|
+
if (!msgs.length) return false;
|
|
159
|
+
var lastMsg;
|
|
160
|
+
lastMsg = msgs["slice"](-1)[0];
|
|
161
|
+
assert(!!lastMsg && (typeof lastMsg === "object"), "Last message must be an object");
|
|
162
|
+
if (!lastMsg.content) return false;
|
|
163
|
+
assert(typeof lastMsg.content === "string", "Message content must be a string");
|
|
164
|
+
return (-1 !== lastMsg.content.indexOf(stop));
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return res1;
|
|
168
|
+
}
|
|
169
|
+
parseArgs;
|
|
170
|
+
tune = require("../dist/tune.js");
|
|
171
|
+
rpc = require("../src/rpc.js");
|
|
172
|
+
path = require("path");
|
|
173
|
+
fs = require("fs");
|
|
174
|
+
os = require("os");
|
|
175
|
+
cp = require("child_process");
|
|
176
|
+
stream = require("stream");
|
|
177
|
+
|
|
178
|
+
function getHomedir(args) {
|
|
179
|
+
assert(!!args && (typeof args === "object"), "getHomedir expects args to be an object");
|
|
180
|
+
if (args.home) assert(typeof args.home === "string", "args.home must be a string");
|
|
181
|
+
return path.resolve(path.normalize((args.home || process.env.TUNE_HOME || "~/.tune")
|
|
182
|
+
.replace("~", os.homedir())));
|
|
183
|
+
}
|
|
184
|
+
getHomedir;
|
|
185
|
+
async function initConfig(args) {
|
|
186
|
+
var homedir, stdout, stderr, _ref, _i;
|
|
187
|
+
assert(!!args && (typeof args === "object"), "initConfig expects args to be an object");
|
|
188
|
+
var homedir;
|
|
189
|
+
homedir = getHomedir(args);
|
|
190
|
+
assert(typeof homedir === "string", "Home directory must be a string");
|
|
191
|
+
if ((!args.force && fs.existsSync(homedir))) return;
|
|
192
|
+
console.error("[tune-sdk] initialize " + homedir);
|
|
193
|
+
fs.mkdirSync(homedir, {
|
|
194
|
+
recursive: true
|
|
195
|
+
});
|
|
196
|
+
console.error("[tune-sdk] copying files");
|
|
197
|
+
fs.cpSync(path.resolve(__dirname, "../config"), path.resolve(homedir), { recursive: true });
|
|
198
|
+
console.error("[tune-sdk] installing npm");
|
|
199
|
+
try {
|
|
200
|
+
_ref = cp.execSync("npm i", {
|
|
201
|
+
cwd: homedir,
|
|
202
|
+
encoding: "utf8"
|
|
203
|
+
});
|
|
204
|
+
stdout = _ref;
|
|
205
|
+
if (stdout.trim()) console.error("[tune-sdk]", stdout.trim());
|
|
206
|
+
//stderr.trim() ? console.error("[tune-sdk]", stderr.trim()) : undefined;
|
|
207
|
+
} catch (err) {
|
|
208
|
+
console.error(err)
|
|
209
|
+
}
|
|
210
|
+
console.error("[tune-sdk] done");
|
|
211
|
+
console.error(`[tune-sdk] edit ${homedir}/.env and add OPENAI_KEY and other keys, change ${homedir}/default.ctx.js to customize tune`);
|
|
212
|
+
}
|
|
213
|
+
initConfig;
|
|
214
|
+
async function suggest(params, ctx) {
|
|
215
|
+
var node, _ref;
|
|
216
|
+
var node;
|
|
217
|
+
node = await ctx.resolve(params.query, {
|
|
218
|
+
output: "all",
|
|
219
|
+
match: "regex"
|
|
220
|
+
});
|
|
221
|
+
if (!node) {
|
|
222
|
+
_ref = [];
|
|
223
|
+
} else if (!Array.isArray(node)) {
|
|
224
|
+
_ref = Array(node);
|
|
225
|
+
} else {
|
|
226
|
+
_ref = node;
|
|
227
|
+
}
|
|
228
|
+
node = _ref;
|
|
229
|
+
return node.map((function(item) {
|
|
230
|
+
return {
|
|
231
|
+
name: item.name,
|
|
232
|
+
dirname: item.dirname,
|
|
233
|
+
source: item.source || item.fullname,
|
|
234
|
+
fullname: item.fullname,
|
|
235
|
+
basename: (item.fullname ? path.basename(item.fullname) : undefined),
|
|
236
|
+
type: item.type
|
|
237
|
+
}
|
|
238
|
+
}));
|
|
239
|
+
}
|
|
240
|
+
suggest;
|
|
241
|
+
async function remoteContext(name, params) {
|
|
242
|
+
var server, node;
|
|
243
|
+
var server;
|
|
244
|
+
server = this;
|
|
245
|
+
var node;
|
|
246
|
+
node = await server.resolve({
|
|
247
|
+
name: name,
|
|
248
|
+
params: params
|
|
249
|
+
});
|
|
250
|
+
if (!node) return
|
|
251
|
+
// backward compatible, remove in the future
|
|
252
|
+
if (node.error) return
|
|
253
|
+
|
|
254
|
+
node.read = (function() {
|
|
255
|
+
return server.read({
|
|
256
|
+
name: name,
|
|
257
|
+
params: params
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
node.exec = (function() {
|
|
261
|
+
return server.exec({
|
|
262
|
+
name: name,
|
|
263
|
+
params: params
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
return node;
|
|
267
|
+
}
|
|
268
|
+
remoteContext;
|
|
269
|
+
async function runRpc(args) {
|
|
270
|
+
var inStream, outStream, debugStream, ctx, server;
|
|
271
|
+
inStream = stream.Readable.toWeb(process.stdin);
|
|
272
|
+
outStream = stream.Writable.toWeb(process.stdout);
|
|
273
|
+
debugStream = ((typeof args.debug === "string") ? fs.createWriteStream(args.debug, {
|
|
274
|
+
flags: "a"
|
|
275
|
+
}) : undefined);
|
|
276
|
+
ctx = await initContext(args);
|
|
277
|
+
server = rpc.jsonrpc({
|
|
278
|
+
inStream: inStream,
|
|
279
|
+
outStream: outStream,
|
|
280
|
+
debug: ((typeof args.debug === "string") ? (function() {
|
|
281
|
+
var _i;
|
|
282
|
+
var args = 1 <= arguments.length ? [].slice.call(arguments, 0, _i = arguments.length - 0) : (_i = 0, []);
|
|
283
|
+
return debugStream.write(args.join(" ") + "\n", "utf8");
|
|
284
|
+
}) : true),
|
|
285
|
+
name: "server"
|
|
286
|
+
}, {
|
|
287
|
+
init: (async function(methods) {
|
|
288
|
+
if ((-1 !== methods.indexOf("resolve"))) ctx.use(remoteContext.bind(server));
|
|
289
|
+
return;
|
|
290
|
+
}),
|
|
291
|
+
file2run: (async function(params, stream) {
|
|
292
|
+
// backward compatibility
|
|
293
|
+
let payload = params
|
|
294
|
+
let args = {}
|
|
295
|
+
if (params.payload) {
|
|
296
|
+
payload = params.payload
|
|
297
|
+
args = params.params
|
|
298
|
+
}
|
|
299
|
+
return ctx.file2run({
|
|
300
|
+
...payload,
|
|
301
|
+
stream
|
|
302
|
+
}, args);
|
|
303
|
+
}),
|
|
304
|
+
suggest: (async function(params) {
|
|
305
|
+
return suggest(params, ctx);
|
|
306
|
+
})
|
|
307
|
+
});
|
|
308
|
+
// const node = await server.resolve("OLLAMA_URL")
|
|
309
|
+
// console.log("node", node)
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
runRpc;
|
|
313
|
+
async function run(args) {
|
|
314
|
+
var ctx, stop, params, res;
|
|
315
|
+
ctx = await initContext(args);
|
|
316
|
+
var stop;
|
|
317
|
+
stop = args.stop || "assistant";
|
|
318
|
+
var params;
|
|
319
|
+
params = args.params || {}
|
|
320
|
+
delete args.params;
|
|
321
|
+
var res;
|
|
322
|
+
res = await ctx.file2run(args, params);
|
|
323
|
+
return (!args.silent ? console.log(res) : undefined);
|
|
324
|
+
}
|
|
325
|
+
run;
|
|
326
|
+
|
|
327
|
+
function flatten(array) {
|
|
328
|
+
return array.reduce((memo, item) => {
|
|
329
|
+
if (Array.isArray(item)) {
|
|
330
|
+
return memo.concat(item)
|
|
331
|
+
}
|
|
332
|
+
memo.push(item)
|
|
333
|
+
return memo
|
|
334
|
+
}, [])
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async function initContext(args) {
|
|
338
|
+
var dirs, pwd, ctx, dir, ctxName, ext, module, m, _i, _ref, _len, _i0, _ref0, _len0;
|
|
339
|
+
var dirs;
|
|
340
|
+
var pwd;
|
|
341
|
+
dirs = [];
|
|
342
|
+
pwd = process.cwd();
|
|
343
|
+
if (args.path) dirs = args.path.split(path.delimiter)
|
|
344
|
+
.map((function(dir) {
|
|
345
|
+
return path.resolve(pwd, dir);
|
|
346
|
+
}));
|
|
347
|
+
dirs.push(getHomedir(args));
|
|
348
|
+
dirs.unshift(pwd);
|
|
349
|
+
process.env.TUNE_PATH = dirs.join(path.delimiter);
|
|
350
|
+
ctx = tune.makeContext({
|
|
351
|
+
TUNE_PATH: process.env.TUNE_PATH,
|
|
352
|
+
TUNE_HOME: getHomedir(args)
|
|
353
|
+
});
|
|
354
|
+
_ref = dirs;
|
|
355
|
+
for (_i = 0, _len = _ref.length; _i < _len; ++_i) {
|
|
356
|
+
dir = _ref[_i];
|
|
357
|
+
var ctxName;
|
|
358
|
+
ctxName = ["default.ctx.js", "default.ctx.cjs", "default.ctx.mjs"]
|
|
359
|
+
.map((function(name) {
|
|
360
|
+
return path.join(dir, name);
|
|
361
|
+
}))
|
|
362
|
+
.find((function(name) {
|
|
363
|
+
return fs.existsSync(name);
|
|
364
|
+
}));
|
|
365
|
+
if (!ctxName) continue;
|
|
366
|
+
var ext;
|
|
367
|
+
var module;
|
|
368
|
+
ext = path.extname(ctxName);
|
|
369
|
+
module = null;
|
|
370
|
+
if ((ext === ".js" || ext === ".cjs")) {
|
|
371
|
+
module = require(ctxName);
|
|
372
|
+
} else {
|
|
373
|
+
module = await import(ctxName);
|
|
374
|
+
module = module.default;
|
|
375
|
+
}
|
|
376
|
+
if ((typeof module === "function")) {
|
|
377
|
+
ctx.use(module);
|
|
378
|
+
} else if (Array.isArray(module)) {
|
|
379
|
+
_ref0 = module.flat(Infinity);
|
|
380
|
+
for (_i0 = 0, _len0 = _ref0.length; _i0 < _len0; ++_i0) {
|
|
381
|
+
m = _ref0[_i0];
|
|
382
|
+
if ((typeof m === "function")) {
|
|
383
|
+
ctx.use(m);
|
|
384
|
+
} else {
|
|
385
|
+
throw Error(tpl("err: Context file export is not an array of functions or function {name}: {module}", {
|
|
386
|
+
name: ctxName,
|
|
387
|
+
module: m
|
|
388
|
+
}));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
} else {
|
|
392
|
+
throw Error(tpl("err: Context file export is not an array of functions or function {name}: {module}", {
|
|
393
|
+
name: ctxName,
|
|
394
|
+
module: module
|
|
395
|
+
}));
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return ctx;
|
|
399
|
+
}
|
|
400
|
+
initContext;
|
|
401
|
+
async function main() {
|
|
402
|
+
var args, _ref;
|
|
403
|
+
try {
|
|
404
|
+
var args;
|
|
405
|
+
args = parseArgs(process.argv.slice(2));
|
|
406
|
+
if (args.help) {
|
|
407
|
+
showHelp();
|
|
408
|
+
process.exit(0);
|
|
409
|
+
}
|
|
410
|
+
if (args.version) {
|
|
411
|
+
try {
|
|
412
|
+
var pkg = require(path.resolve(__dirname, "../package.json"));
|
|
413
|
+
console.log(pkg.version || "0.0.0");
|
|
414
|
+
} catch (e) {
|
|
415
|
+
console.log("0.0.0");
|
|
416
|
+
}
|
|
417
|
+
process.exit(0);
|
|
418
|
+
}
|
|
419
|
+
validateArgs(args);
|
|
420
|
+
if (args.cmd === "rpc") {
|
|
421
|
+
await initConfig(args); // ensure config exists if needed
|
|
422
|
+
_ref = await runRpc(args);
|
|
423
|
+
} else if (args.cmd === "init") {
|
|
424
|
+
await initConfig(args);
|
|
425
|
+
_ref = null;
|
|
426
|
+
} else {
|
|
427
|
+
await initConfig(args); // auto-init if missing
|
|
428
|
+
_ref = await run(args);
|
|
429
|
+
}
|
|
430
|
+
} catch (e) {
|
|
431
|
+
console.error(e.stack);
|
|
432
|
+
_ref = process.exit(1);
|
|
433
|
+
}
|
|
434
|
+
return _ref;
|
|
435
|
+
}
|
|
436
|
+
main;
|
|
437
|
+
|
|
438
|
+
tpl;
|
|
439
|
+
exports.parseArgs = parseArgs;
|
|
440
|
+
exports.rpc = rpc;
|
|
441
|
+
exports.main = main;
|
|
442
|
+
exports.run = run;
|