@opengeni/react 0.46.1 → 0.52.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 +158 -14
- package/dist/artifacts-document.d.ts +3 -0
- package/dist/artifacts-document.js +17 -0
- package/dist/artifacts-document.js.map +1 -0
- package/dist/artifacts-presentation.d.ts +3 -0
- package/dist/artifacts-presentation.js +17 -0
- package/dist/artifacts-presentation.js.map +1 -0
- package/dist/artifacts-spreadsheet.d.ts +3 -0
- package/dist/artifacts-spreadsheet.js +17 -0
- package/dist/artifacts-spreadsheet.js.map +1 -0
- package/dist/artifacts.d.ts +2 -0
- package/dist/artifacts.js +170 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/chunk-6BBWWOOU.js +82 -0
- package/dist/chunk-6BBWWOOU.js.map +1 -0
- package/dist/{chunk-HFO4ERGQ.js → chunk-724BW4TW.js} +586 -35
- package/dist/chunk-724BW4TW.js.map +1 -0
- package/dist/chunk-7I2KB3OK.js +3895 -0
- package/dist/chunk-7I2KB3OK.js.map +1 -0
- package/dist/chunk-7WCLAEFV.js +2828 -0
- package/dist/chunk-7WCLAEFV.js.map +1 -0
- package/dist/{chunk-U3CSCMWA.js → chunk-FK6VG4LL.js} +9 -13
- package/dist/chunk-FK6VG4LL.js.map +1 -0
- package/dist/{chunk-SRFUT2ZU.js → chunk-FWS3KC3Z.js} +120 -64
- package/dist/chunk-FWS3KC3Z.js.map +1 -0
- package/dist/{chunk-EKZH6IDG.js → chunk-HDBG7XGO.js} +62 -1
- package/dist/chunk-HDBG7XGO.js.map +1 -0
- package/dist/chunk-HUIKIDM5.js +2148 -0
- package/dist/chunk-HUIKIDM5.js.map +1 -0
- package/dist/{chunk-LGVMUIRV.js → chunk-JAGDUCZQ.js} +1053 -390
- package/dist/chunk-JAGDUCZQ.js.map +1 -0
- package/dist/{chunk-LWR4MXSS.js → chunk-JR3QFY6B.js} +420 -35
- package/dist/chunk-JR3QFY6B.js.map +1 -0
- package/dist/chunk-JVYQRRC4.js +53 -0
- package/dist/chunk-JVYQRRC4.js.map +1 -0
- package/dist/chunk-KCGXPP5V.js +157 -0
- package/dist/chunk-KCGXPP5V.js.map +1 -0
- package/dist/{chunk-EB67J347.js → chunk-KZ73RL76.js} +111 -31
- package/dist/chunk-KZ73RL76.js.map +1 -0
- package/dist/chunk-PEI6RANI.js +3234 -0
- package/dist/chunk-PEI6RANI.js.map +1 -0
- package/dist/{chunk-3PB7MIT6.js → chunk-ROIRTM37.js} +220 -72
- package/dist/chunk-ROIRTM37.js.map +1 -0
- package/dist/chunk-VZTQ73XT.js +86 -0
- package/dist/chunk-VZTQ73XT.js.map +1 -0
- package/dist/{chunk-VCBFUSUZ.js → chunk-WSK7G5LE.js} +72 -4
- package/dist/chunk-WSK7G5LE.js.map +1 -0
- package/dist/{chunk-FGZUCXZF.js → chunk-WYA65Y3F.js} +16 -16
- package/dist/chunk-WYA65Y3F.js.map +1 -0
- package/dist/{chunk-6EDV6YFE.js → chunk-X4S56Y2H.js} +147 -35
- package/dist/chunk-X4S56Y2H.js.map +1 -0
- package/dist/client.d.ts +7 -1
- package/dist/clipboard.d.ts +1 -0
- package/dist/clipboard.js +7 -0
- package/dist/clipboard.js.map +1 -0
- package/dist/commands/index.d.ts +3 -3
- package/dist/commands/registry.d.ts +1 -1
- package/dist/commands/types.d.ts +1 -1
- package/dist/components/approval-surface.d.ts +2 -2
- package/dist/components/artifacts/artifact-surface.d.ts +18 -0
- package/dist/components/artifacts/document-editor.d.ts +172 -0
- package/dist/components/artifacts/editable-artifact-ui.d.ts +13 -0
- package/dist/components/artifacts/editable-artifact-workbench.d.ts +49 -0
- package/dist/components/artifacts/editable-document.d.ts +24 -0
- package/dist/components/artifacts/editable-presentation.d.ts +24 -0
- package/dist/components/artifacts/editable-spreadsheet.d.ts +32 -0
- package/dist/components/artifacts/index.d.ts +8 -0
- package/dist/components/artifacts/presentation-editor.d.ts +297 -0
- package/dist/components/artifacts/spreadsheet-canvas.d.ts +120 -0
- package/dist/components/artifacts/spreadsheet-grid.d.ts +113 -0
- package/dist/components/browser-viewer.d.ts +29 -0
- package/dist/components/chat-composer.d.ts +13 -8
- package/dist/components/code-editor.d.ts +25 -3
- package/dist/components/command-palette.d.ts +2 -2
- package/dist/components/composer-transcription-control.d.ts +4 -2
- package/dist/components/composer.d.ts +29 -15
- package/dist/components/computer-viewer.d.ts +23 -0
- package/dist/components/copy-button.d.ts +2 -2
- package/dist/components/desktop-viewer.d.ts +25 -21
- package/dist/components/diff-view.d.ts +1 -1
- package/dist/components/enrollment-consent.d.ts +1 -1
- package/dist/components/enrollment-device-flow.d.ts +1 -1
- package/dist/components/file-browser.d.ts +2 -2
- package/dist/components/fleet-tile.d.ts +1 -1
- package/dist/components/generated-video-player.d.ts +10 -0
- package/dist/components/human-input-form.d.ts +1 -1
- package/dist/components/human-input-surface.d.ts +2 -2
- package/dist/components/interactive-workbench-viewers.d.ts +2 -0
- package/dist/components/machine-card.d.ts +2 -2
- package/dist/components/machine-dock-bar.d.ts +3 -3
- package/dist/components/machine-health-pill.d.ts +2 -2
- package/dist/components/machine-input-display.d.ts +1 -1
- package/dist/components/machine-metrics.d.ts +2 -2
- package/dist/components/machine-status-pill.d.ts +4 -4
- package/dist/components/machines/health.d.ts +1 -1
- package/dist/components/machines/machine-detail.d.ts +5 -3
- package/dist/components/machines/metric-history-chart.d.ts +1 -1
- package/dist/components/machines/metric-sparkline.d.ts +2 -2
- package/dist/components/machines/series.d.ts +2 -2
- package/dist/components/machines-dashboard.d.ts +5 -5
- package/dist/components/markdown.d.ts +1 -1
- package/dist/components/message-timeline.d.ts +22 -5
- package/dist/components/model-picker.d.ts +2 -2
- package/dist/components/model-policy-picker.d.ts +7 -7
- package/dist/components/pierre-diff.d.ts +1 -1
- package/dist/components/pierre-file.d.ts +1 -1
- package/dist/components/queue-draft-policy.d.ts +1 -1
- package/dist/components/queue-surface-implementation.d.ts +3 -3
- package/dist/components/queue-surface-state.d.ts +4 -4
- package/dist/components/queue-surface.d.ts +5 -5
- package/dist/components/sandbox-files.d.ts +5 -5
- package/dist/components/sandbox-terminal.d.ts +50 -4
- package/dist/components/sandbox-workspace.d.ts +30 -10
- package/dist/components/session-chrome.d.ts +4 -4
- package/dist/components/session-status.d.ts +2 -2
- package/dist/components/timeline-annotation-selection.d.ts +9 -0
- package/dist/components/timeline-annotations-dialog.d.ts +16 -0
- package/dist/components/timeline-annotations.d.ts +13 -0
- package/dist/components/tooltip.d.ts +4 -4
- package/dist/components/user-message-body.d.ts +2 -2
- package/dist/components/workbench-changes.d.ts +2 -2
- package/dist/components/workspace-dock.d.ts +3 -1
- package/dist/composer.d.ts +11 -11
- package/dist/composer.js +7 -7
- package/dist/hooks/internal.d.ts +7 -1
- package/dist/hooks/use-attached-browsers.d.ts +21 -0
- package/dist/hooks/use-available-models.d.ts +2 -2
- package/dist/hooks/use-billing-usage.d.ts +1 -1
- package/dist/hooks/use-browser-frame-stream.d.ts +28 -0
- package/dist/hooks/use-browser-identities.d.ts +26 -0
- package/dist/hooks/use-browser-session.d.ts +28 -0
- package/dist/hooks/use-browser-sessions.d.ts +32 -0
- package/dist/hooks/use-codex-accounts.d.ts +3 -3
- package/dist/hooks/use-composer.d.ts +36 -9
- package/dist/hooks/use-computer-frame-stream.d.ts +24 -0
- package/dist/hooks/use-computer-session.d.ts +28 -0
- package/dist/hooks/use-computer-sessions.d.ts +28 -0
- package/dist/hooks/use-desktop-stream.d.ts +1 -1
- package/dist/hooks/use-environments.d.ts +11 -5
- package/dist/hooks/use-file-attachments.d.ts +1 -1
- package/dist/hooks/use-goal.d.ts +2 -2
- package/dist/hooks/use-human-input.d.ts +2 -2
- package/dist/hooks/use-last-started-turn-policy.d.ts +2 -2
- package/dist/hooks/use-machine-chip.d.ts +2 -2
- package/dist/hooks/use-machines.d.ts +10 -3
- package/dist/hooks/use-packs.d.ts +7 -2
- package/dist/hooks/use-rigs.d.ts +1 -1
- package/dist/hooks/use-sandbox-files.d.ts +25 -4
- package/dist/hooks/use-sandbox-git.d.ts +13 -5
- package/dist/hooks/use-sandbox-terminal.d.ts +1 -1
- package/dist/hooks/use-scheduled-tasks.d.ts +1 -1
- package/dist/hooks/use-session-capabilities.d.ts +5 -4
- package/dist/hooks/use-session-control.d.ts +1 -1
- package/dist/hooks/use-session-events.d.ts +2 -2
- package/dist/hooks/use-session-lineage.d.ts +1 -1
- package/dist/hooks/use-session-mcp-approval-policy.d.ts +2 -2
- package/dist/hooks/use-session.d.ts +2 -2
- package/dist/hooks/use-slash-commands.d.ts +1 -1
- package/dist/hooks/use-terminal-stream.d.ts +8 -2
- package/dist/hooks/use-transcription.d.ts +2 -2
- package/dist/hooks/use-turn-queue.d.ts +2 -2
- package/dist/hooks/use-video-artifact-playback.d.ts +9 -0
- package/dist/hooks/use-voice-input.d.ts +6 -4
- package/dist/hooks/use-workspace-capture.d.ts +1 -1
- package/dist/hooks/use-workspace-edit.d.ts +1 -1
- package/dist/hooks/use-workspace-sessions.d.ts +1 -1
- package/dist/hooks/use-workspaces.d.ts +1 -1
- package/dist/index.d.ts +158 -154
- package/dist/index.js +1718 -908
- package/dist/index.js.map +1 -1
- package/dist/interaction.d.ts +23 -0
- package/dist/interaction.js +36 -0
- package/dist/interaction.js.map +1 -0
- package/dist/interactive-workbench-viewers-KHNOHWQE.js +13 -0
- package/dist/interactive-workbench-viewers-KHNOHWQE.js.map +1 -0
- package/dist/lib/composer-responsive-context.d.ts +9 -0
- package/dist/lib/relay-wire.d.ts +4 -0
- package/dist/lib/sandbox-liveness.d.ts +9 -0
- package/dist/lib/terminal-capability.d.ts +5 -0
- package/dist/lib/use-portal-token-style.d.ts +14 -3
- package/dist/lib/xterm-theme.d.ts +1 -1
- package/dist/machines.d.ts +30 -30
- package/dist/machines.js +3 -3
- package/dist/provider.d.ts +4 -4
- package/dist/{queue-surface-implementation-J4XJFKRW.js → queue-surface-implementation-2KVJTYXH.js} +117 -64
- package/dist/queue-surface-implementation-2KVJTYXH.js.map +1 -0
- package/dist/realtime/dropdown-menu.d.ts +8 -8
- package/dist/realtime/realtime-control.d.ts +15 -6
- package/dist/realtime.d.ts +4 -4
- package/dist/realtime.js +30 -33
- package/dist/realtime.js.map +1 -1
- package/dist/session-context.d.ts +19 -1
- package/dist/session-ui.d.ts +14 -14
- package/dist/session-ui.js +6 -5
- package/dist/session.d.ts +28 -28
- package/dist/session.js +5 -5
- package/dist/timeline/activity-rail.d.ts +6 -3
- package/dist/timeline/disclosure-context.d.ts +1 -1
- package/dist/timeline/fleet-decision-projection.d.ts +1 -1
- package/dist/timeline/fleet-decision-row.d.ts +3 -3
- package/dist/timeline/index.d.ts +19 -19
- package/dist/timeline/parsers.d.ts +6 -3
- package/dist/timeline/projection.d.ts +3 -3
- package/dist/timeline/registry.d.ts +10 -1
- package/dist/timeline/screenshot-lightbox.d.ts +2 -2
- package/dist/timeline/shared.d.ts +21 -10
- package/dist/timeline/tool-diff.d.ts +2 -2
- package/dist/timeline/tool-renderers.d.ts +1 -1
- package/dist/timeline/turn-summary.d.ts +3 -3
- package/dist/timeline/types.d.ts +42 -2
- package/dist/timeline-annotation-selection-3L7LHGG2.js +187 -0
- package/dist/timeline-annotation-selection-3L7LHGG2.js.map +1 -0
- package/dist/timeline-annotations-dialog-U7EVLR75.js +196 -0
- package/dist/timeline-annotations-dialog-U7EVLR75.js.map +1 -0
- package/dist/voice-recording-store.d.ts +8 -2
- package/package.json +52 -7
- package/src/artifacts-document.ts +31 -0
- package/src/artifacts-presentation.ts +36 -0
- package/src/artifacts-spreadsheet.ts +21 -0
- package/src/artifacts.ts +86 -0
- package/src/client.ts +90 -1
- package/src/clipboard.ts +2 -0
- package/src/components/artifacts/artifact-surface.tsx +93 -0
- package/src/components/artifacts/document-editor.tsx +2255 -0
- package/src/components/artifacts/editable-artifact-ui.tsx +89 -0
- package/src/components/artifacts/editable-artifact-workbench.tsx +203 -0
- package/src/components/artifacts/editable-document.tsx +657 -0
- package/src/components/artifacts/editable-presentation.tsx +1020 -0
- package/src/components/artifacts/editable-spreadsheet.tsx +733 -0
- package/src/components/artifacts/index.ts +99 -0
- package/src/components/artifacts/presentation-editor.tsx +3148 -0
- package/src/components/artifacts/spreadsheet-canvas.ts +799 -0
- package/src/components/artifacts/spreadsheet-grid.tsx +2129 -0
- package/src/components/browser-viewer.tsx +1592 -0
- package/src/components/chat-composer.tsx +25 -2
- package/src/components/code-editor.tsx +90 -31
- package/src/components/command-palette.tsx +1 -1
- package/src/components/composer-transcription-control.tsx +51 -22
- package/src/components/composer.tsx +49 -23
- package/src/components/computer-viewer.tsx +1139 -0
- package/src/components/copy-button.tsx +1 -1
- package/src/components/desktop-viewer.tsx +103 -75
- package/src/components/file-browser.tsx +39 -17
- package/src/components/generated-video-player.tsx +89 -0
- package/src/components/human-input-form.tsx +2 -2
- package/src/components/interactive-workbench-viewers.ts +2 -0
- package/src/components/machine-card.tsx +1 -1
- package/src/components/machine-input-display.ts +3 -0
- package/src/components/machines/machine-detail.tsx +39 -15
- package/src/components/machines-dashboard.tsx +7 -6
- package/src/components/message-timeline.tsx +327 -37
- package/src/components/model-picker.tsx +2 -2
- package/src/components/model-policy-picker.tsx +16 -15
- package/src/components/queue-surface-implementation.tsx +135 -75
- package/src/components/queue-surface-state.tsx +2 -2
- package/src/components/sandbox-files.tsx +142 -62
- package/src/components/sandbox-terminal.tsx +119 -35
- package/src/components/sandbox-workspace.tsx +394 -228
- package/src/components/session-chrome.tsx +67 -27
- package/src/components/timeline-annotation-selection.tsx +221 -0
- package/src/components/timeline-annotations-dialog.tsx +213 -0
- package/src/components/timeline-annotations.tsx +84 -0
- package/src/components/tooltip.tsx +13 -16
- package/src/components/user-message-body.tsx +1 -1
- package/src/components/workbench-changes.tsx +192 -35
- package/src/components/workspace-dock.tsx +13 -5
- package/src/composer.ts +3 -0
- package/src/hooks/internal.ts +132 -38
- package/src/hooks/use-attached-browsers.ts +152 -0
- package/src/hooks/use-browser-frame-stream.ts +316 -0
- package/src/hooks/use-browser-identities.ts +225 -0
- package/src/hooks/use-browser-session.ts +476 -0
- package/src/hooks/use-browser-sessions.ts +281 -0
- package/src/hooks/use-codex-accounts.ts +4 -4
- package/src/hooks/use-composer.ts +769 -24
- package/src/hooks/use-computer-frame-stream.ts +311 -0
- package/src/hooks/use-computer-session.ts +442 -0
- package/src/hooks/use-computer-sessions.ts +245 -0
- package/src/hooks/use-environments.ts +24 -5
- package/src/hooks/use-file-attachments.ts +70 -32
- package/src/hooks/use-goal.ts +17 -5
- package/src/hooks/use-machine-chip.ts +5 -3
- package/src/hooks/use-machines.ts +79 -3
- package/src/hooks/use-packs.ts +51 -0
- package/src/hooks/use-sandbox-files.ts +382 -88
- package/src/hooks/use-sandbox-git.ts +208 -46
- package/src/hooks/use-sandbox-terminal.ts +3 -2
- package/src/hooks/use-session-capabilities.ts +94 -70
- package/src/hooks/use-session-events.ts +18 -4
- package/src/hooks/use-terminal-stream.ts +102 -23
- package/src/hooks/use-transcription.ts +4 -14
- package/src/hooks/use-turn-queue.ts +17 -5
- package/src/hooks/use-video-artifact-playback.ts +28 -0
- package/src/hooks/use-voice-input.ts +163 -19
- package/src/hooks/use-windowed-sections.ts +9 -7
- package/src/hooks/use-workspace-edit.ts +2 -1
- package/src/index.ts +21 -6
- package/src/interaction.ts +71 -0
- package/src/lib/composer-responsive-context.ts +15 -0
- package/src/lib/relay-wire.ts +4 -0
- package/src/lib/sandbox-liveness.ts +11 -0
- package/src/lib/terminal-capability.ts +10 -0
- package/src/lib/use-portal-token-style.ts +60 -15
- package/src/provider.tsx +52 -7
- package/src/realtime/dropdown-menu.tsx +15 -19
- package/src/realtime/realtime-control.tsx +16 -5
- package/src/session-context.ts +90 -0
- package/src/timeline/activity-rail.tsx +31 -6
- package/src/timeline/index.ts +4 -1
- package/src/timeline/parsers.ts +46 -20
- package/src/timeline/projection.ts +233 -13
- package/src/timeline/registry.ts +19 -0
- package/src/timeline/screenshot-lightbox.tsx +30 -9
- package/src/timeline/shared.tsx +59 -11
- package/src/timeline/tool-renderers.tsx +421 -24
- package/src/timeline/turn-summary.tsx +2 -2
- package/src/timeline/types.ts +58 -2
- package/src/voice-recording-store.ts +15 -0
- package/styles/compiled.css +5298 -0
- package/styles/compiled.d.ts +2 -0
- package/styles/effective-tokens.css +27 -0
- package/styles/index.css +18 -0
- package/styles/responsive.css +208 -0
- package/styles/responsive.d.ts +2 -0
- package/styles/tokens.css +50 -17
- package/dist/chunk-3PB7MIT6.js.map +0 -1
- package/dist/chunk-6EDV6YFE.js.map +0 -1
- package/dist/chunk-EB67J347.js.map +0 -1
- package/dist/chunk-EKZH6IDG.js.map +0 -1
- package/dist/chunk-FGZUCXZF.js.map +0 -1
- package/dist/chunk-HFO4ERGQ.js.map +0 -1
- package/dist/chunk-JB5725BI.js +0 -48
- package/dist/chunk-JB5725BI.js.map +0 -1
- package/dist/chunk-LGVMUIRV.js.map +0 -1
- package/dist/chunk-LWR4MXSS.js.map +0 -1
- package/dist/chunk-SRFUT2ZU.js.map +0 -1
- package/dist/chunk-U3CSCMWA.js.map +0 -1
- package/dist/chunk-VCBFUSUZ.js.map +0 -1
- package/dist/fleet-decision-projection-SLNZMS42.js +0 -241
- package/dist/fleet-decision-projection-SLNZMS42.js.map +0 -1
- package/dist/queue-surface-implementation-J4XJFKRW.js.map +0 -1
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computerFrameSocketUrl,
|
|
3
|
+
decodeComputerFrameMessage,
|
|
4
|
+
type ComputerFrame,
|
|
5
|
+
type ComputerFrameStreamOptions,
|
|
6
|
+
type ComputerSessionAttachment,
|
|
7
|
+
} from "@opengeni/sdk/interaction";
|
|
8
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
9
|
+
import {
|
|
10
|
+
decodeStreamFrame,
|
|
11
|
+
decodeStreamOpenAck,
|
|
12
|
+
encodeStreamOpen,
|
|
13
|
+
STREAM_KIND_COMPUTER,
|
|
14
|
+
STREAM_ROLE_CLIENT,
|
|
15
|
+
} from "../lib/relay-wire";
|
|
16
|
+
import {
|
|
17
|
+
type EmbeddedComputerInteractionClientOverride,
|
|
18
|
+
useEmbeddedComputerInteraction,
|
|
19
|
+
} from "../session-context";
|
|
20
|
+
|
|
21
|
+
export type ComputerFrameConnectionState =
|
|
22
|
+
| "idle"
|
|
23
|
+
| "attaching"
|
|
24
|
+
| "connecting"
|
|
25
|
+
| "live"
|
|
26
|
+
| "reconnecting"
|
|
27
|
+
| "error";
|
|
28
|
+
|
|
29
|
+
export type ComputerFrameWebSocket = Pick<
|
|
30
|
+
WebSocket,
|
|
31
|
+
"binaryType" | "readyState" | "close" | "addEventListener" | "removeEventListener"
|
|
32
|
+
> & { send(data: ArrayBuffer): void };
|
|
33
|
+
|
|
34
|
+
export type ComputerFrameWebSocketFactory = (
|
|
35
|
+
url: string,
|
|
36
|
+
protocols: string[],
|
|
37
|
+
) => ComputerFrameWebSocket;
|
|
38
|
+
|
|
39
|
+
const RELAY_TAG_OPEN = 1;
|
|
40
|
+
const RELAY_TAG_OPEN_ACK = 2;
|
|
41
|
+
const RELAY_TAG_FRAME = 3;
|
|
42
|
+
|
|
43
|
+
export type UseComputerFrameStreamOptions = EmbeddedComputerInteractionClientOverride & {
|
|
44
|
+
computerSessionId: string | null;
|
|
45
|
+
targetId: string | null;
|
|
46
|
+
enabled?: boolean | undefined;
|
|
47
|
+
stream?: ComputerFrameStreamOptions | undefined;
|
|
48
|
+
webSocketFactory?: ComputerFrameWebSocketFactory | undefined;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type UseComputerFrameStreamResult = {
|
|
52
|
+
state: ComputerFrameConnectionState;
|
|
53
|
+
frame: ComputerFrame | null;
|
|
54
|
+
attachment: Pick<
|
|
55
|
+
ComputerSessionAttachment,
|
|
56
|
+
"computerSessionId" | "controllerGeneration" | "targetId" | "expiresAt"
|
|
57
|
+
> | null;
|
|
58
|
+
error: Error | null;
|
|
59
|
+
reconnect: () => void;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** Reconnecting, latest-only ComputerSession media attachment. Grants remain in
|
|
63
|
+
* WebSocket subprotocols or the relay's authenticated first binary message. */
|
|
64
|
+
export function useComputerFrameStream(
|
|
65
|
+
options: UseComputerFrameStreamOptions,
|
|
66
|
+
): UseComputerFrameStreamResult {
|
|
67
|
+
const { client, workspaceId } = useEmbeddedComputerInteraction(options);
|
|
68
|
+
const enabled = options.enabled ?? true;
|
|
69
|
+
const [nonce, setNonce] = useState(0);
|
|
70
|
+
const [result, setResult] = useState<Omit<UseComputerFrameStreamResult, "reconnect">>({
|
|
71
|
+
state: "idle",
|
|
72
|
+
frame: null,
|
|
73
|
+
attachment: null,
|
|
74
|
+
error: null,
|
|
75
|
+
});
|
|
76
|
+
const latestRef = useRef<{ key: string; sequence: number }>({
|
|
77
|
+
key: "",
|
|
78
|
+
sequence: -1,
|
|
79
|
+
});
|
|
80
|
+
const factoryRef = useRef(options.webSocketFactory);
|
|
81
|
+
const streamRef = useRef(options.stream);
|
|
82
|
+
factoryRef.current = options.webSocketFactory;
|
|
83
|
+
streamRef.current = options.stream;
|
|
84
|
+
|
|
85
|
+
const reconnect = useCallback(() => setNonce((value) => value + 1), []);
|
|
86
|
+
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
const computerSessionId = options.computerSessionId;
|
|
89
|
+
const targetId = options.targetId;
|
|
90
|
+
if (!enabled || !computerSessionId || !targetId || typeof window === "undefined") {
|
|
91
|
+
setResult({ state: "idle", frame: null, attachment: null, error: null });
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
latestRef.current = { key: "", sequence: -1 };
|
|
96
|
+
setResult({
|
|
97
|
+
state: "attaching",
|
|
98
|
+
frame: null,
|
|
99
|
+
attachment: null,
|
|
100
|
+
error: null,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
let disposed = false;
|
|
104
|
+
let socket: ComputerFrameWebSocket | null = null;
|
|
105
|
+
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
106
|
+
let expiryTimer: ReturnType<typeof setTimeout> | null = null;
|
|
107
|
+
let failures = 0;
|
|
108
|
+
let controllerGeneration: string | null = null;
|
|
109
|
+
let activeStream: ComputerSessionAttachment["stream"] | null = null;
|
|
110
|
+
let relayAccepted = false;
|
|
111
|
+
const attachmentAbort = new AbortController();
|
|
112
|
+
|
|
113
|
+
const clearSocket = () => {
|
|
114
|
+
if (!socket) return;
|
|
115
|
+
socket.removeEventListener("open", onOpen);
|
|
116
|
+
socket.removeEventListener("message", onMessage);
|
|
117
|
+
socket.removeEventListener("error", onError);
|
|
118
|
+
socket.removeEventListener("close", onClose);
|
|
119
|
+
if (socket.readyState === 0 || socket.readyState === 1) {
|
|
120
|
+
socket.close(1000, "viewer detached");
|
|
121
|
+
}
|
|
122
|
+
socket = null;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const scheduleReconnect = () => {
|
|
126
|
+
if (disposed || reconnectTimer) return;
|
|
127
|
+
const delay = Math.min(5_000, 250 * 2 ** Math.min(failures, 5));
|
|
128
|
+
failures += 1;
|
|
129
|
+
setResult((current) => ({ ...current, state: "reconnecting" }));
|
|
130
|
+
reconnectTimer = setTimeout(() => {
|
|
131
|
+
reconnectTimer = null;
|
|
132
|
+
void connect();
|
|
133
|
+
}, delay);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const fail = (cause: unknown) => {
|
|
137
|
+
if (disposed) return;
|
|
138
|
+
const error = cause instanceof Error ? cause : new Error(String(cause));
|
|
139
|
+
setResult((current) => ({ ...current, state: "error", error }));
|
|
140
|
+
clearSocket();
|
|
141
|
+
scheduleReconnect();
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const onOpen = () => {
|
|
145
|
+
if (disposed) return;
|
|
146
|
+
failures = 0;
|
|
147
|
+
if (activeStream?.kind === "relay") {
|
|
148
|
+
const body = encodeStreamOpen({
|
|
149
|
+
channel: { ...activeStream.channel, kind: STREAM_KIND_COMPUTER },
|
|
150
|
+
token: activeStream.token,
|
|
151
|
+
role: STREAM_ROLE_CLIENT,
|
|
152
|
+
resumeFromSeq: "0",
|
|
153
|
+
});
|
|
154
|
+
socket?.send(relayDatagram(RELAY_TAG_OPEN, body));
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
setResult((current) => ({ ...current, state: "live", error: null }));
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const onMessage = (event: MessageEvent) => {
|
|
161
|
+
void (async () => {
|
|
162
|
+
try {
|
|
163
|
+
const bytes = await messageBytes(event.data);
|
|
164
|
+
if (disposed) return;
|
|
165
|
+
let frameBytes = bytes;
|
|
166
|
+
if (activeStream?.kind === "relay") {
|
|
167
|
+
if (bytes.length < 1) throw new Error("computer relay returned an empty message");
|
|
168
|
+
const tag = bytes[0];
|
|
169
|
+
const body = bytes.subarray(1);
|
|
170
|
+
if (tag === RELAY_TAG_OPEN_ACK) {
|
|
171
|
+
const ack = decodeStreamOpenAck(body);
|
|
172
|
+
if (!ack.accepted) {
|
|
173
|
+
throw new Error(ack.error?.message ?? "computer stream was rejected by the relay");
|
|
174
|
+
}
|
|
175
|
+
relayAccepted = true;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (tag !== RELAY_TAG_FRAME || !relayAccepted) return;
|
|
179
|
+
frameBytes = decodeStreamFrame(body).data;
|
|
180
|
+
}
|
|
181
|
+
const frame = await decodeComputerFrameMessage(frameBytes);
|
|
182
|
+
if (disposed) return;
|
|
183
|
+
if (frame.computerSessionId !== computerSessionId || frame.targetId !== targetId) {
|
|
184
|
+
throw new Error("computer frame belongs to another resource");
|
|
185
|
+
}
|
|
186
|
+
if (!controllerGeneration || frame.controllerGeneration !== controllerGeneration) {
|
|
187
|
+
throw new Error("computer frame belongs to a stale controller");
|
|
188
|
+
}
|
|
189
|
+
const key = `${computerSessionId}:${targetId}:${frame.controllerGeneration}:${frame.targetGeneration}`;
|
|
190
|
+
if (latestRef.current.key === key && frame.sequence <= latestRef.current.sequence) return;
|
|
191
|
+
latestRef.current = { key, sequence: frame.sequence };
|
|
192
|
+
setResult((current) => ({
|
|
193
|
+
...current,
|
|
194
|
+
state: "live",
|
|
195
|
+
frame,
|
|
196
|
+
error: null,
|
|
197
|
+
}));
|
|
198
|
+
} catch (cause) {
|
|
199
|
+
fail(cause);
|
|
200
|
+
}
|
|
201
|
+
})();
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const onError = () => fail(new Error("Computer view lost connection."));
|
|
205
|
+
|
|
206
|
+
const onClose = () => {
|
|
207
|
+
if (disposed) return;
|
|
208
|
+
clearSocket();
|
|
209
|
+
scheduleReconnect();
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const connect = async () => {
|
|
213
|
+
if (disposed) return;
|
|
214
|
+
clearSocket();
|
|
215
|
+
if (expiryTimer) clearTimeout(expiryTimer);
|
|
216
|
+
setResult((current) => ({
|
|
217
|
+
...current,
|
|
218
|
+
state: current.attachment ? "reconnecting" : "attaching",
|
|
219
|
+
error: null,
|
|
220
|
+
}));
|
|
221
|
+
try {
|
|
222
|
+
const attachment = await client.attachComputerSession(
|
|
223
|
+
workspaceId,
|
|
224
|
+
computerSessionId,
|
|
225
|
+
{
|
|
226
|
+
targetId,
|
|
227
|
+
expiresInSeconds: 120,
|
|
228
|
+
...(streamRef.current ? { stream: streamRef.current } : {}),
|
|
229
|
+
},
|
|
230
|
+
{ signal: attachmentAbort.signal },
|
|
231
|
+
);
|
|
232
|
+
if (disposed) return;
|
|
233
|
+
if (
|
|
234
|
+
attachment.computerSessionId !== computerSessionId ||
|
|
235
|
+
attachment.targetId !== targetId
|
|
236
|
+
) {
|
|
237
|
+
throw new Error("computer attachment does not match the requested resource");
|
|
238
|
+
}
|
|
239
|
+
controllerGeneration = attachment.controllerGeneration;
|
|
240
|
+
activeStream = attachment.stream;
|
|
241
|
+
relayAccepted = false;
|
|
242
|
+
setResult((current) => ({
|
|
243
|
+
...current,
|
|
244
|
+
state: "connecting",
|
|
245
|
+
attachment: {
|
|
246
|
+
computerSessionId: attachment.computerSessionId,
|
|
247
|
+
controllerGeneration: attachment.controllerGeneration,
|
|
248
|
+
targetId: attachment.targetId,
|
|
249
|
+
expiresAt: attachment.expiresAt,
|
|
250
|
+
},
|
|
251
|
+
error: null,
|
|
252
|
+
}));
|
|
253
|
+
const createSocket =
|
|
254
|
+
factoryRef.current ??
|
|
255
|
+
((url: string, protocols: string[]) => new WebSocket(url, protocols));
|
|
256
|
+
socket = createSocket(
|
|
257
|
+
attachment.stream.kind === "relay"
|
|
258
|
+
? attachment.stream.url
|
|
259
|
+
: computerFrameSocketUrl(attachment, streamRef.current),
|
|
260
|
+
attachment.stream.kind === "direct_websocket" ? [...attachment.stream.protocols] : [],
|
|
261
|
+
);
|
|
262
|
+
socket.binaryType = "arraybuffer";
|
|
263
|
+
socket.addEventListener("open", onOpen);
|
|
264
|
+
socket.addEventListener("message", onMessage);
|
|
265
|
+
socket.addEventListener("error", onError);
|
|
266
|
+
socket.addEventListener("close", onClose);
|
|
267
|
+
const expiresAt = Date.parse(attachment.expiresAt);
|
|
268
|
+
const refreshIn = Number.isFinite(expiresAt)
|
|
269
|
+
? Math.max(1_000, expiresAt - Date.now() - 5_000)
|
|
270
|
+
: 60_000;
|
|
271
|
+
expiryTimer = setTimeout(() => {
|
|
272
|
+
if (disposed) return;
|
|
273
|
+
clearSocket();
|
|
274
|
+
void connect();
|
|
275
|
+
}, refreshIn);
|
|
276
|
+
} catch (cause) {
|
|
277
|
+
if (attachmentAbort.signal.aborted || disposed) return;
|
|
278
|
+
fail(cause);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
void connect();
|
|
283
|
+
return () => {
|
|
284
|
+
disposed = true;
|
|
285
|
+
attachmentAbort.abort();
|
|
286
|
+
if (reconnectTimer) clearTimeout(reconnectTimer);
|
|
287
|
+
if (expiryTimer) clearTimeout(expiryTimer);
|
|
288
|
+
clearSocket();
|
|
289
|
+
};
|
|
290
|
+
}, [client, enabled, nonce, options.computerSessionId, options.targetId, workspaceId]);
|
|
291
|
+
|
|
292
|
+
return { ...result, reconnect };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function relayDatagram(tag: number, body: Uint8Array): ArrayBuffer {
|
|
296
|
+
const message = new Uint8Array(body.length + 1);
|
|
297
|
+
message[0] = tag;
|
|
298
|
+
message.set(body, 1);
|
|
299
|
+
return message.buffer as ArrayBuffer;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async function messageBytes(value: unknown): Promise<Uint8Array> {
|
|
303
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
304
|
+
if (ArrayBuffer.isView(value)) {
|
|
305
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength).slice();
|
|
306
|
+
}
|
|
307
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) {
|
|
308
|
+
return new Uint8Array(await value.arrayBuffer());
|
|
309
|
+
}
|
|
310
|
+
throw new Error("computer frame stream returned a non-binary message");
|
|
311
|
+
}
|