|
| 1 | +import { For, Show } from "solid-js" |
| 2 | +import type { Composer, ComposerMode } from "../composer" |
| 3 | + |
| 4 | +export const ComposerControls = (props: { |
| 5 | + composer: Composer |
| 6 | + connected: boolean |
| 7 | + busy: boolean |
| 8 | + selectMode: (mode: ComposerMode) => void |
| 9 | + submit: () => void |
| 10 | +}) => { |
| 11 | + const state = props.composer.state |
| 12 | + const draft = () => (state.mode === "aside" ? state.aside : state.task) |
| 13 | + const modes = ["aside", "queue", "steer"] as const |
| 14 | + const reason = () => |
| 15 | + !props.connected ? "Disconnected or loading. Reconnect before sending." : props.composer.blockedReason() |
| 16 | + return ( |
| 17 | + <div class="composer-controls" data-composer-controls> |
| 18 | + <Show when={state.mode !== "send" || props.busy}> |
| 19 | + <div class="composer-mode-row"> |
| 20 | + <div class="composer-modes" role="radiogroup" aria-label="Message delivery"> |
| 21 | + <For each={modes}> |
| 22 | + {(mode, index) => ( |
| 23 | + <button |
| 24 | + type="button" |
| 25 | + role="radio" |
| 26 | + aria-checked={state.mode === mode} |
| 27 | + tabIndex={state.mode === mode ? 0 : -1} |
| 28 | + classList={{ selected: state.mode === mode }} |
| 29 | + onClick={() => props.selectMode(mode)} |
| 30 | + onKeyDown={(e) => { |
| 31 | + if (!["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", "Home", "End"].includes(e.key)) return |
| 32 | + e.preventDefault() |
| 33 | + e.stopPropagation() |
| 34 | + const rtl = getComputedStyle(e.currentTarget).direction === "rtl" |
| 35 | + const back = e.key === "ArrowUp" || e.key === (rtl ? "ArrowRight" : "ArrowLeft") |
| 36 | + const next = e.key === "Home" ? 0 : e.key === "End" ? 2 : (index() + (back ? 2 : 1)) % 3 |
| 37 | + const group = e.currentTarget.parentElement |
| 38 | + props.selectMode(modes[next]!) |
| 39 | + group?.querySelectorAll<HTMLButtonElement>("button")[next]?.focus() |
| 40 | + }} |
| 41 | + > |
| 42 | + {mode[0]!.toUpperCase() + mode.slice(1)} |
| 43 | + </button> |
| 44 | + )} |
| 45 | + </For> |
| 46 | + </div> |
| 47 | + <Show when={!props.busy && props.connected}> |
| 48 | + <button onClick={() => props.selectMode("send")}>Use normal Send</button> |
| 49 | + </Show> |
| 50 | + </div> |
| 51 | + </Show> |
| 52 | + <div class="composer-help dim" role="status" aria-live="polite"> |
| 53 | + {state.mode === "aside" |
| 54 | + ? "Ask about a snapshot without changing the task. Task draft saved; the first Aside starts as a copy." |
| 55 | + : state.mode === "queue" |
| 56 | + ? "Wait until the task would otherwise finish. Uses the session agent and model." |
| 57 | + : state.mode === "steer" |
| 58 | + ? "Guide the task at its next safe boundary. Uses the session agent and model." |
| 59 | + : "Send a message to this session."} |
| 60 | + </div> |
| 61 | + <Show when={reason()}> |
| 62 | + <div class="composer-help dim">{reason()}</div> |
| 63 | + </Show> |
| 64 | + <Show when={state.storageError}> |
| 65 | + <div class="err" role="alert"> |
| 66 | + {state.storageError} |
| 67 | + <button onClick={() => props.composer.setImages([])}>Clear saved images</button> |
| 68 | + </div> |
| 69 | + </Show> |
| 70 | + <Show when={state.error}> |
| 71 | + <div class="err" role="alert"> |
| 72 | + {state.error} |
| 73 | + </div> |
| 74 | + </Show> |
| 75 | + <div class="composer-actions"> |
| 76 | + <button |
| 77 | + class="composer-send" |
| 78 | + disabled={!!reason() || (!draft().text.trim() && !draft().images.length)} |
| 79 | + onClick={props.submit} |
| 80 | + > |
| 81 | + {props.composer.actionLabel()} |
| 82 | + </button> |
| 83 | + <span class="dim send-hint">{state.sending ? "Sending..." : "Cmd/Ctrl+Enter"}</span> |
| 84 | + <Show when={state.mode !== "send"}> |
| 85 | + <button |
| 86 | + disabled={!props.connected || state.inputLoading} |
| 87 | + onClick={async () => { |
| 88 | + await props.composer.loadCapabilities() |
| 89 | + await props.composer.refreshInputs() |
| 90 | + }} |
| 91 | + > |
| 92 | + Refresh availability |
| 93 | + </button> |
| 94 | + </Show> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +export const ComposerResults = (props: { composer: Composer; connected: boolean }) => { |
| 101 | + const state = props.composer.state |
| 102 | + return ( |
| 103 | + <div class="composer-results" data-composer-controls> |
| 104 | + <Show when={state.admission}> |
| 105 | + {(admission) => ( |
| 106 | + <section class="input-admission" aria-label="Input admission" aria-live="polite"> |
| 107 | + <b> |
| 108 | + {admission().payload.delivery}:{" "} |
| 109 | + {admission().status === "sending" ? "Confirming admission..." : "Admission unknown"} |
| 110 | + </b> |
| 111 | + <p dir="auto">{admission().payload.text}</p> |
| 112 | + <div class="dim">{admission().error}</div> |
| 113 | + <Show when={admission().status === "unknown"}> |
| 114 | + <button disabled={!props.connected || state.inputLoading} onClick={() => props.composer.refreshInputs()}> |
| 115 | + Check admission |
| 116 | + </button> |
| 117 | + <button disabled={!props.connected || state.sending} onClick={() => props.composer.retryAdmission()}> |
| 118 | + Check and retry same input |
| 119 | + </button> |
| 120 | + </Show> |
| 121 | + </section> |
| 122 | + )} |
| 123 | + </Show> |
| 124 | + <Show when={state.receipts.length}> |
| 125 | + <details class="input-receipts" open={!state.asideRequest}> |
| 126 | + <summary>Task inputs ({state.receipts.filter((item) => item.state === "pending").length} pending)</summary> |
| 127 | + <div class="dim">Added to conversation does not mean the model has finished.</div> |
| 128 | + <button disabled={!props.connected || state.inputLoading} onClick={() => props.composer.refreshInputs()}> |
| 129 | + Refresh inputs |
| 130 | + </button> |
| 131 | + <ul> |
| 132 | + <For each={state.receipts}> |
| 133 | + {(item) => ( |
| 134 | + <li data-request-id={item.requestID}> |
| 135 | + <span> |
| 136 | + <b>{item.delivery}</b>{" "} |
| 137 | + <span class="dim" role="status"> |
| 138 | + {item.state === "promoted" ? "Added to conversation" : item.state} |
| 139 | + </span> |
| 140 | + </span> |
| 141 | + <p dir="auto">{item.text}</p> |
| 142 | + <Show when={item.state === "pending"}> |
| 143 | + <button |
| 144 | + disabled={!props.connected} |
| 145 | + aria-label={`Cancel ${item.delivery} input: ${item.text}`} |
| 146 | + onClick={() => props.composer.cancelInput(item.requestID)} |
| 147 | + > |
| 148 | + Cancel input |
| 149 | + </button> |
| 150 | + </Show> |
| 151 | + </li> |
| 152 | + )} |
| 153 | + </For> |
| 154 | + </ul> |
| 155 | + </details> |
| 156 | + </Show> |
| 157 | + <Show when={state.asideRequest}> |
| 158 | + {(aside) => ( |
| 159 | + <section class="aside-result" aria-label="Aside result"> |
| 160 | + <div class="composer-actions"> |
| 161 | + <b>Aside</b> |
| 162 | + <span role="status" class="dim"> |
| 163 | + {aside().status} |
| 164 | + </span> |
| 165 | + <Show when={aside().status === "running" || aside().status === "unknown"}> |
| 166 | + <button onClick={() => props.composer.cancelAside()}>Cancel aside</button> |
| 167 | + </Show> |
| 168 | + <button onClick={() => props.composer.closeAside()}>Close aside</button> |
| 169 | + </div> |
| 170 | + <p class="aside-question" dir="auto"> |
| 171 | + {aside().question} |
| 172 | + </p> |
| 173 | + <Show when={aside().snapshot}> |
| 174 | + {(snapshot) => ( |
| 175 | + <div class="dim aside-snapshot"> |
| 176 | + Snapshot {new Date(snapshot().capturedAt).toLocaleString()} · {snapshot().activity.status} |
| 177 | + <span> |
| 178 | + Through <bdi>{snapshot().throughMessageID ?? "start of conversation"}</bdi>;{" "} |
| 179 | + {snapshot().excludedMessageCount} messages excluded |
| 180 | + </span> |
| 181 | + </div> |
| 182 | + )} |
| 183 | + </Show> |
| 184 | + <Show when={aside().error}> |
| 185 | + <div role="alert" class="err"> |
| 186 | + {aside().error} |
| 187 | + </div> |
| 188 | + </Show> |
| 189 | + <Show when={aside().text}> |
| 190 | + <div class="aside-answer" dir="auto" tabIndex={0}> |
| 191 | + {aside().text} |
| 192 | + </div> |
| 193 | + </Show> |
| 194 | + </section> |
| 195 | + )} |
| 196 | + </Show> |
| 197 | + </div> |
| 198 | + ) |
| 199 | +} |
0 commit comments