{"version":3,"file":"handle-run.cjs","names":["cloneAgentForRequest","parseRunRequest","isIntelligenceRuntime","handleIntelligenceRun","handleSseRun"],"sources":["../../../../src/v2/runtime/handlers/handle-run.ts"],"sourcesContent":["import { isIntelligenceRuntime } from \"../core/runtime\";\nimport { telemetry } from \"../telemetry\";\nimport {\n  cloneAgentForRequest,\n  configureAgentForRequest,\n  parseRunRequest,\n  RunAgentParameters,\n} from \"./shared/agent-utils\";\nimport { handleIntelligenceRun } from \"./intelligence/run\";\nimport { handleSseRun } from \"./sse/run\";\n\nexport async function handleRunAgent({\n  runtime,\n  request,\n  agentId,\n}: RunAgentParameters) {\n  telemetry.capture(\"oss.runtime.copilot_request_created\", {\n    \"cloud.guardrails.enabled\": false,\n    requestType: \"run\",\n    \"cloud.api_key_provided\": !!request.headers.get(\n      \"x-copilotcloud-public-api-key\",\n    ),\n    ...(request.headers.get(\"x-copilotcloud-public-api-key\")\n      ? {\n          \"cloud.public_api_key\": request.headers.get(\n            \"x-copilotcloud-public-api-key\",\n          )!,\n        }\n      : {}),\n  });\n\n  try {\n    const agent = await cloneAgentForRequest(runtime, agentId);\n    if (agent instanceof Response) {\n      return agent;\n    }\n\n    configureAgentForRequest({ runtime, request, agentId, agent });\n\n    if (\n      runtime.licenseChecker &&\n      !runtime.licenseChecker.checkFeature(\"agents\")\n    ) {\n      console.warn(\n        '[CopilotKit Runtime] Warning: \"agents\" feature is not licensed. Visit copilotkit.ai/pricing',\n      );\n    }\n\n    const input = await parseRunRequest(request);\n    if (input instanceof Response) {\n      return input;\n    }\n\n    agent.setMessages(input.messages);\n    agent.setState(input.state);\n    agent.threadId = input.threadId;\n\n    if (isIntelligenceRuntime(runtime)) {\n      return handleIntelligenceRun({\n        runtime,\n        request,\n        agentId,\n        agent,\n        input,\n      });\n    }\n\n    return handleSseRun({ runtime, request, agent, input });\n  } catch (error) {\n    console.error(\"Error running agent:\", error);\n    console.error(\n      \"Error stack:\",\n      error instanceof Error ? error.stack : \"No stack trace\",\n    );\n    console.error(\"Error details:\", {\n      name: error instanceof Error ? error.name : \"Unknown\",\n      message: error instanceof Error ? error.message : String(error),\n      cause: error instanceof Error ? error.cause : undefined,\n    });\n\n    return new Response(\n      JSON.stringify({\n        error: \"Failed to run agent\",\n        message: error instanceof Error ? error.message : \"Unknown error\",\n      }),\n      {\n        status: 500,\n        headers: { \"Content-Type\": \"application/json\" },\n      },\n    );\n  }\n}\n"],"mappings":";;;;;;;;AAWA,eAAsB,eAAe,EACnC,SACA,SACA,WACqB;AACrB,kCAAU,QAAQ,uCAAuC;EACvD,4BAA4B;EAC5B,aAAa;EACb,0BAA0B,CAAC,CAAC,QAAQ,QAAQ,IAC1C,gCACD;EACD,GAAI,QAAQ,QAAQ,IAAI,gCAAgC,GACpD,EACE,wBAAwB,QAAQ,QAAQ,IACtC,gCACD,EACF,GACD,EAAE;EACP,CAAC;AAEF,KAAI;EACF,MAAM,QAAQ,MAAMA,yCAAqB,SAAS,QAAQ;AAC1D,MAAI,iBAAiB,SACnB,QAAO;AAGT,+CAAyB;GAAE;GAAS;GAAS;GAAS;GAAO,CAAC;AAE9D,MACE,QAAQ,kBACR,CAAC,QAAQ,eAAe,aAAa,SAAS,CAE9C,SAAQ,KACN,gGACD;EAGH,MAAM,QAAQ,MAAMC,oCAAgB,QAAQ;AAC5C,MAAI,iBAAiB,SACnB,QAAO;AAGT,QAAM,YAAY,MAAM,SAAS;AACjC,QAAM,SAAS,MAAM,MAAM;AAC3B,QAAM,WAAW,MAAM;AAEvB,MAAIC,sCAAsB,QAAQ,CAChC,QAAOC,kCAAsB;GAC3B;GACA;GACA;GACA;GACA;GACD,CAAC;AAGJ,SAAOC,2BAAa;GAAE;GAAS;GAAS;GAAO;GAAO,CAAC;UAChD,OAAO;AACd,UAAQ,MAAM,wBAAwB,MAAM;AAC5C,UAAQ,MACN,gBACA,iBAAiB,QAAQ,MAAM,QAAQ,iBACxC;AACD,UAAQ,MAAM,kBAAkB;GAC9B,MAAM,iBAAiB,QAAQ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