{"version":3,"file":"ContextItemDialog.mjs","names":[],"sources":["../../../../src/web/features/activity/ContextItemDialog.tsx"],"sourcesContent":["import type { ContextItemDetail, ContextItemKind, ContextToolWarning } from '@agimon-ai/po-core/contextApi';\nimport { useT } from '@agimon-ai/po-core/web';\nimport {\n  Dialog,\n  DialogBody,\n  DialogContent,\n  DialogHeader,\n  DialogTitle,\n  Panel,\n  Spinner,\n} from '@agimon-ai/po-web-components';\nimport { useEffect, useState } from 'react';\n\nimport { fetchContextItemDetail } from '../../lib/contextDetailApi';\n\n/**\n * What one row of the composition actually is.\n *\n * The panel answers \"what does this cost\"; a reader who clicks a row is asking\n * the follow-up, \"and what am I paying for\". That is the description the model\n * reads, the schema it is handed, and for a tool the split between the two,\n * because the two are cut differently: prose comes out with the package, a\n * schema only with the tool.\n *\n * Fetched on open rather than carried by the projection. The full text of every\n * tool in a composition is an order of magnitude larger than the figures, and\n * is wanted one row at a time.\n */\n\nfunction tokens(value: number): string {\n  return `~${value.toLocaleString()}`;\n}\n\ninterface Target {\n  itemKind: ContextItemKind;\n  name: string;\n  owner: string;\n}\n\nexport interface ContextItemDialogProps {\n  sessionId: string | null;\n  target: Target | null;\n  warnings?: readonly ContextToolWarning[];\n  onClose: () => void;\n}\n\nexport function ContextItemDialog({ sessionId, target, warnings, onClose }: ContextItemDialogProps) {\n  const t = useT();\n  const [detail, setDetail] = useState<ContextItemDetail | null>(null);\n  const [error, setError] = useState<string | null>(null);\n  const itemKind = target?.itemKind ?? null;\n  const name = target?.name ?? null;\n\n  // A second click must not show the first row's answer for a frame, so the\n  // reset happens during render rather than in the effect that refetches.\n  const request =\n    sessionId === null || itemKind === null || name === null ? null : `${sessionId}\\u0000${itemKind}\\u0000${name}`;\n  const [shown, setShown] = useState<string | null>(null);\n  if (shown !== request) {\n    setShown(request);\n    setDetail(null);\n    setError(null);\n  }\n\n  useEffect(() => {\n    if (sessionId === null || itemKind === null || name === null) return;\n    // A reply that lands after the reader moved on belongs to a row that is no\n    // longer open, so it is dropped rather than painted.\n    let live = true;\n    void fetchContextItemDetail(sessionId, itemKind, name).then((result) => {\n      if (!live) return;\n      if (result.ok) setDetail(result.detail);\n      else setError(result.error);\n    });\n    return () => {\n      live = false;\n    };\n  }, [sessionId, itemKind, name]);\n\n  if (target === null) return null;\n\n  return (\n    <Dialog\n      open\n      onOpenChange={(next) => {\n        if (!next) onClose();\n      }}\n    >\n      <DialogContent width=\"lg\" data-testid=\"context-item-dialog\" aria-describedby={undefined}>\n        <DialogHeader>\n          <DialogTitle data-testid=\"context-item-title\">\n            {target.itemKind === 'prompt' ? t('webDashboard.activity.systemPromptLower', 'system prompt') : target.name}\n          </DialogTitle>\n          <span className=\"text-sm text-po-dim\">\n            {target.itemKind} · {target.owner}\n          </span>\n        </DialogHeader>\n        <DialogBody>\n          {target.itemKind === 'tool' && warnings && warnings.length > 0 ? (\n            <Section title={t('webDashboard.activity.sectionWarnings', 'warnings')}>\n              <ul data-testid=\"context-item-warnings\" aria-live=\"polite\" className=\"flex flex-col gap-3\">\n                {warnings.map((warning, index) => (\n                  <li key={index} className=\"flex flex-col gap-1 text-sm break-words\">\n                    <span className=\"text-po-yellow\">{warning.source}</span>\n                    <span className=\"text-sm text-po-dim\">{warning.path}</span>\n                    <p className=\"text-po-text\">{warning.message}</p>\n                  </li>\n                ))}\n              </ul>\n            </Section>\n          ) : null}\n          {error !== null ? (\n            <p data-testid=\"context-item-error\" className=\"text-sm text-po-dim\">\n              {error}\n            </p>\n          ) : detail === null ? (\n            <p data-testid=\"context-item-loading\" className=\"flex items-center gap-2 text-sm text-po-dim\">\n              <Spinner />\n              {t('webDashboard.activity.readingInventory', \"reading the session's inventory\")}\n            </p>\n          ) : (\n            <ContextItemBody detail={detail} />\n          )}\n        </DialogBody>\n      </DialogContent>\n    </Dialog>\n  );\n}\n\nfunction ContextItemBody({ detail }: { detail: ContextItemDetail }) {\n  const t = useT();\n  if (detail.itemKind === 'prompt') return <PromptBody detail={detail} />;\n  return (\n    <div className=\"flex flex-col gap-3\">\n      {detail.itemKind === 'tool' ? <ToolCost detail={detail} /> : <SkillFacts detail={detail} />}\n\n      {detail.itemKind === 'skill' || detail.description !== undefined ? (\n        <Section title={t('webDashboard.activity.sectionDescription', 'description')}>\n          <p\n            data-testid=\"context-item-description\"\n            className=\"text-base leading-relaxed whitespace-pre-wrap text-po-text\"\n          >\n            {detail.description}\n          </p>\n        </Section>\n      ) : null}\n\n      {detail.itemKind === 'skill' && detail.body !== undefined ? (\n        <Section title={t('webDashboard.activity.sectionBody', 'body')}>\n          <Pre testId=\"context-item-body\">{detail.body}</Pre>\n        </Section>\n      ) : null}\n\n      {detail.itemKind === 'tool' && detail.promptSnippet !== undefined ? (\n        <Section title={t('webDashboard.activity.sectionPromptSnippet', 'prompt snippet')}>\n          <Pre testId=\"context-item-snippet\">{detail.promptSnippet}</Pre>\n        </Section>\n      ) : null}\n\n      {detail.itemKind === 'tool' && detail.promptGuidelines !== undefined ? (\n        <Section title={t('webDashboard.activity.sectionPromptGuidelines', 'prompt guidelines')}>\n          <ul data-testid=\"context-item-guidelines\" className=\"flex flex-col gap-1\">\n            {detail.promptGuidelines.map((line) => (\n              <li key={line} className=\"text-sm leading-relaxed text-po-dim\">\n                {line}\n              </li>\n            ))}\n          </ul>\n        </Section>\n      ) : null}\n\n      {detail.itemKind === 'tool' && detail.parameters !== undefined ? (\n        <Section title={t('webDashboard.activity.sectionParameters', 'parameters')}>\n          <Pre testId=\"context-item-schema\">{JSON.stringify(detail.parameters, null, 2)}</Pre>\n        </Section>\n      ) : null}\n    </div>\n  );\n}\n\nfunction Section({ title, children }: { title: string; children: React.ReactNode }) {\n  return (\n    <div className=\"flex flex-col gap-1\">\n      <span className=\"text-lg font-semibold text-po-hi\">{title}</span>\n      {children}\n    </div>\n  );\n}\n\nfunction Pre({ testId, children, tall = false }: { testId: string; children: string; tall?: boolean }) {\n  return (\n    <Panel className={`overflow-auto bg-po-deep px-3 py-2 ${tall ? 'max-h-[60vh]' : 'max-h-64'}`}>\n      <pre data-testid={testId} className=\"text-sm leading-relaxed whitespace-pre-wrap text-po-dim\">\n        {children}\n      </pre>\n    </Panel>\n  );\n}\n\n/**\n * The whole prompt, and the two things a figure alone would mislead about.\n *\n * The count overlaps the panel's own total, because the tool prose and skill\n * listings priced there are inside this text. And a session that has sent\n * nothing has no built prompt to show: assembling one would fire the hooks that\n * a real turn fires, so the pre-hook base is shown and named as such.\n */\nfunction PromptBody({ detail }: { detail: Extract<ContextItemDetail, { itemKind: 'prompt' }> }) {\n  const t = useT();\n  return (\n    <div className=\"flex flex-col gap-3\">\n      <div data-testid=\"context-item-cost\" className=\"flex flex-col gap-1\">\n        <Row label={t('webDashboard.activity.rowPrompt', 'prompt')} value={tokens(detail.tokens)} strong />\n        <p className=\"text-sm text-po-dim\">\n          {t(\n            'webDashboard.activity.promptOverlapNote',\n            'counted apart from the panel total: the tool and skill prose priced there is part of this text',\n          )}\n        </p>\n        {detail.stage === 'base' ? (\n          <p data-testid=\"context-item-stage\" className=\"text-sm text-po-dim\">\n            {t(\n              'webDashboard.activity.promptBaseNote',\n              'this session has sent nothing yet, so this is the prompt before packages add to it at the first message',\n            )}\n          </p>\n        ) : null}\n      </div>\n      <Section title={t('webDashboard.activity.sectionText', 'text')}>\n        <Pre testId=\"context-item-prompt\" tall>\n          {detail.text}\n        </Pre>\n      </Section>\n    </div>\n  );\n}\n\n/** A tool is paid twice, so the split is the useful figure rather than the total. */\nfunction ToolCost({ detail }: { detail: Extract<ContextItemDetail, { itemKind: 'tool' }> }) {\n  const t = useT();\n  return (\n    <div data-testid=\"context-item-cost\" className=\"flex flex-col gap-1\">\n      <Row label={t('webDashboard.activity.rowSchema', 'schema')} value={tokens(detail.tokens.schemaTokens)} />\n      <Row\n        label={t('webDashboard.activity.systemPromptLower', 'system prompt')}\n        value={tokens(detail.tokens.promptTokens)}\n      />\n      <Row label={t('webDashboard.activity.rowTotal', 'total')} value={tokens(detail.tokens.totalTokens)} strong />\n      {detail.active ? null : (\n        <p className=\"text-sm text-po-dim\">\n          {t('webDashboard.activity.notSentToModel', 'not sent to the model; costs nothing until switched on')}\n        </p>\n      )}\n    </div>\n  );\n}\n\nfunction SkillFacts({ detail }: { detail: Extract<ContextItemDetail, { itemKind: 'skill' }> }) {\n  const t = useT();\n  return (\n    <div data-testid=\"context-item-cost\" className=\"flex flex-col gap-1\">\n      <Row label={t('webDashboard.activity.rowListing', 'listing')} value={tokens(detail.tokens)} strong />\n      {detail.filePath ? <Row label={t('webDashboard.activity.rowFile', 'file')} value={detail.filePath} /> : null}\n      {detail.modelInvocable ? null : (\n        <p className=\"text-sm text-po-dim\">\n          {t('webDashboard.activity.notOfferedToModel', 'not offered to the model')}\n        </p>\n      )}\n    </div>\n  );\n}\n\nfunction Row({ label, value, strong = false }: { label: string; value: string; strong?: boolean }) {\n  return (\n    <div className=\"flex items-baseline justify-between gap-3\">\n      <span className=\"text-sm text-po-dim\">{label}</span>\n      <span className={`truncate text-sm ${strong ? 'font-semibold text-po-text' : 'text-po-dim'}`}>{value}</span>\n    </div>\n  );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA6BA,SAAS,OAAO,OAAuB;CACrC,OAAO,IAAI,MAAM,eAAe;AAClC;AAeA,SAAgB,kBAAkB,EAAE,WAAW,QAAQ,UAAU,WAAmC;CAClG,MAAM,IAAI,KAAK;CACf,MAAM,CAAC,QAAQ,aAAa,SAAmC,IAAI;CACnE,MAAM,CAAC,OAAO,YAAY,SAAwB,IAAI;CACtD,MAAM,WAAW,QAAQ,YAAY;CACrC,MAAM,OAAO,QAAQ,QAAQ;CAI7B,MAAM,UACJ,cAAc,QAAQ,aAAa,QAAQ,SAAS,OAAO,OAAO,GAAG,UAAU,QAAQ,SAAS,QAAQ;CAC1G,MAAM,CAAC,OAAO,YAAY,SAAwB,IAAI;CACtD,IAAI,UAAU,SAAS;EACrB,SAAS,OAAO;EAChB,UAAU,IAAI;EACd,SAAS,IAAI;CACf;CAEA,gBAAgB;EACd,IAAI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