tune-sdk 0.3.2 → 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.
package/README.md CHANGED
@@ -94,7 +94,7 @@ image generated
94
94
  ```
95
95
 
96
96
 
97
- ## CLI
97
+ ## Command Line
98
98
 
99
99
  ```bash
100
100
  # install tune globally
@@ -115,6 +115,44 @@ tune --set test="hello" --user "@test" --system "You are echo you print everytht
115
115
 
116
116
  ```
117
117
 
118
+ ### Static web server + context over WebSocket
119
+
120
+ Make simple web apps that share the same tools, files, and models available in Tune Chat. Tune Chat and the web app share the same context:
121
+
122
+ ```javascript
123
+ // Read files
124
+ await ctx.read("path/to/file")
125
+
126
+ // Write files
127
+ await ctx.write("path/to/file", content)
128
+
129
+ // Execute tools
130
+ let result = await ctx.exec("tool", { param: "value" })
131
+ // Note that `result` is always a string. If you expect JSON:
132
+ result = JSON.parse(result)
133
+
134
+ // Also render errors to the user, since you won’t be able to see and debug them otherwise.
135
+
136
+ // Call LLM
137
+ const result = await ctx.file2run({ user: "hi" })
138
+ ```
139
+
140
+ Create an app, e.g. `index.html`:
141
+
142
+ ```html
143
+ ...
144
+ <!-- Load the context into `window.ctx` -->
145
+ <script src="/contextws.js"></script>
146
+ ...
147
+ ```
148
+
149
+ Run the static web server from the folder:
150
+
151
+ ```bash
152
+ $ tune ws
153
+
154
+ listening on http://localhost:8080
155
+ ```
118
156
 
119
157
  ## Javascript SDK
120
158
  `npm install tune-sdk`
@@ -212,11 +250,32 @@ You can access tune manuals and available middlewares manuals from
212
250
  system:
213
251
  @man include all manuals for all connected packages
214
252
  @man/ - list all the manuals, like list directory
215
- @man/tune - get manual for tune core package
253
+ @man/tune-sdk - get manual for tune core package
216
254
  @man/tune-basic-toolset - get manual for tune-basic-tool set package
217
255
 
256
+ read any of it as a file
218
257
  tool_call: rf { "filename": "man/tune-basic-toolset"}
219
258
  tool_result:
220
259
  @man/tune-basic-toolset
260
+ ```
261
+
262
+ To connect man middleware in `default.ctx.js`:
221
263
 
264
+ ```javascript
265
+ const man = require('tune-sdk/man')
266
+
267
+ module.exports = [
268
+ ...
269
+ man(),
270
+ ...
271
+ ]
222
272
  ```
273
+
274
+ To expose `README.md` of your npm package as `man/<package-name>`, in your package/src/index.js add:
275
+ ```javascript
276
+ const man = require("tune-sdk/man");
277
+
278
+ // this method will read package.json and README.md
279
+ man.add(__dirname)
280
+ ````
281
+
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.2",
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",
package/src/man.js CHANGED
@@ -34,7 +34,7 @@ function read(man) {
34
34
  if (!fs.existsSync(filename)) {
35
35
  return `file ${filename} not found`
36
36
  }
37
- return fs.readFileSync(filename)
37
+ return fs.readFileSync(filename, "utf8")
38
38
  }
39
39
 
40
40
  module.exports = ({ mount } = {}) => {