mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
feat(app): float the voice call panel and keep the composer usable (#58587)
This commit is contained in:
committed by
GitHub
Unverified
parent
3347afd09b
commit
d9a0805c25
@@ -8,6 +8,9 @@ export const dict = {
|
||||
"voice.end": "End",
|
||||
"voice.rejoin": "Rejoin",
|
||||
"voice.level": "Audio level",
|
||||
"voice.panel": "Voice call",
|
||||
"voice.captions.show": "Show captions",
|
||||
"voice.captions.hide": "Hide captions",
|
||||
"voice.connecting": "Connecting…",
|
||||
"voice.you": "You:",
|
||||
"voice.assistant": "OpenCode:",
|
||||
|
||||
@@ -15,6 +15,7 @@ export type CallState = {
|
||||
level: number
|
||||
caption: string
|
||||
speaker: "user" | "assistant"
|
||||
utterance?: number
|
||||
title: string
|
||||
}
|
||||
|
||||
@@ -156,7 +157,7 @@ export function createVoiceCall(input: {
|
||||
if (speaker === "user") gates.user(true)
|
||||
|
||||
if (speaker === "assistant") gates.assistant(true)
|
||||
input.change({ caption, speaker })
|
||||
input.change({ caption, speaker, utterance: tail.latest })
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
import { motionTuning } from "@opencode/ui/motion-tuning"
|
||||
import { Button } from "@opencode/ui/button"
|
||||
import { createEffect, onCleanup, onMount, Show, type ParentProps } from "solid-js"
|
||||
import { Icon } from "@opencode/ui/icon"
|
||||
import { IconButton } from "@opencode/ui/icon-button"
|
||||
import { Loader } from "@opencode/ui/loader"
|
||||
import { Schema } from "effect"
|
||||
import { createEffect, For, onCleanup, onMount, Show, untrack, type ParentProps } from "solid-js"
|
||||
import { createStore } from "solid-js/store"
|
||||
import type { SessionInfo } from "@opencode/client/promise"
|
||||
import { useServer } from "@/runtime/server/current"
|
||||
import { useLanguage } from "@/runtime/i18n/language"
|
||||
import { usePlatform } from "@/runtime/platform/platform"
|
||||
import { Persist, persisted } from "@/runtime/persistence/storage"
|
||||
import { Persistence } from "@/runtime/persistence/schema"
|
||||
import en from "@/runtime/i18n/en"
|
||||
import { useVoice } from "./context"
|
||||
import "./voice.css"
|
||||
@@ -17,17 +23,22 @@ const toggle = motionTuning.toggle("voice.calls", {
|
||||
value: false,
|
||||
})
|
||||
|
||||
// Where the panel was dragged to, as an offset from its bottom-right home.
|
||||
const PanelState = Persistence.struct({
|
||||
x: Schema.Number,
|
||||
y: Schema.Number,
|
||||
expanded: Schema.Boolean,
|
||||
})
|
||||
|
||||
export function VoiceComposer(props: ParentProps<{ session: SessionInfo | undefined }>) {
|
||||
const voice = useVoice()
|
||||
const server = useServer()
|
||||
const language = useLanguage()
|
||||
const platform = usePlatform()
|
||||
const [state, setState] = createStore({ enabled: toggle() })
|
||||
onMount(() => onCleanup(voice.mountComposer()))
|
||||
onCleanup(motionTuning.subscribe(() => setState("enabled", toggle())))
|
||||
const enabled = () =>
|
||||
platform.platform === "desktop" && import.meta.env.VITE_OPENCODE_CHANNEL !== "prod" && state.enabled
|
||||
const active = () => voice.state.status !== "idle"
|
||||
createEffect(() => {
|
||||
const session = props.session
|
||||
|
||||
@@ -35,53 +46,137 @@ export function VoiceComposer(props: ParentProps<{ session: SessionInfo | undefi
|
||||
})
|
||||
|
||||
return (
|
||||
<Show
|
||||
when={active()}
|
||||
fallback={
|
||||
<>
|
||||
{props.children}
|
||||
<Show when={enabled() && props.session}>
|
||||
<div class="voice-call-action">
|
||||
<Button
|
||||
size="small"
|
||||
variant="ghost"
|
||||
onClick={() => {
|
||||
const session = props.session
|
||||
<>
|
||||
{props.children}
|
||||
<Show when={enabled() && props.session && voice.state.status === "idle"}>
|
||||
<div class="voice-call-action">
|
||||
<Button
|
||||
size="small"
|
||||
variant="ghost"
|
||||
onClick={() => {
|
||||
const session = props.session
|
||||
|
||||
if (session) void voice.start(server.key, server.ctx, session)
|
||||
}}
|
||||
>
|
||||
{language.t("voice.call")}
|
||||
</Button>
|
||||
</div>
|
||||
</Show>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<VoiceCapsule />
|
||||
</Show>
|
||||
if (session) void voice.start(server.key, server.ctx, session)
|
||||
}}
|
||||
>
|
||||
{language.t("voice.call")}
|
||||
</Button>
|
||||
</div>
|
||||
</Show>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** The call's floating panel. It sits beside the composer, never replaces it, and can be dragged anywhere. */
|
||||
export function VoiceOverlay() {
|
||||
const voice = useVoice()
|
||||
|
||||
return (
|
||||
<Show when={voice.state.status !== "idle" && !voice.state.composers}>
|
||||
<div class="voice-overlay">
|
||||
<VoiceCapsule />
|
||||
</div>
|
||||
<Show when={voice.state.status !== "idle"}>
|
||||
<VoicePanel />
|
||||
</Show>
|
||||
)
|
||||
}
|
||||
|
||||
function VoiceCapsule() {
|
||||
function VoicePanel() {
|
||||
const voice = useVoice()
|
||||
const language = useLanguage()
|
||||
let panel!: HTMLElement
|
||||
|
||||
const [position, setPosition, , ready] = persisted(Persist.global("voice.panel"), PanelState, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
expanded: false,
|
||||
})
|
||||
|
||||
// How far the panel can move while staying inside the window, read once per drag rather than every frame.
|
||||
const bounds = () => {
|
||||
const rect = panel.getBoundingClientRect()
|
||||
|
||||
return {
|
||||
x: [position.x - rect.left, position.x + window.innerWidth - rect.right],
|
||||
y: [position.y - rect.top, position.y + window.innerHeight - rect.bottom],
|
||||
}
|
||||
}
|
||||
|
||||
const clamp = (limits: ReturnType<typeof bounds>, x: number, y: number) => ({
|
||||
x: Math.min(Math.max(x, limits.x[0]), Math.max(limits.x[0], limits.x[1])),
|
||||
y: Math.min(Math.max(y, limits.y[0]), Math.max(limits.y[0], limits.y[1])),
|
||||
})
|
||||
|
||||
// Before stored state loads, a write would mark the store edited and drop the saved position.
|
||||
const settle = () => {
|
||||
if (!ready()) return
|
||||
const next = clamp(bounds(), position.x, position.y)
|
||||
|
||||
if (next.x !== position.x || next.y !== position.y) setPosition(next)
|
||||
}
|
||||
|
||||
const grab = (event: PointerEvent) => {
|
||||
if (
|
||||
event.button !== 0 ||
|
||||
!(event.target instanceof Element) ||
|
||||
event.target.closest("button") ||
|
||||
onScrollbar(event)
|
||||
)
|
||||
return
|
||||
event.preventDefault()
|
||||
const limits = bounds()
|
||||
const start = { x: event.clientX - position.x, y: event.clientY - position.y }
|
||||
panel.setPointerCapture(event.pointerId)
|
||||
|
||||
const move = (moved: PointerEvent) => setPosition(clamp(limits, moved.clientX - start.x, moved.clientY - start.y))
|
||||
|
||||
const drop = () => {
|
||||
panel.removeEventListener("pointermove", move)
|
||||
panel.removeEventListener("pointerup", drop)
|
||||
panel.removeEventListener("pointercancel", drop)
|
||||
panel.removeEventListener("lostpointercapture", drop)
|
||||
}
|
||||
|
||||
panel.addEventListener("pointermove", move)
|
||||
panel.addEventListener("pointerup", drop)
|
||||
panel.addEventListener("pointercancel", drop)
|
||||
panel.addEventListener("lostpointercapture", drop)
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
const observer = new ResizeObserver(settle)
|
||||
observer.observe(panel)
|
||||
window.addEventListener("resize", settle)
|
||||
onCleanup(() => {
|
||||
observer.disconnect()
|
||||
window.removeEventListener("resize", settle)
|
||||
})
|
||||
})
|
||||
|
||||
// A saved position from a larger window is pulled back on screen once storage has loaded.
|
||||
createEffect(() => {
|
||||
if (ready()) untrack(settle)
|
||||
})
|
||||
|
||||
const speaker = (value: "user" | "assistant") => language.t(value === "user" ? "voice.you" : "voice.assistant")
|
||||
|
||||
return (
|
||||
<section class="voice-capsule" aria-label={language.t("voice.call")}>
|
||||
<section
|
||||
ref={panel}
|
||||
class="voice-panel"
|
||||
aria-label={language.t("voice.panel")}
|
||||
style={{ transform: `translate(${position.x}px, ${position.y}px)` }}
|
||||
onPointerDown={grab}
|
||||
// Pointer presses never move focus, so typing in the composer carries on while the call is controlled.
|
||||
onMouseDown={(event) => {
|
||||
if (!onScrollbar(event)) event.preventDefault()
|
||||
}}
|
||||
>
|
||||
<div class="voice-controls">
|
||||
<meter class="voice-level" min={0} max={1} value={voice.state.level} aria-label={language.t("voice.level")} />
|
||||
<span class="voice-title">{voice.state.title}</span>
|
||||
<Show when={voice.state.status === "dropped"}>
|
||||
<Button size="small" variant="outline" onClick={() => void voice.rejoin()}>
|
||||
{language.t("voice.rejoin")}
|
||||
</Button>
|
||||
</Show>
|
||||
<Button
|
||||
size="small"
|
||||
variant="ghost"
|
||||
@@ -91,29 +186,61 @@ function VoiceCapsule() {
|
||||
>
|
||||
{language.t(voice.state.muted ? "voice.unmute" : "voice.mute")}
|
||||
</Button>
|
||||
<meter class="voice-level" min={0} max={1} value={voice.state.level} aria-label={language.t("voice.level")} />
|
||||
<span class="voice-title">{voice.state.title}</span>
|
||||
<Show when={voice.state.status === "dropped"}>
|
||||
<Button size="small" variant="outline" onClick={() => void voice.rejoin()}>
|
||||
{language.t("voice.rejoin")}
|
||||
</Button>
|
||||
</Show>
|
||||
<Button size="small" variant="ghost" onClick={() => void voice.end()}>
|
||||
{language.t("voice.end")}
|
||||
</Button>
|
||||
<IconButton
|
||||
type="button"
|
||||
size="small"
|
||||
variant="ghost-muted"
|
||||
icon={<Icon name={position.expanded ? "chevron-down" : "chevron-up"} />}
|
||||
aria-expanded={position.expanded}
|
||||
aria-label={language.t(position.expanded ? "voice.captions.hide" : "voice.captions.show")}
|
||||
onClick={() => setPosition("expanded", !position.expanded)}
|
||||
/>
|
||||
</div>
|
||||
<Show when={voice.state.status === "connecting"}>
|
||||
<p role="status">{language.t("voice.connecting")}</p>
|
||||
<p class="voice-status" role="status">
|
||||
<Loader class="size-3.5 shrink-0" />
|
||||
{language.t("voice.connecting")}
|
||||
</p>
|
||||
</Show>
|
||||
<Show when={voice.state.status === "dropped"}>
|
||||
<p role="alert">{language.t(`voice.failure.${voice.state.failure ?? "dropped"}`)}</p>
|
||||
</Show>
|
||||
<Show when={voice.state.caption}>
|
||||
<p class="voice-caption">
|
||||
<span>{language.t(voice.state.speaker === "user" ? "voice.you" : "voice.assistant")}</span>{" "}
|
||||
{voice.state.caption}
|
||||
<p class="voice-status" role="alert">
|
||||
{language.t(`voice.failure.${voice.state.failure ?? "dropped"}`)}
|
||||
</p>
|
||||
</Show>
|
||||
<Show
|
||||
when={position.expanded}
|
||||
fallback={
|
||||
<Show when={voice.state.caption}>
|
||||
<p class="voice-caption voice-latest">
|
||||
<span>{speaker(voice.state.speaker)}</span> {voice.state.caption}
|
||||
</p>
|
||||
</Show>
|
||||
}
|
||||
>
|
||||
<div class="voice-captions" data-scrollable>
|
||||
<For each={voice.state.captions}>
|
||||
{(caption) => (
|
||||
<p class="voice-caption">
|
||||
<span>{speaker(caption.speaker)}</span> {caption.text}
|
||||
</p>
|
||||
)}
|
||||
</For>
|
||||
</div>
|
||||
</Show>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
// A press on a scroll container's own scrollbar lands outside its client box; it scrolls rather than drags.
|
||||
function onScrollbar(event: MouseEvent) {
|
||||
const target = event.target
|
||||
|
||||
return (
|
||||
target instanceof HTMLElement &&
|
||||
target.hasAttribute("data-scrollable") &&
|
||||
(event.offsetX >= target.clientWidth || event.offsetY >= target.clientHeight)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -15,14 +15,19 @@ export const { provider: VoiceProvider, use: useVoice } = createSimpleContext({
|
||||
const platform = usePlatform()
|
||||
const navigate = useNavigate()
|
||||
|
||||
const [state, setState] = createStore<CallState & { server?: ServerConnection.Key; composers: number }>({
|
||||
const [state, setState] = createStore<
|
||||
CallState & {
|
||||
server?: ServerConnection.Key
|
||||
captions: { utterance: number; speaker: CallState["speaker"]; text: string }[]
|
||||
}
|
||||
>({
|
||||
status: "idle",
|
||||
muted: false,
|
||||
level: 0,
|
||||
caption: "",
|
||||
speaker: "assistant",
|
||||
title: "",
|
||||
composers: 0,
|
||||
captions: [],
|
||||
})
|
||||
|
||||
const current: { call?: ReturnType<typeof createVoiceCall>; generation: number } = { generation: 0 }
|
||||
@@ -34,7 +39,7 @@ export const { provider: VoiceProvider, use: useVoice } = createSimpleContext({
|
||||
await call?.end()
|
||||
|
||||
if (current.generation === generation)
|
||||
setState({ status: "idle", server: undefined, caption: "", level: 0, muted: false })
|
||||
setState({ status: "idle", server: undefined, caption: "", captions: [], level: 0, muted: false })
|
||||
}
|
||||
|
||||
const unload = () => {
|
||||
@@ -49,14 +54,22 @@ export const { provider: VoiceProvider, use: useVoice } = createSimpleContext({
|
||||
void end()
|
||||
})
|
||||
|
||||
// The call reports each utterance as it grows: its line is extended in place, and the next utterance starts a
|
||||
// new one.
|
||||
const caption = (change: Partial<CallState>) => {
|
||||
const text = change.caption
|
||||
const utterance = change.utterance
|
||||
|
||||
if (!text || utterance === undefined) return
|
||||
const last = state.captions.length - 1
|
||||
|
||||
if (state.captions[last]?.utterance === utterance) return setState("captions", last, "text", text)
|
||||
setState("captions", (list) => [...list, { utterance, speaker: state.speaker, text }].slice(-4))
|
||||
}
|
||||
|
||||
return {
|
||||
state,
|
||||
end,
|
||||
mountComposer() {
|
||||
setState("composers", (count) => count + 1)
|
||||
|
||||
return () => setState("composers", (count) => count - 1)
|
||||
},
|
||||
mute: () => current.call?.mute(),
|
||||
rejoin: () => current.call?.join(),
|
||||
view(server: ServerConnection.Key, session: SessionInfo) {
|
||||
@@ -65,7 +78,14 @@ export const { provider: VoiceProvider, use: useVoice } = createSimpleContext({
|
||||
async start(server: ServerConnection.Key, ctx: ServerCtx, session: SessionInfo) {
|
||||
if (state.status !== "idle") return
|
||||
const generation = ++current.generation
|
||||
setState({ status: "connecting", server, title: session.title ?? session.id, failure: undefined })
|
||||
setState({
|
||||
status: "connecting",
|
||||
server,
|
||||
title: session.title ?? session.id,
|
||||
failure: undefined,
|
||||
caption: "",
|
||||
captions: [],
|
||||
})
|
||||
await import("./call")
|
||||
.then(async ({ createVoiceCall }) => {
|
||||
if (current.generation !== generation) return
|
||||
@@ -75,7 +95,9 @@ export const { provider: VoiceProvider, use: useVoice } = createSimpleContext({
|
||||
platform,
|
||||
open: (next) => navigate(sessionHref(server, next.id)),
|
||||
change: (change) => {
|
||||
if (current.generation === generation) setState(change)
|
||||
if (current.generation !== generation) return
|
||||
setState(change)
|
||||
caption(change)
|
||||
},
|
||||
})
|
||||
await current.call.join()
|
||||
|
||||
@@ -60,4 +60,9 @@ export class TranscriptTail {
|
||||
.join("\n")
|
||||
.slice(-2000)
|
||||
}
|
||||
|
||||
/** The newest utterance; it changes only when `append` starts a new one. */
|
||||
get latest() {
|
||||
return this.entries.at(-1)?.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,48 +3,82 @@
|
||||
justify-content: flex-end;
|
||||
padding-block-start: 4px;
|
||||
}
|
||||
.voice-overlay {
|
||||
/* Home is the bottom-right corner, above the composer; a drag offsets it with a transform. */
|
||||
.voice-panel {
|
||||
position: fixed;
|
||||
inset-inline: 16px;
|
||||
bottom: 16px;
|
||||
max-width: 640px;
|
||||
margin-inline: auto;
|
||||
right: 16px;
|
||||
bottom: 168px;
|
||||
z-index: 50;
|
||||
}
|
||||
.voice-capsule {
|
||||
border: 1px solid var(--border-weak-base);
|
||||
border-radius: 24px;
|
||||
padding: 12px 16px;
|
||||
background: var(--surface-base);
|
||||
width: min(340px, calc(100vw - 32px));
|
||||
padding: 6px 6px 6px 12px;
|
||||
border-radius: 10px;
|
||||
background: var(--v2-background-bg-layer-01);
|
||||
box-shadow: var(--v2-elevation-floating);
|
||||
font-size: 13px;
|
||||
line-height: var(--line-height-base);
|
||||
line-height: var(--line-height-compact);
|
||||
cursor: grab;
|
||||
touch-action: none;
|
||||
user-select: none;
|
||||
}
|
||||
.voice-panel:active {
|
||||
cursor: grabbing;
|
||||
}
|
||||
.voice-controls {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
min-height: 32px;
|
||||
gap: 4px;
|
||||
min-height: 28px;
|
||||
}
|
||||
.voice-title {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
margin-inline-start: 4px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
color: var(--v2-text-text-base);
|
||||
}
|
||||
.voice-level {
|
||||
width: 36px;
|
||||
height: 16px;
|
||||
width: 32px;
|
||||
height: 4px;
|
||||
flex-shrink: 0;
|
||||
appearance: none;
|
||||
border: 0;
|
||||
border-radius: 2px;
|
||||
overflow: hidden;
|
||||
background: var(--v2-background-bg-layer-03);
|
||||
}
|
||||
.voice-capsule p {
|
||||
margin-block-start: 8px;
|
||||
.voice-level::-webkit-meter-bar {
|
||||
border: 0;
|
||||
background: var(--v2-background-bg-layer-03);
|
||||
}
|
||||
.voice-level::-webkit-meter-optimum-value {
|
||||
background: var(--v2-icon-icon-accent);
|
||||
}
|
||||
.voice-status,
|
||||
.voice-caption {
|
||||
max-height: 100px;
|
||||
overflow: auto;
|
||||
margin-block: 4px 2px;
|
||||
padding-inline-end: 6px;
|
||||
overflow-wrap: anywhere;
|
||||
color: var(--v2-text-text-base);
|
||||
}
|
||||
.voice-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
}
|
||||
.voice-latest {
|
||||
display: -webkit-box;
|
||||
-webkit-box-orient: vertical;
|
||||
-webkit-line-clamp: 2;
|
||||
overflow: hidden;
|
||||
}
|
||||
.voice-captions {
|
||||
max-height: 160px;
|
||||
overflow-y: auto;
|
||||
cursor: auto;
|
||||
touch-action: pan-y;
|
||||
}
|
||||
.voice-caption span {
|
||||
color: var(--text-weak);
|
||||
color: var(--v2-text-text-faint);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user