@tensor.chat/companion 0.2.1 → 0.3.1

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
@@ -38,7 +38,8 @@ It then appears as the folder `Devices/<name>/` in that chat's file tree.
38
38
  sandboxed — that is the point of a companion device. Git Bash is used on Windows when
39
39
  it is installed, PowerShell otherwise; `tensor-companion shell <path>` overrides it.
40
40
  A long-running command can be started as a background job and looked at later.
41
- - **Fetch** downloads a page or file through your connection and hands the text to the AI.
41
+ - **Fetch** downloads a page through your connection and hands it to tensor.chat as
42
+ served (up to 2 MB). The AI reads it as Markdown, or as served when it asks for that.
42
43
 
43
44
  ## Approval
44
45
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tensor.chat/companion",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "description": "Lets your tensor.chat AI use this machine: files, shell and internet through your own connection.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://tensor.chat",
package/src/daemon.js CHANGED
@@ -169,7 +169,7 @@ function createDaemon({ serverUrl, token, shell, log, approvedRoots, approvedShe
169
169
  case 'removepath': return await files.removepath(callRoots, a.path, { endgueltig: a.endgueltig === true });
170
170
  case 'renamepath': return files.renamepath(callRoots, a.from, a.to);
171
171
  case 'statpath': return files.statpath(callRoots, a.path);
172
- case 'fetch': return fetchUrl(a.url, { signal });
172
+ case 'fetch': return fetchUrl(a.url, { signal, raw: a.raw === true });
173
173
  default: return { ok: false, text: `(unknown tool "${tool}" on the companion device)` };
174
174
  }
175
175
  }
@@ -2,10 +2,36 @@
2
2
  // Fetches a URL through this machine's own internet connection and returns a text
3
3
  // view: HTML is reduced to its visible text, JSON is pretty-printed, everything else
4
4
  // is shown as text when it is text at all.
5
+ //
6
+ // With raw, the page goes back as served instead, for the server to convert or show
7
+ // (Fetch modes, tensorclaw/src/deviceFetch.js). A server that does not ask for raw
8
+ // gets the text view as before.
5
9
 
6
10
  const MAX_BYTES = 2 * 1024 * 1024;
7
11
  const MAX_CHARS = 50000;
8
12
  const TIMEOUT_MS = 20000;
13
+ // The hub takes a message of up to 4 MB, and JSON doubles every quote and backslash
14
+ // in a page: what goes back raw stays below this many bytes as JSON.
15
+ const RAW_JSON_BYTES = 3 * 1024 * 1024;
16
+
17
+ // The text in the charset the server named; utf-8 when it named none or one this
18
+ // runtime does not know.
19
+ function decode(buf, type) {
20
+ const m = /charset\s*=\s*"?([\w.:-]+)/i.exec(type);
21
+ try { return new TextDecoder(m ? m[1] : 'utf-8').decode(buf); } catch (_) { return buf.toString('utf8'); }
22
+ }
23
+
24
+ // The page goes in `page`, not `text`: the hub cuts a text at 200,000 characters.
25
+ function rawResult(res, url, type, buf, binary, truncated) {
26
+ const head = { ok: res.ok, raw: true, url: res.url || url.href, httpStatus: res.status, type, bytes: buf.length, text: '' };
27
+ if (binary) return { ...head, binary: true, truncated };
28
+ let page = decode(buf, type);
29
+ while (Buffer.byteLength(JSON.stringify(page)) > RAW_JSON_BYTES) {
30
+ page = page.slice(0, Math.floor(page.length * 0.9));
31
+ truncated = true;
32
+ }
33
+ return { ...head, truncated, page };
34
+ }
9
35
 
10
36
  function htmlToText(html) {
11
37
  return String(html)
@@ -21,7 +47,7 @@ function htmlToText(html) {
21
47
  .trim();
22
48
  }
23
49
 
24
- async function fetchUrl(rawUrl, { signal, timeoutMs } = {}) {
50
+ async function fetchUrl(rawUrl, { signal, timeoutMs, raw = false } = {}) {
25
51
  let url;
26
52
  try { url = new URL(String(rawUrl || '').trim()); } catch (_) { return { ok: false, text: '(invalid URL)' }; }
27
53
  if (url.protocol !== 'http:' && url.protocol !== 'https:') return { ok: false, text: '(only http and https URLs can be fetched)' };
@@ -50,7 +76,9 @@ async function fetchUrl(rawUrl, { signal, timeoutMs } = {}) {
50
76
  }
51
77
  }
52
78
  const buf = Buffer.concat(chunks.map((c) => Buffer.from(c)));
53
- if (buf.includes(0) && !/text|json|xml|html|javascript/.test(type)) {
79
+ const binary = buf.includes(0) && !/text|json|xml|html|javascript/.test(type);
80
+ if (raw) return rawResult(res, url, type, buf, binary, truncated);
81
+ if (binary) {
54
82
  return { ok: true, text: `${url.href}\nHTTP ${res.status} ${type || 'unknown type'}\n(binary content, ${size} bytes — not shown)` };
55
83
  }
56
84
  let body = buf.toString('utf8');
@@ -67,4 +95,4 @@ async function fetchUrl(rawUrl, { signal, timeoutMs } = {}) {
67
95
  }
68
96
  }
69
97
 
70
- module.exports = { fetchUrl, htmlToText, MAX_BYTES, MAX_CHARS };
98
+ module.exports = { fetchUrl, htmlToText, MAX_BYTES, MAX_CHARS, RAW_JSON_BYTES };
@@ -172,17 +172,26 @@ function walk(root, onEntry) {
172
172
  // A pattern written as the tree shows it ("Devices/Devbox/game/**", or just the device
173
173
  // folder "Devices/Devbox/**") narrows to that shared folder and continues relative to
174
174
  // it; anything else is matched relative to every shared folder, as before.
175
+ // Every folder's own name is tried before the device folder: the device folder is the
176
+ // start of every name, so tried in the same pass it caught "Devices/PC/VoiceType/*" at
177
+ // the first folder, and only the first shared folder could be searched (03.10.2026).
175
178
  function splitTreePattern(okRoots, pattern) {
176
179
  const p = pattern.replace(/\\/g, '/');
177
180
  const low = p.toLowerCase();
181
+ const rest = (prefix) => {
182
+ if (!prefix) return null;
183
+ const pre = prefix.toLowerCase();
184
+ if (low === pre) return '*';
185
+ return low.startsWith(pre + '/') ? p.slice(prefix.length + 1) || '*' : null;
186
+ };
178
187
  for (const r of okRoots) {
179
- if (!r.label) continue;
180
- const parent = r.label.slice(0, r.label.lastIndexOf('/'));
181
- for (const [prefix, only] of [[r.label, [r]], [parent, okRoots]]) {
182
- if (!prefix) continue;
183
- if (low === prefix.toLowerCase()) return { roots: only, pattern: '*' };
184
- if (low.startsWith(prefix.toLowerCase() + '/')) return { roots: only, pattern: p.slice(prefix.length + 1) || '*' };
185
- }
188
+ const after = r.label ? rest(r.label) : null;
189
+ if (after !== null) return { roots: [r], pattern: after };
190
+ }
191
+ for (const r of okRoots) {
192
+ const cut = r.label ? r.label.lastIndexOf('/') : -1;
193
+ const after = rest(cut > 0 ? r.label.slice(0, cut) : '');
194
+ if (after !== null) return { roots: okRoots, pattern: after };
186
195
  }
187
196
  return { roots: okRoots, pattern: p };
188
197
  }