{"version":3,"file":"pluginSlotProps.cjs","names":["hostSlotFills","slotFills","splitHostId","defaultHostId","webPluginRegistryForHost","workspaceSlotFills","webPluginRegistryRevision","createElement","ToolRendererBoundary","fileTabForPath","pluginContextActions","sendFrame","pluginMcpAppRenderer","renderThread","renderSessionActivity","renderSessionTranscript"],"sources":["../../../src/web/lib/pluginSlotProps.ts"],"sourcesContent":["import type {\n  ComposerCapture,\n  SlotDataFill,\n  SlotDeclaration,\n  TransientTab,\n  WebPluginContextItem,\n  WebPluginSlotProps,\n  WebPluginContextInventoryItem,\n  WebPluginSessionContext,\n} from '@agimon-ai/po-core/web';\nimport { createElement, type ReactNode } from 'react';\n\nimport { fileTabForPath } from './composition';\nimport { defaultHostId, splitHostId } from './hostIdentity';\nimport {\n  hostSlotFills,\n  pluginContextActions,\n  pluginMcpAppRenderer,\n  recordWebPluginRenderFailure,\n  slotFills,\n  webPluginRegistryForHost,\n  webPluginRegistryRevision,\n  workspaceSlotFills,\n} from './pluginRegistry';\nimport { renderSessionActivity } from './sessionActivityRenderer';\nimport { renderSessionTranscript } from './sessionTranscriptRenderer';\nimport { renderThread } from './threadRenderer';\nimport { ToolRendererBoundary } from './ToolRendererBoundary';\nimport { sendFrame } from './transport';\n/** The id-only session context a plugin sees, from the hub's summary of the target session. */\nexport function pluginSessionContext(\n  sessionId: string | null,\n  summary: { workspaceId?: string; parentSessionId?: string; sessionProvenance?: string } | undefined,\n): WebPluginSessionContext | undefined {\n  if (sessionId === null || summary === undefined) return undefined;\n  return {\n    sessionId,\n    ...(summary.workspaceId === undefined ? {} : { workspaceId: summary.workspaceId }),\n    ...(summary.parentSessionId === undefined ? {} : { parentSessionId: summary.parentSessionId }),\n    ...(summary.sessionProvenance === undefined ? {} : { provenance: summary.sessionProvenance }),\n  };\n}\n\n/** Explicit scopes never inherit the unrelated focused session or workspace's fills. */\nexport function pluginSlotFills(slot: string, sessionId: string | null, workspaceId?: string) {\n  if (workspaceId === undefined) return sessionId === null ? hostSlotFills(slot) : slotFills(slot, sessionId);\n  const host = splitHostId(workspaceId);\n  const hostFills =\n    host === undefined || host.hostId === defaultHostId()\n      ? hostSlotFills(slot)\n      : webPluginRegistryForHost(host.hostId).hostSlotFills(slot);\n  const own = workspaceSlotFills(workspaceId, slot);\n  const fills = new Map([...hostFills, ...own].map((fill) => [JSON.stringify([fill.pluginId, fill.id]), fill]));\n  return [...fills.values()].sort(\n    (left, right) =>\n      left.order - right.order || left.pluginId.localeCompare(right.pluginId) || left.id.localeCompare(right.id),\n  );\n}\n\n/**\n * A wrap region: its fills fold around the host's own part, the first fill outermost. Each fill\n * receives the part as it renders without that fill in `children`, so it may decorate or replace\n * it. A fill that throws renders those children instead: a broken customisation drops out and the\n * region stays. Shared by the cockpit and the phone, which reuse these slot props.\n */\nexport function renderRegion(\n  slot: string,\n  inner: ReactNode,\n  props: WebPluginSlotProps,\n  sessionId: string | null,\n  workspaceId?: string,\n): ReactNode {\n  const revision = webPluginRegistryRevision();\n  return pluginSlotFills(slot, sessionId, workspaceId).reduceRight<ReactNode>((children, fill) => {\n    const component = fill.component;\n    if (component === undefined) return children;\n    const toolName = `${fill.pluginId}:${fill.id}`;\n    return createElement(ToolRendererBoundary, {\n      key: `${toolName}:${revision}`,\n      toolName,\n      children: (failed: boolean) => {\n        if (failed) {\n          recordWebPluginRenderFailure(fill.pluginId, fill.id, slot);\n          return children;\n        }\n        return createElement(component, { ...props, children });\n      },\n    });\n  }, inner);\n}\n\n/** The host's hold on the focused session's runtime tabs, bound in by the caller that owns the store. */\nexport interface TransientTabActions {\n  open: (tab: TransientTab) => void;\n  close: (tabId: string) => void;\n}\n\n/**\n * The one place the props every plugin component receives are built. The\n * same object is handed to every fill a slot renders, so a fill that is\n * itself a slot owner can render its own slots with it.\n */\nexport function pluginSlotProps(\n  sessionId: string | null,\n  openTab: (tabId: string | null) => void,\n  statuses: Readonly<Record<string, string>>,\n  tabs: TransientTabActions,\n  appendComposerDraft: (text: string) => void,\n  attachComposerContext: (item: WebPluginContextItem) => void,\n  attachComposerCapture: (capture: ComposerCapture) => void = () => undefined,\n  contextInventory: readonly WebPluginContextInventoryItem[] = [],\n  submitCapture?: (capture: ComposerCapture) => Promise<void>,\n  sessionReservationId?: string,\n  workspaceId?: string,\n): WebPluginSlotProps {\n  const props: WebPluginSlotProps = {\n    sessionId,\n    ...(workspaceId === undefined ? {} : { workspaceId }),\n    ...(sessionReservationId === undefined ? {} : { sessionReservationId }),\n    openTab,\n    openTransientTab: tabs.open,\n    closeTransientTab: tabs.close,\n    // Asked per render rather than bound to a snapshot: which plugin claims a\n    // path is a property of what is installed, and the answer is cheap.\n    fileTabFor: (path, targetSessionId) => fileTabForPath(targetSessionId ?? sessionId, path),\n    appendComposerDraft,\n    attachComposerContext,\n    attachComposerCapture,\n    ...(submitCapture === undefined ? {} : { submitCapture }),\n    contextActionsFor(item) {\n      return pluginContextActions(sessionId).flatMap((action) =>\n        action.kinds.includes(item.kind)\n          ? [\n              {\n                id: `${action.pluginId}.${action.id}`,\n                label: action.label,\n                ...(action.detail === undefined ? {} : { detail: action.detail }),\n                run: () =>\n                  action.run({\n                    item,\n                    sessionId,\n                    openTab,\n                    openTransientTab: tabs.open,\n                    sendSessionFrame: sendFrame,\n                  }),\n              },\n            ]\n          : [],\n      );\n    },\n    sendSessionFrame: sendFrame,\n    statuses,\n    contextInventory,\n    renderMcpApp: (request) => pluginMcpAppRenderer(sessionId)?.(request) ?? null,\n    renderThread(threadId, options): ReactNode {\n      return sessionId === null ? null : renderThread(sessionId, threadId, options);\n    },\n    renderSessionActivity(): ReactNode {\n      return sessionId === null ? null : renderSessionActivity(sessionId, () => openTab(null));\n    },\n    renderSessionTranscript(targetSessionId, options): ReactNode {\n      return renderSessionTranscript(targetSessionId, options);\n    },\n    // Each fill renders behind its own boundary, so one plugin that throws leaves its siblings and the host standing.\n    renderSlot(slot): ReactNode {\n      const revision = webPluginRegistryRevision();\n      return pluginSlotFills(slot, sessionId, workspaceId).flatMap((fill) => {\n        const component = fill.component;\n        if (component === undefined) return [];\n        const toolName = `${fill.pluginId}:${fill.id}`;\n        return [\n          createElement(ToolRendererBoundary, {\n            key: `${toolName}:${revision}`,\n            toolName,\n            children: (failed: boolean) => {\n              if (failed) {\n                recordWebPluginRenderFailure(fill.pluginId, fill.id, slot);\n                return null;\n              }\n              return createElement(component, props);\n            },\n          }),\n        ];\n      });\n    },\n    slotData<Data>(declaration: SlotDeclaration<Data>): readonly SlotDataFill<Data>[] {\n      return pluginSlotFills(declaration.slot, sessionId, workspaceId).flatMap((fill) =>\n        fill.data === undefined\n          ? []\n          : [{ pluginId: fill.pluginId, id: fill.id, order: fill.order, data: fill.data as Data }],\n      );\n    },\n  };\n  return props;\n}\n"],"mappings":";;;;;;;;;;;AA8BA,SAAgB,qBACd,WACA,SACqC;CACrC,IAAI,cAAc,QAAQ,YAAY,KAAA,GAAW,OAAO,KAAA;CACxD,OAAO;EACL;EACA,GAAI,QAAQ,gBAAgB,KAAA,IAAY,CAAC,IAAI,EAAE,aAAa,QAAQ,YAAY;EAChF,GAAI,QAAQ,oBAAoB,KAAA,IAAY,CAAC,IAAI,EAAE,iBAAiB,QAAQ,gBAAgB;EAC5F,GAAI,QAAQ,sBAAsB,KAAA,IAAY,CAAC,IAAI,EAAE,YAAY,QAAQ,kBAAkB;CAC7F;AACF;;AAGA,SAAgB,gBAAgB,MAAc,WAA0B,aAAsB;CAC5F,IAAI,gBAAgB,KAAA,GAAW,OAAO,cAAc,OAAOA,uBAAAA,cAAc,IAAI,IAAIC,uBAAAA,UAAU,MAAM,SAAS;CAC1G,MAAM,OAAOC,qBAAAA,YAAY,WAAW;CACpC,MAAM,YACJ,SAAS,KAAA,KAAa,KAAK,WAAWC,qBAAAA,cAAc,IAChDH,uBAAAA,cAAc,IAAI,IAClBI,uBAAAA,yBAAyB,KAAK,MAAM,CAAC,CAAC,cAAc,IAAI;CAC9D,MAAM,MAAMC,uBAAAA,mBAAmB,aAAa,IAAI;CAEhD,OAAO,CAAC,GAAG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