{"version":3,"file":"composerSubmissionStore.cjs","names":["traceStore","Store","toolPathHint","sessionsStore","composerStore","tabComposerContext","submitMessageWithAck","queueFollowUpWithAck"],"sources":["../../../src/web/stores/composerSubmissionStore.ts"],"sourcesContent":["import type { TransientTab, WebPluginContextItem } from '@agimon-ai/po-core/web';\nimport { Store } from '@tanstack/store';\n\nimport { toolPathHint } from '../lib/attachmentNotes';\nimport { publishComposerSubmission } from '../lib/composerSubmissions';\nimport { traceStore } from '../lib/uiTrace';\nimport {\n  clearComposerState,\n  composerStore,\n  type ComposerAttachment,\n  type ComposerImageAttachment,\n  tabComposerContext,\n} from './composerStore';\nimport { sessionsStore } from './sessionsStore';\nimport { applySessionFrame, queueFollowUpWithAck, submitMessageWithAck } from './sessionStore';\n\n/** Admission belongs to the session, not to whichever template currently shows its composer. */\nexport const composerSubmissionStore = traceStore(\n  'composerSubmissions',\n  new Store<ReadonlySet<string>>(new Set<string>()),\n);\n\nexport function attachmentPrompt(\n  draft: string,\n  attachments: ComposerAttachment[],\n  tabContext?: WebPluginContextItem,\n): string {\n  const parts: string[] = [];\n  const trimmed = draft.trim();\n  if (trimmed) parts.push(trimmed);\n  for (const attachment of attachments) {\n    if (attachment.kind === 'image') continue;\n    const safeName = attachment.name.replace(/[\\r\\n]/g, ' ').slice(0, 200);\n    if (attachment.kind === 'file') {\n      parts.push(\n        `Attached file \"${safeName}\" (${attachment.mimeType}, ${String(attachment.size)} bytes) saved at ${attachment.path}. ${toolPathHint(attachment.path)}`,\n      );\n      continue;\n    }\n    parts.push(\n      attachment.kind === 'context'\n        ? `Referenced context \"${safeName}\":\\n\\n${attachment.content}`\n        : `Attached file \"${safeName}\":\\n\\n${attachment.content}`,\n    );\n    if (attachment.kind === 'text' && attachment.path !== undefined) {\n      parts.push(`\"${safeName}\" is also saved at ${attachment.path}. ${toolPathHint(attachment.path)}`);\n    }\n  }\n  if (parts.length === 0 && attachments.some((attachment) => attachment.kind === 'image')) {\n    parts.push('Please review the attached image.');\n  }\n  for (const attachment of attachments) {\n    if (attachment.kind !== 'image' || attachment.path === undefined) continue;\n    const safeName = attachment.name.replace(/[\\r\\n]/g, ' ').slice(0, 200);\n    parts.push(`Attached image \"${safeName}\" is also saved at ${attachment.path}. ${toolPathHint(attachment.path)}`);\n  }\n  if (tabContext !== undefined) parts.push(`Referenced context \"${tabContext.label}\":\\n\\n${tabContext.content}`);\n  return parts.join('\\n\\n');\n}\n\n/** Reads the latest draft and claims admission synchronously, including across a dashboard remount. */\nexport async function submitComposerDraft(\n  sessionId: string | null,\n  delivery: 'submit' | 'queue',\n  composerContext?: TransientTab['composerContext'],\n): Promise<boolean> {\n  if (sessionId === null || composerSubmissionStore.state.has(sessionId)) return false;\n  const meta = sessionsStore.state.byId[sessionId];\n  const draft = composerStore.state[sessionId];\n  if (\n    !draft ||\n    draft.pendingAttachments > 0 ||\n    (!draft.draft.trim() && draft.attachments.length === 0) ||\n    meta?.attach !== 'attached' ||\n    meta.summary.dormant\n  )\n    return false;\n\n  composerSubmissionStore.setState((pending) => new Set([...pending, sessionId]));\n  try {\n    const tabContext = tabComposerContext(composerContext, sessionId, draft.draft);\n    const message = attachmentPrompt(draft.draft, draft.attachments, tabContext);\n    const images = draft.attachments\n      .filter((attachment): attachment is ComposerImageAttachment => attachment.kind === 'image')\n      .map(({ data, mimeType }) => ({ type: 'image' as const, data, mimeType }));\n    const contextItems: WebPluginContextItem[] = draft.attachments.flatMap((attachment) =>\n      attachment.kind === 'context'\n        ? [\n            {\n              kind: attachment.contextKind,\n              source: attachment.source,\n              id: attachment.contextId,\n              label: attachment.name,\n              content: attachment.content,\n              ...(attachment.url === undefined ? {} : { url: attachment.url }),\n            },\n          ]\n        : [],\n    );\n    if (tabContext !== undefined) contextItems.push(tabContext);\n    const accepted =\n      delivery === 'submit'\n        ? await submitMessageWithAck(message, images, sessionId)\n        : await queueFollowUpWithAck(message, images, sessionId);\n    if (!accepted) return false;\n    publishComposerSubmission({ sessionId, message, delivery, submittedAt: Date.now(), contextItems });\n    const current = composerStore.state[sessionId];\n    if (current?.draft === draft.draft && current.attachments === draft.attachments) clearComposerState(sessionId);\n    return true;\n  } catch (error) {\n    applySessionFrame(sessionId, { type: 'error', message: error instanceof Error ? error.message : String(error) });\n    return false;\n  } finally {\n    composerSubmissionStore.setState((pending) => {\n      const next = new Set(pending);\n      next.delete(sessionId);\n      return next;\n    });\n  }\n}\n"],"mappings":";;;;;;;;;AAiBA,MAAa,0BAA0BA,gBAAAA,WACrC,uBACA,IAAIC,gBAAAA,sBAA2B,IAAI,IAAY,CAAC,CAClD;AAEA,SAAgB,iBACd,OACA,aACA,YACQ;CACR,MAAM,QAAkB,CAAC;CACzB,MAAM,UAAU,MAAM,KAAK;CAC3B,IAAI,SAAS,MAAM,KAAK,OAAO;CAC/B,KAAK,MAAM,cAAc,aAAa;EACpC,IAAI,WAAW,SAAS,SAAS;EACjC,MAAM,WAAW,WAAW,KAAK,QAAQ,WAAW,GAAG,CAAC,CAAC,MAAM,GAAG,GAAG;EACrE,IAAI,WAAW,SAAS,QAAQ;GAC9B,MAAM,KACJ,kBAAkB,SAAS,KAAK,WAAW,SAAS,IAAI,OAAO,WAAW,IAAI,EAAE,mBAAmB,WAAW,KAAK,IAAIC,wBAAAA,aAAa,WAAW,IAAI,GACrJ;GACA;EACF;EACA,MAAM,KACJ,WAAW,SAAS,YAChB,uBAAuB,SAAS,QAAQ,WAAW,YACnD,kBAAkB,SAAS,QAAQ,WAAW,SACpD;EACA,IAAI,WAAW,SAAS,UAAU,WAAW,SAAS,KAAA,GACpD,MAAM,KAAK,IAAI,SAAS,qBAAqB,WAAW,KAAK,IAAIA,wBAAAA,aAAa,WAAW,IAAI,GAAG;CAEpG;CACA,IAAI,MAAM,WAAW,KAAK,YAAY,MAAM,eAAe,WAAW,SAAS,OAAO,GACpF,MAAM,KAAK,mCAAmC;CAEhD,KAAK,MAAM,cAAc,aAAa;EACpC,IAAI,WAAW,SAAS,WAAW,WAAW,SAAS,KAAA,GAAW;EA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