tune-sdk 0.3.4 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tune-sdk",
3
- "version": "0.3.4",
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
  }