UNPKG

@posthog/agent

Version:

TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog

197 lines (194 loc) 6.51 kB
import { z } from 'zod'; // Base event schema with timestamp const BaseEventSchema = z.object({ ts: z.number(), }); // Streaming content events const TokenEventSchema = BaseEventSchema.extend({ type: z.literal("token"), content: z.string(), contentType: z.enum(["text", "thinking", "tool_input"]).optional(), }); const ContentBlockStartEventSchema = BaseEventSchema.extend({ type: z.literal("content_block_start"), index: z.number(), contentType: z.enum(["text", "tool_use", "thinking"]), toolName: z.string().optional(), toolId: z.string().optional(), }); const ContentBlockStopEventSchema = BaseEventSchema.extend({ type: z.literal("content_block_stop"), index: z.number(), }); // Tool events const ToolCallEventSchema = BaseEventSchema.extend({ type: z.literal("tool_call"), toolName: z.string(), callId: z.string(), args: z.record(z.string(), z.unknown()), parentToolUseId: z.string().nullable().optional(), tool: z.unknown().optional(), category: z.unknown().optional(), }); const ToolResultEventSchema = BaseEventSchema.extend({ type: z.literal("tool_result"), toolName: z.string(), callId: z.string(), result: z.unknown(), isError: z.boolean().optional(), parentToolUseId: z.string().nullable().optional(), tool: z.unknown().optional(), category: z.unknown().optional(), }); // Message lifecycle events const MessageStartEventSchema = BaseEventSchema.extend({ type: z.literal("message_start"), messageId: z.string().optional(), model: z.string().optional(), }); const MessageDeltaEventSchema = BaseEventSchema.extend({ type: z.literal("message_delta"), stopReason: z.string().optional(), stopSequence: z.string().optional(), usage: z .object({ outputTokens: z.number(), }) .optional(), }); const MessageStopEventSchema = BaseEventSchema.extend({ type: z.literal("message_stop"), }); // User message events const UserMessageEventSchema = BaseEventSchema.extend({ type: z.literal("user_message"), content: z.string(), isSynthetic: z.boolean().optional(), }); // System events const StatusEventSchema = BaseEventSchema.extend({ type: z.literal("status"), phase: z.string(), kind: z.string().optional(), branch: z.string().optional(), prUrl: z.string().optional(), taskId: z.string().optional(), messageId: z.string().optional(), model: z.string().optional(), }).passthrough(); // Allow additional fields const InitEventSchema = BaseEventSchema.extend({ type: z.literal("init"), model: z.string(), tools: z.array(z.string()), permissionMode: z.string(), cwd: z.string(), apiKeySource: z.string(), agents: z.array(z.string()).optional(), slashCommands: z.array(z.string()).optional(), outputStyle: z.string().optional(), mcpServers: z .array(z.object({ name: z.string(), status: z.string() })) .optional(), }); // Console event for log-style output const ConsoleEventSchema = BaseEventSchema.extend({ type: z.literal("console"), level: z.enum(["debug", "info", "warn", "error"]), message: z.string(), }); const CompactBoundaryEventSchema = BaseEventSchema.extend({ type: z.literal("compact_boundary"), trigger: z.enum(["manual", "auto"]), preTokens: z.number(), }); // Result events const DoneEventSchema = BaseEventSchema.extend({ type: z.literal("done"), result: z.string().optional(), durationMs: z.number().optional(), durationApiMs: z.number().optional(), numTurns: z.number().optional(), totalCostUsd: z.number().optional(), usage: z.unknown().optional(), modelUsage: z .record(z.string(), z.object({ inputTokens: z.number(), outputTokens: z.number(), cacheReadInputTokens: z.number(), cacheCreationInputTokens: z.number(), webSearchRequests: z.number(), costUSD: z.number(), contextWindow: z.number(), })) .optional(), permissionDenials: z .array(z.object({ tool_name: z.string(), tool_use_id: z.string(), tool_input: z.record(z.string(), z.unknown()), })) .optional(), }); const ErrorEventSchema = BaseEventSchema.extend({ type: z.literal("error"), message: z.string(), error: z.unknown().optional(), errorType: z.string().optional(), context: z.record(z.string(), z.unknown()).optional(), sdkError: z.unknown().optional(), }); // Metric and artifact events const MetricEventSchema = BaseEventSchema.extend({ type: z.literal("metric"), key: z.string(), value: z.number(), unit: z.string().optional(), }); const ArtifactEventSchema = BaseEventSchema.extend({ type: z.literal("artifact"), kind: z.string(), content: z.unknown(), }); const RawSDKEventSchema = BaseEventSchema.extend({ type: z.literal("raw_sdk_event"), sdkMessage: z.unknown(), }); const AgentEventSchema = z.discriminatedUnion("type", [ TokenEventSchema, ContentBlockStartEventSchema, ContentBlockStopEventSchema, ToolCallEventSchema, ToolResultEventSchema, MessageStartEventSchema, MessageDeltaEventSchema, MessageStopEventSchema, UserMessageEventSchema, StatusEventSchema, InitEventSchema, ConsoleEventSchema, CompactBoundaryEventSchema, DoneEventSchema, ErrorEventSchema, MetricEventSchema, ArtifactEventSchema, RawSDKEventSchema, ]); /** * Parse and validate an AgentEvent from unknown input. * Returns the parsed event if valid, or null if invalid. */ function parseAgentEvent(input) { const result = AgentEventSchema.safeParse(input); return result.success ? result.data : null; } /** * Parse and validate multiple AgentEvents from an array of unknown inputs. * Invalid entries are discarded. */ function parseAgentEvents(inputs) { return inputs .map((input) => parseAgentEvent(input)) .filter((event) => event !== null); } export { AgentEventSchema, ArtifactEventSchema, CompactBoundaryEventSchema, ConsoleEventSchema, ContentBlockStartEventSchema, ContentBlockStopEventSchema, DoneEventSchema, ErrorEventSchema, InitEventSchema, MessageDeltaEventSchema, MessageStartEventSchema, MessageStopEventSchema, MetricEventSchema, RawSDKEventSchema, StatusEventSchema, TokenEventSchema, ToolCallEventSchema, ToolResultEventSchema, UserMessageEventSchema, parseAgentEvent, parseAgentEvents }; //# sourceMappingURL=schemas.js.map