@openlegion/platform 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/README.md +1189 -2
- package/dist/body.d.ts +9 -0
- package/dist/body.js +28 -0
- package/dist/chat-history.d.ts +53 -0
- package/dist/chat-history.js +11 -0
- package/dist/client.d.ts +217 -0
- package/dist/client.js +367 -0
- package/dist/core.d.ts +47 -0
- package/dist/core.js +1 -0
- package/dist/crypto.d.ts +25 -0
- package/dist/crypto.js +81 -0
- package/dist/errors.d.ts +111 -0
- package/dist/errors.js +134 -0
- package/dist/events.d.ts +131 -0
- package/dist/events.js +5 -0
- package/dist/generated/api.d.ts +5505 -0
- package/dist/generated/api.js +446 -0
- package/dist/handler.d.ts +201 -0
- package/dist/handler.js +682 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +12 -0
- package/dist/kit-agents.d.ts +95 -0
- package/dist/kit-agents.js +280 -0
- package/dist/kit-codes.d.ts +279 -0
- package/dist/kit-codes.js +44 -0
- package/dist/kit-connections.d.ts +21 -0
- package/dist/kit-connections.js +189 -0
- package/dist/kit-files.d.ts +8 -0
- package/dist/kit-files.js +117 -0
- package/dist/kit-model-keys.d.ts +8 -0
- package/dist/kit-model-keys.js +86 -0
- package/dist/kit-threads.d.ts +3 -0
- package/dist/kit-threads.js +92 -0
- package/dist/kit-tools.d.ts +21 -0
- package/dist/kit-tools.js +47 -0
- package/dist/kit.d.ts +130 -0
- package/dist/kit.js +155 -0
- package/dist/mcp-server.d.ts +27 -0
- package/dist/mcp-server.js +166 -0
- package/dist/next.d.ts +24 -0
- package/dist/next.js +6 -0
- package/dist/ol-signed.d.ts +82 -0
- package/dist/ol-signed.js +115 -0
- package/dist/react/add-server.d.ts +90 -0
- package/dist/react/add-server.js +183 -0
- package/dist/react/agent-chat.d.ts +925 -0
- package/dist/react/agent-chat.js +1917 -0
- package/dist/react/agent-editor.d.ts +133 -0
- package/dist/react/agent-editor.js +222 -0
- package/dist/react/agent-kit.d.ts +160 -0
- package/dist/react/agent-kit.js +279 -0
- package/dist/react/agent-list.d.ts +247 -0
- package/dist/react/agent-list.js +248 -0
- package/dist/react/approval-card.d.ts +272 -0
- package/dist/react/approval-card.js +372 -0
- package/dist/react/artifact-card.d.ts +38 -0
- package/dist/react/artifact-card.js +39 -0
- package/dist/react/avatar.d.ts +21 -0
- package/dist/react/avatar.js +33 -0
- package/dist/react/card-ledger.d.ts +30 -0
- package/dist/react/card-ledger.js +49 -0
- package/dist/react/chat-timing.d.ts +121 -0
- package/dist/react/chat-timing.js +229 -0
- package/dist/react/computer-view.d.ts +309 -0
- package/dist/react/computer-view.js +543 -0
- package/dist/react/computer-viewer.d.ts +175 -0
- package/dist/react/computer-viewer.js +555 -0
- package/dist/react/computer-wire.d.ts +178 -0
- package/dist/react/computer-wire.js +244 -0
- package/dist/react/connect-button.d.ts +145 -0
- package/dist/react/connect-button.js +212 -0
- package/dist/react/connection-request.d.ts +16 -0
- package/dist/react/connection-request.js +18 -0
- package/dist/react/connections-list.d.ts +90 -0
- package/dist/react/connections-list.js +76 -0
- package/dist/react/errors.d.ts +250 -0
- package/dist/react/errors.js +146 -0
- package/dist/react/files.d.ts +41 -0
- package/dist/react/files.js +78 -0
- package/dist/react/index.d.ts +35 -0
- package/dist/react/index.js +26 -0
- package/dist/react/limit-words.d.ts +80 -0
- package/dist/react/limit-words.js +53 -0
- package/dist/react/markdown.d.ts +42 -0
- package/dist/react/markdown.js +56 -0
- package/dist/react/md-parse.d.ts +106 -0
- package/dist/react/md-parse.js +638 -0
- package/dist/react/model-keys-card.d.ts +94 -0
- package/dist/react/model-keys-card.js +123 -0
- package/dist/react/reload.d.ts +211 -0
- package/dist/react/reload.js +339 -0
- package/dist/react/reveal.d.ts +17 -0
- package/dist/react/reveal.js +22 -0
- package/dist/react/schedule-editor.d.ts +107 -0
- package/dist/react/schedule-editor.js +172 -0
- package/dist/react/schedule-time.d.ts +66 -0
- package/dist/react/schedule-time.js +90 -0
- package/dist/react/styles.css +1274 -0
- package/dist/react/transport.d.ts +49 -0
- package/dist/react/transport.js +380 -0
- package/dist/react/use-connections.d.ts +155 -0
- package/dist/react/use-connections.js +231 -0
- package/dist/react/use-model-keys.d.ts +47 -0
- package/dist/react/use-model-keys.js +57 -0
- package/dist/react/use-threads.d.ts +63 -0
- package/dist/react/use-threads.js +73 -0
- package/dist/react/vault-form.d.ts +92 -0
- package/dist/react/vault-form.js +151 -0
- package/dist/schema.d.ts +47 -0
- package/dist/schema.js +16 -0
- package/dist/secrets.d.ts +15 -0
- package/dist/secrets.js +18 -0
- package/dist/stream.d.ts +129 -0
- package/dist/stream.js +192 -0
- package/dist/testing.d.ts +33 -0
- package/dist/testing.js +40 -0
- package/dist/webhooks.d.ts +31 -0
- package/dist/webhooks.js +72 -0
- package/package.json +85 -3
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext, useEffect, useId, useRef, useState } from "react";
|
|
4
|
+
import { createComputerViewer } from "./computer-viewer.js";
|
|
5
|
+
import { computerSessionOf, createTouchGesture, escapeMarkAfter, isPasteChord, keyInputOf, pointOnFrame, utf8Clip, VIEWER_CAPS } from "./computer-wire.js";
|
|
6
|
+
export const DEFAULT_COMPUTER_VIEW_LABELS = {
|
|
7
|
+
region: "Your agent's browser",
|
|
8
|
+
rowUsing: "Your agent is using a browser.",
|
|
9
|
+
rowOwnerControl: "A person has control of your agent's browser.",
|
|
10
|
+
watch: "Watch",
|
|
11
|
+
stopWatching: "Stop watching",
|
|
12
|
+
dismiss: "Dismiss",
|
|
13
|
+
connecting: "Connecting to your agent's browser…",
|
|
14
|
+
working: "Your agent is working in the browser.",
|
|
15
|
+
parked: "Your agent is waiting for your answer to its request.",
|
|
16
|
+
challenge: "Your agent stopped at a security check. It doesn't answer these. To go on, take over, answer it yourself, then give control back.",
|
|
17
|
+
signIn: "Your agent needs someone to sign in. Take over to sign in, then give control back.",
|
|
18
|
+
youHaveControl: "You have control. Your agent waits until you give it back.",
|
|
19
|
+
controlSoon: "Control goes back to your agent in under 2 minutes.",
|
|
20
|
+
someoneElse: "Someone else has control, maybe you in another window.",
|
|
21
|
+
gaveBack: "You gave control back. Your agent is carrying on.",
|
|
22
|
+
thenGoOn: "Then tell your agent to go on.",
|
|
23
|
+
waitingForYou: "Your agent's browser is waiting for you. When you're done, give control back, then tell your agent to go on.",
|
|
24
|
+
gaveBackWaiting: "You gave control back. Tell your agent to go on.",
|
|
25
|
+
expired: "Your time with control ran out, so your agent has it again.",
|
|
26
|
+
expiredWaiting: "Your time with control ran out. Tell your agent to go on.",
|
|
27
|
+
dropped: "The connection dropped, so your agent has control again.",
|
|
28
|
+
droppedWaiting: "The connection dropped. Tell your agent to go on.",
|
|
29
|
+
inputLost: "Your last keys may not have reached the page, and your agent has control again. Take over to check them.",
|
|
30
|
+
inputLostWaiting: "Your last keys may not have reached the page. Take over to check them, then give control back.",
|
|
31
|
+
goOn: "Go on",
|
|
32
|
+
goOnMessage: "Go on.",
|
|
33
|
+
showBrowser: "Show the browser",
|
|
34
|
+
takeRefused: "Taking over isn't available right now.",
|
|
35
|
+
reconnecting: "Reconnecting…",
|
|
36
|
+
unreachable: "Can't reach your agent's browser.",
|
|
37
|
+
refused: "You can't watch this browser right now.",
|
|
38
|
+
endedRunEnded: "Your agent finished with the browser.",
|
|
39
|
+
endedClosed: "Your agent closed the browser.",
|
|
40
|
+
endedDeadline: "The browser reached its time limit and closed.",
|
|
41
|
+
endedParkExpired: "The browser closed while it waited for you.",
|
|
42
|
+
endedStopped: "The browser was stopped.",
|
|
43
|
+
endedFailed: "The browser stopped unexpectedly.",
|
|
44
|
+
endedUnknown: "The browser is closed.",
|
|
45
|
+
takeOver: "Take over",
|
|
46
|
+
giveBack: "Give back",
|
|
47
|
+
givingBack: "Giving back…",
|
|
48
|
+
retry: "Try again",
|
|
49
|
+
keyboard: "Keyboard",
|
|
50
|
+
picture: "Live picture of your agent's browser",
|
|
51
|
+
pictureHeld: "Your agent's browser. You have control.",
|
|
52
|
+
hint: "Keys go to the page. Press Esc, then Tab, to leave.",
|
|
53
|
+
};
|
|
54
|
+
const ENDED = {
|
|
55
|
+
run_ended: "endedRunEnded",
|
|
56
|
+
closed: "endedClosed",
|
|
57
|
+
deadline: "endedDeadline",
|
|
58
|
+
park_expired: "endedParkExpired",
|
|
59
|
+
stopped: "endedStopped",
|
|
60
|
+
failed: "endedFailed",
|
|
61
|
+
};
|
|
62
|
+
export function computerWords(snap, session, l, canTakeOver, canGoOn = false) {
|
|
63
|
+
if (snap?.phase === "ended" || session?.status === "ended")
|
|
64
|
+
return { sentence: l[session?.status === "ended" && session.reason ? ENDED[session.reason] : "endedUnknown"], button: null };
|
|
65
|
+
const check = session?.waitingFor === "challenge" ? l.challenge : session?.waitingFor === "sign_in" ? l.signIn : null;
|
|
66
|
+
const handOff = session?.status === "parked" && session.waitingFor !== undefined;
|
|
67
|
+
const ours = DEFAULT_COMPUTER_VIEW_LABELS;
|
|
68
|
+
const waiting = check !== null && handOff ? `${check} ${l.thenGoOn ?? ours.thenGoOn}` : check;
|
|
69
|
+
const gaveBackWaiting = l.gaveBackWaiting ?? ours.gaveBackWaiting;
|
|
70
|
+
if (snap === null) {
|
|
71
|
+
if (handOff && session.waitingFor === "person")
|
|
72
|
+
return { sentence: l.waitingForYou ?? ours.waitingForYou, button: null };
|
|
73
|
+
return { sentence: waiting ?? (session?.status === "parked" ? l.parked : session?.status === "owner_control" ? l.rowOwnerControl : l.rowUsing), button: null };
|
|
74
|
+
}
|
|
75
|
+
switch (snap.phase) {
|
|
76
|
+
case "refused":
|
|
77
|
+
return { sentence: snap.said ?? l.refused, button: snap.retry ? { kind: "retry" } : null };
|
|
78
|
+
case "unreachable":
|
|
79
|
+
return { sentence: l.unreachable, button: { kind: "retry" } };
|
|
80
|
+
case "reconnecting":
|
|
81
|
+
return { sentence: l.reconnecting, button: null };
|
|
82
|
+
case "connecting":
|
|
83
|
+
return { sentence: l.connecting, button: null };
|
|
84
|
+
case "live":
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
if (snap.control === "you")
|
|
88
|
+
return { sentence: snap.soon ? `${l.youHaveControl} ${l.controlSoon}` : l.youHaveControl, button: { kind: "give_back" } };
|
|
89
|
+
if (snap.control === "other")
|
|
90
|
+
return { sentence: l.someoneElse, button: null };
|
|
91
|
+
const button = canTakeOver && snap.canTake ? { kind: "take", primary: waiting !== null } : null;
|
|
92
|
+
const note = snap.note;
|
|
93
|
+
const goOn = (sentence) => (canGoOn ? { sentence, button: button ? { kind: "take", primary: false } : null, goOn: true } : { sentence, button });
|
|
94
|
+
if (note && note.kind !== "take_refused" && note.lost) {
|
|
95
|
+
return { sentence: handOff ? (l.inputLostWaiting ?? ours.inputLostWaiting) : (l.inputLost ?? ours.inputLost), button: canTakeOver && snap.canTake ? { kind: "take", primary: true } : null };
|
|
96
|
+
}
|
|
97
|
+
if (note?.kind === "gave_back")
|
|
98
|
+
return handOff ? goOn(gaveBackWaiting) : { sentence: l.gaveBack, button };
|
|
99
|
+
if (note?.kind === "expired")
|
|
100
|
+
return handOff ? goOn(l.expiredWaiting ?? ours.expiredWaiting) : { sentence: l.expired, button };
|
|
101
|
+
if (note?.kind === "dropped")
|
|
102
|
+
return handOff ? goOn(l.droppedWaiting ?? ours.droppedWaiting) : { sentence: l.dropped, button };
|
|
103
|
+
if (note?.kind === "take_refused")
|
|
104
|
+
return { sentence: note.said ?? l.takeRefused, button };
|
|
105
|
+
if (handOff && session.waitingFor === "person")
|
|
106
|
+
return goOn(gaveBackWaiting);
|
|
107
|
+
return { sentence: waiting ?? (session?.status === "parked" ? l.parked : l.working), button };
|
|
108
|
+
}
|
|
109
|
+
export function currentNote(snap, seen, session, newestIn) {
|
|
110
|
+
if (snap.note === null)
|
|
111
|
+
return { snap, seen: null };
|
|
112
|
+
const key = [session?.status ?? "", session?.waitingFor ?? "", newestIn ?? ""].join("|");
|
|
113
|
+
const at = seen !== null && seen.note === snap.note ? seen : { note: snap.note, key };
|
|
114
|
+
if (at.over === undefined && at.key === key)
|
|
115
|
+
return { snap, seen: at };
|
|
116
|
+
return { snap: { ...snap, note: null }, seen: at.over ? at : { ...at, over: true } };
|
|
117
|
+
}
|
|
118
|
+
export function computerPlaces(messages) {
|
|
119
|
+
const first = new Map();
|
|
120
|
+
const newest = new Map();
|
|
121
|
+
const latest = new Map();
|
|
122
|
+
for (const m of messages)
|
|
123
|
+
m.parts.forEach((p, i) => {
|
|
124
|
+
if (p.type !== "data-computer")
|
|
125
|
+
return;
|
|
126
|
+
const s = computerSessionOf(p.data);
|
|
127
|
+
if (!s)
|
|
128
|
+
return;
|
|
129
|
+
if (!first.has(s.id))
|
|
130
|
+
first.set(s.id, { m: m.id, i });
|
|
131
|
+
newest.set(s.id, { m: m.id, i });
|
|
132
|
+
latest.set(s.id, s);
|
|
133
|
+
});
|
|
134
|
+
const at = (place, m, i) => place !== undefined && place.m === m && place.i === i;
|
|
135
|
+
return {
|
|
136
|
+
row: (m, i, data) => {
|
|
137
|
+
const s = computerSessionOf(data);
|
|
138
|
+
return s && at(first.get(s.id), m, i) ? (latest.get(s.id) ?? null) : null;
|
|
139
|
+
},
|
|
140
|
+
pointer: (m, i, data) => {
|
|
141
|
+
const s = computerSessionOf(data);
|
|
142
|
+
if (!s || !at(newest.get(s.id), m, i) || first.get(s.id)?.m === m)
|
|
143
|
+
return null;
|
|
144
|
+
const now = latest.get(s.id);
|
|
145
|
+
return now && now.status !== "ended" ? now : null;
|
|
146
|
+
},
|
|
147
|
+
newestIn: (id) => newest.get(id)?.m ?? null,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
export function goOnKey(snap, session) {
|
|
151
|
+
return [session?.id ?? "", session?.status ?? "", session?.waitingFor ?? "", snap.phase, snap.control ?? "", snap.note?.kind ?? ""].join("|");
|
|
152
|
+
}
|
|
153
|
+
export function pictureRatio(last, frame) {
|
|
154
|
+
if (!frame || !(Number.isFinite(frame.w) && Number.isFinite(frame.h) && frame.w > 0 && frame.h > 0))
|
|
155
|
+
return last;
|
|
156
|
+
return last && last.w === frame.w && last.h === frame.h ? last : { w: frame.w, h: frame.h };
|
|
157
|
+
}
|
|
158
|
+
function stateName(snap, session) {
|
|
159
|
+
if (snap?.phase === "ended" || session?.status === "ended")
|
|
160
|
+
return "ended";
|
|
161
|
+
if (snap === null)
|
|
162
|
+
return "closed";
|
|
163
|
+
if (snap.phase !== "live")
|
|
164
|
+
return snap.phase;
|
|
165
|
+
return snap.control === "you" ? "control" : snap.control === "other" ? "other" : "watching";
|
|
166
|
+
}
|
|
167
|
+
const HIDDEN = { position: "absolute", width: "1px", height: "1px", margin: "-1px", padding: 0, border: 0, overflow: "hidden", clipPath: "inset(50%)", whiteSpace: "nowrap" };
|
|
168
|
+
export function ComputerViewView(props) {
|
|
169
|
+
const { snap, session, labels: l } = props;
|
|
170
|
+
const hintId = useId();
|
|
171
|
+
const words = computerWords(snap, session, l, props.canTakeOver, props.onGoOn !== undefined);
|
|
172
|
+
const { sentence, button } = words;
|
|
173
|
+
const goOn = words.goOn === true && props.goOnPressed !== goOnKey(snap, session) ? props.onGoOn : undefined;
|
|
174
|
+
const ratio = props.ratio ? { aspectRatio: `${props.ratio.w} / ${props.ratio.h}` } : undefined;
|
|
175
|
+
const state = stateName(snap, session);
|
|
176
|
+
const held = state === "control";
|
|
177
|
+
const site = snap.host ?? session?.site ?? null;
|
|
178
|
+
const picture = state !== "ended" && state !== "refused";
|
|
179
|
+
const showing = Boolean(props.frameUrl) && snap.phase === "live";
|
|
180
|
+
return (_jsxs("div", { ref: props.rootRef, "data-ol-computer": "", "data-ol-computer-state": state, role: "group", "aria-label": l.region, tabIndex: -1, className: props.className, children: [_jsxs("div", { "data-ol-computer-bar": "", children: [_jsxs("div", { "data-ol-computer-words": "", children: [_jsx("p", { "data-ol-computer-sentence": "", role: "status", children: sentence }), site ? _jsx("p", { "data-ol-computer-site": "", children: site }) : null] }), _jsxs("div", { "data-ol-computer-actions": "", children: [goOn ? (_jsx("button", { ref: props.buttonRef, type: "button", "data-ol-computer-go-on": "", "data-ol-computer-primary": "", onClick: goOn, children: l.goOn ?? DEFAULT_COMPUTER_VIEW_LABELS.goOn })) : null, button?.kind === "take" ? (_jsx("button", { ref: goOn ? undefined : props.buttonRef, type: "button", "data-ol-computer-take": "", ...(button.primary ? { "data-ol-computer-primary": "" } : {}), "aria-disabled": snap.taking ? true : undefined, onClick: () => !snap.taking && props.onTake(), children: l.takeOver })) : button?.kind === "give_back" ? (_jsx("button", { ref: props.buttonRef, type: "button", "data-ol-computer-give-back": "", "data-ol-computer-primary": "", "aria-disabled": snap.taking ? true : undefined, "aria-busy": snap.taking ? true : undefined, onClick: () => !snap.taking && props.onGiveBack(), children: snap.taking ? (l.givingBack ?? DEFAULT_COMPUTER_VIEW_LABELS.givingBack) : l.giveBack })) : button?.kind === "retry" ? (_jsx("button", { ref: props.buttonRef, type: "button", "data-ol-computer-retry": "", onClick: props.onRetry, children: l.retry })) : null, held && props.onKeyboard ? (_jsx("button", { type: "button", "data-ol-computer-keyboard": "", "data-ol-button": "secondary", onClick: props.onKeyboard, children: l.keyboard })) : null, props.onClose && !held && state !== "ended" ? (_jsx("button", { type: "button", "data-ol-computer-close": "", "data-ol-button": "secondary", onClick: props.onClose, children: state === "unreachable" || state === "refused" ? (l.dismiss ?? DEFAULT_COMPUTER_VIEW_LABELS.dismiss) : l.stopWatching })) : null] })] }), picture ? (held ? (_jsx("div", { ref: props.pictureRef, "data-ol-computer-picture": "", style: ratio, tabIndex: 0, role: "application", "aria-label": l.pictureHeld, "aria-describedby": hintId, children: props.frameUrl ? _jsx("img", { src: props.frameUrl, alt: "", draggable: false }) : null })) : (_jsx("div", { ref: props.pictureRef, "data-ol-computer-picture": "", style: ratio, ...(showing ? { role: "img", "aria-label": l.picture } : { "aria-hidden": true }), children: showing && props.frameUrl ? _jsx("img", { src: props.frameUrl, alt: "", draggable: false }) : null }))) : null, held ? (_jsx("p", { id: hintId, "data-ol-computer-hint": "", children: l.hint })) : null, held ? props.keys : null] }));
|
|
181
|
+
}
|
|
182
|
+
const CONNECTING = { phase: "connecting", host: null, control: null, canTake: false, taking: false, note: null, soon: false, frame: null, said: null, retry: false };
|
|
183
|
+
const MOVE_MS = 33;
|
|
184
|
+
export function ComputerView(props) {
|
|
185
|
+
const l = { ...DEFAULT_COMPUTER_VIEW_LABELS, ...props.labels };
|
|
186
|
+
const [seenSnap, setSnap] = useState(CONNECTING);
|
|
187
|
+
const noteSeen = useRef(null);
|
|
188
|
+
const noted = currentNote(seenSnap, noteSeen.current, props.session ?? null, props.partAt);
|
|
189
|
+
noteSeen.current = noted.seen;
|
|
190
|
+
const snap = noted.snap;
|
|
191
|
+
const ratio = useRef(null);
|
|
192
|
+
ratio.current = pictureRatio(ratio.current, snap.frame);
|
|
193
|
+
const [goOnPressed, setGoOnPressed] = useState(null);
|
|
194
|
+
const here = goOnKey(snap, props.session ?? null);
|
|
195
|
+
if (goOnPressed !== null && goOnPressed !== here)
|
|
196
|
+
setGoOnPressed(null);
|
|
197
|
+
const viewer = useRef(null);
|
|
198
|
+
const fetchViewer = useRef(props.fetchViewer);
|
|
199
|
+
fetchViewer.current = props.fetchViewer;
|
|
200
|
+
const sessionId = useRef(props.session?.id ?? null);
|
|
201
|
+
sessionId.current = props.session?.id ?? null;
|
|
202
|
+
const onTiming = useRef(props.onTiming);
|
|
203
|
+
onTiming.current = props.onTiming;
|
|
204
|
+
const kernelFetch = useRef(props.fetch);
|
|
205
|
+
kernelFetch.current = props.fetch;
|
|
206
|
+
const root = useRef(null);
|
|
207
|
+
const button = useRef(null);
|
|
208
|
+
const picture = useRef(null);
|
|
209
|
+
const keys = useRef(null);
|
|
210
|
+
const onControlChange = useRef(props.onControlChange);
|
|
211
|
+
onControlChange.current = props.onControlChange;
|
|
212
|
+
const ended = props.session?.status === "ended";
|
|
213
|
+
useEffect(() => {
|
|
214
|
+
const v = createComputerViewer({ fetchViewer: (r) => fetchViewer.current({ ...r, session: sessionId.current }), fetch: (u, i) => (kernelFetch.current ?? globalThis.fetch)(u, i), onChange: setSnap, onTiming: (step, at) => onTiming.current?.(step, at) });
|
|
215
|
+
viewer.current = v;
|
|
216
|
+
v.start();
|
|
217
|
+
return () => {
|
|
218
|
+
v.stop();
|
|
219
|
+
viewer.current = null;
|
|
220
|
+
};
|
|
221
|
+
}, []);
|
|
222
|
+
useEffect(() => {
|
|
223
|
+
if (ended)
|
|
224
|
+
viewer.current?.stop();
|
|
225
|
+
}, [ended]);
|
|
226
|
+
const autoFocus = useRef(props.autoFocus === true);
|
|
227
|
+
useEffect(() => {
|
|
228
|
+
if (autoFocus.current)
|
|
229
|
+
root.current?.focus({ preventScroll: true });
|
|
230
|
+
}, []);
|
|
231
|
+
const reveal = props.reveal ?? 0;
|
|
232
|
+
const revealing = useRef(false);
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
const r = root.current;
|
|
235
|
+
if (!reveal || !r)
|
|
236
|
+
return;
|
|
237
|
+
r.scrollIntoView?.({ block: "center" });
|
|
238
|
+
(button.current ?? r).focus({ preventScroll: true });
|
|
239
|
+
revealing.current = button.current === null;
|
|
240
|
+
}, [reveal]);
|
|
241
|
+
useEffect(() => {
|
|
242
|
+
if (!revealing.current)
|
|
243
|
+
return;
|
|
244
|
+
if (document.activeElement !== root.current)
|
|
245
|
+
revealing.current = false;
|
|
246
|
+
else if (button.current) {
|
|
247
|
+
button.current.focus({ preventScroll: true });
|
|
248
|
+
revealing.current = false;
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
const [frameUrl, setFrameUrl] = useState(null);
|
|
252
|
+
const shown = useRef(null);
|
|
253
|
+
const show = (url) => {
|
|
254
|
+
const old = shown.current;
|
|
255
|
+
if (url && !old) {
|
|
256
|
+
try {
|
|
257
|
+
onTiming.current?.("first_picture", Date.now());
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
shown.current = url;
|
|
263
|
+
setFrameUrl(url);
|
|
264
|
+
if (old && old !== url)
|
|
265
|
+
URL.revokeObjectURL(old);
|
|
266
|
+
};
|
|
267
|
+
useEffect(() => {
|
|
268
|
+
const blob = snap.frame?.blob;
|
|
269
|
+
if (!blob)
|
|
270
|
+
return show(null);
|
|
271
|
+
const url = URL.createObjectURL(blob);
|
|
272
|
+
let current = true;
|
|
273
|
+
const img = new Image();
|
|
274
|
+
img.src = url;
|
|
275
|
+
img.decode().then(() => (current ? show(url) : URL.revokeObjectURL(url)), () => URL.revokeObjectURL(url));
|
|
276
|
+
return () => {
|
|
277
|
+
current = false;
|
|
278
|
+
};
|
|
279
|
+
}, [snap.frame]);
|
|
280
|
+
useEffect(() => () => show(null), []);
|
|
281
|
+
const wasHeld = useRef(false);
|
|
282
|
+
const held = snap.phase === "live" && snap.control === "you" && !ended;
|
|
283
|
+
useEffect(() => {
|
|
284
|
+
if (held === wasHeld.current)
|
|
285
|
+
return;
|
|
286
|
+
wasHeld.current = held;
|
|
287
|
+
onControlChange.current?.(held);
|
|
288
|
+
if (held)
|
|
289
|
+
picture.current?.focus({ preventScroll: true });
|
|
290
|
+
else if (root.current && (document.activeElement === null || document.activeElement === document.body || root.current.contains(document.activeElement)))
|
|
291
|
+
(button.current ?? root.current).focus({ preventScroll: true });
|
|
292
|
+
}, [held]);
|
|
293
|
+
const frame = snap.frame ? { w: snap.frame.w, h: snap.frame.h } : null;
|
|
294
|
+
const frameRef = useRef(frame);
|
|
295
|
+
frameRef.current = frame;
|
|
296
|
+
useEffect(() => {
|
|
297
|
+
const el = picture.current;
|
|
298
|
+
const v = viewer.current;
|
|
299
|
+
if (!held || !el || !v)
|
|
300
|
+
return;
|
|
301
|
+
const send = (events) => v.send(events);
|
|
302
|
+
const at = (e) => (frameRef.current ? pointOnFrame(el.getBoundingClientRect(), frameRef.current, e.clientX, e.clientY) : null);
|
|
303
|
+
const BUTTONS = ["left", "middle", "right"];
|
|
304
|
+
let move = null;
|
|
305
|
+
let pressed = null;
|
|
306
|
+
let moveTimer = null;
|
|
307
|
+
const flushMove = () => {
|
|
308
|
+
moveTimer = null;
|
|
309
|
+
if (move)
|
|
310
|
+
send([{ type: "mouse", action: "move", ...move }]);
|
|
311
|
+
move = null;
|
|
312
|
+
};
|
|
313
|
+
const touch = createTouchGesture();
|
|
314
|
+
let wheelTimer = null;
|
|
315
|
+
const flushWheel = () => {
|
|
316
|
+
wheelTimer = null;
|
|
317
|
+
const w = touch.flush();
|
|
318
|
+
if (w)
|
|
319
|
+
send([w]);
|
|
320
|
+
};
|
|
321
|
+
const down = (e) => {
|
|
322
|
+
const p = at(e);
|
|
323
|
+
if (!p)
|
|
324
|
+
return;
|
|
325
|
+
e.preventDefault();
|
|
326
|
+
el.focus({ preventScroll: true });
|
|
327
|
+
if (e.pointerType === "touch") {
|
|
328
|
+
touch.down(e.pointerId, e.clientX, e.clientY, p);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
el.setPointerCapture?.(e.pointerId);
|
|
332
|
+
flushMove();
|
|
333
|
+
pressed = p;
|
|
334
|
+
send([{ type: "mouse", action: "down", ...p, button: BUTTONS[e.button] ?? "left", clickCount: Math.min(3, Math.max(1, e.detail || 1)) }]);
|
|
335
|
+
};
|
|
336
|
+
const moved = (e) => {
|
|
337
|
+
if (e.pointerType === "touch") {
|
|
338
|
+
const r = el.getBoundingClientRect();
|
|
339
|
+
const scale = frameRef.current && r.width > 0 && r.height > 0 ? Math.max(frameRef.current.w / r.width, frameRef.current.h / r.height) : 1;
|
|
340
|
+
if (touch.move(e.pointerId, e.clientX, e.clientY, scale))
|
|
341
|
+
wheelTimer ??= setTimeout(flushWheel, MOVE_MS);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
const p = at(e);
|
|
345
|
+
if (!p)
|
|
346
|
+
return;
|
|
347
|
+
if (pressed)
|
|
348
|
+
pressed = p;
|
|
349
|
+
move = p;
|
|
350
|
+
moveTimer ??= setTimeout(flushMove, MOVE_MS);
|
|
351
|
+
};
|
|
352
|
+
const up = (e) => {
|
|
353
|
+
if (e.pointerType === "touch") {
|
|
354
|
+
if (wheelTimer)
|
|
355
|
+
clearTimeout(wheelTimer);
|
|
356
|
+
wheelTimer = null;
|
|
357
|
+
const out = touch.up(e.pointerId);
|
|
358
|
+
if (out.length > 0)
|
|
359
|
+
send(out);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const p = at(e) ?? pressed;
|
|
363
|
+
pressed = null;
|
|
364
|
+
if (!p)
|
|
365
|
+
return;
|
|
366
|
+
flushMove();
|
|
367
|
+
send([{ type: "mouse", action: "up", ...p, button: BUTTONS[e.button] ?? "left", clickCount: Math.min(3, Math.max(1, e.detail || 1)) }]);
|
|
368
|
+
};
|
|
369
|
+
const lost = (e) => {
|
|
370
|
+
if (e.pointerType === "touch" || !pressed)
|
|
371
|
+
return;
|
|
372
|
+
const p = pressed;
|
|
373
|
+
pressed = null;
|
|
374
|
+
flushMove();
|
|
375
|
+
send([{ type: "mouse", action: "up", ...p, button: BUTTONS[e.button] ?? "left", clickCount: 1 }]);
|
|
376
|
+
};
|
|
377
|
+
const onWheel = (e) => {
|
|
378
|
+
const p = at(e);
|
|
379
|
+
if (!p)
|
|
380
|
+
return;
|
|
381
|
+
e.preventDefault();
|
|
382
|
+
const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? 800 : 1;
|
|
383
|
+
const clamp = (n) => Math.max(-VIEWER_CAPS.wheel, Math.min(VIEWER_CAPS.wheel, Math.round(n * unit)));
|
|
384
|
+
send([{ type: "mouse", action: "wheel", ...p, deltaX: clamp(e.deltaX), deltaY: clamp(e.deltaY) }]);
|
|
385
|
+
};
|
|
386
|
+
let afterEscape = false;
|
|
387
|
+
const pasting = new Set();
|
|
388
|
+
const keyDown = (e) => {
|
|
389
|
+
if (e.isComposing)
|
|
390
|
+
return;
|
|
391
|
+
if (isPasteChord(e)) {
|
|
392
|
+
pasting.add(e.code || e.key.toLowerCase());
|
|
393
|
+
afterEscape = false;
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
const r = keyInputOf(e, "down", afterEscape);
|
|
397
|
+
afterEscape = escapeMarkAfter(e.key, afterEscape);
|
|
398
|
+
if (r.leave)
|
|
399
|
+
return;
|
|
400
|
+
e.preventDefault();
|
|
401
|
+
if (r.send)
|
|
402
|
+
send([r.send]);
|
|
403
|
+
};
|
|
404
|
+
const keyUp = (e) => {
|
|
405
|
+
if (e.isComposing)
|
|
406
|
+
return;
|
|
407
|
+
if (pasting.delete(e.code || e.key.toLowerCase()))
|
|
408
|
+
return e.preventDefault();
|
|
409
|
+
const r = keyInputOf(e, "up", false);
|
|
410
|
+
e.preventDefault();
|
|
411
|
+
if (r.send)
|
|
412
|
+
send([r.send]);
|
|
413
|
+
};
|
|
414
|
+
const paste = (e) => {
|
|
415
|
+
e.preventDefault();
|
|
416
|
+
const text = utf8Clip(e.clipboardData?.getData("text/plain") ?? "", VIEWER_CAPS.textBytes);
|
|
417
|
+
if (text !== "")
|
|
418
|
+
send([{ type: "text", text }]);
|
|
419
|
+
};
|
|
420
|
+
const beforePaste = (e) => e.preventDefault();
|
|
421
|
+
const menu = (e) => e.preventDefault();
|
|
422
|
+
const cancel = () => {
|
|
423
|
+
if (wheelTimer)
|
|
424
|
+
clearTimeout(wheelTimer);
|
|
425
|
+
wheelTimer = null;
|
|
426
|
+
touch.cancel();
|
|
427
|
+
};
|
|
428
|
+
el.addEventListener("pointerdown", down);
|
|
429
|
+
el.addEventListener("pointermove", moved);
|
|
430
|
+
el.addEventListener("pointerup", up);
|
|
431
|
+
el.addEventListener("pointercancel", cancel);
|
|
432
|
+
el.addEventListener("lostpointercapture", lost);
|
|
433
|
+
el.addEventListener("beforepaste", beforePaste);
|
|
434
|
+
el.addEventListener("wheel", onWheel, { passive: false });
|
|
435
|
+
el.addEventListener("keydown", keyDown);
|
|
436
|
+
el.addEventListener("keyup", keyUp);
|
|
437
|
+
el.addEventListener("paste", paste);
|
|
438
|
+
el.addEventListener("contextmenu", menu);
|
|
439
|
+
return () => {
|
|
440
|
+
if (moveTimer)
|
|
441
|
+
clearTimeout(moveTimer);
|
|
442
|
+
if (wheelTimer)
|
|
443
|
+
clearTimeout(wheelTimer);
|
|
444
|
+
el.removeEventListener("pointerdown", down);
|
|
445
|
+
el.removeEventListener("pointermove", moved);
|
|
446
|
+
el.removeEventListener("pointerup", up);
|
|
447
|
+
el.removeEventListener("pointercancel", cancel);
|
|
448
|
+
el.removeEventListener("lostpointercapture", lost);
|
|
449
|
+
el.removeEventListener("beforepaste", beforePaste);
|
|
450
|
+
el.removeEventListener("wheel", onWheel);
|
|
451
|
+
el.removeEventListener("keydown", keyDown);
|
|
452
|
+
el.removeEventListener("keyup", keyUp);
|
|
453
|
+
el.removeEventListener("paste", paste);
|
|
454
|
+
el.removeEventListener("contextmenu", menu);
|
|
455
|
+
};
|
|
456
|
+
}, [held]);
|
|
457
|
+
const keyField = held ? (_jsx("input", { ref: keys, "data-ol-computer-keys": "", tabIndex: -1, "aria-label": l.keyboard, autoComplete: "off", autoCapitalize: "off", autoCorrect: "off", spellCheck: false, style: HIDDEN, onInput: (e) => {
|
|
458
|
+
if (e.nativeEvent.isComposing)
|
|
459
|
+
return;
|
|
460
|
+
const text = utf8Clip(e.currentTarget.value, VIEWER_CAPS.textBytes);
|
|
461
|
+
e.currentTarget.value = "";
|
|
462
|
+
if (text !== "")
|
|
463
|
+
viewer.current?.send([{ type: "text", text }]);
|
|
464
|
+
}, onCompositionEnd: (e) => {
|
|
465
|
+
const text = utf8Clip(e.currentTarget.value, VIEWER_CAPS.textBytes);
|
|
466
|
+
e.currentTarget.value = "";
|
|
467
|
+
if (text !== "")
|
|
468
|
+
viewer.current?.send([{ type: "text", text }]);
|
|
469
|
+
}, onKeyDown: (e) => {
|
|
470
|
+
if (e.key !== "Backspace" && e.key !== "Enter")
|
|
471
|
+
return;
|
|
472
|
+
e.preventDefault();
|
|
473
|
+
viewer.current?.send([
|
|
474
|
+
{ type: "key", action: "down", key: e.key, code: e.key },
|
|
475
|
+
{ type: "key", action: "up", key: e.key, code: e.key },
|
|
476
|
+
]);
|
|
477
|
+
} })) : null;
|
|
478
|
+
return (_jsx(ComputerViewView, { snap: ended ? { ...snap, phase: "ended", frame: null } : snap, session: props.session ?? null, labels: l, canTakeOver: props.canTakeOver !== false, frameUrl: ended ? null : frameUrl, onTake: () => void viewer.current?.take(), onGiveBack: () => void viewer.current?.giveBack(), onRetry: () => viewer.current?.retry(), onGoOn: props.onGoOn
|
|
479
|
+
? () => {
|
|
480
|
+
setGoOnPressed(here);
|
|
481
|
+
props.onGoOn?.();
|
|
482
|
+
}
|
|
483
|
+
: undefined, goOnPressed: goOnPressed, ratio: ended ? null : ratio.current, onClose: props.onClose, keys: keyField, onKeyboard: () => keys.current?.focus({ preventScroll: true }), rootRef: root, buttonRef: button, pictureRef: picture, className: props.className }));
|
|
484
|
+
}
|
|
485
|
+
export const BrowserControlContext = createContext(null);
|
|
486
|
+
export const ComputerRowsContext = createContext(null);
|
|
487
|
+
export function ComputerPointer({ session, computer }) {
|
|
488
|
+
const l = { ...DEFAULT_COMPUTER_VIEW_LABELS, ...computer?.labels };
|
|
489
|
+
const rows = useContext(ComputerRowsContext);
|
|
490
|
+
const { sentence } = computerWords(null, session, l, false);
|
|
491
|
+
return (_jsxs("div", { "data-ol-computer-pointer": "", role: "group", "aria-label": l.region, children: [_jsx("p", { "data-ol-computer-pointer-sentence": "", children: sentence }), rows ? (_jsx("button", { type: "button", "data-ol-computer-show": "", onClick: () => rows.show(session.id), children: l.showBrowser ?? DEFAULT_COMPUTER_VIEW_LABELS.showBrowser })) : null] }));
|
|
492
|
+
}
|
|
493
|
+
export function controlAfter(held, session, isHeld) {
|
|
494
|
+
if (held.has(session) === isHeld)
|
|
495
|
+
return held;
|
|
496
|
+
const next = new Set(held);
|
|
497
|
+
if (isHeld)
|
|
498
|
+
next.add(session);
|
|
499
|
+
else
|
|
500
|
+
next.delete(session);
|
|
501
|
+
return next;
|
|
502
|
+
}
|
|
503
|
+
export function ComputerRow({ session, computer }) {
|
|
504
|
+
const l = { ...DEFAULT_COMPUTER_VIEW_LABELS, ...computer?.labels };
|
|
505
|
+
const [open, setOpen] = useState(false);
|
|
506
|
+
const watch = useRef(null);
|
|
507
|
+
const reopened = useRef(false);
|
|
508
|
+
useEffect(() => {
|
|
509
|
+
if (!open && reopened.current)
|
|
510
|
+
watch.current?.focus({ preventScroll: true });
|
|
511
|
+
}, [open]);
|
|
512
|
+
const control = useContext(BrowserControlContext);
|
|
513
|
+
const report = useRef(control?.report);
|
|
514
|
+
report.current = control?.report;
|
|
515
|
+
const id = session.id;
|
|
516
|
+
useEffect(() => () => report.current?.(id, false), [id, open]);
|
|
517
|
+
const rows = useContext(ComputerRowsContext);
|
|
518
|
+
const [reveal, setReveal] = useState(0);
|
|
519
|
+
const closedRoot = useRef(null);
|
|
520
|
+
const show = useRef(() => undefined);
|
|
521
|
+
show.current = () => {
|
|
522
|
+
if (computer && (open || session.status !== "ended")) {
|
|
523
|
+
setOpen(true);
|
|
524
|
+
setReveal((n) => n + 1);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
closedRoot.current?.scrollIntoView?.({ block: "center" });
|
|
528
|
+
closedRoot.current?.focus({ preventScroll: true });
|
|
529
|
+
};
|
|
530
|
+
useEffect(() => rows?.add(id, () => show.current()), [rows, id]);
|
|
531
|
+
if (open && computer) {
|
|
532
|
+
return (_jsx(ComputerView, { session: session, partAt: rows?.newestIn(id) ?? null, reveal: reveal, fetchViewer: computer.fetchViewer, canTakeOver: computer.canTakeOver, labels: computer.labels, onControlChange: (held) => {
|
|
533
|
+
report.current?.(id, held);
|
|
534
|
+
computer.onControlChange?.(held);
|
|
535
|
+
}, onGoOn: control?.goOn ?? undefined, fetch: computer.fetch, onTiming: computer.onTiming, autoFocus: true, onClose: () => {
|
|
536
|
+
reopened.current = true;
|
|
537
|
+
setOpen(false);
|
|
538
|
+
} }));
|
|
539
|
+
}
|
|
540
|
+
const { sentence } = computerWords(null, session, l, false);
|
|
541
|
+
const ended = session.status === "ended";
|
|
542
|
+
return (_jsx("div", { ref: closedRoot, "data-ol-computer": "", "data-ol-computer-state": ended ? "ended" : "closed", role: "group", "aria-label": l.region, tabIndex: -1, children: _jsxs("div", { "data-ol-computer-bar": "", children: [_jsxs("div", { "data-ol-computer-words": "", children: [_jsx("p", { "data-ol-computer-sentence": "", role: "status", children: sentence }), session.site ? _jsx("p", { "data-ol-computer-site": "", children: session.site }) : null] }), computer && !ended ? (_jsx("div", { "data-ol-computer-actions": "", children: _jsx("button", { ref: watch, type: "button", "data-ol-computer-watch": "", ...(session.waitingFor ? { "data-ol-computer-primary": "" } : {}), onClick: () => setOpen(true), children: l.watch }) })) : null] }) }));
|
|
543
|
+
}
|