Add five new tRPC query procedures powering the Radar page's per-agent behavioral metrics (questions asked, subagent spawns, compaction events, inter-agent messages) plus the batch repository methods they require. Repository changes: - LogChunkRepository: add findByAgentIds() for batch fetching without N+1 - ConversationRepository: add countByFromAgentIds() and findByFromAgentId() - Drizzle adapters: implement all three new methods using inArray() - InMemoryConversationRepository (integration test): implement new methods tRPC procedures added: - agent.listForRadar: filtered agent list with per-agent metrics computed from log chunks (questionsCount, subagentsCount, compactionsCount) and conversation counts (messagesCount); supports timeRange/status/mode/initiative filters - agent.getCompactionEvents: compact system init chunks for one agent (cap 200) - agent.getSubagentSpawns: Agent tool_use entries with prompt preview (cap 200) - agent.getQuestionsAsked: AskUserQuestion tool calls with questions array (cap 200) - conversation.getByFromAgent: conversations by fromAgentId with toAgentName resolved All 13 new unit tests pass; existing test suite unaffected. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
/**
|
|
* Drizzle Log Chunk Repository Adapter
|
|
*
|
|
* Implements LogChunkRepository interface using Drizzle ORM.
|
|
*/
|
|
|
|
import { eq, asc, max, inArray } from 'drizzle-orm';
|
|
import { nanoid } from 'nanoid';
|
|
import type { DrizzleDatabase } from '../../index.js';
|
|
import { agentLogChunks } from '../../schema.js';
|
|
import type { LogChunkRepository } from '../log-chunk-repository.js';
|
|
|
|
export class DrizzleLogChunkRepository implements LogChunkRepository {
|
|
constructor(private db: DrizzleDatabase) {}
|
|
|
|
async insertChunk(data: {
|
|
agentId: string;
|
|
agentName: string;
|
|
sessionNumber: number;
|
|
content: string;
|
|
}): Promise<void> {
|
|
await this.db.insert(agentLogChunks).values({
|
|
id: nanoid(),
|
|
agentId: data.agentId,
|
|
agentName: data.agentName,
|
|
sessionNumber: data.sessionNumber,
|
|
content: data.content,
|
|
createdAt: new Date(),
|
|
});
|
|
}
|
|
|
|
async findByAgentId(agentId: string): Promise<{ content: string; sessionNumber: number; createdAt: Date }[]> {
|
|
return this.db
|
|
.select({
|
|
content: agentLogChunks.content,
|
|
sessionNumber: agentLogChunks.sessionNumber,
|
|
createdAt: agentLogChunks.createdAt,
|
|
})
|
|
.from(agentLogChunks)
|
|
.where(eq(agentLogChunks.agentId, agentId))
|
|
.orderBy(asc(agentLogChunks.createdAt));
|
|
}
|
|
|
|
async findByAgentIds(agentIds: string[]): Promise<{ agentId: string; content: string; sessionNumber: number; createdAt: Date }[]> {
|
|
if (agentIds.length === 0) return [];
|
|
return this.db
|
|
.select({
|
|
agentId: agentLogChunks.agentId,
|
|
content: agentLogChunks.content,
|
|
sessionNumber: agentLogChunks.sessionNumber,
|
|
createdAt: agentLogChunks.createdAt,
|
|
})
|
|
.from(agentLogChunks)
|
|
.where(inArray(agentLogChunks.agentId, agentIds))
|
|
.orderBy(asc(agentLogChunks.createdAt));
|
|
}
|
|
|
|
async deleteByAgentId(agentId: string): Promise<void> {
|
|
await this.db
|
|
.delete(agentLogChunks)
|
|
.where(eq(agentLogChunks.agentId, agentId));
|
|
}
|
|
|
|
async getSessionCount(agentId: string): Promise<number> {
|
|
const result = await this.db
|
|
.select({ maxSession: max(agentLogChunks.sessionNumber) })
|
|
.from(agentLogChunks)
|
|
.where(eq(agentLogChunks.agentId, agentId));
|
|
|
|
return result[0]?.maxSession ?? 0;
|
|
}
|
|
}
|