tune-sdk 0.2.7 → 0.2.11

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 +59 -22
  2. package/package.json +1 -1
package/dist/tune.js CHANGED
@@ -1036,11 +1036,14 @@ function Context() {
1036
1036
  var _i;
1037
1037
  var args = 1 <= arguments.length ? [].slice.call(arguments, 0, _i = arguments.length - 0) : (_i = 0, []);
1038
1038
  this.ms = args.filter((function(item) {
1039
- return (item.name !== "write");
1039
+ return (item.name !== "write" && item.name !== "usage");
1040
1040
  }));
1041
1041
  this.ws = args.filter((function(item) {
1042
1042
  return (item.name === "write");
1043
1043
  }));
1044
+ this.us = args.filter((function(item) {
1045
+ return (item.name === "usage");
1046
+ }));
1044
1047
  this.stack = [];
1045
1048
  this.type = "Context";
1046
1049
  return this;
@@ -1052,19 +1055,27 @@ Context.prototype.clone = (function() {
1052
1055
  res = new Context();
1053
1056
  res.ms = this.ms.concat([]);
1054
1057
  res.ws = this.ws.concat([]);
1058
+ res.us = this.us.concat([]);
1055
1059
  res.stack = (this.stack || [])
1056
1060
  .concat([]);
1057
1061
  return res;
1058
1062
  });
1059
1063
  Context.prototype.use = (function(middleware) {
1060
- var _ref;
1064
+ var _ref, _ref0;
1061
1065
  if ((typeof middleware !== "function")) {
1062
- _ref = undefined;
1066
+ _ref0 = undefined;
1063
1067
  throw Error(tpl("middleware {} is not a function", ((typeof util !== 'undefined') ? util.inspect(middleware) : middleware)));
1064
1068
  } else {
1065
- _ref = ((middleware.name === "write") ? this.ws.push(middleware) : this.ms.push(middleware));
1069
+ if ((middleware.name === "write")) {
1070
+ _ref = this.ws.push(middleware);
1071
+ } else if (middleware.name === "usage") {
1072
+ _ref = this.us.push(middleware);
1073
+ } else {
1074
+ _ref = this.ms.push(middleware);
1075
+ }
1076
+ _ref0 = _ref;
1066
1077
  }
1067
- return _ref;
1078
+ return _ref0;
1068
1079
  });
1069
1080
  async function resolve(ctx, name, args, middlewares) {
1070
1081
  var i, output, match, type, result, res, md;
@@ -1181,6 +1192,20 @@ Context.prototype.write = (async function(name, args) {
1181
1192
  }
1182
1193
  return _res;
1183
1194
  });
1195
+ Context.prototype.usage = (async function(provider, model, usage) {
1196
+ var us, cur, res, _res;
1197
+ us = (this.us || [])
1198
+ .slice();
1199
+ if (!us.length) return;
1200
+ cur = us.shift();
1201
+ _res = [];
1202
+ while (cur) {
1203
+ res = await cur.call(this, provider, model, usage);
1204
+ cur = us.shift();
1205
+ if (typeof res !== 'undefined') _res.push(res);
1206
+ }
1207
+ return _res;
1208
+ });
1184
1209
 
1185
1210
  function envmd(md) {
1186
1211
  var lmd;
@@ -1441,7 +1466,7 @@ text2call;
1441
1466
  async function text2payload(text, ctx) {
1442
1467
  var ast;
1443
1468
  ast = await text2ast(text, ctx);
1444
- return await ast2payload(ast);
1469
+ return await ast2payload(ast, ctx);
1445
1470
  }
1446
1471
  text2payload;
1447
1472
  async function text2ast(text, ctx, resolve) {
@@ -1612,9 +1637,8 @@ async function text2ast(text, ctx, resolve) {
1612
1637
  return await parse(text, true, ctx);
1613
1638
  }
1614
1639
  text2ast;
1615
-
1616
- function ast2payload(ast) {
1617
- var payload, toolId, tools, llms, messages, roles, lastRole, lastChar;
1640
+ async function ast2payload(ast, ctx) {
1641
+ var payload, toolId, tools, llms, messages, roles, lastRole, lastChar, llm;
1618
1642
  var payload;
1619
1643
  var toolId;
1620
1644
  var tools;
@@ -1855,7 +1879,13 @@ function ast2payload(ast) {
1855
1879
  }))
1856
1880
  .reduce(transformRoles, Array());
1857
1881
  if (tools.length) payload.tools = tools;
1858
- if (llms.length) payload.llm = llms["slice"](-1)[0];
1882
+ var llm;
1883
+ llm;
1884
+ if (llms.length) llm = llms["slice"](-1)[0];
1885
+ if (!llm) llm = await ctx.resolve("default", {
1886
+ type: "llm"
1887
+ });
1888
+ payload.llm = llm;
1859
1889
  return payload;
1860
1890
  }
1861
1891
  ast2payload;
@@ -1864,9 +1894,6 @@ async function payload2http(payload, ctx) {
1864
1894
  var llm;
1865
1895
  llm = payload.llm;
1866
1896
  delete payload.llm;
1867
- if (!llm) llm = await ctx.resolve("default", {
1868
- type: "llm"
1869
- });
1870
1897
  if (!llm) {
1871
1898
  var stack;
1872
1899
  stack = TuneError.ctx2stack(ctx);
@@ -2001,12 +2028,16 @@ function text2run(text, ctx, opts) {
2001
2028
  }
2002
2029
  stop;
2003
2030
  async function doit() {
2004
- var ast, payload, res, ctype, err, reader, data, done, reData, reComment;
2031
+ var ast, payload, provider, model, res, ctype, err, reader, data, done, reData, reComment, usage;
2005
2032
  while (!stop(msgs)) {
2006
2033
  var ast;
2007
2034
  ast = await text2ast(text + "\n" + msg2text(msgs), ctx);
2008
2035
  var payload;
2009
2036
  payload = await ast2payload(ast, ctx);
2037
+ var provider;
2038
+ var model;
2039
+ provider = (((typeof payload !== "undefined") && (payload !== null) && !Number.isNaN(payload) && (typeof payload.llm !== "undefined") && (payload.llm !== null) && !Number.isNaN(payload.llm) && (typeof payload.llm.source !== "undefined") && (payload.llm.source !== null) && !Number.isNaN(payload.llm.source)) ? payload.llm.source : undefined);
2040
+ model = (((typeof payload !== "undefined") && (payload !== null) && !Number.isNaN(payload) && (typeof payload.llm !== "undefined") && (payload.llm !== null) && !Number.isNaN(payload.llm) && (typeof payload.llm.name !== "undefined") && (payload.llm.name !== null) && !Number.isNaN(payload.llm.name)) ? payload.llm.name : undefined);
2010
2041
  if (stream) payload.stream = stream;
2011
2042
  var res;
2012
2043
  res = await toolCall(payload, ctx);
@@ -2023,6 +2054,7 @@ function text2run(text, ctx, opts) {
2023
2054
  ctype = res.headers.get("content-type");
2024
2055
  if ((!stream || ctype.includes("application/json"))) {
2025
2056
  res = await res.json();
2057
+ if (res.usage) await ctx.usage(provider, model, res.usage);
2026
2058
  if (((((typeof res !== "undefined") && (res !== null) && !Number.isNaN(res) && (typeof res[0] !== "undefined") && (res[0] !== null) && !Number.isNaN(res[0]) && (typeof res[0].error !== "undefined") && (res[0].error !== null) && !Number.isNaN(res[0].error)) ? res[0].error : (((typeof res !== "undefined") && (res !== null) && !Number.isNaN(res) && (typeof res.error !== "undefined") && (res.error !== null) && !Number.isNaN(res.error)) ? res.error : undefined)) || (res.object === "error"))) {
2027
2059
  var err;
2028
2060
  err = new TuneError(tpl("{type: }{message}", (((typeof res !== "undefined") && (res !== null) && !Number.isNaN(res) && (typeof res[0] !== "undefined") && (res[0] !== null) && !Number.isNaN(res[0]) && (typeof res[0].error !== "undefined") && (res[0].error !== null) && !Number.isNaN(res[0].error)) ? res[0].error : (((typeof res !== "undefined") && (res !== null) && !Number.isNaN(res) && (typeof res.error !== "undefined") && (res.error !== null) && !Number.isNaN(res.error)) ? res.error : (((typeof res !== "undefined") && (res !== null) && !Number.isNaN(res)) ? res : undefined)))));
@@ -2041,6 +2073,7 @@ function text2run(text, ctx, opts) {
2041
2073
  var done;
2042
2074
  var reData;
2043
2075
  var reComment;
2076
+ var usage;
2044
2077
  reader = res.body
2045
2078
  .pipeThrough(new TextDecoderStream("utf8"))
2046
2079
  .getReader();
@@ -2048,6 +2081,7 @@ function text2run(text, ctx, opts) {
2048
2081
  done = false;
2049
2082
  reData = new RegExp("^data: (.*)");
2050
2083
  reComment = new RegExp("^:.*");
2084
+ usage = undefined;
2051
2085
  if (ctype.includes("text/event-stream")) {
2052
2086
  while (!done) {
2053
2087
  res = await reader.read();
@@ -2079,6 +2113,7 @@ function text2run(text, ctx, opts) {
2079
2113
  }));
2080
2114
  it = it.reduce((function(msg, chunk) {
2081
2115
  var delta, tc, tcIdx;
2116
+ if (chunk.usage) usage = chunk.usage;
2082
2117
  var delta;
2083
2118
  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));
2084
2119
  if ((((typeof chunk !== "undefined") && (chunk !== null) && !Number.isNaN(chunk) && (typeof chunk.error !== "undefined") && (chunk.error !== null) && !Number.isNaN(chunk.error)) ? chunk.error : undefined)) {
@@ -2110,6 +2145,7 @@ function text2run(text, ctx, opts) {
2110
2145
  })(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)));
2111
2146
  }
2112
2147
  msgs = iter.result.value;
2148
+ if (usage) await ctx.usage(provider, model, usage);
2113
2149
  }
2114
2150
  }
2115
2151
  return (stream ? (iter.result = {
@@ -2135,10 +2171,10 @@ function text2run(text, ctx, opts) {
2135
2171
  }
2136
2172
  text2run;
2137
2173
  async function file2run(args, params, ctx) {
2138
- var lctx, text, stop, node, response, res, r, chunk, itergVSO3pP, _ref;
2174
+ var lctx, text, stop, node, response, res, r, chunk, itergXy2XWO, _ref;
2139
2175
  var lctx;
2140
2176
  lctx = ctx.clone();
2141
- lctx.ms.unshift(envmd(params));
2177
+ if (params) lctx.ms.unshift(envmd(params));
2142
2178
  var text;
2143
2179
  var stop;
2144
2180
  text = args.text;
@@ -2147,10 +2183,11 @@ async function file2run(args, params, ctx) {
2147
2183
  node = await ctx.resolve(args.filename);
2148
2184
  if (node) lctx.stack.push(node);
2149
2185
  if ((node && !text)) text = await node.read();
2150
- if ((!node && !text)) throw new TuneError(tpl("'{}' not found", args.filename));
2151
2186
  }
2152
2187
  if ((!text && args.system)) text = tpl("system:\n{system}", args);
2188
+ text = text || "";
2153
2189
  if (args.user) text += tpl("\nuser:\n{user}", args);
2190
+ if (!text) throw new TuneError("ether 'text' or 'system' or 'user' should be specified or 'filename' should exist ");
2154
2191
  var response;
2155
2192
  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));
2156
2193
 
@@ -2201,7 +2238,7 @@ async function file2run(args, params, ctx) {
2201
2238
  stream: true
2202
2239
  });
2203
2240
  chunk = {};
2204
- itergVSO3pP = new AsyncIter();
2241
+ itergXy2XWO = new AsyncIter();
2205
2242
  (async function($lastRes) {
2206
2243
  var _ref;
2207
2244
  try {
@@ -2210,20 +2247,20 @@ async function file2run(args, params, ctx) {
2210
2247
  res = (chunk.value || "");
2211
2248
  if (chunk.done) await save();
2212
2249
  $lastRes = transformOutput(res) || $lastRes;
2213
- itergVSO3pP.result = {
2250
+ itergXy2XWO.result = {
2214
2251
  value: $lastRes
2215
2252
  }
2216
2253
  }
2217
- _ref = itergVSO3pP.result = {
2254
+ _ref = itergXy2XWO.result = {
2218
2255
  value: $lastRes,
2219
2256
  done: true
2220
2257
  }
2221
2258
  } catch (e) {
2222
- _ref = (itergVSO3pP.err = e);
2259
+ _ref = (itergXy2XWO.err = e);
2223
2260
  }
2224
2261
  return _ref;
2225
2262
  })();
2226
- _ref = itergVSO3pP;
2263
+ _ref = itergXy2XWO;
2227
2264
  }
2228
2265
  return _ref;
2229
2266
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tune-sdk",
3
- "version": "0.2.7",
3
+ "version": "0.2.11",
4
4
  "description": "tune - LLM chat in text file",
5
5
  "main": "dist/tune.js",
6
6
  "module": "dist/tune.mjs",