@cognolo/runtime 0.4.2
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/LICENSE +9 -0
- package/README.md +56 -0
- package/dist/agent.d.ts +290 -0
- package/dist/agent.js +245 -0
- package/dist/bundle.d.ts +192 -0
- package/dist/bundle.js +133 -0
- package/dist/cloud.d.ts +301 -0
- package/dist/cloud.js +383 -0
- package/dist/compact.d.ts +58 -0
- package/dist/compact.js +138 -0
- package/dist/context.d.ts +169 -0
- package/dist/context.js +144 -0
- package/dist/gate.d.ts +106 -0
- package/dist/gate.js +82 -0
- package/dist/guard.d.ts +15 -0
- package/dist/guard.js +26 -0
- package/dist/help.d.ts +44 -0
- package/dist/help.js +79 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +25 -0
- package/dist/init-cli.d.ts +40 -0
- package/dist/init-cli.js +317 -0
- package/dist/links.d.ts +39 -0
- package/dist/links.js +89 -0
- package/dist/look.d.ts +462 -0
- package/dist/look.js +362 -0
- package/dist/models.d.ts +79 -0
- package/dist/models.js +145 -0
- package/dist/outside.d.ts +102 -0
- package/dist/outside.js +204 -0
- package/dist/page.d.ts +59 -0
- package/dist/page.js +158 -0
- package/dist/passkey-browser.d.ts +40 -0
- package/dist/passkey-browser.js +48 -0
- package/dist/passkey.d.ts +64 -0
- package/dist/passkey.js +130 -0
- package/dist/pictures.d.ts +23 -0
- package/dist/pictures.js +58 -0
- package/dist/propose.d.ts +36 -0
- package/dist/propose.js +178 -0
- package/dist/rate.d.ts +97 -0
- package/dist/rate.js +147 -0
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.js +7 -0
- package/dist/react/labels.d.ts +167 -0
- package/dist/react/labels.js +114 -0
- package/dist/react/logic.d.ts +77 -0
- package/dist/react/logic.js +140 -0
- package/dist/react/page-view.d.ts +3 -0
- package/dist/react/page-view.js +308 -0
- package/dist/react/panel.d.ts +179 -0
- package/dist/react/panel.js +749 -0
- package/dist/react/use-chat.d.ts +96 -0
- package/dist/react/use-chat.js +252 -0
- package/dist/research.d.ts +109 -0
- package/dist/research.js +207 -0
- package/dist/route.d.ts +405 -0
- package/dist/route.js +1493 -0
- package/dist/search.d.ts +70 -0
- package/dist/search.js +112 -0
- package/dist/sensitive.d.ts +54 -0
- package/dist/sensitive.js +209 -0
- package/dist/setup.d.ts +93 -0
- package/dist/setup.js +658 -0
- package/dist/store-sql.d.ts +52 -0
- package/dist/store-sql.js +150 -0
- package/dist/store.d.ts +166 -0
- package/dist/store.js +201 -0
- package/dist/text.d.ts +9 -0
- package/dist/text.js +21 -0
- package/dist/tool.d.ts +66 -0
- package/dist/tool.js +36 -0
- package/dist/trust.d.ts +13 -0
- package/dist/trust.js +18 -0
- package/dist/version.d.ts +40 -0
- package/dist/version.js +133 -0
- package/dist/webmcp-browser.d.ts +20 -0
- package/dist/webmcp-browser.js +61 -0
- package/dist/workflow.d.ts +253 -0
- package/dist/workflow.js +310 -0
- package/docs/store.md +330 -0
- package/package.json +71 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { DEFAULT_LABELS, fill } from "./labels.js";
|
|
2
|
+
export function creditSeen(el) {
|
|
3
|
+
if (!el || !el.isConnected)
|
|
4
|
+
return false;
|
|
5
|
+
const r = el.getBoundingClientRect();
|
|
6
|
+
if (r.width < 8 || r.height < 8)
|
|
7
|
+
return false;
|
|
8
|
+
let opacity = 1;
|
|
9
|
+
for (let n = el; n; n = n.parentElement) {
|
|
10
|
+
const s = getComputedStyle(n);
|
|
11
|
+
if (s.display === "none")
|
|
12
|
+
return false;
|
|
13
|
+
if (n === el && s.visibility !== "visible")
|
|
14
|
+
return false;
|
|
15
|
+
opacity *= Number(s.opacity);
|
|
16
|
+
}
|
|
17
|
+
return opacity >= 0.3;
|
|
18
|
+
}
|
|
19
|
+
export function decisionsFor(ids, o) {
|
|
20
|
+
const out = {};
|
|
21
|
+
for (const id of ids) {
|
|
22
|
+
const mode = o.all ?? o.choice[id] ?? "decline";
|
|
23
|
+
out[id] = mode === "approve" && id in o.edits ? { edit: o.edits[id] } : mode;
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
export function draftOf(input) {
|
|
28
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
29
|
+
return null;
|
|
30
|
+
const d = {};
|
|
31
|
+
for (const [k, v] of Object.entries(input))
|
|
32
|
+
d[k] = typeof v === "string" || typeof v === "boolean" ? v : typeof v === "number" ? String(v) : JSON.stringify(v) ?? "";
|
|
33
|
+
return d;
|
|
34
|
+
}
|
|
35
|
+
export function editFrom(original, draft, text = DEFAULT_LABELS) {
|
|
36
|
+
const input = {};
|
|
37
|
+
for (const [k, was] of Object.entries(original)) {
|
|
38
|
+
const d = draft[k];
|
|
39
|
+
if (typeof was === "string")
|
|
40
|
+
input[k] = String(d);
|
|
41
|
+
else if (typeof was === "boolean")
|
|
42
|
+
input[k] = d === true || d === "true";
|
|
43
|
+
else if (typeof was === "number") {
|
|
44
|
+
if (typeof d !== "string" || d.trim() === "" || !Number.isFinite(Number(d)))
|
|
45
|
+
return { ok: false, error: fill(text.mustBeNumber, { field: k }) };
|
|
46
|
+
input[k] = Number(d);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
try {
|
|
50
|
+
input[k] = JSON.parse(String(d));
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return { ok: false, error: fill(text.notJson, { field: k }) };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { ok: true, input };
|
|
58
|
+
}
|
|
59
|
+
const PRODUCTION = /^(prod|production|live)$/i;
|
|
60
|
+
export const isProduction = (env) => PRODUCTION.test(env);
|
|
61
|
+
export const PAGE_THEME_ATTRS = ["class", "data-theme", "data-color-scheme", "data-color-mode", "data-bs-theme", "data-mode", "style"];
|
|
62
|
+
export function pageScheme(doc) {
|
|
63
|
+
for (const el of [doc.documentElement, doc.body]) {
|
|
64
|
+
if (!el)
|
|
65
|
+
continue;
|
|
66
|
+
if (el.classList.contains("dark"))
|
|
67
|
+
return "dark";
|
|
68
|
+
if (el.classList.contains("light"))
|
|
69
|
+
return "light";
|
|
70
|
+
for (const a of PAGE_THEME_ATTRS.slice(1, -1)) {
|
|
71
|
+
const v = (el.getAttribute(a) ?? "").toLowerCase();
|
|
72
|
+
if (/\bdark\b|\bnight\b/.test(v))
|
|
73
|
+
return "dark";
|
|
74
|
+
if (/\blight\b/.test(v))
|
|
75
|
+
return "light";
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const cs = (doc.defaultView?.getComputedStyle(doc.documentElement).colorScheme ?? "").trim();
|
|
79
|
+
return cs === "dark" || cs === "only dark" ? "dark" : cs === "light" || cs === "only light" ? "light" : null;
|
|
80
|
+
}
|
|
81
|
+
export const badgeText = (b, rules = "rules v{n}") => [b.env, b.tenant, b.rulesVersion != null ? rules.split("{n}").join(String(b.rulesVersion)) : null, b.commit ? b.commit.slice(0, 7) : null].filter(Boolean).join(" · ");
|
|
82
|
+
export const newerLook = (cur, next) => (cur && cur.version > next.version ? cur : next);
|
|
83
|
+
export { LAYOUTS } from "../look.js";
|
|
84
|
+
export const WINDOW_MIN = { w: 300, h: 320 };
|
|
85
|
+
const GAP = 8;
|
|
86
|
+
export function clampRect(r, vw, vh, side = "right") {
|
|
87
|
+
const num = (v) => (typeof v === "number" && Number.isFinite(v) ? v : undefined);
|
|
88
|
+
const maxW = Math.max(vw - GAP * 2, 0), maxH = Math.max(vh - GAP * 2, 0);
|
|
89
|
+
const w = Math.round(Math.min(Math.max(num(r?.w) ?? 420, Math.min(WINDOW_MIN.w, maxW)), maxW));
|
|
90
|
+
const h = Math.round(Math.min(Math.max(num(r?.h) ?? 600, Math.min(WINDOW_MIN.h, maxH)), maxH));
|
|
91
|
+
const dx = side === "left" ? 24 : vw - w - 24, dy = vh - h - 24;
|
|
92
|
+
const x = Math.round(Math.min(Math.max(num(r?.x) ?? dx, GAP), Math.max(vw - w - GAP, GAP)));
|
|
93
|
+
const y = Math.round(Math.min(Math.max(num(r?.y) ?? dy, GAP), Math.max(vh - h - GAP, GAP)));
|
|
94
|
+
return { x, y, w, h };
|
|
95
|
+
}
|
|
96
|
+
export const MAX_PICTURE_FILE = 20 * 1024 * 1024;
|
|
97
|
+
export async function shrinkPicture(file, longEdge = 1280, limit = 300 * 1024) {
|
|
98
|
+
if (!file.type.startsWith("image/") || file.type === "image/svg+xml")
|
|
99
|
+
return null;
|
|
100
|
+
if (file.size > MAX_PICTURE_FILE)
|
|
101
|
+
return null;
|
|
102
|
+
let bmp;
|
|
103
|
+
const big = file.size > 2 * 1024 * 1024;
|
|
104
|
+
try {
|
|
105
|
+
bmp = big ? await createImageBitmap(file, { resizeWidth: longEdge, resizeQuality: "high" }) : await createImageBitmap(file);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
try {
|
|
109
|
+
bmp = await createImageBitmap(file);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (big && bmp.height > longEdge) {
|
|
116
|
+
try {
|
|
117
|
+
const tall = await createImageBitmap(file, { resizeHeight: longEdge, resizeQuality: "high" });
|
|
118
|
+
bmp.close?.();
|
|
119
|
+
bmp = tall;
|
|
120
|
+
}
|
|
121
|
+
catch { }
|
|
122
|
+
}
|
|
123
|
+
const scale = Math.min(1, longEdge / Math.max(bmp.width, bmp.height));
|
|
124
|
+
const c = document.createElement("canvas");
|
|
125
|
+
c.width = Math.max(1, Math.round(bmp.width * scale));
|
|
126
|
+
c.height = Math.max(1, Math.round(bmp.height * scale));
|
|
127
|
+
const g = c.getContext("2d");
|
|
128
|
+
if (!g)
|
|
129
|
+
return null;
|
|
130
|
+
g.fillStyle = "#fff";
|
|
131
|
+
g.fillRect(0, 0, c.width, c.height);
|
|
132
|
+
g.drawImage(bmp, 0, 0, c.width, c.height);
|
|
133
|
+
bmp.close?.();
|
|
134
|
+
for (const q of [0.85, 0.75, 0.65, 0.55, 0.45, 0.35]) {
|
|
135
|
+
const url = c.toDataURL("image/jpeg", q);
|
|
136
|
+
if (url.length <= limit)
|
|
137
|
+
return url;
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { alreadyMasked, labelGuard, maskLabelled, maskTail, maskText, secretLabel } from "../sensitive.js";
|
|
2
|
+
const SKIP = new Set(["SCRIPT", "STYLE", "NOSCRIPT", "TEMPLATE", "SVG", "CANVAS", "IFRAME", "OBJECT", "EMBED", "VIDEO", "AUDIO", "HEAD", "META", "LINK", "DIALOG"]);
|
|
3
|
+
const BLOCK = new Set(["P", "DIV", "SECTION", "ARTICLE", "MAIN", "ASIDE", "HEADER", "FOOTER", "NAV", "LI", "UL", "OL", "FORM", "FIELDSET", "BR", "HR", "BLOCKQUOTE", "PRE", "FIGURE", "FIGCAPTION", "DD", "DT", "DL", "TR", "TD", "TH"]);
|
|
4
|
+
const MAX_ITEMS = 400, MAX_TEXT = 300, MAX_ROWS = 50, MAX_COLS = 12;
|
|
5
|
+
const NO_AUTOFILL = /(^|\s)(cc-|one-time-code|current-password|new-password)/;
|
|
6
|
+
const clean = (s) => (s ?? "").replace(/\s+/g, " ").trim().slice(0, MAX_TEXT);
|
|
7
|
+
const masked = (s) => maskText(s).text;
|
|
8
|
+
const guarded = (label, value, hint = "") => { const g = labelGuard(label) ?? (hint ? labelGuard(hint) : null); return !value || value === "(empty)" ? value : g === "hidden" ? "[hidden]" : g === "last4" ? (alreadyMasked(value) ? value : maskTail(value)) : maskLabelled(value); };
|
|
9
|
+
const hintOf = (el) => `${el.getAttribute("name") ?? ""} ${el.getAttribute("autocomplete") ?? ""} ${el.getAttribute("aria-label") ?? ""}`.trim();
|
|
10
|
+
function shown(el) {
|
|
11
|
+
if (el.hasAttribute("hidden") || el.getAttribute("aria-hidden") === "true" || el.hasAttribute("data-cognolo-hide") || el.hasAttribute("inert"))
|
|
12
|
+
return false;
|
|
13
|
+
const check = el.checkVisibility;
|
|
14
|
+
if (typeof check === "function")
|
|
15
|
+
return check.call(el, { opacityProperty: true, visibilityProperty: true, contentVisibilityAuto: true });
|
|
16
|
+
const view = el.ownerDocument?.defaultView;
|
|
17
|
+
if (!view)
|
|
18
|
+
return true;
|
|
19
|
+
const s = view.getComputedStyle(el);
|
|
20
|
+
return s.display !== "none" && s.visibility !== "hidden" && s.visibility !== "collapse" && s.opacity !== "0";
|
|
21
|
+
}
|
|
22
|
+
function readable(el) {
|
|
23
|
+
const type = (el.getAttribute("type") ?? "text").toLowerCase();
|
|
24
|
+
return !["password", "hidden", "file", "image", "reset"].includes(type) && !NO_AUTOFILL.test((el.getAttribute("autocomplete") ?? "").toLowerCase());
|
|
25
|
+
}
|
|
26
|
+
const fieldValue = (el) => {
|
|
27
|
+
const tag = el.tagName.toUpperCase(), type = (el.getAttribute("type") ?? "text").toLowerCase();
|
|
28
|
+
if (tag === "SELECT")
|
|
29
|
+
return clean([...el.selectedOptions ?? []].map((o) => o.textContent).join(", "));
|
|
30
|
+
if (type === "checkbox" || type === "radio")
|
|
31
|
+
return el.checked ? "on" : "off";
|
|
32
|
+
return clean(el.value);
|
|
33
|
+
};
|
|
34
|
+
const BUDGET = { elements: 20_000, ms: 100, chars: 24_000 };
|
|
35
|
+
const now = () => (typeof performance !== "undefined" ? performance.now() : Date.now());
|
|
36
|
+
let spent = { elements: 0, until: 0, chars: 0 };
|
|
37
|
+
const over = () => spent.elements > BUDGET.elements || spent.chars > BUDGET.chars || now() > spent.until;
|
|
38
|
+
function textOf(el, fields = true) {
|
|
39
|
+
let out = "";
|
|
40
|
+
const walk = (n) => {
|
|
41
|
+
if (out.length > MAX_TEXT * 2 || over())
|
|
42
|
+
return;
|
|
43
|
+
if (n.nodeType === 3) {
|
|
44
|
+
out += n.textContent ?? "";
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
spent.elements++;
|
|
48
|
+
if (n.nodeType !== 1)
|
|
49
|
+
return;
|
|
50
|
+
const e = n, tag = e.tagName.toUpperCase();
|
|
51
|
+
if (SKIP.has(tag) || !shown(e))
|
|
52
|
+
return;
|
|
53
|
+
if (tag === "INPUT" || tag === "SELECT" || tag === "TEXTAREA") {
|
|
54
|
+
if (fields && readable(e))
|
|
55
|
+
out += ` ${guarded(hintOf(e), fieldValue(e))} `;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (tag === "OPTION")
|
|
59
|
+
return;
|
|
60
|
+
out += " ";
|
|
61
|
+
for (const c of e.childNodes)
|
|
62
|
+
walk(c);
|
|
63
|
+
out += " ";
|
|
64
|
+
};
|
|
65
|
+
for (const c of el.childNodes)
|
|
66
|
+
walk(c);
|
|
67
|
+
return clean(out);
|
|
68
|
+
}
|
|
69
|
+
const shownWithin = (el, root) => { for (let n = el; n && n !== root; n = n.parentElement)
|
|
70
|
+
if (!shown(n))
|
|
71
|
+
return false; return true; };
|
|
72
|
+
export function snapshotPage(doc, skip = (el) => el.classList.contains("cog-root")) {
|
|
73
|
+
const items = [];
|
|
74
|
+
const keep = (it) => { spent.chars += JSON.stringify(it).length; items.push(it); };
|
|
75
|
+
let buf = "", cut = false;
|
|
76
|
+
let under = null;
|
|
77
|
+
let section = null;
|
|
78
|
+
const GENERIC = /(^|\s)(number|no\.?|#|id|code|codes|value|ref|reference|serial)\s*[:#.]?\s*$/i;
|
|
79
|
+
const short = (t) => t.split(" ").length <= 6 && !/[.!?]\s/.test(t);
|
|
80
|
+
const sectionFor = (label) => (section && (section === "hidden" || GENERIC.test(label)) ? section : null);
|
|
81
|
+
const hideOr4 = (g, v) => (g === "hidden" ? "[hidden]" : alreadyMasked(v) ? v : maskTail(v));
|
|
82
|
+
const field = (l, v, hint = "") => { const s = labelGuard(l) || (hint && labelGuard(hint)) ? null : sectionFor(l); return s && v && v !== "(empty)" ? hideOr4(s, v) : guarded(l, v, hint); };
|
|
83
|
+
const onlyLabel = (t) => (!/\d/.test(t) && !/[:=]\s*\S/.test(t) && t.split(" ").length <= 4 ? labelGuard(t) ?? sectionFor(t) : null);
|
|
84
|
+
const codeWord = (w) => /[\d_]|\p{L}-\p{L}/u.test(w);
|
|
85
|
+
const push = (it) => {
|
|
86
|
+
flushText();
|
|
87
|
+
if (it.k === "b" || it.k === "a") {
|
|
88
|
+
if (under && /\d/.test(it.t)) {
|
|
89
|
+
it = { ...it, t: hideOr4(under, it.t) };
|
|
90
|
+
under = null;
|
|
91
|
+
}
|
|
92
|
+
else if (section === "hidden")
|
|
93
|
+
it = { ...it, t: /\s/.test(it.t.trim()) ? it.t.split(" ").map((w) => (codeWord(w) ? "[hidden]" : w)).join(" ") : "[hidden]" };
|
|
94
|
+
}
|
|
95
|
+
else if (it.k === "h") {
|
|
96
|
+
const g = labelGuard(it.t);
|
|
97
|
+
section = g === "hidden" ? (secretLabel(it.t) ? "hidden" : null) : g;
|
|
98
|
+
under = section === "last4" ? "last4" : null;
|
|
99
|
+
}
|
|
100
|
+
else
|
|
101
|
+
under = null;
|
|
102
|
+
if (items.length < MAX_ITEMS)
|
|
103
|
+
keep(it);
|
|
104
|
+
else
|
|
105
|
+
cut = true;
|
|
106
|
+
};
|
|
107
|
+
const flushText = () => {
|
|
108
|
+
const t = clean(buf);
|
|
109
|
+
buf = "";
|
|
110
|
+
if (!t)
|
|
111
|
+
return;
|
|
112
|
+
if (items.length >= MAX_ITEMS) {
|
|
113
|
+
cut = true;
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const label = under === "hidden" || section === "hidden" ? null : onlyLabel(t);
|
|
117
|
+
if (label) {
|
|
118
|
+
under = label;
|
|
119
|
+
keep({ k: "t", t });
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (under && (under === "hidden" || /\d/.test(t))) {
|
|
123
|
+
keep({ k: "t", t: hideOr4(under, t) });
|
|
124
|
+
under = null;
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
keep({ k: "t", t: section === "hidden" && short(t) ? "[hidden]" : maskLabelled(t) });
|
|
128
|
+
};
|
|
129
|
+
const labelFor = (el) => {
|
|
130
|
+
const id = el.getAttribute("id");
|
|
131
|
+
const byFor = id && typeof CSS !== "undefined" && CSS.escape ? doc.querySelector(`label[for="${CSS.escape(id)}"]`) : null;
|
|
132
|
+
const by = el.getAttribute("aria-labelledby")?.split(/\s+/).map((x) => doc.getElementById(x)).filter((x) => !!x).map((x) => textOf(x, false)).join(" ");
|
|
133
|
+
const wrap = el.closest("label");
|
|
134
|
+
return clean(el.getAttribute("aria-label") || by || (byFor ? textOf(byFor, false) : "") || (wrap ? textOf(wrap, false) : "") || el.getAttribute("placeholder"));
|
|
135
|
+
};
|
|
136
|
+
const table = (el) => {
|
|
137
|
+
const all = el.rows ?? el.querySelectorAll("tr");
|
|
138
|
+
const own = [];
|
|
139
|
+
for (let i = 0; i < all.length && own.length < MAX_ROWS * 2 + 10; i++)
|
|
140
|
+
if (all[i].closest("table") === el)
|
|
141
|
+
own.push(all[i]);
|
|
142
|
+
if (all.length > own.length)
|
|
143
|
+
cut = true;
|
|
144
|
+
const cells = (r) => [...r.querySelectorAll(":scope > th, :scope > td")];
|
|
145
|
+
const width = (c) => Math.max(1, Math.min(MAX_COLS, c.colSpan || 1));
|
|
146
|
+
const spread = (cs, repeat) => cs.flatMap((c) => [c, ...Array.from({ length: width(c) - 1 }, () => (repeat ? c : null))]);
|
|
147
|
+
const headRows = own.filter((r) => r.parentElement?.tagName === "THEAD");
|
|
148
|
+
const headRow = headRows.at(-1) ?? (own[0] && cells(own[0]).every((c) => c.tagName === "TH") ? own[0] : undefined);
|
|
149
|
+
const headCells = headRow ? spread(cells(headRow), true) : [];
|
|
150
|
+
const keep = headCells.map((c) => shown(c));
|
|
151
|
+
const seen = new Map();
|
|
152
|
+
const cols = headCells.filter((_, i) => keep[i]).slice(0, MAX_COLS).map((c) => { const n = textOf(c, false) || "column"; const k = (seen.get(n) ?? 0) + 1; seen.set(n, k); return k > 1 ? `${n} ${k}` : n; });
|
|
153
|
+
const body = own.filter((r) => r !== headRow && !headRows.includes(r));
|
|
154
|
+
const gs = cols.map((n) => labelGuard(n) ?? sectionFor(n));
|
|
155
|
+
const allHead = [...headRows, ...(headRow && !headRows.includes(headRow) ? [headRow] : [])].flatMap(cells).map((c) => labelGuard(textOf(c, false)) ?? sectionFor(textOf(c, false)));
|
|
156
|
+
const anyHidden = allHead.includes("hidden") || gs.includes("hidden");
|
|
157
|
+
const loose = allHead.find((g) => g) ?? gs.find((g) => g) ?? null;
|
|
158
|
+
const rows = [];
|
|
159
|
+
for (const r of body) {
|
|
160
|
+
if (over()) {
|
|
161
|
+
cut = true;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
if (!shownWithin(r, el))
|
|
165
|
+
continue;
|
|
166
|
+
const cs = cells(r);
|
|
167
|
+
if (!headRow && cs.length === 2 && cs[0].tagName === "TH") {
|
|
168
|
+
const l = textOf(cs[0], false);
|
|
169
|
+
push({ k: "f", l, v: field(l, textOf(cs[1])) });
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (rows.length >= MAX_ROWS) {
|
|
173
|
+
cut = true;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
const laid = spread(cs, false).filter((_, i) => keep[i] ?? true).slice(0, MAX_COLS);
|
|
177
|
+
const text = (c) => (!c || !shown(c) ? "" : textOf(c));
|
|
178
|
+
if (section === "hidden") {
|
|
179
|
+
rows.push(laid.map((c) => (text(c) ? "[hidden]" : "")));
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (cols.length && loose && laid.length !== cols.length) {
|
|
183
|
+
rows.push(laid.map((c, i) => { const v = text(c); return !v ? "" : anyHidden ? "[hidden]" : gs[i] ? hideOr4(gs[i], v) : /\d/.test(v) ? hideOr4(loose, v) : maskLabelled(v); }));
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
let g = null;
|
|
187
|
+
rows.push(laid.map((c, i) => {
|
|
188
|
+
const v = text(c);
|
|
189
|
+
if (cols[i])
|
|
190
|
+
return gs[i] && v ? hideOr4(gs[i], v) : maskLabelled(v);
|
|
191
|
+
if (g) {
|
|
192
|
+
const out = v ? hideOr4(g, v) : v;
|
|
193
|
+
g = null;
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
const own = v && !/\d/.test(v) ? labelGuard(v) : null;
|
|
197
|
+
if (own) {
|
|
198
|
+
g = own;
|
|
199
|
+
return v;
|
|
200
|
+
}
|
|
201
|
+
return maskLabelled(v);
|
|
202
|
+
}));
|
|
203
|
+
}
|
|
204
|
+
if (rows.length || cols.length)
|
|
205
|
+
push({ k: "table", cols, rows });
|
|
206
|
+
};
|
|
207
|
+
const walk = (node) => {
|
|
208
|
+
if (items.length >= MAX_ITEMS || over()) {
|
|
209
|
+
cut = true;
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (node.nodeType === 3) {
|
|
213
|
+
buf += node.textContent ?? "";
|
|
214
|
+
if (buf.length > MAX_TEXT * 2)
|
|
215
|
+
flushText();
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (node.nodeType !== 1)
|
|
219
|
+
return;
|
|
220
|
+
spent.elements++;
|
|
221
|
+
const el = node;
|
|
222
|
+
const tag = el.tagName.toUpperCase();
|
|
223
|
+
if (SKIP.has(tag) || skip(el) || !shown(el))
|
|
224
|
+
return;
|
|
225
|
+
if (/^H[1-6]$/.test(tag) || el.getAttribute("role") === "heading") {
|
|
226
|
+
push({ k: "h", t: masked(textOf(el)) });
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (tag === "DETAILS" && !el.open) {
|
|
230
|
+
const sum = el.querySelector(":scope > summary");
|
|
231
|
+
push({ k: "c", t: (sum && shown(sum) ? masked(textOf(sum)) : "") || "Folded section" });
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (tag === "TABLE") {
|
|
235
|
+
table(el);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (tag === "DL") {
|
|
239
|
+
flushText();
|
|
240
|
+
let label = "";
|
|
241
|
+
for (const c of el.querySelectorAll("dt, dd")) {
|
|
242
|
+
if (over()) {
|
|
243
|
+
cut = true;
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
if (!shownWithin(c, el))
|
|
247
|
+
continue;
|
|
248
|
+
if (c.tagName === "DT")
|
|
249
|
+
label = textOf(c, false);
|
|
250
|
+
else if (label)
|
|
251
|
+
push({ k: "f", l: label, v: field(label, textOf(c)) || "(empty)" });
|
|
252
|
+
}
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") {
|
|
256
|
+
const type = (el.getAttribute("type") ?? "text").toLowerCase();
|
|
257
|
+
if (type === "submit" || type === "button") {
|
|
258
|
+
push({ k: "b", t: masked(clean(el.value)) });
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (!readable(el)) {
|
|
262
|
+
flushText();
|
|
263
|
+
under = null;
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const hint = `${el.getAttribute("name") ?? ""} ${el.getAttribute("autocomplete") ?? ""}`.trim();
|
|
267
|
+
const l = labelFor(el) || el.getAttribute("name") || "Field";
|
|
268
|
+
push({ k: "f", l, v: field(l, fieldValue(el), hint) || "(empty)" });
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (tag === "LABEL") {
|
|
272
|
+
const fields = el.querySelectorAll("input, select, textarea");
|
|
273
|
+
if (fields.length) {
|
|
274
|
+
for (const f of fields)
|
|
275
|
+
walk(f);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (el.hasAttribute("for"))
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (tag === "BUTTON" || el.getAttribute("role") === "button") {
|
|
282
|
+
const t = textOf(el, false) || clean(el.getAttribute("aria-label"));
|
|
283
|
+
if (t)
|
|
284
|
+
push({ k: "b", t: maskLabelled(t) });
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (tag === "A" && el.hasAttribute("href")) {
|
|
288
|
+
const t = textOf(el, false) || clean(el.getAttribute("aria-label"));
|
|
289
|
+
if (t)
|
|
290
|
+
push({ k: "a", t: maskLabelled(t) });
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const block = BLOCK.has(tag);
|
|
294
|
+
if (block)
|
|
295
|
+
flushText();
|
|
296
|
+
for (const c of el.childNodes)
|
|
297
|
+
walk(c);
|
|
298
|
+
if (block)
|
|
299
|
+
flushText();
|
|
300
|
+
else
|
|
301
|
+
buf += " ";
|
|
302
|
+
};
|
|
303
|
+
shown(doc.body);
|
|
304
|
+
spent = { elements: 0, until: now() + BUDGET.ms, chars: 0 };
|
|
305
|
+
walk(doc.body);
|
|
306
|
+
flushText();
|
|
307
|
+
return { title: masked(clean(doc.title)).slice(0, 120), items, ...(cut ? { cut: true } : {}) };
|
|
308
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { type CSSProperties, type MouseEvent as ReactMouseEvent, type ReactNode } from "react";
|
|
2
|
+
import type { Pending } from "../agent.js";
|
|
3
|
+
import type { TranscriptEntry } from "../route.js";
|
|
4
|
+
import { type Labels } from "./labels.js";
|
|
5
|
+
import { type Draft, type Layout, type Mode } from "./logic.js";
|
|
6
|
+
import { type Chat, type ChatOptions, type LookView } from "./use-chat.js";
|
|
7
|
+
/**
|
|
8
|
+
* Said on the card of a change the person's own AI asked for. Fixed, like the credit: not in `labels` or `render`, so it
|
|
9
|
+
* can't be worded away.
|
|
10
|
+
*/
|
|
11
|
+
export declare const OUTSIDE_ASKED = "Asked by your own AI, not by the assistant. Check it before you approve: it runs only with your passkey.";
|
|
12
|
+
import { type CloudLook } from "../look.js";
|
|
13
|
+
import { type LinkedRef } from "../links.js";
|
|
14
|
+
export type ColorScheme = "light" | "dark" | "auto" | "page";
|
|
15
|
+
export type ProposalEntry = Extract<TranscriptEntry, {
|
|
16
|
+
t: "proposal";
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* What a custom card for a proposal is given. `actions` only record the person's choice for this card: nothing is sent until
|
|
20
|
+
* they press the decision buttons under the conversation, which send every waiting change together (an unanswered one is
|
|
21
|
+
* declined) through the package's own decision logic. So a custom card cannot run a change by itself.
|
|
22
|
+
*/
|
|
23
|
+
export interface ProposalView {
|
|
24
|
+
entry: ProposalEntry;
|
|
25
|
+
/** The passkey prompt is open for this change right now. */
|
|
26
|
+
confirming?: boolean;
|
|
27
|
+
/** Approving this needs the person's passkey here (false where approving is still a click). */
|
|
28
|
+
needsPasskey?: boolean;
|
|
29
|
+
/** The proposed input, while the change is waiting. */
|
|
30
|
+
pending: Pending | undefined;
|
|
31
|
+
waiting: boolean;
|
|
32
|
+
/** What the person picked for this card so far. */
|
|
33
|
+
choice: Mode | undefined;
|
|
34
|
+
/** Whether the person changed it (and so it goes with their changes). */
|
|
35
|
+
edited: boolean;
|
|
36
|
+
busy: boolean;
|
|
37
|
+
actions: {
|
|
38
|
+
approve(): void;
|
|
39
|
+
decline(): void; /** Approve with this input instead. The runtime checks it again before running. */
|
|
40
|
+
edit(input: unknown): void;
|
|
41
|
+
};
|
|
42
|
+
labels: Labels;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Your own drawing for parts of the conversation. Return `undefined` to use the built-in one, `null` to show nothing. What you
|
|
46
|
+
* return is drawn as given: React escapes text, but do not put assistant or customer text into raw HTML.
|
|
47
|
+
*/
|
|
48
|
+
export interface Renderers {
|
|
49
|
+
message?(entry: Extract<TranscriptEntry, {
|
|
50
|
+
t: "user" | "assistant";
|
|
51
|
+
}>): ReactNode;
|
|
52
|
+
activity?(entry: Extract<TranscriptEntry, {
|
|
53
|
+
t: "activity" | "noted" | "refused";
|
|
54
|
+
}>): ReactNode;
|
|
55
|
+
proposal?(view: ProposalView): ReactNode;
|
|
56
|
+
/**
|
|
57
|
+
* Your own drawing for the bar under the conversation that sends the waiting changes. Its actions are the panel's own:
|
|
58
|
+
* they send through the same decision logic as the built-in buttons. Call them only from the person's own click: like any
|
|
59
|
+
* code in the app (which could post to its route too), a bar that calls them by itself sends decisions nobody made.
|
|
60
|
+
* Return nothing (or `null`, `false`, an empty string) for the built-in bar: a bar can't be left out, or the changes would wait for ever.
|
|
61
|
+
*/
|
|
62
|
+
decisionBar?(view: DecisionBarView): ReactNode;
|
|
63
|
+
}
|
|
64
|
+
/** What a custom decision bar gets: how many changes wait, and the panel's own ways to send the decision. */
|
|
65
|
+
export interface DecisionBarView {
|
|
66
|
+
count: number;
|
|
67
|
+
busy: boolean;
|
|
68
|
+
/** Approve every waiting change. */
|
|
69
|
+
approveAll(): void;
|
|
70
|
+
/** Send the choices made on each card (only once every waiting change has one: `canConfirm`). */
|
|
71
|
+
confirmChoices(): void;
|
|
72
|
+
canConfirm: boolean;
|
|
73
|
+
/** Decline every waiting change. */
|
|
74
|
+
declineAll(): void;
|
|
75
|
+
}
|
|
76
|
+
export declare const THEME_CSS: string;
|
|
77
|
+
/**
|
|
78
|
+
* The brand theme as CSS for `data-theme=brand`. Every value comes from `brandTheme`, which only takes a plain #rrggbb that
|
|
79
|
+
* isn't a status hue and gives back colours it worked out, so no cloud text reaches the style block.
|
|
80
|
+
*/
|
|
81
|
+
export declare function brandCss(look: Pick<CloudLook, "brand"> | null | undefined): string;
|
|
82
|
+
/** The owner's header colour as CSS (fill and black or white text), only a plain #rrggbb that isn't a status hue. */
|
|
83
|
+
export declare function headerCss(look: Pick<CloudLook, "header" | "headerColor" | "headerColorDark"> | null | undefined): string;
|
|
84
|
+
export declare function accentCss(look: Pick<CloudLook, "accent" | "accentDark" | "brand" | "theme" | "intensity" | "intensityDark"> | string | null | undefined, scheme?: ColorScheme): string;
|
|
85
|
+
export interface Look {
|
|
86
|
+
/**
|
|
87
|
+
* `page` (the default) follows the page's own light or dark mode and changes with it (a `dark` or `light` class, a
|
|
88
|
+
* `data-theme`-style attribute, or the page's `color-scheme`; light when the page says neither). `light` or `dark` fixes
|
|
89
|
+
* it; `auto` follows the visitor's system setting. The owner's choice in the cloud wins. The `--cognolo-*` variables still win.
|
|
90
|
+
*/
|
|
91
|
+
colorScheme?: ColorScheme;
|
|
92
|
+
/** For a page with a strict content-security policy: the nonce its `style-src` allows. */
|
|
93
|
+
nonce?: string;
|
|
94
|
+
/** On the outer element: set `--cognolo-*` variables here, or any layout of your own. */
|
|
95
|
+
className?: string;
|
|
96
|
+
style?: CSSProperties;
|
|
97
|
+
}
|
|
98
|
+
/** The panel's look: wraps what is inside in the style block and the colour scheme. `CognoloAssistant` does this itself; wrap a bare `ChatView` in it. */
|
|
99
|
+
export declare function CognoloTheme({ colorScheme, nonce, className, style, cloudLook, dir, children }: Look & {
|
|
100
|
+
cloudLook?: LookView | null; /** The text direction; left out, the page's own. */
|
|
101
|
+
dir?: "ltr" | "rtl";
|
|
102
|
+
children: ReactNode;
|
|
103
|
+
}): import("react").JSX.Element;
|
|
104
|
+
export interface PanelOptions {
|
|
105
|
+
title?: string;
|
|
106
|
+
/** Text shown in an empty conversation. */
|
|
107
|
+
intro?: ReactNode;
|
|
108
|
+
/** Buttons that send a first message. */
|
|
109
|
+
examples?: string[];
|
|
110
|
+
/** Which edge of the screen the panel is on. Default `right`, or `left` on a right-to-left page. */
|
|
111
|
+
side?: "left" | "right";
|
|
112
|
+
/** The text direction. Left out, the page's own (`dir` on `<html>`), so a right-to-left page gets a mirrored panel. */
|
|
113
|
+
dir?: "ltr" | "rtl";
|
|
114
|
+
/**
|
|
115
|
+
* Where the panel sits. `docked` (the default): full height on one side. `card`: a chat card in a bottom corner, about
|
|
116
|
+
* half the screen high. `window`: a window the person drags by its title bar and resizes from its corner, kept on
|
|
117
|
+
* screen, its place remembered in this browser only. All three fill the screen on a phone.
|
|
118
|
+
*/
|
|
119
|
+
layout?: Layout;
|
|
120
|
+
/** How wide it gets at most, such as `"32rem"` or `480` (pixels). Under a strict policy that blocks style attributes, set `--cognolo-width` in your own CSS instead. */
|
|
121
|
+
width?: string | number;
|
|
122
|
+
panelClassName?: string;
|
|
123
|
+
panelStyle?: CSSProperties;
|
|
124
|
+
/** Wording for everything the panel says by itself; what you leave out stays English. See `DEFAULT_LABELS`. */
|
|
125
|
+
labels?: Partial<Labels>;
|
|
126
|
+
/** Your own drawing for messages, activity lines and proposal cards. */
|
|
127
|
+
render?: Renderers;
|
|
128
|
+
/**
|
|
129
|
+
* Opens a record link inside your app (for example Next's `router.push`), so the page changes without a reload. Called
|
|
130
|
+
* only for a plain click; without it the browser follows the link.
|
|
131
|
+
*/
|
|
132
|
+
onNavigate?: (href: string) => void;
|
|
133
|
+
/** Text already in the message box when the panel opens (the person still sends it). */
|
|
134
|
+
draftMessage?: string;
|
|
135
|
+
}
|
|
136
|
+
export interface AssistantProps extends ChatOptions, Look, PanelOptions {
|
|
137
|
+
/** Label of the button that opens the panel. */
|
|
138
|
+
buttonLabel?: string;
|
|
139
|
+
/** The button's icon. Leave out for the default; `null` for none. */
|
|
140
|
+
icon?: ReactNode;
|
|
141
|
+
buttonClassName?: string;
|
|
142
|
+
/** Put the button at the bottom of the screen, on the same side as the panel. Off: it sits where you place the component, such as a top bar. */
|
|
143
|
+
floating?: boolean;
|
|
144
|
+
defaultOpen?: boolean;
|
|
145
|
+
}
|
|
146
|
+
export declare function CognoloAssistant({ buttonLabel, icon, buttonClassName, floating, defaultOpen, colorScheme, nonce, className, style, title, side: sideAsked, dir: dirAsked, ...rest }: AssistantProps): import("react").JSX.Element;
|
|
147
|
+
/**
|
|
148
|
+
* The panel itself, for a host that wants to place it (or open it) its own way. Give it the result of `useCognoloChat`, and
|
|
149
|
+
* wrap it in `CognoloTheme`. `autoFocus` moves focus to the message box when it appears. It is not a modal: the page behind
|
|
150
|
+
* stays usable, so focus is moved in but not locked in.
|
|
151
|
+
*/
|
|
152
|
+
export declare function ChatView({ chat, title, intro, examples, side, layout, width, panelClassName, panelStyle, labels, render, onNavigate, draftMessage, onClose, autoFocus }: PanelOptions & {
|
|
153
|
+
chat: Chat;
|
|
154
|
+
onClose?: () => void;
|
|
155
|
+
autoFocus?: boolean;
|
|
156
|
+
}): import("react").JSX.Element;
|
|
157
|
+
/** The built-in card for a proposal, which a custom `render.proposal` can also use inside its own wrapper. */
|
|
158
|
+
export declare function ProposalCard({ view }: {
|
|
159
|
+
view: ProposalView;
|
|
160
|
+
}): import("react").JSX.Element;
|
|
161
|
+
/** A reply as text to copy: the record marks ([[id]]) the panel turns into links are taken out. */
|
|
162
|
+
export declare const plainReply: (text: string) => string;
|
|
163
|
+
type Go = (ev: ReactMouseEvent<HTMLAnchorElement>, href: string) => void;
|
|
164
|
+
type Linkable = LinkedRef & {
|
|
165
|
+
href: string;
|
|
166
|
+
};
|
|
167
|
+
type RichOpts = {
|
|
168
|
+
refs?: Map<string, Linkable>;
|
|
169
|
+
go?: Go;
|
|
170
|
+
};
|
|
171
|
+
export declare function rich(text: string, o?: RichOpts): ReactNode[];
|
|
172
|
+
export declare function EditForm({ original, draft, L, onSave, onCancel }: {
|
|
173
|
+
original: Pending["input"];
|
|
174
|
+
draft: Draft;
|
|
175
|
+
L: Labels;
|
|
176
|
+
onSave: (input: unknown) => void;
|
|
177
|
+
onCancel: () => void;
|
|
178
|
+
}): import("react").JSX.Element;
|
|
179
|
+
export {};
|