mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 05:57:09 +01:00
* feat(terminal): Loki tells you what to type, and typing it is one click
A person looking at the terminal did not know what to do: the agent said
"Please run /login", or they wanted Opus instead of the current model, and
nothing on the page said the answer is a command. The summary offered prose
tasks, and a command sent through Inject was wrapped in project context, so
"/model opus" reached the agent as a sentence about a model.
- config/agent-commands.ts is the one list of each CLI's own commands
(sign in, choose a model, free up memory, start fresh), in plain words.
- lib/terminal-commands.ts reads the screen: signed out, out of quota,
memory nearly full, or a menu waiting on a choice. TerminalActions shows
the sentence and a button that types the command or key into the session.
- The Loki panel knows the commands too. "What's going on?" and every Ask
carry them with the screen, and a step that is a command ("→ /model opus")
becomes a button that types it instead of an Inject draft.
- A draft that is exactly a known command is typed, never injected. "/" in
the Prompt box lists the agent's commands above the prompt library, and
the panel has the full list one click away.
- Clicking or tapping the read-only terminal switches to Type and puts the
cursor where keys go, so the screen can be worked in directly.
- Copy no longer says "tap" to people on a computer: gesture-neutral wording
where possible, usePressVerb() (and widget/press.ts) where the gesture is
the instruction, "pressed" when narrating what someone did.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* test(widget): the watch remark names the control as pressed, not tapped
The widget's narration now says "pressed", which is true on a phone and a
computer alike; this browser assertion still expected the old word.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
---------
Co-authored-by: Claude <noreply@anthropic.com>
299 lines
10 KiB
TypeScript
299 lines
10 KiB
TypeScript
/**
|
|
* Element picking: the full-viewport shield, the hover/selected highlights on
|
|
* HOST-page elements, and the pick bar. Owns the selection; the panel reads it
|
|
* back at submit time.
|
|
*/
|
|
import { h } from "./dom";
|
|
import { pressVerb } from "./press";
|
|
|
|
export type SelectedEl = { elementType: string; elementText: string; selector: string };
|
|
|
|
/**
|
|
* Prefer the thing the visitor meant (link/button/card) over decorative
|
|
* children (img/svg/path/rect). Empty elementText on bare <img> / <rect> made
|
|
* OrangeCat "make this clickable" reports useless for Dispatch fix.
|
|
*/
|
|
function resolvePickTarget(el: Element): Element {
|
|
const interactive = el.closest(
|
|
'a, button, [role="button"], [role="link"], summary, label, [tabindex]:not([tabindex="-1"])',
|
|
);
|
|
if (interactive instanceof Element) return interactive;
|
|
|
|
let cur: Element | null = el;
|
|
while (cur && /^(path|rect|circle|line|polyline|polygon|g)$/i.test(cur.tagName)) {
|
|
cur = cur.parentElement;
|
|
}
|
|
if (cur && cur.tagName.toLowerCase() === "svg") {
|
|
const wrap = cur.parentElement;
|
|
if (wrap && wrap !== document.body) return wrap;
|
|
return cur;
|
|
}
|
|
if (el.tagName.toLowerCase() === "img") {
|
|
const wrap = el.closest("a, button, figure, [role='button'], article, li");
|
|
if (wrap instanceof Element) return wrap;
|
|
}
|
|
return el;
|
|
}
|
|
|
|
function elementLabel(el: Element): string {
|
|
const aria = el.getAttribute("aria-label")?.trim();
|
|
if (aria) return aria.slice(0, 100);
|
|
const alt = el.getAttribute("alt")?.trim();
|
|
if (alt) return alt.slice(0, 100);
|
|
const title = el.getAttribute("title")?.trim();
|
|
if (title) return title.slice(0, 100);
|
|
const text = (el.textContent ?? "").replace(/\s+/g, " ").trim();
|
|
const tag = el.tagName.toLowerCase();
|
|
if (text) {
|
|
// A container's textContent is every word inside it run together
|
|
// ("coldstart-sep10-2339Started from Loki · …", seen live). Name it
|
|
// by its first heading, or the tag plus its opening words.
|
|
if (el.children.length > 0 && text.length > 60) {
|
|
const heading = el.querySelector("h1,h2,h3,h4,legend,summary,[role=heading]");
|
|
const head = heading?.textContent?.replace(/\s+/g, " ").trim();
|
|
return head
|
|
? `${tag}: ${head.slice(0, 60)}`
|
|
: `${tag}: \u201c${text.slice(0, 40)}\u2026\u201d`;
|
|
}
|
|
return text.slice(0, 100);
|
|
}
|
|
return `<${tag}>`;
|
|
}
|
|
|
|
/** id / data-testid first; else tag + up to 2 classes (skip fcw-*). */
|
|
function generateSelector(el: Element): string {
|
|
if (el.id) return `#${CSS.escape(el.id)}`;
|
|
const testId = el.getAttribute("data-testid");
|
|
if (testId) return `[data-testid="${CSS.escape(testId)}"]`;
|
|
const tag = el.tagName.toLowerCase();
|
|
const cls = (el.getAttribute("class") ?? "")
|
|
.split(/\s+/)
|
|
.filter((c) => c && !c.startsWith("fcw-"));
|
|
if (cls.length > 0)
|
|
return `${tag}.${cls
|
|
.slice(0, 2)
|
|
.map((c) => CSS.escape(c))
|
|
.join(".")}`;
|
|
return tag;
|
|
}
|
|
|
|
export type Picker = {
|
|
isPicking(): boolean;
|
|
start(): void;
|
|
stop(): void;
|
|
clearSelection(): void;
|
|
selected(): SelectedEl[];
|
|
};
|
|
|
|
export function createPicker(opts: {
|
|
root: ShadowRoot;
|
|
host: HTMLElement;
|
|
docStyle: HTMLStyleElement;
|
|
backdrop: HTMLElement;
|
|
panel: HTMLElement;
|
|
maxElements: number;
|
|
/** Cancel pressed — the panel resets its own scope before picking stops. */
|
|
onCancel: () => void;
|
|
/** Picking ended — the panel re-syncs its chips and refocuses. */
|
|
onStop: () => void;
|
|
}): Picker {
|
|
const { root, host, docStyle, backdrop, panel, maxElements } = opts;
|
|
let picking = false;
|
|
let selected: SelectedEl[] = [];
|
|
let selectedNodes: Element[] = [];
|
|
let hoverNode: Element | null = null;
|
|
|
|
// ---- element-pick bar ----
|
|
const pickbar = h("div", "pickbar");
|
|
const pickMsg = h("span", "msg");
|
|
const pickCount = h("span", "count");
|
|
const pickLbl = h("span", "lbl");
|
|
pickMsg.append(h("span", "dot"), pickCount, pickLbl);
|
|
const pickDone = h("button", "go", "Done");
|
|
pickDone.addEventListener("click", stopPicking);
|
|
// "This screen": what is on screen right now, marked as one — for the owner
|
|
// on a phone who sees a problem with the whole view, not one control. It
|
|
// picks the blocks visible in the viewport (so Loki gets their markup and
|
|
// where they sit), the way a screenshot would frame them.
|
|
const pickScreen = h("button", "ghost", "This screen");
|
|
pickScreen.type = "button";
|
|
pickScreen.title = "Mark everything on screen right now";
|
|
pickScreen.addEventListener("click", () => {
|
|
clearSelection();
|
|
for (const node of visibleBlocks(maxElements)) select(node);
|
|
syncPickbar();
|
|
if (selected.length) stopPicking();
|
|
});
|
|
const pickCancel = h("button", "ghost", "Cancel");
|
|
pickCancel.addEventListener("click", () => {
|
|
clearSelection();
|
|
opts.onCancel();
|
|
stopPicking();
|
|
});
|
|
pickbar.append(pickMsg, pickScreen, pickDone, pickCancel);
|
|
|
|
/**
|
|
* The blocks a screenshot of the viewport would show: landmarks and
|
|
* sections that intersect it, outermost first, descending into one that is
|
|
* much taller than the screen (a `main` holding the whole page says nothing
|
|
* about THIS screen). Capped, and never the widget's own host.
|
|
*/
|
|
function visibleBlocks(max: number): Element[] {
|
|
const vh = window.innerHeight;
|
|
const vw = window.innerWidth;
|
|
const BLOCKS = "header, nav, main, section, article, aside, footer, form, [role], h1, h2";
|
|
const inView = (r: DOMRect) => r.bottom > 0 && r.top < vh && r.right > 0 && r.left < vw;
|
|
const out: Element[] = [];
|
|
const walk = (parent: Element) => {
|
|
for (const el of Array.from(parent.children)) {
|
|
if (out.length >= max) return;
|
|
if (el === host || el.id === "loki-feedback-host") continue;
|
|
const r = el.getBoundingClientRect();
|
|
if (r.width === 0 || r.height === 0 || !inView(r)) continue;
|
|
const block = el.matches(BLOCKS);
|
|
if (block && r.height <= vh * 1.5) {
|
|
out.push(el);
|
|
continue;
|
|
}
|
|
walk(el);
|
|
}
|
|
};
|
|
walk(document.body);
|
|
return out;
|
|
}
|
|
|
|
function select(target: Element) {
|
|
const selector = generateSelector(target);
|
|
if (selected.some((s) => s.selector === selector) || selected.length >= maxElements) return;
|
|
selected.push({
|
|
elementType: target.tagName.toLowerCase(),
|
|
elementText: elementLabel(target),
|
|
selector,
|
|
});
|
|
selectedNodes.push(target);
|
|
target.classList.add("fcw-selected");
|
|
}
|
|
|
|
// Full-viewport shield: blocks host navigation/handlers during pick.
|
|
// Target resolution uses elementsFromPoint so we still hit the real DOM
|
|
// under the shield (capture-only listeners on the host page were not
|
|
// enough — some Next Link clicks still navigated mid-pick).
|
|
const pickShield = h("div");
|
|
pickShield.id = "fcw-pick-shield";
|
|
pickShield.setAttribute("aria-hidden", "true");
|
|
Object.assign(pickShield.style, {
|
|
position: "fixed",
|
|
inset: "0",
|
|
zIndex: "2147482999",
|
|
cursor: "crosshair",
|
|
background: "transparent",
|
|
} as CSSStyleDeclaration);
|
|
|
|
function targetUnderPoint(x: number, y: number): Element | null {
|
|
const stack = document.elementsFromPoint(x, y);
|
|
for (const node of stack) {
|
|
if (node === pickShield || node === host || host.contains(node)) continue;
|
|
if (node instanceof Element) return resolvePickTarget(node);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function onPickMove(e: MouseEvent) {
|
|
const target = targetUnderPoint(e.clientX, e.clientY);
|
|
if (!target) return;
|
|
if (hoverNode && hoverNode !== target) hoverNode.classList.remove("fcw-hover");
|
|
hoverNode = target;
|
|
target.classList.add("fcw-hover");
|
|
}
|
|
|
|
function onPickClick(e: MouseEvent) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
e.stopImmediatePropagation();
|
|
const target = targetUnderPoint(e.clientX, e.clientY);
|
|
if (!target) return;
|
|
const selector = generateSelector(target);
|
|
const idx = selected.findIndex((s) => s.selector === selector);
|
|
if (idx > -1) {
|
|
selected.splice(idx, 1);
|
|
selectedNodes[idx]?.classList.remove("fcw-selected");
|
|
selectedNodes.splice(idx, 1);
|
|
} else {
|
|
select(target);
|
|
}
|
|
syncPickbar();
|
|
}
|
|
|
|
function syncPickbar() {
|
|
if (selected.length === 0) {
|
|
// Short enough to survive a phone beside "This screen": the hint is
|
|
// the two buttons' names, not a sentence that ellipsises to "Tap w…".
|
|
pickCount.textContent = "Pick";
|
|
pickLbl.textContent = `${pressVerb().Verb} it, or`;
|
|
pickDone.disabled = true;
|
|
return;
|
|
}
|
|
const last = selected[selected.length - 1];
|
|
// One label, ellipsised by CSS — the old bar concatenated every
|
|
// selected element's text into a sentence that ran off the pill.
|
|
const label = (last.elementText || last.selector).replace(/\s+/g, " ").slice(0, 60);
|
|
pickCount.textContent = `${selected.length} selected`;
|
|
pickLbl.textContent =
|
|
selected.length === 1
|
|
? `${label} — ${pressVerb().verb} more, or Done`
|
|
: `last: ${label} — Done when ready`;
|
|
pickDone.disabled = false;
|
|
}
|
|
|
|
function startPicking() {
|
|
if (picking) return;
|
|
picking = true;
|
|
document.head.appendChild(docStyle);
|
|
backdrop.remove();
|
|
panel.remove();
|
|
// Shield lives in the shadow root under the pickbar so Done/Cancel stay
|
|
// clickable; fixed positioning still covers the host page viewport.
|
|
root.append(pickShield, pickbar);
|
|
syncPickbar();
|
|
pickShield.addEventListener("mousemove", onPickMove, true);
|
|
pickShield.addEventListener("click", onPickClick, true);
|
|
// Also swallow pointerdown so Next <Link> / button handlers never fire.
|
|
pickShield.addEventListener(
|
|
"pointerdown",
|
|
(e) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
},
|
|
true,
|
|
);
|
|
}
|
|
|
|
function stopPicking() {
|
|
if (!picking) return;
|
|
picking = false;
|
|
pickShield.removeEventListener("mousemove", onPickMove, true);
|
|
pickShield.removeEventListener("click", onPickClick, true);
|
|
pickShield.remove();
|
|
hoverNode?.classList.remove("fcw-hover");
|
|
hoverNode = null;
|
|
pickbar.remove();
|
|
root.append(backdrop, panel);
|
|
opts.onStop();
|
|
}
|
|
|
|
function clearSelection() {
|
|
for (const node of selectedNodes) node.classList.remove("fcw-selected");
|
|
selected = [];
|
|
selectedNodes = [];
|
|
docStyle.remove();
|
|
}
|
|
|
|
return {
|
|
isPicking: () => picking,
|
|
start: startPicking,
|
|
stop: stopPicking,
|
|
clearSelection,
|
|
selected: () => selected,
|
|
};
|
|
}
|