Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion frontend/src/lib/api/generated/models/DbInsight.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/lib/api/types/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface GenerateInsightRequest {
date_from: string;
date_to: string;
project?: string;
session_id?: string;
prompt?: string;
agent?: AgentName;
// IANA timezone the date range is expressed in, so the server's activity
Expand Down
36 changes: 36 additions & 0 deletions frontend/src/lib/components/layout/SessionBreadcrumb.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
EllipsisVerticalIcon,
FileTextIcon,
FolderIcon,
LightbulbIcon,
LinkIcon,
SearchIcon,
SquareTerminalIcon,
Expand All @@ -34,6 +35,7 @@
import SignalPanel from "../content/SignalPanel.svelte";
import { sessions } from "../../stores/sessions.svelte.js";
import { router } from "../../stores/router.svelte.js";
import { insights } from "../../stores/insights.svelte.js";
import {
supportsResume,
buildResumeCommand,
Expand Down Expand Up @@ -243,6 +245,12 @@
}, 1500);
}

function handleAgentAnalysis() {
if (!session) return;
insights.generateForSession(session);
router.navigate("insights");
}

function toggleMenu() {
menuOpen = !menuOpen;
}
Expand Down Expand Up @@ -760,6 +768,14 @@
>
<ChartColumnIcon size="13" strokeWidth="2" aria-hidden="true" />
</button>
<button
class="insight-btn"
title={m.insights_page_agent_analysis()}
aria-label={m.insights_page_agent_analysis()}
onclick={handleAgentAnalysis}
>
<LightbulbIcon size="13" strokeWidth="2" aria-hidden="true" />
</button>
<button
class="find-btn"
class:find-btn--active={inSessionSearch.isOpen}
Expand Down Expand Up @@ -1181,6 +1197,26 @@
color: var(--accent-blue);
}

.insight-btn {
display: flex;
align-items: center;
justify-content: center;
width: 22px;
height: 22px;
border: none;
border-radius: var(--radius-sm, 4px);
background: transparent;
color: var(--text-muted);
cursor: pointer;
transition: background 0.15s, color 0.15s;
flex-shrink: 0;
}

.insight-btn:hover {
background: var(--bg-surface-hover);
color: var(--accent-blue);
}

.find-btn--active {
color: var(--accent-blue);
background: color-mix(in srgb, var(--accent-blue) 12%, transparent);
Expand Down
36 changes: 36 additions & 0 deletions frontend/src/lib/components/layout/SessionBreadcrumb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ import {
} from "../../api/generated/index";
import { messages } from "../../stores/messages.svelte.js";
import { setLocale } from "../../i18n/index.js";
import { router } from "../../stores/router.svelte.js";

const { generateForSession } = vi.hoisted(() => ({
generateForSession: vi.fn(),
}));

vi.mock("../../stores/insights.svelte.js", () => ({
insights: {
generateForSession,
},
}));

vi.mock("../../api/client.js", () => ({
listOpeners: vi.fn().mockResolvedValue({ openers: [] }),
Expand Down Expand Up @@ -155,6 +166,7 @@ async function flushPromises() {
}

beforeEach(() => {
generateForSession.mockReset();
openersService.getApiV1Openers
.mockReset()
.mockResolvedValue({ openers: [] });
Expand Down Expand Up @@ -418,6 +430,30 @@ describe("SessionBreadcrumb", () => {
unmount(component);
});

it("starts single-session agent analysis from the top bar", async () => {
const navigateSpy = vi.spyOn(router, "navigate");
const session = makeSession("claude");
const component = mount(SessionBreadcrumb, {
target: document.body,
props: {
session,
onBack: () => {},
},
});

await tick();
const button = document.querySelector<HTMLButtonElement>(".insight-btn");
expect(button).toBeTruthy();

button!.click();

expect(generateForSession).toHaveBeenCalledWith(session);
expect(navigateSpy).toHaveBeenCalledWith("insights");

navigateSpy.mockRestore();
unmount(component);
});

it("renders an explicit missing token placeholder when context tokens are absent", async () => {
const component = mount(SessionBreadcrumb, {
target: document.body,
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/lib/stores/insights.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
CannedInsightKind,
AutomatedScope,
InsightGenerationFilters,
Session,
} from "../api/types.js";
import {
ApiError as GeneratedApiError,
Expand All @@ -29,6 +30,7 @@ export interface InsightTask {
kind?: CannedInsightKind;
promptText: string;
automatedScope: AutomatedScope;
sessionId?: string;
sessionFilters?: InsightGenerationFilters;
status: "generating" | "done" | "error";
phase: string;
Expand All @@ -48,6 +50,7 @@ interface GenerationSnapshot {
kind?: CannedInsightKind;
promptText: string;
automatedScope: AutomatedScope;
sessionId?: string;
sessionFilters?: InsightGenerationFilters;
}

Expand Down Expand Up @@ -174,12 +177,36 @@ class InsightsStore {
: undefined,
promptText: this.promptText,
automatedScope: this.automatedScope,
sessionId: undefined,
sessionFilters: this.sessionFilters
? { ...this.sessionFilters }
: undefined,
});
}

generateForSession(session: Session) {
const date = sessionInsightDate(session);
this.type = "agent_analysis";
this.dateFrom = date;
this.dateTo = date;
this.project = session.project || "";
this.automatedScope = "human";
this.#startGeneration(
{
type: "agent_analysis",
dateFrom: date,
dateTo: date,
project: session.project || "",
agent: this.agent,
promptText: this.promptText,
automatedScope: "human",
sessionId: session.id,
},
undefined,
true,
);
}

retryTask(clientId: string) {
const task = this.tasks.find((t) => t.clientId === clientId);
if (!task || task.status === "generating") return;
Expand All @@ -193,6 +220,7 @@ class InsightsStore {
kind: task.kind,
promptText: task.promptText,
automatedScope: task.automatedScope,
sessionId: task.sessionId,
sessionFilters: task.sessionFilters
? { ...task.sessionFilters }
: undefined,
Expand All @@ -217,6 +245,7 @@ class InsightsStore {
kind: snap.kind,
promptText: snap.promptText,
automatedScope: snap.automatedScope,
sessionId: snap.sessionId,
sessionFilters: snap.sessionFilters
? { ...snap.sessionFilters }
: undefined,
Expand Down Expand Up @@ -245,6 +274,7 @@ class InsightsStore {
date_to: snap.dateTo,
project: snap.project || undefined,
prompt: snap.promptText || undefined,
session_id: snap.sessionId,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
agent: snap.agent,
kind: snap.kind,
Expand Down Expand Up @@ -359,3 +389,11 @@ class InsightsStore {
}

export const insights = new InsightsStore();

function sessionInsightDate(session: Session): string {
const ts =
session.started_at ||
session.ended_at ||
session.created_at;
return ts.slice(0, 10);
}
43 changes: 42 additions & 1 deletion frontend/src/lib/stores/insights.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
beforeEach,
} from "vite-plus/test";
import { insights } from "./insights.svelte.js";
import type { Insight } from "../api/types.js";
import type { Insight, Session } from "../api/types.js";

const api = vi.hoisted(() => {
class MockApiError extends Error {
Expand Down Expand Up @@ -63,6 +63,25 @@ function makeInsight(
};
}

function makeSession(overrides: Partial<Session> = {}): Session {
return {
id: "run:session-1",
project: "proj-a",
machine: "local",
agent: "claude",
first_message: "hello",
started_at: "2026-07-05T14:30:00Z",
ended_at: "2026-07-05T14:45:00Z",
message_count: 2,
user_message_count: 1,
total_output_tokens: 0,
peak_context_tokens: 0,
is_automated: false,
created_at: "2026-07-05T14:30:00Z",
...overrides,
};
}

beforeEach(() => {
vi.clearAllMocks();
insights.items = [];
Expand Down Expand Up @@ -340,6 +359,28 @@ describe("generate (multi-task)", () => {
);
});

it("generates agent analysis for a single session", () => {
vi.mocked(api.generateInsight).mockReturnValueOnce({
abort: vi.fn(),
done: Promise.resolve(makeInsight({ id: 32 })),
});

insights.generateForSession(makeSession());

expect(api.generateInsight).toHaveBeenCalledWith(
expect.objectContaining({
type: "agent_analysis",
date_from: "2026-07-05",
date_to: "2026-07-05",
project: "proj-a",
session_id: "run:session-1",
}),
expect.any(Function),
expect.any(Function),
);
expect(insights.selectedTaskId).toBe(insights.tasks[0]?.clientId);
});

it("sends dashboard session filters for canned recommendations", async () => {
insights.setType("llm_canned");
insights.setCannedKind("prompt_maturity_review");
Expand Down
Loading