reifyui 0.5.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -3
- package/dist/{chunk-2LYV4PIX.js → chunk-NTIWFHR4.js} +26 -2
- package/dist/chunk-NTIWFHR4.js.map +1 -0
- package/dist/harness.cjs +72 -21
- package/dist/harness.cjs.map +1 -1
- package/dist/harness.d.ts +23 -0
- package/dist/harness.js +52 -23
- package/dist/harness.js.map +1 -1
- package/dist/index.cjs +1126 -296
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +260 -1
- package/dist/index.js +1101 -293
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Card.jsx +49 -0
- package/src/components/Carousel.jsx +80 -0
- package/src/components/ChatPanel.jsx +461 -0
- package/src/components/Chip.jsx +61 -0
- package/src/components/Composer.jsx +20 -1
- package/src/components/Dialog.jsx +5 -10
- package/src/components/Modal.jsx +75 -0
- package/src/components/Popover.jsx +86 -0
- package/src/components/SearchField.jsx +30 -0
- package/src/components/icons.jsx +9 -0
- package/src/components/overlay.js +63 -0
- package/src/format.js +10 -0
- package/src/harness/attach.js +63 -0
- package/src/harness/index.d.ts +23 -0
- package/src/harness/index.js +6 -18
- package/src/hooks/useTypewriter.js +53 -0
- package/src/index.d.ts +260 -1
- package/src/index.js +27 -1
- package/src/input/dictate.js +60 -0
- package/src/state/turns.js +30 -0
- package/styles/chat.css +131 -1
- package/styles/chip.css +53 -0
- package/styles/dialog.css +42 -37
- package/styles/library.css +205 -0
- package/styles/preview.css +73 -0
- package/styles/sheet.css +16 -14
- package/styles/themes/dark.css +20 -0
- package/styles/themes/light.css +23 -0
- package/dist/chunk-2LYV4PIX.js.map +0 -1
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
// renderSend() replaces the default send button entirely
|
|
10
10
|
// classNames { root, input, row } — each REPLACES the default class when provided,
|
|
11
11
|
// so a product can restyle wholesale without fighting the package CSS
|
|
12
|
+
//
|
|
13
|
+
// NOTE: autoGrow and a CSS-driven min-height on the textarea are mutually exclusive — autoGrow
|
|
14
|
+
// writes an explicit height on every keystroke, which the min-height then cannot raise. A fixed
|
|
15
|
+
// multi-line box wants autoGrow={false} + rows.
|
|
12
16
|
import React, { useEffect, useRef } from 'react';
|
|
13
17
|
import { IcSend } from './icons.jsx';
|
|
14
18
|
|
|
@@ -25,6 +29,11 @@ export function Composer(props) {
|
|
|
25
29
|
rows = 1,
|
|
26
30
|
autoGrow = true,
|
|
27
31
|
maxRows = 15,
|
|
32
|
+
autoFocus,
|
|
33
|
+
// The composer on a landing page often has an ANIMATING placeholder (a typewriter cycling
|
|
34
|
+
// through example prompts), and a placeholder is the textarea's only accessible name unless
|
|
35
|
+
// one is given — so the field announces a different name every few seconds.
|
|
36
|
+
inputAriaLabel,
|
|
28
37
|
attachments,
|
|
29
38
|
accessoriesLeft,
|
|
30
39
|
accessoriesRight,
|
|
@@ -57,8 +66,18 @@ export function Composer(props) {
|
|
|
57
66
|
value={value}
|
|
58
67
|
disabled={disabled}
|
|
59
68
|
rows={rows}
|
|
69
|
+
autoFocus={autoFocus}
|
|
70
|
+
aria-label={inputAriaLabel}
|
|
60
71
|
onChange={(e) => onChange(e.target.value)}
|
|
61
|
-
|
|
72
|
+
// Enter obeys sendDisabled, exactly as the send button does: two ways to trigger one
|
|
73
|
+
// action must not disagree about whether it is available. (It still swallows the key —
|
|
74
|
+
// a newline is not what Enter means in this box.) sendDisabled defaults to undefined,
|
|
75
|
+
// so for a caller that never set it nothing changes.
|
|
76
|
+
onKeyDown={(e) => {
|
|
77
|
+
if (e.key !== 'Enter' || e.shiftKey) return;
|
|
78
|
+
e.preventDefault();
|
|
79
|
+
if (!sendDisabled) onSend();
|
|
80
|
+
}}
|
|
62
81
|
/>
|
|
63
82
|
<div className={classNames.row ?? 'wbx-composer-row'}>
|
|
64
83
|
{accessoriesLeft}
|
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
useId, useMemo, useRef, useState,
|
|
31
31
|
} from 'react';
|
|
32
32
|
import { createPortal } from 'react-dom';
|
|
33
|
+
import { useOverlayEscape } from './overlay.js';
|
|
33
34
|
|
|
34
35
|
const DialogCtx = createContext(null);
|
|
35
36
|
|
|
@@ -102,16 +103,10 @@ function DialogShell({ dialog, isTop, onClose, t }) {
|
|
|
102
103
|
const ref = useRef(null);
|
|
103
104
|
const titleId = useId();
|
|
104
105
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
e.preventDefault();
|
|
110
|
-
onClose(id, cancelValue(spec.kind));
|
|
111
|
-
};
|
|
112
|
-
document.addEventListener('keydown', onKey);
|
|
113
|
-
return () => document.removeEventListener('keydown', onKey);
|
|
114
|
-
}, [id, isTop, onClose, spec.kind]);
|
|
106
|
+
// Escape goes through the package's shared overlay stack rather than a listener of our own, so
|
|
107
|
+
// a Modal or Popover opened ON TOP of this dialog answers the key and this one does not.
|
|
108
|
+
const escape = useCallback(() => onClose(id, cancelValue(spec.kind)), [id, onClose, spec.kind]);
|
|
109
|
+
useOverlayEscape(isTop, escape);
|
|
115
110
|
|
|
116
111
|
// Focus the field if there is one, else the primary action — so Enter does the obvious thing.
|
|
117
112
|
useEffect(() => {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Modal — a dialog whose body is yours: a preview, a big form, a comparison.
|
|
2
|
+
//
|
|
3
|
+
// useDialog() answers a QUESTION and resolves to a value (confirm/prompt/alert). This is the
|
|
4
|
+
// other half: a surface with arbitrary content that the caller opens and closes itself. Products
|
|
5
|
+
// were re-implementing it per feature, each with its own backdrop mousedown handler and its own
|
|
6
|
+
// document keydown listener.
|
|
7
|
+
//
|
|
8
|
+
// It shares the dialog host's Escape ordering through the package's overlay stack (components/
|
|
9
|
+
// overlay.js), so a Modal opened on top of a confirm takes the key and the confirm does not, and
|
|
10
|
+
// both sit at the same z-index layer (--uic-z-overlay). It does NOT render inside DialogHost's
|
|
11
|
+
// portal: that root is created from the host's private state and only exists while a dialog is
|
|
12
|
+
// open, so there is nothing to render into and no API to reach it with. One stack, two roots, is
|
|
13
|
+
// the honest arrangement — and it means Modal works in an app that never mounted DialogHost.
|
|
14
|
+
import React, { useCallback, useEffect, useId } from 'react';
|
|
15
|
+
import { createPortal } from 'react-dom';
|
|
16
|
+
import { IcX } from './icons.jsx';
|
|
17
|
+
import { useOverlayEscape } from './overlay.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* open render it
|
|
21
|
+
* onClose Escape, the ✕, or a click on the backdrop itself
|
|
22
|
+
* title the heading (string or node); also names the dialog for assistive tech
|
|
23
|
+
* description one quiet line under the title
|
|
24
|
+
* actions buttons in the header, before the ✕ (e.g. "Use this template")
|
|
25
|
+
* size 'sm' | 'md' | 'lg' | 'full' — or pass `width` for an exact px/any CSS width
|
|
26
|
+
* labelledBy id of your own heading, when `title` is not where the name lives
|
|
27
|
+
*/
|
|
28
|
+
export function Modal(props) {
|
|
29
|
+
const { open, onClose, title, description, actions, size = 'md', width,
|
|
30
|
+
labelledBy, children, classNames = {} } = props;
|
|
31
|
+
const titleId = useId();
|
|
32
|
+
|
|
33
|
+
const escape = useCallback(() => onClose?.(), [onClose]);
|
|
34
|
+
useOverlayEscape(open, escape);
|
|
35
|
+
|
|
36
|
+
// A modal that leaves the page scrolling behind it drifts under the reader's fingers on a
|
|
37
|
+
// touch device and loses their place in the list they opened it from.
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (!open || typeof document === 'undefined') return undefined;
|
|
40
|
+
const prev = document.body.style.overflow;
|
|
41
|
+
document.body.style.overflow = 'hidden';
|
|
42
|
+
return () => { document.body.style.overflow = prev; };
|
|
43
|
+
}, [open]);
|
|
44
|
+
|
|
45
|
+
if (!open || typeof document === 'undefined') return null;
|
|
46
|
+
// mousedown, not click: a press that STARTED inside the card and ended on the backdrop (a drag
|
|
47
|
+
// that overshot, a text selection) would otherwise close it out from under the person.
|
|
48
|
+
const onBackdrop = (e) => { if (e.target === e.currentTarget) onClose?.(); };
|
|
49
|
+
return createPortal(
|
|
50
|
+
<div className={['uic-modal-backdrop', classNames.backdrop || ''].filter(Boolean).join(' ')}
|
|
51
|
+
onMouseDown={onBackdrop}>
|
|
52
|
+
<div className={['uic-modal', `is-${size}`, classNames.root || ''].filter(Boolean).join(' ')}
|
|
53
|
+
style={width ? { width } : undefined}
|
|
54
|
+
role="dialog" aria-modal="true"
|
|
55
|
+
aria-labelledby={labelledBy || (title ? titleId : undefined)}>
|
|
56
|
+
{(title || actions) ? (
|
|
57
|
+
<div className="uic-modal-h">
|
|
58
|
+
<div className="uic-modal-title">
|
|
59
|
+
{title ? <h3 id={titleId}>{title}</h3> : null}
|
|
60
|
+
{description ? <p>{description}</p> : null}
|
|
61
|
+
</div>
|
|
62
|
+
{actions}
|
|
63
|
+
<button type="button" className="uic-modal-x" aria-label="Close" onClick={onClose}>
|
|
64
|
+
<IcX size={18} />
|
|
65
|
+
</button>
|
|
66
|
+
</div>
|
|
67
|
+
) : null}
|
|
68
|
+
<div className={['uic-modal-b', classNames.body || ''].filter(Boolean).join(' ')}>
|
|
69
|
+
{children}
|
|
70
|
+
</div>
|
|
71
|
+
</div>
|
|
72
|
+
</div>,
|
|
73
|
+
document.body,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Popover — a panel anchored to a control: a menu, a picker, a row's overflow actions.
|
|
2
|
+
//
|
|
3
|
+
// Ten of these were hand-rolled across four apps, each with its own mousedown-outside listener,
|
|
4
|
+
// and exactly ONE of the ten survived a 390px viewport: the other nine positioned themselves with
|
|
5
|
+
// `position: absolute; bottom: 36px` and either ran off the screen or grew taller than it. That
|
|
6
|
+
// one implementation's placement logic is what this component is, so placement is built IN and
|
|
7
|
+
// is not separately exportable — a hook you can skip is how you ship the other nine again.
|
|
8
|
+
//
|
|
9
|
+
// It portals to the body and positions itself `fixed`, so it is never clipped by an ancestor's
|
|
10
|
+
// overflow (the reason the absolute ones lived inside their own scroll containers).
|
|
11
|
+
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
12
|
+
import { createPortal } from 'react-dom';
|
|
13
|
+
import { useOverlayEscape } from './overlay.js';
|
|
14
|
+
|
|
15
|
+
const GAP = 6; // between the anchor and the panel
|
|
16
|
+
const MARGIN = 8; // minimum clearance from the viewport edge
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* open render it
|
|
20
|
+
* anchorRef ref to the control it belongs to; the panel positions against its box
|
|
21
|
+
* onClose Escape, an outside pointerdown, or the caller's own reason
|
|
22
|
+
* width preferred width; clamped to the viewport
|
|
23
|
+
* minHeight never squeeze below this — flip to the other side instead
|
|
24
|
+
* placement 'auto' (default) picks the side with more room; 'above'/'below' force one
|
|
25
|
+
*/
|
|
26
|
+
export function Popover(props) {
|
|
27
|
+
const { open, anchorRef, onClose, width = 280, minHeight = 140, placement = 'auto',
|
|
28
|
+
children, className, label } = props;
|
|
29
|
+
const popRef = useRef(null);
|
|
30
|
+
const [pos, setPos] = useState(null);
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (!open) { setPos(null); return undefined; }
|
|
34
|
+
const place = () => {
|
|
35
|
+
const el = anchorRef?.current;
|
|
36
|
+
if (!el) return;
|
|
37
|
+
const r = el.getBoundingClientRect();
|
|
38
|
+
const vw = window.innerWidth;
|
|
39
|
+
const vh = window.innerHeight;
|
|
40
|
+
const w = Math.min(width, vw - MARGIN * 2);
|
|
41
|
+
const below = vh - r.bottom - GAP - MARGIN;
|
|
42
|
+
const above = r.top - GAP - MARGIN;
|
|
43
|
+
const up = placement === 'above'
|
|
44
|
+
|| (placement === 'auto' && below < minHeight && above > below);
|
|
45
|
+
const maxHeight = Math.max(minHeight, Math.floor(up ? above : below));
|
|
46
|
+
const left = Math.max(MARGIN, Math.min(r.left, vw - w - MARGIN));
|
|
47
|
+
setPos(up
|
|
48
|
+
? { left, bottom: Math.round(vh - r.top + GAP), width: w, maxHeight }
|
|
49
|
+
: { left, top: Math.round(r.bottom + GAP), width: w, maxHeight });
|
|
50
|
+
};
|
|
51
|
+
place();
|
|
52
|
+
window.addEventListener('resize', place);
|
|
53
|
+
// Capture phase: the anchor is usually inside a scrolling pane, and a scroll event on that
|
|
54
|
+
// pane does not bubble to the window.
|
|
55
|
+
window.addEventListener('scroll', place, true);
|
|
56
|
+
return () => {
|
|
57
|
+
window.removeEventListener('resize', place);
|
|
58
|
+
window.removeEventListener('scroll', place, true);
|
|
59
|
+
};
|
|
60
|
+
}, [open, anchorRef, width, minHeight, placement]);
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (!open) return undefined;
|
|
64
|
+
const onDown = (e) => {
|
|
65
|
+
if (popRef.current?.contains(e.target)) return;
|
|
66
|
+
// The anchor closes it through its own onClick; treating the press as "outside" here would
|
|
67
|
+
// close and immediately reopen it.
|
|
68
|
+
if (anchorRef?.current?.contains(e.target)) return;
|
|
69
|
+
onClose?.();
|
|
70
|
+
};
|
|
71
|
+
document.addEventListener('pointerdown', onDown);
|
|
72
|
+
return () => document.removeEventListener('pointerdown', onDown);
|
|
73
|
+
}, [open, anchorRef, onClose]);
|
|
74
|
+
|
|
75
|
+
const escape = useCallback(() => onClose?.(), [onClose]);
|
|
76
|
+
useOverlayEscape(open, escape);
|
|
77
|
+
|
|
78
|
+
if (!open || !pos || typeof document === 'undefined') return null;
|
|
79
|
+
return createPortal(
|
|
80
|
+
<div ref={popRef} className={['uic-pop', className || ''].filter(Boolean).join(' ')}
|
|
81
|
+
style={pos} role="dialog" aria-label={label}>
|
|
82
|
+
{children}
|
|
83
|
+
</div>,
|
|
84
|
+
document.body,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// SearchField — the filter box that sits in a section header.
|
|
2
|
+
//
|
|
3
|
+
// Byte-identical in three apps; the fourth ships the stylesheet for it and no component, so its
|
|
4
|
+
// lists have no search at all. Draws the package's own glyphs, so adopting it does not drag an
|
|
5
|
+
// icon library into a product that had not chosen one.
|
|
6
|
+
import React from 'react';
|
|
7
|
+
import { IcSearch, IcX } from './icons.jsx';
|
|
8
|
+
|
|
9
|
+
/** placeholder doubles as the accessible name — a search box next to a heading rarely has room
|
|
10
|
+
* for a visible label, and "Search templates" is exactly what the label would have said. */
|
|
11
|
+
export function SearchField(props) {
|
|
12
|
+
const { value, onChange, placeholder, clearLabel = 'Clear search', className } = props;
|
|
13
|
+
return (
|
|
14
|
+
<label className={['uic-search', className || ''].filter(Boolean).join(' ')}>
|
|
15
|
+
<span className="uic-search-ic" aria-hidden="true"><IcSearch /></span>
|
|
16
|
+
<input
|
|
17
|
+
type="search"
|
|
18
|
+
value={value}
|
|
19
|
+
placeholder={placeholder}
|
|
20
|
+
aria-label={placeholder}
|
|
21
|
+
onChange={(e) => onChange(e.target.value)}
|
|
22
|
+
/>
|
|
23
|
+
{value ? (
|
|
24
|
+
<button type="button" className="uic-search-x" aria-label={clearLabel} onClick={() => onChange('')}>
|
|
25
|
+
<IcX size={12} />
|
|
26
|
+
</button>
|
|
27
|
+
) : null}
|
|
28
|
+
</label>
|
|
29
|
+
);
|
|
30
|
+
}
|
package/src/components/icons.jsx
CHANGED
|
@@ -31,3 +31,12 @@ export const IcSpawn = () => <Svg s={17}><circle cx="6" cy="6" r="3" /><circle c
|
|
|
31
31
|
export const IcCheck = () => <Svg s={17}><circle cx="12" cy="12" r="9" /><path d="m9 12 2 2 4-4" /></Svg>;
|
|
32
32
|
export const IcThink = () => <Svg s={16}><path d="M9.5 2a5.5 5.5 0 0 0-3 10.1V15a2 2 0 0 0 2 2h3a2 2 0 0 0 2-2v-2.9A5.5 5.5 0 0 0 9.5 2zM9 20h4M10 22h2" /></Svg>;
|
|
33
33
|
export const IcSend = () => <Svg s={16}><path d="M12 19V5" /><path d="M5 12l7-7 7 7" /></Svg>;
|
|
34
|
+
|
|
35
|
+
// ── chrome glyphs (chat panel, chips, search) ──
|
|
36
|
+
// The package draws its own so a consumer never inherits an icon-library dependency from a
|
|
37
|
+
// component: four apps pull the same four glyphs out of lucide today purely to hand them to a
|
|
38
|
+
// panel that could have drawn them itself.
|
|
39
|
+
export const IcX = ({ size = 14 }) => <Svg s={size}><path d="M18 6 6 18M6 6l12 12" /></Svg>;
|
|
40
|
+
export const IcMic = ({ size = 15 }) => <Svg s={size}><rect x="9" y="2" width="6" height="12" rx="3" /><path d="M5 11a7 7 0 0 0 14 0M12 18v4M8 22h8" /></Svg>;
|
|
41
|
+
export const IcPaperclip = ({ size = 16 }) => <Svg s={size}><path d="M21.4 11.1 12.2 20.3a5.5 5.5 0 0 1-7.8-7.8l9.2-9.2a3.7 3.7 0 0 1 5.2 5.2l-9.2 9.2a1.8 1.8 0 0 1-2.6-2.6l8.5-8.5" /></Svg>;
|
|
42
|
+
export const IcPanelRight = ({ size = 17 }) => <Svg s={size}><rect x="3" y="4" width="18" height="16" rx="2" /><path d="M15 4v16" /></Svg>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// One Escape key, one stack.
|
|
2
|
+
//
|
|
3
|
+
// The package now has three things that float above the page — the dialog host, Modal and
|
|
4
|
+
// Popover — and each of them wanting its own document keydown listener is how Escape ends up
|
|
5
|
+
// closing two of them at once. They register here instead: the LAST one opened owns Escape, and
|
|
6
|
+
// nothing below it hears the key.
|
|
7
|
+
//
|
|
8
|
+
// Deliberately not a React context: a Popover inside a Modal inside a dialog is a legitimate
|
|
9
|
+
// arrangement, and the ordering that matters is the order they OPENED in, which is exactly what
|
|
10
|
+
// an array push/splice records. A context would order them by tree position instead.
|
|
11
|
+
//
|
|
12
|
+
// Internal to the package (not exported from the root): a consumer that wants this behaviour
|
|
13
|
+
// gets it by using the overlay components, not by wiring the stack itself.
|
|
14
|
+
import { useEffect } from 'react';
|
|
15
|
+
|
|
16
|
+
const stack = [];
|
|
17
|
+
let listening = false;
|
|
18
|
+
|
|
19
|
+
function onKey(e) {
|
|
20
|
+
if (e.key !== 'Escape') return;
|
|
21
|
+
const top = stack[stack.length - 1];
|
|
22
|
+
if (!top) return;
|
|
23
|
+
e.preventDefault();
|
|
24
|
+
top.onEscape?.();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function listen() {
|
|
28
|
+
if (listening || typeof document === 'undefined') return;
|
|
29
|
+
document.addEventListener('keydown', onKey);
|
|
30
|
+
listening = true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function unlisten() {
|
|
34
|
+
if (!listening || stack.length) return;
|
|
35
|
+
document.removeEventListener('keydown', onKey);
|
|
36
|
+
listening = false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Push while mounted-and-open. Returns the dispose. `onEscape` is read live, so a handler that
|
|
40
|
+
* closes over changing state does not have to re-register. */
|
|
41
|
+
export function pushOverlay(entry) {
|
|
42
|
+
stack.push(entry);
|
|
43
|
+
listen();
|
|
44
|
+
return () => {
|
|
45
|
+
const i = stack.indexOf(entry);
|
|
46
|
+
if (i >= 0) stack.splice(i, 1);
|
|
47
|
+
unlisten();
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Escape closes THIS overlay only while it is the top-most one.
|
|
53
|
+
* `active` is false for an overlay that is closed, or that is not the top of its own local stack
|
|
54
|
+
* (the dialog host layers several at once and only the last should answer).
|
|
55
|
+
*/
|
|
56
|
+
export function useOverlayEscape(active, onEscape) {
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (!active) return undefined;
|
|
59
|
+
// The entry object is stable for the life of this registration; onEscape is captured with it,
|
|
60
|
+
// and the effect re-runs whenever the caller's handler identity changes.
|
|
61
|
+
return pushOverlay({ onEscape });
|
|
62
|
+
}, [active, onEscape]);
|
|
63
|
+
}
|
package/src/format.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Formatting helpers shared by components that display file metadata.
|
|
2
|
+
|
|
3
|
+
/** A byte count as a person reads it. Null/undefined renders as nothing, not as "0 B" — an
|
|
4
|
+
* unknown size is not a size of zero. */
|
|
5
|
+
export function bytesLabel(n) {
|
|
6
|
+
if (n == null) return '';
|
|
7
|
+
if (n < 1024) return `${n} B`;
|
|
8
|
+
if (n < 1048576) return `${Math.round(n / 1024)} KB`;
|
|
9
|
+
return `${(n / 1048576).toFixed(1)} MB`;
|
|
10
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Files, on their way into a turn.
|
|
2
|
+
//
|
|
3
|
+
// One mechanism for every place a file gets attached — the person's own attachment in the chat
|
|
4
|
+
// panel, and an upstream computed cell's artifact handed to the next one. Both become an
|
|
5
|
+
// `input_file` block on the turn, which the gateway writes into the working directory before the
|
|
6
|
+
// agent starts.
|
|
7
|
+
//
|
|
8
|
+
// This lives with the transport because `input_file` is that transport's block shape, not a
|
|
9
|
+
// general idea about files.
|
|
10
|
+
//
|
|
11
|
+
// Deliberately NOT a workspace PUT: that needs a session which a brand-new document does not have
|
|
12
|
+
// yet, it is refused 409 while a turn is running, and it cannot carry bytes that are not text —
|
|
13
|
+
// one kit shipped `await file.text()` here and corrupted every PDF and image it was given.
|
|
14
|
+
|
|
15
|
+
/** Per-file cap the API enforces. Refuse a bigger file by name rather than truncating it. */
|
|
16
|
+
export const FILE_MAX = 25 * 1024 * 1024;
|
|
17
|
+
|
|
18
|
+
const MIME = {
|
|
19
|
+
md: 'text/markdown', txt: 'text/plain', json: 'application/json', csv: 'text/csv',
|
|
20
|
+
tsv: 'text/tab-separated-values', html: 'text/html', py: 'text/x-python', js: 'text/javascript',
|
|
21
|
+
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', svg: 'image/svg+xml',
|
|
22
|
+
pdf: 'application/pdf', docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
23
|
+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const mimeOf = (name, fallback = 'application/octet-stream') =>
|
|
27
|
+
MIME[String(name).split('.').pop().toLowerCase()] || fallback;
|
|
28
|
+
|
|
29
|
+
/** ArrayBuffer -> data: URL, chunked because a single fromCharCode over a large file overflows
|
|
30
|
+
* the argument stack. */
|
|
31
|
+
export function bufferToDataUrl(buf, filename, type) {
|
|
32
|
+
let bin = '';
|
|
33
|
+
const bytes = new Uint8Array(buf);
|
|
34
|
+
for (let i = 0; i < bytes.length; i += 0x8000) {
|
|
35
|
+
bin += String.fromCharCode.apply(null, bytes.subarray(i, i + 0x8000));
|
|
36
|
+
}
|
|
37
|
+
return `data:${type || mimeOf(filename)};base64,${btoa(bin)}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A picked File -> the staged entry a chat panel hands back to its runTurn.
|
|
41
|
+
*
|
|
42
|
+
* The return shape is ChatPanel's StagedFile ({name, size, payload}) on purpose, so a kit's
|
|
43
|
+
* whole attachment story is `attachments={{ prepare: fileToInputBlock }}` and the panel never
|
|
44
|
+
* learns what a block is. `payload` is the `input_file` block; the caller puts it in `input`.
|
|
45
|
+
*
|
|
46
|
+
* Throws with the file's OWN name if it is too large — the message reaches the person, so it
|
|
47
|
+
* names the file they picked rather than reporting a limit in the abstract. */
|
|
48
|
+
export async function fileToInputBlock(file, { maxBytes = FILE_MAX } = {}) {
|
|
49
|
+
if (file.size > maxBytes) {
|
|
50
|
+
throw new Error(`${file.name} is ${Math.round(file.size / 1048576)} MB`
|
|
51
|
+
+ ` — the limit is ${Math.round(maxBytes / 1048576)} MB.`);
|
|
52
|
+
}
|
|
53
|
+
const buf = await file.arrayBuffer();
|
|
54
|
+
return {
|
|
55
|
+
name: file.name,
|
|
56
|
+
size: file.size,
|
|
57
|
+
payload: {
|
|
58
|
+
type: 'input_file',
|
|
59
|
+
filename: file.name,
|
|
60
|
+
file_data: bufferToDataUrl(buf, file.name, file.type),
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
package/src/harness/index.d.ts
CHANGED
|
@@ -84,5 +84,28 @@ export interface ChatMessage {
|
|
|
84
84
|
blocks?: Array<Record<string, unknown>>;
|
|
85
85
|
status?: string;
|
|
86
86
|
}
|
|
87
|
+
/** Also exported from the package root — same function, one implementation (state/turns.js).
|
|
88
|
+
* Converting history must not require importing this fetch layer, and importing this fetch
|
|
89
|
+
* layer must not mean going somewhere else for the converter. */
|
|
87
90
|
export function turnsToMessages(turns: Turn[]): ChatMessage[];
|
|
88
91
|
export function lastAssistantText(turns: Turn[]): string;
|
|
92
|
+
|
|
93
|
+
// ── files on their way into a turn ──────────────────────────────────────────
|
|
94
|
+
/** Per-file cap the API enforces. A bigger file is refused by name, never truncated. */
|
|
95
|
+
export const FILE_MAX: number;
|
|
96
|
+
/** Content type from a filename's extension, for the handful the products actually send. */
|
|
97
|
+
export function mimeOf(name: string, fallback?: string): string;
|
|
98
|
+
/** ArrayBuffer -> data: URL. Chunked: one fromCharCode over a large file overflows the stack. */
|
|
99
|
+
export function bufferToDataUrl(buffer: ArrayBuffer, filename: string, type?: string): string;
|
|
100
|
+
|
|
101
|
+
/** A picked File -> the `input_file` block a turn takes, wrapped in ChatPanel's StagedFile shape
|
|
102
|
+
* so a whole attachment story is `attachments={{ prepare: fileToInputBlock }}`.
|
|
103
|
+
*
|
|
104
|
+
* Not a workspace PUT: that needs a session a brand-new document does not have, is refused 409
|
|
105
|
+
* while a turn runs, and cannot carry bytes that are not text.
|
|
106
|
+
*
|
|
107
|
+
* Rejects with a message naming the FILE when it is over the cap — the person reads it. */
|
|
108
|
+
export function fileToInputBlock(
|
|
109
|
+
file: File,
|
|
110
|
+
options?: { maxBytes?: number },
|
|
111
|
+
): Promise<{ name: string; size: number; payload: { type: 'input_file'; filename: string; file_data: string } }>;
|
package/src/harness/index.js
CHANGED
|
@@ -236,25 +236,13 @@ export async function streamTurn({ sessionId, harnessId, input, instructions, ha
|
|
|
236
236
|
}
|
|
237
237
|
|
|
238
238
|
// ── replaying history ──────────────────────────────────────────────────────
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
};
|
|
239
|
+
// turnsToMessages now lives in state/turns.js and is re-exported from BOTH entry points: it is a
|
|
240
|
+
// pure shape translation, and ChatPanel (a root export) takes the messages it produces, so a root
|
|
241
|
+
// consumer must be able to convert history without pulling this fetch layer into its bundle.
|
|
242
|
+
export { turnsToMessages } from '../state/turns.js';
|
|
243
243
|
|
|
244
|
-
|
|
245
|
-
export
|
|
246
|
-
const out = [];
|
|
247
|
-
for (const t of turns || []) {
|
|
248
|
-
if (t.user) out.push({ role: 'user', text: t.user });
|
|
249
|
-
const steps = (t.tools || []).map((x) => ({ name: x.name, args: x.arguments, result: x.result }));
|
|
250
|
-
const blocks = [];
|
|
251
|
-
if (steps.length) blocks.push({ kind: 'tools', reasoning: '', steps });
|
|
252
|
-
if (t.assistant) blocks.push({ kind: 'text', text: t.assistant });
|
|
253
|
-
const status = TURN_STATUS[t.status] || 'done';
|
|
254
|
-
if (blocks.length || status !== 'done') out.push({ role: 'assistant', blocks, status });
|
|
255
|
-
}
|
|
256
|
-
return out;
|
|
257
|
-
}
|
|
244
|
+
// Files on their way into a turn — `input_file` is this transport's block shape, so it lives here.
|
|
245
|
+
export { FILE_MAX, mimeOf, bufferToDataUrl, fileToInputBlock } from './attach.js';
|
|
258
246
|
|
|
259
247
|
/** The assistant text of a session's last turn — what a document page shows when there is no
|
|
260
248
|
* document yet and the agent has stopped. The real explanation is usually in there. */
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// useTypewriter — cycle example prompts through a placeholder, typed out character by character.
|
|
2
|
+
//
|
|
3
|
+
// Three near-identical implementations existed; the differences between them were 24ms vs 26ms
|
|
4
|
+
// per character and whether the list was shuffled. Those are not configuration, they are drift,
|
|
5
|
+
// so there are no knobs for them here.
|
|
6
|
+
//
|
|
7
|
+
// The part worth sharing is the one thing all three already got right and nobody should have to
|
|
8
|
+
// re-derive: prefers-reduced-motion turns the animation off and shows the first phrase, because
|
|
9
|
+
// text that rewrites itself forever is exactly what that setting is about.
|
|
10
|
+
import { useEffect, useRef, useState } from 'react';
|
|
11
|
+
|
|
12
|
+
const CHAR_MS = 26; // per character while typing
|
|
13
|
+
const HOLD_MS = 2600; // a finished phrase stays long enough to read
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* phrases the lines to cycle (the product's own copy — it stays in the product)
|
|
17
|
+
* active type only while this is true; a box the person is typing in stops animating
|
|
18
|
+
* returns the text to show right now
|
|
19
|
+
*/
|
|
20
|
+
export function useTypewriter(phrases, { active = true } = {}) {
|
|
21
|
+
const list = phrases && phrases.length ? phrases : [''];
|
|
22
|
+
const [text, setText] = useState(list[0]);
|
|
23
|
+
// Snapshot the list so a caller passing a fresh array literal every render does not restart
|
|
24
|
+
// the animation on every render.
|
|
25
|
+
const listRef = useRef(list);
|
|
26
|
+
listRef.current = list;
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!active) return undefined;
|
|
30
|
+
if (typeof window === 'undefined') return undefined;
|
|
31
|
+
if (window.matchMedia?.('(prefers-reduced-motion: reduce)')?.matches) {
|
|
32
|
+
setText(listRef.current[0]);
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
let i = 0;
|
|
36
|
+
let pos = 0;
|
|
37
|
+
let timer;
|
|
38
|
+
const tick = () => {
|
|
39
|
+
const cur = listRef.current[i % listRef.current.length];
|
|
40
|
+
if (pos <= cur.length) {
|
|
41
|
+
setText(cur.slice(0, pos));
|
|
42
|
+
pos += 1;
|
|
43
|
+
timer = window.setTimeout(tick, CHAR_MS);
|
|
44
|
+
} else {
|
|
45
|
+
timer = window.setTimeout(() => { i += 1; pos = 0; tick(); }, HOLD_MS);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
tick();
|
|
49
|
+
return () => window.clearTimeout(timer);
|
|
50
|
+
}, [active]);
|
|
51
|
+
|
|
52
|
+
return text;
|
|
53
|
+
}
|