Skip to content

Commit f70b3a2

Browse files
boris721steipete
authored andcommitted
refactor: bundle export-html templates instead of reading from node_modules
- Copy templates from pi-coding-agent into src/auto-reply/reply/export-html/ - Add build script to copy templates to dist/ - Remove fragile node_modules path traversal - Templates are now self-contained (~250KB total)
1 parent 1eb1a33 commit f70b3a2

8 files changed

Lines changed: 3893 additions & 33 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"android:install": "cd apps/android && ./gradlew :app:installDebug",
5252
"android:run": "cd apps/android && ./gradlew :app:installDebug && adb shell am start -n ai.openclaw.android/.MainActivity",
5353
"android:test": "cd apps/android && ./gradlew :app:testDebugUnitTest",
54-
"build": "pnpm canvas:a2ui:bundle && tsdown && pnpm build:plugin-sdk:dts && node --import tsx scripts/write-plugin-sdk-entry-dts.ts && node --import tsx scripts/canvas-a2ui-copy.ts && node --import tsx scripts/copy-hook-metadata.ts && node --import tsx scripts/write-build-info.ts && node --import tsx scripts/write-cli-compat.ts",
54+
"build": "pnpm canvas:a2ui:bundle && tsdown && pnpm build:plugin-sdk:dts && node --import tsx scripts/write-plugin-sdk-entry-dts.ts && node --import tsx scripts/canvas-a2ui-copy.ts && node --import tsx scripts/copy-hook-metadata.ts && node --import tsx scripts/copy-export-html-templates.ts && node --import tsx scripts/write-build-info.ts && node --import tsx scripts/write-cli-compat.ts",
5555
"build:plugin-sdk:dts": "tsc -p tsconfig.plugin-sdk.dts.json",
5656
"canvas:a2ui:bundle": "bash scripts/bundle-a2ui.sh",
5757
"check": "pnpm format:check && pnpm tsgo && pnpm lint",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env tsx
2+
/**
3+
* Copy export-html templates from src to dist
4+
*/
5+
6+
import fs from "node:fs";
7+
import path from "node:path";
8+
import { fileURLToPath } from "node:url";
9+
10+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
11+
const projectRoot = path.resolve(__dirname, "..");
12+
13+
const srcDir = path.join(projectRoot, "src", "auto-reply", "reply", "export-html");
14+
const distDir = path.join(projectRoot, "dist", "export-html");
15+
16+
function copyExportHtmlTemplates() {
17+
if (!fs.existsSync(srcDir)) {
18+
console.warn("[copy-export-html-templates] Source directory not found:", srcDir);
19+
return;
20+
}
21+
22+
// Create dist directory
23+
if (!fs.existsSync(distDir)) {
24+
fs.mkdirSync(distDir, { recursive: true });
25+
}
26+
27+
// Copy main template files
28+
const templateFiles = ["template.html", "template.css", "template.js"];
29+
for (const file of templateFiles) {
30+
const srcFile = path.join(srcDir, file);
31+
const distFile = path.join(distDir, file);
32+
if (fs.existsSync(srcFile)) {
33+
fs.copyFileSync(srcFile, distFile);
34+
console.log(`[copy-export-html-templates] Copied ${file}`);
35+
}
36+
}
37+
38+
// Copy vendor files
39+
const srcVendor = path.join(srcDir, "vendor");
40+
const distVendor = path.join(distDir, "vendor");
41+
if (fs.existsSync(srcVendor)) {
42+
if (!fs.existsSync(distVendor)) {
43+
fs.mkdirSync(distVendor, { recursive: true });
44+
}
45+
const vendorFiles = fs.readdirSync(srcVendor);
46+
for (const file of vendorFiles) {
47+
const srcFile = path.join(srcVendor, file);
48+
const distFile = path.join(distVendor, file);
49+
if (fs.statSync(srcFile).isFile()) {
50+
fs.copyFileSync(srcFile, distFile);
51+
console.log(`[copy-export-html-templates] Copied vendor/${file}`);
52+
}
53+
}
54+
}
55+
56+
console.log("[copy-export-html-templates] Done");
57+
}
58+
59+
copyExportHtmlTemplates();

src/auto-reply/reply/commands-export-session.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,8 @@ import { loadSessionStore } from "../../config/sessions/store.js";
2525
import { getRemoteSkillEligibility } from "../../infra/skills-remote.js";
2626
import { buildTtsSystemPromptHint } from "../../tts/tts.js";
2727

28-
// Find pi-coding-agent export-html templates by traversing node_modules
29-
function findPiExportDir(): string {
30-
// Start from this file's directory and look for node_modules
31-
const thisDir = path.dirname(fileURLToPath(import.meta.url));
32-
let current = thisDir;
33-
while (current !== path.dirname(current)) {
34-
const candidate = path.join(
35-
current,
36-
"node_modules",
37-
"@mariozechner",
38-
"pi-coding-agent",
39-
"dist",
40-
"core",
41-
"export-html",
42-
);
43-
if (fs.existsSync(candidate)) {
44-
return candidate;
45-
}
46-
current = path.dirname(current);
47-
}
48-
throw new Error("Could not find @mariozechner/pi-coding-agent export-html templates");
49-
}
28+
// Export HTML templates are bundled with this module
29+
const EXPORT_HTML_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "export-html");
5030

5131
interface SessionData {
5232
header: SessionHeader | null;
@@ -56,17 +36,8 @@ interface SessionData {
5636
tools?: Array<{ name: string; description?: string; parameters?: unknown }>;
5737
}
5838

59-
let cachedExportDir: string | null = null;
60-
61-
function getExportDir(): string {
62-
if (!cachedExportDir) {
63-
cachedExportDir = findPiExportDir();
64-
}
65-
return cachedExportDir;
66-
}
67-
6839
function loadTemplate(fileName: string): string {
69-
return fs.readFileSync(path.join(getExportDir(), fileName), "utf-8");
40+
return fs.readFileSync(path.join(EXPORT_HTML_DIR, fileName), "utf-8");
7041
}
7142

7243
function generateHtml(sessionData: SessionData): string {

0 commit comments

Comments
 (0)