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>
90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
/**
|
|
* Drizzle Conversation Repository Adapter
|
|
*
|
|
* Implements ConversationRepository interface using Drizzle ORM.
|
|
*/
|
|
|
|
import { eq, and, asc, count, inArray } from 'drizzle-orm';
|
|
import { nanoid } from 'nanoid';
|
|
import type { DrizzleDatabase } from '../../index.js';
|
|
import { conversations, type Conversation } from '../../schema.js';
|
|
import type { ConversationRepository, CreateConversationData } from '../conversation-repository.js';
|
|
|
|
export class DrizzleConversationRepository implements ConversationRepository {
|
|
constructor(private db: DrizzleDatabase) {}
|
|
|
|
async create(data: CreateConversationData): Promise<Conversation> {
|
|
const now = new Date();
|
|
const id = nanoid();
|
|
await this.db.insert(conversations).values({
|
|
id,
|
|
fromAgentId: data.fromAgentId,
|
|
toAgentId: data.toAgentId,
|
|
initiativeId: data.initiativeId ?? null,
|
|
phaseId: data.phaseId ?? null,
|
|
taskId: data.taskId ?? null,
|
|
question: data.question,
|
|
status: 'pending',
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
return this.findById(id) as Promise<Conversation>;
|
|
}
|
|
|
|
async findById(id: string): Promise<Conversation | null> {
|
|
const rows = await this.db
|
|
.select()
|
|
.from(conversations)
|
|
.where(eq(conversations.id, id))
|
|
.limit(1);
|
|
return rows[0] ?? null;
|
|
}
|
|
|
|
async findPendingForAgent(toAgentId: string): Promise<Conversation[]> {
|
|
return this.db
|
|
.select()
|
|
.from(conversations)
|
|
.where(
|
|
and(
|
|
eq(conversations.toAgentId, toAgentId),
|
|
eq(conversations.status, 'pending' as 'pending' | 'answered'),
|
|
),
|
|
)
|
|
.orderBy(asc(conversations.createdAt));
|
|
}
|
|
|
|
async answer(id: string, answer: string): Promise<Conversation | null> {
|
|
await this.db
|
|
.update(conversations)
|
|
.set({
|
|
answer,
|
|
status: 'answered' as 'pending' | 'answered',
|
|
updatedAt: new Date(),
|
|
})
|
|
.where(eq(conversations.id, id));
|
|
return this.findById(id);
|
|
}
|
|
|
|
async countByFromAgentIds(agentIds: string[]): Promise<{ agentId: string; count: number }[]> {
|
|
if (agentIds.length === 0) return [];
|
|
const rows = await this.db
|
|
.select({
|
|
agentId: conversations.fromAgentId,
|
|
count: count(),
|
|
})
|
|
.from(conversations)
|
|
.where(inArray(conversations.fromAgentId, agentIds))
|
|
.groupBy(conversations.fromAgentId);
|
|
return rows.map(r => ({ agentId: r.agentId, count: Number(r.count) }));
|
|
}
|
|
|
|
async findByFromAgentId(agentId: string): Promise<Conversation[]> {
|
|
return this.db
|
|
.select()
|
|
.from(conversations)
|
|
.where(eq(conversations.fromAgentId, agentId))
|
|
.orderBy(asc(conversations.createdAt))
|
|
.limit(200);
|
|
}
|
|
}
|