{"version":3,"file":"agent-log-event.mjs","names":[],"sources":["../../../../../../../ai/src/agent/agent-log-event.ts"],"sourcesContent":["import type { Logger } from \"@warlock.js/logger\";\nimport type { AgentEventMap } from \"../contracts/events/event-map.type\";\nimport type { LLMTrip } from \"../contracts/result/llm-trip.type\";\nimport type { ToolCall } from \"../contracts/result/tool-call.type\";\nimport type { Usage } from \"../contracts/result/usage.type\";\n\n/**\n * Aggregate snapshot the agent-level log entries need at emission time\n * (agent completion + model name + running totals). Passed instead of\n * storing a back-reference to the Execution class — keeps this helper\n * a pure function.\n */\nexport type AgentLogContext = {\n  /** Dotted logger module, e.g. `\"ai.agent.my-writer\"`. */\n  module: string;\n  /** Maximum trips configured for this run — logged once on `agent.starting`. */\n  maxTrips: number;\n  /** Model identifier — logged once on `agent.starting`. */\n  modelName: string;\n  /** Final running usage totals at the moment `agent.completed` fires. */\n  totalUsage: Usage;\n  /** Monotonic duration (ms) since start at the moment `agent.completed` fires. */\n  totalDurationMs: number;\n  /** All trips recorded so far — logged count on `agent.completed`. */\n  trips: LLMTrip[];\n  /** All tool calls recorded so far — logged count on `agent.completed`. */\n  toolCalls: ToolCall[];\n};\n\n/**\n * Structured logging for agent lifecycle events. Mirrors\n * `Execution.emit`'s call sites without touching class state — the\n * caller passes the ambient snapshot in `ctx`, we route each event to\n * the right logger level and enrich with the per-event forensic\n * detail.\n *\n * Log-level convention:\n * - `info` — lifecycle boundaries (agent starting / completed) users\n *   want to see at default verbosity\n * - `debug` — per-trip + per-tool progress (hot-path, opt-in)\n * - `success` — trip completion + tool success (terminal per-step state)\n * - `warn` — tool failures (recoverable, agent loop continues)\n * - `error` — agent-level terminal errors\n *\n * Streaming deltas (`agent.trip.streaming`) are intentionally skipped\n * to avoid token-granularity log spam.\n */\nexport function logAgentEvent<K extends keyof AgentEventMap>(\n  logger: Logger,\n  ctx: AgentLogContext,\n  event: K,\n  payload: AgentEventMap[K],\n): void {\n  const action = event.replace(/^agent\\./, \"\");\n\n  switch (event) {\n    case \"agent.starting\": {\n      const { input } = payload as AgentEventMap[\"agent.starting\"];\n      logger.info(ctx.module, action, \"agent starting\", {\n        maxTrips: ctx.maxTrips,\n        model: ctx.modelName,\n        inputLength: input.length,\n      });\n      return;\n    }\n\n    case \"agent.trip.started\": {\n      const { tripIndex } = payload as AgentEventMap[\"agent.trip.started\"];\n      logger.debug(ctx.module, action, \"trip started\", { tripIndex });\n      return;\n    }\n\n    case \"agent.trip.streaming\": {\n      // Deltas are too high-volume to log at token granularity.\n      // Skipped on purpose; debug level still fires on trip boundaries.\n      return;\n    }\n\n    case \"agent.trip.completed\": {\n      const { trip } = payload as AgentEventMap[\"agent.trip.completed\"];\n      logger.success(ctx.module, action, \"trip completed\", {\n        tripIndex: trip.index,\n        duration: trip.duration,\n        usage: trip.usage,\n        finishReason: trip.finishReason,\n      });\n      return;\n    }\n\n    case \"agent.tool.calling\": {\n      const { tool, tripIndex } =\n        payload as AgentEventMap[\"agent.tool.calling\"];\n      logger.debug(ctx.module, action, `calling tool \"${tool.name}\"`, {\n        tool: tool.name,\n        action: tool.action,\n        tripIndex,\n      });\n      return;\n    }\n\n    case \"agent.tool.called\": {\n      const toolCall = payload as AgentEventMap[\"agent.tool.called\"];\n      logger.success(ctx.module, action, `tool \"${toolCall.name}\" finished`, {\n        tool: toolCall.name,\n        duration: toolCall.duration,\n        tripIndex: toolCall.tripIndex,\n      });\n      return;\n    }\n\n    case \"agent.tool.failed\": {\n      const { tool, error, tripIndex } =\n        payload as AgentEventMap[\"agent.tool.failed\"];\n\n      logger.warn(ctx.module, action, `tool \"${tool.name}\" failed`, {\n        tool: tool.name,\n        tripIndex,\n        error: {\n          code: error.code,\n          message: error.message,\n          name: error.name,\n          stack: error.stack,\n        },\n      });\n      return;\n    }\n\n    case \"agent.completed\": {\n      logger.info(ctx.module, action, \"agent completed\", {\n        duration: ctx.totalDurationMs,\n        usage: ctx.totalUsage,\n        trips: ctx.trips.length,\n        tools: ctx.toolCalls.length,\n      });\n      return;\n    }\n\n    case \"agent.error\": {\n      const { error } = payload as AgentEventMap[\"agent.error\"];\n      logger.error(ctx.module, action, error.message, {\n        code: error.code,\n        context: error.context,\n      });\n      return;\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA+CA,SAAgB,cACd,QACA,KACA,OACA,SACM;CACN,MAAM,SAAS,MAAM,QAAQ,YAAY,EAAE;CAE3C,QAAQ,OAAR;EACE,KAAK,kBAAkB;GACrB,MAAM,EAAE,UAAU;GAClB,OAAO,KAAK,IAAI,QAAQ,QAAQ,kBAAkB;IAChD,UAAU,IAAI;IACd,OAAO,IAAI;IACX,aAAa,MAAM;GACrB,CAAC;GACD;EACF;EAEA,KAAK,sBAAsB;GACzB,MAAM,EAAE,cAAc;GACtB,OAAO,MAAM,IAAI,QAAQ,QAAQ,gBAAgB,EAAE,UAAU,CAAC;GAC9D;EACF;EAEA,KAAK,wBAGH;EAGF,KAAK,wBAAwB;GAC3B,MAAM,EAAE,SAAS;GACjB,OAAO,QAAQ,IAAI,QAAQ,QAAQ,kBAAkB;IACnD,WAAW,KAAK;IAChB,UAAU,KAAK;IACf,OAAO,KAAK;IACZ,cAAc,KAAK;GACrB,CAAC;GACD;EACF;EAEA,KAAK,sBAAsB;GACzB,MAAM,EAAE,MAAM,cACZ;GACF,OAAO,MAAM,IAAI,QAAQ,QAAQ,iBAAiB,KAAK,KAAK,IAAI;IAC9D,MAAM,KAAK;IACX,QAAQ,KAAK;IACb;GACF,CAAC;GACD;EACF;EAEA,KAAK,qBAAqB;GACxB,MAAM,WAAW;GACjB,OAAO,QAAQ,IAAI,QAAQ,QAAQ,SAAS,SAAS,KAAK,aAAa;IACrE,MAAM,SAAS;IACf,UAAU,SAAS;IACnB,WAAW,SAAS;GACtB,CAAC;GACD;EACF;EAEA,KAAK,qBAAqB;GACxB,MAAM,EAAE,MAAM,OAAO,cACnB;GAEF,OAAO,KAAK,IAAI,QAAQ,QAAQ,SAAS,KAAK,KAAK,WAAW;IAC5D,MA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