@raquezha/notrace 0.1.2 → 0.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.
- package/CHANGELOG.md +10 -0
- package/dist/notrace/index.js +2 -1
- package/dist/notrace/report-app/__tests__/analytics.test.d.ts +1 -0
- package/dist/notrace/report-app/__tests__/analytics.test.js +35 -0
- package/dist/notrace/report-app/__tests__/card.test.d.ts +1 -0
- package/dist/notrace/report-app/__tests__/card.test.js +26 -0
- package/dist/notrace/report-app/__tests__/event.test.d.ts +1 -0
- package/dist/notrace/report-app/__tests__/event.test.js +20 -0
- package/dist/notrace/report-app/__tests__/format.test.d.ts +1 -0
- package/dist/notrace/report-app/__tests__/format.test.js +41 -0
- package/dist/notrace/report-app/__tests__/report.test.d.ts +1 -0
- package/dist/notrace/report-app/__tests__/report.test.js +31 -0
- package/dist/notrace/report-app/analytics.d.ts +3 -0
- package/dist/notrace/report-app/analytics.js +78 -0
- package/dist/notrace/report-app/client.d.ts +2 -0
- package/dist/notrace/report-app/client.js +105 -0
- package/dist/notrace/report-app/components/card.d.ts +4 -0
- package/dist/notrace/report-app/components/card.js +36 -0
- package/dist/notrace/report-app/components/dashboard.d.ts +1 -0
- package/dist/notrace/report-app/components/dashboard.js +16 -0
- package/dist/notrace/report-app/components/event.d.ts +5 -0
- package/dist/notrace/report-app/components/event.js +42 -0
- package/dist/notrace/report-app/components/message.d.ts +2 -0
- package/dist/notrace/report-app/components/message.js +43 -0
- package/dist/notrace/report-app/dashboard-report.d.ts +1 -0
- package/dist/notrace/report-app/dashboard-report.js +6 -0
- package/dist/notrace/report-app/escape.d.ts +1 -0
- package/dist/notrace/report-app/escape.js +10 -0
- package/dist/notrace/report-app/format.d.ts +13 -0
- package/dist/notrace/report-app/format.js +102 -0
- package/dist/notrace/report-app/report.d.ts +1 -0
- package/dist/notrace/report-app/report.js +29 -0
- package/dist/notrace/report-app/shell.d.ts +5 -0
- package/dist/notrace/report-app/shell.js +19 -0
- package/dist/notrace/report-app/styles.d.ts +1 -0
- package/dist/notrace/report-app/styles.js +431 -0
- package/dist/notrace/report-app/types.d.ts +28 -0
- package/dist/notrace/report-app/types.js +1 -0
- package/extensions/notrace/index.ts +2 -1
- package/extensions/notrace/report-app/__tests__/analytics.test.ts +41 -0
- package/extensions/notrace/report-app/__tests__/card.test.ts +29 -0
- package/extensions/notrace/report-app/__tests__/event.test.ts +23 -0
- package/extensions/notrace/report-app/__tests__/format.test.ts +46 -0
- package/extensions/notrace/report-app/__tests__/report.test.ts +33 -0
- package/extensions/notrace/report-app/analytics.ts +79 -0
- package/extensions/notrace/report-app/client.ts +106 -0
- package/extensions/notrace/report-app/components/card.ts +38 -0
- package/extensions/notrace/report-app/components/dashboard.ts +17 -0
- package/extensions/notrace/report-app/components/event.ts +39 -0
- package/extensions/notrace/report-app/components/message.ts +39 -0
- package/extensions/notrace/report-app/dashboard-report.ts +7 -0
- package/extensions/notrace/report-app/escape.ts +10 -0
- package/extensions/notrace/report-app/format.ts +107 -0
- package/extensions/notrace/report-app/report.ts +33 -0
- package/extensions/notrace/report-app/shell.ts +24 -0
- package/extensions/notrace/report-app/styles.ts +431 -0
- package/extensions/notrace/report-app/types.ts +35 -0
- package/package.json +1 -1
- package/templates/dashboard.sample.html +103 -63
- package/templates/dashboard.sample.json +73 -10
- package/templates/render-samples.mjs +119 -1
- package/templates/session.sample.html +125 -168
- package/templates/session.sample.json +66 -7
- package/templates/sessions/019ed2ee-1000-76ee-b353-000000000001/notrace.html +125 -163
- package/templates/sessions/019ed2ee-1000-76ee-b353-000000000001/notrace.json +50 -0
- package/templates/sessions/019ed2ee-1001-76ee-b353-000000000002/notrace.html +125 -162
- package/templates/sessions/019ed2ee-1001-76ee-b353-000000000002/notrace.json +50 -0
- package/templates/sessions/019ed2ee-1002-76ee-b353-000000000003/notrace.html +125 -163
- package/templates/sessions/019ed2ee-1002-76ee-b353-000000000003/notrace.json +50 -0
- package/templates/sessions/019ed2ee-massive/notrace.html +498 -0
- package/templates/sessions/019ed2ee-massive/notrace.json +14660 -0
- package/tsconfig.json +1 -1
- package/dist/notrace/renderer.d.ts +0 -4
- package/dist/notrace/renderer.js +0 -800
- package/extensions/notrace/renderer.ts +0 -810
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export type WorkflowName = "norpiv" | "research" | "generic" | string;
|
|
2
|
+
export type TaskLike = {
|
|
3
|
+
workflow?: WorkflowName | null;
|
|
4
|
+
id?: string | null;
|
|
5
|
+
};
|
|
6
|
+
export type Taskish = {
|
|
7
|
+
task?: TaskLike | null;
|
|
8
|
+
workflow?: WorkflowName | null;
|
|
9
|
+
taskId?: string | null;
|
|
10
|
+
};
|
|
11
|
+
export type RepositoryLike = {
|
|
12
|
+
name?: string | null;
|
|
13
|
+
branch?: string | null;
|
|
14
|
+
};
|
|
15
|
+
export type Repoish = {
|
|
16
|
+
repository?: RepositoryLike | null;
|
|
17
|
+
repositoryName?: string | null;
|
|
18
|
+
repoName?: string | null;
|
|
19
|
+
};
|
|
20
|
+
export type StatusLike = {
|
|
21
|
+
status?: string | null;
|
|
22
|
+
};
|
|
23
|
+
export type EventLike = {
|
|
24
|
+
type?: string | null;
|
|
25
|
+
};
|
|
26
|
+
export type DateValue = string | number;
|
|
27
|
+
export type MaybeNumber = number | null | undefined;
|
|
28
|
+
export type MaybeString = string | null | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -13,7 +13,8 @@ import type {
|
|
|
13
13
|
WorkflowContext,
|
|
14
14
|
} from "./types.js";
|
|
15
15
|
import { getActiveAdapter, type WorkflowAdapter } from "./adapters.js";
|
|
16
|
-
import { generateHtmlReport
|
|
16
|
+
import { generateHtmlReport } from "./report-app/report.js";
|
|
17
|
+
import { generateDashboardHtml } from "./report-app/dashboard-report.js";
|
|
17
18
|
|
|
18
19
|
const REDACTED = "[REDACTED by notrace]";
|
|
19
20
|
const SENSITIVE_VALUE_RE = /(bearer\s+[a-z0-9._~+/=-]{12,}|sk-[a-z0-9_-]{16,}|gh[pousr]_[a-z0-9_]{16,}|AKIA[0-9A-Z]{16})/gi;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { buildModelSummary, buildModelSwitches, groupByModel } from "../analytics.js";
|
|
3
|
+
|
|
4
|
+
const events = [
|
|
5
|
+
{ type: "llm_completion", model: "a", provider: "x", timestamp: 1000, usage: { input: 10, output: 5, totalTokens: 15, cost: { total: 1 } } },
|
|
6
|
+
{ type: "llm_completion", model: "a", provider: "x", timestamp: "2026-06-17T17:00:02.000Z", usage: { input: 20, output: 10, totalTokens: 30, cost: { total: 2 } }, errorMessage: "oops" },
|
|
7
|
+
{ type: "llm_completion", model: "b", provider: "y", timestamp: "2026-06-17T17:00:03.000Z", usage: { input: 40, output: 20, cost: { total: 3 } } }, // missing totalTokens
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
describe("report-app analytics", () => {
|
|
11
|
+
it("groups model usage and falls back when totalTokens is missing", () => {
|
|
12
|
+
const grouped = groupByModel(events as any);
|
|
13
|
+
expect(grouped.a.count).toBe(2);
|
|
14
|
+
expect(grouped.a.inputTokens).toBe(30);
|
|
15
|
+
expect(grouped.a.errors).toBe(1);
|
|
16
|
+
|
|
17
|
+
// b is missing totalTokens, should fallback to input + output (40 + 20)
|
|
18
|
+
expect(grouped.b.totalTokens).toBe(60);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("tracks model switches and normalizes string timestamps", () => {
|
|
22
|
+
const switches = buildModelSwitches(events as any);
|
|
23
|
+
expect(switches).toHaveLength(1);
|
|
24
|
+
expect(switches[0].from).toBe("a");
|
|
25
|
+
expect(switches[0].to).toBe("b");
|
|
26
|
+
expect(switches[0].providerChanged).toBe(true);
|
|
27
|
+
|
|
28
|
+
// timestamp Delta between "2026-06-17T17:00:02.000Z" and "2026-06-17T17:00:03.000Z" is 1000ms
|
|
29
|
+
expect(switches[0].timeDelta).toBe(1000);
|
|
30
|
+
// falls back to input + output when totalTokens is missing
|
|
31
|
+
expect(switches[0].tokens).toBe(60);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("builds summary", () => {
|
|
35
|
+
const summary = buildModelSummary(events as any);
|
|
36
|
+
expect(summary.firstModel).toBe("a");
|
|
37
|
+
expect(summary.finalModel).toBe("b");
|
|
38
|
+
expect(summary.switchCount).toBe(1);
|
|
39
|
+
expect(summary.uniqueModels).toBe(2);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { renderCollapsibleSection, renderKeyValueList, renderToolResultHtml, renderToolUseHtml } from "../components/card.js";
|
|
3
|
+
|
|
4
|
+
describe("report-app card", () => {
|
|
5
|
+
it("renders tool use and result cards", () => {
|
|
6
|
+
const useHtml = renderToolUseHtml("bash", { command: "echo hi" });
|
|
7
|
+
const resultHtml = renderToolResultHtml("tool-1", { stdout: "ok" }, true);
|
|
8
|
+
expect(useHtml).toContain("chat-tool-use");
|
|
9
|
+
expect(useHtml).toContain("bash");
|
|
10
|
+
expect(resultHtml).toContain("Tool Result: tool-1");
|
|
11
|
+
expect(resultHtml).toContain("color: var(--err);");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("escapes injected content", () => {
|
|
15
|
+
const html = renderToolUseHtml("<script>alert(1)</script>", "<script>alert(1)</script>");
|
|
16
|
+
expect(html).toContain("<script>alert(1)</script>");
|
|
17
|
+
expect(html).not.toContain("<script>alert(1)</script>");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("renders collapsible sections and key-value lists", () => {
|
|
21
|
+
const section = renderCollapsibleSection("Models", "<div>body</div>", true);
|
|
22
|
+
const kv = renderKeyValueList([["A", "B"], ["Empty", ""]]);
|
|
23
|
+
expect(section).toContain("panel collapsible");
|
|
24
|
+
expect(section).toContain(" open");
|
|
25
|
+
expect(kv).toContain("kv-list");
|
|
26
|
+
expect(kv).toContain("Empty");
|
|
27
|
+
expect(kv).toContain(">-<");
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { renderEventCard } from "../components/event.js";
|
|
3
|
+
|
|
4
|
+
describe("report-app event", () => {
|
|
5
|
+
it("escapes hostile content in lazy event body attributes", () => {
|
|
6
|
+
const hostileEvent = {
|
|
7
|
+
type: "llm_completion",
|
|
8
|
+
outputContent: '<img src=x onerror=alert(1)><script>alert(1)</script>" autofocus onfocus=alert(1)',
|
|
9
|
+
timestamp: 1000
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const html = renderEventCard(hostileEvent);
|
|
13
|
+
|
|
14
|
+
// The data-lazy-event-body attribute should contain the URI-encoded then HTML-escaped string.
|
|
15
|
+
// It must NOT contain the raw dangerous strings.
|
|
16
|
+
expect(html).not.toContain("<script>alert(1)</script>");
|
|
17
|
+
expect(html).not.toContain('onerror=alert(1)');
|
|
18
|
+
expect(html).toContain("data-lazy-event-body=");
|
|
19
|
+
// Double check that the encoded payload still holds the data safely.
|
|
20
|
+
expect(html).toContain("alert(1)"); // uri encoded form might just be alert(1) but the < > and " will be %3C %3E %22
|
|
21
|
+
expect(html).toContain("%26lt%3Bscript%26gt%3Balert(1)%26lt%3B%2Fscript%26gt%3B");
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { formatDateDay, formatMs, formatTelemetryStatus, formatTimeMinutes, formatTimeSeconds, formatTokens, formatUsd, parseDate, taskDisplay, workflowClassName, workflowDisplayName } from "../format.js";
|
|
3
|
+
|
|
4
|
+
describe("report-app format", () => {
|
|
5
|
+
it("parses valid dates and rejects invalid ones", () => {
|
|
6
|
+
expect(parseDate("2026-06-17T17:00:00Z")?.toISOString()).toBe("2026-06-17T17:00:00.000Z");
|
|
7
|
+
expect(parseDate("nope")).toBeNull();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("formats date and time pieces", () => {
|
|
11
|
+
expect(formatDateDay("2026-06-17T17:00:00Z")).toBe("2026-06-17");
|
|
12
|
+
expect(formatTimeMinutes("2026-06-17T17:00:00Z")).toMatch(/^17:00|\d{2}:\d{2}$/);
|
|
13
|
+
expect(formatTimeSeconds("2026-06-17T17:00:00Z")).toMatch(/^17:00:00|\d{2}:\d{2}:\d{2}$/);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("formats workflow labels and classes", () => {
|
|
17
|
+
expect(workflowDisplayName("norpiv")).toBe("RPIV");
|
|
18
|
+
expect(workflowDisplayName("research")).toBe("Research");
|
|
19
|
+
expect(workflowDisplayName(undefined)).toBe("Generic");
|
|
20
|
+
expect(workflowClassName("norpiv")).toBe("workflow-rpiv");
|
|
21
|
+
expect(workflowClassName("research")).toBe("workflow-research");
|
|
22
|
+
expect(workflowClassName(undefined)).toBe("workflow-generic");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("preserves old taskDisplay semantics for direct and nested shapes", () => {
|
|
26
|
+
expect(taskDisplay({ workflow: "research", id: "branch:main" } as any)).toBe("Branch main");
|
|
27
|
+
expect(taskDisplay({ task: { workflow: "norpiv", id: "NR-101" } } as any)).toBe("NR-101");
|
|
28
|
+
expect(taskDisplay({ workflow: "generic" } as any)).toBe("General session");
|
|
29
|
+
expect(taskDisplay({ workflow: "weird" } as any)).toBe("No active task");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("formats usd, tokens, durations, and telemetry status", () => {
|
|
33
|
+
expect(formatUsd(0)).toBe("$0");
|
|
34
|
+
expect(formatUsd(1.234567)).toBe("$1.23457");
|
|
35
|
+
expect(formatTokens(999)).toBe("999");
|
|
36
|
+
expect(formatTokens(1500)).toBe("1.5k");
|
|
37
|
+
expect(formatTokens(2_500_000)).toBe("2.50M");
|
|
38
|
+
expect(formatMs(0)).toBe("-");
|
|
39
|
+
expect(formatMs(15_000)).toBe("15s");
|
|
40
|
+
expect(formatMs(125_000)).toBe("2m 5s");
|
|
41
|
+
expect(formatMs(3_780_000)).toBe("1h 3m");
|
|
42
|
+
expect(formatTelemetryStatus("active")).toBe("Active");
|
|
43
|
+
expect(formatTelemetryStatus("loaded-disabled")).toBe("Loaded disabled");
|
|
44
|
+
expect(formatTelemetryStatus(undefined)).toBe("Unknown");
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { generateHtmlReport } from "../report.js";
|
|
3
|
+
|
|
4
|
+
const record = {
|
|
5
|
+
traceId: "session-1",
|
|
6
|
+
repository: { name: "nothing", branch: "main" },
|
|
7
|
+
session: { id: "session-1", startedAt: "2026-06-17T17:00:00Z", durationMs: 15000 },
|
|
8
|
+
task: { workflow: "research", id: "branch:main" },
|
|
9
|
+
captureMode: "full",
|
|
10
|
+
conditions: { providers: ["anthropic"] },
|
|
11
|
+
activity: {
|
|
12
|
+
llmCallCount: 1,
|
|
13
|
+
toolCallCount: 1,
|
|
14
|
+
toolErrorCount: 0,
|
|
15
|
+
durationMs: 15000,
|
|
16
|
+
totals: { inputTokens: 10, outputTokens: 5, cacheReadTokens: 0, cacheWriteTokens: 0, totalTokens: 15, totalCostUsd: 0.01 },
|
|
17
|
+
},
|
|
18
|
+
telemetry: { extensions: {} },
|
|
19
|
+
events: [
|
|
20
|
+
{ type: "llm_completion", model: "claude", provider: "anthropic", timestamp: 1710000000000, inputPayload: { messages: [{ role: "user", content: "hi" }] }, outputContent: "hello", usage: { totalTokens: 15, cost: { total: 0.01 } } },
|
|
21
|
+
],
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
describe("report-app report", () => {
|
|
25
|
+
it("renders structural markers", () => {
|
|
26
|
+
const html = generateHtmlReport(record as any);
|
|
27
|
+
expect(html).toContain("Session retrospective");
|
|
28
|
+
expect(html).toContain("Run Summary");
|
|
29
|
+
expect(html).toContain("Timeline");
|
|
30
|
+
expect(html).toContain("Branch main");
|
|
31
|
+
expect(html).toContain("claude");
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export function groupByModel(events: any[]): Record<string, any> {
|
|
2
|
+
const models: Record<string, any> = {};
|
|
3
|
+
for (const ev of events) {
|
|
4
|
+
if (ev.type !== "llm_completion") continue;
|
|
5
|
+
const name = ev.model || "unknown";
|
|
6
|
+
if (!models[name]) {
|
|
7
|
+
models[name] = { count: 0, inputTokens: 0, outputTokens: 0, totalTokens: 0, cost: 0, cacheRead: 0, cacheWrite: 0, errors: 0 };
|
|
8
|
+
}
|
|
9
|
+
const m = models[name];
|
|
10
|
+
m.count++;
|
|
11
|
+
if (ev.usage) {
|
|
12
|
+
const input = Number(ev.usage.inputTokens || ev.usage.input || 0);
|
|
13
|
+
const output = Number(ev.usage.outputTokens || ev.usage.output || 0);
|
|
14
|
+
const cacheR = Number(ev.usage.cacheReadTokens || ev.usage.cacheRead || 0);
|
|
15
|
+
const cacheW = Number(ev.usage.cacheWriteTokens || ev.usage.cacheWrite || 0);
|
|
16
|
+
m.inputTokens += input;
|
|
17
|
+
m.outputTokens += output;
|
|
18
|
+
m.cacheRead += cacheR;
|
|
19
|
+
m.cacheWrite += cacheW;
|
|
20
|
+
m.totalTokens += Number(ev.usage.totalTokens || (input + output + cacheR + cacheW));
|
|
21
|
+
m.cost += Number(ev.usage.cost?.total || 0);
|
|
22
|
+
}
|
|
23
|
+
if (ev.errorMessage) m.errors++;
|
|
24
|
+
}
|
|
25
|
+
return models;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function buildModelSwitches(events: any[]): any[] {
|
|
29
|
+
const switches: any[] = [];
|
|
30
|
+
let lastModel: string | null = null;
|
|
31
|
+
let lastProvider: string | null = null;
|
|
32
|
+
let lastTime: number | null = null;
|
|
33
|
+
let completionIndex = 0;
|
|
34
|
+
|
|
35
|
+
for (const ev of events) {
|
|
36
|
+
if (ev.type !== "llm_completion") continue;
|
|
37
|
+
completionIndex++;
|
|
38
|
+
const currentModel = ev.model || "unknown";
|
|
39
|
+
const currentProvider = ev.provider || "unknown";
|
|
40
|
+
const currentTimestamp = ev.timestamp ? new Date(ev.timestamp).getTime() : 0;
|
|
41
|
+
const validTimestamp = Number.isNaN(currentTimestamp) ? 0 : currentTimestamp;
|
|
42
|
+
|
|
43
|
+
if (lastModel && lastModel !== currentModel) {
|
|
44
|
+
const input = Number(ev.usage?.inputTokens || ev.usage?.input || 0);
|
|
45
|
+
const output = Number(ev.usage?.outputTokens || ev.usage?.output || 0);
|
|
46
|
+
const cacheR = Number(ev.usage?.cacheReadTokens || ev.usage?.cacheRead || 0);
|
|
47
|
+
const cacheW = Number(ev.usage?.cacheWriteTokens || ev.usage?.cacheWrite || 0);
|
|
48
|
+
|
|
49
|
+
switches.push({
|
|
50
|
+
index: completionIndex,
|
|
51
|
+
from: lastModel,
|
|
52
|
+
to: currentModel,
|
|
53
|
+
fromProvider: lastProvider || "unknown",
|
|
54
|
+
toProvider: currentProvider,
|
|
55
|
+
providerChanged: (lastProvider || "unknown") !== currentProvider,
|
|
56
|
+
timestamp: validTimestamp,
|
|
57
|
+
timeDelta: lastTime ? validTimestamp - lastTime : 0,
|
|
58
|
+
cost: Number(ev.usage?.cost?.total || 0),
|
|
59
|
+
tokens: Number(ev.usage?.totalTokens || (input + output + cacheR + cacheW))
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
lastModel = currentModel;
|
|
63
|
+
lastProvider = currentProvider;
|
|
64
|
+
lastTime = validTimestamp;
|
|
65
|
+
}
|
|
66
|
+
return switches;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function buildModelSummary(events: any[]): any {
|
|
70
|
+
const completions = events.filter((ev: any) => ev.type === "llm_completion");
|
|
71
|
+
if (!completions.length) return null;
|
|
72
|
+
const uniqueModels = new Set(completions.map((ev: any) => ev.model || "unknown"));
|
|
73
|
+
return {
|
|
74
|
+
firstModel: completions[0]?.model || "unknown",
|
|
75
|
+
finalModel: completions[completions.length - 1]?.model || "unknown",
|
|
76
|
+
switchCount: buildModelSwitches(events).length,
|
|
77
|
+
uniqueModels: uniqueModels.size,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export const COPY_SCRIPT = `(() => {
|
|
2
|
+
document.querySelectorAll('[data-copy-value]').forEach((button) => {
|
|
3
|
+
button.addEventListener('click', async () => {
|
|
4
|
+
const value = button.getAttribute('data-copy-value') || '';
|
|
5
|
+
try {
|
|
6
|
+
if (navigator.clipboard?.writeText) {
|
|
7
|
+
await navigator.clipboard.writeText(value);
|
|
8
|
+
} else {
|
|
9
|
+
const textarea = document.createElement('textarea');
|
|
10
|
+
textarea.value = value;
|
|
11
|
+
textarea.style.position = 'fixed';
|
|
12
|
+
textarea.style.opacity = '0';
|
|
13
|
+
document.body.appendChild(textarea);
|
|
14
|
+
textarea.focus();
|
|
15
|
+
textarea.select();
|
|
16
|
+
document.execCommand('copy');
|
|
17
|
+
textarea.remove();
|
|
18
|
+
}
|
|
19
|
+
const previous = button.innerHTML;
|
|
20
|
+
button.classList.add('copied');
|
|
21
|
+
button.innerHTML = '<svg width="14" height="14" viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"></path></svg>';
|
|
22
|
+
setTimeout(() => {
|
|
23
|
+
button.classList.remove('copied');
|
|
24
|
+
button.innerHTML = previous;
|
|
25
|
+
}, 1400);
|
|
26
|
+
} catch {
|
|
27
|
+
button.textContent = 'ERR';
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
document.querySelectorAll('details[data-lazy-event-body]').forEach((details) => {
|
|
33
|
+
details.addEventListener('toggle', () => {
|
|
34
|
+
if (!details.open) return;
|
|
35
|
+
if (details.querySelector('.event-body')) return;
|
|
36
|
+
const html = decodeURIComponent(details.getAttribute('data-lazy-event-body') || '');
|
|
37
|
+
details.insertAdjacentHTML('beforeend', html);
|
|
38
|
+
}, { once: false });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const topBtn = document.querySelector('.back-to-top');
|
|
42
|
+
if (topBtn) {
|
|
43
|
+
const syncTopButton = () => {
|
|
44
|
+
const scrollable = document.documentElement.scrollHeight > window.innerHeight + 24;
|
|
45
|
+
const show = scrollable && window.scrollY > 200;
|
|
46
|
+
topBtn.classList.toggle('visible', show);
|
|
47
|
+
};
|
|
48
|
+
window.addEventListener('scroll', syncTopButton, { passive: true });
|
|
49
|
+
window.addEventListener('resize', syncTopButton);
|
|
50
|
+
syncTopButton();
|
|
51
|
+
}
|
|
52
|
+
})();`;
|
|
53
|
+
|
|
54
|
+
export const DASHBOARD_SORT_SCRIPT = `(() => {
|
|
55
|
+
const table = document.querySelector('[data-dashboard-table]');
|
|
56
|
+
if (!table) return;
|
|
57
|
+
const tbody = table.querySelector('tbody');
|
|
58
|
+
if (!tbody) return;
|
|
59
|
+
const buttons = Array.from(document.querySelectorAll('[data-sort-key]'));
|
|
60
|
+
let currentKey = 'index';
|
|
61
|
+
let currentDir = 'desc';
|
|
62
|
+
|
|
63
|
+
function icon(dir) {
|
|
64
|
+
return dir === 'asc' ? '↑' : '↓';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function updateState() {
|
|
68
|
+
buttons.forEach(btn => {
|
|
69
|
+
const key = btn.getAttribute('data-sort-key');
|
|
70
|
+
const state = btn.querySelector('.sort-state');
|
|
71
|
+
if (!state) return;
|
|
72
|
+
state.textContent = key === currentKey ? icon(currentDir) : '';
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function compare(a, b, key) {
|
|
77
|
+
if (key === 'index' || key === 'started' || key === 'tokens' || key === 'cost') {
|
|
78
|
+
return Number(a.dataset[key] || 0) - Number(b.dataset[key] || 0);
|
|
79
|
+
}
|
|
80
|
+
return String(a.dataset[key] || '').localeCompare(String(b.dataset[key] || ''));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function sortBy(key) {
|
|
84
|
+
const rows = Array.from(tbody.querySelectorAll('tr'));
|
|
85
|
+
rows.sort((a, b) => {
|
|
86
|
+
const result = compare(a, b, key);
|
|
87
|
+
return currentDir === 'asc' ? result : -result;
|
|
88
|
+
});
|
|
89
|
+
rows.forEach(row => tbody.appendChild(row));
|
|
90
|
+
updateState();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
buttons.forEach(btn => {
|
|
94
|
+
btn.addEventListener('click', () => {
|
|
95
|
+
const key = btn.getAttribute('data-sort-key') || 'index';
|
|
96
|
+
if (currentKey === key) currentDir = currentDir === 'asc' ? 'desc' : 'asc';
|
|
97
|
+
else {
|
|
98
|
+
currentKey = key;
|
|
99
|
+
currentDir = key === 'workflow' ? 'asc' : 'desc';
|
|
100
|
+
}
|
|
101
|
+
sortBy(currentKey);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
sortBy(currentKey);
|
|
106
|
+
})();`;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { escapeHtml } from "../escape.js";
|
|
2
|
+
|
|
3
|
+
const TOOL_USE_ICON = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"></path></svg>`;
|
|
4
|
+
const TOOL_RESULT_ICON = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 10 4 15 9 20"></polyline><path d="M20 4v7a4 4 0 0 1-4 4H4"></path></svg>`;
|
|
5
|
+
|
|
6
|
+
function normalizeContent(value: any): string {
|
|
7
|
+
return typeof value === "string" ? value : JSON.stringify(value, null, 2);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function parseMaybeJson(value: any): any {
|
|
11
|
+
if (typeof value !== "string") return value;
|
|
12
|
+
try { return JSON.parse(value); } catch { return value; }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function renderToolCard(label: string, icon: string, content: any, options: { headerStyle?: string; cardStyle?: string } = {}): string {
|
|
16
|
+
const headerStyle = options.headerStyle ? ` style="${options.headerStyle}"` : "";
|
|
17
|
+
const cardStyle = options.cardStyle ? ` style="${options.cardStyle}"` : "";
|
|
18
|
+
return `<div class="chat-tool-use"${cardStyle}><div class="chat-tool-header"${headerStyle}>${icon} ${escapeHtml(label)}</div><pre class="chat-tool-body">${escapeHtml(normalizeContent(content))}</pre></div>`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function renderToolUseHtml(name: string, input: any): string {
|
|
22
|
+
return renderToolCard(name, TOOL_USE_ICON, parseMaybeJson(input));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function renderToolResultHtml(id: string, content: any, isError = false): string {
|
|
26
|
+
return renderToolCard(`Tool Result: ${id}`, TOOL_RESULT_ICON, content, {
|
|
27
|
+
headerStyle: isError ? "color: var(--err);" : "color: var(--muted);",
|
|
28
|
+
cardStyle: isError ? "border-color: rgba(239,127,127,0.3);" : undefined,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function renderCollapsibleSection(title: string, body: string, open = false): string {
|
|
33
|
+
return `<details class="panel collapsible"${open ? " open" : ""}><summary>${escapeHtml(title)}</summary><div>${body}</div></details>`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function renderKeyValueList(items: Array<[string, unknown]>): string {
|
|
37
|
+
return `<div class="kv-list">${items.map(([k, v]) => `<div class="kv-row"><span class="kv-key">${escapeHtml(k)}</span><strong class="kv-value">${escapeHtml(v == null || v === "" ? "-" : v)}</strong></div>`).join("")}</div>`;
|
|
38
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { escapeHtml } from "../escape.js";
|
|
2
|
+
import { formatDateDay, formatTimeMinutes, formatUsd, parseDate, taskDisplay, workflowClassName, workflowDisplayName } from "../format.js";
|
|
3
|
+
import { wordmarkSvg } from "../shell.js";
|
|
4
|
+
|
|
5
|
+
function renderDateCell(value: string | number): string {
|
|
6
|
+
const date = parseDate(value);
|
|
7
|
+
if (!date) return `<span>${escapeHtml(value)}</span>`;
|
|
8
|
+
return `<div class="date-cell"><strong>${escapeHtml(formatDateDay(value))}</strong><span>${escapeHtml(formatTimeMinutes(value))}</span></div>`;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function renderDashboardBody(sessions: any[], options: any = {}): string {
|
|
12
|
+
const reversed = sessions.slice().reverse();
|
|
13
|
+
const totalCost = sessions.reduce((sum, s) => sum + Number(s.activity?.totals?.totalCostUsd || 0), 0);
|
|
14
|
+
const totalTokens = sessions.reduce((sum, s) => sum + Number(s.activity?.totals?.totalTokens || 0), 0);
|
|
15
|
+
const homeHref = options?.indexHref || "index.html";
|
|
16
|
+
return `<div class="container"><section class="hero"><div class="hero-split"><a class="brand-link" href="${escapeHtml(homeHref)}">${wordmarkSvg()}</a><div class="hero-right"><div class="hero-session"><strong style="color: var(--text); font-weight: 500;">Global Index</strong><span style="color: var(--muted);">Machine-wide session evidence.</span></div><div class="hero-meta"><span class="hero-pill">${sessions.length} sessions</span></div></div></div><div class="metrics"><div class="metric-card"><small>Sessions</small><strong>${sessions.length}</strong></div><div class="metric-card"><small>Total Tokens</small><strong>${totalTokens.toLocaleString()}</strong></div><div class="metric-card"><small>Total Cost</small><strong>${formatUsd(totalCost)}</strong></div></div></section><section class="panel"><h2 class="section-title">Session Reports</h2>${reversed.length ? `<table data-dashboard-table><thead><tr><th class="col-index sortable-head"><button class="sort-btn" data-sort-key="index"><span class="sort-label">#</span><span class="sort-state">↓</span></button></th><th>Session</th><th>Project</th><th class="sortable-head"><button class="sort-btn" data-sort-key="workflow"><span class="sort-label">Workflow</span><span class="sort-state"></span></button></th><th class="sortable-head"><button class="sort-btn" data-sort-key="started"><span class="sort-label">Started</span><span class="sort-state"></span></button></th><th>Task</th><th class="sortable-head num-cell"><button class="sort-btn" data-sort-key="tokens"><span class="sort-label">Tokens</span><span class="sort-state"></span></button></th><th class="sortable-head num-cell"><button class="sort-btn" data-sort-key="cost"><span class="sort-label">Cost</span><span class="sort-state"></span></button></th></tr></thead><tbody>${reversed.map((s, index) => { const link = s.artifacts?.html ? (s.artifacts.html.startsWith(".notrace/") ? s.artifacts.html.substring(9) : s.artifacts.html) : "#"; const workflow = s.task?.workflow || "generic"; const workflowLabel = workflowDisplayName(workflow); const tokens = Number(s.activity?.totals?.totalTokens || 0); const cost = Number(s.activity?.totals?.totalCostUsd || 0); return `<tr data-index="${reversed.length - index}" data-workflow="${escapeHtml(workflowLabel)}" data-started="${parseDate(s.startedAt)?.getTime() || 0}" data-tokens="${tokens}" data-cost="${cost}"><td class="index-cell">${reversed.length - index}</td><td><a class="session-link" href="${escapeHtml(link)}"><strong>${escapeHtml(String(s.sessionId).slice(0, 8))}</strong><span class="session-sub">${escapeHtml(String(s.sessionId))}</span></a></td><td><span class="hero-pill">${escapeHtml(s.repositoryName || "Unknown")}</span></td><td><span class="workflow-pill ${workflowClassName(workflow)}">${escapeHtml(workflowLabel)}</span></td><td>${renderDateCell(s.startedAt)}</td><td>${escapeHtml(taskDisplay(s))}</td><td class="num-cell">${tokens.toLocaleString()}</td><td class="num-cell">${formatUsd(cost)}</td></tr>`; }).join("")}</tbody></table>` : `<div class="empty">No sessions yet. Run Pi with notrace enabled. New reports appear here.</div>`}</section><footer class="footer-note minimal">notrace • raquezha 2026</footer></div>`;
|
|
17
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { escapeHtml } from "../escape.js";
|
|
2
|
+
import { formatTimeSeconds } from "../format.js";
|
|
3
|
+
import { renderMessages, renderUniversalMessageContent } from "./message.js";
|
|
4
|
+
|
|
5
|
+
export function renderJsonBlock(title: string, value: unknown): string {
|
|
6
|
+
return `<section class="block"><h4>${escapeHtml(title)}</h4><pre>${escapeHtml(typeof value === "string" ? value : JSON.stringify(value, null, 2))}</pre></section>`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function eventBadgeClass(ev: any): string {
|
|
10
|
+
if (ev.type === "llm_completion") return "badge badge-llm";
|
|
11
|
+
if (ev.type === "tool_start" || ev.type === "tool_end") return ev.isError ? "badge badge-error" : "badge badge-tool";
|
|
12
|
+
return "badge badge-system";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function eventTitle(ev: any): string {
|
|
16
|
+
return ev.model || ev.toolName || ev.type;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function renderEventBody(ev: any): string {
|
|
20
|
+
const sections: string[] = [];
|
|
21
|
+
if (ev.type === "llm_completion") {
|
|
22
|
+
sections.push(renderMessages(ev.inputPayload?.messages));
|
|
23
|
+
if (ev.stopReason && ev.stopReason !== "stop" && ev.stopReason !== "toolUse") sections.push(renderJsonBlock("Stop Reason", ev.stopReason));
|
|
24
|
+
if (ev.errorMessage) sections.push(renderJsonBlock("Error Message", ev.errorMessage));
|
|
25
|
+
sections.push(`<section class="block"><h4>Output</h4><div class="msg-content">${renderUniversalMessageContent({ content: ev.outputContent })}</div></section>`);
|
|
26
|
+
if (ev.usage) sections.push(renderJsonBlock("Usage", ev.usage));
|
|
27
|
+
} else if (ev.type === "tool_start") {
|
|
28
|
+
sections.push(`<section class="block"><h4>Arguments</h4><div class="msg-content"><div class="chat-tool-use"><div class="chat-tool-header"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"></path></svg> Execution Input</div><pre class="chat-tool-body">${escapeHtml(typeof ev.args === 'string' ? ev.args : JSON.stringify(ev.args, null, 2))}</pre></div></div></section>`);
|
|
29
|
+
} else if (ev.type === "tool_end") {
|
|
30
|
+
sections.push(`<section class="block"><h4>${ev.isError ? "Error Result" : "Result"}</h4><div class="msg-content"><div class="chat-tool-use" style="${ev.isError ? 'border-color: rgba(239,127,127,0.3);' : ''}"><div class="chat-tool-header" style="${ev.isError ? 'color: var(--err);' : 'color: var(--muted);'}"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 10 4 15 9 20"></polyline><path d="M20 4v7a4 4 0 0 1-4 4H4"></path></svg> Execution Output</div><pre class="chat-tool-body">${escapeHtml(typeof ev.result === 'string' ? ev.result : JSON.stringify(ev.result, null, 2))}</pre></div></div></section>`);
|
|
31
|
+
} else {
|
|
32
|
+
sections.push(renderJsonBlock("Event", ev));
|
|
33
|
+
}
|
|
34
|
+
return `<div class="event-body"><div class="stack">${sections.join("")}</div></div>`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function renderEventCard(ev: any): string {
|
|
38
|
+
return `<details class="event" data-lazy-event-body="${escapeHtml(encodeURIComponent(renderEventBody(ev)))}"><summary><div class="event-main"><span class="${eventBadgeClass(ev)}">${escapeHtml(ev.type)}</span><span class="event-title">${escapeHtml(eventTitle(ev))}</span></div><span class="event-time">${escapeHtml(formatTimeSeconds(ev.timestamp))}</span></summary></details>`;
|
|
39
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { escapeHtml } from "../escape.js";
|
|
2
|
+
import { renderToolResultHtml, renderToolUseHtml } from "./card.js";
|
|
3
|
+
|
|
4
|
+
export function renderUniversalMessageContent(m: any): string {
|
|
5
|
+
if (!m) return "";
|
|
6
|
+
let html = "";
|
|
7
|
+
|
|
8
|
+
if (typeof m.content === "string" && m.content.trim()) {
|
|
9
|
+
html += `<div class="chat-text">${escapeHtml(m.content)}</div>`;
|
|
10
|
+
} else if (Array.isArray(m.content)) {
|
|
11
|
+
html += m.content.map((block: any) => {
|
|
12
|
+
if (!block) return "";
|
|
13
|
+
if (block.type === "text") return `<div class="chat-text">${escapeHtml(block.text)}</div>`;
|
|
14
|
+
if (block.type === "tool_use") return renderToolUseHtml(block.name, block.input);
|
|
15
|
+
if (block.type === "tool_result") return renderToolResultHtml(block.tool_use_id || "unknown", block.content, block.is_error === true);
|
|
16
|
+
return `<pre class="chat-tool-body">${escapeHtml(JSON.stringify(block, null, 2))}</pre>`;
|
|
17
|
+
}).join("");
|
|
18
|
+
} else if (m.content && typeof m.content === "object") {
|
|
19
|
+
html += `<pre class="chat-tool-body">${escapeHtml(JSON.stringify(m.content, null, 2))}</pre>`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (Array.isArray(m.tool_calls)) {
|
|
23
|
+
html += m.tool_calls.map((tc: any) => {
|
|
24
|
+
if (tc.type === "function" && tc.function) return renderToolUseHtml(tc.function.name, tc.function.arguments);
|
|
25
|
+
return "";
|
|
26
|
+
}).join("");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (m.function_call) html += renderToolUseHtml(m.function_call.name, m.function_call.arguments);
|
|
30
|
+
|
|
31
|
+
if (m.role === "tool") html = renderToolResultHtml(m.tool_call_id || m.name || "unknown", m.content, m.is_error === true);
|
|
32
|
+
|
|
33
|
+
return html || `<div class="empty">Empty message</div>`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function renderMessages(messages: any[] | undefined): string {
|
|
37
|
+
if (!messages?.length) return "";
|
|
38
|
+
return `<section class="block"><h4>Input Messages</h4>${messages.map(m => `<div class="msg ${escapeHtml(m?.role || "unknown")} "><div class="msg-head"><span class="msg-role">${escapeHtml(m?.role || "unknown")}</span></div><div class="msg-content">${renderUniversalMessageContent(m)}</div></div>`).join("")}</section>`;
|
|
39
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DASHBOARD_SORT_SCRIPT } from "./client.js";
|
|
2
|
+
import { renderDashboardBody } from "./components/dashboard.js";
|
|
3
|
+
import { shell } from "./shell.js";
|
|
4
|
+
|
|
5
|
+
export function generateDashboardHtml(sessions: any[], options: any = {}): string {
|
|
6
|
+
return shell("notrace", renderDashboardBody(sessions, options), DASHBOARD_SORT_SCRIPT);
|
|
7
|
+
}
|