tune-sdk 0.2.19 → 0.2.21
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/tune.js +101 -40
- package/package.json +1 -1
- package/src/cli.js +36 -6
package/dist/tune.js
CHANGED
|
@@ -2046,12 +2046,14 @@ TunePromise.prototype.finally = (function(onFinally) {
|
|
|
2046
2046
|
});
|
|
2047
2047
|
|
|
2048
2048
|
function text2run(text, ctx, opts) {
|
|
2049
|
-
var msgs, stopVal, stream, hookMsg, resolve, reject, p, iter;
|
|
2049
|
+
var msgs, stopVal, stream, hookMsg, errors, hookTurnEnd, resolve, reject, p, iter;
|
|
2050
2050
|
if (!ctx) throw Error("context not set");
|
|
2051
2051
|
var msgs;
|
|
2052
2052
|
var stopVal;
|
|
2053
2053
|
var stream;
|
|
2054
2054
|
var hookMsg;
|
|
2055
|
+
var errors;
|
|
2056
|
+
var hookTurnEnd;
|
|
2055
2057
|
var resolve;
|
|
2056
2058
|
var reject;
|
|
2057
2059
|
msgs = [];
|
|
@@ -2060,6 +2062,8 @@ function text2run(text, ctx, opts) {
|
|
|
2060
2062
|
hookMsg = (function(msg) {
|
|
2061
2063
|
return msg;
|
|
2062
2064
|
});
|
|
2065
|
+
errors = (((typeof opts !== "undefined") && (opts !== null) && !Number.isNaN(opts) && (typeof opts.errors !== "undefined") && (opts.errors !== null) && !Number.isNaN(opts.errors)) ? opts.errors : (((typeof "throw" !== "undefined") && ("throw" !== null) && !Number.isNaN("throw")) ? "throw" : undefined));
|
|
2066
|
+
hookTurnEnd = (((typeof opts !== "undefined") && (opts !== null) && !Number.isNaN(opts) && (typeof opts.hookTurnEnd !== "undefined") && (opts.hookTurnEnd !== null) && !Number.isNaN(opts.hookTurnEnd)) ? opts.hookTurnEnd : (((typeof(function() {}) !== "undefined") && ((function() {}) !== null) && !Number.isNaN((function() {}))) ? (function() {}) : undefined));
|
|
2063
2067
|
resolve = undefined;
|
|
2064
2068
|
reject = undefined;
|
|
2065
2069
|
var p;
|
|
@@ -2088,7 +2092,7 @@ function text2run(text, ctx, opts) {
|
|
|
2088
2092
|
}
|
|
2089
2093
|
stop;
|
|
2090
2094
|
async function doit() {
|
|
2091
|
-
var ast, payload, provider, model, res, ctype, err, reader, data, done, reData, reComment, usage;
|
|
2095
|
+
var ast, payload, provider, model, res, ctype, err, reader, data, done, reData, reComment, usage, _ref;
|
|
2092
2096
|
while (!stop(msgs)) {
|
|
2093
2097
|
var ast;
|
|
2094
2098
|
ast = await text2ast(text + "\n" + msg2text(msgs), ctx);
|
|
@@ -2115,6 +2119,7 @@ function text2run(text, ctx, opts) {
|
|
|
2115
2119
|
iter.result = {
|
|
2116
2120
|
value: msgs
|
|
2117
2121
|
};
|
|
2122
|
+
hookTurnEnd(msgs);
|
|
2118
2123
|
continue;
|
|
2119
2124
|
}
|
|
2120
2125
|
payload = await payload2http(payload, ctx);
|
|
@@ -2135,6 +2140,7 @@ function text2run(text, ctx, opts) {
|
|
|
2135
2140
|
value: msgs,
|
|
2136
2141
|
done: true
|
|
2137
2142
|
};
|
|
2143
|
+
hookTurnEnd(msgs);
|
|
2138
2144
|
continue;
|
|
2139
2145
|
}
|
|
2140
2146
|
var reader;
|
|
@@ -2181,7 +2187,7 @@ function text2run(text, ctx, opts) {
|
|
|
2181
2187
|
return JSON.parse(item);
|
|
2182
2188
|
}));
|
|
2183
2189
|
it = it.reduce((function(msg, chunk) {
|
|
2184
|
-
var delta, tc, tcIdx;
|
|
2190
|
+
var delta, tc, tcIdx, func;
|
|
2185
2191
|
if (chunk.usage) usage = chunk.usage;
|
|
2186
2192
|
var delta;
|
|
2187
2193
|
delta = (((typeof chunk !== "undefined") && (chunk !== null) && !Number.isNaN(chunk) && (typeof chunk.choices !== "undefined") && (chunk.choices !== null) && !Number.isNaN(chunk.choices) && (typeof chunk.choices[0] !== "undefined") && (chunk.choices[0] !== null) && !Number.isNaN(chunk.choices[0]) && (typeof chunk.choices[0].delta !== "undefined") && (chunk.choices[0].delta !== null) && !Number.isNaN(chunk.choices[0].delta)) ? chunk.choices[0].delta : (((typeof {} !== "undefined") && ({} !== null) && !Number.isNaN({})) ? {} : undefined));
|
|
@@ -2194,11 +2200,21 @@ function text2run(text, ctx, opts) {
|
|
|
2194
2200
|
if (delta.content) {
|
|
2195
2201
|
msg.content = msg.content || "";
|
|
2196
2202
|
msg.content += delta.content;
|
|
2197
|
-
}
|
|
2203
|
+
}
|
|
2204
|
+
if (delta.reasoning) {
|
|
2205
|
+
msg.reasoning = msg.reasoning || "";
|
|
2206
|
+
msg.reasoning += delta.reasoning;
|
|
2207
|
+
}
|
|
2208
|
+
if (delta.tool_calls) {
|
|
2198
2209
|
msg.tool_calls = msg.tool_calls || [];
|
|
2199
2210
|
tc = delta.tool_calls[0];
|
|
2200
2211
|
tcIdx = tc.index || 0;
|
|
2201
|
-
!msg.tool_calls[tcIdx]
|
|
2212
|
+
if (!msg.tool_calls[tcIdx]) {
|
|
2213
|
+
msg.tool_calls[tcIdx] = tc;
|
|
2214
|
+
} else {
|
|
2215
|
+
func = msg.tool_calls[tcIdx].function;
|
|
2216
|
+
func.arguments = (func.arguments || "") + (tc.function.arguments || "");
|
|
2217
|
+
}
|
|
2202
2218
|
}
|
|
2203
2219
|
return msg;
|
|
2204
2220
|
}), {
|
|
@@ -2213,18 +2229,25 @@ function text2run(text, ctx, opts) {
|
|
|
2213
2229
|
})(data += (((typeof res !== "undefined") && (res !== null) && !Number.isNaN(res) && (typeof res.value !== "undefined") && (res.value !== null) && !Number.isNaN(res.value)) ? res.value : (((typeof "" !== "undefined") && ("" !== null) && !Number.isNaN("")) ? "" : undefined)));
|
|
2214
2230
|
}
|
|
2215
2231
|
msgs = iter.result.value;
|
|
2232
|
+
hookTurnEnd(msgs);
|
|
2216
2233
|
if (usage) await ctx.usage(provider, model, usage);
|
|
2217
2234
|
}
|
|
2218
2235
|
}
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2236
|
+
if (stream) {
|
|
2237
|
+
iter.result = {
|
|
2238
|
+
value: msgs,
|
|
2239
|
+
done: true
|
|
2240
|
+
};
|
|
2241
|
+
_ref = hookTurnEnd(msgs);
|
|
2242
|
+
} else {
|
|
2243
|
+
_ref = resolve(msgs);
|
|
2244
|
+
}
|
|
2245
|
+
return _ref;
|
|
2223
2246
|
}
|
|
2224
2247
|
doit;
|
|
2225
2248
|
doit()
|
|
2226
2249
|
.catch((function(e) {
|
|
2227
|
-
var err;
|
|
2250
|
+
var err, _ref, _ref0;
|
|
2228
2251
|
var err;
|
|
2229
2252
|
err = e;
|
|
2230
2253
|
if ((e.name !== "TuneError")) {
|
|
@@ -2232,30 +2255,78 @@ function text2run(text, ctx, opts) {
|
|
|
2232
2255
|
err._stack = TuneError.ctx2stack(ctx);
|
|
2233
2256
|
err.error = e;
|
|
2234
2257
|
}
|
|
2235
|
-
|
|
2258
|
+
if ((errors === "throw")) {
|
|
2259
|
+
_ref0 = (stream ? (iter.err = err) : reject(err));
|
|
2260
|
+
} else {
|
|
2261
|
+
msgs.push({
|
|
2262
|
+
role: "error",
|
|
2263
|
+
content: err.stack
|
|
2264
|
+
});
|
|
2265
|
+
if (stream) {
|
|
2266
|
+
iter.result = {
|
|
2267
|
+
value: msgs,
|
|
2268
|
+
done: true
|
|
2269
|
+
};
|
|
2270
|
+
_ref = hookTurnEnd(msgs);
|
|
2271
|
+
} else {
|
|
2272
|
+
_ref = resolve(msgs);
|
|
2273
|
+
}
|
|
2274
|
+
_ref0 = _ref;
|
|
2275
|
+
}
|
|
2276
|
+
return _ref0;
|
|
2236
2277
|
}));
|
|
2237
2278
|
if (stream) resolve(iter);
|
|
2238
2279
|
return p;
|
|
2239
2280
|
}
|
|
2240
2281
|
text2run;
|
|
2241
2282
|
async function file2run(args, params, ctx) {
|
|
2242
|
-
var lctx, text, stop, node, response, res, r, chunk,
|
|
2283
|
+
var lctx, text, stop, errors, turnsSaved, longFormatRegex, isLong, initialText, node, response, res, r, chunk, itergeNE023, _ref;
|
|
2243
2284
|
var lctx;
|
|
2244
2285
|
lctx = ctx.clone();
|
|
2245
2286
|
if (params) lctx.ms.unshift(envmd(params));
|
|
2246
2287
|
var text;
|
|
2247
2288
|
var stop;
|
|
2289
|
+
var errors;
|
|
2290
|
+
var turnsSaved;
|
|
2248
2291
|
text = args.text;
|
|
2249
2292
|
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));
|
|
2293
|
+
errors = (((typeof args !== "undefined") && (args !== null) && !Number.isNaN(args) && (typeof args.errors !== "undefined") && (args.errors !== null) && !Number.isNaN(args.errors)) ? args.errors : (((typeof "throw" !== "undefined") && ("throw" !== null) && !Number.isNaN("throw")) ? "throw" : undefined));
|
|
2294
|
+
turnsSaved = 0;
|
|
2295
|
+
var longFormatRegex;
|
|
2296
|
+
var isLong;
|
|
2297
|
+
longFormatRegex = /^(system|user|tool_call|tool_result|assistant|error):/;
|
|
2298
|
+
isLong = false;
|
|
2299
|
+
async function save(res) {
|
|
2300
|
+
var _ref;
|
|
2301
|
+
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))) {
|
|
2302
|
+
if ((turnsSaved === res.length)) return;
|
|
2303
|
+
turnsSaved = res.length;
|
|
2304
|
+
_ref = await ctx.write(args.filename, text + "\n" + msg2text(res, isLong));
|
|
2305
|
+
} else {
|
|
2306
|
+
_ref = undefined;
|
|
2307
|
+
}
|
|
2308
|
+
return _ref;
|
|
2309
|
+
}
|
|
2310
|
+
save;
|
|
2311
|
+
var initialText;
|
|
2312
|
+
initialText = (args.system ? tpl("system:\n{system}", args) : "");
|
|
2250
2313
|
if (args.filename) {
|
|
2251
2314
|
node = await ctx.resolve(args.filename);
|
|
2252
|
-
|
|
2315
|
+
node ? lctx.stack.push(node) : lctx.stack.push({
|
|
2316
|
+
name: args.filename,
|
|
2317
|
+
type: "text",
|
|
2318
|
+
fullname: args.filename,
|
|
2319
|
+
read: (async function() {
|
|
2320
|
+
return initialText;
|
|
2321
|
+
})
|
|
2322
|
+
});
|
|
2253
2323
|
if ((node && !text)) text = await node.read();
|
|
2254
2324
|
}
|
|
2255
|
-
if ((!text && args.system)) text =
|
|
2325
|
+
if ((!text && args.system)) text = initialText;
|
|
2256
2326
|
text = text || "";
|
|
2257
|
-
if (args.user) text += tpl("
|
|
2327
|
+
if (args.user) text += ((text ? "\n" : "") + tpl("user:\n{user}", args));
|
|
2258
2328
|
if (!text) throw new TuneError("ether 'text' or 'system' or 'user' should be specified or 'filename' should exist ");
|
|
2329
|
+
isLong = longFormatRegex.test(text);
|
|
2259
2330
|
var response;
|
|
2260
2331
|
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));
|
|
2261
2332
|
|
|
@@ -2272,7 +2343,7 @@ async function file2run(args, params, ctx) {
|
|
|
2272
2343
|
_ref = res;
|
|
2273
2344
|
break;
|
|
2274
2345
|
case "chat":
|
|
2275
|
-
_ref = msg2text(res,
|
|
2346
|
+
_ref = msg2text(res, isLong);
|
|
2276
2347
|
break;
|
|
2277
2348
|
default:
|
|
2278
2349
|
_ref = undefined;
|
|
@@ -2281,54 +2352,44 @@ async function file2run(args, params, ctx) {
|
|
|
2281
2352
|
return _ref;
|
|
2282
2353
|
}
|
|
2283
2354
|
transformOutput;
|
|
2284
|
-
async function save() {
|
|
2285
|
-
var longFormatRegex, _ref;
|
|
2286
|
-
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))) {
|
|
2287
|
-
var longFormatRegex;
|
|
2288
|
-
longFormatRegex = /^(system|user|tool_call|tool_result|assistant|error):/;
|
|
2289
|
-
_ref = await ctx.write(args.filename, text + "\n" + msg2text(res, longFormatRegex.test(text)));
|
|
2290
|
-
} else {
|
|
2291
|
-
_ref = undefined;
|
|
2292
|
-
}
|
|
2293
|
-
return _ref;
|
|
2294
|
-
}
|
|
2295
|
-
save;
|
|
2296
2355
|
if (!args.stream) {
|
|
2297
2356
|
var res;
|
|
2298
2357
|
res = await lctx.text2run(text, {
|
|
2299
|
-
stop: stop
|
|
2358
|
+
stop: stop,
|
|
2359
|
+
errors: errors,
|
|
2360
|
+
hookTurnEnd: save
|
|
2300
2361
|
});
|
|
2301
|
-
await save();
|
|
2302
2362
|
_ref = transformOutput(res);
|
|
2303
2363
|
} else {
|
|
2304
2364
|
r = await lctx.text2run(text, {
|
|
2305
2365
|
stop: stop,
|
|
2306
|
-
|
|
2366
|
+
errors: errors,
|
|
2367
|
+
stream: true,
|
|
2368
|
+
hookTurnEnd: save
|
|
2307
2369
|
});
|
|
2308
2370
|
chunk = {};
|
|
2309
|
-
|
|
2371
|
+
itergeNE023 = new AsyncIter();
|
|
2310
2372
|
(async function($lastRes) {
|
|
2311
2373
|
var _ref;
|
|
2312
2374
|
try {
|
|
2313
2375
|
while (!chunk.done) {
|
|
2314
2376
|
chunk = await r.next();
|
|
2315
2377
|
res = (chunk.value || "");
|
|
2316
|
-
if (chunk.done) await save();
|
|
2317
2378
|
$lastRes = transformOutput(res) || $lastRes;
|
|
2318
|
-
|
|
2379
|
+
itergeNE023.result = {
|
|
2319
2380
|
value: $lastRes
|
|
2320
2381
|
}
|
|
2321
2382
|
}
|
|
2322
|
-
_ref =
|
|
2383
|
+
_ref = itergeNE023.result = {
|
|
2323
2384
|
value: $lastRes,
|
|
2324
2385
|
done: true
|
|
2325
2386
|
}
|
|
2326
2387
|
} catch (e) {
|
|
2327
|
-
_ref = (
|
|
2388
|
+
_ref = (itergeNE023.err = e);
|
|
2328
2389
|
}
|
|
2329
2390
|
return _ref;
|
|
2330
2391
|
})();
|
|
2331
|
-
_ref =
|
|
2392
|
+
_ref = itergeNE023;
|
|
2332
2393
|
}
|
|
2333
2394
|
return _ref;
|
|
2334
2395
|
}
|
|
@@ -2341,7 +2402,7 @@ function msg2text(msg, long) {
|
|
|
2341
2402
|
return (long ? tpl("{role}:{new_line}{content}", {
|
|
2342
2403
|
role: role,
|
|
2343
2404
|
content: content,
|
|
2344
|
-
new_line: ((role === "system" || role === "user" || role === "assistant" || role === "tool_result") ? "\n" : " ")
|
|
2405
|
+
new_line: ((role === "system" || role === "user" || role === "assistant" || role === "tool_result" || role === "error") ? "\n" : " ")
|
|
2345
2406
|
}) : tpl("{role}: {content}", {
|
|
2346
2407
|
role: $roles.long2short[role],
|
|
2347
2408
|
content: content
|
|
@@ -2454,7 +2515,7 @@ function msg2text(msg, long) {
|
|
|
2454
2515
|
_ref = mkline("comment", msg.content);
|
|
2455
2516
|
break;
|
|
2456
2517
|
case "error":
|
|
2457
|
-
_ref = mkline("error
|
|
2518
|
+
_ref = mkline("error", msg.content);
|
|
2458
2519
|
break;
|
|
2459
2520
|
default:
|
|
2460
2521
|
_ref = undefined;
|
|
@@ -2482,7 +2543,7 @@ msg2role;
|
|
|
2482
2543
|
function escape(text) {
|
|
2483
2544
|
return String((((typeof text !== "undefined") && (text !== null) && !Number.isNaN(text)) ? text : (((typeof "" !== "undefined") && ("" !== null) && !Number.isNaN("")) ? "" : undefined)))
|
|
2484
2545
|
.replace(/@/g, "\\@")
|
|
2485
|
-
.replace(/^(s|u|a|c|tr|tc|err|system|user|comment|assistant|tool_call|tool_result):/gm, "\\$1:");
|
|
2546
|
+
.replace(/^(s|u|a|c|tr|tc|err|system|user|comment|assistant|tool_call|tool_result|error):/gm, "\\$1:");
|
|
2486
2547
|
}
|
|
2487
2548
|
escape;
|
|
2488
2549
|
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -88,7 +88,7 @@ function validateArgs(args) {
|
|
|
88
88
|
assert(typeof args.cmd === "string", "Command must be a string");
|
|
89
89
|
assert((args.cmd === "rpc") || (args.cmd === "init"), "Unknown command: " + args.cmd);
|
|
90
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");
|
|
91
|
+
if ((!args.help && !args.version && !args.cmd && !args.user && !args.filename && !args.text)) assert(false, "Must specify --user, --filename, a command (rpc|init), --version, or --help");
|
|
92
92
|
return args;
|
|
93
93
|
}
|
|
94
94
|
validateArgs;
|
|
@@ -274,6 +274,7 @@ async function runRpc(args) {
|
|
|
274
274
|
flags: "a"
|
|
275
275
|
}) : undefined);
|
|
276
276
|
ctx = await initContext(args);
|
|
277
|
+
let cleanCtx = ctx.clone()
|
|
277
278
|
server = rpc.jsonrpc({
|
|
278
279
|
inStream: inStream,
|
|
279
280
|
outStream: outStream,
|
|
@@ -284,10 +285,35 @@ async function runRpc(args) {
|
|
|
284
285
|
}) : true),
|
|
285
286
|
name: "server"
|
|
286
287
|
}, {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
288
|
+
init: (async function(methods) {
|
|
289
|
+
if ((-1 !== methods.indexOf("resolve"))) ctx.use(remoteContext.bind(server));
|
|
290
|
+
return;
|
|
291
|
+
}),
|
|
292
|
+
resolve: async function(name, params) {
|
|
293
|
+
const node = await cleanCtx.resolve(name, params)
|
|
294
|
+
delete node.exec
|
|
295
|
+
delete node.read
|
|
296
|
+
return node
|
|
297
|
+
},
|
|
298
|
+
read: async function read(name) {
|
|
299
|
+
const node = await cleanCtx.resolve(name)
|
|
300
|
+
if (!node) {
|
|
301
|
+
return ""
|
|
302
|
+
}
|
|
303
|
+
// todo binary
|
|
304
|
+
return node.read()
|
|
305
|
+
},
|
|
306
|
+
|
|
307
|
+
write: async function write(name, content) {
|
|
308
|
+
return cleanCtx.write(name, content)
|
|
309
|
+
},
|
|
310
|
+
exec: async function exec(name, params) {
|
|
311
|
+
const node = await cleanCtx.resolve(name)
|
|
312
|
+
if (!node) {
|
|
313
|
+
throw Error(`cant execute ${name} - not found`)
|
|
314
|
+
}
|
|
315
|
+
return node.exec(params, cleanCtx)
|
|
316
|
+
},
|
|
291
317
|
file2run: (async function(params, stream) {
|
|
292
318
|
// backward compatibility
|
|
293
319
|
let payload = params
|
|
@@ -298,6 +324,7 @@ async function runRpc(args) {
|
|
|
298
324
|
}
|
|
299
325
|
return ctx.file2run({
|
|
300
326
|
...payload,
|
|
327
|
+
errors: "message",
|
|
301
328
|
stream
|
|
302
329
|
}, args);
|
|
303
330
|
}),
|
|
@@ -319,7 +346,7 @@ async function run(args) {
|
|
|
319
346
|
params = args.params || {}
|
|
320
347
|
delete args.params;
|
|
321
348
|
var res;
|
|
322
|
-
res = await ctx.file2run(args, params);
|
|
349
|
+
res = await ctx.file2run({ ...args, errors: "message", stop }, params);
|
|
323
350
|
return (!args.silent ? console.log(res) : undefined);
|
|
324
351
|
}
|
|
325
352
|
run;
|
|
@@ -346,6 +373,9 @@ async function initContext(args) {
|
|
|
346
373
|
}));
|
|
347
374
|
dirs.push(getHomedir(args));
|
|
348
375
|
dirs.unshift(pwd);
|
|
376
|
+
if (process.env.TUNE_PATH) {
|
|
377
|
+
dirs = dirs.concat(process.env.TUNE_PATH.split(path.delimiter))
|
|
378
|
+
}
|
|
349
379
|
process.env.TUNE_PATH = dirs.join(path.delimiter);
|
|
350
380
|
ctx = tune.makeContext({
|
|
351
381
|
TUNE_PATH: process.env.TUNE_PATH,
|