@bahulam/code 0.1.1
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.
- package/README.md +93 -0
- package/package.json +56 -0
- package/pulse/app/activity/page.tsx +190 -0
- package/pulse/app/api/activity/route.ts +138 -0
- package/pulse/app/api/benchmark/route.ts +113 -0
- package/pulse/app/api/benchmarks/route.ts +195 -0
- package/pulse/app/api/costs/route.ts +88 -0
- package/pulse/app/api/export/route.ts +77 -0
- package/pulse/app/api/history/route.ts +11 -0
- package/pulse/app/api/import/route.ts +31 -0
- package/pulse/app/api/memory/route.ts +50 -0
- package/pulse/app/api/plans/route.ts +9 -0
- package/pulse/app/api/projects/[slug]/route.ts +96 -0
- package/pulse/app/api/projects/route.ts +121 -0
- package/pulse/app/api/sessions/[id]/replay/route.ts +20 -0
- package/pulse/app/api/sessions/[id]/route.ts +31 -0
- package/pulse/app/api/sessions/route.ts +112 -0
- package/pulse/app/api/settings/route.ts +14 -0
- package/pulse/app/api/stats/route.ts +143 -0
- package/pulse/app/api/todos/route.ts +9 -0
- package/pulse/app/api/tools/route.ts +160 -0
- package/pulse/app/benchmarks/page.tsx +224 -0
- package/pulse/app/costs/page.tsx +179 -0
- package/pulse/app/export/page.tsx +465 -0
- package/pulse/app/favicon.ico +0 -0
- package/pulse/app/globals.css +263 -0
- package/pulse/app/help/page.tsx +143 -0
- package/pulse/app/history/page.tsx +157 -0
- package/pulse/app/layout.tsx +46 -0
- package/pulse/app/memory/page.tsx +365 -0
- package/pulse/app/overview-client.tsx +393 -0
- package/pulse/app/page.tsx +14 -0
- package/pulse/app/plans/page.tsx +308 -0
- package/pulse/app/projects/[slug]/page.tsx +390 -0
- package/pulse/app/projects/page.tsx +110 -0
- package/pulse/app/sessions/[id]/page.tsx +243 -0
- package/pulse/app/sessions/page.tsx +39 -0
- package/pulse/app/settings/page.tsx +188 -0
- package/pulse/app/todos/page.tsx +211 -0
- package/pulse/app/tools/page.tsx +249 -0
- package/pulse/cli.js +164 -0
- package/pulse/components/activity/day-of-week-chart.tsx +35 -0
- package/pulse/components/activity/streak-card.tsx +36 -0
- package/pulse/components/costs/cache-efficiency-panel.tsx +76 -0
- package/pulse/components/costs/cost-by-project-chart.tsx +48 -0
- package/pulse/components/costs/cost-over-time-chart.tsx +95 -0
- package/pulse/components/costs/model-token-table.tsx +60 -0
- package/pulse/components/global-search.tsx +193 -0
- package/pulse/components/keyboard-nav-provider.tsx +23 -0
- package/pulse/components/layout/bottom-nav.tsx +53 -0
- package/pulse/components/layout/client-layout.tsx +31 -0
- package/pulse/components/layout/sidebar-context.tsx +50 -0
- package/pulse/components/layout/sidebar.tsx +183 -0
- package/pulse/components/layout/top-bar.tsx +121 -0
- package/pulse/components/overview/activity-heatmap.tsx +107 -0
- package/pulse/components/overview/conversation-table.tsx +148 -0
- package/pulse/components/overview/model-breakdown-donut.tsx +95 -0
- package/pulse/components/overview/peak-hours-chart.tsx +87 -0
- package/pulse/components/overview/project-activity-donut.tsx +96 -0
- package/pulse/components/overview/stat-card.tsx +102 -0
- package/pulse/components/overview/usage-over-time-chart.tsx +166 -0
- package/pulse/components/projects/project-card.tsx +175 -0
- package/pulse/components/sessions/replay/assistant-markdown.tsx +94 -0
- package/pulse/components/sessions/replay/compaction-card.tsx +25 -0
- package/pulse/components/sessions/replay/session-sidebar.tsx +231 -0
- package/pulse/components/sessions/replay/token-accumulation-chart.tsx +98 -0
- package/pulse/components/sessions/replay/tool-call-badge.tsx +127 -0
- package/pulse/components/sessions/replay/turn-cards.tsx +220 -0
- package/pulse/components/sessions/replay/user-tool-result.tsx +158 -0
- package/pulse/components/sessions/session-badges.tsx +49 -0
- package/pulse/components/sessions/session-table.tsx +299 -0
- package/pulse/components/theme-provider.tsx +44 -0
- package/pulse/components/tools/feature-adoption-table.tsx +58 -0
- package/pulse/components/tools/mcp-server-panel.tsx +45 -0
- package/pulse/components/tools/tool-ranking-chart.tsx +57 -0
- package/pulse/components/tools/version-history-table.tsx +32 -0
- package/pulse/components/ui/alert.tsx +66 -0
- package/pulse/components/ui/badge.tsx +48 -0
- package/pulse/components/ui/breadcrumb.tsx +109 -0
- package/pulse/components/ui/button.tsx +64 -0
- package/pulse/components/ui/calendar.tsx +220 -0
- package/pulse/components/ui/card.tsx +92 -0
- package/pulse/components/ui/command.tsx +158 -0
- package/pulse/components/ui/dialog.tsx +158 -0
- package/pulse/components/ui/input.tsx +21 -0
- package/pulse/components/ui/popover.tsx +89 -0
- package/pulse/components/ui/progress.tsx +31 -0
- package/pulse/components/ui/select.tsx +190 -0
- package/pulse/components/ui/separator.tsx +28 -0
- package/pulse/components/ui/sheet.tsx +143 -0
- package/pulse/components/ui/skeleton.tsx +13 -0
- package/pulse/components/ui/table.tsx +116 -0
- package/pulse/components/ui/tabs.tsx +91 -0
- package/pulse/components/ui/tooltip.tsx +57 -0
- package/pulse/components/use-global-keyboard-nav.ts +79 -0
- package/pulse/components.json +23 -0
- package/pulse/eslint.config.mjs +18 -0
- package/pulse/lib/bahulam-paths.ts +23 -0
- package/pulse/lib/claude-reader.ts +592 -0
- package/pulse/lib/decode.ts +129 -0
- package/pulse/lib/pricing.ts +102 -0
- package/pulse/lib/replay-parser.ts +165 -0
- package/pulse/lib/tool-categories.ts +127 -0
- package/pulse/lib/utils.ts +6 -0
- package/pulse/next-env.d.ts +6 -0
- package/pulse/next.config.ts +16 -0
- package/pulse/package.json +45 -0
- package/pulse/postcss.config.mjs +7 -0
- package/pulse/public/activity.png +0 -0
- package/pulse/public/cc-lens.png +0 -0
- package/pulse/public/command-k.png +0 -0
- package/pulse/public/costs.png +0 -0
- package/pulse/public/dashboard-dark.png +0 -0
- package/pulse/public/dashboard-white.png +0 -0
- package/pulse/public/export.png +0 -0
- package/pulse/public/file.svg +1 -0
- package/pulse/public/globe.svg +1 -0
- package/pulse/public/next.svg +1 -0
- package/pulse/public/projects.png +0 -0
- package/pulse/public/session-chat.png +0 -0
- package/pulse/public/todos.png +0 -0
- package/pulse/public/tools.png +0 -0
- package/pulse/public/vercel.svg +1 -0
- package/pulse/public/window.svg +1 -0
- package/pulse/tsconfig.json +34 -0
- package/pulse/types/claude.ts +294 -0
- package/src/agents/loader.mjs +94 -0
- package/src/agents/multi_workflow_loader.mjs +330 -0
- package/src/agents/parser.mjs +205 -0
- package/src/agents/scaffold.mjs +223 -0
- package/src/agents/teams.mjs +123 -0
- package/src/agents/workflow_loader.mjs +122 -0
- package/src/agents/workflow_scaffold.mjs +249 -0
- package/src/auth/oauth.mjs +220 -0
- package/src/auth/tarang-auth.mjs +312 -0
- package/src/commands/agent.mjs +221 -0
- package/src/commands/workflow.mjs +581 -0
- package/src/config/cli-args.mjs +202 -0
- package/src/config/env.mjs +263 -0
- package/src/config/hook-runner.mjs +100 -0
- package/src/config/memory-loader.mjs +32 -0
- package/src/config/model-catalog.mjs +57 -0
- package/src/config/settings-loader.mjs +45 -0
- package/src/config/settings.mjs +132 -0
- package/src/context/ast-parser.mjs +298 -0
- package/src/context/bm25.mjs +85 -0
- package/src/context/prose-chunker.mjs +255 -0
- package/src/context/retriever.mjs +425 -0
- package/src/context/skeleton.mjs +134 -0
- package/src/context/symbol-indexer.mjs +375 -0
- package/src/core/agent-history.mjs +111 -0
- package/src/core/agent-loop.mjs +486 -0
- package/src/core/approval-log.mjs +145 -0
- package/src/core/approval.mjs +700 -0
- package/src/core/attachments.mjs +666 -0
- package/src/core/backend-url.mjs +68 -0
- package/src/core/bundled-runtime.mjs +418 -0
- package/src/core/cache-control.mjs +92 -0
- package/src/core/cache.mjs +105 -0
- package/src/core/callback-client.mjs +180 -0
- package/src/core/checkpoints.mjs +142 -0
- package/src/core/compact-history.mjs +127 -0
- package/src/core/context-envelope.mjs +54 -0
- package/src/core/context-manager.mjs +198 -0
- package/src/core/error-guidance.mjs +331 -0
- package/src/core/file-diff.mjs +217 -0
- package/src/core/headless.mjs +460 -0
- package/src/core/hooks-manager.mjs +87 -0
- package/src/core/jsonl-writer.mjs +449 -0
- package/src/core/local-agent.mjs +538 -0
- package/src/core/local-store.mjs +836 -0
- package/src/core/mode-selector.mjs +51 -0
- package/src/core/output-filter.mjs +177 -0
- package/src/core/paths.mjs +190 -0
- package/src/core/policy-resolver.mjs +156 -0
- package/src/core/pricing.mjs +336 -0
- package/src/core/project-artifacts.mjs +39 -0
- package/src/core/project-context-loader.mjs +139 -0
- package/src/core/providers.mjs +219 -0
- package/src/core/rate-limit-display.mjs +121 -0
- package/src/core/rate-limiter.mjs +119 -0
- package/src/core/resume-mode.mjs +192 -0
- package/src/core/risk-tier.mjs +388 -0
- package/src/core/safety.mjs +260 -0
- package/src/core/scheduler.mjs +173 -0
- package/src/core/session-manager.mjs +360 -0
- package/src/core/session.mjs +143 -0
- package/src/core/settings-sync.mjs +85 -0
- package/src/core/stagnation.mjs +57 -0
- package/src/core/stream-client.mjs +957 -0
- package/src/core/streaming.mjs +182 -0
- package/src/core/system-prompt.mjs +140 -0
- package/src/core/tasks.mjs +196 -0
- package/src/core/tool-executor.mjs +2231 -0
- package/src/core/trust.mjs +160 -0
- package/src/core/work-scope.mjs +248 -0
- package/src/hooks/engine.mjs +162 -0
- package/src/mcp/client.mjs +253 -0
- package/src/mcp/transport-shttp.mjs +130 -0
- package/src/mcp/transport-sse.mjs +131 -0
- package/src/mcp/transport-ws.mjs +134 -0
- package/src/onboarding/preflight.mjs +374 -0
- package/src/permissions/checker.mjs +57 -0
- package/src/permissions/command-classifier.mjs +700 -0
- package/src/permissions/injection-check.mjs +60 -0
- package/src/permissions/path-check.mjs +102 -0
- package/src/permissions/prompt.mjs +73 -0
- package/src/permissions/sandbox.mjs +112 -0
- package/src/plugins/loader.mjs +138 -0
- package/src/skills/installer.mjs +188 -0
- package/src/skills/loader.mjs +252 -0
- package/src/skills/runner.mjs +55 -0
- package/src/state/orbit.mjs +263 -0
- package/src/state/verbosity.mjs +99 -0
- package/src/telemetry/index.mjs +122 -0
- package/src/terminal/agents.mjs +353 -0
- package/src/terminal/analytics.mjs +292 -0
- package/src/terminal/ansi.mjs +695 -0
- package/src/terminal/init.mjs +145 -0
- package/src/terminal/main.mjs +310 -0
- package/src/terminal/repl-ask-form.mjs +120 -0
- package/src/terminal/repl-explore.mjs +44 -0
- package/src/terminal/repl-format.mjs +317 -0
- package/src/terminal/repl-model-form.mjs +132 -0
- package/src/terminal/repl-render.mjs +833 -0
- package/src/terminal/repl-resume.mjs +640 -0
- package/src/terminal/repl-state.mjs +120 -0
- package/src/terminal/repl-utils.mjs +34 -0
- package/src/terminal/repl.mjs +5032 -0
- package/src/terminal/skills.mjs +54 -0
- package/src/terminal/tool-display.mjs +392 -0
- package/src/tools/agent.mjs +137 -0
- package/src/tools/ask-user.mjs +61 -0
- package/src/tools/bash.mjs +231 -0
- package/src/tools/cron-create.mjs +120 -0
- package/src/tools/cron-delete.mjs +49 -0
- package/src/tools/cron-list.mjs +37 -0
- package/src/tools/edit.mjs +82 -0
- package/src/tools/enter-worktree.mjs +69 -0
- package/src/tools/exit-worktree.mjs +57 -0
- package/src/tools/glob.mjs +117 -0
- package/src/tools/grep.mjs +129 -0
- package/src/tools/lint.mjs +71 -0
- package/src/tools/ls.mjs +58 -0
- package/src/tools/lsp.mjs +115 -0
- package/src/tools/multi-edit.mjs +94 -0
- package/src/tools/notebook-edit.mjs +96 -0
- package/src/tools/project-overview.mjs +703 -0
- package/src/tools/read-mcp-resource.mjs +57 -0
- package/src/tools/read.mjs +138 -0
- package/src/tools/registry.mjs +116 -0
- package/src/tools/remote-trigger.mjs +84 -0
- package/src/tools/send-message.mjs +64 -0
- package/src/tools/skill.mjs +52 -0
- package/src/tools/test-runner.mjs +49 -0
- package/src/tools/todo-write.mjs +68 -0
- package/src/tools/tool-search.mjs +77 -0
- package/src/tools/web-fetch.mjs +65 -0
- package/src/tools/web-search.mjs +89 -0
- package/src/tools/write.mjs +55 -0
- package/src/ui/approval.mjs +510 -0
- package/src/ui/banner.mjs +232 -0
- package/src/ui/commands.mjs +537 -0
- package/src/ui/formatter.mjs +409 -0
- package/src/ui/icons.mjs +170 -0
- package/src/ui/input-dock.mjs +772 -0
- package/src/ui/markdown.mjs +278 -0
- package/src/ui/mission-report.mjs +296 -0
- package/src/ui/palette.mjs +189 -0
- package/src/ui/render-queue.mjs +500 -0
- package/src/ui/slash-commands.mjs +257 -0
- package/src/ui/spinner.mjs +116 -0
- package/src/ui/sub-agent.mjs +167 -0
- package/src/ui/term.mjs +174 -0
- package/src/ui/text-layout.mjs +127 -0
- package/src/ui/tool-card.mjs +740 -0
- package/src/ui/tool-details.mjs +504 -0
- package/src/ui/transcript-block.mjs +20 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import * as http from 'node:http';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { spawn } from 'node:child_process';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
getRecentSessions,
|
|
8
|
+
getSessionStats,
|
|
9
|
+
getToolBreakdown,
|
|
10
|
+
getModelBreakdown,
|
|
11
|
+
getHistory,
|
|
12
|
+
getSessionDetail,
|
|
13
|
+
getStorePaths,
|
|
14
|
+
} from '../core/local-store.mjs';
|
|
15
|
+
|
|
16
|
+
const require = createRequire(import.meta.url);
|
|
17
|
+
const { version } = require('../../package.json');
|
|
18
|
+
|
|
19
|
+
function parseNumber(value, fallback) {
|
|
20
|
+
const num = Number.parseInt(value, 10);
|
|
21
|
+
return Number.isFinite(num) && num >= 0 ? num : fallback;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function readOption(args, name, fallback = null) {
|
|
25
|
+
const idx = args.indexOf(name);
|
|
26
|
+
if (idx === -1 || idx === args.length - 1) return fallback;
|
|
27
|
+
return args[idx + 1];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function hasFlag(args, name) {
|
|
31
|
+
return args.includes(name);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function basename(projectPath) {
|
|
35
|
+
return projectPath ? path.basename(projectPath) : 'unknown';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function formatNumber(num) {
|
|
39
|
+
return new Intl.NumberFormat('en-US').format(num || 0);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function formatWhen(value) {
|
|
43
|
+
if (!value) return 'n/a';
|
|
44
|
+
const date = new Date(value);
|
|
45
|
+
return Number.isNaN(date.getTime()) ? 'n/a' : date.toLocaleString();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function formatDuration(startTime, endTime) {
|
|
49
|
+
if (!startTime || !endTime) return 'n/a';
|
|
50
|
+
const ms = new Date(endTime).getTime() - new Date(startTime).getTime();
|
|
51
|
+
if (!Number.isFinite(ms) || ms < 0) return 'n/a';
|
|
52
|
+
if (ms < 1000) return `${ms}ms`;
|
|
53
|
+
const seconds = Math.round(ms / 100) / 10;
|
|
54
|
+
if (seconds < 60) return `${seconds}s`;
|
|
55
|
+
const minutes = Math.floor(seconds / 60);
|
|
56
|
+
const rem = Math.round((seconds % 60) * 10) / 10;
|
|
57
|
+
return `${minutes}m ${rem}s`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function truncate(text, max = 80) {
|
|
61
|
+
if (!text) return '';
|
|
62
|
+
return text.length > max ? `${text.slice(0, max - 3)}...` : text;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function relativeDaysLabel(days) {
|
|
66
|
+
return days === 0 ? 'all time' : `last ${days} days`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function extractPrompt(entry) {
|
|
70
|
+
return truncate(entry?.display || '', 120);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function formatSessionsReport(sessions, limit) {
|
|
74
|
+
if (!sessions.length) {
|
|
75
|
+
return 'No local sessions found in ~/.bahulam/projects.\n';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const lines = [];
|
|
79
|
+
lines.push(`BAHULAM SESSIONS ${sessions.length} shown`);
|
|
80
|
+
lines.push(`Recent local transcripts from ~/.bahulam/projects (limit ${limit})`);
|
|
81
|
+
lines.push('');
|
|
82
|
+
|
|
83
|
+
for (const session of sessions) {
|
|
84
|
+
const model = session.models[0] || 'backend default';
|
|
85
|
+
const totalTokens = (session.inputTokens || 0) + (session.outputTokens || 0);
|
|
86
|
+
lines.push(`${formatWhen(session.endTime || session.startTime)} ${basename(session.project)}`);
|
|
87
|
+
lines.push(` session ${session.sessionId}`);
|
|
88
|
+
lines.push(` prompt ${truncate(session.firstPrompt || '(no prompt captured)', 96)}`);
|
|
89
|
+
lines.push(` usage ${formatNumber(totalTokens)} tok ${formatNumber(session.toolCalls.reduce((sum, tool) => sum + tool.count, 0))} tools ${session.userMessages} user / ${session.assistantMessages} assistant`);
|
|
90
|
+
lines.push(` model ${model}${session.gitBranch ? ` git:${session.gitBranch}` : ''}`);
|
|
91
|
+
lines.push('');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return lines.join('\n');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function formatStatsReport(stats, tools, models, days, paths) {
|
|
98
|
+
const lines = [];
|
|
99
|
+
lines.push(`BAHULAM STATS ${relativeDaysLabel(days)}`);
|
|
100
|
+
lines.push(`Store: ${paths.bahulamDir}`);
|
|
101
|
+
lines.push('');
|
|
102
|
+
lines.push(`Sessions ${formatNumber(stats.totalSessions)}`);
|
|
103
|
+
lines.push(`Messages ${formatNumber(stats.totalUserMessages + stats.totalAssistantMessages)} (${formatNumber(stats.totalUserMessages)} user, ${formatNumber(stats.totalAssistantMessages)} assistant)`);
|
|
104
|
+
lines.push(`Tokens ${formatNumber(stats.totalInputTokens + stats.totalOutputTokens)} (${formatNumber(stats.totalInputTokens)} in, ${formatNumber(stats.totalOutputTokens)} out)`);
|
|
105
|
+
lines.push(`Cache Read ${formatNumber(stats.totalCacheReadTokens)}`);
|
|
106
|
+
lines.push(`Tool Calls ${formatNumber(stats.totalToolCalls)}`);
|
|
107
|
+
lines.push('');
|
|
108
|
+
|
|
109
|
+
lines.push('Top Tools');
|
|
110
|
+
if (tools.length === 0) {
|
|
111
|
+
lines.push(' none');
|
|
112
|
+
} else {
|
|
113
|
+
for (const tool of tools.slice(0, 8)) {
|
|
114
|
+
lines.push(` ${tool.name.padEnd(18)} ${formatNumber(tool.count)}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
lines.push('');
|
|
118
|
+
|
|
119
|
+
lines.push('Models');
|
|
120
|
+
if (models.length === 0) {
|
|
121
|
+
lines.push(' none');
|
|
122
|
+
} else {
|
|
123
|
+
for (const model of models.slice(0, 8)) {
|
|
124
|
+
lines.push(` ${truncate(model.model, 42).padEnd(42)} ${formatNumber(model.sessions)} sessions`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
lines.push('');
|
|
128
|
+
|
|
129
|
+
return lines.join('\n');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function formatHistoryReport(entries, limit) {
|
|
133
|
+
if (!entries.length) {
|
|
134
|
+
return 'No local prompt history found in ~/.bahulam/history.jsonl.\n';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const lines = [];
|
|
138
|
+
lines.push(`BAHULAM HISTORY ${entries.length} shown`);
|
|
139
|
+
lines.push(`Recent prompts from ~/.bahulam/history.jsonl (limit ${limit})`);
|
|
140
|
+
lines.push('');
|
|
141
|
+
|
|
142
|
+
for (const entry of entries) {
|
|
143
|
+
lines.push(`${formatWhen(entry.timestamp)} ${basename(entry.project)}`);
|
|
144
|
+
lines.push(` ${extractPrompt(entry)}`);
|
|
145
|
+
lines.push('');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return lines.join('\n');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function json(res, status, payload) {
|
|
152
|
+
res.writeHead(status, {
|
|
153
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
154
|
+
'Cache-Control': 'no-store',
|
|
155
|
+
});
|
|
156
|
+
res.end(JSON.stringify(payload));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function escapeHtml(value) {
|
|
160
|
+
return String(value)
|
|
161
|
+
.replace(/&/g, '&')
|
|
162
|
+
.replace(/</g, '<')
|
|
163
|
+
.replace(/>/g, '>')
|
|
164
|
+
.replace(/"/g, '"');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function openBrowser(url) {
|
|
168
|
+
try {
|
|
169
|
+
let child;
|
|
170
|
+
if (process.platform === 'darwin') {
|
|
171
|
+
child = spawn('open', [url], { stdio: 'ignore', detached: true });
|
|
172
|
+
} else if (process.platform === 'win32') {
|
|
173
|
+
child = spawn('cmd', ['/c', 'start', '', url], { stdio: 'ignore', detached: true });
|
|
174
|
+
} else {
|
|
175
|
+
child = spawn('xdg-open', [url], { stdio: 'ignore', detached: true });
|
|
176
|
+
}
|
|
177
|
+
child.unref();
|
|
178
|
+
return true;
|
|
179
|
+
} catch {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export async function runSessionsCommand(args = []) {
|
|
185
|
+
const limit = parseNumber(readOption(args, '--limit', '20'), 20);
|
|
186
|
+
const sessions = getRecentSessions(limit);
|
|
187
|
+
process.stdout.write(formatSessionsReport(sessions, limit));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export async function runStatsCommand(args = []) {
|
|
191
|
+
const days = parseNumber(readOption(args, '--days', '30'), 30);
|
|
192
|
+
const stats = getSessionStats(days);
|
|
193
|
+
const tools = getToolBreakdown(days);
|
|
194
|
+
const models = getModelBreakdown(days);
|
|
195
|
+
const paths = getStorePaths();
|
|
196
|
+
process.stdout.write(formatStatsReport(stats, tools, models, days, paths));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function runHistoryCommand(args = []) {
|
|
200
|
+
const limit = parseNumber(readOption(args, '--limit', '50'), 50);
|
|
201
|
+
const history = getHistory(limit);
|
|
202
|
+
process.stdout.write(formatHistoryReport(history, limit));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Dashboard removed — now using Bahulam Pulse (pulse/cli.js)
|
|
206
|
+
|
|
207
|
+
export async function _runDashboardCommand_REMOVED() { /* see pulse/cli.js */ }
|
|
208
|
+
|
|
209
|
+
export async function _OLD_runDashboardCommand(args = []) {
|
|
210
|
+
const requestedPort = parseNumber(readOption(args, '--port', '4318'), 4318);
|
|
211
|
+
const host = readOption(args, '--host', '127.0.0.1') || '127.0.0.1';
|
|
212
|
+
const shouldOpen = !hasFlag(args, '--no-open');
|
|
213
|
+
|
|
214
|
+
const server = http.createServer(async (req, res) => {
|
|
215
|
+
try {
|
|
216
|
+
const url = new URL(req.url || '/', `http://${req.headers.host || `${host}:${requestedPort}`}`);
|
|
217
|
+
|
|
218
|
+
if (url.pathname === '/') {
|
|
219
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
220
|
+
res.end(dashboardHtml());
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (url.pathname === '/api/overview') {
|
|
225
|
+
const days = parseNumber(url.searchParams.get('days') || '30', 30);
|
|
226
|
+
const limit = parseNumber(url.searchParams.get('limit') || '18', 18);
|
|
227
|
+
const historyLimit = parseNumber(url.searchParams.get('history_limit') || '40', 40);
|
|
228
|
+
const [stats, tools, models, sessions] = await Promise.all([
|
|
229
|
+
getSessionStats(days),
|
|
230
|
+
getToolBreakdown(days),
|
|
231
|
+
getModelBreakdown(days),
|
|
232
|
+
getRecentSessions(limit),
|
|
233
|
+
]);
|
|
234
|
+
json(res, 200, {
|
|
235
|
+
stats,
|
|
236
|
+
tools,
|
|
237
|
+
models,
|
|
238
|
+
sessions,
|
|
239
|
+
history: getHistory(historyLimit),
|
|
240
|
+
paths: getStorePaths(),
|
|
241
|
+
});
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (url.pathname === '/api/session') {
|
|
246
|
+
const sessionId = url.searchParams.get('session_id') || '';
|
|
247
|
+
if (!sessionId) {
|
|
248
|
+
json(res, 400, { error: 'session_id is required' });
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const detail = await getSessionDetail(sessionId);
|
|
252
|
+
if (!detail) {
|
|
253
|
+
json(res, 404, { error: 'Session not found' });
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
json(res, 200, detail);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
json(res, 404, { error: 'Not found' });
|
|
261
|
+
} catch (err) {
|
|
262
|
+
json(res, 500, { error: err.message || 'Internal error' });
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
await new Promise((resolve, reject) => {
|
|
267
|
+
server.once('error', reject);
|
|
268
|
+
server.listen(requestedPort, host, resolve);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const address = server.address();
|
|
272
|
+
const actualPort = typeof address === 'object' && address ? address.port : requestedPort;
|
|
273
|
+
const url = `http://${host}:${actualPort}`;
|
|
274
|
+
|
|
275
|
+
process.stderr.write(`\x1b[36mb0 dashboard\x1b[0m ${url}\n`);
|
|
276
|
+
process.stderr.write(`\x1b[2mReading local analytics from ${getStorePaths().bahulamDir}\x1b[0m\n`);
|
|
277
|
+
process.stderr.write(`\x1b[2mPress Ctrl+C to stop the dashboard server.\x1b[0m\n`);
|
|
278
|
+
|
|
279
|
+
if (shouldOpen) {
|
|
280
|
+
const opened = openBrowser(url);
|
|
281
|
+
if (!opened) {
|
|
282
|
+
process.stderr.write(`\x1b[33mUnable to open a browser automatically. Open ${url} manually.\x1b[0m\n`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const shutdown = () => {
|
|
287
|
+
server.close(() => process.exit(0));
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
process.on('SIGINT', shutdown);
|
|
291
|
+
process.on('SIGTERM', shutdown);
|
|
292
|
+
}
|