{"version":3,"sources":["../src/proto.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2026 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { Timestamp } from '@bufbuild/protobuf';\nimport { AgentSession as pb } from '@livekit/protocol';\nimport type { ChatItem, MetricsReport } from './llm/chat_context.js';\nimport { isInstructions, renderInstructions } from './llm/chat_context.js';\n\n/**\n * Mapping between the SDK's own types and the agent_session wire format.\n *\n * A leaf: nothing here imports voice or telemetry, so every path that puts a ChatItem\n * on the wire can share one implementation.\n */\n\nconst ROLE_MAP: Record<string, pb.ChatRole> = {\n  developer: pb.ChatRole.DEVELOPER,\n  system: pb.ChatRole.SYSTEM,\n  user: pb.ChatRole.USER,\n  assistant: pb.ChatRole.ASSISTANT,\n};\n\nexport function msToTimestamp(ms: number): Timestamp {\n  return Timestamp.fromDate(new Date(ms));\n}\n\nexport function msToUnixSeconds(ms: number): number {\n  const timestamp = msToTimestamp(ms);\n  return Number(timestamp.seconds) + timestamp.nanos / 1e9;\n}\n\nfunction encodeMetrics(metrics: MetricsReport): pb.MetricsReport {\n  const report = new pb.MetricsReport();\n  // wall-clock metrics are seconds here and Timestamps on the wire\n  if (metrics.startedSpeakingAt !== undefined)\n    report.startedSpeakingAt = msToTimestamp(metrics.startedSpeakingAt * 1000);\n  if (metrics.stoppedSpeakingAt !== undefined)\n    report.stoppedSpeakingAt = msToTimestamp(metrics.stoppedSpeakingAt * 1000);\n  if (metrics.transcriptionDelay !== undefined)\n    report.transcriptionDelay = metrics.transcriptionDelay;\n  if (metrics.endOfTurnDelay !== undefined) report.endOfTurnDelay = metrics.endOfTurnDelay;\n  if (metrics.onUserTurnCompletedDelay !== undefined)\n    report.onUserTurnCompletedDelay = metrics.onUserTurnCompletedDelay;\n  if (metrics.llmNodeTtft !== undefined) report.llmNodeTtft = metrics.llmNodeTtft;\n  if (metrics.ttsNodeTtfb !== undefined) report.ttsNodeTtfb = metrics.ttsNodeTtfb;\n  if (metrics.e2eLatency !== undefined) report.e2eLatency = metrics.e2eLatency;\n  return report;\n}\n\nexport function encodeChatItem(item: ChatItem): pb.ChatContext_ChatItem {\n  switch (item.type) {\n    case 'message': {\n      const content: pb.ChatMessage_ChatContent[] = [];\n      for (const c of item.content) {\n        if (typeof c === 'string') {\n          content.push(new pb.ChatMessage_ChatContent({ payload: { case: 'text', value: c } }));\n        } else if (isInstructions(c)) {\n          content.push(\n            new pb.ChatMessage_ChatContent({ payload: { case: 'text', value: c.value } }),\n          );\n        }\n      }\n\n      const pbMsg = new pb.ChatMessage({\n        id: item.id,\n        role: ROLE_MAP[item.role] ?? pb.ChatRole.ASSISTANT,\n        content,\n        interrupted: item.interrupted,\n        metrics: encodeMetrics(item.metrics),\n        // the proto map is <string, string>; livekit/agents coerces the same way\n        extra: Object.fromEntries(Object.entries(item.extra).map(([k, v]) => [k, String(v)])),\n        createdAt: msToTimestamp(item.createdAt),\n      });\n      if (item.transcriptConfidence !== undefined) {\n        pbMsg.transcriptConfidence = item.transcriptConfidence;\n      }\n      return new pb.ChatContext_ChatItem({ item: { case: 'message', value: pbMsg } });\n    }\n    case 'function_call':\n      return new pb.ChatContext_ChatItem({\n        item: {\n          case: 'functionCall',\n          value: new pb.FunctionCall({\n            id: item.id,\n            callId: item.callId,\n            name: item.name,\n            arguments: item.args,\n            createdAt: msToTimestamp(item.createdAt),\n          }),\n        },\n      });\n    case 'function_call_output':\n      return new pb.ChatContext_ChatItem({\n        item: {\n          case: 'functionCallOutput',\n          value: new pb.FunctionCallOutput({\n            id: item.id,\n            callId: item.callId,\n            name: item.name,\n            output: item.output,\n            isError: item.isError,\n            createdAt: msToTimestamp(item.createdAt),\n          }),\n        },\n      });\n    case 'agent_handoff':\n      return new pb.ChatContext_ChatItem({\n        item: {\n          case: 'agentHandoff',\n          value: new pb.AgentHandoff({\n            id: item.id,\n            oldAgentId: item.oldAgentId,\n            newAgentId: item.newAgentId,\n            createdAt: msToTimestamp(item.createdAt),\n          }),\n        },\n      });\n    case 'agent_config_update':\n      return new pb.ChatContext_ChatItem({\n        item: {\n          case: 'agentConfigUpdate',\n          value: new pb.AgentConfigUpdate({\n            id: item.id,\n            instructions:\n              item.instructions !== undefined ? renderInstructions(item.instructions) : undefined,\n            toolsAdded: item.toolsAdded,\n            toolsRemoved: item.toolsRemoved,\n            createdAt: msToTimestamp(item.createdAt),\n          }),\n        },\n      });\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,sBAA0B;AAC1B,sBAAmC;AAEnC,0BAAmD;AASnD,MAAM,WAAwC;AAAA,EAC5C,WAAW,gBAAAA,aAAG,SAAS;AAAA,EACvB,QAAQ,gBAAAA,aAAG,SAAS;AAAA,EACpB,MAAM,gBAAAA,aAAG,SAAS;AAAA,EAClB,WAAW,gBAAAA,aAAG,SAAS;AACzB;AAEO,SAAS,cAAc,IAAuB;AACnD,SAAO,0BAAU,SAAS,IAAI,KAAK,EAAE,CAAC;AACxC;AAEO,SAAS,gBAAgB,IAAoB;AAClD,QAAM,YAAY,cAAc,EAAE;AAClC,SAAO,OAAO,UAAU,OAAO,IAAI,UAAU,QAAQ;AACvD;AAEA,SAAS,cAAc,SAA0C;AAC/D,QAAM,SAAS,IAAI,gBAAAA,aAAG,cAAc;AAEpC,MAAI,QAAQ,sBAAsB;AAChC,WAAO,oBAAoB,cAAc,QAAQ,oBAAoB,GAAI;AAC3E,MAAI,QAAQ,sBAAsB;AAChC,WAAO,oBAAoB,cAAc,QAAQ,oBAAoB,GAAI;AAC3E,MAAI,QAAQ,uBAAuB;AACjC,WAAO,qBAAqB,QAAQ;AACtC,MAAI,QAAQ,mBAAmB,OAAW,QAAO,iBAAiB,QAAQ;AAC1E,MAAI,QAAQ,6BAA6B;AACvC,WAAO,2BAA2B,QAAQ;AAC5C,MAAI,QAAQ,gBAAgB,OAAW,QAAO,cAAc,QAAQ;AACpE,MAAI,QAAQ,gBAAgB,OAAW,QAAO,cAAc,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