import { createMemo, type Setter } from "solid-js" import { useKV } from "./kv" export type ThinkingMode = "show" | "hide" const MODES: readonly ThinkingMode[] = ["show", "hide"] as const // OpenAI's Responses API surfaces reasoning summaries that start with a bolded // title block: "**Inspecting PR workflow**\n\n
". Treat that first block, // or a complete title still awaiting its body while streaming, as disclosure // metadata so the TUI can style its header independently from the markdown body. export function reasoningSummary(text: string) { const content = text.trim() const match = content.match(/^\*\*([^*\n]+)\*\*(?:\r?\n\r?\n|$)/) if (!match) return { title: null, body: content } return { title: match[1].trim(), body: content.slice(match[0].length).trimEnd() } } export function isThinkingMode(value: unknown): value is ThinkingMode { return typeof value === "string" && (MODES as readonly string[]).includes(value) } // Cycle order matches the slash command: show → hide → show. export function nextThinkingMode(current: ThinkingMode): ThinkingMode { const idx = MODES.indexOf(current) return MODES[(idx + 1) % MODES.length] ?? "show" } export function useThinkingMode() { const kv = useKV() // Capture pre-state before `kv.signal` seeds a default, so we can detect // first-time users with a legacy `thinking_visibility` boolean and migrate. // The KVProvider only renders children once kv.ready, so reads here are safe. const hadStored = kv.get("thinking_mode") !== undefined const legacy = kv.get("thinking_visibility") const [stored, setStored] = kv.signal