{"version":3,"file":"runtime-error-reporter.mjs","names":[],"sources":["../../../../src/v2/runtime/core/runtime-error-reporter.ts"],"sourcesContent":["import type {\n  CopilotErrorEvent,\n  CopilotErrorHandler,\n} from \"@copilotkit/shared\";\n\nimport type { CopilotRuntimeLike } from \"./runtime\";\n\nexport const runtimeErrorReporterOption = Symbol(\n  \"copilotkit.runtimeErrorReporter\",\n);\n\nexport type RuntimeErrorPhase =\n  | \"common\"\n  | \"sse.factory\"\n  | \"sse.subscription\"\n  | \"intelligence.startup\"\n  | \"intelligence.subscription\";\n\nexport interface RuntimeErrorReportParams {\n  request: Request;\n  error: unknown;\n  operation: string;\n  agentId?: string;\n  threadId?: string;\n  runId?: string;\n  phase?: RuntimeErrorPhase;\n  startTime?: number;\n}\n\nexport interface RuntimeErrorReporter {\n  report(params: RuntimeErrorReportParams): void;\n}\n\nexport interface RuntimeErrorReporterOptions {\n  [runtimeErrorReporterOption]?: RuntimeErrorReporter;\n}\n\nconst SENSITIVE_REQUEST_HEADERS = new Set([\n  \"authorization\",\n  \"proxy-authorization\",\n  \"cookie\",\n  \"set-cookie\",\n  \"x-api-key\",\n  \"api-key\",\n  \"x-copilotcloud-public-api-key\",\n]);\n\nfunction sanitizeRequestHeaders(headers: Headers): Record<string, string> {\n  const sanitized: Record<string, string> = {};\n  headers.forEach((value, key) => {\n    if (!SENSITIVE_REQUEST_HEADERS.has(key.toLowerCase())) {\n      sanitized[key] = value;\n    }\n  });\n  return sanitized;\n}\n\nexport function createRuntimeErrorReporter(\n  handler?: CopilotErrorHandler,\n): RuntimeErrorReporter {\n  return {\n    report({\n      request,\n      error,\n      operation,\n      agentId,\n      threadId,\n      runId,\n      phase,\n      startTime,\n    }) {\n      if (!handler) return;\n\n      try {\n        const event: CopilotErrorEvent = {\n          type: \"error\",\n          timestamp: Date.now(),\n          context: {\n            source: \"runtime\",\n            ...(threadId ? { threadId } : {}),\n            ...(runId ? { runId } : {}),\n            request: {\n              operation,\n              method: request.method,\n              url: request.url,\n              path: new URL(request.url).pathname,\n              headers: sanitizeRequestHeaders(request.headers),\n              startTime: startTime ?? Date.now(),\n            },\n            ...(agentId ? { agent: { name: agentId } } : {}),\n            ...(phase ? { metadata: { phase } } : {}),\n          },\n          error,\n        };\n\n        const result = handler(event);\n        if (result && typeof (result as Promise<void>).catch === \"function\") {\n          void (result as Promise<void>).catch((handlerError) => {\n            console.error(\n              \"CopilotRuntime onError reporting failed:\",\n              handlerError,\n            );\n          });\n        }\n      } catch (handlerError) {\n        console.error(\"CopilotRuntime onError reporting failed:\", handlerError);\n      }\n    },\n  };\n}\n\nexport function getRuntimeErrorReporterFromOptions(\n  options: object,\n): RuntimeErrorReporter | undefined {\n  return (options as RuntimeErrorReporterOptions)[runtimeErrorReporterOption];\n}\n\nexport function attachRuntimeErrorReporter(\n  runtime: object,\n  reporter: RuntimeErrorReporter,\n): void {\n  Object.defineProperty(runtime, runtimeErrorReporterOption, {\n    configurable: true,\n    value: reporter,\n  });\n}\n\nexport function getRuntimeErrorReporter(\n  runtime: CopilotRuntimeLike,\n): RuntimeErrorReporter | undefined {\n  return (runtime as CopilotRuntimeLike & RuntimeErrorReporterOptions)[\n    runtimeErrorReporterOption\n  ];\n}\n"],"mappings":";;AAOA,MAAa,6BAA6B,OACxC,kCACD;AA4BD,MAAM,4BAA4B,IAAI,IAAI;CACxC;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;AAEF,SAAS,uBAAuB,SAA0C;CACxE,MAAM,YAAoC,EAAE;AAC5C,SAAQ,SAAS,OAAO,QAAQ;AAC9B,MAAI,CAAC,0BAA0B,IAAI,IAAI,aAAa,CAAC,CACnD,WAAU,OAAO;GAEnB;AACF,QAAO;;AAGT,SAAgB,2BACd,SACsB;AACtB,QAAO,EACL,OAAO,EACL,SACA,OACA,WACA,SACA,UACA,OACA,OACA,aACC;AACD,MAAI,CAAC,QAAS;AAEd,MAAI;GAsBF,MAAM,SAAS,QArBkB;IAC/B,MAAM;IACN,WAAW,KAAK,KA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