|
| 1 | +/** |
| 2 | + * Telemetry ORPC schemas |
| 3 | + * |
| 4 | + * Defines input/output schemas for backend telemetry endpoints. |
| 5 | + */ |
| 6 | + |
| 7 | +import { z } from "zod"; |
| 8 | + |
| 9 | +// Error context enum (matches payload.ts) |
| 10 | +const ErrorContextSchema = z.enum([ |
| 11 | + "workspace-creation", |
| 12 | + "workspace-deletion", |
| 13 | + "workspace-switch", |
| 14 | + "message-send", |
| 15 | + "message-stream", |
| 16 | + "project-add", |
| 17 | + "project-remove", |
| 18 | + "git-operation", |
| 19 | +]); |
| 20 | + |
| 21 | +// Individual event payload schemas |
| 22 | +const AppStartedPropertiesSchema = z.object({ |
| 23 | + isFirstLaunch: z.boolean(), |
| 24 | +}); |
| 25 | + |
| 26 | +const WorkspaceCreatedPropertiesSchema = z.object({ |
| 27 | + workspaceId: z.string(), |
| 28 | +}); |
| 29 | + |
| 30 | +const WorkspaceSwitchedPropertiesSchema = z.object({ |
| 31 | + fromWorkspaceId: z.string(), |
| 32 | + toWorkspaceId: z.string(), |
| 33 | +}); |
| 34 | + |
| 35 | +const MessageSentPropertiesSchema = z.object({ |
| 36 | + model: z.string(), |
| 37 | + mode: z.string(), |
| 38 | + message_length_b2: z.number(), |
| 39 | +}); |
| 40 | + |
| 41 | +const ErrorOccurredPropertiesSchema = z.object({ |
| 42 | + errorType: z.string(), |
| 43 | + context: ErrorContextSchema, |
| 44 | +}); |
| 45 | + |
| 46 | +// Union of all telemetry events |
| 47 | +export const TelemetryEventSchema = z.discriminatedUnion("event", [ |
| 48 | + z.object({ |
| 49 | + event: z.literal("app_started"), |
| 50 | + properties: AppStartedPropertiesSchema, |
| 51 | + }), |
| 52 | + z.object({ |
| 53 | + event: z.literal("workspace_created"), |
| 54 | + properties: WorkspaceCreatedPropertiesSchema, |
| 55 | + }), |
| 56 | + z.object({ |
| 57 | + event: z.literal("workspace_switched"), |
| 58 | + properties: WorkspaceSwitchedPropertiesSchema, |
| 59 | + }), |
| 60 | + z.object({ |
| 61 | + event: z.literal("message_sent"), |
| 62 | + properties: MessageSentPropertiesSchema, |
| 63 | + }), |
| 64 | + z.object({ |
| 65 | + event: z.literal("error_occurred"), |
| 66 | + properties: ErrorOccurredPropertiesSchema, |
| 67 | + }), |
| 68 | +]); |
| 69 | + |
| 70 | +// API schemas |
| 71 | +export const telemetry = { |
| 72 | + track: { |
| 73 | + input: TelemetryEventSchema, |
| 74 | + output: z.void(), |
| 75 | + }, |
| 76 | + setEnabled: { |
| 77 | + input: z.object({ enabled: z.boolean() }), |
| 78 | + output: z.void(), |
| 79 | + }, |
| 80 | + isEnabled: { |
| 81 | + input: z.void(), |
| 82 | + output: z.boolean(), |
| 83 | + }, |
| 84 | +}; |
0 commit comments