{"version":3,"file":"newSessionStore.cjs","names":["Store","defaultHostId","createWorkspaceSession","splitHostId","createGeneralSessionRequest","qualifyHostId","waitForSession"],"sources":["../../../src/web/stores/newSessionStore.ts"],"sourcesContent":["import type { LaunchSelection } from '@agimon-ai/po-core/web';\nimport { Store } from '@tanstack/store';\n\nimport { defaultHostId, qualifyHostId, splitHostId } from '../lib/hostIdentity';\nimport { createGeneralSessionRequest, createWorkspaceSession } from '../lib/hubApi';\nimport { applySessionUpsert, waitForSession } from './sessionsStore';\nimport { applyWorkspaceUpsert } from './workspacesStore';\n\n/** Session creation and workspace admission are distinct requests, even without a workspace id. */\nexport interface NewSessionState {\n  open: boolean;\n  kind: 'workspace' | 'general' | 'add-workspace';\n  workspaceId: string | null;\n  hostId?: string;\n}\n\nconst initialState: NewSessionState = { open: false, kind: 'add-workspace', workspaceId: null };\n\nexport const newSessionStore = new Store<NewSessionState>(initialState);\n\nexport function openNewSession(workspaceId: string | null = null): void {\n  const kind = workspaceId === null ? 'add-workspace' : 'workspace';\n  const current = newSessionStore.state;\n  if (current.open && current.kind === kind && current.workspaceId === workspaceId) return;\n  resetDraftProfile(workspaceId);\n  newSessionStore.setState(() => ({ open: true, kind, workspaceId }));\n}\n\nexport function openGeneralSession(hostId: string | undefined = defaultHostId()): void {\n  const current = newSessionStore.state;\n  if (current.open && current.kind === 'general' && current.hostId === hostId) return;\n  resetDraftProfile(generalSessionDraftKey(hostId));\n  newSessionStore.setState(() => ({\n    open: true,\n    kind: 'general',\n    workspaceId: null,\n    ...(hostId === undefined ? {} : { hostId }),\n  }));\n}\n\n/** Independent drafts per target host, never a selected workspace's draft. */\nexport function generalSessionDraftKey(hostId?: string): string {\n  return `general-session:${hostId ?? ''}`;\n}\n\nexport function closeNewSession(): void {\n  const creating = newSessionStore.state;\n  const draftKey = creating.kind === 'general' ? generalSessionDraftKey(creating.hostId) : creating.workspaceId;\n  if (draftKey !== null && newSessionDraftStore.state[draftKey]?.pending) return;\n  if (draftKey !== null)\n    newSessionDraftStore.setState((state) => {\n      const next = { ...state };\n      delete next[draftKey];\n      return next;\n    });\n  newSessionStore.setState((state) => (state.open ? initialState : state));\n}\n\n/** Test seam. */\nexport function resetNewSessionStore(): void {\n  newSessionStore.setState(() => initialState);\n  newSessionDraftStore.setState(() => ({}));\n}\n\nexport interface NewSessionDraft {\n  name: string;\n  /**\n   * The launch profile: undefined until one is chosen (a workspace default may apply), null for an explicit\n   * \"No profile\", otherwise a profile name. Never restored from a past launch.\n   */\n  profile?: string | null | undefined;\n  pending: boolean;\n  error: string;\n  createdId?: string;\n}\nexport const EMPTY_NEW_SESSION_DRAFT: NewSessionDraft = { name: '', pending: false, error: '' };\nexport const newSessionDraftStore = new Store<Record<string, NewSessionDraft>>({});\n\nfunction patchDraft(draftKey: string, update: Partial<NewSessionDraft>): void {\n  newSessionDraftStore.setState((state) => ({\n    ...state,\n    [draftKey]: { ...(state[draftKey] ?? EMPTY_NEW_SESSION_DRAFT), ...update },\n  }));\n}\n\nexport function setNewSessionName(draftKey: string, name: string): void {\n  patchDraft(draftKey, { name });\n}\n\nexport function setNewSessionProfile(draftKey: string, profile: string | null | undefined): void {\n  patchDraft(draftKey, { profile });\n}\n\nfunction resetDraftProfile(draftKey: string | null): void {\n  if (draftKey !== null && newSessionDraftStore.state[draftKey]?.profile !== undefined)\n    setNewSessionProfile(draftKey, undefined);\n}\n\n/** The create request's selection: only a made choice is sent, so omission keeps defaults eligible. */\nfunction draftSelection(draft: NewSessionDraft): LaunchSelection | undefined {\n  return draft.profile === undefined ? undefined : { profile: draft.profile };\n}\n\n/** A remount cannot admit another creation. A visibility retry reuses the already created session. */\nexport function createPlainSession(\n  workspaceId: string,\n  name?: string,\n  selection?: LaunchSelection,\n): Promise<{ sessionId: string } | { error: string }> {\n  // A host-qualified workspace answers with its own local session id; qualify it like the rail does.\n  return createFromDraft(\n    workspaceId,\n    name,\n    selection,\n    (draftName, chosen) =>\n      createWorkspaceSession(workspaceId, { name: draftName, ...(chosen === undefined ? {} : { selection: chosen }) }),\n    splitHostId(workspaceId)?.hostId,\n  );\n}\n\nexport function createGeneralSession(\n  name?: string,\n  hostId?: string,\n  selection?: LaunchSelection,\n): Promise<{ sessionId: string } | { error: string }> {\n  return createFromDraft(\n    generalSessionDraftKey(hostId),\n    name,\n    selection,\n    (draftName, chosen) =>\n      createGeneralSessionRequest({ name: draftName, ...(chosen === undefined ? {} : { selection: chosen }) }, hostId),\n    hostId,\n  );\n}\n\nasync function createFromDraft(\n  draftKey: string,\n  name: string | undefined,\n  selection: LaunchSelection | undefined,\n  create: (\n    name: string | undefined,\n    selection: LaunchSelection | undefined,\n  ) => ReturnType<typeof createWorkspaceSession>,\n  hostId?: string,\n): Promise<{ sessionId: string } | { error: string }> {\n  const draft = newSessionDraftStore.state[draftKey] ?? EMPTY_NEW_SESSION_DRAFT;\n  if (draft.pending) return { error: 'Session creation is already pending.' };\n  patchDraft(draftKey, { pending: true, error: '' });\n  try {\n    let sessionId = draft.createdId;\n    if (sessionId === undefined) {\n      const outcome = await create((name ?? draft.name).trim() || undefined, selection ?? draftSelection(draft));\n      if (!('sessionId' in outcome)) {\n        patchDraft(draftKey, { error: outcome.error });\n        return outcome;\n      }\n      sessionId = hostId === undefined ? outcome.sessionId : qualifyHostId(hostId, outcome.sessionId);\n      patchDraft(draftKey, { createdId: sessionId });\n      if (outcome.workspace !== undefined) applyWorkspaceUpsert({ workspace: outcome.workspace }, hostId);\n      if (outcome.session !== undefined) applySessionUpsert({ session: outcome.session }, hostId);\n    }\n    if (!(await waitForSession(sessionId))) {\n      const error = 'The session was created but has not appeared yet; it will show up in the rail.';\n      patchDraft(draftKey, { error });\n      return { error };\n    }\n    return { sessionId };\n  } catch (cause) {\n    const error = cause instanceof Error ? cause.message : String(cause);\n    patchDraft(draftKey, { error });\n    return { error };\n  } finally {\n    patchDraft(draftKey, { pending: false });\n  }\n}\n"],"mappings":";;;;;;AAgBA,MAAM,eAAgC;CAAE,MAAM;CAAO,MAAM;CAAiB,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