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: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ backend/dist/
frontend/node_modules/
backend/node_modules/
claude-plugins-official/
.claude/settings.local.json
27 changes: 27 additions & 0 deletions backend/src/application/co-usage/get-session-timeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { IEventRepository } from "@/domain/ports/event-repository";

export interface SessionTimelineEventDto {
skillName: string;
pluginName: string | null;
timestamp: string;
}

export interface SessionTimelineResponse {
sessionId: string;
events: SessionTimelineEventDto[];
}

export async function getSessionTimeline(
deps: { events: IEventRepository },
input: { sessionId: string },
): Promise<SessionTimelineResponse> {
const rows = await deps.events.listSessionTimeline(input.sessionId);
return {
sessionId: input.sessionId,
events: rows.map((r) => ({
skillName: r.skillName,
pluginName: r.pluginName,
timestamp: r.timestamp.toISOString(),
})),
};
}
7 changes: 7 additions & 0 deletions backend/src/domain/ports/event-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface SessionSkillActivation {
lastActivatedAt: Date;
}

export interface SessionTimelineEvent {
skillName: string;
pluginName: string | null;
timestamp: Date;
}

export type CohortsWindow = "all" | number;

export interface DirectEventStats {
Expand All @@ -41,5 +47,6 @@ export interface IEventRepository {
listRecentSkillActivations(limit: number): Promise<RecentSkillActivatedEvent[]>;
listUserSkillActivations(window: CohortsWindow): Promise<UserSkillActivation[]>;
listSessionSkillActivations(window: CohortsWindow): Promise<SessionSkillActivation[]>;
listSessionTimeline(sessionId: string): Promise<SessionTimelineEvent[]>;
getDirectStats(): Promise<DirectEventStats>;
}
7 changes: 7 additions & 0 deletions backend/src/http/co-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AppVariables } from "@/types";
import type { AppDeps } from "@/bootstrap/compose";
import { sessionAuth } from "@/middleware/session-auth";
import { getCoUsage } from "@/application/co-usage/get-co-usage";
import { getSessionTimeline } from "@/application/co-usage/get-session-timeline";
import type { CohortsWindow } from "@/domain/ports/event-repository";

function parseWindow(raw: string | undefined): CohortsWindow | { error: string } {
Expand All @@ -22,5 +23,11 @@ export function createCoUsageRoute(deps: Pick<AppDeps, "events">) {
return c.json(await getCoUsage(deps, { window }));
});

route.get("/sessions/:sessionId/timeline", async (c) => {
const sessionId = c.req.param("sessionId");
if (!sessionId) return c.json({ error: "sessionId is required" }, 400);
return c.json(await getSessionTimeline(deps, { sessionId }));
});

return route;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
IEventRepository,
RecentSkillActivatedEvent,
SessionSkillActivation,
SessionTimelineEvent,
UserSkillActivation,
} from "@/domain/ports/event-repository";
import type { NewEvent } from "@/domain/event";
Expand Down Expand Up @@ -193,4 +194,31 @@ export class DrizzleEventRepository implements IEventRepository {
: new Date(r.lastActivatedAt as unknown as string),
}));
}

async listSessionTimeline(sessionId: string): Promise<SessionTimelineEvent[]> {
const skillExpr = sql<string>`(${events.attributes}->>'skill.name')`;
const pluginExpr = sql<string | null>`(${events.attributes}->>'plugin.name')`;
const rows = await this.db
.select({
timestamp: events.timestamp,
skillName: skillExpr,
pluginName: pluginExpr,
})
.from(events)
.where(
and(
eq(events.eventName, EVENT_NAMES.SKILL_ACTIVATED),
eq(events.sessionId, sessionId),
sql`${skillExpr} IS NOT NULL`,
),
)
.orderBy(events.timestamp);

return rows.map((r) => ({
skillName: r.skillName,
pluginName: typeof r.pluginName === "string" ? r.pluginName : null,
timestamp:
r.timestamp instanceof Date ? r.timestamp : new Date(r.timestamp as unknown as string),
}));
}
}
Loading
Loading