mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-06 07:19:36 +01:00
* fix(prompts): one card per saved prompt, schedules in words, dialogs that say why
What the owner found on /prompts from an Android phone, item by item:
1. Saved prompts: saving the same name + words again no longer inserts a
row — POST /api/prompts answers with the prompt already saved
(existing: true); the form opens it, the default's save button says
"Already in your prompts". A card that stands for identical copies says
how many and offers "Remove N extra copies" — one explicit tap, the
caller's own rows only, the same soft delete as Delete
(DELETE /api/prompts/<id>/copies). Same name with different words says
"2 versions with this name, worded differently".
2. Filters: the scope filter is a <Segmented> (All / Any project / One
project) sitting with the category row on the list they both filter,
under "Loki's prompts". Category counts follow the scope. Filter chip
rows on a phone get a wider fade, snap to a chip's start and leave room
after the last chip, so the edge reads as "more" rather than a cut word.
3. New prompt: Create stays disabled until there is a name and a prompt,
and says what is missing beside it. Opening or closing the form never
saved anything; the copy glyph on every default card did, in one tap
with no label on a phone — it is now a labelled "save a copy" button.
4. Schedule: pick Every day / Weekdays / Every week / Every month, a day
and a time; Loki writes the cron (lib/cron-words), with "Write it as a
cron line instead" for experts. The schedule reads back in words with
the time zone it runs in ("Every Monday at 10:00, Zurich time
(Europe/Zurich)") and a link to change it; the zone is sent with the
job. The API refuses a cron line the scheduler cannot read, in words.
Create is the primary button and says why when it cannot be pressed.
5. Run: a prompt that names {{project_name}} asks for a project whatever
its scope (a saved copy is global), defaulting to the project in use;
the prompt is shown only once complete. Each action says in one line
who does the work; Send to terminal works for any of your projects
(tab-inject starts or queues the session) and says why when it cannot.
Project pickers list projectTitle(), not folder keys.
6. Category chips are neutral (ui-tag-neutral); the ui-cat-* rainbow and
the featured star's amber are gone. "control" was a second id for the
fleet prompts — one category now, "Fleet control".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(voice): spoken sentences start with a capital and name a repeated project once
The briefing read "one run is waiting for a builder: loki ... three runs
failed: loki, loki and loki". Sentences now open with a capital, and the
runs' projects are said once each: "Three runs failed on loki.", "Four runs
failed: loki three times and heidi once." The same for runs waiting for a
builder, agents working, and the announcements between turns.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(voice): say why the studio voice is off, with the link that fixes it
POST /api/voice/speak answered 502 with Groq's reason only in the server
console, and the phone fell back to its own voice in silence, so every voice
in the picker sounded the same. Groq's refusal is now classified
(lib/voice/tts-failure: terms gate, bad key, limit, model gone, vendor down,
no key) into one plain sentence and, where one exists, the link that fixes
it — for the Orpheus terms gate, Groq's voice page and "press Accept". The
route returns it in the 502/503 body and records it in debug_logs (once per
reason per half hour) so System shows it. The speaker reports the fallback
to the page; useStudioVoiceStatus keeps it for the tab's session and
StudioVoiceNotice shows it. The phone-voice fallback stays.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* feat(voice): change Loki's voice by saying so, and answer questions about voice mode truthfully
"I don't like your voice, can I change your voice" was answered like a
generic chatbot ("in most voice-enabled AI chat apps... look in settings").
Now "use Troy", "change your voice to Diana", "other voice" and
"switch voice" are a spoken command (config/no-screen.ts, lib/voice/commands)
that the phone answers itself; with no name it lists the voices. A question
that does reach Loki carries voiceModeContext (lib/voice/self.ts), built from
the SSOT: the voices by name, how to change one, the three switches with the
same one-line notes the screen shows (NO_SCREEN_OPTIONS), every command, the
current settings and why studio voices are off, if they are.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(voice): a music bed a phone can actually play, and keep its sound awake
The bed sat at about -38 dBFS RMS alone and -50 under the voice, every note
98-370 Hz behind a 900 Hz low-pass: below what a phone speaker or many
earbuds reproduce, so "music bed on" was silence. It is now an octave
higher (196-740 Hz), about 12 dB louder, ducks by 10 dB instead of 11 more on
top, and earcons sit above it. The AudioContext is resumed inside the Start
tap, on every tap on the circle or headphone button, on return to the page,
and when the phone suspends it mid-session.
useSoundscape also returned a new object every render, so effects keyed on
it re-ran ~20 times a second while listening: the gain was re-ramped every
frame and the 20-second fleet poll was cleared before it could fire. It is
memoized now.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(voice): one h1 per screen, End clear of the feedback launcher, voices as an even grid
- Speaking screen: an h1 below md, where the app top bar steps aside; the
start screen's big "Voice" is no longer a second h1 beside the top bar's.
- The speaking screen sets data-fc-place="tab", so the feedback launcher
and its Watching pill become the slim edge handle instead of sitting on
End; End is larger, with room below it, and the stage scrolls rather than
pushing End off a short screen.
- The voice picker is a <Segmented> grid of four columns (seven voices as
4 + 3, the last two cells wide) instead of a chip row cut off at the edge;
the setting groups have real headings.
- The unclear note under the switches is one short line under each switch,
shared with what Loki is told about itself.
- The page wires the studio-voice notice (under the picker and under the
circle, said aloud once), the spoken voice change, the page's state sent
with each question, and the sound resume on every tap.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* fix(widget): one card per finding, settled page checks, honest fix status, a count that means what needs you
Watching on Loki's own /voice from an Android phone showed six problems in
one panel. Each is fixed where it starts:
- Notices no longer stack. A page's checks own ONE card (checksSlot), which
each new reading replaces in place and a clean reading takes back
(thread.ts pushNoticed / dropNoticed); a remark whose words are already on
screen is not said again; a finding's signature ignores its count and its
placeholder example (findingKind), and the card names its page.
- The heading miscount: the check counted headings inside aria-hidden and
inert subtrees, and measured the page once, mid-swap — /voice's setup
screen (top bar h1 + page h1) leaving for its speaking screen (immersive
mode hides the top bar: no h1). Checks now count only rendered elements and
run once the page has held still, twice, keeping only what both readings
agree on (settledPageChecks / stableChecks). The Voice page itself now has
one h1 on each screen.
- A fix Watch started that failed no longer promises it "goes live by
itself": it says the fix did not finish, and "Open it in Loki to see why"
is a real link to that run's Watch page (owner-view ownerTap; watch-fix
now resolves a failed run's Watch page even without a live page).
- Self-watch: "I couldn't check on your changes just now" replaces the old
sentence; a request that lived through the page being put away (a phone
locking) is never a failure; background reads count only after three
failures in a row; the changes route always answers with CORS headers and
logs what failed.
- The header's one number is what waits on the owner's decision (failed
attempts, saved notes, notes not started, changes said but not saved),
counted by the server over ALL of the owner's rows, not inside the newest
twelve; the open list shows every one of them first. Nothing waiting:
just "Watching".
- The save receipt never quotes a cut sentence: several changes are listed
one per line under it, clamped to whole lines with "Show all".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
* docs(changelog): voice, Watching and Prompts fixes
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>
749 lines
31 KiB
TypeScript
749 lines
31 KiB
TypeScript
/**
|
|
* Loki Feedback Widget — self-contained embed for customer sites.
|
|
*
|
|
* Usage (docs/architecture/feedback-widget.md):
|
|
* <script src="https://<loki-host>/widget.js" data-fc-project="fcw_..." async></script>
|
|
*
|
|
* Constraints that shape this file:
|
|
* - Zero dependencies, one bundle: it must mount on ANY site (static HTML,
|
|
* WordPress, Next, ...) — no React. The one other file is opt-in and lazy:
|
|
* with data-fc-chat="kit" the conversation is drawn by the shared chat
|
|
* (public/widget-chat.js, widget/chat/), fetched on first open, and the
|
|
* panel draws its own if that chunk cannot load.
|
|
* - All UI lives in a Shadow DOM so host CSS and widget CSS can't bleed
|
|
* into each other. The ONE exception: element-picking highlights must
|
|
* style host-page elements, so a tiny fcw-* stylesheet is injected into
|
|
* document.head (removed classes on cleanup).
|
|
* - The token is write-only by design; the API base is derived from this
|
|
* script's own src, so one snippet works on every deployment.
|
|
*
|
|
* This file owns the panel's frame — header (Loki and the page), the one
|
|
* conversation (widget/conversation.ts), hiding — plus the boot gate and the
|
|
* programmatic window.Loki entry point. The launcher, the element picker,
|
|
* watch mode, the stylesheets and the DOM helpers live in their own modules.
|
|
*/
|
|
|
|
import { pageLabel } from "./page-label";
|
|
import { forgetOwnerPass, takeOwnerDenied, takeOwnerPass } from "./owner-pass";
|
|
import { forgetMemberPass, takeMemberPass } from "./member-pass";
|
|
import { startTourFromFragment } from "./tour";
|
|
import type { ReportDiagnostics } from "./report-payload";
|
|
import { DEFAULT_PLACEMENT, normalizePlacement, type Placement } from "./placement";
|
|
import {
|
|
buildDocCSS,
|
|
buildShadowCSS,
|
|
themesFrom,
|
|
themeVarsCSS,
|
|
varTheme,
|
|
type WidgetTheme,
|
|
type WidgetThemes,
|
|
} from "./theme";
|
|
import { createThemeMode } from "./theme-mode";
|
|
import { createBrowseVoice } from "./browse-voice";
|
|
import { createQuickTalk } from "./quick-talk";
|
|
import { isVoiceSupported } from "./voice";
|
|
import { h, spiralMark } from "./dom";
|
|
import { createThoughtsView } from "./thoughts-view";
|
|
import { attachSheet } from "./sheet";
|
|
import {
|
|
isHiddenByVisitor,
|
|
readHiddenMarker,
|
|
VISIBILITY_EVENT,
|
|
readVisitorPlacement,
|
|
restoreRequested,
|
|
writeVisitorPlacement,
|
|
} from "./visitor-placement";
|
|
import { createLauncher } from "./launcher";
|
|
import { createPicker } from "./picker";
|
|
import { startWatchMode, type WatchSession } from "./watch";
|
|
import { installSelfWatch } from "./self-watch";
|
|
import { askAdvisor, READ_QUESTION } from "./loki-api";
|
|
import { createConversation } from "./conversation";
|
|
import { unsavedChanges } from "./conversation-model";
|
|
import { loadChat } from "./chat-loader";
|
|
import { chatkitVars } from "./chat/theme";
|
|
import { createChanges } from "./changes";
|
|
import { assistantFor } from "./thread";
|
|
import { showHideToast } from "./panel-views";
|
|
import { parseWidgetSurfaceModes } from "./surface-modes";
|
|
|
|
const MAX_ELEMENTS = 10;
|
|
|
|
interface ReportInput {
|
|
/** Pre-filled first line so the visitor never faces an empty box. */
|
|
message?: string;
|
|
diagnostics?: ReportDiagnostics;
|
|
}
|
|
|
|
interface LokiApi {
|
|
/**
|
|
* True once the panel can actually be opened — i.e. the boot gate said
|
|
* active AND mount() has run.
|
|
*
|
|
* A host needs this to decide what its own "Report" control should be. The
|
|
* stub below is published synchronously and therefore exists even when the
|
|
* widget will never render (token paused, boot unreachable); a host that
|
|
* treats "report is a function" as "clicking will do something" ships a
|
|
* button that silently no-ops. Read `ready` and fall back to a real link.
|
|
*/
|
|
ready: boolean;
|
|
report(input?: ReportInput): void;
|
|
/**
|
|
* Open the panel in Chat mode, optionally asking a first question — so a
|
|
* host page can put its own "Ask about our projects" box on the page and
|
|
* hand the question to the widget. A no-op on an embed without chat mode.
|
|
*/
|
|
ask(question?: string): void;
|
|
/**
|
|
* Bring the launcher back for a visitor who hid it on this site. The same
|
|
* thing happens when the address carries `#loki`, and when a host calls
|
|
* report() or ask() — an explicit request from the page outranks a hidden
|
|
* button, or the host's own "Report" control would silently do nothing.
|
|
*/
|
|
show(): void;
|
|
/**
|
|
* Hide the launcher for this person on this site — the same choice as "Hide
|
|
* on this site" in the launcher's menu, remembered in this browser. For a
|
|
* host that offers its own on/off switch (Loki's account menu does). Undone
|
|
* by show(), report(), ask() or `#loki`.
|
|
*/
|
|
hide(): void;
|
|
/** Whether this person has the launcher hidden on this site. */
|
|
readonly hidden: boolean;
|
|
}
|
|
|
|
(() => {
|
|
// document.currentScript is null for a `type="module"` tag and for some tag
|
|
// managers' injection; before this fallback such an install rendered nothing
|
|
// and said so only in the console. The attribute is the contract, so find it.
|
|
const script =
|
|
(document.currentScript as HTMLScriptElement | null) ??
|
|
document.querySelector<HTMLScriptElement>('script[src*="widget.js"][data-fc-project]');
|
|
const token = script?.getAttribute("data-fc-project") ?? "";
|
|
if (!token) {
|
|
console.warn("[loki-widget] missing data-fc-project attribute");
|
|
return;
|
|
}
|
|
const apiBase = script?.src ? new URL(script.src).origin : "";
|
|
// Light or dark, as Loki is (theme-mode.ts): one per page, shared by the
|
|
// panel and the walkthrough so they never disagree.
|
|
const themeMode = createThemeMode(token, script?.getAttribute("data-fc-theme"));
|
|
// "Watch the fix" runs apart from the launcher: a hidden button still tours.
|
|
startTourFromFragment(apiBase, token, themeMode);
|
|
// The owner, arriving from Loki's "Open your site" link or returning with the
|
|
// pass kept from it. Their notes start the fix instead of waiting in an inbox.
|
|
const ownerState = takeOwnerPass(token);
|
|
let ownerPass = ownerState.pass;
|
|
// A project member (a client) arriving from their own "Open the site" link
|
|
// in Loki, or returning with it: the panel stays the ordinary one, and
|
|
// their notes are filed as theirs (member-pass.ts).
|
|
let memberPass = takeMemberPass(token);
|
|
// Legacy escape hatch, kept working: px from the bottom edge, set in the
|
|
// customer's own HTML. Superseded by the placement served from boot, which an
|
|
// operator can change without touching their site — but an explicitly set
|
|
// attribute still wins (see boot()).
|
|
const bottomOffset = parseInt(script?.getAttribute("data-fc-bottom") ?? "", 10);
|
|
// Modes are captured with the script tag (async scripts lose currentScript later).
|
|
// No attribute means the default pair, Request a change + Ask Loki — both are
|
|
// about improving THIS site. Chat stays opt-in: it is a studio front desk, and
|
|
// this bundle also runs on pilot sites that never asked for one.
|
|
const modesAttr = script?.getAttribute("data-fc-modes") ?? null;
|
|
// `data-fc-chat="kit"`: draw the conversation with the shared chat (chatkit,
|
|
// the one Loki's own chat uses), loaded as its own chunk on first open.
|
|
// Rolled out on Loki's own widget first; without it, the panel draws its own.
|
|
const useKitChat = script?.getAttribute("data-fc-chat") === "kit";
|
|
const scriptNonce = script?.nonce || undefined;
|
|
|
|
/** Filled by boot() before mount(); the launcher never paints without it. */
|
|
let placement: Placement = { ...DEFAULT_PLACEMENT };
|
|
/** Where the visitor dragged/parked it, if they did. Their choice outranks
|
|
* both the operator's and the auto-avoid, and only for them. */
|
|
let visitorOverride: Placement | null = readVisitorPlacement(token);
|
|
// One widget per page, however many tags a site ends up with (a snippet in
|
|
// the template AND one from a tag manager rendered two launchers). Boot is
|
|
// async, so the host element alone cannot say "someone is already coming";
|
|
// the flag covers that window and is cleared if the boot decides not to
|
|
// render, so an SPA that removes and re-adds the tag still gets its widget.
|
|
const w = window as unknown as { __lokiWidgetBooting?: boolean };
|
|
if (document.getElementById("loki-feedback-host") || w.__lokiWidgetBooting) return;
|
|
w.__lokiWidgetBooting = true;
|
|
|
|
// Publish the programmatic entry point SYNCHRONOUSLY, before the async boot
|
|
// gate decides whether to render. A host page that calls report() from an
|
|
// error toast must not have to know whether the widget has finished booting,
|
|
// so calls made too early are held (latest wins — a double-click on "Report"
|
|
// means the second click, not two panels) and replayed once mount() runs.
|
|
// If the boot gate says inactive, the held report is simply never shown:
|
|
// the widget is off, and a queued submission could not land anyway.
|
|
let pendingReport: ReportInput | null = null;
|
|
let liveReport: ((input: ReportInput) => void) | null = null;
|
|
let pendingAsk: string | null = null;
|
|
let liveAsk: ((question: string) => void) | null = null;
|
|
/** Set once boot says render, even if the visitor hid the launcher — so an
|
|
* explicit report()/ask()/show() can still mount it. */
|
|
let bootedThemes: WidgetThemes | null = null;
|
|
let mounted = false;
|
|
/** Set by mount(): bring back a launcher hidden during this page view. */
|
|
let liveShow: (() => void) | null = null;
|
|
/** Set by mount(): hide a launcher that is on screen, toast optional. */
|
|
let liveHide: ((opts: { toast: boolean }) => void) | null = null;
|
|
const announceVisibility = () =>
|
|
window.dispatchEvent(
|
|
new CustomEvent(VISIBILITY_EVENT, { detail: { hidden: isHiddenByVisitor(visitorOverride) } }),
|
|
);
|
|
/** Show a widget the visitor had hidden, because something asked for it —
|
|
* mounting it first if it was hidden before this page loaded. */
|
|
const unhide = () => {
|
|
if (!isHiddenByVisitor(visitorOverride)) return;
|
|
visitorOverride = null;
|
|
writeVisitorPlacement(token, null);
|
|
if (liveShow) liveShow();
|
|
else if (bootedThemes && !mounted) mountWhenReady(bootedThemes);
|
|
announceVisibility();
|
|
};
|
|
/** The host's switch: same stored choice as the menu's "Hide on this site",
|
|
* without the toast (the host's own control already shows the state). */
|
|
const hide = () => {
|
|
if (isHiddenByVisitor(visitorOverride)) return;
|
|
if (liveHide) liveHide({ toast: false });
|
|
else {
|
|
visitorOverride = readHiddenMarker();
|
|
writeVisitorPlacement(token, { hidden: true });
|
|
announceVisibility();
|
|
}
|
|
};
|
|
const api: LokiApi = {
|
|
ready: false,
|
|
report(input: ReportInput = {}) {
|
|
unhide();
|
|
if (liveReport) liveReport(input);
|
|
else pendingReport = input;
|
|
},
|
|
ask(question = "") {
|
|
unhide();
|
|
if (liveAsk) liveAsk(question);
|
|
else pendingAsk = question;
|
|
},
|
|
show: unhide,
|
|
hide,
|
|
get hidden() {
|
|
return isHiddenByVisitor(visitorOverride);
|
|
},
|
|
};
|
|
(window as unknown as { Loki?: LokiApi }).Loki = api;
|
|
// Arriving from the owner link (or returning through the "This is my site" link): open the
|
|
// conversation straight away, so "look at my site and say what to change"
|
|
// is one step, not a hunt for the button. report({}) only opens the panel.
|
|
if (ownerState.arrived) pendingReport = {};
|
|
// Returned through that link signed in as someone else: say so on the site.
|
|
const ownerDenied = takeOwnerDenied();
|
|
// The owner arriving from their link, or anyone opening the page with
|
|
// `#loki`, gets the launcher back even if this browser hid it earlier. Without
|
|
// this, "Hide on this site" was permanent: nothing on the page could undo it.
|
|
if (ownerState.arrived || ownerDenied || restoreRequested()) {
|
|
visitorOverride = null;
|
|
writeVisitorPlacement(token, null);
|
|
}
|
|
|
|
// Typing #loki onto a page that is already open is a hash change, not a load.
|
|
window.addEventListener("hashchange", () => {
|
|
if (restoreRequested()) api.show();
|
|
});
|
|
|
|
const mountWhenReady = (themes: WidgetThemes) => {
|
|
if (mounted) return;
|
|
mounted = true;
|
|
const go = () => {
|
|
w.__lokiWidgetBooting = false;
|
|
if (document.getElementById("loki-feedback-host")) return;
|
|
mount(themes);
|
|
};
|
|
if (document.body) go();
|
|
else document.addEventListener("DOMContentLoaded", go);
|
|
};
|
|
|
|
const mount = (themes: WidgetThemes) => {
|
|
// Every stylesheet below is written against variables; the host defines
|
|
// them for light and dark and themeMode says which (theme.ts).
|
|
const theme: WidgetTheme = varTheme(themes);
|
|
// ---- shadow scaffold ----
|
|
const host = h("div");
|
|
host.id = "loki-feedback-host";
|
|
themeMode.bind(host);
|
|
const root = host.attachShadow({ mode: "open" });
|
|
const style = h("style");
|
|
style.textContent = themeVarsCSS(themes) + buildShadowCSS(theme);
|
|
root.appendChild(style);
|
|
document.body.appendChild(host);
|
|
|
|
const docStyle = h("style");
|
|
// The host page cannot read the panel's variables: concrete colours.
|
|
docStyle.textContent = buildDocCSS(themes.light);
|
|
|
|
const launcher = createLauncher({
|
|
root,
|
|
host,
|
|
token,
|
|
getPlacement: () => placement,
|
|
getVisitorOverride: () => visitorOverride,
|
|
setVisitorOverride: (value) => {
|
|
visitorOverride = value;
|
|
},
|
|
onOpen: openPanel,
|
|
onHide: () => hideForVisitor(),
|
|
});
|
|
const fab = launcher.fab;
|
|
/** The launcher's resting display: none while the visitor has it hidden. */
|
|
const fabRest = () => (isHiddenByVisitor(visitorOverride) ? "none" : "");
|
|
|
|
// ---- hiding, and getting it back ----
|
|
//
|
|
// Hiding used to live only behind a long-press / right-click on the
|
|
// launcher, and it was one-way: the launcher was removed, the choice stored,
|
|
// and nothing on any page could bring it back — not even the host's own
|
|
// "Report" control. Now it is offered in the panel too, it says how to undo
|
|
// it, and #loki, Loki.show(), report() and ask() all restore it.
|
|
let toast: HTMLElement | null = null;
|
|
function hideForVisitor({ toast: withToast = true }: { toast?: boolean } = {}) {
|
|
if (panel.isConnected) closePanel();
|
|
visitorOverride = readHiddenMarker();
|
|
writeVisitorPlacement(token, { hidden: true });
|
|
fab.style.display = "none";
|
|
toast?.remove();
|
|
toast = withToast ? showHideToast(root, () => api.show()) : null;
|
|
announceVisibility();
|
|
}
|
|
liveHide = hideForVisitor;
|
|
liveShow = () => {
|
|
toast?.remove();
|
|
if (!panel.isConnected) fab.style.display = "";
|
|
launcher.reposition();
|
|
};
|
|
|
|
// The widget watching ITSELF (self-watch.ts) — the owner's, like Watch.
|
|
let selfWatch: ReturnType<typeof installSelfWatch> | null = null;
|
|
|
|
// ---- panel (built once, shown on demand) ----
|
|
const backdrop = h("div", "backdrop");
|
|
backdrop.addEventListener("click", closePanel);
|
|
|
|
const panel = h("div", "panel");
|
|
panel.setAttribute("role", "dialog");
|
|
panel.setAttribute("aria-modal", "true");
|
|
panel.setAttribute("aria-label", "Loki");
|
|
|
|
// ---- header: who you are talking to, about which page, and Watch ----
|
|
const hdr = h("div", "hdr");
|
|
const hdrText = h("div");
|
|
// The brand line is what makes this recognisably Loki on a stranger's
|
|
// site — the same mono micro-label Loki's own pages use.
|
|
const brand = h("div", "brand");
|
|
brand.append(spiralMark(), h("span", "mono", "Loki"));
|
|
const hdrPage = h("div", "page");
|
|
hdrText.append(brand, hdrPage);
|
|
const hdrActions = h("div", "hdr-actions");
|
|
const closeBtn = h("button", "x", "✕");
|
|
closeBtn.setAttribute("aria-label", "Close");
|
|
closeBtn.addEventListener("click", closePanel);
|
|
hdrActions.append(closeBtn);
|
|
hdr.append(hdrText, hdrActions);
|
|
// The owner's changes and where each one is — the half of "Loki tells
|
|
// you when" that happens on the site (widget/changes.ts). Loki says it in
|
|
// the thread when the panel is open, beside the launcher when it is not.
|
|
const changes = createChanges({
|
|
apiBase,
|
|
token,
|
|
pass: () => ownerPass,
|
|
onLive: (c) => {
|
|
const links = c.href ? [{ label: c.action ?? "See it", url: c.href }] : [];
|
|
conversation.say(`Done — “${c.text}” is live on this site.`, links);
|
|
if (panel.isConnected) return;
|
|
unread++;
|
|
syncWatch();
|
|
launcher.say(`“${c.text.slice(0, 60)}${c.text.length > 60 ? "…" : ""}” is live`);
|
|
},
|
|
onTheme: (pref) => themeMode.setPreference(pref),
|
|
onPassRefused: () => {
|
|
forgetOwnerPass(token);
|
|
ownerPass = null;
|
|
syncWatch();
|
|
conversation.refresh();
|
|
},
|
|
// A fix moved (building → live): the card that noticed it follows.
|
|
onUpdate: () => {
|
|
if (panel.isConnected) conversation.refresh();
|
|
},
|
|
onRender: () => thoughts.repaint(),
|
|
onSay: (text) => conversation.say(text),
|
|
// Changes said on the open card wait on the owner's Save: counted, and
|
|
// a row in the list that brings the card into view.
|
|
unsaved: () => unsavedChanges(conversation?.model.snapshot().draft ?? null),
|
|
onUnsaved: () => {
|
|
thoughts.close();
|
|
conversation.showDraft();
|
|
},
|
|
});
|
|
// The owner's one status line under the header: watching, and where
|
|
// their changes are (thoughts-view.ts).
|
|
const thoughts = createThoughtsView(() => {
|
|
if (watchSession?.on()) watchSession.stop();
|
|
else watchSession?.resume();
|
|
syncWatch();
|
|
}, changes);
|
|
function syncWatch() {
|
|
const on = watchSession?.on() ?? false;
|
|
// The spiral in the header turns while Loki watches, stops when it does not.
|
|
brand.classList.toggle("watching", on);
|
|
brand.classList.toggle("paused", !!ownerPass && !on);
|
|
thoughts.el.style.display = ownerPass ? "" : "none";
|
|
// The owner's status line is the header: the brand row and the page
|
|
// name are a visitor's orientation, and for the owner they were two
|
|
// more rows before the conversation. The close button follows the line.
|
|
panel.classList.toggle("owner", !!ownerPass);
|
|
(ownerPass ? thoughts.head : hdrActions).append(closeBtn);
|
|
thoughts.update(watchSession?.trail() ?? [], on);
|
|
// "Hide this button" is a visitor's way out; the owner's panel is their
|
|
// tool, and the link only took space under the composer on a phone.
|
|
hideLink.hidden = !!ownerPass;
|
|
madeWith.hidden = !!ownerPass;
|
|
// The owner's launcher carries the same state as this header.
|
|
launcher.setOwnerStatus(ownerPass ? { watching: on, unread } : null);
|
|
quickTalk?.sync();
|
|
conversation?.refresh();
|
|
}
|
|
/** What Loki said while the panel was closed — the launcher's badge. */
|
|
let unread = 0;
|
|
|
|
// Visible, not only behind a long-press: a visitor who does not want the
|
|
// button should not have to know a gesture to get rid of it.
|
|
const hideLink = h("button", "hide-link", "Hide this button on this site");
|
|
hideLink.addEventListener("click", () => hideForVisitor());
|
|
// The one line a stranger gets about Loki itself: where they are and
|
|
// where to get one. Quiet, last, and gone for the owner, who knows.
|
|
// On a phone only "Made with Loki →" shows (theme.ts .mw-more): the whole
|
|
// sentence wrapped the footer to two rows and pushed Send under it.
|
|
const madeWith = h("a", "made-with", "Made with Loki");
|
|
madeWith.append(h("span", "mw-more", " — build something of your own"), " →");
|
|
madeWith.href = `${apiBase}/build?from=${encodeURIComponent(location.hostname)}`;
|
|
madeWith.target = "_blank";
|
|
madeWith.rel = "noopener";
|
|
|
|
let browseVoice: ReturnType<typeof createBrowseVoice> | null = null;
|
|
let quickTalk: ReturnType<typeof createQuickTalk> | null = null;
|
|
/** Talk while you scroll — from the panel's Talk or beside the launcher. */
|
|
function startTalk() {
|
|
if (conversation.hasDraft()) {
|
|
if (!panel.isConnected) openPanel();
|
|
conversation.say("Save or cancel your current note before starting another.");
|
|
return;
|
|
}
|
|
browseVoice?.start();
|
|
}
|
|
const modes = parseWidgetSurfaceModes(modesAttr);
|
|
const picker = createPicker({
|
|
root,
|
|
host,
|
|
docStyle,
|
|
backdrop,
|
|
panel,
|
|
maxElements: MAX_ELEMENTS,
|
|
onCancel: () => conversation.refresh(),
|
|
onStop: () => {
|
|
if (browseVoice?.active()) {
|
|
browseVoice.pointed();
|
|
closePanel();
|
|
return;
|
|
}
|
|
conversation.refresh();
|
|
conversation.focus();
|
|
},
|
|
});
|
|
/** Set once watch mode starts (owner only) — see the end of mount(). */
|
|
let watchSession: WatchSession | null = null;
|
|
const conversation = createConversation({
|
|
apiBase,
|
|
token,
|
|
assistant: () => assistantFor(modes, ownerPass !== null),
|
|
ownerPass: () => ownerPass,
|
|
onPassRefused: () => {
|
|
// Expired or revoked: stop presenting it, and read as a visitor.
|
|
forgetOwnerPass(token);
|
|
ownerPass = null;
|
|
syncWatch();
|
|
conversation.refresh();
|
|
},
|
|
memberPass: () => memberPass,
|
|
onMemberRefused: () => {
|
|
forgetMemberPass(token);
|
|
memberPass = null;
|
|
},
|
|
picker,
|
|
onBusy: (busy) => {
|
|
brand.classList.toggle("thinking", busy);
|
|
thoughts.el.classList.toggle("thinking", busy);
|
|
},
|
|
// A change was filed: Your changes shows where it went, at once.
|
|
onSent: () => changes.start(),
|
|
watch: () => watchSession,
|
|
known: (key) => changes.known(key),
|
|
// The one microphone: talk while you scroll (browse-voice.ts).
|
|
...(isVoiceSupported() ? { talk: startTalk } : {}),
|
|
});
|
|
// The open card came or went: the header's count and list follow.
|
|
conversation.model.subscribe(() => changes.repaint());
|
|
|
|
const foot = h("div", "panel-foot");
|
|
foot.append(madeWith, hideLink);
|
|
panel.append(hdr, thoughts.el, conversation.el, foot);
|
|
// On a phone the panel is a sheet: drag its top up for more, down to peek
|
|
// at the page under it, further down to put it away (sheet.ts).
|
|
const sheet = attachSheet({ panel, handle: hdr, onClose: closePanel });
|
|
|
|
function openPanel() {
|
|
unread = 0;
|
|
changes.start();
|
|
fab.style.display = "none";
|
|
hdrPage.textContent = pageLabel(document.title, location.pathname);
|
|
root.append(backdrop, panel);
|
|
syncWatch();
|
|
// Opening is to see what is new: land on the newest message.
|
|
conversation.refresh(true);
|
|
if (useKitChat && !conversation.kit())
|
|
void loadChat(apiBase, scriptNonce).then((mod) => {
|
|
// No chunk (CSP, offline): the panel keeps drawing its own — no dead end.
|
|
if (!mod || conversation.kit()) return;
|
|
conversation.useKit(mod, chatkitVars(theme));
|
|
if (panel.isConnected) conversation.focus();
|
|
});
|
|
root.addEventListener("keydown", onKeydown as EventListener);
|
|
document.addEventListener("keydown", onDocumentKeydown);
|
|
conversation.focus();
|
|
// Measured once it has laid out: a panel too wide for this phone is
|
|
// Loki's bug, and Loki should hear about it from here.
|
|
setTimeout(() => {
|
|
if (panel.isConnected) selfWatch?.checkPanel(panel);
|
|
}, 400);
|
|
}
|
|
|
|
function closePanel() {
|
|
if (picker.isPicking()) picker.stop();
|
|
// Abandon any in-flight recording: a lit "recording" indicator after the
|
|
// panel is closed reads as the page still listening.
|
|
conversation.close();
|
|
backdrop.remove();
|
|
panel.remove();
|
|
// Opened again, it is the resting sheet, not the typing one it was
|
|
// closed as (the removed box never reports losing focus).
|
|
sheet.reset();
|
|
root.removeEventListener("keydown", onKeydown as EventListener);
|
|
document.removeEventListener("keydown", onDocumentKeydown);
|
|
fab.style.display = browseVoice?.active() ? "none" : fabRest();
|
|
}
|
|
|
|
function onKeydown(e: KeyboardEvent) {
|
|
// The panel is a modal overlay in a shadow root. Host pages bind global
|
|
// hotkeys (⌘K, "/", "?") on document, and the event retargets to the
|
|
// shadow HOST, so their "is the user typing?" guard reads the wrong node
|
|
// and steals keystrokes (observed on orangecat.ch and loki.orangecat.ch).
|
|
// While the panel is open we own the keyboard — stopped here, at the
|
|
// shadow root on the way OUT, not on document on the way in: stopping
|
|
// it in the capture phase also kept it from every control inside the
|
|
// panel that listens for itself (a composer's Enter, a dialog's Escape).
|
|
e.stopPropagation();
|
|
if (conversation.onKey(e)) return;
|
|
if (e.key === "Escape" && !e.defaultPrevented) {
|
|
if (picker.isPicking()) picker.stop();
|
|
else closePanel();
|
|
}
|
|
}
|
|
|
|
/** Escape with focus outside the panel (on the page behind it) still closes it. */
|
|
function onDocumentKeydown(e: KeyboardEvent) {
|
|
if (e.key !== "Escape" || e.composedPath().includes(host)) return;
|
|
if (picker.isPicking()) picker.stop();
|
|
else closePanel();
|
|
}
|
|
|
|
browseVoice = createBrowseVoice({
|
|
root,
|
|
host,
|
|
theme,
|
|
token,
|
|
endpoint: `${apiBase}/api/widget/transcribe`,
|
|
selected: () => picker.selected(),
|
|
onActive: (active) => {
|
|
if (active) closePanel();
|
|
else {
|
|
if (picker.isPicking()) {
|
|
picker.stop();
|
|
closePanel();
|
|
}
|
|
if (!panel.isConnected) {
|
|
fab.style.display = fabRest();
|
|
root.removeEventListener("keydown", onKeydown as EventListener);
|
|
document.removeEventListener("keydown", onDocumentKeydown);
|
|
}
|
|
}
|
|
syncWatch();
|
|
},
|
|
onTranscript: (text, capture, moments, spokenMs) => {
|
|
if (picker.isPicking()) picker.stop();
|
|
openPanel();
|
|
conversation.recorded(text, capture, moments, spokenMs);
|
|
},
|
|
onError: (message) => {
|
|
openPanel();
|
|
conversation.say(message);
|
|
},
|
|
});
|
|
|
|
if (isVoiceSupported())
|
|
quickTalk = createQuickTalk({
|
|
root,
|
|
fab,
|
|
wanted: () => !!ownerPass && !panel.isConnected && !browseVoice?.active(),
|
|
onTalk: startTalk,
|
|
});
|
|
|
|
// Programmatic entry point: the host's own "Report" control opens the
|
|
// send confirmation prefilled, with whatever the host attached.
|
|
liveReport = (input: ReportInput) => {
|
|
if (!panel.isConnected) openPanel();
|
|
if (input.message || input.diagnostics)
|
|
conversation.draft(input.message ?? "", input.diagnostics ?? null);
|
|
};
|
|
liveAsk = (question: string) => {
|
|
if (!panel.isConnected) openPanel();
|
|
if (question.trim()) conversation.ask(question);
|
|
};
|
|
// The owner: watch mode (widget/watch.ts). Its state lives on the launcher
|
|
// ("Loki · watching", a count of what it said); what it notices, Loki says
|
|
// in this conversation and, while the panel is closed, in a bubble beside
|
|
// the launcher. The top bar appears only when neither can be seen.
|
|
const watchOpts = { root, host, theme, token, apiBase, pass: () => ownerPass };
|
|
if (ownerPass)
|
|
selfWatch = installSelfWatch({
|
|
apiBase,
|
|
token,
|
|
pass: () => ownerPass,
|
|
active: () => watchSession?.on() ?? false,
|
|
onFinding: (say) => conversation.model.selfNote(say),
|
|
});
|
|
if (ownerPass)
|
|
watchSession = startWatchMode({
|
|
...watchOpts,
|
|
onOwnRequest: (method, url, status, ms) => selfWatch?.ownRequest(method, url, status, ms),
|
|
onShow: () => {
|
|
if (!panel.isConnected) openPanel();
|
|
},
|
|
onPoint: () => {
|
|
openPanel();
|
|
picker.start();
|
|
},
|
|
onRemark: (r) => {
|
|
conversation.model.noticed(r);
|
|
if (panel.isConnected || browseVoice?.active()) return;
|
|
unread++;
|
|
syncWatch();
|
|
launcher.say(r.short ?? r.say.split("\n")[0]);
|
|
},
|
|
onWithdraw: (slot) => conversation.model.unnoticed(slot),
|
|
statusShown: () => !!browseVoice?.active() || panel.isConnected || launcher.isShown(),
|
|
onChange: syncWatch,
|
|
onTrail: (trail) => thoughts.update(trail, watchSession?.on() ?? false),
|
|
known: (key) => changes.known(key),
|
|
knownReady: () => changes.ready(),
|
|
read: async () => {
|
|
const pass = ownerPass;
|
|
if (!pass) return null;
|
|
const a = await askAdvisor({
|
|
apiBase,
|
|
token,
|
|
question: READ_QUESTION,
|
|
scope: "page",
|
|
selected: [],
|
|
history: [],
|
|
ownerPass: pass,
|
|
read: true,
|
|
});
|
|
if (a.degraded) return null;
|
|
return {
|
|
say: a.messages.map((m: { text: string }) => m.text).join("\n"),
|
|
changes: a.changes,
|
|
};
|
|
},
|
|
});
|
|
syncWatch();
|
|
// The owner's changes are asked for on arrival, so a change that went
|
|
// live since their last visit is announced before they open anything.
|
|
changes.start();
|
|
// Only now can a click actually open something — see LokiApi.ready.
|
|
api.ready = true;
|
|
if (ownerDenied) {
|
|
openPanel();
|
|
conversation.say(
|
|
"You're signed in to Loki, but with an account that can't edit this site. If it should be yours, ask its owner to share the project with you in Loki as an editor, then sign in here again. Until then you can still ask me anything and send notes.",
|
|
);
|
|
}
|
|
if (pendingAsk !== null) {
|
|
const held = pendingAsk;
|
|
pendingAsk = null;
|
|
liveAsk(held);
|
|
}
|
|
if (pendingReport) {
|
|
const held = pendingReport;
|
|
pendingReport = null;
|
|
liveReport(held);
|
|
}
|
|
};
|
|
|
|
// Boot gate — the server decides whether to render at all. This makes the
|
|
// Loki token row a remote kill switch: pausing/revoking hides the FAB
|
|
// on the customer site within the cache window, no deploy needed. It also
|
|
// doubles as the heartbeat behind the setup UI's "Live" state. Fail closed:
|
|
// if Loki is unreachable, submissions couldn't land anyway — don't
|
|
// render a dead FAB.
|
|
const boot = async () => {
|
|
try {
|
|
const res = await fetch(`${apiBase}/api/widget-boot?token=${encodeURIComponent(token)}`);
|
|
const body = (await res.json()) as {
|
|
active?: boolean;
|
|
placement?: unknown;
|
|
theme?: WidgetTheme;
|
|
themes?: WidgetThemes;
|
|
};
|
|
// Theme must come from boot — the widget has no fallback palette.
|
|
// If boot doesn't provide colors, the widget doesn't render.
|
|
const themes = themesFrom(body);
|
|
if (body.active !== true || !themes) {
|
|
w.__lokiWidgetBooting = false;
|
|
return;
|
|
}
|
|
// Placement arrives with the render verdict, so the launcher paints once
|
|
// in its final corner instead of appearing bottom-right and jumping.
|
|
placement = normalizePlacement(body.placement);
|
|
// The legacy data-fc-bottom attribute still wins where a customer set it:
|
|
// their HTML is an explicit instruction from someone who looked at the
|
|
// page, and silently overriding it would move a launcher they had already
|
|
// positioned by hand.
|
|
if (Number.isFinite(bottomOffset)) placement.offsetY = bottomOffset;
|
|
bootedThemes = themes;
|
|
// A report()/ask() made before boot finished is an explicit request.
|
|
if (pendingReport || pendingAsk !== null) unhide();
|
|
// A visitor who dismissed the widget on this site gets no launcher.
|
|
// Checked after boot so a revoked token still short-circuits first — the
|
|
// operator's kill switch outranks the preference. The API stays usable:
|
|
// report()/ask()/show() from the host page bring the widget back.
|
|
if (isHiddenByVisitor(visitorOverride)) {
|
|
api.ready = true;
|
|
w.__lokiWidgetBooting = false;
|
|
return;
|
|
}
|
|
mountWhenReady(themes);
|
|
} catch {
|
|
w.__lokiWidgetBooting = false;
|
|
}
|
|
};
|
|
void boot();
|
|
})();
|