|
| 1 | +/** |
| 2 | + * Agent observability operation schemas |
| 3 | + * |
| 4 | + * Schemas for AI agent event collection and session management |
| 5 | + */ |
| 6 | + |
| 7 | +import { z } from 'zod'; |
| 8 | + |
| 9 | +// === BASE SCHEMAS === |
| 10 | + |
| 11 | +export const ObservabilityAgentTypeSchema = z.enum([ |
| 12 | + 'github-copilot', |
| 13 | + 'claude-code', |
| 14 | + 'cursor', |
| 15 | + 'gemini-cli', |
| 16 | + 'cline', |
| 17 | + 'aider', |
| 18 | + 'mcp-generic', |
| 19 | +]).describe('Type of AI coding agent'); |
| 20 | + |
| 21 | +export const AgentEventTypeSchema = z.enum([ |
| 22 | + 'session_start', |
| 23 | + 'session_end', |
| 24 | + 'file_read', |
| 25 | + 'file_write', |
| 26 | + 'file_create', |
| 27 | + 'file_delete', |
| 28 | + 'command_execute', |
| 29 | + 'test_run', |
| 30 | + 'build_trigger', |
| 31 | + 'search_performed', |
| 32 | + 'llm_request', |
| 33 | + 'llm_response', |
| 34 | + 'error_encountered', |
| 35 | + 'rollback_performed', |
| 36 | + 'commit_created', |
| 37 | + 'tool_invocation', |
| 38 | + 'user_interaction', |
| 39 | + 'context_switch', |
| 40 | +]).describe('Type of agent event'); |
| 41 | + |
| 42 | +export const EventSeveritySchema = z.enum([ |
| 43 | + 'debug', |
| 44 | + 'info', |
| 45 | + 'warning', |
| 46 | + 'error', |
| 47 | + 'critical', |
| 48 | +]).describe('Severity level of the event'); |
| 49 | + |
| 50 | +export const SessionOutcomeSchema = z.enum([ |
| 51 | + 'success', |
| 52 | + 'partial', |
| 53 | + 'failure', |
| 54 | + 'abandoned', |
| 55 | +]).describe('Outcome of the agent session'); |
| 56 | + |
| 57 | +export const SessionIdSchema = z.string().uuid().describe('Session identifier (UUID)'); |
| 58 | + |
| 59 | +export const AgentVersionSchema = z.string().describe('Version of the agent'); |
| 60 | + |
| 61 | +// Use numeric project ID for agent observability (database integer) |
| 62 | +export const AgentProjectIdSchema = z.number().int().positive().describe('Project identifier'); |
| 63 | + |
| 64 | +// === START SESSION === |
| 65 | + |
| 66 | +export const StartAgentSessionSchema = z.object({ |
| 67 | + agentId: ObservabilityAgentTypeSchema, |
| 68 | + agentVersion: AgentVersionSchema, |
| 69 | + projectId: AgentProjectIdSchema, |
| 70 | + objective: z.string().optional().describe('What the agent is trying to achieve'), |
| 71 | + devlogId: z.number().int().positive().optional().describe('Associated devlog entry ID'), |
| 72 | + branch: z.string().describe('Git branch name'), |
| 73 | + initialCommit: z.string().describe('Git commit SHA at session start'), |
| 74 | + triggeredBy: z.enum(['user', 'automation', 'schedule']).default('user').describe('How the session was triggered'), |
| 75 | +}); |
| 76 | + |
| 77 | +// === END SESSION === |
| 78 | + |
| 79 | +export const EndAgentSessionSchema = z.object({ |
| 80 | + sessionId: SessionIdSchema, |
| 81 | + outcome: SessionOutcomeSchema, |
| 82 | + qualityScore: z.number().min(0).max(100).optional().describe('Quality score (0-100)'), |
| 83 | + finalCommit: z.string().optional().describe('Git commit SHA at session end'), |
| 84 | +}); |
| 85 | + |
| 86 | +// === LOG EVENT === |
| 87 | + |
| 88 | +export const LogAgentEventSchema = z.object({ |
| 89 | + sessionId: SessionIdSchema, |
| 90 | + type: AgentEventTypeSchema, |
| 91 | + agentId: ObservabilityAgentTypeSchema, |
| 92 | + agentVersion: AgentVersionSchema, |
| 93 | + projectId: AgentProjectIdSchema, |
| 94 | + filePath: z.string().optional().describe('File path if relevant to the event'), |
| 95 | + workingDirectory: z.string().describe('Current working directory'), |
| 96 | + branch: z.string().optional().describe('Git branch'), |
| 97 | + commit: z.string().optional().describe('Git commit SHA'), |
| 98 | + devlogId: z.number().int().positive().optional().describe('Associated devlog entry ID'), |
| 99 | + data: z.record(z.any()).default({}).describe('Event-specific data (flexible JSON)'), |
| 100 | + metrics: z.object({ |
| 101 | + duration: z.number().optional().describe('Event duration in milliseconds'), |
| 102 | + tokenCount: z.number().optional().describe('LLM tokens used'), |
| 103 | + fileSize: z.number().optional().describe('File size in bytes'), |
| 104 | + linesChanged: z.number().optional().describe('Lines added or removed'), |
| 105 | + }).optional().describe('Event metrics'), |
| 106 | + parentEventId: z.string().uuid().optional().describe('Parent event ID for causality'), |
| 107 | + relatedEventIds: z.array(z.string().uuid()).optional().describe('Related event IDs'), |
| 108 | + tags: z.array(z.string()).optional().describe('Searchable tags'), |
| 109 | + severity: EventSeveritySchema.optional(), |
| 110 | +}); |
| 111 | + |
| 112 | +// === QUERY EVENTS === |
| 113 | + |
| 114 | +export const QueryAgentEventsSchema = z.object({ |
| 115 | + sessionId: SessionIdSchema.optional(), |
| 116 | + projectId: AgentProjectIdSchema.optional(), |
| 117 | + agentId: ObservabilityAgentTypeSchema.optional(), |
| 118 | + eventType: AgentEventTypeSchema.optional(), |
| 119 | + severity: EventSeveritySchema.optional(), |
| 120 | + startTime: z.string().datetime().optional().describe('Filter events after this time (ISO 8601)'), |
| 121 | + endTime: z.string().datetime().optional().describe('Filter events before this time (ISO 8601)'), |
| 122 | + tags: z.array(z.string()).optional().describe('Filter by tags'), |
| 123 | + limit: z.number().int().positive().max(1000).default(100).describe('Maximum number of events to return'), |
| 124 | + offset: z.number().int().nonnegative().default(0).describe('Number of events to skip'), |
| 125 | +}); |
| 126 | + |
| 127 | +// === QUERY SESSIONS === |
| 128 | + |
| 129 | +export const QueryAgentSessionsSchema = z.object({ |
| 130 | + projectId: AgentProjectIdSchema.optional(), |
| 131 | + agentId: ObservabilityAgentTypeSchema.optional(), |
| 132 | + outcome: SessionOutcomeSchema.optional(), |
| 133 | + startTimeFrom: z.string().datetime().optional().describe('Filter sessions starting after this time (ISO 8601)'), |
| 134 | + startTimeTo: z.string().datetime().optional().describe('Filter sessions starting before this time (ISO 8601)'), |
| 135 | + minQualityScore: z.number().min(0).max(100).optional().describe('Minimum quality score'), |
| 136 | + maxQualityScore: z.number().min(0).max(100).optional().describe('Maximum quality score'), |
| 137 | + limit: z.number().int().positive().max(1000).default(100).describe('Maximum number of sessions to return'), |
| 138 | + offset: z.number().int().nonnegative().default(0).describe('Number of sessions to skip'), |
| 139 | +}); |
| 140 | + |
| 141 | +// === GET SESSION === |
| 142 | + |
| 143 | +export const GetAgentSessionSchema = z.object({ |
| 144 | + sessionId: SessionIdSchema, |
| 145 | +}); |
| 146 | + |
| 147 | +// === GET EVENT STATS === |
| 148 | + |
| 149 | +export const GetEventStatsSchema = z.object({ |
| 150 | + sessionId: SessionIdSchema.optional(), |
| 151 | + projectId: AgentProjectIdSchema.optional(), |
| 152 | + startTime: z.string().datetime().optional().describe('Start of time range (ISO 8601)'), |
| 153 | + endTime: z.string().datetime().optional().describe('End of time range (ISO 8601)'), |
| 154 | +}); |
| 155 | + |
| 156 | +// === GET SESSION STATS === |
| 157 | + |
| 158 | +export const GetSessionStatsSchema = z.object({ |
| 159 | + projectId: AgentProjectIdSchema.optional(), |
| 160 | + startTimeFrom: z.string().datetime().optional().describe('Start of time range (ISO 8601)'), |
| 161 | + startTimeTo: z.string().datetime().optional().describe('End of time range (ISO 8601)'), |
| 162 | +}); |
| 163 | + |
| 164 | +// === GET ACTIVE SESSIONS === |
| 165 | + |
| 166 | +export const GetActiveSessionsSchema = z.object({ |
| 167 | + projectId: AgentProjectIdSchema.optional(), |
| 168 | +}); |
0 commit comments