tune-sdk 0.3.3 → 0.3.4

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.
Files changed (2) hide show
  1. package/dist/tune.js +53 -22
  2. package/package.json +1 -1
package/dist/tune.js CHANGED
@@ -1219,7 +1219,7 @@ Context.prototype.exec = (async function(name, args) {
1219
1219
  }
1220
1220
  return _ref;
1221
1221
  });
1222
- Context.prototype.write = (async function(name, args) {
1222
+ Context.prototype.write = (async function(name, content, props) {
1223
1223
  var ws, cur, res, _res, _ref;
1224
1224
  ws = (this.ws || [])
1225
1225
  .slice();
@@ -1227,7 +1227,7 @@ Context.prototype.write = (async function(name, args) {
1227
1227
  cur = ws.shift();
1228
1228
  _res = [];
1229
1229
  while (cur) {
1230
- res = await cur.call(this, name, args, this);
1230
+ res = await cur.call(this, name, content, props);
1231
1231
  if (res) break;
1232
1232
  if (typeof(_ref = (cur = ws.shift())) !== 'undefined') _res.push(_ref);
1233
1233
  }
@@ -1730,11 +1730,16 @@ async function ast2payload(ast, ctx) {
1730
1730
  roles.push(lastRole);
1731
1731
 
1732
1732
  function transformRoles(memo, item, index, arr) {
1733
- var tcItem, lines, toolIndex, tc;
1734
- if ((item.role === "user" || item.role === "system" || item.role === "tool_call")) item.content = (Array.isArray(item.content) ? item.content.map((function(item) {
1735
- if (item.text) item.text = unescape(item.text);
1733
+ var escapeOptions, tcItem, lines, toolIndex, tc;
1734
+ var escapeOptions;
1735
+ escapeOptions = {
1736
+ vars: item.role === "user" || item.role === "system" || item.role === "tool_call",
1737
+ roles: true
1738
+ };
1739
+ item.content = (Array.isArray(item.content) ? item.content.map((function(item) {
1740
+ if (item.text) item.text = unescape(item.text, escapeOptions);
1736
1741
  return item;
1737
- })) : unescape(item.content));
1742
+ })) : unescape(item.content, escapeOptions));
1738
1743
 
1739
1744
  function findToolCalls() {
1740
1745
  var item1, _i, _ref, _len;
@@ -2018,7 +2023,10 @@ async function toolCall(payload, ctx) {
2018
2023
  transformRes;
2019
2024
  var content;
2020
2025
  content = transformRes(res);
2021
- if ((((typeof tools !== "undefined") && (tools !== null) && !Number.isNaN(tools) && (typeof tools[tc.name] !== "undefined") && (tools[tc.name] !== null) && !Number.isNaN(tools[tc.name]) && (typeof tools[tc.name].escapeOutput !== "undefined") && (tools[tc.name].escapeOutput !== null) && !Number.isNaN(tools[tc.name].escapeOutput)) ? tools[tc.name].escapeOutput : (((typeof true !== "undefined") && (true !== null) && !Number.isNaN(true)) ? true : undefined))) content = escape(content);
2026
+ if ((((typeof tools !== "undefined") && (tools !== null) && !Number.isNaN(tools) && (typeof tools[tc.name] !== "undefined") && (tools[tc.name] !== null) && !Number.isNaN(tools[tc.name]) && (typeof tools[tc.name].escapeOutput !== "undefined") && (tools[tc.name].escapeOutput !== null) && !Number.isNaN(tools[tc.name].escapeOutput)) ? tools[tc.name].escapeOutput : (((typeof true !== "undefined") && (true !== null) && !Number.isNaN(true)) ? true : undefined))) content = escape(content, {
2027
+ vars: true,
2028
+ roles: false
2029
+ });
2022
2030
  return {
2023
2031
  role: "tool",
2024
2032
  id: item.id,
@@ -2282,7 +2290,7 @@ function text2run(text, ctx, opts) {
2282
2290
  }
2283
2291
  text2run;
2284
2292
  async function file2run(args, params, ctx) {
2285
- var lctx, text, stop, errors, turnsSaved, node, longFormatRegex, isLong, initialText, response, res, r, chunk, itergMKZ8fG, _ref;
2293
+ var lctx, text, stop, errors, turnsSaved, node, longFormatRegex, isLong, initialText, response, res, r, chunk, iterg8EYRCS, _ref;
2286
2294
  var lctx;
2287
2295
  lctx = ctx.clone();
2288
2296
  if (params) lctx.ms.unshift(envmd(params));
@@ -2374,7 +2382,7 @@ async function file2run(args, params, ctx) {
2374
2382
  hookTurnEnd: save
2375
2383
  });
2376
2384
  chunk = {};
2377
- itergMKZ8fG = new AsyncIter();
2385
+ iterg8EYRCS = new AsyncIter();
2378
2386
  (async function($lastRes) {
2379
2387
  var _ref;
2380
2388
  try {
@@ -2382,20 +2390,20 @@ async function file2run(args, params, ctx) {
2382
2390
  chunk = await r.next();
2383
2391
  res = (chunk.value || "");
2384
2392
  $lastRes = transformOutput(res) || $lastRes;
2385
- itergMKZ8fG.result = {
2393
+ iterg8EYRCS.result = {
2386
2394
  value: $lastRes
2387
2395
  }
2388
2396
  }
2389
- _ref = itergMKZ8fG.result = {
2397
+ _ref = iterg8EYRCS.result = {
2390
2398
  value: $lastRes,
2391
2399
  done: true
2392
2400
  }
2393
2401
  } catch (e) {
2394
- _ref = (itergMKZ8fG.err = e);
2402
+ _ref = (iterg8EYRCS.err = e);
2395
2403
  }
2396
2404
  return _ref;
2397
2405
  })();
2398
- _ref = itergMKZ8fG;
2406
+ _ref = iterg8EYRCS;
2399
2407
  }
2400
2408
  return _ref;
2401
2409
  }
@@ -2405,6 +2413,10 @@ function msg2text(msg, long) {
2405
2413
  var _ref, _ref0, _ref1;
2406
2414
 
2407
2415
  function mkline(role, content) {
2416
+ content = escape(content, {
2417
+ vars: role === "user" || role === "system",
2418
+ roles: true
2419
+ });
2408
2420
  return (long ? tpl("{role}:{new_line}{content}", {
2409
2421
  role: role,
2410
2422
  content: content,
@@ -2503,7 +2515,9 @@ function msg2text(msg, long) {
2503
2515
  return tpl("{role}: {name}{ args}{\ntext}", {
2504
2516
  name: tc.function.name,
2505
2517
  args: args,
2506
- text: text,
2518
+ text: escape(text, {
2519
+ roles: true
2520
+ }),
2507
2521
  role: (long ? "tool_call" : "tc")
2508
2522
  });
2509
2523
  }))
@@ -2546,18 +2560,35 @@ function msg2role(msg) {
2546
2560
  }
2547
2561
  msg2role;
2548
2562
 
2549
- function escape(text) {
2550
- return String((((typeof text !== "undefined") && (text !== null) && !Number.isNaN(text)) ? text : (((typeof "" !== "undefined") && ("" !== null) && !Number.isNaN("")) ? "" : undefined)))
2551
- .replace(/@/g, "\\@")
2552
- .replace(/^(s|u|a|c|tr|tc|err|system|user|comment|assistant|tool_call|tool_result|error):/gm, "\\$1:");
2563
+ function escape(text, opts) {
2564
+ var result;
2565
+ var opts;
2566
+ var result;
2567
+ opts = opts || {
2568
+ vars: true,
2569
+ roles: true
2570
+ }
2571
+ result = String((((typeof text !== "undefined") && (text !== null) && !Number.isNaN(text)) ? text : (((typeof "" !== "undefined") && ("" !== null) && !Number.isNaN("")) ? "" : undefined)));
2572
+ if (opts.vars) result = result.replace(/@/g, "\\@");
2573
+ if (opts.roles) result = result.replace(/^(s|u|a|c|tr|tc|err|system|user|comment|assistant|tool_call|tool_result|error):/gm, "\\$1:");
2574
+ return result;
2553
2575
  }
2554
2576
  escape;
2555
2577
 
2556
- function unescape(text) {
2557
- return String((((typeof text !== "undefined") && (text !== null) && !Number.isNaN(text)) ? text : (((typeof "" !== "undefined") && ("" !== null) && !Number.isNaN("")) ? "" : undefined)))
2578
+ function unescape(text, opts) {
2579
+ var result;
2580
+ var opts;
2581
+ var result;
2582
+ opts = opts || {
2583
+ vars: true,
2584
+ roles: true
2585
+ }
2586
+ result = String((((typeof text !== "undefined") && (text !== null) && !Number.isNaN(text)) ? text : (((typeof "" !== "undefined") && ("" !== null) && !Number.isNaN("")) ? "" : undefined)));
2587
+ if (opts.vars) result = result
2558
2588
  .replace(/\\(?<item>@{1,2}\{\s*[~\.\-\w/]+\s*)(?<proc>(?:\s*\|\s*\w+(?:\s+\w+)*)*\})/g, "$<item>$<proc>")
2559
- .replace(/\\(?<item>@{1,2}[~\.\-\w/]+)/g, "$<item>")
2560
- .replace(/^\\(s|system|u|user|a|assistant|c|comment|tr|tool_result|tc|tool_call|err|error):/gm, "$1:");
2589
+ .replace(/\\(?<item>@{1,2}[~\.\-\w/]+)/g, "$<item>");
2590
+ if (opts.roles) result = result.replace(/^\\(s|system|u|user|a|assistant|c|comment|tr|tool_result|tc|tool_call|err|error):/gm, "$1:");
2591
+ return result;
2561
2592
  }
2562
2593
  unescape;
2563
2594
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tune-sdk",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "chat with llm in a text file. core package",
5
5
  "main": "dist/tune.js",
6
6
  "module": "dist/tune.mjs",