tune-sdk 0.3.3 → 0.3.5

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 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.5",
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/contextws.js CHANGED
@@ -72,7 +72,6 @@ function ContextWebsocket(url, { debug } = {}) {
72
72
  assert(typeof url !== 'undefined', 'url is not set');
73
73
 
74
74
  socket = new WebSocket(url);
75
- console.log(socket)
76
75
 
77
76
  this.socket = socket;
78
77
  this.callbacks = Object.create(null);
@@ -105,7 +104,7 @@ ContextWebsocket.prototype._onClose = function () {
105
104
 
106
105
  ContextWebsocket.prototype._send = async function (payload) {
107
106
  const data = JSON.stringify(payload);
108
- this.debug('==>', data);
107
+ this.debug('tune ==>', data);
109
108
  await this.ready;
110
109
  this.socket.send(data);
111
110
  };
@@ -135,7 +134,7 @@ ContextWebsocket.prototype._onMessage = function (event) {
135
134
  return;
136
135
  }
137
136
 
138
- this.debug('<==', msg);
137
+ this.debug('tune <==', msg);
139
138
  if (!msg || !msg.id) return;
140
139
 
141
140
  const cb = this.callbacks[msg.id];
@@ -176,6 +175,7 @@ ContextWebsocket.prototype.read = function (name, binary) {
176
175
  return this._call('read', [name, binary], false);
177
176
  };
178
177
 
178
+
179
179
  ContextWebsocket.prototype.write = function (name, content) {
180
180
  return this._call('write', [name, content], false);
181
181
  };
@@ -197,5 +197,12 @@ if (typeof module !== 'undefined' && module.exports) {
197
197
  module.exports = ContextWebsocket;
198
198
  } else {
199
199
  window.ContextWebsocket = ContextWebsocket;
200
- window.ctx = new ContextWebsocket(window.location.origin.replace(/^http/, 'ws'));
200
+ window.ctx = new ContextWebsocket(window.location.origin.replace(/^http/, 'ws'), { debug: console.debug });
201
+ window.addEventListener('error', async (event) => {
202
+ window.ctx._call('log', [event.error?.stack || event.message ], false);
203
+ })
204
+ window.addEventListener('unhandledrejection', async (event) => {
205
+ const err = event.reason instanceof Error ? event.reason : new Error(String(event.reason));
206
+ window.ctx._call('log', [err.stack || err.message], false);
207
+ });
201
208
  }
package/src/ws.js CHANGED
@@ -1,9 +1,27 @@
1
1
  const http = require('http');
2
2
  const fs = require('fs');
3
3
  const path = require('path');
4
+ const os = require('os');
4
5
  const WebSocket = require('ws');
5
6
  const mime = require('mime-types');
6
7
 
8
+ // skips loopback, docker (172.x, 192.168.x docker ranges), and common vpn (tun/tap) interfaces
9
+ function getPublicIPv4() {
10
+ const ifaces = os.networkInterfaces();
11
+ const skipNames = /^(docker|br-|veth|tun|tap|vmnet|vbox)/i;
12
+ const skipRanges = /^(172\.(1[6-9]|2\d|3[01])\.|10\.)/; // docker bridge ranges
13
+
14
+ for (const [name, addrs] of Object.entries(ifaces)) {
15
+ if (skipNames.test(name)) continue;
16
+ for (const addr of addrs) {
17
+ if (addr.family === 'IPv4' && !addr.internal && !skipRanges.test(addr.address)) {
18
+ return addr.address;
19
+ }
20
+ }
21
+ }
22
+ return 'localhost';
23
+ }
24
+
7
25
  function makeServer({ port, static, root, ctx }) {
8
26
  root = path.join(root || process.cwd());
9
27
 
@@ -46,6 +64,7 @@ function makeServer({ port, static, root, ctx }) {
46
64
  let lctx = ctx.clone()
47
65
  let { id, method, args, ...rest } = JSON.parse(msg.toString());
48
66
  let result = { id }
67
+ console.log("[context]", method, ...args)
49
68
  if (method === "read") {
50
69
  const [ name, binary ] = args
51
70
  let content = await lctx.read(name, binary)
@@ -104,6 +123,9 @@ function makeServer({ port, static, root, ctx }) {
104
123
  }
105
124
  })()
106
125
  }
126
+ } else if (method === "log") {
127
+ console.log("[browser]:", ...args)
128
+ result.done = true
107
129
  } else {
108
130
  result.error = { message: `method not found ${method}`}
109
131
  }
@@ -115,7 +137,8 @@ function makeServer({ port, static, root, ctx }) {
115
137
  if (Number.isNaN(parseInt(port))) {
116
138
  console.log(`listening ${port}`);
117
139
  } else {
118
- console.log(`listening http://localhost:${port}`);
140
+ const host = getPublicIPv4();
141
+ console.log(`listening http://${host}:${port}`);
119
142
  }
120
143
  });
121
144
  }