mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-05 21:28:35 +01:00
The widget's chat was the fleet's own hand-rolled chat; with data-fc-chat="kit" it is now drawn by @bitbaum/chatkit, the thread and composer Loki's own chat renders, over the same conversation model. - widget/chat/: a separate lazy chunk (public/widget-chat.js, ~24 KB gzipped, Preact standing in for React), fetched on first open, carrying the tag's nonce. chatkit's CSS is rewritten for the shadow root (:root -> :host, rem -> px) and coloured from the boot theme. - widget/conversation-kit.ts maps the model's snapshot to what chatkit draws; Fix this, the changes, the confirmation card, the About chips and Continue in Loki go into chatkit's slots as the widget's own nodes. - A chunk that cannot load leaves the panel's own view: no dead end. - /widget-chat.js is public in the auth proxy, like /widget.js. - Tests: widget-kit-browser (34 checks: ask, card, screenshot, fallback, phone width, host hotkeys, weight budget), widget-chat-css, and widget-self-contained now guards the chunk/panel seam. Claude-Session: https://claude.ai/code/session_01BWXjqiHm8jFta4FXuc7Tck Co-authored-by: Claude <noreply@anthropic.com>
66 lines
2.2 KiB
TypeScript
66 lines
2.2 KiB
TypeScript
/**
|
|
* Builds the widget's chat chunk — widget/chat/entry.tsx → public/widget-chat.js
|
|
* (gitignored, like public/widget.js).
|
|
*
|
|
* The chunk is the shared chat (chatkit) for the widget, loaded only when the
|
|
* panel opens (widget/chat-loader.ts). chatkit is written for React; here
|
|
* Preact stands in for it through preact/compat, which keeps the chunk a
|
|
* fraction of React's size on someone else's site. chatkit's stylesheet is
|
|
* inlined as a string, corrected for a Shadow DOM (widget/chat/shadow-css.ts).
|
|
*
|
|
* Run: npx tsx scripts/build-widget-chat.ts
|
|
*/
|
|
import { readFileSync } from "node:fs";
|
|
import { createRequire } from "node:module";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { build, type Plugin } from "esbuild";
|
|
import { shadowChatkitCss } from "../widget/chat/shadow-css";
|
|
|
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const require = createRequire(join(root, "package.json"));
|
|
|
|
/** `import css from "@bitbaum/chatkit/styles.css"` → the corrected CSS text. */
|
|
const chatkitCss: Plugin = {
|
|
name: "chatkit-css",
|
|
setup(b) {
|
|
b.onResolve({ filter: /^@bitbaum\/chatkit\/styles\.css$/ }, () => ({
|
|
path: require.resolve("@bitbaum/chatkit/styles.css"),
|
|
namespace: "chatkit-css",
|
|
}));
|
|
b.onLoad({ filter: /.*/, namespace: "chatkit-css" }, (args) => ({
|
|
contents: shadowChatkitCss(readFileSync(args.path, "utf8")),
|
|
loader: "text",
|
|
}));
|
|
},
|
|
};
|
|
|
|
export async function buildWidgetChat(opts: { write?: boolean; outfile?: string } = {}) {
|
|
return build({
|
|
entryPoints: [join(root, "widget/chat/entry.tsx")],
|
|
outfile: opts.outfile ?? join(root, "public/widget-chat.js"),
|
|
write: opts.write ?? true,
|
|
bundle: true,
|
|
minify: true,
|
|
format: "iife",
|
|
jsx: "automatic",
|
|
jsxImportSource: "preact",
|
|
alias: {
|
|
react: "preact/compat",
|
|
"react-dom": "preact/compat",
|
|
"react-dom/client": "preact/compat/client",
|
|
"react/jsx-runtime": "preact/jsx-runtime",
|
|
},
|
|
define: { "process.env.NODE_ENV": '"production"' },
|
|
plugins: [chatkitCss],
|
|
logLevel: "warning",
|
|
});
|
|
}
|
|
|
|
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
buildWidgetChat().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|
|
}
|