{"version":3,"file":"toolPrompt.mjs","names":[],"sources":["../../../src/web/lib/toolPrompt.ts"],"sourcesContent":["import type { DialogPromptContribution, ToolPromptContribution, ToolPromptDialog } from '@agimon-ai/po-core/web';\n\nimport { HOST_SLOTS, pluginToolRenderer, sessionToolRenderer, slotFills } from './pluginRegistry';\nimport type { DialogRequest, SessionState, ToolEntry } from './sessionModel';\n\n/**\n * Which running tool, if any, owns the open extension UI request.\n *\n * The composer and the dialog have to agree, or the reader gets both surfaces\n * or neither, so the verdict is one pure function they both read rather than\n * a flag each keeps. It is pure for the same reason the rest of sessionModel\n * is: the interesting cases are combinations of session state, and none of\n * them need a browser to exercise.\n *\n * The pairing is an inference. Pi's protocol does not say which tool a request\n * belongs to, only that one is open, so this reads the newest running tool\n * whose plugin declared a prompt and lets that plugin confirm the request is\n * its own. A tool that declares no prompt never participates, and a request no\n * prompt claims stays with the host's dialog.\n */\nexport interface ToolPromptClaim {\n  entry: ToolEntry;\n  prompt: ToolPromptContribution;\n  dialog: ToolPromptDialog;\n}\n\nexport interface DialogPromptClaim {\n  entry?: undefined;\n  prompt: DialogPromptContribution;\n  dialog: ToolPromptDialog;\n}\n\nexport type ComposerPromptClaim = ToolPromptClaim | DialogPromptClaim;\n/** The request as a plugin reads it; the same fields, without the host's own type. */\nfunction promptDialog(dialog: DialogRequest): ToolPromptDialog {\n  return {\n    id: dialog.id,\n    method: dialog.method,\n    title: dialog.title,\n    message: dialog.message,\n    options: dialog.options,\n    placeholder: dialog.placeholder,\n    prefill: dialog.prefill,\n    ...(dialog.approval === undefined ? {} : { approval: dialog.approval }),\n  };\n}\n\n/** The three facts the verdict rests on, so a caller can memoize on exactly those. */\nexport type ToolPromptInput = Pick<SessionState, 'dialog' | 'entries' | 'activeTools' | 'statuses'>;\n\n/**\n * `spokenFor` is a request some other surface has already taken: the one the\n * selection bar claimed with a click, or one whose prompt threw and handed it\n * back. Passed in rather than read, because both live in stores and this layer\n * sits below them so that every surface can reach it.\n */\nexport function toolPromptClaim(\n  state: ToolPromptInput,\n  spokenFor: string | null,\n  sessionId?: string,\n): ComposerPromptClaim | null {\n  const request = state.dialog;\n  if (request === null) return null;\n  if (spokenFor === request.id) return null;\n\n  const dialog = promptDialog(request);\n  // A dialog-owned prompt needs no guessed tool pairing, including parallel code-mode calls.\n  for (const fill of slotFills(HOST_SLOTS.dialogPrompt, sessionId)) {\n    const prompt = fill.data as Partial<DialogPromptContribution> | undefined;\n    if (typeof prompt?.claims !== 'function' || typeof prompt.component !== 'function') continue;\n    try {\n      if (prompt.claims(dialog)) return { prompt: prompt as DialogPromptContribution, dialog };\n    } catch {\n      // A broken claim leaves the generic answer surface reachable.\n      continue;\n    }\n  }\n  // An approval must never be taken by another tool's broad prompt matcher.\n  if (dialog.approval !== undefined) return null;\n  // Live tools are kept outside the protocol-owned transcript. Search those\n  // first, then fall back to timeline tools for threads and direct frame clients.\n  const groups = [state.activeTools, state.entries] as const;\n  for (const entries of groups) {\n    for (let index = entries.length - 1; index >= 0; index -= 1) {\n      const entry = entries[index];\n      if (entry === undefined || entry.kind !== 'tool' || !entry.running) continue;\n      const renderer =\n        sessionId === undefined\n          ? pluginToolRenderer(entry.name, state.statuses, entry.result?.details)\n          : sessionToolRenderer(sessionId, entry.name, state.statuses, entry.result?.details);\n      const prompt = renderer?.prompt;\n      if (prompt === undefined) continue;\n      try {\n        if (prompt.claims?.(dialog, entry.args) === false) continue;\n      } catch {\n        // A broken claimant must leave the host's answer and cancellation surface reachable.\n        continue;\n      }\n      return { entry, prompt, dialog };\n    }\n  }\n  return null;\n}\n"],"mappings":";;;AAkCA,SAAS,aAAa,QAAyC;CAC7D,OAAO;EACL,IAAI,OAAO;EACX,QAAQ,OAAO;EACf,OAAO,OAAO;EACd,SAAS,OAAO;EAChB,SAAS,OAAO;EAChB,aAAa,OAAO;EACpB,SAAS,OAAO;EAChB,GAAI,OAAO,aAAa,KAAA,IAAY,CAAC,IAAI,EAAE,UAAU,OAAO,SAAS;CACvE;AACF;;;;;;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