import { Component, createSignal, Show, For, createEffect } from "solid-js" import { Dialog } from "@kobalte/core/dialog" import type { Session, Agent } from "../types/session" import { getParentSessions, createSession, setActiveParentSession } from "../stores/sessions" import { instances, stopInstance } from "../stores/instances" import { agents } from "../stores/sessions" import { getLogger } from "../lib/logger" import { useI18n } from "../lib/i18n" const log = getLogger("session") interface SessionPickerProps { instanceId: string open: boolean onClose: () => void } const SessionPicker: Component = (props) => { const { t } = useI18n() const [selectedAgent, setSelectedAgent] = createSignal("") const [isCreating, setIsCreating] = createSignal(false) const instance = () => instances().get(props.instanceId) const parentSessions = () => getParentSessions(props.instanceId) const agentList = () => agents().get(props.instanceId) || [] createEffect(() => { const list = agentList() if (list.length === 0) { setSelectedAgent("") return } const current = selectedAgent() if (!current || !list.some((agent) => agent.name === current)) { setSelectedAgent(list[0].name) } }) function formatRelativeTime(timestamp: number): string { const seconds = Math.floor((Date.now() - timestamp) / 1000) const minutes = Math.floor(seconds / 60) const hours = Math.floor(minutes / 60) const days = Math.floor(hours / 24) if (days > 0) return t("time.relative.daysAgoShort", { count: days }) if (hours > 0) return t("time.relative.hoursAgoShort", { count: hours }) if (minutes > 0) return t("time.relative.minutesAgoShort", { count: minutes }) return t("time.relative.justNow") } async function handleSessionSelect(sessionId: string) { setActiveParentSession(props.instanceId, sessionId) props.onClose() } async function handleNewSession() { setIsCreating(true) try { const session = await createSession(props.instanceId, selectedAgent()) setActiveParentSession(props.instanceId, session.id) props.onClose() } catch (error) { log.error("Failed to create session:", error) } finally { setIsCreating(false) } } async function handleCancel() { await stopInstance(props.instanceId) props.onClose() } return ( !open && handleCancel()}>
{t("sessionPicker.title", { folder: instance()?.folder.split("/").pop() })}
0} fallback={
{t("sessionPicker.empty.noPrevious")}
} >

{t("sessionPicker.resume.title", { count: parentSessions().length })}

{(session) => ( )}
{t("sessionPicker.divider.or")}

{t("sessionPicker.new.title")}

0} fallback={
{t("sessionPicker.agents.loading")}
} >
) } export default SessionPicker