Spaces:
Running
Running
File size: 10,704 Bytes
ad1b8ec 64c242d ad1b8ec ff29fba ad1b8ec 5d5f29a ad1b8ec 64c242d ad1b8ec ff29fba ad1b8ec 5d5f29a ad1b8ec 64c242d ad1b8ec 98f89da ad1b8ec 5d5f29a ad1b8ec 98f89da ad1b8ec ff29fba ad1b8ec 5d5f29a ad1b8ec 64c242d 98f89da 64c242d 98f89da 64c242d 98f89da 12b4e8a 64c242d ad1b8ec 64c242d ad1b8ec 5d5f29a ad1b8ec 5d5f29a ad1b8ec 5d5f29a ad1b8ec 5d5f29a ad1b8ec 5d5f29a ad1b8ec 64c242d ad1b8ec ff29fba ad1b8ec 5d5f29a ad1b8ec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | import React, { useEffect, useRef, useState } from 'react';
import type { OutputField as OutputFieldData } from '../types';
import { KEYBOARD_HINT, useDragResize } from '../hooks/useDragResize';
import { useUIF } from './UIFContext';
import { ResizeCorners } from './ResizeCorners';
interface ViewProps {
field: OutputFieldData;
active: boolean;
flash: number;
popped?: boolean;
onHeaderPointerDown?: (e: React.PointerEvent) => void;
/** Keyboard move/resize for the header; makes the header focusable. */
onHeaderKeyDown?: (e: React.KeyboardEvent) => void;
onRename?: (title: string) => void;
/** Send a chat message into this output. Shows a reply box when set. */
onSubmit?: (text: string) => void;
/** Keep a reply in .memory. Shows a β button on replies when set. */
onRemember?: (text: string) => void;
onPopOut?: () => void;
/** Read replies aloud: shows a π toggle when set. */
onSpeak?: () => void;
onHome?: () => void;
/** Fill the screen or shrink back: a β€’ button, and a double-click on the header (the title still renames). */
onMaximize?: () => void;
onClose: () => void;
}
/** Header and message list, shared by the in-page panel and the pop-out window. */
export function OutputView({
field,
active,
flash,
popped,
onHeaderPointerDown,
onHeaderKeyDown,
onRename,
onSubmit,
onRemember,
onPopOut,
onSpeak,
onHome,
onMaximize,
onClose,
}: ViewProps) {
const [kept, setKept] = useState<Record<string, boolean>>({});
const listRef = useRef<HTMLDivElement>(null);
const [editing, setEditing] = useState(false);
const [reply, setReply] = useState('');
const busy = field.messages.some(m => m.pending);
const finishRename = (title: string | null) => {
setEditing(false);
if (title !== null && title.trim() && title.trim() !== field.title) onRename?.(title);
};
const submit = (e: React.FormEvent) => {
e.preventDefault();
if (!reply.trim() || busy) return;
onSubmit?.(reply);
setReply('');
};
const last = field.messages[field.messages.length - 1];
useEffect(() => {
const el = listRef.current;
if (el) el.scrollTop = el.scrollHeight;
}, [field.messages.length, last?.text]);
return (
<>
{flash > 0 && <div key={flash} className="uif-flash" style={{ '--uif-color': field.color } as React.CSSProperties} />}
<div
className={`uif-output-header ${busy ? 'is-busy' : ''}`}
onPointerDown={editing ? undefined : onHeaderPointerDown}
onKeyDown={editing ? undefined : onHeaderKeyDown}
onDoubleClick={e => {
// the title's own double-click renames; anywhere else on the header maximizes or shrinks back
if (!editing && onMaximize && !(e.target as HTMLElement).closest('.uif-output-title, .uif-output-buttons')) onMaximize();
}}
tabIndex={onHeaderKeyDown ? 0 : undefined}
role={onHeaderKeyDown ? 'group' : undefined}
aria-label={onHeaderKeyDown ? `${field.title} output. ${KEYBOARD_HINT}` : undefined}
>
<span className="uif-output-dot" style={{ background: field.color }} />
<span className="uif-output-avatar" aria-hidden="true" />
{editing ? (
<input
className="uif-output-rename"
defaultValue={field.title}
aria-label="Output name"
autoFocus
onFocus={e => e.currentTarget.select()}
onPointerDown={e => e.stopPropagation()}
onKeyDown={e => {
e.stopPropagation();
if (e.key === 'Enter') finishRename(e.currentTarget.value);
if (e.key === 'Escape') finishRename(null);
}}
onBlur={e => finishRename(e.currentTarget.value)}
/>
) : (
<span
className="uif-output-title"
title={onRename ? 'Double-click to rename' : undefined}
onDoubleClick={onRename ? () => setEditing(true) : undefined}
>
{field.title}
</span>
)}
{active && <span className="uif-output-badge">active</span>}
<span className="uif-output-buttons" onPointerDown={e => e.stopPropagation()}>
{onSpeak && (
<button
type="button"
className={field.speak ? 'is-on' : ''}
title={field.speak ? 'Stop reading replies aloud' : 'Read replies aloud'}
aria-pressed={!!field.speak}
onClick={onSpeak}
>
{field.speak ? 'π' : 'π'}
</button>
)}
{onPopOut && (
<button type="button" title="Pop out to a window" onClick={onPopOut}>β§</button>
)}
{onHome && (
<button type="button" title={popped ? 'Call home' : 'Back beside the field'} onClick={onHome}>β</button>
)}
{onMaximize && (
<button type="button" title={field.maximized ? 'Shrink back (or double-click the header, or Esc)' : 'Fill the screen (or double-click the header)'}
aria-pressed={!!field.maximized} onClick={onMaximize}>{field.maximized ? '‑' : '‒'}</button>
)}
<button type="button" title="Close" onClick={onClose}>β</button>
</span>
</div>
<div className="uif-output-body" ref={listRef}>
{field.messages.length === 0 && <div className="uif-output-empty">No messages yet.</div>}
{field.messages.map(m => {
if (m.role === 'system' && !m.text && !m.pending) return null; // a status line the engine cleared
const keep = onRemember && m.role === 'assistant' && !m.pending && !m.undelivered && m.text;
const msg = (
<div key={m.id} className={['uif-msg', `uif-msg-${m.role}`, m.kind && `is-${m.kind}`, m.tone && `is-${m.tone}`, m.undelivered && 'is-undelivered'].filter(Boolean).join(' ')}>
{m.text}
{m.pending && <span className="uif-caret">β</span>}
{m.media && (
<span className="uif-msg-media">
{m.media.kind === 'video'
? <video src={m.media.url} controls playsInline loop className="uif-media" aria-label={m.text || 'video'} />
: <img src={m.media.url} alt={m.text || 'image'} className="uif-media" />}
<a className="uif-media-save" href={m.media.url} download={m.media.name || (m.media.kind === 'video' ? 'video.mp4' : 'image.png')}>β€ save</a>
</span>
)}
{m.silent && field.speak && (
<span className="uif-msg-silent" title="Not read aloud: the engine did not vouch for this reply" aria-label="not read aloud">
{' '}π
</span>
)}
</div>
);
if (!keep) return msg;
// The keep button sits beside the reply, not inside it, so copying the reply copies only the reply.
return (
<div key={m.id} className="uif-msg-row">
{msg}
<button
type="button"
className={`uif-msg-keep ${kept[m.id] ? 'is-kept' : ''}`}
title={kept[m.id] ? 'Kept in .memory' : 'Keep this reply in .memory'}
aria-label="Remember this reply"
disabled={kept[m.id]}
onClick={() => {
onRemember(m.text);
setKept(k => ({ ...k, [m.id]: true }));
}}
>
β
</button>
</div>
);
})}
</div>
{onSubmit && (
<form className="uif-output-reply" onSubmit={submit}>
<input
value={reply}
onChange={e => setReply(e.target.value)}
placeholder={`Reply in ${field.title}β¦`}
aria-label={`Message ${field.title}`}
className="uif-output-reply-input"
/>
<button type="submit" disabled={!reply.trim() || busy} aria-label="Send">
β€
</button>
</form>
)}
</>
);
}
/** A floating, draggable output panel inside the hub page. */
export function OutputField({ field }: { field: OutputFieldData }) {
const { outputs, setRect, flashes, raise, zIndexOf, sendTo, context } = useUIF();
const { position, size, setPosition, setSize, startDrag, startResize, onHandleKeyDown, isDragging } = useDragResize({
initialPosition: { x: field.rect.x, y: field.rect.y },
initialSize: { width: field.rect.width, height: field.rect.height },
minWidth: 220,
minHeight: 120,
onCommit: rect => setRect(field.id, rect),
});
// Follow rect changes made elsewhere (call home restacks the panels).
useEffect(() => {
setPosition({ x: field.rect.x, y: field.rect.y });
setSize({ width: field.rect.width, height: field.rect.height });
}, [field.rect, setPosition, setSize]);
const active = outputs.activeId === field.id;
const max = !!field.maximized;
// Esc shrinks the window that fills the screen back to where it was
useEffect(() => {
if (!max) return;
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape' && outputs.activeId === field.id) outputs.maximize(field.id, false); };
document.addEventListener('keydown', onKey);
return () => document.removeEventListener('keydown', onKey);
}, [max, field.id, outputs]);
return (
<div
className={`uif-output ${active ? 'is-active' : ''} ${isDragging ? 'is-dragging' : ''} ${max ? 'is-maximized' : ''}`}
data-uif-output={field.id}
style={
{
left: max ? 0 : position.x,
top: max ? 0 : position.y,
width: max ? '100vw' : size.width,
height: max ? '100vh' : size.height,
zIndex: zIndexOf(field.id),
'--uif-color': field.color,
} as React.CSSProperties
}
onPointerDownCapture={() => {
outputs.setActive(field.id);
raise(field.id);
}}
>
<OutputView
field={field}
active={active}
flash={flashes[field.id] ?? 0}
onHeaderPointerDown={max ? undefined : startDrag}
onHeaderKeyDown={onHandleKeyDown}
onMaximize={() => outputs.maximize(field.id)}
onRename={title => outputs.rename(field.id, title)}
onSubmit={text => sendTo(text, field.id)}
onRemember={text => void context.remember(text, 'response')}
onPopOut={() => outputs.popOut(field.id)}
onSpeak={() => outputs.setSpeak(field.id, !field.speak)}
onClose={() => outputs.close(field.id)}
/>
{!max && <ResizeCorners onStart={startResize} />}
</div>
);
}
|