toon-memory 3.1.0 → 3.2.0

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.
@@ -0,0 +1,76 @@
1
+ import { writeFileSync, mkdirSync } from "fs"
2
+ import { join } from "path"
3
+ import { execSync } from "child_process"
4
+ import { buildViewerData } from "./data"
5
+ import { generateHtml } from "./html"
6
+ import { createServer, type IncomingMessage, type ServerResponse } from "http"
7
+
8
+ export { buildViewerData } from "./data"
9
+
10
+ function openBrowser(url: string): void {
11
+ const platform = process.platform
12
+ try {
13
+ if (platform === "darwin") execSync(`open "${url}"`)
14
+ else if (platform === "win32") execSync(`start "" "${url}"`)
15
+ else execSync(`xdg-open "${url}" || sensible-browser "${url}" || x-www-browser "${url}"`, { stdio: "ignore" })
16
+ } catch {
17
+ console.log(`Open in browser: ${url}`)
18
+ }
19
+ }
20
+
21
+ export function viewer(portArg?: string): void {
22
+ const PORT = parseInt(portArg || process.env.PORT || "3000", 10)
23
+ let currentHtml = generateHtml(buildViewerData())
24
+
25
+ const server = createServer((_req: IncomingMessage, res: ServerResponse) => {
26
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" })
27
+ res.end(currentHtml)
28
+ })
29
+
30
+ server.listen(PORT, "127.0.0.1", () => {
31
+ const url = `http://127.0.0.1:${PORT}`
32
+ const data = buildViewerData()
33
+ console.log(`\ntoon-memory viewer`)
34
+ console.log(` Entries: ${data.totalEntries} | Edges: ${data.edges.length}`)
35
+ console.log(` Serving at: ${url}`)
36
+ console.log(` Press 'r' in terminal to reload | Ctrl+C to stop\n`)
37
+ openBrowser(url)
38
+ })
39
+
40
+ if (process.stdin.isTTY) {
41
+ process.stdin.setRawMode?.(true)
42
+ process.stdin.resume()
43
+ process.stdin.on("data", (buf) => {
44
+ const key = buf.toString()
45
+ if (key === "r" || key === "R") {
46
+ const data = buildViewerData()
47
+ currentHtml = generateHtml(data)
48
+ console.log(` Reloaded: ${data.totalEntries} entries, ${data.edges.length} edges`)
49
+ }
50
+ if (key === "\u0003") process.exit(0)
51
+ })
52
+ }
53
+
54
+ server.on("error", (err: NodeJS.ErrnoException) => {
55
+ if (err.code === "EADDRINUSE") {
56
+ console.error(`Port ${PORT} already in use. Try: PORT=3001 toon-memory viewer`)
57
+ } else {
58
+ console.error(`Server error: ${err.message}`)
59
+ }
60
+ process.exit(1)
61
+ })
62
+ }
63
+
64
+ export function viewerExport(): void {
65
+ const viewerData = buildViewerData()
66
+ const html = generateHtml(viewerData)
67
+
68
+ const projectRoot = process.cwd()
69
+ const outDir = join(projectRoot, ".toon-memory", "viewer")
70
+ mkdirSync(outDir, { recursive: true })
71
+ const outFile = join(outDir, "index.html")
72
+ writeFileSync(outFile, html, "utf-8")
73
+ console.log(`Viewer saved: ${outFile}`)
74
+ console.log(`Entries: ${viewerData.totalEntries} | Edges: ${viewerData.edges.length}`)
75
+ openBrowser(outFile)
76
+ }
@@ -0,0 +1 @@
1
+ export { viewer as viewerServe } from "./index"
@@ -0,0 +1,29 @@
1
+ export interface ViewerNode {
2
+ id: string
3
+ category: string
4
+ content: string
5
+ tags: string[]
6
+ quality: number
7
+ accessCount: number
8
+ date: string
9
+ file: string
10
+ links: string[]
11
+ ttl: string
12
+ lastAccessed: string
13
+ }
14
+
15
+ export interface ViewerData {
16
+ nodes: ViewerNode[]
17
+ edges: { source: string; target: string }[]
18
+ categories: Record<string, number>
19
+ tagCounts: Record<string, number>
20
+ totalEntries: number
21
+ }
22
+
23
+ export const COLORS: Record<string, string> = {
24
+ decision: "#a78bfa",
25
+ bug: "#ef4444",
26
+ pattern: "#22c55e",
27
+ knowledge: "#06b6d4",
28
+ architecture: "#f59e0b",
29
+ }