@x-tech-laboratories/x-app 0.109.0 → 0.110.0

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
@@ -25,10 +25,11 @@ an app moves over by changing the package name in its imports and its `x-libs-*`
25
25
  | `csv` | CSV as RFC 4180 writes it: `parse_csv` into rows, a byte-order mark and blank rows dropped, `csv_records` by the header's names, `tsv_records` for unquoted tab-separated text such as LibraryThing's export, and `csv_field`, `write_csv` and `write_csv_records`, which quote only the cells that need it. Written in-house, as it is a page of code and the libraries add streaming and type guessing no app uses. |
26
26
  | `zip` | Several text files as one zip, such as an export of a few CSV tables: `zip_text_files`, `unzip_text_files` (folders left out, a file that is no zip refused in plain words) and `is_zip`, on fflate, chosen over JSZip because it works synchronously in a browser, on Node and in a phone's page with no dependencies, at a tenth the size. |
27
27
  | `text_index` | A local search index's text helpers, for SQLite FTS5 beside stored vectors: `text_hash` (cyrb53, to tell changed text, not for security), `chunk_text(text, size, overlap)` cut at a space near the end so a sentence across a cut is in both pieces, `unit`, `dot`, `to_blob` and `from_blob` for vectors kept as blobs, `fts_query(text, "AND" | "OR")` quoting each word as a prefix so none is read as an operator, and `fuse_ranks`, reciprocal rank fusion of several rankings. |
28
- | `html_text` | `html_to_text(html, base)`: a web page's title and readable text, a block to a line, scripts, styles and drawings left out, each link kept as plain text with its address made whole after it; `unescape_html` for character references. Runs anywhere, with no DOM. |
28
+ | `html_text` | `html_to_text(html, base)`: a web page's title and readable text, a block to a line, scripts, styles and drawings left out, each link kept as plain text with its address made whole after it, or with `{ blank_lines: true }` a blank line between blocks, as for a note's paragraphs; `unescape_html` for character references. Runs anywhere, with no DOM. |
29
29
  | `untrusted_text` | Text anyone could have written, such as another app's item or a fetched page, fenced before a model reads it: `untrusted_block` wraps it in `<untrusted-data>` with fence tags taken out, and `remove_instructions` blanks each sentence that gives the reader orders (`redacted_instruction`), keeping the facts around it. |
30
30
  | `repetition` | `is_repeating`: text going round in circles, such as a model stuck in a loop, told by its last 1200 characters packing to under a fifth (on fflate, so it runs in a page too); `repetition_watch()` checks streaming text every 200 characters. |
31
- | `endpoints` | An API endpoint a user points an app at: `is_local_endpoint` (loopback, private or link-local, and `localhost`), `host_of(endpoint, default_endpoint)` by host and port alone, `hide_keys`, `PlainError` and `PlainFailure` (a message with its raw `detail`), `ContextOverflow` and `overflow_of` for a request too large for the model, with the tokens counted when the server says, and `explain_endpoint_failure(error, where)`: a timeout, an unreachable server, a refused key, an unknown model or path, a rate limit or an overflow in plain words naming the `label` ("engine"), its `settings`, `key_name`, `serves`, `retry`, `rate_limit_advice` and `overflow_advice`; `keep_unknown` keeps a failure of the app's own as it is. |
31
+ | `endpoints` | An API endpoint a user points an app at: `is_local_endpoint` (loopback, private or link-local, and `localhost`), `is_loopback_address` (this machine's own alone: `localhost`, a name under `.localhost`, 127/8, `::1` or 127/8 mapped into IPv6), `host_of(endpoint, default_endpoint)` by host and port alone, `hide_keys`, `PlainError` and `PlainFailure` (a message with its raw `detail`), `ContextOverflow` and `overflow_of` for a request too large for the model, with the tokens counted when the server says, and `explain_endpoint_failure(error, where)`: a timeout, an unreachable server, a refused key, an unknown model or path, a rate limit or an overflow in plain words naming the `label` ("engine"), its `settings`, `key_name`, `serves`, `retry`, `rate_limit_advice` and `overflow_advice`; `keep_unknown` keeps a failure of the app's own as it is. |
32
+ | `mcp_servers` | The MCP servers a user adds, as a page and a server both see them, with nothing of Node: `McpServerTarget` (streamable HTTP at an address with an optional token, or a local command with its arguments and the variables passed), `McpTransport`, `McpToolRecord` (a listed tool with its read-only mark and input schema), `McpListing` (the tools and the server's instructions), and `sends_outside`, true unless the server is a local command or answers on a loopback address. `server/mcp_client` reaches them. |
32
33
  | `endpoint_models` | `list_endpoint_models(endpoint, api_key, signal, fetcher)`: the models an OpenAI-compatible endpoint lists at GET /models, each once, at most 500, with a llama.cpp router's load state (`state_of`), never throwing: a failure is no list and a `problem` in words that never hold the key; `models_of` reads an answer, `model_label` says "qwen3 (loaded)", and `may_load` whether the next request may wait while the server loads the model. |
33
34
  | `embeddings` | `embeddings_client(settings, { api_key, fetcher, words })`: any OpenAI-compatible embeddings endpoint, Ollama's on this computer unless set: `embed(texts)` one vector per text in order, checked against the `dimensions` set (768 unless set), `embed_query` with the `query_prefix` before it, `is_local`, the key asked for on each call, its failures thrown as `PlainFailure` through `explain_endpoint_failure`. |
34
35
  | `dates` | Local dates as `"2026-06-10"` or `"2026-06-10T09:30"`: reading, writing, adding days and months, overdue, and Yesterday/Today/Tomorrow labels; days and months as text, such as `today`, `shift_day`, `month_of`, `shift_month`, `months_from` and `format_month` for `"June 2026"`, the month taken from the text so a time zone never moves the 1st. |
@@ -127,7 +128,7 @@ an app moves over by changing the package name in its imports and its `x-libs-*`
127
128
  | `server/notify/*` | Notification channels, each a settings reader that throws in plain words and a sender that throws when delivery fails: `ntfy` (topic, priority by name or 1-5, tags, a link to open, token or name and password), `webhook` (JSON or a `{name}` template, as text when it is a string, with headers and a bearer token; `headers_file` names a JSON object of more headers, read at each send so a rotated secret needs no restart and in place of inline ones of the same name, a missing or malformed file failing that send with its path but never its contents), `mail` (SMTP on nodemailer, an optional peer, over STARTTLS, TLS or none), `apprise` (the `apprise` program, its URLs passed in the environment, never on its command line); `settings` reads a secret inline or from `<key>_file` and a setting given once or per kind. |
128
129
  | `server/notify/channels` | `read_channels`: the channels in a config list, each picked by its `type` (`ntfy`, `webhook`, `mail` or `apprise`) and read at once so a mistake stops the app starting, named by the list's key; `Notifier` sends a notification (title, body, level) to all of them, a failing one logged while the rest still get it; the level sets ntfy's priority and Apprise's type. |
129
130
  | `server/mcp` | `register_mcp_route`: the calls an `api_spec` describes, served as MCP tools over the official SDK's streamable HTTP (`@modelcontextprotocol/server`), each marked read-only or not, and destructive when its spec says `destructive: true`, as a call that deletes does, a call whose spec says `page_only: true` left out and a use the `page_only` rule names (from `page_only_calls`) refused, its arguments checked against the schema, its result as JSON text and a failure as a tool error in the call's own words; a read-only token runs only the tools that read, and a scoped one its scope's calls too; it sits behind the app's sign-in like any other API path. `route_tools` offers a REST app's routes the same way, running each route inside the app with the caller's headers and address, so it is signed in and checked exactly as the page's request is. |
130
- | `server/mcp_client` | `create_mcp_client({ name, version, limits })`: a client of the MCP servers a user adds (`McpServerTarget`), on the official SDK's client (`@modelcontextprotocol/client`): streamable HTTP with an optional bearer token, or a local command started without a shell with only the SDK's safe variables and those named (`passed_variables`); `list_tools` with the server's instructions (`instructions_of`) and each tool's read-only mark, `call_tool` answering in text (`result_text`), `forget` and `open_connections`; one connection per server kept while used and closed when idle, a server that exits, hangs or answers badly a plain error naming it and never its token. |
131
+ | `server/mcp_client` | `create_mcp_client({ name, version, limits })`: a client of the MCP servers a user adds (`McpServerTarget` from `mcp_servers`, whose shapes it also re-exports), on the official SDK's client (`@modelcontextprotocol/client`): streamable HTTP with an optional bearer token, or a local command started without a shell with only the SDK's safe variables and those named (`passed_variables`); `list_tools` with the server's instructions (`instructions_of`) and each tool's read-only mark, `call_tool` answering in text (`result_text`), `forget` and `open_connections`; one connection per server kept while used and closed when idle, a server that exits, hangs or answers badly a plain error naming it and never its token. |
131
132
  | `server/address_guard` | Which web addresses a server may fetch for its user or a program: `checked_web_address` (http or https, no name or password, port 80 or 443 unless `allowed_ports`, read from a setting by `ports_of`, with a `ports_hint`), `is_public_address` and `public_addresses`, refusing a host any of whose addresses is loopback, private, link-local or otherwise not public, IPv4 inside IPv6 judged as itself, and `guarded_lookup` for node:http's `lookup`, so a name that answers differently when connecting still reaches only checked addresses. |
132
133
  | `server/cors` | `register_open_cors`: any origin may call the API, as a native or mobile shell does from a page of its own, with the client id header allowed. |
133
134
  | `web/dom`, `web/format`, `web/router`, `web/theme` | The page's element builder and `cx` class joiner, formatters (including `compact_count`, such as `"1.4M"`, and `format_count`, such as `"60,000"`), hash router and theme choice, kept in this browser or the app's settings, and `follow_host_theme` for taking a host page's colours, such as the Linux sidebar's. |
@@ -1,4 +1,5 @@
1
1
  export declare function is_local_endpoint(endpoint: string): boolean;
2
+ export declare function is_loopback_address(address: string): boolean;
2
3
  export declare function host_of(endpoint: string, default_endpoint?: string): string;
3
4
  export interface PlainError {
4
5
  message: string;
package/dist/endpoints.js CHANGED
@@ -14,33 +14,41 @@ function ipv4_parts(host) {
14
14
  function is_local_ipv4([first, second]) {
15
15
  return first === 127 || first === 10 || (first === 172 && second >= 16 && second <= 31) || (first === 192 && second === 168) || (first === 169 && second === 254);
16
16
  }
17
+ // The IPv4 address mapped into an IPv6 one, written either way or as URL writes it (::ffff:c0a8:102 for 192.168.1.2),
18
+ // or null for any other.
19
+ function mapped_ipv4(bare) {
20
+ const mapped = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/.exec(bare);
21
+ if (mapped)
22
+ return ipv4_parts(mapped[1]);
23
+ const mapped_hex = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(bare);
24
+ if (!mapped_hex)
25
+ return null;
26
+ const [high, low] = [parseInt(mapped_hex[1], 16), parseInt(mapped_hex[2], 16)];
27
+ return [high >> 8, high & 255, low >> 8, low & 255];
28
+ }
17
29
  // Loopback (::1), unique local (fc00::/7), link-local (fe80::/10) or an IPv4 address mapped into IPv6.
18
- function is_local_ipv6(host) {
19
- const bare = host.replace(/^\[|\]$/g, "").toLowerCase();
30
+ function is_local_ipv6(bare) {
20
31
  if (bare === "::1")
21
32
  return true;
22
- const mapped = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/.exec(bare);
23
- if (mapped) {
24
- const parts = ipv4_parts(mapped[1]);
25
- return parts !== null && is_local_ipv4(parts);
26
- }
27
- // The same, as URL writes it: two groups of hex, such as ::ffff:c0a8:102 for 192.168.1.2.
28
- const mapped_hex = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(bare);
29
- if (mapped_hex) {
30
- const [high, low] = [parseInt(mapped_hex[1], 16), parseInt(mapped_hex[2], 16)];
31
- return is_local_ipv4([high >> 8, high & 255, low >> 8, low & 255]);
32
- }
33
+ const mapped = mapped_ipv4(bare);
34
+ if (mapped)
35
+ return is_local_ipv4(mapped);
33
36
  return /^f[cd][0-9a-f]{0,2}:/.test(bare) || /^fe[89ab][0-9a-f]?:/.test(bare);
34
37
  }
35
- // Whether the endpoint's host is loopback or a private-network address; an empty or unreadable endpoint is not.
36
- export function is_local_endpoint(endpoint) {
37
- let host;
38
+ // An address's host, lower case and without the brackets around IPv6, or null when it is no whole address.
39
+ function host_of_address(address) {
38
40
  try {
39
- host = new URL(endpoint.trim()).hostname.toLowerCase();
41
+ return new URL(address.trim()).hostname.replace(/^\[|\]$/g, "").toLowerCase();
40
42
  }
41
43
  catch {
42
- return false;
44
+ return null;
43
45
  }
46
+ }
47
+ // Whether the endpoint's host is loopback or a private-network address; an empty or unreadable endpoint is not.
48
+ export function is_local_endpoint(endpoint) {
49
+ const host = host_of_address(endpoint);
50
+ if (host === null)
51
+ return false;
44
52
  if (host === "localhost" || host.endsWith(".localhost"))
45
53
  return true;
46
54
  const parts = ipv4_parts(host);
@@ -48,6 +56,17 @@ export function is_local_endpoint(endpoint) {
48
56
  return is_local_ipv4(parts);
49
57
  return host.includes(":") && is_local_ipv6(host);
50
58
  }
59
+ // Whether an address is this machine's own: localhost, a name under .localhost, or a loopback address (127/8, ::1, or
60
+ // 127/8 mapped into IPv6); one on the local network is not.
61
+ export function is_loopback_address(address) {
62
+ const host = host_of_address(address);
63
+ if (host === null)
64
+ return false;
65
+ if (host === "localhost" || host.endsWith(".localhost") || host === "::1")
66
+ return true;
67
+ const parts = ipv4_parts(host) ?? (host.includes(":") ? mapped_ipv4(host) : null);
68
+ return parts !== null && parts[0] === 127;
69
+ }
51
70
  // The endpoint's host and port, never its path, user or password; the default endpoint when it is empty.
52
71
  export function host_of(endpoint, default_endpoint = "") {
53
72
  try {
@@ -3,4 +3,7 @@ export interface PageText {
3
3
  title: string;
4
4
  text: string;
5
5
  }
6
- export declare function html_to_text(html: string, base?: string): PageText;
6
+ export interface HtmlTextOptions {
7
+ blank_lines?: boolean;
8
+ }
9
+ export declare function html_to_text(html: string, base?: string, options?: HtmlTextOptions): PageText;
package/dist/html_text.js CHANGED
@@ -38,7 +38,7 @@ function link_address(href, base) {
38
38
  }
39
39
  }
40
40
  // The page's title and readable text; `base` is the page's address, which makes its relative links whole.
41
- export function html_to_text(html, base = "") {
41
+ export function html_to_text(html, base = "", options = {}) {
42
42
  const parts = [];
43
43
  const title_parts = [];
44
44
  let hidden = 0;
@@ -118,5 +118,5 @@ export function html_to_text(html, base = "") {
118
118
  const words = (text) => text.split(/\s+/).filter(Boolean).join(" ");
119
119
  const title = words(title_parts.join(" "));
120
120
  const lines = parts.join("").split(/\r\n|\r|\n/).map(words).filter(Boolean);
121
- return { title: Array.from(title).slice(0, 500).join(""), text: lines.join("\n") };
121
+ return { title: Array.from(title).slice(0, 500).join(""), text: lines.join(options.blank_lines ? "\n\n" : "\n") };
122
122
  }
@@ -0,0 +1,23 @@
1
+ import type { JsonSchema } from "./api_spec.ts";
2
+ export type McpTransport = "http" | "stdio";
3
+ export interface McpServerTarget {
4
+ id: string;
5
+ name: string;
6
+ transport: McpTransport;
7
+ address?: string;
8
+ token?: string;
9
+ command?: string;
10
+ args?: string[];
11
+ variables?: string[];
12
+ }
13
+ export interface McpToolRecord {
14
+ name: string;
15
+ description: string;
16
+ read_only: boolean;
17
+ input_schema: JsonSchema;
18
+ }
19
+ export interface McpListing {
20
+ tools: McpToolRecord[];
21
+ instructions: string;
22
+ }
23
+ export declare function sends_outside(server: Pick<McpServerTarget, "transport" | "address">): boolean;
@@ -0,0 +1,10 @@
1
+ // The MCP servers a user adds, as a page and a server both see them: each server, reached over streamable HTTP or
2
+ // started as a local command over stdio, the tools it lists, and whether its tools stay on this machine. It needs
3
+ // nothing of Node or a page, so it runs anywhere; `server/mcp_client` reaches the servers.
4
+ import { is_loopback_address } from "./endpoints.js";
5
+ // ── Where it runs ────────────────────────────────────────────────────────────
6
+ // A server's tools stay on this machine only when it is a local command or answers on a loopback address
7
+ // (`endpoints` `is_loopback_address`).
8
+ export function sends_outside(server) {
9
+ return server.transport === "http" && !is_loopback_address(server.address ?? "");
10
+ }
@@ -1,25 +1,6 @@
1
1
  import { type CallToolResult } from "@modelcontextprotocol/client";
2
- import type { JsonSchema } from "../api_spec.ts";
3
- export interface McpServerTarget {
4
- id: string;
5
- name: string;
6
- transport: "http" | "stdio";
7
- address?: string;
8
- token?: string;
9
- command?: string;
10
- args?: string[];
11
- variables?: string[];
12
- }
13
- export interface McpToolRecord {
14
- name: string;
15
- description: string;
16
- read_only: boolean;
17
- input_schema: JsonSchema;
18
- }
19
- export interface McpListing {
20
- tools: McpToolRecord[];
21
- instructions: string;
22
- }
2
+ import type { McpListing, McpServerTarget } from "../mcp_servers.ts";
3
+ export type { McpListing, McpServerTarget, McpToolRecord, McpTransport } from "../mcp_servers.ts";
23
4
  export interface McpLimits {
24
5
  connect_ms: number;
25
6
  call_ms: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-tech-laboratories/x-app",
3
- "version": "0.109.0",
3
+ "version": "0.110.0",
4
4
  "description": "Code the X-apps share: agents and their long runs, passwords and sign-in, the Fastify server pieces, durations, and the web page's helpers and widgets",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "repository": {
@@ -56,7 +56,7 @@
56
56
  "@shikijs/rehype": "^4.6.0",
57
57
  "@sqlite.org/sqlite-wasm": "3.53.4-build2",
58
58
  "@vitejs/plugin-react": "^6.2.1",
59
- "@x-tech-laboratories/halon": "^0.31.0 || ^0.32.0 || ^0.33.0 || ^0.34.0",
59
+ "@x-tech-laboratories/halon": "^0.31.0 || ^0.32.0 || ^0.33.0 || ^0.34.0 || ^0.35.0",
60
60
  "electron-vite": "^6.0.0",
61
61
  "fastify": "^5.12.7",
62
62
  "mermaid": "^11.0.0",